blob: 8dc07483c18159f8ec168ddef1439e4610f21f36 [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 Worth0b27b5f2010-05-10 16:16:06 -070031#define YYLEX_PARAM parser->scanner
Carl Worth3a37b872010-05-10 11:44:09 -070032
33void
Carl Wortha1e32bc2010-05-10 13:17:25 -070034yyerror (void *scanner, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070035
Carl Worth33cc4002010-05-12 12:17:10 -070036void
Carl Worthfcbbb462010-05-13 09:36:23 -070037_define_object_macro (glcpp_parser_t *parser,
38 const char *macro,
Carl Wortha807fb72010-05-18 22:10:04 -070039 const char *replacement);
Carl Worthfcbbb462010-05-13 09:36:23 -070040
41void
42_define_function_macro (glcpp_parser_t *parser,
43 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070044 string_list_t *parameters,
Carl Wortha807fb72010-05-18 22:10:04 -070045 const char *replacement);
Carl Worthfcbbb462010-05-13 09:36:23 -070046
Carl Wortha807fb72010-05-18 22:10:04 -070047void
Carl Worth2be8be02010-05-14 10:31:43 -070048_expand_object_macro (glcpp_parser_t *parser, const char *identifier);
49
Carl Wortha807fb72010-05-18 22:10:04 -070050void
Carl Worth2be8be02010-05-14 10:31:43 -070051_expand_function_macro (glcpp_parser_t *parser,
52 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -070053 argument_list_t *arguments);
Carl Worthfcbbb462010-05-13 09:36:23 -070054
55void
Carl Worth2be8be02010-05-14 10:31:43 -070056_print_string_list (string_list_t *list);
Carl Worth33cc4002010-05-12 12:17:10 -070057
Carl Worth610053b2010-05-14 10:05:11 -070058string_list_t *
59_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070060
61void
Carl Worth610053b2010-05-14 10:05:11 -070062_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070063
64void
Carl Worth610053b2010-05-14 10:05:11 -070065_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070066
Carl Worthdcc2ecd2010-05-13 12:56:42 -070067int
Carl Worth610053b2010-05-14 10:05:11 -070068_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070069
Carl Worthdcc2ecd2010-05-13 12:56:42 -070070int
Carl Worth610053b2010-05-14 10:05:11 -070071_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070072
Carl Worth8f6a8282010-05-14 10:44:19 -070073argument_list_t *
74_argument_list_create (void *ctx);
75
76void
77_argument_list_append (argument_list_t *list, string_list_t *argument);
78
79int
80_argument_list_length (argument_list_t *list);
81
82string_list_t *
83_argument_list_member_at (argument_list_t *list, int index);
84
Carl Worth3a37b872010-05-10 11:44:09 -070085%}
86
Carl Worth33cc4002010-05-12 12:17:10 -070087%union {
88 char *str;
Carl Worth8f6a8282010-05-14 10:44:19 -070089 string_list_t *string_list;
90 argument_list_t *argument_list;
Carl Worth33cc4002010-05-12 12:17:10 -070091}
92
Carl Worth0b27b5f2010-05-10 16:16:06 -070093%parse-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -070094%lex-param {void *scanner}
95
Carl Wortha807fb72010-05-18 22:10:04 -070096%token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO REPLACEMENT TOKEN UNDEF
97%type <str> FUNC_MACRO IDENTIFIER OBJ_MACRO REPLACEMENT TOKEN word
98%type <string_list> argument macro parameter_list
Carl Worth8f6a8282010-05-14 10:44:19 -070099%type <argument_list> argument_list
Carl Worth3a37b872010-05-10 11:44:09 -0700100
Carl Worth796e1f02010-05-17 12:45:16 -0700101/* Hard to remove shift/reduce conflicts documented as follows:
102 *
103 * 1. '(' after FUNC_MACRO name which is correctly resolved to shift
104 * to form macro invocation rather than reducing directly to
105 * content.
106 */
107%expect 1
108
Carl Worth3a37b872010-05-10 11:44:09 -0700109%%
110
Carl Worth33cc4002010-05-12 12:17:10 -0700111input:
112 /* empty */
Carl Worth8bcb6f12010-05-12 13:21:20 -0700113| input content
Carl Worth3a37b872010-05-10 11:44:09 -0700114;
115
Carl Worth04af1352010-05-14 10:17:38 -0700116 /* We do all printing at the content level */
Carl Worth33cc4002010-05-12 12:17:10 -0700117content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700118 IDENTIFIER {
119 printf ("%s", $1);
120 talloc_free ($1);
121 }
122| TOKEN {
123 printf ("%s", $1);
124 talloc_free ($1);
125 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700126| FUNC_MACRO {
127 printf ("%s", $1);
128 talloc_free ($1);
129 }
Carl Wortha807fb72010-05-18 22:10:04 -0700130| directive {
131 printf ("\n");
Carl Worth2be8be02010-05-14 10:31:43 -0700132 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700133| '(' { printf ("("); }
134| ')' { printf (")"); }
135| ',' { printf (","); }
Carl Wortha807fb72010-05-18 22:10:04 -0700136| macro
Carl Worthcd27e642010-05-12 13:11:50 -0700137;
138
Carl Worthfcbbb462010-05-13 09:36:23 -0700139macro:
140 FUNC_MACRO '(' argument_list ')' {
Carl Wortha807fb72010-05-18 22:10:04 -0700141 _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700142 }
143| OBJ_MACRO {
Carl Wortha807fb72010-05-18 22:10:04 -0700144 _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700145 talloc_free ($1);
146 }
147;
148
149argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700150 /* empty */ {
151 $$ = _argument_list_create (parser);
152 }
153| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700154 $$ = _argument_list_create (parser);
155 _argument_list_append ($$, $1);
156 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700157| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700158 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700159 $$ = $1;
160 }
161;
162
163argument:
Carl Worthac070e82010-05-14 11:33:00 -0700164 word {
Carl Worth610053b2010-05-14 10:05:11 -0700165 $$ = _string_list_create (parser);
Carl Worthac070e82010-05-14 11:33:00 -0700166 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700167 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700168| macro {
Carl Wortha807fb72010-05-18 22:10:04 -0700169 $$ = _string_list_create (parser);
Carl Worth38bd27b2010-05-14 12:05:37 -0700170 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700171| argument word {
Carl Worth610053b2010-05-14 10:05:11 -0700172 _string_list_append_item ($1, $2);
Carl Worthfcbbb462010-05-13 09:36:23 -0700173 talloc_free ($2);
Carl Worth3596bb12010-05-14 16:53:52 -0700174 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700175 }
Carl Worth3596bb12010-05-14 16:53:52 -0700176| argument '(' argument ')' {
177 _string_list_append_item ($1, "(");
178 _string_list_append_list ($1, $3);
179 _string_list_append_item ($1, ")");
180 $$ = $1;
181 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700182;
183
Carl Worth33cc4002010-05-12 12:17:10 -0700184directive:
Carl Wortha807fb72010-05-18 22:10:04 -0700185 DEFINE IDENTIFIER REPLACEMENT {
186 _define_object_macro (parser, $2, $3);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700187 }
Carl Wortha807fb72010-05-18 22:10:04 -0700188| DEFINE IDENTIFIER '(' parameter_list ')' REPLACEMENT {
Carl Worth81f01432010-05-14 17:08:45 -0700189 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700190 }
Carl Wortha807fb72010-05-18 22:10:04 -0700191| UNDEF IDENTIFIER {
192 string_list_t *macro = hash_table_find (parser->defines, $2);
193 if (macro) {
Carl Worthfcbbb462010-05-13 09:36:23 -0700194 /* XXX: Need hash table to support a real way
195 * to remove an element rather than prefixing
196 * a new node with data of NULL like this. */
197 hash_table_insert (parser->defines, NULL, $2);
Carl Wortha807fb72010-05-18 22:10:04 -0700198 talloc_free (macro);
Carl Worthfcbbb462010-05-13 09:36:23 -0700199 }
200 talloc_free ($2);
201 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700202;
203
Carl Worthfcbbb462010-05-13 09:36:23 -0700204parameter_list:
205 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700206 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700207 }
Carl Wortha807fb72010-05-18 22:10:04 -0700208| IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700209 $$ = _string_list_create (parser);
210 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700211 talloc_free ($1);
212 }
Carl Wortha807fb72010-05-18 22:10:04 -0700213| parameter_list ',' IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700214 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700215 talloc_free ($3);
216 $$ = $1;
217 }
218;
219
Carl Worth0a93cbb2010-05-13 10:29:07 -0700220word:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700221 IDENTIFIER { $$ = $1; }
Carl Worth9f62a7e2010-05-13 07:38:29 -0700222| TOKEN { $$ = $1; }
Carl Worth33cc4002010-05-12 12:17:10 -0700223;
224
225%%
226
Carl Worth610053b2010-05-14 10:05:11 -0700227string_list_t *
228_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700229{
Carl Worth610053b2010-05-14 10:05:11 -0700230 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700231
Carl Worth610053b2010-05-14 10:05:11 -0700232 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700233 list->head = NULL;
234 list->tail = NULL;
235
236 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700237}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700238
Carl Worth33cc4002010-05-12 12:17:10 -0700239void
Carl Worth610053b2010-05-14 10:05:11 -0700240_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700241{
242 if (list->head == NULL) {
243 list->head = tail->head;
244 } else {
245 list->tail->next = tail->head;
246 }
247
248 list->tail = tail->tail;
249}
250
251void
Carl Worth610053b2010-05-14 10:05:11 -0700252_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700253{
Carl Worth610053b2010-05-14 10:05:11 -0700254 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700255
Carl Worth610053b2010-05-14 10:05:11 -0700256 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700257 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700258
259 node->next = NULL;
260
261 if (list->head == NULL) {
262 list->head = node;
263 } else {
264 list->tail->next = node;
265 }
266
267 list->tail = node;
268}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700269
270int
Carl Worth610053b2010-05-14 10:05:11 -0700271_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700272{
Carl Worth610053b2010-05-14 10:05:11 -0700273 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700274 int i;
275
276 if (list == NULL)
277 return 0;
278
279 for (i = 0, node = list->head; node; i++, node = node->next) {
280 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700281 if (index)
282 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700283 return 1;
284 }
285 }
286
287 return 0;
288}
289
290int
Carl Worth610053b2010-05-14 10:05:11 -0700291_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700292{
293 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700294 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700295
296 if (list == NULL)
297 return 0;
298
299 for (node = list->head; node; node = node->next)
300 length++;
301
302 return length;
303}
304
Carl Worth2be8be02010-05-14 10:31:43 -0700305void
306_print_string_list (string_list_t *list)
307{
308 string_node_t *node;
309
310 if (list == NULL)
311 return;
312
Carl Worth81f01432010-05-14 17:08:45 -0700313 for (node = list->head; node; node = node->next) {
Carl Worth2be8be02010-05-14 10:31:43 -0700314 printf ("%s", node->str);
Carl Worth81f01432010-05-14 17:08:45 -0700315 if (node->next)
316 printf (" ");
317 }
Carl Worth2be8be02010-05-14 10:31:43 -0700318}
319
Carl Worth8f6a8282010-05-14 10:44:19 -0700320argument_list_t *
321_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700322{
Carl Worth8f6a8282010-05-14 10:44:19 -0700323 argument_list_t *list;
324
325 list = xtalloc (ctx, argument_list_t);
326 list->head = NULL;
327 list->tail = NULL;
328
329 return list;
330}
331
332void
333_argument_list_append (argument_list_t *list, string_list_t *argument)
334{
335 argument_node_t *node;
336
337 if (argument == NULL || argument->head == NULL)
338 return;
339
340 node = xtalloc (list, argument_node_t);
341 node->argument = argument;
342
343 node->next = NULL;
344
345 if (list->head == NULL) {
346 list->head = node;
347 } else {
348 list->tail->next = node;
349 }
350
351 list->tail = node;
352}
353
354int
355_argument_list_length (argument_list_t *list)
356{
357 int length = 0;
358 argument_node_t *node;
359
360 if (list == NULL)
361 return 0;
362
363 for (node = list->head; node; node = node->next)
364 length++;
365
366 return length;
367}
368
369string_list_t *
370_argument_list_member_at (argument_list_t *list, int index)
371{
372 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700373 int i;
374
375 if (list == NULL)
376 return NULL;
377
378 node = list->head;
379 for (i = 0; i < index; i++) {
380 node = node->next;
381 if (node == NULL)
382 break;
383 }
384
385 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700386 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700387
388 return NULL;
389}
Carl Worth33cc4002010-05-12 12:17:10 -0700390
Carl Worth3a37b872010-05-10 11:44:09 -0700391void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700392yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700393{
394 fprintf (stderr, "Parse error: %s\n", error);
395}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700396
Carl Worth33cc4002010-05-12 12:17:10 -0700397glcpp_parser_t *
398glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700399{
Carl Worth33cc4002010-05-12 12:17:10 -0700400 glcpp_parser_t *parser;
401
Carl Worth5070a202010-05-12 12:45:33 -0700402 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700403
Carl Worth5070a202010-05-12 12:45:33 -0700404 yylex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700405 parser->defines = hash_table_ctor (32, hash_table_string_hash,
406 hash_table_string_compare);
Carl Wortha807fb72010-05-18 22:10:04 -0700407 parser->expansions = NULL;
408
409 parser->lex_stack = xtalloc (parser, glcpp_lex_stack_t);
410 parser->lex_stack->parser = parser;
411 parser->lex_stack->head = NULL;
Carl Worth33cc4002010-05-12 12:17:10 -0700412
413 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700414}
415
416int
417glcpp_parser_parse (glcpp_parser_t *parser)
418{
419 return yyparse (parser);
420}
421
422void
Carl Worth33cc4002010-05-12 12:17:10 -0700423glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700424{
425 yylex_destroy (parser->scanner);
426 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700427 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700428}
Carl Worthc6d5af32010-05-11 12:30:09 -0700429
Carl Worthbe0e2e92010-05-19 07:29:22 -0700430static int
431glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
432{
433 expansion_node_t *node;
434
435 for (node = parser->expansions; node; node = node->next) {
436 if (node->macro &&
437 strcmp (node->macro->identifier, member) == 0)
438 {
439 return 1;
440 }
441 }
442
443 return 0;
444}
445
Carl Wortha807fb72010-05-18 22:10:04 -0700446token_class_t
447glcpp_parser_classify_token (glcpp_parser_t *parser,
448 const char *identifier,
449 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700450{
Carl Worthfcbbb462010-05-13 09:36:23 -0700451 macro_t *macro;
452
Carl Wortha807fb72010-05-18 22:10:04 -0700453 /* First we check if we are currently expanding a
454 * function-like macro, and if so, whether the parameter list
455 * contains a parameter matching this token name. */
456 if (parser->expansions &&
457 parser->expansions->macro &&
458 parser->expansions->macro->parameters)
459 {
460 string_list_t *list;
461
462 list = parser->expansions->macro->parameters;
463
464 if (_string_list_contains (list, identifier, parameter_index))
465 return TOKEN_CLASS_ARGUMENT;
466 }
467
468 /* If not a function-like macro parameter, we next check if
469 * this token is a macro itself. */
470
Carl Worthfcbbb462010-05-13 09:36:23 -0700471 macro = hash_table_find (parser->defines, identifier);
472
473 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700474 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700475
Carl Worthbe0e2e92010-05-19 07:29:22 -0700476 /* Don't consider this a macro if we are already actively
477 * expanding this macro. */
478 if (glcpp_parser_is_expanding (parser, identifier))
479 return TOKEN_CLASS_IDENTIFIER;
480
481 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700482 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700483 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700484 else
Carl Wortha807fb72010-05-18 22:10:04 -0700485 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700486}
487
Carl Worth33cc4002010-05-12 12:17:10 -0700488void
Carl Worthfcbbb462010-05-13 09:36:23 -0700489_define_object_macro (glcpp_parser_t *parser,
490 const char *identifier,
Carl Wortha807fb72010-05-18 22:10:04 -0700491 const char *replacement)
Carl Worthfcbbb462010-05-13 09:36:23 -0700492{
493 macro_t *macro;
494
495 macro = xtalloc (parser, macro_t);
496
497 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700498 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700499 macro->identifier = talloc_strdup (macro, identifier);
500 macro->replacement = talloc_steal (macro, replacement);
Carl Worthfcbbb462010-05-13 09:36:23 -0700501
502 hash_table_insert (parser->defines, macro, identifier);
503}
504
505void
506_define_function_macro (glcpp_parser_t *parser,
507 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700508 string_list_t *parameters,
Carl Wortha807fb72010-05-18 22:10:04 -0700509 const char *replacement)
Carl Worthfcbbb462010-05-13 09:36:23 -0700510{
511 macro_t *macro;
512
513 macro = xtalloc (parser, macro_t);
514
515 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700516 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700517 macro->identifier = talloc_strdup (macro, identifier);
518 macro->replacement = talloc_steal (macro, replacement);
Carl Worthfcbbb462010-05-13 09:36:23 -0700519
520 hash_table_insert (parser->defines, macro, identifier);
521}
522
Carl Wortha807fb72010-05-18 22:10:04 -0700523static void
524_glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
525 macro_t *macro,
526 argument_list_t *arguments,
527 const char * replacement)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700528{
Carl Wortha807fb72010-05-18 22:10:04 -0700529 expansion_node_t *node;
530
531 node = xtalloc (parser, expansion_node_t);
532
533 node->macro = macro;
534 node->arguments = arguments;
535
536 node->next = parser->expansions;
537 parser->expansions = node;
538
539 glcpp_lex_stack_push (parser->lex_stack, replacement);
540}
541
542void
543glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
544 macro_t *macro,
545 argument_list_t *arguments)
546{
547 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
548 macro->replacement);
549}
550
551void
552glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
553 int argument_index)
554{
555 argument_list_t *arguments;
556 string_list_t *argument;
Carl Worth610053b2010-05-14 10:05:11 -0700557 string_node_t *node;
Carl Wortha807fb72010-05-18 22:10:04 -0700558 char *argument_str, *s;
559 int length;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700560
Carl Wortha807fb72010-05-18 22:10:04 -0700561 arguments = parser->expansions->arguments;
Carl Worth2be8be02010-05-14 10:31:43 -0700562
Carl Wortha807fb72010-05-18 22:10:04 -0700563 argument = _argument_list_member_at (arguments, argument_index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700564
Carl Wortha807fb72010-05-18 22:10:04 -0700565 length = 0;
566 for (node = argument->head; node; node = node->next)
567 length += strlen (node->str) + 1;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700568
Carl Wortha807fb72010-05-18 22:10:04 -0700569 argument_str = xtalloc_size (parser, length);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700570
Carl Wortha807fb72010-05-18 22:10:04 -0700571 *argument_str = '\0';
572 s = argument_str;
573 for (node = argument->head; node; node = node->next) {
574 strcpy (s, node->str);
575 s += strlen (node->str);
576 if (node->next) {
577 *s = ' ';
578 s++;
579 *s = '\0';
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700580 }
581 }
Carl Worth2be8be02010-05-14 10:31:43 -0700582
Carl Wortha807fb72010-05-18 22:10:04 -0700583 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
584 argument_str);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700585}
586
Carl Wortha807fb72010-05-18 22:10:04 -0700587/* The lexer calls this when it exhausts a string. */
588void
589glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700590{
Carl Wortha807fb72010-05-18 22:10:04 -0700591 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700592
Carl Wortha807fb72010-05-18 22:10:04 -0700593 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700594
Carl Wortha807fb72010-05-18 22:10:04 -0700595 if (node == NULL) {
596 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
597 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700598 }
599
Carl Wortha807fb72010-05-18 22:10:04 -0700600 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700601
Carl Wortha807fb72010-05-18 22:10:04 -0700602 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700603}
604
Carl Wortha807fb72010-05-18 22:10:04 -0700605void
Carl Worth2be8be02010-05-14 10:31:43 -0700606_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700607{
Carl Worthfcbbb462010-05-13 09:36:23 -0700608 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700609
Carl Worthfcbbb462010-05-13 09:36:23 -0700610 macro = hash_table_find (parser->defines, identifier);
611 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700612 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700613
Carl Worthbe0e2e92010-05-19 07:29:22 -0700614 glcpp_parser_push_expansion_macro (parser, macro, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700615}
616
Carl Wortha807fb72010-05-18 22:10:04 -0700617void
Carl Worth2be8be02010-05-14 10:31:43 -0700618_expand_function_macro (glcpp_parser_t *parser,
619 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700620 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700621{
Carl Worthfcbbb462010-05-13 09:36:23 -0700622 macro_t *macro;
623
624 macro = hash_table_find (parser->defines, identifier);
625 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700626 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700627
Carl Worth8f6a8282010-05-14 10:44:19 -0700628 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700629 _string_list_length (macro->parameters))
630 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700631 fprintf (stderr,
632 "Error: macro %s invoked with %d arguments (expected %d)\n",
633 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700634 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700635 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700636 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700637 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700638
Carl Worthbe0e2e92010-05-19 07:29:22 -0700639 glcpp_parser_push_expansion_macro (parser, macro, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700640}