blob: b2eaa5ba69637c22ec2bd1432588970423c6d8e1 [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
Carl Worth796e1f02010-05-17 12:45:16 -0700119/* Hard to remove shift/reduce conflicts documented as follows:
120 *
121 * 1. '(' after FUNC_MACRO name which is correctly resolved to shift
122 * to form macro invocation rather than reducing directly to
123 * content.
124 */
125%expect 1
126
Carl Worth3a37b872010-05-10 11:44:09 -0700127%%
128
Carl Worth33cc4002010-05-12 12:17:10 -0700129input:
130 /* empty */
Carl Worth8bcb6f12010-05-12 13:21:20 -0700131| input content
Carl Worth3a37b872010-05-10 11:44:09 -0700132;
133
Carl Worth04af1352010-05-14 10:17:38 -0700134 /* We do all printing at the content level */
Carl Worth33cc4002010-05-12 12:17:10 -0700135content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700136 IDENTIFIER {
137 printf ("%s", $1);
138 talloc_free ($1);
139 }
140| TOKEN {
141 printf ("%s", $1);
142 talloc_free ($1);
143 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700144| FUNC_MACRO {
145 printf ("%s", $1);
146 talloc_free ($1);
147 }
Carl Worth2be8be02010-05-14 10:31:43 -0700148| macro {
149 _print_string_list ($1);
150 }
Carl Worth04af1352010-05-14 10:17:38 -0700151| directive_with_newline { printf ("\n"); }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700152| '(' { printf ("("); }
153| ')' { printf (")"); }
154| ',' { printf (","); }
Carl Worthcd27e642010-05-12 13:11:50 -0700155;
156
Carl Worthfcbbb462010-05-13 09:36:23 -0700157macro:
158 FUNC_MACRO '(' argument_list ')' {
Carl Worth2be8be02010-05-14 10:31:43 -0700159 $$ = _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700160 }
161| OBJ_MACRO {
Carl Worth2be8be02010-05-14 10:31:43 -0700162 $$ = _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700163 talloc_free ($1);
164 }
165;
166
167argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700168 /* empty */ {
169 $$ = _argument_list_create (parser);
170 }
171| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700172 $$ = _argument_list_create (parser);
173 _argument_list_append ($$, $1);
174 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700175| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700176 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700177 $$ = $1;
178 }
179;
180
181argument:
Carl Worthac070e82010-05-14 11:33:00 -0700182 word {
Carl Worth610053b2010-05-14 10:05:11 -0700183 $$ = _string_list_create (parser);
Carl Worthac070e82010-05-14 11:33:00 -0700184 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700185 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700186| macro {
187 $$ = $1;
188 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700189| argument word {
Carl Worth610053b2010-05-14 10:05:11 -0700190 _string_list_append_item ($1, $2);
Carl Worthfcbbb462010-05-13 09:36:23 -0700191 talloc_free ($2);
Carl Worth3596bb12010-05-14 16:53:52 -0700192 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700193 }
Carl Worth3596bb12010-05-14 16:53:52 -0700194| argument '(' argument ')' {
195 _string_list_append_item ($1, "(");
196 _string_list_append_list ($1, $3);
197 _string_list_append_item ($1, ")");
198 $$ = $1;
199 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700200;
201
Carl Worthcd27e642010-05-12 13:11:50 -0700202directive_with_newline:
Carl Worth04af1352010-05-14 10:17:38 -0700203 directive NEWLINE
Carl Worth3a37b872010-05-10 11:44:09 -0700204;
205
Carl Worth33cc4002010-05-12 12:17:10 -0700206directive:
Carl Worth0a93cbb2010-05-13 10:29:07 -0700207 DEFINE IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700208 string_list_t *list = _string_list_create (parser);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700209 _define_object_macro (parser, $2, list);
210 }
211| DEFINE IDENTIFIER SPACE replacement_list {
212 _define_object_macro (parser, $2, $4);
Carl Worthcd27e642010-05-12 13:11:50 -0700213 }
Carl Worth48b94da2010-05-13 10:46:29 -0700214| DEFINE IDENTIFIER '(' parameter_list ')' {
Carl Worth610053b2010-05-14 10:05:11 -0700215 string_list_t *list = _string_list_create (parser);
Carl Worth48b94da2010-05-13 10:46:29 -0700216 _define_function_macro (parser, $2, $4, list);
217 }
Carl Worth81f01432010-05-14 17:08:45 -0700218| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list {
219 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700220 }
221| UNDEF FUNC_MACRO {
Carl Worth610053b2010-05-14 10:05:11 -0700222 string_list_t *replacement = hash_table_find (parser->defines, $2);
Carl Worthfcbbb462010-05-13 09:36:23 -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);
231 }
232| UNDEF OBJ_MACRO {
Carl Worth610053b2010-05-14 10:05:11 -0700233 string_list_t *replacement = hash_table_find (parser->defines, $2);
Carl Worthcd27e642010-05-12 13:11:50 -0700234 if (replacement) {
235 /* XXX: Need hash table to support a real way
236 * to remove an element rather than prefixing
237 * a new node with data of NULL like this. */
238 hash_table_insert (parser->defines, NULL, $2);
239 talloc_free (replacement);
240 }
241 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -0700242 }
243;
244
245replacement_list:
Carl Worth38bd27b2010-05-14 12:05:37 -0700246 replacement_word {
Carl Worth610053b2010-05-14 10:05:11 -0700247 $$ = _string_list_create (parser);
248 _string_list_append_item ($$, $1);
Carl Worth48b94da2010-05-13 10:46:29 -0700249 talloc_free ($1);
Carl Worth33cc4002010-05-12 12:17:10 -0700250 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700251| replacement_list replacement_word {
Carl Worth610053b2010-05-14 10:05:11 -0700252 _string_list_append_item ($1, $2);
Carl Worth5070a202010-05-12 12:45:33 -0700253 talloc_free ($2);
Carl Worth33cc4002010-05-12 12:17:10 -0700254 $$ = $1;
255 }
256;
257
Carl Worth38bd27b2010-05-14 12:05:37 -0700258replacement_word:
259 word { $$ = $1; }
260| FUNC_MACRO { $$ = $1; }
261| OBJ_MACRO { $$ = $1; }
262| '(' { $$ = xtalloc_strdup (parser, "("); }
263| ')' { $$ = xtalloc_strdup (parser, ")"); }
264| ',' { $$ = xtalloc_strdup (parser, ","); }
Carl Worth38bd27b2010-05-14 12:05:37 -0700265;
266
Carl Worthfcbbb462010-05-13 09:36:23 -0700267parameter_list:
268 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700269 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700270 }
Carl Worth7f9aa362010-05-13 12:58:49 -0700271| identifier_perhaps_macro {
Carl Worth610053b2010-05-14 10:05:11 -0700272 $$ = _string_list_create (parser);
273 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700274 talloc_free ($1);
275 }
Carl Worth7f9aa362010-05-13 12:58:49 -0700276| parameter_list ',' identifier_perhaps_macro {
Carl Worth610053b2010-05-14 10:05:11 -0700277 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700278 talloc_free ($3);
279 $$ = $1;
280 }
281;
282
Carl Worth7f9aa362010-05-13 12:58:49 -0700283identifier_perhaps_macro:
284 IDENTIFIER { $$ = $1; }
285| FUNC_MACRO { $$ = $1; }
286| OBJ_MACRO { $$ = $1; }
287;
288
Carl Worth0a93cbb2010-05-13 10:29:07 -0700289word:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700290 IDENTIFIER { $$ = $1; }
Carl Worth9f62a7e2010-05-13 07:38:29 -0700291| TOKEN { $$ = $1; }
Carl Worth33cc4002010-05-12 12:17:10 -0700292;
293
294%%
295
Carl Worth610053b2010-05-14 10:05:11 -0700296string_list_t *
297_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700298{
Carl Worth610053b2010-05-14 10:05:11 -0700299 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700300
Carl Worth610053b2010-05-14 10:05:11 -0700301 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700302 list->head = NULL;
303 list->tail = NULL;
304
305 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700306}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700307
Carl Worth33cc4002010-05-12 12:17:10 -0700308void
Carl Worth610053b2010-05-14 10:05:11 -0700309_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700310{
311 if (list->head == NULL) {
312 list->head = tail->head;
313 } else {
314 list->tail->next = tail->head;
315 }
316
317 list->tail = tail->tail;
318}
319
320void
Carl Worth610053b2010-05-14 10:05:11 -0700321_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700322{
Carl Worth610053b2010-05-14 10:05:11 -0700323 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700324
Carl Worth610053b2010-05-14 10:05:11 -0700325 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700326 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700327
328 node->next = NULL;
329
330 if (list->head == NULL) {
331 list->head = node;
332 } else {
333 list->tail->next = node;
334 }
335
336 list->tail = node;
337}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700338
Carl Worth420d05a2010-05-17 10:15:23 -0700339void
340_string_list_push (string_list_t *list, const char *str)
341{
342 string_node_t *node;
343
344 node = xtalloc (list, string_node_t);
345 node->str = xtalloc_strdup (node, str);
346
347 node->next = list->head;
348
349 if (list->tail == NULL) {
350 list->tail = node;
351 }
352
353 list->head = node;
354}
355
356void
357_string_list_pop (string_list_t *list)
358{
359 string_node_t *node;
360
361 node = list->head;
362
363 if (node == NULL) {
364 fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
365 exit (1);
366 }
367
368 list->head = node->next;
369
370 if (list->tail == node) {
371 assert (node->next == NULL);
372 list->tail = NULL;
373 }
374
375 talloc_free (node);
376}
377
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700378int
Carl Worth610053b2010-05-14 10:05:11 -0700379_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700380{
Carl Worth610053b2010-05-14 10:05:11 -0700381 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700382 int i;
383
384 if (list == NULL)
385 return 0;
386
387 for (i = 0, node = list->head; node; i++, node = node->next) {
388 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700389 if (index)
390 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700391 return 1;
392 }
393 }
394
395 return 0;
396}
397
398int
Carl Worth610053b2010-05-14 10:05:11 -0700399_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700400{
401 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700402 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700403
404 if (list == NULL)
405 return 0;
406
407 for (node = list->head; node; node = node->next)
408 length++;
409
410 return length;
411}
412
Carl Worth2be8be02010-05-14 10:31:43 -0700413void
414_print_string_list (string_list_t *list)
415{
416 string_node_t *node;
417
418 if (list == NULL)
419 return;
420
Carl Worth81f01432010-05-14 17:08:45 -0700421 for (node = list->head; node; node = node->next) {
Carl Worth2be8be02010-05-14 10:31:43 -0700422 printf ("%s", node->str);
Carl Worth81f01432010-05-14 17:08:45 -0700423 if (node->next)
424 printf (" ");
425 }
Carl Worth2be8be02010-05-14 10:31:43 -0700426}
427
Carl Worth8f6a8282010-05-14 10:44:19 -0700428argument_list_t *
429_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700430{
Carl Worth8f6a8282010-05-14 10:44:19 -0700431 argument_list_t *list;
432
433 list = xtalloc (ctx, argument_list_t);
434 list->head = NULL;
435 list->tail = NULL;
436
437 return list;
438}
439
440void
441_argument_list_append (argument_list_t *list, string_list_t *argument)
442{
443 argument_node_t *node;
444
445 if (argument == NULL || argument->head == NULL)
446 return;
447
448 node = xtalloc (list, argument_node_t);
449 node->argument = argument;
450
451 node->next = NULL;
452
453 if (list->head == NULL) {
454 list->head = node;
455 } else {
456 list->tail->next = node;
457 }
458
459 list->tail = node;
460}
461
462int
463_argument_list_length (argument_list_t *list)
464{
465 int length = 0;
466 argument_node_t *node;
467
468 if (list == NULL)
469 return 0;
470
471 for (node = list->head; node; node = node->next)
472 length++;
473
474 return length;
475}
476
477string_list_t *
478_argument_list_member_at (argument_list_t *list, int index)
479{
480 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700481 int i;
482
483 if (list == NULL)
484 return NULL;
485
486 node = list->head;
487 for (i = 0; i < index; i++) {
488 node = node->next;
489 if (node == NULL)
490 break;
491 }
492
493 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700494 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700495
496 return NULL;
497}
Carl Worth33cc4002010-05-12 12:17:10 -0700498
Carl Worth3a37b872010-05-10 11:44:09 -0700499void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700500yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700501{
502 fprintf (stderr, "Parse error: %s\n", error);
503}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700504
Carl Worth33cc4002010-05-12 12:17:10 -0700505glcpp_parser_t *
506glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700507{
Carl Worth33cc4002010-05-12 12:17:10 -0700508 glcpp_parser_t *parser;
509
Carl Worth5070a202010-05-12 12:45:33 -0700510 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700511
Carl Worth5070a202010-05-12 12:45:33 -0700512 yylex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700513 parser->defines = hash_table_ctor (32, hash_table_string_hash,
514 hash_table_string_compare);
Carl Worth33cc4002010-05-12 12:17:10 -0700515
516 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700517}
518
519int
520glcpp_parser_parse (glcpp_parser_t *parser)
521{
522 return yyparse (parser);
523}
524
525void
Carl Worth33cc4002010-05-12 12:17:10 -0700526glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700527{
528 yylex_destroy (parser->scanner);
529 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700530 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700531}
Carl Worthc6d5af32010-05-11 12:30:09 -0700532
Carl Worthfcbbb462010-05-13 09:36:23 -0700533macro_type_t
534glcpp_parser_macro_type (glcpp_parser_t *parser, const char *identifier)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700535{
Carl Worthfcbbb462010-05-13 09:36:23 -0700536 macro_t *macro;
537
538 macro = hash_table_find (parser->defines, identifier);
539
540 if (macro == NULL)
541 return MACRO_TYPE_UNDEFINED;
542
543 if (macro->is_function)
544 return MACRO_TYPE_FUNCTION;
545 else
546 return MACRO_TYPE_OBJECT;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700547}
548
Carl Worth33cc4002010-05-12 12:17:10 -0700549void
Carl Worthfcbbb462010-05-13 09:36:23 -0700550_define_object_macro (glcpp_parser_t *parser,
551 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700552 string_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700553{
554 macro_t *macro;
555
556 macro = xtalloc (parser, macro_t);
557
558 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700559 macro->parameters = NULL;
560 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700561
562 hash_table_insert (parser->defines, macro, identifier);
563}
564
565void
566_define_function_macro (glcpp_parser_t *parser,
567 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700568 string_list_t *parameters,
569 string_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700570{
571 macro_t *macro;
572
573 macro = xtalloc (parser, macro_t);
574
575 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700576 macro->parameters = talloc_steal (macro, parameters);
577 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700578
579 hash_table_insert (parser->defines, macro, identifier);
580}
581
Carl Worth2be8be02010-05-14 10:31:43 -0700582static string_list_t *
583_expand_macro_recursive (glcpp_parser_t *parser,
584 const char *token,
Carl Worth420d05a2010-05-17 10:15:23 -0700585 string_list_t *active,
Carl Worth2be8be02010-05-14 10:31:43 -0700586 string_list_t *parameters,
Carl Worth8f6a8282010-05-14 10:44:19 -0700587 argument_list_t *arguments);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700588
Carl Worth2be8be02010-05-14 10:31:43 -0700589static string_list_t *
590_expand_string_list_recursive (glcpp_parser_t *parser,
591 string_list_t *list,
Carl Worth420d05a2010-05-17 10:15:23 -0700592 string_list_t *active,
Carl Worth2be8be02010-05-14 10:31:43 -0700593 string_list_t *parameters,
Carl Worth8f6a8282010-05-14 10:44:19 -0700594 argument_list_t *arguments)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700595{
Carl Worth2be8be02010-05-14 10:31:43 -0700596 string_list_t *result;
597 string_list_t *child;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700598 const char *token;
Carl Worth610053b2010-05-14 10:05:11 -0700599 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700600 int index;
601
Carl Worth2be8be02010-05-14 10:31:43 -0700602 result = _string_list_create (parser);
603
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700604 for (node = list->head ; node ; node = node->next) {
605 token = node->str;
606
Carl Worth420d05a2010-05-17 10:15:23 -0700607 /* Don't expand this macro if it's on the active
608 * stack, (meaning we're already in the process of
609 * expanding it). */
610 if (_string_list_contains (active, token, NULL)) {
Carl Worth2be8be02010-05-14 10:31:43 -0700611 _string_list_append_item (result, token);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700612 continue;
613 }
614
Carl Worth610053b2010-05-14 10:05:11 -0700615 if (_string_list_contains (parameters, token, &index)) {
Carl Worth8f6a8282010-05-14 10:44:19 -0700616 string_list_t *argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700617
Carl Worth8f6a8282010-05-14 10:44:19 -0700618 argument = _argument_list_member_at (arguments, index);
619 child = _expand_string_list_recursive (parser, argument,
Carl Worth420d05a2010-05-17 10:15:23 -0700620 active, NULL, NULL);
Carl Worth2be8be02010-05-14 10:31:43 -0700621 _string_list_append_list (result, child);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700622 } else {
Carl Worth2be8be02010-05-14 10:31:43 -0700623 child = _expand_macro_recursive (parser, token,
Carl Worth420d05a2010-05-17 10:15:23 -0700624 active, parameters,
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700625 arguments);
Carl Worth2be8be02010-05-14 10:31:43 -0700626 _string_list_append_list (result, child);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700627 }
628 }
Carl Worth2be8be02010-05-14 10:31:43 -0700629
630 return result;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700631}
632
633
Carl Worth2be8be02010-05-14 10:31:43 -0700634static string_list_t *
635_expand_macro_recursive (glcpp_parser_t *parser,
636 const char *token,
Carl Worth420d05a2010-05-17 10:15:23 -0700637 string_list_t *active,
Carl Worth2be8be02010-05-14 10:31:43 -0700638 string_list_t *parameters,
Carl Worth8f6a8282010-05-14 10:44:19 -0700639 argument_list_t *arguments)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700640{
641 macro_t *macro;
Carl Worthc5e98552010-05-14 10:12:21 -0700642 string_list_t *replacements;
Carl Worth420d05a2010-05-17 10:15:23 -0700643 string_list_t *result;
644
645 if (active == NULL)
646 active = _string_list_create (NULL);
647
648 _string_list_push (active, token);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700649
650 macro = hash_table_find (parser->defines, token);
651 if (macro == NULL) {
Carl Worth2be8be02010-05-14 10:31:43 -0700652 string_list_t *result;
653
654 result = _string_list_create (parser);
655 _string_list_append_item (result, token);
656 return result;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700657 }
658
Carl Worthc5e98552010-05-14 10:12:21 -0700659 replacements = macro->replacements;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700660
Carl Worth420d05a2010-05-17 10:15:23 -0700661 result = _expand_string_list_recursive (parser, replacements,
662 active, parameters, arguments);
663
664 _string_list_pop (active);
665 if (_string_list_length (active) == 0)
666 talloc_free (active);
667
668 return result;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700669}
670
Carl Worth2be8be02010-05-14 10:31:43 -0700671string_list_t *
672_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700673{
Carl Worthfcbbb462010-05-13 09:36:23 -0700674 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700675
Carl Worthfcbbb462010-05-13 09:36:23 -0700676 macro = hash_table_find (parser->defines, identifier);
677 assert (! macro->is_function);
678
Carl Worth420d05a2010-05-17 10:15:23 -0700679 return _expand_macro_recursive (parser, identifier, NULL,
Carl Worth2be8be02010-05-14 10:31:43 -0700680 NULL, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700681}
682
Carl Worth2be8be02010-05-14 10:31:43 -0700683string_list_t *
684_expand_function_macro (glcpp_parser_t *parser,
685 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700686 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700687{
Carl Worthfcbbb462010-05-13 09:36:23 -0700688 macro_t *macro;
689
690 macro = hash_table_find (parser->defines, identifier);
691 assert (macro->is_function);
692
Carl Worth8f6a8282010-05-14 10:44:19 -0700693 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700694 _string_list_length (macro->parameters))
695 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700696 fprintf (stderr,
697 "Error: macro %s invoked with %d arguments (expected %d)\n",
698 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700699 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700700 _string_list_length (macro->parameters));
Carl Worth2be8be02010-05-14 10:31:43 -0700701 return NULL;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700702 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700703
Carl Worth420d05a2010-05-17 10:15:23 -0700704 return _expand_macro_recursive (parser, identifier, NULL,
Carl Worth2be8be02010-05-14 10:31:43 -0700705 macro->parameters, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700706}