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 {
11 namespace statement {
12 
13 namespace assert {
18 struct Exception {};
19 } // namespace assert
20 
22 class Assert : public NodeBase, public hilti::trait::isStatement {
23 public:
31  Assert(::hilti::Expression expr, std::optional<::hilti::Expression> msg, Meta m = Meta())
32  : NodeBase(nodes(std::move(expr), node::none, std::move(msg)), std::move(m)) {}
33 
44  Assert(assert::Exception /*unused*/, ::hilti::Expression expr, std::optional<Type> excpt,
45  std::optional<::hilti::Expression> msg, Meta m = Meta())
46  : NodeBase(nodes(std::move(expr), std::move(excpt), std::move(msg)), std::move(m)), _expects_exception(true) {}
47 
48  bool expectsException() const { return _expects_exception; }
49  const auto& expression() const { return child<::hilti::Expression>(0); }
50  auto exception() const { return type::effectiveOptionalType(childs()[1].tryAs<Type>()); }
51  auto message() const { return childs()[2].tryReferenceAs<::hilti::Expression>(); }
52 
53  bool operator==(const Assert& other) const {
54  return _expects_exception == other._expects_exception && expression() == other.expression() &&
55  exception() == other.exception() && message() == other.message();
56  }
57 
59  auto isEqual(const Statement& other) const { return node::isEqual(this, other); }
60 
62  auto properties() const { return node::Properties{{"expects-exception", _expects_exception}}; }
63 
71  static Statement setCondition(const Assert& e, const hilti::Expression& c) {
72  auto x = Statement(e)._clone().as<Assert>();
73  x.childs()[0] = c;
74  return x;
75  }
76 
77 private:
78  bool _expects_exception = false;
79 };
80 
81 } // namespace statement
82 } // namespace hilti
Assert(::hilti::Expression expr, std::optional<::hilti::Expression > msg, Meta m=Meta())
Definition: assert.h:31
std::vector< T > childs(int begin, int end) const
Definition: node.h:373
auto isEqual(const Statement &other) const
Definition: assert.h:59
auto properties() const
Definition: assert.h:62
Definition: optional.h:79
static Statement setCondition(const Assert &e, const hilti::Expression &c)
Definition: assert.h:71
Definition: meta.h:18
Assert(assert::Exception, ::hilti::Expression expr, std::optional< Type > excpt, std::optional<::hilti::Expression > msg, Meta m=Meta())
Definition: assert.h:44
Definition: statement.h:14
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
Definition: assert.h:22
Definition: node.h:318