blob: a4383574506a8bf88ce294854b1918765265292e [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"
Ian Romanick06143ea2010-06-30 16:27:22 -070031#include "main/mtypes.h"
Carl Wortha1e32bc2010-05-10 13:17:25 -070032
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -070033#define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
34#define glcpp_printf(stream, fmt, args...) \
35 stream = talloc_asprintf_append(stream, fmt, args)
36
Carl Worth5aa7ea02010-05-25 18:39:43 -070037static void
Kenneth Graunke465e03e2010-06-16 16:35:57 -070038yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070039
Carl Worth5aa7ea02010-05-25 18:39:43 -070040static void
Carl Worthfcbbb462010-05-13 09:36:23 -070041_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070042 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070043 const char *macro,
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 void
Carl Worthfcbbb462010-05-13 09:36:23 -070047_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070048 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070049 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070050 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070051 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070052
Carl Worth5aa7ea02010-05-25 18:39:43 -070053static string_list_t *
Carl Worth610053b2010-05-14 10:05:11 -070054_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070055
Carl Worth5aa7ea02010-05-25 18:39:43 -070056static void
Carl Worth610053b2010-05-14 10:05:11 -070057_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070058
Carl Worth5aa7ea02010-05-25 18:39:43 -070059static int
Carl Worth610053b2010-05-14 10:05:11 -070060_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070061
Carl Worth5aa7ea02010-05-25 18:39:43 -070062static int
Carl Worth610053b2010-05-14 10:05:11 -070063_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070064
Carl Worth5aa7ea02010-05-25 18:39:43 -070065static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070066_argument_list_create (void *ctx);
67
Carl Worth5aa7ea02010-05-25 18:39:43 -070068static void
Carl Worth47252442010-05-19 13:54:37 -070069_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070070
Carl Worth5aa7ea02010-05-25 18:39:43 -070071static int
Carl Worth8f6a8282010-05-14 10:44:19 -070072_argument_list_length (argument_list_t *list);
73
Carl Worth5aa7ea02010-05-25 18:39:43 -070074static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070075_argument_list_member_at (argument_list_t *list, int index);
76
Carl Worth808401f2010-05-25 14:52:43 -070077/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070078static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070079_token_create_str (void *ctx, int type, char *str);
80
Carl Worth5aa7ea02010-05-25 18:39:43 -070081static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070082_token_create_ival (void *ctx, int type, int ival);
83
Carl Worth5aa7ea02010-05-25 18:39:43 -070084static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070085_token_list_create (void *ctx);
86
Carl Worthb1ae61a2010-05-26 08:10:38 -070087/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070088 *
89 * You may want to talloc_unlink any current reference if you no
90 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070091static void
Carl Worth808401f2010-05-25 14:52:43 -070092_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070093
Carl Worth5aa7ea02010-05-25 18:39:43 -070094static void
Carl Worth47252442010-05-19 13:54:37 -070095_token_list_append_list (token_list_t *list, token_list_t *tail);
96
Carl Worth22b3ace2010-06-02 15:32:03 -070097static active_list_t *
98_active_list_push (active_list_t *list,
99 const char *identifier,
100 token_node_t *marker);
101
102static active_list_t *
103_active_list_pop (active_list_t *list);
104
105int
106_active_list_contains (active_list_t *list, const char *identifier);
107
Carl Worth5aa7ea02010-05-25 18:39:43 -0700108static void
Kenneth Graunke16b4eed2010-08-04 16:10:03 -0700109_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list);
110
111static void
Carl Worth681afbc2010-05-28 15:06:02 -0700112_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
113 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700114
Carl Worthaaa9acb2010-05-19 13:28:24 -0700115static void
Carl Worth681afbc2010-05-28 15:06:02 -0700116_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
117 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700118
119static void
Kenneth Graunke07745232010-06-17 12:41:46 -0700120_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
121 int condition);
Carl Worthb20d33c2010-05-20 22:27:07 -0700122
123static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700124_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
125 const char *type, int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700126
Carl Worthb20d33c2010-05-20 22:27:07 -0700127static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700128_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
Carl Worthb20d33c2010-05-20 22:27:07 -0700129
Carl Worth0293b2e2010-05-19 10:05:40 -0700130#define yylex glcpp_parser_lex
131
Carl Worth8f38aff2010-05-19 10:01:29 -0700132static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700133glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700134
Carl Worth8e82fcb2010-05-26 11:15:21 -0700135static void
136glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
137
Eric Anholtd4a04f32010-07-28 16:58:39 -0700138static void
139add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
140
Carl Worth3a37b872010-05-10 11:44:09 -0700141%}
142
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700143%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700144%error-verbose
Carl Worth485f84d2010-08-10 16:58:28 -0700145
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700146%locations
Carl Worth485f84d2010-08-10 16:58:28 -0700147%initial-action {
148 @$.first_line = 1;
149 @$.first_column = 1;
150 @$.last_line = 1;
151 @$.last_column = 1;
152 @$.source = 0;
153}
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700154
Carl Worth0b27b5f2010-05-10 16:16:06 -0700155%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700156%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700157
Carl Worthefef9502010-07-28 11:10:52 -0700158%expect 0
Eric Anholtd4a04f32010-07-28 16:58:39 -0700159%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 HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE
Carl Worth8fed1cd2010-05-26 09:32:12 -0700160%token PASTE
Eric Anholtd4a04f32010-07-28 16:58:39 -0700161%type <ival> expression INTEGER operator SPACE integer_constant
Carl Worth050e3de2010-05-27 14:36:29 -0700162%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700163%type <string_list> identifier_list
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700164%type <token> preprocessing_token conditional_token
165%type <token_list> pp_tokens replacement_list text_line conditional_tokens
Carl Worth8fed1cd2010-05-26 09:32:12 -0700166%left OR
167%left AND
168%left '|'
169%left '^'
170%left '&'
171%left EQUAL NOT_EQUAL
172%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
173%left LEFT_SHIFT RIGHT_SHIFT
174%left '+' '-'
175%left '*' '/' '%'
176%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700177
178%%
179
Carl Worth33cc4002010-05-12 12:17:10 -0700180input:
Carl Worth3ff81672010-05-25 13:09:03 -0700181 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700182| input line
183;
184
185line:
186 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700187 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700188 }
Carl Worth808401f2010-05-25 14:52:43 -0700189| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700190 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700191 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700192 talloc_free ($1);
193 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700194| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700195| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700196;
197
Carl Worth8e82fcb2010-05-26 11:15:21 -0700198expanded_line:
199 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700200 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700201 }
202| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700203 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700204 }
205;
206
Carl Worth3ff81672010-05-25 13:09:03 -0700207control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700208 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700209 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700210 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700211| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700212 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700213 }
214| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700215 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700216 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700217| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700218 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700219 if (macro) {
Carl Worth61ebc012010-07-19 18:02:12 -0700220 hash_table_remove (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700221 talloc_free (macro);
222 }
223 talloc_free ($2);
224 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700225| HASH_IF conditional_tokens NEWLINE {
Kenneth Graunkef4239872010-08-04 16:24:39 -0700226 /* If we're skipping to the next #elif/#else case or to #endif,
227 * don't bother expanding or parsing the expression.
228 */
229 if (parser->skip_stack != NULL && parser->skip_stack->type != SKIP_NO_SKIP) {
230 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
231 parser->skip_stack->type = SKIP_TO_ENDIF;
232 } else {
233 _glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
234 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700235 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700236| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700237 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700238 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700239 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700240 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700241| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700242 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700243 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700244 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700245 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700246| HASH_ELIF conditional_tokens NEWLINE {
Kenneth Graunkef4239872010-08-04 16:24:39 -0700247 /* If we just finished a non-skipped #if/#ifdef/#ifndef block,
248 * don't bother expanding or parsing the expression.
249 */
250 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP)
251 parser->skip_stack->type = SKIP_TO_ENDIF;
252 else
253 _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700254 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700255| HASH_ELIF NEWLINE {
256 /* #elif without an expression results in a warning if the
257 * condition doesn't matter (we just handled #if 1 or such)
258 * but an error otherwise. */
259 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
260 parser->skip_stack->type = SKIP_TO_ENDIF;
261 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
262 } else {
263 glcpp_error(& @1, parser, "#elif needs an expression");
264 }
265 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700266| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700267 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700268 }
269| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700270 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700271 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700272| HASH_VERSION integer_constant NEWLINE {
273 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
274 if (macro) {
275 hash_table_remove (parser->defines, "__VERSION__");
276 talloc_free (macro);
277 }
278 add_builtin_define (parser, "__VERSION__", $2);
279 glcpp_printf(parser->output, "#version %" PRIiMAX "\n", $2);
280 }
Carl Worth3ff81672010-05-25 13:09:03 -0700281| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700282;
283
Eric Anholtd4a04f32010-07-28 16:58:39 -0700284integer_constant:
Carl Worth050e3de2010-05-27 14:36:29 -0700285 INTEGER_STRING {
286 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
287 $$ = strtoll ($1 + 2, NULL, 16);
288 } else if ($1[0] == '0') {
289 $$ = strtoll ($1, NULL, 8);
290 } else {
291 $$ = strtoll ($1, NULL, 10);
292 }
293 }
294| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700295 $$ = $1;
296 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700297
298expression:
299 integer_constant
Carl Worth8fed1cd2010-05-26 09:32:12 -0700300| expression OR expression {
301 $$ = $1 || $3;
302 }
303| expression AND expression {
304 $$ = $1 && $3;
305 }
306| expression '|' expression {
307 $$ = $1 | $3;
308 }
309| expression '^' expression {
310 $$ = $1 ^ $3;
311 }
312| expression '&' expression {
313 $$ = $1 & $3;
314 }
315| expression NOT_EQUAL expression {
316 $$ = $1 != $3;
317 }
318| expression EQUAL expression {
319 $$ = $1 == $3;
320 }
321| expression GREATER_OR_EQUAL expression {
322 $$ = $1 >= $3;
323 }
324| expression LESS_OR_EQUAL expression {
325 $$ = $1 <= $3;
326 }
327| expression '>' expression {
328 $$ = $1 > $3;
329 }
330| expression '<' expression {
331 $$ = $1 < $3;
332 }
333| expression RIGHT_SHIFT expression {
334 $$ = $1 >> $3;
335 }
336| expression LEFT_SHIFT expression {
337 $$ = $1 << $3;
338 }
339| expression '-' expression {
340 $$ = $1 - $3;
341 }
342| expression '+' expression {
343 $$ = $1 + $3;
344 }
345| expression '%' expression {
346 $$ = $1 % $3;
347 }
348| expression '/' expression {
349 $$ = $1 / $3;
350 }
351| expression '*' expression {
352 $$ = $1 * $3;
353 }
354| '!' expression %prec UNARY {
355 $$ = ! $2;
356 }
357| '~' expression %prec UNARY {
358 $$ = ~ $2;
359 }
360| '-' expression %prec UNARY {
361 $$ = - $2;
362 }
363| '+' expression %prec UNARY {
364 $$ = + $2;
365 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700366| '(' expression ')' {
367 $$ = $2;
368 }
369;
370
Carl Worth3ff81672010-05-25 13:09:03 -0700371identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700372 IDENTIFIER {
373 $$ = _string_list_create (parser);
374 _string_list_append_item ($$, $1);
375 talloc_steal ($$, $1);
376 }
377| identifier_list ',' IDENTIFIER {
378 $$ = $1;
379 _string_list_append_item ($$, $3);
380 talloc_steal ($$, $3);
381 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700382;
383
Carl Worth3ff81672010-05-25 13:09:03 -0700384text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700385 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700386| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700387;
388
Carl Worth3ff81672010-05-25 13:09:03 -0700389non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700390 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700391 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700392 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700393;
394
Carl Worthaaa9acb2010-05-19 13:28:24 -0700395replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700396 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700397| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700398;
399
Kenneth Graunke65875742010-06-18 20:08:15 -0700400junk:
401 /* empty */
402| pp_tokens {
403 glcpp_warning(&@1, parser, "extra tokens at end of directive");
404 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700405;
406
407conditional_token:
408 /* Handle "defined" operator */
409 DEFINED IDENTIFIER {
410 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
411 $$ = _token_create_ival (parser, INTEGER, v);
412 }
413| DEFINED '(' IDENTIFIER ')' {
414 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
415 $$ = _token_create_ival (parser, INTEGER, v);
416 }
417| preprocessing_token
418;
419
420conditional_tokens:
421 /* Exactly the same as pp_tokens, but using conditional_token */
422 conditional_token {
423 parser->space_tokens = 1;
424 $$ = _token_list_create (parser);
425 _token_list_append ($$, $1);
426 talloc_unlink (parser, $1);
427 }
428| conditional_tokens conditional_token {
429 $$ = $1;
430 _token_list_append ($$, $2);
431 talloc_unlink (parser, $2);
432 }
433;
Kenneth Graunke65875742010-06-18 20:08:15 -0700434
Carl Worthaaa9acb2010-05-19 13:28:24 -0700435pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700436 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700437 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700438 $$ = _token_list_create (parser);
439 _token_list_append ($$, $1);
440 talloc_unlink (parser, $1);
441 }
442| pp_tokens preprocessing_token {
443 $$ = $1;
444 _token_list_append ($$, $2);
445 talloc_unlink (parser, $2);
446 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700447;
448
Carl Worth3ff81672010-05-25 13:09:03 -0700449preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700450 IDENTIFIER {
451 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700452 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700453 }
Carl Worth050e3de2010-05-27 14:36:29 -0700454| INTEGER_STRING {
455 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700456 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700457 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700458| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700459 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700460 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700461 }
462| OTHER {
463 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700464 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700465 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700466| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700467 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700468 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700469 }
Carl Worth3ff81672010-05-25 13:09:03 -0700470;
471
Carl Worth8e82fcb2010-05-26 11:15:21 -0700472operator:
Carl Worth808401f2010-05-25 14:52:43 -0700473 '[' { $$ = '['; }
474| ']' { $$ = ']'; }
475| '(' { $$ = '('; }
476| ')' { $$ = ')'; }
477| '{' { $$ = '{'; }
478| '}' { $$ = '}'; }
479| '.' { $$ = '.'; }
480| '&' { $$ = '&'; }
481| '*' { $$ = '*'; }
482| '+' { $$ = '+'; }
483| '-' { $$ = '-'; }
484| '~' { $$ = '~'; }
485| '!' { $$ = '!'; }
486| '/' { $$ = '/'; }
487| '%' { $$ = '%'; }
488| LEFT_SHIFT { $$ = LEFT_SHIFT; }
489| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
490| '<' { $$ = '<'; }
491| '>' { $$ = '>'; }
492| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
493| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
494| EQUAL { $$ = EQUAL; }
495| NOT_EQUAL { $$ = NOT_EQUAL; }
496| '^' { $$ = '^'; }
497| '|' { $$ = '|'; }
498| AND { $$ = AND; }
499| OR { $$ = OR; }
500| ';' { $$ = ';'; }
501| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700502| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700503| PASTE { $$ = PASTE; }
Carl Worth3ff81672010-05-25 13:09:03 -0700504;
505
Carl Worth33cc4002010-05-12 12:17:10 -0700506%%
507
Carl Worth610053b2010-05-14 10:05:11 -0700508string_list_t *
509_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700510{
Carl Worth610053b2010-05-14 10:05:11 -0700511 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700512
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700513 list = talloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700514 list->head = NULL;
515 list->tail = NULL;
516
517 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700518}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700519
Carl Worth33cc4002010-05-12 12:17:10 -0700520void
Carl Worth610053b2010-05-14 10:05:11 -0700521_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700522{
Carl Worth610053b2010-05-14 10:05:11 -0700523 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700524
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700525 node = talloc (list, string_node_t);
526 node->str = talloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700527
Carl Worth33cc4002010-05-12 12:17:10 -0700528 node->next = NULL;
529
530 if (list->head == NULL) {
531 list->head = node;
532 } else {
533 list->tail->next = node;
534 }
535
536 list->tail = node;
537}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700538
539int
Carl Worth610053b2010-05-14 10:05:11 -0700540_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700541{
Carl Worth610053b2010-05-14 10:05:11 -0700542 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700543 int i;
544
545 if (list == NULL)
546 return 0;
547
548 for (i = 0, node = list->head; node; i++, node = node->next) {
549 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700550 if (index)
551 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700552 return 1;
553 }
554 }
555
556 return 0;
557}
558
559int
Carl Worth610053b2010-05-14 10:05:11 -0700560_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700561{
562 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700563 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700564
565 if (list == NULL)
566 return 0;
567
568 for (node = list->head; node; node = node->next)
569 length++;
570
571 return length;
572}
573
Carl Worth8f6a8282010-05-14 10:44:19 -0700574argument_list_t *
575_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700576{
Carl Worth8f6a8282010-05-14 10:44:19 -0700577 argument_list_t *list;
578
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700579 list = talloc (ctx, argument_list_t);
Carl Worth8f6a8282010-05-14 10:44:19 -0700580 list->head = NULL;
581 list->tail = NULL;
582
583 return list;
584}
585
586void
Carl Worth47252442010-05-19 13:54:37 -0700587_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700588{
589 argument_node_t *node;
590
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700591 node = talloc (list, argument_node_t);
Carl Worth8f6a8282010-05-14 10:44:19 -0700592 node->argument = argument;
593
594 node->next = NULL;
595
596 if (list->head == NULL) {
597 list->head = node;
598 } else {
599 list->tail->next = node;
600 }
601
602 list->tail = node;
603}
604
605int
606_argument_list_length (argument_list_t *list)
607{
608 int length = 0;
609 argument_node_t *node;
610
611 if (list == NULL)
612 return 0;
613
614 for (node = list->head; node; node = node->next)
615 length++;
616
617 return length;
618}
619
Carl Worth47252442010-05-19 13:54:37 -0700620token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700621_argument_list_member_at (argument_list_t *list, int index)
622{
623 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700624 int i;
625
626 if (list == NULL)
627 return NULL;
628
629 node = list->head;
630 for (i = 0; i < index; i++) {
631 node = node->next;
632 if (node == NULL)
633 break;
634 }
635
636 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700637 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700638
639 return NULL;
640}
Carl Worth47252442010-05-19 13:54:37 -0700641
Carl Worth808401f2010-05-25 14:52:43 -0700642/* Note: This function talloc_steal()s the str pointer. */
643token_t *
644_token_create_str (void *ctx, int type, char *str)
645{
646 token_t *token;
647
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700648 token = talloc (ctx, token_t);
Carl Worth808401f2010-05-25 14:52:43 -0700649 token->type = type;
650 token->value.str = talloc_steal (token, str);
651
652 return token;
653}
654
655token_t *
656_token_create_ival (void *ctx, int type, int ival)
657{
658 token_t *token;
659
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700660 token = talloc (ctx, token_t);
Carl Worth808401f2010-05-25 14:52:43 -0700661 token->type = type;
662 token->value.ival = ival;
663
664 return token;
665}
666
Carl Worth47252442010-05-19 13:54:37 -0700667token_list_t *
668_token_list_create (void *ctx)
669{
670 token_list_t *list;
671
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700672 list = talloc (ctx, token_list_t);
Carl Worth47252442010-05-19 13:54:37 -0700673 list->head = NULL;
674 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700675 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700676
677 return list;
678}
679
680void
Carl Worth808401f2010-05-25 14:52:43 -0700681_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700682{
683 token_node_t *node;
684
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700685 node = talloc (list, token_node_t);
686 node->token = talloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700687
688 node->next = NULL;
689
690 if (list->head == NULL) {
691 list->head = node;
692 } else {
693 list->tail->next = node;
694 }
695
696 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700697 if (token->type != SPACE)
698 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700699}
700
701void
702_token_list_append_list (token_list_t *list, token_list_t *tail)
703{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700704 if (tail == NULL || tail->head == NULL)
705 return;
706
Carl Worth47252442010-05-19 13:54:37 -0700707 if (list->head == NULL) {
708 list->head = tail->head;
709 } else {
710 list->tail->next = tail->head;
711 }
712
713 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700714 list->non_space_tail = tail->non_space_tail;
715}
716
Carl Worthd80dcaf2010-07-20 15:55:21 -0700717static token_list_t *
Carl Worth681afbc2010-05-28 15:06:02 -0700718_token_list_copy (void *ctx, token_list_t *other)
719{
720 token_list_t *copy;
721 token_node_t *node;
722
723 if (other == NULL)
724 return NULL;
725
726 copy = _token_list_create (ctx);
727 for (node = other->head; node; node = node->next)
728 _token_list_append (copy, node->token);
729
730 return copy;
731}
732
Carl Worthd80dcaf2010-07-20 15:55:21 -0700733static void
Carl Worth10ae4382010-05-25 20:35:01 -0700734_token_list_trim_trailing_space (token_list_t *list)
735{
736 token_node_t *tail, *next;
737
738 if (list->non_space_tail) {
739 tail = list->non_space_tail->next;
740 list->non_space_tail->next = NULL;
741 list->tail = list->non_space_tail;
742
743 while (tail) {
744 next = tail->next;
745 talloc_free (tail);
746 tail = next;
747 }
748 }
Carl Worth47252442010-05-19 13:54:37 -0700749}
Carl Worth80dc60b2010-05-25 14:42:00 -0700750
Carl Worth0197e9b2010-05-26 08:05:19 -0700751static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700752_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700753{
754 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700755 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700756 return;
757 }
758
759 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700760 case INTEGER:
Eric Anholt8605c292010-07-28 16:53:51 -0700761 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700762 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700763 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700764 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700765 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700766 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700767 break;
768 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700769 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700770 break;
771 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700772 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700773 break;
774 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700775 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700776 break;
777 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700778 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700779 break;
780 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700781 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700782 break;
783 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700784 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700785 break;
786 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700787 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700788 break;
789 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700790 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700791 break;
792 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700793 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700794 break;
795 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700796 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700797 break;
Carl Worthdd749002010-05-27 10:12:33 -0700798 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700799 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700800 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700801 case PLACEHOLDER:
802 /* Nothing to print. */
803 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700804 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700805 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700806 break;
807 }
808}
809
Carl Worthb06096e2010-05-29 05:54:19 -0700810/* Return a new token (talloc()ed off of 'token') formed by pasting
811 * 'token' and 'other'. Note that this function may return 'token' or
812 * 'other' directly rather than allocating anything new.
813 *
814 * Caution: Only very cursory error-checking is performed to see if
815 * the final result is a valid single token. */
816static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700817_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700818{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700819 token_t *combined = NULL;
820
Carl Worth85b50e82010-05-27 14:01:18 -0700821 /* Pasting a placeholder onto anything makes no change. */
822 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700823 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700824
Carl Worthb06096e2010-05-29 05:54:19 -0700825 /* When 'token' is a placeholder, just return 'other'. */
826 if (token->type == PLACEHOLDER)
827 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700828
Carl Worthad0dee62010-05-26 09:04:50 -0700829 /* A very few single-character punctuators can be combined
830 * with another to form a multi-character punctuator. */
831 switch (token->type) {
832 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700833 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700834 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700835 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700836 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700837 break;
838 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700839 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700840 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700841 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700842 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700843 break;
844 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700845 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700846 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700847 break;
848 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700849 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700850 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700851 break;
852 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700853 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700854 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700855 break;
856 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700857 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700858 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700859 break;
860 }
861
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700862 if (combined != NULL) {
863 /* Inherit the location from the first token */
864 combined->location = token->location;
865 return combined;
866 }
867
Carl Worthad0dee62010-05-26 09:04:50 -0700868 /* Two string-valued tokens can usually just be mashed
869 * together.
870 *
Carl Worth050e3de2010-05-27 14:36:29 -0700871 * XXX: This isn't actually legitimate. Several things here
872 * should result in a diagnostic since the result cannot be a
873 * valid, single pre-processing token. For example, pasting
874 * "123" and "abc" is not legal, but we don't catch that
875 * here. */
876 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
877 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700878 {
Carl Worthb06096e2010-05-29 05:54:19 -0700879 char *str;
880
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700881 str = talloc_asprintf (token, "%s%s", token->value.str,
882 other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700883 combined = _token_create_str (token, token->type, str);
884 combined->location = token->location;
885 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700886 }
887
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700888 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700889 glcpp_print (parser->info_log, "Pasting \"");
890 _token_print (&parser->info_log, token);
891 glcpp_print (parser->info_log, "\" and \"");
892 _token_print (&parser->info_log, other);
893 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700894
895 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700896}
897
Carl Worth0197e9b2010-05-26 08:05:19 -0700898static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700899_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700900{
901 token_node_t *node;
902
903 if (list == NULL)
904 return;
905
906 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700907 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700908}
909
Carl Worth3a37b872010-05-10 11:44:09 -0700910void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700911yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700912{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700913 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700914}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700915
Eric Anholtd4a04f32010-07-28 16:58:39 -0700916static void add_builtin_define(glcpp_parser_t *parser,
917 const char *name, int value)
918{
919 token_t *tok;
920 token_list_t *list;
921
922 tok = _token_create_ival (parser, INTEGER, value);
923
924 list = _token_list_create(parser);
925 _token_list_append(list, tok);
926 _define_object_macro(parser, NULL, name, list);
927
928 talloc_unlink(parser, tok);
929}
930
Carl Worth33cc4002010-05-12 12:17:10 -0700931glcpp_parser_t *
Ian Romanick06143ea2010-06-30 16:27:22 -0700932glcpp_parser_create (const struct gl_extensions *extensions)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700933{
Carl Worth33cc4002010-05-12 12:17:10 -0700934 glcpp_parser_t *parser;
Eric Anholtd4a04f32010-07-28 16:58:39 -0700935 int language_version;
Carl Worth33cc4002010-05-12 12:17:10 -0700936
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700937 parser = talloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700938
Carl Worth8f38aff2010-05-19 10:01:29 -0700939 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700940 parser->defines = hash_table_ctor (32, hash_table_string_hash,
941 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700942 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700943 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700944 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700945 parser->newline_as_space = 0;
946 parser->in_control_line = 0;
947 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700948
Carl Worthb20d33c2010-05-20 22:27:07 -0700949 parser->skip_stack = NULL;
950
Carl Worth8e82fcb2010-05-26 11:15:21 -0700951 parser->lex_from_list = NULL;
952 parser->lex_from_node = NULL;
953
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700954 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700955 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke1b85c462010-06-21 13:55:12 -0700956 parser->error = 0;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700957
Ian Romanick2d122362010-06-30 16:03:19 -0700958 /* Add pre-defined macros. */
Eric Anholtd4a04f32010-07-28 16:58:39 -0700959 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
960 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
Ian Romanick2d122362010-06-30 16:03:19 -0700961
Eric Anholtd4a04f32010-07-28 16:58:39 -0700962 if (extensions != NULL) {
963 if (extensions->EXT_texture_array) {
964 add_builtin_define(parser, "GL_EXT_texture_array", 1);
965 }
Ian Romanick2d122362010-06-30 16:03:19 -0700966
Eric Anholtd4a04f32010-07-28 16:58:39 -0700967 if (extensions->ARB_fragment_coord_conventions)
968 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
969 1);
Ian Romanick06143ea2010-06-30 16:27:22 -0700970 }
971
Eric Anholtd4a04f32010-07-28 16:58:39 -0700972 language_version = 110;
Eric Anholtd4a04f32010-07-28 16:58:39 -0700973 add_builtin_define(parser, "__VERSION__", language_version);
Ian Romanick2d122362010-06-30 16:03:19 -0700974
Carl Worth33cc4002010-05-12 12:17:10 -0700975 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700976}
977
978int
979glcpp_parser_parse (glcpp_parser_t *parser)
980{
981 return yyparse (parser);
982}
983
984void
Carl Worth33cc4002010-05-12 12:17:10 -0700985glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700986{
Carl Worthb20d33c2010-05-20 22:27:07 -0700987 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700988 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700989 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700990 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700991 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700992}
Carl Worthc6d5af32010-05-11 12:30:09 -0700993
Carl Worthb1854fd2010-05-25 16:28:26 -0700994typedef enum function_status
995{
996 FUNCTION_STATUS_SUCCESS,
997 FUNCTION_NOT_A_FUNCTION,
998 FUNCTION_UNBALANCED_PARENTHESES
999} function_status_t;
1000
1001/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -07001002 * balanced set of parentheses.
1003 *
1004 * When called, 'node' should be the opening-parenthesis token, (or
1005 * perhaps preceeding SPACE tokens). Upon successful return *last will
1006 * be the last consumed node, (corresponding to the closing right
1007 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -07001008 *
1009 * Return values:
1010 *
1011 * FUNCTION_STATUS_SUCCESS:
1012 *
1013 * Successfully parsed a set of function arguments.
1014 *
1015 * FUNCTION_NOT_A_FUNCTION:
1016 *
1017 * Macro name not followed by a '('. This is not an error, but
1018 * simply that the macro name should be treated as a non-macro.
1019 *
Carl Worth14c98a52010-06-02 15:49:54 -07001020 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -07001021 *
1022 * Macro name is not followed by a balanced set of parentheses.
1023 */
1024static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -07001025_arguments_parse (argument_list_t *arguments,
1026 token_node_t *node,
1027 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001028{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001029 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001030 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001031
Carl Worthb1854fd2010-05-25 16:28:26 -07001032 node = node->next;
1033
1034 /* Ignore whitespace before first parenthesis. */
1035 while (node && node->token->type == SPACE)
1036 node = node->next;
1037
1038 if (node == NULL || node->token->type != '(')
1039 return FUNCTION_NOT_A_FUNCTION;
1040
Carl Worth652fa272010-05-25 17:45:22 -07001041 node = node->next;
1042
Carl Wortha19297b2010-05-27 13:29:19 -07001043 argument = _token_list_create (arguments);
1044 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001045
Carl Worth681afbc2010-05-28 15:06:02 -07001046 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001047 if (node->token->type == '(')
1048 {
1049 paren_count++;
1050 }
1051 else if (node->token->type == ')')
1052 {
1053 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001054 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001055 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001056 }
Carl Worth652fa272010-05-25 17:45:22 -07001057
1058 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001059 paren_count == 1)
1060 {
Carl Wortha19297b2010-05-27 13:29:19 -07001061 _token_list_trim_trailing_space (argument);
1062 argument = _token_list_create (arguments);
1063 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001064 }
1065 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001066 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001067 /* Don't treat initial whitespace as
1068 * part of the arguement. */
1069 if (node->token->type == SPACE)
1070 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001071 }
1072 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001073 }
Carl Worthc7581c22010-05-25 17:41:07 -07001074 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001075
Carl Worth681afbc2010-05-28 15:06:02 -07001076 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001077 return FUNCTION_UNBALANCED_PARENTHESES;
1078
Carl Worth681afbc2010-05-28 15:06:02 -07001079 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001080
1081 return FUNCTION_STATUS_SUCCESS;
1082}
1083
Carl Worthe1acbfc2010-07-20 16:44:03 -07001084static token_list_t *
1085_token_list_create_with_one_space (void *ctx)
1086{
1087 token_list_t *list;
1088 token_t *space;
1089
1090 list = _token_list_create (ctx);
1091 space = _token_create_ival (list, SPACE, SPACE);
1092 _token_list_append (list, space);
1093
1094 return list;
1095}
1096
Kenneth Graunke16b4eed2010-08-04 16:10:03 -07001097static void
1098_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1099{
1100 token_list_t *expanded;
1101 token_t *token;
1102
1103 expanded = _token_list_create (parser);
1104 token = _token_create_ival (parser, type, type);
1105 _token_list_append (expanded, token);
1106 _glcpp_parser_expand_token_list (parser, list);
1107 _token_list_append_list (expanded, list);
1108 glcpp_parser_lex_from (parser, expanded);
1109}
1110
Carl Worth681afbc2010-05-28 15:06:02 -07001111/* This is a helper function that's essentially part of the
1112 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1113 * except for by that function.
1114 *
1115 * Returns NULL if node is a simple token with no expansion, (that is,
1116 * although 'node' corresponds to an identifier defined as a
1117 * function-like macro, it is not followed with a parenthesized
1118 * argument list).
1119 *
1120 * Compute the complete expansion of node (which is a function-like
1121 * macro) and subsequent nodes which are arguments.
1122 *
1123 * Returns the token list that results from the expansion and sets
1124 * *last to the last node in the list that was consumed by the
Carl Worthfbe42402010-07-22 16:36:04 -07001125 * expansion. Specifically, *last will be set as follows: as the
Carl Worth681afbc2010-05-28 15:06:02 -07001126 * token of the closing right parenthesis.
1127 */
1128static token_list_t *
1129_glcpp_parser_expand_function (glcpp_parser_t *parser,
1130 token_node_t *node,
1131 token_node_t **last)
1132
Carl Worthb1854fd2010-05-25 16:28:26 -07001133{
1134 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001135 const char *identifier;
1136 argument_list_t *arguments;
1137 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001138 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001139 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001140
Carl Worthb1854fd2010-05-25 16:28:26 -07001141 identifier = node->token->value.str;
1142
1143 macro = hash_table_find (parser->defines, identifier);
1144
1145 assert (macro->is_function);
1146
Carl Worth9ce18cf2010-05-25 17:32:21 -07001147 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001148 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001149
1150 switch (status) {
1151 case FUNCTION_STATUS_SUCCESS:
1152 break;
1153 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001154 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001155 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001156 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001157 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001158 }
1159
Carl Worthe1acbfc2010-07-20 16:44:03 -07001160 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth9ce18cf2010-05-25 17:32:21 -07001161 if (macro->replacements == NULL) {
1162 talloc_free (arguments);
Carl Worthe1acbfc2010-07-20 16:44:03 -07001163 return _token_list_create_with_one_space (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001164 }
1165
Carl Wortha19297b2010-05-27 13:29:19 -07001166 if (! ((_argument_list_length (arguments) ==
1167 _string_list_length (macro->parameters)) ||
1168 (_string_list_length (macro->parameters) == 0 &&
1169 _argument_list_length (arguments) == 1 &&
1170 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001171 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001172 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001173 "Error: macro %s invoked with %d arguments (expected %d)\n",
1174 identifier,
1175 _argument_list_length (arguments),
1176 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001177 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001178 }
1179
Carl Worth0197e9b2010-05-26 08:05:19 -07001180 /* Perform argument substitution on the replacement list. */
1181 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001182
Carl Worthce540f22010-05-26 08:25:44 -07001183 for (node = macro->replacements->head; node; node = node->next)
1184 {
1185 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001186 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001187 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001188 &parameter_index))
1189 {
1190 token_list_t *argument;
1191 argument = _argument_list_member_at (arguments,
1192 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001193 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001194 * tokens, or append a placeholder token for
1195 * an empty argument. */
1196 if (argument->head) {
Carl Worthfbe42402010-07-22 16:36:04 -07001197 token_list_t *expanded_argument;
1198 expanded_argument = _token_list_copy (parser,
1199 argument);
Carl Worth681afbc2010-05-28 15:06:02 -07001200 _glcpp_parser_expand_token_list (parser,
Carl Worthfbe42402010-07-22 16:36:04 -07001201 expanded_argument);
1202 _token_list_append_list (substituted,
1203 expanded_argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001204 } else {
1205 token_t *new_token;
1206
1207 new_token = _token_create_ival (substituted,
1208 PLACEHOLDER,
1209 PLACEHOLDER);
1210 _token_list_append (substituted, new_token);
1211 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001212 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001213 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001214 }
1215 }
1216
Carl Worthad0dee62010-05-26 09:04:50 -07001217 /* After argument substitution, and before further expansion
1218 * below, implement token pasting. */
1219
Carl Worthb06096e2010-05-29 05:54:19 -07001220 _token_list_trim_trailing_space (substituted);
1221
Carl Worthad0dee62010-05-26 09:04:50 -07001222 node = substituted->head;
1223 while (node)
1224 {
1225 token_node_t *next_non_space;
1226
1227 /* Look ahead for a PASTE token, skipping space. */
1228 next_non_space = node->next;
1229 while (next_non_space && next_non_space->token->type == SPACE)
1230 next_non_space = next_non_space->next;
1231
1232 if (next_non_space == NULL)
1233 break;
1234
1235 if (next_non_space->token->type != PASTE) {
1236 node = next_non_space;
1237 continue;
1238 }
1239
1240 /* Now find the next non-space token after the PASTE. */
1241 next_non_space = next_non_space->next;
1242 while (next_non_space && next_non_space->token->type == SPACE)
1243 next_non_space = next_non_space->next;
1244
1245 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001246 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001247 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001248 }
1249
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001250 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001251 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001252 if (next_non_space == substituted->tail)
1253 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001254
1255 node = node->next;
1256 }
1257
Carl Worthb06096e2010-05-29 05:54:19 -07001258 substituted->non_space_tail = substituted->tail;
1259
Carl Worth681afbc2010-05-28 15:06:02 -07001260 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001261}
1262
Carl Worth681afbc2010-05-28 15:06:02 -07001263/* Compute the complete expansion of node, (and subsequent nodes after
1264 * 'node' in the case that 'node' is a function-like macro and
1265 * subsequent nodes are arguments).
1266 *
1267 * Returns NULL if node is a simple token with no expansion.
1268 *
1269 * Otherwise, returns the token list that results from the expansion
1270 * and sets *last to the last node in the list that was consumed by
Carl Worthe1acbfc2010-07-20 16:44:03 -07001271 * the expansion. Specifically, *last will be set as follows:
Carl Worth681afbc2010-05-28 15:06:02 -07001272 *
1273 * As 'node' in the case of object-like macro expansion.
1274 *
1275 * As the token of the closing right parenthesis in the case of
1276 * function-like macro expansion.
1277 */
1278static token_list_t *
1279_glcpp_parser_expand_node (glcpp_parser_t *parser,
1280 token_node_t *node,
1281 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001282{
Carl Worth681afbc2010-05-28 15:06:02 -07001283 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001284 const char *identifier;
1285 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001286
1287 /* We only expand identifiers */
1288 if (token->type != IDENTIFIER) {
1289 /* We change any COMMA into a COMMA_FINAL to prevent
1290 * it being mistaken for an argument separator
1291 * later. */
1292 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001293 token->type = COMMA_FINAL;
1294 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001295 }
Carl Worth681afbc2010-05-28 15:06:02 -07001296
1297 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001298 }
1299
1300 /* Look up this identifier in the hash table. */
1301 identifier = token->value.str;
1302 macro = hash_table_find (parser->defines, identifier);
1303
Carl Worth681afbc2010-05-28 15:06:02 -07001304 /* Not a macro, so no expansion needed. */
1305 if (macro == NULL)
1306 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001307
1308 /* Finally, don't expand this macro if we're already actively
1309 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001310 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001311 /* We change the token type here from IDENTIFIER to
1312 * OTHER to prevent any future expansion of this
1313 * unexpanded token. */
1314 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001315 token_list_t *expansion;
1316 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001317
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001318 str = talloc_strdup (parser, token->value.str);
Carl Worth681afbc2010-05-28 15:06:02 -07001319 final = _token_create_str (parser, OTHER, str);
1320 expansion = _token_list_create (parser);
1321 _token_list_append (expansion, final);
1322 *last = node;
1323 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001324 }
1325
Carl Worth681afbc2010-05-28 15:06:02 -07001326 if (! macro->is_function)
1327 {
1328 *last = node;
1329
Carl Worthe1acbfc2010-07-20 16:44:03 -07001330 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth681afbc2010-05-28 15:06:02 -07001331 if (macro->replacements == NULL)
Carl Worthe1acbfc2010-07-20 16:44:03 -07001332 return _token_list_create_with_one_space (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001333
Carl Worth22b3ace2010-06-02 15:32:03 -07001334 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001335 }
Carl Worth681afbc2010-05-28 15:06:02 -07001336
1337 return _glcpp_parser_expand_function (parser, node, last);
1338}
1339
Carl Worth22b3ace2010-06-02 15:32:03 -07001340/* Push a new identifier onto the active list, returning the new list.
1341 *
1342 * Here, 'marker' is the token node that appears in the list after the
1343 * expansion of 'identifier'. That is, when the list iterator begins
1344 * examinging 'marker', then it is time to pop this node from the
1345 * active stack.
1346 */
1347active_list_t *
1348_active_list_push (active_list_t *list,
1349 const char *identifier,
1350 token_node_t *marker)
1351{
1352 active_list_t *node;
1353
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001354 node = talloc (list, active_list_t);
1355 node->identifier = talloc_strdup (node, identifier);
Carl Worth22b3ace2010-06-02 15:32:03 -07001356 node->marker = marker;
1357 node->next = list;
1358
1359 return node;
1360}
1361
1362active_list_t *
1363_active_list_pop (active_list_t *list)
1364{
1365 active_list_t *node = list;
1366
1367 if (node == NULL)
1368 return NULL;
1369
1370 node = list->next;
1371 talloc_free (list);
1372
1373 return node;
1374}
1375
1376int
1377_active_list_contains (active_list_t *list, const char *identifier)
1378{
1379 active_list_t *node;
1380
1381 if (list == NULL)
1382 return 0;
1383
1384 for (node = list; node; node = node->next)
1385 if (strcmp (node->identifier, identifier) == 0)
1386 return 1;
1387
1388 return 0;
1389}
1390
Carl Worth681afbc2010-05-28 15:06:02 -07001391/* Walk over the token list replacing nodes with their expansion.
1392 * Whenever nodes are expanded the walking will walk over the new
1393 * nodes, continuing to expand as necessary. The results are placed in
1394 * 'list' itself;
1395 */
1396static void
1397_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1398 token_list_t *list)
1399{
1400 token_node_t *node_prev;
Carl Worthc42e6402010-06-22 15:51:34 -07001401 token_node_t *node, *last = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001402 token_list_t *expansion;
1403
1404 if (list == NULL)
1405 return;
1406
1407 _token_list_trim_trailing_space (list);
1408
1409 node_prev = NULL;
1410 node = list->head;
1411
1412 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001413
1414 while (parser->active && parser->active->marker == node)
1415 parser->active = _active_list_pop (parser->active);
1416
Carl Worth681afbc2010-05-28 15:06:02 -07001417 /* Find the expansion for node, which will replace all
1418 * nodes from node to last, inclusive. */
1419 expansion = _glcpp_parser_expand_node (parser, node, &last);
1420 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001421 token_node_t *n;
1422
1423 for (n = node; n != last->next; n = n->next)
1424 while (parser->active &&
1425 parser->active->marker == n)
1426 {
1427 parser->active = _active_list_pop (parser->active);
1428 }
1429
1430 parser->active = _active_list_push (parser->active,
1431 node->token->value.str,
1432 last->next);
1433
Carl Worth681afbc2010-05-28 15:06:02 -07001434 /* Splice expansion into list, supporting a
1435 * simple deletion if the expansion is
1436 * empty. */
1437 if (expansion->head) {
1438 if (node_prev)
1439 node_prev->next = expansion->head;
1440 else
1441 list->head = expansion->head;
1442 expansion->tail->next = last->next;
1443 if (last == list->tail)
1444 list->tail = expansion->tail;
1445 } else {
1446 if (node_prev)
1447 node_prev->next = last->next;
1448 else
1449 list->head = last->next;
1450 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001451 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001452 }
1453 } else {
1454 node_prev = node;
1455 }
1456 node = node_prev ? node_prev->next : list->head;
1457 }
1458
Carl Worth22b3ace2010-06-02 15:32:03 -07001459 while (parser->active)
1460 parser->active = _active_list_pop (parser->active);
1461
Carl Worth681afbc2010-05-28 15:06:02 -07001462 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001463}
1464
Carl Worthae6517f2010-05-25 15:24:59 -07001465void
1466_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1467 token_list_t *list)
1468{
Carl Worthae6517f2010-05-25 15:24:59 -07001469 if (list == NULL)
1470 return;
1471
Carl Worth681afbc2010-05-28 15:06:02 -07001472 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001473
Carl Worth681afbc2010-05-28 15:06:02 -07001474 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001475
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001476 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001477}
1478
Carl Worthd80dcaf2010-07-20 15:55:21 -07001479static void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001480_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1481 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001482{
1483 /* According to the GLSL specification, macro names starting with "__"
1484 * or "GL_" are reserved for future use. So, don't allow them.
1485 */
1486 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001487 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001488 }
1489 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001490 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001491 }
1492}
1493
1494void
Carl Worthfcbbb462010-05-13 09:36:23 -07001495_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001496 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001497 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001498 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001499{
1500 macro_t *macro;
1501
Ian Romanick2d122362010-06-30 16:03:19 -07001502 if (loc != NULL)
1503 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001504
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001505 macro = talloc (parser, macro_t);
Carl Worthfcbbb462010-05-13 09:36:23 -07001506
1507 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001508 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001509 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001510 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001511
1512 hash_table_insert (parser->defines, macro, identifier);
1513}
1514
1515void
1516_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001517 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001518 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001519 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001520 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001521{
1522 macro_t *macro;
1523
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001524 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001525
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001526 macro = talloc (parser, macro_t);
Carl Worthfcbbb462010-05-13 09:36:23 -07001527
1528 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001529 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001530 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001531 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001532
1533 hash_table_insert (parser->defines, macro, identifier);
1534}
1535
Carl Worth8f38aff2010-05-19 10:01:29 -07001536static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001537glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001538{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001539 token_node_t *node;
1540 int ret;
1541
Carl Worth95951ea2010-05-26 15:57:10 -07001542 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001543 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001544
1545 /* XXX: This ugly block of code exists for the sole
1546 * purpose of converting a NEWLINE token into a SPACE
1547 * token, but only in the case where we have seen a
1548 * function-like macro name, but have not yet seen its
1549 * closing parenthesis.
1550 *
1551 * There's perhaps a more compact way to do this with
1552 * mid-rule actions in the grammar.
1553 *
1554 * I'm definitely not pleased with the complexity of
1555 * this code here.
1556 */
1557 if (parser->newline_as_space)
1558 {
1559 if (ret == '(') {
1560 parser->paren_count++;
1561 } else if (ret == ')') {
1562 parser->paren_count--;
1563 if (parser->paren_count == 0)
1564 parser->newline_as_space = 0;
1565 } else if (ret == NEWLINE) {
1566 ret = SPACE;
1567 } else if (ret != SPACE) {
1568 if (parser->paren_count == 0)
1569 parser->newline_as_space = 0;
1570 }
1571 }
1572 else if (parser->in_control_line)
1573 {
1574 if (ret == NEWLINE)
1575 parser->in_control_line = 0;
1576 }
1577 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1578 ret == HASH_UNDEF || ret == HASH_IF ||
1579 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1580 ret == HASH_ELIF || ret == HASH_ELSE ||
1581 ret == HASH_ENDIF || ret == HASH)
1582 {
1583 parser->in_control_line = 1;
1584 }
1585 else if (ret == IDENTIFIER)
1586 {
1587 macro_t *macro;
1588 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001589 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001590 if (macro && macro->is_function) {
1591 parser->newline_as_space = 1;
1592 parser->paren_count = 0;
1593 }
1594 }
1595
1596 return ret;
1597 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001598
1599 node = parser->lex_from_node;
1600
1601 if (node == NULL) {
1602 talloc_free (parser->lex_from_list);
1603 parser->lex_from_list = NULL;
1604 return NEWLINE;
1605 }
1606
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001607 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001608 ret = node->token->type;
1609
1610 parser->lex_from_node = node->next;
1611
1612 return ret;
1613}
1614
1615static void
1616glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1617{
1618 token_node_t *node;
1619
1620 assert (parser->lex_from_list == NULL);
1621
1622 /* Copy list, eliminating any space tokens. */
1623 parser->lex_from_list = _token_list_create (parser);
1624
1625 for (node = list->head; node; node = node->next) {
1626 if (node->token->type == SPACE)
1627 continue;
1628 _token_list_append (parser->lex_from_list, node->token);
1629 }
1630
1631 talloc_free (list);
1632
1633 parser->lex_from_node = parser->lex_from_list->head;
1634
1635 /* It's possible the list consisted of nothing but whitespace. */
1636 if (parser->lex_from_node == NULL) {
1637 talloc_free (parser->lex_from_list);
1638 parser->lex_from_list = NULL;
1639 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001640}
Carl Worthb20d33c2010-05-20 22:27:07 -07001641
1642static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001643_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1644 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001645{
1646 skip_type_t current = SKIP_NO_SKIP;
1647 skip_node_t *node;
1648
1649 if (parser->skip_stack)
1650 current = parser->skip_stack->type;
1651
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001652 node = talloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001653 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001654
1655 if (current == SKIP_NO_SKIP) {
1656 if (condition)
1657 node->type = SKIP_NO_SKIP;
1658 else
1659 node->type = SKIP_TO_ELSE;
1660 } else {
1661 node->type = SKIP_TO_ENDIF;
1662 }
1663
1664 node->next = parser->skip_stack;
1665 parser->skip_stack = node;
1666}
1667
1668static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001669_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1670 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001671{
1672 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001673 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001674 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001675 }
1676
1677 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1678 if (condition)
1679 parser->skip_stack->type = SKIP_NO_SKIP;
1680 } else {
1681 parser->skip_stack->type = SKIP_TO_ENDIF;
1682 }
1683}
Carl Worth80dc60b2010-05-25 14:42:00 -07001684
Carl Worthb20d33c2010-05-20 22:27:07 -07001685static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001686_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001687{
1688 skip_node_t *node;
1689
1690 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001691 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001692 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001693 }
1694
1695 node = parser->skip_stack;
1696 parser->skip_stack = node->next;
1697 talloc_free (node);
1698}