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/auto.h>
12 
13 namespace hilti::declaration {
14 
16 class Constant : public DeclarationBase {
17 public:
18  Constant(ID id, ::hilti::Type type, hilti::Expression value = {}, Linkage linkage = Linkage::Private,
19  Meta m = Meta())
20  : DeclarationBase(nodes(std::move(id), std::move(type), std::move(value)), std::move(m)), _linkage(linkage) {}
21 
22  Constant(ID id, hilti::Expression value, Linkage linkage = Linkage::Private, Meta m = Meta())
23  : DeclarationBase(nodes(std::move(id), node::none, std::move(value)), std::move(m)), _linkage(linkage) {}
24 
25  const auto& value() const { return child<hilti::Expression>(2); }
26 
27  const auto& type() const {
28  if ( auto t = children()[1].tryAs<hilti::Type>() )
29  return *t;
30  else
31  return value().type();
32  }
33 
34  void setValue(const hilti::Expression& i) { children()[2] = i; }
35 
36  bool operator==(const Constant& other) const { return id() == other.id() && value() == other.value(); }
37 
39  bool isConstant() const { return true; }
41  const ID& id() const { return child<ID>(0); }
43  Linkage linkage() const { return _linkage; }
45  std::string displayName() const { return "constant"; };
47  auto isEqual(const Declaration& other) const { return node::isEqual(this, other); }
48 
50  auto properties() const { return node::Properties{{"linkage", to_string(_linkage)}}; }
51 
52 private:
53  Linkage _linkage;
54 };
55 
56 } // namespace hilti::declaration
const ID & id() const
Definition: constant.h:41
Definition: declaration.h:53
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:471
Definition: meta.h:19
Definition: type.h:160
Definition: declaration.h:87
std::string displayName() const
Definition: constant.h:45
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:97
auto properties() const
Definition: constant.h:50
Linkage linkage() const
Definition: constant.h:43
auto isEqual(const Declaration &other) const
Definition: constant.h:47
Definition: constant.h:16
Definition: declaration.h:18
bool isConstant() const
Definition: constant.h:39
Definition: id.h:18