blob: b2684d06d98a3b0070430763251658766fc6f508 [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 Worthdd749002010-05-27 10:12:33 -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 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{
669 if (list->head == NULL) {
670 list->head = tail->head;
671 } else {
672 list->tail->next = tail->head;
673 }
674
675 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700676 list->non_space_tail = tail->non_space_tail;
677}
678
679void
680_token_list_trim_trailing_space (token_list_t *list)
681{
682 token_node_t *tail, *next;
683
684 if (list->non_space_tail) {
685 tail = list->non_space_tail->next;
686 list->non_space_tail->next = NULL;
687 list->tail = list->non_space_tail;
688
689 while (tail) {
690 next = tail->next;
691 talloc_free (tail);
692 tail = next;
693 }
694 }
Carl Worth47252442010-05-19 13:54:37 -0700695}
Carl Worth80dc60b2010-05-25 14:42:00 -0700696
Carl Worth0197e9b2010-05-26 08:05:19 -0700697static void
698_token_print (token_t *token)
699{
700 if (token->type < 256) {
701 printf ("%c", token->type);
702 return;
703 }
704
705 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700706 case INTEGER:
707 printf ("%" PRIxMAX, token->value.ival);
708 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700709 case IDENTIFIER:
710 case OTHER:
711 printf ("%s", token->value.str);
712 break;
713 case SPACE:
714 printf (" ");
715 break;
716 case LEFT_SHIFT:
717 printf ("<<");
718 break;
719 case RIGHT_SHIFT:
720 printf (">>");
721 break;
722 case LESS_OR_EQUAL:
723 printf ("<=");
724 break;
725 case GREATER_OR_EQUAL:
726 printf (">=");
727 break;
728 case EQUAL:
729 printf ("==");
730 break;
731 case NOT_EQUAL:
732 printf ("!=");
733 break;
734 case AND:
735 printf ("&&");
736 break;
737 case OR:
738 printf ("||");
739 break;
740 case PASTE:
741 printf ("##");
742 break;
Carl Worthdd749002010-05-27 10:12:33 -0700743 case COMMA_FINAL:
744 printf (",");
745 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700746 default:
747 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
748 break;
749 }
750}
751
Carl Worthad0dee62010-05-26 09:04:50 -0700752/* Change 'token' into a new token formed by pasting 'other'. */
753static void
754_token_paste (token_t *token, token_t *other)
755{
756 /* A very few single-character punctuators can be combined
757 * with another to form a multi-character punctuator. */
758 switch (token->type) {
759 case '<':
760 if (other->type == '<') {
761 token->type = LEFT_SHIFT;
762 token->value.ival = LEFT_SHIFT;
763 return;
764 } else if (other->type == '=') {
765 token->type = LESS_OR_EQUAL;
766 token->value.ival = LESS_OR_EQUAL;
767 return;
768 }
769 break;
770 case '>':
771 if (other->type == '>') {
772 token->type = RIGHT_SHIFT;
773 token->value.ival = RIGHT_SHIFT;
774 return;
775 } else if (other->type == '=') {
776 token->type = GREATER_OR_EQUAL;
777 token->value.ival = GREATER_OR_EQUAL;
778 return;
779 }
780 break;
781 case '=':
782 if (other->type == '=') {
783 token->type = EQUAL;
784 token->value.ival = EQUAL;
785 return;
786 }
787 break;
788 case '!':
789 if (other->type == '=') {
790 token->type = NOT_EQUAL;
791 token->value.ival = NOT_EQUAL;
792 return;
793 }
794 break;
795 case '&':
796 if (other->type == '&') {
797 token->type = AND;
798 token->value.ival = AND;
799 return;
800 }
801 break;
802 case '|':
803 if (other->type == '|') {
804 token->type = OR;
805 token->value.ival = OR;
806 return;
807 }
808 break;
809 }
810
811 /* Two string-valued tokens can usually just be mashed
812 * together.
813 *
814 * XXX: Since our 'OTHER' case is currently so loose, this may
815 * allow some things thruogh that should be treated as
816 * errors. */
817 if ((token->type == IDENTIFIER || token->type == OTHER) &&
818 (other->type == IDENTIFIER || other->type == OTHER))
819 {
820 token->value.str = talloc_strdup_append (token->value.str,
821 other->value.str);
822 return;
823 }
824
825 printf ("Error: Pasting \"");
826 _token_print (token);
827 printf ("\" and \"");
828 _token_print (other);
829 printf ("\" does not give a valid preprocessing token.\n");
830}
831
Carl Worth0197e9b2010-05-26 08:05:19 -0700832static void
833_token_list_print (token_list_t *list)
834{
835 token_node_t *node;
836
837 if (list == NULL)
838 return;
839
840 for (node = list->head; node; node = node->next)
841 _token_print (node->token);
842}
843
Carl Worth3a37b872010-05-10 11:44:09 -0700844void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700845yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700846{
847 fprintf (stderr, "Parse error: %s\n", error);
848}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700849
Carl Worth33cc4002010-05-12 12:17:10 -0700850glcpp_parser_t *
851glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700852{
Carl Worth33cc4002010-05-12 12:17:10 -0700853 glcpp_parser_t *parser;
854
Carl Worth5070a202010-05-12 12:45:33 -0700855 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700856
Carl Worth8f38aff2010-05-19 10:01:29 -0700857 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700858 parser->defines = hash_table_ctor (32, hash_table_string_hash,
859 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700860 parser->active = _string_list_create (parser);
Carl Worthf34a0002010-05-25 16:59:02 -0700861 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700862 parser->newline_as_space = 0;
863 parser->in_control_line = 0;
864 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700865
Carl Worthb20d33c2010-05-20 22:27:07 -0700866 parser->skip_stack = NULL;
867
Carl Worth8e82fcb2010-05-26 11:15:21 -0700868 parser->lex_from_list = NULL;
869 parser->lex_from_node = NULL;
870
Carl Worth33cc4002010-05-12 12:17:10 -0700871 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700872}
873
874int
875glcpp_parser_parse (glcpp_parser_t *parser)
876{
877 return yyparse (parser);
878}
879
880void
Carl Worth33cc4002010-05-12 12:17:10 -0700881glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700882{
Carl Worthb20d33c2010-05-20 22:27:07 -0700883 if (parser->skip_stack)
884 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700885 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700886 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700887 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700888}
Carl Worthc6d5af32010-05-11 12:30:09 -0700889
Carl Worth8e82fcb2010-05-26 11:15:21 -0700890/* Replace any occurences of DEFINED tokens in 'list' with either a
891 * '0' or '1' INTEGER token depending on whether the next token in the
892 * list is defined or not. */
893static void
894_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
895 token_list_t *list)
896{
897 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700898 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700899
900 if (list == NULL)
901 return;
902
903 for (node = list->head; node; node = node->next) {
904 if (node->token->type != DEFINED)
905 continue;
906 next = node->next;
907 while (next && next->token->type == SPACE)
908 next = next->next;
909 if (next == NULL || next->token->type != IDENTIFIER) {
910 fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
911 exit (1);
912 }
913 macro = hash_table_find (parser->defines,
914 next->token->value.str);
915
916 node->token->type = INTEGER;
917 node->token->value.ival = (macro != NULL);
918 node->next = next->next;
919 }
920}
921
922
Carl Worth0197e9b2010-05-26 08:05:19 -0700923/* Appends onto 'expansion' a non-macro token or the expansion of an
924 * object-like macro.
Carl Worthb1854fd2010-05-25 16:28:26 -0700925 *
Carl Worth0197e9b2010-05-26 08:05:19 -0700926 * Returns 0 if this token is completely processed.
Carl Worthb1854fd2010-05-25 16:28:26 -0700927 *
928 * Returns 1 in the case that 'token' is a function-like macro that
929 * needs further expansion.
930 */
931static int
Carl Worth7db24022010-05-26 17:01:57 -0700932_expand_token_onto (glcpp_parser_t *parser,
933 token_t *token,
934 token_list_t *result)
Carl Worthae6517f2010-05-25 15:24:59 -0700935{
936 const char *identifier;
937 macro_t *macro;
Carl Worth0197e9b2010-05-26 08:05:19 -0700938 token_list_t *expansion;
Carl Worthae6517f2010-05-25 15:24:59 -0700939
940 /* We only expand identifiers */
941 if (token->type != IDENTIFIER) {
Carl Worthdd749002010-05-27 10:12:33 -0700942 /* We change any COMMA into a COMMA_FINAL to prevent
943 * it being mistaken for an argument separator
944 * later. */
945 if (token->type == ',') {
946 token_t *new_token;
947
948 new_token = _token_create_ival (result, COMMA_FINAL,
949 COMMA_FINAL);
950 _token_list_append (result, new_token);
951 } else {
952 _token_list_append (result, token);
953 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700954 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700955 }
956
957 /* Look up this identifier in the hash table. */
958 identifier = token->value.str;
959 macro = hash_table_find (parser->defines, identifier);
960
Carl Worth0197e9b2010-05-26 08:05:19 -0700961 /* Not a macro, so just append. */
Carl Worthae6517f2010-05-25 15:24:59 -0700962 if (macro == NULL) {
Carl Worth0197e9b2010-05-26 08:05:19 -0700963 _token_list_append (result, token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700964 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700965 }
966
Carl Worthae6517f2010-05-25 15:24:59 -0700967 /* Finally, don't expand this macro if we're already actively
968 * expanding it, (to avoid infinite recursion). */
Carl Worthec4ada02010-05-26 08:15:49 -0700969 if (_string_list_contains (parser->active, identifier, NULL))
970 {
971 /* We change the token type here from IDENTIFIER to
972 * OTHER to prevent any future expansion of this
973 * unexpanded token. */
974 char *str;
975 token_t *new_token;
976
977 str = xtalloc_strdup (result, token->value.str);
978 new_token = _token_create_str (result, OTHER, str);
979 _token_list_append (result, new_token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700980 return 0;
981 }
982
Carl Worthc0607d52010-05-26 08:01:42 -0700983 /* For function-like macros return 1 for further processing. */
984 if (macro->is_function) {
985 return 1;
986 }
987
Carl Worthb1854fd2010-05-25 16:28:26 -0700988 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -0700989 _glcpp_parser_expand_token_list_onto (parser,
990 macro->replacements,
991 result);
Carl Worthb1854fd2010-05-25 16:28:26 -0700992 _string_list_pop (parser->active);
993
994 return 0;
995}
996
997typedef enum function_status
998{
999 FUNCTION_STATUS_SUCCESS,
1000 FUNCTION_NOT_A_FUNCTION,
1001 FUNCTION_UNBALANCED_PARENTHESES
1002} function_status_t;
1003
1004/* Find a set of function-like macro arguments by looking for a
1005 * balanced set of parentheses. Upon return *node will be the last
1006 * consumed node, such that further processing can continue with
1007 * node->next.
1008 *
1009 * Return values:
1010 *
1011 * FUNCTION_STATUS_SUCCESS:
1012 *
1013 * Successfully parsed a set of function arguments.
1014 *
1015 * FUNCTION_NOT_A_FUNCTION:
1016 *
1017 * Macro name not followed by a '('. This is not an error, but
1018 * simply that the macro name should be treated as a non-macro.
1019 *
1020 * FUNCTION_UNBLANCED_PARENTHESES
1021 *
1022 * Macro name is not followed by a balanced set of parentheses.
1023 */
1024static function_status_t
Carl Worth9ce18cf2010-05-25 17:32:21 -07001025_arguments_parse (argument_list_t *arguments, token_node_t **node_ret)
Carl Worthb1854fd2010-05-25 16:28:26 -07001026{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001027 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001028 token_node_t *node = *node_ret, *last;
1029 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001030
1031 last = node;
1032 node = node->next;
1033
1034 /* Ignore whitespace before first parenthesis. */
1035 while (node && node->token->type == SPACE)
1036 node = node->next;
1037
1038 if (node == NULL || node->token->type != '(')
1039 return FUNCTION_NOT_A_FUNCTION;
1040
Carl Worth652fa272010-05-25 17:45:22 -07001041 last = node;
1042 node = node->next;
1043
Carl Worth9ce18cf2010-05-25 17:32:21 -07001044 argument = NULL;
1045
Carl Worth652fa272010-05-25 17:45:22 -07001046 for (paren_count = 1; node; last = node, node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001047 if (node->token->type == '(')
1048 {
1049 paren_count++;
1050 }
1051 else if (node->token->type == ')')
1052 {
1053 paren_count--;
Carl Worthc7581c22010-05-25 17:41:07 -07001054 if (paren_count == 0) {
1055 last = node;
1056 node = node->next;
1057 break;
1058 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001059 }
Carl Worth652fa272010-05-25 17:45:22 -07001060
1061 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001062 paren_count == 1)
1063 {
Carl Worth10ae4382010-05-25 20:35:01 -07001064 if (argument)
1065 _token_list_trim_trailing_space (argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001066 argument = NULL;
1067 }
1068 else {
1069 if (argument == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001070 /* Don't treat initial whitespace as
1071 * part of the arguement. */
1072 if (node->token->type == SPACE)
1073 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001074 argument = _token_list_create (arguments);
1075 _argument_list_append (arguments, argument);
1076 }
1077 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001078 }
Carl Worthc7581c22010-05-25 17:41:07 -07001079 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001080
1081 if (node && paren_count)
1082 return FUNCTION_UNBALANCED_PARENTHESES;
1083
1084 *node_ret = last;
1085
1086 return FUNCTION_STATUS_SUCCESS;
1087}
1088
1089/* Prints the expansion of *node (consuming further tokens from the
1090 * list as necessary). Upon return *node will be the last consumed
1091 * node, such that further processing can continue with node->next. */
Carl Worth7db24022010-05-26 17:01:57 -07001092static function_status_t
1093_expand_function_onto (glcpp_parser_t *parser,
1094 token_node_t **node_ret,
1095 token_list_t *result)
Carl Worthb1854fd2010-05-25 16:28:26 -07001096{
1097 macro_t *macro;
1098 token_node_t *node;
1099 const char *identifier;
1100 argument_list_t *arguments;
1101 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001102 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001103 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001104
1105 node = *node_ret;
1106 identifier = node->token->value.str;
1107
1108 macro = hash_table_find (parser->defines, identifier);
1109
1110 assert (macro->is_function);
1111
Carl Worth9ce18cf2010-05-25 17:32:21 -07001112 arguments = _argument_list_create (parser);
1113 status = _arguments_parse (arguments, node_ret);
Carl Worthb1854fd2010-05-25 16:28:26 -07001114
1115 switch (status) {
1116 case FUNCTION_STATUS_SUCCESS:
1117 break;
1118 case FUNCTION_NOT_A_FUNCTION:
Carl Worth0197e9b2010-05-26 08:05:19 -07001119 _token_list_append (result, node->token);
Carl Worth7db24022010-05-26 17:01:57 -07001120 return FUNCTION_NOT_A_FUNCTION;
Carl Worthb1854fd2010-05-25 16:28:26 -07001121 case FUNCTION_UNBALANCED_PARENTHESES:
1122 fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n",
1123 identifier);
1124 exit (1);
Carl Worthae6517f2010-05-25 15:24:59 -07001125 }
1126
Carl Worth9ce18cf2010-05-25 17:32:21 -07001127 if (macro->replacements == NULL) {
1128 talloc_free (arguments);
Carl Worth7db24022010-05-26 17:01:57 -07001129 return FUNCTION_STATUS_SUCCESS;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001130 }
1131
Carl Worth9ce18cf2010-05-25 17:32:21 -07001132 if (_argument_list_length (arguments) !=
1133 _string_list_length (macro->parameters))
1134 {
1135 fprintf (stderr,
1136 "Error: macro %s invoked with %d arguments (expected %d)\n",
1137 identifier,
1138 _argument_list_length (arguments),
1139 _string_list_length (macro->parameters));
Carl Worth7db24022010-05-26 17:01:57 -07001140 exit (1);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001141 }
1142
Carl Worth0197e9b2010-05-26 08:05:19 -07001143 /* Perform argument substitution on the replacement list. */
1144 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001145
Carl Worthce540f22010-05-26 08:25:44 -07001146 for (node = macro->replacements->head; node; node = node->next)
1147 {
1148 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001149 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001150 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001151 &parameter_index))
1152 {
1153 token_list_t *argument;
1154 argument = _argument_list_member_at (arguments,
1155 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001156 /* Before substituting, we expand the argument
1157 * tokens. */
1158 _glcpp_parser_expand_token_list_onto (parser,
1159 argument,
1160 substituted);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001161 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001162 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001163 }
1164 }
1165
Carl Worthad0dee62010-05-26 09:04:50 -07001166 /* After argument substitution, and before further expansion
1167 * below, implement token pasting. */
1168
1169 node = substituted->head;
1170 while (node)
1171 {
1172 token_node_t *next_non_space;
1173
1174 /* Look ahead for a PASTE token, skipping space. */
1175 next_non_space = node->next;
1176 while (next_non_space && next_non_space->token->type == SPACE)
1177 next_non_space = next_non_space->next;
1178
1179 if (next_non_space == NULL)
1180 break;
1181
1182 if (next_non_space->token->type != PASTE) {
1183 node = next_non_space;
1184 continue;
1185 }
1186
1187 /* Now find the next non-space token after the PASTE. */
1188 next_non_space = next_non_space->next;
1189 while (next_non_space && next_non_space->token->type == SPACE)
1190 next_non_space = next_non_space->next;
1191
1192 if (next_non_space == NULL) {
1193 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
1194 exit (1);
1195 }
1196
1197 _token_paste (node->token, next_non_space->token);
1198 node->next = next_non_space->next;
1199
1200 node = node->next;
1201 }
1202
Carl Worthae6517f2010-05-25 15:24:59 -07001203 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -07001204 _glcpp_parser_expand_token_list_onto (parser, substituted, result);
Carl Worthae6517f2010-05-25 15:24:59 -07001205 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001206
1207 talloc_free (arguments);
Carl Worth7db24022010-05-26 17:01:57 -07001208
1209 return FUNCTION_STATUS_SUCCESS;
Carl Worthae6517f2010-05-25 15:24:59 -07001210}
1211
Carl Worth0197e9b2010-05-26 08:05:19 -07001212static void
1213_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
1214 token_list_t *list,
1215 token_list_t *result)
1216{
1217 token_node_t *node;
Carl Worth7db24022010-05-26 17:01:57 -07001218 token_list_t *intermediate, *list_orig = list;
1219 int i, need_rescan = 0;
Carl Worth0197e9b2010-05-26 08:05:19 -07001220
1221 if (list == NULL)
1222 return;
1223
Carl Worth7db24022010-05-26 17:01:57 -07001224 intermediate = _token_list_create (parser);
1225
1226 /* XXX: The two-pass expansion here is really ugly. The
1227 * problem this is solving is that we can expand a macro into
1228 * a function-like macro name, and then we need to recognize
1229 * that as a function-like macro, but perhaps the parentheses
1230 * and arguments aren't on the token list yet, (since they are
1231 * in the actual content so they are part of what we are
1232 * expanding.
1233 *
1234 * This ugly hack works, but is messy, fragile, and hard to
1235 * maintain. I think a cleaner solution would separate the
1236 * notions of expanding and appending and avoid this problem
1237 * altogether.
1238 */
1239
1240 for (i = 0; i < 2; i++) {
1241 if (i == 1) {
1242 list = intermediate;
1243 intermediate = _token_list_create (parser);
Carl Worth0197e9b2010-05-26 08:05:19 -07001244 }
Carl Worth7db24022010-05-26 17:01:57 -07001245 for (node = list->head; node; node = node->next)
1246 {
1247 if (_expand_token_onto (parser, node->token,
1248 intermediate))
1249 {
1250 if (_expand_function_onto (parser, &node,
1251 intermediate))
1252 {
1253 need_rescan = 1;
1254 }
1255 }
1256 }
1257 if (list != list_orig)
1258 talloc_free (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001259 }
Carl Worth7db24022010-05-26 17:01:57 -07001260
1261 _token_list_append_list (result, intermediate);
Carl Worth0197e9b2010-05-26 08:05:19 -07001262}
1263
Carl Worthae6517f2010-05-25 15:24:59 -07001264void
1265_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1266 token_list_t *list)
1267{
Carl Worth0197e9b2010-05-26 08:05:19 -07001268 token_list_t *expanded;
Carl Worthae6517f2010-05-25 15:24:59 -07001269 token_node_t *node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001270 function_status_t function_status;
Carl Worthae6517f2010-05-25 15:24:59 -07001271
1272 if (list == NULL)
1273 return;
1274
Carl Worth0197e9b2010-05-26 08:05:19 -07001275 expanded = _token_list_create (parser);
Carl Worth10ae4382010-05-25 20:35:01 -07001276
Carl Worth0197e9b2010-05-26 08:05:19 -07001277 _glcpp_parser_expand_token_list_onto (parser, list, expanded);
1278
1279 _token_list_trim_trailing_space (expanded);
1280
1281 _token_list_print (expanded);
1282
1283 talloc_free (expanded);
Carl Worthae6517f2010-05-25 15:24:59 -07001284}
1285
1286void
Carl Worthfcbbb462010-05-13 09:36:23 -07001287_define_object_macro (glcpp_parser_t *parser,
1288 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001289 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001290{
1291 macro_t *macro;
1292
1293 macro = xtalloc (parser, macro_t);
1294
1295 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001296 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001297 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001298 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001299
1300 hash_table_insert (parser->defines, macro, identifier);
1301}
1302
1303void
1304_define_function_macro (glcpp_parser_t *parser,
1305 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001306 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001307 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001308{
1309 macro_t *macro;
1310
1311 macro = xtalloc (parser, macro_t);
1312
1313 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001314 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001315 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001316 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001317
1318 hash_table_insert (parser->defines, macro, identifier);
1319}
1320
Carl Worth8f38aff2010-05-19 10:01:29 -07001321static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001322glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001323{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001324 token_node_t *node;
1325 int ret;
1326
Carl Worth95951ea2010-05-26 15:57:10 -07001327 if (parser->lex_from_list == NULL) {
1328 ret = glcpp_lex (parser->scanner);
1329
1330 /* XXX: This ugly block of code exists for the sole
1331 * purpose of converting a NEWLINE token into a SPACE
1332 * token, but only in the case where we have seen a
1333 * function-like macro name, but have not yet seen its
1334 * closing parenthesis.
1335 *
1336 * There's perhaps a more compact way to do this with
1337 * mid-rule actions in the grammar.
1338 *
1339 * I'm definitely not pleased with the complexity of
1340 * this code here.
1341 */
1342 if (parser->newline_as_space)
1343 {
1344 if (ret == '(') {
1345 parser->paren_count++;
1346 } else if (ret == ')') {
1347 parser->paren_count--;
1348 if (parser->paren_count == 0)
1349 parser->newline_as_space = 0;
1350 } else if (ret == NEWLINE) {
1351 ret = SPACE;
1352 } else if (ret != SPACE) {
1353 if (parser->paren_count == 0)
1354 parser->newline_as_space = 0;
1355 }
1356 }
1357 else if (parser->in_control_line)
1358 {
1359 if (ret == NEWLINE)
1360 parser->in_control_line = 0;
1361 }
1362 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1363 ret == HASH_UNDEF || ret == HASH_IF ||
1364 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1365 ret == HASH_ELIF || ret == HASH_ELSE ||
1366 ret == HASH_ENDIF || ret == HASH)
1367 {
1368 parser->in_control_line = 1;
1369 }
1370 else if (ret == IDENTIFIER)
1371 {
1372 macro_t *macro;
1373 macro = hash_table_find (parser->defines,
1374 yylval.str);
1375 if (macro && macro->is_function) {
1376 parser->newline_as_space = 1;
1377 parser->paren_count = 0;
1378 }
1379 }
1380
1381 return ret;
1382 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001383
1384 node = parser->lex_from_node;
1385
1386 if (node == NULL) {
1387 talloc_free (parser->lex_from_list);
1388 parser->lex_from_list = NULL;
1389 return NEWLINE;
1390 }
1391
1392 yylval = node->token->value;
1393 ret = node->token->type;
1394
1395 parser->lex_from_node = node->next;
1396
1397 return ret;
1398}
1399
1400static void
1401glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1402{
1403 token_node_t *node;
1404
1405 assert (parser->lex_from_list == NULL);
1406
1407 /* Copy list, eliminating any space tokens. */
1408 parser->lex_from_list = _token_list_create (parser);
1409
1410 for (node = list->head; node; node = node->next) {
1411 if (node->token->type == SPACE)
1412 continue;
1413 _token_list_append (parser->lex_from_list, node->token);
1414 }
1415
1416 talloc_free (list);
1417
1418 parser->lex_from_node = parser->lex_from_list->head;
1419
1420 /* It's possible the list consisted of nothing but whitespace. */
1421 if (parser->lex_from_node == NULL) {
1422 talloc_free (parser->lex_from_list);
1423 parser->lex_from_list = NULL;
1424 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001425}
Carl Worthb20d33c2010-05-20 22:27:07 -07001426
1427static void
1428_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1429{
1430 skip_type_t current = SKIP_NO_SKIP;
1431 skip_node_t *node;
1432
1433 if (parser->skip_stack)
1434 current = parser->skip_stack->type;
1435
1436 node = xtalloc (parser, skip_node_t);
1437
1438 if (current == SKIP_NO_SKIP) {
1439 if (condition)
1440 node->type = SKIP_NO_SKIP;
1441 else
1442 node->type = SKIP_TO_ELSE;
1443 } else {
1444 node->type = SKIP_TO_ENDIF;
1445 }
1446
1447 node->next = parser->skip_stack;
1448 parser->skip_stack = node;
1449}
1450
1451static void
1452_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1453 int condition)
1454{
1455 if (parser->skip_stack == NULL) {
1456 fprintf (stderr, "Error: %s without #if\n", type);
1457 exit (1);
1458 }
1459
1460 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1461 if (condition)
1462 parser->skip_stack->type = SKIP_NO_SKIP;
1463 } else {
1464 parser->skip_stack->type = SKIP_TO_ENDIF;
1465 }
1466}
Carl Worth80dc60b2010-05-25 14:42:00 -07001467
Carl Worthb20d33c2010-05-20 22:27:07 -07001468static void
1469_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1470{
1471 skip_node_t *node;
1472
1473 if (parser->skip_stack == NULL) {
1474 fprintf (stderr, "Error: #endif without #if\n");
1475 exit (1);
1476 }
1477
1478 node = parser->skip_stack;
1479 parser->skip_stack = node->next;
1480 talloc_free (node);
1481}