Spicy
grouping.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 
7 #include <hilti/ast/declarations/local-variable.h>
8 #include <hilti/ast/expression.h>
9 #include <hilti/ast/type.h>
10 
11 namespace hilti::expression {
12 
20 class Grouping : public Expression {
21 public:
22  auto local() const { return child<declaration::LocalVariable>(0); }
23  auto expressions() const { return children<Expression>(1, {}); }
24 
25  QualifiedType* type() const final {
26  if ( auto* last = child<Expression>(-1) )
27  return last->type();
28  else
29  return nullptr;
30  }
31 
32  void setExpressions(ASTContext* ctx, Expressions exprs) {
33  removeChildren(1, {});
34  addChildren(ctx, std::move(exprs));
35  }
36 
37  void removeLocal(ASTContext* ctx) { setChild(ctx, 0, nullptr); }
38 
39  static auto create(ASTContext* ctx, Expressions exprs, Meta meta = {}) {
40  Nodes nodes = {nullptr};
41  nodes.insert(nodes.end(), exprs.begin(), exprs.end());
42  return ctx->make<Grouping>(ctx, std::move(nodes), std::move(meta));
43  }
44 
45  static auto create(ASTContext* ctx, declaration::LocalVariable* local, Expressions exprs, Meta meta = {}) {
46  Nodes nodes = {local};
47  nodes.insert(nodes.end(), exprs.begin(), exprs.end());
48  return ctx->make<Grouping>(ctx, std::move(nodes), std::move(meta));
49  }
50 
51 protected:
52  Grouping(ASTContext* ctx, Nodes children, Meta meta)
53  : Expression(ctx, NodeTags, std::move(children), std::move(meta)) {}
54 
55  HILTI_NODE_1(expression::Grouping, Expression, final);
56 };
57 
58 } // namespace hilti::expression
Definition: ast-context.h:128
Definition: expression.h:15
void setChild(ASTContext *ctx, size_t idx, Node *n)
Definition: node.h:631
const auto & children() const
Definition: node.h:382
const auto & meta() const
Definition: node.h:324
void removeChildren(int begin, std::optional< int > end)
Definition: node.h:604
void addChildren(ASTContext *ctx, const Nodes &children)
Definition: node.h:574
Definition: type.h:365
UnqualifiedType * type(bool follow=true) const
Definition: type.h:372
Definition: grouping.h:20
QualifiedType * type() const final
Definition: grouping.h:25