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