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