| 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 | |
| 19 | #include <stdio.h> |
| 20 | #include <unistd.h> |
| 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <assert.h> |
| 24 | #include <glib.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <errno.h> |
| 27 | #include <babeltrace/babeltrace-internal.h> |
| 28 | #include <babeltrace/list.h> |
| 29 | #include "ctf-scanner.h" |
| 30 | #include "ctf-parser.h" |
| 31 | #include "ctf-ast.h" |
| 32 | |
| 33 | #define fprintf_dbg(fd, fmt, args...) fprintf(fd, "%s: " fmt, __func__, ## args) |
| 34 | |
| 35 | static |
| 36 | int ctf_visitor_unary_expression(FILE *fd, int depth, struct ctf_node *node) |
| 37 | { |
| 38 | int ret = 0; |
| 39 | |
| 40 | switch (node->u.unary_expression.link) { |
| 41 | case UNARY_LINK_UNKNOWN: |
| 42 | case UNARY_DOTLINK: |
| 43 | case UNARY_ARROWLINK: |
| 44 | case UNARY_DOTDOTDOT: |
| 45 | break; |
| 46 | default: |
| 47 | fprintf(fd, "[error] %s: unknown expression link type %d\n", __func__, |
| 48 | (int) node->u.unary_expression.link); |
| 49 | return -EINVAL; |
| 50 | } |
| 51 | |
| 52 | switch (node->u.unary_expression.type) { |
| 53 | case UNARY_STRING: |
| 54 | case UNARY_SIGNED_CONSTANT: |
| 55 | case UNARY_UNSIGNED_CONSTANT: |
| 56 | break; |
| 57 | case UNARY_SBRAC: |
| 58 | node->u.unary_expression.u.sbrac_exp->parent = node; |
| 59 | ret = ctf_visitor_unary_expression(fd, depth + 1, |
| 60 | node->u.unary_expression.u.sbrac_exp); |
| 61 | if (ret) |
| 62 | return ret; |
| 63 | break; |
| 64 | case UNARY_NESTED: |
| 65 | node->u.unary_expression.u.nested_exp->parent = node; |
| 66 | ret = ctf_visitor_unary_expression(fd, depth + 1, |
| 67 | node->u.unary_expression.u.nested_exp); |
| 68 | if (ret) |
| 69 | return ret; |
| 70 | break; |
| 71 | |
| 72 | case UNARY_UNKNOWN: |
| 73 | default: |
| 74 | fprintf(fd, "[error] %s: unknown expression type %d\n", __func__, |
| 75 | (int) node->u.unary_expression.type); |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static |
| 82 | int ctf_visitor_type_specifier(FILE *fd, int depth, struct ctf_node *node) |
| 83 | { |
| 84 | int ret; |
| 85 | |
| 86 | switch (node->u.type_specifier.type) { |
| 87 | case TYPESPEC_VOID: |
| 88 | case TYPESPEC_CHAR: |
| 89 | case TYPESPEC_SHORT: |
| 90 | case TYPESPEC_INT: |
| 91 | case TYPESPEC_LONG: |
| 92 | case TYPESPEC_FLOAT: |
| 93 | case TYPESPEC_DOUBLE: |
| 94 | case TYPESPEC_SIGNED: |
| 95 | case TYPESPEC_UNSIGNED: |
| 96 | case TYPESPEC_BOOL: |
| 97 | case TYPESPEC_COMPLEX: |
| 98 | case TYPESPEC_IMAGINARY: |
| 99 | case TYPESPEC_CONST: |
| 100 | case TYPESPEC_ID_TYPE: |
| 101 | break; |
| 102 | case TYPESPEC_FLOATING_POINT: |
| 103 | case TYPESPEC_INTEGER: |
| 104 | case TYPESPEC_STRING: |
| 105 | case TYPESPEC_STRUCT: |
| 106 | case TYPESPEC_VARIANT: |
| 107 | case TYPESPEC_ENUM: |
| 108 | node->u.type_specifier.node->parent = node; |
| 109 | ret = ctf_visitor_parent_links(fd, depth + 1, node->u.type_specifier.node); |
| 110 | if (ret) |
| 111 | return ret; |
| 112 | break; |
| 113 | |
| 114 | case TYPESPEC_UNKNOWN: |
| 115 | default: |
| 116 | fprintf(fd, "[error] %s: unknown type specifier %d\n", __func__, |
| 117 | (int) node->u.type_specifier.type); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static |
| 124 | int ctf_visitor_type_declarator(FILE *fd, int depth, struct ctf_node *node) |
| 125 | { |
| 126 | int ret = 0; |
| 127 | struct ctf_node *iter; |
| 128 | |
| 129 | depth++; |
| 130 | |
| 131 | cds_list_for_each_entry(iter, &node->u.type_declarator.pointers, |
| 132 | siblings) { |
| 133 | iter->parent = node; |
| 134 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | switch (node->u.type_declarator.type) { |
| 140 | case TYPEDEC_ID: |
| 141 | break; |
| 142 | case TYPEDEC_NESTED: |
| 143 | if (node->u.type_declarator.u.nested.type_declarator) { |
| 144 | node->u.type_declarator.u.nested.type_declarator->parent = node; |
| 145 | ret = ctf_visitor_parent_links(fd, depth + 1, |
| 146 | node->u.type_declarator.u.nested.type_declarator); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | } |
| 150 | if (!node->u.type_declarator.u.nested.abstract_array) { |
| 151 | cds_list_for_each_entry(iter, &node->u.type_declarator.u.nested.length, |
| 152 | siblings) { |
| 153 | iter->parent = node; |
| 154 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 155 | if (ret) |
| 156 | return ret; |
| 157 | } |
| 158 | } |
| 159 | if (node->u.type_declarator.bitfield_len) { |
| 160 | node->u.type_declarator.bitfield_len = node; |
| 161 | ret = ctf_visitor_parent_links(fd, depth + 1, |
| 162 | node->u.type_declarator.bitfield_len); |
| 163 | if (ret) |
| 164 | return ret; |
| 165 | } |
| 166 | break; |
| 167 | case TYPEDEC_UNKNOWN: |
| 168 | default: |
| 169 | fprintf(fd, "[error] %s: unknown type declarator %d\n", __func__, |
| 170 | (int) node->u.type_declarator.type); |
| 171 | return -EINVAL; |
| 172 | } |
| 173 | depth--; |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | int ctf_visitor_parent_links(FILE *fd, int depth, struct ctf_node *node) |
| 178 | { |
| 179 | int ret = 0; |
| 180 | struct ctf_node *iter; |
| 181 | |
| 182 | switch (node->type) { |
| 183 | case NODE_ROOT: |
| 184 | cds_list_for_each_entry(iter, &node->u.root.declaration_list, siblings) { |
| 185 | iter->parent = node; |
| 186 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 187 | if (ret) |
| 188 | return ret; |
| 189 | } |
| 190 | cds_list_for_each_entry(iter, &node->u.root.trace, siblings) { |
| 191 | iter->parent = node; |
| 192 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | } |
| 196 | cds_list_for_each_entry(iter, &node->u.root.stream, siblings) { |
| 197 | iter->parent = node; |
| 198 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 199 | if (ret) |
| 200 | return ret; |
| 201 | } |
| 202 | cds_list_for_each_entry(iter, &node->u.root.event, siblings) { |
| 203 | iter->parent = node; |
| 204 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | } |
| 208 | break; |
| 209 | |
| 210 | case NODE_EVENT: |
| 211 | cds_list_for_each_entry(iter, &node->u.event.declaration_list, siblings) { |
| 212 | iter->parent = node; |
| 213 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | } |
| 217 | break; |
| 218 | case NODE_STREAM: |
| 219 | cds_list_for_each_entry(iter, &node->u.stream.declaration_list, siblings) { |
| 220 | iter->parent = node; |
| 221 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 222 | if (ret) |
| 223 | return ret; |
| 224 | } |
| 225 | break; |
| 226 | case NODE_TRACE: |
| 227 | cds_list_for_each_entry(iter, &node->u.trace.declaration_list, siblings) { |
| 228 | iter->parent = node; |
| 229 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 230 | if (ret) |
| 231 | return ret; |
| 232 | } |
| 233 | break; |
| 234 | |
| 235 | case NODE_CTF_EXPRESSION: |
| 236 | depth++; |
| 237 | cds_list_for_each_entry(iter, &node->u.ctf_expression.left, siblings) { |
| 238 | iter->parent = node; |
| 239 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 240 | if (ret) |
| 241 | return ret; |
| 242 | } |
| 243 | cds_list_for_each_entry(iter, &node->u.ctf_expression.right, siblings) { |
| 244 | iter->parent = node; |
| 245 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | } |
| 249 | depth--; |
| 250 | break; |
| 251 | case NODE_UNARY_EXPRESSION: |
| 252 | return ctf_visitor_unary_expression(fd, depth, node); |
| 253 | |
| 254 | case NODE_TYPEDEF: |
| 255 | depth++; |
| 256 | node->u._typedef.type_specifier_list->parent = node; |
| 257 | ret = ctf_visitor_parent_links(fd, depth + 1, node->u._typedef.type_specifier_list); |
| 258 | if (ret) |
| 259 | return ret; |
| 260 | cds_list_for_each_entry(iter, &node->u._typedef.type_declarators, siblings) { |
| 261 | iter->parent = node; |
| 262 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 263 | if (ret) |
| 264 | return ret; |
| 265 | } |
| 266 | depth--; |
| 267 | break; |
| 268 | case NODE_TYPEALIAS_TARGET: |
| 269 | depth++; |
| 270 | node->u.typealias_target.type_specifier_list->parent = node; |
| 271 | ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_target.type_specifier_list); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | cds_list_for_each_entry(iter, &node->u.typealias_target.type_declarators, siblings) { |
| 275 | iter->parent = node; |
| 276 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 277 | if (ret) |
| 278 | return ret; |
| 279 | } |
| 280 | depth--; |
| 281 | break; |
| 282 | case NODE_TYPEALIAS_ALIAS: |
| 283 | depth++; |
| 284 | node->u.typealias_alias.type_specifier_list->parent = node; |
| 285 | ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias_alias.type_specifier_list); |
| 286 | if (ret) |
| 287 | return ret; |
| 288 | cds_list_for_each_entry(iter, &node->u.typealias_alias.type_declarators, siblings) { |
| 289 | iter->parent = node; |
| 290 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 291 | if (ret) |
| 292 | return ret; |
| 293 | } |
| 294 | depth--; |
| 295 | break; |
| 296 | case NODE_TYPEALIAS: |
| 297 | node->u.typealias.target->parent = node; |
| 298 | ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.target); |
| 299 | if (ret) |
| 300 | return ret; |
| 301 | node->u.typealias.alias->parent = node; |
| 302 | ret = ctf_visitor_parent_links(fd, depth + 1, node->u.typealias.alias); |
| 303 | if (ret) |
| 304 | return ret; |
| 305 | break; |
| 306 | |
| 307 | case NODE_TYPE_SPECIFIER_LIST: |
| 308 | cds_list_for_each_entry(iter, &node->u.type_specifier_list.head, siblings) { |
| 309 | iter->parent = node; |
| 310 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | } |
| 314 | break; |
| 315 | |
| 316 | case NODE_TYPE_SPECIFIER: |
| 317 | ret = ctf_visitor_type_specifier(fd, depth, node); |
| 318 | if (ret) |
| 319 | return ret; |
| 320 | break; |
| 321 | case NODE_POINTER: |
| 322 | break; |
| 323 | case NODE_TYPE_DECLARATOR: |
| 324 | ret = ctf_visitor_type_declarator(fd, depth, node); |
| 325 | if (ret) |
| 326 | return ret; |
| 327 | break; |
| 328 | |
| 329 | case NODE_FLOATING_POINT: |
| 330 | cds_list_for_each_entry(iter, &node->u.floating_point.expressions, siblings) { |
| 331 | iter->parent = node; |
| 332 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 333 | if (ret) |
| 334 | return ret; |
| 335 | } |
| 336 | break; |
| 337 | case NODE_INTEGER: |
| 338 | cds_list_for_each_entry(iter, &node->u.integer.expressions, siblings) { |
| 339 | iter->parent = node; |
| 340 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 341 | if (ret) |
| 342 | return ret; |
| 343 | } |
| 344 | break; |
| 345 | case NODE_STRING: |
| 346 | cds_list_for_each_entry(iter, &node->u.string.expressions, siblings) { |
| 347 | iter->parent = node; |
| 348 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 349 | if (ret) |
| 350 | return ret; |
| 351 | } |
| 352 | break; |
| 353 | case NODE_ENUMERATOR: |
| 354 | cds_list_for_each_entry(iter, &node->u.enumerator.values, siblings) { |
| 355 | iter->parent = node; |
| 356 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 357 | if (ret) |
| 358 | return ret; |
| 359 | } |
| 360 | break; |
| 361 | case NODE_ENUM: |
| 362 | depth++; |
| 363 | if (node->u._enum.container_type) { |
| 364 | ret = ctf_visitor_parent_links(fd, depth + 1, node->u._enum.container_type); |
| 365 | if (ret) |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | cds_list_for_each_entry(iter, &node->u._enum.enumerator_list, siblings) { |
| 370 | iter->parent = node; |
| 371 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 372 | if (ret) |
| 373 | return ret; |
| 374 | } |
| 375 | depth--; |
| 376 | break; |
| 377 | case NODE_STRUCT_OR_VARIANT_DECLARATION: |
| 378 | node->u.struct_or_variant_declaration.type_specifier_list->parent = node; |
| 379 | ret = ctf_visitor_parent_links(fd, depth + 1, |
| 380 | node->u.struct_or_variant_declaration.type_specifier_list); |
| 381 | if (ret) |
| 382 | return ret; |
| 383 | cds_list_for_each_entry(iter, &node->u.struct_or_variant_declaration.type_declarators, siblings) { |
| 384 | iter->parent = node; |
| 385 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 386 | if (ret) |
| 387 | return ret; |
| 388 | } |
| 389 | break; |
| 390 | case NODE_VARIANT: |
| 391 | cds_list_for_each_entry(iter, &node->u.variant.declaration_list, siblings) { |
| 392 | iter->parent = node; |
| 393 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 394 | if (ret) |
| 395 | return ret; |
| 396 | } |
| 397 | break; |
| 398 | case NODE_STRUCT: |
| 399 | cds_list_for_each_entry(iter, &node->u._struct.declaration_list, siblings) { |
| 400 | iter->parent = node; |
| 401 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 402 | if (ret) |
| 403 | return ret; |
| 404 | } |
| 405 | cds_list_for_each_entry(iter, &node->u._struct.min_align, |
| 406 | siblings) { |
| 407 | iter->parent = node; |
| 408 | ret = ctf_visitor_parent_links(fd, depth + 1, iter); |
| 409 | if (ret) |
| 410 | return ret; |
| 411 | } |
| 412 | break; |
| 413 | |
| 414 | case NODE_UNKNOWN: |
| 415 | default: |
| 416 | fprintf(fd, "[error] %s: unknown node type %d\n", __func__, |
| 417 | (int) node->type); |
| 418 | return -EINVAL; |
| 419 | } |
| 420 | return ret; |
| 421 | } |