13 #include <hilti/ast/ctors/integer.h> 14 #include <hilti/ast/ctors/string.h> 15 #include <hilti/ast/expression.h> 16 #include <hilti/ast/expressions/ctor.h> 17 #include <hilti/ast/node.h> 18 #include <hilti/base/logger.h> 19 #include <hilti/base/result.h> 20 #include <hilti/base/util.h> 21 #include <hilti/compiler/coercion.h> 22 #include <hilti/global.h> 48 :
NodeBase({std::move(v)}, std::move(m)), _tag(std::move(tag)) {}
51 const auto&
tag()
const {
return _tag; }
76 return result::Error(hilti::util::fmt(
"attribute '%s' requires an expression", _tag));
78 if ( !
value().isA<Expression>() )
79 return result::Error(hilti::util::fmt(
"value for attribute '%s' must be an expression", _tag));
81 return {
value().as<Expression>()};
86 return result::Error(hilti::util::fmt(
"attribute '%s' requires a string", _tag));
88 if (
auto e =
value().tryAs<expression::Ctor>() )
92 return result::Error(hilti::util::fmt(
"value for attribute '%s' must be a string", _tag));
97 return result::Error(hilti::util::fmt(
"attribute '%s' requires an integer", _tag));
99 if (
auto e =
value().tryAs<expression::Ctor>() ) {
104 return static_cast<int64_t
>(s->value());
107 return result::Error(hilti::util::fmt(
"value for attribute '%s' must be an integer", _tag));
123 if ( ! type::isResolved(dst) )
126 auto ne = coerceExpression(*x, dst);
128 return result::Error(util::fmt(
"cannot coerce attribute's expression from type '%s' to '%s' (%s)",
129 x->get().type(), dst,
tag()));
142 bool operator==(
const Attribute& other)
const {
143 if ( _tag != other._tag )
148 return y && *x == *y;
151 else if (
auto x = valueAsString() ) {
152 auto y = other.valueAsString();
153 return y && *x == *y;
156 else if (
auto x = valueAsInteger() ) {
157 auto y = other.valueAsInteger();
158 return y && *x == *y;
192 auto attributes()
const {
return children<Attribute>(0, -1); }
201 for (
const auto& a : attributes() )
202 if ( a.tag() ==
tag )
216 for (
const auto& a : attributes() )
217 if ( a.tag() ==
tag )
233 if ( ! type::isResolved(dst) )
238 if ( a.tag() !=
tag )
241 if (
auto e = a.valueAsExpression() ) {
242 auto ne = coerceExpression(*e, dst);
247 n =
Attribute(tag, std::move(*ne.nexpr));
263 bool has(std::string_view tag)
const {
return find(tag).has_value(); }
271 operator bool()
const {
return !
children().empty(); }
309 std::string_view tag) {
311 return attrs->find(tag);
325 return attrs->findAll(tag);
338 std::string_view tag) {
340 return attrs->findAll(tag);
352 static bool has(
const std::optional<AttributeSet>& attrs, std::string_view tag) {
354 return attrs->has(tag);
364 inline Node to_node(std::optional<AttributeSet>&& i) {
return i ? to_node(*i) :
node::none; }
Attribute(std::string tag, Node v, Meta m=Meta())
Definition: attribute.h:47
bool hasValue() const
Definition: attribute.h:54
static hilti::node::Set< Attribute > findAll(const hilti::optional_ref< const AttributeSet > &attrs, std::string_view tag)
Definition: attribute.h:337
Result< bool > coerceValueTo(const Type &dst)
Definition: attribute.h:118
const Node none
Definition: node.cc:14
Definition: optional.h:79
static hilti::node::Set< Attribute > findAll(const std::optional< const AttributeSet > &attrs, std::string_view tag)
Definition: attribute.h:323
const auto & children() const
Definition: node.h:471
void addChild(Node n)
Definition: node.h:460
AttributeSet(std::vector< Attribute > a, Meta m=Meta())
Definition: attribute.h:182
const Node & value() const
Definition: attribute.h:61
static hilti::optional_ref< const Attribute > find(const hilti::optional_ref< const AttributeSet > &attrs, std::string_view tag)
Definition: attribute.h:308
Definition: attribute.h:174
Definition: optional-ref.h:22
auto findAll(std::string_view tag) const
Definition: attribute.h:213
Attribute(std::string tag, Meta m=Meta())
Definition: attribute.h:37
hilti::optional_ref< const Attribute > find(std::string_view tag) const
Definition: attribute.h:200
Definition: attribute.h:27
auto properties() const
Definition: attribute.h:140
const auto & tag() const
Definition: attribute.h:51
static bool has(const std::optional< AttributeSet > &attrs, std::string_view tag)
Definition: attribute.h:352
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:97
AttributeSet(Meta m=Meta())
Definition: attribute.h:189
Result< bool > coerceValueTo(const std::string &tag, const Type &dst)
Definition: attribute.h:232
auto properties() const
Definition: attribute.h:266
Result< std::reference_wrapper< const Expression > > valueAsExpression() const
Definition: attribute.h:74
static AttributeSet add(std::optional< AttributeSet > s, Attribute a)
Definition: attribute.h:292
static AttributeSet add(AttributeSet s, Attribute a)
Definition: attribute.h:280
auto & meta() const
Definition: node.h:475
bool has(std::string_view tag) const
Definition: attribute.h:263
auto attributes() const
Definition: attribute.h:192