Spicy
type.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <utility>
7 
8 #include <hilti/ast/attribute.h>
9 #include <hilti/ast/id.h>
10 #include <hilti/ast/type.h>
11 
12 namespace hilti {
13 namespace declaration {
14 
16 class Type : public NodeBase, public hilti::trait::isDeclaration {
17 public:
18  Type(ID id, ::hilti::Type type, Linkage linkage = Linkage::Private, Meta m = Meta())
19  : NodeBase({std::move(id), std::move(type), node::none}, std::move(m)), _linkage(linkage) {}
20 
21  Type(ID id, ::hilti::Type type, std::optional<AttributeSet> attrs, Linkage linkage = Linkage::Private,
22  Meta m = Meta())
23  : NodeBase(nodes(std::move(id), std::move(type), std::move(attrs)), std::move(m)), _linkage(linkage) {}
24 
25  auto type() const { return type::effectiveType(child<hilti::Type>(1)); }
26  auto attributes() const { return childs()[2].tryReferenceAs<AttributeSet>(); }
27 
28  bool isOnHeap() const {
29  if ( type::isOnHeap(type()) )
30  return true;
31 
32  auto x = attributes();
33  return x && x->find("&on-heap");
34  }
35 
37  auto typeID() const { return childs()[1].as<hilti::Type>().typeID(); }
38 
40  auto cxxID() const { return childs()[1].as<hilti::Type>().cxxID(); }
41 
42  bool operator==(const Type& other) const { return id() == other.id() && type() == other.type(); }
43 
45  auto& _typeNode() { return childs()[1]; }
46 
48  bool isConstant() const { return true; }
50  const ID& id() const { return child<ID>(0); }
52  Linkage linkage() const { return _linkage; }
54  std::string displayName() const { return "type"; };
56  auto isEqual(const Declaration& other) const { return node::isEqual(this, other); }
57 
59  auto properties() const { return node::Properties{{"linkage", to_string(_linkage)}}; }
60 
68  static Type setType(const Type& d, const ::hilti::Type& t) {
69  auto x = Declaration(d)._clone().as<Type>();
70  x.childs()[1] = t;
71  return x;
72  }
73 
74 private:
75  Linkage _linkage;
76 };
77 
78 } // namespace declaration
79 } // namespace hilti
auto & childs() const
Definition: node.h:445
std::vector< T > childs(int begin, int end) const
Definition: node.h:373
Definition: type.h:16
bool isConstant() const
Definition: type.h:48
const Node none
Definition: node.cc:12
const ID & id() const
Definition: type.h:50
auto typeID() const
Definition: type.h:37
auto isEqual(const Declaration &other) const
Definition: type.h:56
auto & _typeNode()
Definition: type.h:45
auto properties() const
Definition: type.h:59
static Type setType(const Type &d, const ::hilti::Type &t)
Definition: type.h:68
Definition: meta.h:18
Definition: attribute.h:159
auto cxxID() const
Definition: type.h:40
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
Definition: declaration.h:15
Linkage linkage() const
Definition: type.h:52
std::string displayName() const
Definition: type.h:54
Definition: id.h:18
Definition: node.h:318