blob: df1a649d9bcc0008e5e4ec8697da7a3d7b7f75fb [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
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700145%locations
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700146
Carl Worth0b27b5f2010-05-10 16:16:06 -0700147%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700148%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700149
Carl Worthefef9502010-07-28 11:10:52 -0700150%expect 0
Eric Anholtd4a04f32010-07-28 16:58:39 -0700151%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 -0700152%token PASTE
Eric Anholtd4a04f32010-07-28 16:58:39 -0700153%type <ival> expression INTEGER operator SPACE integer_constant
Carl Worth050e3de2010-05-27 14:36:29 -0700154%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700155%type <string_list> identifier_list
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700156%type <token> preprocessing_token conditional_token
157%type <token_list> pp_tokens replacement_list text_line conditional_tokens
Carl Worth8fed1cd2010-05-26 09:32:12 -0700158%left OR
159%left AND
160%left '|'
161%left '^'
162%left '&'
163%left EQUAL NOT_EQUAL
164%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
165%left LEFT_SHIFT RIGHT_SHIFT
166%left '+' '-'
167%left '*' '/' '%'
168%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700169
170%%
171
Carl Worth33cc4002010-05-12 12:17:10 -0700172input:
Carl Worth3ff81672010-05-25 13:09:03 -0700173 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700174| input line
175;
176
177line:
178 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700179 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700180 }
Carl Worth808401f2010-05-25 14:52:43 -0700181| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700182 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700183 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700184 talloc_free ($1);
185 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700186| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700187| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700188;
189
Carl Worth8e82fcb2010-05-26 11:15:21 -0700190expanded_line:
191 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700192 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700193 }
194| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700195 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700196 }
197;
198
Carl Worth3ff81672010-05-25 13:09:03 -0700199control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700200 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700201 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700202 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700203| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700204 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700205 }
206| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700207 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700208 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700209| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700210 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700211 if (macro) {
Carl Worth61ebc012010-07-19 18:02:12 -0700212 hash_table_remove (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700213 talloc_free (macro);
214 }
215 talloc_free ($2);
216 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700217| HASH_IF conditional_tokens NEWLINE {
Kenneth Graunkef4239872010-08-04 16:24:39 -0700218 /* If we're skipping to the next #elif/#else case or to #endif,
219 * don't bother expanding or parsing the expression.
220 */
221 if (parser->skip_stack != NULL && parser->skip_stack->type != SKIP_NO_SKIP) {
222 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
223 parser->skip_stack->type = SKIP_TO_ENDIF;
224 } else {
225 _glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
226 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700227 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700228| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700229 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700230 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700231 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700232 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700233| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700234 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700235 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700236 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700237 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700238| HASH_ELIF conditional_tokens NEWLINE {
Kenneth Graunkef4239872010-08-04 16:24:39 -0700239 /* If we just finished a non-skipped #if/#ifdef/#ifndef block,
240 * don't bother expanding or parsing the expression.
241 */
242 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP)
243 parser->skip_stack->type = SKIP_TO_ENDIF;
244 else
245 _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700246 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700247| HASH_ELIF NEWLINE {
248 /* #elif without an expression results in a warning if the
249 * condition doesn't matter (we just handled #if 1 or such)
250 * but an error otherwise. */
251 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
252 parser->skip_stack->type = SKIP_TO_ENDIF;
253 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
254 } else {
255 glcpp_error(& @1, parser, "#elif needs an expression");
256 }
257 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700258| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700259 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700260 }
261| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700262 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700263 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700264| HASH_VERSION integer_constant NEWLINE {
265 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
266 if (macro) {
267 hash_table_remove (parser->defines, "__VERSION__");
268 talloc_free (macro);
269 }
270 add_builtin_define (parser, "__VERSION__", $2);
271 glcpp_printf(parser->output, "#version %" PRIiMAX "\n", $2);
272 }
Carl Worth3ff81672010-05-25 13:09:03 -0700273| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700274;
275
Eric Anholtd4a04f32010-07-28 16:58:39 -0700276integer_constant:
Carl Worth050e3de2010-05-27 14:36:29 -0700277 INTEGER_STRING {
278 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
279 $$ = strtoll ($1 + 2, NULL, 16);
280 } else if ($1[0] == '0') {
281 $$ = strtoll ($1, NULL, 8);
282 } else {
283 $$ = strtoll ($1, NULL, 10);
284 }
285 }
286| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700287 $$ = $1;
288 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700289
290expression:
291 integer_constant
Carl Worth8fed1cd2010-05-26 09:32:12 -0700292| expression OR expression {
293 $$ = $1 || $3;
294 }
295| expression AND 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 NOT_EQUAL expression {
308 $$ = $1 != $3;
309 }
310| expression EQUAL expression {
311 $$ = $1 == $3;
312 }
313| expression GREATER_OR_EQUAL expression {
314 $$ = $1 >= $3;
315 }
316| expression LESS_OR_EQUAL expression {
317 $$ = $1 <= $3;
318 }
319| expression '>' expression {
320 $$ = $1 > $3;
321 }
322| expression '<' expression {
323 $$ = $1 < $3;
324 }
325| expression RIGHT_SHIFT expression {
326 $$ = $1 >> $3;
327 }
328| expression LEFT_SHIFT expression {
329 $$ = $1 << $3;
330 }
331| expression '-' expression {
332 $$ = $1 - $3;
333 }
334| expression '+' expression {
335 $$ = $1 + $3;
336 }
337| expression '%' expression {
338 $$ = $1 % $3;
339 }
340| expression '/' expression {
341 $$ = $1 / $3;
342 }
343| expression '*' expression {
344 $$ = $1 * $3;
345 }
346| '!' expression %prec UNARY {
347 $$ = ! $2;
348 }
349| '~' expression %prec UNARY {
350 $$ = ~ $2;
351 }
352| '-' expression %prec UNARY {
353 $$ = - $2;
354 }
355| '+' expression %prec UNARY {
356 $$ = + $2;
357 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700358| '(' expression ')' {
359 $$ = $2;
360 }
361;
362
Carl Worth3ff81672010-05-25 13:09:03 -0700363identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700364 IDENTIFIER {
365 $$ = _string_list_create (parser);
366 _string_list_append_item ($$, $1);
367 talloc_steal ($$, $1);
368 }
369| identifier_list ',' IDENTIFIER {
370 $$ = $1;
371 _string_list_append_item ($$, $3);
372 talloc_steal ($$, $3);
373 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700374;
375
Carl Worth3ff81672010-05-25 13:09:03 -0700376text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700377 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700378| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700379;
380
Carl Worth3ff81672010-05-25 13:09:03 -0700381non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700382 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700383 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700384 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700385;
386
Carl Worthaaa9acb2010-05-19 13:28:24 -0700387replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700388 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700389| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700390;
391
Kenneth Graunke65875742010-06-18 20:08:15 -0700392junk:
393 /* empty */
394| pp_tokens {
395 glcpp_warning(&@1, parser, "extra tokens at end of directive");
396 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700397;
398
399conditional_token:
400 /* Handle "defined" operator */
401 DEFINED IDENTIFIER {
402 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
403 $$ = _token_create_ival (parser, INTEGER, v);
404 }
405| DEFINED '(' IDENTIFIER ')' {
406 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
407 $$ = _token_create_ival (parser, INTEGER, v);
408 }
409| preprocessing_token
410;
411
412conditional_tokens:
413 /* Exactly the same as pp_tokens, but using conditional_token */
414 conditional_token {
415 parser->space_tokens = 1;
416 $$ = _token_list_create (parser);
417 _token_list_append ($$, $1);
418 talloc_unlink (parser, $1);
419 }
420| conditional_tokens conditional_token {
421 $$ = $1;
422 _token_list_append ($$, $2);
423 talloc_unlink (parser, $2);
424 }
425;
Kenneth Graunke65875742010-06-18 20:08:15 -0700426
Carl Worthaaa9acb2010-05-19 13:28:24 -0700427pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700428 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700429 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700430 $$ = _token_list_create (parser);
431 _token_list_append ($$, $1);
432 talloc_unlink (parser, $1);
433 }
434| pp_tokens preprocessing_token {
435 $$ = $1;
436 _token_list_append ($$, $2);
437 talloc_unlink (parser, $2);
438 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700439;
440
Carl Worth3ff81672010-05-25 13:09:03 -0700441preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700442 IDENTIFIER {
443 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700444 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700445 }
Carl Worth050e3de2010-05-27 14:36:29 -0700446| INTEGER_STRING {
447 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700448 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700449 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700450| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700451 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700452 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700453 }
454| OTHER {
455 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700456 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700457 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700458| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700459 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700460 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700461 }
Carl Worth3ff81672010-05-25 13:09:03 -0700462;
463
Carl Worth8e82fcb2010-05-26 11:15:21 -0700464operator:
Carl Worth808401f2010-05-25 14:52:43 -0700465 '[' { $$ = '['; }
466| ']' { $$ = ']'; }
467| '(' { $$ = '('; }
468| ')' { $$ = ')'; }
469| '{' { $$ = '{'; }
470| '}' { $$ = '}'; }
471| '.' { $$ = '.'; }
472| '&' { $$ = '&'; }
473| '*' { $$ = '*'; }
474| '+' { $$ = '+'; }
475| '-' { $$ = '-'; }
476| '~' { $$ = '~'; }
477| '!' { $$ = '!'; }
478| '/' { $$ = '/'; }
479| '%' { $$ = '%'; }
480| LEFT_SHIFT { $$ = LEFT_SHIFT; }
481| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
482| '<' { $$ = '<'; }
483| '>' { $$ = '>'; }
484| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
485| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
486| EQUAL { $$ = EQUAL; }
487| NOT_EQUAL { $$ = NOT_EQUAL; }
488| '^' { $$ = '^'; }
489| '|' { $$ = '|'; }
490| AND { $$ = AND; }
491| OR { $$ = OR; }
492| ';' { $$ = ';'; }
493| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700494| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700495| PASTE { $$ = PASTE; }
Carl Worth3ff81672010-05-25 13:09:03 -0700496;
497
Carl Worth33cc4002010-05-12 12:17:10 -0700498%%
499
Carl Worth610053b2010-05-14 10:05:11 -0700500string_list_t *
501_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700502{
Carl Worth610053b2010-05-14 10:05:11 -0700503 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700504
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700505 list = talloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700506 list->head = NULL;
507 list->tail = NULL;
508
509 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700510}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700511
Carl Worth33cc4002010-05-12 12:17:10 -0700512void
Carl Worth610053b2010-05-14 10:05:11 -0700513_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700514{
Carl Worth610053b2010-05-14 10:05:11 -0700515 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700516
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700517 node = talloc (list, string_node_t);
518 node->str = talloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700519
Carl Worth33cc4002010-05-12 12:17:10 -0700520 node->next = NULL;
521
522 if (list->head == NULL) {
523 list->head = node;
524 } else {
525 list->tail->next = node;
526 }
527
528 list->tail = node;
529}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700530
531int
Carl Worth610053b2010-05-14 10:05:11 -0700532_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700533{
Carl Worth610053b2010-05-14 10:05:11 -0700534 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700535 int i;
536
537 if (list == NULL)
538 return 0;
539
540 for (i = 0, node = list->head; node; i++, node = node->next) {
541 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700542 if (index)
543 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700544 return 1;
545 }
546 }
547
548 return 0;
549}
550
551int
Carl Worth610053b2010-05-14 10:05:11 -0700552_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700553{
554 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700555 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700556
557 if (list == NULL)
558 return 0;
559
560 for (node = list->head; node; node = node->next)
561 length++;
562
563 return length;
564}
565
Carl Worth8f6a8282010-05-14 10:44:19 -0700566argument_list_t *
567_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700568{
Carl Worth8f6a8282010-05-14 10:44:19 -0700569 argument_list_t *list;
570
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700571 list = talloc (ctx, argument_list_t);
Carl Worth8f6a8282010-05-14 10:44:19 -0700572 list->head = NULL;
573 list->tail = NULL;
574
575 return list;
576}
577
578void
Carl Worth47252442010-05-19 13:54:37 -0700579_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700580{
581 argument_node_t *node;
582
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700583 node = talloc (list, argument_node_t);
Carl Worth8f6a8282010-05-14 10:44:19 -0700584 node->argument = argument;
585
586 node->next = NULL;
587
588 if (list->head == NULL) {
589 list->head = node;
590 } else {
591 list->tail->next = node;
592 }
593
594 list->tail = node;
595}
596
597int
598_argument_list_length (argument_list_t *list)
599{
600 int length = 0;
601 argument_node_t *node;
602
603 if (list == NULL)
604 return 0;
605
606 for (node = list->head; node; node = node->next)
607 length++;
608
609 return length;
610}
611
Carl Worth47252442010-05-19 13:54:37 -0700612token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700613_argument_list_member_at (argument_list_t *list, int index)
614{
615 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700616 int i;
617
618 if (list == NULL)
619 return NULL;
620
621 node = list->head;
622 for (i = 0; i < index; i++) {
623 node = node->next;
624 if (node == NULL)
625 break;
626 }
627
628 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700629 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700630
631 return NULL;
632}
Carl Worth47252442010-05-19 13:54:37 -0700633
Carl Worth808401f2010-05-25 14:52:43 -0700634/* Note: This function talloc_steal()s the str pointer. */
635token_t *
636_token_create_str (void *ctx, int type, char *str)
637{
638 token_t *token;
639
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700640 token = talloc (ctx, token_t);
Carl Worth808401f2010-05-25 14:52:43 -0700641 token->type = type;
642 token->value.str = talloc_steal (token, str);
643
644 return token;
645}
646
647token_t *
648_token_create_ival (void *ctx, int type, int ival)
649{
650 token_t *token;
651
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700652 token = talloc (ctx, token_t);
Carl Worth808401f2010-05-25 14:52:43 -0700653 token->type = type;
654 token->value.ival = ival;
655
656 return token;
657}
658
Carl Worth47252442010-05-19 13:54:37 -0700659token_list_t *
660_token_list_create (void *ctx)
661{
662 token_list_t *list;
663
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700664 list = talloc (ctx, token_list_t);
Carl Worth47252442010-05-19 13:54:37 -0700665 list->head = NULL;
666 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700667 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700668
669 return list;
670}
671
672void
Carl Worth808401f2010-05-25 14:52:43 -0700673_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700674{
675 token_node_t *node;
676
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700677 node = talloc (list, token_node_t);
678 node->token = talloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700679
680 node->next = NULL;
681
682 if (list->head == NULL) {
683 list->head = node;
684 } else {
685 list->tail->next = node;
686 }
687
688 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700689 if (token->type != SPACE)
690 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700691}
692
693void
694_token_list_append_list (token_list_t *list, token_list_t *tail)
695{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700696 if (tail == NULL || tail->head == NULL)
697 return;
698
Carl Worth47252442010-05-19 13:54:37 -0700699 if (list->head == NULL) {
700 list->head = tail->head;
701 } else {
702 list->tail->next = tail->head;
703 }
704
705 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700706 list->non_space_tail = tail->non_space_tail;
707}
708
Carl Worthd80dcaf2010-07-20 15:55:21 -0700709static token_list_t *
Carl Worth681afbc2010-05-28 15:06:02 -0700710_token_list_copy (void *ctx, token_list_t *other)
711{
712 token_list_t *copy;
713 token_node_t *node;
714
715 if (other == NULL)
716 return NULL;
717
718 copy = _token_list_create (ctx);
719 for (node = other->head; node; node = node->next)
720 _token_list_append (copy, node->token);
721
722 return copy;
723}
724
Carl Worthd80dcaf2010-07-20 15:55:21 -0700725static void
Carl Worth10ae4382010-05-25 20:35:01 -0700726_token_list_trim_trailing_space (token_list_t *list)
727{
728 token_node_t *tail, *next;
729
730 if (list->non_space_tail) {
731 tail = list->non_space_tail->next;
732 list->non_space_tail->next = NULL;
733 list->tail = list->non_space_tail;
734
735 while (tail) {
736 next = tail->next;
737 talloc_free (tail);
738 tail = next;
739 }
740 }
Carl Worth47252442010-05-19 13:54:37 -0700741}
Carl Worth80dc60b2010-05-25 14:42:00 -0700742
Carl Worth0197e9b2010-05-26 08:05:19 -0700743static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700744_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700745{
746 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700747 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700748 return;
749 }
750
751 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700752 case INTEGER:
Eric Anholt8605c292010-07-28 16:53:51 -0700753 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700754 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700755 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700756 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700757 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700758 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700759 break;
760 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700761 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700762 break;
763 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700764 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700765 break;
766 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700767 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700768 break;
769 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700770 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700771 break;
772 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700773 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700774 break;
775 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700776 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700777 break;
778 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700779 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700780 break;
781 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700782 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700783 break;
784 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700785 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700786 break;
787 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700788 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700789 break;
Carl Worthdd749002010-05-27 10:12:33 -0700790 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700791 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700792 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700793 case PLACEHOLDER:
794 /* Nothing to print. */
795 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700796 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700797 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700798 break;
799 }
800}
801
Carl Worthb06096e2010-05-29 05:54:19 -0700802/* Return a new token (talloc()ed off of 'token') formed by pasting
803 * 'token' and 'other'. Note that this function may return 'token' or
804 * 'other' directly rather than allocating anything new.
805 *
806 * Caution: Only very cursory error-checking is performed to see if
807 * the final result is a valid single token. */
808static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700809_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700810{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700811 token_t *combined = NULL;
812
Carl Worth85b50e82010-05-27 14:01:18 -0700813 /* Pasting a placeholder onto anything makes no change. */
814 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700815 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700816
Carl Worthb06096e2010-05-29 05:54:19 -0700817 /* When 'token' is a placeholder, just return 'other'. */
818 if (token->type == PLACEHOLDER)
819 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700820
Carl Worthad0dee62010-05-26 09:04:50 -0700821 /* A very few single-character punctuators can be combined
822 * with another to form a multi-character punctuator. */
823 switch (token->type) {
824 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700825 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700826 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700827 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700828 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700829 break;
830 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700831 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700832 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700833 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700834 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700835 break;
836 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700837 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700838 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700839 break;
840 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700841 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700842 combined = _token_create_ival (token, NOT_EQUAL, NOT_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, AND, AND);
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, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700851 break;
852 }
853
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700854 if (combined != NULL) {
855 /* Inherit the location from the first token */
856 combined->location = token->location;
857 return combined;
858 }
859
Carl Worthad0dee62010-05-26 09:04:50 -0700860 /* Two string-valued tokens can usually just be mashed
861 * together.
862 *
Carl Worth050e3de2010-05-27 14:36:29 -0700863 * XXX: This isn't actually legitimate. Several things here
864 * should result in a diagnostic since the result cannot be a
865 * valid, single pre-processing token. For example, pasting
866 * "123" and "abc" is not legal, but we don't catch that
867 * here. */
868 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
869 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700870 {
Carl Worthb06096e2010-05-29 05:54:19 -0700871 char *str;
872
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700873 str = talloc_asprintf (token, "%s%s", token->value.str,
874 other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700875 combined = _token_create_str (token, token->type, str);
876 combined->location = token->location;
877 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700878 }
879
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700880 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700881 glcpp_print (parser->info_log, "Pasting \"");
882 _token_print (&parser->info_log, token);
883 glcpp_print (parser->info_log, "\" and \"");
884 _token_print (&parser->info_log, other);
885 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700886
887 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700888}
889
Carl Worth0197e9b2010-05-26 08:05:19 -0700890static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700891_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700892{
893 token_node_t *node;
894
895 if (list == NULL)
896 return;
897
898 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700899 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700900}
901
Carl Worth3a37b872010-05-10 11:44:09 -0700902void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700903yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700904{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700905 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700906}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700907
Eric Anholtd4a04f32010-07-28 16:58:39 -0700908static void add_builtin_define(glcpp_parser_t *parser,
909 const char *name, int value)
910{
911 token_t *tok;
912 token_list_t *list;
913
914 tok = _token_create_ival (parser, INTEGER, value);
915
916 list = _token_list_create(parser);
917 _token_list_append(list, tok);
918 _define_object_macro(parser, NULL, name, list);
919
920 talloc_unlink(parser, tok);
921}
922
Carl Worth33cc4002010-05-12 12:17:10 -0700923glcpp_parser_t *
Ian Romanick06143ea2010-06-30 16:27:22 -0700924glcpp_parser_create (const struct gl_extensions *extensions)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700925{
Carl Worth33cc4002010-05-12 12:17:10 -0700926 glcpp_parser_t *parser;
Eric Anholtd4a04f32010-07-28 16:58:39 -0700927 int language_version;
Carl Worth33cc4002010-05-12 12:17:10 -0700928
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700929 parser = talloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700930
Carl Worth8f38aff2010-05-19 10:01:29 -0700931 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700932 parser->defines = hash_table_ctor (32, hash_table_string_hash,
933 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700934 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700935 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700936 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700937 parser->newline_as_space = 0;
938 parser->in_control_line = 0;
939 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700940
Carl Worthb20d33c2010-05-20 22:27:07 -0700941 parser->skip_stack = NULL;
942
Carl Worth8e82fcb2010-05-26 11:15:21 -0700943 parser->lex_from_list = NULL;
944 parser->lex_from_node = NULL;
945
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700946 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700947 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke1b85c462010-06-21 13:55:12 -0700948 parser->error = 0;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700949
Ian Romanick2d122362010-06-30 16:03:19 -0700950 /* Add pre-defined macros. */
Eric Anholtd4a04f32010-07-28 16:58:39 -0700951 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
952 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
Ian Romanick2d122362010-06-30 16:03:19 -0700953
Eric Anholtd4a04f32010-07-28 16:58:39 -0700954 if (extensions != NULL) {
955 if (extensions->EXT_texture_array) {
956 add_builtin_define(parser, "GL_EXT_texture_array", 1);
957 }
Ian Romanick2d122362010-06-30 16:03:19 -0700958
Eric Anholtd4a04f32010-07-28 16:58:39 -0700959 if (extensions->ARB_fragment_coord_conventions)
960 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
961 1);
Ian Romanick06143ea2010-06-30 16:27:22 -0700962 }
963
Eric Anholtd4a04f32010-07-28 16:58:39 -0700964 language_version = 110;
Eric Anholtd4a04f32010-07-28 16:58:39 -0700965 add_builtin_define(parser, "__VERSION__", language_version);
Ian Romanick2d122362010-06-30 16:03:19 -0700966
Carl Worth33cc4002010-05-12 12:17:10 -0700967 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700968}
969
970int
971glcpp_parser_parse (glcpp_parser_t *parser)
972{
973 return yyparse (parser);
974}
975
976void
Carl Worth33cc4002010-05-12 12:17:10 -0700977glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700978{
Carl Worthb20d33c2010-05-20 22:27:07 -0700979 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700980 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700981 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700982 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700983 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700984}
Carl Worthc6d5af32010-05-11 12:30:09 -0700985
Carl Worthb1854fd2010-05-25 16:28:26 -0700986typedef enum function_status
987{
988 FUNCTION_STATUS_SUCCESS,
989 FUNCTION_NOT_A_FUNCTION,
990 FUNCTION_UNBALANCED_PARENTHESES
991} function_status_t;
992
993/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700994 * balanced set of parentheses.
995 *
996 * When called, 'node' should be the opening-parenthesis token, (or
997 * perhaps preceeding SPACE tokens). Upon successful return *last will
998 * be the last consumed node, (corresponding to the closing right
999 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -07001000 *
1001 * Return values:
1002 *
1003 * FUNCTION_STATUS_SUCCESS:
1004 *
1005 * Successfully parsed a set of function arguments.
1006 *
1007 * FUNCTION_NOT_A_FUNCTION:
1008 *
1009 * Macro name not followed by a '('. This is not an error, but
1010 * simply that the macro name should be treated as a non-macro.
1011 *
Carl Worth14c98a52010-06-02 15:49:54 -07001012 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -07001013 *
1014 * Macro name is not followed by a balanced set of parentheses.
1015 */
1016static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -07001017_arguments_parse (argument_list_t *arguments,
1018 token_node_t *node,
1019 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001020{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001021 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001022 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001023
Carl Worthb1854fd2010-05-25 16:28:26 -07001024 node = node->next;
1025
1026 /* Ignore whitespace before first parenthesis. */
1027 while (node && node->token->type == SPACE)
1028 node = node->next;
1029
1030 if (node == NULL || node->token->type != '(')
1031 return FUNCTION_NOT_A_FUNCTION;
1032
Carl Worth652fa272010-05-25 17:45:22 -07001033 node = node->next;
1034
Carl Wortha19297b2010-05-27 13:29:19 -07001035 argument = _token_list_create (arguments);
1036 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001037
Carl Worth681afbc2010-05-28 15:06:02 -07001038 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001039 if (node->token->type == '(')
1040 {
1041 paren_count++;
1042 }
1043 else if (node->token->type == ')')
1044 {
1045 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001046 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001047 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001048 }
Carl Worth652fa272010-05-25 17:45:22 -07001049
1050 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001051 paren_count == 1)
1052 {
Carl Wortha19297b2010-05-27 13:29:19 -07001053 _token_list_trim_trailing_space (argument);
1054 argument = _token_list_create (arguments);
1055 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001056 }
1057 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001058 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001059 /* Don't treat initial whitespace as
1060 * part of the arguement. */
1061 if (node->token->type == SPACE)
1062 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001063 }
1064 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001065 }
Carl Worthc7581c22010-05-25 17:41:07 -07001066 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001067
Carl Worth681afbc2010-05-28 15:06:02 -07001068 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001069 return FUNCTION_UNBALANCED_PARENTHESES;
1070
Carl Worth681afbc2010-05-28 15:06:02 -07001071 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001072
1073 return FUNCTION_STATUS_SUCCESS;
1074}
1075
Carl Worthe1acbfc2010-07-20 16:44:03 -07001076static token_list_t *
1077_token_list_create_with_one_space (void *ctx)
1078{
1079 token_list_t *list;
1080 token_t *space;
1081
1082 list = _token_list_create (ctx);
1083 space = _token_create_ival (list, SPACE, SPACE);
1084 _token_list_append (list, space);
1085
1086 return list;
1087}
1088
Kenneth Graunke16b4eed2010-08-04 16:10:03 -07001089static void
1090_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1091{
1092 token_list_t *expanded;
1093 token_t *token;
1094
1095 expanded = _token_list_create (parser);
1096 token = _token_create_ival (parser, type, type);
1097 _token_list_append (expanded, token);
1098 _glcpp_parser_expand_token_list (parser, list);
1099 _token_list_append_list (expanded, list);
1100 glcpp_parser_lex_from (parser, expanded);
1101}
1102
Carl Worth681afbc2010-05-28 15:06:02 -07001103/* This is a helper function that's essentially part of the
1104 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1105 * except for by that function.
1106 *
1107 * Returns NULL if node is a simple token with no expansion, (that is,
1108 * although 'node' corresponds to an identifier defined as a
1109 * function-like macro, it is not followed with a parenthesized
1110 * argument list).
1111 *
1112 * Compute the complete expansion of node (which is a function-like
1113 * macro) and subsequent nodes which are arguments.
1114 *
1115 * Returns the token list that results from the expansion and sets
1116 * *last to the last node in the list that was consumed by the
Carl Worthfbe42402010-07-22 16:36:04 -07001117 * expansion. Specifically, *last will be set as follows: as the
Carl Worth681afbc2010-05-28 15:06:02 -07001118 * token of the closing right parenthesis.
1119 */
1120static token_list_t *
1121_glcpp_parser_expand_function (glcpp_parser_t *parser,
1122 token_node_t *node,
1123 token_node_t **last)
1124
Carl Worthb1854fd2010-05-25 16:28:26 -07001125{
1126 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001127 const char *identifier;
1128 argument_list_t *arguments;
1129 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001130 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001131 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001132
Carl Worthb1854fd2010-05-25 16:28:26 -07001133 identifier = node->token->value.str;
1134
1135 macro = hash_table_find (parser->defines, identifier);
1136
1137 assert (macro->is_function);
1138
Carl Worth9ce18cf2010-05-25 17:32:21 -07001139 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001140 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001141
1142 switch (status) {
1143 case FUNCTION_STATUS_SUCCESS:
1144 break;
1145 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001146 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001147 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001148 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001149 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001150 }
1151
Carl Worthe1acbfc2010-07-20 16:44:03 -07001152 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth9ce18cf2010-05-25 17:32:21 -07001153 if (macro->replacements == NULL) {
1154 talloc_free (arguments);
Carl Worthe1acbfc2010-07-20 16:44:03 -07001155 return _token_list_create_with_one_space (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001156 }
1157
Carl Wortha19297b2010-05-27 13:29:19 -07001158 if (! ((_argument_list_length (arguments) ==
1159 _string_list_length (macro->parameters)) ||
1160 (_string_list_length (macro->parameters) == 0 &&
1161 _argument_list_length (arguments) == 1 &&
1162 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001163 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001164 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001165 "Error: macro %s invoked with %d arguments (expected %d)\n",
1166 identifier,
1167 _argument_list_length (arguments),
1168 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001169 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001170 }
1171
Carl Worth0197e9b2010-05-26 08:05:19 -07001172 /* Perform argument substitution on the replacement list. */
1173 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001174
Carl Worthce540f22010-05-26 08:25:44 -07001175 for (node = macro->replacements->head; node; node = node->next)
1176 {
1177 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001178 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001179 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001180 &parameter_index))
1181 {
1182 token_list_t *argument;
1183 argument = _argument_list_member_at (arguments,
1184 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001185 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001186 * tokens, or append a placeholder token for
1187 * an empty argument. */
1188 if (argument->head) {
Carl Worthfbe42402010-07-22 16:36:04 -07001189 token_list_t *expanded_argument;
1190 expanded_argument = _token_list_copy (parser,
1191 argument);
Carl Worth681afbc2010-05-28 15:06:02 -07001192 _glcpp_parser_expand_token_list (parser,
Carl Worthfbe42402010-07-22 16:36:04 -07001193 expanded_argument);
1194 _token_list_append_list (substituted,
1195 expanded_argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001196 } else {
1197 token_t *new_token;
1198
1199 new_token = _token_create_ival (substituted,
1200 PLACEHOLDER,
1201 PLACEHOLDER);
1202 _token_list_append (substituted, new_token);
1203 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001204 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001205 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001206 }
1207 }
1208
Carl Worthad0dee62010-05-26 09:04:50 -07001209 /* After argument substitution, and before further expansion
1210 * below, implement token pasting. */
1211
Carl Worthb06096e2010-05-29 05:54:19 -07001212 _token_list_trim_trailing_space (substituted);
1213
Carl Worthad0dee62010-05-26 09:04:50 -07001214 node = substituted->head;
1215 while (node)
1216 {
1217 token_node_t *next_non_space;
1218
1219 /* Look ahead for a PASTE token, skipping space. */
1220 next_non_space = node->next;
1221 while (next_non_space && next_non_space->token->type == SPACE)
1222 next_non_space = next_non_space->next;
1223
1224 if (next_non_space == NULL)
1225 break;
1226
1227 if (next_non_space->token->type != PASTE) {
1228 node = next_non_space;
1229 continue;
1230 }
1231
1232 /* Now find the next non-space token after the PASTE. */
1233 next_non_space = next_non_space->next;
1234 while (next_non_space && next_non_space->token->type == SPACE)
1235 next_non_space = next_non_space->next;
1236
1237 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001238 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001239 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001240 }
1241
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001242 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001243 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001244 if (next_non_space == substituted->tail)
1245 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001246
1247 node = node->next;
1248 }
1249
Carl Worthb06096e2010-05-29 05:54:19 -07001250 substituted->non_space_tail = substituted->tail;
1251
Carl Worth681afbc2010-05-28 15:06:02 -07001252 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001253}
1254
Carl Worth681afbc2010-05-28 15:06:02 -07001255/* Compute the complete expansion of node, (and subsequent nodes after
1256 * 'node' in the case that 'node' is a function-like macro and
1257 * subsequent nodes are arguments).
1258 *
1259 * Returns NULL if node is a simple token with no expansion.
1260 *
1261 * Otherwise, returns the token list that results from the expansion
1262 * and sets *last to the last node in the list that was consumed by
Carl Worthe1acbfc2010-07-20 16:44:03 -07001263 * the expansion. Specifically, *last will be set as follows:
Carl Worth681afbc2010-05-28 15:06:02 -07001264 *
1265 * As 'node' in the case of object-like macro expansion.
1266 *
1267 * As the token of the closing right parenthesis in the case of
1268 * function-like macro expansion.
1269 */
1270static token_list_t *
1271_glcpp_parser_expand_node (glcpp_parser_t *parser,
1272 token_node_t *node,
1273 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001274{
Carl Worth681afbc2010-05-28 15:06:02 -07001275 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001276 const char *identifier;
1277 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001278
1279 /* We only expand identifiers */
1280 if (token->type != IDENTIFIER) {
1281 /* We change any COMMA into a COMMA_FINAL to prevent
1282 * it being mistaken for an argument separator
1283 * later. */
1284 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001285 token->type = COMMA_FINAL;
1286 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001287 }
Carl Worth681afbc2010-05-28 15:06:02 -07001288
1289 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001290 }
1291
1292 /* Look up this identifier in the hash table. */
1293 identifier = token->value.str;
1294 macro = hash_table_find (parser->defines, identifier);
1295
Carl Worth681afbc2010-05-28 15:06:02 -07001296 /* Not a macro, so no expansion needed. */
1297 if (macro == NULL)
1298 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001299
1300 /* Finally, don't expand this macro if we're already actively
1301 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001302 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001303 /* We change the token type here from IDENTIFIER to
1304 * OTHER to prevent any future expansion of this
1305 * unexpanded token. */
1306 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001307 token_list_t *expansion;
1308 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001309
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001310 str = talloc_strdup (parser, token->value.str);
Carl Worth681afbc2010-05-28 15:06:02 -07001311 final = _token_create_str (parser, OTHER, str);
1312 expansion = _token_list_create (parser);
1313 _token_list_append (expansion, final);
1314 *last = node;
1315 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001316 }
1317
Carl Worth681afbc2010-05-28 15:06:02 -07001318 if (! macro->is_function)
1319 {
1320 *last = node;
1321
Carl Worthe1acbfc2010-07-20 16:44:03 -07001322 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth681afbc2010-05-28 15:06:02 -07001323 if (macro->replacements == NULL)
Carl Worthe1acbfc2010-07-20 16:44:03 -07001324 return _token_list_create_with_one_space (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001325
Carl Worth22b3ace2010-06-02 15:32:03 -07001326 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001327 }
Carl Worth681afbc2010-05-28 15:06:02 -07001328
1329 return _glcpp_parser_expand_function (parser, node, last);
1330}
1331
Carl Worth22b3ace2010-06-02 15:32:03 -07001332/* Push a new identifier onto the active list, returning the new list.
1333 *
1334 * Here, 'marker' is the token node that appears in the list after the
1335 * expansion of 'identifier'. That is, when the list iterator begins
1336 * examinging 'marker', then it is time to pop this node from the
1337 * active stack.
1338 */
1339active_list_t *
1340_active_list_push (active_list_t *list,
1341 const char *identifier,
1342 token_node_t *marker)
1343{
1344 active_list_t *node;
1345
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001346 node = talloc (list, active_list_t);
1347 node->identifier = talloc_strdup (node, identifier);
Carl Worth22b3ace2010-06-02 15:32:03 -07001348 node->marker = marker;
1349 node->next = list;
1350
1351 return node;
1352}
1353
1354active_list_t *
1355_active_list_pop (active_list_t *list)
1356{
1357 active_list_t *node = list;
1358
1359 if (node == NULL)
1360 return NULL;
1361
1362 node = list->next;
1363 talloc_free (list);
1364
1365 return node;
1366}
1367
1368int
1369_active_list_contains (active_list_t *list, const char *identifier)
1370{
1371 active_list_t *node;
1372
1373 if (list == NULL)
1374 return 0;
1375
1376 for (node = list; node; node = node->next)
1377 if (strcmp (node->identifier, identifier) == 0)
1378 return 1;
1379
1380 return 0;
1381}
1382
Carl Worth681afbc2010-05-28 15:06:02 -07001383/* Walk over the token list replacing nodes with their expansion.
1384 * Whenever nodes are expanded the walking will walk over the new
1385 * nodes, continuing to expand as necessary. The results are placed in
1386 * 'list' itself;
1387 */
1388static void
1389_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1390 token_list_t *list)
1391{
1392 token_node_t *node_prev;
Carl Worthc42e6402010-06-22 15:51:34 -07001393 token_node_t *node, *last = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001394 token_list_t *expansion;
1395
1396 if (list == NULL)
1397 return;
1398
1399 _token_list_trim_trailing_space (list);
1400
1401 node_prev = NULL;
1402 node = list->head;
1403
1404 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001405
1406 while (parser->active && parser->active->marker == node)
1407 parser->active = _active_list_pop (parser->active);
1408
Carl Worth681afbc2010-05-28 15:06:02 -07001409 /* Find the expansion for node, which will replace all
1410 * nodes from node to last, inclusive. */
1411 expansion = _glcpp_parser_expand_node (parser, node, &last);
1412 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001413 token_node_t *n;
1414
1415 for (n = node; n != last->next; n = n->next)
1416 while (parser->active &&
1417 parser->active->marker == n)
1418 {
1419 parser->active = _active_list_pop (parser->active);
1420 }
1421
1422 parser->active = _active_list_push (parser->active,
1423 node->token->value.str,
1424 last->next);
1425
Carl Worth681afbc2010-05-28 15:06:02 -07001426 /* Splice expansion into list, supporting a
1427 * simple deletion if the expansion is
1428 * empty. */
1429 if (expansion->head) {
1430 if (node_prev)
1431 node_prev->next = expansion->head;
1432 else
1433 list->head = expansion->head;
1434 expansion->tail->next = last->next;
1435 if (last == list->tail)
1436 list->tail = expansion->tail;
1437 } else {
1438 if (node_prev)
1439 node_prev->next = last->next;
1440 else
1441 list->head = last->next;
1442 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001443 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001444 }
1445 } else {
1446 node_prev = node;
1447 }
1448 node = node_prev ? node_prev->next : list->head;
1449 }
1450
Carl Worth22b3ace2010-06-02 15:32:03 -07001451 while (parser->active)
1452 parser->active = _active_list_pop (parser->active);
1453
Carl Worth681afbc2010-05-28 15:06:02 -07001454 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001455}
1456
Carl Worthae6517f2010-05-25 15:24:59 -07001457void
1458_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1459 token_list_t *list)
1460{
Carl Worthae6517f2010-05-25 15:24:59 -07001461 if (list == NULL)
1462 return;
1463
Carl Worth681afbc2010-05-28 15:06:02 -07001464 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001465
Carl Worth681afbc2010-05-28 15:06:02 -07001466 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001467
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001468 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001469}
1470
Carl Worthd80dcaf2010-07-20 15:55:21 -07001471static void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001472_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1473 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001474{
1475 /* According to the GLSL specification, macro names starting with "__"
1476 * or "GL_" are reserved for future use. So, don't allow them.
1477 */
1478 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001479 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001480 }
1481 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001482 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001483 }
1484}
1485
1486void
Carl Worthfcbbb462010-05-13 09:36:23 -07001487_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001488 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001489 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001490 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001491{
1492 macro_t *macro;
1493
Ian Romanick2d122362010-06-30 16:03:19 -07001494 if (loc != NULL)
1495 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001496
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001497 macro = talloc (parser, macro_t);
Carl Worthfcbbb462010-05-13 09:36:23 -07001498
1499 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001500 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001501 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001502 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001503
1504 hash_table_insert (parser->defines, macro, identifier);
1505}
1506
1507void
1508_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001509 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001510 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001511 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001512 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001513{
1514 macro_t *macro;
1515
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001516 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001517
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001518 macro = talloc (parser, macro_t);
Carl Worthfcbbb462010-05-13 09:36:23 -07001519
1520 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001521 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001522 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001523 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001524
1525 hash_table_insert (parser->defines, macro, identifier);
1526}
1527
Carl Worth8f38aff2010-05-19 10:01:29 -07001528static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001529glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001530{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001531 token_node_t *node;
1532 int ret;
1533
Carl Worth95951ea2010-05-26 15:57:10 -07001534 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001535 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001536
1537 /* XXX: This ugly block of code exists for the sole
1538 * purpose of converting a NEWLINE token into a SPACE
1539 * token, but only in the case where we have seen a
1540 * function-like macro name, but have not yet seen its
1541 * closing parenthesis.
1542 *
1543 * There's perhaps a more compact way to do this with
1544 * mid-rule actions in the grammar.
1545 *
1546 * I'm definitely not pleased with the complexity of
1547 * this code here.
1548 */
1549 if (parser->newline_as_space)
1550 {
1551 if (ret == '(') {
1552 parser->paren_count++;
1553 } else if (ret == ')') {
1554 parser->paren_count--;
1555 if (parser->paren_count == 0)
1556 parser->newline_as_space = 0;
1557 } else if (ret == NEWLINE) {
1558 ret = SPACE;
1559 } else if (ret != SPACE) {
1560 if (parser->paren_count == 0)
1561 parser->newline_as_space = 0;
1562 }
1563 }
1564 else if (parser->in_control_line)
1565 {
1566 if (ret == NEWLINE)
1567 parser->in_control_line = 0;
1568 }
1569 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1570 ret == HASH_UNDEF || ret == HASH_IF ||
1571 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1572 ret == HASH_ELIF || ret == HASH_ELSE ||
1573 ret == HASH_ENDIF || ret == HASH)
1574 {
1575 parser->in_control_line = 1;
1576 }
1577 else if (ret == IDENTIFIER)
1578 {
1579 macro_t *macro;
1580 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001581 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001582 if (macro && macro->is_function) {
1583 parser->newline_as_space = 1;
1584 parser->paren_count = 0;
1585 }
1586 }
1587
1588 return ret;
1589 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001590
1591 node = parser->lex_from_node;
1592
1593 if (node == NULL) {
1594 talloc_free (parser->lex_from_list);
1595 parser->lex_from_list = NULL;
1596 return NEWLINE;
1597 }
1598
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001599 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001600 ret = node->token->type;
1601
1602 parser->lex_from_node = node->next;
1603
1604 return ret;
1605}
1606
1607static void
1608glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1609{
1610 token_node_t *node;
1611
1612 assert (parser->lex_from_list == NULL);
1613
1614 /* Copy list, eliminating any space tokens. */
1615 parser->lex_from_list = _token_list_create (parser);
1616
1617 for (node = list->head; node; node = node->next) {
1618 if (node->token->type == SPACE)
1619 continue;
1620 _token_list_append (parser->lex_from_list, node->token);
1621 }
1622
1623 talloc_free (list);
1624
1625 parser->lex_from_node = parser->lex_from_list->head;
1626
1627 /* It's possible the list consisted of nothing but whitespace. */
1628 if (parser->lex_from_node == NULL) {
1629 talloc_free (parser->lex_from_list);
1630 parser->lex_from_list = NULL;
1631 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001632}
Carl Worthb20d33c2010-05-20 22:27:07 -07001633
1634static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001635_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1636 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001637{
1638 skip_type_t current = SKIP_NO_SKIP;
1639 skip_node_t *node;
1640
1641 if (parser->skip_stack)
1642 current = parser->skip_stack->type;
1643
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001644 node = talloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001645 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001646
1647 if (current == SKIP_NO_SKIP) {
1648 if (condition)
1649 node->type = SKIP_NO_SKIP;
1650 else
1651 node->type = SKIP_TO_ELSE;
1652 } else {
1653 node->type = SKIP_TO_ENDIF;
1654 }
1655
1656 node->next = parser->skip_stack;
1657 parser->skip_stack = node;
1658}
1659
1660static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001661_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1662 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001663{
1664 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001665 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001666 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001667 }
1668
1669 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1670 if (condition)
1671 parser->skip_stack->type = SKIP_NO_SKIP;
1672 } else {
1673 parser->skip_stack->type = SKIP_TO_ENDIF;
1674 }
1675}
Carl Worth80dc60b2010-05-25 14:42:00 -07001676
Carl Worthb20d33c2010-05-20 22:27:07 -07001677static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001678_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001679{
1680 skip_node_t *node;
1681
1682 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001683 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001684 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001685 }
1686
1687 node = parser->skip_stack;
1688 parser->skip_stack = node->next;
1689 talloc_free (node);
1690}