Spicy
if.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <memory>
6 #include <utility>
7 
8 #include <hilti/ast/declarations/local-variable.h>
9 #include <hilti/ast/expression.h>
10 #include <hilti/ast/statement.h>
11 #include <hilti/base/logger.h>
12 
13 namespace hilti::statement {
14 
16 class If : public Statement {
17 public:
18  auto init() const { return child<hilti::declaration::LocalVariable>(0); }
19  auto condition() const { return child<::hilti::Expression>(1); }
20  auto true_() const { return child<hilti::Statement>(2); }
21  auto false_() const { return child<Statement>(3); }
22 
23  void setCondition(ASTContext* ctx, hilti::Expression* c) { setChild(ctx, 1, c); }
24 
25  static auto create(ASTContext* ctx,
26  hilti::Declaration* init,
27  hilti::Expression* cond,
28  Statement* true_,
29  Statement* false_,
30  Meta meta = {}) {
31  return ctx->make<If>(ctx, {init, cond, true_, false_}, std::move(meta));
32  }
33 
34  static auto create(ASTContext* ctx, hilti::Expression* cond, Statement* true_, Statement* false_, Meta meta = {}) {
35  return ctx->make<If>(ctx, {nullptr, cond, true_, false_}, std::move(meta));
36  }
37 
38 protected:
39  If(ASTContext* ctx, Nodes children, Meta meta) : Statement(ctx, NodeTags, std::move(children), std::move(meta)) {
40  if ( child(0) && ! child(0)->isA<declaration::LocalVariable>() )
41  logger().internalError("initialization for 'if' must be a local declaration");
42  }
43 
44  HILTI_NODE_1(statement::If, Statement, final);
45 };
46 
47 } // namespace hilti::statement
Definition: ast-context.h:128
T * make(Args &&... args)
Definition: ast-context.h:382
Definition: declaration.h:53
Definition: expression.h:15
Definition: meta.h:30
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
T * child(int i) const
Definition: node.h:393
Definition: forward.h:757
Definition: statement.h:15
Definition: if.h:16