blob: 2383c93117f3a541b339f972b201b0fa4a909ee3 [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 Worthaaa9acb2010-05-19 13:28:24 -070037 string_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 Worthaaa9acb2010-05-19 13:28:24 -070043 string_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070044
Carl Wortha807fb72010-05-18 22:10:04 -070045void
Carl Worth2be8be02010-05-14 10:31:43 -070046_expand_object_macro (glcpp_parser_t *parser, const char *identifier);
47
Carl Wortha807fb72010-05-18 22:10:04 -070048void
Carl Worth2be8be02010-05-14 10:31:43 -070049_expand_function_macro (glcpp_parser_t *parser,
50 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -070051 argument_list_t *arguments);
Carl Worthfcbbb462010-05-13 09:36:23 -070052
53void
Carl Worth2be8be02010-05-14 10:31:43 -070054_print_string_list (string_list_t *list);
Carl Worth33cc4002010-05-12 12:17:10 -070055
Carl Worth610053b2010-05-14 10:05:11 -070056string_list_t *
57_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070058
59void
Carl Worth610053b2010-05-14 10:05:11 -070060_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070061
62void
Carl Worth610053b2010-05-14 10:05:11 -070063_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070064
Carl Worthdcc2ecd2010-05-13 12:56:42 -070065int
Carl Worth610053b2010-05-14 10:05:11 -070066_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070067
Carl Worthdcc2ecd2010-05-13 12:56:42 -070068int
Carl Worth610053b2010-05-14 10:05:11 -070069_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070070
Carl Worth8f6a8282010-05-14 10:44:19 -070071argument_list_t *
72_argument_list_create (void *ctx);
73
74void
75_argument_list_append (argument_list_t *list, string_list_t *argument);
76
77int
78_argument_list_length (argument_list_t *list);
79
80string_list_t *
81_argument_list_member_at (argument_list_t *list, int index);
82
Carl Worthaaa9acb2010-05-19 13:28:24 -070083static void
84glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
85 macro_t *macro,
86 argument_list_t *arguments);
87
88static void
89glcpp_parser_pop_expansion (glcpp_parser_t *parser);
90
Carl Worth0293b2e2010-05-19 10:05:40 -070091#define yylex glcpp_parser_lex
92
Carl Worth8f38aff2010-05-19 10:01:29 -070093static int
Carl Worth0293b2e2010-05-19 10:05:40 -070094glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -070095
Carl Worth3a37b872010-05-10 11:44:09 -070096%}
97
Carl Worth33cc4002010-05-12 12:17:10 -070098%union {
99 char *str;
Carl Worth8f6a8282010-05-14 10:44:19 -0700100 string_list_t *string_list;
101 argument_list_t *argument_list;
Carl Worth33cc4002010-05-12 12:17:10 -0700102}
103
Carl Worth0b27b5f2010-05-10 16:16:06 -0700104%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700105%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700106
Carl Worthaaa9acb2010-05-19 13:28:24 -0700107%token DEFINE FUNC_MACRO IDENTIFIER OBJ_MACRO NEWLINE SPACE TOKEN UNDEF
108%type <str> argument_word FUNC_MACRO IDENTIFIER OBJ_MACRO TOKEN
109%type <string_list> argument macro parameter_list replacement_list pp_tokens
Carl Worth8f6a8282010-05-14 10:44:19 -0700110%type <argument_list> argument_list
Carl Worth3a37b872010-05-10 11:44:09 -0700111
Carl Worth796e1f02010-05-17 12:45:16 -0700112/* Hard to remove shift/reduce conflicts documented as follows:
113 *
114 * 1. '(' after FUNC_MACRO name which is correctly resolved to shift
115 * to form macro invocation rather than reducing directly to
116 * content.
Carl Worth69f390d2010-05-19 07:42:42 -0700117 *
118 * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
119 * shift to form macro invocation rather than reducing directly to
120 * argument.
Carl Worth796e1f02010-05-17 12:45:16 -0700121 */
Carl Worth69f390d2010-05-19 07:42:42 -0700122%expect 2
Carl Worth796e1f02010-05-17 12:45:16 -0700123
Carl Worth3a37b872010-05-10 11:44:09 -0700124%%
125
Carl Worth33cc4002010-05-12 12:17:10 -0700126input:
127 /* empty */
Carl Worth8bcb6f12010-05-12 13:21:20 -0700128| input content
Carl Worth3a37b872010-05-10 11:44:09 -0700129;
130
Carl Worth04af1352010-05-14 10:17:38 -0700131 /* We do all printing at the content level */
Carl Worth33cc4002010-05-12 12:17:10 -0700132content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700133 IDENTIFIER {
134 printf ("%s", $1);
135 talloc_free ($1);
136 }
137| TOKEN {
138 printf ("%s", $1);
139 talloc_free ($1);
140 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700141| FUNC_MACRO {
142 printf ("%s", $1);
143 talloc_free ($1);
144 }
Carl Wortha807fb72010-05-18 22:10:04 -0700145| directive {
146 printf ("\n");
Carl Worth2be8be02010-05-14 10:31:43 -0700147 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700148| '(' { printf ("("); }
149| ')' { printf (")"); }
150| ',' { printf (","); }
Carl Wortha807fb72010-05-18 22:10:04 -0700151| macro
Carl Worthcd27e642010-05-12 13:11:50 -0700152;
153
Carl Worthfcbbb462010-05-13 09:36:23 -0700154macro:
155 FUNC_MACRO '(' argument_list ')' {
Carl Wortha807fb72010-05-18 22:10:04 -0700156 _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700157 }
158| OBJ_MACRO {
Carl Wortha807fb72010-05-18 22:10:04 -0700159 _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700160 talloc_free ($1);
161 }
162;
163
164argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700165 /* empty */ {
166 $$ = _argument_list_create (parser);
167 }
168| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700169 $$ = _argument_list_create (parser);
170 _argument_list_append ($$, $1);
171 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700172| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700173 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700174 $$ = $1;
175 }
176;
177
178argument:
Carl Worth59ca9892010-05-19 07:49:47 -0700179 argument_word {
Carl Worth610053b2010-05-14 10:05:11 -0700180 $$ = _string_list_create (parser);
Carl Worthac070e82010-05-14 11:33:00 -0700181 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700182 }
Carl Worth59ca9892010-05-19 07:49:47 -0700183| argument argument_word {
Carl Worth610053b2010-05-14 10:05:11 -0700184 _string_list_append_item ($1, $2);
Carl Worthfcbbb462010-05-13 09:36:23 -0700185 talloc_free ($2);
Carl Worth3596bb12010-05-14 16:53:52 -0700186 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700187 }
Carl Worth3596bb12010-05-14 16:53:52 -0700188| argument '(' argument ')' {
189 _string_list_append_item ($1, "(");
190 _string_list_append_list ($1, $3);
191 _string_list_append_item ($1, ")");
192 $$ = $1;
193 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700194;
195
Carl Worth59ca9892010-05-19 07:49:47 -0700196argument_word:
197 IDENTIFIER { $$ = $1; }
198| TOKEN { $$ = $1; }
199| FUNC_MACRO { $$ = $1; }
Carl Worth5d211422010-05-19 07:57:03 -0700200| macro { $$ = xtalloc_strdup (parser, ""); }
Carl Worth59ca9892010-05-19 07:49:47 -0700201;
202
203
Carl Worth33cc4002010-05-12 12:17:10 -0700204directive:
Carl Worthaaa9acb2010-05-19 13:28:24 -0700205 DEFINE IDENTIFIER NEWLINE {
206 string_list_t *list = _string_list_create (parser);
207 _define_object_macro (parser, $2, list);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700208 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700209| DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
210 _define_object_macro (parser, $2, $4);
211 }
212| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
Carl Worth81f01432010-05-14 17:08:45 -0700213 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700214 }
Carl Wortha807fb72010-05-18 22:10:04 -0700215| UNDEF IDENTIFIER {
216 string_list_t *macro = hash_table_find (parser->defines, $2);
217 if (macro) {
Carl Worthfcbbb462010-05-13 09:36:23 -0700218 /* XXX: Need hash table to support a real way
219 * to remove an element rather than prefixing
220 * a new node with data of NULL like this. */
221 hash_table_insert (parser->defines, NULL, $2);
Carl Wortha807fb72010-05-18 22:10:04 -0700222 talloc_free (macro);
Carl Worthfcbbb462010-05-13 09:36:23 -0700223 }
224 talloc_free ($2);
225 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700226;
227
Carl Worthfcbbb462010-05-13 09:36:23 -0700228parameter_list:
229 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700230 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700231 }
Carl Wortha807fb72010-05-18 22:10:04 -0700232| IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700233 $$ = _string_list_create (parser);
234 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700235 talloc_free ($1);
236 }
Carl Wortha807fb72010-05-18 22:10:04 -0700237| parameter_list ',' IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700238 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700239 talloc_free ($3);
240 $$ = $1;
241 }
242;
243
Carl Worthaaa9acb2010-05-19 13:28:24 -0700244replacement_list:
245 /* empty */ {
246 $$ = _string_list_create (parser);
247 }
248| pp_tokens {
249 $$ = $1;
250 }
251;
252
253
254pp_tokens:
255 TOKEN {
256 $$ = _string_list_create (parser);
257 _string_list_append_item ($$, $1);
258 }
259| pp_tokens TOKEN {
260 _string_list_append_item ($1, $2);
261 $$ = $1;
262 }
263;
264
Carl Worth33cc4002010-05-12 12:17:10 -0700265%%
266
Carl Worth610053b2010-05-14 10:05:11 -0700267string_list_t *
268_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700269{
Carl Worth610053b2010-05-14 10:05:11 -0700270 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700271
Carl Worth610053b2010-05-14 10:05:11 -0700272 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700273 list->head = NULL;
274 list->tail = NULL;
275
276 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700277}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700278
Carl Worth33cc4002010-05-12 12:17:10 -0700279void
Carl Worth610053b2010-05-14 10:05:11 -0700280_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700281{
282 if (list->head == NULL) {
283 list->head = tail->head;
284 } else {
285 list->tail->next = tail->head;
286 }
287
288 list->tail = tail->tail;
289}
290
291void
Carl Worth610053b2010-05-14 10:05:11 -0700292_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700293{
Carl Worth610053b2010-05-14 10:05:11 -0700294 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700295
Carl Worth610053b2010-05-14 10:05:11 -0700296 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700297 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700298
299 node->next = NULL;
300
301 if (list->head == NULL) {
302 list->head = node;
303 } else {
304 list->tail->next = node;
305 }
306
307 list->tail = node;
308}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700309
310int
Carl Worth610053b2010-05-14 10:05:11 -0700311_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700312{
Carl Worth610053b2010-05-14 10:05:11 -0700313 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700314 int i;
315
316 if (list == NULL)
317 return 0;
318
319 for (i = 0, node = list->head; node; i++, node = node->next) {
320 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700321 if (index)
322 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700323 return 1;
324 }
325 }
326
327 return 0;
328}
329
330int
Carl Worth610053b2010-05-14 10:05:11 -0700331_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700332{
333 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700334 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700335
336 if (list == NULL)
337 return 0;
338
339 for (node = list->head; node; node = node->next)
340 length++;
341
342 return length;
343}
344
Carl Worth2be8be02010-05-14 10:31:43 -0700345void
346_print_string_list (string_list_t *list)
347{
348 string_node_t *node;
349
350 if (list == NULL)
351 return;
352
Carl Worth81f01432010-05-14 17:08:45 -0700353 for (node = list->head; node; node = node->next) {
Carl Worth2be8be02010-05-14 10:31:43 -0700354 printf ("%s", node->str);
Carl Worth81f01432010-05-14 17:08:45 -0700355 if (node->next)
356 printf (" ");
357 }
Carl Worth2be8be02010-05-14 10:31:43 -0700358}
359
Carl Worth8f6a8282010-05-14 10:44:19 -0700360argument_list_t *
361_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700362{
Carl Worth8f6a8282010-05-14 10:44:19 -0700363 argument_list_t *list;
364
365 list = xtalloc (ctx, argument_list_t);
366 list->head = NULL;
367 list->tail = NULL;
368
369 return list;
370}
371
372void
373_argument_list_append (argument_list_t *list, string_list_t *argument)
374{
375 argument_node_t *node;
376
377 if (argument == NULL || argument->head == NULL)
378 return;
379
380 node = xtalloc (list, argument_node_t);
381 node->argument = argument;
382
383 node->next = NULL;
384
385 if (list->head == NULL) {
386 list->head = node;
387 } else {
388 list->tail->next = node;
389 }
390
391 list->tail = node;
392}
393
394int
395_argument_list_length (argument_list_t *list)
396{
397 int length = 0;
398 argument_node_t *node;
399
400 if (list == NULL)
401 return 0;
402
403 for (node = list->head; node; node = node->next)
404 length++;
405
406 return length;
407}
408
409string_list_t *
410_argument_list_member_at (argument_list_t *list, int index)
411{
412 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700413 int i;
414
415 if (list == NULL)
416 return NULL;
417
418 node = list->head;
419 for (i = 0; i < index; i++) {
420 node = node->next;
421 if (node == NULL)
422 break;
423 }
424
425 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700426 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700427
428 return NULL;
429}
Carl Worth33cc4002010-05-12 12:17:10 -0700430
Carl Worth3a37b872010-05-10 11:44:09 -0700431void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700432yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700433{
434 fprintf (stderr, "Parse error: %s\n", error);
435}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700436
Carl Worth33cc4002010-05-12 12:17:10 -0700437glcpp_parser_t *
438glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700439{
Carl Worth33cc4002010-05-12 12:17:10 -0700440 glcpp_parser_t *parser;
441
Carl Worth5070a202010-05-12 12:45:33 -0700442 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700443
Carl Worth8f38aff2010-05-19 10:01:29 -0700444 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700445 parser->defines = hash_table_ctor (32, hash_table_string_hash,
446 hash_table_string_compare);
Carl Wortha807fb72010-05-18 22:10:04 -0700447 parser->expansions = NULL;
448
Carl Worth33cc4002010-05-12 12:17:10 -0700449 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700450}
451
452int
453glcpp_parser_parse (glcpp_parser_t *parser)
454{
455 return yyparse (parser);
456}
457
458void
Carl Worth33cc4002010-05-12 12:17:10 -0700459glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700460{
Carl Worth8f38aff2010-05-19 10:01:29 -0700461 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700462 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700463 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700464}
Carl Worthc6d5af32010-05-11 12:30:09 -0700465
Carl Worthbe0e2e92010-05-19 07:29:22 -0700466static int
467glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
468{
469 expansion_node_t *node;
470
471 for (node = parser->expansions; node; node = node->next) {
472 if (node->macro &&
473 strcmp (node->macro->identifier, member) == 0)
474 {
475 return 1;
476 }
477 }
478
479 return 0;
480}
481
Carl Wortha807fb72010-05-18 22:10:04 -0700482token_class_t
483glcpp_parser_classify_token (glcpp_parser_t *parser,
484 const char *identifier,
485 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700486{
Carl Worthfcbbb462010-05-13 09:36:23 -0700487 macro_t *macro;
488
Carl Wortha807fb72010-05-18 22:10:04 -0700489 /* First we check if we are currently expanding a
490 * function-like macro, and if so, whether the parameter list
491 * contains a parameter matching this token name. */
492 if (parser->expansions &&
493 parser->expansions->macro &&
494 parser->expansions->macro->parameters)
495 {
496 string_list_t *list;
497
498 list = parser->expansions->macro->parameters;
499
500 if (_string_list_contains (list, identifier, parameter_index))
501 return TOKEN_CLASS_ARGUMENT;
502 }
503
504 /* If not a function-like macro parameter, we next check if
505 * this token is a macro itself. */
506
Carl Worthfcbbb462010-05-13 09:36:23 -0700507 macro = hash_table_find (parser->defines, identifier);
508
509 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700510 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700511
Carl Worthbe0e2e92010-05-19 07:29:22 -0700512 /* Don't consider this a macro if we are already actively
513 * expanding this macro. */
514 if (glcpp_parser_is_expanding (parser, identifier))
515 return TOKEN_CLASS_IDENTIFIER;
516
517 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700518 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700519 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700520 else
Carl Wortha807fb72010-05-18 22:10:04 -0700521 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700522}
523
Carl Worth33cc4002010-05-12 12:17:10 -0700524void
Carl Worthfcbbb462010-05-13 09:36:23 -0700525_define_object_macro (glcpp_parser_t *parser,
526 const char *identifier,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700527 string_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700528{
529 macro_t *macro;
530
531 macro = xtalloc (parser, macro_t);
532
533 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700534 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700535 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700536 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700537
538 hash_table_insert (parser->defines, macro, identifier);
539}
540
541void
542_define_function_macro (glcpp_parser_t *parser,
543 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700544 string_list_t *parameters,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700545 string_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700546{
547 macro_t *macro;
548
549 macro = xtalloc (parser, macro_t);
550
551 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700552 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700553 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700554 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700555
556 hash_table_insert (parser->defines, macro, identifier);
557}
558
Carl Wortha807fb72010-05-18 22:10:04 -0700559static void
560_glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
561 macro_t *macro,
562 argument_list_t *arguments,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700563 string_node_t *replacements)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700564{
Carl Wortha807fb72010-05-18 22:10:04 -0700565 expansion_node_t *node;
566
567 node = xtalloc (parser, expansion_node_t);
568
569 node->macro = macro;
570 node->arguments = arguments;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700571 node->replacements = replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700572
573 node->next = parser->expansions;
574 parser->expansions = node;
Carl Wortha807fb72010-05-18 22:10:04 -0700575}
576
Carl Worthaaa9acb2010-05-19 13:28:24 -0700577static void
Carl Wortha807fb72010-05-18 22:10:04 -0700578glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
579 macro_t *macro,
580 argument_list_t *arguments)
581{
582 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700583 macro->replacements->head);
Carl Wortha807fb72010-05-18 22:10:04 -0700584}
585
586void
587glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
588 int argument_index)
589{
590 argument_list_t *arguments;
591 string_list_t *argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700592
Carl Wortha807fb72010-05-18 22:10:04 -0700593 arguments = parser->expansions->arguments;
Carl Worth2be8be02010-05-14 10:31:43 -0700594
Carl Wortha807fb72010-05-18 22:10:04 -0700595 argument = _argument_list_member_at (arguments, argument_index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700596
Carl Wortha807fb72010-05-18 22:10:04 -0700597 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700598 argument->head);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700599}
600
Carl Worthaaa9acb2010-05-19 13:28:24 -0700601static void
Carl Wortha807fb72010-05-18 22:10:04 -0700602glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700603{
Carl Wortha807fb72010-05-18 22:10:04 -0700604 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700605
Carl Wortha807fb72010-05-18 22:10:04 -0700606 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700607
Carl Wortha807fb72010-05-18 22:10:04 -0700608 if (node == NULL) {
609 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
610 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700611 }
612
Carl Wortha807fb72010-05-18 22:10:04 -0700613 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700614
Carl Wortha807fb72010-05-18 22:10:04 -0700615 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700616}
617
Carl Wortha807fb72010-05-18 22:10:04 -0700618void
Carl Worth2be8be02010-05-14 10:31:43 -0700619_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700620{
Carl Worthfcbbb462010-05-13 09:36:23 -0700621 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700622
Carl Worthfcbbb462010-05-13 09:36:23 -0700623 macro = hash_table_find (parser->defines, identifier);
624 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700625 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700626
Carl Worthbe0e2e92010-05-19 07:29:22 -0700627 glcpp_parser_push_expansion_macro (parser, macro, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700628}
629
Carl Wortha807fb72010-05-18 22:10:04 -0700630void
Carl Worth2be8be02010-05-14 10:31:43 -0700631_expand_function_macro (glcpp_parser_t *parser,
632 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700633 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700634{
Carl Worthfcbbb462010-05-13 09:36:23 -0700635 macro_t *macro;
636
637 macro = hash_table_find (parser->defines, identifier);
638 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700639 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700640
Carl Worth8f6a8282010-05-14 10:44:19 -0700641 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700642 _string_list_length (macro->parameters))
643 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700644 fprintf (stderr,
645 "Error: macro %s invoked with %d arguments (expected %d)\n",
646 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700647 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700648 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700649 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700650 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700651
Carl Worthbe0e2e92010-05-19 07:29:22 -0700652 glcpp_parser_push_expansion_macro (parser, macro, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700653}
Carl Worth8f38aff2010-05-19 10:01:29 -0700654
655static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700656glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700657{
Carl Worthaaa9acb2010-05-19 13:28:24 -0700658 expansion_node_t *expansion;
659 string_node_t *replacements;
660 int parameter_index;
661
662 /* Who says C can't do efficient tail recursion? */
663 RECURSE:
664
665 expansion = parser->expansions;
666
667 if (expansion == NULL)
668 return glcpp_lex (parser->scanner);
669
670 replacements = expansion->replacements;
671
672 /* Pop expansion when replacements is exhausted. */
673 if (replacements == NULL) {
674 glcpp_parser_pop_expansion (parser);
675 goto RECURSE;
676 }
677
678 expansion->replacements = replacements->next;
679
680 if (strcmp (replacements->str, "(") == 0)
681 return '(';
682 else if (strcmp (replacements->str, ")") == 0)
683 return ')';
684 else if (strcmp (replacements->str, ",") == 0)
685 return ',';
686
687 yylval.str = xtalloc_strdup (parser, replacements->str);
688
689 switch (glcpp_parser_classify_token (parser, yylval.str,
690 &parameter_index))
691 {
692 case TOKEN_CLASS_ARGUMENT:
693 talloc_free (yylval.str);
694 glcpp_parser_push_expansion_argument (parser,
695 parameter_index);
696 goto RECURSE;
697 break;
698 case TOKEN_CLASS_IDENTIFIER:
699 return IDENTIFIER;
700 break;
701 case TOKEN_CLASS_FUNC_MACRO:
702 return FUNC_MACRO;
703 break;
704 default:
705 case TOKEN_CLASS_OBJ_MACRO:
706 return OBJ_MACRO;
707 break;
708 }
Carl Worth8f38aff2010-05-19 10:01:29 -0700709}