blob: 1346b65aff6196a1b471158ade777688a2c3072f [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 Worth8e82fcb2010-05-26 11:15:21 -0700135%token 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;
743 default:
744 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
745 break;
746 }
747}
748
Carl Worthad0dee62010-05-26 09:04:50 -0700749/* Change 'token' into a new token formed by pasting 'other'. */
750static void
751_token_paste (token_t *token, token_t *other)
752{
753 /* A very few single-character punctuators can be combined
754 * with another to form a multi-character punctuator. */
755 switch (token->type) {
756 case '<':
757 if (other->type == '<') {
758 token->type = LEFT_SHIFT;
759 token->value.ival = LEFT_SHIFT;
760 return;
761 } else if (other->type == '=') {
762 token->type = LESS_OR_EQUAL;
763 token->value.ival = LESS_OR_EQUAL;
764 return;
765 }
766 break;
767 case '>':
768 if (other->type == '>') {
769 token->type = RIGHT_SHIFT;
770 token->value.ival = RIGHT_SHIFT;
771 return;
772 } else if (other->type == '=') {
773 token->type = GREATER_OR_EQUAL;
774 token->value.ival = GREATER_OR_EQUAL;
775 return;
776 }
777 break;
778 case '=':
779 if (other->type == '=') {
780 token->type = EQUAL;
781 token->value.ival = EQUAL;
782 return;
783 }
784 break;
785 case '!':
786 if (other->type == '=') {
787 token->type = NOT_EQUAL;
788 token->value.ival = NOT_EQUAL;
789 return;
790 }
791 break;
792 case '&':
793 if (other->type == '&') {
794 token->type = AND;
795 token->value.ival = AND;
796 return;
797 }
798 break;
799 case '|':
800 if (other->type == '|') {
801 token->type = OR;
802 token->value.ival = OR;
803 return;
804 }
805 break;
806 }
807
808 /* Two string-valued tokens can usually just be mashed
809 * together.
810 *
811 * XXX: Since our 'OTHER' case is currently so loose, this may
812 * allow some things thruogh that should be treated as
813 * errors. */
814 if ((token->type == IDENTIFIER || token->type == OTHER) &&
815 (other->type == IDENTIFIER || other->type == OTHER))
816 {
817 token->value.str = talloc_strdup_append (token->value.str,
818 other->value.str);
819 return;
820 }
821
822 printf ("Error: Pasting \"");
823 _token_print (token);
824 printf ("\" and \"");
825 _token_print (other);
826 printf ("\" does not give a valid preprocessing token.\n");
827}
828
Carl Worth0197e9b2010-05-26 08:05:19 -0700829static void
830_token_list_print (token_list_t *list)
831{
832 token_node_t *node;
833
834 if (list == NULL)
835 return;
836
837 for (node = list->head; node; node = node->next)
838 _token_print (node->token);
839}
840
Carl Worth3a37b872010-05-10 11:44:09 -0700841void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700842yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700843{
844 fprintf (stderr, "Parse error: %s\n", error);
845}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700846
Carl Worth33cc4002010-05-12 12:17:10 -0700847glcpp_parser_t *
848glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700849{
Carl Worth33cc4002010-05-12 12:17:10 -0700850 glcpp_parser_t *parser;
851
Carl Worth5070a202010-05-12 12:45:33 -0700852 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700853
Carl Worth8f38aff2010-05-19 10:01:29 -0700854 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700855 parser->defines = hash_table_ctor (32, hash_table_string_hash,
856 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700857 parser->active = _string_list_create (parser);
Carl Worthf34a0002010-05-25 16:59:02 -0700858 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700859 parser->newline_as_space = 0;
860 parser->in_control_line = 0;
861 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700862
Carl Worthb20d33c2010-05-20 22:27:07 -0700863 parser->skip_stack = NULL;
864
Carl Worth8e82fcb2010-05-26 11:15:21 -0700865 parser->lex_from_list = NULL;
866 parser->lex_from_node = NULL;
867
Carl Worth33cc4002010-05-12 12:17:10 -0700868 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700869}
870
871int
872glcpp_parser_parse (glcpp_parser_t *parser)
873{
874 return yyparse (parser);
875}
876
877void
Carl Worth33cc4002010-05-12 12:17:10 -0700878glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700879{
Carl Worthb20d33c2010-05-20 22:27:07 -0700880 if (parser->skip_stack)
881 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700882 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700883 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700884 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700885}
Carl Worthc6d5af32010-05-11 12:30:09 -0700886
Carl Worth8e82fcb2010-05-26 11:15:21 -0700887/* Replace any occurences of DEFINED tokens in 'list' with either a
888 * '0' or '1' INTEGER token depending on whether the next token in the
889 * list is defined or not. */
890static void
891_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
892 token_list_t *list)
893{
894 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700895 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700896
897 if (list == NULL)
898 return;
899
900 for (node = list->head; node; node = node->next) {
901 if (node->token->type != DEFINED)
902 continue;
903 next = node->next;
904 while (next && next->token->type == SPACE)
905 next = next->next;
906 if (next == NULL || next->token->type != IDENTIFIER) {
907 fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
908 exit (1);
909 }
910 macro = hash_table_find (parser->defines,
911 next->token->value.str);
912
913 node->token->type = INTEGER;
914 node->token->value.ival = (macro != NULL);
915 node->next = next->next;
916 }
917}
918
919
Carl Worth0197e9b2010-05-26 08:05:19 -0700920/* Appends onto 'expansion' a non-macro token or the expansion of an
921 * object-like macro.
Carl Worthb1854fd2010-05-25 16:28:26 -0700922 *
Carl Worth0197e9b2010-05-26 08:05:19 -0700923 * Returns 0 if this token is completely processed.
Carl Worthb1854fd2010-05-25 16:28:26 -0700924 *
925 * Returns 1 in the case that 'token' is a function-like macro that
926 * needs further expansion.
927 */
928static int
Carl Worth0197e9b2010-05-26 08:05:19 -0700929_glcpp_parser_expand_token_onto (glcpp_parser_t *parser,
930 token_t *token,
931 token_list_t *result)
Carl Worthae6517f2010-05-25 15:24:59 -0700932{
933 const char *identifier;
934 macro_t *macro;
Carl Worth0197e9b2010-05-26 08:05:19 -0700935 token_list_t *expansion;
Carl Worthae6517f2010-05-25 15:24:59 -0700936
937 /* We only expand identifiers */
938 if (token->type != IDENTIFIER) {
Carl Worth0197e9b2010-05-26 08:05:19 -0700939 _token_list_append (result, token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700940 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700941 }
942
943 /* Look up this identifier in the hash table. */
944 identifier = token->value.str;
945 macro = hash_table_find (parser->defines, identifier);
946
Carl Worth0197e9b2010-05-26 08:05:19 -0700947 /* Not a macro, so just append. */
Carl Worthae6517f2010-05-25 15:24:59 -0700948 if (macro == NULL) {
Carl Worth0197e9b2010-05-26 08:05:19 -0700949 _token_list_append (result, token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700950 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700951 }
952
Carl Worthae6517f2010-05-25 15:24:59 -0700953 /* Finally, don't expand this macro if we're already actively
954 * expanding it, (to avoid infinite recursion). */
Carl Worthec4ada02010-05-26 08:15:49 -0700955 if (_string_list_contains (parser->active, identifier, NULL))
956 {
957 /* We change the token type here from IDENTIFIER to
958 * OTHER to prevent any future expansion of this
959 * unexpanded token. */
960 char *str;
961 token_t *new_token;
962
963 str = xtalloc_strdup (result, token->value.str);
964 new_token = _token_create_str (result, OTHER, str);
965 _token_list_append (result, new_token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700966 return 0;
967 }
968
Carl Worthc0607d52010-05-26 08:01:42 -0700969 /* For function-like macros return 1 for further processing. */
970 if (macro->is_function) {
971 return 1;
972 }
973
Carl Worthb1854fd2010-05-25 16:28:26 -0700974 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -0700975 _glcpp_parser_expand_token_list_onto (parser,
976 macro->replacements,
977 result);
Carl Worthb1854fd2010-05-25 16:28:26 -0700978 _string_list_pop (parser->active);
979
980 return 0;
981}
982
983typedef enum function_status
984{
985 FUNCTION_STATUS_SUCCESS,
986 FUNCTION_NOT_A_FUNCTION,
987 FUNCTION_UNBALANCED_PARENTHESES
988} function_status_t;
989
990/* Find a set of function-like macro arguments by looking for a
991 * balanced set of parentheses. Upon return *node will be the last
992 * consumed node, such that further processing can continue with
993 * node->next.
994 *
995 * Return values:
996 *
997 * FUNCTION_STATUS_SUCCESS:
998 *
999 * Successfully parsed a set of function arguments.
1000 *
1001 * FUNCTION_NOT_A_FUNCTION:
1002 *
1003 * Macro name not followed by a '('. This is not an error, but
1004 * simply that the macro name should be treated as a non-macro.
1005 *
1006 * FUNCTION_UNBLANCED_PARENTHESES
1007 *
1008 * Macro name is not followed by a balanced set of parentheses.
1009 */
1010static function_status_t
Carl Worth9ce18cf2010-05-25 17:32:21 -07001011_arguments_parse (argument_list_t *arguments, token_node_t **node_ret)
Carl Worthb1854fd2010-05-25 16:28:26 -07001012{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001013 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001014 token_node_t *node = *node_ret, *last;
1015 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001016
1017 last = node;
1018 node = node->next;
1019
1020 /* Ignore whitespace before first parenthesis. */
1021 while (node && node->token->type == SPACE)
1022 node = node->next;
1023
1024 if (node == NULL || node->token->type != '(')
1025 return FUNCTION_NOT_A_FUNCTION;
1026
Carl Worth652fa272010-05-25 17:45:22 -07001027 last = node;
1028 node = node->next;
1029
Carl Worth9ce18cf2010-05-25 17:32:21 -07001030 argument = NULL;
1031
Carl Worth652fa272010-05-25 17:45:22 -07001032 for (paren_count = 1; node; last = node, node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001033 if (node->token->type == '(')
1034 {
1035 paren_count++;
1036 }
1037 else if (node->token->type == ')')
1038 {
1039 paren_count--;
Carl Worthc7581c22010-05-25 17:41:07 -07001040 if (paren_count == 0) {
1041 last = node;
1042 node = node->next;
1043 break;
1044 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001045 }
Carl Worth652fa272010-05-25 17:45:22 -07001046
1047 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001048 paren_count == 1)
1049 {
Carl Worth10ae4382010-05-25 20:35:01 -07001050 if (argument)
1051 _token_list_trim_trailing_space (argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001052 argument = NULL;
1053 }
1054 else {
1055 if (argument == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001056 /* Don't treat initial whitespace as
1057 * part of the arguement. */
1058 if (node->token->type == SPACE)
1059 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001060 argument = _token_list_create (arguments);
1061 _argument_list_append (arguments, argument);
1062 }
1063 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001064 }
Carl Worthc7581c22010-05-25 17:41:07 -07001065 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001066
1067 if (node && paren_count)
1068 return FUNCTION_UNBALANCED_PARENTHESES;
1069
1070 *node_ret = last;
1071
1072 return FUNCTION_STATUS_SUCCESS;
1073}
1074
1075/* Prints the expansion of *node (consuming further tokens from the
1076 * list as necessary). Upon return *node will be the last consumed
1077 * node, such that further processing can continue with node->next. */
1078static void
Carl Worth0197e9b2010-05-26 08:05:19 -07001079_glcpp_parser_expand_function_onto (glcpp_parser_t *parser,
1080 token_node_t **node_ret,
1081 token_list_t *result)
Carl Worthb1854fd2010-05-25 16:28:26 -07001082{
1083 macro_t *macro;
1084 token_node_t *node;
1085 const char *identifier;
1086 argument_list_t *arguments;
1087 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001088 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001089 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001090
1091 node = *node_ret;
1092 identifier = node->token->value.str;
1093
1094 macro = hash_table_find (parser->defines, identifier);
1095
1096 assert (macro->is_function);
1097
Carl Worth9ce18cf2010-05-25 17:32:21 -07001098 arguments = _argument_list_create (parser);
1099 status = _arguments_parse (arguments, node_ret);
Carl Worthb1854fd2010-05-25 16:28:26 -07001100
1101 switch (status) {
1102 case FUNCTION_STATUS_SUCCESS:
1103 break;
1104 case FUNCTION_NOT_A_FUNCTION:
Carl Worth0197e9b2010-05-26 08:05:19 -07001105 _token_list_append (result, node->token);
Carl Worthae6517f2010-05-25 15:24:59 -07001106 return;
Carl Worthb1854fd2010-05-25 16:28:26 -07001107 case FUNCTION_UNBALANCED_PARENTHESES:
1108 fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n",
1109 identifier);
1110 exit (1);
Carl Worthae6517f2010-05-25 15:24:59 -07001111 }
1112
Carl Worth9ce18cf2010-05-25 17:32:21 -07001113 if (macro->replacements == NULL) {
1114 talloc_free (arguments);
1115 return;
1116 }
1117
Carl Worth9ce18cf2010-05-25 17:32:21 -07001118 if (_argument_list_length (arguments) !=
1119 _string_list_length (macro->parameters))
1120 {
1121 fprintf (stderr,
1122 "Error: macro %s invoked with %d arguments (expected %d)\n",
1123 identifier,
1124 _argument_list_length (arguments),
1125 _string_list_length (macro->parameters));
1126 return;
1127 }
1128
Carl Worth0197e9b2010-05-26 08:05:19 -07001129 /* Perform argument substitution on the replacement list. */
1130 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001131
Carl Worthce540f22010-05-26 08:25:44 -07001132 for (node = macro->replacements->head; node; node = node->next)
1133 {
1134 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001135 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001136 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001137 &parameter_index))
1138 {
1139 token_list_t *argument;
1140 argument = _argument_list_member_at (arguments,
1141 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001142 /* Before substituting, we expand the argument
1143 * tokens. */
1144 _glcpp_parser_expand_token_list_onto (parser,
1145 argument,
1146 substituted);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001147 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001148 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001149 }
1150 }
1151
Carl Worthad0dee62010-05-26 09:04:50 -07001152 /* After argument substitution, and before further expansion
1153 * below, implement token pasting. */
1154
1155 node = substituted->head;
1156 while (node)
1157 {
1158 token_node_t *next_non_space;
1159
1160 /* Look ahead for a PASTE token, skipping space. */
1161 next_non_space = node->next;
1162 while (next_non_space && next_non_space->token->type == SPACE)
1163 next_non_space = next_non_space->next;
1164
1165 if (next_non_space == NULL)
1166 break;
1167
1168 if (next_non_space->token->type != PASTE) {
1169 node = next_non_space;
1170 continue;
1171 }
1172
1173 /* Now find the next non-space token after the PASTE. */
1174 next_non_space = next_non_space->next;
1175 while (next_non_space && next_non_space->token->type == SPACE)
1176 next_non_space = next_non_space->next;
1177
1178 if (next_non_space == NULL) {
1179 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
1180 exit (1);
1181 }
1182
1183 _token_paste (node->token, next_non_space->token);
1184 node->next = next_non_space->next;
1185
1186 node = node->next;
1187 }
1188
Carl Worthae6517f2010-05-25 15:24:59 -07001189 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -07001190 _glcpp_parser_expand_token_list_onto (parser, substituted, result);
Carl Worthae6517f2010-05-25 15:24:59 -07001191 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001192
1193 talloc_free (arguments);
Carl Worthae6517f2010-05-25 15:24:59 -07001194}
1195
Carl Worth0197e9b2010-05-26 08:05:19 -07001196static void
1197_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
1198 token_list_t *list,
1199 token_list_t *result)
1200{
1201 token_node_t *node;
1202
1203 if (list == NULL)
1204 return;
1205
1206 for (node = list->head; node; node = node->next)
1207 {
1208 if (_glcpp_parser_expand_token_onto (parser, node->token,
1209 result))
1210 {
1211 _glcpp_parser_expand_function_onto (parser, &node,
1212 result);
1213 }
1214 }
1215}
1216
Carl Worthae6517f2010-05-25 15:24:59 -07001217void
1218_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1219 token_list_t *list)
1220{
Carl Worth0197e9b2010-05-26 08:05:19 -07001221 token_list_t *expanded;
Carl Worthae6517f2010-05-25 15:24:59 -07001222 token_node_t *node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001223 function_status_t function_status;
Carl Worthae6517f2010-05-25 15:24:59 -07001224
1225 if (list == NULL)
1226 return;
1227
Carl Worth0197e9b2010-05-26 08:05:19 -07001228 expanded = _token_list_create (parser);
Carl Worth10ae4382010-05-25 20:35:01 -07001229
Carl Worth0197e9b2010-05-26 08:05:19 -07001230 _glcpp_parser_expand_token_list_onto (parser, list, expanded);
1231
1232 _token_list_trim_trailing_space (expanded);
1233
1234 _token_list_print (expanded);
1235
1236 talloc_free (expanded);
Carl Worthae6517f2010-05-25 15:24:59 -07001237}
1238
1239void
Carl Worthfcbbb462010-05-13 09:36:23 -07001240_define_object_macro (glcpp_parser_t *parser,
1241 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001242 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001243{
1244 macro_t *macro;
1245
1246 macro = xtalloc (parser, macro_t);
1247
1248 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001249 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001250 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001251 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001252
1253 hash_table_insert (parser->defines, macro, identifier);
1254}
1255
1256void
1257_define_function_macro (glcpp_parser_t *parser,
1258 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001259 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001260 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001261{
1262 macro_t *macro;
1263
1264 macro = xtalloc (parser, macro_t);
1265
1266 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001267 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001268 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001269 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001270
1271 hash_table_insert (parser->defines, macro, identifier);
1272}
1273
Carl Worth8f38aff2010-05-19 10:01:29 -07001274static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001275glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001276{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001277 token_node_t *node;
1278 int ret;
1279
Carl Worth95951ea2010-05-26 15:57:10 -07001280 if (parser->lex_from_list == NULL) {
1281 ret = glcpp_lex (parser->scanner);
1282
1283 /* XXX: This ugly block of code exists for the sole
1284 * purpose of converting a NEWLINE token into a SPACE
1285 * token, but only in the case where we have seen a
1286 * function-like macro name, but have not yet seen its
1287 * closing parenthesis.
1288 *
1289 * There's perhaps a more compact way to do this with
1290 * mid-rule actions in the grammar.
1291 *
1292 * I'm definitely not pleased with the complexity of
1293 * this code here.
1294 */
1295 if (parser->newline_as_space)
1296 {
1297 if (ret == '(') {
1298 parser->paren_count++;
1299 } else if (ret == ')') {
1300 parser->paren_count--;
1301 if (parser->paren_count == 0)
1302 parser->newline_as_space = 0;
1303 } else if (ret == NEWLINE) {
1304 ret = SPACE;
1305 } else if (ret != SPACE) {
1306 if (parser->paren_count == 0)
1307 parser->newline_as_space = 0;
1308 }
1309 }
1310 else if (parser->in_control_line)
1311 {
1312 if (ret == NEWLINE)
1313 parser->in_control_line = 0;
1314 }
1315 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1316 ret == HASH_UNDEF || ret == HASH_IF ||
1317 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1318 ret == HASH_ELIF || ret == HASH_ELSE ||
1319 ret == HASH_ENDIF || ret == HASH)
1320 {
1321 parser->in_control_line = 1;
1322 }
1323 else if (ret == IDENTIFIER)
1324 {
1325 macro_t *macro;
1326 macro = hash_table_find (parser->defines,
1327 yylval.str);
1328 if (macro && macro->is_function) {
1329 parser->newline_as_space = 1;
1330 parser->paren_count = 0;
1331 }
1332 }
1333
1334 return ret;
1335 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001336
1337 node = parser->lex_from_node;
1338
1339 if (node == NULL) {
1340 talloc_free (parser->lex_from_list);
1341 parser->lex_from_list = NULL;
1342 return NEWLINE;
1343 }
1344
1345 yylval = node->token->value;
1346 ret = node->token->type;
1347
1348 parser->lex_from_node = node->next;
1349
1350 return ret;
1351}
1352
1353static void
1354glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1355{
1356 token_node_t *node;
1357
1358 assert (parser->lex_from_list == NULL);
1359
1360 /* Copy list, eliminating any space tokens. */
1361 parser->lex_from_list = _token_list_create (parser);
1362
1363 for (node = list->head; node; node = node->next) {
1364 if (node->token->type == SPACE)
1365 continue;
1366 _token_list_append (parser->lex_from_list, node->token);
1367 }
1368
1369 talloc_free (list);
1370
1371 parser->lex_from_node = parser->lex_from_list->head;
1372
1373 /* It's possible the list consisted of nothing but whitespace. */
1374 if (parser->lex_from_node == NULL) {
1375 talloc_free (parser->lex_from_list);
1376 parser->lex_from_list = NULL;
1377 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001378}
Carl Worthb20d33c2010-05-20 22:27:07 -07001379
1380static void
1381_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1382{
1383 skip_type_t current = SKIP_NO_SKIP;
1384 skip_node_t *node;
1385
1386 if (parser->skip_stack)
1387 current = parser->skip_stack->type;
1388
1389 node = xtalloc (parser, skip_node_t);
1390
1391 if (current == SKIP_NO_SKIP) {
1392 if (condition)
1393 node->type = SKIP_NO_SKIP;
1394 else
1395 node->type = SKIP_TO_ELSE;
1396 } else {
1397 node->type = SKIP_TO_ENDIF;
1398 }
1399
1400 node->next = parser->skip_stack;
1401 parser->skip_stack = node;
1402}
1403
1404static void
1405_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1406 int condition)
1407{
1408 if (parser->skip_stack == NULL) {
1409 fprintf (stderr, "Error: %s without #if\n", type);
1410 exit (1);
1411 }
1412
1413 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1414 if (condition)
1415 parser->skip_stack->type = SKIP_NO_SKIP;
1416 } else {
1417 parser->skip_stack->type = SKIP_TO_ENDIF;
1418 }
1419}
Carl Worth80dc60b2010-05-25 14:42:00 -07001420
Carl Worthb20d33c2010-05-20 22:27:07 -07001421static void
1422_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1423{
1424 skip_node_t *node;
1425
1426 if (parser->skip_stack == NULL) {
1427 fprintf (stderr, "Error: #endif without #if\n");
1428 exit (1);
1429 }
1430
1431 node = parser->skip_stack;
1432 parser->skip_stack = node->next;
1433 talloc_free (node);
1434}