blob: 16d2a28a007ccf5669868749df670722071e598d [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 Worth33cc4002010-05-12 12:17:10 -070028#include <talloc.h>
Carl Worth3a37b872010-05-10 11:44:09 -070029
Carl Wortha1e32bc2010-05-10 13:17:25 -070030#include "glcpp.h"
31
Carl Worth0b27b5f2010-05-10 16:16:06 -070032#define YYLEX_PARAM parser->scanner
Carl Worth3a37b872010-05-10 11:44:09 -070033
Carl Worthfcbbb462010-05-13 09:36:23 -070034typedef struct {
35 int is_function;
Carl Worthc5e98552010-05-14 10:12:21 -070036 string_list_t *parameters;
37 string_list_t *replacements;
Carl Worthfcbbb462010-05-13 09:36:23 -070038} macro_t;
39
Carl Worth33cc4002010-05-12 12:17:10 -070040struct glcpp_parser {
41 yyscan_t scanner;
42 struct hash_table *defines;
43};
44
Carl Worth3a37b872010-05-10 11:44:09 -070045void
Carl Wortha1e32bc2010-05-10 13:17:25 -070046yyerror (void *scanner, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070047
Carl Worth33cc4002010-05-12 12:17:10 -070048void
Carl Worthfcbbb462010-05-13 09:36:23 -070049_define_object_macro (glcpp_parser_t *parser,
50 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070051 string_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070052
53void
54_define_function_macro (glcpp_parser_t *parser,
55 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070056 string_list_t *parameters,
57 string_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070058
Carl Worth2be8be02010-05-14 10:31:43 -070059string_list_t *
60_expand_object_macro (glcpp_parser_t *parser, const char *identifier);
61
62string_list_t *
63_expand_function_macro (glcpp_parser_t *parser,
64 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -070065 argument_list_t *arguments);
Carl Worthfcbbb462010-05-13 09:36:23 -070066
67void
Carl Worth2be8be02010-05-14 10:31:43 -070068_print_string_list (string_list_t *list);
Carl Worth33cc4002010-05-12 12:17:10 -070069
Carl Worth610053b2010-05-14 10:05:11 -070070string_list_t *
71_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070072
73void
Carl Worth610053b2010-05-14 10:05:11 -070074_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070075
76void
Carl Worth610053b2010-05-14 10:05:11 -070077_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070078
Carl Worth420d05a2010-05-17 10:15:23 -070079void
80_string_list_push (string_list_t *list, const char *str);
81
82void
83_string_list_pop (string_list_t *list);
84
Carl Worthdcc2ecd2010-05-13 12:56:42 -070085int
Carl Worth610053b2010-05-14 10:05:11 -070086_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070087
Carl Worthdcc2ecd2010-05-13 12:56:42 -070088int
Carl Worth610053b2010-05-14 10:05:11 -070089_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070090
Carl Worth8f6a8282010-05-14 10:44:19 -070091argument_list_t *
92_argument_list_create (void *ctx);
93
94void
95_argument_list_append (argument_list_t *list, string_list_t *argument);
96
97int
98_argument_list_length (argument_list_t *list);
99
100string_list_t *
101_argument_list_member_at (argument_list_t *list, int index);
102
Carl Worth3a37b872010-05-10 11:44:09 -0700103%}
104
Carl Worth33cc4002010-05-12 12:17:10 -0700105%union {
106 char *str;
Carl Worth8f6a8282010-05-14 10:44:19 -0700107 string_list_t *string_list;
108 argument_list_t *argument_list;
Carl Worth33cc4002010-05-12 12:17:10 -0700109}
110
Carl Worth0b27b5f2010-05-10 16:16:06 -0700111%parse-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700112%lex-param {void *scanner}
113
Carl Worth0a93cbb2010-05-13 10:29:07 -0700114%token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO SPACE TOKEN UNDEF
Carl Worth38bd27b2010-05-14 12:05:37 -0700115%type <str> FUNC_MACRO IDENTIFIER identifier_perhaps_macro OBJ_MACRO replacement_word TOKEN word
Carl Worth8f6a8282010-05-14 10:44:19 -0700116%type <string_list> argument macro parameter_list replacement_list
117%type <argument_list> argument_list
Carl Worth3a37b872010-05-10 11:44:09 -0700118
119%%
120
Carl Worth33cc4002010-05-12 12:17:10 -0700121input:
122 /* empty */
Carl Worth8bcb6f12010-05-12 13:21:20 -0700123| input content
Carl Worth3a37b872010-05-10 11:44:09 -0700124;
125
Carl Worth04af1352010-05-14 10:17:38 -0700126 /* We do all printing at the content level */
Carl Worth33cc4002010-05-12 12:17:10 -0700127content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700128 IDENTIFIER {
129 printf ("%s", $1);
130 talloc_free ($1);
131 }
132| TOKEN {
133 printf ("%s", $1);
134 talloc_free ($1);
135 }
Carl Worth2be8be02010-05-14 10:31:43 -0700136| macro {
137 _print_string_list ($1);
138 }
Carl Worth04af1352010-05-14 10:17:38 -0700139| directive_with_newline { printf ("\n"); }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700140| NEWLINE { printf ("\n"); }
141| '(' { printf ("("); }
142| ')' { printf (")"); }
143| ',' { printf (","); }
Carl Worthcd27e642010-05-12 13:11:50 -0700144;
145
Carl Worthfcbbb462010-05-13 09:36:23 -0700146macro:
147 FUNC_MACRO '(' argument_list ')' {
Carl Worth2be8be02010-05-14 10:31:43 -0700148 $$ = _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700149 }
150| OBJ_MACRO {
Carl Worth2be8be02010-05-14 10:31:43 -0700151 $$ = _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700152 talloc_free ($1);
153 }
154;
155
156argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700157 /* empty */ {
158 $$ = _argument_list_create (parser);
159 }
160| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700161 $$ = _argument_list_create (parser);
162 _argument_list_append ($$, $1);
163 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700164| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700165 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700166 $$ = $1;
167 }
168;
169
170argument:
Carl Worthac070e82010-05-14 11:33:00 -0700171 word {
Carl Worth610053b2010-05-14 10:05:11 -0700172 $$ = _string_list_create (parser);
Carl Worthac070e82010-05-14 11:33:00 -0700173 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700174 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700175| macro {
176 $$ = $1;
177 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700178| argument word {
Carl Worth610053b2010-05-14 10:05:11 -0700179 _string_list_append_item ($1, $2);
Carl Worthfcbbb462010-05-13 09:36:23 -0700180 talloc_free ($2);
Carl Worth3596bb12010-05-14 16:53:52 -0700181 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700182 }
Carl Worth3596bb12010-05-14 16:53:52 -0700183| argument '(' argument ')' {
184 _string_list_append_item ($1, "(");
185 _string_list_append_list ($1, $3);
186 _string_list_append_item ($1, ")");
187 $$ = $1;
188 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700189;
190
Carl Worthcd27e642010-05-12 13:11:50 -0700191directive_with_newline:
Carl Worth04af1352010-05-14 10:17:38 -0700192 directive NEWLINE
Carl Worth3a37b872010-05-10 11:44:09 -0700193;
194
Carl Worth33cc4002010-05-12 12:17:10 -0700195directive:
Carl Worth0a93cbb2010-05-13 10:29:07 -0700196 DEFINE IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700197 string_list_t *list = _string_list_create (parser);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700198 _define_object_macro (parser, $2, list);
199 }
200| DEFINE IDENTIFIER SPACE replacement_list {
201 _define_object_macro (parser, $2, $4);
Carl Worthcd27e642010-05-12 13:11:50 -0700202 }
Carl Worth48b94da2010-05-13 10:46:29 -0700203| DEFINE IDENTIFIER '(' parameter_list ')' {
Carl Worth610053b2010-05-14 10:05:11 -0700204 string_list_t *list = _string_list_create (parser);
Carl Worth48b94da2010-05-13 10:46:29 -0700205 _define_function_macro (parser, $2, $4, list);
206 }
Carl Worth81f01432010-05-14 17:08:45 -0700207| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list {
208 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700209 }
210| UNDEF FUNC_MACRO {
Carl Worth610053b2010-05-14 10:05:11 -0700211 string_list_t *replacement = hash_table_find (parser->defines, $2);
Carl Worthfcbbb462010-05-13 09:36:23 -0700212 if (replacement) {
213 /* XXX: Need hash table to support a real way
214 * to remove an element rather than prefixing
215 * a new node with data of NULL like this. */
216 hash_table_insert (parser->defines, NULL, $2);
217 talloc_free (replacement);
218 }
219 talloc_free ($2);
220 }
221| UNDEF OBJ_MACRO {
Carl Worth610053b2010-05-14 10:05:11 -0700222 string_list_t *replacement = hash_table_find (parser->defines, $2);
Carl Worthcd27e642010-05-12 13:11:50 -0700223 if (replacement) {
224 /* XXX: Need hash table to support a real way
225 * to remove an element rather than prefixing
226 * a new node with data of NULL like this. */
227 hash_table_insert (parser->defines, NULL, $2);
228 talloc_free (replacement);
229 }
230 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -0700231 }
232;
233
234replacement_list:
Carl Worth38bd27b2010-05-14 12:05:37 -0700235 replacement_word {
Carl Worth610053b2010-05-14 10:05:11 -0700236 $$ = _string_list_create (parser);
237 _string_list_append_item ($$, $1);
Carl Worth48b94da2010-05-13 10:46:29 -0700238 talloc_free ($1);
Carl Worth33cc4002010-05-12 12:17:10 -0700239 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700240| replacement_list replacement_word {
Carl Worth610053b2010-05-14 10:05:11 -0700241 _string_list_append_item ($1, $2);
Carl Worth5070a202010-05-12 12:45:33 -0700242 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -0700243 $$ = $1;
244 }
245;
246
Carl Worth38bd27b2010-05-14 12:05:37 -0700247replacement_word:
248 word { $$ = $1; }
249| FUNC_MACRO { $$ = $1; }
250| OBJ_MACRO { $$ = $1; }
251| '(' { $$ = xtalloc_strdup (parser, "("); }
252| ')' { $$ = xtalloc_strdup (parser, ")"); }
253| ',' { $$ = xtalloc_strdup (parser, ","); }
Carl Worth38bd27b2010-05-14 12:05:37 -0700254;
255
Carl Worthfcbbb462010-05-13 09:36:23 -0700256parameter_list:
257 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700258 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700259 }
Carl Worth7f9aa362010-05-13 12:58:49 -0700260| identifier_perhaps_macro {
Carl Worth610053b2010-05-14 10:05:11 -0700261 $$ = _string_list_create (parser);
262 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700263 talloc_free ($1);
264 }
Carl Worth7f9aa362010-05-13 12:58:49 -0700265| parameter_list ',' identifier_perhaps_macro {
Carl Worth610053b2010-05-14 10:05:11 -0700266 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700267 talloc_free ($3);
268 $$ = $1;
269 }
270;
271
Carl Worth7f9aa362010-05-13 12:58:49 -0700272identifier_perhaps_macro:
273 IDENTIFIER { $$ = $1; }
274| FUNC_MACRO { $$ = $1; }
275| OBJ_MACRO { $$ = $1; }
276;
277
Carl Worth0a93cbb2010-05-13 10:29:07 -0700278word:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700279 IDENTIFIER { $$ = $1; }
Carl Worth9f62a7e2010-05-13 07:38:29 -0700280| TOKEN { $$ = $1; }
Carl Worth33cc4002010-05-12 12:17:10 -0700281;
282
283%%
284
Carl Worth610053b2010-05-14 10:05:11 -0700285string_list_t *
286_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700287{
Carl Worth610053b2010-05-14 10:05:11 -0700288 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700289
Carl Worth610053b2010-05-14 10:05:11 -0700290 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700291 list->head = NULL;
292 list->tail = NULL;
293
294 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700295}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700296
Carl Worth33cc4002010-05-12 12:17:10 -0700297void
Carl Worth610053b2010-05-14 10:05:11 -0700298_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700299{
300 if (list->head == NULL) {
301 list->head = tail->head;
302 } else {
303 list->tail->next = tail->head;
304 }
305
306 list->tail = tail->tail;
307}
308
309void
Carl Worth610053b2010-05-14 10:05:11 -0700310_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700311{
Carl Worth610053b2010-05-14 10:05:11 -0700312 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700313
Carl Worth610053b2010-05-14 10:05:11 -0700314 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700315 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700316
317 node->next = NULL;
318
319 if (list->head == NULL) {
320 list->head = node;
321 } else {
322 list->tail->next = node;
323 }
324
325 list->tail = node;
326}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700327
Carl Worth420d05a2010-05-17 10:15:23 -0700328void
329_string_list_push (string_list_t *list, const char *str)
330{
331 string_node_t *node;
332
333 node = xtalloc (list, string_node_t);
334 node->str = xtalloc_strdup (node, str);
335
336 node->next = list->head;
337
338 if (list->tail == NULL) {
339 list->tail = node;
340 }
341
342 list->head = node;
343}
344
345void
346_string_list_pop (string_list_t *list)
347{
348 string_node_t *node;
349
350 node = list->head;
351
352 if (node == NULL) {
353 fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
354 exit (1);
355 }
356
357 list->head = node->next;
358
359 if (list->tail == node) {
360 assert (node->next == NULL);
361 list->tail = NULL;
362 }
363
364 talloc_free (node);
365}
366
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700367int
Carl Worth610053b2010-05-14 10:05:11 -0700368_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700369{
Carl Worth610053b2010-05-14 10:05:11 -0700370 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700371 int i;
372
373 if (list == NULL)
374 return 0;
375
376 for (i = 0, node = list->head; node; i++, node = node->next) {
377 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700378 if (index)
379 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700380 return 1;
381 }
382 }
383
384 return 0;
385}
386
387int
Carl Worth610053b2010-05-14 10:05:11 -0700388_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700389{
390 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700391 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700392
393 if (list == NULL)
394 return 0;
395
396 for (node = list->head; node; node = node->next)
397 length++;
398
399 return length;
400}
401
Carl Worth2be8be02010-05-14 10:31:43 -0700402void
403_print_string_list (string_list_t *list)
404{
405 string_node_t *node;
406
407 if (list == NULL)
408 return;
409
Carl Worth81f01432010-05-14 17:08:45 -0700410 for (node = list->head; node; node = node->next) {
Carl Worth2be8be02010-05-14 10:31:43 -0700411 printf ("%s", node->str);
Carl Worth81f01432010-05-14 17:08:45 -0700412 if (node->next)
413 printf (" ");
414 }
Carl Worth2be8be02010-05-14 10:31:43 -0700415}
416
Carl Worth8f6a8282010-05-14 10:44:19 -0700417argument_list_t *
418_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700419{
Carl Worth8f6a8282010-05-14 10:44:19 -0700420 argument_list_t *list;
421
422 list = xtalloc (ctx, argument_list_t);
423 list->head = NULL;
424 list->tail = NULL;
425
426 return list;
427}
428
429void
430_argument_list_append (argument_list_t *list, string_list_t *argument)
431{
432 argument_node_t *node;
433
434 if (argument == NULL || argument->head == NULL)
435 return;
436
437 node = xtalloc (list, argument_node_t);
438 node->argument = argument;
439
440 node->next = NULL;
441
442 if (list->head == NULL) {
443 list->head = node;
444 } else {
445 list->tail->next = node;
446 }
447
448 list->tail = node;
449}
450
451int
452_argument_list_length (argument_list_t *list)
453{
454 int length = 0;
455 argument_node_t *node;
456
457 if (list == NULL)
458 return 0;
459
460 for (node = list->head; node; node = node->next)
461 length++;
462
463 return length;
464}
465
466string_list_t *
467_argument_list_member_at (argument_list_t *list, int index)
468{
469 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700470 int i;
471
472 if (list == NULL)
473 return NULL;
474
475 node = list->head;
476 for (i = 0; i < index; i++) {
477 node = node->next;
478 if (node == NULL)
479 break;
480 }
481
482 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700483 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700484
485 return NULL;
486}
Carl Worth33cc4002010-05-12 12:17:10 -0700487
Carl Worth3a37b872010-05-10 11:44:09 -0700488void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700489yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700490{
491 fprintf (stderr, "Parse error: %s\n", error);
492}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700493
Carl Worth33cc4002010-05-12 12:17:10 -0700494glcpp_parser_t *
495glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700496{
Carl Worth33cc4002010-05-12 12:17:10 -0700497 glcpp_parser_t *parser;
498
Carl Worth5070a202010-05-12 12:45:33 -0700499 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700500
Carl Worth5070a202010-05-12 12:45:33 -0700501 yylex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700502 parser->defines = hash_table_ctor (32, hash_table_string_hash,
503 hash_table_string_compare);
Carl Worth33cc4002010-05-12 12:17:10 -0700504
505 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700506}
507
508int
509glcpp_parser_parse (glcpp_parser_t *parser)
510{
511 return yyparse (parser);
512}
513
514void
Carl Worth33cc4002010-05-12 12:17:10 -0700515glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700516{
517 yylex_destroy (parser->scanner);
518 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700519 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700520}
Carl Worthc6d5af32010-05-11 12:30:09 -0700521
Carl Worthfcbbb462010-05-13 09:36:23 -0700522macro_type_t
523glcpp_parser_macro_type (glcpp_parser_t *parser, const char *identifier)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700524{
Carl Worthfcbbb462010-05-13 09:36:23 -0700525 macro_t *macro;
526
527 macro = hash_table_find (parser->defines, identifier);
528
529 if (macro == NULL)
530 return MACRO_TYPE_UNDEFINED;
531
532 if (macro->is_function)
533 return MACRO_TYPE_FUNCTION;
534 else
535 return MACRO_TYPE_OBJECT;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700536}
537
Carl Worth33cc4002010-05-12 12:17:10 -0700538void
Carl Worthfcbbb462010-05-13 09:36:23 -0700539_define_object_macro (glcpp_parser_t *parser,
540 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700541 string_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700542{
543 macro_t *macro;
544
545 macro = xtalloc (parser, macro_t);
546
547 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700548 macro->parameters = NULL;
549 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700550
551 hash_table_insert (parser->defines, macro, identifier);
552}
553
554void
555_define_function_macro (glcpp_parser_t *parser,
556 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700557 string_list_t *parameters,
558 string_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700559{
560 macro_t *macro;
561
562 macro = xtalloc (parser, macro_t);
563
564 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700565 macro->parameters = talloc_steal (macro, parameters);
566 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700567
568 hash_table_insert (parser->defines, macro, identifier);
569}
570
Carl Worth2be8be02010-05-14 10:31:43 -0700571static string_list_t *
572_expand_macro_recursive (glcpp_parser_t *parser,
573 const char *token,
Carl Worth420d05a2010-05-17 10:15:23 -0700574 string_list_t *active,
Carl Worth2be8be02010-05-14 10:31:43 -0700575 string_list_t *parameters,
Carl Worth8f6a8282010-05-14 10:44:19 -0700576 argument_list_t *arguments);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700577
Carl Worth2be8be02010-05-14 10:31:43 -0700578static string_list_t *
579_expand_string_list_recursive (glcpp_parser_t *parser,
580 string_list_t *list,
Carl Worth420d05a2010-05-17 10:15:23 -0700581 string_list_t *active,
Carl Worth2be8be02010-05-14 10:31:43 -0700582 string_list_t *parameters,
Carl Worth8f6a8282010-05-14 10:44:19 -0700583 argument_list_t *arguments)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700584{
Carl Worth2be8be02010-05-14 10:31:43 -0700585 string_list_t *result;
586 string_list_t *child;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700587 const char *token;
Carl Worth610053b2010-05-14 10:05:11 -0700588 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700589 int index;
590
Carl Worth2be8be02010-05-14 10:31:43 -0700591 result = _string_list_create (parser);
592
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700593 for (node = list->head ; node ; node = node->next) {
594 token = node->str;
595
Carl Worth420d05a2010-05-17 10:15:23 -0700596 /* Don't expand this macro if it's on the active
597 * stack, (meaning we're already in the process of
598 * expanding it). */
599 if (_string_list_contains (active, token, NULL)) {
Carl Worth2be8be02010-05-14 10:31:43 -0700600 _string_list_append_item (result, token);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700601 continue;
602 }
603
Carl Worth610053b2010-05-14 10:05:11 -0700604 if (_string_list_contains (parameters, token, &index)) {
Carl Worth8f6a8282010-05-14 10:44:19 -0700605 string_list_t *argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700606
Carl Worth8f6a8282010-05-14 10:44:19 -0700607 argument = _argument_list_member_at (arguments, index);
608 child = _expand_string_list_recursive (parser, argument,
Carl Worth420d05a2010-05-17 10:15:23 -0700609 active, NULL, NULL);
Carl Worth2be8be02010-05-14 10:31:43 -0700610 _string_list_append_list (result, child);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700611 } else {
Carl Worth2be8be02010-05-14 10:31:43 -0700612 child = _expand_macro_recursive (parser, token,
Carl Worth420d05a2010-05-17 10:15:23 -0700613 active, parameters,
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700614 arguments);
Carl Worth2be8be02010-05-14 10:31:43 -0700615 _string_list_append_list (result, child);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700616 }
617 }
Carl Worth2be8be02010-05-14 10:31:43 -0700618
619 return result;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700620}
621
622
Carl Worth2be8be02010-05-14 10:31:43 -0700623static string_list_t *
624_expand_macro_recursive (glcpp_parser_t *parser,
625 const char *token,
Carl Worth420d05a2010-05-17 10:15:23 -0700626 string_list_t *active,
Carl Worth2be8be02010-05-14 10:31:43 -0700627 string_list_t *parameters,
Carl Worth8f6a8282010-05-14 10:44:19 -0700628 argument_list_t *arguments)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700629{
630 macro_t *macro;
Carl Worthc5e98552010-05-14 10:12:21 -0700631 string_list_t *replacements;
Carl Worth420d05a2010-05-17 10:15:23 -0700632 string_list_t *result;
633
634 if (active == NULL)
635 active = _string_list_create (NULL);
636
637 _string_list_push (active, token);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700638
639 macro = hash_table_find (parser->defines, token);
640 if (macro == NULL) {
Carl Worth2be8be02010-05-14 10:31:43 -0700641 string_list_t *result;
642
643 result = _string_list_create (parser);
644 _string_list_append_item (result, token);
645 return result;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700646 }
647
Carl Worthc5e98552010-05-14 10:12:21 -0700648 replacements = macro->replacements;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700649
Carl Worth420d05a2010-05-17 10:15:23 -0700650 result = _expand_string_list_recursive (parser, replacements,
651 active, parameters, arguments);
652
653 _string_list_pop (active);
654 if (_string_list_length (active) == 0)
655 talloc_free (active);
656
657 return result;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700658}
659
Carl Worth2be8be02010-05-14 10:31:43 -0700660string_list_t *
661_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700662{
Carl Worthfcbbb462010-05-13 09:36:23 -0700663 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700664
Carl Worthfcbbb462010-05-13 09:36:23 -0700665 macro = hash_table_find (parser->defines, identifier);
666 assert (! macro->is_function);
667
Carl Worth420d05a2010-05-17 10:15:23 -0700668 return _expand_macro_recursive (parser, identifier, NULL,
Carl Worth2be8be02010-05-14 10:31:43 -0700669 NULL, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700670}
671
Carl Worth2be8be02010-05-14 10:31:43 -0700672string_list_t *
673_expand_function_macro (glcpp_parser_t *parser,
674 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700675 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700676{
Carl Worthfcbbb462010-05-13 09:36:23 -0700677 macro_t *macro;
678
679 macro = hash_table_find (parser->defines, identifier);
680 assert (macro->is_function);
681
Carl Worth8f6a8282010-05-14 10:44:19 -0700682 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700683 _string_list_length (macro->parameters))
684 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700685 fprintf (stderr,
686 "Error: macro %s invoked with %d arguments (expected %d)\n",
687 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700688 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700689 _string_list_length (macro->parameters));
Carl Worth2be8be02010-05-14 10:31:43 -0700690 return NULL;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700691 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700692
Carl Worth420d05a2010-05-17 10:15:23 -0700693 return _expand_macro_recursive (parser, identifier, NULL,
Carl Worth2be8be02010-05-14 10:31:43 -0700694 macro->parameters, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700695}