Spicy
constant.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/declaration.h>
9 #include <hilti/ast/expression.h>
10 #include <hilti/ast/id.h>
11 #include <hilti/ast/types/unknown.h>
12 
13 namespace hilti {
14 namespace declaration {
15 
18 public:
19  Constant(ID id, ::hilti::Type type, hilti::Expression value = {}, Linkage linkage = Linkage::Private,
20  Meta m = Meta())
21  : NodeBase(nodes(std::move(id), std::move(type), std::move(value)), std::move(m)), _linkage(linkage) {}
22 
23  Constant(ID id, hilti::Expression value, Linkage linkage = Linkage::Private, Meta m = Meta())
24  : NodeBase({std::move(id), node::none, std::move(value)}, std::move(m)), _linkage(linkage) {}
25 
26  const auto& value() const { return child<hilti::Expression>(2); }
27 
28  ::hilti::Type type() const {
29  if ( auto t = childs()[1].tryAs<::hilti::Type>(); t && *t != type::unknown )
30  return type::effectiveType(std::move(*t));
31 
32  return value().type();
33  }
34 
38  auto hasAutomaticType() const { return ! childs()[1].isA<::hilti::Type>(); }
39 
40  bool operator==(const Constant& other) const { return id() == other.id() && value() == other.value(); }
41 
43  bool isConstant() const { return true; }
45  const ID& id() const { return child<ID>(0); }
47  Linkage linkage() const { return _linkage; }
49  std::string displayName() const { return "constant"; };
51  auto isEqual(const Declaration& other) const { return node::isEqual(this, other); }
52 
54  auto properties() const { return node::Properties{{"linkage", to_string(_linkage)}}; }
55 
63  static Declaration setValue(const Constant& d, const hilti::Expression& i) {
64  auto x = Declaration(d)._clone().as<Constant>();
65  x.childs()[2] = i;
66  return std::move(x);
67  }
68 
69 private:
70  Linkage _linkage;
71 };
72 
73 } // namespace declaration
74 } // namespace hilti
auto & childs() const
Definition: node.h:445
const ID & id() const
Definition: constant.h:45
std::vector< T > childs(int begin, int end) const
Definition: node.h:373
auto hasAutomaticType() const
Definition: constant.h:38
const Node none
Definition: node.cc:12
static Declaration setValue(const Constant &d, const hilti::Expression &i)
Definition: constant.h:63
Definition: meta.h:18
std::string displayName() const
Definition: constant.h:49
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
auto properties() const
Definition: constant.h:54
Linkage linkage() const
Definition: constant.h:47
auto isEqual(const Declaration &other) const
Definition: constant.h:51
Definition: constant.h:17
bool isConstant() const
Definition: constant.h:43
Definition: declaration.h:15
Definition: id.h:18
Definition: node.h:318