Spicy
field.h
1 
2 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
3 
4 #pragma once
5 
6 #include <string>
7 #include <utility>
8 
9 #include <hilti/ast/attribute.h>
10 #include <hilti/ast/declaration.h>
11 #include <hilti/ast/expression.h>
12 #include <hilti/ast/function.h>
13 #include <hilti/ast/id.h>
14 #include <hilti/ast/types/auto.h>
15 #include <hilti/ast/types/unknown.h>
16 
17 namespace hilti {
18 namespace declaration {
19 
21 class Field : public DeclarationBase {
22 public:
23  Field() : DeclarationBase({ID("<no id>"), type::unknown, node::none, node::none}, Meta()) {}
24  Field(ID id, hilti::Type t, std::optional<AttributeSet> attrs = {}, Meta m = Meta())
25  : DeclarationBase(nodes(std::move(id), std::move(t), std::move(attrs), node::none), std::move(m)) {}
26  Field(ID id, ::hilti::function::CallingConvention cc, type::Function ft, std::optional<AttributeSet> attrs = {},
27  Meta m = Meta())
28  : DeclarationBase(nodes(std::move(id), std::move(ft), std::move(attrs), node::none), std::move(m)), _cc(cc) {}
29  Field(ID id, hilti::Function inline_func, std::optional<AttributeSet> attrs = {}, Meta m = Meta())
30  : DeclarationBase(nodes(std::move(id), node::none, std::move(attrs), std::move(inline_func)), std::move(m)),
31  _cc(inline_func.callingConvention()) {}
32 
33  const auto& id() const { return child<ID>(0); }
34 
35  auto callingConvention() const { return _cc; }
36  auto inlineFunction() const { return children()[3].tryAs<hilti::Function>(); }
37  auto attributes() const { return children()[2].tryAs<AttributeSet>(); }
38  bool isResolved(type::ResolvedState* rstate) const {
39  if ( children()[1].isA<type::Function>() )
40  return true;
41 
42  if ( auto func = inlineFunction() )
43  return type::detail::isResolved(func->type(), rstate);
44  else
45  return type::detail::isResolved(child<hilti::Type>(1), rstate);
46  }
47 
48  const hilti::Type& type() const {
49  if ( const auto& func = inlineFunction() )
50  return func->type();
51  else
52  return child<hilti::Type>(1);
53  }
54 
55  NodeRef typeRef() {
56  if ( inlineFunction() )
57  return children()[3].as<hilti::Function>().typeRef();
58  else
59  return NodeRef(children()[1]);
60  }
61 
63  if ( auto a = AttributeSet::find(attributes(), "&default") ) {
64  if ( auto x = a->valueAsExpression() )
65  return x->get();
66  else
67  return {};
68  }
69 
70  return {};
71  }
72 
73  auto isInternal() const { return AttributeSet::find(attributes(), "&internal").has_value(); }
74  auto isOptional() const { return AttributeSet::find(attributes(), "&optional").has_value(); }
75  auto isStatic() const { return AttributeSet::find(attributes(), "&static").has_value(); }
76  auto isNoEmit() const { return AttributeSet::find(attributes(), "&no-emit").has_value(); }
77 
79  auto& _typeNode() {
80  if ( auto func = inlineFunction() )
81  return const_cast<::hilti::Function&>(*func)._typeNode();
82  else
83  return children()[1];
84  }
85 
86  void setAttributes(AttributeSet attrs) { children()[2] = std::move(attrs); }
87 
88  bool operator==(const Field& other) const {
89  return id() == other.id() && type() == other.type() && attributes() == other.attributes() && _cc == other._cc;
90  }
91 
93  declaration::Linkage linkage() const { return declaration::Linkage::Struct; }
94 
96  bool isConstant() const { return false; }
97 
99  std::string displayName() const { return "struct field"; }
100 
102  auto isEqual(const Declaration& other) const { return node::isEqual(this, other); }
103 
105  auto properties() const { return node::Properties{{"cc", to_string(_cc)}}; }
106 
107 private:
108  ::hilti::function::CallingConvention _cc = ::hilti::function::CallingConvention::Standard;
109 }; // namespace struct_
110 
111 } // namespace declaration
112 } // namespace hilti
Definition: function.h:71
declaration::Linkage linkage() const
Definition: field.h:93
Definition: declaration.h:53
auto isEqual(const Declaration &other) const
Definition: field.h:102
auto & _typeNode()
Definition: field.h:79
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:470
Definition: function.h:44
bool isConstant() const
Definition: field.h:96
std::string displayName() const
Definition: field.h:99
Definition: meta.h:18
Definition: attribute.h:145
Definition: type.h:159
Definition: declaration.h:87
Definition: optional-ref.h:22
Node & _typeNode()
Definition: function.h:72
hilti::optional_ref< const Attribute > find(std::string_view tag) const
Definition: attribute.h:171
auto properties() const
Definition: field.h:105
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
Definition: node-ref.h:44
Definition: field.h:21
Definition: id.h:18