blob: 0691619acf4b936d7b3345f09db8d4280de0c987 [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
Carl Worthaaa9acb2010-05-19 13:28:24 -070090glcpp_parser_pop_expansion (glcpp_parser_t *parser);
91
Carl Worth0293b2e2010-05-19 10:05:40 -070092#define yylex glcpp_parser_lex
93
Carl Worth8f38aff2010-05-19 10:01:29 -070094static int
Carl Worth0293b2e2010-05-19 10:05:40 -070095glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -070096
Carl Worth3a37b872010-05-10 11:44:09 -070097%}
98
Carl Worth33cc4002010-05-12 12:17:10 -070099%union {
Carl Worth005b3202010-05-20 14:19:57 -0700100 int ival;
Carl Worth33cc4002010-05-12 12:17:10 -0700101 char *str;
Carl Worth8f6a8282010-05-14 10:44:19 -0700102 argument_list_t *argument_list;
Carl Worth47252442010-05-19 13:54:37 -0700103 string_list_t *string_list;
Carl Worthb5693832010-05-20 08:01:44 -0700104 token_t token;
Carl Worth47252442010-05-19 13:54:37 -0700105 token_list_t *token_list;
Carl Worth33cc4002010-05-12 12:17:10 -0700106}
107
Carl Worth0b27b5f2010-05-10 16:16:06 -0700108%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700109%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700110
Carl Worth005b3202010-05-20 14:19:57 -0700111%token DEFINE FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO NEWLINE SEPARATOR SPACE TOKEN UNDEF
Carl Worth5a6b9a22010-05-20 14:29:43 -0700112%type <ival> punctuator
Carl Worth005b3202010-05-20 14:19:57 -0700113%type <str> content FUNC_MACRO IDENTIFIER IDENTIFIER_FINALIZED OBJ_MACRO
Carl Worth8f6a8282010-05-14 10:44:19 -0700114%type <argument_list> argument_list
Carl Worth47252442010-05-19 13:54:37 -0700115%type <string_list> macro parameter_list
Carl Worth9f3d2c42010-05-20 08:42:02 -0700116%type <token> TOKEN argument_word argument_word_or_comma
117%type <token_list> argument argument_or_comma replacement_list pp_tokens
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.
Carl Worth69f390d2010-05-19 07:42:42 -0700124 *
125 * 2. Similarly, '(' after FUNC_MACRO which is correctly resolved to
126 * shift to form macro invocation rather than reducing directly to
127 * argument.
Carl Worth9f3d2c42010-05-20 08:42:02 -0700128 *
129 * 3. Similarly again now that we added argument_or_comma as well.
Carl Worth796e1f02010-05-17 12:45:16 -0700130 */
Carl Worth9f3d2c42010-05-20 08:42:02 -0700131%expect 3
Carl Worth796e1f02010-05-17 12:45:16 -0700132
Carl Worth3a37b872010-05-10 11:44:09 -0700133%%
134
Carl Worth005b3202010-05-20 14:19:57 -0700135 /* We do all printing at the input level.
136 *
137 * The value for "input" is simply TOKEN or SEPARATOR so we
138 * can decide whether it's necessary to print a space
139 * character between any two. */
Carl Worth33cc4002010-05-12 12:17:10 -0700140input:
Carl Worth005b3202010-05-20 14:19:57 -0700141 /* empty */ {
Carl Worth5a6b9a22010-05-20 14:29:43 -0700142 parser->just_printed_separator = 1;
Carl Worth005b3202010-05-20 14:19:57 -0700143 }
144| input content {
145 int is_token;
146
147 if ($2 && strlen ($2)) {
148 int c = $2[0];
149 int is_not_separator = ((c >= 'a' && c <= 'z') ||
150 (c >= 'A' && c <= 'Z') ||
151 (c >= 'A' && c <= 'Z') ||
152 (c >= '0' && c <= '9') ||
153 (c == '_'));
154
Carl Worth5a6b9a22010-05-20 14:29:43 -0700155 if (! parser->just_printed_separator && is_not_separator)
156 {
Carl Worth005b3202010-05-20 14:19:57 -0700157 printf (" ");
Carl Worth5a6b9a22010-05-20 14:29:43 -0700158 }
Carl Worth005b3202010-05-20 14:19:57 -0700159 printf ("%s", $2);
Carl Worth5a6b9a22010-05-20 14:29:43 -0700160
Carl Worth005b3202010-05-20 14:19:57 -0700161 if (is_not_separator)
Carl Worth5a6b9a22010-05-20 14:29:43 -0700162 parser->just_printed_separator = 0;
Carl Worth005b3202010-05-20 14:19:57 -0700163 else
Carl Worth5a6b9a22010-05-20 14:29:43 -0700164 parser->just_printed_separator = 1;
Carl Worth005b3202010-05-20 14:19:57 -0700165 }
Carl Worth5a6b9a22010-05-20 14:29:43 -0700166
Carl Worth005b3202010-05-20 14:19:57 -0700167 if ($2)
168 talloc_free ($2);
Carl Worth876e5102010-05-20 14:38:06 -0700169
170 if (parser->need_newline) {
171 printf ("\n");
172 parser->just_printed_separator = 1;
173 parser->need_newline = 0;
174 }
Carl Worth005b3202010-05-20 14:19:57 -0700175 }
Carl Worth3a37b872010-05-10 11:44:09 -0700176;
177
Carl Worth33cc4002010-05-12 12:17:10 -0700178content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700179 IDENTIFIER {
Carl Worth005b3202010-05-20 14:19:57 -0700180 $$ = $1;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700181 }
Carl Worthb5693832010-05-20 08:01:44 -0700182| IDENTIFIER_FINALIZED {
Carl Worth005b3202010-05-20 14:19:57 -0700183 $$ = $1;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700184 }
Carl Worthb5693832010-05-20 08:01:44 -0700185| TOKEN {
Carl Worth005b3202010-05-20 14:19:57 -0700186 $$ = $1.value;
Carl Worthb5693832010-05-20 08:01:44 -0700187 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700188| FUNC_MACRO {
Carl Worth005b3202010-05-20 14:19:57 -0700189 $$ = $1;
Carl Worthacf87bc2010-05-17 10:34:29 -0700190 }
Carl Wortha807fb72010-05-18 22:10:04 -0700191| directive {
Carl Worth005b3202010-05-20 14:19:57 -0700192 $$ = talloc_strdup (parser, "\n");
Carl Worth2be8be02010-05-14 10:31:43 -0700193 }
Carl Worth005b3202010-05-20 14:19:57 -0700194| punctuator {
195 $$ = talloc_asprintf (parser, "%c", $1);
196 }
197| macro {
198 $$ = NULL;
199 }
Carl Worthcd27e642010-05-12 13:11:50 -0700200;
201
Carl Worth005b3202010-05-20 14:19:57 -0700202punctuator:
203 '(' { $$ = '('; }
204| ')' { $$ = ')'; }
205| ',' { $$ = ','; }
206 ;
207
Carl Worthfcbbb462010-05-13 09:36:23 -0700208macro:
209 FUNC_MACRO '(' argument_list ')' {
Carl Wortha807fb72010-05-18 22:10:04 -0700210 _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700211 }
212| OBJ_MACRO {
Carl Wortha807fb72010-05-18 22:10:04 -0700213 _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700214 talloc_free ($1);
215 }
216;
217
218argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700219 /* empty */ {
220 $$ = _argument_list_create (parser);
221 }
222| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700223 $$ = _argument_list_create (parser);
224 _argument_list_append ($$, $1);
225 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700226| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700227 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700228 $$ = $1;
229 }
230;
231
232argument:
Carl Worth59ca9892010-05-19 07:49:47 -0700233 argument_word {
Carl Worth47252442010-05-19 13:54:37 -0700234 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700235 _token_list_append ($$, $1.type, $1.value);
Carl Worthfcbbb462010-05-13 09:36:23 -0700236 }
Carl Worth59ca9892010-05-19 07:49:47 -0700237| argument argument_word {
Carl Worthb5693832010-05-20 08:01:44 -0700238 _token_list_append ($1, $2.type, $2.value);
239 talloc_free ($2.value);
Carl Worth3596bb12010-05-14 16:53:52 -0700240 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700241 }
Carl Worth9f3d2c42010-05-20 08:42:02 -0700242| argument '(' argument_or_comma ')' {
Carl Worth47252442010-05-19 13:54:37 -0700243 _token_list_append ($1, '(', "(");
244 _token_list_append_list ($1, $3);
245 _token_list_append ($1, ')', ")");
Carl Worth3596bb12010-05-14 16:53:52 -0700246 $$ = $1;
247 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700248;
249
Carl Worth59ca9892010-05-19 07:49:47 -0700250argument_word:
Carl Worthb5693832010-05-20 08:01:44 -0700251 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
252| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
Carl Worth59ca9892010-05-19 07:49:47 -0700253| TOKEN { $$ = $1; }
Carl Worthb5693832010-05-20 08:01:44 -0700254| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
255| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
Carl Worth59ca9892010-05-19 07:49:47 -0700256;
257
Carl Worth9f3d2c42010-05-20 08:42:02 -0700258 /* XXX: The body of argument_or_comma is the same as the body
259 * of argument, but with "argument" and "argument_word"
260 * changed to "argument_or_comma" and
261 * "argument_word_or_comma". It would be nice to have less
262 * redundancy here, but I'm not sure how.
263 *
264 * It would also be nice to have a less ugly grammar to have
265 * to implement, but such is the C preprocessor.
266 */
267argument_or_comma:
268 argument_word_or_comma {
269 $$ = _token_list_create (parser);
270 _token_list_append ($$, $1.type, $1.value);
271 }
272| argument_or_comma argument_word_or_comma {
273 _token_list_append ($1, $2.type, $2.value);
274 $$ = $1;
275 }
276| argument_or_comma '(' argument_or_comma ')' {
277 _token_list_append ($1, '(', "(");
278 _token_list_append_list ($1, $3);
279 _token_list_append ($1, ')', ")");
280 $$ = $1;
281 }
282;
283
284argument_word_or_comma:
285 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
286| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
287| TOKEN { $$ = $1; }
288| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
289| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
290| ',' { $$.type = ','; $$.value = xtalloc_strdup (parser, ","); }
291;
Carl Worth59ca9892010-05-19 07:49:47 -0700292
Carl Worth33cc4002010-05-12 12:17:10 -0700293directive:
Carl Worthaaa9acb2010-05-19 13:28:24 -0700294 DEFINE IDENTIFIER NEWLINE {
Carl Worth47252442010-05-19 13:54:37 -0700295 token_list_t *list = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700296 _define_object_macro (parser, $2, list);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700297 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700298| DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
299 _define_object_macro (parser, $2, $4);
300 }
301| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
Carl Worth81f01432010-05-14 17:08:45 -0700302 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700303 }
Carl Wortha807fb72010-05-18 22:10:04 -0700304| UNDEF IDENTIFIER {
305 string_list_t *macro = hash_table_find (parser->defines, $2);
306 if (macro) {
Carl Worthfcbbb462010-05-13 09:36:23 -0700307 /* XXX: Need hash table to support a real way
308 * to remove an element rather than prefixing
309 * a new node with data of NULL like this. */
310 hash_table_insert (parser->defines, NULL, $2);
Carl Wortha807fb72010-05-18 22:10:04 -0700311 talloc_free (macro);
Carl Worthfcbbb462010-05-13 09:36:23 -0700312 }
313 talloc_free ($2);
314 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700315;
316
Carl Worthfcbbb462010-05-13 09:36:23 -0700317parameter_list:
318 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700319 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700320 }
Carl Wortha807fb72010-05-18 22:10:04 -0700321| IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700322 $$ = _string_list_create (parser);
323 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700324 talloc_free ($1);
325 }
Carl Wortha807fb72010-05-18 22:10:04 -0700326| parameter_list ',' IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700327 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700328 talloc_free ($3);
329 $$ = $1;
330 }
331;
332
Carl Worthaaa9acb2010-05-19 13:28:24 -0700333replacement_list:
334 /* empty */ {
Carl Worth47252442010-05-19 13:54:37 -0700335 $$ = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700336 }
337| pp_tokens {
338 $$ = $1;
339 }
340;
341
342
343pp_tokens:
344 TOKEN {
Carl Worth47252442010-05-19 13:54:37 -0700345 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700346 _token_list_append ($$, $1.type, $1.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700347 }
348| pp_tokens TOKEN {
Carl Worthb5693832010-05-20 08:01:44 -0700349 _token_list_append ($1, $2.type, $2.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700350 $$ = $1;
351 }
352;
353
Carl Worth33cc4002010-05-12 12:17:10 -0700354%%
355
Carl Worth610053b2010-05-14 10:05:11 -0700356string_list_t *
357_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700358{
Carl Worth610053b2010-05-14 10:05:11 -0700359 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700360
Carl Worth610053b2010-05-14 10:05:11 -0700361 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700362 list->head = NULL;
363 list->tail = NULL;
364
365 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700366}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700367
Carl Worth33cc4002010-05-12 12:17:10 -0700368void
Carl Worth610053b2010-05-14 10:05:11 -0700369_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700370{
371 if (list->head == NULL) {
372 list->head = tail->head;
373 } else {
374 list->tail->next = tail->head;
375 }
376
377 list->tail = tail->tail;
378}
379
380void
Carl Worth610053b2010-05-14 10:05:11 -0700381_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700382{
Carl Worth610053b2010-05-14 10:05:11 -0700383 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700384
Carl Worth610053b2010-05-14 10:05:11 -0700385 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700386 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700387
388 node->next = NULL;
389
390 if (list->head == NULL) {
391 list->head = node;
392 } else {
393 list->tail->next = node;
394 }
395
396 list->tail = node;
397}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700398
399int
Carl Worth610053b2010-05-14 10:05:11 -0700400_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700401{
Carl Worth610053b2010-05-14 10:05:11 -0700402 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700403 int i;
404
405 if (list == NULL)
406 return 0;
407
408 for (i = 0, node = list->head; node; i++, node = node->next) {
409 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700410 if (index)
411 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700412 return 1;
413 }
414 }
415
416 return 0;
417}
418
419int
Carl Worth610053b2010-05-14 10:05:11 -0700420_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700421{
422 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700423 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700424
425 if (list == NULL)
426 return 0;
427
428 for (node = list->head; node; node = node->next)
429 length++;
430
431 return length;
432}
433
Carl Worth8f6a8282010-05-14 10:44:19 -0700434argument_list_t *
435_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700436{
Carl Worth8f6a8282010-05-14 10:44:19 -0700437 argument_list_t *list;
438
439 list = xtalloc (ctx, argument_list_t);
440 list->head = NULL;
441 list->tail = NULL;
442
443 return list;
444}
445
446void
Carl Worth47252442010-05-19 13:54:37 -0700447_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700448{
449 argument_node_t *node;
450
451 if (argument == NULL || argument->head == NULL)
452 return;
453
454 node = xtalloc (list, argument_node_t);
455 node->argument = argument;
456
457 node->next = NULL;
458
459 if (list->head == NULL) {
460 list->head = node;
461 } else {
462 list->tail->next = node;
463 }
464
465 list->tail = node;
466}
467
468int
469_argument_list_length (argument_list_t *list)
470{
471 int length = 0;
472 argument_node_t *node;
473
474 if (list == NULL)
475 return 0;
476
477 for (node = list->head; node; node = node->next)
478 length++;
479
480 return length;
481}
482
Carl Worth47252442010-05-19 13:54:37 -0700483token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700484_argument_list_member_at (argument_list_t *list, int index)
485{
486 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700487 int i;
488
489 if (list == NULL)
490 return NULL;
491
492 node = list->head;
493 for (i = 0; i < index; i++) {
494 node = node->next;
495 if (node == NULL)
496 break;
497 }
498
499 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700500 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700501
502 return NULL;
503}
Carl Worth47252442010-05-19 13:54:37 -0700504
505token_list_t *
506_token_list_create (void *ctx)
507{
508 token_list_t *list;
509
510 list = xtalloc (ctx, token_list_t);
511 list->head = NULL;
512 list->tail = NULL;
513
514 return list;
515}
516
517void
518_token_list_append (token_list_t *list, int type, const char *value)
519{
520 token_node_t *node;
521
522 node = xtalloc (list, token_node_t);
523 node->type = type;
524 node->value = xtalloc_strdup (list, value);
525
526 node->next = NULL;
527
528 if (list->head == NULL) {
529 list->head = node;
530 } else {
531 list->tail->next = node;
532 }
533
534 list->tail = node;
535}
536
537void
538_token_list_append_list (token_list_t *list, token_list_t *tail)
539{
540 if (list->head == NULL) {
541 list->head = tail->head;
542 } else {
543 list->tail->next = tail->head;
544 }
545
546 list->tail = tail->tail;
547}
Carl Worth33cc4002010-05-12 12:17:10 -0700548
Carl Worth3a37b872010-05-10 11:44:09 -0700549void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700550yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700551{
552 fprintf (stderr, "Parse error: %s\n", error);
553}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700554
Carl Worth33cc4002010-05-12 12:17:10 -0700555glcpp_parser_t *
556glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700557{
Carl Worth33cc4002010-05-12 12:17:10 -0700558 glcpp_parser_t *parser;
559
Carl Worth5070a202010-05-12 12:45:33 -0700560 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700561
Carl Worth8f38aff2010-05-19 10:01:29 -0700562 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700563 parser->defines = hash_table_ctor (32, hash_table_string_hash,
564 hash_table_string_compare);
Carl Wortha807fb72010-05-18 22:10:04 -0700565 parser->expansions = NULL;
566
Carl Worth5a6b9a22010-05-20 14:29:43 -0700567 parser->just_printed_separator = 1;
Carl Worth876e5102010-05-20 14:38:06 -0700568 parser->need_newline = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700569
Carl Worth33cc4002010-05-12 12:17:10 -0700570 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700571}
572
573int
574glcpp_parser_parse (glcpp_parser_t *parser)
575{
576 return yyparse (parser);
577}
578
579void
Carl Worth33cc4002010-05-12 12:17:10 -0700580glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700581{
Carl Worth876e5102010-05-20 14:38:06 -0700582 if (parser->need_newline)
583 printf ("\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700584 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700585 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700586 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700587}
Carl Worthc6d5af32010-05-11 12:30:09 -0700588
Carl Worthbe0e2e92010-05-19 07:29:22 -0700589static int
590glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
591{
592 expansion_node_t *node;
593
594 for (node = parser->expansions; node; node = node->next) {
595 if (node->macro &&
596 strcmp (node->macro->identifier, member) == 0)
597 {
598 return 1;
599 }
600 }
601
602 return 0;
603}
604
Carl Wortha807fb72010-05-18 22:10:04 -0700605token_class_t
606glcpp_parser_classify_token (glcpp_parser_t *parser,
607 const char *identifier,
608 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700609{
Carl Worthfcbbb462010-05-13 09:36:23 -0700610 macro_t *macro;
611
Carl Worthc10a51b2010-05-20 15:15:26 -0700612 /* Is this token a defined macro? */
Carl Worthfcbbb462010-05-13 09:36:23 -0700613 macro = hash_table_find (parser->defines, identifier);
614
615 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700616 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700617
Carl Worthbe0e2e92010-05-19 07:29:22 -0700618 /* Don't consider this a macro if we are already actively
619 * expanding this macro. */
620 if (glcpp_parser_is_expanding (parser, identifier))
Carl Worthb5693832010-05-20 08:01:44 -0700621 return TOKEN_CLASS_IDENTIFIER_FINALIZED;
Carl Worthbe0e2e92010-05-19 07:29:22 -0700622
623 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700624 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700625 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700626 else
Carl Wortha807fb72010-05-18 22:10:04 -0700627 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700628}
629
Carl Worth33cc4002010-05-12 12:17:10 -0700630void
Carl Worthfcbbb462010-05-13 09:36:23 -0700631_define_object_macro (glcpp_parser_t *parser,
632 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -0700633 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700634{
635 macro_t *macro;
636
637 macro = xtalloc (parser, macro_t);
638
639 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700640 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700641 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700642 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700643
644 hash_table_insert (parser->defines, macro, identifier);
645}
646
647void
648_define_function_macro (glcpp_parser_t *parser,
649 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700650 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -0700651 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700652{
653 macro_t *macro;
654
655 macro = xtalloc (parser, macro_t);
656
657 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700658 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700659 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700660 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700661
662 hash_table_insert (parser->defines, macro, identifier);
663}
664
Carl Wortha807fb72010-05-18 22:10:04 -0700665static void
Carl Worthc10a51b2010-05-20 15:15:26 -0700666_glcpp_parser_push_expansion (glcpp_parser_t *parser,
667 macro_t *macro,
668 token_node_t *replacements)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700669{
Carl Wortha807fb72010-05-18 22:10:04 -0700670 expansion_node_t *node;
671
672 node = xtalloc (parser, expansion_node_t);
673
674 node->macro = macro;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700675 node->replacements = replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700676
677 node->next = parser->expansions;
678 parser->expansions = node;
Carl Wortha807fb72010-05-18 22:10:04 -0700679}
680
Carl Worthaaa9acb2010-05-19 13:28:24 -0700681static void
Carl Wortha807fb72010-05-18 22:10:04 -0700682glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700683{
Carl Wortha807fb72010-05-18 22:10:04 -0700684 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700685
Carl Wortha807fb72010-05-18 22:10:04 -0700686 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700687
Carl Wortha807fb72010-05-18 22:10:04 -0700688 if (node == NULL) {
689 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
690 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700691 }
692
Carl Wortha807fb72010-05-18 22:10:04 -0700693 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700694
Carl Wortha807fb72010-05-18 22:10:04 -0700695 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700696}
697
Carl Wortha807fb72010-05-18 22:10:04 -0700698void
Carl Worth2be8be02010-05-14 10:31:43 -0700699_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700700{
Carl Worthfcbbb462010-05-13 09:36:23 -0700701 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700702
Carl Worthfcbbb462010-05-13 09:36:23 -0700703 macro = hash_table_find (parser->defines, identifier);
704 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700705 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700706
Carl Worthc10a51b2010-05-20 15:15:26 -0700707 _glcpp_parser_push_expansion (parser, macro, macro->replacements->head);
Carl Worthfcbbb462010-05-13 09:36:23 -0700708}
709
Carl Wortha807fb72010-05-18 22:10:04 -0700710void
Carl Worth2be8be02010-05-14 10:31:43 -0700711_expand_function_macro (glcpp_parser_t *parser,
712 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700713 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700714{
Carl Worthfcbbb462010-05-13 09:36:23 -0700715 macro_t *macro;
Carl Worthc10a51b2010-05-20 15:15:26 -0700716 token_list_t *expanded;
717 token_node_t *i, *j;
718 int parameter_index;
Carl Worthfcbbb462010-05-13 09:36:23 -0700719
720 macro = hash_table_find (parser->defines, identifier);
721 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700722 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700723
Carl Worth8f6a8282010-05-14 10:44:19 -0700724 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700725 _string_list_length (macro->parameters))
726 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700727 fprintf (stderr,
728 "Error: macro %s invoked with %d arguments (expected %d)\n",
729 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700730 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700731 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700732 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700733 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700734
Carl Worthc10a51b2010-05-20 15:15:26 -0700735 expanded = _token_list_create (macro);
736
737 for (i = macro->replacements->head; i; i = i->next) {
738 if (_string_list_contains (macro->parameters, i->value,
739 &parameter_index))
740 {
741 token_list_t *argument;
742 argument = _argument_list_member_at (arguments,
743 parameter_index);
744 for (j = argument->head; j; j = j->next)
745 {
746 _token_list_append (expanded, j->type,
747 j->value);
748 }
749 } else {
750 _token_list_append (expanded, i->type, i->value);
751 }
752 }
753
754 _glcpp_parser_push_expansion (parser, macro, expanded->head);
Carl Worth33cc4002010-05-12 12:17:10 -0700755}
Carl Worth8f38aff2010-05-19 10:01:29 -0700756
757static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700758glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700759{
Carl Worthaaa9acb2010-05-19 13:28:24 -0700760 expansion_node_t *expansion;
Carl Worth47252442010-05-19 13:54:37 -0700761 token_node_t *replacements;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700762 int parameter_index;
763
764 /* Who says C can't do efficient tail recursion? */
765 RECURSE:
766
767 expansion = parser->expansions;
768
769 if (expansion == NULL)
770 return glcpp_lex (parser->scanner);
771
772 replacements = expansion->replacements;
773
774 /* Pop expansion when replacements is exhausted. */
775 if (replacements == NULL) {
776 glcpp_parser_pop_expansion (parser);
777 goto RECURSE;
778 }
779
780 expansion->replacements = replacements->next;
781
Carl Worth47252442010-05-19 13:54:37 -0700782 if (strcmp (replacements->value, "(") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700783 return '(';
Carl Worth47252442010-05-19 13:54:37 -0700784 else if (strcmp (replacements->value, ")") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700785 return ')';
Carl Worthaaa9acb2010-05-19 13:28:24 -0700786
Carl Worth47252442010-05-19 13:54:37 -0700787 yylval.str = xtalloc_strdup (parser, replacements->value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700788
Carl Worthb5693832010-05-20 08:01:44 -0700789 /* Carefully refuse to expand any finalized identifier. */
790 if (replacements->type == IDENTIFIER_FINALIZED)
791 return IDENTIFIER_FINALIZED;
792
Carl Worthaaa9acb2010-05-19 13:28:24 -0700793 switch (glcpp_parser_classify_token (parser, yylval.str,
794 &parameter_index))
795 {
Carl Worthaaa9acb2010-05-19 13:28:24 -0700796 case TOKEN_CLASS_IDENTIFIER:
797 return IDENTIFIER;
798 break;
Carl Worthb5693832010-05-20 08:01:44 -0700799 case TOKEN_CLASS_IDENTIFIER_FINALIZED:
800 return IDENTIFIER_FINALIZED;
801 break;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700802 case TOKEN_CLASS_FUNC_MACRO:
803 return FUNC_MACRO;
804 break;
805 default:
806 case TOKEN_CLASS_OBJ_MACRO:
807 return OBJ_MACRO;
808 break;
809 }
Carl Worth8f38aff2010-05-19 10:01:29 -0700810}