/proc/self/cwd/common/ast_visitor.h
Line | Count | Source |
1 | | // Copyright 2024 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef THIRD_PARTY_CEL_CPP_COMMON_AST_VISITOR_NATIVE_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_COMMON_AST_VISITOR_NATIVE_H_ |
17 | | |
18 | | #include "common/constant.h" |
19 | | #include "common/expr.h" |
20 | | |
21 | | namespace cel { |
22 | | |
23 | | // ComprehensionArg specifies arg_num values passed to PostVisitArg |
24 | | // for subexpressions of Comprehension. |
25 | | enum ComprehensionArg { |
26 | | ITER_RANGE, |
27 | | ACCU_INIT, |
28 | | LOOP_CONDITION, |
29 | | LOOP_STEP, |
30 | | RESULT, |
31 | | }; |
32 | | |
33 | | // Callback handler class, used in conjunction with AstTraverse. |
34 | | // Methods of this class are invoked when AST nodes with corresponding |
35 | | // types are processed. |
36 | | // |
37 | | // For all types with children, the children will be visited in the natural |
38 | | // order from first to last. For structs, keys are visited before values. |
39 | | class AstVisitor { |
40 | | public: |
41 | 10.3k | virtual ~AstVisitor() = default; |
42 | | |
43 | | // Expr node handler method. Called for all Expr nodes. |
44 | | // Is invoked before child Expr nodes being processed. |
45 | | virtual void PreVisitExpr(const Expr&) = 0; |
46 | | |
47 | | // Expr node handler method. Called for all Expr nodes. |
48 | | // Is invoked after child Expr nodes are processed. |
49 | | virtual void PostVisitExpr(const Expr&) = 0; |
50 | | |
51 | | // Const node handler. |
52 | | // Invoked after child nodes are processed. |
53 | | virtual void PostVisitConst(const Expr&, const Constant&) = 0; |
54 | | |
55 | | // Ident node handler. |
56 | | // Invoked after child nodes are processed. |
57 | | virtual void PostVisitIdent(const Expr&, const IdentExpr&) = 0; |
58 | | |
59 | | // Select node handler |
60 | | // Invoked before child nodes are processed. |
61 | | virtual void PreVisitSelect(const Expr&, const SelectExpr&) = 0; |
62 | | |
63 | | // Select node handler |
64 | | // Invoked after child nodes are processed. |
65 | | virtual void PostVisitSelect(const Expr&, const SelectExpr&) = 0; |
66 | | |
67 | | // Call node handler group |
68 | | // We provide finer granularity for Call node callbacks to allow special |
69 | | // handling for short-circuiting |
70 | | // PreVisitCall is invoked before child nodes are processed. |
71 | | virtual void PreVisitCall(const Expr&, const CallExpr&) = 0; |
72 | | |
73 | | // Invoked after all child nodes are processed. |
74 | | virtual void PostVisitCall(const Expr&, const CallExpr&) = 0; |
75 | | |
76 | | // Invoked after target node is processed. |
77 | | // Expr is the call expression. |
78 | | virtual void PostVisitTarget(const Expr&) = 0; |
79 | | |
80 | | // Invoked before all child nodes are processed. |
81 | | virtual void PreVisitComprehension(const Expr&, const ComprehensionExpr&) = 0; |
82 | | |
83 | | // Invoked before comprehension child node is processed. |
84 | | virtual void PreVisitComprehensionSubexpression( |
85 | | const Expr&, const ComprehensionExpr& compr, |
86 | 0 | ComprehensionArg comprehension_arg) {} |
87 | | |
88 | | // Invoked after comprehension child node is processed. |
89 | | virtual void PostVisitComprehensionSubexpression( |
90 | | const Expr&, const ComprehensionExpr& compr, |
91 | 0 | ComprehensionArg comprehension_arg) {} |
92 | | |
93 | | // Invoked after all child nodes are processed. |
94 | | virtual void PostVisitComprehension(const Expr&, |
95 | | const ComprehensionExpr&) = 0; |
96 | | |
97 | | // Invoked after each argument node processed. |
98 | | // For Call arg_num is the index of the argument. |
99 | | // For Comprehension arg_num is specified by ComprehensionArg. |
100 | | // Expr is the call expression. |
101 | | virtual void PostVisitArg(const Expr&, int arg_num) = 0; |
102 | | |
103 | | // List node handler |
104 | | // Invoked after child nodes are processed. |
105 | | virtual void PostVisitList(const Expr&, const ListExpr&) = 0; |
106 | | |
107 | | // Struct node handler |
108 | | // Invoked after child nodes are processed. |
109 | | virtual void PostVisitStruct(const Expr&, const StructExpr&) = 0; |
110 | | |
111 | | // Map node handler |
112 | | // Invoked after child nodes are processed. |
113 | | virtual void PostVisitMap(const Expr&, const MapExpr&) = 0; |
114 | | }; |
115 | | |
116 | | } // namespace cel |
117 | | |
118 | | #endif // THIRD_PARTY_CEL_CPP_COMMON_AST_VISITOR_NATIVE_H_ |