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::declaration {
18 
20 class Field : public DeclarationBase {
21 public:
22  Field() : DeclarationBase({ID("<no id>"), type::unknown, node::none, node::none}, Meta()) {}
23  Field(ID id, hilti::Type t, std::optional<AttributeSet> attrs = {}, Meta m = Meta())
24  : DeclarationBase(nodes(std::move(id), std::move(t), std::move(attrs), node::none), std::move(m)) {}
25  Field(ID id, ::hilti::function::CallingConvention cc, type::Function ft, std::optional<AttributeSet> attrs = {},
26  Meta m = Meta())
27  : DeclarationBase(nodes(std::move(id), std::move(ft), std::move(attrs), node::none), std::move(m)), _cc(cc) {}
28  Field(ID id, const hilti::Function& inline_func, std::optional<AttributeSet> attrs = {}, Meta m = Meta())
29  : DeclarationBase(nodes(std::move(id), node::none, std::move(attrs), inline_func), std::move(m)),
30  _cc(inline_func.callingConvention()) {}
31 
32  const auto& id() const { return child<ID>(0); }
33 
34  auto callingConvention() const { return _cc; }
35  auto inlineFunction() const { return children()[3].tryAs<hilti::Function>(); }
36  auto attributes() const { return children()[2].tryAs<AttributeSet>(); }
37  bool isResolved(type::ResolvedState* rstate) const {
38  if ( children()[1].isA<type::Function>() )
39  return true;
40 
41  if ( auto func = inlineFunction() )
42  return type::detail::isResolved(func->type(), rstate);
43  else
44  return type::detail::isResolved(child<hilti::Type>(1), rstate);
45  }
46 
47  const hilti::Type& type() const {
48  if ( const auto& func = inlineFunction() )
49  return func->type();
50  else
51  return child<hilti::Type>(1);
52  }
53 
54  NodeRef typeRef() {
55  if ( inlineFunction() )
56  return children()[3].as<hilti::Function>().typeRef();
57  else
58  return NodeRef(children()[1]);
59  }
60 
62  if ( auto a = AttributeSet::find(attributes(), "&default") ) {
63  if ( auto x = a->valueAsExpression() )
64  return x->get();
65  else
66  return {};
67  }
68 
69  return {};
70  }
71 
72  auto isInternal() const { return AttributeSet::find(attributes(), "&internal").has_value(); }
73  auto isOptional() const { return AttributeSet::find(attributes(), "&optional").has_value(); }
74  auto isStatic() const { return AttributeSet::find(attributes(), "&static").has_value(); }
75  auto isNoEmit() const { return AttributeSet::find(attributes(), "&no-emit").has_value(); }
76 
78  auto& _typeNode() {
79  if ( auto func = inlineFunction() )
80  return const_cast<::hilti::Function&>(*func)._typeNode();
81  else
82  return children()[1];
83  }
84 
85  void setAttributes(const AttributeSet& attrs) { children()[2] = attrs; }
86 
87  bool operator==(const Field& other) const {
88  return id() == other.id() && type() == other.type() && attributes() == other.attributes() && _cc == other._cc;
89  }
90 
92  declaration::Linkage linkage() const { return declaration::Linkage::Struct; }
93 
95  bool isConstant() const { return false; }
96 
98  std::string displayName() const { return "struct field"; }
99 
101  auto isEqual(const Declaration& other) const { return node::isEqual(this, other); }
102 
104  auto properties() const { return node::Properties{{"cc", to_string(_cc)}}; }
105 
106 private:
107  ::hilti::function::CallingConvention _cc = ::hilti::function::CallingConvention::Standard;
108 }; // namespace struct_
109 
110 } // namespace hilti::declaration
Definition: function.h:71
declaration::Linkage linkage() const
Definition: field.h:92
Definition: declaration.h:54
auto isEqual(const Declaration &other) const
Definition: field.h:101
auto & _typeNode()
Definition: field.h:78
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:472
Definition: function.h:44
bool isConstant() const
Definition: field.h:95
std::string displayName() const
Definition: field.h:98
Definition: meta.h:19
Definition: attribute.h:174
Definition: type.h:160
Definition: declaration.h:88
Definition: optional-ref.h:22
Node & _typeNode()
Definition: function.h:73
hilti::optional_ref< const Attribute > find(std::string_view tag) const
Definition: attribute.h:200
auto properties() const
Definition: field.h:104
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:98
Definition: node-ref.h:45
Definition: declaration.h:19
Definition: field.h:20
Definition: id.h:18