| 1 | /* |
| 2 | * ctf-visitor-parent-links.c |
| 3 | * |
| 4 | * Common Trace Format Metadata Parent Link Creator. |
| 5 | * |
| 6 | * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #define BT_LOG_TAG "PLUGIN-CTF-METADATA-PARENT-LINKS-VISITOR" |
| 28 | #include "logging.h" |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <unistd.h> |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <babeltrace/assert-internal.h> |
| 35 | #include <glib.h> |
| 36 | #include <inttypes.h> |
| 37 | #include <errno.h> |
| 38 | #include <babeltrace/babeltrace-internal.h> |
| 39 | #include <babeltrace/list-internal.h> |
| 40 | #include "scanner.h" |
| 41 | #include "parser.h" |
| 42 | #include "ast.h" |
| 43 | |
| 44 | static |
| 45 | int ctf_visitor_unary_expression(int depth, struct ctf_node *node) |
| 46 | { |
| 47 | int ret = 0; |
| 48 | |
| 49 | switch (node->u.unary_expression.link) { |
| 50 | case UNARY_LINK_UNKNOWN: |
| 51 | case UNARY_DOTLINK: |
| 52 | case UNARY_ARROWLINK: |
| 53 | case UNARY_DOTDOTDOT: |
| 54 | break; |
| 55 | default: |
| 56 | _BT_LOGE_LINENO(node->lineno, |
| 57 | "Unknown expression link type: type=%d\n", |
| 58 | node->u.unary_expression.link); |
| 59 | return -EINVAL; |
| 60 | } |
| 61 | |
| 62 | switch (node->u.unary_expression.type) { |
| 63 | case UNARY_STRING: |
| 64 | case UNARY_SIGNED_CONSTANT: |
| 65 | case UNARY_UNSIGNED_CONSTANT: |
| 66 | break; |
| 67 | case UNARY_SBRAC: |
| 68 | node->u.unary_expression.u.sbrac_exp->parent = node; |
| 69 | ret = ctf_visitor_unary_expression(depth + 1, |
| 70 | node->u.unary_expression.u.sbrac_exp); |
| 71 | if (ret) |
| 72 | return ret; |
| 73 | break; |
| 74 | |
| 75 | case UNARY_UNKNOWN: |
| 76 | default: |
| 77 | _BT_LOGE_LINENO(node->lineno, |
| 78 | "Unknown expression link type: type=%d\n", |
| 79 | node->u.unary_expression.link); |
| 80 | return -EINVAL; |
| 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | static |
| 86 | int ctf_visitor_type_specifier(int depth, struct ctf_node *node) |
| 87 | { |
| 88 | int ret; |
| 89 | |
| 90 | switch (node->u.type_specifier.type) { |
| 91 | case TYPESPEC_VOID: |
| 92 | case TYPESPEC_CHAR: |
| 93 | case TYPESPEC_SHORT: |
| 94 | case TYPESPEC_INT: |
| 95 | case TYPESPEC_LONG: |
| 96 | case TYPESPEC_FLOAT: |
| 97 | case TYPESPEC_DOUBLE: |
| 98 | case TYPESPEC_SIGNED: |
| 99 | case TYPESPEC_UNSIGNED: |
| 100 | case TYPESPEC_BOOL: |
| 101 | case TYPESPEC_COMPLEX: |
| 102 | case TYPESPEC_IMAGINARY: |
| 103 | case TYPESPEC_CONST: |
| 104 | case TYPESPEC_ID_TYPE: |
| 105 | break; |
| 106 | case TYPESPEC_FLOATING_POINT: |
| 107 | case TYPESPEC_INTEGER: |
| 108 | case TYPESPEC_STRING: |
| 109 | case TYPESPEC_STRUCT: |
| 110 | case TYPESPEC_VARIANT: |
| 111 | case TYPESPEC_ENUM: |
| 112 | node->u.type_specifier.node->parent = node; |
| 113 | ret = ctf_visitor_parent_links(depth + 1, node->u.type_specifier.node); |
| 114 | if (ret) |
| 115 | return ret; |
| 116 | break; |
| 117 | |
| 118 | case TYPESPEC_UNKNOWN: |
| 119 | default: |
| 120 | _BT_LOGE_LINENO(node->lineno, |
| 121 | "Unknown type specifier: type=%d\n", |
| 122 | node->u.type_specifier.type); |
| 123 | return -EINVAL; |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static |
| 129 | int ctf_visitor_type_declarator(int depth, struct ctf_node *node) |
| 130 | { |
| 131 | int ret = 0; |
| 132 | struct ctf_node *iter; |
| 133 | |
| 134 | depth++; |
| 135 | |
| 136 | bt_list_for_each_entry(iter, &node->u.type_declarator.pointers, |
| 137 | siblings) { |
| 138 | iter->parent = node; |
| 139 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 140 | if (ret) |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | switch (node->u.type_declarator.type) { |
| 145 | case TYPEDEC_ID: |
| 146 | break; |
| 147 | case TYPEDEC_NESTED: |
| 148 | if (node->u.type_declarator.u.nested.type_declarator) { |
| 149 | node->u.type_declarator.u.nested.type_declarator->parent = node; |
| 150 | ret = ctf_visitor_parent_links(depth + 1, |
| 151 | node->u.type_declarator.u.nested.type_declarator); |
| 152 | if (ret) |
| 153 | return ret; |
| 154 | } |
| 155 | if (!node->u.type_declarator.u.nested.abstract_array) { |
| 156 | bt_list_for_each_entry(iter, &node->u.type_declarator.u.nested.length, |
| 157 | siblings) { |
| 158 | iter->parent = node; |
| 159 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 160 | if (ret) |
| 161 | return ret; |
| 162 | } |
| 163 | } |
| 164 | if (node->u.type_declarator.bitfield_len) { |
| 165 | node->u.type_declarator.bitfield_len = node; |
| 166 | ret = ctf_visitor_parent_links(depth + 1, |
| 167 | node->u.type_declarator.bitfield_len); |
| 168 | if (ret) |
| 169 | return ret; |
| 170 | } |
| 171 | break; |
| 172 | case TYPEDEC_UNKNOWN: |
| 173 | default: |
| 174 | _BT_LOGE_LINENO(node->lineno, |
| 175 | "Unknown type declarator: type=%d\n", |
| 176 | node->u.type_declarator.type); |
| 177 | return -EINVAL; |
| 178 | } |
| 179 | depth--; |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | int ctf_visitor_parent_links(int depth, struct ctf_node *node) |
| 184 | { |
| 185 | int ret = 0; |
| 186 | struct ctf_node *iter; |
| 187 | |
| 188 | if (node->visited) |
| 189 | return 0; |
| 190 | |
| 191 | switch (node->type) { |
| 192 | case NODE_ROOT: |
| 193 | bt_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) { |
| 194 | iter->parent = node; |
| 195 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | } |
| 199 | bt_list_for_each_entry(iter, &node->u.root.trace, siblings) { |
| 200 | iter->parent = node; |
| 201 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 202 | if (ret) |
| 203 | return ret; |
| 204 | } |
| 205 | bt_list_for_each_entry(iter, &node->u.root.stream, siblings) { |
| 206 | iter->parent = node; |
| 207 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 208 | if (ret) |
| 209 | return ret; |
| 210 | } |
| 211 | bt_list_for_each_entry(iter, &node->u.root.event, siblings) { |
| 212 | iter->parent = node; |
| 213 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | } |
| 217 | bt_list_for_each_entry(iter, &node->u.root.clock, siblings) { |
| 218 | iter->parent = node; |
| 219 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 220 | if (ret) |
| 221 | return ret; |
| 222 | } |
| 223 | bt_list_for_each_entry(iter, &node->u.root.callsite, siblings) { |
| 224 | iter->parent = node; |
| 225 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 226 | if (ret) |
| 227 | return ret; |
| 228 | } |
| 229 | break; |
| 230 | |
| 231 | case NODE_EVENT: |
| 232 | bt_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) { |
| 233 | iter->parent = node; |
| 234 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 235 | if (ret) |
| 236 | return ret; |
| 237 | } |
| 238 | break; |
| 239 | case NODE_STREAM: |
| 240 | bt_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) { |
| 241 | iter->parent = node; |
| 242 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 243 | if (ret) |
| 244 | return ret; |
| 245 | } |
| 246 | break; |
| 247 | case NODE_ENV: |
| 248 | bt_list_for_each_entry(iter, &node->u.env.declaration_list, siblings) { |
| 249 | iter->parent = node; |
| 250 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 251 | if (ret) |
| 252 | return ret; |
| 253 | } |
| 254 | break; |
| 255 | case NODE_TRACE: |
| 256 | bt_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) { |
| 257 | iter->parent = node; |
| 258 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 259 | if (ret) |
| 260 | return ret; |
| 261 | } |
| 262 | break; |
| 263 | case NODE_CLOCK: |
| 264 | bt_list_for_each_entry(iter, &node->u.clock.declaration_list, siblings) { |
| 265 | iter->parent = node; |
| 266 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 267 | if (ret) |
| 268 | return ret; |
| 269 | } |
| 270 | break; |
| 271 | case NODE_CALLSITE: |
| 272 | bt_list_for_each_entry(iter, &node->u.callsite.declaration_list, siblings) { |
| 273 | iter->parent = node; |
| 274 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 275 | if (ret) |
| 276 | return ret; |
| 277 | } |
| 278 | break; |
| 279 | |
| 280 | case NODE_CTF_EXPRESSION: |
| 281 | depth++; |
| 282 | bt_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) { |
| 283 | iter->parent = node; |
| 284 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 285 | if (ret) |
| 286 | return ret; |
| 287 | } |
| 288 | bt_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) { |
| 289 | iter->parent = node; |
| 290 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 291 | if (ret) |
| 292 | return ret; |
| 293 | } |
| 294 | depth--; |
| 295 | break; |
| 296 | case NODE_UNARY_EXPRESSION: |
| 297 | return ctf_visitor_unary_expression(depth, node); |
| 298 | |
| 299 | case NODE_TYPEDEF: |
| 300 | depth++; |
| 301 | node->u._typedef.type_specifier_list->parent = node; |
| 302 | ret = ctf_visitor_parent_links(depth + 1, node->u._typedef.type_specifier_list); |
| 303 | if (ret) |
| 304 | return ret; |
| 305 | bt_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) { |
| 306 | iter->parent = node; |
| 307 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 308 | if (ret) |
| 309 | return ret; |
| 310 | } |
| 311 | depth--; |
| 312 | break; |
| 313 | case NODE_TYPEALIAS_TARGET: |
| 314 | depth++; |
| 315 | node->u.typealias_target.type_specifier_list->parent = node; |
| 316 | ret = ctf_visitor_parent_links(depth + 1, node->u.typealias_target.type_specifier_list); |
| 317 | if (ret) |
| 318 | return ret; |
| 319 | bt_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) { |
| 320 | iter->parent = node; |
| 321 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 322 | if (ret) |
| 323 | return ret; |
| 324 | } |
| 325 | depth--; |
| 326 | break; |
| 327 | case NODE_TYPEALIAS_ALIAS: |
| 328 | depth++; |
| 329 | node->u.typealias_alias.type_specifier_list->parent = node; |
| 330 | ret = ctf_visitor_parent_links(depth + 1, node->u.typealias_alias.type_specifier_list); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | bt_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) { |
| 334 | iter->parent = node; |
| 335 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 336 | if (ret) |
| 337 | return ret; |
| 338 | } |
| 339 | depth--; |
| 340 | break; |
| 341 | case NODE_TYPEALIAS: |
| 342 | node->u.typealias.target->parent = node; |
| 343 | ret = ctf_visitor_parent_links(depth + 1, node->u.typealias.target); |
| 344 | if (ret) |
| 345 | return ret; |
| 346 | node->u.typealias.alias->parent = node; |
| 347 | ret = ctf_visitor_parent_links(depth + 1, node->u.typealias.alias); |
| 348 | if (ret) |
| 349 | return ret; |
| 350 | break; |
| 351 | |
| 352 | case NODE_TYPE_SPECIFIER_LIST: |
| 353 | bt_list_for_each_entry(iter, &node->u.type_specifier_list.head, siblings) { |
| 354 | iter->parent = node; |
| 355 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 356 | if (ret) |
| 357 | return ret; |
| 358 | } |
| 359 | break; |
| 360 | |
| 361 | case NODE_TYPE_SPECIFIER: |
| 362 | ret = ctf_visitor_type_specifier(depth, node); |
| 363 | if (ret) |
| 364 | return ret; |
| 365 | break; |
| 366 | case NODE_POINTER: |
| 367 | break; |
| 368 | case NODE_TYPE_DECLARATOR: |
| 369 | ret = ctf_visitor_type_declarator(depth, node); |
| 370 | if (ret) |
| 371 | return ret; |
| 372 | break; |
| 373 | |
| 374 | case NODE_FLOATING_POINT: |
| 375 | bt_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) { |
| 376 | iter->parent = node; |
| 377 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 378 | if (ret) |
| 379 | return ret; |
| 380 | } |
| 381 | break; |
| 382 | case NODE_INTEGER: |
| 383 | bt_list_for_each_entry(iter, &node->u.integer.expressions, siblings) { |
| 384 | iter->parent = node; |
| 385 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 386 | if (ret) |
| 387 | return ret; |
| 388 | } |
| 389 | break; |
| 390 | case NODE_STRING: |
| 391 | bt_list_for_each_entry(iter, &node->u.string.expressions, siblings) { |
| 392 | iter->parent = node; |
| 393 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 394 | if (ret) |
| 395 | return ret; |
| 396 | } |
| 397 | break; |
| 398 | case NODE_ENUMERATOR: |
| 399 | bt_list_for_each_entry(iter, &node->u.enumerator.values, siblings) { |
| 400 | iter->parent = node; |
| 401 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 402 | if (ret) |
| 403 | return ret; |
| 404 | } |
| 405 | break; |
| 406 | case NODE_ENUM: |
| 407 | depth++; |
| 408 | if (node->u._enum.container_type) { |
| 409 | ret = ctf_visitor_parent_links(depth + 1, node->u._enum.container_type); |
| 410 | if (ret) |
| 411 | return ret; |
| 412 | } |
| 413 | |
| 414 | bt_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) { |
| 415 | iter->parent = node; |
| 416 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 417 | if (ret) |
| 418 | return ret; |
| 419 | } |
| 420 | depth--; |
| 421 | break; |
| 422 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 423 | node->u.struct_or_variant_declaration.type_specifier_list->parent = node; |
| 424 | ret = ctf_visitor_parent_links(depth + 1, |
| 425 | node->u.struct_or_variant_declaration.type_specifier_list); |
| 426 | if (ret) |
| 427 | return ret; |
| 428 | bt_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) { |
| 429 | iter->parent = node; |
| 430 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 431 | if (ret) |
| 432 | return ret; |
| 433 | } |
| 434 | break; |
| 435 | case NODE_VARIANT: |
| 436 | bt_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) { |
| 437 | iter->parent = node; |
| 438 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 439 | if (ret) |
| 440 | return ret; |
| 441 | } |
| 442 | break; |
| 443 | case NODE_STRUCT: |
| 444 | bt_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) { |
| 445 | iter->parent = node; |
| 446 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 447 | if (ret) |
| 448 | return ret; |
| 449 | } |
| 450 | bt_list_for_each_entry(iter, &node->u._struct.min_align, |
| 451 | siblings) { |
| 452 | iter->parent = node; |
| 453 | ret = ctf_visitor_parent_links(depth + 1, iter); |
| 454 | if (ret) |
| 455 | return ret; |
| 456 | } |
| 457 | break; |
| 458 | |
| 459 | case NODE_UNKNOWN: |
| 460 | default: |
| 461 | _BT_LOGE_LINENO(node->lineno, |
| 462 | "Unknown node type: type=%d\n", node->type); |
| 463 | return -EINVAL; |
| 464 | } |
| 465 | return ret; |
| 466 | } |