blob: 9f97b2a282ac53fcece02a69588ddf2b7c48e7ba [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 Worth8fed1cd2010-05-26 09:32:12 -070028#include <inttypes.h>
Carl Worth3a37b872010-05-10 11:44:09 -070029
Carl Wortha1e32bc2010-05-10 13:17:25 -070030#include "glcpp.h"
31
Carl Worth5aa7ea02010-05-25 18:39:43 -070032static void
Carl Wortha1e32bc2010-05-10 13:17:25 -070033yyerror (void *scanner, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070034
Carl Worth5aa7ea02010-05-25 18:39:43 -070035static void
Carl Worthfcbbb462010-05-13 09:36:23 -070036_define_object_macro (glcpp_parser_t *parser,
37 const char *macro,
Carl Worth47252442010-05-19 13:54:37 -070038 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070039
Carl Worth5aa7ea02010-05-25 18:39:43 -070040static void
Carl Worthfcbbb462010-05-13 09:36:23 -070041_define_function_macro (glcpp_parser_t *parser,
42 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070043 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070044 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070045
Carl Worth5aa7ea02010-05-25 18:39:43 -070046static string_list_t *
Carl Worth610053b2010-05-14 10:05:11 -070047_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070048
Carl Worth5aa7ea02010-05-25 18:39:43 -070049static void
Carl Worth610053b2010-05-14 10:05:11 -070050_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070051
Carl Worth5aa7ea02010-05-25 18:39:43 -070052static void
Carl Worth610053b2010-05-14 10:05:11 -070053_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070054
Carl Worth5aa7ea02010-05-25 18:39:43 -070055static void
Carl Worthae6517f2010-05-25 15:24:59 -070056_string_list_push (string_list_t *list, const char *str);
57
Carl Worth5aa7ea02010-05-25 18:39:43 -070058static void
Carl Worthae6517f2010-05-25 15:24:59 -070059_string_list_pop (string_list_t *list);
60
Carl Worth5aa7ea02010-05-25 18:39:43 -070061static int
Carl Worth610053b2010-05-14 10:05:11 -070062_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070063
Carl Worth5aa7ea02010-05-25 18:39:43 -070064static int
Carl Worth610053b2010-05-14 10:05:11 -070065_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070066
Carl Worth5aa7ea02010-05-25 18:39:43 -070067static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070068_argument_list_create (void *ctx);
69
Carl Worth5aa7ea02010-05-25 18:39:43 -070070static void
Carl Worth47252442010-05-19 13:54:37 -070071_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070072
Carl Worth5aa7ea02010-05-25 18:39:43 -070073static int
Carl Worth8f6a8282010-05-14 10:44:19 -070074_argument_list_length (argument_list_t *list);
75
Carl Worth5aa7ea02010-05-25 18:39:43 -070076static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070077_argument_list_member_at (argument_list_t *list, int index);
78
Carl Worth808401f2010-05-25 14:52:43 -070079/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070080static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070081_token_create_str (void *ctx, int type, char *str);
82
Carl Worth5aa7ea02010-05-25 18:39:43 -070083static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070084_token_create_ival (void *ctx, int type, int ival);
85
Carl Worth5aa7ea02010-05-25 18:39:43 -070086static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070087_token_list_create (void *ctx);
88
Carl Worthb1ae61a2010-05-26 08:10:38 -070089/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070090 *
91 * You may want to talloc_unlink any current reference if you no
92 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070093static void
Carl Worth808401f2010-05-25 14:52:43 -070094_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070095
Carl Worth5aa7ea02010-05-25 18:39:43 -070096static void
Carl Worth47252442010-05-19 13:54:37 -070097_token_list_append_list (token_list_t *list, token_list_t *tail);
98
Carl Worth5aa7ea02010-05-25 18:39:43 -070099static void
Carl Worth8e82fcb2010-05-26 11:15:21 -0700100_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
101 token_list_t *list);
102
103static void
Carl Worthae6517f2010-05-25 15:24:59 -0700104_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
105 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700106
Carl Worthaaa9acb2010-05-19 13:28:24 -0700107static void
Carl Worth0197e9b2010-05-26 08:05:19 -0700108_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
109 token_list_t *list,
110 token_list_t *result);
111
112static void
Carl Worthb20d33c2010-05-20 22:27:07 -0700113_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
114
115static void
116_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
117 int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700118
Carl Worthb20d33c2010-05-20 22:27:07 -0700119static void
120_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
121
Carl Worth0293b2e2010-05-19 10:05:40 -0700122#define yylex glcpp_parser_lex
123
Carl Worth8f38aff2010-05-19 10:01:29 -0700124static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700125glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700126
Carl Worth8e82fcb2010-05-26 11:15:21 -0700127static void
128glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
129
Carl Worth3a37b872010-05-10 11:44:09 -0700130%}
131
Carl Worth0b27b5f2010-05-10 16:16:06 -0700132%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700133%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700134
Carl Worth050e3de2010-05-27 14:36:29 -0700135%token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_UNDEF IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE
Carl Worth8fed1cd2010-05-26 09:32:12 -0700136%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700137%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700138%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700139%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700140%type <token> preprocessing_token
141%type <token_list> pp_tokens replacement_list text_line
Carl Worth8fed1cd2010-05-26 09:32:12 -0700142%left OR
143%left AND
144%left '|'
145%left '^'
146%left '&'
147%left EQUAL NOT_EQUAL
148%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
149%left LEFT_SHIFT RIGHT_SHIFT
150%left '+' '-'
151%left '*' '/' '%'
152%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700153
154%%
155
Carl Worth33cc4002010-05-12 12:17:10 -0700156input:
Carl Worth3ff81672010-05-25 13:09:03 -0700157 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700158| input line
159;
160
161line:
162 control_line {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700163 if (parser->skip_stack == NULL ||
164 parser->skip_stack->type == SKIP_NO_SKIP)
165 {
166 printf ("\n");
167 }
Carl Worthae6517f2010-05-25 15:24:59 -0700168 }
Carl Worth808401f2010-05-25 14:52:43 -0700169| text_line {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700170 if (parser->skip_stack == NULL ||
171 parser->skip_stack->type == SKIP_NO_SKIP)
172 {
173 _glcpp_parser_print_expanded_token_list (parser, $1);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700174 printf ("\n");
Carl Worth8fed1cd2010-05-26 09:32:12 -0700175 }
Carl Worth808401f2010-05-25 14:52:43 -0700176 talloc_free ($1);
177 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700178| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700179| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700180;
181
Carl Worth8e82fcb2010-05-26 11:15:21 -0700182expanded_line:
183 IF_EXPANDED expression NEWLINE {
184 _glcpp_parser_skip_stack_push_if (parser, $2);
185 }
186| ELIF_EXPANDED expression NEWLINE {
187 _glcpp_parser_skip_stack_change_if (parser, "elif", $2);
188 }
189;
190
Carl Worth3ff81672010-05-25 13:09:03 -0700191control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700192 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Carl Worthae6517f2010-05-25 15:24:59 -0700193 _define_object_macro (parser, $2, $3);
194 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700195| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
196 _define_function_macro (parser, $2, NULL, $5);
197 }
198| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
199 _define_function_macro (parser, $2, $4, $6);
200 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700201| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700202 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700203 if (macro) {
204 /* XXX: Need hash table to support a real way
205 * to remove an element rather than prefixing
206 * a new node with data of NULL like this. */
207 hash_table_insert (parser->defines, NULL, $2);
208 talloc_free (macro);
209 }
210 talloc_free ($2);
211 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700212| HASH_IF pp_tokens NEWLINE {
213 token_list_t *expanded;
214 token_t *token;
215
216 expanded = _token_list_create (parser);
217 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
218 _token_list_append (expanded, token);
219 talloc_unlink (parser, token);
220 _glcpp_parser_evaluate_defined (parser, $2);
221 _glcpp_parser_expand_token_list_onto (parser, $2, expanded);
222 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700223 }
224| HASH_IFDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700225 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700226 talloc_free ($2);
227 _glcpp_parser_skip_stack_push_if (parser, macro != NULL);
228 }
229| HASH_IFNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700230 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700231 talloc_free ($2);
232 _glcpp_parser_skip_stack_push_if (parser, macro == NULL);
233 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700234| HASH_ELIF pp_tokens NEWLINE {
235 token_list_t *expanded;
236 token_t *token;
237
238 expanded = _token_list_create (parser);
239 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
240 _token_list_append (expanded, token);
241 talloc_unlink (parser, token);
242 _glcpp_parser_evaluate_defined (parser, $2);
243 _glcpp_parser_expand_token_list_onto (parser, $2, expanded);
244 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700245 }
246| HASH_ELSE NEWLINE {
247 _glcpp_parser_skip_stack_change_if (parser, "else", 1);
248 }
249| HASH_ENDIF NEWLINE {
250 _glcpp_parser_skip_stack_pop (parser);
251 }
Carl Worth3ff81672010-05-25 13:09:03 -0700252| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700253;
254
Carl Worth8fed1cd2010-05-26 09:32:12 -0700255expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700256 INTEGER_STRING {
257 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
258 $$ = strtoll ($1 + 2, NULL, 16);
259 } else if ($1[0] == '0') {
260 $$ = strtoll ($1, NULL, 8);
261 } else {
262 $$ = strtoll ($1, NULL, 10);
263 }
264 }
265| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700266 $$ = $1;
267 }
268| expression OR expression {
269 $$ = $1 || $3;
270 }
271| expression AND expression {
272 $$ = $1 && $3;
273 }
274| expression '|' expression {
275 $$ = $1 | $3;
276 }
277| expression '^' expression {
278 $$ = $1 ^ $3;
279 }
280| expression '&' expression {
281 $$ = $1 & $3;
282 }
283| expression NOT_EQUAL expression {
284 $$ = $1 != $3;
285 }
286| expression EQUAL expression {
287 $$ = $1 == $3;
288 }
289| expression GREATER_OR_EQUAL expression {
290 $$ = $1 >= $3;
291 }
292| expression LESS_OR_EQUAL expression {
293 $$ = $1 <= $3;
294 }
295| expression '>' expression {
296 $$ = $1 > $3;
297 }
298| expression '<' expression {
299 $$ = $1 < $3;
300 }
301| expression RIGHT_SHIFT expression {
302 $$ = $1 >> $3;
303 }
304| expression LEFT_SHIFT expression {
305 $$ = $1 << $3;
306 }
307| expression '-' expression {
308 $$ = $1 - $3;
309 }
310| expression '+' expression {
311 $$ = $1 + $3;
312 }
313| expression '%' expression {
314 $$ = $1 % $3;
315 }
316| expression '/' expression {
317 $$ = $1 / $3;
318 }
319| expression '*' expression {
320 $$ = $1 * $3;
321 }
322| '!' expression %prec UNARY {
323 $$ = ! $2;
324 }
325| '~' expression %prec UNARY {
326 $$ = ~ $2;
327 }
328| '-' expression %prec UNARY {
329 $$ = - $2;
330 }
331| '+' expression %prec UNARY {
332 $$ = + $2;
333 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700334| '(' expression ')' {
335 $$ = $2;
336 }
337;
338
Carl Worth3ff81672010-05-25 13:09:03 -0700339identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700340 IDENTIFIER {
341 $$ = _string_list_create (parser);
342 _string_list_append_item ($$, $1);
343 talloc_steal ($$, $1);
344 }
345| identifier_list ',' IDENTIFIER {
346 $$ = $1;
347 _string_list_append_item ($$, $3);
348 talloc_steal ($$, $3);
349 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700350;
351
Carl Worth3ff81672010-05-25 13:09:03 -0700352text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700353 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700354| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700355;
356
Carl Worth3ff81672010-05-25 13:09:03 -0700357non_directive:
358 pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700359;
360
Carl Worthaaa9acb2010-05-19 13:28:24 -0700361replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700362 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700363| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700364;
365
Carl Worthaaa9acb2010-05-19 13:28:24 -0700366pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700367 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700368 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700369 $$ = _token_list_create (parser);
370 _token_list_append ($$, $1);
371 talloc_unlink (parser, $1);
372 }
373| pp_tokens preprocessing_token {
374 $$ = $1;
375 _token_list_append ($$, $2);
376 talloc_unlink (parser, $2);
377 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700378;
379
Carl Worth3ff81672010-05-25 13:09:03 -0700380preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700381 IDENTIFIER {
382 $$ = _token_create_str (parser, IDENTIFIER, $1);
383 }
Carl Worth050e3de2010-05-27 14:36:29 -0700384| INTEGER_STRING {
385 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700386 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700387| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700388 $$ = _token_create_ival (parser, $1, $1);
389 }
390| OTHER {
391 $$ = _token_create_str (parser, OTHER, $1);
392 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700393| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700394 $$ = _token_create_ival (parser, SPACE, SPACE);
Carl Worthb1854fd2010-05-25 16:28:26 -0700395 }
Carl Worth3ff81672010-05-25 13:09:03 -0700396;
397
Carl Worth8e82fcb2010-05-26 11:15:21 -0700398operator:
Carl Worth808401f2010-05-25 14:52:43 -0700399 '[' { $$ = '['; }
400| ']' { $$ = ']'; }
401| '(' { $$ = '('; }
402| ')' { $$ = ')'; }
403| '{' { $$ = '{'; }
404| '}' { $$ = '}'; }
405| '.' { $$ = '.'; }
406| '&' { $$ = '&'; }
407| '*' { $$ = '*'; }
408| '+' { $$ = '+'; }
409| '-' { $$ = '-'; }
410| '~' { $$ = '~'; }
411| '!' { $$ = '!'; }
412| '/' { $$ = '/'; }
413| '%' { $$ = '%'; }
414| LEFT_SHIFT { $$ = LEFT_SHIFT; }
415| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
416| '<' { $$ = '<'; }
417| '>' { $$ = '>'; }
418| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
419| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
420| EQUAL { $$ = EQUAL; }
421| NOT_EQUAL { $$ = NOT_EQUAL; }
422| '^' { $$ = '^'; }
423| '|' { $$ = '|'; }
424| AND { $$ = AND; }
425| OR { $$ = OR; }
426| ';' { $$ = ';'; }
427| ',' { $$ = ','; }
428| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700429| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700430;
431
Carl Worth33cc4002010-05-12 12:17:10 -0700432%%
433
Carl Worth610053b2010-05-14 10:05:11 -0700434string_list_t *
435_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700436{
Carl Worth610053b2010-05-14 10:05:11 -0700437 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700438
Carl Worth610053b2010-05-14 10:05:11 -0700439 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700440 list->head = NULL;
441 list->tail = NULL;
442
443 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700444}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700445
Carl Worth33cc4002010-05-12 12:17:10 -0700446void
Carl Worth610053b2010-05-14 10:05:11 -0700447_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700448{
449 if (list->head == NULL) {
450 list->head = tail->head;
451 } else {
452 list->tail->next = tail->head;
453 }
454
455 list->tail = tail->tail;
456}
457
458void
Carl Worth610053b2010-05-14 10:05:11 -0700459_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700460{
Carl Worth610053b2010-05-14 10:05:11 -0700461 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700462
Carl Worth610053b2010-05-14 10:05:11 -0700463 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700464 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700465
Carl Worth33cc4002010-05-12 12:17:10 -0700466 node->next = NULL;
467
468 if (list->head == NULL) {
469 list->head = node;
470 } else {
471 list->tail->next = node;
472 }
473
474 list->tail = node;
475}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700476
Carl Worthae6517f2010-05-25 15:24:59 -0700477void
478_string_list_push (string_list_t *list, const char *str)
479{
480 string_node_t *node;
481
482 node = xtalloc (list, string_node_t);
483 node->str = xtalloc_strdup (node, str);
484 node->next = list->head;
485
486 if (list->tail == NULL) {
487 list->tail = node;
488 }
489 list->head = node;
490}
491
492void
493_string_list_pop (string_list_t *list)
494{
495 string_node_t *node;
496
497 node = list->head;
498
499 if (node == NULL) {
500 fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
501 exit (1);
502 }
503
504 list->head = node->next;
505 if (list->tail == node) {
506 assert (node->next == NULL);
507 list->tail = NULL;
508 }
509
510 talloc_free (node);
511}
512
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700513int
Carl Worth610053b2010-05-14 10:05:11 -0700514_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700515{
Carl Worth610053b2010-05-14 10:05:11 -0700516 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700517 int i;
518
519 if (list == NULL)
520 return 0;
521
522 for (i = 0, node = list->head; node; i++, node = node->next) {
523 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700524 if (index)
525 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700526 return 1;
527 }
528 }
529
530 return 0;
531}
532
533int
Carl Worth610053b2010-05-14 10:05:11 -0700534_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700535{
536 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700537 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700538
539 if (list == NULL)
540 return 0;
541
542 for (node = list->head; node; node = node->next)
543 length++;
544
545 return length;
546}
547
Carl Worth8f6a8282010-05-14 10:44:19 -0700548argument_list_t *
549_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700550{
Carl Worth8f6a8282010-05-14 10:44:19 -0700551 argument_list_t *list;
552
553 list = xtalloc (ctx, argument_list_t);
554 list->head = NULL;
555 list->tail = NULL;
556
557 return list;
558}
559
560void
Carl Worth47252442010-05-19 13:54:37 -0700561_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700562{
563 argument_node_t *node;
564
Carl Worth8f6a8282010-05-14 10:44:19 -0700565 node = xtalloc (list, argument_node_t);
566 node->argument = argument;
567
568 node->next = NULL;
569
570 if (list->head == NULL) {
571 list->head = node;
572 } else {
573 list->tail->next = node;
574 }
575
576 list->tail = node;
577}
578
579int
580_argument_list_length (argument_list_t *list)
581{
582 int length = 0;
583 argument_node_t *node;
584
585 if (list == NULL)
586 return 0;
587
588 for (node = list->head; node; node = node->next)
589 length++;
590
591 return length;
592}
593
Carl Worth47252442010-05-19 13:54:37 -0700594token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700595_argument_list_member_at (argument_list_t *list, int index)
596{
597 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700598 int i;
599
600 if (list == NULL)
601 return NULL;
602
603 node = list->head;
604 for (i = 0; i < index; i++) {
605 node = node->next;
606 if (node == NULL)
607 break;
608 }
609
610 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700611 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700612
613 return NULL;
614}
Carl Worth47252442010-05-19 13:54:37 -0700615
Carl Worth808401f2010-05-25 14:52:43 -0700616/* Note: This function talloc_steal()s the str pointer. */
617token_t *
618_token_create_str (void *ctx, int type, char *str)
619{
620 token_t *token;
621
622 token = xtalloc (ctx, token_t);
623 token->type = type;
624 token->value.str = talloc_steal (token, str);
625
626 return token;
627}
628
629token_t *
630_token_create_ival (void *ctx, int type, int ival)
631{
632 token_t *token;
633
634 token = xtalloc (ctx, token_t);
635 token->type = type;
636 token->value.ival = ival;
637
638 return token;
639}
640
Carl Worth47252442010-05-19 13:54:37 -0700641token_list_t *
642_token_list_create (void *ctx)
643{
644 token_list_t *list;
645
646 list = xtalloc (ctx, token_list_t);
647 list->head = NULL;
648 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700649 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700650
651 return list;
652}
653
654void
Carl Worth808401f2010-05-25 14:52:43 -0700655_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700656{
657 token_node_t *node;
658
659 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700660 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700661
662 node->next = NULL;
663
664 if (list->head == NULL) {
665 list->head = node;
666 } else {
667 list->tail->next = node;
668 }
669
670 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700671 if (token->type != SPACE)
672 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700673}
674
675void
676_token_list_append_list (token_list_t *list, token_list_t *tail)
677{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700678 if (tail == NULL || tail->head == NULL)
679 return;
680
Carl Worth47252442010-05-19 13:54:37 -0700681 if (list->head == NULL) {
682 list->head = tail->head;
683 } else {
684 list->tail->next = tail->head;
685 }
686
687 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700688 list->non_space_tail = tail->non_space_tail;
689}
690
691void
692_token_list_trim_trailing_space (token_list_t *list)
693{
694 token_node_t *tail, *next;
695
696 if (list->non_space_tail) {
697 tail = list->non_space_tail->next;
698 list->non_space_tail->next = NULL;
699 list->tail = list->non_space_tail;
700
701 while (tail) {
702 next = tail->next;
703 talloc_free (tail);
704 tail = next;
705 }
706 }
Carl Worth47252442010-05-19 13:54:37 -0700707}
Carl Worth80dc60b2010-05-25 14:42:00 -0700708
Carl Worth0197e9b2010-05-26 08:05:19 -0700709static void
710_token_print (token_t *token)
711{
712 if (token->type < 256) {
713 printf ("%c", token->type);
714 return;
715 }
716
717 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700718 case INTEGER:
719 printf ("%" PRIxMAX, token->value.ival);
720 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700721 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700722 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700723 case OTHER:
724 printf ("%s", token->value.str);
725 break;
726 case SPACE:
727 printf (" ");
728 break;
729 case LEFT_SHIFT:
730 printf ("<<");
731 break;
732 case RIGHT_SHIFT:
733 printf (">>");
734 break;
735 case LESS_OR_EQUAL:
736 printf ("<=");
737 break;
738 case GREATER_OR_EQUAL:
739 printf (">=");
740 break;
741 case EQUAL:
742 printf ("==");
743 break;
744 case NOT_EQUAL:
745 printf ("!=");
746 break;
747 case AND:
748 printf ("&&");
749 break;
750 case OR:
751 printf ("||");
752 break;
753 case PASTE:
754 printf ("##");
755 break;
Carl Worthdd749002010-05-27 10:12:33 -0700756 case COMMA_FINAL:
757 printf (",");
758 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700759 case PLACEHOLDER:
760 /* Nothing to print. */
761 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700762 default:
763 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
764 break;
765 }
766}
767
Carl Worthad0dee62010-05-26 09:04:50 -0700768/* Change 'token' into a new token formed by pasting 'other'. */
769static void
770_token_paste (token_t *token, token_t *other)
771{
Carl Worth85b50e82010-05-27 14:01:18 -0700772 /* Pasting a placeholder onto anything makes no change. */
773 if (other->type == PLACEHOLDER)
774 return;
775
776 /* When 'token' is a placeholder, just return contents of 'other'. */
777 if (token->type == PLACEHOLDER) {
778 token->type = other->type;
779 token->value = other->value;
780 return;
781 }
782
Carl Worthad0dee62010-05-26 09:04:50 -0700783 /* A very few single-character punctuators can be combined
784 * with another to form a multi-character punctuator. */
785 switch (token->type) {
786 case '<':
787 if (other->type == '<') {
788 token->type = LEFT_SHIFT;
789 token->value.ival = LEFT_SHIFT;
790 return;
791 } else if (other->type == '=') {
792 token->type = LESS_OR_EQUAL;
793 token->value.ival = LESS_OR_EQUAL;
794 return;
795 }
796 break;
797 case '>':
798 if (other->type == '>') {
799 token->type = RIGHT_SHIFT;
800 token->value.ival = RIGHT_SHIFT;
801 return;
802 } else if (other->type == '=') {
803 token->type = GREATER_OR_EQUAL;
804 token->value.ival = GREATER_OR_EQUAL;
805 return;
806 }
807 break;
808 case '=':
809 if (other->type == '=') {
810 token->type = EQUAL;
811 token->value.ival = EQUAL;
812 return;
813 }
814 break;
815 case '!':
816 if (other->type == '=') {
817 token->type = NOT_EQUAL;
818 token->value.ival = NOT_EQUAL;
819 return;
820 }
821 break;
822 case '&':
823 if (other->type == '&') {
824 token->type = AND;
825 token->value.ival = AND;
826 return;
827 }
828 break;
829 case '|':
830 if (other->type == '|') {
831 token->type = OR;
832 token->value.ival = OR;
833 return;
834 }
835 break;
836 }
837
838 /* Two string-valued tokens can usually just be mashed
839 * together.
840 *
Carl Worth050e3de2010-05-27 14:36:29 -0700841 * XXX: This isn't actually legitimate. Several things here
842 * should result in a diagnostic since the result cannot be a
843 * valid, single pre-processing token. For example, pasting
844 * "123" and "abc" is not legal, but we don't catch that
845 * here. */
846 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
847 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700848 {
849 token->value.str = talloc_strdup_append (token->value.str,
850 other->value.str);
851 return;
852 }
853
854 printf ("Error: Pasting \"");
855 _token_print (token);
856 printf ("\" and \"");
857 _token_print (other);
858 printf ("\" does not give a valid preprocessing token.\n");
859}
860
Carl Worth0197e9b2010-05-26 08:05:19 -0700861static void
862_token_list_print (token_list_t *list)
863{
864 token_node_t *node;
865
866 if (list == NULL)
867 return;
868
869 for (node = list->head; node; node = node->next)
870 _token_print (node->token);
871}
872
Carl Worth3a37b872010-05-10 11:44:09 -0700873void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700874yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700875{
876 fprintf (stderr, "Parse error: %s\n", error);
877}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700878
Carl Worth33cc4002010-05-12 12:17:10 -0700879glcpp_parser_t *
880glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700881{
Carl Worth33cc4002010-05-12 12:17:10 -0700882 glcpp_parser_t *parser;
883
Carl Worth5070a202010-05-12 12:45:33 -0700884 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700885
Carl Worth8f38aff2010-05-19 10:01:29 -0700886 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700887 parser->defines = hash_table_ctor (32, hash_table_string_hash,
888 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700889 parser->active = _string_list_create (parser);
Carl Worthf34a0002010-05-25 16:59:02 -0700890 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700891 parser->newline_as_space = 0;
892 parser->in_control_line = 0;
893 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700894
Carl Worthb20d33c2010-05-20 22:27:07 -0700895 parser->skip_stack = NULL;
896
Carl Worth8e82fcb2010-05-26 11:15:21 -0700897 parser->lex_from_list = NULL;
898 parser->lex_from_node = NULL;
899
Carl Worth33cc4002010-05-12 12:17:10 -0700900 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700901}
902
903int
904glcpp_parser_parse (glcpp_parser_t *parser)
905{
906 return yyparse (parser);
907}
908
909void
Carl Worth33cc4002010-05-12 12:17:10 -0700910glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700911{
Carl Worthb20d33c2010-05-20 22:27:07 -0700912 if (parser->skip_stack)
913 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700914 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700915 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700916 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700917}
Carl Worthc6d5af32010-05-11 12:30:09 -0700918
Carl Worth8e82fcb2010-05-26 11:15:21 -0700919/* Replace any occurences of DEFINED tokens in 'list' with either a
920 * '0' or '1' INTEGER token depending on whether the next token in the
921 * list is defined or not. */
922static void
923_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
924 token_list_t *list)
925{
926 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700927 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700928
929 if (list == NULL)
930 return;
931
932 for (node = list->head; node; node = node->next) {
933 if (node->token->type != DEFINED)
934 continue;
935 next = node->next;
936 while (next && next->token->type == SPACE)
937 next = next->next;
938 if (next == NULL || next->token->type != IDENTIFIER) {
939 fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
940 exit (1);
941 }
942 macro = hash_table_find (parser->defines,
943 next->token->value.str);
944
945 node->token->type = INTEGER;
946 node->token->value.ival = (macro != NULL);
947 node->next = next->next;
948 }
949}
950
Carl Worthb1854fd2010-05-25 16:28:26 -0700951typedef enum function_status
952{
953 FUNCTION_STATUS_SUCCESS,
954 FUNCTION_NOT_A_FUNCTION,
955 FUNCTION_UNBALANCED_PARENTHESES
956} function_status_t;
957
958/* Find a set of function-like macro arguments by looking for a
959 * balanced set of parentheses. Upon return *node will be the last
960 * consumed node, such that further processing can continue with
961 * node->next.
962 *
963 * Return values:
964 *
965 * FUNCTION_STATUS_SUCCESS:
966 *
967 * Successfully parsed a set of function arguments.
968 *
969 * FUNCTION_NOT_A_FUNCTION:
970 *
971 * Macro name not followed by a '('. This is not an error, but
972 * simply that the macro name should be treated as a non-macro.
973 *
974 * FUNCTION_UNBLANCED_PARENTHESES
975 *
976 * Macro name is not followed by a balanced set of parentheses.
977 */
978static function_status_t
Carl Worth9ce18cf2010-05-25 17:32:21 -0700979_arguments_parse (argument_list_t *arguments, token_node_t **node_ret)
Carl Worthb1854fd2010-05-25 16:28:26 -0700980{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700981 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700982 token_node_t *node = *node_ret, *last;
983 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700984
985 last = node;
986 node = node->next;
987
988 /* Ignore whitespace before first parenthesis. */
989 while (node && node->token->type == SPACE)
990 node = node->next;
991
992 if (node == NULL || node->token->type != '(')
993 return FUNCTION_NOT_A_FUNCTION;
994
Carl Worth652fa272010-05-25 17:45:22 -0700995 last = node;
996 node = node->next;
997
Carl Wortha19297b2010-05-27 13:29:19 -0700998 argument = _token_list_create (arguments);
999 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001000
Carl Worth652fa272010-05-25 17:45:22 -07001001 for (paren_count = 1; node; last = node, node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001002 if (node->token->type == '(')
1003 {
1004 paren_count++;
1005 }
1006 else if (node->token->type == ')')
1007 {
1008 paren_count--;
Carl Worthc7581c22010-05-25 17:41:07 -07001009 if (paren_count == 0) {
1010 last = node;
1011 node = node->next;
1012 break;
1013 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001014 }
Carl Worth652fa272010-05-25 17:45:22 -07001015
1016 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001017 paren_count == 1)
1018 {
Carl Wortha19297b2010-05-27 13:29:19 -07001019 _token_list_trim_trailing_space (argument);
1020 argument = _token_list_create (arguments);
1021 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001022 }
1023 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001024 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001025 /* Don't treat initial whitespace as
1026 * part of the arguement. */
1027 if (node->token->type == SPACE)
1028 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001029 }
1030 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001031 }
Carl Worthc7581c22010-05-25 17:41:07 -07001032 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001033
1034 if (node && paren_count)
1035 return FUNCTION_UNBALANCED_PARENTHESES;
1036
1037 *node_ret = last;
1038
1039 return FUNCTION_STATUS_SUCCESS;
1040}
1041
Carl Worth3c93d392010-05-28 08:17:46 -07001042/* Appends expansion of *node (consuming further tokens from the list
1043 * as necessary) onto result. Upon return *node will be the last
1044 * consumed node, such that further processing can continue with
1045 * node->next. */
Carl Worth95ec4332010-05-28 08:00:43 -07001046static void
1047_glcpp_parser_expand_function_onto (glcpp_parser_t *parser,
1048 token_node_t **node_ret,
1049 token_list_t *result)
Carl Worthb1854fd2010-05-25 16:28:26 -07001050{
1051 macro_t *macro;
1052 token_node_t *node;
1053 const char *identifier;
1054 argument_list_t *arguments;
1055 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001056 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001057 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001058
1059 node = *node_ret;
1060 identifier = node->token->value.str;
1061
1062 macro = hash_table_find (parser->defines, identifier);
1063
1064 assert (macro->is_function);
1065
Carl Worth9ce18cf2010-05-25 17:32:21 -07001066 arguments = _argument_list_create (parser);
1067 status = _arguments_parse (arguments, node_ret);
Carl Worthb1854fd2010-05-25 16:28:26 -07001068
1069 switch (status) {
1070 case FUNCTION_STATUS_SUCCESS:
1071 break;
1072 case FUNCTION_NOT_A_FUNCTION:
Carl Worth0197e9b2010-05-26 08:05:19 -07001073 _token_list_append (result, node->token);
Carl Worth95ec4332010-05-28 08:00:43 -07001074 return;
Carl Worthb1854fd2010-05-25 16:28:26 -07001075 case FUNCTION_UNBALANCED_PARENTHESES:
1076 fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n",
1077 identifier);
1078 exit (1);
Carl Worthae6517f2010-05-25 15:24:59 -07001079 }
1080
Carl Worth9ce18cf2010-05-25 17:32:21 -07001081 if (macro->replacements == NULL) {
1082 talloc_free (arguments);
Carl Worth95ec4332010-05-28 08:00:43 -07001083 return;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001084 }
1085
Carl Wortha19297b2010-05-27 13:29:19 -07001086 if (! ((_argument_list_length (arguments) ==
1087 _string_list_length (macro->parameters)) ||
1088 (_string_list_length (macro->parameters) == 0 &&
1089 _argument_list_length (arguments) == 1 &&
1090 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001091 {
1092 fprintf (stderr,
1093 "Error: macro %s invoked with %d arguments (expected %d)\n",
1094 identifier,
1095 _argument_list_length (arguments),
1096 _string_list_length (macro->parameters));
Carl Worth95ec4332010-05-28 08:00:43 -07001097 return;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001098 }
1099
Carl Worth0197e9b2010-05-26 08:05:19 -07001100 /* Perform argument substitution on the replacement list. */
1101 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001102
Carl Worthce540f22010-05-26 08:25:44 -07001103 for (node = macro->replacements->head; node; node = node->next)
1104 {
1105 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001106 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001107 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001108 &parameter_index))
1109 {
1110 token_list_t *argument;
1111 argument = _argument_list_member_at (arguments,
1112 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001113 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001114 * tokens, or append a placeholder token for
1115 * an empty argument. */
1116 if (argument->head) {
1117 _glcpp_parser_expand_token_list_onto (parser,
1118 argument,
1119 substituted);
1120 } else {
1121 token_t *new_token;
1122
1123 new_token = _token_create_ival (substituted,
1124 PLACEHOLDER,
1125 PLACEHOLDER);
1126 _token_list_append (substituted, new_token);
1127 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001128 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001129 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001130 }
1131 }
1132
Carl Worthad0dee62010-05-26 09:04:50 -07001133 /* After argument substitution, and before further expansion
1134 * below, implement token pasting. */
1135
1136 node = substituted->head;
1137 while (node)
1138 {
1139 token_node_t *next_non_space;
1140
1141 /* Look ahead for a PASTE token, skipping space. */
1142 next_non_space = node->next;
1143 while (next_non_space && next_non_space->token->type == SPACE)
1144 next_non_space = next_non_space->next;
1145
1146 if (next_non_space == NULL)
1147 break;
1148
1149 if (next_non_space->token->type != PASTE) {
1150 node = next_non_space;
1151 continue;
1152 }
1153
1154 /* Now find the next non-space token after the PASTE. */
1155 next_non_space = next_non_space->next;
1156 while (next_non_space && next_non_space->token->type == SPACE)
1157 next_non_space = next_non_space->next;
1158
1159 if (next_non_space == NULL) {
1160 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth3c93d392010-05-28 08:17:46 -07001161 return;
Carl Worthad0dee62010-05-26 09:04:50 -07001162 }
1163
1164 _token_paste (node->token, next_non_space->token);
1165 node->next = next_non_space->next;
1166
1167 node = node->next;
1168 }
1169
Carl Worthae6517f2010-05-25 15:24:59 -07001170 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -07001171 _glcpp_parser_expand_token_list_onto (parser, substituted, result);
Carl Worthae6517f2010-05-25 15:24:59 -07001172 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001173
1174 talloc_free (arguments);
Carl Worthae6517f2010-05-25 15:24:59 -07001175}
1176
Carl Worth3c93d392010-05-28 08:17:46 -07001177
1178/* Appends the expansion of the token in *node onto result.
1179 * Upon return *node will be the last consumed node, such that further
1180 * processing can continue with node->next. */
1181static void
1182_glcpp_parser_expand_token_onto (glcpp_parser_t *parser,
1183 token_node_t **node,
1184 token_list_t *result)
1185{
1186 token_t *token = (*node)->token;
1187 const char *identifier;
1188 macro_t *macro;
1189 token_list_t *expansion;
1190
1191 /* We only expand identifiers */
1192 if (token->type != IDENTIFIER) {
1193 /* We change any COMMA into a COMMA_FINAL to prevent
1194 * it being mistaken for an argument separator
1195 * later. */
1196 if (token->type == ',') {
1197 token_t *new_token;
1198
1199 new_token = _token_create_ival (result, COMMA_FINAL,
1200 COMMA_FINAL);
1201 _token_list_append (result, new_token);
1202 } else {
1203 _token_list_append (result, token);
1204 }
1205 return;
1206 }
1207
1208 /* Look up this identifier in the hash table. */
1209 identifier = token->value.str;
1210 macro = hash_table_find (parser->defines, identifier);
1211
1212 /* Not a macro, so just append. */
1213 if (macro == NULL) {
1214 _token_list_append (result, token);
1215 return;
1216 }
1217
1218 /* Finally, don't expand this macro if we're already actively
1219 * expanding it, (to avoid infinite recursion). */
1220 if (_string_list_contains (parser->active, identifier, NULL))
1221 {
1222 /* We change the token type here from IDENTIFIER to
1223 * OTHER to prevent any future expansion of this
1224 * unexpanded token. */
1225 char *str;
1226 token_t *new_token;
1227
1228 str = xtalloc_strdup (result, token->value.str);
1229 new_token = _token_create_str (result, OTHER, str);
1230 _token_list_append (result, new_token);
1231 return;
1232 }
1233
1234 if (macro->is_function) {
1235 _glcpp_parser_expand_function_onto (parser, node, result);
1236 } else {
1237 _string_list_push (parser->active, identifier);
1238 _glcpp_parser_expand_token_list_onto (parser,
1239 macro->replacements,
1240 result);
1241 _string_list_pop (parser->active);
1242 }
1243}
1244
Carl Worth0197e9b2010-05-26 08:05:19 -07001245static void
1246_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
1247 token_list_t *list,
1248 token_list_t *result)
1249{
1250 token_node_t *node;
1251
Carl Wortha65cf7b2010-05-27 11:55:36 -07001252 if (list == NULL || list->head == NULL)
Carl Worth0197e9b2010-05-26 08:05:19 -07001253 return;
1254
Carl Worth95ec4332010-05-28 08:00:43 -07001255 for (node = list->head; node; node = node->next)
1256 {
Carl Worth3c93d392010-05-28 08:17:46 -07001257 _glcpp_parser_expand_token_onto (parser, &node, result);
Carl Worth0197e9b2010-05-26 08:05:19 -07001258 }
1259}
1260
Carl Worthae6517f2010-05-25 15:24:59 -07001261void
1262_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1263 token_list_t *list)
1264{
Carl Worth0197e9b2010-05-26 08:05:19 -07001265 token_list_t *expanded;
Carl Worthae6517f2010-05-25 15:24:59 -07001266 token_node_t *node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001267 function_status_t function_status;
Carl Worthae6517f2010-05-25 15:24:59 -07001268
1269 if (list == NULL)
1270 return;
1271
Carl Worth0197e9b2010-05-26 08:05:19 -07001272 expanded = _token_list_create (parser);
Carl Worth10ae4382010-05-25 20:35:01 -07001273
Carl Worth0197e9b2010-05-26 08:05:19 -07001274 _glcpp_parser_expand_token_list_onto (parser, list, expanded);
1275
1276 _token_list_trim_trailing_space (expanded);
1277
1278 _token_list_print (expanded);
1279
1280 talloc_free (expanded);
Carl Worthae6517f2010-05-25 15:24:59 -07001281}
1282
1283void
Carl Worthfcbbb462010-05-13 09:36:23 -07001284_define_object_macro (glcpp_parser_t *parser,
1285 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001286 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001287{
1288 macro_t *macro;
1289
1290 macro = xtalloc (parser, macro_t);
1291
1292 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001293 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001294 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001295 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001296
1297 hash_table_insert (parser->defines, macro, identifier);
1298}
1299
1300void
1301_define_function_macro (glcpp_parser_t *parser,
1302 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001303 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001304 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001305{
1306 macro_t *macro;
1307
1308 macro = xtalloc (parser, macro_t);
1309
1310 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001311 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001312 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001313 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001314
1315 hash_table_insert (parser->defines, macro, identifier);
1316}
1317
Carl Worth8f38aff2010-05-19 10:01:29 -07001318static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001319glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001320{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001321 token_node_t *node;
1322 int ret;
1323
Carl Worth95951ea2010-05-26 15:57:10 -07001324 if (parser->lex_from_list == NULL) {
1325 ret = glcpp_lex (parser->scanner);
1326
1327 /* XXX: This ugly block of code exists for the sole
1328 * purpose of converting a NEWLINE token into a SPACE
1329 * token, but only in the case where we have seen a
1330 * function-like macro name, but have not yet seen its
1331 * closing parenthesis.
1332 *
1333 * There's perhaps a more compact way to do this with
1334 * mid-rule actions in the grammar.
1335 *
1336 * I'm definitely not pleased with the complexity of
1337 * this code here.
1338 */
1339 if (parser->newline_as_space)
1340 {
1341 if (ret == '(') {
1342 parser->paren_count++;
1343 } else if (ret == ')') {
1344 parser->paren_count--;
1345 if (parser->paren_count == 0)
1346 parser->newline_as_space = 0;
1347 } else if (ret == NEWLINE) {
1348 ret = SPACE;
1349 } else if (ret != SPACE) {
1350 if (parser->paren_count == 0)
1351 parser->newline_as_space = 0;
1352 }
1353 }
1354 else if (parser->in_control_line)
1355 {
1356 if (ret == NEWLINE)
1357 parser->in_control_line = 0;
1358 }
1359 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1360 ret == HASH_UNDEF || ret == HASH_IF ||
1361 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1362 ret == HASH_ELIF || ret == HASH_ELSE ||
1363 ret == HASH_ENDIF || ret == HASH)
1364 {
1365 parser->in_control_line = 1;
1366 }
1367 else if (ret == IDENTIFIER)
1368 {
1369 macro_t *macro;
1370 macro = hash_table_find (parser->defines,
1371 yylval.str);
1372 if (macro && macro->is_function) {
1373 parser->newline_as_space = 1;
1374 parser->paren_count = 0;
1375 }
1376 }
1377
1378 return ret;
1379 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001380
1381 node = parser->lex_from_node;
1382
1383 if (node == NULL) {
1384 talloc_free (parser->lex_from_list);
1385 parser->lex_from_list = NULL;
1386 return NEWLINE;
1387 }
1388
1389 yylval = node->token->value;
1390 ret = node->token->type;
1391
1392 parser->lex_from_node = node->next;
1393
1394 return ret;
1395}
1396
1397static void
1398glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1399{
1400 token_node_t *node;
1401
1402 assert (parser->lex_from_list == NULL);
1403
1404 /* Copy list, eliminating any space tokens. */
1405 parser->lex_from_list = _token_list_create (parser);
1406
1407 for (node = list->head; node; node = node->next) {
1408 if (node->token->type == SPACE)
1409 continue;
1410 _token_list_append (parser->lex_from_list, node->token);
1411 }
1412
1413 talloc_free (list);
1414
1415 parser->lex_from_node = parser->lex_from_list->head;
1416
1417 /* It's possible the list consisted of nothing but whitespace. */
1418 if (parser->lex_from_node == NULL) {
1419 talloc_free (parser->lex_from_list);
1420 parser->lex_from_list = NULL;
1421 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001422}
Carl Worthb20d33c2010-05-20 22:27:07 -07001423
1424static void
1425_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1426{
1427 skip_type_t current = SKIP_NO_SKIP;
1428 skip_node_t *node;
1429
1430 if (parser->skip_stack)
1431 current = parser->skip_stack->type;
1432
1433 node = xtalloc (parser, skip_node_t);
1434
1435 if (current == SKIP_NO_SKIP) {
1436 if (condition)
1437 node->type = SKIP_NO_SKIP;
1438 else
1439 node->type = SKIP_TO_ELSE;
1440 } else {
1441 node->type = SKIP_TO_ENDIF;
1442 }
1443
1444 node->next = parser->skip_stack;
1445 parser->skip_stack = node;
1446}
1447
1448static void
1449_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1450 int condition)
1451{
1452 if (parser->skip_stack == NULL) {
1453 fprintf (stderr, "Error: %s without #if\n", type);
1454 exit (1);
1455 }
1456
1457 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1458 if (condition)
1459 parser->skip_stack->type = SKIP_NO_SKIP;
1460 } else {
1461 parser->skip_stack->type = SKIP_TO_ENDIF;
1462 }
1463}
Carl Worth80dc60b2010-05-25 14:42:00 -07001464
Carl Worthb20d33c2010-05-20 22:27:07 -07001465static void
1466_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1467{
1468 skip_node_t *node;
1469
1470 if (parser->skip_stack == NULL) {
1471 fprintf (stderr, "Error: #endif without #if\n");
1472 exit (1);
1473 }
1474
1475 node = parser->skip_stack;
1476 parser->skip_stack = node->next;
1477 talloc_free (node);
1478}