blob: a2bff6e0adaa6643400383f50c31c602d595a786 [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
Carl Worth10ae4382010-05-25 20:35:01 -0700474_glcpp_parser_print_token (glcpp_parser_t *parser, token_t *token)
Carl Worth808401f2010-05-25 14:52:43 -0700475{
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;
Carl Worth10ae4382010-05-25 20:35:01 -0700530 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700531
532 return list;
533}
534
535void
Carl Worth808401f2010-05-25 14:52:43 -0700536_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700537{
538 token_node_t *node;
539
540 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700541 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700542
543 node->next = NULL;
544
545 if (list->head == NULL) {
546 list->head = node;
547 } else {
548 list->tail->next = node;
549 }
550
551 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700552 if (token->type != SPACE)
553 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700554}
555
556void
557_token_list_append_list (token_list_t *list, token_list_t *tail)
558{
559 if (list->head == NULL) {
560 list->head = tail->head;
561 } else {
562 list->tail->next = tail->head;
563 }
564
565 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700566 list->non_space_tail = tail->non_space_tail;
567}
568
569void
570_token_list_trim_trailing_space (token_list_t *list)
571{
572 token_node_t *tail, *next;
573
574 if (list->non_space_tail) {
575 tail = list->non_space_tail->next;
576 list->non_space_tail->next = NULL;
577 list->tail = list->non_space_tail;
578
579 while (tail) {
580 next = tail->next;
581 talloc_free (tail);
582 tail = next;
583 }
584 }
Carl Worth47252442010-05-19 13:54:37 -0700585}
Carl Worth80dc60b2010-05-25 14:42:00 -0700586
Carl Worth3a37b872010-05-10 11:44:09 -0700587void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700588yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700589{
590 fprintf (stderr, "Parse error: %s\n", error);
591}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700592
Carl Worth33cc4002010-05-12 12:17:10 -0700593glcpp_parser_t *
594glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700595{
Carl Worth33cc4002010-05-12 12:17:10 -0700596 glcpp_parser_t *parser;
597
Carl Worth5070a202010-05-12 12:45:33 -0700598 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700599
Carl Worth8f38aff2010-05-19 10:01:29 -0700600 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700601 parser->defines = hash_table_ctor (32, hash_table_string_hash,
602 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700603 parser->active = _string_list_create (parser);
Carl Worthf34a0002010-05-25 16:59:02 -0700604 parser->space_tokens = 1;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700605
Carl Worthb20d33c2010-05-20 22:27:07 -0700606 parser->skip_stack = NULL;
607
Carl Worth33cc4002010-05-12 12:17:10 -0700608 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700609}
610
611int
612glcpp_parser_parse (glcpp_parser_t *parser)
613{
614 return yyparse (parser);
615}
616
617void
Carl Worth33cc4002010-05-12 12:17:10 -0700618glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700619{
Carl Worthb20d33c2010-05-20 22:27:07 -0700620 if (parser->skip_stack)
621 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700622 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700623 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700624 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700625}
Carl Worthc6d5af32010-05-11 12:30:09 -0700626
Carl Worthb1854fd2010-05-25 16:28:26 -0700627/* Print a non-macro token, or the expansion of an object-like macro.
628 *
629 * Returns 0 if this token is completely printed.
630 *
631 * Returns 1 in the case that 'token' is a function-like macro that
632 * needs further expansion.
633 */
634static int
Carl Worthae6517f2010-05-25 15:24:59 -0700635_glcpp_parser_print_expanded_token (glcpp_parser_t *parser,
636 token_t *token)
637{
638 const char *identifier;
639 macro_t *macro;
640
641 /* We only expand identifiers */
642 if (token->type != IDENTIFIER) {
Carl Worth10ae4382010-05-25 20:35:01 -0700643 _glcpp_parser_print_token (parser, token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700644 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700645 }
646
647 /* Look up this identifier in the hash table. */
648 identifier = token->value.str;
649 macro = hash_table_find (parser->defines, identifier);
650
651 /* Not a macro, so just print directly. */
652 if (macro == NULL) {
653 printf ("%s", identifier);
Carl Worthb1854fd2010-05-25 16:28:26 -0700654 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700655 }
656
Carl Worthae6517f2010-05-25 15:24:59 -0700657 /* Finally, don't expand this macro if we're already actively
658 * expanding it, (to avoid infinite recursion). */
659 if (_string_list_contains (parser->active, identifier, NULL)) {
660 printf ("%s", identifier);
Carl Worthb1854fd2010-05-25 16:28:26 -0700661 return 0;
662 }
663
Carl Worthc0607d52010-05-26 08:01:42 -0700664 /* For function-like macros return 1 for further processing. */
665 if (macro->is_function) {
666 return 1;
667 }
668
Carl Worthb1854fd2010-05-25 16:28:26 -0700669 _string_list_push (parser->active, identifier);
670 _glcpp_parser_print_expanded_token_list (parser,
671 macro->replacements);
672 _string_list_pop (parser->active);
673
674 return 0;
675}
676
677typedef enum function_status
678{
679 FUNCTION_STATUS_SUCCESS,
680 FUNCTION_NOT_A_FUNCTION,
681 FUNCTION_UNBALANCED_PARENTHESES
682} function_status_t;
683
684/* Find a set of function-like macro arguments by looking for a
685 * balanced set of parentheses. Upon return *node will be the last
686 * consumed node, such that further processing can continue with
687 * node->next.
688 *
689 * Return values:
690 *
691 * FUNCTION_STATUS_SUCCESS:
692 *
693 * Successfully parsed a set of function arguments.
694 *
695 * FUNCTION_NOT_A_FUNCTION:
696 *
697 * Macro name not followed by a '('. This is not an error, but
698 * simply that the macro name should be treated as a non-macro.
699 *
700 * FUNCTION_UNBLANCED_PARENTHESES
701 *
702 * Macro name is not followed by a balanced set of parentheses.
703 */
704static function_status_t
Carl Worth9ce18cf2010-05-25 17:32:21 -0700705_arguments_parse (argument_list_t *arguments, token_node_t **node_ret)
Carl Worthb1854fd2010-05-25 16:28:26 -0700706{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700707 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700708 token_node_t *node = *node_ret, *last;
709 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700710
711 last = node;
712 node = node->next;
713
714 /* Ignore whitespace before first parenthesis. */
715 while (node && node->token->type == SPACE)
716 node = node->next;
717
718 if (node == NULL || node->token->type != '(')
719 return FUNCTION_NOT_A_FUNCTION;
720
Carl Worth652fa272010-05-25 17:45:22 -0700721 last = node;
722 node = node->next;
723
Carl Worth9ce18cf2010-05-25 17:32:21 -0700724 argument = NULL;
725
Carl Worth652fa272010-05-25 17:45:22 -0700726 for (paren_count = 1; node; last = node, node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -0700727 if (node->token->type == '(')
728 {
729 paren_count++;
730 }
731 else if (node->token->type == ')')
732 {
733 paren_count--;
Carl Worthc7581c22010-05-25 17:41:07 -0700734 if (paren_count == 0) {
735 last = node;
736 node = node->next;
737 break;
738 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700739 }
Carl Worth652fa272010-05-25 17:45:22 -0700740
741 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -0700742 paren_count == 1)
743 {
Carl Worth10ae4382010-05-25 20:35:01 -0700744 if (argument)
745 _token_list_trim_trailing_space (argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -0700746 argument = NULL;
747 }
748 else {
749 if (argument == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -0700750 /* Don't treat initial whitespace as
751 * part of the arguement. */
752 if (node->token->type == SPACE)
753 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -0700754 argument = _token_list_create (arguments);
755 _argument_list_append (arguments, argument);
756 }
757 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700758 }
Carl Worthc7581c22010-05-25 17:41:07 -0700759 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700760
761 if (node && paren_count)
762 return FUNCTION_UNBALANCED_PARENTHESES;
763
764 *node_ret = last;
765
766 return FUNCTION_STATUS_SUCCESS;
767}
768
769/* Prints the expansion of *node (consuming further tokens from the
770 * list as necessary). Upon return *node will be the last consumed
771 * node, such that further processing can continue with node->next. */
772static void
773_glcpp_parser_print_expanded_function (glcpp_parser_t *parser,
774 token_node_t **node_ret)
775{
776 macro_t *macro;
777 token_node_t *node;
778 const char *identifier;
779 argument_list_t *arguments;
780 function_status_t status;
Carl Worth9ce18cf2010-05-25 17:32:21 -0700781 token_list_t *expanded;
782 token_node_t *i, *j;
783 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -0700784
785 node = *node_ret;
786 identifier = node->token->value.str;
787
788 macro = hash_table_find (parser->defines, identifier);
789
790 assert (macro->is_function);
791
Carl Worth9ce18cf2010-05-25 17:32:21 -0700792 arguments = _argument_list_create (parser);
793 status = _arguments_parse (arguments, node_ret);
Carl Worthb1854fd2010-05-25 16:28:26 -0700794
795 switch (status) {
796 case FUNCTION_STATUS_SUCCESS:
797 break;
798 case FUNCTION_NOT_A_FUNCTION:
799 printf ("%s", identifier);
Carl Worthae6517f2010-05-25 15:24:59 -0700800 return;
Carl Worthb1854fd2010-05-25 16:28:26 -0700801 case FUNCTION_UNBALANCED_PARENTHESES:
802 fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n",
803 identifier);
804 exit (1);
Carl Worthae6517f2010-05-25 15:24:59 -0700805 }
806
Carl Worth9ce18cf2010-05-25 17:32:21 -0700807 if (macro->replacements == NULL) {
808 talloc_free (arguments);
809 return;
810 }
811
812
813 if (_argument_list_length (arguments) !=
814 _string_list_length (macro->parameters))
815 {
816 fprintf (stderr,
817 "Error: macro %s invoked with %d arguments (expected %d)\n",
818 identifier,
819 _argument_list_length (arguments),
820 _string_list_length (macro->parameters));
821 return;
822 }
823
824 expanded = _token_list_create (arguments);
825
826 for (i = macro->replacements->head; i; i = i->next) {
827 if (i->token->type == IDENTIFIER &&
828 _string_list_contains (macro->parameters,
829 i->token->value.str,
830 &parameter_index))
831 {
832 token_list_t *argument;
833 argument = _argument_list_member_at (arguments,
834 parameter_index);
835 for (j = argument->head; j; j = j->next)
836 {
837 _token_list_append (expanded, j->token);
838 }
839 } else {
840 _token_list_append (expanded, i->token);
841 }
842 }
843
Carl Worthae6517f2010-05-25 15:24:59 -0700844 _string_list_push (parser->active, identifier);
Carl Worth9ce18cf2010-05-25 17:32:21 -0700845 _glcpp_parser_print_expanded_token_list (parser, expanded);
Carl Worthae6517f2010-05-25 15:24:59 -0700846 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -0700847
848 talloc_free (arguments);
Carl Worthae6517f2010-05-25 15:24:59 -0700849}
850
851void
852_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
853 token_list_t *list)
854{
855 token_node_t *node;
Carl Worthb1854fd2010-05-25 16:28:26 -0700856 function_status_t function_status;
Carl Worthae6517f2010-05-25 15:24:59 -0700857
858 if (list == NULL)
859 return;
860
Carl Worth10ae4382010-05-25 20:35:01 -0700861 _token_list_trim_trailing_space (list);
862
Carl Worthae6517f2010-05-25 15:24:59 -0700863 for (node = list->head; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -0700864 if (_glcpp_parser_print_expanded_token (parser, node->token))
865 _glcpp_parser_print_expanded_function (parser, &node);
Carl Worthae6517f2010-05-25 15:24:59 -0700866 }
867}
868
869void
Carl Worthfcbbb462010-05-13 09:36:23 -0700870_define_object_macro (glcpp_parser_t *parser,
871 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -0700872 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700873{
874 macro_t *macro;
875
876 macro = xtalloc (parser, macro_t);
877
878 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700879 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700880 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700881 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700882
883 hash_table_insert (parser->defines, macro, identifier);
884}
885
886void
887_define_function_macro (glcpp_parser_t *parser,
888 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700889 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -0700890 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700891{
892 macro_t *macro;
893
894 macro = xtalloc (parser, macro_t);
895
896 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700897 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700898 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700899 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700900
901 hash_table_insert (parser->defines, macro, identifier);
902}
903
Carl Worth8f38aff2010-05-19 10:01:29 -0700904static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700905glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700906{
Carl Worth5aa7ea02010-05-25 18:39:43 -0700907 return glcpp_lex (parser->scanner);
Carl Worth8f38aff2010-05-19 10:01:29 -0700908}
Carl Worthb20d33c2010-05-20 22:27:07 -0700909
910static void
911_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
912{
913 skip_type_t current = SKIP_NO_SKIP;
914 skip_node_t *node;
915
916 if (parser->skip_stack)
917 current = parser->skip_stack->type;
918
919 node = xtalloc (parser, skip_node_t);
920
921 if (current == SKIP_NO_SKIP) {
922 if (condition)
923 node->type = SKIP_NO_SKIP;
924 else
925 node->type = SKIP_TO_ELSE;
926 } else {
927 node->type = SKIP_TO_ENDIF;
928 }
929
930 node->next = parser->skip_stack;
931 parser->skip_stack = node;
932}
933
934static void
935_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
936 int condition)
937{
938 if (parser->skip_stack == NULL) {
939 fprintf (stderr, "Error: %s without #if\n", type);
940 exit (1);
941 }
942
943 if (parser->skip_stack->type == SKIP_TO_ELSE) {
944 if (condition)
945 parser->skip_stack->type = SKIP_NO_SKIP;
946 } else {
947 parser->skip_stack->type = SKIP_TO_ENDIF;
948 }
949}
Carl Worth80dc60b2010-05-25 14:42:00 -0700950
Carl Worthb20d33c2010-05-20 22:27:07 -0700951static void
952_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
953{
954 skip_node_t *node;
955
956 if (parser->skip_stack == NULL) {
957 fprintf (stderr, "Error: #endif without #if\n");
958 exit (1);
959 }
960
961 node = parser->skip_stack;
962 parser->skip_stack = node->next;
963 talloc_free (node);
964}