blob: 3e0a96528b4a6a92371839cf122ef7a8feb2cd0e [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{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700669 if (tail == NULL || tail->head == NULL)
670 return;
671
Carl Worth47252442010-05-19 13:54:37 -0700672 if (list->head == NULL) {
673 list->head = tail->head;
674 } else {
675 list->tail->next = tail->head;
676 }
677
678 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700679 list->non_space_tail = tail->non_space_tail;
680}
681
682void
683_token_list_trim_trailing_space (token_list_t *list)
684{
685 token_node_t *tail, *next;
686
687 if (list->non_space_tail) {
688 tail = list->non_space_tail->next;
689 list->non_space_tail->next = NULL;
690 list->tail = list->non_space_tail;
691
692 while (tail) {
693 next = tail->next;
694 talloc_free (tail);
695 tail = next;
696 }
697 }
Carl Worth47252442010-05-19 13:54:37 -0700698}
Carl Worth80dc60b2010-05-25 14:42:00 -0700699
Carl Worth0197e9b2010-05-26 08:05:19 -0700700static void
701_token_print (token_t *token)
702{
703 if (token->type < 256) {
704 printf ("%c", token->type);
705 return;
706 }
707
708 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700709 case INTEGER:
710 printf ("%" PRIxMAX, token->value.ival);
711 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700712 case IDENTIFIER:
713 case OTHER:
714 printf ("%s", token->value.str);
715 break;
716 case SPACE:
717 printf (" ");
718 break;
719 case LEFT_SHIFT:
720 printf ("<<");
721 break;
722 case RIGHT_SHIFT:
723 printf (">>");
724 break;
725 case LESS_OR_EQUAL:
726 printf ("<=");
727 break;
728 case GREATER_OR_EQUAL:
729 printf (">=");
730 break;
731 case EQUAL:
732 printf ("==");
733 break;
734 case NOT_EQUAL:
735 printf ("!=");
736 break;
737 case AND:
738 printf ("&&");
739 break;
740 case OR:
741 printf ("||");
742 break;
743 case PASTE:
744 printf ("##");
745 break;
Carl Worthdd749002010-05-27 10:12:33 -0700746 case COMMA_FINAL:
747 printf (",");
748 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700749 default:
750 fprintf (stderr, "Error: Don't know how to print token type %d\n", token->type);
751 break;
752 }
753}
754
Carl Worthad0dee62010-05-26 09:04:50 -0700755/* Change 'token' into a new token formed by pasting 'other'. */
756static void
757_token_paste (token_t *token, token_t *other)
758{
759 /* A very few single-character punctuators can be combined
760 * with another to form a multi-character punctuator. */
761 switch (token->type) {
762 case '<':
763 if (other->type == '<') {
764 token->type = LEFT_SHIFT;
765 token->value.ival = LEFT_SHIFT;
766 return;
767 } else if (other->type == '=') {
768 token->type = LESS_OR_EQUAL;
769 token->value.ival = LESS_OR_EQUAL;
770 return;
771 }
772 break;
773 case '>':
774 if (other->type == '>') {
775 token->type = RIGHT_SHIFT;
776 token->value.ival = RIGHT_SHIFT;
777 return;
778 } else if (other->type == '=') {
779 token->type = GREATER_OR_EQUAL;
780 token->value.ival = GREATER_OR_EQUAL;
781 return;
782 }
783 break;
784 case '=':
785 if (other->type == '=') {
786 token->type = EQUAL;
787 token->value.ival = EQUAL;
788 return;
789 }
790 break;
791 case '!':
792 if (other->type == '=') {
793 token->type = NOT_EQUAL;
794 token->value.ival = NOT_EQUAL;
795 return;
796 }
797 break;
798 case '&':
799 if (other->type == '&') {
800 token->type = AND;
801 token->value.ival = AND;
802 return;
803 }
804 break;
805 case '|':
806 if (other->type == '|') {
807 token->type = OR;
808 token->value.ival = OR;
809 return;
810 }
811 break;
812 }
813
814 /* Two string-valued tokens can usually just be mashed
815 * together.
816 *
817 * XXX: Since our 'OTHER' case is currently so loose, this may
818 * allow some things thruogh that should be treated as
819 * errors. */
820 if ((token->type == IDENTIFIER || token->type == OTHER) &&
821 (other->type == IDENTIFIER || other->type == OTHER))
822 {
823 token->value.str = talloc_strdup_append (token->value.str,
824 other->value.str);
825 return;
826 }
827
828 printf ("Error: Pasting \"");
829 _token_print (token);
830 printf ("\" and \"");
831 _token_print (other);
832 printf ("\" does not give a valid preprocessing token.\n");
833}
834
Carl Worth0197e9b2010-05-26 08:05:19 -0700835static void
836_token_list_print (token_list_t *list)
837{
838 token_node_t *node;
839
840 if (list == NULL)
841 return;
842
843 for (node = list->head; node; node = node->next)
844 _token_print (node->token);
845}
846
Carl Worth3a37b872010-05-10 11:44:09 -0700847void
Carl Wortha1e32bc2010-05-10 13:17:25 -0700848yyerror (void *scanner, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700849{
850 fprintf (stderr, "Parse error: %s\n", error);
851}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700852
Carl Worth33cc4002010-05-12 12:17:10 -0700853glcpp_parser_t *
854glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700855{
Carl Worth33cc4002010-05-12 12:17:10 -0700856 glcpp_parser_t *parser;
857
Carl Worth5070a202010-05-12 12:45:33 -0700858 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700859
Carl Worth8f38aff2010-05-19 10:01:29 -0700860 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700861 parser->defines = hash_table_ctor (32, hash_table_string_hash,
862 hash_table_string_compare);
Carl Worthae6517f2010-05-25 15:24:59 -0700863 parser->active = _string_list_create (parser);
Carl Worthf34a0002010-05-25 16:59:02 -0700864 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700865 parser->newline_as_space = 0;
866 parser->in_control_line = 0;
867 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700868
Carl Worthb20d33c2010-05-20 22:27:07 -0700869 parser->skip_stack = NULL;
870
Carl Worth8e82fcb2010-05-26 11:15:21 -0700871 parser->lex_from_list = NULL;
872 parser->lex_from_node = NULL;
873
Carl Worth33cc4002010-05-12 12:17:10 -0700874 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700875}
876
877int
878glcpp_parser_parse (glcpp_parser_t *parser)
879{
880 return yyparse (parser);
881}
882
883void
Carl Worth33cc4002010-05-12 12:17:10 -0700884glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700885{
Carl Worthb20d33c2010-05-20 22:27:07 -0700886 if (parser->skip_stack)
887 fprintf (stderr, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700888 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700889 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700890 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700891}
Carl Worthc6d5af32010-05-11 12:30:09 -0700892
Carl Worth8e82fcb2010-05-26 11:15:21 -0700893/* Replace any occurences of DEFINED tokens in 'list' with either a
894 * '0' or '1' INTEGER token depending on whether the next token in the
895 * list is defined or not. */
896static void
897_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
898 token_list_t *list)
899{
900 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700901 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700902
903 if (list == NULL)
904 return;
905
906 for (node = list->head; node; node = node->next) {
907 if (node->token->type != DEFINED)
908 continue;
909 next = node->next;
910 while (next && next->token->type == SPACE)
911 next = next->next;
912 if (next == NULL || next->token->type != IDENTIFIER) {
913 fprintf (stderr, "Error: operator \"defined\" requires an identifier\n");
914 exit (1);
915 }
916 macro = hash_table_find (parser->defines,
917 next->token->value.str);
918
919 node->token->type = INTEGER;
920 node->token->value.ival = (macro != NULL);
921 node->next = next->next;
922 }
923}
924
925
Carl Worth0197e9b2010-05-26 08:05:19 -0700926/* Appends onto 'expansion' a non-macro token or the expansion of an
927 * object-like macro.
Carl Worthb1854fd2010-05-25 16:28:26 -0700928 *
Carl Worth0197e9b2010-05-26 08:05:19 -0700929 * Returns 0 if this token is completely processed.
Carl Worthb1854fd2010-05-25 16:28:26 -0700930 *
931 * Returns 1 in the case that 'token' is a function-like macro that
932 * needs further expansion.
933 */
934static int
Carl Worth7db24022010-05-26 17:01:57 -0700935_expand_token_onto (glcpp_parser_t *parser,
936 token_t *token,
937 token_list_t *result)
Carl Worthae6517f2010-05-25 15:24:59 -0700938{
939 const char *identifier;
940 macro_t *macro;
Carl Worth0197e9b2010-05-26 08:05:19 -0700941 token_list_t *expansion;
Carl Worthae6517f2010-05-25 15:24:59 -0700942
943 /* We only expand identifiers */
944 if (token->type != IDENTIFIER) {
Carl Worthdd749002010-05-27 10:12:33 -0700945 /* We change any COMMA into a COMMA_FINAL to prevent
946 * it being mistaken for an argument separator
947 * later. */
948 if (token->type == ',') {
949 token_t *new_token;
950
951 new_token = _token_create_ival (result, COMMA_FINAL,
952 COMMA_FINAL);
953 _token_list_append (result, new_token);
954 } else {
955 _token_list_append (result, token);
956 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700957 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700958 }
959
960 /* Look up this identifier in the hash table. */
961 identifier = token->value.str;
962 macro = hash_table_find (parser->defines, identifier);
963
Carl Worth0197e9b2010-05-26 08:05:19 -0700964 /* Not a macro, so just append. */
Carl Worthae6517f2010-05-25 15:24:59 -0700965 if (macro == NULL) {
Carl Worth0197e9b2010-05-26 08:05:19 -0700966 _token_list_append (result, token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700967 return 0;
Carl Worthae6517f2010-05-25 15:24:59 -0700968 }
969
Carl Worthae6517f2010-05-25 15:24:59 -0700970 /* Finally, don't expand this macro if we're already actively
971 * expanding it, (to avoid infinite recursion). */
Carl Worthec4ada02010-05-26 08:15:49 -0700972 if (_string_list_contains (parser->active, identifier, NULL))
973 {
974 /* We change the token type here from IDENTIFIER to
975 * OTHER to prevent any future expansion of this
976 * unexpanded token. */
977 char *str;
978 token_t *new_token;
979
980 str = xtalloc_strdup (result, token->value.str);
981 new_token = _token_create_str (result, OTHER, str);
982 _token_list_append (result, new_token);
Carl Worthb1854fd2010-05-25 16:28:26 -0700983 return 0;
984 }
985
Carl Worthc0607d52010-05-26 08:01:42 -0700986 /* For function-like macros return 1 for further processing. */
987 if (macro->is_function) {
988 return 1;
989 }
990
Carl Worthb1854fd2010-05-25 16:28:26 -0700991 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -0700992 _glcpp_parser_expand_token_list_onto (parser,
993 macro->replacements,
994 result);
Carl Worthb1854fd2010-05-25 16:28:26 -0700995 _string_list_pop (parser->active);
996
997 return 0;
998}
999
1000typedef enum function_status
1001{
1002 FUNCTION_STATUS_SUCCESS,
1003 FUNCTION_NOT_A_FUNCTION,
1004 FUNCTION_UNBALANCED_PARENTHESES
1005} function_status_t;
1006
1007/* Find a set of function-like macro arguments by looking for a
1008 * balanced set of parentheses. Upon return *node will be the last
1009 * consumed node, such that further processing can continue with
1010 * node->next.
1011 *
1012 * Return values:
1013 *
1014 * FUNCTION_STATUS_SUCCESS:
1015 *
1016 * Successfully parsed a set of function arguments.
1017 *
1018 * FUNCTION_NOT_A_FUNCTION:
1019 *
1020 * Macro name not followed by a '('. This is not an error, but
1021 * simply that the macro name should be treated as a non-macro.
1022 *
1023 * FUNCTION_UNBLANCED_PARENTHESES
1024 *
1025 * Macro name is not followed by a balanced set of parentheses.
1026 */
1027static function_status_t
Carl Worth9ce18cf2010-05-25 17:32:21 -07001028_arguments_parse (argument_list_t *arguments, token_node_t **node_ret)
Carl Worthb1854fd2010-05-25 16:28:26 -07001029{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001030 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001031 token_node_t *node = *node_ret, *last;
1032 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001033
1034 last = node;
1035 node = node->next;
1036
1037 /* Ignore whitespace before first parenthesis. */
1038 while (node && node->token->type == SPACE)
1039 node = node->next;
1040
1041 if (node == NULL || node->token->type != '(')
1042 return FUNCTION_NOT_A_FUNCTION;
1043
Carl Worth652fa272010-05-25 17:45:22 -07001044 last = node;
1045 node = node->next;
1046
Carl Wortha19297b2010-05-27 13:29:19 -07001047 argument = _token_list_create (arguments);
1048 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001049
Carl Worth652fa272010-05-25 17:45:22 -07001050 for (paren_count = 1; node; last = node, node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001051 if (node->token->type == '(')
1052 {
1053 paren_count++;
1054 }
1055 else if (node->token->type == ')')
1056 {
1057 paren_count--;
Carl Worthc7581c22010-05-25 17:41:07 -07001058 if (paren_count == 0) {
1059 last = node;
1060 node = node->next;
1061 break;
1062 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001063 }
Carl Worth652fa272010-05-25 17:45:22 -07001064
1065 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001066 paren_count == 1)
1067 {
Carl Wortha19297b2010-05-27 13:29:19 -07001068 _token_list_trim_trailing_space (argument);
1069 argument = _token_list_create (arguments);
1070 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001071 }
1072 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001073 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001074 /* Don't treat initial whitespace as
1075 * part of the arguement. */
1076 if (node->token->type == SPACE)
1077 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001078 }
1079 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001080 }
Carl Worthc7581c22010-05-25 17:41:07 -07001081 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001082
1083 if (node && paren_count)
1084 return FUNCTION_UNBALANCED_PARENTHESES;
1085
1086 *node_ret = last;
1087
1088 return FUNCTION_STATUS_SUCCESS;
1089}
1090
1091/* Prints the expansion of *node (consuming further tokens from the
1092 * list as necessary). Upon return *node will be the last consumed
1093 * node, such that further processing can continue with node->next. */
Carl Worth7db24022010-05-26 17:01:57 -07001094static function_status_t
1095_expand_function_onto (glcpp_parser_t *parser,
1096 token_node_t **node_ret,
1097 token_list_t *result)
Carl Worthb1854fd2010-05-25 16:28:26 -07001098{
1099 macro_t *macro;
1100 token_node_t *node;
1101 const char *identifier;
1102 argument_list_t *arguments;
1103 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001104 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001105 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001106
1107 node = *node_ret;
1108 identifier = node->token->value.str;
1109
1110 macro = hash_table_find (parser->defines, identifier);
1111
1112 assert (macro->is_function);
1113
Carl Worth9ce18cf2010-05-25 17:32:21 -07001114 arguments = _argument_list_create (parser);
1115 status = _arguments_parse (arguments, node_ret);
Carl Worthb1854fd2010-05-25 16:28:26 -07001116
1117 switch (status) {
1118 case FUNCTION_STATUS_SUCCESS:
1119 break;
1120 case FUNCTION_NOT_A_FUNCTION:
Carl Worth0197e9b2010-05-26 08:05:19 -07001121 _token_list_append (result, node->token);
Carl Worth7db24022010-05-26 17:01:57 -07001122 return FUNCTION_NOT_A_FUNCTION;
Carl Worthb1854fd2010-05-25 16:28:26 -07001123 case FUNCTION_UNBALANCED_PARENTHESES:
1124 fprintf (stderr, "Error: Macro %s call has unbalanced parentheses\n",
1125 identifier);
1126 exit (1);
Carl Worthae6517f2010-05-25 15:24:59 -07001127 }
1128
Carl Worth9ce18cf2010-05-25 17:32:21 -07001129 if (macro->replacements == NULL) {
1130 talloc_free (arguments);
Carl Worth7db24022010-05-26 17:01:57 -07001131 return FUNCTION_STATUS_SUCCESS;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001132 }
1133
Carl Wortha19297b2010-05-27 13:29:19 -07001134 if (! ((_argument_list_length (arguments) ==
1135 _string_list_length (macro->parameters)) ||
1136 (_string_list_length (macro->parameters) == 0 &&
1137 _argument_list_length (arguments) == 1 &&
1138 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001139 {
1140 fprintf (stderr,
1141 "Error: macro %s invoked with %d arguments (expected %d)\n",
1142 identifier,
1143 _argument_list_length (arguments),
1144 _string_list_length (macro->parameters));
Carl Worth7db24022010-05-26 17:01:57 -07001145 exit (1);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001146 }
1147
Carl Worth0197e9b2010-05-26 08:05:19 -07001148 /* Perform argument substitution on the replacement list. */
1149 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001150
Carl Worthce540f22010-05-26 08:25:44 -07001151 for (node = macro->replacements->head; node; node = node->next)
1152 {
1153 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001154 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001155 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001156 &parameter_index))
1157 {
1158 token_list_t *argument;
1159 argument = _argument_list_member_at (arguments,
1160 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001161 /* Before substituting, we expand the argument
1162 * tokens. */
1163 _glcpp_parser_expand_token_list_onto (parser,
1164 argument,
1165 substituted);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001166 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001167 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001168 }
1169 }
1170
Carl Worthad0dee62010-05-26 09:04:50 -07001171 /* After argument substitution, and before further expansion
1172 * below, implement token pasting. */
1173
1174 node = substituted->head;
1175 while (node)
1176 {
1177 token_node_t *next_non_space;
1178
1179 /* Look ahead for a PASTE token, skipping space. */
1180 next_non_space = node->next;
1181 while (next_non_space && next_non_space->token->type == SPACE)
1182 next_non_space = next_non_space->next;
1183
1184 if (next_non_space == NULL)
1185 break;
1186
1187 if (next_non_space->token->type != PASTE) {
1188 node = next_non_space;
1189 continue;
1190 }
1191
1192 /* Now find the next non-space token after the PASTE. */
1193 next_non_space = next_non_space->next;
1194 while (next_non_space && next_non_space->token->type == SPACE)
1195 next_non_space = next_non_space->next;
1196
1197 if (next_non_space == NULL) {
1198 fprintf (stderr, "Error: '##' cannot appear at either end of a macro expansion\n");
1199 exit (1);
1200 }
1201
1202 _token_paste (node->token, next_non_space->token);
1203 node->next = next_non_space->next;
1204
1205 node = node->next;
1206 }
1207
Carl Worthae6517f2010-05-25 15:24:59 -07001208 _string_list_push (parser->active, identifier);
Carl Worth0197e9b2010-05-26 08:05:19 -07001209 _glcpp_parser_expand_token_list_onto (parser, substituted, result);
Carl Worthae6517f2010-05-25 15:24:59 -07001210 _string_list_pop (parser->active);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001211
1212 talloc_free (arguments);
Carl Worth7db24022010-05-26 17:01:57 -07001213
1214 return FUNCTION_STATUS_SUCCESS;
Carl Worthae6517f2010-05-25 15:24:59 -07001215}
1216
Carl Worth0197e9b2010-05-26 08:05:19 -07001217static void
1218_glcpp_parser_expand_token_list_onto (glcpp_parser_t *parser,
1219 token_list_t *list,
1220 token_list_t *result)
1221{
1222 token_node_t *node;
Carl Worth7db24022010-05-26 17:01:57 -07001223 token_list_t *intermediate, *list_orig = list;
1224 int i, need_rescan = 0;
Carl Worth0197e9b2010-05-26 08:05:19 -07001225
Carl Wortha65cf7b2010-05-27 11:55:36 -07001226 if (list == NULL || list->head == NULL)
Carl Worth0197e9b2010-05-26 08:05:19 -07001227 return;
1228
Carl Worth7db24022010-05-26 17:01:57 -07001229 intermediate = _token_list_create (parser);
1230
1231 /* XXX: The two-pass expansion here is really ugly. The
1232 * problem this is solving is that we can expand a macro into
1233 * a function-like macro name, and then we need to recognize
1234 * that as a function-like macro, but perhaps the parentheses
1235 * and arguments aren't on the token list yet, (since they are
1236 * in the actual content so they are part of what we are
1237 * expanding.
1238 *
1239 * This ugly hack works, but is messy, fragile, and hard to
1240 * maintain. I think a cleaner solution would separate the
1241 * notions of expanding and appending and avoid this problem
1242 * altogether.
1243 */
1244
1245 for (i = 0; i < 2; i++) {
1246 if (i == 1) {
1247 list = intermediate;
1248 intermediate = _token_list_create (parser);
Carl Worth0197e9b2010-05-26 08:05:19 -07001249 }
Carl Worth7db24022010-05-26 17:01:57 -07001250 for (node = list->head; node; node = node->next)
1251 {
1252 if (_expand_token_onto (parser, node->token,
1253 intermediate))
1254 {
1255 if (_expand_function_onto (parser, &node,
1256 intermediate))
1257 {
1258 need_rescan = 1;
1259 }
1260 }
1261 }
1262 if (list != list_orig)
1263 talloc_free (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001264 }
Carl Worth7db24022010-05-26 17:01:57 -07001265
1266 _token_list_append_list (result, intermediate);
Carl Worth0197e9b2010-05-26 08:05:19 -07001267}
1268
Carl Worthae6517f2010-05-25 15:24:59 -07001269void
1270_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1271 token_list_t *list)
1272{
Carl Worth0197e9b2010-05-26 08:05:19 -07001273 token_list_t *expanded;
Carl Worthae6517f2010-05-25 15:24:59 -07001274 token_node_t *node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001275 function_status_t function_status;
Carl Worthae6517f2010-05-25 15:24:59 -07001276
1277 if (list == NULL)
1278 return;
1279
Carl Worth0197e9b2010-05-26 08:05:19 -07001280 expanded = _token_list_create (parser);
Carl Worth10ae4382010-05-25 20:35:01 -07001281
Carl Worth0197e9b2010-05-26 08:05:19 -07001282 _glcpp_parser_expand_token_list_onto (parser, list, expanded);
1283
1284 _token_list_trim_trailing_space (expanded);
1285
1286 _token_list_print (expanded);
1287
1288 talloc_free (expanded);
Carl Worthae6517f2010-05-25 15:24:59 -07001289}
1290
1291void
Carl Worthfcbbb462010-05-13 09:36:23 -07001292_define_object_macro (glcpp_parser_t *parser,
1293 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001294 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001295{
1296 macro_t *macro;
1297
1298 macro = xtalloc (parser, macro_t);
1299
1300 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001301 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001302 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001303 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001304
1305 hash_table_insert (parser->defines, macro, identifier);
1306}
1307
1308void
1309_define_function_macro (glcpp_parser_t *parser,
1310 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001311 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001312 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001313{
1314 macro_t *macro;
1315
1316 macro = xtalloc (parser, macro_t);
1317
1318 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001319 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001320 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001321 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001322
1323 hash_table_insert (parser->defines, macro, identifier);
1324}
1325
Carl Worth8f38aff2010-05-19 10:01:29 -07001326static int
Carl Worth0293b2e2010-05-19 10:05:40 -07001327glcpp_parser_lex (glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001328{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001329 token_node_t *node;
1330 int ret;
1331
Carl Worth95951ea2010-05-26 15:57:10 -07001332 if (parser->lex_from_list == NULL) {
1333 ret = glcpp_lex (parser->scanner);
1334
1335 /* XXX: This ugly block of code exists for the sole
1336 * purpose of converting a NEWLINE token into a SPACE
1337 * token, but only in the case where we have seen a
1338 * function-like macro name, but have not yet seen its
1339 * closing parenthesis.
1340 *
1341 * There's perhaps a more compact way to do this with
1342 * mid-rule actions in the grammar.
1343 *
1344 * I'm definitely not pleased with the complexity of
1345 * this code here.
1346 */
1347 if (parser->newline_as_space)
1348 {
1349 if (ret == '(') {
1350 parser->paren_count++;
1351 } else if (ret == ')') {
1352 parser->paren_count--;
1353 if (parser->paren_count == 0)
1354 parser->newline_as_space = 0;
1355 } else if (ret == NEWLINE) {
1356 ret = SPACE;
1357 } else if (ret != SPACE) {
1358 if (parser->paren_count == 0)
1359 parser->newline_as_space = 0;
1360 }
1361 }
1362 else if (parser->in_control_line)
1363 {
1364 if (ret == NEWLINE)
1365 parser->in_control_line = 0;
1366 }
1367 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1368 ret == HASH_UNDEF || ret == HASH_IF ||
1369 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1370 ret == HASH_ELIF || ret == HASH_ELSE ||
1371 ret == HASH_ENDIF || ret == HASH)
1372 {
1373 parser->in_control_line = 1;
1374 }
1375 else if (ret == IDENTIFIER)
1376 {
1377 macro_t *macro;
1378 macro = hash_table_find (parser->defines,
1379 yylval.str);
1380 if (macro && macro->is_function) {
1381 parser->newline_as_space = 1;
1382 parser->paren_count = 0;
1383 }
1384 }
1385
1386 return ret;
1387 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001388
1389 node = parser->lex_from_node;
1390
1391 if (node == NULL) {
1392 talloc_free (parser->lex_from_list);
1393 parser->lex_from_list = NULL;
1394 return NEWLINE;
1395 }
1396
1397 yylval = node->token->value;
1398 ret = node->token->type;
1399
1400 parser->lex_from_node = node->next;
1401
1402 return ret;
1403}
1404
1405static void
1406glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1407{
1408 token_node_t *node;
1409
1410 assert (parser->lex_from_list == NULL);
1411
1412 /* Copy list, eliminating any space tokens. */
1413 parser->lex_from_list = _token_list_create (parser);
1414
1415 for (node = list->head; node; node = node->next) {
1416 if (node->token->type == SPACE)
1417 continue;
1418 _token_list_append (parser->lex_from_list, node->token);
1419 }
1420
1421 talloc_free (list);
1422
1423 parser->lex_from_node = parser->lex_from_list->head;
1424
1425 /* It's possible the list consisted of nothing but whitespace. */
1426 if (parser->lex_from_node == NULL) {
1427 talloc_free (parser->lex_from_list);
1428 parser->lex_from_list = NULL;
1429 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001430}
Carl Worthb20d33c2010-05-20 22:27:07 -07001431
1432static void
1433_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1434{
1435 skip_type_t current = SKIP_NO_SKIP;
1436 skip_node_t *node;
1437
1438 if (parser->skip_stack)
1439 current = parser->skip_stack->type;
1440
1441 node = xtalloc (parser, skip_node_t);
1442
1443 if (current == SKIP_NO_SKIP) {
1444 if (condition)
1445 node->type = SKIP_NO_SKIP;
1446 else
1447 node->type = SKIP_TO_ELSE;
1448 } else {
1449 node->type = SKIP_TO_ENDIF;
1450 }
1451
1452 node->next = parser->skip_stack;
1453 parser->skip_stack = node;
1454}
1455
1456static void
1457_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1458 int condition)
1459{
1460 if (parser->skip_stack == NULL) {
1461 fprintf (stderr, "Error: %s without #if\n", type);
1462 exit (1);
1463 }
1464
1465 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1466 if (condition)
1467 parser->skip_stack->type = SKIP_NO_SKIP;
1468 } else {
1469 parser->skip_stack->type = SKIP_TO_ENDIF;
1470 }
1471}
Carl Worth80dc60b2010-05-25 14:42:00 -07001472
Carl Worthb20d33c2010-05-20 22:27:07 -07001473static void
1474_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1475{
1476 skip_node_t *node;
1477
1478 if (parser->skip_stack == NULL) {
1479 fprintf (stderr, "Error: #endif without #if\n");
1480 exit (1);
1481 }
1482
1483 node = parser->skip_stack;
1484 parser->skip_stack = node->next;
1485 talloc_free (node);
1486}