blob: f972ec372b84cd10b417a0e956892bd061479f62 [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 Worthdcc2ecd2010-05-13 12:56:42 -070079int
Carl Worth610053b2010-05-14 10:05:11 -070080_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070081
Carl Worthdcc2ecd2010-05-13 12:56:42 -070082int
Carl Worth610053b2010-05-14 10:05:11 -070083_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070084
Carl Worth8f6a8282010-05-14 10:44:19 -070085argument_list_t *
86_argument_list_create (void *ctx);
87
88void
89_argument_list_append (argument_list_t *list, string_list_t *argument);
90
91int
92_argument_list_length (argument_list_t *list);
93
94string_list_t *
95_argument_list_member_at (argument_list_t *list, int index);
96
Carl Worth3a37b872010-05-10 11:44:09 -070097%}
98
Carl Worth33cc4002010-05-12 12:17:10 -070099%union {
100 char *str;
Carl Worth8f6a8282010-05-14 10:44:19 -0700101 string_list_t *string_list;
102 argument_list_t *argument_list;
Carl Worth33cc4002010-05-12 12:17:10 -0700103}
104
Carl Worth0b27b5f2010-05-10 16:16:06 -0700105%parse-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700106%lex-param {void *scanner}
107
Carl Worth0a93cbb2010-05-13 10:29:07 -0700108%token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO SPACE TOKEN UNDEF
Carl Worth38bd27b2010-05-14 12:05:37 -0700109%type <str> FUNC_MACRO IDENTIFIER identifier_perhaps_macro OBJ_MACRO replacement_word TOKEN word
Carl Worth8f6a8282010-05-14 10:44:19 -0700110%type <string_list> argument macro parameter_list replacement_list
111%type <argument_list> argument_list
Carl Worth3a37b872010-05-10 11:44:09 -0700112
113%%
114
Carl Worth33cc4002010-05-12 12:17:10 -0700115input:
116 /* empty */
Carl Worth8bcb6f12010-05-12 13:21:20 -0700117| input content
Carl Worth3a37b872010-05-10 11:44:09 -0700118;
119
Carl Worth04af1352010-05-14 10:17:38 -0700120 /* We do all printing at the content level */
Carl Worth33cc4002010-05-12 12:17:10 -0700121content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700122 IDENTIFIER {
123 printf ("%s", $1);
124 talloc_free ($1);
125 }
126| TOKEN {
127 printf ("%s", $1);
128 talloc_free ($1);
129 }
Carl Worth2be8be02010-05-14 10:31:43 -0700130| macro {
131 _print_string_list ($1);
132 }
Carl Worth04af1352010-05-14 10:17:38 -0700133| directive_with_newline { printf ("\n"); }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700134| NEWLINE { printf ("\n"); }
135| '(' { printf ("("); }
136| ')' { printf (")"); }
137| ',' { printf (","); }
Carl Worth48b94da2010-05-13 10:46:29 -0700138| SPACE { printf (" "); }
Carl Worthcd27e642010-05-12 13:11:50 -0700139;
140
Carl Worthfcbbb462010-05-13 09:36:23 -0700141macro:
142 FUNC_MACRO '(' argument_list ')' {
Carl Worth2be8be02010-05-14 10:31:43 -0700143 $$ = _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700144 }
145| OBJ_MACRO {
Carl Worth2be8be02010-05-14 10:31:43 -0700146 $$ = _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700147 talloc_free ($1);
148 }
149;
150
151argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700152 /* empty */ {
153 $$ = _argument_list_create (parser);
154 }
155| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700156 $$ = _argument_list_create (parser);
157 _argument_list_append ($$, $1);
158 }
159| argument_list ',' SPACE argument {
160 _argument_list_append ($1, $4);
161 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700162 }
163| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700164 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700165 $$ = $1;
166 }
167;
168
169argument:
Carl Worthac070e82010-05-14 11:33:00 -0700170 word {
Carl Worth610053b2010-05-14 10:05:11 -0700171 $$ = _string_list_create (parser);
Carl Worthac070e82010-05-14 11:33:00 -0700172 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700173 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700174| macro {
175 $$ = $1;
176 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700177| argument word {
Carl Worth610053b2010-05-14 10:05:11 -0700178 _string_list_append_item ($1, $2);
Carl Worthfcbbb462010-05-13 09:36:23 -0700179 talloc_free ($2);
180 }
Carl Worth8f6a8282010-05-14 10:44:19 -0700181| argument SPACE word {
182 _string_list_append_item ($1, " ");
183 _string_list_append_item ($1, $3);
184 talloc_free ($3);
185 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700186| argument '(' argument ')'
187;
188
Carl Worthcd27e642010-05-12 13:11:50 -0700189directive_with_newline:
Carl Worth04af1352010-05-14 10:17:38 -0700190 directive NEWLINE
Carl Worth3a37b872010-05-10 11:44:09 -0700191;
192
Carl Worth33cc4002010-05-12 12:17:10 -0700193directive:
Carl Worth0a93cbb2010-05-13 10:29:07 -0700194 DEFINE IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700195 string_list_t *list = _string_list_create (parser);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700196 _define_object_macro (parser, $2, list);
197 }
198| DEFINE IDENTIFIER SPACE replacement_list {
199 _define_object_macro (parser, $2, $4);
Carl Worthcd27e642010-05-12 13:11:50 -0700200 }
Carl Worth48b94da2010-05-13 10:46:29 -0700201| DEFINE IDENTIFIER '(' parameter_list ')' {
Carl Worth610053b2010-05-14 10:05:11 -0700202 string_list_t *list = _string_list_create (parser);
Carl Worth48b94da2010-05-13 10:46:29 -0700203 _define_function_macro (parser, $2, $4, list);
204 }
205| DEFINE IDENTIFIER '(' parameter_list ')' SPACE replacement_list {
206 _define_function_macro (parser, $2, $4, $7);
Carl Worthfcbbb462010-05-13 09:36:23 -0700207 }
208| UNDEF FUNC_MACRO {
Carl Worth610053b2010-05-14 10:05:11 -0700209 string_list_t *replacement = hash_table_find (parser->defines, $2);
Carl Worthfcbbb462010-05-13 09:36:23 -0700210 if (replacement) {
211 /* XXX: Need hash table to support a real way
212 * to remove an element rather than prefixing
213 * a new node with data of NULL like this. */
214 hash_table_insert (parser->defines, NULL, $2);
215 talloc_free (replacement);
216 }
217 talloc_free ($2);
218 }
219| UNDEF OBJ_MACRO {
Carl Worth610053b2010-05-14 10:05:11 -0700220 string_list_t *replacement = hash_table_find (parser->defines, $2);
Carl Worthcd27e642010-05-12 13:11:50 -0700221 if (replacement) {
222 /* XXX: Need hash table to support a real way
223 * to remove an element rather than prefixing
224 * a new node with data of NULL like this. */
225 hash_table_insert (parser->defines, NULL, $2);
226 talloc_free (replacement);
227 }
228 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -0700229 }
230;
231
232replacement_list:
Carl Worth38bd27b2010-05-14 12:05:37 -0700233 replacement_word {
Carl Worth610053b2010-05-14 10:05:11 -0700234 $$ = _string_list_create (parser);
235 _string_list_append_item ($$, $1);
Carl Worth48b94da2010-05-13 10:46:29 -0700236 talloc_free ($1);
Carl Worth33cc4002010-05-12 12:17:10 -0700237 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700238| replacement_list replacement_word {
Carl Worth610053b2010-05-14 10:05:11 -0700239 _string_list_append_item ($1, $2);
Carl Worth5070a202010-05-12 12:45:33 -0700240 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -0700241 $$ = $1;
242 }
243;
244
Carl Worth38bd27b2010-05-14 12:05:37 -0700245replacement_word:
246 word { $$ = $1; }
247| FUNC_MACRO { $$ = $1; }
248| OBJ_MACRO { $$ = $1; }
249| '(' { $$ = xtalloc_strdup (parser, "("); }
250| ')' { $$ = xtalloc_strdup (parser, ")"); }
251| ',' { $$ = xtalloc_strdup (parser, ","); }
252| SPACE { $$ = xtalloc_strdup (parser, " "); }
253;
254
Carl Worthfcbbb462010-05-13 09:36:23 -0700255parameter_list:
256 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700257 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700258 }
Carl Worth7f9aa362010-05-13 12:58:49 -0700259| identifier_perhaps_macro {
Carl Worth610053b2010-05-14 10:05:11 -0700260 $$ = _string_list_create (parser);
261 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700262 talloc_free ($1);
263 }
Carl Worth7f9aa362010-05-13 12:58:49 -0700264| parameter_list ',' identifier_perhaps_macro {
Carl Worth610053b2010-05-14 10:05:11 -0700265 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700266 talloc_free ($3);
267 $$ = $1;
268 }
269;
270
Carl Worth7f9aa362010-05-13 12:58:49 -0700271identifier_perhaps_macro:
272 IDENTIFIER { $$ = $1; }
273| FUNC_MACRO { $$ = $1; }
274| OBJ_MACRO { $$ = $1; }
275;
276
Carl Worth0a93cbb2010-05-13 10:29:07 -0700277word:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700278 IDENTIFIER { $$ = $1; }
Carl Worth9f62a7e2010-05-13 07:38:29 -0700279| TOKEN { $$ = $1; }
Carl Worth33cc4002010-05-12 12:17:10 -0700280;
281
282%%
283
Carl Worth610053b2010-05-14 10:05:11 -0700284string_list_t *
285_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700286{
Carl Worth610053b2010-05-14 10:05:11 -0700287 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700288
Carl Worth610053b2010-05-14 10:05:11 -0700289 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700290 list->head = NULL;
291 list->tail = NULL;
292
293 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700294}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700295
Carl Worth33cc4002010-05-12 12:17:10 -0700296void
Carl Worth610053b2010-05-14 10:05:11 -0700297_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700298{
299 if (list->head == NULL) {
300 list->head = tail->head;
301 } else {
302 list->tail->next = tail->head;
303 }
304
305 list->tail = tail->tail;
306}
307
308void
Carl Worth610053b2010-05-14 10:05:11 -0700309_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700310{
Carl Worth610053b2010-05-14 10:05:11 -0700311 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700312
Carl Worth610053b2010-05-14 10:05:11 -0700313 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700314 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700315
316 node->next = NULL;
317
318 if (list->head == NULL) {
319 list->head = node;
320 } else {
321 list->tail->next = node;
322 }
323
324 list->tail = node;
325}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700326
327int
Carl Worth610053b2010-05-14 10:05:11 -0700328_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700329{
Carl Worth610053b2010-05-14 10:05:11 -0700330 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700331 int i;
332
333 if (list == NULL)
334 return 0;
335
336 for (i = 0, node = list->head; node; i++, node = node->next) {
337 if (strcmp (node->str, member) == 0) {
338 *index = i;
339 return 1;
340 }
341 }
342
343 return 0;
344}
345
346int
Carl Worth610053b2010-05-14 10:05:11 -0700347_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700348{
349 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700350 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700351
352 if (list == NULL)
353 return 0;
354
355 for (node = list->head; node; node = node->next)
356 length++;
357
358 return length;
359}
360
Carl Worth2be8be02010-05-14 10:31:43 -0700361void
362_print_string_list (string_list_t *list)
363{
364 string_node_t *node;
365
366 if (list == NULL)
367 return;
368
369 for (node = list->head; node; node = node->next)
370 printf ("%s", node->str);
371}
372
Carl Worth8f6a8282010-05-14 10:44:19 -0700373argument_list_t *
374_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700375{
Carl Worth8f6a8282010-05-14 10:44:19 -0700376 argument_list_t *list;
377
378 list = xtalloc (ctx, argument_list_t);
379 list->head = NULL;
380 list->tail = NULL;
381
382 return list;
383}
384
385void
386_argument_list_append (argument_list_t *list, string_list_t *argument)
387{
388 argument_node_t *node;
389
390 if (argument == NULL || argument->head == NULL)
391 return;
392
393 node = xtalloc (list, argument_node_t);
394 node->argument = argument;
395
396 node->next = NULL;
397
398 if (list->head == NULL) {
399 list->head = node;
400 } else {
401 list->tail->next = node;
402 }
403
404 list->tail = node;
405}
406
407int
408_argument_list_length (argument_list_t *list)
409{
410 int length = 0;
411 argument_node_t *node;
412
413 if (list == NULL)
414 return 0;
415
416 for (node = list->head; node; node = node->next)
417 length++;
418
419 return length;
420}
421
422string_list_t *
423_argument_list_member_at (argument_list_t *list, int index)
424{
425 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700426 int i;
427
428 if (list == NULL)
429 return NULL;
430
431 node = list->head;
432 for (i = 0; i < index; i++) {
433 node = node->next;
434 if (node == NULL)
435 break;
436 }
437
438 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700439 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700440
441 return NULL;
442}
Carl Worth33cc4002010-05-12 12:17:10 -0700443
Carl Worth3a37b872010-05-10 11:44:09 -0700444void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700445yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700446{
447 fprintf (stderr, "Parse error: %s\n", error);
448}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700449
Carl Worth33cc4002010-05-12 12:17:10 -0700450glcpp_parser_t *
451glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700452{
Carl Worth33cc4002010-05-12 12:17:10 -0700453 glcpp_parser_t *parser;
454
Carl Worth5070a202010-05-12 12:45:33 -0700455 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700456
Carl Worth5070a202010-05-12 12:45:33 -0700457 yylex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700458 parser->defines = hash_table_ctor (32, hash_table_string_hash,
459 hash_table_string_compare);
Carl Worth33cc4002010-05-12 12:17:10 -0700460
461 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700462}
463
464int
465glcpp_parser_parse (glcpp_parser_t *parser)
466{
467 return yyparse (parser);
468}
469
470void
Carl Worth33cc4002010-05-12 12:17:10 -0700471glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700472{
473 yylex_destroy (parser->scanner);
474 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700475 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700476}
Carl Worthc6d5af32010-05-11 12:30:09 -0700477
Carl Worthfcbbb462010-05-13 09:36:23 -0700478macro_type_t
479glcpp_parser_macro_type (glcpp_parser_t *parser, const char *identifier)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700480{
Carl Worthfcbbb462010-05-13 09:36:23 -0700481 macro_t *macro;
482
483 macro = hash_table_find (parser->defines, identifier);
484
485 if (macro == NULL)
486 return MACRO_TYPE_UNDEFINED;
487
488 if (macro->is_function)
489 return MACRO_TYPE_FUNCTION;
490 else
491 return MACRO_TYPE_OBJECT;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700492}
493
Carl Worth33cc4002010-05-12 12:17:10 -0700494void
Carl Worthfcbbb462010-05-13 09:36:23 -0700495_define_object_macro (glcpp_parser_t *parser,
496 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700497 string_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700498{
499 macro_t *macro;
500
501 macro = xtalloc (parser, macro_t);
502
503 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700504 macro->parameters = NULL;
505 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700506
507 hash_table_insert (parser->defines, macro, identifier);
508}
509
510void
511_define_function_macro (glcpp_parser_t *parser,
512 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700513 string_list_t *parameters,
514 string_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700515{
516 macro_t *macro;
517
518 macro = xtalloc (parser, macro_t);
519
520 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700521 macro->parameters = talloc_steal (macro, parameters);
522 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700523
524 hash_table_insert (parser->defines, macro, identifier);
525}
526
Carl Worth2be8be02010-05-14 10:31:43 -0700527static string_list_t *
528_expand_macro_recursive (glcpp_parser_t *parser,
529 const char *token,
530 const char *orig,
531 string_list_t *parameters,
Carl Worth8f6a8282010-05-14 10:44:19 -0700532 argument_list_t *arguments);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700533
Carl Worth2be8be02010-05-14 10:31:43 -0700534static string_list_t *
535_expand_string_list_recursive (glcpp_parser_t *parser,
536 string_list_t *list,
537 const char *orig,
538 string_list_t *parameters,
Carl Worth8f6a8282010-05-14 10:44:19 -0700539 argument_list_t *arguments)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700540{
Carl Worth2be8be02010-05-14 10:31:43 -0700541 string_list_t *result;
542 string_list_t *child;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700543 const char *token;
Carl Worth610053b2010-05-14 10:05:11 -0700544 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700545 int index;
546
Carl Worth2be8be02010-05-14 10:31:43 -0700547 result = _string_list_create (parser);
548
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700549 for (node = list->head ; node ; node = node->next) {
550 token = node->str;
551
552 if (strcmp (token, orig) == 0) {
Carl Worth2be8be02010-05-14 10:31:43 -0700553 _string_list_append_item (result, token);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700554 continue;
555 }
556
Carl Worth610053b2010-05-14 10:05:11 -0700557 if (_string_list_contains (parameters, token, &index)) {
Carl Worth8f6a8282010-05-14 10:44:19 -0700558 string_list_t *argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700559
Carl Worth8f6a8282010-05-14 10:44:19 -0700560 argument = _argument_list_member_at (arguments, index);
561 child = _expand_string_list_recursive (parser, argument,
562 orig, NULL, NULL);
Carl Worth2be8be02010-05-14 10:31:43 -0700563 _string_list_append_list (result, child);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700564 } else {
Carl Worth2be8be02010-05-14 10:31:43 -0700565 child = _expand_macro_recursive (parser, token,
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700566 orig, parameters,
567 arguments);
Carl Worth2be8be02010-05-14 10:31:43 -0700568 _string_list_append_list (result, child);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700569 }
570 }
Carl Worth2be8be02010-05-14 10:31:43 -0700571
572 return result;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700573}
574
575
Carl Worth2be8be02010-05-14 10:31:43 -0700576static string_list_t *
577_expand_macro_recursive (glcpp_parser_t *parser,
578 const char *token,
579 const char *orig,
580 string_list_t *parameters,
Carl Worth8f6a8282010-05-14 10:44:19 -0700581 argument_list_t *arguments)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700582{
583 macro_t *macro;
Carl Worthc5e98552010-05-14 10:12:21 -0700584 string_list_t *replacements;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700585
586 macro = hash_table_find (parser->defines, token);
587 if (macro == NULL) {
Carl Worth2be8be02010-05-14 10:31:43 -0700588 string_list_t *result;
589
590 result = _string_list_create (parser);
591 _string_list_append_item (result, token);
592 return result;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700593 }
594
Carl Worthc5e98552010-05-14 10:12:21 -0700595 replacements = macro->replacements;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700596
Carl Worth2be8be02010-05-14 10:31:43 -0700597 return _expand_string_list_recursive (parser, replacements,
598 orig, parameters, arguments);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700599}
600
Carl Worth2be8be02010-05-14 10:31:43 -0700601string_list_t *
602_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700603{
Carl Worthfcbbb462010-05-13 09:36:23 -0700604 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700605
Carl Worthfcbbb462010-05-13 09:36:23 -0700606 macro = hash_table_find (parser->defines, identifier);
607 assert (! macro->is_function);
608
Carl Worth2be8be02010-05-14 10:31:43 -0700609 return _expand_macro_recursive (parser, identifier, identifier,
610 NULL, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700611}
612
Carl Worth2be8be02010-05-14 10:31:43 -0700613string_list_t *
614_expand_function_macro (glcpp_parser_t *parser,
615 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700616 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700617{
Carl Worth2be8be02010-05-14 10:31:43 -0700618 string_list_t *result;
Carl Worthfcbbb462010-05-13 09:36:23 -0700619 macro_t *macro;
620
Carl Worth2be8be02010-05-14 10:31:43 -0700621 result = _string_list_create (parser);
622
Carl Worthfcbbb462010-05-13 09:36:23 -0700623 macro = hash_table_find (parser->defines, identifier);
624 assert (macro->is_function);
625
Carl Worth8f6a8282010-05-14 10:44:19 -0700626 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700627 _string_list_length (macro->parameters))
628 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700629 fprintf (stderr,
630 "Error: macro %s invoked with %d arguments (expected %d)\n",
631 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700632 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700633 _string_list_length (macro->parameters));
Carl Worth2be8be02010-05-14 10:31:43 -0700634 return NULL;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700635 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700636
Carl Worth2be8be02010-05-14 10:31:43 -0700637 return _expand_macro_recursive (parser, identifier, identifier,
638 macro->parameters, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700639}