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