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 {
14 namespace declaration {
15 
17 class Constant : public DeclarationBase {
18 public:
19  Constant(ID id, ::hilti::Type type, hilti::Expression value = {}, Linkage linkage = Linkage::Private,
20  Meta m = Meta())
21  : DeclarationBase(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  : DeclarationBase(nodes(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  const auto& type() const {
29  if ( auto t = childs()[1].tryAs<hilti::Type>() )
30  return *t;
31  else
32  return value().type();
33  }
34 
35  void setValue(hilti::Expression i) { childs()[2] = std::move(i); }
36 
37  bool operator==(const Constant& other) const { return id() == other.id() && value() == other.value(); }
38 
40  bool isConstant() const { return true; }
42  const ID& id() const { return child<ID>(0); }
44  Linkage linkage() const { return _linkage; }
46  std::string displayName() const { return "constant"; };
48  auto isEqual(const Declaration& other) const { return node::isEqual(this, other); }
49 
51  auto properties() const { return node::Properties{{"linkage", to_string(_linkage)}}; }
52 
53 private:
54  Linkage _linkage;
55 };
56 
57 } // namespace declaration
58 } // namespace hilti
const ID & id() const
Definition: constant.h:42
Definition: declaration.h:53
const Node none
Definition: node.cc:14
Definition: meta.h:18
Definition: type.h:159
Definition: declaration.h:87
std::string displayName() const
Definition: constant.h:46
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
auto properties() const
Definition: constant.h:51
Linkage linkage() const
Definition: constant.h:44
auto isEqual(const Declaration &other) const
Definition: constant.h:48
Definition: constant.h:17
bool isConstant() const
Definition: constant.h:40
const auto & childs() const
Definition: node.h:470
Definition: id.h:18