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 childs()[0].tryAs<declaration::Parameter>(); }
32  auto parameterRef() const { return childs()[0].isA<declaration::Parameter>() ? NodeRef(childs()[0]) : NodeRef(); }
33  const auto& body() const { return child<hilti::Statement>(1); }
34 
36  auto& _bodyNode() { return childs()[1]; }
37 
39  auto properties() const { return node::Properties{}; }
40 
41  bool operator==(const Catch& other) const { return parameter() == other.parameter() && body() == other.body(); }
42 };
43 
44 } // namespace try_
45 
47 class Try : public NodeBase, public hilti::trait::isStatement {
48 public:
49  Try(hilti::Statement body, std::vector<try_::Catch> catches, Meta m = Meta())
50  : NodeBase(nodes(std::move(body), std::move(catches)), std::move(m)) {}
51 
52  const auto& body() const { return child<hilti::Statement>(0); }
53  auto catches() const { return childs<try_::Catch>(1, -1); }
54 
55  bool operator==(const Try& other) const { return body() == other.body() && catches() == other.catches(); }
56 
58  auto& _bodyNode() { return childs()[0]; }
59 
61  auto& _lastCatchNode() { return childs().back(); }
62 
64  void _addCatch(try_::Catch catch_) { addChild(std::move(catch_)); }
65 
67  auto isEqual(const hilti::Statement& other) const { return node::isEqual(this, other); }
68 
70  auto properties() const { return node::Properties{}; }
71 };
72 
73 } // namespace statement
74 } // namespace hilti
Definition: try.h:21
Definition: declaration.h:53
auto properties() const
Definition: try.h:39
Definition: try.h:47
const Node none
Definition: node.cc:14
void addChild(Node n)
Definition: node.h:459
auto & _bodyNode()
Definition: try.h:58
Definition: meta.h:18
auto properties() const
Definition: try.h:70
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:67
Definition: node-ref.h:44
void _addCatch(try_::Catch catch_)
Definition: try.h:64
const auto & childs() const
Definition: node.h:470
auto & _bodyNode()
Definition: try.h:36
auto & _lastCatchNode()
Definition: try.h:61
Definition: node.h:358