blob: c8d1919d9c58cb44000dfd21ae783982f42812c3 [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 Worth3a37b872010-05-10 11:44:09 -070031void
Carl Wortha1e32bc2010-05-10 13:17:25 -070032yyerror (void *scanner, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070033
Carl Worth33cc4002010-05-12 12:17:10 -070034void
Carl Worthfcbbb462010-05-13 09:36:23 -070035_define_object_macro (glcpp_parser_t *parser,
36 const char *macro,
Carl Worth47252442010-05-19 13:54:37 -070037 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070038
39void
40_define_function_macro (glcpp_parser_t *parser,
41 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070042 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070043 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070044
Carl Wortha807fb72010-05-18 22:10:04 -070045void
Carl Worth2be8be02010-05-14 10:31:43 -070046_expand_object_macro (glcpp_parser_t *parser, const char *identifier);
47
Carl Wortha807fb72010-05-18 22:10:04 -070048void
Carl Worth2be8be02010-05-14 10:31:43 -070049_expand_function_macro (glcpp_parser_t *parser,
50 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -070051 argument_list_t *arguments);
Carl Worthfcbbb462010-05-13 09:36:23 -070052
53void
Carl Worth2be8be02010-05-14 10:31:43 -070054_print_string_list (string_list_t *list);
Carl Worth33cc4002010-05-12 12:17:10 -070055
Carl Worth610053b2010-05-14 10:05:11 -070056string_list_t *
57_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070058
59void
Carl Worth610053b2010-05-14 10:05:11 -070060_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070061
62void
Carl Worth610053b2010-05-14 10:05:11 -070063_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070064
Carl Worthdcc2ecd2010-05-13 12:56:42 -070065int
Carl Worth610053b2010-05-14 10:05:11 -070066_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070067
Carl Worthdcc2ecd2010-05-13 12:56:42 -070068int
Carl Worth610053b2010-05-14 10:05:11 -070069_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070070
Carl Worth8f6a8282010-05-14 10:44:19 -070071argument_list_t *
72_argument_list_create (void *ctx);
73
74void
Carl Worth47252442010-05-19 13:54:37 -070075_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070076
77int
78_argument_list_length (argument_list_t *list);
79
Carl Worth47252442010-05-19 13:54:37 -070080token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070081_argument_list_member_at (argument_list_t *list, int index);
82
Carl Worth47252442010-05-19 13:54:37 -070083token_list_t *
84_token_list_create (void *ctx);
85
86void
87_token_list_append (token_list_t *list, int type, const char *value);
88
89void
90_token_list_append_list (token_list_t *list, token_list_t *tail);
91
Carl Worthaaa9acb2010-05-19 13:28:24 -070092static void
93glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
94 macro_t *macro,
95 argument_list_t *arguments);
96
97static void
98glcpp_parser_pop_expansion (glcpp_parser_t *parser);
99
Carl Worth0293b2e2010-05-19 10:05:40 -0700100#define yylex glcpp_parser_lex
101
Carl Worth8f38aff2010-05-19 10:01:29 -0700102static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700103glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700104
Carl Worth3a37b872010-05-10 11:44:09 -0700105%}
106
Carl Worth33cc4002010-05-12 12:17:10 -0700107%union {
108 char *str;
Carl Worth8f6a8282010-05-14 10:44:19 -0700109 argument_list_t *argument_list;
Carl Worth47252442010-05-19 13:54:37 -0700110 string_list_t *string_list;
111 token_list_t *token_list;
Carl Worth33cc4002010-05-12 12:17:10 -0700112}
113
Carl Worth0b27b5f2010-05-10 16:16:06 -0700114%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700115%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700116
Carl Worthaaa9acb2010-05-19 13:28:24 -0700117%token DEFINE FUNC_MACRO IDENTIFIER OBJ_MACRO NEWLINE SPACE TOKEN UNDEF
118%type <str> argument_word FUNC_MACRO IDENTIFIER OBJ_MACRO TOKEN
Carl Worth8f6a8282010-05-14 10:44:19 -0700119%type <argument_list> argument_list
Carl Worth47252442010-05-19 13:54:37 -0700120%type <string_list> macro parameter_list
121%type <token_list> argument replacement_list pp_tokens
Carl Worth3a37b872010-05-10 11:44:09 -0700122
Carl Worth796e1f02010-05-17 12:45:16 -0700123/* Hard to remove shift/reduce conflicts documented as follows:
124 *
125 * 1. '(' after FUNC_MACRO name which is correctly resolved to shift
126 * to form macro invocation rather than reducing directly to
127 * content.
Carl Worth69f390d2010-05-19 07:42:42 -0700128 *
129 * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
130 * shift to form macro invocation rather than reducing directly to
131 * argument.
Carl Worth796e1f02010-05-17 12:45:16 -0700132 */
Carl Worth69f390d2010-05-19 07:42:42 -0700133%expect 2
Carl Worth796e1f02010-05-17 12:45:16 -0700134
Carl Worth3a37b872010-05-10 11:44:09 -0700135%%
136
Carl Worth33cc4002010-05-12 12:17:10 -0700137input:
138 /* empty */
Carl Worth8bcb6f12010-05-12 13:21:20 -0700139| input content
Carl Worth3a37b872010-05-10 11:44:09 -0700140;
141
Carl Worth04af1352010-05-14 10:17:38 -0700142 /* We do all printing at the content level */
Carl Worth33cc4002010-05-12 12:17:10 -0700143content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700144 IDENTIFIER {
145 printf ("%s", $1);
146 talloc_free ($1);
147 }
148| TOKEN {
149 printf ("%s", $1);
150 talloc_free ($1);
151 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700152| FUNC_MACRO {
153 printf ("%s", $1);
154 talloc_free ($1);
155 }
Carl Wortha807fb72010-05-18 22:10:04 -0700156| directive {
157 printf ("\n");
Carl Worth2be8be02010-05-14 10:31:43 -0700158 }
Carl Worth0a93cbb2010-05-13 10:29:07 -0700159| '(' { printf ("("); }
160| ')' { printf (")"); }
161| ',' { printf (","); }
Carl Wortha807fb72010-05-18 22:10:04 -0700162| macro
Carl Worthcd27e642010-05-12 13:11:50 -0700163;
164
Carl Worthfcbbb462010-05-13 09:36:23 -0700165macro:
166 FUNC_MACRO '(' argument_list ')' {
Carl Wortha807fb72010-05-18 22:10:04 -0700167 _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700168 }
169| OBJ_MACRO {
Carl Wortha807fb72010-05-18 22:10:04 -0700170 _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700171 talloc_free ($1);
172 }
173;
174
175argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700176 /* empty */ {
177 $$ = _argument_list_create (parser);
178 }
179| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700180 $$ = _argument_list_create (parser);
181 _argument_list_append ($$, $1);
182 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700183| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700184 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700185 $$ = $1;
186 }
187;
188
189argument:
Carl Worth59ca9892010-05-19 07:49:47 -0700190 argument_word {
Carl Worth47252442010-05-19 13:54:37 -0700191 $$ = _token_list_create (parser);
192 _token_list_append ($$, IDENTIFIER, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700193 }
Carl Worth59ca9892010-05-19 07:49:47 -0700194| argument argument_word {
Carl Worth47252442010-05-19 13:54:37 -0700195 _token_list_append ($1, IDENTIFIER, $2);
Carl Worthfcbbb462010-05-13 09:36:23 -0700196 talloc_free ($2);
Carl Worth3596bb12010-05-14 16:53:52 -0700197 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700198 }
Carl Worth3596bb12010-05-14 16:53:52 -0700199| argument '(' argument ')' {
Carl Worth47252442010-05-19 13:54:37 -0700200 _token_list_append ($1, '(', "(");
201 _token_list_append_list ($1, $3);
202 _token_list_append ($1, ')', ")");
Carl Worth3596bb12010-05-14 16:53:52 -0700203 $$ = $1;
204 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700205;
206
Carl Worth59ca9892010-05-19 07:49:47 -0700207argument_word:
208 IDENTIFIER { $$ = $1; }
209| TOKEN { $$ = $1; }
210| FUNC_MACRO { $$ = $1; }
Carl Worth5d211422010-05-19 07:57:03 -0700211| macro { $$ = xtalloc_strdup (parser, ""); }
Carl Worth59ca9892010-05-19 07:49:47 -0700212;
213
214
Carl Worth33cc4002010-05-12 12:17:10 -0700215directive:
Carl Worthaaa9acb2010-05-19 13:28:24 -0700216 DEFINE IDENTIFIER NEWLINE {
Carl Worth47252442010-05-19 13:54:37 -0700217 token_list_t *list = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700218 _define_object_macro (parser, $2, list);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700219 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700220| DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
221 _define_object_macro (parser, $2, $4);
222 }
223| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
Carl Worth81f01432010-05-14 17:08:45 -0700224 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700225 }
Carl Wortha807fb72010-05-18 22:10:04 -0700226| UNDEF IDENTIFIER {
227 string_list_t *macro = hash_table_find (parser->defines, $2);
228 if (macro) {
Carl Worthfcbbb462010-05-13 09:36:23 -0700229 /* XXX: Need hash table to support a real way
230 * to remove an element rather than prefixing
231 * a new node with data of NULL like this. */
232 hash_table_insert (parser->defines, NULL, $2);
Carl Wortha807fb72010-05-18 22:10:04 -0700233 talloc_free (macro);
Carl Worthfcbbb462010-05-13 09:36:23 -0700234 }
235 talloc_free ($2);
236 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700237;
238
Carl Worthfcbbb462010-05-13 09:36:23 -0700239parameter_list:
240 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700241 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700242 }
Carl Wortha807fb72010-05-18 22:10:04 -0700243| IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700244 $$ = _string_list_create (parser);
245 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700246 talloc_free ($1);
247 }
Carl Wortha807fb72010-05-18 22:10:04 -0700248| parameter_list ',' IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700249 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700250 talloc_free ($3);
251 $$ = $1;
252 }
253;
254
Carl Worthaaa9acb2010-05-19 13:28:24 -0700255replacement_list:
256 /* empty */ {
Carl Worth47252442010-05-19 13:54:37 -0700257 $$ = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700258 }
259| pp_tokens {
260 $$ = $1;
261 }
262;
263
264
265pp_tokens:
266 TOKEN {
Carl Worth47252442010-05-19 13:54:37 -0700267 $$ = _token_list_create (parser);
268 _token_list_append ($$, TOKEN, $1);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700269 }
270| pp_tokens TOKEN {
Carl Worth47252442010-05-19 13:54:37 -0700271 _token_list_append ($1, TOKEN, $2);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700272 $$ = $1;
273 }
274;
275
Carl Worth33cc4002010-05-12 12:17:10 -0700276%%
277
Carl Worth610053b2010-05-14 10:05:11 -0700278string_list_t *
279_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700280{
Carl Worth610053b2010-05-14 10:05:11 -0700281 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700282
Carl Worth610053b2010-05-14 10:05:11 -0700283 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700284 list->head = NULL;
285 list->tail = NULL;
286
287 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700288}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700289
Carl Worth33cc4002010-05-12 12:17:10 -0700290void
Carl Worth610053b2010-05-14 10:05:11 -0700291_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700292{
293 if (list->head == NULL) {
294 list->head = tail->head;
295 } else {
296 list->tail->next = tail->head;
297 }
298
299 list->tail = tail->tail;
300}
301
302void
Carl Worth610053b2010-05-14 10:05:11 -0700303_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700304{
Carl Worth610053b2010-05-14 10:05:11 -0700305 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700306
Carl Worth610053b2010-05-14 10:05:11 -0700307 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700308 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700309
310 node->next = NULL;
311
312 if (list->head == NULL) {
313 list->head = node;
314 } else {
315 list->tail->next = node;
316 }
317
318 list->tail = node;
319}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700320
321int
Carl Worth610053b2010-05-14 10:05:11 -0700322_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700323{
Carl Worth610053b2010-05-14 10:05:11 -0700324 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700325 int i;
326
327 if (list == NULL)
328 return 0;
329
330 for (i = 0, node = list->head; node; i++, node = node->next) {
331 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700332 if (index)
333 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700334 return 1;
335 }
336 }
337
338 return 0;
339}
340
341int
Carl Worth610053b2010-05-14 10:05:11 -0700342_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700343{
344 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700345 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700346
347 if (list == NULL)
348 return 0;
349
350 for (node = list->head; node; node = node->next)
351 length++;
352
353 return length;
354}
355
Carl Worth2be8be02010-05-14 10:31:43 -0700356void
357_print_string_list (string_list_t *list)
358{
359 string_node_t *node;
360
361 if (list == NULL)
362 return;
363
Carl Worth81f01432010-05-14 17:08:45 -0700364 for (node = list->head; node; node = node->next) {
Carl Worth2be8be02010-05-14 10:31:43 -0700365 printf ("%s", node->str);
Carl Worth81f01432010-05-14 17:08:45 -0700366 if (node->next)
367 printf (" ");
368 }
Carl Worth2be8be02010-05-14 10:31:43 -0700369}
370
Carl Worth8f6a8282010-05-14 10:44:19 -0700371argument_list_t *
372_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700373{
Carl Worth8f6a8282010-05-14 10:44:19 -0700374 argument_list_t *list;
375
376 list = xtalloc (ctx, argument_list_t);
377 list->head = NULL;
378 list->tail = NULL;
379
380 return list;
381}
382
383void
Carl Worth47252442010-05-19 13:54:37 -0700384_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700385{
386 argument_node_t *node;
387
388 if (argument == NULL || argument->head == NULL)
389 return;
390
391 node = xtalloc (list, argument_node_t);
392 node->argument = argument;
393
394 node->next = NULL;
395
396 if (list->head == NULL) {
397 list->head = node;
398 } else {
399 list->tail->next = node;
400 }
401
402 list->tail = node;
403}
404
405int
406_argument_list_length (argument_list_t *list)
407{
408 int length = 0;
409 argument_node_t *node;
410
411 if (list == NULL)
412 return 0;
413
414 for (node = list->head; node; node = node->next)
415 length++;
416
417 return length;
418}
419
Carl Worth47252442010-05-19 13:54:37 -0700420token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700421_argument_list_member_at (argument_list_t *list, int index)
422{
423 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700424 int i;
425
426 if (list == NULL)
427 return NULL;
428
429 node = list->head;
430 for (i = 0; i < index; i++) {
431 node = node->next;
432 if (node == NULL)
433 break;
434 }
435
436 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700437 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700438
439 return NULL;
440}
Carl Worth47252442010-05-19 13:54:37 -0700441
442token_list_t *
443_token_list_create (void *ctx)
444{
445 token_list_t *list;
446
447 list = xtalloc (ctx, token_list_t);
448 list->head = NULL;
449 list->tail = NULL;
450
451 return list;
452}
453
454void
455_token_list_append (token_list_t *list, int type, const char *value)
456{
457 token_node_t *node;
458
459 node = xtalloc (list, token_node_t);
460 node->type = type;
461 node->value = xtalloc_strdup (list, value);
462
463 node->next = NULL;
464
465 if (list->head == NULL) {
466 list->head = node;
467 } else {
468 list->tail->next = node;
469 }
470
471 list->tail = node;
472}
473
474void
475_token_list_append_list (token_list_t *list, token_list_t *tail)
476{
477 if (list->head == NULL) {
478 list->head = tail->head;
479 } else {
480 list->tail->next = tail->head;
481 }
482
483 list->tail = tail->tail;
484}
Carl Worth33cc4002010-05-12 12:17:10 -0700485
Carl Worth3a37b872010-05-10 11:44:09 -0700486void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700487yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700488{
489 fprintf (stderr, "Parse error: %s\n", error);
490}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700491
Carl Worth33cc4002010-05-12 12:17:10 -0700492glcpp_parser_t *
493glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700494{
Carl Worth33cc4002010-05-12 12:17:10 -0700495 glcpp_parser_t *parser;
496
Carl Worth5070a202010-05-12 12:45:33 -0700497 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700498
Carl Worth8f38aff2010-05-19 10:01:29 -0700499 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700500 parser->defines = hash_table_ctor (32, hash_table_string_hash,
501 hash_table_string_compare);
Carl Wortha807fb72010-05-18 22:10:04 -0700502 parser->expansions = NULL;
503
Carl Worth33cc4002010-05-12 12:17:10 -0700504 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700505}
506
507int
508glcpp_parser_parse (glcpp_parser_t *parser)
509{
510 return yyparse (parser);
511}
512
513void
Carl Worth33cc4002010-05-12 12:17:10 -0700514glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700515{
Carl Worth8f38aff2010-05-19 10:01:29 -0700516 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700517 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700518 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700519}
Carl Worthc6d5af32010-05-11 12:30:09 -0700520
Carl Worthbe0e2e92010-05-19 07:29:22 -0700521static int
522glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
523{
524 expansion_node_t *node;
525
526 for (node = parser->expansions; node; node = node->next) {
527 if (node->macro &&
528 strcmp (node->macro->identifier, member) == 0)
529 {
530 return 1;
531 }
532 }
533
534 return 0;
535}
536
Carl Wortha807fb72010-05-18 22:10:04 -0700537token_class_t
538glcpp_parser_classify_token (glcpp_parser_t *parser,
539 const char *identifier,
540 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700541{
Carl Worthfcbbb462010-05-13 09:36:23 -0700542 macro_t *macro;
543
Carl Wortha807fb72010-05-18 22:10:04 -0700544 /* First we check if we are currently expanding a
545 * function-like macro, and if so, whether the parameter list
546 * contains a parameter matching this token name. */
547 if (parser->expansions &&
548 parser->expansions->macro &&
549 parser->expansions->macro->parameters)
550 {
551 string_list_t *list;
552
553 list = parser->expansions->macro->parameters;
554
555 if (_string_list_contains (list, identifier, parameter_index))
556 return TOKEN_CLASS_ARGUMENT;
557 }
558
559 /* If not a function-like macro parameter, we next check if
560 * this token is a macro itself. */
561
Carl Worthfcbbb462010-05-13 09:36:23 -0700562 macro = hash_table_find (parser->defines, identifier);
563
564 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700565 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700566
Carl Worthbe0e2e92010-05-19 07:29:22 -0700567 /* Don't consider this a macro if we are already actively
568 * expanding this macro. */
569 if (glcpp_parser_is_expanding (parser, identifier))
570 return TOKEN_CLASS_IDENTIFIER;
571
572 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700573 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700574 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700575 else
Carl Wortha807fb72010-05-18 22:10:04 -0700576 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700577}
578
Carl Worth33cc4002010-05-12 12:17:10 -0700579void
Carl Worthfcbbb462010-05-13 09:36:23 -0700580_define_object_macro (glcpp_parser_t *parser,
581 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -0700582 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700583{
584 macro_t *macro;
585
586 macro = xtalloc (parser, macro_t);
587
588 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700589 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700590 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700591 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700592
593 hash_table_insert (parser->defines, macro, identifier);
594}
595
596void
597_define_function_macro (glcpp_parser_t *parser,
598 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700599 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -0700600 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700601{
602 macro_t *macro;
603
604 macro = xtalloc (parser, macro_t);
605
606 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700607 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700608 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700609 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700610
611 hash_table_insert (parser->defines, macro, identifier);
612}
613
Carl Wortha807fb72010-05-18 22:10:04 -0700614static void
615_glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
616 macro_t *macro,
617 argument_list_t *arguments,
Carl Worth47252442010-05-19 13:54:37 -0700618 token_node_t *replacements)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700619{
Carl Wortha807fb72010-05-18 22:10:04 -0700620 expansion_node_t *node;
621
622 node = xtalloc (parser, expansion_node_t);
623
624 node->macro = macro;
625 node->arguments = arguments;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700626 node->replacements = replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700627
628 node->next = parser->expansions;
629 parser->expansions = node;
Carl Wortha807fb72010-05-18 22:10:04 -0700630}
631
Carl Worthaaa9acb2010-05-19 13:28:24 -0700632static void
Carl Wortha807fb72010-05-18 22:10:04 -0700633glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
634 macro_t *macro,
635 argument_list_t *arguments)
636{
637 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700638 macro->replacements->head);
Carl Wortha807fb72010-05-18 22:10:04 -0700639}
640
641void
642glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
643 int argument_index)
644{
645 argument_list_t *arguments;
Carl Worth47252442010-05-19 13:54:37 -0700646 token_list_t *argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700647
Carl Wortha807fb72010-05-18 22:10:04 -0700648 arguments = parser->expansions->arguments;
Carl Worth2be8be02010-05-14 10:31:43 -0700649
Carl Wortha807fb72010-05-18 22:10:04 -0700650 argument = _argument_list_member_at (arguments, argument_index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700651
Carl Wortha807fb72010-05-18 22:10:04 -0700652 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700653 argument->head);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700654}
655
Carl Worthaaa9acb2010-05-19 13:28:24 -0700656static void
Carl Wortha807fb72010-05-18 22:10:04 -0700657glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700658{
Carl Wortha807fb72010-05-18 22:10:04 -0700659 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700660
Carl Wortha807fb72010-05-18 22:10:04 -0700661 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700662
Carl Wortha807fb72010-05-18 22:10:04 -0700663 if (node == NULL) {
664 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
665 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700666 }
667
Carl Wortha807fb72010-05-18 22:10:04 -0700668 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700669
Carl Wortha807fb72010-05-18 22:10:04 -0700670 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700671}
672
Carl Wortha807fb72010-05-18 22:10:04 -0700673void
Carl Worth2be8be02010-05-14 10:31:43 -0700674_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700675{
Carl Worthfcbbb462010-05-13 09:36:23 -0700676 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700677
Carl Worthfcbbb462010-05-13 09:36:23 -0700678 macro = hash_table_find (parser->defines, identifier);
679 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700680 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700681
Carl Worthbe0e2e92010-05-19 07:29:22 -0700682 glcpp_parser_push_expansion_macro (parser, macro, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700683}
684
Carl Wortha807fb72010-05-18 22:10:04 -0700685void
Carl Worth2be8be02010-05-14 10:31:43 -0700686_expand_function_macro (glcpp_parser_t *parser,
687 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700688 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700689{
Carl Worthfcbbb462010-05-13 09:36:23 -0700690 macro_t *macro;
691
692 macro = hash_table_find (parser->defines, identifier);
693 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700694 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700695
Carl Worth8f6a8282010-05-14 10:44:19 -0700696 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700697 _string_list_length (macro->parameters))
698 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700699 fprintf (stderr,
700 "Error: macro %s invoked with %d arguments (expected %d)\n",
701 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700702 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700703 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700704 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700705 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700706
Carl Worthbe0e2e92010-05-19 07:29:22 -0700707 glcpp_parser_push_expansion_macro (parser, macro, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700708}
Carl Worth8f38aff2010-05-19 10:01:29 -0700709
710static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700711glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700712{
Carl Worthaaa9acb2010-05-19 13:28:24 -0700713 expansion_node_t *expansion;
Carl Worth47252442010-05-19 13:54:37 -0700714 token_node_t *replacements;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700715 int parameter_index;
716
717 /* Who says C can't do efficient tail recursion? */
718 RECURSE:
719
720 expansion = parser->expansions;
721
722 if (expansion == NULL)
723 return glcpp_lex (parser->scanner);
724
725 replacements = expansion->replacements;
726
727 /* Pop expansion when replacements is exhausted. */
728 if (replacements == NULL) {
729 glcpp_parser_pop_expansion (parser);
730 goto RECURSE;
731 }
732
733 expansion->replacements = replacements->next;
734
Carl Worth47252442010-05-19 13:54:37 -0700735 if (strcmp (replacements->value, "(") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700736 return '(';
Carl Worth47252442010-05-19 13:54:37 -0700737 else if (strcmp (replacements->value, ")") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700738 return ')';
Carl Worth47252442010-05-19 13:54:37 -0700739 else if (strcmp (replacements->value, ",") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700740 return ',';
741
Carl Worth47252442010-05-19 13:54:37 -0700742 yylval.str = xtalloc_strdup (parser, replacements->value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700743
744 switch (glcpp_parser_classify_token (parser, yylval.str,
745 &parameter_index))
746 {
747 case TOKEN_CLASS_ARGUMENT:
748 talloc_free (yylval.str);
749 glcpp_parser_push_expansion_argument (parser,
750 parameter_index);
751 goto RECURSE;
752 break;
753 case TOKEN_CLASS_IDENTIFIER:
754 return IDENTIFIER;
755 break;
756 case TOKEN_CLASS_FUNC_MACRO:
757 return FUNC_MACRO;
758 break;
759 default:
760 case TOKEN_CLASS_OBJ_MACRO:
761 return OBJ_MACRO;
762 break;
763 }
Carl Worth8f38aff2010-05-19 10:01:29 -0700764}