blob: b3ef177a6dac3153dcd7267be6a101c8a8b51204 [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 Worthae6517f2010-05-25 15:24:59 -070062void
63_string_list_push (string_list_t *list, const char *str);
64
65void
66_string_list_pop (string_list_t *list);
67
Carl Worthdcc2ecd2010-05-13 12:56:42 -070068int
Carl Worth610053b2010-05-14 10:05:11 -070069_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070070
Carl Worthdcc2ecd2010-05-13 12:56:42 -070071int
Carl Worth610053b2010-05-14 10:05:11 -070072_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070073
Carl Worth8f6a8282010-05-14 10:44:19 -070074argument_list_t *
75_argument_list_create (void *ctx);
76
77void
Carl Worth47252442010-05-19 13:54:37 -070078_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070079
80int
81_argument_list_length (argument_list_t *list);
82
Carl Worth47252442010-05-19 13:54:37 -070083token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070084_argument_list_member_at (argument_list_t *list, int index);
85
Carl Worth808401f2010-05-25 14:52:43 -070086/* Note: This function talloc_steal()s the str pointer. */
87token_t *
88_token_create_str (void *ctx, int type, char *str);
89
90token_t *
91_token_create_ival (void *ctx, int type, int ival);
92
Carl Worth47252442010-05-19 13:54:37 -070093token_list_t *
94_token_list_create (void *ctx);
95
Carl Worth808401f2010-05-25 14:52:43 -070096/* Note: This function add a talloc_reference() to token.
97 *
98 * You may want to talloc_unlink any current reference if you no
99 * longer need it. */
Carl Worth47252442010-05-19 13:54:37 -0700100void
Carl Worth808401f2010-05-25 14:52:43 -0700101_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -0700102
103void
104_token_list_append_list (token_list_t *list, token_list_t *tail);
105
Carl Worth808401f2010-05-25 14:52:43 -0700106void
Carl Worthae6517f2010-05-25 15:24:59 -0700107_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
108 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700109
Carl Worthaaa9acb2010-05-19 13:28:24 -0700110static void
Carl Worthaaa9acb2010-05-19 13:28:24 -0700111glcpp_parser_pop_expansion (glcpp_parser_t *parser);
112
Carl Worthb20d33c2010-05-20 22:27:07 -0700113static void
114_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
115
116static void
117_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
118 int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700119
Carl Worthb20d33c2010-05-20 22:27:07 -0700120static void
121_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
122
Carl Worth0293b2e2010-05-19 10:05:40 -0700123#define yylex glcpp_parser_lex
124
Carl Worth8f38aff2010-05-19 10:01:29 -0700125static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700126glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700127
Carl Worth3a37b872010-05-10 11:44:09 -0700128%}
129
Carl Worth33cc4002010-05-12 12:17:10 -0700130%union {
Carl Worth005b3202010-05-20 14:19:57 -0700131 int ival;
Carl Worth33cc4002010-05-12 12:17:10 -0700132 char *str;
Carl Worth808401f2010-05-25 14:52:43 -0700133 token_t *token;
Carl Worth47252442010-05-19 13:54:37 -0700134 token_list_t *token_list;
Carl Worth33cc4002010-05-12 12:17:10 -0700135}
136
Carl Worth0b27b5f2010-05-10 16:16:06 -0700137%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700138%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700139
Carl Worth3ff81672010-05-25 13:09:03 -0700140%token HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH IDENTIFIER NEWLINE OTHER HASH_UNDEF
141%token LEFT_SHIFT RIGHT_SHIFT LESS_OR_EQUAL GREATER_OR_EQUAL EQUAL NOT_EQUAL AND OR PASTE
Carl Worth808401f2010-05-25 14:52:43 -0700142%type <ival> punctuator
143%type <str> IDENTIFIER OTHER
144%type <token> preprocessing_token
145%type <token_list> pp_tokens replacement_list text_line
Carl Worth3a37b872010-05-10 11:44:09 -0700146
Carl Worth3ff81672010-05-25 13:09:03 -0700147 /* Stale stuff just to allow code to compile. */
148%token IDENTIFIER_FINALIZED FUNC_MACRO OBJ_MACRO
Carl Worth796e1f02010-05-17 12:45:16 -0700149
Carl Worth3a37b872010-05-10 11:44:09 -0700150%%
151
Carl Worth33cc4002010-05-12 12:17:10 -0700152input:
Carl Worth3ff81672010-05-25 13:09:03 -0700153 /* empty */
Carl Worthae6517f2010-05-25 15:24:59 -0700154| input line {
155 printf ("\n");
156 }
Carl Worth3a37b872010-05-10 11:44:09 -0700157;
158
Carl Worth3ff81672010-05-25 13:09:03 -0700159line:
160 control_line
Carl Worth808401f2010-05-25 14:52:43 -0700161| text_line {
Carl Worthae6517f2010-05-25 15:24:59 -0700162 _glcpp_parser_print_expanded_token_list (parser, $1);
Carl Worth808401f2010-05-25 14:52:43 -0700163 talloc_free ($1);
164 }
Carl Worth3ff81672010-05-25 13:09:03 -0700165| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700166;
167
Carl Worth3ff81672010-05-25 13:09:03 -0700168control_line:
Carl Worthae6517f2010-05-25 15:24:59 -0700169 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
170 _define_object_macro (parser, $2, $3);
171 }
Carl Worth3ff81672010-05-25 13:09:03 -0700172| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE
173| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE
174| HASH_UNDEF IDENTIFIER NEWLINE
175| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700176;
177
Carl Worth3ff81672010-05-25 13:09:03 -0700178identifier_list:
179 IDENTIFIER
180| identifier_list ',' IDENTIFIER
Carl Worthfcbbb462010-05-13 09:36:23 -0700181;
182
Carl Worth3ff81672010-05-25 13:09:03 -0700183text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700184 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700185| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700186;
187
Carl Worth3ff81672010-05-25 13:09:03 -0700188non_directive:
189 pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700190;
191
Carl Worthaaa9acb2010-05-19 13:28:24 -0700192replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700193 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700194| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700195;
196
Carl Worthaaa9acb2010-05-19 13:28:24 -0700197pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700198 preprocessing_token {
199 $$ = _token_list_create (parser);
200 _token_list_append ($$, $1);
201 talloc_unlink (parser, $1);
202 }
203| pp_tokens preprocessing_token {
204 $$ = $1;
205 _token_list_append ($$, $2);
206 talloc_unlink (parser, $2);
207 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700208;
209
Carl Worth3ff81672010-05-25 13:09:03 -0700210preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700211 IDENTIFIER {
212 $$ = _token_create_str (parser, IDENTIFIER, $1);
213 }
214| punctuator {
215 $$ = _token_create_ival (parser, $1, $1);
216 }
217| OTHER {
218 $$ = _token_create_str (parser, OTHER, $1);
219 }
Carl Worth3ff81672010-05-25 13:09:03 -0700220;
221
222punctuator:
Carl Worth808401f2010-05-25 14:52:43 -0700223 '[' { $$ = '['; }
224| ']' { $$ = ']'; }
225| '(' { $$ = '('; }
226| ')' { $$ = ')'; }
227| '{' { $$ = '{'; }
228| '}' { $$ = '}'; }
229| '.' { $$ = '.'; }
230| '&' { $$ = '&'; }
231| '*' { $$ = '*'; }
232| '+' { $$ = '+'; }
233| '-' { $$ = '-'; }
234| '~' { $$ = '~'; }
235| '!' { $$ = '!'; }
236| '/' { $$ = '/'; }
237| '%' { $$ = '%'; }
238| LEFT_SHIFT { $$ = LEFT_SHIFT; }
239| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
240| '<' { $$ = '<'; }
241| '>' { $$ = '>'; }
242| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
243| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
244| EQUAL { $$ = EQUAL; }
245| NOT_EQUAL { $$ = NOT_EQUAL; }
246| '^' { $$ = '^'; }
247| '|' { $$ = '|'; }
248| AND { $$ = AND; }
249| OR { $$ = OR; }
250| ';' { $$ = ';'; }
251| ',' { $$ = ','; }
252| PASTE { $$ = PASTE; }
Carl Worth3ff81672010-05-25 13:09:03 -0700253;
254
Carl Worth33cc4002010-05-12 12:17:10 -0700255%%
256
Carl Worth610053b2010-05-14 10:05:11 -0700257string_list_t *
258_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700259{
Carl Worth610053b2010-05-14 10:05:11 -0700260 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700261
Carl Worth610053b2010-05-14 10:05:11 -0700262 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700263 list->head = NULL;
264 list->tail = NULL;
265
266 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700267}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700268
Carl Worth33cc4002010-05-12 12:17:10 -0700269void
Carl Worth610053b2010-05-14 10:05:11 -0700270_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700271{
272 if (list->head == NULL) {
273 list->head = tail->head;
274 } else {
275 list->tail->next = tail->head;
276 }
277
278 list->tail = tail->tail;
279}
280
281void
Carl Worth610053b2010-05-14 10:05:11 -0700282_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700283{
Carl Worth610053b2010-05-14 10:05:11 -0700284 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700285
Carl Worth610053b2010-05-14 10:05:11 -0700286 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700287 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700288
Carl Worth33cc4002010-05-12 12:17:10 -0700289 node->next = NULL;
290
291 if (list->head == NULL) {
292 list->head = node;
293 } else {
294 list->tail->next = node;
295 }
296
297 list->tail = node;
298}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700299
Carl Worthae6517f2010-05-25 15:24:59 -0700300void
301_string_list_push (string_list_t *list, const char *str)
302{
303 string_node_t *node;
304
305 node = xtalloc (list, string_node_t);
306 node->str = xtalloc_strdup (node, str);
307 node->next = list->head;
308
309 if (list->tail == NULL) {
310 list->tail = node;
311 }
312 list->head = node;
313}
314
315void
316_string_list_pop (string_list_t *list)
317{
318 string_node_t *node;
319
320 node = list->head;
321
322 if (node == NULL) {
323 fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
324 exit (1);
325 }
326
327 list->head = node->next;
328 if (list->tail == node) {
329 assert (node->next == NULL);
330 list->tail = NULL;
331 }
332
333 talloc_free (node);
334}
335
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700336int
Carl Worth610053b2010-05-14 10:05:11 -0700337_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700338{
Carl Worth610053b2010-05-14 10:05:11 -0700339 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700340 int i;
341
342 if (list == NULL)
343 return 0;
344
345 for (i = 0, node = list->head; node; i++, node = node->next) {
346 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700347 if (index)
348 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700349 return 1;
350 }
351 }
352
353 return 0;
354}
355
356int
Carl Worth610053b2010-05-14 10:05:11 -0700357_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700358{
359 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700360 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700361
362 if (list == NULL)
363 return 0;
364
365 for (node = list->head; node; node = node->next)
366 length++;
367
368 return length;
369}
370
Carl Worth8f6a8282010-05-14 10:44:19 -0700371argument_list_t *
372_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700373{
Carl Worth8f6a8282010-05-14 10:44:19 -0700374 argument_list_t *list;
375
376 list = xtalloc (ctx, argument_list_t);
377 list->head = NULL;
378 list->tail = NULL;
379
380 return list;
381}
382
383void
Carl Worth47252442010-05-19 13:54:37 -0700384_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700385{
386 argument_node_t *node;
387
388 if (argument == NULL || argument->head == NULL)
389 return;
390
391 node = xtalloc (list, argument_node_t);
392 node->argument = argument;
393
394 node->next = NULL;
395
396 if (list->head == NULL) {
397 list->head = node;
398 } else {
399 list->tail->next = node;
400 }
401
402 list->tail = node;
403}
404
405int
406_argument_list_length (argument_list_t *list)
407{
408 int length = 0;
409 argument_node_t *node;
410
411 if (list == NULL)
412 return 0;
413
414 for (node = list->head; node; node = node->next)
415 length++;
416
417 return length;
418}
419
Carl Worth47252442010-05-19 13:54:37 -0700420token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700421_argument_list_member_at (argument_list_t *list, int index)
422{
423 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700424 int i;
425
426 if (list == NULL)
427 return NULL;
428
429 node = list->head;
430 for (i = 0; i < index; i++) {
431 node = node->next;
432 if (node == NULL)
433 break;
434 }
435
436 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700437 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700438
439 return NULL;
440}
Carl Worth47252442010-05-19 13:54:37 -0700441
Carl Worth808401f2010-05-25 14:52:43 -0700442/* Note: This function talloc_steal()s the str pointer. */
443token_t *
444_token_create_str (void *ctx, int type, char *str)
445{
446 token_t *token;
447
448 token = xtalloc (ctx, token_t);
449 token->type = type;
450 token->value.str = talloc_steal (token, str);
451
452 return token;
453}
454
455token_t *
456_token_create_ival (void *ctx, int type, int ival)
457{
458 token_t *token;
459
460 token = xtalloc (ctx, token_t);
461 token->type = type;
462 token->value.ival = ival;
463
464 return token;
465}
466
467void
468_token_print (token_t *token)
469{
470 if (token->type < 256) {
471 printf ("%c", token->type);
472 return;
473 }
474
475 switch (token->type) {
476 case IDENTIFIER:
477 case OTHER:
478 printf ("%s", token->value.str);
479 break;
480 case LEFT_SHIFT:
481 printf ("<<");
482 break;
483 case RIGHT_SHIFT:
484 printf (">>");
485 break;
486 case LESS_OR_EQUAL:
487 printf ("<=");
488 break;
489 case GREATER_OR_EQUAL:
490 printf (">=");
491 break;
492 case EQUAL:
493 printf ("==");
494 break;
495 case NOT_EQUAL:
496 printf ("!=");
497 break;
498 case AND:
499 printf ("&&");
500 break;
501 case OR:
502 printf ("||");
503 break;
504 case PASTE:
505 printf ("##");
506 break;
507 default:
508 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
509 break;
510 }
511}
512
Carl Worth47252442010-05-19 13:54:37 -0700513token_list_t *
514_token_list_create (void *ctx)
515{
516 token_list_t *list;
517
518 list = xtalloc (ctx, token_list_t);
519 list->head = NULL;
520 list->tail = NULL;
521
522 return list;
523}
524
525void
Carl Worth808401f2010-05-25 14:52:43 -0700526_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700527{
528 token_node_t *node;
529
530 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700531 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700532
533 node->next = NULL;
534
535 if (list->head == NULL) {
536 list->head = node;
537 } else {
538 list->tail->next = node;
539 }
540
541 list->tail = node;
542}
543
544void
545_token_list_append_list (token_list_t *list, token_list_t *tail)
546{
547 if (list->head == NULL) {
548 list->head = tail->head;
549 } else {
550 list->tail->next = tail->head;
551 }
552
553 list->tail = tail->tail;
554}
Carl Worth80dc60b2010-05-25 14:42:00 -0700555
Carl Worth3a37b872010-05-10 11:44:09 -0700556void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700557yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700558{
559 fprintf (stderr, "Parse error: %s\n", error);
560}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700561
Carl Worth33cc4002010-05-12 12:17:10 -0700562glcpp_parser_t *
563glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700564{
Carl Worth33cc4002010-05-12 12:17:10 -0700565 glcpp_parser_t *parser;
566
Carl Worth5070a202010-05-12 12:45:33 -0700567 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700568
Carl Worth8f38aff2010-05-19 10:01:29 -0700569 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700570 parser->defines = hash_table_ctor (32, hash_table_string_hash,
571 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700572 parser->active = _string_list_create (parser);
Carl Wortha807fb72010-05-18 22:10:04 -0700573 parser->expansions = NULL;
574
Carl Worth5a6b9a22010-05-20 14:29:43 -0700575 parser->just_printed_separator = 1;
Carl Worth876e5102010-05-20 14:38:06 -0700576 parser->need_newline = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700577
Carl Worthb20d33c2010-05-20 22:27:07 -0700578 parser->skip_stack = NULL;
579
Carl Worth33cc4002010-05-12 12:17:10 -0700580 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700581}
582
583int
584glcpp_parser_parse (glcpp_parser_t *parser)
585{
586 return yyparse (parser);
587}
588
589void
Carl Worth33cc4002010-05-12 12:17:10 -0700590glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700591{
Carl Worth876e5102010-05-20 14:38:06 -0700592 if (parser->need_newline)
593 printf ("\n");
Carl Worthb20d33c2010-05-20 22:27:07 -0700594 if (parser->skip_stack)
595 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700596 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700597 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700598 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700599}
Carl Worthc6d5af32010-05-11 12:30:09 -0700600
Carl Worthbe0e2e92010-05-19 07:29:22 -0700601static int
602glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
603{
604 expansion_node_t *node;
605
606 for (node = parser->expansions; node; node = node->next) {
607 if (node->macro &&
608 strcmp (node->macro->identifier, member) == 0)
609 {
610 return 1;
611 }
612 }
613
614 return 0;
615}
616
Carl Wortha807fb72010-05-18 22:10:04 -0700617token_class_t
618glcpp_parser_classify_token (glcpp_parser_t *parser,
619 const char *identifier,
620 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700621{
Carl Worthfcbbb462010-05-13 09:36:23 -0700622 macro_t *macro;
623
Carl Worthc10a51b2010-05-20 15:15:26 -0700624 /* Is this token a defined macro? */
Carl Worthfcbbb462010-05-13 09:36:23 -0700625 macro = hash_table_find (parser->defines, identifier);
626
627 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700628 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700629
Carl Worthbe0e2e92010-05-19 07:29:22 -0700630 /* Don't consider this a macro if we are already actively
631 * expanding this macro. */
632 if (glcpp_parser_is_expanding (parser, identifier))
Carl Worthb5693832010-05-20 08:01:44 -0700633 return TOKEN_CLASS_IDENTIFIER_FINALIZED;
Carl Worthbe0e2e92010-05-19 07:29:22 -0700634
635 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700636 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700637 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700638 else
Carl Wortha807fb72010-05-18 22:10:04 -0700639 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700640}
641
Carl Worth33cc4002010-05-12 12:17:10 -0700642void
Carl Worthae6517f2010-05-25 15:24:59 -0700643_glcpp_parser_print_expanded_token (glcpp_parser_t *parser,
644 token_t *token)
645{
646 const char *identifier;
647 macro_t *macro;
648
649 /* We only expand identifiers */
650 if (token->type != IDENTIFIER) {
651 _token_print (token);
652 return;
653 }
654
655 /* Look up this identifier in the hash table. */
656 identifier = token->value.str;
657 macro = hash_table_find (parser->defines, identifier);
658
659 /* Not a macro, so just print directly. */
660 if (macro == NULL) {
661 printf ("%s", identifier);
662 return;
663 }
664
665 /* We're not (yet) supporting function-like macros. */
666 if (macro->is_function) {
667 printf ("%s", identifier);
668 return;
669 }
670
671 /* Finally, don't expand this macro if we're already actively
672 * expanding it, (to avoid infinite recursion). */
673 if (_string_list_contains (parser->active, identifier, NULL)) {
674 printf ("%s", identifier);
675 return;
676 }
677
678 _string_list_push (parser->active, identifier);
679 _glcpp_parser_print_expanded_token_list (parser,
680 macro->replacements);
681 _string_list_pop (parser->active);
682}
683
684void
685_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
686 token_list_t *list)
687{
688 token_node_t *node;
689
690 if (list == NULL)
691 return;
692
693 for (node = list->head; node; node = node->next) {
694 _glcpp_parser_print_expanded_token (parser, node->token);
695 if (node->next)
696 printf (" ");
697 }
698}
699
700void
Carl Worthfcbbb462010-05-13 09:36:23 -0700701_define_object_macro (glcpp_parser_t *parser,
702 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -0700703 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700704{
705 macro_t *macro;
706
707 macro = xtalloc (parser, macro_t);
708
709 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700710 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700711 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700712 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700713
714 hash_table_insert (parser->defines, macro, identifier);
715}
716
717void
718_define_function_macro (glcpp_parser_t *parser,
719 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700720 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -0700721 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700722{
723 macro_t *macro;
724
725 macro = xtalloc (parser, macro_t);
726
727 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700728 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700729 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700730 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700731
732 hash_table_insert (parser->defines, macro, identifier);
733}
734
Carl Wortha807fb72010-05-18 22:10:04 -0700735static void
Carl Worthc10a51b2010-05-20 15:15:26 -0700736_glcpp_parser_push_expansion (glcpp_parser_t *parser,
737 macro_t *macro,
738 token_node_t *replacements)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700739{
Carl Wortha807fb72010-05-18 22:10:04 -0700740 expansion_node_t *node;
741
742 node = xtalloc (parser, expansion_node_t);
743
744 node->macro = macro;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700745 node->replacements = replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700746
747 node->next = parser->expansions;
748 parser->expansions = node;
Carl Wortha807fb72010-05-18 22:10:04 -0700749}
750
Carl Worthaaa9acb2010-05-19 13:28:24 -0700751static void
Carl Wortha807fb72010-05-18 22:10:04 -0700752glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700753{
Carl Wortha807fb72010-05-18 22:10:04 -0700754 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700755
Carl Wortha807fb72010-05-18 22:10:04 -0700756 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700757
Carl Wortha807fb72010-05-18 22:10:04 -0700758 if (node == NULL) {
759 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
760 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700761 }
762
Carl Wortha807fb72010-05-18 22:10:04 -0700763 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700764
Carl Wortha807fb72010-05-18 22:10:04 -0700765 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700766}
767
Carl Wortha807fb72010-05-18 22:10:04 -0700768void
Carl Worth2be8be02010-05-14 10:31:43 -0700769_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700770{
Carl Worthfcbbb462010-05-13 09:36:23 -0700771 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700772
Carl Worthfcbbb462010-05-13 09:36:23 -0700773 macro = hash_table_find (parser->defines, identifier);
774 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700775 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700776
Carl Worthc10a51b2010-05-20 15:15:26 -0700777 _glcpp_parser_push_expansion (parser, macro, macro->replacements->head);
Carl Worthfcbbb462010-05-13 09:36:23 -0700778}
779
Carl Wortha807fb72010-05-18 22:10:04 -0700780void
Carl Worth2be8be02010-05-14 10:31:43 -0700781_expand_function_macro (glcpp_parser_t *parser,
782 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700783 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700784{
Carl Worthfcbbb462010-05-13 09:36:23 -0700785 macro_t *macro;
Carl Worthc10a51b2010-05-20 15:15:26 -0700786 token_list_t *expanded;
787 token_node_t *i, *j;
788 int parameter_index;
Carl Worthfcbbb462010-05-13 09:36:23 -0700789
790 macro = hash_table_find (parser->defines, identifier);
791 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700792 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700793
Carl Worth8f6a8282010-05-14 10:44:19 -0700794 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700795 _string_list_length (macro->parameters))
796 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700797 fprintf (stderr,
798 "Error: macro %s invoked with %d arguments (expected %d)\n",
799 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700800 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700801 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700802 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700803 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700804
Carl Worthc10a51b2010-05-20 15:15:26 -0700805 expanded = _token_list_create (macro);
806
807 for (i = macro->replacements->head; i; i = i->next) {
Carl Worth808401f2010-05-25 14:52:43 -0700808 if (_string_list_contains (macro->parameters,
809 i->token->value.str,
Carl Worthc10a51b2010-05-20 15:15:26 -0700810 &parameter_index))
811 {
812 token_list_t *argument;
813 argument = _argument_list_member_at (arguments,
814 parameter_index);
815 for (j = argument->head; j; j = j->next)
816 {
Carl Worth808401f2010-05-25 14:52:43 -0700817 _token_list_append (expanded, j->token);
Carl Worthc10a51b2010-05-20 15:15:26 -0700818 }
819 } else {
Carl Worth808401f2010-05-25 14:52:43 -0700820 _token_list_append (expanded, i->token);
Carl Worthc10a51b2010-05-20 15:15:26 -0700821 }
822 }
823
824 _glcpp_parser_push_expansion (parser, macro, expanded->head);
Carl Worth33cc4002010-05-12 12:17:10 -0700825}
Carl Worth8f38aff2010-05-19 10:01:29 -0700826
827static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700828glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700829{
Carl Worthaaa9acb2010-05-19 13:28:24 -0700830 expansion_node_t *expansion;
Carl Worth47252442010-05-19 13:54:37 -0700831 token_node_t *replacements;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700832 int parameter_index;
Carl Worthd8327e52010-05-20 15:18:54 -0700833 const char *token;
834 token_class_t class;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700835
836 /* Who says C can't do efficient tail recursion? */
837 RECURSE:
838
839 expansion = parser->expansions;
840
841 if (expansion == NULL)
842 return glcpp_lex (parser->scanner);
843
844 replacements = expansion->replacements;
845
846 /* Pop expansion when replacements is exhausted. */
847 if (replacements == NULL) {
848 glcpp_parser_pop_expansion (parser);
849 goto RECURSE;
850 }
851
852 expansion->replacements = replacements->next;
853
Carl Worth808401f2010-05-25 14:52:43 -0700854 token = replacements->token->value.str;
Carl Worthd8327e52010-05-20 15:18:54 -0700855
856 /* Implement token pasting. */
Carl Worth808401f2010-05-25 14:52:43 -0700857 if (replacements->next && strcmp (replacements->next->token->value.str, "##") == 0) {
Carl Worthd8327e52010-05-20 15:18:54 -0700858 token_node_t *next_node;
859
860 next_node = replacements->next->next;
861
862 if (next_node == NULL) {
863 fprintf (stderr, "Error: '##' cannot appear at the end of a macro expansion.\n");
864 exit (1);
865 }
866
867 token = xtalloc_asprintf (parser, "%s%s",
Carl Worth808401f2010-05-25 14:52:43 -0700868 token, next_node->token->value.str);
Carl Worthd8327e52010-05-20 15:18:54 -0700869 expansion->replacements = next_node->next;
870 }
871
872
873 if (strcmp (token, "(") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700874 return '(';
Carl Worthd8327e52010-05-20 15:18:54 -0700875 else if (strcmp (token, ")") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700876 return ')';
Carl Worthaaa9acb2010-05-19 13:28:24 -0700877
Carl Worthd8327e52010-05-20 15:18:54 -0700878 yylval.str = xtalloc_strdup (parser, token);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700879
Carl Worthb5693832010-05-20 08:01:44 -0700880 /* Carefully refuse to expand any finalized identifier. */
Carl Worth808401f2010-05-25 14:52:43 -0700881 if (replacements->token->type == IDENTIFIER_FINALIZED)
Carl Worthb5693832010-05-20 08:01:44 -0700882 return IDENTIFIER_FINALIZED;
883
Carl Worthaaa9acb2010-05-19 13:28:24 -0700884 switch (glcpp_parser_classify_token (parser, yylval.str,
885 &parameter_index))
886 {
Carl Worthaaa9acb2010-05-19 13:28:24 -0700887 case TOKEN_CLASS_IDENTIFIER:
888 return IDENTIFIER;
889 break;
Carl Worthb5693832010-05-20 08:01:44 -0700890 case TOKEN_CLASS_IDENTIFIER_FINALIZED:
891 return IDENTIFIER_FINALIZED;
892 break;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700893 case TOKEN_CLASS_FUNC_MACRO:
894 return FUNC_MACRO;
895 break;
896 default:
897 case TOKEN_CLASS_OBJ_MACRO:
898 return OBJ_MACRO;
899 break;
900 }
Carl Worth8f38aff2010-05-19 10:01:29 -0700901}
Carl Worthb20d33c2010-05-20 22:27:07 -0700902
903static void
904_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
905{
906 skip_type_t current = SKIP_NO_SKIP;
907 skip_node_t *node;
908
909 if (parser->skip_stack)
910 current = parser->skip_stack->type;
911
912 node = xtalloc (parser, skip_node_t);
913
914 if (current == SKIP_NO_SKIP) {
915 if (condition)
916 node->type = SKIP_NO_SKIP;
917 else
918 node->type = SKIP_TO_ELSE;
919 } else {
920 node->type = SKIP_TO_ENDIF;
921 }
922
923 node->next = parser->skip_stack;
924 parser->skip_stack = node;
925}
926
927static void
928_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
929 int condition)
930{
931 if (parser->skip_stack == NULL) {
932 fprintf (stderr, "Error: %s without #if\n", type);
933 exit (1);
934 }
935
936 if (parser->skip_stack->type == SKIP_TO_ELSE) {
937 if (condition)
938 parser->skip_stack->type = SKIP_NO_SKIP;
939 } else {
940 parser->skip_stack->type = SKIP_TO_ENDIF;
941 }
942}
Carl Worth80dc60b2010-05-25 14:42:00 -0700943
Carl Worthb20d33c2010-05-20 22:27:07 -0700944static void
945_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
946{
947 skip_node_t *node;
948
949 if (parser->skip_stack == NULL) {
950 fprintf (stderr, "Error: #endif without #if\n");
951 exit (1);
952 }
953
954 node = parser->skip_stack;
955 parser->skip_stack = node->next;
956 talloc_free (node);
957}