blob: 93713a3f0cae1328e8708aa5e43f6eec8131ea55 [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);
174 }
Carl Worth3a37b872010-05-10 11:44:09 -0700175;
176
Carl Worth33cc4002010-05-12 12:17:10 -0700177content:
Carl Worth9f62a7e2010-05-13 07:38:29 -0700178 IDENTIFIER {
Carl Worth005b3202010-05-20 14:19:57 -0700179 $$ = $1;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700180 }
Carl Worthb5693832010-05-20 08:01:44 -0700181| IDENTIFIER_FINALIZED {
Carl Worth005b3202010-05-20 14:19:57 -0700182 $$ = $1;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700183 }
Carl Worthb5693832010-05-20 08:01:44 -0700184| TOKEN {
Carl Worth005b3202010-05-20 14:19:57 -0700185 $$ = $1.value;
Carl Worthb5693832010-05-20 08:01:44 -0700186 }
Carl Worthacf87bc2010-05-17 10:34:29 -0700187| FUNC_MACRO {
Carl Worth005b3202010-05-20 14:19:57 -0700188 $$ = $1;
Carl Worthacf87bc2010-05-17 10:34:29 -0700189 }
Carl Wortha807fb72010-05-18 22:10:04 -0700190| directive {
Carl Worth005b3202010-05-20 14:19:57 -0700191 $$ = talloc_strdup (parser, "\n");
Carl Worth2be8be02010-05-14 10:31:43 -0700192 }
Carl Worth005b3202010-05-20 14:19:57 -0700193| punctuator {
194 $$ = talloc_asprintf (parser, "%c", $1);
195 }
196| macro {
197 $$ = NULL;
198 }
Carl Worthcd27e642010-05-12 13:11:50 -0700199;
200
Carl Worth005b3202010-05-20 14:19:57 -0700201punctuator:
202 '(' { $$ = '('; }
203| ')' { $$ = ')'; }
204| ',' { $$ = ','; }
205 ;
206
Carl Worthfcbbb462010-05-13 09:36:23 -0700207macro:
208 FUNC_MACRO '(' argument_list ')' {
Carl Wortha807fb72010-05-18 22:10:04 -0700209 _expand_function_macro (parser, $1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700210 }
211| OBJ_MACRO {
Carl Wortha807fb72010-05-18 22:10:04 -0700212 _expand_object_macro (parser, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700213 talloc_free ($1);
214 }
215;
216
217argument_list:
Carl Worthac070e82010-05-14 11:33:00 -0700218 /* empty */ {
219 $$ = _argument_list_create (parser);
220 }
221| argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700222 $$ = _argument_list_create (parser);
223 _argument_list_append ($$, $1);
224 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700225| argument_list ',' argument {
Carl Worth8f6a8282010-05-14 10:44:19 -0700226 _argument_list_append ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700227 $$ = $1;
228 }
229;
230
231argument:
Carl Worth59ca9892010-05-19 07:49:47 -0700232 argument_word {
Carl Worth47252442010-05-19 13:54:37 -0700233 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700234 _token_list_append ($$, $1.type, $1.value);
Carl Worthfcbbb462010-05-13 09:36:23 -0700235 }
Carl Worth59ca9892010-05-19 07:49:47 -0700236| argument argument_word {
Carl Worthb5693832010-05-20 08:01:44 -0700237 _token_list_append ($1, $2.type, $2.value);
238 talloc_free ($2.value);
Carl Worth3596bb12010-05-14 16:53:52 -0700239 $$ = $1;
Carl Worthfcbbb462010-05-13 09:36:23 -0700240 }
Carl Worth9f3d2c42010-05-20 08:42:02 -0700241| argument '(' argument_or_comma ')' {
Carl Worth47252442010-05-19 13:54:37 -0700242 _token_list_append ($1, '(', "(");
243 _token_list_append_list ($1, $3);
244 _token_list_append ($1, ')', ")");
Carl Worth3596bb12010-05-14 16:53:52 -0700245 $$ = $1;
246 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700247;
248
Carl Worth59ca9892010-05-19 07:49:47 -0700249argument_word:
Carl Worthb5693832010-05-20 08:01:44 -0700250 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
251| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
Carl Worth59ca9892010-05-19 07:49:47 -0700252| TOKEN { $$ = $1; }
Carl Worthb5693832010-05-20 08:01:44 -0700253| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
254| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
Carl Worth59ca9892010-05-19 07:49:47 -0700255;
256
Carl Worth9f3d2c42010-05-20 08:42:02 -0700257 /* XXX: The body of argument_or_comma is the same as the body
258 * of argument, but with "argument" and "argument_word"
259 * changed to "argument_or_comma" and
260 * "argument_word_or_comma". It would be nice to have less
261 * redundancy here, but I'm not sure how.
262 *
263 * It would also be nice to have a less ugly grammar to have
264 * to implement, but such is the C preprocessor.
265 */
266argument_or_comma:
267 argument_word_or_comma {
268 $$ = _token_list_create (parser);
269 _token_list_append ($$, $1.type, $1.value);
270 }
271| argument_or_comma argument_word_or_comma {
272 _token_list_append ($1, $2.type, $2.value);
273 $$ = $1;
274 }
275| argument_or_comma '(' argument_or_comma ')' {
276 _token_list_append ($1, '(', "(");
277 _token_list_append_list ($1, $3);
278 _token_list_append ($1, ')', ")");
279 $$ = $1;
280 }
281;
282
283argument_word_or_comma:
284 IDENTIFIER { $$.type = IDENTIFIER; $$.value = $1; }
285| IDENTIFIER_FINALIZED { $$.type = IDENTIFIER_FINALIZED; $$.value = $1; }
286| TOKEN { $$ = $1; }
287| FUNC_MACRO { $$.type = FUNC_MACRO; $$.value = $1; }
288| macro { $$.type = TOKEN; $$.value = xtalloc_strdup (parser, ""); }
289| ',' { $$.type = ','; $$.value = xtalloc_strdup (parser, ","); }
290;
Carl Worth59ca9892010-05-19 07:49:47 -0700291
Carl Worth33cc4002010-05-12 12:17:10 -0700292directive:
Carl Worthaaa9acb2010-05-19 13:28:24 -0700293 DEFINE IDENTIFIER NEWLINE {
Carl Worth47252442010-05-19 13:54:37 -0700294 token_list_t *list = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700295 _define_object_macro (parser, $2, list);
Carl Worth0a93cbb2010-05-13 10:29:07 -0700296 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700297| DEFINE IDENTIFIER SPACE replacement_list NEWLINE {
298 _define_object_macro (parser, $2, $4);
299 }
300| DEFINE IDENTIFIER '(' parameter_list ')' replacement_list NEWLINE {
Carl Worth81f01432010-05-14 17:08:45 -0700301 _define_function_macro (parser, $2, $4, $6);
Carl Worthfcbbb462010-05-13 09:36:23 -0700302 }
Carl Wortha807fb72010-05-18 22:10:04 -0700303| UNDEF IDENTIFIER {
304 string_list_t *macro = hash_table_find (parser->defines, $2);
305 if (macro) {
Carl Worthfcbbb462010-05-13 09:36:23 -0700306 /* XXX: Need hash table to support a real way
307 * to remove an element rather than prefixing
308 * a new node with data of NULL like this. */
309 hash_table_insert (parser->defines, NULL, $2);
Carl Wortha807fb72010-05-18 22:10:04 -0700310 talloc_free (macro);
Carl Worthfcbbb462010-05-13 09:36:23 -0700311 }
312 talloc_free ($2);
313 }
Carl Worth38bd27b2010-05-14 12:05:37 -0700314;
315
Carl Worthfcbbb462010-05-13 09:36:23 -0700316parameter_list:
317 /* empty */ {
Carl Worth610053b2010-05-14 10:05:11 -0700318 $$ = _string_list_create (parser);
Carl Worthfcbbb462010-05-13 09:36:23 -0700319 }
Carl Wortha807fb72010-05-18 22:10:04 -0700320| IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700321 $$ = _string_list_create (parser);
322 _string_list_append_item ($$, $1);
Carl Worthfcbbb462010-05-13 09:36:23 -0700323 talloc_free ($1);
324 }
Carl Wortha807fb72010-05-18 22:10:04 -0700325| parameter_list ',' IDENTIFIER {
Carl Worth610053b2010-05-14 10:05:11 -0700326 _string_list_append_item ($1, $3);
Carl Worthfcbbb462010-05-13 09:36:23 -0700327 talloc_free ($3);
328 $$ = $1;
329 }
330;
331
Carl Worthaaa9acb2010-05-19 13:28:24 -0700332replacement_list:
333 /* empty */ {
Carl Worth47252442010-05-19 13:54:37 -0700334 $$ = _token_list_create (parser);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700335 }
336| pp_tokens {
337 $$ = $1;
338 }
339;
340
341
342pp_tokens:
343 TOKEN {
Carl Worth47252442010-05-19 13:54:37 -0700344 $$ = _token_list_create (parser);
Carl Worthb5693832010-05-20 08:01:44 -0700345 _token_list_append ($$, $1.type, $1.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700346 }
347| pp_tokens TOKEN {
Carl Worthb5693832010-05-20 08:01:44 -0700348 _token_list_append ($1, $2.type, $2.value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700349 $$ = $1;
350 }
351;
352
Carl Worth33cc4002010-05-12 12:17:10 -0700353%%
354
Carl Worth610053b2010-05-14 10:05:11 -0700355string_list_t *
356_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700357{
Carl Worth610053b2010-05-14 10:05:11 -0700358 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700359
Carl Worth610053b2010-05-14 10:05:11 -0700360 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700361 list->head = NULL;
362 list->tail = NULL;
363
364 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700365}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700366
Carl Worth33cc4002010-05-12 12:17:10 -0700367void
Carl Worth610053b2010-05-14 10:05:11 -0700368_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700369{
370 if (list->head == NULL) {
371 list->head = tail->head;
372 } else {
373 list->tail->next = tail->head;
374 }
375
376 list->tail = tail->tail;
377}
378
379void
Carl Worth610053b2010-05-14 10:05:11 -0700380_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700381{
Carl Worth610053b2010-05-14 10:05:11 -0700382 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700383
Carl Worth610053b2010-05-14 10:05:11 -0700384 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700385 node->str = xtalloc_strdup (node, str);
Carl Worth33cc4002010-05-12 12:17:10 -0700386
387 node->next = NULL;
388
389 if (list->head == NULL) {
390 list->head = node;
391 } else {
392 list->tail->next = node;
393 }
394
395 list->tail = node;
396}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700397
398int
Carl Worth610053b2010-05-14 10:05:11 -0700399_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700400{
Carl Worth610053b2010-05-14 10:05:11 -0700401 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700402 int i;
403
404 if (list == NULL)
405 return 0;
406
407 for (i = 0, node = list->head; node; i++, node = node->next) {
408 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700409 if (index)
410 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700411 return 1;
412 }
413 }
414
415 return 0;
416}
417
418int
Carl Worth610053b2010-05-14 10:05:11 -0700419_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700420{
421 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700422 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700423
424 if (list == NULL)
425 return 0;
426
427 for (node = list->head; node; node = node->next)
428 length++;
429
430 return length;
431}
432
Carl Worth8f6a8282010-05-14 10:44:19 -0700433argument_list_t *
434_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700435{
Carl Worth8f6a8282010-05-14 10:44:19 -0700436 argument_list_t *list;
437
438 list = xtalloc (ctx, argument_list_t);
439 list->head = NULL;
440 list->tail = NULL;
441
442 return list;
443}
444
445void
Carl Worth47252442010-05-19 13:54:37 -0700446_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700447{
448 argument_node_t *node;
449
450 if (argument == NULL || argument->head == NULL)
451 return;
452
453 node = xtalloc (list, argument_node_t);
454 node->argument = argument;
455
456 node->next = NULL;
457
458 if (list->head == NULL) {
459 list->head = node;
460 } else {
461 list->tail->next = node;
462 }
463
464 list->tail = node;
465}
466
467int
468_argument_list_length (argument_list_t *list)
469{
470 int length = 0;
471 argument_node_t *node;
472
473 if (list == NULL)
474 return 0;
475
476 for (node = list->head; node; node = node->next)
477 length++;
478
479 return length;
480}
481
Carl Worth47252442010-05-19 13:54:37 -0700482token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700483_argument_list_member_at (argument_list_t *list, int index)
484{
485 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700486 int i;
487
488 if (list == NULL)
489 return NULL;
490
491 node = list->head;
492 for (i = 0; i < index; i++) {
493 node = node->next;
494 if (node == NULL)
495 break;
496 }
497
498 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700499 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700500
501 return NULL;
502}
Carl Worth47252442010-05-19 13:54:37 -0700503
504token_list_t *
505_token_list_create (void *ctx)
506{
507 token_list_t *list;
508
509 list = xtalloc (ctx, token_list_t);
510 list->head = NULL;
511 list->tail = NULL;
512
513 return list;
514}
515
516void
517_token_list_append (token_list_t *list, int type, const char *value)
518{
519 token_node_t *node;
520
521 node = xtalloc (list, token_node_t);
522 node->type = type;
523 node->value = xtalloc_strdup (list, value);
524
525 node->next = NULL;
526
527 if (list->head == NULL) {
528 list->head = node;
529 } else {
530 list->tail->next = node;
531 }
532
533 list->tail = node;
534}
535
536void
537_token_list_append_list (token_list_t *list, token_list_t *tail)
538{
539 if (list->head == NULL) {
540 list->head = tail->head;
541 } else {
542 list->tail->next = tail->head;
543 }
544
545 list->tail = tail->tail;
546}
Carl Worth33cc4002010-05-12 12:17:10 -0700547
Carl Worth3a37b872010-05-10 11:44:09 -0700548void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700549yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700550{
551 fprintf (stderr, "Parse error: %s\n", error);
552}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700553
Carl Worth33cc4002010-05-12 12:17:10 -0700554glcpp_parser_t *
555glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700556{
Carl Worth33cc4002010-05-12 12:17:10 -0700557 glcpp_parser_t *parser;
558
Carl Worth5070a202010-05-12 12:45:33 -0700559 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700560
Carl Worth8f38aff2010-05-19 10:01:29 -0700561 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700562 parser->defines = hash_table_ctor (32, hash_table_string_hash,
563 hash_table_string_compare);
Carl Wortha807fb72010-05-18 22:10:04 -0700564 parser->expansions = NULL;
565
Carl Worth5a6b9a22010-05-20 14:29:43 -0700566 parser->just_printed_separator = 1;
567
Carl Worth33cc4002010-05-12 12:17:10 -0700568 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700569}
570
571int
572glcpp_parser_parse (glcpp_parser_t *parser)
573{
574 return yyparse (parser);
575}
576
577void
Carl Worth33cc4002010-05-12 12:17:10 -0700578glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700579{
Carl Worth8f38aff2010-05-19 10:01:29 -0700580 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700581 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700582 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700583}
Carl Worthc6d5af32010-05-11 12:30:09 -0700584
Carl Worthbe0e2e92010-05-19 07:29:22 -0700585static int
586glcpp_parser_is_expanding (glcpp_parser_t *parser, const char *member)
587{
588 expansion_node_t *node;
589
590 for (node = parser->expansions; node; node = node->next) {
591 if (node->macro &&
592 strcmp (node->macro->identifier, member) == 0)
593 {
594 return 1;
595 }
596 }
597
598 return 0;
599}
600
Carl Wortha807fb72010-05-18 22:10:04 -0700601token_class_t
602glcpp_parser_classify_token (glcpp_parser_t *parser,
603 const char *identifier,
604 int *parameter_index)
Carl Worth9f62a7e2010-05-13 07:38:29 -0700605{
Carl Worthfcbbb462010-05-13 09:36:23 -0700606 macro_t *macro;
607
Carl Wortha807fb72010-05-18 22:10:04 -0700608 /* First we check if we are currently expanding a
609 * function-like macro, and if so, whether the parameter list
610 * contains a parameter matching this token name. */
611 if (parser->expansions &&
612 parser->expansions->macro &&
613 parser->expansions->macro->parameters)
614 {
615 string_list_t *list;
616
617 list = parser->expansions->macro->parameters;
618
619 if (_string_list_contains (list, identifier, parameter_index))
620 return TOKEN_CLASS_ARGUMENT;
621 }
622
623 /* If not a function-like macro parameter, we next check if
624 * this token is a macro itself. */
625
Carl Worthfcbbb462010-05-13 09:36:23 -0700626 macro = hash_table_find (parser->defines, identifier);
627
628 if (macro == NULL)
Carl Wortha807fb72010-05-18 22:10:04 -0700629 return TOKEN_CLASS_IDENTIFIER;
Carl Worthfcbbb462010-05-13 09:36:23 -0700630
Carl Worthbe0e2e92010-05-19 07:29:22 -0700631 /* Don't consider this a macro if we are already actively
632 * expanding this macro. */
633 if (glcpp_parser_is_expanding (parser, identifier))
Carl Worthb5693832010-05-20 08:01:44 -0700634 return TOKEN_CLASS_IDENTIFIER_FINALIZED;
Carl Worthbe0e2e92010-05-19 07:29:22 -0700635
636 /* Definitely a macro. Just need to check if it's function-like. */
Carl Worthfcbbb462010-05-13 09:36:23 -0700637 if (macro->is_function)
Carl Wortha807fb72010-05-18 22:10:04 -0700638 return TOKEN_CLASS_FUNC_MACRO;
Carl Worthfcbbb462010-05-13 09:36:23 -0700639 else
Carl Wortha807fb72010-05-18 22:10:04 -0700640 return TOKEN_CLASS_OBJ_MACRO;
Carl Worth9f62a7e2010-05-13 07:38:29 -0700641}
642
Carl Worth33cc4002010-05-12 12:17:10 -0700643void
Carl Worthfcbbb462010-05-13 09:36:23 -0700644_define_object_macro (glcpp_parser_t *parser,
645 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -0700646 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700647{
648 macro_t *macro;
649
650 macro = xtalloc (parser, macro_t);
651
652 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -0700653 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -0700654 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700655 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700656
657 hash_table_insert (parser->defines, macro, identifier);
658}
659
660void
661_define_function_macro (glcpp_parser_t *parser,
662 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -0700663 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -0700664 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -0700665{
666 macro_t *macro;
667
668 macro = xtalloc (parser, macro_t);
669
670 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -0700671 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -0700672 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700673 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -0700674
675 hash_table_insert (parser->defines, macro, identifier);
676}
677
Carl Wortha807fb72010-05-18 22:10:04 -0700678static void
679_glcpp_parser_push_expansion_internal (glcpp_parser_t *parser,
680 macro_t *macro,
681 argument_list_t *arguments,
Carl Worth47252442010-05-19 13:54:37 -0700682 token_node_t *replacements)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700683{
Carl Wortha807fb72010-05-18 22:10:04 -0700684 expansion_node_t *node;
685
686 node = xtalloc (parser, expansion_node_t);
687
688 node->macro = macro;
689 node->arguments = arguments;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700690 node->replacements = replacements;
Carl Wortha807fb72010-05-18 22:10:04 -0700691
692 node->next = parser->expansions;
693 parser->expansions = node;
Carl Wortha807fb72010-05-18 22:10:04 -0700694}
695
Carl Worthaaa9acb2010-05-19 13:28:24 -0700696static void
Carl Wortha807fb72010-05-18 22:10:04 -0700697glcpp_parser_push_expansion_macro (glcpp_parser_t *parser,
698 macro_t *macro,
699 argument_list_t *arguments)
700{
701 _glcpp_parser_push_expansion_internal (parser, macro, arguments,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700702 macro->replacements->head);
Carl Wortha807fb72010-05-18 22:10:04 -0700703}
704
705void
706glcpp_parser_push_expansion_argument (glcpp_parser_t *parser,
707 int argument_index)
708{
709 argument_list_t *arguments;
Carl Worth47252442010-05-19 13:54:37 -0700710 token_list_t *argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700711
Carl Wortha807fb72010-05-18 22:10:04 -0700712 arguments = parser->expansions->arguments;
Carl Worth2be8be02010-05-14 10:31:43 -0700713
Carl Wortha807fb72010-05-18 22:10:04 -0700714 argument = _argument_list_member_at (arguments, argument_index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700715
Carl Wortha807fb72010-05-18 22:10:04 -0700716 _glcpp_parser_push_expansion_internal (parser, NULL, NULL,
Carl Worthaaa9acb2010-05-19 13:28:24 -0700717 argument->head);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700718}
719
Carl Worthaaa9acb2010-05-19 13:28:24 -0700720static void
Carl Wortha807fb72010-05-18 22:10:04 -0700721glcpp_parser_pop_expansion (glcpp_parser_t *parser)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700722{
Carl Wortha807fb72010-05-18 22:10:04 -0700723 expansion_node_t *node;
Carl Worth420d05a2010-05-17 10:15:23 -0700724
Carl Wortha807fb72010-05-18 22:10:04 -0700725 node = parser->expansions;
Carl Worth420d05a2010-05-17 10:15:23 -0700726
Carl Wortha807fb72010-05-18 22:10:04 -0700727 if (node == NULL) {
728 fprintf (stderr, "Internal error: _expansion_list_pop called on an empty list.\n");
729 exit (1);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700730 }
731
Carl Wortha807fb72010-05-18 22:10:04 -0700732 parser->expansions = node->next;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700733
Carl Wortha807fb72010-05-18 22:10:04 -0700734 talloc_free (node);
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700735}
736
Carl Wortha807fb72010-05-18 22:10:04 -0700737void
Carl Worth2be8be02010-05-14 10:31:43 -0700738_expand_object_macro (glcpp_parser_t *parser, const char *identifier)
Carl Worth33cc4002010-05-12 12:17:10 -0700739{
Carl Worthfcbbb462010-05-13 09:36:23 -0700740 macro_t *macro;
Carl Worth33cc4002010-05-12 12:17:10 -0700741
Carl Worthfcbbb462010-05-13 09:36:23 -0700742 macro = hash_table_find (parser->defines, identifier);
743 assert (! macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700744 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700745
Carl Worthbe0e2e92010-05-19 07:29:22 -0700746 glcpp_parser_push_expansion_macro (parser, macro, NULL);
Carl Worthfcbbb462010-05-13 09:36:23 -0700747}
748
Carl Wortha807fb72010-05-18 22:10:04 -0700749void
Carl Worth2be8be02010-05-14 10:31:43 -0700750_expand_function_macro (glcpp_parser_t *parser,
751 const char *identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700752 argument_list_t *arguments)
Carl Worthfcbbb462010-05-13 09:36:23 -0700753{
Carl Worthfcbbb462010-05-13 09:36:23 -0700754 macro_t *macro;
755
756 macro = hash_table_find (parser->defines, identifier);
757 assert (macro->is_function);
Carl Worthbe0e2e92010-05-19 07:29:22 -0700758 assert (! glcpp_parser_is_expanding (parser, identifier));
Carl Worthfcbbb462010-05-13 09:36:23 -0700759
Carl Worth8f6a8282010-05-14 10:44:19 -0700760 if (_argument_list_length (arguments) !=
Carl Worth2be8be02010-05-14 10:31:43 -0700761 _string_list_length (macro->parameters))
762 {
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700763 fprintf (stderr,
764 "Error: macro %s invoked with %d arguments (expected %d)\n",
765 identifier,
Carl Worth8f6a8282010-05-14 10:44:19 -0700766 _argument_list_length (arguments),
Carl Worthc5e98552010-05-14 10:12:21 -0700767 _string_list_length (macro->parameters));
Carl Wortha807fb72010-05-18 22:10:04 -0700768 return;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700769 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700770
Carl Worthbe0e2e92010-05-19 07:29:22 -0700771 glcpp_parser_push_expansion_macro (parser, macro, arguments);
Carl Worth33cc4002010-05-12 12:17:10 -0700772}
Carl Worth8f38aff2010-05-19 10:01:29 -0700773
774static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700775glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -0700776{
Carl Worthaaa9acb2010-05-19 13:28:24 -0700777 expansion_node_t *expansion;
Carl Worth47252442010-05-19 13:54:37 -0700778 token_node_t *replacements;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700779 int parameter_index;
780
781 /* Who says C can't do efficient tail recursion? */
782 RECURSE:
783
784 expansion = parser->expansions;
785
786 if (expansion == NULL)
787 return glcpp_lex (parser->scanner);
788
789 replacements = expansion->replacements;
790
791 /* Pop expansion when replacements is exhausted. */
792 if (replacements == NULL) {
793 glcpp_parser_pop_expansion (parser);
794 goto RECURSE;
795 }
796
797 expansion->replacements = replacements->next;
798
Carl Worth47252442010-05-19 13:54:37 -0700799 if (strcmp (replacements->value, "(") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700800 return '(';
Carl Worth47252442010-05-19 13:54:37 -0700801 else if (strcmp (replacements->value, ")") == 0)
Carl Worthaaa9acb2010-05-19 13:28:24 -0700802 return ')';
Carl Worthaaa9acb2010-05-19 13:28:24 -0700803
Carl Worth47252442010-05-19 13:54:37 -0700804 yylval.str = xtalloc_strdup (parser, replacements->value);
Carl Worthaaa9acb2010-05-19 13:28:24 -0700805
Carl Worthb5693832010-05-20 08:01:44 -0700806 /* Carefully refuse to expand any finalized identifier. */
807 if (replacements->type == IDENTIFIER_FINALIZED)
808 return IDENTIFIER_FINALIZED;
809
Carl Worthaaa9acb2010-05-19 13:28:24 -0700810 switch (glcpp_parser_classify_token (parser, yylval.str,
811 &parameter_index))
812 {
813 case TOKEN_CLASS_ARGUMENT:
814 talloc_free (yylval.str);
815 glcpp_parser_push_expansion_argument (parser,
816 parameter_index);
817 goto RECURSE;
818 break;
819 case TOKEN_CLASS_IDENTIFIER:
820 return IDENTIFIER;
821 break;
Carl Worthb5693832010-05-20 08:01:44 -0700822 case TOKEN_CLASS_IDENTIFIER_FINALIZED:
823 return IDENTIFIER_FINALIZED;
824 break;
Carl Worthaaa9acb2010-05-19 13:28:24 -0700825 case TOKEN_CLASS_FUNC_MACRO:
826 return FUNC_MACRO;
827 break;
828 default:
829 case TOKEN_CLASS_OBJ_MACRO:
830 return OBJ_MACRO;
831 break;
832 }
Carl Worth8f38aff2010-05-19 10:01:29 -0700833}