blob: 02286cd8e09391268466c455e947375623dc26d5 [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 Worth5aa7ea02010-05-25 18:39:43 -070031static void
Carl Wortha1e32bc2010-05-10 13:17:25 -070032yyerror (void *scanner, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070033
Carl Worth5aa7ea02010-05-25 18:39:43 -070034static void
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
Carl Worth5aa7ea02010-05-25 18:39:43 -070039static void
Carl Worthfcbbb462010-05-13 09:36:23 -070040_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 Worth5aa7ea02010-05-25 18:39:43 -070045static string_list_t *
Carl Worth610053b2010-05-14 10:05:11 -070046_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070047
Carl Worth5aa7ea02010-05-25 18:39:43 -070048static void
Carl Worth610053b2010-05-14 10:05:11 -070049_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070050
Carl Worth5aa7ea02010-05-25 18:39:43 -070051static void
Carl Worth610053b2010-05-14 10:05:11 -070052_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070053
Carl Worth5aa7ea02010-05-25 18:39:43 -070054static void
Carl Worthae6517f2010-05-25 15:24:59 -070055_string_list_push (string_list_t *list, const char *str);
56
Carl Worth5aa7ea02010-05-25 18:39:43 -070057static void
Carl Worthae6517f2010-05-25 15:24:59 -070058_string_list_pop (string_list_t *list);
59
Carl Worth5aa7ea02010-05-25 18:39:43 -070060static int
Carl Worth610053b2010-05-14 10:05:11 -070061_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070062
Carl Worth5aa7ea02010-05-25 18:39:43 -070063static int
Carl Worth610053b2010-05-14 10:05:11 -070064_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070065
Carl Worth5aa7ea02010-05-25 18:39:43 -070066static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070067_argument_list_create (void *ctx);
68
Carl Worth5aa7ea02010-05-25 18:39:43 -070069static void
Carl Worth47252442010-05-19 13:54:37 -070070_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070071
Carl Worth5aa7ea02010-05-25 18:39:43 -070072static int
Carl Worth8f6a8282010-05-14 10:44:19 -070073_argument_list_length (argument_list_t *list);
74
Carl Worth5aa7ea02010-05-25 18:39:43 -070075static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070076_argument_list_member_at (argument_list_t *list, int index);
77
Carl Worth808401f2010-05-25 14:52:43 -070078/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070079static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070080_token_create_str (void *ctx, int type, char *str);
81
Carl Worth5aa7ea02010-05-25 18:39:43 -070082static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070083_token_create_ival (void *ctx, int type, int ival);
84
Carl Worth5aa7ea02010-05-25 18:39:43 -070085static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070086_token_list_create (void *ctx);
87
Carl Worth808401f2010-05-25 14:52:43 -070088/* Note: This function add a talloc_reference() to token.
89 *
90 * You may want to talloc_unlink any current reference if you no
91 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070092static void
Carl Worth808401f2010-05-25 14:52:43 -070093_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070094
Carl Worth5aa7ea02010-05-25 18:39:43 -070095static void
Carl Worth47252442010-05-19 13:54:37 -070096_token_list_append_list (token_list_t *list, token_list_t *tail);
97
Carl Worth5aa7ea02010-05-25 18:39:43 -070098static void
Carl Worthae6517f2010-05-25 15:24:59 -070099_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
100 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700101
Carl Worthaaa9acb2010-05-19 13:28:24 -0700102static void
Carl Worthb20d33c2010-05-20 22:27:07 -0700103_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
104
105static void
106_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
107 int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700108
Carl Worthb20d33c2010-05-20 22:27:07 -0700109static void
110_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
111
Carl Worth0293b2e2010-05-19 10:05:40 -0700112#define yylex glcpp_parser_lex
113
Carl Worth8f38aff2010-05-19 10:01:29 -0700114static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700115glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700116
Carl Worth3a37b872010-05-10 11:44:09 -0700117%}
118
Carl Worth0b27b5f2010-05-10 16:16:06 -0700119%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700120%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700121
Carl Worthb1854fd2010-05-25 16:28:26 -0700122%token HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_UNDEF IDENTIFIER NEWLINE OTHER SPACE
Carl Worth3ff81672010-05-25 13:09:03 -0700123%token LEFT_SHIFT RIGHT_SHIFT LESS_OR_EQUAL GREATER_OR_EQUAL EQUAL NOT_EQUAL AND OR PASTE
Carl Worthe9397862010-05-25 17:08:07 -0700124%type <ival> punctuator SPACE
125%type <str> IDENTIFIER OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700126%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700127%type <token> preprocessing_token
128%type <token_list> pp_tokens replacement_list text_line
Carl Worth3a37b872010-05-10 11:44:09 -0700129
Carl Worth3ff81672010-05-25 13:09:03 -0700130 /* Stale stuff just to allow code to compile. */
131%token IDENTIFIER_FINALIZED FUNC_MACRO OBJ_MACRO
Carl Worth796e1f02010-05-17 12:45:16 -0700132
Carl Worth3a37b872010-05-10 11:44:09 -0700133%%
134
Carl Worth33cc4002010-05-12 12:17:10 -0700135input:
Carl Worth3ff81672010-05-25 13:09:03 -0700136 /* empty */
Carl Worthae6517f2010-05-25 15:24:59 -0700137| input line {
138 printf ("\n");
139 }
Carl Worth3a37b872010-05-10 11:44:09 -0700140;
141
Carl Worth3ff81672010-05-25 13:09:03 -0700142line:
143 control_line
Carl Worth808401f2010-05-25 14:52:43 -0700144| text_line {
Carl Worthae6517f2010-05-25 15:24:59 -0700145 _glcpp_parser_print_expanded_token_list (parser, $1);
Carl Worth808401f2010-05-25 14:52:43 -0700146 talloc_free ($1);
147 }
Carl Worth3ff81672010-05-25 13:09:03 -0700148| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700149;
150
Carl Worth3ff81672010-05-25 13:09:03 -0700151control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700152 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Carl Worthae6517f2010-05-25 15:24:59 -0700153 _define_object_macro (parser, $2, $3);
154 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700155| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
156 _define_function_macro (parser, $2, NULL, $5);
157 }
158| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
159 _define_function_macro (parser, $2, $4, $6);
160 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700161| HASH_UNDEF IDENTIFIER NEWLINE {
162 string_list_t *macro = hash_table_find (parser->defines, $2);
163 if (macro) {
164 /* XXX: Need hash table to support a real way
165 * to remove an element rather than prefixing
166 * a new node with data of NULL like this. */
167 hash_table_insert (parser->defines, NULL, $2);
168 talloc_free (macro);
169 }
170 talloc_free ($2);
171 }
Carl Worth3ff81672010-05-25 13:09:03 -0700172| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700173;
174
Carl Worth3ff81672010-05-25 13:09:03 -0700175identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700176 IDENTIFIER {
177 $$ = _string_list_create (parser);
178 _string_list_append_item ($$, $1);
179 talloc_steal ($$, $1);
180 }
181| identifier_list ',' IDENTIFIER {
182 $$ = $1;
183 _string_list_append_item ($$, $3);
184 talloc_steal ($$, $3);
185 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700186;
187
Carl Worth3ff81672010-05-25 13:09:03 -0700188text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700189 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700190| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700191;
192
Carl Worth3ff81672010-05-25 13:09:03 -0700193non_directive:
194 pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700195;
196
Carl Worthaaa9acb2010-05-19 13:28:24 -0700197replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700198 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700199| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700200;
201
Carl Worthaaa9acb2010-05-19 13:28:24 -0700202pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700203 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700204 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700205 $$ = _token_list_create (parser);
206 _token_list_append ($$, $1);
207 talloc_unlink (parser, $1);
208 }
209| pp_tokens preprocessing_token {
210 $$ = $1;
211 _token_list_append ($$, $2);
212 talloc_unlink (parser, $2);
213 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700214;
215
Carl Worth3ff81672010-05-25 13:09:03 -0700216preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700217 IDENTIFIER {
218 $$ = _token_create_str (parser, IDENTIFIER, $1);
219 }
220| punctuator {
221 $$ = _token_create_ival (parser, $1, $1);
222 }
223| OTHER {
224 $$ = _token_create_str (parser, OTHER, $1);
225 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700226| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700227 $$ = _token_create_ival (parser, SPACE, SPACE);
Carl Worthb1854fd2010-05-25 16:28:26 -0700228 }
Carl Worth3ff81672010-05-25 13:09:03 -0700229;
230
231punctuator:
Carl Worth808401f2010-05-25 14:52:43 -0700232 '[' { $$ = '['; }
233| ']' { $$ = ']'; }
234| '(' { $$ = '('; }
235| ')' { $$ = ')'; }
236| '{' { $$ = '{'; }
237| '}' { $$ = '}'; }
238| '.' { $$ = '.'; }
239| '&' { $$ = '&'; }
240| '*' { $$ = '*'; }
241| '+' { $$ = '+'; }
242| '-' { $$ = '-'; }
243| '~' { $$ = '~'; }
244| '!' { $$ = '!'; }
245| '/' { $$ = '/'; }
246| '%' { $$ = '%'; }
247| LEFT_SHIFT { $$ = LEFT_SHIFT; }
248| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
249| '<' { $$ = '<'; }
250| '>' { $$ = '>'; }
251| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
252| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
253| EQUAL { $$ = EQUAL; }
254| NOT_EQUAL { $$ = NOT_EQUAL; }
255| '^' { $$ = '^'; }
256| '|' { $$ = '|'; }
257| AND { $$ = AND; }
258| OR { $$ = OR; }
259| ';' { $$ = ';'; }
260| ',' { $$ = ','; }
261| PASTE { $$ = PASTE; }
Carl Worth3ff81672010-05-25 13:09:03 -0700262;
263
Carl Worth33cc4002010-05-12 12:17:10 -0700264%%
265
Carl Worth610053b2010-05-14 10:05:11 -0700266string_list_t *
267_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700268{
Carl Worth610053b2010-05-14 10:05:11 -0700269 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700270
Carl Worth610053b2010-05-14 10:05:11 -0700271 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700272 list->head = NULL;
273 list->tail = NULL;
274
275 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700276}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700277
Carl Worth33cc4002010-05-12 12:17:10 -0700278void
Carl Worth610053b2010-05-14 10:05:11 -0700279_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700280{
281 if (list->head == NULL) {
282 list->head = tail->head;
283 } else {
284 list->tail->next = tail->head;
285 }
286
287 list->tail = tail->tail;
288}
289
290void
Carl Worth610053b2010-05-14 10:05:11 -0700291_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700292{
Carl Worth610053b2010-05-14 10:05:11 -0700293 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700294
Carl Worth610053b2010-05-14 10:05:11 -0700295 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700296 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700297
Carl Worth33cc4002010-05-12 12:17:10 -0700298 node->next = NULL;
299
300 if (list->head == NULL) {
301 list->head = node;
302 } else {
303 list->tail->next = node;
304 }
305
306 list->tail = node;
307}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700308
Carl Worthae6517f2010-05-25 15:24:59 -0700309void
310_string_list_push (string_list_t *list, const char *str)
311{
312 string_node_t *node;
313
314 node = xtalloc (list, string_node_t);
315 node->str = xtalloc_strdup (node, str);
316 node->next = list->head;
317
318 if (list->tail == NULL) {
319 list->tail = node;
320 }
321 list->head = node;
322}
323
324void
325_string_list_pop (string_list_t *list)
326{
327 string_node_t *node;
328
329 node = list->head;
330
331 if (node == NULL) {
332 fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
333 exit (1);
334 }
335
336 list->head = node->next;
337 if (list->tail == node) {
338 assert (node->next == NULL);
339 list->tail = NULL;
340 }
341
342 talloc_free (node);
343}
344
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700345int
Carl Worth610053b2010-05-14 10:05:11 -0700346_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700347{
Carl Worth610053b2010-05-14 10:05:11 -0700348 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700349 int i;
350
351 if (list == NULL)
352 return 0;
353
354 for (i = 0, node = list->head; node; i++, node = node->next) {
355 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700356 if (index)
357 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700358 return 1;
359 }
360 }
361
362 return 0;
363}
364
365int
Carl Worth610053b2010-05-14 10:05:11 -0700366_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700367{
368 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700369 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700370
371 if (list == NULL)
372 return 0;
373
374 for (node = list->head; node; node = node->next)
375 length++;
376
377 return length;
378}
379
Carl Worth8f6a8282010-05-14 10:44:19 -0700380argument_list_t *
381_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700382{
Carl Worth8f6a8282010-05-14 10:44:19 -0700383 argument_list_t *list;
384
385 list = xtalloc (ctx, argument_list_t);
386 list->head = NULL;
387 list->tail = NULL;
388
389 return list;
390}
391
392void
Carl Worth47252442010-05-19 13:54:37 -0700393_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700394{
395 argument_node_t *node;
396
Carl Worth8f6a8282010-05-14 10:44:19 -0700397 node = xtalloc (list, argument_node_t);
398 node->argument = argument;
399
400 node->next = NULL;
401
402 if (list->head == NULL) {
403 list->head = node;
404 } else {
405 list->tail->next = node;
406 }
407
408 list->tail = node;
409}
410
411int
412_argument_list_length (argument_list_t *list)
413{
414 int length = 0;
415 argument_node_t *node;
416
417 if (list == NULL)
418 return 0;
419
420 for (node = list->head; node; node = node->next)
421 length++;
422
423 return length;
424}
425
Carl Worth47252442010-05-19 13:54:37 -0700426token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700427_argument_list_member_at (argument_list_t *list, int index)
428{
429 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700430 int i;
431
432 if (list == NULL)
433 return NULL;
434
435 node = list->head;
436 for (i = 0; i < index; i++) {
437 node = node->next;
438 if (node == NULL)
439 break;
440 }
441
442 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700443 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700444
445 return NULL;
446}
Carl Worth47252442010-05-19 13:54:37 -0700447
Carl Worth808401f2010-05-25 14:52:43 -0700448/* Note: This function talloc_steal()s the str pointer. */
449token_t *
450_token_create_str (void *ctx, int type, char *str)
451{
452 token_t *token;
453
454 token = xtalloc (ctx, token_t);
455 token->type = type;
456 token->value.str = talloc_steal (token, str);
457
458 return token;
459}
460
461token_t *
462_token_create_ival (void *ctx, int type, int ival)
463{
464 token_t *token;
465
466 token = xtalloc (ctx, token_t);
467 token->type = type;
468 token->value.ival = ival;
469
470 return token;
471}
472
473void
474_token_print (token_t *token)
475{
476 if (token->type < 256) {
477 printf ("%c", token->type);
478 return;
479 }
480
481 switch (token->type) {
482 case IDENTIFIER:
483 case OTHER:
484 printf ("%s", token->value.str);
485 break;
Carl Worthe9397862010-05-25 17:08:07 -0700486 case SPACE:
487 printf (" ");
488 break;
Carl Worth808401f2010-05-25 14:52:43 -0700489 case LEFT_SHIFT:
490 printf ("<<");
491 break;
492 case RIGHT_SHIFT:
493 printf (">>");
494 break;
495 case LESS_OR_EQUAL:
496 printf ("<=");
497 break;
498 case GREATER_OR_EQUAL:
499 printf (">=");
500 break;
501 case EQUAL:
502 printf ("==");
503 break;
504 case NOT_EQUAL:
505 printf ("!=");
506 break;
507 case AND:
508 printf ("&&");
509 break;
510 case OR:
511 printf ("||");
512 break;
513 case PASTE:
514 printf ("##");
515 break;
516 default:
517 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
518 break;
519 }
520}
521
Carl Worth47252442010-05-19 13:54:37 -0700522token_list_t *
523_token_list_create (void *ctx)
524{
525 token_list_t *list;
526
527 list = xtalloc (ctx, token_list_t);
528 list->head = NULL;
529 list->tail = NULL;
530
531 return list;
532}
533
534void
Carl Worth808401f2010-05-25 14:52:43 -0700535_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700536{
537 token_node_t *node;
538
539 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700540 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700541
542 node->next = NULL;
543
544 if (list->head == NULL) {
545 list->head = node;
546 } else {
547 list->tail->next = node;
548 }
549
550 list->tail = node;
551}
552
553void
554_token_list_append_list (token_list_t *list, token_list_t *tail)
555{
556 if (list->head == NULL) {
557 list->head = tail->head;
558 } else {
559 list->tail->next = tail->head;
560 }
561
562 list->tail = tail->tail;
563}
Carl Worth80dc60b2010-05-25 14:42:00 -0700564
Carl Worth3a37b872010-05-10 11:44:09 -0700565void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700566yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700567{
568 fprintf (stderr, "Parse error: %s\n", error);
569}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700570
Carl Worth33cc4002010-05-12 12:17:10 -0700571glcpp_parser_t *
572glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700573{
Carl Worth33cc4002010-05-12 12:17:10 -0700574 glcpp_parser_t *parser;
575
Carl Worth5070a202010-05-12 12:45:33 -0700576 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700577
Carl Worth8f38aff2010-05-19 10:01:29 -0700578 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700579 parser->defines = hash_table_ctor (32, hash_table_string_hash,
580 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700581 parser->active = _string_list_create (parser);
Carl Worthf34a0002010-05-25 16:59:02 -0700582 parser->space_tokens = 1;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700583
Carl Worthb20d33c2010-05-20 22:27:07 -0700584 parser->skip_stack = NULL;
585
Carl Worth33cc4002010-05-12 12:17:10 -0700586 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700587}
588
589int
590glcpp_parser_parse (glcpp_parser_t *parser)
591{
592 return yyparse (parser);
593}
594
595void
Carl Worth33cc4002010-05-12 12:17:10 -0700596glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700597{
Carl Worthb20d33c2010-05-20 22:27:07 -0700598 if (parser->skip_stack)
599 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700600 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700601 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700602 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700603}
Carl Worthc6d5af32010-05-11 12:30:09 -0700604
Carl Worthb1854fd2010-05-25 16:28:26 -0700605/* Print a non-macro token, or the expansion of an object-like macro.
606 *
607 * Returns 0 if this token is completely printed.
608 *
609 * Returns 1 in the case that 'token' is a function-like macro that
610 * needs further expansion.
611 */
612static int
Carl Worthae6517f2010-05-25 15:24:59 -0700613_glcpp_parser_print_expanded_token (glcpp_parser_t *parser,
614 token_t *token)
615{
616 const char *identifier;
617 macro_t *macro;
618
619 /* We only expand identifiers */
620 if (token->type != IDENTIFIER) {
621 _token_print (token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700622 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700623 }
624
625 /* Look up this identifier in the hash table. */
626 identifier = token->value.str;
627 macro = hash_table_find (parser->defines, identifier);
628
629 /* Not a macro, so just print directly. */
630 if (macro == NULL) {
631 printf ("%s", identifier);
Carl Worthb1854fd2010-05-25 16:28:26 -0700632 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700633 }
634
Carl Worthb1854fd2010-05-25 16:28:26 -0700635 /* For function-like macros return 1 for further processing. */
Carl Worthae6517f2010-05-25 15:24:59 -0700636 if (macro->is_function) {
Carl Worthb1854fd2010-05-25 16:28:26 -0700637 return 1;
Carl Worthae6517f2010-05-25 15:24:59 -0700638 }
639
640 /* Finally, don't expand this macro if we're already actively
641 * expanding it, (to avoid infinite recursion). */
642 if (_string_list_contains (parser->active, identifier, NULL)) {
643 printf ("%s", identifier);
Carl Worthb1854fd2010-05-25 16:28:26 -0700644 return 0;
645 }
646
647 _string_list_push (parser->active, identifier);
648 _glcpp_parser_print_expanded_token_list (parser,
649 macro->replacements);
650 _string_list_pop (parser->active);
651
652 return 0;
653}
654
655typedef enum function_status
656{
657 FUNCTION_STATUS_SUCCESS,
658 FUNCTION_NOT_A_FUNCTION,
659 FUNCTION_UNBALANCED_PARENTHESES
660} function_status_t;
661
662/* Find a set of function-like macro arguments by looking for a
663 * balanced set of parentheses. Upon return *node will be the last
664 * consumed node, such that further processing can continue with
665 * node->next.
666 *
667 * Return values:
668 *
669 * FUNCTION_STATUS_SUCCESS:
670 *
671 * Successfully parsed a set of function arguments.
672 *
673 * FUNCTION_NOT_A_FUNCTION:
674 *
675 * Macro name not followed by a '('. This is not an error, but
676 * simply that the macro name should be treated as a non-macro.
677 *
678 * FUNCTION_UNBLANCED_PARENTHESES
679 *
680 * Macro name is not followed by a balanced set of parentheses.
681 */
682static function_status_t
Carl Worth9ce18cf2010-05-25 17:32:21 -0700683_arguments_parse (argument_list_t *arguments, token_node_t **node_ret)
Carl Worthb1854fd2010-05-25 16:28:26 -0700684{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700685 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700686 token_node_t *node = *node_ret, *last;
687 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700688
689 last = node;
690 node = node->next;
691
692 /* Ignore whitespace before first parenthesis. */
693 while (node && node->token->type == SPACE)
694 node = node->next;
695
696 if (node == NULL || node->token->type != '(')
697 return FUNCTION_NOT_A_FUNCTION;
698
Carl Worth652fa272010-05-25 17:45:22 -0700699 last = node;
700 node = node->next;
701
Carl Worth9ce18cf2010-05-25 17:32:21 -0700702 argument = NULL;
703
Carl Worth652fa272010-05-25 17:45:22 -0700704 for (paren_count = 1; node; last = node, node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -0700705 if (node->token->type == '(')
706 {
707 paren_count++;
708 }
709 else if (node->token->type == ')')
710 {
711 paren_count--;
Carl Worthc7581c22010-05-25 17:41:07 -0700712 if (paren_count == 0) {
713 last = node;
714 node = node->next;
715 break;
716 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700717 }
Carl Worth652fa272010-05-25 17:45:22 -0700718
719 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -0700720 paren_count == 1)
721 {
Carl Worth9ce18cf2010-05-25 17:32:21 -0700722 argument = NULL;
723 }
724 else {
725 if (argument == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -0700726 /* Don't treat initial whitespace as
727 * part of the arguement. */
728 if (node->token->type == SPACE)
729 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -0700730 argument = _token_list_create (arguments);
731 _argument_list_append (arguments, argument);
732 }
733 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700734 }
Carl Worthc7581c22010-05-25 17:41:07 -0700735 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700736
737 if (node && paren_count)
738 return FUNCTION_UNBALANCED_PARENTHESES;
739
740 *node_ret = last;
741
742 return FUNCTION_STATUS_SUCCESS;
743}
744
745/* Prints the expansion of *node (consuming further tokens from the
746 * list as necessary). Upon return *node will be the last consumed
747 * node, such that further processing can continue with node->next. */
748static void
749_glcpp_parser_print_expanded_function (glcpp_parser_t *parser,
750 token_node_t **node_ret)
751{
752 macro_t *macro;
753 token_node_t *node;
754 const char *identifier;
755 argument_list_t *arguments;
756 function_status_t status;
Carl Worth9ce18cf2010-05-25 17:32:21 -0700757 token_list_t *expanded;
758 token_node_t *i, *j;
759 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -0700760
761 node = *node_ret;
762 identifier = node->token->value.str;
763
764 macro = hash_table_find (parser->defines, identifier);
765
766 assert (macro->is_function);
767
Carl Worth9ce18cf2010-05-25 17:32:21 -0700768 arguments = _argument_list_create (parser);
769 status = _arguments_parse (arguments, node_ret);
Carl Worthb1854fd2010-05-25 16:28:26 -0700770
771 switch (status) {
772 case FUNCTION_STATUS_SUCCESS:
773 break;
774 case FUNCTION_NOT_A_FUNCTION:
775 printf ("%s", identifier);
Carl Worthae6517f2010-05-25 15:24:59 -0700776 return;
Carl Worthb1854fd2010-05-25 16:28:26 -0700777 case FUNCTION_UNBALANCED_PARENTHESES:
778 fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n",
779 identifier);
780 exit (1);
Carl Worthae6517f2010-05-25 15:24:59 -0700781 }
782
Carl Worth9ce18cf2010-05-25 17:32:21 -0700783 if (macro->replacements == NULL) {
784 talloc_free (arguments);
785 return;
786 }
787
788
789 if (_argument_list_length (arguments) !=
790 _string_list_length (macro->parameters))
791 {
792 fprintf (stderr,
793 "Error: macro %s invoked with %d arguments (expected %d)\n",
794 identifier,
795 _argument_list_length (arguments),
796 _string_list_length (macro->parameters));
797 return;
798 }
799
800 expanded = _token_list_create (arguments);
801
802 for (i = macro->replacements->head; i; i = i->next) {
803 if (i->token->type == IDENTIFIER &&
804 _string_list_contains (macro->parameters,
805 i->token->value.str,
806 &parameter_index))
807 {
808 token_list_t *argument;
809 argument = _argument_list_member_at (arguments,
810 parameter_index);
811 for (j = argument->head; j; j = j->next)
812 {
813 _token_list_append (expanded, j->token);
814 }
815 } else {
816 _token_list_append (expanded, i->token);
817 }
818 }
819
Carl Worthae6517f2010-05-25 15:24:59 -0700820 _string_list_push (parser->active, identifier);
Carl Worth9ce18cf2010-05-25 17:32:21 -0700821 _glcpp_parser_print_expanded_token_list (parser, expanded);
Carl Worthae6517f2010-05-25 15:24:59 -0700822 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -0700823
824 talloc_free (arguments);
Carl Worthae6517f2010-05-25 15:24:59 -0700825}
826
827void
828_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
829 token_list_t *list)
830{
831 token_node_t *node;
Carl Worthb1854fd2010-05-25 16:28:26 -0700832 function_status_t function_status;
Carl Worthae6517f2010-05-25 15:24:59 -0700833
834 if (list == NULL)
835 return;
836
837 for (node = list->head; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -0700838 if (_glcpp_parser_print_expanded_token (parser, node->token))
839 _glcpp_parser_print_expanded_function (parser, &node);
Carl Worthae6517f2010-05-25 15:24:59 -0700840 }
841}
842
843void
Carl Worthfcbbb462010-05-13 09:36:23 -0700844_define_object_macro (glcpp_parser_t *parser,
845 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -0700846 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700847{
848 macro_t *macro;
849
850 macro = xtalloc (parser, macro_t);
851
852 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700853 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700854 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700855 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700856
857 hash_table_insert (parser->defines, macro, identifier);
858}
859
860void
861_define_function_macro (glcpp_parser_t *parser,
862 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700863 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -0700864 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700865{
866 macro_t *macro;
867
868 macro = xtalloc (parser, macro_t);
869
870 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700871 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700872 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700873 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700874
875 hash_table_insert (parser->defines, macro, identifier);
876}
877
Carl Worth8f38aff2010-05-19 10:01:29 -0700878static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700879glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700880{
Carl Worth5aa7ea02010-05-25 18:39:43 -0700881 return glcpp_lex (parser->scanner);
Carl Worth8f38aff2010-05-19 10:01:29 -0700882}
Carl Worthb20d33c2010-05-20 22:27:07 -0700883
884static void
885_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
886{
887 skip_type_t current = SKIP_NO_SKIP;
888 skip_node_t *node;
889
890 if (parser->skip_stack)
891 current = parser->skip_stack->type;
892
893 node = xtalloc (parser, skip_node_t);
894
895 if (current == SKIP_NO_SKIP) {
896 if (condition)
897 node->type = SKIP_NO_SKIP;
898 else
899 node->type = SKIP_TO_ELSE;
900 } else {
901 node->type = SKIP_TO_ENDIF;
902 }
903
904 node->next = parser->skip_stack;
905 parser->skip_stack = node;
906}
907
908static void
909_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
910 int condition)
911{
912 if (parser->skip_stack == NULL) {
913 fprintf (stderr, "Error: %s without #if\n", type);
914 exit (1);
915 }
916
917 if (parser->skip_stack->type == SKIP_TO_ELSE) {
918 if (condition)
919 parser->skip_stack->type = SKIP_NO_SKIP;
920 } else {
921 parser->skip_stack->type = SKIP_TO_ENDIF;
922 }
923}
Carl Worth80dc60b2010-05-25 14:42:00 -0700924
Carl Worthb20d33c2010-05-20 22:27:07 -0700925static void
926_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
927{
928 skip_node_t *node;
929
930 if (parser->skip_stack == NULL) {
931 fprintf (stderr, "Error: #endif without #if\n");
932 exit (1);
933 }
934
935 node = parser->skip_stack;
936 parser->skip_stack = node->next;
937 talloc_free (node);
938}