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 childs()[2].tryReferenceAs<Expression>(); }
29  auto attributes() const { return childs()[3].tryReferenceAs<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 field interface
39  Type itemType() const { return type::effectiveType(child<Type>(1)); }
40  auto isEqual(const Item& other) const { return node::isEqual(this, other); }
41 
42  // Node interface.
43  auto properties() const { return node::Properties{}; }
44 };
45 
46 } // namespace spicy::type::unit::item
NodeBase(Meta meta)
Definition: node.h:325
auto & childs() const
Definition: node.h:445
Definition: variable.h:21
Definition: meta.h:18
Definition: attribute.h:159
std::optional< Attribute > find(std::string_view tag) const
Definition: attribute.h:190
Definition: unit-item.h:19
Definition: unit-item.h:39
Definition: id.h:18
Definition: node.h:318