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