blob: 5b2d0d3927a5e706c48b26ce775508bffff031b3 [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
951
Carl Worth0197e9b2010-05-26 08:05:19 -0700952/* Appends onto 'expansion' a non-macro token or the expansion of an
953 * object-like macro.
Carl Worthb1854fd2010-05-25 16:28:26 -0700954 *
Carl Worth0197e9b2010-05-26 08:05:19 -0700955 * Returns 0 if this token is completely processed.
Carl Worthb1854fd2010-05-25 16:28:26 -0700956 *
957 * Returns 1 in the case that 'token' is a function-like macro that
958 * needs further expansion.
959 */
960static int
Carl Worth7db24022010-05-26 17:01:57 -0700961_expand_token_onto (glcpp_parser_t *parser,
962 token_t *token,
963 token_list_t *result)
Carl Worthae6517f2010-05-25 15:24:59 -0700964{
965 const char *identifier;
966 macro_t *macro;
Carl Worth0197e9b2010-05-26 08:05:19 -0700967 token_list_t *expansion;
Carl Worthae6517f2010-05-25 15:24:59 -0700968
969 /* We only expand identifiers */
970 if (token->type != IDENTIFIER) {
Carl Worthdd749002010-05-27 10:12:33 -0700971 /* We change any COMMA into a COMMA_FINAL to prevent
972 * it being mistaken for an argument separator
973 * later. */
974 if (token->type == ',') {
975 token_t *new_token;
976
977 new_token = _token_create_ival (result, COMMA_FINAL,
978 COMMA_FINAL);
979 _token_list_append (result, new_token);
980 } else {
981 _token_list_append (result, token);
982 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700983 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700984 }
985
986 /* Look up this identifier in the hash table. */
987 identifier = token->value.str;
988 macro = hash_table_find (parser->defines, identifier);
989
Carl Worth0197e9b2010-05-26 08:05:19 -0700990 /* Not a macro, so just append. */
Carl Worthae6517f2010-05-25 15:24:59 -0700991 if (macro == NULL) {
Carl Worth0197e9b2010-05-26 08:05:19 -0700992 _token_list_append (result, token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700993 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700994 }
995
Carl Worthae6517f2010-05-25 15:24:59 -0700996 /* Finally, don't expand this macro if we're already actively
997 * expanding it, (to avoid infinite recursion). */
Carl Worthec4ada02010-05-26 08:15:49 -0700998 if (_string_list_contains (parser->active, identifier, NULL))
999 {
1000 /* We change the token type here from IDENTIFIER to
1001 * OTHER to prevent any future expansion of this
1002 * unexpanded token. */
1003 char *str;
1004 token_t *new_token;
1005
1006 str = xtalloc_strdup (result, token->value.str);
1007 new_token = _token_create_str (result, OTHER, str);
1008 _token_list_append (result, new_token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001009 return 0;
1010 }
1011
Carl Worthc0607d52010-05-26 08:01:42 -07001012 /* For function-like macros return 1 for further processing. */
1013 if (macro->is_function) {
1014 return 1;
1015 }
1016
Carl Worthb1854fd2010-05-25 16:28:26 -07001017 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -07001018 _glcpp_parser_expand_token_list_onto (parser,
1019 macro->replacements,
1020 result);
Carl Worthb1854fd2010-05-25 16:28:26 -07001021 _string_list_pop (parser->active);
1022
1023 return 0;
1024}
1025
1026typedef enum function_status
1027{
1028 FUNCTION_STATUS_SUCCESS,
1029 FUNCTION_NOT_A_FUNCTION,
1030 FUNCTION_UNBALANCED_PARENTHESES
1031} function_status_t;
1032
1033/* Find a set of function-like macro arguments by looking for a
1034 * balanced set of parentheses. Upon return *node will be the last
1035 * consumed node, such that further processing can continue with
1036 * node->next.
1037 *
1038 * Return values:
1039 *
1040 * FUNCTION_STATUS_SUCCESS:
1041 *
1042 * Successfully parsed a set of function arguments.
1043 *
1044 * FUNCTION_NOT_A_FUNCTION:
1045 *
1046 * Macro name not followed by a '('. This is not an error, but
1047 * simply that the macro name should be treated as a non-macro.
1048 *
1049 * FUNCTION_UNBLANCED_PARENTHESES
1050 *
1051 * Macro name is not followed by a balanced set of parentheses.
1052 */
1053static function_status_t
Carl Worth9ce18cf2010-05-25 17:32:21 -07001054_arguments_parse (argument_list_t *arguments, token_node_t **node_ret)
Carl Worthb1854fd2010-05-25 16:28:26 -07001055{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001056 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001057 token_node_t *node = *node_ret, *last;
1058 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001059
1060 last = node;
1061 node = node->next;
1062
1063 /* Ignore whitespace before first parenthesis. */
1064 while (node && node->token->type == SPACE)
1065 node = node->next;
1066
1067 if (node == NULL || node->token->type != '(')
1068 return FUNCTION_NOT_A_FUNCTION;
1069
Carl Worth652fa272010-05-25 17:45:22 -07001070 last = node;
1071 node = node->next;
1072
Carl Wortha19297b2010-05-27 13:29:19 -07001073 argument = _token_list_create (arguments);
1074 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001075
Carl Worth652fa272010-05-25 17:45:22 -07001076 for (paren_count = 1; node; last = node, node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001077 if (node->token->type == '(')
1078 {
1079 paren_count++;
1080 }
1081 else if (node->token->type == ')')
1082 {
1083 paren_count--;
Carl Worthc7581c22010-05-25 17:41:07 -07001084 if (paren_count == 0) {
1085 last = node;
1086 node = node->next;
1087 break;
1088 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001089 }
Carl Worth652fa272010-05-25 17:45:22 -07001090
1091 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001092 paren_count == 1)
1093 {
Carl Wortha19297b2010-05-27 13:29:19 -07001094 _token_list_trim_trailing_space (argument);
1095 argument = _token_list_create (arguments);
1096 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001097 }
1098 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001099 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001100 /* Don't treat initial whitespace as
1101 * part of the arguement. */
1102 if (node->token->type == SPACE)
1103 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001104 }
1105 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001106 }
Carl Worthc7581c22010-05-25 17:41:07 -07001107 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001108
1109 if (node && paren_count)
1110 return FUNCTION_UNBALANCED_PARENTHESES;
1111
1112 *node_ret = last;
1113
1114 return FUNCTION_STATUS_SUCCESS;
1115}
1116
1117/* Prints the expansion of *node (consuming further tokens from the
1118 * list as necessary). Upon return *node will be the last consumed
1119 * node, such that further processing can continue with node->next. */
Carl Worth7db24022010-05-26 17:01:57 -07001120static function_status_t
1121_expand_function_onto (glcpp_parser_t *parser,
1122 token_node_t **node_ret,
1123 token_list_t *result)
Carl Worthb1854fd2010-05-25 16:28:26 -07001124{
1125 macro_t *macro;
1126 token_node_t *node;
1127 const char *identifier;
1128 argument_list_t *arguments;
1129 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001130 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001131 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001132
1133 node = *node_ret;
1134 identifier = node->token->value.str;
1135
1136 macro = hash_table_find (parser->defines, identifier);
1137
1138 assert (macro->is_function);
1139
Carl Worth9ce18cf2010-05-25 17:32:21 -07001140 arguments = _argument_list_create (parser);
1141 status = _arguments_parse (arguments, node_ret);
Carl Worthb1854fd2010-05-25 16:28:26 -07001142
1143 switch (status) {
1144 case FUNCTION_STATUS_SUCCESS:
1145 break;
1146 case FUNCTION_NOT_A_FUNCTION:
Carl Worth0197e9b2010-05-26 08:05:19 -07001147 _token_list_append (result, node->token);
Carl Worth7db24022010-05-26 17:01:57 -07001148 return FUNCTION_NOT_A_FUNCTION;
Carl Worthb1854fd2010-05-25 16:28:26 -07001149 case FUNCTION_UNBALANCED_PARENTHESES:
1150 fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n",
1151 identifier);
1152 exit (1);
Carl Worthae6517f2010-05-25 15:24:59 -07001153 }
1154
Carl Worth9ce18cf2010-05-25 17:32:21 -07001155 if (macro->replacements == NULL) {
1156 talloc_free (arguments);
Carl Worth7db24022010-05-26 17:01:57 -07001157 return FUNCTION_STATUS_SUCCESS;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001158 }
1159
Carl Wortha19297b2010-05-27 13:29:19 -07001160 if (! ((_argument_list_length (arguments) ==
1161 _string_list_length (macro->parameters)) ||
1162 (_string_list_length (macro->parameters) == 0 &&
1163 _argument_list_length (arguments) == 1 &&
1164 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001165 {
1166 fprintf (stderr,
1167 "Error: macro %s invoked with %d arguments (expected %d)\n",
1168 identifier,
1169 _argument_list_length (arguments),
1170 _string_list_length (macro->parameters));
Carl Worth7db24022010-05-26 17:01:57 -07001171 exit (1);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001172 }
1173
Carl Worth0197e9b2010-05-26 08:05:19 -07001174 /* Perform argument substitution on the replacement list. */
1175 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001176
Carl Worthce540f22010-05-26 08:25:44 -07001177 for (node = macro->replacements->head; node; node = node->next)
1178 {
1179 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001180 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001181 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001182 &parameter_index))
1183 {
1184 token_list_t *argument;
1185 argument = _argument_list_member_at (arguments,
1186 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001187 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001188 * tokens, or append a placeholder token for
1189 * an empty argument. */
1190 if (argument->head) {
1191 _glcpp_parser_expand_token_list_onto (parser,
1192 argument,
1193 substituted);
1194 } else {
1195 token_t *new_token;
1196
1197 new_token = _token_create_ival (substituted,
1198 PLACEHOLDER,
1199 PLACEHOLDER);
1200 _token_list_append (substituted, new_token);
1201 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001202 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001203 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001204 }
1205 }
1206
Carl Worthad0dee62010-05-26 09:04:50 -07001207 /* After argument substitution, and before further expansion
1208 * below, implement token pasting. */
1209
1210 node = substituted->head;
1211 while (node)
1212 {
1213 token_node_t *next_non_space;
1214
1215 /* Look ahead for a PASTE token, skipping space. */
1216 next_non_space = node->next;
1217 while (next_non_space && next_non_space->token->type == SPACE)
1218 next_non_space = next_non_space->next;
1219
1220 if (next_non_space == NULL)
1221 break;
1222
1223 if (next_non_space->token->type != PASTE) {
1224 node = next_non_space;
1225 continue;
1226 }
1227
1228 /* Now find the next non-space token after the PASTE. */
1229 next_non_space = next_non_space->next;
1230 while (next_non_space && next_non_space->token->type == SPACE)
1231 next_non_space = next_non_space->next;
1232
1233 if (next_non_space == NULL) {
1234 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth85b50e82010-05-27 14:01:18 -07001235 return FUNCTION_STATUS_SUCCESS;
Carl Worthad0dee62010-05-26 09:04:50 -07001236 }
1237
1238 _token_paste (node->token, next_non_space->token);
1239 node->next = next_non_space->next;
1240
1241 node = node->next;
1242 }
1243
Carl Worthae6517f2010-05-25 15:24:59 -07001244 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -07001245 _glcpp_parser_expand_token_list_onto (parser, substituted, result);
Carl Worthae6517f2010-05-25 15:24:59 -07001246 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001247
1248 talloc_free (arguments);
Carl Worth7db24022010-05-26 17:01:57 -07001249
1250 return FUNCTION_STATUS_SUCCESS;
Carl Worthae6517f2010-05-25 15:24:59 -07001251}
1252
Carl Worth0197e9b2010-05-26 08:05:19 -07001253static void
1254_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
1255 token_list_t *list,
1256 token_list_t *result)
1257{
1258 token_node_t *node;
Carl Worth7db24022010-05-26 17:01:57 -07001259 token_list_t *intermediate, *list_orig = list;
1260 int i, need_rescan = 0;
Carl Worth0197e9b2010-05-26 08:05:19 -07001261
Carl Wortha65cf7b2010-05-27 11:55:36 -07001262 if (list == NULL || list->head == NULL)
Carl Worth0197e9b2010-05-26 08:05:19 -07001263 return;
1264
Carl Worth7db24022010-05-26 17:01:57 -07001265 intermediate = _token_list_create (parser);
1266
1267 /* XXX: The two-pass expansion here is really ugly. The
1268 * problem this is solving is that we can expand a macro into
1269 * a function-like macro name, and then we need to recognize
1270 * that as a function-like macro, but perhaps the parentheses
1271 * and arguments aren't on the token list yet, (since they are
1272 * in the actual content so they are part of what we are
1273 * expanding.
1274 *
1275 * This ugly hack works, but is messy, fragile, and hard to
1276 * maintain. I think a cleaner solution would separate the
1277 * notions of expanding and appending and avoid this problem
1278 * altogether.
1279 */
1280
1281 for (i = 0; i < 2; i++) {
1282 if (i == 1) {
1283 list = intermediate;
1284 intermediate = _token_list_create (parser);
Carl Worth0197e9b2010-05-26 08:05:19 -07001285 }
Carl Worth7db24022010-05-26 17:01:57 -07001286 for (node = list->head; node; node = node->next)
1287 {
1288 if (_expand_token_onto (parser, node->token,
1289 intermediate))
1290 {
1291 if (_expand_function_onto (parser, &node,
1292 intermediate))
1293 {
1294 need_rescan = 1;
1295 }
1296 }
1297 }
1298 if (list != list_orig)
1299 talloc_free (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001300 }
Carl Worth7db24022010-05-26 17:01:57 -07001301
1302 _token_list_append_list (result, intermediate);
Carl Worth0197e9b2010-05-26 08:05:19 -07001303}
1304
Carl Worthae6517f2010-05-25 15:24:59 -07001305void
1306_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1307 token_list_t *list)
1308{
Carl Worth0197e9b2010-05-26 08:05:19 -07001309 token_list_t *expanded;
Carl Worthae6517f2010-05-25 15:24:59 -07001310 token_node_t *node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001311 function_status_t function_status;
Carl Worthae6517f2010-05-25 15:24:59 -07001312
1313 if (list == NULL)
1314 return;
1315
Carl Worth0197e9b2010-05-26 08:05:19 -07001316 expanded = _token_list_create (parser);
Carl Worth10ae4382010-05-25 20:35:01 -07001317
Carl Worth0197e9b2010-05-26 08:05:19 -07001318 _glcpp_parser_expand_token_list_onto (parser, list, expanded);
1319
1320 _token_list_trim_trailing_space (expanded);
1321
1322 _token_list_print (expanded);
1323
1324 talloc_free (expanded);
Carl Worthae6517f2010-05-25 15:24:59 -07001325}
1326
1327void
Carl Worthfcbbb462010-05-13 09:36:23 -07001328_define_object_macro (glcpp_parser_t *parser,
1329 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001330 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001331{
1332 macro_t *macro;
1333
1334 macro = xtalloc (parser, macro_t);
1335
1336 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001337 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001338 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001339 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001340
1341 hash_table_insert (parser->defines, macro, identifier);
1342}
1343
1344void
1345_define_function_macro (glcpp_parser_t *parser,
1346 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001347 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001348 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001349{
1350 macro_t *macro;
1351
1352 macro = xtalloc (parser, macro_t);
1353
1354 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001355 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001356 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001357 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001358
1359 hash_table_insert (parser->defines, macro, identifier);
1360}
1361
Carl Worth8f38aff2010-05-19 10:01:29 -07001362static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001363glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001364{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001365 token_node_t *node;
1366 int ret;
1367
Carl Worth95951ea2010-05-26 15:57:10 -07001368 if (parser->lex_from_list == NULL) {
1369 ret = glcpp_lex (parser->scanner);
1370
1371 /* XXX: This ugly block of code exists for the sole
1372 * purpose of converting a NEWLINE token into a SPACE
1373 * token, but only in the case where we have seen a
1374 * function-like macro name, but have not yet seen its
1375 * closing parenthesis.
1376 *
1377 * There's perhaps a more compact way to do this with
1378 * mid-rule actions in the grammar.
1379 *
1380 * I'm definitely not pleased with the complexity of
1381 * this code here.
1382 */
1383 if (parser->newline_as_space)
1384 {
1385 if (ret == '(') {
1386 parser->paren_count++;
1387 } else if (ret == ')') {
1388 parser->paren_count--;
1389 if (parser->paren_count == 0)
1390 parser->newline_as_space = 0;
1391 } else if (ret == NEWLINE) {
1392 ret = SPACE;
1393 } else if (ret != SPACE) {
1394 if (parser->paren_count == 0)
1395 parser->newline_as_space = 0;
1396 }
1397 }
1398 else if (parser->in_control_line)
1399 {
1400 if (ret == NEWLINE)
1401 parser->in_control_line = 0;
1402 }
1403 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1404 ret == HASH_UNDEF || ret == HASH_IF ||
1405 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1406 ret == HASH_ELIF || ret == HASH_ELSE ||
1407 ret == HASH_ENDIF || ret == HASH)
1408 {
1409 parser->in_control_line = 1;
1410 }
1411 else if (ret == IDENTIFIER)
1412 {
1413 macro_t *macro;
1414 macro = hash_table_find (parser->defines,
1415 yylval.str);
1416 if (macro && macro->is_function) {
1417 parser->newline_as_space = 1;
1418 parser->paren_count = 0;
1419 }
1420 }
1421
1422 return ret;
1423 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001424
1425 node = parser->lex_from_node;
1426
1427 if (node == NULL) {
1428 talloc_free (parser->lex_from_list);
1429 parser->lex_from_list = NULL;
1430 return NEWLINE;
1431 }
1432
1433 yylval = node->token->value;
1434 ret = node->token->type;
1435
1436 parser->lex_from_node = node->next;
1437
1438 return ret;
1439}
1440
1441static void
1442glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1443{
1444 token_node_t *node;
1445
1446 assert (parser->lex_from_list == NULL);
1447
1448 /* Copy list, eliminating any space tokens. */
1449 parser->lex_from_list = _token_list_create (parser);
1450
1451 for (node = list->head; node; node = node->next) {
1452 if (node->token->type == SPACE)
1453 continue;
1454 _token_list_append (parser->lex_from_list, node->token);
1455 }
1456
1457 talloc_free (list);
1458
1459 parser->lex_from_node = parser->lex_from_list->head;
1460
1461 /* It's possible the list consisted of nothing but whitespace. */
1462 if (parser->lex_from_node == NULL) {
1463 talloc_free (parser->lex_from_list);
1464 parser->lex_from_list = NULL;
1465 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001466}
Carl Worthb20d33c2010-05-20 22:27:07 -07001467
1468static void
1469_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1470{
1471 skip_type_t current = SKIP_NO_SKIP;
1472 skip_node_t *node;
1473
1474 if (parser->skip_stack)
1475 current = parser->skip_stack->type;
1476
1477 node = xtalloc (parser, skip_node_t);
1478
1479 if (current == SKIP_NO_SKIP) {
1480 if (condition)
1481 node->type = SKIP_NO_SKIP;
1482 else
1483 node->type = SKIP_TO_ELSE;
1484 } else {
1485 node->type = SKIP_TO_ENDIF;
1486 }
1487
1488 node->next = parser->skip_stack;
1489 parser->skip_stack = node;
1490}
1491
1492static void
1493_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1494 int condition)
1495{
1496 if (parser->skip_stack == NULL) {
1497 fprintf (stderr, "Error: %s without #if\n", type);
1498 exit (1);
1499 }
1500
1501 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1502 if (condition)
1503 parser->skip_stack->type = SKIP_NO_SKIP;
1504 } else {
1505 parser->skip_stack->type = SKIP_TO_ENDIF;
1506 }
1507}
Carl Worth80dc60b2010-05-25 14:42:00 -07001508
Carl Worthb20d33c2010-05-20 22:27:07 -07001509static void
1510_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1511{
1512 skip_node_t *node;
1513
1514 if (parser->skip_stack == NULL) {
1515 fprintf (stderr, "Error: #endif without #if\n");
1516 exit (1);
1517 }
1518
1519 node = parser->skip_stack;
1520 parser->skip_stack = node->next;
1521 talloc_free (node);
1522}