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 {
14 namespace statement {
15 
16 namespace try_ {
17 
21 class Catch : public NodeBase {
22 public:
23  Catch(hilti::Statement body, Meta m = Meta()) : NodeBase(nodes(node::none, std::move(body)), std::move(m)) {}
24  Catch(const hilti::Declaration& param, Statement body, Meta m = Meta())
25  : NodeBase(nodes(param, std::move(body)), std::move(m)) {
26  if ( ! param.isA<hilti::declaration::Parameter>() )
27  logger().internalError("'catch' hilti::Declaration must be parameter");
28  }
29  Catch() = default;
30 
31  auto parameter() const { return children()[0].tryAs<declaration::Parameter>(); }
32  auto parameterRef() const {
33  return children()[0].isA<declaration::Parameter>() ? NodeRef(children()[0]) : NodeRef();
34  }
35  const auto& body() const { return child<hilti::Statement>(1); }
36 
38  auto& _bodyNode() { return children()[1]; }
39 
41  auto properties() const { return node::Properties{}; }
42 
43  bool operator==(const Catch& other) const { return parameter() == other.parameter() && body() == other.body(); }
44 };
45 
46 } // namespace try_
47 
49 class Try : public NodeBase, public hilti::trait::isStatement {
50 public:
51  Try(hilti::Statement body, std::vector<try_::Catch> catches, Meta m = Meta())
52  : NodeBase(nodes(std::move(body), std::move(catches)), std::move(m)) {}
53 
54  const auto& body() const { return child<hilti::Statement>(0); }
55  auto catches() const { return children<try_::Catch>(1, -1); }
56 
57  bool operator==(const Try& other) const { return body() == other.body() && catches() == other.catches(); }
58 
60  auto& _bodyNode() { return children()[0]; }
61 
63  auto& _lastCatchNode() { return children().back(); }
64 
66  void _addCatch(try_::Catch catch_) { addChild(std::move(catch_)); }
67 
69  auto isEqual(const hilti::Statement& other) const { return node::isEqual(this, other); }
70 
72  auto properties() const { return node::Properties{}; }
73 };
74 
75 } // namespace statement
76 } // namespace hilti
Definition: try.h:21
Definition: declaration.h:53
auto properties() const
Definition: try.h:41
Definition: try.h:49
const Node none
Definition: node.cc:14
const auto & children() const
Definition: node.h:470
void addChild(Node n)
Definition: node.h:459
auto & _bodyNode()
Definition: try.h:60
Definition: meta.h:18
auto properties() const
Definition: try.h:72
Definition: statement.h:14
Definition: parameter.h:47
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:99
auto isEqual(const hilti::Statement &other) const
Definition: try.h:69
Definition: node-ref.h:44
void _addCatch(try_::Catch catch_)
Definition: try.h:66
auto & _bodyNode()
Definition: try.h:38
auto & _lastCatchNode()
Definition: try.h:63
Definition: node.h:358