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 
12 namespace hilti {
13 namespace 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  std::optional<hilti::declaration::Parameter> parameter() const {
31  auto d = childs()[0].tryAs<hilti::Declaration>();
32  if ( d )
33  return d->as<hilti::declaration::Parameter>();
34 
35  return {};
36  }
37 
38  const auto& body() const { return child<hilti::Statement>(1); }
39 
41  auto& _bodyNode() { return childs()[1]; }
42 
44  auto properties() const { return node::Properties{}; }
45 
46  bool operator==(const Catch& other) const { return parameter() == other.parameter() && body() == other.body(); }
47 };
48 
49 } // namespace try_
50 
52 class Try : public NodeBase, public hilti::trait::isStatement {
53 public:
54  Try(hilti::Statement body, std::vector<try_::Catch> catches, Meta m = Meta())
55  : NodeBase(nodes(std::move(body), std::move(catches)), std::move(m)) {}
56 
57  const auto& body() const { return child<hilti::Statement>(0); }
58  auto catches() const { return childs<try_::Catch>(1, -1); }
59 
60  bool operator==(const Try& other) const { return body() == other.body() && catches() == other.catches(); }
61 
63  auto& _bodyNode() { return childs()[0]; }
64 
66  auto& _lastCatchNode() { return childs().back(); }
67 
69  void _addCatch(try_::Catch catch_) { addChild(std::move(catch_)); }
70 
72  auto isEqual(const hilti::Statement& other) const { return node::isEqual(this, other); }
73 
75  auto properties() const { return node::Properties{}; }
76 };
77 
78 } // namespace statement
79 } // namespace hilti
auto & childs() const
Definition: node.h:445
Definition: try.h:20
auto properties() const
Definition: try.h:44
Definition: try.h:52
const Node none
Definition: node.cc:12
void addChild(Node n)
Definition: node.h:434
auto & _bodyNode()
Definition: try.h:63
Definition: meta.h:18
auto properties() const
Definition: try.h:75
Definition: statement.h:14
Definition: parameter.h:45
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
auto isEqual(const hilti::Statement &other) const
Definition: try.h:72
void _addCatch(try_::Catch catch_)
Definition: try.h:69
auto & _bodyNode()
Definition: try.h:41
auto & _lastCatchNode()
Definition: try.h:66
Definition: node.h:318