Spicy
variable.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 
7 #include <spicy/ast/aliases.h>
8 #include <spicy/ast/engine.h>
9 #include <spicy/ast/types/unit-item.h>
10 #include <spicy/ast/types/unit.h>
11 
12 namespace spicy::type::unit::item {
13 
22 public:
23  Variable(ID id, Type type, const std::optional<Expression>& default_, std::optional<AttributeSet> attrs = {},
24  Meta m = Meta())
25  : NodeBase(nodes(std::move(id), std::move(type), default_, std::move(attrs)), std::move(m)) {}
26 
27  const auto& id() const { return child<ID>(0); }
28  auto default_() const { return children()[2].tryAs<Expression>(); }
29  auto attributes() const { return children()[3].tryAs<AttributeSet>(); }
30 
31  bool isOptional() const { return AttributeSet::find(attributes(), "&optional").has_value(); }
32 
33  bool operator==(const Variable& other) const {
34  return id() == other.id() && itemType() == other.itemType() && default_() == other.default_() &&
35  attributes() == other.attributes();
36  }
37 
38  // Unit item interface
39  const Type& itemType() const { return child<Type>(1); }
40  bool isResolved() const { return type::isResolved(itemType()); }
41  auto isEqual(const Item& other) const { return node::isEqual(this, other); }
42 
43  // Node interface.
44  auto properties() const { return node::Properties{}; }
45 };
46 
47 } // namespace spicy::type::unit::item
NodeBase(Meta meta)
Definition: node.h:367
const auto & children() const
Definition: node.h:472
Definition: variable.h:21
Definition: meta.h:19
Definition: attribute.h:174
Definition: type.h:160
hilti::optional_ref< const Attribute > find(std::string_view tag) const
Definition: attribute.h:200
Definition: unit-item.h:19
Definition: hook.h:22
Definition: id.h:18
Definition: node.h:360