Spicy
assert.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/expression.h>
8 #include <hilti/ast/statement.h>
9 #include <hilti/ast/type.h>
10 
11 namespace hilti::statement {
12 
13 namespace assert {
18 struct Exception {};
19 } // namespace assert
20 
22 class Assert : public Statement {
23 public:
24  auto expression() const { return child<::hilti::Expression>(0); }
25  auto exception() const { return child<UnqualifiedType>(1); }
26  auto message() const { return child<::hilti::Expression>(2); }
27  bool expectException() const { return _expect_exception; }
28 
29  node::Properties properties() const final {
30  auto p = node::Properties{{"expect_exception", _expect_exception}};
31  return Statement::properties() + std::move(p);
32  }
33 
34  void setExpression(ASTContext* ctx, hilti::Expression* c) { setChild(ctx, 0, c); }
35 
43  static auto create(ASTContext* ctx, hilti::Expression* expr, hilti::Expression* msg = nullptr, Meta meta = {}) {
44  return ctx->make<Assert>(ctx, {expr, nullptr, msg}, false, std::move(meta));
45  }
46 
57  static auto create(ASTContext* ctx, assert::Exception /*unused*/, hilti::Expression* expr, UnqualifiedType* excpt,
58  hilti::Expression* msg = nullptr, Meta meta = {}) {
59  return ctx->make<Assert>(ctx, {expr, excpt, msg}, true, std::move(meta));
60  }
61 
62 protected:
63  Assert(ASTContext* ctx, Nodes children, bool expect_exception, Meta meta)
64  : Statement(ctx, NodeTags, std::move(children), std::move(meta)), _expect_exception(expect_exception) {}
65 
66  HILTI_NODE_1(statement::Assert, Statement, final);
67 
68 private:
69  bool _expect_exception;
70 };
71 
72 } // namespace hilti::statement
Definition: ast-context.h:121
T * make(Args &&... args)
Definition: ast-context.h:366
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
virtual node::Properties properties() const
Definition: node.h:891
Definition: statement.h:15
Definition: type.h:148
Definition: assert.h:22
static auto create(ASTContext *ctx, hilti::Expression *expr, hilti::Expression *msg=nullptr, Meta meta={})
Definition: assert.h:43
static auto create(ASTContext *ctx, assert::Exception, hilti::Expression *expr, UnqualifiedType *excpt, hilti::Expression *msg=nullptr, Meta meta={})
Definition: assert.h:57
node::Properties properties() const final
Definition: assert.h:29