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, hilti::Declaration* init, hilti::Expression* cond, Statement* true_,
26  Statement* false_, Meta meta = {}) {
27  return ctx->make<If>(ctx, {init, cond, true_, false_}, std::move(meta));
28  }
29 
30  static auto create(ASTContext* ctx, hilti::Expression* cond, Statement* true_, Statement* false_, Meta meta = {}) {
31  return ctx->make<If>(ctx, {nullptr, cond, true_, false_}, std::move(meta));
32  }
33 
34 protected:
35  If(ASTContext* ctx, Nodes children, Meta meta) : Statement(ctx, NodeTags, std::move(children), std::move(meta)) {
36  if ( child(0) && ! child(0)->isA<declaration::LocalVariable>() )
37  logger().internalError("initialization for 'if' must be a local declaration");
38  }
39 
40  HILTI_NODE_1(statement::If, Statement, final);
41 };
42 
43 } // namespace hilti::statement
Definition: ast-context.h:121
T * make(Args &&... args)
Definition: ast-context.h:366
Definition: declaration.h:48
Definition: expression.h:15
Definition: meta.h:30
void setChild(ASTContext *ctx, size_t idx, Node *n)
Definition: node.h:602
const auto & children() const
Definition: node.h:364
const auto & meta() const
Definition: node.h:306
T * child(unsigned int i) const
Definition: node.h:374
Definition: forward.h:758
Definition: statement.h:15
Definition: if.h:16