blob: f4c834e038fd5da2c6ed1f46b110975d3619032c [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 Worthb06096e2010-05-29 05:54:19 -0700786/* Return a new token (talloc()ed off of 'token') formed by pasting
787 * 'token' and 'other'. Note that this function may return 'token' or
788 * 'other' directly rather than allocating anything new.
789 *
790 * Caution: Only very cursory error-checking is performed to see if
791 * the final result is a valid single token. */
792static token_t *
Carl Worthad0dee62010-05-26 09:04:50 -0700793_token_paste (token_t *token, token_t *other)
794{
Carl Worth85b50e82010-05-27 14:01:18 -0700795 /* Pasting a placeholder onto anything makes no change. */
796 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700797 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700798
Carl Worthb06096e2010-05-29 05:54:19 -0700799 /* When 'token' is a placeholder, just return 'other'. */
800 if (token->type == PLACEHOLDER)
801 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700802
Carl Worthad0dee62010-05-26 09:04:50 -0700803 /* A very few single-character punctuators can be combined
804 * with another to form a multi-character punctuator. */
805 switch (token->type) {
806 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700807 if (other->type == '<')
808 return _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
809 else if (other->type == '=')
810 return _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700811 break;
812 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700813 if (other->type == '>')
814 return _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
815 else if (other->type == '=')
816 return _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700817 break;
818 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700819 if (other->type == '=')
820 return _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700821 break;
822 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700823 if (other->type == '=')
824 return _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700825 break;
826 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700827 if (other->type == '&')
828 return _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700829 break;
830 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700831 if (other->type == '|')
832 return _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700833 break;
834 }
835
836 /* Two string-valued tokens can usually just be mashed
837 * together.
838 *
Carl Worth050e3de2010-05-27 14:36:29 -0700839 * XXX: This isn't actually legitimate. Several things here
840 * should result in a diagnostic since the result cannot be a
841 * valid, single pre-processing token. For example, pasting
842 * "123" and "abc" is not legal, but we don't catch that
843 * here. */
844 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
845 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700846 {
Carl Worthb06096e2010-05-29 05:54:19 -0700847 char *str;
848
849 str = xtalloc_asprintf (token, "%s%s",
850 token->value.str, other->value.str);
851 return _token_create_str (token, token->type, str);
Carl Worthad0dee62010-05-26 09:04:50 -0700852 }
853
854 printf ("Error: Pasting \"");
855 _token_print (token);
856 printf ("\" and \"");
857 _token_print (other);
858 printf ("\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700859
860 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700861}
862
Carl Worth0197e9b2010-05-26 08:05:19 -0700863static void
864_token_list_print (token_list_t *list)
865{
866 token_node_t *node;
867
868 if (list == NULL)
869 return;
870
871 for (node = list->head; node; node = node->next)
872 _token_print (node->token);
873}
874
Carl Worth3a37b872010-05-10 11:44:09 -0700875void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700876yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700877{
878 fprintf (stderr, "Parse error: %s\n", error);
879}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700880
Carl Worth33cc4002010-05-12 12:17:10 -0700881glcpp_parser_t *
882glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700883{
Carl Worth33cc4002010-05-12 12:17:10 -0700884 glcpp_parser_t *parser;
885
Carl Worth5070a202010-05-12 12:45:33 -0700886 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700887
Carl Worth8f38aff2010-05-19 10:01:29 -0700888 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700889 parser->defines = hash_table_ctor (32, hash_table_string_hash,
890 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700891 parser->active = _string_list_create (parser);
Carl Worthf34a0002010-05-25 16:59:02 -0700892 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700893 parser->newline_as_space = 0;
894 parser->in_control_line = 0;
895 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700896
Carl Worthb20d33c2010-05-20 22:27:07 -0700897 parser->skip_stack = NULL;
898
Carl Worth8e82fcb2010-05-26 11:15:21 -0700899 parser->lex_from_list = NULL;
900 parser->lex_from_node = NULL;
901
Carl Worth33cc4002010-05-12 12:17:10 -0700902 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700903}
904
905int
906glcpp_parser_parse (glcpp_parser_t *parser)
907{
908 return yyparse (parser);
909}
910
911void
Carl Worth33cc4002010-05-12 12:17:10 -0700912glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700913{
Carl Worthb20d33c2010-05-20 22:27:07 -0700914 if (parser->skip_stack)
915 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700916 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700917 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700918 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700919}
Carl Worthc6d5af32010-05-11 12:30:09 -0700920
Carl Worth8e82fcb2010-05-26 11:15:21 -0700921/* Replace any occurences of DEFINED tokens in 'list' with either a
922 * '0' or '1' INTEGER token depending on whether the next token in the
923 * list is defined or not. */
924static void
925_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
926 token_list_t *list)
927{
928 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700929 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700930
931 if (list == NULL)
932 return;
933
934 for (node = list->head; node; node = node->next) {
935 if (node->token->type != DEFINED)
936 continue;
937 next = node->next;
938 while (next && next->token->type == SPACE)
939 next = next->next;
940 if (next == NULL || next->token->type != IDENTIFIER) {
941 fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
942 exit (1);
943 }
944 macro = hash_table_find (parser->defines,
945 next->token->value.str);
946
947 node->token->type = INTEGER;
948 node->token->value.ival = (macro != NULL);
949 node->next = next->next;
950 }
951}
952
Carl Worthb1854fd2010-05-25 16:28:26 -0700953typedef enum function_status
954{
955 FUNCTION_STATUS_SUCCESS,
956 FUNCTION_NOT_A_FUNCTION,
957 FUNCTION_UNBALANCED_PARENTHESES
958} function_status_t;
959
960/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700961 * balanced set of parentheses.
962 *
963 * When called, 'node' should be the opening-parenthesis token, (or
964 * perhaps preceeding SPACE tokens). Upon successful return *last will
965 * be the last consumed node, (corresponding to the closing right
966 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700967 *
968 * Return values:
969 *
970 * FUNCTION_STATUS_SUCCESS:
971 *
972 * Successfully parsed a set of function arguments.
973 *
974 * FUNCTION_NOT_A_FUNCTION:
975 *
976 * Macro name not followed by a '('. This is not an error, but
977 * simply that the macro name should be treated as a non-macro.
978 *
979 * FUNCTION_UNBLANCED_PARENTHESES
980 *
981 * Macro name is not followed by a balanced set of parentheses.
982 */
983static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700984_arguments_parse (argument_list_t *arguments,
985 token_node_t *node,
986 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700987{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700988 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700989 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700990
Carl Worthb1854fd2010-05-25 16:28:26 -0700991 node = node->next;
992
993 /* Ignore whitespace before first parenthesis. */
994 while (node && node->token->type == SPACE)
995 node = node->next;
996
997 if (node == NULL || node->token->type != '(')
998 return FUNCTION_NOT_A_FUNCTION;
999
Carl Worth652fa272010-05-25 17:45:22 -07001000 node = node->next;
1001
Carl Wortha19297b2010-05-27 13:29:19 -07001002 argument = _token_list_create (arguments);
1003 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001004
Carl Worth681afbc2010-05-28 15:06:02 -07001005 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001006 if (node->token->type == '(')
1007 {
1008 paren_count++;
1009 }
1010 else if (node->token->type == ')')
1011 {
1012 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001013 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001014 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001015 }
Carl Worth652fa272010-05-25 17:45:22 -07001016
1017 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001018 paren_count == 1)
1019 {
Carl Wortha19297b2010-05-27 13:29:19 -07001020 _token_list_trim_trailing_space (argument);
1021 argument = _token_list_create (arguments);
1022 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001023 }
1024 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001025 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001026 /* Don't treat initial whitespace as
1027 * part of the arguement. */
1028 if (node->token->type == SPACE)
1029 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001030 }
1031 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001032 }
Carl Worthc7581c22010-05-25 17:41:07 -07001033 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001034
Carl Worth681afbc2010-05-28 15:06:02 -07001035 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001036 return FUNCTION_UNBALANCED_PARENTHESES;
1037
Carl Worth681afbc2010-05-28 15:06:02 -07001038 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001039
1040 return FUNCTION_STATUS_SUCCESS;
1041}
1042
Carl Worth681afbc2010-05-28 15:06:02 -07001043/* This is a helper function that's essentially part of the
1044 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1045 * except for by that function.
1046 *
1047 * Returns NULL if node is a simple token with no expansion, (that is,
1048 * although 'node' corresponds to an identifier defined as a
1049 * function-like macro, it is not followed with a parenthesized
1050 * argument list).
1051 *
1052 * Compute the complete expansion of node (which is a function-like
1053 * macro) and subsequent nodes which are arguments.
1054 *
1055 * Returns the token list that results from the expansion and sets
1056 * *last to the last node in the list that was consumed by the
1057 * expansion. Specificallty, *last will be set as follows: as the
1058 * token of the closing right parenthesis.
1059 */
1060static token_list_t *
1061_glcpp_parser_expand_function (glcpp_parser_t *parser,
1062 token_node_t *node,
1063 token_node_t **last)
1064
Carl Worthb1854fd2010-05-25 16:28:26 -07001065{
1066 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001067 const char *identifier;
1068 argument_list_t *arguments;
1069 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001070 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001071 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001072
Carl Worthb1854fd2010-05-25 16:28:26 -07001073 identifier = node->token->value.str;
1074
1075 macro = hash_table_find (parser->defines, identifier);
1076
1077 assert (macro->is_function);
1078
Carl Worth9ce18cf2010-05-25 17:32:21 -07001079 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001080 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001081
1082 switch (status) {
1083 case FUNCTION_STATUS_SUCCESS:
1084 break;
1085 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001086 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001087 case FUNCTION_UNBALANCED_PARENTHESES:
Carl Worth681afbc2010-05-28 15:06:02 -07001088 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001089 }
1090
Carl Worth9ce18cf2010-05-25 17:32:21 -07001091 if (macro->replacements == NULL) {
1092 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001093 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001094 }
1095
Carl Wortha19297b2010-05-27 13:29:19 -07001096 if (! ((_argument_list_length (arguments) ==
1097 _string_list_length (macro->parameters)) ||
1098 (_string_list_length (macro->parameters) == 0 &&
1099 _argument_list_length (arguments) == 1 &&
1100 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001101 {
1102 fprintf (stderr,
1103 "Error: macro %s invoked with %d arguments (expected %d)\n",
1104 identifier,
1105 _argument_list_length (arguments),
1106 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001107 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001108 }
1109
Carl Worth0197e9b2010-05-26 08:05:19 -07001110 /* Perform argument substitution on the replacement list. */
1111 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001112
Carl Worthce540f22010-05-26 08:25:44 -07001113 for (node = macro->replacements->head; node; node = node->next)
1114 {
1115 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001116 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001117 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001118 &parameter_index))
1119 {
1120 token_list_t *argument;
1121 argument = _argument_list_member_at (arguments,
1122 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001123 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001124 * tokens, or append a placeholder token for
1125 * an empty argument. */
1126 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001127 _glcpp_parser_expand_token_list (parser,
1128 argument);
1129 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001130 } else {
1131 token_t *new_token;
1132
1133 new_token = _token_create_ival (substituted,
1134 PLACEHOLDER,
1135 PLACEHOLDER);
1136 _token_list_append (substituted, new_token);
1137 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001138 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001139 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001140 }
1141 }
1142
Carl Worthad0dee62010-05-26 09:04:50 -07001143 /* After argument substitution, and before further expansion
1144 * below, implement token pasting. */
1145
Carl Worthb06096e2010-05-29 05:54:19 -07001146 _token_list_trim_trailing_space (substituted);
1147
Carl Worthad0dee62010-05-26 09:04:50 -07001148 node = substituted->head;
1149 while (node)
1150 {
1151 token_node_t *next_non_space;
1152
1153 /* Look ahead for a PASTE token, skipping space. */
1154 next_non_space = node->next;
1155 while (next_non_space && next_non_space->token->type == SPACE)
1156 next_non_space = next_non_space->next;
1157
1158 if (next_non_space == NULL)
1159 break;
1160
1161 if (next_non_space->token->type != PASTE) {
1162 node = next_non_space;
1163 continue;
1164 }
1165
1166 /* Now find the next non-space token after the PASTE. */
1167 next_non_space = next_non_space->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 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001173 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001174 }
1175
Carl Worthb06096e2010-05-29 05:54:19 -07001176 node->token = _token_paste (node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001177 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001178 if (next_non_space == substituted->tail)
1179 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001180
1181 node = node->next;
1182 }
1183
Carl Worthb06096e2010-05-29 05:54:19 -07001184 substituted->non_space_tail = substituted->tail;
1185
Carl Worthae6517f2010-05-25 15:24:59 -07001186 _string_list_push (parser->active, identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001187 _glcpp_parser_expand_token_list (parser, substituted);
Carl Worthae6517f2010-05-25 15:24:59 -07001188 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001189
Carl Worth681afbc2010-05-28 15:06:02 -07001190 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001191}
1192
Carl Worth681afbc2010-05-28 15:06:02 -07001193/* Compute the complete expansion of node, (and subsequent nodes after
1194 * 'node' in the case that 'node' is a function-like macro and
1195 * subsequent nodes are arguments).
1196 *
1197 * Returns NULL if node is a simple token with no expansion.
1198 *
1199 * Otherwise, returns the token list that results from the expansion
1200 * and sets *last to the last node in the list that was consumed by
1201 * the expansion. Specificallty, *last will be set as follows:
1202 *
1203 * As 'node' in the case of object-like macro expansion.
1204 *
1205 * As the token of the closing right parenthesis in the case of
1206 * function-like macro expansion.
1207 */
1208static token_list_t *
1209_glcpp_parser_expand_node (glcpp_parser_t *parser,
1210 token_node_t *node,
1211 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001212{
Carl Worth681afbc2010-05-28 15:06:02 -07001213 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001214 const char *identifier;
1215 macro_t *macro;
1216 token_list_t *expansion;
1217
1218 /* We only expand identifiers */
1219 if (token->type != IDENTIFIER) {
1220 /* We change any COMMA into a COMMA_FINAL to prevent
1221 * it being mistaken for an argument separator
1222 * later. */
1223 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001224 token->type = COMMA_FINAL;
1225 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001226 }
Carl Worth681afbc2010-05-28 15:06:02 -07001227
1228 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001229 }
1230
1231 /* Look up this identifier in the hash table. */
1232 identifier = token->value.str;
1233 macro = hash_table_find (parser->defines, identifier);
1234
Carl Worth681afbc2010-05-28 15:06:02 -07001235 /* Not a macro, so no expansion needed. */
1236 if (macro == NULL)
1237 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001238
1239 /* Finally, don't expand this macro if we're already actively
1240 * expanding it, (to avoid infinite recursion). */
Carl Worth681afbc2010-05-28 15:06:02 -07001241 if (_string_list_contains (parser->active, identifier, NULL)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001242 /* We change the token type here from IDENTIFIER to
1243 * OTHER to prevent any future expansion of this
1244 * unexpanded token. */
1245 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001246 token_list_t *expansion;
1247 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001248
Carl Worth681afbc2010-05-28 15:06:02 -07001249 str = xtalloc_strdup (parser, token->value.str);
1250 final = _token_create_str (parser, OTHER, str);
1251 expansion = _token_list_create (parser);
1252 _token_list_append (expansion, final);
1253 *last = node;
1254 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001255 }
1256
Carl Worth681afbc2010-05-28 15:06:02 -07001257 if (! macro->is_function)
1258 {
1259 *last = node;
1260
1261 if (macro->replacements == NULL)
1262 return _token_list_create (parser);
1263
1264 expansion = _token_list_copy (parser, macro->replacements);
1265
Carl Worth3c93d392010-05-28 08:17:46 -07001266 _string_list_push (parser->active, identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001267 _glcpp_parser_expand_token_list (parser, expansion);
Carl Worth3c93d392010-05-28 08:17:46 -07001268 _string_list_pop (parser->active);
Carl Worth681afbc2010-05-28 15:06:02 -07001269
1270 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001271 }
Carl Worth681afbc2010-05-28 15:06:02 -07001272
1273 return _glcpp_parser_expand_function (parser, node, last);
1274}
1275
1276/* Walk over the token list replacing nodes with their expansion.
1277 * Whenever nodes are expanded the walking will walk over the new
1278 * nodes, continuing to expand as necessary. The results are placed in
1279 * 'list' itself;
1280 */
1281static void
1282_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1283 token_list_t *list)
1284{
1285 token_node_t *node_prev;
1286 token_node_t *node, *last;
1287 token_list_t *expansion;
1288
1289 if (list == NULL)
1290 return;
1291
1292 _token_list_trim_trailing_space (list);
1293
1294 node_prev = NULL;
1295 node = list->head;
1296
1297 while (node) {
1298 /* Find the expansion for node, which will replace all
1299 * nodes from node to last, inclusive. */
1300 expansion = _glcpp_parser_expand_node (parser, node, &last);
1301 if (expansion) {
1302 /* Splice expansion into list, supporting a
1303 * simple deletion if the expansion is
1304 * empty. */
1305 if (expansion->head) {
1306 if (node_prev)
1307 node_prev->next = expansion->head;
1308 else
1309 list->head = expansion->head;
1310 expansion->tail->next = last->next;
1311 if (last == list->tail)
1312 list->tail = expansion->tail;
1313 } else {
1314 if (node_prev)
1315 node_prev->next = last->next;
1316 else
1317 list->head = last->next;
1318 if (last == list->tail)
1319 list->tail == NULL;
1320 }
1321 } else {
1322 node_prev = node;
1323 }
1324 node = node_prev ? node_prev->next : list->head;
1325 }
1326
1327 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001328}
1329
Carl Worth0197e9b2010-05-26 08:05:19 -07001330static void
1331_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
1332 token_list_t *list,
1333 token_list_t *result)
1334{
Carl Worth681afbc2010-05-28 15:06:02 -07001335 _glcpp_parser_expand_token_list (parser, list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001336
Carl Worth681afbc2010-05-28 15:06:02 -07001337 _token_list_append_list (result, list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001338}
1339
Carl Worthae6517f2010-05-25 15:24:59 -07001340void
1341_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1342 token_list_t *list)
1343{
Carl Worthae6517f2010-05-25 15:24:59 -07001344 if (list == NULL)
1345 return;
1346
Carl Worth681afbc2010-05-28 15:06:02 -07001347 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001348
Carl Worth681afbc2010-05-28 15:06:02 -07001349 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001350
Carl Worth681afbc2010-05-28 15:06:02 -07001351 _token_list_print (list);
Carl Worthae6517f2010-05-25 15:24:59 -07001352}
1353
1354void
Carl Worthfcbbb462010-05-13 09:36:23 -07001355_define_object_macro (glcpp_parser_t *parser,
1356 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001357 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001358{
1359 macro_t *macro;
1360
1361 macro = xtalloc (parser, macro_t);
1362
1363 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001364 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001365 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001366 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001367
1368 hash_table_insert (parser->defines, macro, identifier);
1369}
1370
1371void
1372_define_function_macro (glcpp_parser_t *parser,
1373 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001374 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001375 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001376{
1377 macro_t *macro;
1378
1379 macro = xtalloc (parser, macro_t);
1380
1381 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001382 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001383 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001384 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001385
1386 hash_table_insert (parser->defines, macro, identifier);
1387}
1388
Carl Worth8f38aff2010-05-19 10:01:29 -07001389static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001390glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001391{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001392 token_node_t *node;
1393 int ret;
1394
Carl Worth95951ea2010-05-26 15:57:10 -07001395 if (parser->lex_from_list == NULL) {
1396 ret = glcpp_lex (parser->scanner);
1397
1398 /* XXX: This ugly block of code exists for the sole
1399 * purpose of converting a NEWLINE token into a SPACE
1400 * token, but only in the case where we have seen a
1401 * function-like macro name, but have not yet seen its
1402 * closing parenthesis.
1403 *
1404 * There's perhaps a more compact way to do this with
1405 * mid-rule actions in the grammar.
1406 *
1407 * I'm definitely not pleased with the complexity of
1408 * this code here.
1409 */
1410 if (parser->newline_as_space)
1411 {
1412 if (ret == '(') {
1413 parser->paren_count++;
1414 } else if (ret == ')') {
1415 parser->paren_count--;
1416 if (parser->paren_count == 0)
1417 parser->newline_as_space = 0;
1418 } else if (ret == NEWLINE) {
1419 ret = SPACE;
1420 } else if (ret != SPACE) {
1421 if (parser->paren_count == 0)
1422 parser->newline_as_space = 0;
1423 }
1424 }
1425 else if (parser->in_control_line)
1426 {
1427 if (ret == NEWLINE)
1428 parser->in_control_line = 0;
1429 }
1430 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1431 ret == HASH_UNDEF || ret == HASH_IF ||
1432 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1433 ret == HASH_ELIF || ret == HASH_ELSE ||
1434 ret == HASH_ENDIF || ret == HASH)
1435 {
1436 parser->in_control_line = 1;
1437 }
1438 else if (ret == IDENTIFIER)
1439 {
1440 macro_t *macro;
1441 macro = hash_table_find (parser->defines,
1442 yylval.str);
1443 if (macro && macro->is_function) {
1444 parser->newline_as_space = 1;
1445 parser->paren_count = 0;
1446 }
1447 }
1448
1449 return ret;
1450 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001451
1452 node = parser->lex_from_node;
1453
1454 if (node == NULL) {
1455 talloc_free (parser->lex_from_list);
1456 parser->lex_from_list = NULL;
1457 return NEWLINE;
1458 }
1459
1460 yylval = node->token->value;
1461 ret = node->token->type;
1462
1463 parser->lex_from_node = node->next;
1464
1465 return ret;
1466}
1467
1468static void
1469glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1470{
1471 token_node_t *node;
1472
1473 assert (parser->lex_from_list == NULL);
1474
1475 /* Copy list, eliminating any space tokens. */
1476 parser->lex_from_list = _token_list_create (parser);
1477
1478 for (node = list->head; node; node = node->next) {
1479 if (node->token->type == SPACE)
1480 continue;
1481 _token_list_append (parser->lex_from_list, node->token);
1482 }
1483
1484 talloc_free (list);
1485
1486 parser->lex_from_node = parser->lex_from_list->head;
1487
1488 /* It's possible the list consisted of nothing but whitespace. */
1489 if (parser->lex_from_node == NULL) {
1490 talloc_free (parser->lex_from_list);
1491 parser->lex_from_list = NULL;
1492 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001493}
Carl Worthb20d33c2010-05-20 22:27:07 -07001494
1495static void
1496_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1497{
1498 skip_type_t current = SKIP_NO_SKIP;
1499 skip_node_t *node;
1500
1501 if (parser->skip_stack)
1502 current = parser->skip_stack->type;
1503
1504 node = xtalloc (parser, skip_node_t);
1505
1506 if (current == SKIP_NO_SKIP) {
1507 if (condition)
1508 node->type = SKIP_NO_SKIP;
1509 else
1510 node->type = SKIP_TO_ELSE;
1511 } else {
1512 node->type = SKIP_TO_ENDIF;
1513 }
1514
1515 node->next = parser->skip_stack;
1516 parser->skip_stack = node;
1517}
1518
1519static void
1520_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1521 int condition)
1522{
1523 if (parser->skip_stack == NULL) {
1524 fprintf (stderr, "Error: %s without #if\n", type);
1525 exit (1);
1526 }
1527
1528 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1529 if (condition)
1530 parser->skip_stack->type = SKIP_NO_SKIP;
1531 } else {
1532 parser->skip_stack->type = SKIP_TO_ENDIF;
1533 }
1534}
Carl Worth80dc60b2010-05-25 14:42:00 -07001535
Carl Worthb20d33c2010-05-20 22:27:07 -07001536static void
1537_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1538{
1539 skip_node_t *node;
1540
1541 if (parser->skip_stack == NULL) {
1542 fprintf (stderr, "Error: #endif without #if\n");
1543 exit (1);
1544 }
1545
1546 node = parser->skip_stack;
1547 parser->skip_stack = node->next;
1548 talloc_free (node);
1549}