Spicy
type-wrapped.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 <hilti/ast/expression.h>
8 #include <hilti/ast/types/computed.h>
9 
10 namespace hilti {
11 namespace expression {
12 
36 class TypeWrapped : public NodeBase, public trait::isExpression {
37 public:
38  struct ValidateTypeMatch {};
39 
40  TypeWrapped(Expression e, Meta m = Meta()) : NodeBase(nodes(std::move(e), node::none), std::move(m)) {}
41 
42  TypeWrapped(Expression e, Type t, Meta m = Meta()) : NodeBase(nodes(std::move(e), std::move(t)), std::move(m)) {}
43 
44  TypeWrapped(Expression e, Type t, ValidateTypeMatch _, Meta m = Meta())
45  : NodeBase(nodes(std::move(e), std::move(t)), std::move(m)), _validate_type_match(true) {}
46 
47  TypeWrapped(Expression e, NodeRef t, Meta m = Meta())
48  : NodeBase(nodes(std::move(e)), std::move(m)), _type_node_ref(std::move(t)) {}
49 
50  TypeWrapped(Expression e, NodeRef t, ValidateTypeMatch _, Meta m = Meta())
51  : NodeBase(nodes(std::move(e)), std::move(m)), _validate_type_match(true), _type_node_ref(std::move(t)) {}
52 
53  const auto& expression() const { return child<Expression>(0); }
54  bool validateTypeMatch() const { return _validate_type_match; }
55 
56  bool operator==(const TypeWrapped& other) const {
57  return expression() == other.expression() && type() == other.type();
58  }
59 
61  bool isLhs() const { return expression().isLhs(); }
63  bool isTemporary() const { return expression().isTemporary(); }
65  Type type() const {
66  if ( _type_node_ref )
67  return _type_node_ref->template as<Type>();
68 
69  if ( auto t = childs()[1].tryAs<Type>() ) {
70  if ( t->template isA<type::Computed>() )
71  // Don't call effectiveType() here, we want to keep
72  // evaluation pending.
73  return *t;
74 
75  return type::effectiveType(*t);
76  }
77 
78  return type::Computed(expression(), meta());
79  }
80 
82  auto isConstant() const { return expression().isConstant(); }
84  auto isEqual(const Expression& other) const { return node::isEqual(this, other); }
85 
87  auto properties() const { return node::Properties{{"validate_type_match", _validate_type_match}}; }
88 
89 private:
90  bool _validate_type_match = false;
91  NodeRef _type_node_ref;
92 };
93 
94 } // namespace expression
95 } // namespace hilti
auto & childs() const
Definition: node.h:445
const Node none
Definition: node.cc:12
Type type() const
Definition: type-wrapped.h:65
Definition: computed.h:28
Definition: expression.h:16
auto properties() const
Definition: type-wrapped.h:87
Definition: meta.h:18
Definition: type-wrapped.h:36
auto isConstant() const
Definition: type-wrapped.h:82
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
bool isLhs() const
Definition: type-wrapped.h:61
Definition: node_ref.h:44
auto isEqual(const Expression &other) const
Definition: type-wrapped.h:84
bool isTemporary() const
Definition: type-wrapped.h:63
auto & meta() const
Definition: node.h:449
Definition: node.h:318