| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef _CTF_AST_H |
| 8 | #define _CTF_AST_H |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | #include <stdio.h> |
| 12 | #include <glib.h> |
| 13 | #include "common/list.h" |
| 14 | #include <babeltrace2/babeltrace.h> |
| 15 | #include "common/macros.h" |
| 16 | #include "common/assert.h" |
| 17 | |
| 18 | #include "decoder.h" |
| 19 | #include "ctf-meta.h" |
| 20 | |
| 21 | // the parameter name (of the reentrant 'yyparse' function) |
| 22 | // data is a pointer to a 'SParserParam' structure |
| 23 | //#define YYPARSE_PARAM scanner |
| 24 | |
| 25 | struct ctf_node; |
| 26 | struct ctf_parser; |
| 27 | struct ctf_visitor_generate_ir; |
| 28 | |
| 29 | #define EINCOMPLETE 1000 |
| 30 | |
| 31 | #define FOREACH_CTF_NODES(F) \ |
| 32 | F(NODE_UNKNOWN) \ |
| 33 | F(NODE_ROOT) \ |
| 34 | F(NODE_ERROR) \ |
| 35 | F(NODE_EVENT) \ |
| 36 | F(NODE_STREAM) \ |
| 37 | F(NODE_ENV) \ |
| 38 | F(NODE_TRACE) \ |
| 39 | F(NODE_CLOCK) \ |
| 40 | F(NODE_CALLSITE) \ |
| 41 | F(NODE_CTF_EXPRESSION) \ |
| 42 | F(NODE_UNARY_EXPRESSION) \ |
| 43 | F(NODE_TYPEDEF) \ |
| 44 | F(NODE_TYPEALIAS_TARGET) \ |
| 45 | F(NODE_TYPEALIAS_ALIAS) \ |
| 46 | F(NODE_TYPEALIAS) \ |
| 47 | F(NODE_TYPE_SPECIFIER) \ |
| 48 | F(NODE_TYPE_SPECIFIER_LIST) \ |
| 49 | F(NODE_POINTER) \ |
| 50 | F(NODE_TYPE_DECLARATOR) \ |
| 51 | F(NODE_FLOATING_POINT) \ |
| 52 | F(NODE_INTEGER) \ |
| 53 | F(NODE_STRING) \ |
| 54 | F(NODE_ENUMERATOR) \ |
| 55 | F(NODE_ENUM) \ |
| 56 | F(NODE_STRUCT_OR_VARIANT_DECLARATION) \ |
| 57 | F(NODE_VARIANT) \ |
| 58 | F(NODE_STRUCT) |
| 59 | |
| 60 | enum node_type { |
| 61 | #define ENTRY(S) S, |
| 62 | FOREACH_CTF_NODES(ENTRY) |
| 63 | #undef ENTRY |
| 64 | NR_NODE_TYPES, |
| 65 | }; |
| 66 | |
| 67 | struct ctf_node { |
| 68 | /* |
| 69 | * Parent node is only set on demand by specific visitor. |
| 70 | */ |
| 71 | struct ctf_node *parent; |
| 72 | struct bt_list_head siblings; |
| 73 | struct bt_list_head tmp_head; |
| 74 | unsigned int lineno; |
| 75 | /* |
| 76 | * We mark nodes visited in the generate-ir phase (last |
| 77 | * phase). We only mark the 1-depth level nodes as visited |
| 78 | * (never the root node, and not their sub-nodes). This allows |
| 79 | * skipping already visited nodes when doing incremental |
| 80 | * metadata append. |
| 81 | */ |
| 82 | int visited; |
| 83 | |
| 84 | enum node_type type; |
| 85 | union { |
| 86 | struct { |
| 87 | } unknown; |
| 88 | struct { |
| 89 | /* |
| 90 | * Children nodes are ctf_expression, field_class_def, |
| 91 | * field_class_alias and field_class_specifier_list. |
| 92 | */ |
| 93 | struct bt_list_head declaration_list; |
| 94 | struct bt_list_head trace; |
| 95 | struct bt_list_head env; |
| 96 | struct bt_list_head stream; |
| 97 | struct bt_list_head event; |
| 98 | struct bt_list_head clock; |
| 99 | struct bt_list_head callsite; |
| 100 | } root; |
| 101 | struct { |
| 102 | /* |
| 103 | * Children nodes are ctf_expression, field_class_def, |
| 104 | * field_class_alias and field_class_specifier_list. |
| 105 | */ |
| 106 | struct bt_list_head declaration_list; |
| 107 | } event; |
| 108 | struct { |
| 109 | /* |
| 110 | * Children nodes are ctf_expression, field_class_def, |
| 111 | * field_class_alias and field_class_specifier_list. |
| 112 | */ |
| 113 | struct bt_list_head declaration_list; |
| 114 | } stream; |
| 115 | struct { |
| 116 | /* |
| 117 | * Children nodes are ctf_expression, field_class_def, |
| 118 | * field_class_alias and field_class_specifier_list. |
| 119 | */ |
| 120 | struct bt_list_head declaration_list; |
| 121 | } env; |
| 122 | struct { |
| 123 | /* |
| 124 | * Children nodes are ctf_expression, field_class_def, |
| 125 | * field_class_alias and field_class_specifier_list. |
| 126 | */ |
| 127 | struct bt_list_head declaration_list; |
| 128 | } trace; |
| 129 | struct { |
| 130 | /* |
| 131 | * Children nodes are ctf_expression, field_class_def, |
| 132 | * field_class_alias and field_class_specifier_list. |
| 133 | */ |
| 134 | struct bt_list_head declaration_list; |
| 135 | } clock; |
| 136 | struct { |
| 137 | /* |
| 138 | * Children nodes are ctf_expression, field_class_def, |
| 139 | * field_class_alias and field_class_specifier_list. |
| 140 | */ |
| 141 | struct bt_list_head declaration_list; |
| 142 | } callsite; |
| 143 | struct { |
| 144 | struct bt_list_head left; /* Should be string */ |
| 145 | struct bt_list_head right; /* Unary exp. or type */ |
| 146 | } ctf_expression; |
| 147 | struct { |
| 148 | enum { |
| 149 | UNARY_UNKNOWN = 0, |
| 150 | UNARY_STRING, |
| 151 | UNARY_SIGNED_CONSTANT, |
| 152 | UNARY_UNSIGNED_CONSTANT, |
| 153 | UNARY_SBRAC, |
| 154 | } type; |
| 155 | union { |
| 156 | /* |
| 157 | * string for identifier, id_type, keywords, |
| 158 | * string literals and character constants. |
| 159 | */ |
| 160 | char *string; |
| 161 | int64_t signed_constant; |
| 162 | uint64_t unsigned_constant; |
| 163 | struct ctf_node *sbrac_exp; |
| 164 | } u; |
| 165 | enum { |
| 166 | UNARY_LINK_UNKNOWN = 0, |
| 167 | UNARY_DOTLINK, |
| 168 | UNARY_ARROWLINK, |
| 169 | UNARY_DOTDOTDOT, |
| 170 | } link; |
| 171 | } unary_expression; |
| 172 | struct { |
| 173 | struct ctf_node *field_class_specifier_list; |
| 174 | struct bt_list_head field_class_declarators; |
| 175 | } field_class_def; |
| 176 | /* new type is "alias", existing type "target" */ |
| 177 | struct { |
| 178 | struct ctf_node *field_class_specifier_list; |
| 179 | struct bt_list_head field_class_declarators; |
| 180 | } field_class_alias_target; |
| 181 | struct { |
| 182 | struct ctf_node *field_class_specifier_list; |
| 183 | struct bt_list_head field_class_declarators; |
| 184 | } field_class_alias_name; |
| 185 | struct { |
| 186 | struct ctf_node *target; |
| 187 | struct ctf_node *alias; |
| 188 | } field_class_alias; |
| 189 | struct { |
| 190 | enum { |
| 191 | TYPESPEC_UNKNOWN = 0, |
| 192 | TYPESPEC_VOID, |
| 193 | TYPESPEC_CHAR, |
| 194 | TYPESPEC_SHORT, |
| 195 | TYPESPEC_INT, |
| 196 | TYPESPEC_LONG, |
| 197 | TYPESPEC_FLOAT, |
| 198 | TYPESPEC_DOUBLE, |
| 199 | TYPESPEC_SIGNED, |
| 200 | TYPESPEC_UNSIGNED, |
| 201 | TYPESPEC_BOOL, |
| 202 | TYPESPEC_COMPLEX, |
| 203 | TYPESPEC_IMAGINARY, |
| 204 | TYPESPEC_CONST, |
| 205 | TYPESPEC_ID_TYPE, |
| 206 | TYPESPEC_FLOATING_POINT, |
| 207 | TYPESPEC_INTEGER, |
| 208 | TYPESPEC_STRING, |
| 209 | TYPESPEC_STRUCT, |
| 210 | TYPESPEC_VARIANT, |
| 211 | TYPESPEC_ENUM, |
| 212 | } type; |
| 213 | /* For struct, variant and enum */ |
| 214 | struct ctf_node *node; |
| 215 | const char *id_type; |
| 216 | } field_class_specifier; |
| 217 | struct { |
| 218 | /* list of field_class_specifier */ |
| 219 | struct bt_list_head head; |
| 220 | } field_class_specifier_list; |
| 221 | struct { |
| 222 | unsigned int const_qualifier; |
| 223 | } pointer; |
| 224 | struct { |
| 225 | struct bt_list_head pointers; |
| 226 | enum { |
| 227 | TYPEDEC_UNKNOWN = 0, |
| 228 | TYPEDEC_ID, /* identifier */ |
| 229 | TYPEDEC_NESTED, /* (), array or sequence */ |
| 230 | } type; |
| 231 | union { |
| 232 | char *id; |
| 233 | struct { |
| 234 | /* typedec has no pointer list */ |
| 235 | struct ctf_node *field_class_declarator; |
| 236 | /* |
| 237 | * unary expression (value) or |
| 238 | * field_class_specifier_list. |
| 239 | */ |
| 240 | struct bt_list_head length; |
| 241 | /* for abstract type declarator */ |
| 242 | unsigned int abstract_array; |
| 243 | } nested; |
| 244 | } u; |
| 245 | struct ctf_node *bitfield_len; |
| 246 | } field_class_declarator; |
| 247 | struct { |
| 248 | /* Children nodes are ctf_expression. */ |
| 249 | struct bt_list_head expressions; |
| 250 | } floating_point; |
| 251 | struct { |
| 252 | /* Children nodes are ctf_expression. */ |
| 253 | struct bt_list_head expressions; |
| 254 | } integer; |
| 255 | struct { |
| 256 | /* Children nodes are ctf_expression. */ |
| 257 | struct bt_list_head expressions; |
| 258 | } string; |
| 259 | struct { |
| 260 | char *id; |
| 261 | /* |
| 262 | * Range list or single value node. Contains unary |
| 263 | * expressions. |
| 264 | */ |
| 265 | struct bt_list_head values; |
| 266 | } enumerator; |
| 267 | struct { |
| 268 | char *enum_id; |
| 269 | /* |
| 270 | * Either NULL, or points to unary expression or |
| 271 | * field_class_specifier_list. |
| 272 | */ |
| 273 | struct ctf_node *container_field_class; |
| 274 | struct bt_list_head enumerator_list; |
| 275 | int has_body; |
| 276 | } _enum; |
| 277 | struct { |
| 278 | struct ctf_node *field_class_specifier_list; |
| 279 | struct bt_list_head field_class_declarators; |
| 280 | } struct_or_variant_declaration; |
| 281 | struct { |
| 282 | char *name; |
| 283 | char *choice; |
| 284 | /* |
| 285 | * list of field_class_def, field_class_alias and |
| 286 | * declarations |
| 287 | */ |
| 288 | struct bt_list_head declaration_list; |
| 289 | int has_body; |
| 290 | } variant; |
| 291 | struct { |
| 292 | char *name; |
| 293 | /* |
| 294 | * list of field_class_def, field_class_alias and |
| 295 | * declarations |
| 296 | */ |
| 297 | struct bt_list_head declaration_list; |
| 298 | int has_body; |
| 299 | struct bt_list_head min_align; /* align() attribute */ |
| 300 | } _struct; |
| 301 | } u; |
| 302 | }; |
| 303 | |
| 304 | struct ctf_ast { |
| 305 | struct ctf_node root; |
| 306 | }; |
| 307 | |
| 308 | const char *node_type(struct ctf_node *node); |
| 309 | |
| 310 | struct meta_log_config; |
| 311 | |
| 312 | BT_HIDDEN |
| 313 | struct ctf_visitor_generate_ir *ctf_visitor_generate_ir_create( |
| 314 | const struct ctf_metadata_decoder_config *config); |
| 315 | |
| 316 | void ctf_visitor_generate_ir_destroy(struct ctf_visitor_generate_ir *visitor); |
| 317 | |
| 318 | BT_HIDDEN |
| 319 | bt_trace_class *ctf_visitor_generate_ir_get_ir_trace_class( |
| 320 | struct ctf_visitor_generate_ir *visitor); |
| 321 | |
| 322 | BT_HIDDEN |
| 323 | struct ctf_trace_class *ctf_visitor_generate_ir_borrow_ctf_trace_class( |
| 324 | struct ctf_visitor_generate_ir *visitor); |
| 325 | |
| 326 | BT_HIDDEN |
| 327 | int ctf_visitor_generate_ir_visit_node(struct ctf_visitor_generate_ir *visitor, |
| 328 | struct ctf_node *node); |
| 329 | |
| 330 | BT_HIDDEN |
| 331 | int ctf_visitor_semantic_check(int depth, struct ctf_node *node, |
| 332 | struct meta_log_config *log_cfg); |
| 333 | |
| 334 | BT_HIDDEN |
| 335 | int ctf_visitor_parent_links(int depth, struct ctf_node *node, |
| 336 | struct meta_log_config *log_cfg); |
| 337 | |
| 338 | static inline |
| 339 | char *ctf_ast_concatenate_unary_strings(struct bt_list_head *head) |
| 340 | { |
| 341 | int i = 0; |
| 342 | GString *str; |
| 343 | struct ctf_node *node; |
| 344 | |
| 345 | str = g_string_new(NULL); |
| 346 | BT_ASSERT(str); |
| 347 | |
| 348 | bt_list_for_each_entry(node, head, siblings) { |
| 349 | char *src_string; |
| 350 | |
| 351 | if ( |
| 352 | node->type != NODE_UNARY_EXPRESSION || |
| 353 | node->u.unary_expression.type != UNARY_STRING || |
| 354 | !( |
| 355 | ( |
| 356 | node->u.unary_expression.link != |
| 357 | UNARY_LINK_UNKNOWN |
| 358 | ) ^ (i == 0) |
| 359 | ) |
| 360 | ) { |
| 361 | goto error; |
| 362 | } |
| 363 | |
| 364 | switch (node->u.unary_expression.link) { |
| 365 | case UNARY_DOTLINK: |
| 366 | g_string_append(str, "."); |
| 367 | break; |
| 368 | case UNARY_ARROWLINK: |
| 369 | g_string_append(str, "->"); |
| 370 | break; |
| 371 | case UNARY_DOTDOTDOT: |
| 372 | g_string_append(str, "..."); |
| 373 | break; |
| 374 | default: |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | src_string = node->u.unary_expression.u.string; |
| 379 | g_string_append(str, src_string); |
| 380 | i++; |
| 381 | } |
| 382 | |
| 383 | /* Destroys the container, returns the underlying string */ |
| 384 | return g_string_free(str, FALSE); |
| 385 | |
| 386 | error: |
| 387 | /* This always returns NULL */ |
| 388 | return g_string_free(str, TRUE); |
| 389 | } |
| 390 | |
| 391 | static inline |
| 392 | int ctf_ast_get_unary_uuid(struct bt_list_head *head, |
| 393 | bt_uuid_t uuid, int log_level, bt_self_component *self_comp) |
| 394 | { |
| 395 | int i = 0; |
| 396 | int ret = 0; |
| 397 | struct ctf_node *node; |
| 398 | |
| 399 | bt_list_for_each_entry(node, head, siblings) { |
| 400 | int uexpr_type = node->u.unary_expression.type; |
| 401 | int uexpr_link = node->u.unary_expression.link; |
| 402 | const char *src_string; |
| 403 | |
| 404 | if (node->type != NODE_UNARY_EXPRESSION || |
| 405 | uexpr_type != UNARY_STRING || |
| 406 | uexpr_link != UNARY_LINK_UNKNOWN || |
| 407 | i != 0) { |
| 408 | ret = -EINVAL; |
| 409 | goto end; |
| 410 | } |
| 411 | |
| 412 | src_string = node->u.unary_expression.u.string; |
| 413 | ret = bt_uuid_from_str(src_string, uuid); |
| 414 | if (ret) { |
| 415 | #ifdef BT_COMP_LOG_CUR_LVL |
| 416 | BT_COMP_LOG_CUR_LVL(BT_LOG_ERROR, log_level, |
| 417 | self_comp, |
| 418 | "Cannot parse UUID: uuid=\"%s\"", src_string); |
| 419 | #endif |
| 420 | goto end; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | end: |
| 425 | return ret; |
| 426 | } |
| 427 | |
| 428 | #endif /* _CTF_AST_H */ |