Implement support for brackets in filter expressions
[lttng-tools.git] / src / lib / lttng-ctl / 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 * This library is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License, version 2.1 only,
13 * as published by the Free Software Foundation.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this library; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 /*
26 * Note: filter-ast.h should be included before filter-parser.h.
27 */
28
29 #include <urcu/list.h>
30 #include <stdint.h>
31
32 #define printf_debug(fmt, args...) \
33 do { \
34 if (filter_parser_debug) \
35 fprintf(stdout, "[debug] " fmt, ## args); \
36 } while (0)
37
38 // the parameter name (of the reentrant 'yyparse' function)
39 // data is a pointer to a 'SParserParam' structure
40 //#define YYPARSE_PARAM parser_ctx
41
42 #ifndef YY_TYPEDEF_YY_SCANNER_T
43 #define YY_TYPEDEF_YY_SCANNER_T
44 typedef void* yyscan_t;
45 #endif
46
47 extern int filter_parser_debug;
48
49 struct filter_node;
50 struct filter_parser;
51
52 enum node_type {
53 NODE_UNKNOWN = 0,
54 NODE_ROOT,
55
56 NODE_EXPRESSION,
57 NODE_OP,
58 NODE_UNARY_OP,
59
60 NR_NODE_TYPES,
61 };
62
63 enum op_type {
64 AST_OP_UNKNOWN = 0,
65 AST_OP_MUL,
66 AST_OP_DIV,
67 AST_OP_MOD,
68 AST_OP_PLUS,
69 AST_OP_MINUS,
70 AST_OP_RSHIFT,
71 AST_OP_LSHIFT,
72 AST_OP_AND,
73 AST_OP_OR,
74 AST_OP_BIN_AND,
75 AST_OP_BIN_OR,
76 AST_OP_BIN_XOR,
77
78 AST_OP_EQ,
79 AST_OP_NE,
80 AST_OP_GT,
81 AST_OP_LT,
82 AST_OP_GE,
83 AST_OP_LE,
84 };
85
86 enum unary_op_type {
87 AST_UNARY_UNKNOWN = 0,
88 AST_UNARY_PLUS,
89 AST_UNARY_MINUS,
90 AST_UNARY_NOT,
91 AST_UNARY_BIN_NOT,
92 };
93
94 enum ast_link_type {
95 AST_LINK_UNKNOWN = 0,
96 AST_LINK_DOT,
97 AST_LINK_RARROW,
98 AST_LINK_BRACKET,
99 };
100
101 struct filter_node {
102 /*
103 * Parent node is only set on demand by specific visitor.
104 */
105 struct filter_node *parent;
106 struct cds_list_head gc;
107
108 enum node_type type;
109 union {
110 struct {
111 } unknown;
112 struct {
113 struct filter_node *child;
114 } root;
115 struct {
116 enum {
117 AST_EXP_UNKNOWN = 0,
118 AST_EXP_STRING,
119 AST_EXP_CONSTANT,
120 AST_EXP_FLOAT_CONSTANT,
121 AST_EXP_IDENTIFIER,
122 AST_EXP_GLOBAL_IDENTIFIER,
123 AST_EXP_NESTED,
124 } type;
125 enum ast_link_type post_op; /* reverse */
126 enum ast_link_type pre_op; /* forward */
127 union {
128 char *string;
129 uint64_t constant;
130 double float_constant;
131 char *identifier;
132 /*
133 * child can be nested.
134 */
135 struct filter_node *child;
136 } u;
137 /* prev: linked dot/arrow chain (postfix expression) */
138 struct filter_node *prev;
139 /* next: linked bracket chain (prefix expression) */
140 struct filter_node *next;
141 } expression;
142 struct {
143 enum op_type type;
144 struct filter_node *lchild;
145 struct filter_node *rchild;
146 } op;
147 struct {
148 enum unary_op_type type;
149 struct filter_node *child;
150 } unary_op;
151 } u;
152 };
153
154 struct filter_ast {
155 struct filter_node root;
156 struct cds_list_head allocated_nodes;
157 };
158
159 const char *node_type(struct filter_node *node);
160
161 struct ir_op;
162
163 struct filter_parser_ctx {
164 yyscan_t scanner;
165 struct filter_ast *ast;
166 struct cds_list_head allocated_strings;
167 struct ir_op *ir_root;
168 struct lttng_filter_bytecode_alloc *bytecode;
169 struct lttng_filter_bytecode_alloc *bytecode_reloc;
170 };
171
172 struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input);
173 void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx);
174 int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx);
175
176 static inline
177 struct filter_ast *filter_parser_get_ast(struct filter_parser_ctx *parser_ctx)
178 {
179 return parser_ctx->ast;
180 }
181
182 int filter_visitor_set_parent(struct filter_parser_ctx *ctx);
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);
191 int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx);
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);
194
195 #endif /* _FILTER_AST_H */
This page took 0.042723 seconds and 4 git commands to generate.