blob: dd8e133f5506ff9ec38a3105c5d11b26dcbdee36 [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 Wortha771a402010-06-01 11:20:18 -0700162 printf ("\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700163 }
Carl Worth808401f2010-05-25 14:52:43 -0700164| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700165 _glcpp_parser_print_expanded_token_list (parser, $1);
166 printf ("\n");
Carl Worth808401f2010-05-25 14:52:43 -0700167 talloc_free ($1);
168 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700169| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700170| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700171;
172
Carl Worth8e82fcb2010-05-26 11:15:21 -0700173expanded_line:
174 IF_EXPANDED expression NEWLINE {
175 _glcpp_parser_skip_stack_push_if (parser, $2);
176 }
177| ELIF_EXPANDED expression NEWLINE {
178 _glcpp_parser_skip_stack_change_if (parser, "elif", $2);
179 }
180;
181
Carl Worth3ff81672010-05-25 13:09:03 -0700182control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700183 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Carl Worthae6517f2010-05-25 15:24:59 -0700184 _define_object_macro (parser, $2, $3);
185 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700186| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
187 _define_function_macro (parser, $2, NULL, $5);
188 }
189| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
190 _define_function_macro (parser, $2, $4, $6);
191 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700192| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700193 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700194 if (macro) {
195 /* XXX: Need hash table to support a real way
196 * to remove an element rather than prefixing
197 * a new node with data of NULL like this. */
198 hash_table_insert (parser->defines, NULL, $2);
199 talloc_free (macro);
200 }
201 talloc_free ($2);
202 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700203| HASH_IF pp_tokens NEWLINE {
204 token_list_t *expanded;
205 token_t *token;
206
207 expanded = _token_list_create (parser);
208 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
209 _token_list_append (expanded, token);
210 talloc_unlink (parser, token);
211 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700212 _glcpp_parser_expand_token_list (parser, $2);
213 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700214 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700215 }
216| HASH_IFDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700217 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700218 talloc_free ($2);
219 _glcpp_parser_skip_stack_push_if (parser, macro != NULL);
220 }
221| HASH_IFNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700222 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700223 talloc_free ($2);
224 _glcpp_parser_skip_stack_push_if (parser, macro == NULL);
225 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700226| HASH_ELIF pp_tokens NEWLINE {
227 token_list_t *expanded;
228 token_t *token;
229
230 expanded = _token_list_create (parser);
231 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
232 _token_list_append (expanded, token);
233 talloc_unlink (parser, token);
234 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700235 _glcpp_parser_expand_token_list (parser, $2);
236 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700237 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700238 }
239| HASH_ELSE NEWLINE {
240 _glcpp_parser_skip_stack_change_if (parser, "else", 1);
241 }
242| HASH_ENDIF NEWLINE {
243 _glcpp_parser_skip_stack_pop (parser);
244 }
Carl Worth3ff81672010-05-25 13:09:03 -0700245| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700246;
247
Carl Worth8fed1cd2010-05-26 09:32:12 -0700248expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700249 INTEGER_STRING {
250 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
251 $$ = strtoll ($1 + 2, NULL, 16);
252 } else if ($1[0] == '0') {
253 $$ = strtoll ($1, NULL, 8);
254 } else {
255 $$ = strtoll ($1, NULL, 10);
256 }
257 }
258| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700259 $$ = $1;
260 }
261| expression OR expression {
262 $$ = $1 || $3;
263 }
264| expression AND expression {
265 $$ = $1 && $3;
266 }
267| expression '|' expression {
268 $$ = $1 | $3;
269 }
270| expression '^' expression {
271 $$ = $1 ^ $3;
272 }
273| expression '&' expression {
274 $$ = $1 & $3;
275 }
276| expression NOT_EQUAL expression {
277 $$ = $1 != $3;
278 }
279| expression EQUAL expression {
280 $$ = $1 == $3;
281 }
282| expression GREATER_OR_EQUAL expression {
283 $$ = $1 >= $3;
284 }
285| expression LESS_OR_EQUAL expression {
286 $$ = $1 <= $3;
287 }
288| expression '>' expression {
289 $$ = $1 > $3;
290 }
291| expression '<' expression {
292 $$ = $1 < $3;
293 }
294| expression RIGHT_SHIFT expression {
295 $$ = $1 >> $3;
296 }
297| expression LEFT_SHIFT expression {
298 $$ = $1 << $3;
299 }
300| expression '-' expression {
301 $$ = $1 - $3;
302 }
303| expression '+' expression {
304 $$ = $1 + $3;
305 }
306| expression '%' expression {
307 $$ = $1 % $3;
308 }
309| expression '/' expression {
310 $$ = $1 / $3;
311 }
312| expression '*' expression {
313 $$ = $1 * $3;
314 }
315| '!' expression %prec UNARY {
316 $$ = ! $2;
317 }
318| '~' expression %prec UNARY {
319 $$ = ~ $2;
320 }
321| '-' expression %prec UNARY {
322 $$ = - $2;
323 }
324| '+' expression %prec UNARY {
325 $$ = + $2;
326 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700327| '(' expression ')' {
328 $$ = $2;
329 }
330;
331
Carl Worth3ff81672010-05-25 13:09:03 -0700332identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700333 IDENTIFIER {
334 $$ = _string_list_create (parser);
335 _string_list_append_item ($$, $1);
336 talloc_steal ($$, $1);
337 }
338| identifier_list ',' IDENTIFIER {
339 $$ = $1;
340 _string_list_append_item ($$, $3);
341 talloc_steal ($$, $3);
342 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700343;
344
Carl Worth3ff81672010-05-25 13:09:03 -0700345text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700346 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700347| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700348;
349
Carl Worth3ff81672010-05-25 13:09:03 -0700350non_directive:
351 pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700352;
353
Carl Worthaaa9acb2010-05-19 13:28:24 -0700354replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700355 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700356| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700357;
358
Carl Worthaaa9acb2010-05-19 13:28:24 -0700359pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700360 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700361 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700362 $$ = _token_list_create (parser);
363 _token_list_append ($$, $1);
364 talloc_unlink (parser, $1);
365 }
366| pp_tokens preprocessing_token {
367 $$ = $1;
368 _token_list_append ($$, $2);
369 talloc_unlink (parser, $2);
370 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700371;
372
Carl Worth3ff81672010-05-25 13:09:03 -0700373preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700374 IDENTIFIER {
375 $$ = _token_create_str (parser, IDENTIFIER, $1);
376 }
Carl Worth050e3de2010-05-27 14:36:29 -0700377| INTEGER_STRING {
378 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700379 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700380| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700381 $$ = _token_create_ival (parser, $1, $1);
382 }
383| OTHER {
384 $$ = _token_create_str (parser, OTHER, $1);
385 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700386| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700387 $$ = _token_create_ival (parser, SPACE, SPACE);
Carl Worthb1854fd2010-05-25 16:28:26 -0700388 }
Carl Worth3ff81672010-05-25 13:09:03 -0700389;
390
Carl Worth8e82fcb2010-05-26 11:15:21 -0700391operator:
Carl Worth808401f2010-05-25 14:52:43 -0700392 '[' { $$ = '['; }
393| ']' { $$ = ']'; }
394| '(' { $$ = '('; }
395| ')' { $$ = ')'; }
396| '{' { $$ = '{'; }
397| '}' { $$ = '}'; }
398| '.' { $$ = '.'; }
399| '&' { $$ = '&'; }
400| '*' { $$ = '*'; }
401| '+' { $$ = '+'; }
402| '-' { $$ = '-'; }
403| '~' { $$ = '~'; }
404| '!' { $$ = '!'; }
405| '/' { $$ = '/'; }
406| '%' { $$ = '%'; }
407| LEFT_SHIFT { $$ = LEFT_SHIFT; }
408| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
409| '<' { $$ = '<'; }
410| '>' { $$ = '>'; }
411| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
412| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
413| EQUAL { $$ = EQUAL; }
414| NOT_EQUAL { $$ = NOT_EQUAL; }
415| '^' { $$ = '^'; }
416| '|' { $$ = '|'; }
417| AND { $$ = AND; }
418| OR { $$ = OR; }
419| ';' { $$ = ';'; }
420| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700421| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700422| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700423| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700424;
425
Carl Worth33cc4002010-05-12 12:17:10 -0700426%%
427
Carl Worth610053b2010-05-14 10:05:11 -0700428string_list_t *
429_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700430{
Carl Worth610053b2010-05-14 10:05:11 -0700431 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700432
Carl Worth610053b2010-05-14 10:05:11 -0700433 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700434 list->head = NULL;
435 list->tail = NULL;
436
437 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700438}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700439
Carl Worth33cc4002010-05-12 12:17:10 -0700440void
Carl Worth610053b2010-05-14 10:05:11 -0700441_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700442{
443 if (list->head == NULL) {
444 list->head = tail->head;
445 } else {
446 list->tail->next = tail->head;
447 }
448
449 list->tail = tail->tail;
450}
451
452void
Carl Worth610053b2010-05-14 10:05:11 -0700453_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700454{
Carl Worth610053b2010-05-14 10:05:11 -0700455 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700456
Carl Worth610053b2010-05-14 10:05:11 -0700457 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700458 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700459
Carl Worth33cc4002010-05-12 12:17:10 -0700460 node->next = NULL;
461
462 if (list->head == NULL) {
463 list->head = node;
464 } else {
465 list->tail->next = node;
466 }
467
468 list->tail = node;
469}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700470
Carl Worthae6517f2010-05-25 15:24:59 -0700471void
472_string_list_push (string_list_t *list, const char *str)
473{
474 string_node_t *node;
475
476 node = xtalloc (list, string_node_t);
477 node->str = xtalloc_strdup (node, str);
478 node->next = list->head;
479
480 if (list->tail == NULL) {
481 list->tail = node;
482 }
483 list->head = node;
484}
485
486void
487_string_list_pop (string_list_t *list)
488{
489 string_node_t *node;
490
491 node = list->head;
492
493 if (node == NULL) {
494 fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
495 exit (1);
496 }
497
498 list->head = node->next;
499 if (list->tail == node) {
500 assert (node->next == NULL);
501 list->tail = NULL;
502 }
503
504 talloc_free (node);
505}
506
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700507int
Carl Worth610053b2010-05-14 10:05:11 -0700508_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700509{
Carl Worth610053b2010-05-14 10:05:11 -0700510 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700511 int i;
512
513 if (list == NULL)
514 return 0;
515
516 for (i = 0, node = list->head; node; i++, node = node->next) {
517 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700518 if (index)
519 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700520 return 1;
521 }
522 }
523
524 return 0;
525}
526
527int
Carl Worth610053b2010-05-14 10:05:11 -0700528_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700529{
530 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700531 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700532
533 if (list == NULL)
534 return 0;
535
536 for (node = list->head; node; node = node->next)
537 length++;
538
539 return length;
540}
541
Carl Worth8f6a8282010-05-14 10:44:19 -0700542argument_list_t *
543_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700544{
Carl Worth8f6a8282010-05-14 10:44:19 -0700545 argument_list_t *list;
546
547 list = xtalloc (ctx, argument_list_t);
548 list->head = NULL;
549 list->tail = NULL;
550
551 return list;
552}
553
554void
Carl Worth47252442010-05-19 13:54:37 -0700555_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700556{
557 argument_node_t *node;
558
Carl Worth8f6a8282010-05-14 10:44:19 -0700559 node = xtalloc (list, argument_node_t);
560 node->argument = argument;
561
562 node->next = NULL;
563
564 if (list->head == NULL) {
565 list->head = node;
566 } else {
567 list->tail->next = node;
568 }
569
570 list->tail = node;
571}
572
573int
574_argument_list_length (argument_list_t *list)
575{
576 int length = 0;
577 argument_node_t *node;
578
579 if (list == NULL)
580 return 0;
581
582 for (node = list->head; node; node = node->next)
583 length++;
584
585 return length;
586}
587
Carl Worth47252442010-05-19 13:54:37 -0700588token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700589_argument_list_member_at (argument_list_t *list, int index)
590{
591 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700592 int i;
593
594 if (list == NULL)
595 return NULL;
596
597 node = list->head;
598 for (i = 0; i < index; i++) {
599 node = node->next;
600 if (node == NULL)
601 break;
602 }
603
604 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700605 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700606
607 return NULL;
608}
Carl Worth47252442010-05-19 13:54:37 -0700609
Carl Worth808401f2010-05-25 14:52:43 -0700610/* Note: This function talloc_steal()s the str pointer. */
611token_t *
612_token_create_str (void *ctx, int type, char *str)
613{
614 token_t *token;
615
616 token = xtalloc (ctx, token_t);
617 token->type = type;
618 token->value.str = talloc_steal (token, str);
619
620 return token;
621}
622
623token_t *
624_token_create_ival (void *ctx, int type, int ival)
625{
626 token_t *token;
627
628 token = xtalloc (ctx, token_t);
629 token->type = type;
630 token->value.ival = ival;
631
632 return token;
633}
634
Carl Worth47252442010-05-19 13:54:37 -0700635token_list_t *
636_token_list_create (void *ctx)
637{
638 token_list_t *list;
639
640 list = xtalloc (ctx, token_list_t);
641 list->head = NULL;
642 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700643 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700644
645 return list;
646}
647
648void
Carl Worth808401f2010-05-25 14:52:43 -0700649_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700650{
651 token_node_t *node;
652
653 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700654 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700655
656 node->next = NULL;
657
658 if (list->head == NULL) {
659 list->head = node;
660 } else {
661 list->tail->next = node;
662 }
663
664 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700665 if (token->type != SPACE)
666 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700667}
668
669void
670_token_list_append_list (token_list_t *list, token_list_t *tail)
671{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700672 if (tail == NULL || tail->head == NULL)
673 return;
674
Carl Worth47252442010-05-19 13:54:37 -0700675 if (list->head == NULL) {
676 list->head = tail->head;
677 } else {
678 list->tail->next = tail->head;
679 }
680
681 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700682 list->non_space_tail = tail->non_space_tail;
683}
684
Carl Worth681afbc2010-05-28 15:06:02 -0700685token_list_t *
686_token_list_copy (void *ctx, token_list_t *other)
687{
688 token_list_t *copy;
689 token_node_t *node;
690
691 if (other == NULL)
692 return NULL;
693
694 copy = _token_list_create (ctx);
695 for (node = other->head; node; node = node->next)
696 _token_list_append (copy, node->token);
697
698 return copy;
699}
700
Carl Worth10ae4382010-05-25 20:35:01 -0700701void
702_token_list_trim_trailing_space (token_list_t *list)
703{
704 token_node_t *tail, *next;
705
706 if (list->non_space_tail) {
707 tail = list->non_space_tail->next;
708 list->non_space_tail->next = NULL;
709 list->tail = list->non_space_tail;
710
711 while (tail) {
712 next = tail->next;
713 talloc_free (tail);
714 tail = next;
715 }
716 }
Carl Worth47252442010-05-19 13:54:37 -0700717}
Carl Worth80dc60b2010-05-25 14:42:00 -0700718
Carl Worth0197e9b2010-05-26 08:05:19 -0700719static void
720_token_print (token_t *token)
721{
722 if (token->type < 256) {
723 printf ("%c", token->type);
724 return;
725 }
726
727 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700728 case INTEGER:
729 printf ("%" PRIxMAX, token->value.ival);
730 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700731 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700732 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700733 case OTHER:
734 printf ("%s", token->value.str);
735 break;
736 case SPACE:
737 printf (" ");
738 break;
739 case LEFT_SHIFT:
740 printf ("<<");
741 break;
742 case RIGHT_SHIFT:
743 printf (">>");
744 break;
745 case LESS_OR_EQUAL:
746 printf ("<=");
747 break;
748 case GREATER_OR_EQUAL:
749 printf (">=");
750 break;
751 case EQUAL:
752 printf ("==");
753 break;
754 case NOT_EQUAL:
755 printf ("!=");
756 break;
757 case AND:
758 printf ("&&");
759 break;
760 case OR:
761 printf ("||");
762 break;
763 case PASTE:
764 printf ("##");
765 break;
Carl Worthdd749002010-05-27 10:12:33 -0700766 case COMMA_FINAL:
767 printf (",");
768 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700769 case PLACEHOLDER:
770 /* Nothing to print. */
771 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700772 default:
773 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
774 break;
775 }
776}
777
Carl Worthb06096e2010-05-29 05:54:19 -0700778/* Return a new token (talloc()ed off of 'token') formed by pasting
779 * 'token' and 'other'. Note that this function may return 'token' or
780 * 'other' directly rather than allocating anything new.
781 *
782 * Caution: Only very cursory error-checking is performed to see if
783 * the final result is a valid single token. */
784static token_t *
Carl Worthad0dee62010-05-26 09:04:50 -0700785_token_paste (token_t *token, token_t *other)
786{
Carl Worth85b50e82010-05-27 14:01:18 -0700787 /* Pasting a placeholder onto anything makes no change. */
788 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700789 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700790
Carl Worthb06096e2010-05-29 05:54:19 -0700791 /* When 'token' is a placeholder, just return 'other'. */
792 if (token->type == PLACEHOLDER)
793 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700794
Carl Worthad0dee62010-05-26 09:04:50 -0700795 /* A very few single-character punctuators can be combined
796 * with another to form a multi-character punctuator. */
797 switch (token->type) {
798 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700799 if (other->type == '<')
800 return _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
801 else if (other->type == '=')
802 return _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700803 break;
804 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700805 if (other->type == '>')
806 return _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
807 else if (other->type == '=')
808 return _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700809 break;
810 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700811 if (other->type == '=')
812 return _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700813 break;
814 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700815 if (other->type == '=')
816 return _token_create_ival (token, NOT_EQUAL, NOT_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, AND, AND);
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, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700825 break;
826 }
827
828 /* Two string-valued tokens can usually just be mashed
829 * together.
830 *
Carl Worth050e3de2010-05-27 14:36:29 -0700831 * XXX: This isn't actually legitimate. Several things here
832 * should result in a diagnostic since the result cannot be a
833 * valid, single pre-processing token. For example, pasting
834 * "123" and "abc" is not legal, but we don't catch that
835 * here. */
836 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
837 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700838 {
Carl Worthb06096e2010-05-29 05:54:19 -0700839 char *str;
840
841 str = xtalloc_asprintf (token, "%s%s",
842 token->value.str, other->value.str);
843 return _token_create_str (token, token->type, str);
Carl Worthad0dee62010-05-26 09:04:50 -0700844 }
845
846 printf ("Error: Pasting \"");
847 _token_print (token);
848 printf ("\" and \"");
849 _token_print (other);
850 printf ("\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700851
852 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700853}
854
Carl Worth0197e9b2010-05-26 08:05:19 -0700855static void
856_token_list_print (token_list_t *list)
857{
858 token_node_t *node;
859
860 if (list == NULL)
861 return;
862
863 for (node = list->head; node; node = node->next)
864 _token_print (node->token);
865}
866
Carl Worth3a37b872010-05-10 11:44:09 -0700867void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700868yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700869{
870 fprintf (stderr, "Parse error: %s\n", error);
871}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700872
Carl Worth33cc4002010-05-12 12:17:10 -0700873glcpp_parser_t *
874glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700875{
Carl Worth33cc4002010-05-12 12:17:10 -0700876 glcpp_parser_t *parser;
877
Carl Worth5070a202010-05-12 12:45:33 -0700878 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700879
Carl Worth8f38aff2010-05-19 10:01:29 -0700880 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700881 parser->defines = hash_table_ctor (32, hash_table_string_hash,
882 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700883 parser->active = _string_list_create (parser);
Carl Wortha771a402010-06-01 11:20:18 -0700884 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700885 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700886 parser->newline_as_space = 0;
887 parser->in_control_line = 0;
888 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700889
Carl Worthb20d33c2010-05-20 22:27:07 -0700890 parser->skip_stack = NULL;
891
Carl Worth8e82fcb2010-05-26 11:15:21 -0700892 parser->lex_from_list = NULL;
893 parser->lex_from_node = NULL;
894
Carl Worth33cc4002010-05-12 12:17:10 -0700895 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700896}
897
898int
899glcpp_parser_parse (glcpp_parser_t *parser)
900{
901 return yyparse (parser);
902}
903
904void
Carl Worth33cc4002010-05-12 12:17:10 -0700905glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700906{
Carl Worthb20d33c2010-05-20 22:27:07 -0700907 if (parser->skip_stack)
908 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700909 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700910 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700911 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700912}
Carl Worthc6d5af32010-05-11 12:30:09 -0700913
Carl Worth8e82fcb2010-05-26 11:15:21 -0700914/* Replace any occurences of DEFINED tokens in 'list' with either a
915 * '0' or '1' INTEGER token depending on whether the next token in the
916 * list is defined or not. */
917static void
918_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
919 token_list_t *list)
920{
921 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700922 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700923
924 if (list == NULL)
925 return;
926
927 for (node = list->head; node; node = node->next) {
928 if (node->token->type != DEFINED)
929 continue;
930 next = node->next;
931 while (next && next->token->type == SPACE)
932 next = next->next;
933 if (next == NULL || next->token->type != IDENTIFIER) {
934 fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
935 exit (1);
936 }
937 macro = hash_table_find (parser->defines,
938 next->token->value.str);
939
940 node->token->type = INTEGER;
941 node->token->value.ival = (macro != NULL);
942 node->next = next->next;
943 }
944}
945
Carl Worthb1854fd2010-05-25 16:28:26 -0700946typedef enum function_status
947{
948 FUNCTION_STATUS_SUCCESS,
949 FUNCTION_NOT_A_FUNCTION,
950 FUNCTION_UNBALANCED_PARENTHESES
951} function_status_t;
952
953/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700954 * balanced set of parentheses.
955 *
956 * When called, 'node' should be the opening-parenthesis token, (or
957 * perhaps preceeding SPACE tokens). Upon successful return *last will
958 * be the last consumed node, (corresponding to the closing right
959 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700960 *
961 * Return values:
962 *
963 * FUNCTION_STATUS_SUCCESS:
964 *
965 * Successfully parsed a set of function arguments.
966 *
967 * FUNCTION_NOT_A_FUNCTION:
968 *
969 * Macro name not followed by a '('. This is not an error, but
970 * simply that the macro name should be treated as a non-macro.
971 *
972 * FUNCTION_UNBLANCED_PARENTHESES
973 *
974 * Macro name is not followed by a balanced set of parentheses.
975 */
976static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700977_arguments_parse (argument_list_t *arguments,
978 token_node_t *node,
979 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700980{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700981 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700982 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700983
Carl Worthb1854fd2010-05-25 16:28:26 -0700984 node = node->next;
985
986 /* Ignore whitespace before first parenthesis. */
987 while (node && node->token->type == SPACE)
988 node = node->next;
989
990 if (node == NULL || node->token->type != '(')
991 return FUNCTION_NOT_A_FUNCTION;
992
Carl Worth652fa272010-05-25 17:45:22 -0700993 node = node->next;
994
Carl Wortha19297b2010-05-27 13:29:19 -0700995 argument = _token_list_create (arguments);
996 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -0700997
Carl Worth681afbc2010-05-28 15:06:02 -0700998 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -0700999 if (node->token->type == '(')
1000 {
1001 paren_count++;
1002 }
1003 else if (node->token->type == ')')
1004 {
1005 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001006 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001007 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001008 }
Carl Worth652fa272010-05-25 17:45:22 -07001009
1010 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001011 paren_count == 1)
1012 {
Carl Wortha19297b2010-05-27 13:29:19 -07001013 _token_list_trim_trailing_space (argument);
1014 argument = _token_list_create (arguments);
1015 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001016 }
1017 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001018 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001019 /* Don't treat initial whitespace as
1020 * part of the arguement. */
1021 if (node->token->type == SPACE)
1022 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001023 }
1024 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001025 }
Carl Worthc7581c22010-05-25 17:41:07 -07001026 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001027
Carl Worth681afbc2010-05-28 15:06:02 -07001028 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001029 return FUNCTION_UNBALANCED_PARENTHESES;
1030
Carl Worth681afbc2010-05-28 15:06:02 -07001031 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001032
1033 return FUNCTION_STATUS_SUCCESS;
1034}
1035
Carl Worth681afbc2010-05-28 15:06:02 -07001036/* This is a helper function that's essentially part of the
1037 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1038 * except for by that function.
1039 *
1040 * Returns NULL if node is a simple token with no expansion, (that is,
1041 * although 'node' corresponds to an identifier defined as a
1042 * function-like macro, it is not followed with a parenthesized
1043 * argument list).
1044 *
1045 * Compute the complete expansion of node (which is a function-like
1046 * macro) and subsequent nodes which are arguments.
1047 *
1048 * Returns the token list that results from the expansion and sets
1049 * *last to the last node in the list that was consumed by the
1050 * expansion. Specificallty, *last will be set as follows: as the
1051 * token of the closing right parenthesis.
1052 */
1053static token_list_t *
1054_glcpp_parser_expand_function (glcpp_parser_t *parser,
1055 token_node_t *node,
1056 token_node_t **last)
1057
Carl Worthb1854fd2010-05-25 16:28:26 -07001058{
1059 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001060 const char *identifier;
1061 argument_list_t *arguments;
1062 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001063 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001064 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001065
Carl Worthb1854fd2010-05-25 16:28:26 -07001066 identifier = node->token->value.str;
1067
1068 macro = hash_table_find (parser->defines, identifier);
1069
1070 assert (macro->is_function);
1071
Carl Worth9ce18cf2010-05-25 17:32:21 -07001072 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001073 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001074
1075 switch (status) {
1076 case FUNCTION_STATUS_SUCCESS:
1077 break;
1078 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001079 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001080 case FUNCTION_UNBALANCED_PARENTHESES:
Carl Worth681afbc2010-05-28 15:06:02 -07001081 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001082 }
1083
Carl Worth9ce18cf2010-05-25 17:32:21 -07001084 if (macro->replacements == NULL) {
1085 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001086 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001087 }
1088
Carl Wortha19297b2010-05-27 13:29:19 -07001089 if (! ((_argument_list_length (arguments) ==
1090 _string_list_length (macro->parameters)) ||
1091 (_string_list_length (macro->parameters) == 0 &&
1092 _argument_list_length (arguments) == 1 &&
1093 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001094 {
1095 fprintf (stderr,
1096 "Error: macro %s invoked with %d arguments (expected %d)\n",
1097 identifier,
1098 _argument_list_length (arguments),
1099 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001100 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001101 }
1102
Carl Worth0197e9b2010-05-26 08:05:19 -07001103 /* Perform argument substitution on the replacement list. */
1104 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001105
Carl Worthce540f22010-05-26 08:25:44 -07001106 for (node = macro->replacements->head; node; node = node->next)
1107 {
1108 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001109 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001110 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001111 &parameter_index))
1112 {
1113 token_list_t *argument;
1114 argument = _argument_list_member_at (arguments,
1115 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001116 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001117 * tokens, or append a placeholder token for
1118 * an empty argument. */
1119 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001120 _glcpp_parser_expand_token_list (parser,
1121 argument);
1122 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001123 } else {
1124 token_t *new_token;
1125
1126 new_token = _token_create_ival (substituted,
1127 PLACEHOLDER,
1128 PLACEHOLDER);
1129 _token_list_append (substituted, new_token);
1130 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001131 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001132 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001133 }
1134 }
1135
Carl Worthad0dee62010-05-26 09:04:50 -07001136 /* After argument substitution, and before further expansion
1137 * below, implement token pasting. */
1138
Carl Worthb06096e2010-05-29 05:54:19 -07001139 _token_list_trim_trailing_space (substituted);
1140
Carl Worthad0dee62010-05-26 09:04:50 -07001141 node = substituted->head;
1142 while (node)
1143 {
1144 token_node_t *next_non_space;
1145
1146 /* Look ahead for a PASTE token, skipping space. */
1147 next_non_space = node->next;
1148 while (next_non_space && next_non_space->token->type == SPACE)
1149 next_non_space = next_non_space->next;
1150
1151 if (next_non_space == NULL)
1152 break;
1153
1154 if (next_non_space->token->type != PASTE) {
1155 node = next_non_space;
1156 continue;
1157 }
1158
1159 /* Now find the next non-space token after the PASTE. */
1160 next_non_space = next_non_space->next;
1161 while (next_non_space && next_non_space->token->type == SPACE)
1162 next_non_space = next_non_space->next;
1163
1164 if (next_non_space == NULL) {
1165 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001166 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001167 }
1168
Carl Worthb06096e2010-05-29 05:54:19 -07001169 node->token = _token_paste (node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001170 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001171 if (next_non_space == substituted->tail)
1172 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001173
1174 node = node->next;
1175 }
1176
Carl Worthb06096e2010-05-29 05:54:19 -07001177 substituted->non_space_tail = substituted->tail;
1178
Carl Worthae6517f2010-05-25 15:24:59 -07001179 _string_list_push (parser->active, identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001180 _glcpp_parser_expand_token_list (parser, substituted);
Carl Worthae6517f2010-05-25 15:24:59 -07001181 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001182
Carl Worth681afbc2010-05-28 15:06:02 -07001183 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001184}
1185
Carl Worth681afbc2010-05-28 15:06:02 -07001186/* Compute the complete expansion of node, (and subsequent nodes after
1187 * 'node' in the case that 'node' is a function-like macro and
1188 * subsequent nodes are arguments).
1189 *
1190 * Returns NULL if node is a simple token with no expansion.
1191 *
1192 * Otherwise, returns the token list that results from the expansion
1193 * and sets *last to the last node in the list that was consumed by
1194 * the expansion. Specificallty, *last will be set as follows:
1195 *
1196 * As 'node' in the case of object-like macro expansion.
1197 *
1198 * As the token of the closing right parenthesis in the case of
1199 * function-like macro expansion.
1200 */
1201static token_list_t *
1202_glcpp_parser_expand_node (glcpp_parser_t *parser,
1203 token_node_t *node,
1204 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001205{
Carl Worth681afbc2010-05-28 15:06:02 -07001206 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001207 const char *identifier;
1208 macro_t *macro;
1209 token_list_t *expansion;
1210
1211 /* We only expand identifiers */
1212 if (token->type != IDENTIFIER) {
1213 /* We change any COMMA into a COMMA_FINAL to prevent
1214 * it being mistaken for an argument separator
1215 * later. */
1216 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001217 token->type = COMMA_FINAL;
1218 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001219 }
Carl Worth681afbc2010-05-28 15:06:02 -07001220
1221 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001222 }
1223
1224 /* Look up this identifier in the hash table. */
1225 identifier = token->value.str;
1226 macro = hash_table_find (parser->defines, identifier);
1227
Carl Worth681afbc2010-05-28 15:06:02 -07001228 /* Not a macro, so no expansion needed. */
1229 if (macro == NULL)
1230 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001231
1232 /* Finally, don't expand this macro if we're already actively
1233 * expanding it, (to avoid infinite recursion). */
Carl Worth681afbc2010-05-28 15:06:02 -07001234 if (_string_list_contains (parser->active, identifier, NULL)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001235 /* We change the token type here from IDENTIFIER to
1236 * OTHER to prevent any future expansion of this
1237 * unexpanded token. */
1238 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001239 token_list_t *expansion;
1240 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001241
Carl Worth681afbc2010-05-28 15:06:02 -07001242 str = xtalloc_strdup (parser, token->value.str);
1243 final = _token_create_str (parser, OTHER, str);
1244 expansion = _token_list_create (parser);
1245 _token_list_append (expansion, final);
1246 *last = node;
1247 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001248 }
1249
Carl Worth681afbc2010-05-28 15:06:02 -07001250 if (! macro->is_function)
1251 {
1252 *last = node;
1253
1254 if (macro->replacements == NULL)
1255 return _token_list_create (parser);
1256
1257 expansion = _token_list_copy (parser, macro->replacements);
1258
Carl Worth3c93d392010-05-28 08:17:46 -07001259 _string_list_push (parser->active, identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001260 _glcpp_parser_expand_token_list (parser, expansion);
Carl Worth3c93d392010-05-28 08:17:46 -07001261 _string_list_pop (parser->active);
Carl Worth681afbc2010-05-28 15:06:02 -07001262
1263 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001264 }
Carl Worth681afbc2010-05-28 15:06:02 -07001265
1266 return _glcpp_parser_expand_function (parser, node, last);
1267}
1268
1269/* Walk over the token list replacing nodes with their expansion.
1270 * Whenever nodes are expanded the walking will walk over the new
1271 * nodes, continuing to expand as necessary. The results are placed in
1272 * 'list' itself;
1273 */
1274static void
1275_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1276 token_list_t *list)
1277{
1278 token_node_t *node_prev;
1279 token_node_t *node, *last;
1280 token_list_t *expansion;
1281
1282 if (list == NULL)
1283 return;
1284
1285 _token_list_trim_trailing_space (list);
1286
1287 node_prev = NULL;
1288 node = list->head;
1289
1290 while (node) {
1291 /* Find the expansion for node, which will replace all
1292 * nodes from node to last, inclusive. */
1293 expansion = _glcpp_parser_expand_node (parser, node, &last);
1294 if (expansion) {
1295 /* Splice expansion into list, supporting a
1296 * simple deletion if the expansion is
1297 * empty. */
1298 if (expansion->head) {
1299 if (node_prev)
1300 node_prev->next = expansion->head;
1301 else
1302 list->head = expansion->head;
1303 expansion->tail->next = last->next;
1304 if (last == list->tail)
1305 list->tail = expansion->tail;
1306 } else {
1307 if (node_prev)
1308 node_prev->next = last->next;
1309 else
1310 list->head = last->next;
1311 if (last == list->tail)
1312 list->tail == NULL;
1313 }
1314 } else {
1315 node_prev = node;
1316 }
1317 node = node_prev ? node_prev->next : list->head;
1318 }
1319
1320 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001321}
1322
Carl Worth0197e9b2010-05-26 08:05:19 -07001323static void
1324_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
1325 token_list_t *list,
1326 token_list_t *result)
1327{
Carl Worth681afbc2010-05-28 15:06:02 -07001328 _glcpp_parser_expand_token_list (parser, list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001329
Carl Worth681afbc2010-05-28 15:06:02 -07001330 _token_list_append_list (result, list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001331}
1332
Carl Worthae6517f2010-05-25 15:24:59 -07001333void
1334_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1335 token_list_t *list)
1336{
Carl Worthae6517f2010-05-25 15:24:59 -07001337 if (list == NULL)
1338 return;
1339
Carl Worth681afbc2010-05-28 15:06:02 -07001340 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001341
Carl Worth681afbc2010-05-28 15:06:02 -07001342 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001343
Carl Worth681afbc2010-05-28 15:06:02 -07001344 _token_list_print (list);
Carl Worthae6517f2010-05-25 15:24:59 -07001345}
1346
1347void
Carl Worthfcbbb462010-05-13 09:36:23 -07001348_define_object_macro (glcpp_parser_t *parser,
1349 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001350 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001351{
1352 macro_t *macro;
1353
1354 macro = xtalloc (parser, macro_t);
1355
1356 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001357 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001358 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001359 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001360
1361 hash_table_insert (parser->defines, macro, identifier);
1362}
1363
1364void
1365_define_function_macro (glcpp_parser_t *parser,
1366 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001367 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001368 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001369{
1370 macro_t *macro;
1371
1372 macro = xtalloc (parser, macro_t);
1373
1374 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001375 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001376 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001377 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001378
1379 hash_table_insert (parser->defines, macro, identifier);
1380}
1381
Carl Worth8f38aff2010-05-19 10:01:29 -07001382static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001383glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001384{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001385 token_node_t *node;
1386 int ret;
1387
Carl Worth95951ea2010-05-26 15:57:10 -07001388 if (parser->lex_from_list == NULL) {
1389 ret = glcpp_lex (parser->scanner);
1390
1391 /* XXX: This ugly block of code exists for the sole
1392 * purpose of converting a NEWLINE token into a SPACE
1393 * token, but only in the case where we have seen a
1394 * function-like macro name, but have not yet seen its
1395 * closing parenthesis.
1396 *
1397 * There's perhaps a more compact way to do this with
1398 * mid-rule actions in the grammar.
1399 *
1400 * I'm definitely not pleased with the complexity of
1401 * this code here.
1402 */
1403 if (parser->newline_as_space)
1404 {
1405 if (ret == '(') {
1406 parser->paren_count++;
1407 } else if (ret == ')') {
1408 parser->paren_count--;
1409 if (parser->paren_count == 0)
1410 parser->newline_as_space = 0;
1411 } else if (ret == NEWLINE) {
1412 ret = SPACE;
1413 } else if (ret != SPACE) {
1414 if (parser->paren_count == 0)
1415 parser->newline_as_space = 0;
1416 }
1417 }
1418 else if (parser->in_control_line)
1419 {
1420 if (ret == NEWLINE)
1421 parser->in_control_line = 0;
1422 }
1423 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1424 ret == HASH_UNDEF || ret == HASH_IF ||
1425 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1426 ret == HASH_ELIF || ret == HASH_ELSE ||
1427 ret == HASH_ENDIF || ret == HASH)
1428 {
1429 parser->in_control_line = 1;
1430 }
1431 else if (ret == IDENTIFIER)
1432 {
1433 macro_t *macro;
1434 macro = hash_table_find (parser->defines,
1435 yylval.str);
1436 if (macro && macro->is_function) {
1437 parser->newline_as_space = 1;
1438 parser->paren_count = 0;
1439 }
1440 }
1441
1442 return ret;
1443 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001444
1445 node = parser->lex_from_node;
1446
1447 if (node == NULL) {
1448 talloc_free (parser->lex_from_list);
1449 parser->lex_from_list = NULL;
1450 return NEWLINE;
1451 }
1452
1453 yylval = node->token->value;
1454 ret = node->token->type;
1455
1456 parser->lex_from_node = node->next;
1457
1458 return ret;
1459}
1460
1461static void
1462glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1463{
1464 token_node_t *node;
1465
1466 assert (parser->lex_from_list == NULL);
1467
1468 /* Copy list, eliminating any space tokens. */
1469 parser->lex_from_list = _token_list_create (parser);
1470
1471 for (node = list->head; node; node = node->next) {
1472 if (node->token->type == SPACE)
1473 continue;
1474 _token_list_append (parser->lex_from_list, node->token);
1475 }
1476
1477 talloc_free (list);
1478
1479 parser->lex_from_node = parser->lex_from_list->head;
1480
1481 /* It's possible the list consisted of nothing but whitespace. */
1482 if (parser->lex_from_node == NULL) {
1483 talloc_free (parser->lex_from_list);
1484 parser->lex_from_list = NULL;
1485 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001486}
Carl Worthb20d33c2010-05-20 22:27:07 -07001487
1488static void
1489_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1490{
1491 skip_type_t current = SKIP_NO_SKIP;
1492 skip_node_t *node;
1493
1494 if (parser->skip_stack)
1495 current = parser->skip_stack->type;
1496
1497 node = xtalloc (parser, skip_node_t);
1498
1499 if (current == SKIP_NO_SKIP) {
1500 if (condition)
1501 node->type = SKIP_NO_SKIP;
1502 else
1503 node->type = SKIP_TO_ELSE;
1504 } else {
1505 node->type = SKIP_TO_ENDIF;
1506 }
1507
1508 node->next = parser->skip_stack;
1509 parser->skip_stack = node;
1510}
1511
1512static void
1513_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1514 int condition)
1515{
1516 if (parser->skip_stack == NULL) {
1517 fprintf (stderr, "Error: %s without #if\n", type);
1518 exit (1);
1519 }
1520
1521 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1522 if (condition)
1523 parser->skip_stack->type = SKIP_NO_SKIP;
1524 } else {
1525 parser->skip_stack->type = SKIP_TO_ENDIF;
1526 }
1527}
Carl Worth80dc60b2010-05-25 14:42:00 -07001528
Carl Worthb20d33c2010-05-20 22:27:07 -07001529static void
1530_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1531{
1532 skip_node_t *node;
1533
1534 if (parser->skip_stack == NULL) {
1535 fprintf (stderr, "Error: #endif without #if\n");
1536 exit (1);
1537 }
1538
1539 node = parser->skip_stack;
1540 parser->skip_stack = node->next;
1541 talloc_free (node);
1542}