Spicy
assert.h
1 // Copyright (c) 2020-2021 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 
10 namespace hilti::statement {
11 
12 namespace assert {
17 struct Exception {};
18 } // namespace assert
19 
21 class Assert : public NodeBase, public hilti::trait::isStatement {
22 public:
30  Assert(::hilti::Expression expr, std::optional<::hilti::Expression> msg, Meta m = Meta())
31  : NodeBase(nodes(std::move(expr), node::none, std::move(msg)), std::move(m)) {}
32 
43  Assert(assert::Exception /*unused*/, ::hilti::Expression expr, std::optional<Type> excpt,
44  std::optional<::hilti::Expression> msg, Meta m = Meta())
45  : NodeBase(nodes(std::move(expr), std::move(excpt), std::move(msg)), std::move(m)), _expects_exception(true) {}
46 
47  bool expectsException() const { return _expects_exception; }
48  const auto& expression() const { return child<::hilti::Expression>(0); }
49  auto exception() const { return children()[1].tryAs<Type>(); }
50  auto message() const { return children()[2].tryAs<::hilti::Expression>(); }
51 
52  void setCondition(const hilti::Expression& c) { children()[0] = c; }
53 
54  bool operator==(const Assert& other) const {
55  return _expects_exception == other._expects_exception && expression() == other.expression() &&
56  exception() == other.exception() && message() == other.message();
57  }
58 
60  auto isEqual(const Statement& other) const { return node::isEqual(this, other); }
61 
63  auto properties() const { return node::Properties{{"expects-exception", _expects_exception}}; }
64 
65 private:
66  bool _expects_exception = false;
67 };
68 
69 } // namespace hilti::statement
Assert(::hilti::Expression expr, std::optional<::hilti::Expression > msg, Meta m=Meta())
Definition: assert.h:30
auto isEqual(const Statement &other) const
Definition: assert.h:60
auto properties() const
Definition: assert.h:63
Definition: optional.h:79
Definition: meta.h:19
Assert(assert::Exception, ::hilti::Expression expr, std::optional< Type > excpt, std::optional<::hilti::Expression > msg, Meta m=Meta())
Definition: assert.h:43
Definition: type.h:160
Definition: statement.h:14
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:98
Definition: assert.h:21
Definition: node.h:360