| 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 <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 | |
| 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 | |
| 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, |
| 60 | AST_OP_BIT_RSHIFT, |
| 61 | AST_OP_BIT_LSHIFT, |
| 62 | AST_OP_AND, |
| 63 | AST_OP_OR, |
| 64 | AST_OP_BIT_AND, |
| 65 | AST_OP_BIT_OR, |
| 66 | AST_OP_BIT_XOR, |
| 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, |
| 81 | AST_UNARY_BIT_NOT, |
| 82 | }; |
| 83 | |
| 84 | enum ast_link_type { |
| 85 | AST_LINK_UNKNOWN = 0, |
| 86 | AST_LINK_DOT, |
| 87 | AST_LINK_RARROW, |
| 88 | AST_LINK_BRACKET, |
| 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, |
| 110 | AST_EXP_FLOAT_CONSTANT, |
| 111 | AST_EXP_IDENTIFIER, |
| 112 | AST_EXP_GLOBAL_IDENTIFIER, |
| 113 | AST_EXP_NESTED, |
| 114 | } type; |
| 115 | enum ast_link_type post_op; /* reverse */ |
| 116 | enum ast_link_type pre_op; /* forward */ |
| 117 | union { |
| 118 | const char *string; |
| 119 | uint64_t constant; |
| 120 | double float_constant; |
| 121 | const char *identifier; |
| 122 | /* |
| 123 | * child can be nested. |
| 124 | */ |
| 125 | struct filter_node *child; |
| 126 | } u; |
| 127 | /* prev: backward dot/arrow chain (postfix expression) */ |
| 128 | struct filter_node *prev; |
| 129 | /* next: forward dot/arrow chain, generated by a visitor. */ |
| 130 | struct filter_node *next; |
| 131 | /* next_bracket: linked bracket chain (prefix expression) */ |
| 132 | struct filter_node *next_bracket; |
| 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; |
| 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; |
| 160 | struct lttng_filter_bytecode_alloc *bytecode; |
| 161 | struct lttng_filter_bytecode_alloc *bytecode_reloc; |
| 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); |
| 167 | int filter_parser_ctx_create_from_filter_expression( |
| 168 | const char *filter_expression, struct filter_parser_ctx **ctxp); |
| 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 | |
| 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); |
| 184 | int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx); |
| 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); |
| 187 | |
| 188 | #endif /* _FILTER_AST_H */ |