Spicy
try.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <utility>
6 #include <vector>
7 
8 #include <hilti/ast/declarations/parameter.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 
15 namespace try_ {
16 
20 class Catch : public NodeBase {
21 public:
22  Catch(hilti::Statement body, Meta m = Meta()) : NodeBase(nodes(node::none, std::move(body)), std::move(m)) {}
23  Catch(const hilti::Declaration& param, Statement body, Meta m = Meta())
24  : NodeBase(nodes(param, std::move(body)), std::move(m)) {
25  if ( ! param.isA<hilti::declaration::Parameter>() )
26  logger().internalError("'catch' hilti::Declaration must be parameter");
27  }
28  Catch() = default;
29 
30  auto parameter() const { return children()[0].tryAs<declaration::Parameter>(); }
31  auto parameterRef() const {
32  return children()[0].isA<declaration::Parameter>() ? NodeRef(children()[0]) : NodeRef();
33  }
34  const auto& body() const { return child<hilti::Statement>(1); }
35 
37  auto& _bodyNode() { return children()[1]; }
38 
40  auto properties() const { return node::Properties{}; }
41 
42  bool operator==(const Catch& other) const { return parameter() == other.parameter() && body() == other.body(); }
43 };
44 
45 } // namespace try_
46 
48 class Try : public NodeBase, public hilti::trait::isStatement {
49 public:
50  Try(hilti::Statement body, std::vector<try_::Catch> catches, Meta m = Meta())
51  : NodeBase(nodes(std::move(body), std::move(catches)), std::move(m)) {}
52 
53  const auto& body() const { return child<hilti::Statement>(0); }
54  auto catches() const { return children<try_::Catch>(1, -1); }
55 
56  bool operator==(const Try& other) const { return body() == other.body() && catches() == other.catches(); }
57 
59  auto& _bodyNode() { return children()[0]; }
60 
62  auto& _lastCatchNode() { return children().back(); }
63 
65  void _addCatch(try_::Catch catch_) { addChild(std::move(catch_)); }
66 
68  auto isEqual(const hilti::Statement& other) const { return node::isEqual(this, other); }
69 
71  auto properties() const { return node::Properties{}; }
72 };
73 
74 } // namespace hilti::statement
Definition: try.h:20
Definition: declaration.h:54
auto properties() const
Definition: try.h:40
Definition: try.h:48
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:472
void addChild(Node n)
Definition: node.h:461
auto & _bodyNode()
Definition: try.h:59
Definition: meta.h:19
auto properties() const
Definition: try.h:71
Definition: statement.h:14
Definition: parameter.h:46
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:98
auto isEqual(const hilti::Statement &other) const
Definition: try.h:68
Definition: node-ref.h:45
void _addCatch(try_::Catch catch_)
Definition: try.h:65
auto & _bodyNode()
Definition: try.h:37
auto & _lastCatchNode()
Definition: try.h:62
Definition: node.h:360