blob: c9edc5c3040e7ee6eaf8bcdd2b1dc34060237cb0 [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 Worth9f3d2c42010-05-20 08:42:02 -0700122%type <token> TOKEN argument_word argument_word_or_comma
123%type <token_list> argument argument_or_comma 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 Worth9f3d2c42010-05-20 08:42:02 -0700134 *
135 * 3. Similarly again now that we added argument_or_comma as well.
Carl Worth796e1f02010-05-17 12:45:16 -0700136 */
Carl Worth9f3d2c42010-05-20 08:42:02 -0700137%expect 3
Carl Worth796e1f02010-05-17 12:45:16 -0700138
Carl Worth3a37b872010-05-10 11:44:09 -0700139%%
140
Carl Worth33cc4002010-05-12 12:17:10 -0700141input:
142 /* empty */
Carl Worth8bcb6f12010-05-12 13:21:20 -0700143| input content
Carl Worth3a37b872010-05-10 11:44:09 -0700144;
145
Carl Worth04af1352010-05-14 10:17:38 -0700146 /* We do all printing at the content level */
Carl Worth33cc4002010-05-12 12:17:10 -0700147content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700148 IDENTIFIER {
149 printf ("%s", $1);
150 talloc_free ($1);
151 }
Carl Worthb5693832010-05-20 08:01:44 -0700152| IDENTIFIER_FINALIZED {
Carl Worth9f62a7e2010-05-13 07:38:29 -0700153 printf ("%s", $1);
154 talloc_free ($1);
155 }
Carl Worthb5693832010-05-20 08:01:44 -0700156| TOKEN {
157 printf ("%s", $1.value);
158 talloc_free ($1.value);
159 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700160| FUNC_MACRO {
161 printf ("%s", $1);
162 talloc_free ($1);
163 }
Carl Wortha807fb72010-05-18 22:10:04 -0700164| directive {
165 printf ("\n");
Carl Worth2be8be02010-05-14 10:31:43 -0700166 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700167| '(' { printf ("("); }
168| ')' { printf (")"); }
169| ',' { printf (","); }
Carl Wortha807fb72010-05-18 22:10:04 -0700170| macro
Carl Worthcd27e642010-05-12 13:11:50 -0700171;
172
Carl Worthfcbbb462010-05-13 09:36:23 -0700173macro:
174 FUNC_MACRO '(' argument_list ')' {
Carl Wortha807fb72010-05-18 22:10:04 -0700175 _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700176 }
177| OBJ_MACRO {
Carl Wortha807fb72010-05-18 22:10:04 -0700178 _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700179 talloc_free ($1);
180 }
181;
182
183argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700184 /* empty */ {
185 $$ = _argument_list_create (parser);
186 }
187| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700188 $$ = _argument_list_create (parser);
189 _argument_list_append ($$, $1);
190 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700191| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700192 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700193 $$ = $1;
194 }
195;
196
197argument:
Carl Worth59ca9892010-05-19 07:49:47 -0700198 argument_word {
Carl Worth47252442010-05-19 13:54:37 -0700199 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700200 _token_list_append ($$, $1.type, $1.value);
Carl Worthfcbbb462010-05-13 09:36:23 -0700201 }
Carl Worth59ca9892010-05-19 07:49:47 -0700202| argument argument_word {
Carl Worthb5693832010-05-20 08:01:44 -0700203 _token_list_append ($1, $2.type, $2.value);
204 talloc_free ($2.value);
Carl Worth3596bb12010-05-14 16:53:52 -0700205 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700206 }
Carl Worth9f3d2c42010-05-20 08:42:02 -0700207| argument '(' argument_or_comma ')' {
Carl Worth47252442010-05-19 13:54:37 -0700208 _token_list_append ($1, '(', "(");
209 _token_list_append_list ($1, $3);
210 _token_list_append ($1, ')', ")");
Carl Worth3596bb12010-05-14 16:53:52 -0700211 $$ = $1;
212 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700213;
214
Carl Worth59ca9892010-05-19 07:49:47 -0700215argument_word:
Carl Worthb5693832010-05-20 08:01:44 -0700216 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
217| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
Carl Worth59ca9892010-05-19 07:49:47 -0700218| TOKEN { $$ = $1; }
Carl Worthb5693832010-05-20 08:01:44 -0700219| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
220| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
Carl Worth59ca9892010-05-19 07:49:47 -0700221;
222
Carl Worth9f3d2c42010-05-20 08:42:02 -0700223 /* XXX: The body of argument_or_comma is the same as the body
224 * of argument, but with "argument" and "argument_word"
225 * changed to "argument_or_comma" and
226 * "argument_word_or_comma". It would be nice to have less
227 * redundancy here, but I'm not sure how.
228 *
229 * It would also be nice to have a less ugly grammar to have
230 * to implement, but such is the C preprocessor.
231 */
232argument_or_comma:
233 argument_word_or_comma {
234 $$ = _token_list_create (parser);
235 _token_list_append ($$, $1.type, $1.value);
236 }
237| argument_or_comma argument_word_or_comma {
238 _token_list_append ($1, $2.type, $2.value);
239 $$ = $1;
240 }
241| argument_or_comma '(' argument_or_comma ')' {
242 _token_list_append ($1, '(', "(");
243 _token_list_append_list ($1, $3);
244 _token_list_append ($1, ')', ")");
245 $$ = $1;
246 }
247;
248
249argument_word_or_comma:
250 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
251| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
252| TOKEN { $$ = $1; }
253| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
254| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
255| ',' { $$.type = ','; $$.value = xtalloc_strdup (parser, ","); }
256;
Carl Worth59ca9892010-05-19 07:49:47 -0700257
Carl Worth33cc4002010-05-12 12:17:10 -0700258directive:
Carl Worthaaa9acb2010-05-19 13:28:24 -0700259 DEFINE IDENTIFIER NEWLINE {
Carl Worth47252442010-05-19 13:54:37 -0700260 token_list_t *list = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700261 _define_object_macro (parser, $2, list);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700262 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700263| DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
264 _define_object_macro (parser, $2, $4);
265 }
266| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
Carl Worth81f01432010-05-14 17:08:45 -0700267 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700268 }
Carl Wortha807fb72010-05-18 22:10:04 -0700269| UNDEF IDENTIFIER {
270 string_list_t *macro = hash_table_find (parser->defines, $2);
271 if (macro) {
Carl Worthfcbbb462010-05-13 09:36:23 -0700272 /* XXX: Need hash table to support a real way
273 * to remove an element rather than prefixing
274 * a new node with data of NULL like this. */
275 hash_table_insert (parser->defines, NULL, $2);
Carl Wortha807fb72010-05-18 22:10:04 -0700276 talloc_free (macro);
Carl Worthfcbbb462010-05-13 09:36:23 -0700277 }
278 talloc_free ($2);
279 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700280;
281
Carl Worthfcbbb462010-05-13 09:36:23 -0700282parameter_list:
283 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700284 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700285 }
Carl Wortha807fb72010-05-18 22:10:04 -0700286| IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700287 $$ = _string_list_create (parser);
288 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700289 talloc_free ($1);
290 }
Carl Wortha807fb72010-05-18 22:10:04 -0700291| parameter_list ',' IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700292 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700293 talloc_free ($3);
294 $$ = $1;
295 }
296;
297
Carl Worthaaa9acb2010-05-19 13:28:24 -0700298replacement_list:
299 /* empty */ {
Carl Worth47252442010-05-19 13:54:37 -0700300 $$ = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700301 }
302| pp_tokens {
303 $$ = $1;
304 }
305;
306
307
308pp_tokens:
309 TOKEN {
Carl Worth47252442010-05-19 13:54:37 -0700310 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700311 _token_list_append ($$, $1.type, $1.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700312 }
313| pp_tokens TOKEN {
Carl Worthb5693832010-05-20 08:01:44 -0700314 _token_list_append ($1, $2.type, $2.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700315 $$ = $1;
316 }
317;
318
Carl Worth33cc4002010-05-12 12:17:10 -0700319%%
320
Carl Worth610053b2010-05-14 10:05:11 -0700321string_list_t *
322_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700323{
Carl Worth610053b2010-05-14 10:05:11 -0700324 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700325
Carl Worth610053b2010-05-14 10:05:11 -0700326 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700327 list->head = NULL;
328 list->tail = NULL;
329
330 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700331}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700332
Carl Worth33cc4002010-05-12 12:17:10 -0700333void
Carl Worth610053b2010-05-14 10:05:11 -0700334_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700335{
336 if (list->head == NULL) {
337 list->head = tail->head;
338 } else {
339 list->tail->next = tail->head;
340 }
341
342 list->tail = tail->tail;
343}
344
345void
Carl Worth610053b2010-05-14 10:05:11 -0700346_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700347{
Carl Worth610053b2010-05-14 10:05:11 -0700348 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700349
Carl Worth610053b2010-05-14 10:05:11 -0700350 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700351 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700352
353 node->next = NULL;
354
355 if (list->head == NULL) {
356 list->head = node;
357 } else {
358 list->tail->next = node;
359 }
360
361 list->tail = node;
362}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700363
364int
Carl Worth610053b2010-05-14 10:05:11 -0700365_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700366{
Carl Worth610053b2010-05-14 10:05:11 -0700367 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700368 int i;
369
370 if (list == NULL)
371 return 0;
372
373 for (i = 0, node = list->head; node; i++, node = node->next) {
374 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700375 if (index)
376 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700377 return 1;
378 }
379 }
380
381 return 0;
382}
383
384int
Carl Worth610053b2010-05-14 10:05:11 -0700385_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700386{
387 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700388 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700389
390 if (list == NULL)
391 return 0;
392
393 for (node = list->head; node; node = node->next)
394 length++;
395
396 return length;
397}
398
Carl Worth2be8be02010-05-14 10:31:43 -0700399void
400_print_string_list (string_list_t *list)
401{
402 string_node_t *node;
403
404 if (list == NULL)
405 return;
406
Carl Worth81f01432010-05-14 17:08:45 -0700407 for (node = list->head; node; node = node->next) {
Carl Worth2be8be02010-05-14 10:31:43 -0700408 printf ("%s", node->str);
Carl Worth81f01432010-05-14 17:08:45 -0700409 if (node->next)
410 printf (" ");
411 }
Carl Worth2be8be02010-05-14 10:31:43 -0700412}
413
Carl Worth8f6a8282010-05-14 10:44:19 -0700414argument_list_t *
415_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700416{
Carl Worth8f6a8282010-05-14 10:44:19 -0700417 argument_list_t *list;
418
419 list = xtalloc (ctx, argument_list_t);
420 list->head = NULL;
421 list->tail = NULL;
422
423 return list;
424}
425
426void
Carl Worth47252442010-05-19 13:54:37 -0700427_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700428{
429 argument_node_t *node;
430
431 if (argument == NULL || argument->head == NULL)
432 return;
433
434 node = xtalloc (list, argument_node_t);
435 node->argument = argument;
436
437 node->next = NULL;
438
439 if (list->head == NULL) {
440 list->head = node;
441 } else {
442 list->tail->next = node;
443 }
444
445 list->tail = node;
446}
447
448int
449_argument_list_length (argument_list_t *list)
450{
451 int length = 0;
452 argument_node_t *node;
453
454 if (list == NULL)
455 return 0;
456
457 for (node = list->head; node; node = node->next)
458 length++;
459
460 return length;
461}
462
Carl Worth47252442010-05-19 13:54:37 -0700463token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700464_argument_list_member_at (argument_list_t *list, int index)
465{
466 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700467 int i;
468
469 if (list == NULL)
470 return NULL;
471
472 node = list->head;
473 for (i = 0; i < index; i++) {
474 node = node->next;
475 if (node == NULL)
476 break;
477 }
478
479 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700480 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700481
482 return NULL;
483}
Carl Worth47252442010-05-19 13:54:37 -0700484
485token_list_t *
486_token_list_create (void *ctx)
487{
488 token_list_t *list;
489
490 list = xtalloc (ctx, token_list_t);
491 list->head = NULL;
492 list->tail = NULL;
493
494 return list;
495}
496
497void
498_token_list_append (token_list_t *list, int type, const char *value)
499{
500 token_node_t *node;
501
502 node = xtalloc (list, token_node_t);
503 node->type = type;
504 node->value = xtalloc_strdup (list, value);
505
506 node->next = NULL;
507
508 if (list->head == NULL) {
509 list->head = node;
510 } else {
511 list->tail->next = node;
512 }
513
514 list->tail = node;
515}
516
517void
518_token_list_append_list (token_list_t *list, token_list_t *tail)
519{
520 if (list->head == NULL) {
521 list->head = tail->head;
522 } else {
523 list->tail->next = tail->head;
524 }
525
526 list->tail = tail->tail;
527}
Carl Worth33cc4002010-05-12 12:17:10 -0700528
Carl Worth3a37b872010-05-10 11:44:09 -0700529void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700530yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700531{
532 fprintf (stderr, "Parse error: %s\n", error);
533}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700534
Carl Worth33cc4002010-05-12 12:17:10 -0700535glcpp_parser_t *
536glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700537{
Carl Worth33cc4002010-05-12 12:17:10 -0700538 glcpp_parser_t *parser;
539
Carl Worth5070a202010-05-12 12:45:33 -0700540 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700541
Carl Worth8f38aff2010-05-19 10:01:29 -0700542 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700543 parser->defines = hash_table_ctor (32, hash_table_string_hash,
544 hash_table_string_compare);
Carl Wortha807fb72010-05-18 22:10:04 -0700545 parser->expansions = NULL;
546
Carl Worth33cc4002010-05-12 12:17:10 -0700547 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700548}
549
550int
551glcpp_parser_parse (glcpp_parser_t *parser)
552{
553 return yyparse (parser);
554}
555
556void
Carl Worth33cc4002010-05-12 12:17:10 -0700557glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700558{
Carl Worth8f38aff2010-05-19 10:01:29 -0700559 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700560 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700561 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700562}
Carl Worthc6d5af32010-05-11 12:30:09 -0700563
Carl Worthbe0e2e92010-05-19 07:29:22 -0700564static int
565glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
566{
567 expansion_node_t *node;
568
569 for (node = parser->expansions; node; node = node->next) {
570 if (node->macro &&
571 strcmp (node->macro->identifier, member) == 0)
572 {
573 return 1;
574 }
575 }
576
577 return 0;
578}
579
Carl Wortha807fb72010-05-18 22:10:04 -0700580token_class_t
581glcpp_parser_classify_token (glcpp_parser_t *parser,
582 const char *identifier,
583 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700584{
Carl Worthfcbbb462010-05-13 09:36:23 -0700585 macro_t *macro;
586
Carl Wortha807fb72010-05-18 22:10:04 -0700587 /* First we check if we are currently expanding a
588 * function-like macro, and if so, whether the parameter list
589 * contains a parameter matching this token name. */
590 if (parser->expansions &&
591 parser->expansions->macro &&
592 parser->expansions->macro->parameters)
593 {
594 string_list_t *list;
595
596 list = parser->expansions->macro->parameters;
597
598 if (_string_list_contains (list, identifier, parameter_index))
599 return TOKEN_CLASS_ARGUMENT;
600 }
601
602 /* If not a function-like macro parameter, we next check if
603 * this token is a macro itself. */
604
Carl Worthfcbbb462010-05-13 09:36:23 -0700605 macro = hash_table_find (parser->defines, identifier);
606
607 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700608 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700609
Carl Worthbe0e2e92010-05-19 07:29:22 -0700610 /* Don't consider this a macro if we are already actively
611 * expanding this macro. */
612 if (glcpp_parser_is_expanding (parser, identifier))
Carl Worthb5693832010-05-20 08:01:44 -0700613 return TOKEN_CLASS_IDENTIFIER_FINALIZED;
Carl Worthbe0e2e92010-05-19 07:29:22 -0700614
615 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700616 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700617 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700618 else
Carl Wortha807fb72010-05-18 22:10:04 -0700619 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700620}
621
Carl Worth33cc4002010-05-12 12:17:10 -0700622void
Carl Worthfcbbb462010-05-13 09:36:23 -0700623_define_object_macro (glcpp_parser_t *parser,
624 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -0700625 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700626{
627 macro_t *macro;
628
629 macro = xtalloc (parser, macro_t);
630
631 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700632 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700633 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700634 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700635
636 hash_table_insert (parser->defines, macro, identifier);
637}
638
639void
640_define_function_macro (glcpp_parser_t *parser,
641 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700642 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -0700643 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700644{
645 macro_t *macro;
646
647 macro = xtalloc (parser, macro_t);
648
649 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700650 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700651 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700652 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700653
654 hash_table_insert (parser->defines, macro, identifier);
655}
656
Carl Wortha807fb72010-05-18 22:10:04 -0700657static void
658_glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
659 macro_t *macro,
660 argument_list_t *arguments,
Carl Worth47252442010-05-19 13:54:37 -0700661 token_node_t *replacements)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700662{
Carl Wortha807fb72010-05-18 22:10:04 -0700663 expansion_node_t *node;
664
665 node = xtalloc (parser, expansion_node_t);
666
667 node->macro = macro;
668 node->arguments = arguments;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700669 node->replacements = replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700670
671 node->next = parser->expansions;
672 parser->expansions = node;
Carl Wortha807fb72010-05-18 22:10:04 -0700673}
674
Carl Worthaaa9acb2010-05-19 13:28:24 -0700675static void
Carl Wortha807fb72010-05-18 22:10:04 -0700676glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
677 macro_t *macro,
678 argument_list_t *arguments)
679{
680 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700681 macro->replacements->head);
Carl Wortha807fb72010-05-18 22:10:04 -0700682}
683
684void
685glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
686 int argument_index)
687{
688 argument_list_t *arguments;
Carl Worth47252442010-05-19 13:54:37 -0700689 token_list_t *argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700690
Carl Wortha807fb72010-05-18 22:10:04 -0700691 arguments = parser->expansions->arguments;
Carl Worth2be8be02010-05-14 10:31:43 -0700692
Carl Wortha807fb72010-05-18 22:10:04 -0700693 argument = _argument_list_member_at (arguments, argument_index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700694
Carl Wortha807fb72010-05-18 22:10:04 -0700695 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700696 argument->head);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700697}
698
Carl Worthaaa9acb2010-05-19 13:28:24 -0700699static void
Carl Wortha807fb72010-05-18 22:10:04 -0700700glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700701{
Carl Wortha807fb72010-05-18 22:10:04 -0700702 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700703
Carl Wortha807fb72010-05-18 22:10:04 -0700704 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700705
Carl Wortha807fb72010-05-18 22:10:04 -0700706 if (node == NULL) {
707 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
708 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700709 }
710
Carl Wortha807fb72010-05-18 22:10:04 -0700711 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700712
Carl Wortha807fb72010-05-18 22:10:04 -0700713 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700714}
715
Carl Wortha807fb72010-05-18 22:10:04 -0700716void
Carl Worth2be8be02010-05-14 10:31:43 -0700717_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700718{
Carl Worthfcbbb462010-05-13 09:36:23 -0700719 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700720
Carl Worthfcbbb462010-05-13 09:36:23 -0700721 macro = hash_table_find (parser->defines, identifier);
722 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700723 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700724
Carl Worthbe0e2e92010-05-19 07:29:22 -0700725 glcpp_parser_push_expansion_macro (parser, macro, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700726}
727
Carl Wortha807fb72010-05-18 22:10:04 -0700728void
Carl Worth2be8be02010-05-14 10:31:43 -0700729_expand_function_macro (glcpp_parser_t *parser,
730 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700731 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700732{
Carl Worthfcbbb462010-05-13 09:36:23 -0700733 macro_t *macro;
734
735 macro = hash_table_find (parser->defines, identifier);
736 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700737 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700738
Carl Worth8f6a8282010-05-14 10:44:19 -0700739 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700740 _string_list_length (macro->parameters))
741 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700742 fprintf (stderr,
743 "Error: macro %s invoked with %d arguments (expected %d)\n",
744 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700745 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700746 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700747 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700748 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700749
Carl Worthbe0e2e92010-05-19 07:29:22 -0700750 glcpp_parser_push_expansion_macro (parser, macro, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700751}
Carl Worth8f38aff2010-05-19 10:01:29 -0700752
753static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700754glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700755{
Carl Worthaaa9acb2010-05-19 13:28:24 -0700756 expansion_node_t *expansion;
Carl Worth47252442010-05-19 13:54:37 -0700757 token_node_t *replacements;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700758 int parameter_index;
759
760 /* Who says C can't do efficient tail recursion? */
761 RECURSE:
762
763 expansion = parser->expansions;
764
765 if (expansion == NULL)
766 return glcpp_lex (parser->scanner);
767
768 replacements = expansion->replacements;
769
770 /* Pop expansion when replacements is exhausted. */
771 if (replacements == NULL) {
772 glcpp_parser_pop_expansion (parser);
773 goto RECURSE;
774 }
775
776 expansion->replacements = replacements->next;
777
Carl Worth47252442010-05-19 13:54:37 -0700778 if (strcmp (replacements->value, "(") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700779 return '(';
Carl Worth47252442010-05-19 13:54:37 -0700780 else if (strcmp (replacements->value, ")") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700781 return ')';
Carl Worth47252442010-05-19 13:54:37 -0700782 else if (strcmp (replacements->value, ",") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700783 return ',';
784
Carl Worth47252442010-05-19 13:54:37 -0700785 yylval.str = xtalloc_strdup (parser, replacements->value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700786
Carl Worthb5693832010-05-20 08:01:44 -0700787 /* Carefully refuse to expand any finalized identifier. */
788 if (replacements->type == IDENTIFIER_FINALIZED)
789 return IDENTIFIER_FINALIZED;
790
Carl Worthaaa9acb2010-05-19 13:28:24 -0700791 switch (glcpp_parser_classify_token (parser, yylval.str,
792 &parameter_index))
793 {
794 case TOKEN_CLASS_ARGUMENT:
795 talloc_free (yylval.str);
796 glcpp_parser_push_expansion_argument (parser,
797 parameter_index);
798 goto RECURSE;
799 break;
800 case TOKEN_CLASS_IDENTIFIER:
801 return IDENTIFIER;
802 break;
Carl Worthb5693832010-05-20 08:01:44 -0700803 case TOKEN_CLASS_IDENTIFIER_FINALIZED:
804 return IDENTIFIER_FINALIZED;
805 break;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700806 case TOKEN_CLASS_FUNC_MACRO:
807 return FUNC_MACRO;
808 break;
809 default:
810 case TOKEN_CLASS_OBJ_MACRO:
811 return OBJ_MACRO;
812 break;
813 }
Carl Worth8f38aff2010-05-19 10:01:29 -0700814}