blob: 28e79ebf9f781db648c2614c44e34fb33c97e08d [file] [log] [blame]
Carl Worth3a37b872010-05-10 11:44:09 -07001%{
2/*
3 * Copyright © 2010 Intel Corporation
4 *
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:
11 *
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
14 * Software.
15 *
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.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
Carl Worthfcbbb462010-05-13 09:36:23 -070027#include <assert.h>
Carl Worth3a37b872010-05-10 11:44:09 -070028
Carl Wortha1e32bc2010-05-10 13:17:25 -070029#include "glcpp.h"
30
Carl Worth3a37b872010-05-10 11:44:09 -070031void
Carl Wortha1e32bc2010-05-10 13:17:25 -070032yyerror (void *scanner, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070033
Carl Worth33cc4002010-05-12 12:17:10 -070034void
Carl Worthfcbbb462010-05-13 09:36:23 -070035_define_object_macro (glcpp_parser_t *parser,
36 const char *macro,
Carl Worth47252442010-05-19 13:54:37 -070037 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070038
39void
40_define_function_macro (glcpp_parser_t *parser,
41 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070042 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070043 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070044
Carl Wortha807fb72010-05-18 22:10:04 -070045void
Carl Worth2be8be02010-05-14 10:31:43 -070046_expand_object_macro (glcpp_parser_t *parser, const char *identifier);
47
Carl Wortha807fb72010-05-18 22:10:04 -070048void
Carl Worth2be8be02010-05-14 10:31:43 -070049_expand_function_macro (glcpp_parser_t *parser,
50 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -070051 argument_list_t *arguments);
Carl Worthfcbbb462010-05-13 09:36:23 -070052
53void
Carl Worth2be8be02010-05-14 10:31:43 -070054_print_string_list (string_list_t *list);
Carl Worth33cc4002010-05-12 12:17:10 -070055
Carl Worth610053b2010-05-14 10:05:11 -070056string_list_t *
57_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070058
59void
Carl Worth610053b2010-05-14 10:05:11 -070060_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070061
62void
Carl Worth610053b2010-05-14 10:05:11 -070063_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070064
Carl Worthdcc2ecd2010-05-13 12:56:42 -070065int
Carl Worth610053b2010-05-14 10:05:11 -070066_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070067
Carl Worthdcc2ecd2010-05-13 12:56:42 -070068int
Carl Worth610053b2010-05-14 10:05:11 -070069_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070070
Carl Worth8f6a8282010-05-14 10:44:19 -070071argument_list_t *
72_argument_list_create (void *ctx);
73
74void
Carl Worth47252442010-05-19 13:54:37 -070075_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070076
77int
78_argument_list_length (argument_list_t *list);
79
Carl Worth47252442010-05-19 13:54:37 -070080token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070081_argument_list_member_at (argument_list_t *list, int index);
82
Carl Worth47252442010-05-19 13:54:37 -070083token_list_t *
84_token_list_create (void *ctx);
85
86void
87_token_list_append (token_list_t *list, int type, const char *value);
88
89void
90_token_list_append_list (token_list_t *list, token_list_t *tail);
91
Carl Worthaaa9acb2010-05-19 13:28:24 -070092static void
93glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
94 macro_t *macro,
95 argument_list_t *arguments);
96
97static void
98glcpp_parser_pop_expansion (glcpp_parser_t *parser);
99
Carl Worth0293b2e2010-05-19 10:05:40 -0700100#define yylex glcpp_parser_lex
101
Carl Worth8f38aff2010-05-19 10:01:29 -0700102static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700103glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700104
Carl Worth3a37b872010-05-10 11:44:09 -0700105%}
106
Carl Worth33cc4002010-05-12 12:17:10 -0700107%union {
108 char *str;
Carl Worth8f6a8282010-05-14 10:44:19 -0700109 argument_list_t *argument_list;
Carl Worth47252442010-05-19 13:54:37 -0700110 string_list_t *string_list;
Carl Worthb5693832010-05-20 08:01:44 -0700111 token_t token;
Carl Worth47252442010-05-19 13:54:37 -0700112 token_list_t *token_list;
Carl Worth33cc4002010-05-12 12:17:10 -0700113}
114
Carl Worth0b27b5f2010-05-10 16:16:06 -0700115%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700116%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700117
Carl Worthb5693832010-05-20 08:01:44 -0700118%token DEFINE FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO NEWLINE SPACE TOKEN UNDEF
119%type <str> FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO
Carl Worth8f6a8282010-05-14 10:44:19 -0700120%type <argument_list> argument_list
Carl Worth47252442010-05-19 13:54:37 -0700121%type <string_list> macro parameter_list
Carl Worthb5693832010-05-20 08:01:44 -0700122%type <token> TOKEN argument_word
Carl Worth47252442010-05-19 13:54:37 -0700123%type <token_list> argument replacement_list pp_tokens
Carl Worth3a37b872010-05-10 11:44:09 -0700124
Carl Worth796e1f02010-05-17 12:45:16 -0700125/* Hard to remove shift/reduce conflicts documented as follows:
126 *
127 * 1. '(' after FUNC_MACRO name which is correctly resolved to shift
128 * to form macro invocation rather than reducing directly to
129 * content.
Carl Worth69f390d2010-05-19 07:42:42 -0700130 *
131 * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
132 * shift to form macro invocation rather than reducing directly to
133 * argument.
Carl Worth796e1f02010-05-17 12:45:16 -0700134 */
Carl Worth69f390d2010-05-19 07:42:42 -0700135%expect 2
Carl Worth796e1f02010-05-17 12:45:16 -0700136
Carl Worth3a37b872010-05-10 11:44:09 -0700137%%
138
Carl Worth33cc4002010-05-12 12:17:10 -0700139input:
140 /* empty */
Carl Worth8bcb6f12010-05-12 13:21:20 -0700141| input content
Carl Worth3a37b872010-05-10 11:44:09 -0700142;
143
Carl Worth04af1352010-05-14 10:17:38 -0700144 /* We do all printing at the content level */
Carl Worth33cc4002010-05-12 12:17:10 -0700145content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700146 IDENTIFIER {
147 printf ("%s", $1);
148 talloc_free ($1);
149 }
Carl Worthb5693832010-05-20 08:01:44 -0700150| IDENTIFIER_FINALIZED {
Carl Worth9f62a7e2010-05-13 07:38:29 -0700151 printf ("%s", $1);
152 talloc_free ($1);
153 }
Carl Worthb5693832010-05-20 08:01:44 -0700154| TOKEN {
155 printf ("%s", $1.value);
156 talloc_free ($1.value);
157 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700158| FUNC_MACRO {
159 printf ("%s", $1);
160 talloc_free ($1);
161 }
Carl Wortha807fb72010-05-18 22:10:04 -0700162| directive {
163 printf ("\n");
Carl Worth2be8be02010-05-14 10:31:43 -0700164 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700165| '(' { printf ("("); }
166| ')' { printf (")"); }
167| ',' { printf (","); }
Carl Wortha807fb72010-05-18 22:10:04 -0700168| macro
Carl Worthcd27e642010-05-12 13:11:50 -0700169;
170
Carl Worthfcbbb462010-05-13 09:36:23 -0700171macro:
172 FUNC_MACRO '(' argument_list ')' {
Carl Wortha807fb72010-05-18 22:10:04 -0700173 _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700174 }
175| OBJ_MACRO {
Carl Wortha807fb72010-05-18 22:10:04 -0700176 _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700177 talloc_free ($1);
178 }
179;
180
181argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700182 /* empty */ {
183 $$ = _argument_list_create (parser);
184 }
185| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700186 $$ = _argument_list_create (parser);
187 _argument_list_append ($$, $1);
188 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700189| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700190 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700191 $$ = $1;
192 }
193;
194
195argument:
Carl Worth59ca9892010-05-19 07:49:47 -0700196 argument_word {
Carl Worth47252442010-05-19 13:54:37 -0700197 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700198 _token_list_append ($$, $1.type, $1.value);
Carl Worthfcbbb462010-05-13 09:36:23 -0700199 }
Carl Worth59ca9892010-05-19 07:49:47 -0700200| argument argument_word {
Carl Worthb5693832010-05-20 08:01:44 -0700201 _token_list_append ($1, $2.type, $2.value);
202 talloc_free ($2.value);
Carl Worth3596bb12010-05-14 16:53:52 -0700203 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700204 }
Carl Worth3596bb12010-05-14 16:53:52 -0700205| argument '(' argument ')' {
Carl Worth47252442010-05-19 13:54:37 -0700206 _token_list_append ($1, '(', "(");
207 _token_list_append_list ($1, $3);
208 _token_list_append ($1, ')', ")");
Carl Worth3596bb12010-05-14 16:53:52 -0700209 $$ = $1;
210 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700211;
212
Carl Worth59ca9892010-05-19 07:49:47 -0700213argument_word:
Carl Worthb5693832010-05-20 08:01:44 -0700214 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
215| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
Carl Worth59ca9892010-05-19 07:49:47 -0700216| TOKEN { $$ = $1; }
Carl Worthb5693832010-05-20 08:01:44 -0700217| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
218| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
Carl Worth59ca9892010-05-19 07:49:47 -0700219;
220
221
Carl Worth33cc4002010-05-12 12:17:10 -0700222directive:
Carl Worthaaa9acb2010-05-19 13:28:24 -0700223 DEFINE IDENTIFIER NEWLINE {
Carl Worth47252442010-05-19 13:54:37 -0700224 token_list_t *list = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700225 _define_object_macro (parser, $2, list);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700226 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700227| DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
228 _define_object_macro (parser, $2, $4);
229 }
230| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
Carl Worth81f01432010-05-14 17:08:45 -0700231 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700232 }
Carl Wortha807fb72010-05-18 22:10:04 -0700233| UNDEF IDENTIFIER {
234 string_list_t *macro = hash_table_find (parser->defines, $2);
235 if (macro) {
Carl Worthfcbbb462010-05-13 09:36:23 -0700236 /* XXX: Need hash table to support a real way
237 * to remove an element rather than prefixing
238 * a new node with data of NULL like this. */
239 hash_table_insert (parser->defines, NULL, $2);
Carl Wortha807fb72010-05-18 22:10:04 -0700240 talloc_free (macro);
Carl Worthfcbbb462010-05-13 09:36:23 -0700241 }
242 talloc_free ($2);
243 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700244;
245
Carl Worthfcbbb462010-05-13 09:36:23 -0700246parameter_list:
247 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700248 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700249 }
Carl Wortha807fb72010-05-18 22:10:04 -0700250| IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700251 $$ = _string_list_create (parser);
252 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700253 talloc_free ($1);
254 }
Carl Wortha807fb72010-05-18 22:10:04 -0700255| parameter_list ',' IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700256 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700257 talloc_free ($3);
258 $$ = $1;
259 }
260;
261
Carl Worthaaa9acb2010-05-19 13:28:24 -0700262replacement_list:
263 /* empty */ {
Carl Worth47252442010-05-19 13:54:37 -0700264 $$ = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700265 }
266| pp_tokens {
267 $$ = $1;
268 }
269;
270
271
272pp_tokens:
273 TOKEN {
Carl Worth47252442010-05-19 13:54:37 -0700274 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700275 _token_list_append ($$, $1.type, $1.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700276 }
277| pp_tokens TOKEN {
Carl Worthb5693832010-05-20 08:01:44 -0700278 _token_list_append ($1, $2.type, $2.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700279 $$ = $1;
280 }
281;
282
Carl Worth33cc4002010-05-12 12:17:10 -0700283%%
284
Carl Worth610053b2010-05-14 10:05:11 -0700285string_list_t *
286_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700287{
Carl Worth610053b2010-05-14 10:05:11 -0700288 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700289
Carl Worth610053b2010-05-14 10:05:11 -0700290 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700291 list->head = NULL;
292 list->tail = NULL;
293
294 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700295}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700296
Carl Worth33cc4002010-05-12 12:17:10 -0700297void
Carl Worth610053b2010-05-14 10:05:11 -0700298_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700299{
300 if (list->head == NULL) {
301 list->head = tail->head;
302 } else {
303 list->tail->next = tail->head;
304 }
305
306 list->tail = tail->tail;
307}
308
309void
Carl Worth610053b2010-05-14 10:05:11 -0700310_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700311{
Carl Worth610053b2010-05-14 10:05:11 -0700312 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700313
Carl Worth610053b2010-05-14 10:05:11 -0700314 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700315 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700316
317 node->next = NULL;
318
319 if (list->head == NULL) {
320 list->head = node;
321 } else {
322 list->tail->next = node;
323 }
324
325 list->tail = node;
326}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700327
328int
Carl Worth610053b2010-05-14 10:05:11 -0700329_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700330{
Carl Worth610053b2010-05-14 10:05:11 -0700331 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700332 int i;
333
334 if (list == NULL)
335 return 0;
336
337 for (i = 0, node = list->head; node; i++, node = node->next) {
338 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700339 if (index)
340 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700341 return 1;
342 }
343 }
344
345 return 0;
346}
347
348int
Carl Worth610053b2010-05-14 10:05:11 -0700349_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700350{
351 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700352 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700353
354 if (list == NULL)
355 return 0;
356
357 for (node = list->head; node; node = node->next)
358 length++;
359
360 return length;
361}
362
Carl Worth2be8be02010-05-14 10:31:43 -0700363void
364_print_string_list (string_list_t *list)
365{
366 string_node_t *node;
367
368 if (list == NULL)
369 return;
370
Carl Worth81f01432010-05-14 17:08:45 -0700371 for (node = list->head; node; node = node->next) {
Carl Worth2be8be02010-05-14 10:31:43 -0700372 printf ("%s", node->str);
Carl Worth81f01432010-05-14 17:08:45 -0700373 if (node->next)
374 printf (" ");
375 }
Carl Worth2be8be02010-05-14 10:31:43 -0700376}
377
Carl Worth8f6a8282010-05-14 10:44:19 -0700378argument_list_t *
379_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700380{
Carl Worth8f6a8282010-05-14 10:44:19 -0700381 argument_list_t *list;
382
383 list = xtalloc (ctx, argument_list_t);
384 list->head = NULL;
385 list->tail = NULL;
386
387 return list;
388}
389
390void
Carl Worth47252442010-05-19 13:54:37 -0700391_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700392{
393 argument_node_t *node;
394
395 if (argument == NULL || argument->head == NULL)
396 return;
397
398 node = xtalloc (list, argument_node_t);
399 node->argument = argument;
400
401 node->next = NULL;
402
403 if (list->head == NULL) {
404 list->head = node;
405 } else {
406 list->tail->next = node;
407 }
408
409 list->tail = node;
410}
411
412int
413_argument_list_length (argument_list_t *list)
414{
415 int length = 0;
416 argument_node_t *node;
417
418 if (list == NULL)
419 return 0;
420
421 for (node = list->head; node; node = node->next)
422 length++;
423
424 return length;
425}
426
Carl Worth47252442010-05-19 13:54:37 -0700427token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700428_argument_list_member_at (argument_list_t *list, int index)
429{
430 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700431 int i;
432
433 if (list == NULL)
434 return NULL;
435
436 node = list->head;
437 for (i = 0; i < index; i++) {
438 node = node->next;
439 if (node == NULL)
440 break;
441 }
442
443 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700444 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700445
446 return NULL;
447}
Carl Worth47252442010-05-19 13:54:37 -0700448
449token_list_t *
450_token_list_create (void *ctx)
451{
452 token_list_t *list;
453
454 list = xtalloc (ctx, token_list_t);
455 list->head = NULL;
456 list->tail = NULL;
457
458 return list;
459}
460
461void
462_token_list_append (token_list_t *list, int type, const char *value)
463{
464 token_node_t *node;
465
466 node = xtalloc (list, token_node_t);
467 node->type = type;
468 node->value = xtalloc_strdup (list, value);
469
470 node->next = NULL;
471
472 if (list->head == NULL) {
473 list->head = node;
474 } else {
475 list->tail->next = node;
476 }
477
478 list->tail = node;
479}
480
481void
482_token_list_append_list (token_list_t *list, token_list_t *tail)
483{
484 if (list->head == NULL) {
485 list->head = tail->head;
486 } else {
487 list->tail->next = tail->head;
488 }
489
490 list->tail = tail->tail;
491}
Carl Worth33cc4002010-05-12 12:17:10 -0700492
Carl Worth3a37b872010-05-10 11:44:09 -0700493void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700494yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700495{
496 fprintf (stderr, "Parse error: %s\n", error);
497}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700498
Carl Worth33cc4002010-05-12 12:17:10 -0700499glcpp_parser_t *
500glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700501{
Carl Worth33cc4002010-05-12 12:17:10 -0700502 glcpp_parser_t *parser;
503
Carl Worth5070a202010-05-12 12:45:33 -0700504 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700505
Carl Worth8f38aff2010-05-19 10:01:29 -0700506 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700507 parser->defines = hash_table_ctor (32, hash_table_string_hash,
508 hash_table_string_compare);
Carl Wortha807fb72010-05-18 22:10:04 -0700509 parser->expansions = NULL;
510
Carl Worth33cc4002010-05-12 12:17:10 -0700511 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700512}
513
514int
515glcpp_parser_parse (glcpp_parser_t *parser)
516{
517 return yyparse (parser);
518}
519
520void
Carl Worth33cc4002010-05-12 12:17:10 -0700521glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700522{
Carl Worth8f38aff2010-05-19 10:01:29 -0700523 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700524 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700525 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700526}
Carl Worthc6d5af32010-05-11 12:30:09 -0700527
Carl Worthbe0e2e92010-05-19 07:29:22 -0700528static int
529glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
530{
531 expansion_node_t *node;
532
533 for (node = parser->expansions; node; node = node->next) {
534 if (node->macro &&
535 strcmp (node->macro->identifier, member) == 0)
536 {
537 return 1;
538 }
539 }
540
541 return 0;
542}
543
Carl Wortha807fb72010-05-18 22:10:04 -0700544token_class_t
545glcpp_parser_classify_token (glcpp_parser_t *parser,
546 const char *identifier,
547 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700548{
Carl Worthfcbbb462010-05-13 09:36:23 -0700549 macro_t *macro;
550
Carl Wortha807fb72010-05-18 22:10:04 -0700551 /* First we check if we are currently expanding a
552 * function-like macro, and if so, whether the parameter list
553 * contains a parameter matching this token name. */
554 if (parser->expansions &&
555 parser->expansions->macro &&
556 parser->expansions->macro->parameters)
557 {
558 string_list_t *list;
559
560 list = parser->expansions->macro->parameters;
561
562 if (_string_list_contains (list, identifier, parameter_index))
563 return TOKEN_CLASS_ARGUMENT;
564 }
565
566 /* If not a function-like macro parameter, we next check if
567 * this token is a macro itself. */
568
Carl Worthfcbbb462010-05-13 09:36:23 -0700569 macro = hash_table_find (parser->defines, identifier);
570
571 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700572 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700573
Carl Worthbe0e2e92010-05-19 07:29:22 -0700574 /* Don't consider this a macro if we are already actively
575 * expanding this macro. */
576 if (glcpp_parser_is_expanding (parser, identifier))
Carl Worthb5693832010-05-20 08:01:44 -0700577 return TOKEN_CLASS_IDENTIFIER_FINALIZED;
Carl Worthbe0e2e92010-05-19 07:29:22 -0700578
579 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700580 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700581 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700582 else
Carl Wortha807fb72010-05-18 22:10:04 -0700583 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700584}
585
Carl Worth33cc4002010-05-12 12:17:10 -0700586void
Carl Worthfcbbb462010-05-13 09:36:23 -0700587_define_object_macro (glcpp_parser_t *parser,
588 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -0700589 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700590{
591 macro_t *macro;
592
593 macro = xtalloc (parser, macro_t);
594
595 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700596 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700597 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700598 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700599
600 hash_table_insert (parser->defines, macro, identifier);
601}
602
603void
604_define_function_macro (glcpp_parser_t *parser,
605 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700606 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -0700607 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700608{
609 macro_t *macro;
610
611 macro = xtalloc (parser, macro_t);
612
613 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700614 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700615 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700616 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700617
618 hash_table_insert (parser->defines, macro, identifier);
619}
620
Carl Wortha807fb72010-05-18 22:10:04 -0700621static void
622_glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
623 macro_t *macro,
624 argument_list_t *arguments,
Carl Worth47252442010-05-19 13:54:37 -0700625 token_node_t *replacements)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700626{
Carl Wortha807fb72010-05-18 22:10:04 -0700627 expansion_node_t *node;
628
629 node = xtalloc (parser, expansion_node_t);
630
631 node->macro = macro;
632 node->arguments = arguments;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700633 node->replacements = replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700634
635 node->next = parser->expansions;
636 parser->expansions = node;
Carl Wortha807fb72010-05-18 22:10:04 -0700637}
638
Carl Worthaaa9acb2010-05-19 13:28:24 -0700639static void
Carl Wortha807fb72010-05-18 22:10:04 -0700640glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
641 macro_t *macro,
642 argument_list_t *arguments)
643{
644 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700645 macro->replacements->head);
Carl Wortha807fb72010-05-18 22:10:04 -0700646}
647
648void
649glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
650 int argument_index)
651{
652 argument_list_t *arguments;
Carl Worth47252442010-05-19 13:54:37 -0700653 token_list_t *argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700654
Carl Wortha807fb72010-05-18 22:10:04 -0700655 arguments = parser->expansions->arguments;
Carl Worth2be8be02010-05-14 10:31:43 -0700656
Carl Wortha807fb72010-05-18 22:10:04 -0700657 argument = _argument_list_member_at (arguments, argument_index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700658
Carl Wortha807fb72010-05-18 22:10:04 -0700659 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700660 argument->head);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700661}
662
Carl Worthaaa9acb2010-05-19 13:28:24 -0700663static void
Carl Wortha807fb72010-05-18 22:10:04 -0700664glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700665{
Carl Wortha807fb72010-05-18 22:10:04 -0700666 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700667
Carl Wortha807fb72010-05-18 22:10:04 -0700668 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700669
Carl Wortha807fb72010-05-18 22:10:04 -0700670 if (node == NULL) {
671 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
672 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700673 }
674
Carl Wortha807fb72010-05-18 22:10:04 -0700675 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700676
Carl Wortha807fb72010-05-18 22:10:04 -0700677 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700678}
679
Carl Wortha807fb72010-05-18 22:10:04 -0700680void
Carl Worth2be8be02010-05-14 10:31:43 -0700681_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700682{
Carl Worthfcbbb462010-05-13 09:36:23 -0700683 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700684
Carl Worthfcbbb462010-05-13 09:36:23 -0700685 macro = hash_table_find (parser->defines, identifier);
686 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700687 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700688
Carl Worthbe0e2e92010-05-19 07:29:22 -0700689 glcpp_parser_push_expansion_macro (parser, macro, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700690}
691
Carl Wortha807fb72010-05-18 22:10:04 -0700692void
Carl Worth2be8be02010-05-14 10:31:43 -0700693_expand_function_macro (glcpp_parser_t *parser,
694 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700695 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700696{
Carl Worthfcbbb462010-05-13 09:36:23 -0700697 macro_t *macro;
698
699 macro = hash_table_find (parser->defines, identifier);
700 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700701 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700702
Carl Worth8f6a8282010-05-14 10:44:19 -0700703 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700704 _string_list_length (macro->parameters))
705 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700706 fprintf (stderr,
707 "Error: macro %s invoked with %d arguments (expected %d)\n",
708 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700709 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700710 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700711 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700712 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700713
Carl Worthbe0e2e92010-05-19 07:29:22 -0700714 glcpp_parser_push_expansion_macro (parser, macro, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700715}
Carl Worth8f38aff2010-05-19 10:01:29 -0700716
717static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700718glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700719{
Carl Worthaaa9acb2010-05-19 13:28:24 -0700720 expansion_node_t *expansion;
Carl Worth47252442010-05-19 13:54:37 -0700721 token_node_t *replacements;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700722 int parameter_index;
723
724 /* Who says C can't do efficient tail recursion? */
725 RECURSE:
726
727 expansion = parser->expansions;
728
729 if (expansion == NULL)
730 return glcpp_lex (parser->scanner);
731
732 replacements = expansion->replacements;
733
734 /* Pop expansion when replacements is exhausted. */
735 if (replacements == NULL) {
736 glcpp_parser_pop_expansion (parser);
737 goto RECURSE;
738 }
739
740 expansion->replacements = replacements->next;
741
Carl Worth47252442010-05-19 13:54:37 -0700742 if (strcmp (replacements->value, "(") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700743 return '(';
Carl Worth47252442010-05-19 13:54:37 -0700744 else if (strcmp (replacements->value, ")") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700745 return ')';
Carl Worth47252442010-05-19 13:54:37 -0700746 else if (strcmp (replacements->value, ",") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700747 return ',';
748
Carl Worth47252442010-05-19 13:54:37 -0700749 yylval.str = xtalloc_strdup (parser, replacements->value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700750
Carl Worthb5693832010-05-20 08:01:44 -0700751 /* Carefully refuse to expand any finalized identifier. */
752 if (replacements->type == IDENTIFIER_FINALIZED)
753 return IDENTIFIER_FINALIZED;
754
Carl Worthaaa9acb2010-05-19 13:28:24 -0700755 switch (glcpp_parser_classify_token (parser, yylval.str,
756 &parameter_index))
757 {
758 case TOKEN_CLASS_ARGUMENT:
759 talloc_free (yylval.str);
760 glcpp_parser_push_expansion_argument (parser,
761 parameter_index);
762 goto RECURSE;
763 break;
764 case TOKEN_CLASS_IDENTIFIER:
765 return IDENTIFIER;
766 break;
Carl Worthb5693832010-05-20 08:01:44 -0700767 case TOKEN_CLASS_IDENTIFIER_FINALIZED:
768 return IDENTIFIER_FINALIZED;
769 break;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700770 case TOKEN_CLASS_FUNC_MACRO:
771 return FUNC_MACRO;
772 break;
773 default:
774 case TOKEN_CLASS_OBJ_MACRO:
775 return OBJ_MACRO;
776 break;
777 }
Carl Worth8f38aff2010-05-19 10:01:29 -0700778}