3 * Copyright © 2010 Intel Corporation
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
32 yyerror (void *scanner, const char *error);
35 _define_object_macro (glcpp_parser_t *parser,
37 token_list_t *replacements);
40 _define_function_macro (glcpp_parser_t *parser,
42 string_list_t *parameters,
43 token_list_t *replacements);
46 _expand_object_macro (glcpp_parser_t *parser, const char *identifier);
49 _expand_function_macro (glcpp_parser_t *parser,
50 const char *identifier,
51 argument_list_t *arguments);
54 _string_list_create (void *ctx);
57 _string_list_append_item (string_list_t *list, const char *str);
60 _string_list_append_list (string_list_t *list, string_list_t *tail);
63 _string_list_contains (string_list_t *list, const char *member, int *index);
66 _string_list_length (string_list_t *list);
69 _argument_list_create (void *ctx);
72 _argument_list_append (argument_list_t *list, token_list_t *argument);
75 _argument_list_length (argument_list_t *list);
78 _argument_list_member_at (argument_list_t *list, int index);
81 _token_list_create (void *ctx);
84 _token_list_append (token_list_t *list, int type, const char *value);
87 _token_list_append_list (token_list_t *list, token_list_t *tail);
90 glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
92 argument_list_t *arguments);
95 glcpp_parser_pop_expansion (glcpp_parser_t *parser);
97 #define yylex glcpp_parser_lex
100 glcpp_parser_lex (glcpp_parser_t *parser);
107 argument_list_t *argument_list;
108 string_list_t *string_list;
110 token_list_t *token_list;
113 %parse-param {glcpp_parser_t *parser}
114 %lex-param {glcpp_parser_t *parser}
116 %token DEFINE FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO NEWLINE SEPARATOR SPACE TOKEN UNDEF
117 %type <ival> punctuator
118 %type <str> content FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO
119 %type <argument_list> argument_list
120 %type <string_list> macro parameter_list
121 %type <token> TOKEN argument_word argument_word_or_comma
122 %type <token_list> argument argument_or_comma replacement_list pp_tokens
124 /* Hard to remove shift/reduce conflicts documented as follows:
126 * 1. '(' after FUNC_MACRO name which is correctly resolved to shift
127 * to form macro invocation rather than reducing directly to
130 * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
131 * shift to form macro invocation rather than reducing directly to
134 * 3. Similarly again now that we added argument_or_comma as well.
140 /* We do all printing at the input level.
142 * The value for "input" is simply TOKEN or SEPARATOR so we
143 * can decide whether it's necessary to print a space
144 * character between any two. */
147 parser->just_printed_separator = 1;
152 if ($2 && strlen ($2)) {
154 int is_not_separator = ((c >= 'a' && c <= 'z') ||
155 (c >= 'A' && c <= 'Z') ||
156 (c >= 'A' && c <= 'Z') ||
157 (c >= '0' && c <= '9') ||
160 if (! parser->just_printed_separator && is_not_separator)
166 if (is_not_separator)
167 parser->just_printed_separator = 0;
169 parser->just_printed_separator = 1;
181 | IDENTIFIER_FINALIZED {
191 $$ = talloc_strdup (parser, "\n");
194 $$ = talloc_asprintf (parser, "%c", $1);
208 FUNC_MACRO '(' argument_list ')' {
209 _expand_function_macro (parser, $1, $3);
212 _expand_object_macro (parser, $1);
219 $$ = _argument_list_create (parser);
222 $$ = _argument_list_create (parser);
223 _argument_list_append ($$, $1);
225 | argument_list ',' argument {
226 _argument_list_append ($1, $3);
233 $$ = _token_list_create (parser);
234 _token_list_append ($$, $1.type, $1.value);
236 | argument argument_word {
237 _token_list_append ($1, $2.type, $2.value);
238 talloc_free ($2.value);
241 | argument '(' argument_or_comma ')' {
242 _token_list_append ($1, '(', "(");
243 _token_list_append_list ($1, $3);
244 _token_list_append ($1, ')', ")");
250 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
251 | IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
253 | FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
254 | macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
257 /* XXX: The body of argument_or_comma is the same as the body
258 * of argument, but with "argument" and "argument_word"
259 * changed to "argument_or_comma" and
260 * "argument_word_or_comma". It would be nice to have less
261 * redundancy here, but I'm not sure how.
263 * It would also be nice to have a less ugly grammar to have
264 * to implement, but such is the C preprocessor.
267 argument_word_or_comma {
268 $$ = _token_list_create (parser);
269 _token_list_append ($$, $1.type, $1.value);
271 | argument_or_comma argument_word_or_comma {
272 _token_list_append ($1, $2.type, $2.value);
275 | argument_or_comma '(' argument_or_comma ')' {
276 _token_list_append ($1, '(', "(");
277 _token_list_append_list ($1, $3);
278 _token_list_append ($1, ')', ")");
283 argument_word_or_comma:
284 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
285 | IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
287 | FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
288 | macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
289 | ',' { $$.type = ','; $$.value = xtalloc_strdup (parser, ","); }
293 DEFINE IDENTIFIER NEWLINE {
294 token_list_t *list = _token_list_create (parser);
295 _define_object_macro (parser, $2, list);
297 | DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
298 _define_object_macro (parser, $2, $4);
300 | DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
301 _define_function_macro (parser, $2, $4, $6);
304 string_list_t *macro = hash_table_find (parser->defines, $2);
306 /* XXX: Need hash table to support a real way
307 * to remove an element rather than prefixing
308 * a new node with data of NULL like this. */
309 hash_table_insert (parser->defines, NULL, $2);
318 $$ = _string_list_create (parser);
321 $$ = _string_list_create (parser);
322 _string_list_append_item ($$, $1);
325 | parameter_list ',' IDENTIFIER {
326 _string_list_append_item ($1, $3);
334 $$ = _token_list_create (parser);
344 $$ = _token_list_create (parser);
345 _token_list_append ($$, $1.type, $1.value);
348 _token_list_append ($1, $2.type, $2.value);
356 _string_list_create (void *ctx)
360 list = xtalloc (ctx, string_list_t);
368 _string_list_append_list (string_list_t *list, string_list_t *tail)
370 if (list->head == NULL) {
371 list->head = tail->head;
373 list->tail->next = tail->head;
376 list->tail = tail->tail;
380 _string_list_append_item (string_list_t *list, const char *str)
384 node = xtalloc (list, string_node_t);
385 node->str = xtalloc_strdup (node, str);
389 if (list->head == NULL) {
392 list->tail->next = node;
399 _string_list_contains (string_list_t *list, const char *member, int *index)
407 for (i = 0, node = list->head; node; i++, node = node->next) {
408 if (strcmp (node->str, member) == 0) {
419 _string_list_length (string_list_t *list)
427 for (node = list->head; node; node = node->next)
434 _argument_list_create (void *ctx)
436 argument_list_t *list;
438 list = xtalloc (ctx, argument_list_t);
446 _argument_list_append (argument_list_t *list, token_list_t *argument)
448 argument_node_t *node;
450 if (argument == NULL || argument->head == NULL)
453 node = xtalloc (list, argument_node_t);
454 node->argument = argument;
458 if (list->head == NULL) {
461 list->tail->next = node;
468 _argument_list_length (argument_list_t *list)
471 argument_node_t *node;
476 for (node = list->head; node; node = node->next)
483 _argument_list_member_at (argument_list_t *list, int index)
485 argument_node_t *node;
492 for (i = 0; i < index; i++) {
499 return node->argument;
505 _token_list_create (void *ctx)
509 list = xtalloc (ctx, token_list_t);
517 _token_list_append (token_list_t *list, int type, const char *value)
521 node = xtalloc (list, token_node_t);
523 node->value = xtalloc_strdup (list, value);
527 if (list->head == NULL) {
530 list->tail->next = node;
537 _token_list_append_list (token_list_t *list, token_list_t *tail)
539 if (list->head == NULL) {
540 list->head = tail->head;
542 list->tail->next = tail->head;
545 list->tail = tail->tail;
549 yyerror (void *scanner, const char *error)
551 fprintf (stderr, "Parse error: %s\n", error);
555 glcpp_parser_create (void)
557 glcpp_parser_t *parser;
559 parser = xtalloc (NULL, glcpp_parser_t);
561 glcpp_lex_init_extra (parser, &parser->scanner);
562 parser->defines = hash_table_ctor (32, hash_table_string_hash,
563 hash_table_string_compare);
564 parser->expansions = NULL;
566 parser->just_printed_separator = 1;
572 glcpp_parser_parse (glcpp_parser_t *parser)
574 return yyparse (parser);
578 glcpp_parser_destroy (glcpp_parser_t *parser)
580 glcpp_lex_destroy (parser->scanner);
581 hash_table_dtor (parser->defines);
582 talloc_free (parser);
586 glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
588 expansion_node_t *node;
590 for (node = parser->expansions; node; node = node->next) {
592 strcmp (node->macro->identifier, member) == 0)
602 glcpp_parser_classify_token (glcpp_parser_t *parser,
603 const char *identifier,
604 int *parameter_index)
608 /* First we check if we are currently expanding a
609 * function-like macro, and if so, whether the parameter list
610 * contains a parameter matching this token name. */
611 if (parser->expansions &&
612 parser->expansions->macro &&
613 parser->expansions->macro->parameters)
617 list = parser->expansions->macro->parameters;
619 if (_string_list_contains (list, identifier, parameter_index))
620 return TOKEN_CLASS_ARGUMENT;
623 /* If not a function-like macro parameter, we next check if
624 * this token is a macro itself. */
626 macro = hash_table_find (parser->defines, identifier);
629 return TOKEN_CLASS_IDENTIFIER;
631 /* Don't consider this a macro if we are already actively
632 * expanding this macro. */
633 if (glcpp_parser_is_expanding (parser, identifier))
634 return TOKEN_CLASS_IDENTIFIER_FINALIZED;
636 /* Definitely a macro. Just need to check if it's function-like. */
637 if (macro->is_function)
638 return TOKEN_CLASS_FUNC_MACRO;
640 return TOKEN_CLASS_OBJ_MACRO;
644 _define_object_macro (glcpp_parser_t *parser,
645 const char *identifier,
646 token_list_t *replacements)
650 macro = xtalloc (parser, macro_t);
652 macro->is_function = 0;
653 macro->parameters = NULL;
654 macro->identifier = talloc_strdup (macro, identifier);
655 macro->replacements = talloc_steal (macro, replacements);
657 hash_table_insert (parser->defines, macro, identifier);
661 _define_function_macro (glcpp_parser_t *parser,
662 const char *identifier,
663 string_list_t *parameters,
664 token_list_t *replacements)
668 macro = xtalloc (parser, macro_t);
670 macro->is_function = 1;
671 macro->parameters = talloc_steal (macro, parameters);
672 macro->identifier = talloc_strdup (macro, identifier);
673 macro->replacements = talloc_steal (macro, replacements);
675 hash_table_insert (parser->defines, macro, identifier);
679 _glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
681 argument_list_t *arguments,
682 token_node_t *replacements)
684 expansion_node_t *node;
686 node = xtalloc (parser, expansion_node_t);
689 node->arguments = arguments;
690 node->replacements = replacements;
692 node->next = parser->expansions;
693 parser->expansions = node;
697 glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
699 argument_list_t *arguments)
701 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
702 macro->replacements->head);
706 glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
709 argument_list_t *arguments;
710 token_list_t *argument;
712 arguments = parser->expansions->arguments;
714 argument = _argument_list_member_at (arguments, argument_index);
716 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
721 glcpp_parser_pop_expansion (glcpp_parser_t *parser)
723 expansion_node_t *node;
725 node = parser->expansions;
728 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
732 parser->expansions = node->next;
738 _expand_object_macro (glcpp_parser_t *parser, const char *identifier)
742 macro = hash_table_find (parser->defines, identifier);
743 assert (! macro->is_function);
744 assert (! glcpp_parser_is_expanding (parser, identifier));
746 glcpp_parser_push_expansion_macro (parser, macro, NULL);
750 _expand_function_macro (glcpp_parser_t *parser,
751 const char *identifier,
752 argument_list_t *arguments)
756 macro = hash_table_find (parser->defines, identifier);
757 assert (macro->is_function);
758 assert (! glcpp_parser_is_expanding (parser, identifier));
760 if (_argument_list_length (arguments) !=
761 _string_list_length (macro->parameters))
764 "Error: macro %s invoked with %d arguments (expected %d)\n",
766 _argument_list_length (arguments),
767 _string_list_length (macro->parameters));
771 glcpp_parser_push_expansion_macro (parser, macro, arguments);
775 glcpp_parser_lex (glcpp_parser_t *parser)
777 expansion_node_t *expansion;
778 token_node_t *replacements;
781 /* Who says C can't do efficient tail recursion? */
784 expansion = parser->expansions;
786 if (expansion == NULL)
787 return glcpp_lex (parser->scanner);
789 replacements = expansion->replacements;
791 /* Pop expansion when replacements is exhausted. */
792 if (replacements == NULL) {
793 glcpp_parser_pop_expansion (parser);
797 expansion->replacements = replacements->next;
799 if (strcmp (replacements->value, "(") == 0)
801 else if (strcmp (replacements->value, ")") == 0)
804 yylval.str = xtalloc_strdup (parser, replacements->value);
806 /* Carefully refuse to expand any finalized identifier. */
807 if (replacements->type == IDENTIFIER_FINALIZED)
808 return IDENTIFIER_FINALIZED;
810 switch (glcpp_parser_classify_token (parser, yylval.str,
813 case TOKEN_CLASS_ARGUMENT:
814 talloc_free (yylval.str);
815 glcpp_parser_push_expansion_argument (parser,
819 case TOKEN_CLASS_IDENTIFIER:
822 case TOKEN_CLASS_IDENTIFIER_FINALIZED:
823 return IDENTIFIER_FINALIZED;
825 case TOKEN_CLASS_FUNC_MACRO:
829 case TOKEN_CLASS_OBJ_MACRO: