blob: 1c7c84dac7a7690257bb84460162961bc92a291f [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 int
Carl Worth610053b2010-05-14 10:05:11 -070056_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070057
Carl Worth5aa7ea02010-05-25 18:39:43 -070058static int
Carl Worth610053b2010-05-14 10:05:11 -070059_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070060
Carl Worth5aa7ea02010-05-25 18:39:43 -070061static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070062_argument_list_create (void *ctx);
63
Carl Worth5aa7ea02010-05-25 18:39:43 -070064static void
Carl Worth47252442010-05-19 13:54:37 -070065_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070066
Carl Worth5aa7ea02010-05-25 18:39:43 -070067static int
Carl Worth8f6a8282010-05-14 10:44:19 -070068_argument_list_length (argument_list_t *list);
69
Carl Worth5aa7ea02010-05-25 18:39:43 -070070static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070071_argument_list_member_at (argument_list_t *list, int index);
72
Carl Worth808401f2010-05-25 14:52:43 -070073/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070074static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070075_token_create_str (void *ctx, int type, char *str);
76
Carl Worth5aa7ea02010-05-25 18:39:43 -070077static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070078_token_create_ival (void *ctx, int type, int ival);
79
Carl Worth5aa7ea02010-05-25 18:39:43 -070080static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070081_token_list_create (void *ctx);
82
Carl Worthb1ae61a2010-05-26 08:10:38 -070083/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070084 *
85 * You may want to talloc_unlink any current reference if you no
86 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070087static void
Carl Worth808401f2010-05-25 14:52:43 -070088_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070089
Carl Worth5aa7ea02010-05-25 18:39:43 -070090static void
Carl Worth47252442010-05-19 13:54:37 -070091_token_list_append_list (token_list_t *list, token_list_t *tail);
92
Carl Worth22b3ace2010-06-02 15:32:03 -070093static int
94_token_list_length (token_list_t *list);
95
96static active_list_t *
97_active_list_push (active_list_t *list,
98 const char *identifier,
99 token_node_t *marker);
100
101static active_list_t *
102_active_list_pop (active_list_t *list);
103
104int
105_active_list_contains (active_list_t *list, const char *identifier);
106
Carl Worth5aa7ea02010-05-25 18:39:43 -0700107static void
Carl Worth8e82fcb2010-05-26 11:15:21 -0700108_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
109 token_list_t *list);
110
111static void
Carl Worth681afbc2010-05-28 15:06:02 -0700112_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
113 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700114
Carl Worthaaa9acb2010-05-19 13:28:24 -0700115static void
Carl Worth681afbc2010-05-28 15:06:02 -0700116_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
117 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700118
119static void
Carl Worthb20d33c2010-05-20 22:27:07 -0700120_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
121
122static void
123_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
124 int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700125
Carl Worthb20d33c2010-05-20 22:27:07 -0700126static void
127_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
128
Carl Worth0293b2e2010-05-19 10:05:40 -0700129#define yylex glcpp_parser_lex
130
Carl Worth8f38aff2010-05-19 10:01:29 -0700131static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700132glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700133
Carl Worth8e82fcb2010-05-26 11:15:21 -0700134static void
135glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
136
Carl Worth3a37b872010-05-10 11:44:09 -0700137%}
138
Carl Worth0b27b5f2010-05-10 16:16:06 -0700139%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700140%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700141
Carl Worth050e3de2010-05-27 14:36:29 -0700142%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 -0700143%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700144%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700145%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700146%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700147%type <token> preprocessing_token
148%type <token_list> pp_tokens replacement_list text_line
Carl Worth8fed1cd2010-05-26 09:32:12 -0700149%left OR
150%left AND
151%left '|'
152%left '^'
153%left '&'
154%left EQUAL NOT_EQUAL
155%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
156%left LEFT_SHIFT RIGHT_SHIFT
157%left '+' '-'
158%left '*' '/' '%'
159%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700160
161%%
162
Carl Worth33cc4002010-05-12 12:17:10 -0700163input:
Carl Worth3ff81672010-05-25 13:09:03 -0700164 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700165| input line
166;
167
168line:
169 control_line {
Carl Wortha771a402010-06-01 11:20:18 -0700170 printf ("\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700171 }
Carl Worth808401f2010-05-25 14:52:43 -0700172| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700173 _glcpp_parser_print_expanded_token_list (parser, $1);
174 printf ("\n");
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
479int
Carl Worth610053b2010-05-14 10:05:11 -0700480_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700481{
Carl Worth610053b2010-05-14 10:05:11 -0700482 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700483 int i;
484
485 if (list == NULL)
486 return 0;
487
488 for (i = 0, node = list->head; node; i++, node = node->next) {
489 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700490 if (index)
491 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700492 return 1;
493 }
494 }
495
496 return 0;
497}
498
499int
Carl Worth610053b2010-05-14 10:05:11 -0700500_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700501{
502 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700503 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700504
505 if (list == NULL)
506 return 0;
507
508 for (node = list->head; node; node = node->next)
509 length++;
510
511 return length;
512}
513
Carl Worth8f6a8282010-05-14 10:44:19 -0700514argument_list_t *
515_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700516{
Carl Worth8f6a8282010-05-14 10:44:19 -0700517 argument_list_t *list;
518
519 list = xtalloc (ctx, argument_list_t);
520 list->head = NULL;
521 list->tail = NULL;
522
523 return list;
524}
525
526void
Carl Worth47252442010-05-19 13:54:37 -0700527_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700528{
529 argument_node_t *node;
530
Carl Worth8f6a8282010-05-14 10:44:19 -0700531 node = xtalloc (list, argument_node_t);
532 node->argument = argument;
533
534 node->next = NULL;
535
536 if (list->head == NULL) {
537 list->head = node;
538 } else {
539 list->tail->next = node;
540 }
541
542 list->tail = node;
543}
544
545int
546_argument_list_length (argument_list_t *list)
547{
548 int length = 0;
549 argument_node_t *node;
550
551 if (list == NULL)
552 return 0;
553
554 for (node = list->head; node; node = node->next)
555 length++;
556
557 return length;
558}
559
Carl Worth47252442010-05-19 13:54:37 -0700560token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700561_argument_list_member_at (argument_list_t *list, int index)
562{
563 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700564 int i;
565
566 if (list == NULL)
567 return NULL;
568
569 node = list->head;
570 for (i = 0; i < index; i++) {
571 node = node->next;
572 if (node == NULL)
573 break;
574 }
575
576 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700577 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700578
579 return NULL;
580}
Carl Worth47252442010-05-19 13:54:37 -0700581
Carl Worth808401f2010-05-25 14:52:43 -0700582/* Note: This function talloc_steal()s the str pointer. */
583token_t *
584_token_create_str (void *ctx, int type, char *str)
585{
586 token_t *token;
587
588 token = xtalloc (ctx, token_t);
589 token->type = type;
590 token->value.str = talloc_steal (token, str);
591
592 return token;
593}
594
595token_t *
596_token_create_ival (void *ctx, int type, int ival)
597{
598 token_t *token;
599
600 token = xtalloc (ctx, token_t);
601 token->type = type;
602 token->value.ival = ival;
603
604 return token;
605}
606
Carl Worth47252442010-05-19 13:54:37 -0700607token_list_t *
608_token_list_create (void *ctx)
609{
610 token_list_t *list;
611
612 list = xtalloc (ctx, token_list_t);
613 list->head = NULL;
614 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700615 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700616
617 return list;
618}
619
620void
Carl Worth808401f2010-05-25 14:52:43 -0700621_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700622{
623 token_node_t *node;
624
625 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700626 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700627
628 node->next = NULL;
629
630 if (list->head == NULL) {
631 list->head = node;
632 } else {
633 list->tail->next = node;
634 }
635
636 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700637 if (token->type != SPACE)
638 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700639}
640
641void
642_token_list_append_list (token_list_t *list, token_list_t *tail)
643{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700644 if (tail == NULL || tail->head == NULL)
645 return;
646
Carl Worth47252442010-05-19 13:54:37 -0700647 if (list->head == NULL) {
648 list->head = tail->head;
649 } else {
650 list->tail->next = tail->head;
651 }
652
653 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700654 list->non_space_tail = tail->non_space_tail;
655}
656
Carl Worth681afbc2010-05-28 15:06:02 -0700657token_list_t *
658_token_list_copy (void *ctx, token_list_t *other)
659{
660 token_list_t *copy;
661 token_node_t *node;
662
663 if (other == NULL)
664 return NULL;
665
666 copy = _token_list_create (ctx);
667 for (node = other->head; node; node = node->next)
668 _token_list_append (copy, node->token);
669
670 return copy;
671}
672
Carl Worth10ae4382010-05-25 20:35:01 -0700673void
674_token_list_trim_trailing_space (token_list_t *list)
675{
676 token_node_t *tail, *next;
677
678 if (list->non_space_tail) {
679 tail = list->non_space_tail->next;
680 list->non_space_tail->next = NULL;
681 list->tail = list->non_space_tail;
682
683 while (tail) {
684 next = tail->next;
685 talloc_free (tail);
686 tail = next;
687 }
688 }
Carl Worth47252442010-05-19 13:54:37 -0700689}
Carl Worth80dc60b2010-05-25 14:42:00 -0700690
Carl Worth22b3ace2010-06-02 15:32:03 -0700691static int
692_token_list_length (token_list_t *list)
693{
694 int length = 0;
695 token_node_t *node;
696
697 if (list == NULL)
698 return 0;
699
700 for (node = list->head; node; node = node->next)
701 length++;
702
703 return length;
704}
705
Carl Worth0197e9b2010-05-26 08:05:19 -0700706static void
707_token_print (token_t *token)
708{
709 if (token->type < 256) {
710 printf ("%c", token->type);
711 return;
712 }
713
714 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700715 case INTEGER:
716 printf ("%" PRIxMAX, token->value.ival);
717 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700718 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700719 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700720 case OTHER:
721 printf ("%s", token->value.str);
722 break;
723 case SPACE:
724 printf (" ");
725 break;
726 case LEFT_SHIFT:
727 printf ("<<");
728 break;
729 case RIGHT_SHIFT:
730 printf (">>");
731 break;
732 case LESS_OR_EQUAL:
733 printf ("<=");
734 break;
735 case GREATER_OR_EQUAL:
736 printf (">=");
737 break;
738 case EQUAL:
739 printf ("==");
740 break;
741 case NOT_EQUAL:
742 printf ("!=");
743 break;
744 case AND:
745 printf ("&&");
746 break;
747 case OR:
748 printf ("||");
749 break;
750 case PASTE:
751 printf ("##");
752 break;
Carl Worthdd749002010-05-27 10:12:33 -0700753 case COMMA_FINAL:
754 printf (",");
755 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700756 case PLACEHOLDER:
757 /* Nothing to print. */
758 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700759 default:
760 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
761 break;
762 }
763}
764
Carl Worthb06096e2010-05-29 05:54:19 -0700765/* Return a new token (talloc()ed off of 'token') formed by pasting
766 * 'token' and 'other'. Note that this function may return 'token' or
767 * 'other' directly rather than allocating anything new.
768 *
769 * Caution: Only very cursory error-checking is performed to see if
770 * the final result is a valid single token. */
771static token_t *
Carl Worthad0dee62010-05-26 09:04:50 -0700772_token_paste (token_t *token, token_t *other)
773{
Carl Worth85b50e82010-05-27 14:01:18 -0700774 /* Pasting a placeholder onto anything makes no change. */
775 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700776 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700777
Carl Worthb06096e2010-05-29 05:54:19 -0700778 /* When 'token' is a placeholder, just return 'other'. */
779 if (token->type == PLACEHOLDER)
780 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700781
Carl Worthad0dee62010-05-26 09:04:50 -0700782 /* A very few single-character punctuators can be combined
783 * with another to form a multi-character punctuator. */
784 switch (token->type) {
785 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700786 if (other->type == '<')
787 return _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
788 else if (other->type == '=')
789 return _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700790 break;
791 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700792 if (other->type == '>')
793 return _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
794 else if (other->type == '=')
795 return _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700796 break;
797 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700798 if (other->type == '=')
799 return _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700800 break;
801 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700802 if (other->type == '=')
803 return _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700804 break;
805 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700806 if (other->type == '&')
807 return _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700808 break;
809 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700810 if (other->type == '|')
811 return _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700812 break;
813 }
814
815 /* Two string-valued tokens can usually just be mashed
816 * together.
817 *
Carl Worth050e3de2010-05-27 14:36:29 -0700818 * XXX: This isn't actually legitimate. Several things here
819 * should result in a diagnostic since the result cannot be a
820 * valid, single pre-processing token. For example, pasting
821 * "123" and "abc" is not legal, but we don't catch that
822 * here. */
823 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
824 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700825 {
Carl Worthb06096e2010-05-29 05:54:19 -0700826 char *str;
827
828 str = xtalloc_asprintf (token, "%s%s",
829 token->value.str, other->value.str);
830 return _token_create_str (token, token->type, str);
Carl Worthad0dee62010-05-26 09:04:50 -0700831 }
832
833 printf ("Error: Pasting \"");
834 _token_print (token);
835 printf ("\" and \"");
836 _token_print (other);
837 printf ("\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700838
839 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700840}
841
Carl Worth0197e9b2010-05-26 08:05:19 -0700842static void
843_token_list_print (token_list_t *list)
844{
845 token_node_t *node;
846
847 if (list == NULL)
848 return;
849
850 for (node = list->head; node; node = node->next)
851 _token_print (node->token);
852}
853
Carl Worth3a37b872010-05-10 11:44:09 -0700854void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700855yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700856{
857 fprintf (stderr, "Parse error: %s\n", error);
858}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700859
Carl Worth33cc4002010-05-12 12:17:10 -0700860glcpp_parser_t *
861glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700862{
Carl Worth33cc4002010-05-12 12:17:10 -0700863 glcpp_parser_t *parser;
864
Carl Worth5070a202010-05-12 12:45:33 -0700865 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700866
Carl Worth8f38aff2010-05-19 10:01:29 -0700867 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700868 parser->defines = hash_table_ctor (32, hash_table_string_hash,
869 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700870 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700871 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700872 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700873 parser->newline_as_space = 0;
874 parser->in_control_line = 0;
875 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700876
Carl Worthb20d33c2010-05-20 22:27:07 -0700877 parser->skip_stack = NULL;
878
Carl Worth8e82fcb2010-05-26 11:15:21 -0700879 parser->lex_from_list = NULL;
880 parser->lex_from_node = NULL;
881
Carl Worth33cc4002010-05-12 12:17:10 -0700882 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700883}
884
885int
886glcpp_parser_parse (glcpp_parser_t *parser)
887{
888 return yyparse (parser);
889}
890
891void
Carl Worth33cc4002010-05-12 12:17:10 -0700892glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700893{
Carl Worthb20d33c2010-05-20 22:27:07 -0700894 if (parser->skip_stack)
895 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700896 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700897 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700898 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700899}
Carl Worthc6d5af32010-05-11 12:30:09 -0700900
Carl Worth8e82fcb2010-05-26 11:15:21 -0700901/* Replace any occurences of DEFINED tokens in 'list' with either a
902 * '0' or '1' INTEGER token depending on whether the next token in the
903 * list is defined or not. */
904static void
905_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
906 token_list_t *list)
907{
908 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700909 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700910
911 if (list == NULL)
912 return;
913
914 for (node = list->head; node; node = node->next) {
915 if (node->token->type != DEFINED)
916 continue;
917 next = node->next;
918 while (next && next->token->type == SPACE)
919 next = next->next;
920 if (next == NULL || next->token->type != IDENTIFIER) {
921 fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
922 exit (1);
923 }
924 macro = hash_table_find (parser->defines,
925 next->token->value.str);
926
927 node->token->type = INTEGER;
928 node->token->value.ival = (macro != NULL);
929 node->next = next->next;
930 }
931}
932
Carl Worthb1854fd2010-05-25 16:28:26 -0700933typedef enum function_status
934{
935 FUNCTION_STATUS_SUCCESS,
936 FUNCTION_NOT_A_FUNCTION,
937 FUNCTION_UNBALANCED_PARENTHESES
938} function_status_t;
939
940/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700941 * balanced set of parentheses.
942 *
943 * When called, 'node' should be the opening-parenthesis token, (or
944 * perhaps preceeding SPACE tokens). Upon successful return *last will
945 * be the last consumed node, (corresponding to the closing right
946 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700947 *
948 * Return values:
949 *
950 * FUNCTION_STATUS_SUCCESS:
951 *
952 * Successfully parsed a set of function arguments.
953 *
954 * FUNCTION_NOT_A_FUNCTION:
955 *
956 * Macro name not followed by a '('. This is not an error, but
957 * simply that the macro name should be treated as a non-macro.
958 *
959 * FUNCTION_UNBLANCED_PARENTHESES
960 *
961 * Macro name is not followed by a balanced set of parentheses.
962 */
963static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700964_arguments_parse (argument_list_t *arguments,
965 token_node_t *node,
966 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700967{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700968 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700969 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700970
Carl Worthb1854fd2010-05-25 16:28:26 -0700971 node = node->next;
972
973 /* Ignore whitespace before first parenthesis. */
974 while (node && node->token->type == SPACE)
975 node = node->next;
976
977 if (node == NULL || node->token->type != '(')
978 return FUNCTION_NOT_A_FUNCTION;
979
Carl Worth652fa272010-05-25 17:45:22 -0700980 node = node->next;
981
Carl Wortha19297b2010-05-27 13:29:19 -0700982 argument = _token_list_create (arguments);
983 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -0700984
Carl Worth681afbc2010-05-28 15:06:02 -0700985 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -0700986 if (node->token->type == '(')
987 {
988 paren_count++;
989 }
990 else if (node->token->type == ')')
991 {
992 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -0700993 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -0700994 break;
Carl Worthb1854fd2010-05-25 16:28:26 -0700995 }
Carl Worth652fa272010-05-25 17:45:22 -0700996
997 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -0700998 paren_count == 1)
999 {
Carl Wortha19297b2010-05-27 13:29:19 -07001000 _token_list_trim_trailing_space (argument);
1001 argument = _token_list_create (arguments);
1002 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001003 }
1004 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001005 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001006 /* Don't treat initial whitespace as
1007 * part of the arguement. */
1008 if (node->token->type == SPACE)
1009 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001010 }
1011 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001012 }
Carl Worthc7581c22010-05-25 17:41:07 -07001013 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001014
Carl Worth681afbc2010-05-28 15:06:02 -07001015 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001016 return FUNCTION_UNBALANCED_PARENTHESES;
1017
Carl Worth681afbc2010-05-28 15:06:02 -07001018 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001019
1020 return FUNCTION_STATUS_SUCCESS;
1021}
1022
Carl Worth681afbc2010-05-28 15:06:02 -07001023/* This is a helper function that's essentially part of the
1024 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1025 * except for by that function.
1026 *
1027 * Returns NULL if node is a simple token with no expansion, (that is,
1028 * although 'node' corresponds to an identifier defined as a
1029 * function-like macro, it is not followed with a parenthesized
1030 * argument list).
1031 *
1032 * Compute the complete expansion of node (which is a function-like
1033 * macro) and subsequent nodes which are arguments.
1034 *
1035 * Returns the token list that results from the expansion and sets
1036 * *last to the last node in the list that was consumed by the
1037 * expansion. Specificallty, *last will be set as follows: as the
1038 * token of the closing right parenthesis.
1039 */
1040static token_list_t *
1041_glcpp_parser_expand_function (glcpp_parser_t *parser,
1042 token_node_t *node,
1043 token_node_t **last)
1044
Carl Worthb1854fd2010-05-25 16:28:26 -07001045{
1046 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001047 const char *identifier;
1048 argument_list_t *arguments;
1049 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001050 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001051 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001052
Carl Worthb1854fd2010-05-25 16:28:26 -07001053 identifier = node->token->value.str;
1054
1055 macro = hash_table_find (parser->defines, identifier);
1056
1057 assert (macro->is_function);
1058
Carl Worth9ce18cf2010-05-25 17:32:21 -07001059 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001060 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001061
1062 switch (status) {
1063 case FUNCTION_STATUS_SUCCESS:
1064 break;
1065 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001066 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001067 case FUNCTION_UNBALANCED_PARENTHESES:
Carl Worth681afbc2010-05-28 15:06:02 -07001068 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001069 }
1070
Carl Worth9ce18cf2010-05-25 17:32:21 -07001071 if (macro->replacements == NULL) {
1072 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001073 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001074 }
1075
Carl Wortha19297b2010-05-27 13:29:19 -07001076 if (! ((_argument_list_length (arguments) ==
1077 _string_list_length (macro->parameters)) ||
1078 (_string_list_length (macro->parameters) == 0 &&
1079 _argument_list_length (arguments) == 1 &&
1080 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001081 {
1082 fprintf (stderr,
1083 "Error: macro %s invoked with %d arguments (expected %d)\n",
1084 identifier,
1085 _argument_list_length (arguments),
1086 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001087 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001088 }
1089
Carl Worth0197e9b2010-05-26 08:05:19 -07001090 /* Perform argument substitution on the replacement list. */
1091 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001092
Carl Worthce540f22010-05-26 08:25:44 -07001093 for (node = macro->replacements->head; node; node = node->next)
1094 {
1095 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001096 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001097 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001098 &parameter_index))
1099 {
1100 token_list_t *argument;
1101 argument = _argument_list_member_at (arguments,
1102 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001103 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001104 * tokens, or append a placeholder token for
1105 * an empty argument. */
1106 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001107 _glcpp_parser_expand_token_list (parser,
1108 argument);
1109 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001110 } else {
1111 token_t *new_token;
1112
1113 new_token = _token_create_ival (substituted,
1114 PLACEHOLDER,
1115 PLACEHOLDER);
1116 _token_list_append (substituted, new_token);
1117 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001118 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001119 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001120 }
1121 }
1122
Carl Worthad0dee62010-05-26 09:04:50 -07001123 /* After argument substitution, and before further expansion
1124 * below, implement token pasting. */
1125
Carl Worthb06096e2010-05-29 05:54:19 -07001126 _token_list_trim_trailing_space (substituted);
1127
Carl Worthad0dee62010-05-26 09:04:50 -07001128 node = substituted->head;
1129 while (node)
1130 {
1131 token_node_t *next_non_space;
1132
1133 /* Look ahead for a PASTE token, skipping space. */
1134 next_non_space = node->next;
1135 while (next_non_space && next_non_space->token->type == SPACE)
1136 next_non_space = next_non_space->next;
1137
1138 if (next_non_space == NULL)
1139 break;
1140
1141 if (next_non_space->token->type != PASTE) {
1142 node = next_non_space;
1143 continue;
1144 }
1145
1146 /* Now find the next non-space token after the PASTE. */
1147 next_non_space = next_non_space->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 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001153 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001154 }
1155
Carl Worthb06096e2010-05-29 05:54:19 -07001156 node->token = _token_paste (node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001157 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001158 if (next_non_space == substituted->tail)
1159 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001160
1161 node = node->next;
1162 }
1163
Carl Worthb06096e2010-05-29 05:54:19 -07001164 substituted->non_space_tail = substituted->tail;
1165
Carl Worth681afbc2010-05-28 15:06:02 -07001166 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001167}
1168
Carl Worth681afbc2010-05-28 15:06:02 -07001169/* Compute the complete expansion of node, (and subsequent nodes after
1170 * 'node' in the case that 'node' is a function-like macro and
1171 * subsequent nodes are arguments).
1172 *
1173 * Returns NULL if node is a simple token with no expansion.
1174 *
1175 * Otherwise, returns the token list that results from the expansion
1176 * and sets *last to the last node in the list that was consumed by
1177 * the expansion. Specificallty, *last will be set as follows:
1178 *
1179 * As 'node' in the case of object-like macro expansion.
1180 *
1181 * As the token of the closing right parenthesis in the case of
1182 * function-like macro expansion.
1183 */
1184static token_list_t *
1185_glcpp_parser_expand_node (glcpp_parser_t *parser,
1186 token_node_t *node,
1187 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001188{
Carl Worth681afbc2010-05-28 15:06:02 -07001189 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001190 const char *identifier;
1191 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001192
1193 /* We only expand identifiers */
1194 if (token->type != IDENTIFIER) {
1195 /* We change any COMMA into a COMMA_FINAL to prevent
1196 * it being mistaken for an argument separator
1197 * later. */
1198 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001199 token->type = COMMA_FINAL;
1200 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001201 }
Carl Worth681afbc2010-05-28 15:06:02 -07001202
1203 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001204 }
1205
1206 /* Look up this identifier in the hash table. */
1207 identifier = token->value.str;
1208 macro = hash_table_find (parser->defines, identifier);
1209
Carl Worth681afbc2010-05-28 15:06:02 -07001210 /* Not a macro, so no expansion needed. */
1211 if (macro == NULL)
1212 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001213
1214 /* Finally, don't expand this macro if we're already actively
1215 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001216 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001217 /* We change the token type here from IDENTIFIER to
1218 * OTHER to prevent any future expansion of this
1219 * unexpanded token. */
1220 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001221 token_list_t *expansion;
1222 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001223
Carl Worth681afbc2010-05-28 15:06:02 -07001224 str = xtalloc_strdup (parser, token->value.str);
1225 final = _token_create_str (parser, OTHER, str);
1226 expansion = _token_list_create (parser);
1227 _token_list_append (expansion, final);
1228 *last = node;
1229 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001230 }
1231
Carl Worth681afbc2010-05-28 15:06:02 -07001232 if (! macro->is_function)
1233 {
1234 *last = node;
1235
1236 if (macro->replacements == NULL)
1237 return _token_list_create (parser);
1238
Carl Worth22b3ace2010-06-02 15:32:03 -07001239 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001240 }
Carl Worth681afbc2010-05-28 15:06:02 -07001241
1242 return _glcpp_parser_expand_function (parser, node, last);
1243}
1244
Carl Worth22b3ace2010-06-02 15:32:03 -07001245/* Push a new identifier onto the active list, returning the new list.
1246 *
1247 * Here, 'marker' is the token node that appears in the list after the
1248 * expansion of 'identifier'. That is, when the list iterator begins
1249 * examinging 'marker', then it is time to pop this node from the
1250 * active stack.
1251 */
1252active_list_t *
1253_active_list_push (active_list_t *list,
1254 const char *identifier,
1255 token_node_t *marker)
1256{
1257 active_list_t *node;
1258
1259 node = xtalloc (list, active_list_t);
1260 node->identifier = xtalloc_strdup (node, identifier);
1261 node->marker = marker;
1262 node->next = list;
1263
1264 return node;
1265}
1266
1267active_list_t *
1268_active_list_pop (active_list_t *list)
1269{
1270 active_list_t *node = list;
1271
1272 if (node == NULL)
1273 return NULL;
1274
1275 node = list->next;
1276 talloc_free (list);
1277
1278 return node;
1279}
1280
1281int
1282_active_list_contains (active_list_t *list, const char *identifier)
1283{
1284 active_list_t *node;
1285
1286 if (list == NULL)
1287 return 0;
1288
1289 for (node = list; node; node = node->next)
1290 if (strcmp (node->identifier, identifier) == 0)
1291 return 1;
1292
1293 return 0;
1294}
1295
Carl Worth681afbc2010-05-28 15:06:02 -07001296/* Walk over the token list replacing nodes with their expansion.
1297 * Whenever nodes are expanded the walking will walk over the new
1298 * nodes, continuing to expand as necessary. The results are placed in
1299 * 'list' itself;
1300 */
1301static void
1302_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1303 token_list_t *list)
1304{
1305 token_node_t *node_prev;
1306 token_node_t *node, *last;
1307 token_list_t *expansion;
1308
1309 if (list == NULL)
1310 return;
1311
1312 _token_list_trim_trailing_space (list);
1313
1314 node_prev = NULL;
1315 node = list->head;
1316
1317 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001318
1319 while (parser->active && parser->active->marker == node)
1320 parser->active = _active_list_pop (parser->active);
1321
Carl Worth681afbc2010-05-28 15:06:02 -07001322 /* Find the expansion for node, which will replace all
1323 * nodes from node to last, inclusive. */
1324 expansion = _glcpp_parser_expand_node (parser, node, &last);
1325 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001326 token_node_t *n;
1327
1328 for (n = node; n != last->next; n = n->next)
1329 while (parser->active &&
1330 parser->active->marker == n)
1331 {
1332 parser->active = _active_list_pop (parser->active);
1333 }
1334
1335 parser->active = _active_list_push (parser->active,
1336 node->token->value.str,
1337 last->next);
1338
Carl Worth681afbc2010-05-28 15:06:02 -07001339 /* Splice expansion into list, supporting a
1340 * simple deletion if the expansion is
1341 * empty. */
1342 if (expansion->head) {
1343 if (node_prev)
1344 node_prev->next = expansion->head;
1345 else
1346 list->head = expansion->head;
1347 expansion->tail->next = last->next;
1348 if (last == list->tail)
1349 list->tail = expansion->tail;
1350 } else {
1351 if (node_prev)
1352 node_prev->next = last->next;
1353 else
1354 list->head = last->next;
1355 if (last == list->tail)
1356 list->tail == NULL;
1357 }
1358 } else {
1359 node_prev = node;
1360 }
1361 node = node_prev ? node_prev->next : list->head;
1362 }
1363
Carl Worth22b3ace2010-06-02 15:32:03 -07001364 while (parser->active)
1365 parser->active = _active_list_pop (parser->active);
1366
Carl Worth681afbc2010-05-28 15:06:02 -07001367 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001368}
1369
Carl Worthae6517f2010-05-25 15:24:59 -07001370void
1371_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1372 token_list_t *list)
1373{
Carl Worthae6517f2010-05-25 15:24:59 -07001374 if (list == NULL)
1375 return;
1376
Carl Worth681afbc2010-05-28 15:06:02 -07001377 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001378
Carl Worth681afbc2010-05-28 15:06:02 -07001379 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001380
Carl Worth681afbc2010-05-28 15:06:02 -07001381 _token_list_print (list);
Carl Worthae6517f2010-05-25 15:24:59 -07001382}
1383
1384void
Carl Worthfcbbb462010-05-13 09:36:23 -07001385_define_object_macro (glcpp_parser_t *parser,
1386 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001387 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001388{
1389 macro_t *macro;
1390
1391 macro = xtalloc (parser, macro_t);
1392
1393 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001394 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001395 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001396 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001397
1398 hash_table_insert (parser->defines, macro, identifier);
1399}
1400
1401void
1402_define_function_macro (glcpp_parser_t *parser,
1403 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001404 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001405 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001406{
1407 macro_t *macro;
1408
1409 macro = xtalloc (parser, macro_t);
1410
1411 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001412 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001413 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001414 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001415
1416 hash_table_insert (parser->defines, macro, identifier);
1417}
1418
Carl Worth8f38aff2010-05-19 10:01:29 -07001419static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001420glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001421{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001422 token_node_t *node;
1423 int ret;
1424
Carl Worth95951ea2010-05-26 15:57:10 -07001425 if (parser->lex_from_list == NULL) {
1426 ret = glcpp_lex (parser->scanner);
1427
1428 /* XXX: This ugly block of code exists for the sole
1429 * purpose of converting a NEWLINE token into a SPACE
1430 * token, but only in the case where we have seen a
1431 * function-like macro name, but have not yet seen its
1432 * closing parenthesis.
1433 *
1434 * There's perhaps a more compact way to do this with
1435 * mid-rule actions in the grammar.
1436 *
1437 * I'm definitely not pleased with the complexity of
1438 * this code here.
1439 */
1440 if (parser->newline_as_space)
1441 {
1442 if (ret == '(') {
1443 parser->paren_count++;
1444 } else if (ret == ')') {
1445 parser->paren_count--;
1446 if (parser->paren_count == 0)
1447 parser->newline_as_space = 0;
1448 } else if (ret == NEWLINE) {
1449 ret = SPACE;
1450 } else if (ret != SPACE) {
1451 if (parser->paren_count == 0)
1452 parser->newline_as_space = 0;
1453 }
1454 }
1455 else if (parser->in_control_line)
1456 {
1457 if (ret == NEWLINE)
1458 parser->in_control_line = 0;
1459 }
1460 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1461 ret == HASH_UNDEF || ret == HASH_IF ||
1462 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1463 ret == HASH_ELIF || ret == HASH_ELSE ||
1464 ret == HASH_ENDIF || ret == HASH)
1465 {
1466 parser->in_control_line = 1;
1467 }
1468 else if (ret == IDENTIFIER)
1469 {
1470 macro_t *macro;
1471 macro = hash_table_find (parser->defines,
1472 yylval.str);
1473 if (macro && macro->is_function) {
1474 parser->newline_as_space = 1;
1475 parser->paren_count = 0;
1476 }
1477 }
1478
1479 return ret;
1480 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001481
1482 node = parser->lex_from_node;
1483
1484 if (node == NULL) {
1485 talloc_free (parser->lex_from_list);
1486 parser->lex_from_list = NULL;
1487 return NEWLINE;
1488 }
1489
1490 yylval = node->token->value;
1491 ret = node->token->type;
1492
1493 parser->lex_from_node = node->next;
1494
1495 return ret;
1496}
1497
1498static void
1499glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1500{
1501 token_node_t *node;
1502
1503 assert (parser->lex_from_list == NULL);
1504
1505 /* Copy list, eliminating any space tokens. */
1506 parser->lex_from_list = _token_list_create (parser);
1507
1508 for (node = list->head; node; node = node->next) {
1509 if (node->token->type == SPACE)
1510 continue;
1511 _token_list_append (parser->lex_from_list, node->token);
1512 }
1513
1514 talloc_free (list);
1515
1516 parser->lex_from_node = parser->lex_from_list->head;
1517
1518 /* It's possible the list consisted of nothing but whitespace. */
1519 if (parser->lex_from_node == NULL) {
1520 talloc_free (parser->lex_from_list);
1521 parser->lex_from_list = NULL;
1522 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001523}
Carl Worthb20d33c2010-05-20 22:27:07 -07001524
1525static void
1526_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1527{
1528 skip_type_t current = SKIP_NO_SKIP;
1529 skip_node_t *node;
1530
1531 if (parser->skip_stack)
1532 current = parser->skip_stack->type;
1533
1534 node = xtalloc (parser, skip_node_t);
1535
1536 if (current == SKIP_NO_SKIP) {
1537 if (condition)
1538 node->type = SKIP_NO_SKIP;
1539 else
1540 node->type = SKIP_TO_ELSE;
1541 } else {
1542 node->type = SKIP_TO_ENDIF;
1543 }
1544
1545 node->next = parser->skip_stack;
1546 parser->skip_stack = node;
1547}
1548
1549static void
1550_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1551 int condition)
1552{
1553 if (parser->skip_stack == NULL) {
1554 fprintf (stderr, "Error: %s without #if\n", type);
1555 exit (1);
1556 }
1557
1558 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1559 if (condition)
1560 parser->skip_stack->type = SKIP_NO_SKIP;
1561 } else {
1562 parser->skip_stack->type = SKIP_TO_ENDIF;
1563 }
1564}
Carl Worth80dc60b2010-05-25 14:42:00 -07001565
Carl Worthb20d33c2010-05-20 22:27:07 -07001566static void
1567_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1568{
1569 skip_node_t *node;
1570
1571 if (parser->skip_stack == NULL) {
1572 fprintf (stderr, "Error: #endif without #if\n");
1573 exit (1);
1574 }
1575
1576 node = parser->skip_stack;
1577 parser->skip_stack = node->next;
1578 talloc_free (node);
1579}