blob: c89d7bf159c5cd6217987dfe07aa986cc4cf455f [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 Worth681afbc2010-05-28 15:06:02 -0700104_glcpp_parser_expand_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 Worth681afbc2010-05-28 15:06:02 -0700108_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
109 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700110
111static void
Carl Worthb20d33c2010-05-20 22:27:07 -0700112_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
113
114static void
115_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
116 int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700117
Carl Worthb20d33c2010-05-20 22:27:07 -0700118static void
119_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
120
Carl Worth0293b2e2010-05-19 10:05:40 -0700121#define yylex glcpp_parser_lex
122
Carl Worth8f38aff2010-05-19 10:01:29 -0700123static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700124glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700125
Carl Worth8e82fcb2010-05-26 11:15:21 -0700126static void
127glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
128
Carl Worth3a37b872010-05-10 11:44:09 -0700129%}
130
Carl Worth0b27b5f2010-05-10 16:16:06 -0700131%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700132%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700133
Carl Worth050e3de2010-05-27 14:36:29 -0700134%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 -0700135%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700136%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700137%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700138%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700139%type <token> preprocessing_token
140%type <token_list> pp_tokens replacement_list text_line
Carl Worth8fed1cd2010-05-26 09:32:12 -0700141%left OR
142%left AND
143%left '|'
144%left '^'
145%left '&'
146%left EQUAL NOT_EQUAL
147%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
148%left LEFT_SHIFT RIGHT_SHIFT
149%left '+' '-'
150%left '*' '/' '%'
151%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700152
153%%
154
Carl Worth33cc4002010-05-12 12:17:10 -0700155input:
Carl Worth3ff81672010-05-25 13:09:03 -0700156 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700157| input line
158;
159
160line:
161 control_line {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700162 if (parser->skip_stack == NULL ||
163 parser->skip_stack->type == SKIP_NO_SKIP)
164 {
165 printf ("\n");
166 }
Carl Worthae6517f2010-05-25 15:24:59 -0700167 }
Carl Worth808401f2010-05-25 14:52:43 -0700168| text_line {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700169 if (parser->skip_stack == NULL ||
170 parser->skip_stack->type == SKIP_NO_SKIP)
171 {
172 _glcpp_parser_print_expanded_token_list (parser, $1);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700173 printf ("\n");
Carl Worth8fed1cd2010-05-26 09:32:12 -0700174 }
Carl Worth808401f2010-05-25 14:52:43 -0700175 talloc_free ($1);
176 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700177| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700178| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700179;
180
Carl Worth8e82fcb2010-05-26 11:15:21 -0700181expanded_line:
182 IF_EXPANDED expression NEWLINE {
183 _glcpp_parser_skip_stack_push_if (parser, $2);
184 }
185| ELIF_EXPANDED expression NEWLINE {
186 _glcpp_parser_skip_stack_change_if (parser, "elif", $2);
187 }
188;
189
Carl Worth3ff81672010-05-25 13:09:03 -0700190control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700191 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Carl Worthae6517f2010-05-25 15:24:59 -0700192 _define_object_macro (parser, $2, $3);
193 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700194| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
195 _define_function_macro (parser, $2, NULL, $5);
196 }
197| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
198 _define_function_macro (parser, $2, $4, $6);
199 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700200| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700201 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700202 if (macro) {
203 /* XXX: Need hash table to support a real way
204 * to remove an element rather than prefixing
205 * a new node with data of NULL like this. */
206 hash_table_insert (parser->defines, NULL, $2);
207 talloc_free (macro);
208 }
209 talloc_free ($2);
210 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700211| HASH_IF pp_tokens NEWLINE {
212 token_list_t *expanded;
213 token_t *token;
214
215 expanded = _token_list_create (parser);
216 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
217 _token_list_append (expanded, token);
218 talloc_unlink (parser, token);
219 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700220 _glcpp_parser_expand_token_list (parser, $2);
221 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700222 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);
Carl Worth681afbc2010-05-28 15:06:02 -0700243 _glcpp_parser_expand_token_list (parser, $2);
244 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700245 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700246 }
247| HASH_ELSE NEWLINE {
248 _glcpp_parser_skip_stack_change_if (parser, "else", 1);
249 }
250| HASH_ENDIF NEWLINE {
251 _glcpp_parser_skip_stack_pop (parser);
252 }
Carl Worth3ff81672010-05-25 13:09:03 -0700253| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700254;
255
Carl Worth8fed1cd2010-05-26 09:32:12 -0700256expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700257 INTEGER_STRING {
258 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
259 $$ = strtoll ($1 + 2, NULL, 16);
260 } else if ($1[0] == '0') {
261 $$ = strtoll ($1, NULL, 8);
262 } else {
263 $$ = strtoll ($1, NULL, 10);
264 }
265 }
266| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700267 $$ = $1;
268 }
269| expression OR expression {
270 $$ = $1 || $3;
271 }
272| expression AND expression {
273 $$ = $1 && $3;
274 }
275| expression '|' expression {
276 $$ = $1 | $3;
277 }
278| expression '^' expression {
279 $$ = $1 ^ $3;
280 }
281| expression '&' expression {
282 $$ = $1 & $3;
283 }
284| expression NOT_EQUAL expression {
285 $$ = $1 != $3;
286 }
287| expression EQUAL expression {
288 $$ = $1 == $3;
289 }
290| expression GREATER_OR_EQUAL expression {
291 $$ = $1 >= $3;
292 }
293| expression LESS_OR_EQUAL expression {
294 $$ = $1 <= $3;
295 }
296| expression '>' expression {
297 $$ = $1 > $3;
298 }
299| expression '<' expression {
300 $$ = $1 < $3;
301 }
302| expression RIGHT_SHIFT expression {
303 $$ = $1 >> $3;
304 }
305| expression LEFT_SHIFT expression {
306 $$ = $1 << $3;
307 }
308| expression '-' expression {
309 $$ = $1 - $3;
310 }
311| expression '+' expression {
312 $$ = $1 + $3;
313 }
314| expression '%' expression {
315 $$ = $1 % $3;
316 }
317| expression '/' expression {
318 $$ = $1 / $3;
319 }
320| expression '*' expression {
321 $$ = $1 * $3;
322 }
323| '!' expression %prec UNARY {
324 $$ = ! $2;
325 }
326| '~' expression %prec UNARY {
327 $$ = ~ $2;
328 }
329| '-' expression %prec UNARY {
330 $$ = - $2;
331 }
332| '+' expression %prec UNARY {
333 $$ = + $2;
334 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700335| '(' expression ')' {
336 $$ = $2;
337 }
338;
339
Carl Worth3ff81672010-05-25 13:09:03 -0700340identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700341 IDENTIFIER {
342 $$ = _string_list_create (parser);
343 _string_list_append_item ($$, $1);
344 talloc_steal ($$, $1);
345 }
346| identifier_list ',' IDENTIFIER {
347 $$ = $1;
348 _string_list_append_item ($$, $3);
349 talloc_steal ($$, $3);
350 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700351;
352
Carl Worth3ff81672010-05-25 13:09:03 -0700353text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700354 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700355| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700356;
357
Carl Worth3ff81672010-05-25 13:09:03 -0700358non_directive:
359 pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700360;
361
Carl Worthaaa9acb2010-05-19 13:28:24 -0700362replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700363 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700364| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700365;
366
Carl Worthaaa9acb2010-05-19 13:28:24 -0700367pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700368 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700369 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700370 $$ = _token_list_create (parser);
371 _token_list_append ($$, $1);
372 talloc_unlink (parser, $1);
373 }
374| pp_tokens preprocessing_token {
375 $$ = $1;
376 _token_list_append ($$, $2);
377 talloc_unlink (parser, $2);
378 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700379;
380
Carl Worth3ff81672010-05-25 13:09:03 -0700381preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700382 IDENTIFIER {
383 $$ = _token_create_str (parser, IDENTIFIER, $1);
384 }
Carl Worth050e3de2010-05-27 14:36:29 -0700385| INTEGER_STRING {
386 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700387 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700388| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700389 $$ = _token_create_ival (parser, $1, $1);
390 }
391| OTHER {
392 $$ = _token_create_str (parser, OTHER, $1);
393 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700394| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700395 $$ = _token_create_ival (parser, SPACE, SPACE);
Carl Worthb1854fd2010-05-25 16:28:26 -0700396 }
Carl Worth3ff81672010-05-25 13:09:03 -0700397;
398
Carl Worth8e82fcb2010-05-26 11:15:21 -0700399operator:
Carl Worth808401f2010-05-25 14:52:43 -0700400 '[' { $$ = '['; }
401| ']' { $$ = ']'; }
402| '(' { $$ = '('; }
403| ')' { $$ = ')'; }
404| '{' { $$ = '{'; }
405| '}' { $$ = '}'; }
406| '.' { $$ = '.'; }
407| '&' { $$ = '&'; }
408| '*' { $$ = '*'; }
409| '+' { $$ = '+'; }
410| '-' { $$ = '-'; }
411| '~' { $$ = '~'; }
412| '!' { $$ = '!'; }
413| '/' { $$ = '/'; }
414| '%' { $$ = '%'; }
415| LEFT_SHIFT { $$ = LEFT_SHIFT; }
416| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
417| '<' { $$ = '<'; }
418| '>' { $$ = '>'; }
419| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
420| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
421| EQUAL { $$ = EQUAL; }
422| NOT_EQUAL { $$ = NOT_EQUAL; }
423| '^' { $$ = '^'; }
424| '|' { $$ = '|'; }
425| AND { $$ = AND; }
426| OR { $$ = OR; }
427| ';' { $$ = ';'; }
428| ',' { $$ = ','; }
429| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700430| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700431;
432
Carl Worth33cc4002010-05-12 12:17:10 -0700433%%
434
Carl Worth610053b2010-05-14 10:05:11 -0700435string_list_t *
436_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700437{
Carl Worth610053b2010-05-14 10:05:11 -0700438 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700439
Carl Worth610053b2010-05-14 10:05:11 -0700440 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700441 list->head = NULL;
442 list->tail = NULL;
443
444 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700445}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700446
Carl Worth33cc4002010-05-12 12:17:10 -0700447void
Carl Worth610053b2010-05-14 10:05:11 -0700448_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700449{
450 if (list->head == NULL) {
451 list->head = tail->head;
452 } else {
453 list->tail->next = tail->head;
454 }
455
456 list->tail = tail->tail;
457}
458
459void
Carl Worth610053b2010-05-14 10:05:11 -0700460_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700461{
Carl Worth610053b2010-05-14 10:05:11 -0700462 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700463
Carl Worth610053b2010-05-14 10:05:11 -0700464 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700465 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700466
Carl Worth33cc4002010-05-12 12:17:10 -0700467 node->next = NULL;
468
469 if (list->head == NULL) {
470 list->head = node;
471 } else {
472 list->tail->next = node;
473 }
474
475 list->tail = node;
476}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700477
Carl Worthae6517f2010-05-25 15:24:59 -0700478void
479_string_list_push (string_list_t *list, const char *str)
480{
481 string_node_t *node;
482
483 node = xtalloc (list, string_node_t);
484 node->str = xtalloc_strdup (node, str);
485 node->next = list->head;
486
487 if (list->tail == NULL) {
488 list->tail = node;
489 }
490 list->head = node;
491}
492
493void
494_string_list_pop (string_list_t *list)
495{
496 string_node_t *node;
497
498 node = list->head;
499
500 if (node == NULL) {
501 fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
502 exit (1);
503 }
504
505 list->head = node->next;
506 if (list->tail == node) {
507 assert (node->next == NULL);
508 list->tail = NULL;
509 }
510
511 talloc_free (node);
512}
513
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700514int
Carl Worth610053b2010-05-14 10:05:11 -0700515_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700516{
Carl Worth610053b2010-05-14 10:05:11 -0700517 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700518 int i;
519
520 if (list == NULL)
521 return 0;
522
523 for (i = 0, node = list->head; node; i++, node = node->next) {
524 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700525 if (index)
526 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700527 return 1;
528 }
529 }
530
531 return 0;
532}
533
534int
Carl Worth610053b2010-05-14 10:05:11 -0700535_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700536{
537 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700538 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700539
540 if (list == NULL)
541 return 0;
542
543 for (node = list->head; node; node = node->next)
544 length++;
545
546 return length;
547}
548
Carl Worth8f6a8282010-05-14 10:44:19 -0700549argument_list_t *
550_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700551{
Carl Worth8f6a8282010-05-14 10:44:19 -0700552 argument_list_t *list;
553
554 list = xtalloc (ctx, argument_list_t);
555 list->head = NULL;
556 list->tail = NULL;
557
558 return list;
559}
560
561void
Carl Worth47252442010-05-19 13:54:37 -0700562_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700563{
564 argument_node_t *node;
565
Carl Worth8f6a8282010-05-14 10:44:19 -0700566 node = xtalloc (list, argument_node_t);
567 node->argument = argument;
568
569 node->next = NULL;
570
571 if (list->head == NULL) {
572 list->head = node;
573 } else {
574 list->tail->next = node;
575 }
576
577 list->tail = node;
578}
579
580int
581_argument_list_length (argument_list_t *list)
582{
583 int length = 0;
584 argument_node_t *node;
585
586 if (list == NULL)
587 return 0;
588
589 for (node = list->head; node; node = node->next)
590 length++;
591
592 return length;
593}
594
Carl Worth47252442010-05-19 13:54:37 -0700595token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700596_argument_list_member_at (argument_list_t *list, int index)
597{
598 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700599 int i;
600
601 if (list == NULL)
602 return NULL;
603
604 node = list->head;
605 for (i = 0; i < index; i++) {
606 node = node->next;
607 if (node == NULL)
608 break;
609 }
610
611 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700612 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700613
614 return NULL;
615}
Carl Worth47252442010-05-19 13:54:37 -0700616
Carl Worth808401f2010-05-25 14:52:43 -0700617/* Note: This function talloc_steal()s the str pointer. */
618token_t *
619_token_create_str (void *ctx, int type, char *str)
620{
621 token_t *token;
622
623 token = xtalloc (ctx, token_t);
624 token->type = type;
625 token->value.str = talloc_steal (token, str);
626
627 return token;
628}
629
630token_t *
631_token_create_ival (void *ctx, int type, int ival)
632{
633 token_t *token;
634
635 token = xtalloc (ctx, token_t);
636 token->type = type;
637 token->value.ival = ival;
638
639 return token;
640}
641
Carl Worth47252442010-05-19 13:54:37 -0700642token_list_t *
643_token_list_create (void *ctx)
644{
645 token_list_t *list;
646
647 list = xtalloc (ctx, token_list_t);
648 list->head = NULL;
649 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700650 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700651
652 return list;
653}
654
655void
Carl Worth808401f2010-05-25 14:52:43 -0700656_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700657{
658 token_node_t *node;
659
660 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700661 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700662
663 node->next = NULL;
664
665 if (list->head == NULL) {
666 list->head = node;
667 } else {
668 list->tail->next = node;
669 }
670
671 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700672 if (token->type != SPACE)
673 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700674}
675
676void
677_token_list_append_list (token_list_t *list, token_list_t *tail)
678{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700679 if (tail == NULL || tail->head == NULL)
680 return;
681
Carl Worth47252442010-05-19 13:54:37 -0700682 if (list->head == NULL) {
683 list->head = tail->head;
684 } else {
685 list->tail->next = tail->head;
686 }
687
688 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700689 list->non_space_tail = tail->non_space_tail;
690}
691
Carl Worth681afbc2010-05-28 15:06:02 -0700692token_list_t *
693_token_list_copy (void *ctx, token_list_t *other)
694{
695 token_list_t *copy;
696 token_node_t *node;
697
698 if (other == NULL)
699 return NULL;
700
701 copy = _token_list_create (ctx);
702 for (node = other->head; node; node = node->next)
703 _token_list_append (copy, node->token);
704
705 return copy;
706}
707
Carl Worth10ae4382010-05-25 20:35:01 -0700708void
709_token_list_trim_trailing_space (token_list_t *list)
710{
711 token_node_t *tail, *next;
712
713 if (list->non_space_tail) {
714 tail = list->non_space_tail->next;
715 list->non_space_tail->next = NULL;
716 list->tail = list->non_space_tail;
717
718 while (tail) {
719 next = tail->next;
720 talloc_free (tail);
721 tail = next;
722 }
723 }
Carl Worth47252442010-05-19 13:54:37 -0700724}
Carl Worth80dc60b2010-05-25 14:42:00 -0700725
Carl Worth0197e9b2010-05-26 08:05:19 -0700726static void
727_token_print (token_t *token)
728{
729 if (token->type < 256) {
730 printf ("%c", token->type);
731 return;
732 }
733
734 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700735 case INTEGER:
736 printf ("%" PRIxMAX, token->value.ival);
737 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700738 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700739 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700740 case OTHER:
741 printf ("%s", token->value.str);
742 break;
743 case SPACE:
744 printf (" ");
745 break;
746 case LEFT_SHIFT:
747 printf ("<<");
748 break;
749 case RIGHT_SHIFT:
750 printf (">>");
751 break;
752 case LESS_OR_EQUAL:
753 printf ("<=");
754 break;
755 case GREATER_OR_EQUAL:
756 printf (">=");
757 break;
758 case EQUAL:
759 printf ("==");
760 break;
761 case NOT_EQUAL:
762 printf ("!=");
763 break;
764 case AND:
765 printf ("&&");
766 break;
767 case OR:
768 printf ("||");
769 break;
770 case PASTE:
771 printf ("##");
772 break;
Carl Worthdd749002010-05-27 10:12:33 -0700773 case COMMA_FINAL:
774 printf (",");
775 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700776 case PLACEHOLDER:
777 /* Nothing to print. */
778 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700779 default:
780 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
781 break;
782 }
783}
784
Carl Worthad0dee62010-05-26 09:04:50 -0700785/* Change 'token' into a new token formed by pasting 'other'. */
786static void
787_token_paste (token_t *token, token_t *other)
788{
Carl Worth85b50e82010-05-27 14:01:18 -0700789 /* Pasting a placeholder onto anything makes no change. */
790 if (other->type == PLACEHOLDER)
791 return;
792
793 /* When 'token' is a placeholder, just return contents of 'other'. */
794 if (token->type == PLACEHOLDER) {
795 token->type = other->type;
796 token->value = other->value;
797 return;
798 }
799
Carl Worthad0dee62010-05-26 09:04:50 -0700800 /* A very few single-character punctuators can be combined
801 * with another to form a multi-character punctuator. */
802 switch (token->type) {
803 case '<':
804 if (other->type == '<') {
805 token->type = LEFT_SHIFT;
806 token->value.ival = LEFT_SHIFT;
807 return;
808 } else if (other->type == '=') {
809 token->type = LESS_OR_EQUAL;
810 token->value.ival = LESS_OR_EQUAL;
811 return;
812 }
813 break;
814 case '>':
815 if (other->type == '>') {
816 token->type = RIGHT_SHIFT;
817 token->value.ival = RIGHT_SHIFT;
818 return;
819 } else if (other->type == '=') {
820 token->type = GREATER_OR_EQUAL;
821 token->value.ival = GREATER_OR_EQUAL;
822 return;
823 }
824 break;
825 case '=':
826 if (other->type == '=') {
827 token->type = EQUAL;
828 token->value.ival = EQUAL;
829 return;
830 }
831 break;
832 case '!':
833 if (other->type == '=') {
834 token->type = NOT_EQUAL;
835 token->value.ival = NOT_EQUAL;
836 return;
837 }
838 break;
839 case '&':
840 if (other->type == '&') {
841 token->type = AND;
842 token->value.ival = AND;
843 return;
844 }
845 break;
846 case '|':
847 if (other->type == '|') {
848 token->type = OR;
849 token->value.ival = OR;
850 return;
851 }
852 break;
853 }
854
855 /* Two string-valued tokens can usually just be mashed
856 * together.
857 *
Carl Worth050e3de2010-05-27 14:36:29 -0700858 * XXX: This isn't actually legitimate. Several things here
859 * should result in a diagnostic since the result cannot be a
860 * valid, single pre-processing token. For example, pasting
861 * "123" and "abc" is not legal, but we don't catch that
862 * here. */
863 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
864 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700865 {
866 token->value.str = talloc_strdup_append (token->value.str,
867 other->value.str);
868 return;
869 }
870
871 printf ("Error: Pasting \"");
872 _token_print (token);
873 printf ("\" and \"");
874 _token_print (other);
875 printf ("\" does not give a valid preprocessing token.\n");
876}
877
Carl Worth0197e9b2010-05-26 08:05:19 -0700878static void
879_token_list_print (token_list_t *list)
880{
881 token_node_t *node;
882
883 if (list == NULL)
884 return;
885
886 for (node = list->head; node; node = node->next)
887 _token_print (node->token);
888}
889
Carl Worth3a37b872010-05-10 11:44:09 -0700890void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700891yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700892{
893 fprintf (stderr, "Parse error: %s\n", error);
894}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700895
Carl Worth33cc4002010-05-12 12:17:10 -0700896glcpp_parser_t *
897glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700898{
Carl Worth33cc4002010-05-12 12:17:10 -0700899 glcpp_parser_t *parser;
900
Carl Worth5070a202010-05-12 12:45:33 -0700901 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700902
Carl Worth8f38aff2010-05-19 10:01:29 -0700903 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700904 parser->defines = hash_table_ctor (32, hash_table_string_hash,
905 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700906 parser->active = _string_list_create (parser);
Carl Worthf34a0002010-05-25 16:59:02 -0700907 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700908 parser->newline_as_space = 0;
909 parser->in_control_line = 0;
910 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700911
Carl Worthb20d33c2010-05-20 22:27:07 -0700912 parser->skip_stack = NULL;
913
Carl Worth8e82fcb2010-05-26 11:15:21 -0700914 parser->lex_from_list = NULL;
915 parser->lex_from_node = NULL;
916
Carl Worth33cc4002010-05-12 12:17:10 -0700917 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700918}
919
920int
921glcpp_parser_parse (glcpp_parser_t *parser)
922{
923 return yyparse (parser);
924}
925
926void
Carl Worth33cc4002010-05-12 12:17:10 -0700927glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700928{
Carl Worthb20d33c2010-05-20 22:27:07 -0700929 if (parser->skip_stack)
930 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700931 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700932 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700933 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700934}
Carl Worthc6d5af32010-05-11 12:30:09 -0700935
Carl Worth8e82fcb2010-05-26 11:15:21 -0700936/* Replace any occurences of DEFINED tokens in 'list' with either a
937 * '0' or '1' INTEGER token depending on whether the next token in the
938 * list is defined or not. */
939static void
940_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
941 token_list_t *list)
942{
943 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700944 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700945
946 if (list == NULL)
947 return;
948
949 for (node = list->head; node; node = node->next) {
950 if (node->token->type != DEFINED)
951 continue;
952 next = node->next;
953 while (next && next->token->type == SPACE)
954 next = next->next;
955 if (next == NULL || next->token->type != IDENTIFIER) {
956 fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
957 exit (1);
958 }
959 macro = hash_table_find (parser->defines,
960 next->token->value.str);
961
962 node->token->type = INTEGER;
963 node->token->value.ival = (macro != NULL);
964 node->next = next->next;
965 }
966}
967
Carl Worthb1854fd2010-05-25 16:28:26 -0700968typedef enum function_status
969{
970 FUNCTION_STATUS_SUCCESS,
971 FUNCTION_NOT_A_FUNCTION,
972 FUNCTION_UNBALANCED_PARENTHESES
973} function_status_t;
974
975/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700976 * balanced set of parentheses.
977 *
978 * When called, 'node' should be the opening-parenthesis token, (or
979 * perhaps preceeding SPACE tokens). Upon successful return *last will
980 * be the last consumed node, (corresponding to the closing right
981 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700982 *
983 * Return values:
984 *
985 * FUNCTION_STATUS_SUCCESS:
986 *
987 * Successfully parsed a set of function arguments.
988 *
989 * FUNCTION_NOT_A_FUNCTION:
990 *
991 * Macro name not followed by a '('. This is not an error, but
992 * simply that the macro name should be treated as a non-macro.
993 *
994 * FUNCTION_UNBLANCED_PARENTHESES
995 *
996 * Macro name is not followed by a balanced set of parentheses.
997 */
998static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700999_arguments_parse (argument_list_t *arguments,
1000 token_node_t *node,
1001 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001002{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001003 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001004 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001005
Carl Worthb1854fd2010-05-25 16:28:26 -07001006 node = node->next;
1007
1008 /* Ignore whitespace before first parenthesis. */
1009 while (node && node->token->type == SPACE)
1010 node = node->next;
1011
1012 if (node == NULL || node->token->type != '(')
1013 return FUNCTION_NOT_A_FUNCTION;
1014
Carl Worth652fa272010-05-25 17:45:22 -07001015 node = node->next;
1016
Carl Wortha19297b2010-05-27 13:29:19 -07001017 argument = _token_list_create (arguments);
1018 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001019
Carl Worth681afbc2010-05-28 15:06:02 -07001020 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001021 if (node->token->type == '(')
1022 {
1023 paren_count++;
1024 }
1025 else if (node->token->type == ')')
1026 {
1027 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001028 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001029 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001030 }
Carl Worth652fa272010-05-25 17:45:22 -07001031
1032 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001033 paren_count == 1)
1034 {
Carl Wortha19297b2010-05-27 13:29:19 -07001035 _token_list_trim_trailing_space (argument);
1036 argument = _token_list_create (arguments);
1037 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001038 }
1039 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001040 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001041 /* Don't treat initial whitespace as
1042 * part of the arguement. */
1043 if (node->token->type == SPACE)
1044 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001045 }
1046 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001047 }
Carl Worthc7581c22010-05-25 17:41:07 -07001048 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001049
Carl Worth681afbc2010-05-28 15:06:02 -07001050 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001051 return FUNCTION_UNBALANCED_PARENTHESES;
1052
Carl Worth681afbc2010-05-28 15:06:02 -07001053 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001054
1055 return FUNCTION_STATUS_SUCCESS;
1056}
1057
Carl Worth681afbc2010-05-28 15:06:02 -07001058/* This is a helper function that's essentially part of the
1059 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1060 * except for by that function.
1061 *
1062 * Returns NULL if node is a simple token with no expansion, (that is,
1063 * although 'node' corresponds to an identifier defined as a
1064 * function-like macro, it is not followed with a parenthesized
1065 * argument list).
1066 *
1067 * Compute the complete expansion of node (which is a function-like
1068 * macro) and subsequent nodes which are arguments.
1069 *
1070 * Returns the token list that results from the expansion and sets
1071 * *last to the last node in the list that was consumed by the
1072 * expansion. Specificallty, *last will be set as follows: as the
1073 * token of the closing right parenthesis.
1074 */
1075static token_list_t *
1076_glcpp_parser_expand_function (glcpp_parser_t *parser,
1077 token_node_t *node,
1078 token_node_t **last)
1079
Carl Worthb1854fd2010-05-25 16:28:26 -07001080{
1081 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001082 const char *identifier;
1083 argument_list_t *arguments;
1084 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001085 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001086 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001087
Carl Worthb1854fd2010-05-25 16:28:26 -07001088 identifier = node->token->value.str;
1089
1090 macro = hash_table_find (parser->defines, identifier);
1091
1092 assert (macro->is_function);
1093
Carl Worth9ce18cf2010-05-25 17:32:21 -07001094 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001095 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001096
1097 switch (status) {
1098 case FUNCTION_STATUS_SUCCESS:
1099 break;
1100 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001101 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001102 case FUNCTION_UNBALANCED_PARENTHESES:
Carl Worth681afbc2010-05-28 15:06:02 -07001103 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001104 }
1105
Carl Worth9ce18cf2010-05-25 17:32:21 -07001106 if (macro->replacements == NULL) {
1107 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001108 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001109 }
1110
Carl Wortha19297b2010-05-27 13:29:19 -07001111 if (! ((_argument_list_length (arguments) ==
1112 _string_list_length (macro->parameters)) ||
1113 (_string_list_length (macro->parameters) == 0 &&
1114 _argument_list_length (arguments) == 1 &&
1115 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001116 {
1117 fprintf (stderr,
1118 "Error: macro %s invoked with %d arguments (expected %d)\n",
1119 identifier,
1120 _argument_list_length (arguments),
1121 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001122 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001123 }
1124
Carl Worth0197e9b2010-05-26 08:05:19 -07001125 /* Perform argument substitution on the replacement list. */
1126 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001127
Carl Worthce540f22010-05-26 08:25:44 -07001128 for (node = macro->replacements->head; node; node = node->next)
1129 {
1130 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001131 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001132 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001133 &parameter_index))
1134 {
1135 token_list_t *argument;
1136 argument = _argument_list_member_at (arguments,
1137 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001138 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001139 * tokens, or append a placeholder token for
1140 * an empty argument. */
1141 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001142 _glcpp_parser_expand_token_list (parser,
1143 argument);
1144 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001145 } else {
1146 token_t *new_token;
1147
1148 new_token = _token_create_ival (substituted,
1149 PLACEHOLDER,
1150 PLACEHOLDER);
1151 _token_list_append (substituted, new_token);
1152 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001153 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001154 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001155 }
1156 }
1157
Carl Worthad0dee62010-05-26 09:04:50 -07001158 /* After argument substitution, and before further expansion
1159 * below, implement token pasting. */
1160
1161 node = substituted->head;
1162 while (node)
1163 {
1164 token_node_t *next_non_space;
1165
1166 /* Look ahead for a PASTE token, skipping space. */
1167 next_non_space = node->next;
1168 while (next_non_space && next_non_space->token->type == SPACE)
1169 next_non_space = next_non_space->next;
1170
1171 if (next_non_space == NULL)
1172 break;
1173
1174 if (next_non_space->token->type != PASTE) {
1175 node = next_non_space;
1176 continue;
1177 }
1178
1179 /* Now find the next non-space token after the PASTE. */
1180 next_non_space = next_non_space->next;
1181 while (next_non_space && next_non_space->token->type == SPACE)
1182 next_non_space = next_non_space->next;
1183
1184 if (next_non_space == NULL) {
1185 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001186 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001187 }
1188
1189 _token_paste (node->token, next_non_space->token);
1190 node->next = next_non_space->next;
1191
1192 node = node->next;
1193 }
1194
Carl Worthae6517f2010-05-25 15:24:59 -07001195 _string_list_push (parser->active, identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001196 _glcpp_parser_expand_token_list (parser, substituted);
Carl Worthae6517f2010-05-25 15:24:59 -07001197 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001198
Carl Worth681afbc2010-05-28 15:06:02 -07001199 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001200}
1201
Carl Worth681afbc2010-05-28 15:06:02 -07001202/* Compute the complete expansion of node, (and subsequent nodes after
1203 * 'node' in the case that 'node' is a function-like macro and
1204 * subsequent nodes are arguments).
1205 *
1206 * Returns NULL if node is a simple token with no expansion.
1207 *
1208 * Otherwise, returns the token list that results from the expansion
1209 * and sets *last to the last node in the list that was consumed by
1210 * the expansion. Specificallty, *last will be set as follows:
1211 *
1212 * As 'node' in the case of object-like macro expansion.
1213 *
1214 * As the token of the closing right parenthesis in the case of
1215 * function-like macro expansion.
1216 */
1217static token_list_t *
1218_glcpp_parser_expand_node (glcpp_parser_t *parser,
1219 token_node_t *node,
1220 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001221{
Carl Worth681afbc2010-05-28 15:06:02 -07001222 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001223 const char *identifier;
1224 macro_t *macro;
1225 token_list_t *expansion;
1226
1227 /* We only expand identifiers */
1228 if (token->type != IDENTIFIER) {
1229 /* We change any COMMA into a COMMA_FINAL to prevent
1230 * it being mistaken for an argument separator
1231 * later. */
1232 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001233 token->type = COMMA_FINAL;
1234 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001235 }
Carl Worth681afbc2010-05-28 15:06:02 -07001236
1237 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001238 }
1239
1240 /* Look up this identifier in the hash table. */
1241 identifier = token->value.str;
1242 macro = hash_table_find (parser->defines, identifier);
1243
Carl Worth681afbc2010-05-28 15:06:02 -07001244 /* Not a macro, so no expansion needed. */
1245 if (macro == NULL)
1246 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001247
1248 /* Finally, don't expand this macro if we're already actively
1249 * expanding it, (to avoid infinite recursion). */
Carl Worth681afbc2010-05-28 15:06:02 -07001250 if (_string_list_contains (parser->active, identifier, NULL)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001251 /* We change the token type here from IDENTIFIER to
1252 * OTHER to prevent any future expansion of this
1253 * unexpanded token. */
1254 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001255 token_list_t *expansion;
1256 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001257
Carl Worth681afbc2010-05-28 15:06:02 -07001258 str = xtalloc_strdup (parser, token->value.str);
1259 final = _token_create_str (parser, OTHER, str);
1260 expansion = _token_list_create (parser);
1261 _token_list_append (expansion, final);
1262 *last = node;
1263 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001264 }
1265
Carl Worth681afbc2010-05-28 15:06:02 -07001266 if (! macro->is_function)
1267 {
1268 *last = node;
1269
1270 if (macro->replacements == NULL)
1271 return _token_list_create (parser);
1272
1273 expansion = _token_list_copy (parser, macro->replacements);
1274
Carl Worth3c93d392010-05-28 08:17:46 -07001275 _string_list_push (parser->active, identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001276 _glcpp_parser_expand_token_list (parser, expansion);
Carl Worth3c93d392010-05-28 08:17:46 -07001277 _string_list_pop (parser->active);
Carl Worth681afbc2010-05-28 15:06:02 -07001278
1279 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001280 }
Carl Worth681afbc2010-05-28 15:06:02 -07001281
1282 return _glcpp_parser_expand_function (parser, node, last);
1283}
1284
1285/* Walk over the token list replacing nodes with their expansion.
1286 * Whenever nodes are expanded the walking will walk over the new
1287 * nodes, continuing to expand as necessary. The results are placed in
1288 * 'list' itself;
1289 */
1290static void
1291_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1292 token_list_t *list)
1293{
1294 token_node_t *node_prev;
1295 token_node_t *node, *last;
1296 token_list_t *expansion;
1297
1298 if (list == NULL)
1299 return;
1300
1301 _token_list_trim_trailing_space (list);
1302
1303 node_prev = NULL;
1304 node = list->head;
1305
1306 while (node) {
1307 /* Find the expansion for node, which will replace all
1308 * nodes from node to last, inclusive. */
1309 expansion = _glcpp_parser_expand_node (parser, node, &last);
1310 if (expansion) {
1311 /* Splice expansion into list, supporting a
1312 * simple deletion if the expansion is
1313 * empty. */
1314 if (expansion->head) {
1315 if (node_prev)
1316 node_prev->next = expansion->head;
1317 else
1318 list->head = expansion->head;
1319 expansion->tail->next = last->next;
1320 if (last == list->tail)
1321 list->tail = expansion->tail;
1322 } else {
1323 if (node_prev)
1324 node_prev->next = last->next;
1325 else
1326 list->head = last->next;
1327 if (last == list->tail)
1328 list->tail == NULL;
1329 }
1330 } else {
1331 node_prev = node;
1332 }
1333 node = node_prev ? node_prev->next : list->head;
1334 }
1335
1336 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001337}
1338
Carl Worth0197e9b2010-05-26 08:05:19 -07001339static void
1340_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
1341 token_list_t *list,
1342 token_list_t *result)
1343{
Carl Worth681afbc2010-05-28 15:06:02 -07001344 _glcpp_parser_expand_token_list (parser, list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001345
Carl Worth681afbc2010-05-28 15:06:02 -07001346 _token_list_append_list (result, list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001347}
1348
Carl Worthae6517f2010-05-25 15:24:59 -07001349void
1350_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1351 token_list_t *list)
1352{
Carl Worthae6517f2010-05-25 15:24:59 -07001353 if (list == NULL)
1354 return;
1355
Carl Worth681afbc2010-05-28 15:06:02 -07001356 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001357
Carl Worth681afbc2010-05-28 15:06:02 -07001358 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001359
Carl Worth681afbc2010-05-28 15:06:02 -07001360 _token_list_print (list);
Carl Worthae6517f2010-05-25 15:24:59 -07001361}
1362
1363void
Carl Worthfcbbb462010-05-13 09:36:23 -07001364_define_object_macro (glcpp_parser_t *parser,
1365 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001366 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001367{
1368 macro_t *macro;
1369
1370 macro = xtalloc (parser, macro_t);
1371
1372 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001373 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001374 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001375 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001376
1377 hash_table_insert (parser->defines, macro, identifier);
1378}
1379
1380void
1381_define_function_macro (glcpp_parser_t *parser,
1382 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001383 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001384 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001385{
1386 macro_t *macro;
1387
1388 macro = xtalloc (parser, macro_t);
1389
1390 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001391 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001392 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001393 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001394
1395 hash_table_insert (parser->defines, macro, identifier);
1396}
1397
Carl Worth8f38aff2010-05-19 10:01:29 -07001398static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001399glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001400{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001401 token_node_t *node;
1402 int ret;
1403
Carl Worth95951ea2010-05-26 15:57:10 -07001404 if (parser->lex_from_list == NULL) {
1405 ret = glcpp_lex (parser->scanner);
1406
1407 /* XXX: This ugly block of code exists for the sole
1408 * purpose of converting a NEWLINE token into a SPACE
1409 * token, but only in the case where we have seen a
1410 * function-like macro name, but have not yet seen its
1411 * closing parenthesis.
1412 *
1413 * There's perhaps a more compact way to do this with
1414 * mid-rule actions in the grammar.
1415 *
1416 * I'm definitely not pleased with the complexity of
1417 * this code here.
1418 */
1419 if (parser->newline_as_space)
1420 {
1421 if (ret == '(') {
1422 parser->paren_count++;
1423 } else if (ret == ')') {
1424 parser->paren_count--;
1425 if (parser->paren_count == 0)
1426 parser->newline_as_space = 0;
1427 } else if (ret == NEWLINE) {
1428 ret = SPACE;
1429 } else if (ret != SPACE) {
1430 if (parser->paren_count == 0)
1431 parser->newline_as_space = 0;
1432 }
1433 }
1434 else if (parser->in_control_line)
1435 {
1436 if (ret == NEWLINE)
1437 parser->in_control_line = 0;
1438 }
1439 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1440 ret == HASH_UNDEF || ret == HASH_IF ||
1441 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1442 ret == HASH_ELIF || ret == HASH_ELSE ||
1443 ret == HASH_ENDIF || ret == HASH)
1444 {
1445 parser->in_control_line = 1;
1446 }
1447 else if (ret == IDENTIFIER)
1448 {
1449 macro_t *macro;
1450 macro = hash_table_find (parser->defines,
1451 yylval.str);
1452 if (macro && macro->is_function) {
1453 parser->newline_as_space = 1;
1454 parser->paren_count = 0;
1455 }
1456 }
1457
1458 return ret;
1459 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001460
1461 node = parser->lex_from_node;
1462
1463 if (node == NULL) {
1464 talloc_free (parser->lex_from_list);
1465 parser->lex_from_list = NULL;
1466 return NEWLINE;
1467 }
1468
1469 yylval = node->token->value;
1470 ret = node->token->type;
1471
1472 parser->lex_from_node = node->next;
1473
1474 return ret;
1475}
1476
1477static void
1478glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1479{
1480 token_node_t *node;
1481
1482 assert (parser->lex_from_list == NULL);
1483
1484 /* Copy list, eliminating any space tokens. */
1485 parser->lex_from_list = _token_list_create (parser);
1486
1487 for (node = list->head; node; node = node->next) {
1488 if (node->token->type == SPACE)
1489 continue;
1490 _token_list_append (parser->lex_from_list, node->token);
1491 }
1492
1493 talloc_free (list);
1494
1495 parser->lex_from_node = parser->lex_from_list->head;
1496
1497 /* It's possible the list consisted of nothing but whitespace. */
1498 if (parser->lex_from_node == NULL) {
1499 talloc_free (parser->lex_from_list);
1500 parser->lex_from_list = NULL;
1501 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001502}
Carl Worthb20d33c2010-05-20 22:27:07 -07001503
1504static void
1505_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1506{
1507 skip_type_t current = SKIP_NO_SKIP;
1508 skip_node_t *node;
1509
1510 if (parser->skip_stack)
1511 current = parser->skip_stack->type;
1512
1513 node = xtalloc (parser, skip_node_t);
1514
1515 if (current == SKIP_NO_SKIP) {
1516 if (condition)
1517 node->type = SKIP_NO_SKIP;
1518 else
1519 node->type = SKIP_TO_ELSE;
1520 } else {
1521 node->type = SKIP_TO_ENDIF;
1522 }
1523
1524 node->next = parser->skip_stack;
1525 parser->skip_stack = node;
1526}
1527
1528static void
1529_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1530 int condition)
1531{
1532 if (parser->skip_stack == NULL) {
1533 fprintf (stderr, "Error: %s without #if\n", type);
1534 exit (1);
1535 }
1536
1537 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1538 if (condition)
1539 parser->skip_stack->type = SKIP_NO_SKIP;
1540 } else {
1541 parser->skip_stack->type = SKIP_TO_ENDIF;
1542 }
1543}
Carl Worth80dc60b2010-05-25 14:42:00 -07001544
Carl Worthb20d33c2010-05-20 22:27:07 -07001545static void
1546_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1547{
1548 skip_node_t *node;
1549
1550 if (parser->skip_stack == NULL) {
1551 fprintf (stderr, "Error: #endif without #if\n");
1552 exit (1);
1553 }
1554
1555 node = parser->skip_stack;
1556 parser->skip_stack = node->next;
1557 talloc_free (node);
1558}