blob: ddc2a258cd8736c9b2c8392520c6f76239735f8e [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
Carl Worth610053b2010-05-14 10:05:11 -070053string_list_t *
54_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070055
56void
Carl Worth610053b2010-05-14 10:05:11 -070057_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070058
59void
Carl Worth610053b2010-05-14 10:05:11 -070060_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070061
Carl Worthdcc2ecd2010-05-13 12:56:42 -070062int
Carl Worth610053b2010-05-14 10:05:11 -070063_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070064
Carl Worthdcc2ecd2010-05-13 12:56:42 -070065int
Carl Worth610053b2010-05-14 10:05:11 -070066_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070067
Carl Worth8f6a8282010-05-14 10:44:19 -070068argument_list_t *
69_argument_list_create (void *ctx);
70
71void
Carl Worth47252442010-05-19 13:54:37 -070072_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070073
74int
75_argument_list_length (argument_list_t *list);
76
Carl Worth47252442010-05-19 13:54:37 -070077token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070078_argument_list_member_at (argument_list_t *list, int index);
79
Carl Worth47252442010-05-19 13:54:37 -070080token_list_t *
81_token_list_create (void *ctx);
82
83void
84_token_list_append (token_list_t *list, int type, const char *value);
85
86void
87_token_list_append_list (token_list_t *list, token_list_t *tail);
88
Carl Worthaaa9acb2010-05-19 13:28:24 -070089static void
90glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
91 macro_t *macro,
92 argument_list_t *arguments);
93
94static void
95glcpp_parser_pop_expansion (glcpp_parser_t *parser);
96
Carl Worth0293b2e2010-05-19 10:05:40 -070097#define yylex glcpp_parser_lex
98
Carl Worth8f38aff2010-05-19 10:01:29 -070099static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700100glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700101
Carl Worth3a37b872010-05-10 11:44:09 -0700102%}
103
Carl Worth33cc4002010-05-12 12:17:10 -0700104%union {
Carl Worth005b3202010-05-20 14:19:57 -0700105 int ival;
Carl Worth33cc4002010-05-12 12:17:10 -0700106 char *str;
Carl Worth8f6a8282010-05-14 10:44:19 -0700107 argument_list_t *argument_list;
Carl Worth47252442010-05-19 13:54:37 -0700108 string_list_t *string_list;
Carl Worthb5693832010-05-20 08:01:44 -0700109 token_t token;
Carl Worth47252442010-05-19 13:54:37 -0700110 token_list_t *token_list;
Carl Worth33cc4002010-05-12 12:17:10 -0700111}
112
Carl Worth0b27b5f2010-05-10 16:16:06 -0700113%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700114%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700115
Carl Worth005b3202010-05-20 14:19:57 -0700116%token DEFINE FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO NEWLINE SEPARATOR SPACE TOKEN UNDEF
Carl Worth5a6b9a22010-05-20 14:29:43 -0700117%type <ival> punctuator
Carl Worth005b3202010-05-20 14:19:57 -0700118%type <str> content FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO
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
Carl Worth9f3d2c42010-05-20 08:42:02 -0700121%type <token> TOKEN argument_word argument_word_or_comma
122%type <token_list> argument argument_or_comma replacement_list pp_tokens
Carl Worth3a37b872010-05-10 11:44:09 -0700123
Carl Worth796e1f02010-05-17 12:45:16 -0700124/* Hard to remove shift/reduce conflicts documented as follows:
125 *
126 * 1. '(' after FUNC_MACRO name which is correctly resolved to shift
127 * to form macro invocation rather than reducing directly to
128 * content.
Carl Worth69f390d2010-05-19 07:42:42 -0700129 *
130 * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
131 * shift to form macro invocation rather than reducing directly to
132 * argument.
Carl Worth9f3d2c42010-05-20 08:42:02 -0700133 *
134 * 3. Similarly again now that we added argument_or_comma as well.
Carl Worth796e1f02010-05-17 12:45:16 -0700135 */
Carl Worth9f3d2c42010-05-20 08:42:02 -0700136%expect 3
Carl Worth796e1f02010-05-17 12:45:16 -0700137
Carl Worth3a37b872010-05-10 11:44:09 -0700138%%
139
Carl Worth005b3202010-05-20 14:19:57 -0700140 /* We do all printing at the input level.
141 *
142 * The value for "input" is simply TOKEN or SEPARATOR so we
143 * can decide whether it's necessary to print a space
144 * character between any two. */
Carl Worth33cc4002010-05-12 12:17:10 -0700145input:
Carl Worth005b3202010-05-20 14:19:57 -0700146 /* empty */ {
Carl Worth5a6b9a22010-05-20 14:29:43 -0700147 parser->just_printed_separator = 1;
Carl Worth005b3202010-05-20 14:19:57 -0700148 }
149| input content {
150 int is_token;
151
152 if ($2 && strlen ($2)) {
153 int c = $2[0];
154 int is_not_separator = ((c >= 'a' && c <= 'z') ||
155 (c >= 'A' && c <= 'Z') ||
156 (c >= 'A' && c <= 'Z') ||
157 (c >= '0' && c <= '9') ||
158 (c == '_'));
159
Carl Worth5a6b9a22010-05-20 14:29:43 -0700160 if (! parser->just_printed_separator && is_not_separator)
161 {
Carl Worth005b3202010-05-20 14:19:57 -0700162 printf (" ");
Carl Worth5a6b9a22010-05-20 14:29:43 -0700163 }
Carl Worth005b3202010-05-20 14:19:57 -0700164 printf ("%s", $2);
Carl Worth5a6b9a22010-05-20 14:29:43 -0700165
Carl Worth005b3202010-05-20 14:19:57 -0700166 if (is_not_separator)
Carl Worth5a6b9a22010-05-20 14:29:43 -0700167 parser->just_printed_separator = 0;
Carl Worth005b3202010-05-20 14:19:57 -0700168 else
Carl Worth5a6b9a22010-05-20 14:29:43 -0700169 parser->just_printed_separator = 1;
Carl Worth005b3202010-05-20 14:19:57 -0700170 }
Carl Worth5a6b9a22010-05-20 14:29:43 -0700171
Carl Worth005b3202010-05-20 14:19:57 -0700172 if ($2)
173 talloc_free ($2);
Carl Worth876e5102010-05-20 14:38:06 -0700174
175 if (parser->need_newline) {
176 printf ("\n");
177 parser->just_printed_separator = 1;
178 parser->need_newline = 0;
179 }
Carl Worth005b3202010-05-20 14:19:57 -0700180 }
Carl Worth3a37b872010-05-10 11:44:09 -0700181;
182
Carl Worth33cc4002010-05-12 12:17:10 -0700183content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700184 IDENTIFIER {
Carl Worth005b3202010-05-20 14:19:57 -0700185 $$ = $1;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700186 }
Carl Worthb5693832010-05-20 08:01:44 -0700187| IDENTIFIER_FINALIZED {
Carl Worth005b3202010-05-20 14:19:57 -0700188 $$ = $1;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700189 }
Carl Worthb5693832010-05-20 08:01:44 -0700190| TOKEN {
Carl Worth005b3202010-05-20 14:19:57 -0700191 $$ = $1.value;
Carl Worthb5693832010-05-20 08:01:44 -0700192 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700193| FUNC_MACRO {
Carl Worth005b3202010-05-20 14:19:57 -0700194 $$ = $1;
Carl Worthacf87bc2010-05-17 10:34:29 -0700195 }
Carl Wortha807fb72010-05-18 22:10:04 -0700196| directive {
Carl Worth005b3202010-05-20 14:19:57 -0700197 $$ = talloc_strdup (parser, "\n");
Carl Worth2be8be02010-05-14 10:31:43 -0700198 }
Carl Worth005b3202010-05-20 14:19:57 -0700199| punctuator {
200 $$ = talloc_asprintf (parser, "%c", $1);
201 }
202| macro {
203 $$ = NULL;
204 }
Carl Worthcd27e642010-05-12 13:11:50 -0700205;
206
Carl Worth005b3202010-05-20 14:19:57 -0700207punctuator:
208 '(' { $$ = '('; }
209| ')' { $$ = ')'; }
210| ',' { $$ = ','; }
211 ;
212
Carl Worthfcbbb462010-05-13 09:36:23 -0700213macro:
214 FUNC_MACRO '(' argument_list ')' {
Carl Wortha807fb72010-05-18 22:10:04 -0700215 _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700216 }
217| OBJ_MACRO {
Carl Wortha807fb72010-05-18 22:10:04 -0700218 _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700219 talloc_free ($1);
220 }
221;
222
223argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700224 /* empty */ {
225 $$ = _argument_list_create (parser);
226 }
227| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700228 $$ = _argument_list_create (parser);
229 _argument_list_append ($$, $1);
230 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700231| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700232 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700233 $$ = $1;
234 }
235;
236
237argument:
Carl Worth59ca9892010-05-19 07:49:47 -0700238 argument_word {
Carl Worth47252442010-05-19 13:54:37 -0700239 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700240 _token_list_append ($$, $1.type, $1.value);
Carl Worthfcbbb462010-05-13 09:36:23 -0700241 }
Carl Worth59ca9892010-05-19 07:49:47 -0700242| argument argument_word {
Carl Worthb5693832010-05-20 08:01:44 -0700243 _token_list_append ($1, $2.type, $2.value);
244 talloc_free ($2.value);
Carl Worth3596bb12010-05-14 16:53:52 -0700245 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700246 }
Carl Worth9f3d2c42010-05-20 08:42:02 -0700247| argument '(' argument_or_comma ')' {
Carl Worth47252442010-05-19 13:54:37 -0700248 _token_list_append ($1, '(', "(");
249 _token_list_append_list ($1, $3);
250 _token_list_append ($1, ')', ")");
Carl Worth3596bb12010-05-14 16:53:52 -0700251 $$ = $1;
252 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700253;
254
Carl Worth59ca9892010-05-19 07:49:47 -0700255argument_word:
Carl Worthb5693832010-05-20 08:01:44 -0700256 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
257| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
Carl Worth59ca9892010-05-19 07:49:47 -0700258| TOKEN { $$ = $1; }
Carl Worthb5693832010-05-20 08:01:44 -0700259| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
260| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
Carl Worth59ca9892010-05-19 07:49:47 -0700261;
262
Carl Worth9f3d2c42010-05-20 08:42:02 -0700263 /* XXX: The body of argument_or_comma is the same as the body
264 * of argument, but with "argument" and "argument_word"
265 * changed to "argument_or_comma" and
266 * "argument_word_or_comma". It would be nice to have less
267 * redundancy here, but I'm not sure how.
268 *
269 * It would also be nice to have a less ugly grammar to have
270 * to implement, but such is the C preprocessor.
271 */
272argument_or_comma:
273 argument_word_or_comma {
274 $$ = _token_list_create (parser);
275 _token_list_append ($$, $1.type, $1.value);
276 }
277| argument_or_comma argument_word_or_comma {
278 _token_list_append ($1, $2.type, $2.value);
279 $$ = $1;
280 }
281| argument_or_comma '(' argument_or_comma ')' {
282 _token_list_append ($1, '(', "(");
283 _token_list_append_list ($1, $3);
284 _token_list_append ($1, ')', ")");
285 $$ = $1;
286 }
287;
288
289argument_word_or_comma:
290 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
291| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
292| TOKEN { $$ = $1; }
293| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
294| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
295| ',' { $$.type = ','; $$.value = xtalloc_strdup (parser, ","); }
296;
Carl Worth59ca9892010-05-19 07:49:47 -0700297
Carl Worth33cc4002010-05-12 12:17:10 -0700298directive:
Carl Worthaaa9acb2010-05-19 13:28:24 -0700299 DEFINE IDENTIFIER NEWLINE {
Carl Worth47252442010-05-19 13:54:37 -0700300 token_list_t *list = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700301 _define_object_macro (parser, $2, list);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700302 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700303| DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
304 _define_object_macro (parser, $2, $4);
305 }
306| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
Carl Worth81f01432010-05-14 17:08:45 -0700307 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700308 }
Carl Wortha807fb72010-05-18 22:10:04 -0700309| UNDEF IDENTIFIER {
310 string_list_t *macro = hash_table_find (parser->defines, $2);
311 if (macro) {
Carl Worthfcbbb462010-05-13 09:36:23 -0700312 /* XXX: Need hash table to support a real way
313 * to remove an element rather than prefixing
314 * a new node with data of NULL like this. */
315 hash_table_insert (parser->defines, NULL, $2);
Carl Wortha807fb72010-05-18 22:10:04 -0700316 talloc_free (macro);
Carl Worthfcbbb462010-05-13 09:36:23 -0700317 }
318 talloc_free ($2);
319 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700320;
321
Carl Worthfcbbb462010-05-13 09:36:23 -0700322parameter_list:
323 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700324 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700325 }
Carl Wortha807fb72010-05-18 22:10:04 -0700326| IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700327 $$ = _string_list_create (parser);
328 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700329 talloc_free ($1);
330 }
Carl Wortha807fb72010-05-18 22:10:04 -0700331| parameter_list ',' IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700332 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700333 talloc_free ($3);
334 $$ = $1;
335 }
336;
337
Carl Worthaaa9acb2010-05-19 13:28:24 -0700338replacement_list:
339 /* empty */ {
Carl Worth47252442010-05-19 13:54:37 -0700340 $$ = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700341 }
342| pp_tokens {
343 $$ = $1;
344 }
345;
346
347
348pp_tokens:
349 TOKEN {
Carl Worth47252442010-05-19 13:54:37 -0700350 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700351 _token_list_append ($$, $1.type, $1.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700352 }
353| pp_tokens TOKEN {
Carl Worthb5693832010-05-20 08:01:44 -0700354 _token_list_append ($1, $2.type, $2.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700355 $$ = $1;
356 }
357;
358
Carl Worth33cc4002010-05-12 12:17:10 -0700359%%
360
Carl Worth610053b2010-05-14 10:05:11 -0700361string_list_t *
362_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700363{
Carl Worth610053b2010-05-14 10:05:11 -0700364 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700365
Carl Worth610053b2010-05-14 10:05:11 -0700366 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700367 list->head = NULL;
368 list->tail = NULL;
369
370 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700371}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700372
Carl Worth33cc4002010-05-12 12:17:10 -0700373void
Carl Worth610053b2010-05-14 10:05:11 -0700374_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700375{
376 if (list->head == NULL) {
377 list->head = tail->head;
378 } else {
379 list->tail->next = tail->head;
380 }
381
382 list->tail = tail->tail;
383}
384
385void
Carl Worth610053b2010-05-14 10:05:11 -0700386_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700387{
Carl Worth610053b2010-05-14 10:05:11 -0700388 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700389
Carl Worth610053b2010-05-14 10:05:11 -0700390 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700391 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700392
393 node->next = NULL;
394
395 if (list->head == NULL) {
396 list->head = node;
397 } else {
398 list->tail->next = node;
399 }
400
401 list->tail = node;
402}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700403
404int
Carl Worth610053b2010-05-14 10:05:11 -0700405_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700406{
Carl Worth610053b2010-05-14 10:05:11 -0700407 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700408 int i;
409
410 if (list == NULL)
411 return 0;
412
413 for (i = 0, node = list->head; node; i++, node = node->next) {
414 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700415 if (index)
416 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700417 return 1;
418 }
419 }
420
421 return 0;
422}
423
424int
Carl Worth610053b2010-05-14 10:05:11 -0700425_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700426{
427 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700428 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700429
430 if (list == NULL)
431 return 0;
432
433 for (node = list->head; node; node = node->next)
434 length++;
435
436 return length;
437}
438
Carl Worth8f6a8282010-05-14 10:44:19 -0700439argument_list_t *
440_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700441{
Carl Worth8f6a8282010-05-14 10:44:19 -0700442 argument_list_t *list;
443
444 list = xtalloc (ctx, argument_list_t);
445 list->head = NULL;
446 list->tail = NULL;
447
448 return list;
449}
450
451void
Carl Worth47252442010-05-19 13:54:37 -0700452_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700453{
454 argument_node_t *node;
455
456 if (argument == NULL || argument->head == NULL)
457 return;
458
459 node = xtalloc (list, argument_node_t);
460 node->argument = argument;
461
462 node->next = NULL;
463
464 if (list->head == NULL) {
465 list->head = node;
466 } else {
467 list->tail->next = node;
468 }
469
470 list->tail = node;
471}
472
473int
474_argument_list_length (argument_list_t *list)
475{
476 int length = 0;
477 argument_node_t *node;
478
479 if (list == NULL)
480 return 0;
481
482 for (node = list->head; node; node = node->next)
483 length++;
484
485 return length;
486}
487
Carl Worth47252442010-05-19 13:54:37 -0700488token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700489_argument_list_member_at (argument_list_t *list, int index)
490{
491 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700492 int i;
493
494 if (list == NULL)
495 return NULL;
496
497 node = list->head;
498 for (i = 0; i < index; i++) {
499 node = node->next;
500 if (node == NULL)
501 break;
502 }
503
504 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700505 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700506
507 return NULL;
508}
Carl Worth47252442010-05-19 13:54:37 -0700509
510token_list_t *
511_token_list_create (void *ctx)
512{
513 token_list_t *list;
514
515 list = xtalloc (ctx, token_list_t);
516 list->head = NULL;
517 list->tail = NULL;
518
519 return list;
520}
521
522void
523_token_list_append (token_list_t *list, int type, const char *value)
524{
525 token_node_t *node;
526
527 node = xtalloc (list, token_node_t);
528 node->type = type;
529 node->value = xtalloc_strdup (list, value);
530
531 node->next = NULL;
532
533 if (list->head == NULL) {
534 list->head = node;
535 } else {
536 list->tail->next = node;
537 }
538
539 list->tail = node;
540}
541
542void
543_token_list_append_list (token_list_t *list, token_list_t *tail)
544{
545 if (list->head == NULL) {
546 list->head = tail->head;
547 } else {
548 list->tail->next = tail->head;
549 }
550
551 list->tail = tail->tail;
552}
Carl Worth33cc4002010-05-12 12:17:10 -0700553
Carl Worth3a37b872010-05-10 11:44:09 -0700554void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700555yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700556{
557 fprintf (stderr, "Parse error: %s\n", error);
558}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700559
Carl Worth33cc4002010-05-12 12:17:10 -0700560glcpp_parser_t *
561glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700562{
Carl Worth33cc4002010-05-12 12:17:10 -0700563 glcpp_parser_t *parser;
564
Carl Worth5070a202010-05-12 12:45:33 -0700565 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700566
Carl Worth8f38aff2010-05-19 10:01:29 -0700567 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700568 parser->defines = hash_table_ctor (32, hash_table_string_hash,
569 hash_table_string_compare);
Carl Wortha807fb72010-05-18 22:10:04 -0700570 parser->expansions = NULL;
571
Carl Worth5a6b9a22010-05-20 14:29:43 -0700572 parser->just_printed_separator = 1;
Carl Worth876e5102010-05-20 14:38:06 -0700573 parser->need_newline = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700574
Carl Worth33cc4002010-05-12 12:17:10 -0700575 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700576}
577
578int
579glcpp_parser_parse (glcpp_parser_t *parser)
580{
581 return yyparse (parser);
582}
583
584void
Carl Worth33cc4002010-05-12 12:17:10 -0700585glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700586{
Carl Worth876e5102010-05-20 14:38:06 -0700587 if (parser->need_newline)
588 printf ("\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700589 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700590 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700591 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700592}
Carl Worthc6d5af32010-05-11 12:30:09 -0700593
Carl Worthbe0e2e92010-05-19 07:29:22 -0700594static int
595glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
596{
597 expansion_node_t *node;
598
599 for (node = parser->expansions; node; node = node->next) {
600 if (node->macro &&
601 strcmp (node->macro->identifier, member) == 0)
602 {
603 return 1;
604 }
605 }
606
607 return 0;
608}
609
Carl Wortha807fb72010-05-18 22:10:04 -0700610token_class_t
611glcpp_parser_classify_token (glcpp_parser_t *parser,
612 const char *identifier,
613 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700614{
Carl Worthfcbbb462010-05-13 09:36:23 -0700615 macro_t *macro;
616
Carl Wortha807fb72010-05-18 22:10:04 -0700617 /* First we check if we are currently expanding a
618 * function-like macro, and if so, whether the parameter list
619 * contains a parameter matching this token name. */
620 if (parser->expansions &&
621 parser->expansions->macro &&
622 parser->expansions->macro->parameters)
623 {
624 string_list_t *list;
625
626 list = parser->expansions->macro->parameters;
627
628 if (_string_list_contains (list, identifier, parameter_index))
629 return TOKEN_CLASS_ARGUMENT;
630 }
631
632 /* If not a function-like macro parameter, we next check if
633 * this token is a macro itself. */
634
Carl Worthfcbbb462010-05-13 09:36:23 -0700635 macro = hash_table_find (parser->defines, identifier);
636
637 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700638 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700639
Carl Worthbe0e2e92010-05-19 07:29:22 -0700640 /* Don't consider this a macro if we are already actively
641 * expanding this macro. */
642 if (glcpp_parser_is_expanding (parser, identifier))
Carl Worthb5693832010-05-20 08:01:44 -0700643 return TOKEN_CLASS_IDENTIFIER_FINALIZED;
Carl Worthbe0e2e92010-05-19 07:29:22 -0700644
645 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700646 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700647 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700648 else
Carl Wortha807fb72010-05-18 22:10:04 -0700649 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700650}
651
Carl Worth33cc4002010-05-12 12:17:10 -0700652void
Carl Worthfcbbb462010-05-13 09:36:23 -0700653_define_object_macro (glcpp_parser_t *parser,
654 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -0700655 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700656{
657 macro_t *macro;
658
659 macro = xtalloc (parser, macro_t);
660
661 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700662 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700663 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700664 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700665
666 hash_table_insert (parser->defines, macro, identifier);
667}
668
669void
670_define_function_macro (glcpp_parser_t *parser,
671 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700672 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -0700673 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700674{
675 macro_t *macro;
676
677 macro = xtalloc (parser, macro_t);
678
679 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700680 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700681 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700682 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700683
684 hash_table_insert (parser->defines, macro, identifier);
685}
686
Carl Wortha807fb72010-05-18 22:10:04 -0700687static void
688_glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
689 macro_t *macro,
690 argument_list_t *arguments,
Carl Worth47252442010-05-19 13:54:37 -0700691 token_node_t *replacements)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700692{
Carl Wortha807fb72010-05-18 22:10:04 -0700693 expansion_node_t *node;
694
695 node = xtalloc (parser, expansion_node_t);
696
697 node->macro = macro;
698 node->arguments = arguments;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700699 node->replacements = replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700700
701 node->next = parser->expansions;
702 parser->expansions = node;
Carl Wortha807fb72010-05-18 22:10:04 -0700703}
704
Carl Worthaaa9acb2010-05-19 13:28:24 -0700705static void
Carl Wortha807fb72010-05-18 22:10:04 -0700706glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
707 macro_t *macro,
708 argument_list_t *arguments)
709{
710 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700711 macro->replacements->head);
Carl Wortha807fb72010-05-18 22:10:04 -0700712}
713
714void
715glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
716 int argument_index)
717{
718 argument_list_t *arguments;
Carl Worth47252442010-05-19 13:54:37 -0700719 token_list_t *argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700720
Carl Wortha807fb72010-05-18 22:10:04 -0700721 arguments = parser->expansions->arguments;
Carl Worth2be8be02010-05-14 10:31:43 -0700722
Carl Wortha807fb72010-05-18 22:10:04 -0700723 argument = _argument_list_member_at (arguments, argument_index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700724
Carl Wortha807fb72010-05-18 22:10:04 -0700725 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700726 argument->head);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700727}
728
Carl Worthaaa9acb2010-05-19 13:28:24 -0700729static void
Carl Wortha807fb72010-05-18 22:10:04 -0700730glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700731{
Carl Wortha807fb72010-05-18 22:10:04 -0700732 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700733
Carl Wortha807fb72010-05-18 22:10:04 -0700734 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700735
Carl Wortha807fb72010-05-18 22:10:04 -0700736 if (node == NULL) {
737 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
738 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700739 }
740
Carl Wortha807fb72010-05-18 22:10:04 -0700741 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700742
Carl Wortha807fb72010-05-18 22:10:04 -0700743 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700744}
745
Carl Wortha807fb72010-05-18 22:10:04 -0700746void
Carl Worth2be8be02010-05-14 10:31:43 -0700747_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700748{
Carl Worthfcbbb462010-05-13 09:36:23 -0700749 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700750
Carl Worthfcbbb462010-05-13 09:36:23 -0700751 macro = hash_table_find (parser->defines, identifier);
752 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700753 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700754
Carl Worthbe0e2e92010-05-19 07:29:22 -0700755 glcpp_parser_push_expansion_macro (parser, macro, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700756}
757
Carl Wortha807fb72010-05-18 22:10:04 -0700758void
Carl Worth2be8be02010-05-14 10:31:43 -0700759_expand_function_macro (glcpp_parser_t *parser,
760 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700761 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700762{
Carl Worthfcbbb462010-05-13 09:36:23 -0700763 macro_t *macro;
764
765 macro = hash_table_find (parser->defines, identifier);
766 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700767 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700768
Carl Worth8f6a8282010-05-14 10:44:19 -0700769 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700770 _string_list_length (macro->parameters))
771 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700772 fprintf (stderr,
773 "Error: macro %s invoked with %d arguments (expected %d)\n",
774 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700775 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700776 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700777 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700778 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700779
Carl Worthbe0e2e92010-05-19 07:29:22 -0700780 glcpp_parser_push_expansion_macro (parser, macro, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700781}
Carl Worth8f38aff2010-05-19 10:01:29 -0700782
783static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700784glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700785{
Carl Worthaaa9acb2010-05-19 13:28:24 -0700786 expansion_node_t *expansion;
Carl Worth47252442010-05-19 13:54:37 -0700787 token_node_t *replacements;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700788 int parameter_index;
789
790 /* Who says C can't do efficient tail recursion? */
791 RECURSE:
792
793 expansion = parser->expansions;
794
795 if (expansion == NULL)
796 return glcpp_lex (parser->scanner);
797
798 replacements = expansion->replacements;
799
800 /* Pop expansion when replacements is exhausted. */
801 if (replacements == NULL) {
802 glcpp_parser_pop_expansion (parser);
803 goto RECURSE;
804 }
805
806 expansion->replacements = replacements->next;
807
Carl Worth47252442010-05-19 13:54:37 -0700808 if (strcmp (replacements->value, "(") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700809 return '(';
Carl Worth47252442010-05-19 13:54:37 -0700810 else if (strcmp (replacements->value, ")") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700811 return ')';
Carl Worthaaa9acb2010-05-19 13:28:24 -0700812
Carl Worth47252442010-05-19 13:54:37 -0700813 yylval.str = xtalloc_strdup (parser, replacements->value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700814
Carl Worthb5693832010-05-20 08:01:44 -0700815 /* Carefully refuse to expand any finalized identifier. */
816 if (replacements->type == IDENTIFIER_FINALIZED)
817 return IDENTIFIER_FINALIZED;
818
Carl Worthaaa9acb2010-05-19 13:28:24 -0700819 switch (glcpp_parser_classify_token (parser, yylval.str,
820 &parameter_index))
821 {
822 case TOKEN_CLASS_ARGUMENT:
823 talloc_free (yylval.str);
824 glcpp_parser_push_expansion_argument (parser,
825 parameter_index);
826 goto RECURSE;
827 break;
828 case TOKEN_CLASS_IDENTIFIER:
829 return IDENTIFIER;
830 break;
Carl Worthb5693832010-05-20 08:01:44 -0700831 case TOKEN_CLASS_IDENTIFIER_FINALIZED:
832 return IDENTIFIER_FINALIZED;
833 break;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700834 case TOKEN_CLASS_FUNC_MACRO:
835 return FUNC_MACRO;
836 break;
837 default:
838 case TOKEN_CLASS_OBJ_MACRO:
839 return OBJ_MACRO;
840 break;
841 }
Carl Worth8f38aff2010-05-19 10:01:29 -0700842}