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::ctor {
16 
18 class Tuple : public NodeBase, public hilti::trait::isCtor {
19 public:
20  Tuple(const std::vector<Expression>& v, Meta m = Meta()) : NodeBase(nodes(_inferType(v), v), std::move(m)) {}
21 
22  auto value() const { return children<Expression>(1, -1); }
23 
24  void setElementTypes(std::vector<Type> t) { children()[0] = Type(type::Tuple(std::move(t), meta())); }
25 
26  bool operator==(const Tuple& other) const { return value() == other.value(); }
27 
29  const auto& type() const { return child<Type>(0); }
30 
32  bool isConstant() const { return true; }
33 
35  auto isLhs() const {
36  const auto& v = value();
37 
38  if ( v.empty() )
39  return false;
40 
41  return std::all_of(v.begin(), v.end(), [](const auto& e) { return e.isLhs(); });
42  }
43 
45  auto isTemporary() const { return true; }
47  auto isEqual(const Ctor& other) const { return node::isEqual(this, other); }
49  auto properties() const { return node::Properties{}; }
50 
51 private:
52  Type _inferType(const std::vector<Expression>& exprs) {
53  for ( const auto& e : exprs ) {
54  if ( ! expression::isResolved(e) )
55  return type::auto_;
56  }
57 
58  std::vector<Type> types;
59  types.reserve(exprs.size());
60  for ( const auto& e : exprs )
61  types.push_back(e.type());
62 
63  return type::Tuple(std::move(types));
64  }
65 };
66 
67 } // namespace hilti::ctor
auto isLhs() const
Definition: tuple.h:35
const auto & children() const
Definition: node.h:471
const auto & type() const
Definition: tuple.h:29
Definition: meta.h:19
Definition: type.h:158
auto isTemporary() const
Definition: tuple.h:45
auto properties() const
Definition: tuple.h:49
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:97
bool isConstant() const
Definition: tuple.h:32
Definition: ctor.h:15
Definition: tuple.h:39
auto isEqual(const Ctor &other) const
Definition: tuple.h:47
auto & meta() const
Definition: node.h:475
Definition: tuple.h:18
Definition: node.h:359