Spicy
global-variable.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 #include <vector>
8 
9 #include <hilti/ast/declaration.h>
10 #include <hilti/ast/expression.h>
11 #include <hilti/ast/id.h>
12 #include <hilti/ast/type.h>
13 #include <hilti/ast/types/unknown.h>
14 
15 namespace hilti {
16 namespace declaration {
17 
20 public:
21  GlobalVariable(ID id, ::hilti::Type type, Linkage linkage = Linkage::Private, Meta m = Meta())
22  : NodeBase(nodes(std::move(id), std::move(type), node::none), std::move(m)), _linkage(linkage) {}
23 
24  GlobalVariable(ID id, ::hilti::Type type, std::optional<hilti::Expression> init = {},
25  Linkage linkage = Linkage::Private, Meta m = Meta())
26  : NodeBase(nodes(std::move(id), std::move(type), std::move(init)), std::move(m)), _linkage(linkage) {}
27 
28  GlobalVariable(ID id, ::hilti::Type type, std::vector<hilti::Expression> args,
29  std::optional<hilti::Expression> init = {}, Linkage linkage = Linkage::Private, Meta m = Meta())
30  : NodeBase(nodes(std::move(id), std::move(type), std::move(init), std::move(args)), std::move(m)),
31  _linkage(linkage) {}
32 
33  GlobalVariable(ID id, hilti::Expression init, Linkage linkage = Linkage::Private, Meta m = Meta())
34  : NodeBase(nodes(std::move(id), node::none, std::move(init)), std::move(m)), _linkage(linkage) {}
35 
36  auto init() const { return childs()[2].tryReferenceAs<hilti::Expression>(); }
37  auto typeArguments() const { return childs<hilti::Expression>(3, -1); }
38 
39  ::hilti::Type type() const {
40  if ( auto t = childs()[1].tryAs<::hilti::Type>(); t && *t != type::unknown )
41  return type::effectiveType(std::move(*t));
42 
43  if ( auto i = init() )
44  return i->type();
45 
46  return type::unknown;
47  }
48 
53  auto hasAutomaticType() const { return ! childs()[1].isA<::hilti::Type>(); }
54 
55  bool operator==(const GlobalVariable& other) const {
56  return id() == other.id() && type() == other.type() && init() == other.init();
57  }
58 
60  bool isConstant() const { return false; }
62  const ID& id() const { return child<ID>(0); }
64  Linkage linkage() const { return _linkage; }
66  std::string displayName() const { return "global variable"; };
68  auto isEqual(const Declaration& other) const { return node::isEqual(this, other); }
69 
71  auto properties() const { return node::Properties{{"linkage", to_string(_linkage)}}; }
72 
80  static Declaration setType(const GlobalVariable& d, std::optional<hilti::Type> t) {
81  auto x = Declaration(d)._clone().as<GlobalVariable>();
82  if ( t )
83  x.childs()[1] = *t;
84  else
85  x.childs()[1] = node::none;
86 
87  return std::move(x);
88  }
89 
97  static Declaration setInit(const GlobalVariable& d, const hilti::Expression& i) {
98  auto x = Declaration(d)._clone().as<GlobalVariable>();
99  x.childs()[2] = i;
100  return std::move(x);
101  }
102 
110  static Declaration setTypeArguments(const GlobalVariable& d, std::vector<hilti::Expression> args) {
111  auto x = Declaration(d)._clone().as<GlobalVariable>();
112  x.childs() = x.childs<Node>(0, 3);
113  for ( auto&& a : args )
114  x.childs().emplace_back(std::move(a));
115 
116  return std::move(x);
117  }
118 
119 private:
120  Linkage _linkage;
121 };
122 
123 } // namespace declaration
124 } // namespace hilti
auto & childs() const
Definition: node.h:445
std::vector< T > childs(int begin, int end) const
Definition: node.h:373
const Node none
Definition: node.cc:12
static Declaration setInit(const GlobalVariable &d, const hilti::Expression &i)
Definition: global-variable.h:97
auto properties() const
Definition: global-variable.h:71
std::string displayName() const
Definition: global-variable.h:66
bool isConstant() const
Definition: global-variable.h:60
auto isEqual(const Declaration &other) const
Definition: global-variable.h:68
Definition: meta.h:18
const ID & id() const
Definition: global-variable.h:62
static Declaration setType(const GlobalVariable &d, std::optional< hilti::Type > t)
Definition: global-variable.h:80
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
auto hasAutomaticType() const
Definition: global-variable.h:53
Definition: node.h:97
Definition: declaration.h:15
Linkage linkage() const
Definition: global-variable.h:64
static Declaration setTypeArguments(const GlobalVariable &d, std::vector< hilti::Expression > args)
Definition: global-variable.h:110
Definition: id.h:18
Definition: global-variable.h:19
Definition: node.h:318