blob: 01ca08ec740864cca426df8f1f2350cf95f0ba9c [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| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700429| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700430| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700431| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700432;
433
Carl Worth33cc4002010-05-12 12:17:10 -0700434%%
435
Carl Worth610053b2010-05-14 10:05:11 -0700436string_list_t *
437_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700438{
Carl Worth610053b2010-05-14 10:05:11 -0700439 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700440
Carl Worth610053b2010-05-14 10:05:11 -0700441 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700442 list->head = NULL;
443 list->tail = NULL;
444
445 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700446}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700447
Carl Worth33cc4002010-05-12 12:17:10 -0700448void
Carl Worth610053b2010-05-14 10:05:11 -0700449_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700450{
451 if (list->head == NULL) {
452 list->head = tail->head;
453 } else {
454 list->tail->next = tail->head;
455 }
456
457 list->tail = tail->tail;
458}
459
460void
Carl Worth610053b2010-05-14 10:05:11 -0700461_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700462{
Carl Worth610053b2010-05-14 10:05:11 -0700463 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700464
Carl Worth610053b2010-05-14 10:05:11 -0700465 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700466 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700467
Carl Worth33cc4002010-05-12 12:17:10 -0700468 node->next = NULL;
469
470 if (list->head == NULL) {
471 list->head = node;
472 } else {
473 list->tail->next = node;
474 }
475
476 list->tail = node;
477}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700478
Carl Worthae6517f2010-05-25 15:24:59 -0700479void
480_string_list_push (string_list_t *list, const char *str)
481{
482 string_node_t *node;
483
484 node = xtalloc (list, string_node_t);
485 node->str = xtalloc_strdup (node, str);
486 node->next = list->head;
487
488 if (list->tail == NULL) {
489 list->tail = node;
490 }
491 list->head = node;
492}
493
494void
495_string_list_pop (string_list_t *list)
496{
497 string_node_t *node;
498
499 node = list->head;
500
501 if (node == NULL) {
502 fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
503 exit (1);
504 }
505
506 list->head = node->next;
507 if (list->tail == node) {
508 assert (node->next == NULL);
509 list->tail = NULL;
510 }
511
512 talloc_free (node);
513}
514
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700515int
Carl Worth610053b2010-05-14 10:05:11 -0700516_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700517{
Carl Worth610053b2010-05-14 10:05:11 -0700518 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700519 int i;
520
521 if (list == NULL)
522 return 0;
523
524 for (i = 0, node = list->head; node; i++, node = node->next) {
525 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700526 if (index)
527 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700528 return 1;
529 }
530 }
531
532 return 0;
533}
534
535int
Carl Worth610053b2010-05-14 10:05:11 -0700536_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700537{
538 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700539 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700540
541 if (list == NULL)
542 return 0;
543
544 for (node = list->head; node; node = node->next)
545 length++;
546
547 return length;
548}
549
Carl Worth8f6a8282010-05-14 10:44:19 -0700550argument_list_t *
551_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700552{
Carl Worth8f6a8282010-05-14 10:44:19 -0700553 argument_list_t *list;
554
555 list = xtalloc (ctx, argument_list_t);
556 list->head = NULL;
557 list->tail = NULL;
558
559 return list;
560}
561
562void
Carl Worth47252442010-05-19 13:54:37 -0700563_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700564{
565 argument_node_t *node;
566
Carl Worth8f6a8282010-05-14 10:44:19 -0700567 node = xtalloc (list, argument_node_t);
568 node->argument = argument;
569
570 node->next = NULL;
571
572 if (list->head == NULL) {
573 list->head = node;
574 } else {
575 list->tail->next = node;
576 }
577
578 list->tail = node;
579}
580
581int
582_argument_list_length (argument_list_t *list)
583{
584 int length = 0;
585 argument_node_t *node;
586
587 if (list == NULL)
588 return 0;
589
590 for (node = list->head; node; node = node->next)
591 length++;
592
593 return length;
594}
595
Carl Worth47252442010-05-19 13:54:37 -0700596token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700597_argument_list_member_at (argument_list_t *list, int index)
598{
599 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700600 int i;
601
602 if (list == NULL)
603 return NULL;
604
605 node = list->head;
606 for (i = 0; i < index; i++) {
607 node = node->next;
608 if (node == NULL)
609 break;
610 }
611
612 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700613 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700614
615 return NULL;
616}
Carl Worth47252442010-05-19 13:54:37 -0700617
Carl Worth808401f2010-05-25 14:52:43 -0700618/* Note: This function talloc_steal()s the str pointer. */
619token_t *
620_token_create_str (void *ctx, int type, char *str)
621{
622 token_t *token;
623
624 token = xtalloc (ctx, token_t);
625 token->type = type;
626 token->value.str = talloc_steal (token, str);
627
628 return token;
629}
630
631token_t *
632_token_create_ival (void *ctx, int type, int ival)
633{
634 token_t *token;
635
636 token = xtalloc (ctx, token_t);
637 token->type = type;
638 token->value.ival = ival;
639
640 return token;
641}
642
Carl Worth47252442010-05-19 13:54:37 -0700643token_list_t *
644_token_list_create (void *ctx)
645{
646 token_list_t *list;
647
648 list = xtalloc (ctx, token_list_t);
649 list->head = NULL;
650 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700651 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700652
653 return list;
654}
655
656void
Carl Worth808401f2010-05-25 14:52:43 -0700657_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700658{
659 token_node_t *node;
660
661 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700662 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700663
664 node->next = NULL;
665
666 if (list->head == NULL) {
667 list->head = node;
668 } else {
669 list->tail->next = node;
670 }
671
672 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700673 if (token->type != SPACE)
674 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700675}
676
677void
678_token_list_append_list (token_list_t *list, token_list_t *tail)
679{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700680 if (tail == NULL || tail->head == NULL)
681 return;
682
Carl Worth47252442010-05-19 13:54:37 -0700683 if (list->head == NULL) {
684 list->head = tail->head;
685 } else {
686 list->tail->next = tail->head;
687 }
688
689 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700690 list->non_space_tail = tail->non_space_tail;
691}
692
Carl Worth681afbc2010-05-28 15:06:02 -0700693token_list_t *
694_token_list_copy (void *ctx, token_list_t *other)
695{
696 token_list_t *copy;
697 token_node_t *node;
698
699 if (other == NULL)
700 return NULL;
701
702 copy = _token_list_create (ctx);
703 for (node = other->head; node; node = node->next)
704 _token_list_append (copy, node->token);
705
706 return copy;
707}
708
Carl Worth10ae4382010-05-25 20:35:01 -0700709void
710_token_list_trim_trailing_space (token_list_t *list)
711{
712 token_node_t *tail, *next;
713
714 if (list->non_space_tail) {
715 tail = list->non_space_tail->next;
716 list->non_space_tail->next = NULL;
717 list->tail = list->non_space_tail;
718
719 while (tail) {
720 next = tail->next;
721 talloc_free (tail);
722 tail = next;
723 }
724 }
Carl Worth47252442010-05-19 13:54:37 -0700725}
Carl Worth80dc60b2010-05-25 14:42:00 -0700726
Carl Worth0197e9b2010-05-26 08:05:19 -0700727static void
728_token_print (token_t *token)
729{
730 if (token->type < 256) {
731 printf ("%c", token->type);
732 return;
733 }
734
735 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700736 case INTEGER:
737 printf ("%" PRIxMAX, token->value.ival);
738 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700739 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700740 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700741 case OTHER:
742 printf ("%s", token->value.str);
743 break;
744 case SPACE:
745 printf (" ");
746 break;
747 case LEFT_SHIFT:
748 printf ("<<");
749 break;
750 case RIGHT_SHIFT:
751 printf (">>");
752 break;
753 case LESS_OR_EQUAL:
754 printf ("<=");
755 break;
756 case GREATER_OR_EQUAL:
757 printf (">=");
758 break;
759 case EQUAL:
760 printf ("==");
761 break;
762 case NOT_EQUAL:
763 printf ("!=");
764 break;
765 case AND:
766 printf ("&&");
767 break;
768 case OR:
769 printf ("||");
770 break;
771 case PASTE:
772 printf ("##");
773 break;
Carl Worthdd749002010-05-27 10:12:33 -0700774 case COMMA_FINAL:
775 printf (",");
776 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700777 case PLACEHOLDER:
778 /* Nothing to print. */
779 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700780 default:
781 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
782 break;
783 }
784}
785
Carl Worthad0dee62010-05-26 09:04:50 -0700786/* Change 'token' into a new token formed by pasting 'other'. */
787static void
788_token_paste (token_t *token, token_t *other)
789{
Carl Worth85b50e82010-05-27 14:01:18 -0700790 /* Pasting a placeholder onto anything makes no change. */
791 if (other->type == PLACEHOLDER)
792 return;
793
794 /* When 'token' is a placeholder, just return contents of 'other'. */
795 if (token->type == PLACEHOLDER) {
796 token->type = other->type;
797 token->value = other->value;
798 return;
799 }
800
Carl Worthad0dee62010-05-26 09:04:50 -0700801 /* A very few single-character punctuators can be combined
802 * with another to form a multi-character punctuator. */
803 switch (token->type) {
804 case '<':
805 if (other->type == '<') {
806 token->type = LEFT_SHIFT;
807 token->value.ival = LEFT_SHIFT;
808 return;
809 } else if (other->type == '=') {
810 token->type = LESS_OR_EQUAL;
811 token->value.ival = LESS_OR_EQUAL;
812 return;
813 }
814 break;
815 case '>':
816 if (other->type == '>') {
817 token->type = RIGHT_SHIFT;
818 token->value.ival = RIGHT_SHIFT;
819 return;
820 } else if (other->type == '=') {
821 token->type = GREATER_OR_EQUAL;
822 token->value.ival = GREATER_OR_EQUAL;
823 return;
824 }
825 break;
826 case '=':
827 if (other->type == '=') {
828 token->type = EQUAL;
829 token->value.ival = EQUAL;
830 return;
831 }
832 break;
833 case '!':
834 if (other->type == '=') {
835 token->type = NOT_EQUAL;
836 token->value.ival = NOT_EQUAL;
837 return;
838 }
839 break;
840 case '&':
841 if (other->type == '&') {
842 token->type = AND;
843 token->value.ival = AND;
844 return;
845 }
846 break;
847 case '|':
848 if (other->type == '|') {
849 token->type = OR;
850 token->value.ival = OR;
851 return;
852 }
853 break;
854 }
855
856 /* Two string-valued tokens can usually just be mashed
857 * together.
858 *
Carl Worth050e3de2010-05-27 14:36:29 -0700859 * XXX: This isn't actually legitimate. Several things here
860 * should result in a diagnostic since the result cannot be a
861 * valid, single pre-processing token. For example, pasting
862 * "123" and "abc" is not legal, but we don't catch that
863 * here. */
864 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
865 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700866 {
867 token->value.str = talloc_strdup_append (token->value.str,
868 other->value.str);
869 return;
870 }
871
872 printf ("Error: Pasting \"");
873 _token_print (token);
874 printf ("\" and \"");
875 _token_print (other);
876 printf ("\" does not give a valid preprocessing token.\n");
877}
878
Carl Worth0197e9b2010-05-26 08:05:19 -0700879static void
880_token_list_print (token_list_t *list)
881{
882 token_node_t *node;
883
884 if (list == NULL)
885 return;
886
887 for (node = list->head; node; node = node->next)
888 _token_print (node->token);
889}
890
Carl Worth3a37b872010-05-10 11:44:09 -0700891void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700892yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700893{
894 fprintf (stderr, "Parse error: %s\n", error);
895}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700896
Carl Worth33cc4002010-05-12 12:17:10 -0700897glcpp_parser_t *
898glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700899{
Carl Worth33cc4002010-05-12 12:17:10 -0700900 glcpp_parser_t *parser;
901
Carl Worth5070a202010-05-12 12:45:33 -0700902 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700903
Carl Worth8f38aff2010-05-19 10:01:29 -0700904 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700905 parser->defines = hash_table_ctor (32, hash_table_string_hash,
906 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700907 parser->active = _string_list_create (parser);
Carl Worthf34a0002010-05-25 16:59:02 -0700908 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700909 parser->newline_as_space = 0;
910 parser->in_control_line = 0;
911 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700912
Carl Worthb20d33c2010-05-20 22:27:07 -0700913 parser->skip_stack = NULL;
914
Carl Worth8e82fcb2010-05-26 11:15:21 -0700915 parser->lex_from_list = NULL;
916 parser->lex_from_node = NULL;
917
Carl Worth33cc4002010-05-12 12:17:10 -0700918 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700919}
920
921int
922glcpp_parser_parse (glcpp_parser_t *parser)
923{
924 return yyparse (parser);
925}
926
927void
Carl Worth33cc4002010-05-12 12:17:10 -0700928glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700929{
Carl Worthb20d33c2010-05-20 22:27:07 -0700930 if (parser->skip_stack)
931 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700932 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700933 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700934 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700935}
Carl Worthc6d5af32010-05-11 12:30:09 -0700936
Carl Worth8e82fcb2010-05-26 11:15:21 -0700937/* Replace any occurences of DEFINED tokens in 'list' with either a
938 * '0' or '1' INTEGER token depending on whether the next token in the
939 * list is defined or not. */
940static void
941_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
942 token_list_t *list)
943{
944 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700945 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700946
947 if (list == NULL)
948 return;
949
950 for (node = list->head; node; node = node->next) {
951 if (node->token->type != DEFINED)
952 continue;
953 next = node->next;
954 while (next && next->token->type == SPACE)
955 next = next->next;
956 if (next == NULL || next->token->type != IDENTIFIER) {
957 fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
958 exit (1);
959 }
960 macro = hash_table_find (parser->defines,
961 next->token->value.str);
962
963 node->token->type = INTEGER;
964 node->token->value.ival = (macro != NULL);
965 node->next = next->next;
966 }
967}
968
Carl Worthb1854fd2010-05-25 16:28:26 -0700969typedef enum function_status
970{
971 FUNCTION_STATUS_SUCCESS,
972 FUNCTION_NOT_A_FUNCTION,
973 FUNCTION_UNBALANCED_PARENTHESES
974} function_status_t;
975
976/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700977 * balanced set of parentheses.
978 *
979 * When called, 'node' should be the opening-parenthesis token, (or
980 * perhaps preceeding SPACE tokens). Upon successful return *last will
981 * be the last consumed node, (corresponding to the closing right
982 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700983 *
984 * Return values:
985 *
986 * FUNCTION_STATUS_SUCCESS:
987 *
988 * Successfully parsed a set of function arguments.
989 *
990 * FUNCTION_NOT_A_FUNCTION:
991 *
992 * Macro name not followed by a '('. This is not an error, but
993 * simply that the macro name should be treated as a non-macro.
994 *
995 * FUNCTION_UNBLANCED_PARENTHESES
996 *
997 * Macro name is not followed by a balanced set of parentheses.
998 */
999static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -07001000_arguments_parse (argument_list_t *arguments,
1001 token_node_t *node,
1002 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001003{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001004 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001005 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001006
Carl Worthb1854fd2010-05-25 16:28:26 -07001007 node = node->next;
1008
1009 /* Ignore whitespace before first parenthesis. */
1010 while (node && node->token->type == SPACE)
1011 node = node->next;
1012
1013 if (node == NULL || node->token->type != '(')
1014 return FUNCTION_NOT_A_FUNCTION;
1015
Carl Worth652fa272010-05-25 17:45:22 -07001016 node = node->next;
1017
Carl Wortha19297b2010-05-27 13:29:19 -07001018 argument = _token_list_create (arguments);
1019 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001020
Carl Worth681afbc2010-05-28 15:06:02 -07001021 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001022 if (node->token->type == '(')
1023 {
1024 paren_count++;
1025 }
1026 else if (node->token->type == ')')
1027 {
1028 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001029 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001030 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001031 }
Carl Worth652fa272010-05-25 17:45:22 -07001032
1033 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001034 paren_count == 1)
1035 {
Carl Wortha19297b2010-05-27 13:29:19 -07001036 _token_list_trim_trailing_space (argument);
1037 argument = _token_list_create (arguments);
1038 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001039 }
1040 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001041 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001042 /* Don't treat initial whitespace as
1043 * part of the arguement. */
1044 if (node->token->type == SPACE)
1045 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001046 }
1047 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001048 }
Carl Worthc7581c22010-05-25 17:41:07 -07001049 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001050
Carl Worth681afbc2010-05-28 15:06:02 -07001051 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001052 return FUNCTION_UNBALANCED_PARENTHESES;
1053
Carl Worth681afbc2010-05-28 15:06:02 -07001054 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001055
1056 return FUNCTION_STATUS_SUCCESS;
1057}
1058
Carl Worth681afbc2010-05-28 15:06:02 -07001059/* This is a helper function that's essentially part of the
1060 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1061 * except for by that function.
1062 *
1063 * Returns NULL if node is a simple token with no expansion, (that is,
1064 * although 'node' corresponds to an identifier defined as a
1065 * function-like macro, it is not followed with a parenthesized
1066 * argument list).
1067 *
1068 * Compute the complete expansion of node (which is a function-like
1069 * macro) and subsequent nodes which are arguments.
1070 *
1071 * Returns the token list that results from the expansion and sets
1072 * *last to the last node in the list that was consumed by the
1073 * expansion. Specificallty, *last will be set as follows: as the
1074 * token of the closing right parenthesis.
1075 */
1076static token_list_t *
1077_glcpp_parser_expand_function (glcpp_parser_t *parser,
1078 token_node_t *node,
1079 token_node_t **last)
1080
Carl Worthb1854fd2010-05-25 16:28:26 -07001081{
1082 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001083 const char *identifier;
1084 argument_list_t *arguments;
1085 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001086 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001087 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001088
Carl Worthb1854fd2010-05-25 16:28:26 -07001089 identifier = node->token->value.str;
1090
1091 macro = hash_table_find (parser->defines, identifier);
1092
1093 assert (macro->is_function);
1094
Carl Worth9ce18cf2010-05-25 17:32:21 -07001095 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001096 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001097
1098 switch (status) {
1099 case FUNCTION_STATUS_SUCCESS:
1100 break;
1101 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001102 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001103 case FUNCTION_UNBALANCED_PARENTHESES:
Carl Worth681afbc2010-05-28 15:06:02 -07001104 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001105 }
1106
Carl Worth9ce18cf2010-05-25 17:32:21 -07001107 if (macro->replacements == NULL) {
1108 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001109 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001110 }
1111
Carl Wortha19297b2010-05-27 13:29:19 -07001112 if (! ((_argument_list_length (arguments) ==
1113 _string_list_length (macro->parameters)) ||
1114 (_string_list_length (macro->parameters) == 0 &&
1115 _argument_list_length (arguments) == 1 &&
1116 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001117 {
1118 fprintf (stderr,
1119 "Error: macro %s invoked with %d arguments (expected %d)\n",
1120 identifier,
1121 _argument_list_length (arguments),
1122 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001123 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001124 }
1125
Carl Worth0197e9b2010-05-26 08:05:19 -07001126 /* Perform argument substitution on the replacement list. */
1127 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001128
Carl Worthce540f22010-05-26 08:25:44 -07001129 for (node = macro->replacements->head; node; node = node->next)
1130 {
1131 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001132 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001133 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001134 &parameter_index))
1135 {
1136 token_list_t *argument;
1137 argument = _argument_list_member_at (arguments,
1138 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001139 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001140 * tokens, or append a placeholder token for
1141 * an empty argument. */
1142 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001143 _glcpp_parser_expand_token_list (parser,
1144 argument);
1145 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001146 } else {
1147 token_t *new_token;
1148
1149 new_token = _token_create_ival (substituted,
1150 PLACEHOLDER,
1151 PLACEHOLDER);
1152 _token_list_append (substituted, new_token);
1153 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001154 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001155 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001156 }
1157 }
1158
Carl Worthad0dee62010-05-26 09:04:50 -07001159 /* After argument substitution, and before further expansion
1160 * below, implement token pasting. */
1161
1162 node = substituted->head;
1163 while (node)
1164 {
1165 token_node_t *next_non_space;
1166
1167 /* Look ahead for a PASTE token, skipping space. */
1168 next_non_space = node->next;
1169 while (next_non_space && next_non_space->token->type == SPACE)
1170 next_non_space = next_non_space->next;
1171
1172 if (next_non_space == NULL)
1173 break;
1174
1175 if (next_non_space->token->type != PASTE) {
1176 node = next_non_space;
1177 continue;
1178 }
1179
1180 /* Now find the next non-space token after the PASTE. */
1181 next_non_space = next_non_space->next;
1182 while (next_non_space && next_non_space->token->type == SPACE)
1183 next_non_space = next_non_space->next;
1184
1185 if (next_non_space == NULL) {
1186 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001187 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001188 }
1189
1190 _token_paste (node->token, next_non_space->token);
1191 node->next = next_non_space->next;
1192
1193 node = node->next;
1194 }
1195
Carl Worthae6517f2010-05-25 15:24:59 -07001196 _string_list_push (parser->active, identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001197 _glcpp_parser_expand_token_list (parser, substituted);
Carl Worthae6517f2010-05-25 15:24:59 -07001198 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001199
Carl Worth681afbc2010-05-28 15:06:02 -07001200 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001201}
1202
Carl Worth681afbc2010-05-28 15:06:02 -07001203/* Compute the complete expansion of node, (and subsequent nodes after
1204 * 'node' in the case that 'node' is a function-like macro and
1205 * subsequent nodes are arguments).
1206 *
1207 * Returns NULL if node is a simple token with no expansion.
1208 *
1209 * Otherwise, returns the token list that results from the expansion
1210 * and sets *last to the last node in the list that was consumed by
1211 * the expansion. Specificallty, *last will be set as follows:
1212 *
1213 * As 'node' in the case of object-like macro expansion.
1214 *
1215 * As the token of the closing right parenthesis in the case of
1216 * function-like macro expansion.
1217 */
1218static token_list_t *
1219_glcpp_parser_expand_node (glcpp_parser_t *parser,
1220 token_node_t *node,
1221 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001222{
Carl Worth681afbc2010-05-28 15:06:02 -07001223 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001224 const char *identifier;
1225 macro_t *macro;
1226 token_list_t *expansion;
1227
1228 /* We only expand identifiers */
1229 if (token->type != IDENTIFIER) {
1230 /* We change any COMMA into a COMMA_FINAL to prevent
1231 * it being mistaken for an argument separator
1232 * later. */
1233 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001234 token->type = COMMA_FINAL;
1235 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001236 }
Carl Worth681afbc2010-05-28 15:06:02 -07001237
1238 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001239 }
1240
1241 /* Look up this identifier in the hash table. */
1242 identifier = token->value.str;
1243 macro = hash_table_find (parser->defines, identifier);
1244
Carl Worth681afbc2010-05-28 15:06:02 -07001245 /* Not a macro, so no expansion needed. */
1246 if (macro == NULL)
1247 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001248
1249 /* Finally, don't expand this macro if we're already actively
1250 * expanding it, (to avoid infinite recursion). */
Carl Worth681afbc2010-05-28 15:06:02 -07001251 if (_string_list_contains (parser->active, identifier, NULL)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001252 /* We change the token type here from IDENTIFIER to
1253 * OTHER to prevent any future expansion of this
1254 * unexpanded token. */
1255 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001256 token_list_t *expansion;
1257 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001258
Carl Worth681afbc2010-05-28 15:06:02 -07001259 str = xtalloc_strdup (parser, token->value.str);
1260 final = _token_create_str (parser, OTHER, str);
1261 expansion = _token_list_create (parser);
1262 _token_list_append (expansion, final);
1263 *last = node;
1264 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001265 }
1266
Carl Worth681afbc2010-05-28 15:06:02 -07001267 if (! macro->is_function)
1268 {
1269 *last = node;
1270
1271 if (macro->replacements == NULL)
1272 return _token_list_create (parser);
1273
1274 expansion = _token_list_copy (parser, macro->replacements);
1275
Carl Worth3c93d392010-05-28 08:17:46 -07001276 _string_list_push (parser->active, identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001277 _glcpp_parser_expand_token_list (parser, expansion);
Carl Worth3c93d392010-05-28 08:17:46 -07001278 _string_list_pop (parser->active);
Carl Worth681afbc2010-05-28 15:06:02 -07001279
1280 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001281 }
Carl Worth681afbc2010-05-28 15:06:02 -07001282
1283 return _glcpp_parser_expand_function (parser, node, last);
1284}
1285
1286/* Walk over the token list replacing nodes with their expansion.
1287 * Whenever nodes are expanded the walking will walk over the new
1288 * nodes, continuing to expand as necessary. The results are placed in
1289 * 'list' itself;
1290 */
1291static void
1292_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1293 token_list_t *list)
1294{
1295 token_node_t *node_prev;
1296 token_node_t *node, *last;
1297 token_list_t *expansion;
1298
1299 if (list == NULL)
1300 return;
1301
1302 _token_list_trim_trailing_space (list);
1303
1304 node_prev = NULL;
1305 node = list->head;
1306
1307 while (node) {
1308 /* Find the expansion for node, which will replace all
1309 * nodes from node to last, inclusive. */
1310 expansion = _glcpp_parser_expand_node (parser, node, &last);
1311 if (expansion) {
1312 /* Splice expansion into list, supporting a
1313 * simple deletion if the expansion is
1314 * empty. */
1315 if (expansion->head) {
1316 if (node_prev)
1317 node_prev->next = expansion->head;
1318 else
1319 list->head = expansion->head;
1320 expansion->tail->next = last->next;
1321 if (last == list->tail)
1322 list->tail = expansion->tail;
1323 } else {
1324 if (node_prev)
1325 node_prev->next = last->next;
1326 else
1327 list->head = last->next;
1328 if (last == list->tail)
1329 list->tail == NULL;
1330 }
1331 } else {
1332 node_prev = node;
1333 }
1334 node = node_prev ? node_prev->next : list->head;
1335 }
1336
1337 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001338}
1339
Carl Worth0197e9b2010-05-26 08:05:19 -07001340static void
1341_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
1342 token_list_t *list,
1343 token_list_t *result)
1344{
Carl Worth681afbc2010-05-28 15:06:02 -07001345 _glcpp_parser_expand_token_list (parser, list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001346
Carl Worth681afbc2010-05-28 15:06:02 -07001347 _token_list_append_list (result, list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001348}
1349
Carl Worthae6517f2010-05-25 15:24:59 -07001350void
1351_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1352 token_list_t *list)
1353{
Carl Worthae6517f2010-05-25 15:24:59 -07001354 if (list == NULL)
1355 return;
1356
Carl Worth681afbc2010-05-28 15:06:02 -07001357 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001358
Carl Worth681afbc2010-05-28 15:06:02 -07001359 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001360
Carl Worth681afbc2010-05-28 15:06:02 -07001361 _token_list_print (list);
Carl Worthae6517f2010-05-25 15:24:59 -07001362}
1363
1364void
Carl Worthfcbbb462010-05-13 09:36:23 -07001365_define_object_macro (glcpp_parser_t *parser,
1366 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001367 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001368{
1369 macro_t *macro;
1370
1371 macro = xtalloc (parser, macro_t);
1372
1373 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001374 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001375 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001376 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001377
1378 hash_table_insert (parser->defines, macro, identifier);
1379}
1380
1381void
1382_define_function_macro (glcpp_parser_t *parser,
1383 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001384 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001385 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001386{
1387 macro_t *macro;
1388
1389 macro = xtalloc (parser, macro_t);
1390
1391 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001392 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001393 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001394 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001395
1396 hash_table_insert (parser->defines, macro, identifier);
1397}
1398
Carl Worth8f38aff2010-05-19 10:01:29 -07001399static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001400glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001401{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001402 token_node_t *node;
1403 int ret;
1404
Carl Worth95951ea2010-05-26 15:57:10 -07001405 if (parser->lex_from_list == NULL) {
1406 ret = glcpp_lex (parser->scanner);
1407
1408 /* XXX: This ugly block of code exists for the sole
1409 * purpose of converting a NEWLINE token into a SPACE
1410 * token, but only in the case where we have seen a
1411 * function-like macro name, but have not yet seen its
1412 * closing parenthesis.
1413 *
1414 * There's perhaps a more compact way to do this with
1415 * mid-rule actions in the grammar.
1416 *
1417 * I'm definitely not pleased with the complexity of
1418 * this code here.
1419 */
1420 if (parser->newline_as_space)
1421 {
1422 if (ret == '(') {
1423 parser->paren_count++;
1424 } else if (ret == ')') {
1425 parser->paren_count--;
1426 if (parser->paren_count == 0)
1427 parser->newline_as_space = 0;
1428 } else if (ret == NEWLINE) {
1429 ret = SPACE;
1430 } else if (ret != SPACE) {
1431 if (parser->paren_count == 0)
1432 parser->newline_as_space = 0;
1433 }
1434 }
1435 else if (parser->in_control_line)
1436 {
1437 if (ret == NEWLINE)
1438 parser->in_control_line = 0;
1439 }
1440 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1441 ret == HASH_UNDEF || ret == HASH_IF ||
1442 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1443 ret == HASH_ELIF || ret == HASH_ELSE ||
1444 ret == HASH_ENDIF || ret == HASH)
1445 {
1446 parser->in_control_line = 1;
1447 }
1448 else if (ret == IDENTIFIER)
1449 {
1450 macro_t *macro;
1451 macro = hash_table_find (parser->defines,
1452 yylval.str);
1453 if (macro && macro->is_function) {
1454 parser->newline_as_space = 1;
1455 parser->paren_count = 0;
1456 }
1457 }
1458
1459 return ret;
1460 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001461
1462 node = parser->lex_from_node;
1463
1464 if (node == NULL) {
1465 talloc_free (parser->lex_from_list);
1466 parser->lex_from_list = NULL;
1467 return NEWLINE;
1468 }
1469
1470 yylval = node->token->value;
1471 ret = node->token->type;
1472
1473 parser->lex_from_node = node->next;
1474
1475 return ret;
1476}
1477
1478static void
1479glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1480{
1481 token_node_t *node;
1482
1483 assert (parser->lex_from_list == NULL);
1484
1485 /* Copy list, eliminating any space tokens. */
1486 parser->lex_from_list = _token_list_create (parser);
1487
1488 for (node = list->head; node; node = node->next) {
1489 if (node->token->type == SPACE)
1490 continue;
1491 _token_list_append (parser->lex_from_list, node->token);
1492 }
1493
1494 talloc_free (list);
1495
1496 parser->lex_from_node = parser->lex_from_list->head;
1497
1498 /* It's possible the list consisted of nothing but whitespace. */
1499 if (parser->lex_from_node == NULL) {
1500 talloc_free (parser->lex_from_list);
1501 parser->lex_from_list = NULL;
1502 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001503}
Carl Worthb20d33c2010-05-20 22:27:07 -07001504
1505static void
1506_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1507{
1508 skip_type_t current = SKIP_NO_SKIP;
1509 skip_node_t *node;
1510
1511 if (parser->skip_stack)
1512 current = parser->skip_stack->type;
1513
1514 node = xtalloc (parser, skip_node_t);
1515
1516 if (current == SKIP_NO_SKIP) {
1517 if (condition)
1518 node->type = SKIP_NO_SKIP;
1519 else
1520 node->type = SKIP_TO_ELSE;
1521 } else {
1522 node->type = SKIP_TO_ENDIF;
1523 }
1524
1525 node->next = parser->skip_stack;
1526 parser->skip_stack = node;
1527}
1528
1529static void
1530_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1531 int condition)
1532{
1533 if (parser->skip_stack == NULL) {
1534 fprintf (stderr, "Error: %s without #if\n", type);
1535 exit (1);
1536 }
1537
1538 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1539 if (condition)
1540 parser->skip_stack->type = SKIP_NO_SKIP;
1541 } else {
1542 parser->skip_stack->type = SKIP_TO_ENDIF;
1543 }
1544}
Carl Worth80dc60b2010-05-25 14:42:00 -07001545
Carl Worthb20d33c2010-05-20 22:27:07 -07001546static void
1547_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1548{
1549 skip_node_t *node;
1550
1551 if (parser->skip_stack == NULL) {
1552 fprintf (stderr, "Error: #endif without #if\n");
1553 exit (1);
1554 }
1555
1556 node = parser->skip_stack;
1557 parser->skip_stack = node->next;
1558 talloc_free (node);
1559}