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/auto.h>
14 
15 namespace hilti::declaration {
16 
19 public:
20  GlobalVariable(ID id, ::hilti::Type type, std::optional<hilti::Expression> init = {},
21  Linkage linkage = Linkage::Private, Meta m = Meta())
22  : DeclarationBase(nodes(std::move(id), std::move(type), std::move(init)), std::move(m)), _linkage(linkage) {}
23 
24  GlobalVariable(ID id, ::hilti::Type type, Linkage linkage = Linkage::Private, Meta m = Meta())
25  : DeclarationBase(nodes(std::move(id), std::move(type), node::none), std::move(m)), _linkage(linkage) {}
26 
27  GlobalVariable(ID id, hilti::Expression init, Linkage linkage = Linkage::Private, Meta m = Meta())
28  : DeclarationBase(nodes(std::move(id), node::none, std::move(init)), std::move(m)), _linkage(linkage) {}
29 
30  GlobalVariable(ID id, ::hilti::Type type, std::vector<hilti::Expression> args,
31  std::optional<hilti::Expression> init = {}, Linkage linkage = Linkage::Private, Meta m = Meta())
32  : DeclarationBase(nodes(std::move(id), std::move(type), std::move(init), std::move(args)), std::move(m)),
33  _linkage(linkage) {}
34 
35  auto init() const { return children()[2].tryAs<hilti::Expression>(); }
36 
37  const auto& type() const {
38  if ( auto t = children()[1].tryAs<hilti::Type>() )
39  return *t;
40  else {
41  assert(init());
42  return init()->type();
43  }
44  }
45 
46  auto typeArguments() const { return children<hilti::Expression>(3, -1); }
47 
48  void setInit(const hilti::Expression& i) { children()[2] = i; }
49  void setTypeArguments(std::vector<hilti::Expression> args) {
50  auto& c = children();
51  c.erase(c.begin() + 3, c.end());
52  for ( auto&& a : args )
53  c.emplace_back(std::move(a));
54  }
55 
56  bool operator==(const GlobalVariable& other) const {
57  return id() == other.id() && type() == other.type() && init() == other.init();
58  }
59 
61  bool isConstant() const { return false; }
63  const ID& id() const { return child<ID>(0); }
65  Linkage linkage() const { return _linkage; }
67  std::string displayName() const { return "global variable"; };
69  auto isEqual(const Declaration& other) const { return node::isEqual(this, other); }
70 
72  auto properties() const { return node::Properties{{"linkage", to_string(_linkage)}}; }
73 
74 private:
75  Linkage _linkage;
76 };
77 
78 } // namespace hilti::declaration
Definition: declaration.h:54
const Node none
Definition: node.cc:14
auto properties() const
Definition: global-variable.h:72
const auto & children() const
Definition: node.h:472
std::string displayName() const
Definition: global-variable.h:67
bool isConstant() const
Definition: global-variable.h:61
auto isEqual(const Declaration &other) const
Definition: global-variable.h:69
Definition: meta.h:19
Definition: type.h:160
Definition: declaration.h:88
const ID & id() const
Definition: global-variable.h:63
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:98
Definition: declaration.h:19
Linkage linkage() const
Definition: global-variable.h:65
Definition: id.h:18
Definition: global-variable.h:18