blob: 6ef1cae0eca068fb8f1b28634dc4b9f98106e7bc [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 Worth8f38aff2010-05-19 10:01:29 -070085static int
86yylex (yyscan_t scanner);
87
Carl Worth3a37b872010-05-10 11:44:09 -070088%}
89
Carl Worth33cc4002010-05-12 12:17:10 -070090%union {
91 char *str;
Carl Worth8f6a8282010-05-14 10:44:19 -070092 string_list_t *string_list;
93 argument_list_t *argument_list;
Carl Worth33cc4002010-05-12 12:17:10 -070094}
95
Carl Worth0b27b5f2010-05-10 16:16:06 -070096%parse-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -070097%lex-param {void *scanner}
98
Carl Wortha807fb72010-05-18 22:10:04 -070099%token DEFINE FUNC_MACRO IDENTIFIER NEWLINE OBJ_MACRO REPLACEMENT TOKEN UNDEF
Carl Worth59ca9892010-05-19 07:49:47 -0700100%type <str> argument_word FUNC_MACRO IDENTIFIER OBJ_MACRO REPLACEMENT TOKEN
Carl Wortha807fb72010-05-18 22:10:04 -0700101%type <string_list> argument macro parameter_list
Carl Worth8f6a8282010-05-14 10:44:19 -0700102%type <argument_list> argument_list
Carl Worth3a37b872010-05-10 11:44:09 -0700103
Carl Worth796e1f02010-05-17 12:45:16 -0700104/* Hard to remove shift/reduce conflicts documented as follows:
105 *
106 * 1. '(' after FUNC_MACRO name which is correctly resolved to shift
107 * to form macro invocation rather than reducing directly to
108 * content.
Carl Worth69f390d2010-05-19 07:42:42 -0700109 *
110 * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
111 * shift to form macro invocation rather than reducing directly to
112 * argument.
Carl Worth796e1f02010-05-17 12:45:16 -0700113 */
Carl Worth69f390d2010-05-19 07:42:42 -0700114%expect 2
Carl Worth796e1f02010-05-17 12:45:16 -0700115
Carl Worth3a37b872010-05-10 11:44:09 -0700116%%
117
Carl Worth33cc4002010-05-12 12:17:10 -0700118input:
119 /* empty */
Carl Worth8bcb6f12010-05-12 13:21:20 -0700120| input content
Carl Worth3a37b872010-05-10 11:44:09 -0700121;
122
Carl Worth04af1352010-05-14 10:17:38 -0700123 /* We do all printing at the content level */
Carl Worth33cc4002010-05-12 12:17:10 -0700124content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700125 IDENTIFIER {
126 printf ("%s", $1);
127 talloc_free ($1);
128 }
129| TOKEN {
130 printf ("%s", $1);
131 talloc_free ($1);
132 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700133| FUNC_MACRO {
134 printf ("%s", $1);
135 talloc_free ($1);
136 }
Carl Wortha807fb72010-05-18 22:10:04 -0700137| directive {
138 printf ("\n");
Carl Worth2be8be02010-05-14 10:31:43 -0700139 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700140| '(' { printf ("("); }
141| ')' { printf (")"); }
142| ',' { printf (","); }
Carl Wortha807fb72010-05-18 22:10:04 -0700143| macro
Carl Worthcd27e642010-05-12 13:11:50 -0700144;
145
Carl Worthfcbbb462010-05-13 09:36:23 -0700146macro:
147 FUNC_MACRO '(' argument_list ')' {
Carl Wortha807fb72010-05-18 22:10:04 -0700148 _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700149 }
150| OBJ_MACRO {
Carl Wortha807fb72010-05-18 22:10:04 -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 Worth59ca9892010-05-19 07:49:47 -0700171 argument_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 Worth59ca9892010-05-19 07:49:47 -0700175| argument argument_word {
Carl Worth610053b2010-05-14 10:05:11 -0700176 _string_list_append_item ($1, $2);
Carl Worthfcbbb462010-05-13 09:36:23 -0700177 talloc_free ($2);
Carl Worth3596bb12010-05-14 16:53:52 -0700178 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700179 }
Carl Worth3596bb12010-05-14 16:53:52 -0700180| argument '(' argument ')' {
181 _string_list_append_item ($1, "(");
182 _string_list_append_list ($1, $3);
183 _string_list_append_item ($1, ")");
184 $$ = $1;
185 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700186;
187
Carl Worth59ca9892010-05-19 07:49:47 -0700188argument_word:
189 IDENTIFIER { $$ = $1; }
190| TOKEN { $$ = $1; }
191| FUNC_MACRO { $$ = $1; }
Carl Worth5d211422010-05-19 07:57:03 -0700192| macro { $$ = xtalloc_strdup (parser, ""); }
Carl Worth59ca9892010-05-19 07:49:47 -0700193;
194
195
Carl Worth33cc4002010-05-12 12:17:10 -0700196directive:
Carl Wortha807fb72010-05-18 22:10:04 -0700197 DEFINE IDENTIFIER REPLACEMENT {
198 _define_object_macro (parser, $2, $3);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700199 }
Carl Wortha807fb72010-05-18 22:10:04 -0700200| DEFINE IDENTIFIER '(' parameter_list ')' REPLACEMENT {
Carl Worth81f01432010-05-14 17:08:45 -0700201 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700202 }
Carl Wortha807fb72010-05-18 22:10:04 -0700203| UNDEF IDENTIFIER {
204 string_list_t *macro = hash_table_find (parser->defines, $2);
205 if (macro) {
Carl Worthfcbbb462010-05-13 09:36:23 -0700206 /* XXX: Need hash table to support a real way
207 * to remove an element rather than prefixing
208 * a new node with data of NULL like this. */
209 hash_table_insert (parser->defines, NULL, $2);
Carl Wortha807fb72010-05-18 22:10:04 -0700210 talloc_free (macro);
Carl Worthfcbbb462010-05-13 09:36:23 -0700211 }
212 talloc_free ($2);
213 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700214;
215
Carl Worthfcbbb462010-05-13 09:36:23 -0700216parameter_list:
217 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700218 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700219 }
Carl Wortha807fb72010-05-18 22:10:04 -0700220| IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700221 $$ = _string_list_create (parser);
222 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700223 talloc_free ($1);
224 }
Carl Wortha807fb72010-05-18 22:10:04 -0700225| parameter_list ',' IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700226 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700227 talloc_free ($3);
228 $$ = $1;
229 }
230;
231
Carl Worth33cc4002010-05-12 12:17:10 -0700232%%
233
Carl Worth610053b2010-05-14 10:05:11 -0700234string_list_t *
235_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700236{
Carl Worth610053b2010-05-14 10:05:11 -0700237 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700238
Carl Worth610053b2010-05-14 10:05:11 -0700239 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700240 list->head = NULL;
241 list->tail = NULL;
242
243 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700244}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700245
Carl Worth33cc4002010-05-12 12:17:10 -0700246void
Carl Worth610053b2010-05-14 10:05:11 -0700247_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700248{
249 if (list->head == NULL) {
250 list->head = tail->head;
251 } else {
252 list->tail->next = tail->head;
253 }
254
255 list->tail = tail->tail;
256}
257
258void
Carl Worth610053b2010-05-14 10:05:11 -0700259_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700260{
Carl Worth610053b2010-05-14 10:05:11 -0700261 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700262
Carl Worth610053b2010-05-14 10:05:11 -0700263 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700264 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700265
266 node->next = NULL;
267
268 if (list->head == NULL) {
269 list->head = node;
270 } else {
271 list->tail->next = node;
272 }
273
274 list->tail = node;
275}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700276
277int
Carl Worth610053b2010-05-14 10:05:11 -0700278_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700279{
Carl Worth610053b2010-05-14 10:05:11 -0700280 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700281 int i;
282
283 if (list == NULL)
284 return 0;
285
286 for (i = 0, node = list->head; node; i++, node = node->next) {
287 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700288 if (index)
289 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700290 return 1;
291 }
292 }
293
294 return 0;
295}
296
297int
Carl Worth610053b2010-05-14 10:05:11 -0700298_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700299{
300 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700301 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700302
303 if (list == NULL)
304 return 0;
305
306 for (node = list->head; node; node = node->next)
307 length++;
308
309 return length;
310}
311
Carl Worth2be8be02010-05-14 10:31:43 -0700312void
313_print_string_list (string_list_t *list)
314{
315 string_node_t *node;
316
317 if (list == NULL)
318 return;
319
Carl Worth81f01432010-05-14 17:08:45 -0700320 for (node = list->head; node; node = node->next) {
Carl Worth2be8be02010-05-14 10:31:43 -0700321 printf ("%s", node->str);
Carl Worth81f01432010-05-14 17:08:45 -0700322 if (node->next)
323 printf (" ");
324 }
Carl Worth2be8be02010-05-14 10:31:43 -0700325}
326
Carl Worth8f6a8282010-05-14 10:44:19 -0700327argument_list_t *
328_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700329{
Carl Worth8f6a8282010-05-14 10:44:19 -0700330 argument_list_t *list;
331
332 list = xtalloc (ctx, argument_list_t);
333 list->head = NULL;
334 list->tail = NULL;
335
336 return list;
337}
338
339void
340_argument_list_append (argument_list_t *list, string_list_t *argument)
341{
342 argument_node_t *node;
343
344 if (argument == NULL || argument->head == NULL)
345 return;
346
347 node = xtalloc (list, argument_node_t);
348 node->argument = argument;
349
350 node->next = NULL;
351
352 if (list->head == NULL) {
353 list->head = node;
354 } else {
355 list->tail->next = node;
356 }
357
358 list->tail = node;
359}
360
361int
362_argument_list_length (argument_list_t *list)
363{
364 int length = 0;
365 argument_node_t *node;
366
367 if (list == NULL)
368 return 0;
369
370 for (node = list->head; node; node = node->next)
371 length++;
372
373 return length;
374}
375
376string_list_t *
377_argument_list_member_at (argument_list_t *list, int index)
378{
379 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700380 int i;
381
382 if (list == NULL)
383 return NULL;
384
385 node = list->head;
386 for (i = 0; i < index; i++) {
387 node = node->next;
388 if (node == NULL)
389 break;
390 }
391
392 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700393 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700394
395 return NULL;
396}
Carl Worth33cc4002010-05-12 12:17:10 -0700397
Carl Worth3a37b872010-05-10 11:44:09 -0700398void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700399yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700400{
401 fprintf (stderr, "Parse error: %s\n", error);
402}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700403
Carl Worth33cc4002010-05-12 12:17:10 -0700404glcpp_parser_t *
405glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700406{
Carl Worth33cc4002010-05-12 12:17:10 -0700407 glcpp_parser_t *parser;
408
Carl Worth5070a202010-05-12 12:45:33 -0700409 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700410
Carl Worth8f38aff2010-05-19 10:01:29 -0700411 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700412 parser->defines = hash_table_ctor (32, hash_table_string_hash,
413 hash_table_string_compare);
Carl Wortha807fb72010-05-18 22:10:04 -0700414 parser->expansions = NULL;
415
416 parser->lex_stack = xtalloc (parser, glcpp_lex_stack_t);
417 parser->lex_stack->parser = parser;
418 parser->lex_stack->head = NULL;
Carl Worth33cc4002010-05-12 12:17:10 -0700419
420 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700421}
422
423int
424glcpp_parser_parse (glcpp_parser_t *parser)
425{
426 return yyparse (parser);
427}
428
429void
Carl Worth33cc4002010-05-12 12:17:10 -0700430glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700431{
Carl Worth8f38aff2010-05-19 10:01:29 -0700432 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700433 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700434 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700435}
Carl Worthc6d5af32010-05-11 12:30:09 -0700436
Carl Worthbe0e2e92010-05-19 07:29:22 -0700437static int
438glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
439{
440 expansion_node_t *node;
441
442 for (node = parser->expansions; node; node = node->next) {
443 if (node->macro &&
444 strcmp (node->macro->identifier, member) == 0)
445 {
446 return 1;
447 }
448 }
449
450 return 0;
451}
452
Carl Wortha807fb72010-05-18 22:10:04 -0700453token_class_t
454glcpp_parser_classify_token (glcpp_parser_t *parser,
455 const char *identifier,
456 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700457{
Carl Worthfcbbb462010-05-13 09:36:23 -0700458 macro_t *macro;
459
Carl Wortha807fb72010-05-18 22:10:04 -0700460 /* First we check if we are currently expanding a
461 * function-like macro, and if so, whether the parameter list
462 * contains a parameter matching this token name. */
463 if (parser->expansions &&
464 parser->expansions->macro &&
465 parser->expansions->macro->parameters)
466 {
467 string_list_t *list;
468
469 list = parser->expansions->macro->parameters;
470
471 if (_string_list_contains (list, identifier, parameter_index))
472 return TOKEN_CLASS_ARGUMENT;
473 }
474
475 /* If not a function-like macro parameter, we next check if
476 * this token is a macro itself. */
477
Carl Worthfcbbb462010-05-13 09:36:23 -0700478 macro = hash_table_find (parser->defines, identifier);
479
480 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700481 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700482
Carl Worthbe0e2e92010-05-19 07:29:22 -0700483 /* Don't consider this a macro if we are already actively
484 * expanding this macro. */
485 if (glcpp_parser_is_expanding (parser, identifier))
486 return TOKEN_CLASS_IDENTIFIER;
487
488 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700489 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700490 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700491 else
Carl Wortha807fb72010-05-18 22:10:04 -0700492 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700493}
494
Carl Worth33cc4002010-05-12 12:17:10 -0700495void
Carl Worthfcbbb462010-05-13 09:36:23 -0700496_define_object_macro (glcpp_parser_t *parser,
497 const char *identifier,
Carl Wortha807fb72010-05-18 22:10:04 -0700498 const char *replacement)
Carl Worthfcbbb462010-05-13 09:36:23 -0700499{
500 macro_t *macro;
501
502 macro = xtalloc (parser, macro_t);
503
504 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700505 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700506 macro->identifier = talloc_strdup (macro, identifier);
507 macro->replacement = talloc_steal (macro, replacement);
Carl Worthfcbbb462010-05-13 09:36:23 -0700508
509 hash_table_insert (parser->defines, macro, identifier);
510}
511
512void
513_define_function_macro (glcpp_parser_t *parser,
514 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700515 string_list_t *parameters,
Carl Wortha807fb72010-05-18 22:10:04 -0700516 const char *replacement)
Carl Worthfcbbb462010-05-13 09:36:23 -0700517{
518 macro_t *macro;
519
520 macro = xtalloc (parser, macro_t);
521
522 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700523 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700524 macro->identifier = talloc_strdup (macro, identifier);
525 macro->replacement = talloc_steal (macro, replacement);
Carl Worthfcbbb462010-05-13 09:36:23 -0700526
527 hash_table_insert (parser->defines, macro, identifier);
528}
529
Carl Wortha807fb72010-05-18 22:10:04 -0700530static void
531_glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
532 macro_t *macro,
533 argument_list_t *arguments,
534 const char * replacement)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700535{
Carl Wortha807fb72010-05-18 22:10:04 -0700536 expansion_node_t *node;
537
538 node = xtalloc (parser, expansion_node_t);
539
540 node->macro = macro;
541 node->arguments = arguments;
542
543 node->next = parser->expansions;
544 parser->expansions = node;
545
546 glcpp_lex_stack_push (parser->lex_stack, replacement);
547}
548
549void
550glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
551 macro_t *macro,
552 argument_list_t *arguments)
553{
554 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
555 macro->replacement);
556}
557
558void
559glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
560 int argument_index)
561{
562 argument_list_t *arguments;
563 string_list_t *argument;
Carl Worth610053b2010-05-14 10:05:11 -0700564 string_node_t *node;
Carl Wortha807fb72010-05-18 22:10:04 -0700565 char *argument_str, *s;
566 int length;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700567
Carl Wortha807fb72010-05-18 22:10:04 -0700568 arguments = parser->expansions->arguments;
Carl Worth2be8be02010-05-14 10:31:43 -0700569
Carl Wortha807fb72010-05-18 22:10:04 -0700570 argument = _argument_list_member_at (arguments, argument_index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700571
Carl Wortha807fb72010-05-18 22:10:04 -0700572 length = 0;
573 for (node = argument->head; node; node = node->next)
574 length += strlen (node->str) + 1;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700575
Carl Wortha807fb72010-05-18 22:10:04 -0700576 argument_str = xtalloc_size (parser, length);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700577
Carl Wortha807fb72010-05-18 22:10:04 -0700578 *argument_str = '\0';
579 s = argument_str;
580 for (node = argument->head; node; node = node->next) {
581 strcpy (s, node->str);
582 s += strlen (node->str);
583 if (node->next) {
584 *s = ' ';
585 s++;
586 *s = '\0';
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700587 }
588 }
Carl Worth2be8be02010-05-14 10:31:43 -0700589
Carl Wortha807fb72010-05-18 22:10:04 -0700590 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
591 argument_str);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700592}
593
Carl Wortha807fb72010-05-18 22:10:04 -0700594/* The lexer calls this when it exhausts a string. */
595void
596glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700597{
Carl Wortha807fb72010-05-18 22:10:04 -0700598 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700599
Carl Wortha807fb72010-05-18 22:10:04 -0700600 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700601
Carl Wortha807fb72010-05-18 22:10:04 -0700602 if (node == NULL) {
603 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
604 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700605 }
606
Carl Wortha807fb72010-05-18 22:10:04 -0700607 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700608
Carl Wortha807fb72010-05-18 22:10:04 -0700609 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700610}
611
Carl Wortha807fb72010-05-18 22:10:04 -0700612void
Carl Worth2be8be02010-05-14 10:31:43 -0700613_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700614{
Carl Worthfcbbb462010-05-13 09:36:23 -0700615 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700616
Carl Worthfcbbb462010-05-13 09:36:23 -0700617 macro = hash_table_find (parser->defines, identifier);
618 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700619 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700620
Carl Worthbe0e2e92010-05-19 07:29:22 -0700621 glcpp_parser_push_expansion_macro (parser, macro, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700622}
623
Carl Wortha807fb72010-05-18 22:10:04 -0700624void
Carl Worth2be8be02010-05-14 10:31:43 -0700625_expand_function_macro (glcpp_parser_t *parser,
626 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700627 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700628{
Carl Worthfcbbb462010-05-13 09:36:23 -0700629 macro_t *macro;
630
631 macro = hash_table_find (parser->defines, identifier);
632 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700633 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700634
Carl Worth8f6a8282010-05-14 10:44:19 -0700635 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700636 _string_list_length (macro->parameters))
637 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700638 fprintf (stderr,
639 "Error: macro %s invoked with %d arguments (expected %d)\n",
640 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700641 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700642 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700643 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700644 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700645
Carl Worthbe0e2e92010-05-19 07:29:22 -0700646 glcpp_parser_push_expansion_macro (parser, macro, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700647}
Carl Worth8f38aff2010-05-19 10:01:29 -0700648
649static int
650yylex (yyscan_t scanner)
651{
652 return glcpp_lex (scanner);
653}