Spicy
tuple.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <algorithm>
6 #include <utility>
7 #include <vector>
8 
9 #include <hilti/ast/ctor.h>
10 #include <hilti/ast/expression.h>
11 #include <hilti/ast/types/auto.h>
12 #include <hilti/ast/types/tuple.h>
13 #include <hilti/ast/types/unknown.h>
14 
15 namespace hilti {
16 namespace ctor {
17 
19 class Tuple : public NodeBase, public hilti::trait::isCtor {
20 public:
21  Tuple(std::vector<Expression> v, Meta m = Meta()) : NodeBase(nodes(_inferType(v), v), std::move(m)) {}
22 
23  auto value() const { return childs<Expression>(1, -1); }
24 
25  void setElementTypes(std::vector<Type> t) { childs()[0] = Type(type::Tuple(std::move(t), meta())); }
26 
27  bool operator==(const Tuple& other) const { return value() == other.value(); }
28 
30  const auto& type() const { return child<Type>(0); }
31 
33  bool isConstant() const { return true; }
34 
36  auto isLhs() const {
37  if ( value().empty() )
38  return false;
39 
40  for ( const auto& e : value() ) {
41  if ( ! e.isLhs() )
42  return false;
43  }
44 
45  return true;
46  }
47 
49  auto isTemporary() const { return true; }
51  auto isEqual(const Ctor& other) const { return node::isEqual(this, other); }
53  auto properties() const { return node::Properties{}; }
54 
55 private:
56  Type _inferType(const std::vector<Expression>& exprs) {
57  for ( const auto& e : exprs ) {
58  if ( ! expression::isResolved(e) )
59  return type::auto_;
60  }
61 
62  std::vector<Type> types;
63  for ( const auto& e : exprs )
64  types.push_back(e.type());
65 
66  return type::Tuple(std::move(types));
67  }
68 };
69 
70 } // namespace ctor
71 } // namespace hilti
auto isLhs() const
Definition: tuple.h:36
const auto & type() const
Definition: tuple.h:30
Definition: meta.h:18
Definition: type.h:159
auto isTemporary() const
Definition: tuple.h:49
auto properties() const
Definition: tuple.h:53
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
bool isConstant() const
Definition: tuple.h:33
Definition: ctor.h:15
const auto & childs() const
Definition: node.h:470
Definition: tuple.h:39
auto isEqual(const Ctor &other) const
Definition: tuple.h:51
auto & meta() const
Definition: node.h:474
Definition: tuple.h:19
Definition: node.h:358