blob: 79a8ec2cf2a08a801a9ff409cccc4837ff461532 [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
Carl Worth610053b2010-05-14 10:05:11 -070053string_list_t *
54_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070055
56void
Carl Worth610053b2010-05-14 10:05:11 -070057_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070058
59void
Carl Worth610053b2010-05-14 10:05:11 -070060_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070061
Carl Worthdcc2ecd2010-05-13 12:56:42 -070062int
Carl Worth610053b2010-05-14 10:05:11 -070063_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070064
Carl Worthdcc2ecd2010-05-13 12:56:42 -070065int
Carl Worth610053b2010-05-14 10:05:11 -070066_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070067
Carl Worth8f6a8282010-05-14 10:44:19 -070068argument_list_t *
69_argument_list_create (void *ctx);
70
71void
Carl Worth47252442010-05-19 13:54:37 -070072_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070073
74int
75_argument_list_length (argument_list_t *list);
76
Carl Worth47252442010-05-19 13:54:37 -070077token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070078_argument_list_member_at (argument_list_t *list, int index);
79
Carl Worth47252442010-05-19 13:54:37 -070080token_list_t *
81_token_list_create (void *ctx);
82
83void
84_token_list_append (token_list_t *list, int type, const char *value);
85
86void
87_token_list_append_list (token_list_t *list, token_list_t *tail);
88
Carl Worthaaa9acb2010-05-19 13:28:24 -070089static void
90glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
91 macro_t *macro,
92 argument_list_t *arguments);
93
94static void
95glcpp_parser_pop_expansion (glcpp_parser_t *parser);
96
Carl Worth0293b2e2010-05-19 10:05:40 -070097#define yylex glcpp_parser_lex
98
Carl Worth8f38aff2010-05-19 10:01:29 -070099static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700100glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700101
Carl Worth3a37b872010-05-10 11:44:09 -0700102%}
103
Carl Worth33cc4002010-05-12 12:17:10 -0700104%union {
105 char *str;
Carl Worth8f6a8282010-05-14 10:44:19 -0700106 argument_list_t *argument_list;
Carl Worth47252442010-05-19 13:54:37 -0700107 string_list_t *string_list;
Carl Worthb5693832010-05-20 08:01:44 -0700108 token_t token;
Carl Worth47252442010-05-19 13:54:37 -0700109 token_list_t *token_list;
Carl Worth33cc4002010-05-12 12:17:10 -0700110}
111
Carl Worth0b27b5f2010-05-10 16:16:06 -0700112%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700113%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700114
Carl Worthb5693832010-05-20 08:01:44 -0700115%token DEFINE FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO NEWLINE SPACE TOKEN UNDEF
116%type <str> FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO
Carl Worth8f6a8282010-05-14 10:44:19 -0700117%type <argument_list> argument_list
Carl Worth47252442010-05-19 13:54:37 -0700118%type <string_list> macro parameter_list
Carl Worth9f3d2c42010-05-20 08:42:02 -0700119%type <token> TOKEN argument_word argument_word_or_comma
120%type <token_list> argument argument_or_comma replacement_list pp_tokens
Carl Worth3a37b872010-05-10 11:44:09 -0700121
Carl Worth796e1f02010-05-17 12:45:16 -0700122/* Hard to remove shift/reduce conflicts documented as follows:
123 *
124 * 1. '(' after FUNC_MACRO name which is correctly resolved to shift
125 * to form macro invocation rather than reducing directly to
126 * content.
Carl Worth69f390d2010-05-19 07:42:42 -0700127 *
128 * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
129 * shift to form macro invocation rather than reducing directly to
130 * argument.
Carl Worth9f3d2c42010-05-20 08:42:02 -0700131 *
132 * 3. Similarly again now that we added argument_or_comma as well.
Carl Worth796e1f02010-05-17 12:45:16 -0700133 */
Carl Worth9f3d2c42010-05-20 08:42:02 -0700134%expect 3
Carl Worth796e1f02010-05-17 12:45:16 -0700135
Carl Worth3a37b872010-05-10 11:44:09 -0700136%%
137
Carl Worth33cc4002010-05-12 12:17:10 -0700138input:
139 /* empty */
Carl Worth8bcb6f12010-05-12 13:21:20 -0700140| input content
Carl Worth3a37b872010-05-10 11:44:09 -0700141;
142
Carl Worth04af1352010-05-14 10:17:38 -0700143 /* We do all printing at the content level */
Carl Worth33cc4002010-05-12 12:17:10 -0700144content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700145 IDENTIFIER {
146 printf ("%s", $1);
147 talloc_free ($1);
148 }
Carl Worthb5693832010-05-20 08:01:44 -0700149| IDENTIFIER_FINALIZED {
Carl Worth9f62a7e2010-05-13 07:38:29 -0700150 printf ("%s", $1);
151 talloc_free ($1);
152 }
Carl Worthb5693832010-05-20 08:01:44 -0700153| TOKEN {
154 printf ("%s", $1.value);
155 talloc_free ($1.value);
156 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700157| FUNC_MACRO {
158 printf ("%s", $1);
159 talloc_free ($1);
160 }
Carl Wortha807fb72010-05-18 22:10:04 -0700161| directive {
162 printf ("\n");
Carl Worth2be8be02010-05-14 10:31:43 -0700163 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700164| '(' { printf ("("); }
165| ')' { printf (")"); }
166| ',' { printf (","); }
Carl Wortha807fb72010-05-18 22:10:04 -0700167| macro
Carl Worthcd27e642010-05-12 13:11:50 -0700168;
169
Carl Worthfcbbb462010-05-13 09:36:23 -0700170macro:
171 FUNC_MACRO '(' argument_list ')' {
Carl Wortha807fb72010-05-18 22:10:04 -0700172 _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700173 }
174| OBJ_MACRO {
Carl Wortha807fb72010-05-18 22:10:04 -0700175 _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700176 talloc_free ($1);
177 }
178;
179
180argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700181 /* empty */ {
182 $$ = _argument_list_create (parser);
183 }
184| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700185 $$ = _argument_list_create (parser);
186 _argument_list_append ($$, $1);
187 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700188| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700189 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700190 $$ = $1;
191 }
192;
193
194argument:
Carl Worth59ca9892010-05-19 07:49:47 -0700195 argument_word {
Carl Worth47252442010-05-19 13:54:37 -0700196 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700197 _token_list_append ($$, $1.type, $1.value);
Carl Worthfcbbb462010-05-13 09:36:23 -0700198 }
Carl Worth59ca9892010-05-19 07:49:47 -0700199| argument argument_word {
Carl Worthb5693832010-05-20 08:01:44 -0700200 _token_list_append ($1, $2.type, $2.value);
201 talloc_free ($2.value);
Carl Worth3596bb12010-05-14 16:53:52 -0700202 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700203 }
Carl Worth9f3d2c42010-05-20 08:42:02 -0700204| argument '(' argument_or_comma ')' {
Carl Worth47252442010-05-19 13:54:37 -0700205 _token_list_append ($1, '(', "(");
206 _token_list_append_list ($1, $3);
207 _token_list_append ($1, ')', ")");
Carl Worth3596bb12010-05-14 16:53:52 -0700208 $$ = $1;
209 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700210;
211
Carl Worth59ca9892010-05-19 07:49:47 -0700212argument_word:
Carl Worthb5693832010-05-20 08:01:44 -0700213 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
214| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
Carl Worth59ca9892010-05-19 07:49:47 -0700215| TOKEN { $$ = $1; }
Carl Worthb5693832010-05-20 08:01:44 -0700216| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
217| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
Carl Worth59ca9892010-05-19 07:49:47 -0700218;
219
Carl Worth9f3d2c42010-05-20 08:42:02 -0700220 /* XXX: The body of argument_or_comma is the same as the body
221 * of argument, but with "argument" and "argument_word"
222 * changed to "argument_or_comma" and
223 * "argument_word_or_comma". It would be nice to have less
224 * redundancy here, but I'm not sure how.
225 *
226 * It would also be nice to have a less ugly grammar to have
227 * to implement, but such is the C preprocessor.
228 */
229argument_or_comma:
230 argument_word_or_comma {
231 $$ = _token_list_create (parser);
232 _token_list_append ($$, $1.type, $1.value);
233 }
234| argument_or_comma argument_word_or_comma {
235 _token_list_append ($1, $2.type, $2.value);
236 $$ = $1;
237 }
238| argument_or_comma '(' argument_or_comma ')' {
239 _token_list_append ($1, '(', "(");
240 _token_list_append_list ($1, $3);
241 _token_list_append ($1, ')', ")");
242 $$ = $1;
243 }
244;
245
246argument_word_or_comma:
247 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
248| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
249| TOKEN { $$ = $1; }
250| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
251| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
252| ',' { $$.type = ','; $$.value = xtalloc_strdup (parser, ","); }
253;
Carl Worth59ca9892010-05-19 07:49:47 -0700254
Carl Worth33cc4002010-05-12 12:17:10 -0700255directive:
Carl Worthaaa9acb2010-05-19 13:28:24 -0700256 DEFINE IDENTIFIER NEWLINE {
Carl Worth47252442010-05-19 13:54:37 -0700257 token_list_t *list = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700258 _define_object_macro (parser, $2, list);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700259 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700260| DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
261 _define_object_macro (parser, $2, $4);
262 }
263| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
Carl Worth81f01432010-05-14 17:08:45 -0700264 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700265 }
Carl Wortha807fb72010-05-18 22:10:04 -0700266| UNDEF IDENTIFIER {
267 string_list_t *macro = hash_table_find (parser->defines, $2);
268 if (macro) {
Carl Worthfcbbb462010-05-13 09:36:23 -0700269 /* XXX: Need hash table to support a real way
270 * to remove an element rather than prefixing
271 * a new node with data of NULL like this. */
272 hash_table_insert (parser->defines, NULL, $2);
Carl Wortha807fb72010-05-18 22:10:04 -0700273 talloc_free (macro);
Carl Worthfcbbb462010-05-13 09:36:23 -0700274 }
275 talloc_free ($2);
276 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700277;
278
Carl Worthfcbbb462010-05-13 09:36:23 -0700279parameter_list:
280 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700281 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700282 }
Carl Wortha807fb72010-05-18 22:10:04 -0700283| IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700284 $$ = _string_list_create (parser);
285 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700286 talloc_free ($1);
287 }
Carl Wortha807fb72010-05-18 22:10:04 -0700288| parameter_list ',' IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700289 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700290 talloc_free ($3);
291 $$ = $1;
292 }
293;
294
Carl Worthaaa9acb2010-05-19 13:28:24 -0700295replacement_list:
296 /* empty */ {
Carl Worth47252442010-05-19 13:54:37 -0700297 $$ = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700298 }
299| pp_tokens {
300 $$ = $1;
301 }
302;
303
304
305pp_tokens:
306 TOKEN {
Carl Worth47252442010-05-19 13:54:37 -0700307 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700308 _token_list_append ($$, $1.type, $1.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700309 }
310| pp_tokens TOKEN {
Carl Worthb5693832010-05-20 08:01:44 -0700311 _token_list_append ($1, $2.type, $2.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700312 $$ = $1;
313 }
314;
315
Carl Worth33cc4002010-05-12 12:17:10 -0700316%%
317
Carl Worth610053b2010-05-14 10:05:11 -0700318string_list_t *
319_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700320{
Carl Worth610053b2010-05-14 10:05:11 -0700321 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700322
Carl Worth610053b2010-05-14 10:05:11 -0700323 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700324 list->head = NULL;
325 list->tail = NULL;
326
327 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700328}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700329
Carl Worth33cc4002010-05-12 12:17:10 -0700330void
Carl Worth610053b2010-05-14 10:05:11 -0700331_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700332{
333 if (list->head == NULL) {
334 list->head = tail->head;
335 } else {
336 list->tail->next = tail->head;
337 }
338
339 list->tail = tail->tail;
340}
341
342void
Carl Worth610053b2010-05-14 10:05:11 -0700343_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700344{
Carl Worth610053b2010-05-14 10:05:11 -0700345 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700346
Carl Worth610053b2010-05-14 10:05:11 -0700347 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700348 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700349
350 node->next = NULL;
351
352 if (list->head == NULL) {
353 list->head = node;
354 } else {
355 list->tail->next = node;
356 }
357
358 list->tail = node;
359}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700360
361int
Carl Worth610053b2010-05-14 10:05:11 -0700362_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700363{
Carl Worth610053b2010-05-14 10:05:11 -0700364 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700365 int i;
366
367 if (list == NULL)
368 return 0;
369
370 for (i = 0, node = list->head; node; i++, node = node->next) {
371 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700372 if (index)
373 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700374 return 1;
375 }
376 }
377
378 return 0;
379}
380
381int
Carl Worth610053b2010-05-14 10:05:11 -0700382_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700383{
384 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700385 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700386
387 if (list == NULL)
388 return 0;
389
390 for (node = list->head; node; node = node->next)
391 length++;
392
393 return length;
394}
395
Carl Worth8f6a8282010-05-14 10:44:19 -0700396argument_list_t *
397_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700398{
Carl Worth8f6a8282010-05-14 10:44:19 -0700399 argument_list_t *list;
400
401 list = xtalloc (ctx, argument_list_t);
402 list->head = NULL;
403 list->tail = NULL;
404
405 return list;
406}
407
408void
Carl Worth47252442010-05-19 13:54:37 -0700409_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700410{
411 argument_node_t *node;
412
413 if (argument == NULL || argument->head == NULL)
414 return;
415
416 node = xtalloc (list, argument_node_t);
417 node->argument = argument;
418
419 node->next = NULL;
420
421 if (list->head == NULL) {
422 list->head = node;
423 } else {
424 list->tail->next = node;
425 }
426
427 list->tail = node;
428}
429
430int
431_argument_list_length (argument_list_t *list)
432{
433 int length = 0;
434 argument_node_t *node;
435
436 if (list == NULL)
437 return 0;
438
439 for (node = list->head; node; node = node->next)
440 length++;
441
442 return length;
443}
444
Carl Worth47252442010-05-19 13:54:37 -0700445token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700446_argument_list_member_at (argument_list_t *list, int index)
447{
448 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700449 int i;
450
451 if (list == NULL)
452 return NULL;
453
454 node = list->head;
455 for (i = 0; i < index; i++) {
456 node = node->next;
457 if (node == NULL)
458 break;
459 }
460
461 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700462 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700463
464 return NULL;
465}
Carl Worth47252442010-05-19 13:54:37 -0700466
467token_list_t *
468_token_list_create (void *ctx)
469{
470 token_list_t *list;
471
472 list = xtalloc (ctx, token_list_t);
473 list->head = NULL;
474 list->tail = NULL;
475
476 return list;
477}
478
479void
480_token_list_append (token_list_t *list, int type, const char *value)
481{
482 token_node_t *node;
483
484 node = xtalloc (list, token_node_t);
485 node->type = type;
486 node->value = xtalloc_strdup (list, value);
487
488 node->next = NULL;
489
490 if (list->head == NULL) {
491 list->head = node;
492 } else {
493 list->tail->next = node;
494 }
495
496 list->tail = node;
497}
498
499void
500_token_list_append_list (token_list_t *list, token_list_t *tail)
501{
502 if (list->head == NULL) {
503 list->head = tail->head;
504 } else {
505 list->tail->next = tail->head;
506 }
507
508 list->tail = tail->tail;
509}
Carl Worth33cc4002010-05-12 12:17:10 -0700510
Carl Worth3a37b872010-05-10 11:44:09 -0700511void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700512yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700513{
514 fprintf (stderr, "Parse error: %s\n", error);
515}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700516
Carl Worth33cc4002010-05-12 12:17:10 -0700517glcpp_parser_t *
518glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700519{
Carl Worth33cc4002010-05-12 12:17:10 -0700520 glcpp_parser_t *parser;
521
Carl Worth5070a202010-05-12 12:45:33 -0700522 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700523
Carl Worth8f38aff2010-05-19 10:01:29 -0700524 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700525 parser->defines = hash_table_ctor (32, hash_table_string_hash,
526 hash_table_string_compare);
Carl Wortha807fb72010-05-18 22:10:04 -0700527 parser->expansions = NULL;
528
Carl Worth33cc4002010-05-12 12:17:10 -0700529 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700530}
531
532int
533glcpp_parser_parse (glcpp_parser_t *parser)
534{
535 return yyparse (parser);
536}
537
538void
Carl Worth33cc4002010-05-12 12:17:10 -0700539glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700540{
Carl Worth8f38aff2010-05-19 10:01:29 -0700541 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700542 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700543 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700544}
Carl Worthc6d5af32010-05-11 12:30:09 -0700545
Carl Worthbe0e2e92010-05-19 07:29:22 -0700546static int
547glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
548{
549 expansion_node_t *node;
550
551 for (node = parser->expansions; node; node = node->next) {
552 if (node->macro &&
553 strcmp (node->macro->identifier, member) == 0)
554 {
555 return 1;
556 }
557 }
558
559 return 0;
560}
561
Carl Wortha807fb72010-05-18 22:10:04 -0700562token_class_t
563glcpp_parser_classify_token (glcpp_parser_t *parser,
564 const char *identifier,
565 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700566{
Carl Worthfcbbb462010-05-13 09:36:23 -0700567 macro_t *macro;
568
Carl Wortha807fb72010-05-18 22:10:04 -0700569 /* First we check if we are currently expanding a
570 * function-like macro, and if so, whether the parameter list
571 * contains a parameter matching this token name. */
572 if (parser->expansions &&
573 parser->expansions->macro &&
574 parser->expansions->macro->parameters)
575 {
576 string_list_t *list;
577
578 list = parser->expansions->macro->parameters;
579
580 if (_string_list_contains (list, identifier, parameter_index))
581 return TOKEN_CLASS_ARGUMENT;
582 }
583
584 /* If not a function-like macro parameter, we next check if
585 * this token is a macro itself. */
586
Carl Worthfcbbb462010-05-13 09:36:23 -0700587 macro = hash_table_find (parser->defines, identifier);
588
589 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700590 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700591
Carl Worthbe0e2e92010-05-19 07:29:22 -0700592 /* Don't consider this a macro if we are already actively
593 * expanding this macro. */
594 if (glcpp_parser_is_expanding (parser, identifier))
Carl Worthb5693832010-05-20 08:01:44 -0700595 return TOKEN_CLASS_IDENTIFIER_FINALIZED;
Carl Worthbe0e2e92010-05-19 07:29:22 -0700596
597 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700598 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700599 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700600 else
Carl Wortha807fb72010-05-18 22:10:04 -0700601 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700602}
603
Carl Worth33cc4002010-05-12 12:17:10 -0700604void
Carl Worthfcbbb462010-05-13 09:36:23 -0700605_define_object_macro (glcpp_parser_t *parser,
606 const char *identifier,
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 = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700614 macro->parameters = NULL;
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
621void
622_define_function_macro (glcpp_parser_t *parser,
623 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700624 string_list_t *parameters,
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 = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700632 macro->parameters = talloc_steal (macro, parameters);
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
Carl Wortha807fb72010-05-18 22:10:04 -0700639static void
640_glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
641 macro_t *macro,
642 argument_list_t *arguments,
Carl Worth47252442010-05-19 13:54:37 -0700643 token_node_t *replacements)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700644{
Carl Wortha807fb72010-05-18 22:10:04 -0700645 expansion_node_t *node;
646
647 node = xtalloc (parser, expansion_node_t);
648
649 node->macro = macro;
650 node->arguments = arguments;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700651 node->replacements = replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700652
653 node->next = parser->expansions;
654 parser->expansions = node;
Carl Wortha807fb72010-05-18 22:10:04 -0700655}
656
Carl Worthaaa9acb2010-05-19 13:28:24 -0700657static void
Carl Wortha807fb72010-05-18 22:10:04 -0700658glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
659 macro_t *macro,
660 argument_list_t *arguments)
661{
662 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700663 macro->replacements->head);
Carl Wortha807fb72010-05-18 22:10:04 -0700664}
665
666void
667glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
668 int argument_index)
669{
670 argument_list_t *arguments;
Carl Worth47252442010-05-19 13:54:37 -0700671 token_list_t *argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700672
Carl Wortha807fb72010-05-18 22:10:04 -0700673 arguments = parser->expansions->arguments;
Carl Worth2be8be02010-05-14 10:31:43 -0700674
Carl Wortha807fb72010-05-18 22:10:04 -0700675 argument = _argument_list_member_at (arguments, argument_index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700676
Carl Wortha807fb72010-05-18 22:10:04 -0700677 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700678 argument->head);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700679}
680
Carl Worthaaa9acb2010-05-19 13:28:24 -0700681static void
Carl Wortha807fb72010-05-18 22:10:04 -0700682glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700683{
Carl Wortha807fb72010-05-18 22:10:04 -0700684 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700685
Carl Wortha807fb72010-05-18 22:10:04 -0700686 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700687
Carl Wortha807fb72010-05-18 22:10:04 -0700688 if (node == NULL) {
689 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
690 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700691 }
692
Carl Wortha807fb72010-05-18 22:10:04 -0700693 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700694
Carl Wortha807fb72010-05-18 22:10:04 -0700695 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700696}
697
Carl Wortha807fb72010-05-18 22:10:04 -0700698void
Carl Worth2be8be02010-05-14 10:31:43 -0700699_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700700{
Carl Worthfcbbb462010-05-13 09:36:23 -0700701 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700702
Carl Worthfcbbb462010-05-13 09:36:23 -0700703 macro = hash_table_find (parser->defines, identifier);
704 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700705 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700706
Carl Worthbe0e2e92010-05-19 07:29:22 -0700707 glcpp_parser_push_expansion_macro (parser, macro, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700708}
709
Carl Wortha807fb72010-05-18 22:10:04 -0700710void
Carl Worth2be8be02010-05-14 10:31:43 -0700711_expand_function_macro (glcpp_parser_t *parser,
712 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700713 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700714{
Carl Worthfcbbb462010-05-13 09:36:23 -0700715 macro_t *macro;
716
717 macro = hash_table_find (parser->defines, identifier);
718 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700719 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700720
Carl Worth8f6a8282010-05-14 10:44:19 -0700721 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700722 _string_list_length (macro->parameters))
723 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700724 fprintf (stderr,
725 "Error: macro %s invoked with %d arguments (expected %d)\n",
726 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700727 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700728 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700729 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700730 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700731
Carl Worthbe0e2e92010-05-19 07:29:22 -0700732 glcpp_parser_push_expansion_macro (parser, macro, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700733}
Carl Worth8f38aff2010-05-19 10:01:29 -0700734
735static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700736glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700737{
Carl Worthaaa9acb2010-05-19 13:28:24 -0700738 expansion_node_t *expansion;
Carl Worth47252442010-05-19 13:54:37 -0700739 token_node_t *replacements;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700740 int parameter_index;
741
742 /* Who says C can't do efficient tail recursion? */
743 RECURSE:
744
745 expansion = parser->expansions;
746
747 if (expansion == NULL)
748 return glcpp_lex (parser->scanner);
749
750 replacements = expansion->replacements;
751
752 /* Pop expansion when replacements is exhausted. */
753 if (replacements == NULL) {
754 glcpp_parser_pop_expansion (parser);
755 goto RECURSE;
756 }
757
758 expansion->replacements = replacements->next;
759
Carl Worth47252442010-05-19 13:54:37 -0700760 if (strcmp (replacements->value, "(") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700761 return '(';
Carl Worth47252442010-05-19 13:54:37 -0700762 else if (strcmp (replacements->value, ")") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700763 return ')';
Carl Worthaaa9acb2010-05-19 13:28:24 -0700764
Carl Worth47252442010-05-19 13:54:37 -0700765 yylval.str = xtalloc_strdup (parser, replacements->value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700766
Carl Worthb5693832010-05-20 08:01:44 -0700767 /* Carefully refuse to expand any finalized identifier. */
768 if (replacements->type == IDENTIFIER_FINALIZED)
769 return IDENTIFIER_FINALIZED;
770
Carl Worthaaa9acb2010-05-19 13:28:24 -0700771 switch (glcpp_parser_classify_token (parser, yylval.str,
772 &parameter_index))
773 {
774 case TOKEN_CLASS_ARGUMENT:
775 talloc_free (yylval.str);
776 glcpp_parser_push_expansion_argument (parser,
777 parameter_index);
778 goto RECURSE;
779 break;
780 case TOKEN_CLASS_IDENTIFIER:
781 return IDENTIFIER;
782 break;
Carl Worthb5693832010-05-20 08:01:44 -0700783 case TOKEN_CLASS_IDENTIFIER_FINALIZED:
784 return IDENTIFIER_FINALIZED;
785 break;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700786 case TOKEN_CLASS_FUNC_MACRO:
787 return FUNC_MACRO;
788 break;
789 default:
790 case TOKEN_CLASS_OBJ_MACRO:
791 return OBJ_MACRO;
792 break;
793 }
Carl Worth8f38aff2010-05-19 10:01:29 -0700794}