blob: d587a4bf338fb03c62ee1384b1b25513b6edd344 [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 Worthae6517f2010-05-25 15:24:59 -0700104_glcpp_parser_print_expanded_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 Worth0197e9b2010-05-26 08:05:19 -0700108_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
109 token_list_t *list,
110 token_list_t *result);
111
112static void
Carl Worthb20d33c2010-05-20 22:27:07 -0700113_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
114
115static void
116_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
117 int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700118
Carl Worthb20d33c2010-05-20 22:27:07 -0700119static void
120_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
121
Carl Worth0293b2e2010-05-19 10:05:40 -0700122#define yylex glcpp_parser_lex
123
Carl Worth8f38aff2010-05-19 10:01:29 -0700124static int
Carl Worth0293b2e2010-05-19 10:05:40 -0700125glcpp_parser_lex (glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700126
Carl Worth8e82fcb2010-05-26 11:15:21 -0700127static void
128glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
129
Carl Worth3a37b872010-05-10 11:44:09 -0700130%}
131
Carl Worth0b27b5f2010-05-10 16:16:06 -0700132%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700133%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700134
Carl Worth85b50e82010-05-27 14:01:18 -0700135%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 NEWLINE OTHER PLACEHOLDER SPACE
Carl Worth8fed1cd2010-05-26 09:32:12 -0700136%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700137%type <ival> expression INTEGER operator SPACE
Carl Worthe9397862010-05-25 17:08:07 -0700138%type <str> IDENTIFIER OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700139%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700140%type <token> preprocessing_token
141%type <token_list> pp_tokens replacement_list text_line
Carl Worth8fed1cd2010-05-26 09:32:12 -0700142%left OR
143%left AND
144%left '|'
145%left '^'
146%left '&'
147%left EQUAL NOT_EQUAL
148%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
149%left LEFT_SHIFT RIGHT_SHIFT
150%left '+' '-'
151%left '*' '/' '%'
152%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700153
154%%
155
Carl Worth33cc4002010-05-12 12:17:10 -0700156input:
Carl Worth3ff81672010-05-25 13:09:03 -0700157 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700158| input line
159;
160
161line:
162 control_line {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700163 if (parser->skip_stack == NULL ||
164 parser->skip_stack->type == SKIP_NO_SKIP)
165 {
166 printf ("\n");
167 }
Carl Worthae6517f2010-05-25 15:24:59 -0700168 }
Carl Worth808401f2010-05-25 14:52:43 -0700169| text_line {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700170 if (parser->skip_stack == NULL ||
171 parser->skip_stack->type == SKIP_NO_SKIP)
172 {
173 _glcpp_parser_print_expanded_token_list (parser, $1);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700174 printf ("\n");
Carl Worth8fed1cd2010-05-26 09:32:12 -0700175 }
Carl Worth808401f2010-05-25 14:52:43 -0700176 talloc_free ($1);
177 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700178| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700179| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700180;
181
Carl Worth8e82fcb2010-05-26 11:15:21 -0700182expanded_line:
183 IF_EXPANDED expression NEWLINE {
184 _glcpp_parser_skip_stack_push_if (parser, $2);
185 }
186| ELIF_EXPANDED expression NEWLINE {
187 _glcpp_parser_skip_stack_change_if (parser, "elif", $2);
188 }
189;
190
Carl Worth3ff81672010-05-25 13:09:03 -0700191control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700192 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Carl Worthae6517f2010-05-25 15:24:59 -0700193 _define_object_macro (parser, $2, $3);
194 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700195| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
196 _define_function_macro (parser, $2, NULL, $5);
197 }
198| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
199 _define_function_macro (parser, $2, $4, $6);
200 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700201| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700202 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700203 if (macro) {
204 /* XXX: Need hash table to support a real way
205 * to remove an element rather than prefixing
206 * a new node with data of NULL like this. */
207 hash_table_insert (parser->defines, NULL, $2);
208 talloc_free (macro);
209 }
210 talloc_free ($2);
211 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700212| HASH_IF pp_tokens NEWLINE {
213 token_list_t *expanded;
214 token_t *token;
215
216 expanded = _token_list_create (parser);
217 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
218 _token_list_append (expanded, token);
219 talloc_unlink (parser, token);
220 _glcpp_parser_evaluate_defined (parser, $2);
221 _glcpp_parser_expand_token_list_onto (parser, $2, expanded);
222 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);
243 _glcpp_parser_expand_token_list_onto (parser, $2, expanded);
244 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700245 }
246| HASH_ELSE NEWLINE {
247 _glcpp_parser_skip_stack_change_if (parser, "else", 1);
248 }
249| HASH_ENDIF NEWLINE {
250 _glcpp_parser_skip_stack_pop (parser);
251 }
Carl Worth3ff81672010-05-25 13:09:03 -0700252| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700253;
254
Carl Worth8fed1cd2010-05-26 09:32:12 -0700255expression:
256 INTEGER {
257 $$ = $1;
258 }
259| expression OR expression {
260 $$ = $1 || $3;
261 }
262| expression AND expression {
263 $$ = $1 && $3;
264 }
265| expression '|' expression {
266 $$ = $1 | $3;
267 }
268| expression '^' expression {
269 $$ = $1 ^ $3;
270 }
271| expression '&' expression {
272 $$ = $1 & $3;
273 }
274| expression NOT_EQUAL expression {
275 $$ = $1 != $3;
276 }
277| expression EQUAL expression {
278 $$ = $1 == $3;
279 }
280| expression GREATER_OR_EQUAL expression {
281 $$ = $1 >= $3;
282 }
283| expression LESS_OR_EQUAL expression {
284 $$ = $1 <= $3;
285 }
286| expression '>' expression {
287 $$ = $1 > $3;
288 }
289| expression '<' expression {
290 $$ = $1 < $3;
291 }
292| expression RIGHT_SHIFT expression {
293 $$ = $1 >> $3;
294 }
295| expression LEFT_SHIFT expression {
296 $$ = $1 << $3;
297 }
298| expression '-' expression {
299 $$ = $1 - $3;
300 }
301| expression '+' expression {
302 $$ = $1 + $3;
303 }
304| expression '%' expression {
305 $$ = $1 % $3;
306 }
307| expression '/' expression {
308 $$ = $1 / $3;
309 }
310| expression '*' expression {
311 $$ = $1 * $3;
312 }
313| '!' expression %prec UNARY {
314 $$ = ! $2;
315 }
316| '~' expression %prec UNARY {
317 $$ = ~ $2;
318 }
319| '-' expression %prec UNARY {
320 $$ = - $2;
321 }
322| '+' expression %prec UNARY {
323 $$ = + $2;
324 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700325| '(' expression ')' {
326 $$ = $2;
327 }
328;
329
Carl Worth3ff81672010-05-25 13:09:03 -0700330identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700331 IDENTIFIER {
332 $$ = _string_list_create (parser);
333 _string_list_append_item ($$, $1);
334 talloc_steal ($$, $1);
335 }
336| identifier_list ',' IDENTIFIER {
337 $$ = $1;
338 _string_list_append_item ($$, $3);
339 talloc_steal ($$, $3);
340 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700341;
342
Carl Worth3ff81672010-05-25 13:09:03 -0700343text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700344 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700345| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700346;
347
Carl Worth3ff81672010-05-25 13:09:03 -0700348non_directive:
349 pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700350;
351
Carl Worthaaa9acb2010-05-19 13:28:24 -0700352replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700353 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700354| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700355;
356
Carl Worthaaa9acb2010-05-19 13:28:24 -0700357pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700358 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700359 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700360 $$ = _token_list_create (parser);
361 _token_list_append ($$, $1);
362 talloc_unlink (parser, $1);
363 }
364| pp_tokens preprocessing_token {
365 $$ = $1;
366 _token_list_append ($$, $2);
367 talloc_unlink (parser, $2);
368 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700369;
370
Carl Worth3ff81672010-05-25 13:09:03 -0700371preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700372 IDENTIFIER {
373 $$ = _token_create_str (parser, IDENTIFIER, $1);
374 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700375| INTEGER {
376 $$ = _token_create_ival (parser, INTEGER, $1);
377 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700378| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700379 $$ = _token_create_ival (parser, $1, $1);
380 }
381| OTHER {
382 $$ = _token_create_str (parser, OTHER, $1);
383 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700384| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700385 $$ = _token_create_ival (parser, SPACE, SPACE);
Carl Worthb1854fd2010-05-25 16:28:26 -0700386 }
Carl Worth3ff81672010-05-25 13:09:03 -0700387;
388
Carl Worth8e82fcb2010-05-26 11:15:21 -0700389operator:
Carl Worth808401f2010-05-25 14:52:43 -0700390 '[' { $$ = '['; }
391| ']' { $$ = ']'; }
392| '(' { $$ = '('; }
393| ')' { $$ = ')'; }
394| '{' { $$ = '{'; }
395| '}' { $$ = '}'; }
396| '.' { $$ = '.'; }
397| '&' { $$ = '&'; }
398| '*' { $$ = '*'; }
399| '+' { $$ = '+'; }
400| '-' { $$ = '-'; }
401| '~' { $$ = '~'; }
402| '!' { $$ = '!'; }
403| '/' { $$ = '/'; }
404| '%' { $$ = '%'; }
405| LEFT_SHIFT { $$ = LEFT_SHIFT; }
406| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
407| '<' { $$ = '<'; }
408| '>' { $$ = '>'; }
409| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
410| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
411| EQUAL { $$ = EQUAL; }
412| NOT_EQUAL { $$ = NOT_EQUAL; }
413| '^' { $$ = '^'; }
414| '|' { $$ = '|'; }
415| AND { $$ = AND; }
416| OR { $$ = OR; }
417| ';' { $$ = ';'; }
418| ',' { $$ = ','; }
419| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700420| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700421;
422
Carl Worth33cc4002010-05-12 12:17:10 -0700423%%
424
Carl Worth610053b2010-05-14 10:05:11 -0700425string_list_t *
426_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700427{
Carl Worth610053b2010-05-14 10:05:11 -0700428 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700429
Carl Worth610053b2010-05-14 10:05:11 -0700430 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700431 list->head = NULL;
432 list->tail = NULL;
433
434 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700435}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700436
Carl Worth33cc4002010-05-12 12:17:10 -0700437void
Carl Worth610053b2010-05-14 10:05:11 -0700438_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700439{
440 if (list->head == NULL) {
441 list->head = tail->head;
442 } else {
443 list->tail->next = tail->head;
444 }
445
446 list->tail = tail->tail;
447}
448
449void
Carl Worth610053b2010-05-14 10:05:11 -0700450_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700451{
Carl Worth610053b2010-05-14 10:05:11 -0700452 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700453
Carl Worth610053b2010-05-14 10:05:11 -0700454 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700455 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700456
Carl Worth33cc4002010-05-12 12:17:10 -0700457 node->next = NULL;
458
459 if (list->head == NULL) {
460 list->head = node;
461 } else {
462 list->tail->next = node;
463 }
464
465 list->tail = node;
466}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700467
Carl Worthae6517f2010-05-25 15:24:59 -0700468void
469_string_list_push (string_list_t *list, const char *str)
470{
471 string_node_t *node;
472
473 node = xtalloc (list, string_node_t);
474 node->str = xtalloc_strdup (node, str);
475 node->next = list->head;
476
477 if (list->tail == NULL) {
478 list->tail = node;
479 }
480 list->head = node;
481}
482
483void
484_string_list_pop (string_list_t *list)
485{
486 string_node_t *node;
487
488 node = list->head;
489
490 if (node == NULL) {
491 fprintf (stderr, "Internal error: _string_list_pop called on an empty list.\n");
492 exit (1);
493 }
494
495 list->head = node->next;
496 if (list->tail == node) {
497 assert (node->next == NULL);
498 list->tail = NULL;
499 }
500
501 talloc_free (node);
502}
503
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700504int
Carl Worth610053b2010-05-14 10:05:11 -0700505_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700506{
Carl Worth610053b2010-05-14 10:05:11 -0700507 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700508 int i;
509
510 if (list == NULL)
511 return 0;
512
513 for (i = 0, node = list->head; node; i++, node = node->next) {
514 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700515 if (index)
516 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700517 return 1;
518 }
519 }
520
521 return 0;
522}
523
524int
Carl Worth610053b2010-05-14 10:05:11 -0700525_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700526{
527 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700528 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700529
530 if (list == NULL)
531 return 0;
532
533 for (node = list->head; node; node = node->next)
534 length++;
535
536 return length;
537}
538
Carl Worth8f6a8282010-05-14 10:44:19 -0700539argument_list_t *
540_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700541{
Carl Worth8f6a8282010-05-14 10:44:19 -0700542 argument_list_t *list;
543
544 list = xtalloc (ctx, argument_list_t);
545 list->head = NULL;
546 list->tail = NULL;
547
548 return list;
549}
550
551void
Carl Worth47252442010-05-19 13:54:37 -0700552_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700553{
554 argument_node_t *node;
555
Carl Worth8f6a8282010-05-14 10:44:19 -0700556 node = xtalloc (list, argument_node_t);
557 node->argument = argument;
558
559 node->next = NULL;
560
561 if (list->head == NULL) {
562 list->head = node;
563 } else {
564 list->tail->next = node;
565 }
566
567 list->tail = node;
568}
569
570int
571_argument_list_length (argument_list_t *list)
572{
573 int length = 0;
574 argument_node_t *node;
575
576 if (list == NULL)
577 return 0;
578
579 for (node = list->head; node; node = node->next)
580 length++;
581
582 return length;
583}
584
Carl Worth47252442010-05-19 13:54:37 -0700585token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700586_argument_list_member_at (argument_list_t *list, int index)
587{
588 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700589 int i;
590
591 if (list == NULL)
592 return NULL;
593
594 node = list->head;
595 for (i = 0; i < index; i++) {
596 node = node->next;
597 if (node == NULL)
598 break;
599 }
600
601 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700602 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700603
604 return NULL;
605}
Carl Worth47252442010-05-19 13:54:37 -0700606
Carl Worth808401f2010-05-25 14:52:43 -0700607/* Note: This function talloc_steal()s the str pointer. */
608token_t *
609_token_create_str (void *ctx, int type, char *str)
610{
611 token_t *token;
612
613 token = xtalloc (ctx, token_t);
614 token->type = type;
615 token->value.str = talloc_steal (token, str);
616
617 return token;
618}
619
620token_t *
621_token_create_ival (void *ctx, int type, int ival)
622{
623 token_t *token;
624
625 token = xtalloc (ctx, token_t);
626 token->type = type;
627 token->value.ival = ival;
628
629 return token;
630}
631
Carl Worth47252442010-05-19 13:54:37 -0700632token_list_t *
633_token_list_create (void *ctx)
634{
635 token_list_t *list;
636
637 list = xtalloc (ctx, token_list_t);
638 list->head = NULL;
639 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700640 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700641
642 return list;
643}
644
645void
Carl Worth808401f2010-05-25 14:52:43 -0700646_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700647{
648 token_node_t *node;
649
650 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700651 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700652
653 node->next = NULL;
654
655 if (list->head == NULL) {
656 list->head = node;
657 } else {
658 list->tail->next = node;
659 }
660
661 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700662 if (token->type != SPACE)
663 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700664}
665
666void
667_token_list_append_list (token_list_t *list, token_list_t *tail)
668{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700669 if (tail == NULL || tail->head == NULL)
670 return;
671
Carl Worth47252442010-05-19 13:54:37 -0700672 if (list->head == NULL) {
673 list->head = tail->head;
674 } else {
675 list->tail->next = tail->head;
676 }
677
678 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700679 list->non_space_tail = tail->non_space_tail;
680}
681
682void
683_token_list_trim_trailing_space (token_list_t *list)
684{
685 token_node_t *tail, *next;
686
687 if (list->non_space_tail) {
688 tail = list->non_space_tail->next;
689 list->non_space_tail->next = NULL;
690 list->tail = list->non_space_tail;
691
692 while (tail) {
693 next = tail->next;
694 talloc_free (tail);
695 tail = next;
696 }
697 }
Carl Worth47252442010-05-19 13:54:37 -0700698}
Carl Worth80dc60b2010-05-25 14:42:00 -0700699
Carl Worth0197e9b2010-05-26 08:05:19 -0700700static void
701_token_print (token_t *token)
702{
703 if (token->type < 256) {
704 printf ("%c", token->type);
705 return;
706 }
707
708 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700709 case INTEGER:
710 printf ("%" PRIxMAX, token->value.ival);
711 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700712 case IDENTIFIER:
713 case OTHER:
714 printf ("%s", token->value.str);
715 break;
716 case SPACE:
717 printf (" ");
718 break;
719 case LEFT_SHIFT:
720 printf ("<<");
721 break;
722 case RIGHT_SHIFT:
723 printf (">>");
724 break;
725 case LESS_OR_EQUAL:
726 printf ("<=");
727 break;
728 case GREATER_OR_EQUAL:
729 printf (">=");
730 break;
731 case EQUAL:
732 printf ("==");
733 break;
734 case NOT_EQUAL:
735 printf ("!=");
736 break;
737 case AND:
738 printf ("&&");
739 break;
740 case OR:
741 printf ("||");
742 break;
743 case PASTE:
744 printf ("##");
745 break;
Carl Worthdd749002010-05-27 10:12:33 -0700746 case COMMA_FINAL:
747 printf (",");
748 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700749 case PLACEHOLDER:
750 /* Nothing to print. */
751 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700752 default:
753 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
754 break;
755 }
756}
757
Carl Worthad0dee62010-05-26 09:04:50 -0700758/* Change 'token' into a new token formed by pasting 'other'. */
759static void
760_token_paste (token_t *token, token_t *other)
761{
Carl Worth85b50e82010-05-27 14:01:18 -0700762 /* Pasting a placeholder onto anything makes no change. */
763 if (other->type == PLACEHOLDER)
764 return;
765
766 /* When 'token' is a placeholder, just return contents of 'other'. */
767 if (token->type == PLACEHOLDER) {
768 token->type = other->type;
769 token->value = other->value;
770 return;
771 }
772
Carl Worthad0dee62010-05-26 09:04:50 -0700773 /* A very few single-character punctuators can be combined
774 * with another to form a multi-character punctuator. */
775 switch (token->type) {
776 case '<':
777 if (other->type == '<') {
778 token->type = LEFT_SHIFT;
779 token->value.ival = LEFT_SHIFT;
780 return;
781 } else if (other->type == '=') {
782 token->type = LESS_OR_EQUAL;
783 token->value.ival = LESS_OR_EQUAL;
784 return;
785 }
786 break;
787 case '>':
788 if (other->type == '>') {
789 token->type = RIGHT_SHIFT;
790 token->value.ival = RIGHT_SHIFT;
791 return;
792 } else if (other->type == '=') {
793 token->type = GREATER_OR_EQUAL;
794 token->value.ival = GREATER_OR_EQUAL;
795 return;
796 }
797 break;
798 case '=':
799 if (other->type == '=') {
800 token->type = EQUAL;
801 token->value.ival = EQUAL;
802 return;
803 }
804 break;
805 case '!':
806 if (other->type == '=') {
807 token->type = NOT_EQUAL;
808 token->value.ival = NOT_EQUAL;
809 return;
810 }
811 break;
812 case '&':
813 if (other->type == '&') {
814 token->type = AND;
815 token->value.ival = AND;
816 return;
817 }
818 break;
819 case '|':
820 if (other->type == '|') {
821 token->type = OR;
822 token->value.ival = OR;
823 return;
824 }
825 break;
826 }
827
828 /* Two string-valued tokens can usually just be mashed
829 * together.
830 *
831 * XXX: Since our 'OTHER' case is currently so loose, this may
832 * allow some things thruogh that should be treated as
833 * errors. */
834 if ((token->type == IDENTIFIER || token->type == OTHER) &&
835 (other->type == IDENTIFIER || other->type == OTHER))
836 {
837 token->value.str = talloc_strdup_append (token->value.str,
838 other->value.str);
839 return;
840 }
841
842 printf ("Error: Pasting \"");
843 _token_print (token);
844 printf ("\" and \"");
845 _token_print (other);
846 printf ("\" does not give a valid preprocessing token.\n");
847}
848
Carl Worth0197e9b2010-05-26 08:05:19 -0700849static void
850_token_list_print (token_list_t *list)
851{
852 token_node_t *node;
853
854 if (list == NULL)
855 return;
856
857 for (node = list->head; node; node = node->next)
858 _token_print (node->token);
859}
860
Carl Worth3a37b872010-05-10 11:44:09 -0700861void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700862yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700863{
864 fprintf (stderr, "Parse error: %s\n", error);
865}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700866
Carl Worth33cc4002010-05-12 12:17:10 -0700867glcpp_parser_t *
868glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700869{
Carl Worth33cc4002010-05-12 12:17:10 -0700870 glcpp_parser_t *parser;
871
Carl Worth5070a202010-05-12 12:45:33 -0700872 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700873
Carl Worth8f38aff2010-05-19 10:01:29 -0700874 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700875 parser->defines = hash_table_ctor (32, hash_table_string_hash,
876 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700877 parser->active = _string_list_create (parser);
Carl Worthf34a0002010-05-25 16:59:02 -0700878 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700879 parser->newline_as_space = 0;
880 parser->in_control_line = 0;
881 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700882
Carl Worthb20d33c2010-05-20 22:27:07 -0700883 parser->skip_stack = NULL;
884
Carl Worth8e82fcb2010-05-26 11:15:21 -0700885 parser->lex_from_list = NULL;
886 parser->lex_from_node = NULL;
887
Carl Worth33cc4002010-05-12 12:17:10 -0700888 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700889}
890
891int
892glcpp_parser_parse (glcpp_parser_t *parser)
893{
894 return yyparse (parser);
895}
896
897void
Carl Worth33cc4002010-05-12 12:17:10 -0700898glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700899{
Carl Worthb20d33c2010-05-20 22:27:07 -0700900 if (parser->skip_stack)
901 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700902 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700903 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700904 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700905}
Carl Worthc6d5af32010-05-11 12:30:09 -0700906
Carl Worth8e82fcb2010-05-26 11:15:21 -0700907/* Replace any occurences of DEFINED tokens in 'list' with either a
908 * '0' or '1' INTEGER token depending on whether the next token in the
909 * list is defined or not. */
910static void
911_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
912 token_list_t *list)
913{
914 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700915 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700916
917 if (list == NULL)
918 return;
919
920 for (node = list->head; node; node = node->next) {
921 if (node->token->type != DEFINED)
922 continue;
923 next = node->next;
924 while (next && next->token->type == SPACE)
925 next = next->next;
926 if (next == NULL || next->token->type != IDENTIFIER) {
927 fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
928 exit (1);
929 }
930 macro = hash_table_find (parser->defines,
931 next->token->value.str);
932
933 node->token->type = INTEGER;
934 node->token->value.ival = (macro != NULL);
935 node->next = next->next;
936 }
937}
938
939
Carl Worth0197e9b2010-05-26 08:05:19 -0700940/* Appends onto 'expansion' a non-macro token or the expansion of an
941 * object-like macro.
Carl Worthb1854fd2010-05-25 16:28:26 -0700942 *
Carl Worth0197e9b2010-05-26 08:05:19 -0700943 * Returns 0 if this token is completely processed.
Carl Worthb1854fd2010-05-25 16:28:26 -0700944 *
945 * Returns 1 in the case that 'token' is a function-like macro that
946 * needs further expansion.
947 */
948static int
Carl Worth7db24022010-05-26 17:01:57 -0700949_expand_token_onto (glcpp_parser_t *parser,
950 token_t *token,
951 token_list_t *result)
Carl Worthae6517f2010-05-25 15:24:59 -0700952{
953 const char *identifier;
954 macro_t *macro;
Carl Worth0197e9b2010-05-26 08:05:19 -0700955 token_list_t *expansion;
Carl Worthae6517f2010-05-25 15:24:59 -0700956
957 /* We only expand identifiers */
958 if (token->type != IDENTIFIER) {
Carl Worthdd749002010-05-27 10:12:33 -0700959 /* We change any COMMA into a COMMA_FINAL to prevent
960 * it being mistaken for an argument separator
961 * later. */
962 if (token->type == ',') {
963 token_t *new_token;
964
965 new_token = _token_create_ival (result, COMMA_FINAL,
966 COMMA_FINAL);
967 _token_list_append (result, new_token);
968 } else {
969 _token_list_append (result, token);
970 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700971 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700972 }
973
974 /* Look up this identifier in the hash table. */
975 identifier = token->value.str;
976 macro = hash_table_find (parser->defines, identifier);
977
Carl Worth0197e9b2010-05-26 08:05:19 -0700978 /* Not a macro, so just append. */
Carl Worthae6517f2010-05-25 15:24:59 -0700979 if (macro == NULL) {
Carl Worth0197e9b2010-05-26 08:05:19 -0700980 _token_list_append (result, token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700981 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700982 }
983
Carl Worthae6517f2010-05-25 15:24:59 -0700984 /* Finally, don't expand this macro if we're already actively
985 * expanding it, (to avoid infinite recursion). */
Carl Worthec4ada02010-05-26 08:15:49 -0700986 if (_string_list_contains (parser->active, identifier, NULL))
987 {
988 /* We change the token type here from IDENTIFIER to
989 * OTHER to prevent any future expansion of this
990 * unexpanded token. */
991 char *str;
992 token_t *new_token;
993
994 str = xtalloc_strdup (result, token->value.str);
995 new_token = _token_create_str (result, OTHER, str);
996 _token_list_append (result, new_token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700997 return 0;
998 }
999
Carl Worthc0607d52010-05-26 08:01:42 -07001000 /* For function-like macros return 1 for further processing. */
1001 if (macro->is_function) {
1002 return 1;
1003 }
1004
Carl Worthb1854fd2010-05-25 16:28:26 -07001005 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -07001006 _glcpp_parser_expand_token_list_onto (parser,
1007 macro->replacements,
1008 result);
Carl Worthb1854fd2010-05-25 16:28:26 -07001009 _string_list_pop (parser->active);
1010
1011 return 0;
1012}
1013
1014typedef enum function_status
1015{
1016 FUNCTION_STATUS_SUCCESS,
1017 FUNCTION_NOT_A_FUNCTION,
1018 FUNCTION_UNBALANCED_PARENTHESES
1019} function_status_t;
1020
1021/* Find a set of function-like macro arguments by looking for a
1022 * balanced set of parentheses. Upon return *node will be the last
1023 * consumed node, such that further processing can continue with
1024 * node->next.
1025 *
1026 * Return values:
1027 *
1028 * FUNCTION_STATUS_SUCCESS:
1029 *
1030 * Successfully parsed a set of function arguments.
1031 *
1032 * FUNCTION_NOT_A_FUNCTION:
1033 *
1034 * Macro name not followed by a '('. This is not an error, but
1035 * simply that the macro name should be treated as a non-macro.
1036 *
1037 * FUNCTION_UNBLANCED_PARENTHESES
1038 *
1039 * Macro name is not followed by a balanced set of parentheses.
1040 */
1041static function_status_t
Carl Worth9ce18cf2010-05-25 17:32:21 -07001042_arguments_parse (argument_list_t *arguments, token_node_t **node_ret)
Carl Worthb1854fd2010-05-25 16:28:26 -07001043{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001044 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001045 token_node_t *node = *node_ret, *last;
1046 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001047
1048 last = node;
1049 node = node->next;
1050
1051 /* Ignore whitespace before first parenthesis. */
1052 while (node && node->token->type == SPACE)
1053 node = node->next;
1054
1055 if (node == NULL || node->token->type != '(')
1056 return FUNCTION_NOT_A_FUNCTION;
1057
Carl Worth652fa272010-05-25 17:45:22 -07001058 last = node;
1059 node = node->next;
1060
Carl Wortha19297b2010-05-27 13:29:19 -07001061 argument = _token_list_create (arguments);
1062 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001063
Carl Worth652fa272010-05-25 17:45:22 -07001064 for (paren_count = 1; node; last = node, node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001065 if (node->token->type == '(')
1066 {
1067 paren_count++;
1068 }
1069 else if (node->token->type == ')')
1070 {
1071 paren_count--;
Carl Worthc7581c22010-05-25 17:41:07 -07001072 if (paren_count == 0) {
1073 last = node;
1074 node = node->next;
1075 break;
1076 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001077 }
Carl Worth652fa272010-05-25 17:45:22 -07001078
1079 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001080 paren_count == 1)
1081 {
Carl Wortha19297b2010-05-27 13:29:19 -07001082 _token_list_trim_trailing_space (argument);
1083 argument = _token_list_create (arguments);
1084 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001085 }
1086 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001087 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001088 /* Don't treat initial whitespace as
1089 * part of the arguement. */
1090 if (node->token->type == SPACE)
1091 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001092 }
1093 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001094 }
Carl Worthc7581c22010-05-25 17:41:07 -07001095 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001096
1097 if (node && paren_count)
1098 return FUNCTION_UNBALANCED_PARENTHESES;
1099
1100 *node_ret = last;
1101
1102 return FUNCTION_STATUS_SUCCESS;
1103}
1104
1105/* Prints the expansion of *node (consuming further tokens from the
1106 * list as necessary). Upon return *node will be the last consumed
1107 * node, such that further processing can continue with node->next. */
Carl Worth7db24022010-05-26 17:01:57 -07001108static function_status_t
1109_expand_function_onto (glcpp_parser_t *parser,
1110 token_node_t **node_ret,
1111 token_list_t *result)
Carl Worthb1854fd2010-05-25 16:28:26 -07001112{
1113 macro_t *macro;
1114 token_node_t *node;
1115 const char *identifier;
1116 argument_list_t *arguments;
1117 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001118 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001119 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001120
1121 node = *node_ret;
1122 identifier = node->token->value.str;
1123
1124 macro = hash_table_find (parser->defines, identifier);
1125
1126 assert (macro->is_function);
1127
Carl Worth9ce18cf2010-05-25 17:32:21 -07001128 arguments = _argument_list_create (parser);
1129 status = _arguments_parse (arguments, node_ret);
Carl Worthb1854fd2010-05-25 16:28:26 -07001130
1131 switch (status) {
1132 case FUNCTION_STATUS_SUCCESS:
1133 break;
1134 case FUNCTION_NOT_A_FUNCTION:
Carl Worth0197e9b2010-05-26 08:05:19 -07001135 _token_list_append (result, node->token);
Carl Worth7db24022010-05-26 17:01:57 -07001136 return FUNCTION_NOT_A_FUNCTION;
Carl Worthb1854fd2010-05-25 16:28:26 -07001137 case FUNCTION_UNBALANCED_PARENTHESES:
1138 fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n",
1139 identifier);
1140 exit (1);
Carl Worthae6517f2010-05-25 15:24:59 -07001141 }
1142
Carl Worth9ce18cf2010-05-25 17:32:21 -07001143 if (macro->replacements == NULL) {
1144 talloc_free (arguments);
Carl Worth7db24022010-05-26 17:01:57 -07001145 return FUNCTION_STATUS_SUCCESS;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001146 }
1147
Carl Wortha19297b2010-05-27 13:29:19 -07001148 if (! ((_argument_list_length (arguments) ==
1149 _string_list_length (macro->parameters)) ||
1150 (_string_list_length (macro->parameters) == 0 &&
1151 _argument_list_length (arguments) == 1 &&
1152 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001153 {
1154 fprintf (stderr,
1155 "Error: macro %s invoked with %d arguments (expected %d)\n",
1156 identifier,
1157 _argument_list_length (arguments),
1158 _string_list_length (macro->parameters));
Carl Worth7db24022010-05-26 17:01:57 -07001159 exit (1);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001160 }
1161
Carl Worth0197e9b2010-05-26 08:05:19 -07001162 /* Perform argument substitution on the replacement list. */
1163 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001164
Carl Worthce540f22010-05-26 08:25:44 -07001165 for (node = macro->replacements->head; node; node = node->next)
1166 {
1167 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001168 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001169 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001170 &parameter_index))
1171 {
1172 token_list_t *argument;
1173 argument = _argument_list_member_at (arguments,
1174 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001175 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001176 * tokens, or append a placeholder token for
1177 * an empty argument. */
1178 if (argument->head) {
1179 _glcpp_parser_expand_token_list_onto (parser,
1180 argument,
1181 substituted);
1182 } else {
1183 token_t *new_token;
1184
1185 new_token = _token_create_ival (substituted,
1186 PLACEHOLDER,
1187 PLACEHOLDER);
1188 _token_list_append (substituted, new_token);
1189 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001190 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001191 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001192 }
1193 }
1194
Carl Worthad0dee62010-05-26 09:04:50 -07001195 /* After argument substitution, and before further expansion
1196 * below, implement token pasting. */
1197
1198 node = substituted->head;
1199 while (node)
1200 {
1201 token_node_t *next_non_space;
1202
1203 /* Look ahead for a PASTE token, skipping space. */
1204 next_non_space = node->next;
1205 while (next_non_space && next_non_space->token->type == SPACE)
1206 next_non_space = next_non_space->next;
1207
1208 if (next_non_space == NULL)
1209 break;
1210
1211 if (next_non_space->token->type != PASTE) {
1212 node = next_non_space;
1213 continue;
1214 }
1215
1216 /* Now find the next non-space token after the PASTE. */
1217 next_non_space = next_non_space->next;
1218 while (next_non_space && next_non_space->token->type == SPACE)
1219 next_non_space = next_non_space->next;
1220
1221 if (next_non_space == NULL) {
1222 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth85b50e82010-05-27 14:01:18 -07001223 return FUNCTION_STATUS_SUCCESS;
Carl Worthad0dee62010-05-26 09:04:50 -07001224 }
1225
1226 _token_paste (node->token, next_non_space->token);
1227 node->next = next_non_space->next;
1228
1229 node = node->next;
1230 }
1231
Carl Worthae6517f2010-05-25 15:24:59 -07001232 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -07001233 _glcpp_parser_expand_token_list_onto (parser, substituted, result);
Carl Worthae6517f2010-05-25 15:24:59 -07001234 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001235
1236 talloc_free (arguments);
Carl Worth7db24022010-05-26 17:01:57 -07001237
1238 return FUNCTION_STATUS_SUCCESS;
Carl Worthae6517f2010-05-25 15:24:59 -07001239}
1240
Carl Worth0197e9b2010-05-26 08:05:19 -07001241static void
1242_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
1243 token_list_t *list,
1244 token_list_t *result)
1245{
1246 token_node_t *node;
Carl Worth7db24022010-05-26 17:01:57 -07001247 token_list_t *intermediate, *list_orig = list;
1248 int i, need_rescan = 0;
Carl Worth0197e9b2010-05-26 08:05:19 -07001249
Carl Wortha65cf7b2010-05-27 11:55:36 -07001250 if (list == NULL || list->head == NULL)
Carl Worth0197e9b2010-05-26 08:05:19 -07001251 return;
1252
Carl Worth7db24022010-05-26 17:01:57 -07001253 intermediate = _token_list_create (parser);
1254
1255 /* XXX: The two-pass expansion here is really ugly. The
1256 * problem this is solving is that we can expand a macro into
1257 * a function-like macro name, and then we need to recognize
1258 * that as a function-like macro, but perhaps the parentheses
1259 * and arguments aren't on the token list yet, (since they are
1260 * in the actual content so they are part of what we are
1261 * expanding.
1262 *
1263 * This ugly hack works, but is messy, fragile, and hard to
1264 * maintain. I think a cleaner solution would separate the
1265 * notions of expanding and appending and avoid this problem
1266 * altogether.
1267 */
1268
1269 for (i = 0; i < 2; i++) {
1270 if (i == 1) {
1271 list = intermediate;
1272 intermediate = _token_list_create (parser);
Carl Worth0197e9b2010-05-26 08:05:19 -07001273 }
Carl Worth7db24022010-05-26 17:01:57 -07001274 for (node = list->head; node; node = node->next)
1275 {
1276 if (_expand_token_onto (parser, node->token,
1277 intermediate))
1278 {
1279 if (_expand_function_onto (parser, &node,
1280 intermediate))
1281 {
1282 need_rescan = 1;
1283 }
1284 }
1285 }
1286 if (list != list_orig)
1287 talloc_free (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001288 }
Carl Worth7db24022010-05-26 17:01:57 -07001289
1290 _token_list_append_list (result, intermediate);
Carl Worth0197e9b2010-05-26 08:05:19 -07001291}
1292
Carl Worthae6517f2010-05-25 15:24:59 -07001293void
1294_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1295 token_list_t *list)
1296{
Carl Worth0197e9b2010-05-26 08:05:19 -07001297 token_list_t *expanded;
Carl Worthae6517f2010-05-25 15:24:59 -07001298 token_node_t *node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001299 function_status_t function_status;
Carl Worthae6517f2010-05-25 15:24:59 -07001300
1301 if (list == NULL)
1302 return;
1303
Carl Worth0197e9b2010-05-26 08:05:19 -07001304 expanded = _token_list_create (parser);
Carl Worth10ae4382010-05-25 20:35:01 -07001305
Carl Worth0197e9b2010-05-26 08:05:19 -07001306 _glcpp_parser_expand_token_list_onto (parser, list, expanded);
1307
1308 _token_list_trim_trailing_space (expanded);
1309
1310 _token_list_print (expanded);
1311
1312 talloc_free (expanded);
Carl Worthae6517f2010-05-25 15:24:59 -07001313}
1314
1315void
Carl Worthfcbbb462010-05-13 09:36:23 -07001316_define_object_macro (glcpp_parser_t *parser,
1317 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001318 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001319{
1320 macro_t *macro;
1321
1322 macro = xtalloc (parser, macro_t);
1323
1324 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001325 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001326 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001327 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001328
1329 hash_table_insert (parser->defines, macro, identifier);
1330}
1331
1332void
1333_define_function_macro (glcpp_parser_t *parser,
1334 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001335 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001336 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001337{
1338 macro_t *macro;
1339
1340 macro = xtalloc (parser, macro_t);
1341
1342 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001343 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001344 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001345 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001346
1347 hash_table_insert (parser->defines, macro, identifier);
1348}
1349
Carl Worth8f38aff2010-05-19 10:01:29 -07001350static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001351glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001352{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001353 token_node_t *node;
1354 int ret;
1355
Carl Worth95951ea2010-05-26 15:57:10 -07001356 if (parser->lex_from_list == NULL) {
1357 ret = glcpp_lex (parser->scanner);
1358
1359 /* XXX: This ugly block of code exists for the sole
1360 * purpose of converting a NEWLINE token into a SPACE
1361 * token, but only in the case where we have seen a
1362 * function-like macro name, but have not yet seen its
1363 * closing parenthesis.
1364 *
1365 * There's perhaps a more compact way to do this with
1366 * mid-rule actions in the grammar.
1367 *
1368 * I'm definitely not pleased with the complexity of
1369 * this code here.
1370 */
1371 if (parser->newline_as_space)
1372 {
1373 if (ret == '(') {
1374 parser->paren_count++;
1375 } else if (ret == ')') {
1376 parser->paren_count--;
1377 if (parser->paren_count == 0)
1378 parser->newline_as_space = 0;
1379 } else if (ret == NEWLINE) {
1380 ret = SPACE;
1381 } else if (ret != SPACE) {
1382 if (parser->paren_count == 0)
1383 parser->newline_as_space = 0;
1384 }
1385 }
1386 else if (parser->in_control_line)
1387 {
1388 if (ret == NEWLINE)
1389 parser->in_control_line = 0;
1390 }
1391 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1392 ret == HASH_UNDEF || ret == HASH_IF ||
1393 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1394 ret == HASH_ELIF || ret == HASH_ELSE ||
1395 ret == HASH_ENDIF || ret == HASH)
1396 {
1397 parser->in_control_line = 1;
1398 }
1399 else if (ret == IDENTIFIER)
1400 {
1401 macro_t *macro;
1402 macro = hash_table_find (parser->defines,
1403 yylval.str);
1404 if (macro && macro->is_function) {
1405 parser->newline_as_space = 1;
1406 parser->paren_count = 0;
1407 }
1408 }
1409
1410 return ret;
1411 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001412
1413 node = parser->lex_from_node;
1414
1415 if (node == NULL) {
1416 talloc_free (parser->lex_from_list);
1417 parser->lex_from_list = NULL;
1418 return NEWLINE;
1419 }
1420
1421 yylval = node->token->value;
1422 ret = node->token->type;
1423
1424 parser->lex_from_node = node->next;
1425
1426 return ret;
1427}
1428
1429static void
1430glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1431{
1432 token_node_t *node;
1433
1434 assert (parser->lex_from_list == NULL);
1435
1436 /* Copy list, eliminating any space tokens. */
1437 parser->lex_from_list = _token_list_create (parser);
1438
1439 for (node = list->head; node; node = node->next) {
1440 if (node->token->type == SPACE)
1441 continue;
1442 _token_list_append (parser->lex_from_list, node->token);
1443 }
1444
1445 talloc_free (list);
1446
1447 parser->lex_from_node = parser->lex_from_list->head;
1448
1449 /* It's possible the list consisted of nothing but whitespace. */
1450 if (parser->lex_from_node == NULL) {
1451 talloc_free (parser->lex_from_list);
1452 parser->lex_from_list = NULL;
1453 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001454}
Carl Worthb20d33c2010-05-20 22:27:07 -07001455
1456static void
1457_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1458{
1459 skip_type_t current = SKIP_NO_SKIP;
1460 skip_node_t *node;
1461
1462 if (parser->skip_stack)
1463 current = parser->skip_stack->type;
1464
1465 node = xtalloc (parser, skip_node_t);
1466
1467 if (current == SKIP_NO_SKIP) {
1468 if (condition)
1469 node->type = SKIP_NO_SKIP;
1470 else
1471 node->type = SKIP_TO_ELSE;
1472 } else {
1473 node->type = SKIP_TO_ENDIF;
1474 }
1475
1476 node->next = parser->skip_stack;
1477 parser->skip_stack = node;
1478}
1479
1480static void
1481_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1482 int condition)
1483{
1484 if (parser->skip_stack == NULL) {
1485 fprintf (stderr, "Error: %s without #if\n", type);
1486 exit (1);
1487 }
1488
1489 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1490 if (condition)
1491 parser->skip_stack->type = SKIP_NO_SKIP;
1492 } else {
1493 parser->skip_stack->type = SKIP_TO_ENDIF;
1494 }
1495}
Carl Worth80dc60b2010-05-25 14:42:00 -07001496
Carl Worthb20d33c2010-05-20 22:27:07 -07001497static void
1498_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1499{
1500 skip_node_t *node;
1501
1502 if (parser->skip_stack == NULL) {
1503 fprintf (stderr, "Error: #endif without #if\n");
1504 exit (1);
1505 }
1506
1507 node = parser->skip_stack;
1508 parser->skip_stack = node->next;
1509 talloc_free (node);
1510}