Commit | Line | Data |
---|---|---|
953192ba MD |
1 | #ifndef _FILTER_AST_H |
2 | #define _FILTER_AST_H | |
3 | ||
4 | /* | |
5 | * filter-ast.h | |
6 | * | |
7 | * LTTng filter AST | |
8 | * | |
ab5be9fa | 9 | * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
953192ba | 10 | * |
ab5be9fa | 11 | * SPDX-License-Identifier: LGPL-2.1-only |
953192ba | 12 | * |
953192ba MD |
13 | */ |
14 | ||
95b9bd90 MD |
15 | /* |
16 | * Note: filter-ast.h should be included before filter-parser.h. | |
17 | */ | |
18 | ||
953192ba | 19 | #include <urcu/list.h> |
0ae3cfc6 | 20 | #include <stdio.h> |
953192ba MD |
21 | #include <stdint.h> |
22 | ||
48a40005 SM |
23 | #if defined(__cplusplus) |
24 | extern "C" { | |
25 | #endif | |
26 | ||
953192ba MD |
27 | #define printf_debug(fmt, args...) \ |
28 | do { \ | |
29 | if (filter_parser_debug) \ | |
30 | fprintf(stdout, "[debug] " fmt, ## args); \ | |
31 | } while (0) | |
32 | ||
af71d06b MD |
33 | // the parameter name (of the reentrant 'yyparse' function) |
34 | // data is a pointer to a 'SParserParam' structure | |
35 | //#define YYPARSE_PARAM parser_ctx | |
36 | ||
953192ba MD |
37 | #ifndef YY_TYPEDEF_YY_SCANNER_T |
38 | #define YY_TYPEDEF_YY_SCANNER_T | |
39 | typedef void* yyscan_t; | |
40 | #endif | |
41 | ||
42 | extern int filter_parser_debug; | |
43 | ||
44 | struct filter_node; | |
45 | struct filter_parser; | |
46 | ||
47 | enum node_type { | |
48 | NODE_UNKNOWN = 0, | |
49 | NODE_ROOT, | |
50 | ||
51 | NODE_EXPRESSION, | |
52 | NODE_OP, | |
53 | NODE_UNARY_OP, | |
54 | ||
55 | NR_NODE_TYPES, | |
56 | }; | |
57 | ||
58 | enum op_type { | |
59 | AST_OP_UNKNOWN = 0, | |
60 | AST_OP_MUL, | |
61 | AST_OP_DIV, | |
62 | AST_OP_MOD, | |
63 | AST_OP_PLUS, | |
64 | AST_OP_MINUS, | |
116d3c01 MD |
65 | AST_OP_BIT_RSHIFT, |
66 | AST_OP_BIT_LSHIFT, | |
953192ba MD |
67 | AST_OP_AND, |
68 | AST_OP_OR, | |
bff988fa MD |
69 | AST_OP_BIT_AND, |
70 | AST_OP_BIT_OR, | |
71 | AST_OP_BIT_XOR, | |
953192ba MD |
72 | |
73 | AST_OP_EQ, | |
74 | AST_OP_NE, | |
75 | AST_OP_GT, | |
76 | AST_OP_LT, | |
77 | AST_OP_GE, | |
78 | AST_OP_LE, | |
79 | }; | |
80 | ||
81 | enum unary_op_type { | |
82 | AST_UNARY_UNKNOWN = 0, | |
83 | AST_UNARY_PLUS, | |
84 | AST_UNARY_MINUS, | |
85 | AST_UNARY_NOT, | |
bff988fa | 86 | AST_UNARY_BIT_NOT, |
953192ba MD |
87 | }; |
88 | ||
89 | enum ast_link_type { | |
90 | AST_LINK_UNKNOWN = 0, | |
91 | AST_LINK_DOT, | |
92 | AST_LINK_RARROW, | |
661dfdd1 | 93 | AST_LINK_BRACKET, |
953192ba MD |
94 | }; |
95 | ||
96 | struct filter_node { | |
97 | /* | |
98 | * Parent node is only set on demand by specific visitor. | |
99 | */ | |
100 | struct filter_node *parent; | |
101 | struct cds_list_head gc; | |
102 | ||
103 | enum node_type type; | |
104 | union { | |
105 | struct { | |
48a40005 SM |
106 | /* Avoid -Wextern-c-compat warning with clang++. */ |
107 | char unused; | |
953192ba MD |
108 | } unknown; |
109 | struct { | |
110 | struct filter_node *child; | |
111 | } root; | |
112 | struct { | |
113 | enum { | |
114 | AST_EXP_UNKNOWN = 0, | |
115 | AST_EXP_STRING, | |
116 | AST_EXP_CONSTANT, | |
e90d8561 | 117 | AST_EXP_FLOAT_CONSTANT, |
953192ba | 118 | AST_EXP_IDENTIFIER, |
586dc72f | 119 | AST_EXP_GLOBAL_IDENTIFIER, |
953192ba MD |
120 | AST_EXP_NESTED, |
121 | } type; | |
122 | enum ast_link_type post_op; /* reverse */ | |
123 | enum ast_link_type pre_op; /* forward */ | |
124 | union { | |
b53d4e59 | 125 | const char *string; |
953192ba | 126 | uint64_t constant; |
e90d8561 | 127 | double float_constant; |
b53d4e59 | 128 | const char *identifier; |
953192ba MD |
129 | /* |
130 | * child can be nested. | |
131 | */ | |
132 | struct filter_node *child; | |
133 | } u; | |
bff988fa | 134 | /* prev: backward dot/arrow chain (postfix expression) */ |
953192ba | 135 | struct filter_node *prev; |
bff988fa | 136 | /* next: forward dot/arrow chain, generated by a visitor. */ |
953192ba | 137 | struct filter_node *next; |
bff988fa MD |
138 | /* next_bracket: linked bracket chain (prefix expression) */ |
139 | struct filter_node *next_bracket; | |
953192ba MD |
140 | } expression; |
141 | struct { | |
142 | enum op_type type; | |
143 | struct filter_node *lchild; | |
144 | struct filter_node *rchild; | |
145 | } op; | |
146 | struct { | |
147 | enum unary_op_type type; | |
148 | struct filter_node *child; | |
149 | } unary_op; | |
150 | } u; | |
151 | }; | |
152 | ||
153 | struct filter_ast { | |
154 | struct filter_node root; | |
155 | struct cds_list_head allocated_nodes; | |
156 | }; | |
157 | ||
158 | const char *node_type(struct filter_node *node); | |
159 | ||
160 | struct ir_op; | |
953192ba MD |
161 | |
162 | struct filter_parser_ctx { | |
163 | yyscan_t scanner; | |
164 | struct filter_ast *ast; | |
165 | struct cds_list_head allocated_strings; | |
166 | struct ir_op *ir_root; | |
2b00d462 SM |
167 | struct lttng_bytecode_alloc *bytecode; |
168 | struct lttng_bytecode_alloc *bytecode_reloc; | |
953192ba MD |
169 | }; |
170 | ||
171 | struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input); | |
172 | void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx); | |
173 | int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx); | |
e4d2f27a JR |
174 | int filter_parser_ctx_create_from_filter_expression( |
175 | const char *filter_expression, struct filter_parser_ctx **ctxp); | |
953192ba MD |
176 | |
177 | static inline | |
178 | struct filter_ast *filter_parser_get_ast(struct filter_parser_ctx *parser_ctx) | |
179 | { | |
180 | return parser_ctx->ast; | |
181 | } | |
182 | ||
953192ba MD |
183 | int filter_visitor_print_xml(struct filter_parser_ctx *ctx, FILE *stream, |
184 | int indent); | |
185 | int filter_visitor_ir_generate(struct filter_parser_ctx *ctx); | |
186 | void filter_ir_free(struct filter_parser_ctx *ctx); | |
187 | int filter_visitor_bytecode_generate(struct filter_parser_ctx *ctx); | |
188 | void filter_bytecode_free(struct filter_parser_ctx *ctx); | |
189 | int filter_visitor_ir_check_binary_op_nesting(struct filter_parser_ctx *ctx); | |
190 | int filter_visitor_ir_check_binary_comparator(struct filter_parser_ctx *ctx); | |
dcd5daf2 | 191 | int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx); |
9f449915 PP |
192 | int filter_visitor_ir_normalize_glob_patterns(struct filter_parser_ctx *ctx); |
193 | int filter_visitor_ir_validate_globbing(struct filter_parser_ctx *ctx); | |
953192ba | 194 | |
48a40005 SM |
195 | #if defined(__cplusplus) |
196 | } | |
197 | #endif | |
198 | ||
953192ba | 199 | #endif /* _FILTER_AST_H */ |