12 #include <hilti/ast/ctors/integer.h> 13 #include <hilti/ast/ctors/string.h> 14 #include <hilti/ast/expression.h> 15 #include <hilti/ast/expressions/ctor.h> 16 #include <hilti/ast/node.h> 17 #include <hilti/base/logger.h> 18 #include <hilti/base/result.h> 19 #include <hilti/base/util.h> 20 #include <hilti/compiler/coercion.h> 21 #include <hilti/global.h> 47 :
NodeBase({std::move(v)}, std::move(m)), _tag(std::move(tag)) {}
50 const auto&
tag()
const {
return _tag; }
75 if constexpr ( std::is_same<T, Expression>::value ) {
79 if (
auto e =
value().tryAs<Expression>() )
85 if constexpr ( std::is_same<T, std::string>::value ) {
89 if (
auto e =
value().tryAs<expression::Ctor>() )
95 if constexpr ( std::is_same<T, int64_t>::value ) {
99 if (
auto e =
value().tryAs<expression::Ctor>() ) {
104 return static_cast<int64_t
>(s->value());
110 logger().internalError(
hilti::util::fmt(
"unsupported attribute value type requested (%s)",
typeid(T).name()));
116 bool operator==(
const Attribute& other)
const {
117 if ( _tag != other._tag )
120 if (
auto x = valueAs<Expression>() ) {
121 auto y = other.
valueAs<Expression>();
122 return y && *x == *y;
125 if (
auto x = valueAs<std::string>() ) {
126 auto y = other.
valueAs<std::string>();
127 return y && *x == *y;
142 x.childs().emplace_back(std::move(n));
144 x.childs()[0] = std::move(n);
178 if ( _cache.attributes.empty() )
179 _cache.attributes = childs<Attribute>(0, -1);
181 return _cache.attributes;
190 std::optional<Attribute>
find(std::string_view tag)
const {
191 for (
auto& a : attributes() )
192 if ( a.tag() ==
tag )
203 std::vector<Attribute>
findAll(std::string_view tag)
const {
204 std::vector<Attribute> result;
206 for (
auto& a : attributes() )
207 if ( a.tag() ==
tag )
223 for (
auto& n :
childs() ) {
225 if ( a.tag() !=
tag )
228 if (
auto e = a.valueAs<Expression>() ) {
229 auto ne = coerceExpression(*e, dst);
234 n =
Attribute(tag, std::move(*ne.nexpr));
250 bool has(std::string_view tag)
const {
return find(tag).has_value(); }
255 bool operator==(
const AttributeSet& other)
const {
return attributes() == other.attributes(); };
258 operator bool()
const {
return !
childs().empty(); }
279 static std::optional<AttributeSet>
add(std::optional<AttributeSet> s,
Attribute a) {
295 static std::optional<Attribute>
find(
const std::optional<AttributeSet>& attrs, std::string_view tag) {
297 return attrs->find(tag);
309 static std::vector<Attribute>
findAll(
const std::optional<AttributeSet>& attrs, std::string_view tag) {
311 return attrs->findAll(tag);
323 static bool has(
const std::optional<AttributeSet>& attrs, std::string_view tag) {
325 return attrs->has(tag);
330 void clearCache() { _cache.attributes.clear(); }
333 mutable struct { std::vector<Attribute> attributes; } _cache;
340 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:46
bool hasValue() const
Definition: attribute.h:53
static Attribute setValue(const Attribute &a, Node n)
Definition: attribute.h:139
auto & childs() const
Definition: node.h:445
std::vector< T > childs(int begin, int end) const
Definition: node.h:373
static std::optional< Attribute > find(const std::optional< AttributeSet > &attrs, std::string_view tag)
Definition: attribute.h:295
const Node none
Definition: node.cc:12
std::vector< Attribute > findAll(std::string_view tag) const
Definition: attribute.h:203
Definition: optional.h:79
void addChild(Node n)
Definition: node.h:434
AttributeSet(std::vector< Attribute > a, Meta m=Meta())
Definition: attribute.h:167
const auto & attributes() const
Definition: attribute.h:177
const Node & value() const
Definition: attribute.h:60
Definition: attribute.h:159
static std::vector< Attribute > findAll(const std::optional< AttributeSet > &attrs, std::string_view tag)
Definition: attribute.h:309
Result< T > valueAs() const
Definition: attribute.h:74
Attribute(std::string tag, Meta m=Meta())
Definition: attribute.h:36
std::string fmt(const char *fmt, const Args &... args)
Definition: util.h:80
Definition: attribute.h:26
auto properties() const
Definition: attribute.h:114
const auto & tag() const
Definition: attribute.h:50
static bool has(const std::optional< AttributeSet > &attrs, std::string_view tag)
Definition: attribute.h:323
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
AttributeSet(Meta m=Meta())
Definition: attribute.h:174
Result< bool > coerceValueTo(const std::string &tag, const Type &dst)
Definition: attribute.h:222
static std::optional< AttributeSet > add(std::optional< AttributeSet > s, Attribute a)
Definition: attribute.h:279
auto properties() const
Definition: attribute.h:253
std::optional< Attribute > find(std::string_view tag) const
Definition: attribute.h:190
static AttributeSet add(AttributeSet s, Attribute a)
Definition: attribute.h:267
auto & meta() const
Definition: node.h:449
bool has(std::string_view tag) const
Definition: attribute.h:250
void clearCache()
Definition: node.h:457