Spicy
try.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <memory>
6 #include <string>
7 #include <utility>
8 #include <vector>
9 
10 #include <hilti/ast/declarations/parameter.h>
11 #include <hilti/ast/expression.h>
12 #include <hilti/ast/statement.h>
13 #include <hilti/base/logger.h>
14 
15 namespace hilti::statement {
16 
17 namespace try_ {
18 
20 class Catch final : public Node {
21 public:
22  auto parameter() const { return child<declaration::Parameter>(0); }
23  auto body() const { return child<hilti::Statement>(1); }
24 
25  static auto create(ASTContext* ctx, hilti::Declaration* param, Statement* body, Meta meta = {}) {
26  return ctx->make<Catch>(ctx, {param, body}, std::move(meta));
27  }
28 
29  static auto create(ASTContext* ctx, Statement* body, Meta meta = {}) {
30  return ctx->make<Catch>(ctx, {nullptr, body}, std::move(meta));
31  }
32 
33 protected:
34  Catch(ASTContext* ctx, Nodes children, Meta meta = {}) : Node(ctx, NodeTags, std::move(children), std::move(meta)) {
35  if ( child(0) && ! child(0)->isA<declaration::Parameter>() )
36  logger().internalError("'catch' first child must be parameter");
37  }
38 
39  std::string _dump() const final;
40 
41  HILTI_NODE_0(statement::try_::Catch, final);
42 };
43 
44 using Catches = NodeVector<Catch>;
45 
46 } // namespace try_
47 
49 class Try : public Statement {
50 public:
51  auto body() const { return child<hilti::Statement>(0); }
52  auto catches() const { return children<try_::Catch>(1, {}); }
53 
54  void addCatch(ASTContext* ctx, try_::Catch* c) { addChild(ctx, c); }
55 
56  static auto create(ASTContext* ctx, Statement* body, const try_::Catches& catches, Meta meta = {}) {
57  return ctx->make<Try>(ctx, node::flatten(body, catches), std::move(meta));
58  }
59 
60 protected:
61  Try(ASTContext* ctx, Nodes children, Meta meta) : Statement(ctx, NodeTags, std::move(children), std::move(meta)) {}
62 
63  HILTI_NODE_1(statement::Try, Statement, final);
64 };
65 
66 } // namespace hilti::statement
Definition: ast-context.h:121
T * make(Args &&... args)
Definition: ast-context.h:366
Definition: declaration.h:48
Definition: meta.h:30
Definition: node.h:240
void addChild(ASTContext *ctx, Node *n)
Definition: node.h:522
const auto & children() const
Definition: node.h:364
const auto & meta() const
Definition: node.h:306
Node(ASTContext *ctx, node::Tags node_tags, Nodes children, Meta meta)
Definition: node.h:922
T * child(unsigned int i) const
Definition: node.h:374
Definition: forward.h:758
Definition: statement.h:15
Definition: try.h:49
Definition: try.h:20
std::string _dump() const final
Definition: try.cc:7