5 * Common Trace Format Metadata Grammar.
13 #include <helpers/list.h>
15 #include "ctf-parser.h"
18 #define printf_dbg(fmt, args...) fprintf(stderr, "%s: " fmt, __func__, args)
19 #define printf_dbg_noarg(fmt) fprintf(stderr, "%s: " fmt, __func__)
24 static CDS_LIST_HEAD(allocated_strings);
34 struct cds_list_head gc;
38 struct scope root_scope;
39 struct scope *cs = &root_scope; /* current scope */
41 char *strredup(char **dest, const char *src)
43 size_t len = strlen(src) + 1;
45 *dest = realloc(*dest, len);
52 static struct gc_string *gc_string_alloc(size_t len)
54 struct gc_string *gstr;
56 gstr = malloc(sizeof(*gstr) + len);
57 cds_list_add(&gstr->gc, &allocated_strings);
61 void setstring(const char *src)
63 yylval.gs = gc_string_alloc(strlen(src) + 1);
64 strcpy(yylval.gs->s, src);
67 static void init_scope(struct scope *scope, struct scope *parent)
69 scope->parent = parent;
70 scope->types = g_hash_table_new_full(g_str_hash, g_str_equal,
71 (GDestroyNotify) free, NULL);
74 static void finalize_scope(struct scope *scope)
76 g_hash_table_destroy(scope->types);
79 static void push_scope(void)
83 printf_dbg_noarg("push scope\n");
84 ns = malloc(sizeof(struct scope));
89 static void pop_scope(void)
93 printf_dbg_noarg("pop scope\n");
100 int lookup_type(struct scope *s, const char *id)
104 ret = (int) g_hash_table_lookup(s->types, id);
105 printf_dbg("lookup %p %s %d\n", s, id, ret);
109 int is_type(const char *id)
114 for (it = cs; it != NULL; it = it->parent) {
115 if (lookup_type(it, id)) {
120 printf_dbg("is type %s %d\n", id, ret);
124 static void add_type(const char *id)
126 char *type_id = NULL;
128 printf_dbg("add type %s\n", id);
129 if (lookup_type(cs, id))
131 strredup(&type_id, id);
132 g_hash_table_insert(cs->types, type_id, type_id);
135 void yyerror(const char *str)
137 fprintf(stderr, "error %s\n", str);
145 static void free_strings(void)
147 struct gc_string *gstr, *tmp;
149 cds_list_for_each_entry_safe(gstr, tmp, &allocated_strings, gc)
153 int main(int argc, char **argv)
156 init_scope(&root_scope, NULL);
158 finalize_scope(&root_scope);
166 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE ESCSEQ CHAR_STRING_TOKEN LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW STAR PLUS MINUS LT GT TYPEASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA CONST CHAR DOUBLE ENUM EVENT FLOATING_POINT FLOAT INTEGER INT LONG SHORT SIGNED STREAM STRING STRUCT TRACE TYPEALIAS TYPEDEF UNSIGNED VARIANT VOID _BOOL _COMPLEX _IMAGINARY DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT
167 %token <gs> IDENTIFIER ID_TYPE
173 struct gc_string *gs;
213 | c_char_sequence c_char
221 /* 1.6 String literals */
225 | s_char_sequence s_char
233 /* 2: Phrase structure grammar */
241 | HEXADECIMAL_CONSTANT
242 | STRING_LITERAL_START DQUOTE
243 | STRING_LITERAL_START s_char_sequence DQUOTE
244 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
245 | LPAREN unary_expression RPAREN
246 | postfix_expression LSBRAC unary_expression RSBRAC
247 | postfix_expression DOT IDENTIFIER
248 | postfix_expression DOT ID_TYPE
249 | postfix_expression RARROW IDENTIFIER
250 | postfix_expression RARROW ID_TYPE
255 | PLUS postfix_expression
256 | MINUS postfix_expression
259 unary_expression_or_range:
260 unary_expression DOTDOTDOT unary_expression
264 /* 2.2: Declarations */
267 declaration_specifiers SEMICOLON
271 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
272 | TYPEDEF declaration_specifiers type_declarator_list SEMICOLON
273 | declaration_specifiers TYPEDEF type_declarator_list SEMICOLON
274 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON declaration_specifiers abstract_type_declarator_list SEMICOLON
275 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON type_declarator_list SEMICOLON
279 event_declaration_begin event_declaration_end
280 | event_declaration_begin ctf_assignment_expression_list event_declaration_end
283 event_declaration_begin:
290 event_declaration_end:
299 stream_declaration_begin stream_declaration_end
300 | stream_declaration_begin ctf_assignment_expression_list stream_declaration_end
303 stream_declaration_begin:
310 stream_declaration_end:
319 trace_declaration_begin trace_declaration_end
320 | trace_declaration_begin ctf_assignment_expression_list trace_declaration_end
323 trace_declaration_begin:
330 trace_declaration_end:
337 declaration_specifiers:
340 | declaration_specifiers CONST
341 | declaration_specifiers type_specifier
344 type_declarator_list:
346 | type_declarator_list COMMA type_declarator
349 abstract_type_declarator_list:
350 abstract_type_declarator
351 | abstract_type_declarator_list COMMA abstract_type_declarator
367 | FLOATING_POINT LBRAC RBRAC
368 | FLOATING_POINT LBRAC ctf_assignment_expression_list RBRAC
369 | INTEGER LBRAC RBRAC
370 | INTEGER LBRAC ctf_assignment_expression_list RBRAC
372 | STRING LBRAC ctf_assignment_expression_list RBRAC
373 | ENUM enum_type_specifier
374 | VARIANT variant_type_specifier
375 | STRUCT struct_type_specifier
378 struct_type_specifier:
379 struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
380 | IDENTIFIER struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
381 | ID_TYPE struct_declaration_begin struct_or_variant_declaration_list struct_declaration_end
386 struct_declaration_begin:
393 struct_declaration_end:
400 variant_type_specifier:
401 variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
402 | LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
403 | LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
404 | IDENTIFIER variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
405 | IDENTIFIER LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
406 | IDENTIFIER LT IDENTIFIER GT
407 | IDENTIFIER LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
408 | IDENTIFIER LT ID_TYPE GT
409 | ID_TYPE variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
410 | ID_TYPE LT IDENTIFIER GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
411 | ID_TYPE LT IDENTIFIER GT
412 | ID_TYPE LT ID_TYPE GT variant_declaration_begin struct_or_variant_declaration_list variant_declaration_end
413 | ID_TYPE LT ID_TYPE GT
416 variant_declaration_begin:
423 variant_declaration_end:
430 type_specifier_or_integer_constant:
431 declaration_specifiers
434 | HEXADECIMAL_CONSTANT
438 LBRAC enumerator_list RBRAC
439 | LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
440 | IDENTIFIER LBRAC enumerator_list RBRAC
441 | IDENTIFIER LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
442 | ID_TYPE LBRAC enumerator_list RBRAC
443 | ID_TYPE LT type_specifier_or_integer_constant GT LBRAC enumerator_list RBRAC
444 | LBRAC enumerator_list COMMA RBRAC
445 | LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
446 | IDENTIFIER LBRAC enumerator_list COMMA RBRAC
447 | IDENTIFIER LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
449 | IDENTIFIER LT type_specifier_or_integer_constant GT
450 | ID_TYPE LBRAC enumerator_list COMMA RBRAC
451 | ID_TYPE LT type_specifier_or_integer_constant GT LBRAC enumerator_list COMMA RBRAC
453 | ID_TYPE LT type_specifier_or_integer_constant GT
456 struct_or_variant_declaration_list:
458 | struct_or_variant_declaration_list struct_or_variant_declaration
461 struct_or_variant_declaration:
462 specifier_qualifier_list struct_or_variant_declarator_list SEMICOLON
463 | specifier_qualifier_list TYPEDEF specifier_qualifier_list type_declarator_list SEMICOLON
464 | TYPEDEF specifier_qualifier_list type_declarator_list SEMICOLON
465 | specifier_qualifier_list TYPEDEF type_declarator_list SEMICOLON
466 | TYPEALIAS specifier_qualifier_list abstract_declarator_list COLON specifier_qualifier_list abstract_type_declarator_list SEMICOLON
467 | TYPEALIAS specifier_qualifier_list abstract_declarator_list COLON type_declarator_list SEMICOLON
470 specifier_qualifier_list:
473 | specifier_qualifier_list CONST
474 | specifier_qualifier_list type_specifier
477 struct_or_variant_declarator_list:
478 struct_or_variant_declarator
479 | struct_or_variant_declarator_list COMMA struct_or_variant_declarator
482 struct_or_variant_declarator:
484 | COLON unary_expression
485 | declarator COLON unary_expression
490 | enumerator_list COMMA enumerator
497 | STRING_LITERAL_START DQUOTE
498 | STRING_LITERAL_START s_char_sequence DQUOTE
499 | IDENTIFIER EQUAL unary_expression_or_range
500 | ID_TYPE EQUAL unary_expression_or_range
501 | keywords EQUAL unary_expression_or_range
502 | STRING_LITERAL_START DQUOTE EQUAL unary_expression_or_range
503 | STRING_LITERAL_START s_char_sequence DQUOTE EQUAL unary_expression_or_range
506 abstract_declarator_list:
508 | abstract_declarator_list COMMA abstract_declarator
512 direct_abstract_declarator
513 | pointer direct_abstract_declarator
516 direct_abstract_declarator:
519 | LPAREN abstract_declarator RPAREN
520 | direct_abstract_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
521 | direct_abstract_declarator LSBRAC RSBRAC
526 | pointer direct_declarator
531 | LPAREN declarator RPAREN
532 | direct_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
536 direct_type_declarator
537 | pointer direct_type_declarator
540 direct_type_declarator:
545 | LPAREN type_declarator RPAREN
546 | direct_type_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
549 abstract_type_declarator:
550 direct_abstract_type_declarator
551 | pointer direct_abstract_type_declarator
554 direct_abstract_type_declarator:
560 | LPAREN abstract_type_declarator RPAREN
561 | direct_abstract_type_declarator LSBRAC type_specifier_or_integer_constant RSBRAC
562 | direct_abstract_type_declarator LSBRAC RSBRAC
568 | STAR type_qualifier_list pointer
573 | type_qualifier_list CONST
576 /* 2.3: CTF-specific declarations */
578 ctf_assignment_expression_list:
579 ctf_assignment_expression SEMICOLON
580 | ctf_assignment_expression_list ctf_assignment_expression SEMICOLON
583 ctf_assignment_expression:
584 unary_expression EQUAL unary_expression
585 | unary_expression TYPEASSIGN type_specifier
586 | declaration_specifiers TYPEDEF declaration_specifiers type_declarator_list
587 | TYPEDEF declaration_specifiers type_declarator_list
588 | declaration_specifiers TYPEDEF type_declarator_list
589 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON declaration_specifiers abstract_type_declarator_list
590 | TYPEALIAS declaration_specifiers abstract_declarator_list COLON type_declarator_list