5033b46ea403a2817b794d3d56ebf3460fb062f9
[lttng-tools.git] / src / common / filter / filter-ast.h
1 #ifndef _FILTER_AST_H
2 #define _FILTER_AST_H
3
4 /*
5 * filter-ast.h
6 *
7 * LTTng filter AST
8 *
9 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * SPDX-License-Identifier: LGPL-2.1-only
12 *
13 */
14
15 /*
16 * Note: filter-ast.h should be included before filter-parser.h.
17 */
18
19 #include <urcu/list.h>
20 #include <stdio.h>
21 #include <stdint.h>
22
23 #if defined(__cplusplus)
24 extern "C" {
25 #endif
26
27 #define printf_debug(fmt, args...) \
28 do { \
29 if (filter_parser_debug) \
30 fprintf(stdout, "[debug] " fmt, ## args); \
31 } while (0)
32
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
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,
65 AST_OP_BIT_RSHIFT,
66 AST_OP_BIT_LSHIFT,
67 AST_OP_AND,
68 AST_OP_OR,
69 AST_OP_BIT_AND,
70 AST_OP_BIT_OR,
71 AST_OP_BIT_XOR,
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,
86 AST_UNARY_BIT_NOT,
87 };
88
89 enum ast_link_type {
90 AST_LINK_UNKNOWN = 0,
91 AST_LINK_DOT,
92 AST_LINK_RARROW,
93 AST_LINK_BRACKET,
94 };
95
96 enum ast_expt_type {
97 AST_EXP_UNKNOWN = 0,
98 AST_EXP_STRING,
99 AST_EXP_CONSTANT,
100 AST_EXP_FLOAT_CONSTANT,
101 AST_EXP_IDENTIFIER,
102 AST_EXP_GLOBAL_IDENTIFIER,
103 AST_EXP_NESTED,
104 };
105
106 struct filter_node {
107 /*
108 * Parent node is only set on demand by specific visitor.
109 */
110 struct filter_node *parent;
111 struct cds_list_head gc;
112
113 enum node_type type;
114 union {
115 struct {
116 /* Avoid -Wextern-c-compat warning with clang++. */
117 char unused;
118 } unknown;
119 struct {
120 struct filter_node *child;
121 } root;
122 struct {
123 enum ast_expt_type type;
124 enum ast_link_type post_op; /* reverse */
125 enum ast_link_type pre_op; /* forward */
126 union {
127 const char *string;
128 uint64_t constant;
129 double float_constant;
130 const char *identifier;
131 /*
132 * child can be nested.
133 */
134 struct filter_node *child;
135 } u;
136 /* prev: backward dot/arrow chain (postfix expression) */
137 struct filter_node *prev;
138 /* next: forward dot/arrow chain, generated by a visitor. */
139 struct filter_node *next;
140 /* next_bracket: linked bracket chain (prefix expression) */
141 struct filter_node *next_bracket;
142 } expression;
143 struct {
144 enum op_type type;
145 struct filter_node *lchild;
146 struct filter_node *rchild;
147 } op;
148 struct {
149 enum unary_op_type type;
150 struct filter_node *child;
151 } unary_op;
152 } u;
153 };
154
155 struct filter_ast {
156 struct filter_node root;
157 struct cds_list_head allocated_nodes;
158 };
159
160 const char *node_type(struct filter_node *node);
161
162 struct ir_op;
163
164 struct filter_parser_ctx {
165 yyscan_t scanner;
166 struct filter_ast *ast;
167 struct cds_list_head allocated_strings;
168 struct ir_op *ir_root;
169 struct lttng_bytecode_alloc *bytecode;
170 struct lttng_bytecode_alloc *bytecode_reloc;
171 };
172
173 struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input);
174 void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx);
175 int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx);
176 int filter_parser_ctx_create_from_filter_expression(
177 const char *filter_expression, struct filter_parser_ctx **ctxp);
178
179 static inline
180 struct filter_ast *filter_parser_get_ast(struct filter_parser_ctx *parser_ctx)
181 {
182 return parser_ctx->ast;
183 }
184
185 int filter_visitor_print_xml(struct filter_parser_ctx *ctx, FILE *stream,
186 int indent);
187 int filter_visitor_ir_generate(struct filter_parser_ctx *ctx);
188 void filter_ir_free(struct filter_parser_ctx *ctx);
189 int filter_visitor_bytecode_generate(struct filter_parser_ctx *ctx);
190 void filter_bytecode_free(struct filter_parser_ctx *ctx);
191 int filter_visitor_ir_check_binary_op_nesting(struct filter_parser_ctx *ctx);
192 int filter_visitor_ir_check_binary_comparator(struct filter_parser_ctx *ctx);
193 int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx);
194 int filter_visitor_ir_normalize_glob_patterns(struct filter_parser_ctx *ctx);
195 int filter_visitor_ir_validate_globbing(struct filter_parser_ctx *ctx);
196
197 #if defined(__cplusplus)
198 }
199 #endif
200
201 #endif /* _FILTER_AST_H */
This page took 0.032621 seconds and 3 git commands to generate.