| 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 <stdint.h> |
| 20 | #include <stdio.h> |
| 21 | #include <urcu/list.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 | |
| 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 | |
| 33 | #ifndef YY_TYPEDEF_YY_SCANNER_T |
| 34 | #define YY_TYPEDEF_YY_SCANNER_T |
| 35 | using yyscan_t = void *; |
| 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, |
| 61 | AST_OP_BIT_RSHIFT, |
| 62 | AST_OP_BIT_LSHIFT, |
| 63 | AST_OP_AND, |
| 64 | AST_OP_OR, |
| 65 | AST_OP_BIT_AND, |
| 66 | AST_OP_BIT_OR, |
| 67 | AST_OP_BIT_XOR, |
| 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, |
| 82 | AST_UNARY_BIT_NOT, |
| 83 | }; |
| 84 | |
| 85 | enum ast_link_type { |
| 86 | AST_LINK_UNKNOWN = 0, |
| 87 | AST_LINK_DOT, |
| 88 | AST_LINK_RARROW, |
| 89 | AST_LINK_BRACKET, |
| 90 | }; |
| 91 | |
| 92 | enum ast_expt_type { |
| 93 | AST_EXP_UNKNOWN = 0, |
| 94 | AST_EXP_STRING, |
| 95 | AST_EXP_CONSTANT, |
| 96 | AST_EXP_FLOAT_CONSTANT, |
| 97 | AST_EXP_IDENTIFIER, |
| 98 | AST_EXP_GLOBAL_IDENTIFIER, |
| 99 | AST_EXP_NESTED, |
| 100 | }; |
| 101 | |
| 102 | struct filter_node { |
| 103 | /* |
| 104 | * Parent node is only set on demand by specific visitor. |
| 105 | */ |
| 106 | struct filter_node *parent; |
| 107 | struct cds_list_head gc; |
| 108 | |
| 109 | enum node_type type; |
| 110 | union { |
| 111 | struct { |
| 112 | /* Avoid -Wextern-c-compat warning with clang++. */ |
| 113 | char unused; |
| 114 | } unknown; |
| 115 | struct { |
| 116 | struct filter_node *child; |
| 117 | } root; |
| 118 | struct { |
| 119 | enum ast_expt_type type; |
| 120 | enum ast_link_type post_op; /* reverse */ |
| 121 | enum ast_link_type pre_op; /* forward */ |
| 122 | union { |
| 123 | const char *string; |
| 124 | uint64_t constant; |
| 125 | double float_constant; |
| 126 | const char *identifier; |
| 127 | /* |
| 128 | * child can be nested. |
| 129 | */ |
| 130 | struct filter_node *child; |
| 131 | } u; |
| 132 | /* prev: backward dot/arrow chain (postfix expression) */ |
| 133 | struct filter_node *prev; |
| 134 | /* next: forward dot/arrow chain, generated by a visitor. */ |
| 135 | struct filter_node *next; |
| 136 | /* next_bracket: linked bracket chain (prefix expression) */ |
| 137 | struct filter_node *next_bracket; |
| 138 | } expression; |
| 139 | struct { |
| 140 | enum op_type type; |
| 141 | struct filter_node *lchild; |
| 142 | struct filter_node *rchild; |
| 143 | } op; |
| 144 | struct { |
| 145 | enum unary_op_type type; |
| 146 | struct filter_node *child; |
| 147 | } unary_op; |
| 148 | } u; |
| 149 | }; |
| 150 | |
| 151 | struct filter_ast { |
| 152 | struct filter_node root; |
| 153 | struct cds_list_head allocated_nodes; |
| 154 | }; |
| 155 | |
| 156 | const char *node_type(struct filter_node *node); |
| 157 | |
| 158 | struct ir_op; |
| 159 | |
| 160 | struct filter_parser_ctx { |
| 161 | yyscan_t scanner; |
| 162 | struct filter_ast *ast; |
| 163 | struct cds_list_head allocated_strings; |
| 164 | struct ir_op *ir_root; |
| 165 | struct lttng_bytecode_alloc *bytecode; |
| 166 | struct lttng_bytecode_alloc *bytecode_reloc; |
| 167 | }; |
| 168 | |
| 169 | struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input); |
| 170 | void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx); |
| 171 | int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx); |
| 172 | int filter_parser_ctx_create_from_filter_expression(const char *filter_expression, |
| 173 | struct filter_parser_ctx **ctxp); |
| 174 | |
| 175 | static inline struct filter_ast *filter_parser_get_ast(struct filter_parser_ctx *parser_ctx) |
| 176 | { |
| 177 | return parser_ctx->ast; |
| 178 | } |
| 179 | |
| 180 | int filter_visitor_print_xml(struct filter_parser_ctx *ctx, FILE *stream, int indent); |
| 181 | int filter_visitor_ir_generate(struct filter_parser_ctx *ctx); |
| 182 | void filter_ir_free(struct filter_parser_ctx *ctx); |
| 183 | int filter_visitor_bytecode_generate(struct filter_parser_ctx *ctx); |
| 184 | void filter_bytecode_free(struct filter_parser_ctx *ctx); |
| 185 | int filter_visitor_ir_check_binary_op_nesting(struct filter_parser_ctx *ctx); |
| 186 | int filter_visitor_ir_check_binary_comparator(struct filter_parser_ctx *ctx); |
| 187 | int filter_visitor_ir_validate_string(struct filter_parser_ctx *ctx); |
| 188 | int filter_visitor_ir_normalize_glob_patterns(struct filter_parser_ctx *ctx); |
| 189 | int filter_visitor_ir_validate_globbing(struct filter_parser_ctx *ctx); |
| 190 | |
| 191 | #endif /* _FILTER_AST_H */ |