blob: c91da15519edb2779efe6f7f207c5154b78f132c [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>
Ian Romanick4ca4edd2010-08-12 10:07:29 -070027#include <string.h>
Carl Worthfcbbb462010-05-13 09:36:23 -070028#include <assert.h>
José Fonseca93493792010-08-14 16:01:24 +010029#include <inttypes.h>
Carl Worth3a37b872010-05-10 11:44:09 -070030
Carl Wortha1e32bc2010-05-10 13:17:25 -070031#include "glcpp.h"
Ian Romanick06143ea2010-06-30 16:27:22 -070032#include "main/mtypes.h"
Carl Wortha1e32bc2010-05-10 13:17:25 -070033
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -070034#define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
Vinson Lee07ca55b2010-08-13 17:11:21 -070035#define glcpp_printf(stream, fmt, args, ...) \
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -070036 stream = talloc_asprintf_append(stream, fmt, args)
37
Carl Worth5aa7ea02010-05-25 18:39:43 -070038static void
Kenneth Graunke465e03e2010-06-16 16:35:57 -070039yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070040
Carl Worth5aa7ea02010-05-25 18:39:43 -070041static void
Carl Worthfcbbb462010-05-13 09:36:23 -070042_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070043 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070044 const char *macro,
Carl Worth47252442010-05-19 13:54:37 -070045 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070046
Carl Worth5aa7ea02010-05-25 18:39:43 -070047static void
Carl Worthfcbbb462010-05-13 09:36:23 -070048_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070049 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070050 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070051 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070052 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070053
Carl Worth5aa7ea02010-05-25 18:39:43 -070054static string_list_t *
Carl Worth610053b2010-05-14 10:05:11 -070055_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070056
Carl Worth5aa7ea02010-05-25 18:39:43 -070057static void
Carl Worth610053b2010-05-14 10:05:11 -070058_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070059
Carl Worth5aa7ea02010-05-25 18:39:43 -070060static int
Carl Worth610053b2010-05-14 10:05:11 -070061_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070062
Carl Worth5aa7ea02010-05-25 18:39:43 -070063static int
Carl Worth610053b2010-05-14 10:05:11 -070064_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070065
Carl Worth5aa7ea02010-05-25 18:39:43 -070066static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070067_argument_list_create (void *ctx);
68
Carl Worth5aa7ea02010-05-25 18:39:43 -070069static void
Carl Worth47252442010-05-19 13:54:37 -070070_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070071
Carl Worth5aa7ea02010-05-25 18:39:43 -070072static int
Carl Worth8f6a8282010-05-14 10:44:19 -070073_argument_list_length (argument_list_t *list);
74
Carl Worth5aa7ea02010-05-25 18:39:43 -070075static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070076_argument_list_member_at (argument_list_t *list, int index);
77
Carl Worth808401f2010-05-25 14:52:43 -070078/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070079static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070080_token_create_str (void *ctx, int type, char *str);
81
Carl Worth5aa7ea02010-05-25 18:39:43 -070082static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070083_token_create_ival (void *ctx, int type, int ival);
84
Carl Worth5aa7ea02010-05-25 18:39:43 -070085static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070086_token_list_create (void *ctx);
87
Carl Worthb1ae61a2010-05-26 08:10:38 -070088/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070089 *
90 * You may want to talloc_unlink any current reference if you no
91 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070092static void
Carl Worth808401f2010-05-25 14:52:43 -070093_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070094
Carl Worth5aa7ea02010-05-25 18:39:43 -070095static void
Carl Worth47252442010-05-19 13:54:37 -070096_token_list_append_list (token_list_t *list, token_list_t *tail);
97
Carl Worth22b3ace2010-06-02 15:32:03 -070098static active_list_t *
99_active_list_push (active_list_t *list,
100 const char *identifier,
101 token_node_t *marker);
102
103static active_list_t *
104_active_list_pop (active_list_t *list);
105
106int
107_active_list_contains (active_list_t *list, const char *identifier);
108
Carl Worth5aa7ea02010-05-25 18:39:43 -0700109static void
Kenneth Graunke16b4eed2010-08-04 16:10:03 -0700110_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list);
111
112static void
Carl Worth681afbc2010-05-28 15:06:02 -0700113_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
114 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700115
Carl Worthaaa9acb2010-05-19 13:28:24 -0700116static void
Carl Worth681afbc2010-05-28 15:06:02 -0700117_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
118 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700119
120static void
Kenneth Graunke07745232010-06-17 12:41:46 -0700121_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
122 int condition);
Carl Worthb20d33c2010-05-20 22:27:07 -0700123
124static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700125_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
126 const char *type, int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700127
Carl Worthb20d33c2010-05-20 22:27:07 -0700128static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700129_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
Carl Worthb20d33c2010-05-20 22:27:07 -0700130
Carl Worth0293b2e2010-05-19 10:05:40 -0700131#define yylex glcpp_parser_lex
132
Carl Worth8f38aff2010-05-19 10:01:29 -0700133static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700134glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700135
Carl Worth8e82fcb2010-05-26 11:15:21 -0700136static void
137glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
138
Eric Anholtd4a04f32010-07-28 16:58:39 -0700139static void
140add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
141
Carl Worth3a37b872010-05-10 11:44:09 -0700142%}
143
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700144%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700145%error-verbose
Carl Worth485f84d2010-08-10 16:58:28 -0700146
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700147%locations
Carl Worth485f84d2010-08-10 16:58:28 -0700148%initial-action {
149 @$.first_line = 1;
150 @$.first_column = 1;
151 @$.last_line = 1;
152 @$.last_column = 1;
153 @$.source = 0;
154}
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700155
Carl Worth0b27b5f2010-05-10 16:16:06 -0700156%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700157%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700158
Carl Worthefef9502010-07-28 11:10:52 -0700159%expect 0
Eric Anholtd4a04f32010-07-28 16:58:39 -0700160%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 -0700161%token PASTE
Eric Anholtd4a04f32010-07-28 16:58:39 -0700162%type <ival> expression INTEGER operator SPACE integer_constant
Carl Worth050e3de2010-05-27 14:36:29 -0700163%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700164%type <string_list> identifier_list
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700165%type <token> preprocessing_token conditional_token
166%type <token_list> pp_tokens replacement_list text_line conditional_tokens
Carl Worth8fed1cd2010-05-26 09:32:12 -0700167%left OR
168%left AND
169%left '|'
170%left '^'
171%left '&'
172%left EQUAL NOT_EQUAL
173%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
174%left LEFT_SHIFT RIGHT_SHIFT
175%left '+' '-'
176%left '*' '/' '%'
177%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700178
179%%
180
Carl Worth33cc4002010-05-12 12:17:10 -0700181input:
Carl Worth3ff81672010-05-25 13:09:03 -0700182 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700183| input line
184;
185
186line:
187 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700188 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700189 }
Carl Worth808401f2010-05-25 14:52:43 -0700190| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700191 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700192 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700193 talloc_free ($1);
194 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700195| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700196| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700197;
198
Carl Worth8e82fcb2010-05-26 11:15:21 -0700199expanded_line:
200 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700201 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700202 }
203| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700204 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700205 }
206;
207
Carl Worth3ff81672010-05-25 13:09:03 -0700208control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700209 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700210 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700211 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700212| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700213 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700214 }
215| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700216 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700217 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700218| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700219 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700220 if (macro) {
Carl Worth61ebc012010-07-19 18:02:12 -0700221 hash_table_remove (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700222 talloc_free (macro);
223 }
224 talloc_free ($2);
225 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700226| HASH_IF conditional_tokens NEWLINE {
Carl Worth48ba0582010-08-11 12:43:44 -0700227 /* Be careful to only evaluate the 'if' expression if
228 * we are not skipping. When we are skipping, we
229 * simply push a new 0-valued 'if' onto the skip
230 * stack.
231 *
232 * This avoids generating diagnostics for invalid
233 * expressions that are being skipped. */
234 if (parser->skip_stack == NULL ||
235 parser->skip_stack->type == SKIP_NO_SKIP)
236 {
237 _glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
238 }
239 else
240 {
Kenneth Graunkef4239872010-08-04 16:24:39 -0700241 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
242 parser->skip_stack->type = SKIP_TO_ENDIF;
Kenneth Graunkef4239872010-08-04 16:24:39 -0700243 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700244 }
Carl Worth253cad32010-08-11 13:59:22 -0700245| HASH_IF NEWLINE {
246 /* #if without an expression is only an error if we
247 * are not skipping */
248 if (parser->skip_stack == NULL ||
249 parser->skip_stack->type == SKIP_NO_SKIP)
250 {
251 glcpp_error(& @1, parser, "#if with no expression");
252 }
253 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
254 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700255| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700256 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700257 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700258 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700259 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700260| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700261 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700262 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700263 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700264 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700265| HASH_ELIF conditional_tokens NEWLINE {
Carl Worth48ba0582010-08-11 12:43:44 -0700266 /* Be careful to only evaluate the 'elif' expression
267 * if we are not skipping. When we are skipping, we
268 * simply change to a 0-valued 'elif' on the skip
269 * stack.
270 *
271 * This avoids generating diagnostics for invalid
272 * expressions that are being skipped. */
273 if (parser->skip_stack &&
274 parser->skip_stack->type == SKIP_TO_ELSE)
275 {
Kenneth Graunkef4239872010-08-04 16:24:39 -0700276 _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
Carl Worth48ba0582010-08-11 12:43:44 -0700277 }
278 else
279 {
280 _glcpp_parser_skip_stack_change_if (parser, & @1,
281 "elif", 0);
282 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700283 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700284| HASH_ELIF NEWLINE {
Carl Worth48ba0582010-08-11 12:43:44 -0700285 /* #elif without an expression is an error unless we
286 * are skipping. */
287 if (parser->skip_stack &&
288 parser->skip_stack->type == SKIP_TO_ELSE)
289 {
Carl Worth624dd582010-08-11 13:50:51 -0700290 glcpp_error(& @1, parser, "#elif with no expression");
Kenneth Graunke332fc472010-06-21 12:20:22 -0700291 }
Carl Worth48ba0582010-08-11 12:43:44 -0700292 else
293 {
294 _glcpp_parser_skip_stack_change_if (parser, & @1,
295 "elif", 0);
296 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
297 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700298 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700299| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700300 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700301 }
302| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700303 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700304 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700305| HASH_VERSION integer_constant NEWLINE {
306 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
307 if (macro) {
308 hash_table_remove (parser->defines, "__VERSION__");
309 talloc_free (macro);
310 }
311 add_builtin_define (parser, "__VERSION__", $2);
Kenneth Graunke6be3a8b2010-08-16 13:42:04 -0700312 glcpp_printf(parser->output, "#version %" PRIiMAX, $2);
Eric Anholtd4a04f32010-07-28 16:58:39 -0700313 }
Carl Worth3ff81672010-05-25 13:09:03 -0700314| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700315;
316
Eric Anholtd4a04f32010-07-28 16:58:39 -0700317integer_constant:
Carl Worth050e3de2010-05-27 14:36:29 -0700318 INTEGER_STRING {
319 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
320 $$ = strtoll ($1 + 2, NULL, 16);
321 } else if ($1[0] == '0') {
322 $$ = strtoll ($1, NULL, 8);
323 } else {
324 $$ = strtoll ($1, NULL, 10);
325 }
326 }
327| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700328 $$ = $1;
329 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700330
331expression:
332 integer_constant
Carl Worth8fed1cd2010-05-26 09:32:12 -0700333| expression OR expression {
334 $$ = $1 || $3;
335 }
336| expression AND 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 NOT_EQUAL expression {
349 $$ = $1 != $3;
350 }
351| expression EQUAL expression {
352 $$ = $1 == $3;
353 }
354| expression GREATER_OR_EQUAL expression {
355 $$ = $1 >= $3;
356 }
357| expression LESS_OR_EQUAL expression {
358 $$ = $1 <= $3;
359 }
360| expression '>' expression {
361 $$ = $1 > $3;
362 }
363| expression '<' expression {
364 $$ = $1 < $3;
365 }
366| expression RIGHT_SHIFT expression {
367 $$ = $1 >> $3;
368 }
369| expression LEFT_SHIFT expression {
370 $$ = $1 << $3;
371 }
372| expression '-' expression {
373 $$ = $1 - $3;
374 }
375| expression '+' expression {
376 $$ = $1 + $3;
377 }
378| expression '%' expression {
379 $$ = $1 % $3;
380 }
381| expression '/' expression {
382 $$ = $1 / $3;
383 }
384| expression '*' expression {
385 $$ = $1 * $3;
386 }
387| '!' expression %prec UNARY {
388 $$ = ! $2;
389 }
390| '~' expression %prec UNARY {
391 $$ = ~ $2;
392 }
393| '-' expression %prec UNARY {
394 $$ = - $2;
395 }
396| '+' expression %prec UNARY {
397 $$ = + $2;
398 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700399| '(' expression ')' {
400 $$ = $2;
401 }
402;
403
Carl Worth3ff81672010-05-25 13:09:03 -0700404identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700405 IDENTIFIER {
406 $$ = _string_list_create (parser);
407 _string_list_append_item ($$, $1);
408 talloc_steal ($$, $1);
409 }
410| identifier_list ',' IDENTIFIER {
411 $$ = $1;
412 _string_list_append_item ($$, $3);
413 talloc_steal ($$, $3);
414 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700415;
416
Carl Worth3ff81672010-05-25 13:09:03 -0700417text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700418 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700419| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700420;
421
Carl Worth3ff81672010-05-25 13:09:03 -0700422non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700423 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700424 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700425 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700426;
427
Carl Worthaaa9acb2010-05-19 13:28:24 -0700428replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700429 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700430| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700431;
432
Kenneth Graunke65875742010-06-18 20:08:15 -0700433junk:
434 /* empty */
435| pp_tokens {
436 glcpp_warning(&@1, parser, "extra tokens at end of directive");
437 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700438;
439
440conditional_token:
441 /* Handle "defined" operator */
442 DEFINED IDENTIFIER {
443 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
444 $$ = _token_create_ival (parser, INTEGER, v);
445 }
446| DEFINED '(' IDENTIFIER ')' {
447 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
448 $$ = _token_create_ival (parser, INTEGER, v);
449 }
450| preprocessing_token
451;
452
453conditional_tokens:
454 /* Exactly the same as pp_tokens, but using conditional_token */
455 conditional_token {
456 parser->space_tokens = 1;
457 $$ = _token_list_create (parser);
458 _token_list_append ($$, $1);
459 talloc_unlink (parser, $1);
460 }
461| conditional_tokens conditional_token {
462 $$ = $1;
463 _token_list_append ($$, $2);
464 talloc_unlink (parser, $2);
465 }
466;
Kenneth Graunke65875742010-06-18 20:08:15 -0700467
Carl Worthaaa9acb2010-05-19 13:28:24 -0700468pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700469 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700470 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700471 $$ = _token_list_create (parser);
472 _token_list_append ($$, $1);
473 talloc_unlink (parser, $1);
474 }
475| pp_tokens preprocessing_token {
476 $$ = $1;
477 _token_list_append ($$, $2);
478 talloc_unlink (parser, $2);
479 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700480;
481
Carl Worth3ff81672010-05-25 13:09:03 -0700482preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700483 IDENTIFIER {
484 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700485 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700486 }
Carl Worth050e3de2010-05-27 14:36:29 -0700487| INTEGER_STRING {
488 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700489 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700490 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700491| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700492 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700493 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700494 }
495| OTHER {
496 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700497 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700498 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700499| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700500 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700501 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700502 }
Carl Worth3ff81672010-05-25 13:09:03 -0700503;
504
Carl Worth8e82fcb2010-05-26 11:15:21 -0700505operator:
Carl Worth808401f2010-05-25 14:52:43 -0700506 '[' { $$ = '['; }
507| ']' { $$ = ']'; }
508| '(' { $$ = '('; }
509| ')' { $$ = ')'; }
510| '{' { $$ = '{'; }
511| '}' { $$ = '}'; }
512| '.' { $$ = '.'; }
513| '&' { $$ = '&'; }
514| '*' { $$ = '*'; }
515| '+' { $$ = '+'; }
516| '-' { $$ = '-'; }
517| '~' { $$ = '~'; }
518| '!' { $$ = '!'; }
519| '/' { $$ = '/'; }
520| '%' { $$ = '%'; }
521| LEFT_SHIFT { $$ = LEFT_SHIFT; }
522| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
523| '<' { $$ = '<'; }
524| '>' { $$ = '>'; }
525| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
526| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
527| EQUAL { $$ = EQUAL; }
528| NOT_EQUAL { $$ = NOT_EQUAL; }
529| '^' { $$ = '^'; }
530| '|' { $$ = '|'; }
531| AND { $$ = AND; }
532| OR { $$ = OR; }
533| ';' { $$ = ';'; }
534| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700535| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700536| PASTE { $$ = PASTE; }
Carl Worth3ff81672010-05-25 13:09:03 -0700537;
538
Carl Worth33cc4002010-05-12 12:17:10 -0700539%%
540
Carl Worth610053b2010-05-14 10:05:11 -0700541string_list_t *
542_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700543{
Carl Worth610053b2010-05-14 10:05:11 -0700544 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700545
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700546 list = talloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700547 list->head = NULL;
548 list->tail = NULL;
549
550 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700551}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700552
Carl Worth33cc4002010-05-12 12:17:10 -0700553void
Carl Worth610053b2010-05-14 10:05:11 -0700554_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700555{
Carl Worth610053b2010-05-14 10:05:11 -0700556 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700557
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700558 node = talloc (list, string_node_t);
559 node->str = talloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700560
Carl Worth33cc4002010-05-12 12:17:10 -0700561 node->next = NULL;
562
563 if (list->head == NULL) {
564 list->head = node;
565 } else {
566 list->tail->next = node;
567 }
568
569 list->tail = node;
570}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700571
572int
Carl Worth610053b2010-05-14 10:05:11 -0700573_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700574{
Carl Worth610053b2010-05-14 10:05:11 -0700575 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700576 int i;
577
578 if (list == NULL)
579 return 0;
580
581 for (i = 0, node = list->head; node; i++, node = node->next) {
582 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700583 if (index)
584 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700585 return 1;
586 }
587 }
588
589 return 0;
590}
591
592int
Carl Worth610053b2010-05-14 10:05:11 -0700593_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700594{
595 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700596 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700597
598 if (list == NULL)
599 return 0;
600
601 for (node = list->head; node; node = node->next)
602 length++;
603
604 return length;
605}
606
Carl Worth8f6a8282010-05-14 10:44:19 -0700607argument_list_t *
608_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700609{
Carl Worth8f6a8282010-05-14 10:44:19 -0700610 argument_list_t *list;
611
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700612 list = talloc (ctx, argument_list_t);
Carl Worth8f6a8282010-05-14 10:44:19 -0700613 list->head = NULL;
614 list->tail = NULL;
615
616 return list;
617}
618
619void
Carl Worth47252442010-05-19 13:54:37 -0700620_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700621{
622 argument_node_t *node;
623
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700624 node = talloc (list, argument_node_t);
Carl Worth8f6a8282010-05-14 10:44:19 -0700625 node->argument = argument;
626
627 node->next = NULL;
628
629 if (list->head == NULL) {
630 list->head = node;
631 } else {
632 list->tail->next = node;
633 }
634
635 list->tail = node;
636}
637
638int
639_argument_list_length (argument_list_t *list)
640{
641 int length = 0;
642 argument_node_t *node;
643
644 if (list == NULL)
645 return 0;
646
647 for (node = list->head; node; node = node->next)
648 length++;
649
650 return length;
651}
652
Carl Worth47252442010-05-19 13:54:37 -0700653token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700654_argument_list_member_at (argument_list_t *list, int index)
655{
656 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700657 int i;
658
659 if (list == NULL)
660 return NULL;
661
662 node = list->head;
663 for (i = 0; i < index; i++) {
664 node = node->next;
665 if (node == NULL)
666 break;
667 }
668
669 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700670 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700671
672 return NULL;
673}
Carl Worth47252442010-05-19 13:54:37 -0700674
Carl Worth808401f2010-05-25 14:52:43 -0700675/* Note: This function talloc_steal()s the str pointer. */
676token_t *
677_token_create_str (void *ctx, int type, char *str)
678{
679 token_t *token;
680
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700681 token = talloc (ctx, token_t);
Carl Worth808401f2010-05-25 14:52:43 -0700682 token->type = type;
683 token->value.str = talloc_steal (token, str);
684
685 return token;
686}
687
688token_t *
689_token_create_ival (void *ctx, int type, int ival)
690{
691 token_t *token;
692
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700693 token = talloc (ctx, token_t);
Carl Worth808401f2010-05-25 14:52:43 -0700694 token->type = type;
695 token->value.ival = ival;
696
697 return token;
698}
699
Carl Worth47252442010-05-19 13:54:37 -0700700token_list_t *
701_token_list_create (void *ctx)
702{
703 token_list_t *list;
704
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700705 list = talloc (ctx, token_list_t);
Carl Worth47252442010-05-19 13:54:37 -0700706 list->head = NULL;
707 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700708 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700709
710 return list;
711}
712
713void
Carl Worth808401f2010-05-25 14:52:43 -0700714_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700715{
716 token_node_t *node;
717
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700718 node = talloc (list, token_node_t);
719 node->token = talloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700720
721 node->next = NULL;
722
723 if (list->head == NULL) {
724 list->head = node;
725 } else {
726 list->tail->next = node;
727 }
728
729 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700730 if (token->type != SPACE)
731 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700732}
733
734void
735_token_list_append_list (token_list_t *list, token_list_t *tail)
736{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700737 if (tail == NULL || tail->head == NULL)
738 return;
739
Carl Worth47252442010-05-19 13:54:37 -0700740 if (list->head == NULL) {
741 list->head = tail->head;
742 } else {
743 list->tail->next = tail->head;
744 }
745
746 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700747 list->non_space_tail = tail->non_space_tail;
748}
749
Carl Worthd80dcaf2010-07-20 15:55:21 -0700750static token_list_t *
Carl Worth681afbc2010-05-28 15:06:02 -0700751_token_list_copy (void *ctx, token_list_t *other)
752{
753 token_list_t *copy;
754 token_node_t *node;
755
756 if (other == NULL)
757 return NULL;
758
759 copy = _token_list_create (ctx);
760 for (node = other->head; node; node = node->next)
761 _token_list_append (copy, node->token);
762
763 return copy;
764}
765
Carl Worthd80dcaf2010-07-20 15:55:21 -0700766static void
Carl Worth10ae4382010-05-25 20:35:01 -0700767_token_list_trim_trailing_space (token_list_t *list)
768{
769 token_node_t *tail, *next;
770
771 if (list->non_space_tail) {
772 tail = list->non_space_tail->next;
773 list->non_space_tail->next = NULL;
774 list->tail = list->non_space_tail;
775
776 while (tail) {
777 next = tail->next;
778 talloc_free (tail);
779 tail = next;
780 }
781 }
Carl Worth47252442010-05-19 13:54:37 -0700782}
Carl Worth80dc60b2010-05-25 14:42:00 -0700783
Carl Worth0197e9b2010-05-26 08:05:19 -0700784static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700785_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700786{
787 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700788 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700789 return;
790 }
791
792 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700793 case INTEGER:
Eric Anholt8605c292010-07-28 16:53:51 -0700794 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700795 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700796 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700797 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700798 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700799 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700800 break;
801 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700802 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700803 break;
804 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700805 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700806 break;
807 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700808 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700809 break;
810 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700811 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700812 break;
813 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700814 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700815 break;
816 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700817 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700818 break;
819 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700820 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700821 break;
822 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700823 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700824 break;
825 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700826 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700827 break;
828 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700829 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700830 break;
Carl Worthdd749002010-05-27 10:12:33 -0700831 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700832 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700833 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700834 case PLACEHOLDER:
835 /* Nothing to print. */
836 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700837 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700838 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700839 break;
840 }
841}
842
Carl Worthb06096e2010-05-29 05:54:19 -0700843/* Return a new token (talloc()ed off of 'token') formed by pasting
844 * 'token' and 'other'. Note that this function may return 'token' or
845 * 'other' directly rather than allocating anything new.
846 *
847 * Caution: Only very cursory error-checking is performed to see if
848 * the final result is a valid single token. */
849static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700850_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700851{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700852 token_t *combined = NULL;
853
Carl Worth85b50e82010-05-27 14:01:18 -0700854 /* Pasting a placeholder onto anything makes no change. */
855 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700856 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700857
Carl Worthb06096e2010-05-29 05:54:19 -0700858 /* When 'token' is a placeholder, just return 'other'. */
859 if (token->type == PLACEHOLDER)
860 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700861
Carl Worthad0dee62010-05-26 09:04:50 -0700862 /* A very few single-character punctuators can be combined
863 * with another to form a multi-character punctuator. */
864 switch (token->type) {
865 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700866 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700867 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700868 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700869 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700870 break;
871 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700872 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700873 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700874 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700875 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700876 break;
877 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700878 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700879 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700880 break;
881 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700882 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700883 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700884 break;
885 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700886 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700887 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700888 break;
889 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700890 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700891 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700892 break;
893 }
894
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700895 if (combined != NULL) {
896 /* Inherit the location from the first token */
897 combined->location = token->location;
898 return combined;
899 }
900
Carl Worthad0dee62010-05-26 09:04:50 -0700901 /* Two string-valued tokens can usually just be mashed
902 * together.
903 *
Carl Worth050e3de2010-05-27 14:36:29 -0700904 * XXX: This isn't actually legitimate. Several things here
905 * should result in a diagnostic since the result cannot be a
906 * valid, single pre-processing token. For example, pasting
907 * "123" and "abc" is not legal, but we don't catch that
908 * here. */
909 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
910 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700911 {
Carl Worthb06096e2010-05-29 05:54:19 -0700912 char *str;
913
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700914 str = talloc_asprintf (token, "%s%s", token->value.str,
915 other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700916 combined = _token_create_str (token, token->type, str);
917 combined->location = token->location;
918 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700919 }
920
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700921 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700922 glcpp_print (parser->info_log, "Pasting \"");
923 _token_print (&parser->info_log, token);
924 glcpp_print (parser->info_log, "\" and \"");
925 _token_print (&parser->info_log, other);
926 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700927
928 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700929}
930
Carl Worth0197e9b2010-05-26 08:05:19 -0700931static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700932_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700933{
934 token_node_t *node;
935
936 if (list == NULL)
937 return;
938
939 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700940 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700941}
942
Carl Worth3a37b872010-05-10 11:44:09 -0700943void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700944yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700945{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700946 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700947}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700948
Eric Anholtd4a04f32010-07-28 16:58:39 -0700949static void add_builtin_define(glcpp_parser_t *parser,
950 const char *name, int value)
951{
952 token_t *tok;
953 token_list_t *list;
954
955 tok = _token_create_ival (parser, INTEGER, value);
956
957 list = _token_list_create(parser);
958 _token_list_append(list, tok);
959 _define_object_macro(parser, NULL, name, list);
960
961 talloc_unlink(parser, tok);
962}
963
Carl Worth33cc4002010-05-12 12:17:10 -0700964glcpp_parser_t *
Ian Romanick06143ea2010-06-30 16:27:22 -0700965glcpp_parser_create (const struct gl_extensions *extensions)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700966{
Carl Worth33cc4002010-05-12 12:17:10 -0700967 glcpp_parser_t *parser;
Eric Anholtd4a04f32010-07-28 16:58:39 -0700968 int language_version;
Carl Worth33cc4002010-05-12 12:17:10 -0700969
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700970 parser = talloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700971
Carl Worth8f38aff2010-05-19 10:01:29 -0700972 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700973 parser->defines = hash_table_ctor (32, hash_table_string_hash,
974 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700975 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700976 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700977 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700978 parser->newline_as_space = 0;
979 parser->in_control_line = 0;
980 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700981
Carl Worthb20d33c2010-05-20 22:27:07 -0700982 parser->skip_stack = NULL;
983
Carl Worth8e82fcb2010-05-26 11:15:21 -0700984 parser->lex_from_list = NULL;
985 parser->lex_from_node = NULL;
986
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700987 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700988 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke1b85c462010-06-21 13:55:12 -0700989 parser->error = 0;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700990
Ian Romanick2d122362010-06-30 16:03:19 -0700991 /* Add pre-defined macros. */
Eric Anholtd4a04f32010-07-28 16:58:39 -0700992 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
993 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
Ian Romanick2d122362010-06-30 16:03:19 -0700994
Eric Anholtd4a04f32010-07-28 16:58:39 -0700995 if (extensions != NULL) {
996 if (extensions->EXT_texture_array) {
997 add_builtin_define(parser, "GL_EXT_texture_array", 1);
998 }
Ian Romanick2d122362010-06-30 16:03:19 -0700999
Eric Anholtd4a04f32010-07-28 16:58:39 -07001000 if (extensions->ARB_fragment_coord_conventions)
1001 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1002 1);
Ian Romanick06143ea2010-06-30 16:27:22 -07001003 }
1004
Eric Anholtd4a04f32010-07-28 16:58:39 -07001005 language_version = 110;
Eric Anholtd4a04f32010-07-28 16:58:39 -07001006 add_builtin_define(parser, "__VERSION__", language_version);
Ian Romanick2d122362010-06-30 16:03:19 -07001007
Carl Worth33cc4002010-05-12 12:17:10 -07001008 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -07001009}
1010
1011int
1012glcpp_parser_parse (glcpp_parser_t *parser)
1013{
1014 return yyparse (parser);
1015}
1016
1017void
Carl Worth33cc4002010-05-12 12:17:10 -07001018glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -07001019{
Carl Worth8f38aff2010-05-19 10:01:29 -07001020 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -07001021 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -07001022 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -07001023}
Carl Worthc6d5af32010-05-11 12:30:09 -07001024
Carl Worthb1854fd2010-05-25 16:28:26 -07001025typedef enum function_status
1026{
1027 FUNCTION_STATUS_SUCCESS,
1028 FUNCTION_NOT_A_FUNCTION,
1029 FUNCTION_UNBALANCED_PARENTHESES
1030} function_status_t;
1031
1032/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -07001033 * balanced set of parentheses.
1034 *
1035 * When called, 'node' should be the opening-parenthesis token, (or
1036 * perhaps preceeding SPACE tokens). Upon successful return *last will
1037 * be the last consumed node, (corresponding to the closing right
1038 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -07001039 *
1040 * Return values:
1041 *
1042 * FUNCTION_STATUS_SUCCESS:
1043 *
1044 * Successfully parsed a set of function arguments.
1045 *
1046 * FUNCTION_NOT_A_FUNCTION:
1047 *
1048 * Macro name not followed by a '('. This is not an error, but
1049 * simply that the macro name should be treated as a non-macro.
1050 *
Carl Worth14c98a52010-06-02 15:49:54 -07001051 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -07001052 *
1053 * Macro name is not followed by a balanced set of parentheses.
1054 */
1055static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -07001056_arguments_parse (argument_list_t *arguments,
1057 token_node_t *node,
1058 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001059{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001060 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001061 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001062
Carl Worthb1854fd2010-05-25 16:28:26 -07001063 node = node->next;
1064
1065 /* Ignore whitespace before first parenthesis. */
1066 while (node && node->token->type == SPACE)
1067 node = node->next;
1068
1069 if (node == NULL || node->token->type != '(')
1070 return FUNCTION_NOT_A_FUNCTION;
1071
Carl Worth652fa272010-05-25 17:45:22 -07001072 node = node->next;
1073
Carl Wortha19297b2010-05-27 13:29:19 -07001074 argument = _token_list_create (arguments);
1075 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001076
Carl Worth681afbc2010-05-28 15:06:02 -07001077 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001078 if (node->token->type == '(')
1079 {
1080 paren_count++;
1081 }
1082 else if (node->token->type == ')')
1083 {
1084 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001085 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001086 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001087 }
Carl Worth652fa272010-05-25 17:45:22 -07001088
1089 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001090 paren_count == 1)
1091 {
Carl Wortha19297b2010-05-27 13:29:19 -07001092 _token_list_trim_trailing_space (argument);
1093 argument = _token_list_create (arguments);
1094 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001095 }
1096 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001097 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001098 /* Don't treat initial whitespace as
1099 * part of the arguement. */
1100 if (node->token->type == SPACE)
1101 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001102 }
1103 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001104 }
Carl Worthc7581c22010-05-25 17:41:07 -07001105 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001106
Carl Worth681afbc2010-05-28 15:06:02 -07001107 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001108 return FUNCTION_UNBALANCED_PARENTHESES;
1109
Carl Worth681afbc2010-05-28 15:06:02 -07001110 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001111
1112 return FUNCTION_STATUS_SUCCESS;
1113}
1114
Carl Worthe1acbfc2010-07-20 16:44:03 -07001115static token_list_t *
1116_token_list_create_with_one_space (void *ctx)
1117{
1118 token_list_t *list;
1119 token_t *space;
1120
1121 list = _token_list_create (ctx);
1122 space = _token_create_ival (list, SPACE, SPACE);
1123 _token_list_append (list, space);
1124
1125 return list;
1126}
1127
Kenneth Graunke16b4eed2010-08-04 16:10:03 -07001128static void
1129_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1130{
1131 token_list_t *expanded;
1132 token_t *token;
1133
1134 expanded = _token_list_create (parser);
1135 token = _token_create_ival (parser, type, type);
1136 _token_list_append (expanded, token);
1137 _glcpp_parser_expand_token_list (parser, list);
1138 _token_list_append_list (expanded, list);
1139 glcpp_parser_lex_from (parser, expanded);
1140}
1141
Carl Worth681afbc2010-05-28 15:06:02 -07001142/* This is a helper function that's essentially part of the
1143 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1144 * except for by that function.
1145 *
1146 * Returns NULL if node is a simple token with no expansion, (that is,
1147 * although 'node' corresponds to an identifier defined as a
1148 * function-like macro, it is not followed with a parenthesized
1149 * argument list).
1150 *
1151 * Compute the complete expansion of node (which is a function-like
1152 * macro) and subsequent nodes which are arguments.
1153 *
1154 * Returns the token list that results from the expansion and sets
1155 * *last to the last node in the list that was consumed by the
Carl Worthfbe42402010-07-22 16:36:04 -07001156 * expansion. Specifically, *last will be set as follows: as the
Carl Worth681afbc2010-05-28 15:06:02 -07001157 * token of the closing right parenthesis.
1158 */
1159static token_list_t *
1160_glcpp_parser_expand_function (glcpp_parser_t *parser,
1161 token_node_t *node,
1162 token_node_t **last)
1163
Carl Worthb1854fd2010-05-25 16:28:26 -07001164{
1165 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001166 const char *identifier;
1167 argument_list_t *arguments;
1168 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001169 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001170 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001171
Carl Worthb1854fd2010-05-25 16:28:26 -07001172 identifier = node->token->value.str;
1173
1174 macro = hash_table_find (parser->defines, identifier);
1175
1176 assert (macro->is_function);
1177
Carl Worth9ce18cf2010-05-25 17:32:21 -07001178 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001179 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001180
1181 switch (status) {
1182 case FUNCTION_STATUS_SUCCESS:
1183 break;
1184 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001185 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001186 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001187 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001188 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001189 }
1190
Carl Worthe1acbfc2010-07-20 16:44:03 -07001191 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth9ce18cf2010-05-25 17:32:21 -07001192 if (macro->replacements == NULL) {
1193 talloc_free (arguments);
Carl Worthe1acbfc2010-07-20 16:44:03 -07001194 return _token_list_create_with_one_space (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001195 }
1196
Carl Wortha19297b2010-05-27 13:29:19 -07001197 if (! ((_argument_list_length (arguments) ==
1198 _string_list_length (macro->parameters)) ||
1199 (_string_list_length (macro->parameters) == 0 &&
1200 _argument_list_length (arguments) == 1 &&
1201 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001202 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001203 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001204 "Error: macro %s invoked with %d arguments (expected %d)\n",
1205 identifier,
1206 _argument_list_length (arguments),
1207 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001208 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001209 }
1210
Carl Worth0197e9b2010-05-26 08:05:19 -07001211 /* Perform argument substitution on the replacement list. */
1212 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001213
Carl Worthce540f22010-05-26 08:25:44 -07001214 for (node = macro->replacements->head; node; node = node->next)
1215 {
1216 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001217 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001218 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001219 &parameter_index))
1220 {
1221 token_list_t *argument;
1222 argument = _argument_list_member_at (arguments,
1223 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001224 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001225 * tokens, or append a placeholder token for
1226 * an empty argument. */
1227 if (argument->head) {
Carl Worthfbe42402010-07-22 16:36:04 -07001228 token_list_t *expanded_argument;
1229 expanded_argument = _token_list_copy (parser,
1230 argument);
Carl Worth681afbc2010-05-28 15:06:02 -07001231 _glcpp_parser_expand_token_list (parser,
Carl Worthfbe42402010-07-22 16:36:04 -07001232 expanded_argument);
1233 _token_list_append_list (substituted,
1234 expanded_argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001235 } else {
1236 token_t *new_token;
1237
1238 new_token = _token_create_ival (substituted,
1239 PLACEHOLDER,
1240 PLACEHOLDER);
1241 _token_list_append (substituted, new_token);
1242 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001243 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001244 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001245 }
1246 }
1247
Carl Worthad0dee62010-05-26 09:04:50 -07001248 /* After argument substitution, and before further expansion
1249 * below, implement token pasting. */
1250
Carl Worthb06096e2010-05-29 05:54:19 -07001251 _token_list_trim_trailing_space (substituted);
1252
Carl Worthad0dee62010-05-26 09:04:50 -07001253 node = substituted->head;
1254 while (node)
1255 {
1256 token_node_t *next_non_space;
1257
1258 /* Look ahead for a PASTE token, skipping space. */
1259 next_non_space = node->next;
1260 while (next_non_space && next_non_space->token->type == SPACE)
1261 next_non_space = next_non_space->next;
1262
1263 if (next_non_space == NULL)
1264 break;
1265
1266 if (next_non_space->token->type != PASTE) {
1267 node = next_non_space;
1268 continue;
1269 }
1270
1271 /* Now find the next non-space token after the PASTE. */
1272 next_non_space = next_non_space->next;
1273 while (next_non_space && next_non_space->token->type == SPACE)
1274 next_non_space = next_non_space->next;
1275
1276 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001277 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001278 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001279 }
1280
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001281 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001282 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001283 if (next_non_space == substituted->tail)
1284 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001285
1286 node = node->next;
1287 }
1288
Carl Worthb06096e2010-05-29 05:54:19 -07001289 substituted->non_space_tail = substituted->tail;
1290
Carl Worth681afbc2010-05-28 15:06:02 -07001291 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001292}
1293
Carl Worth681afbc2010-05-28 15:06:02 -07001294/* Compute the complete expansion of node, (and subsequent nodes after
1295 * 'node' in the case that 'node' is a function-like macro and
1296 * subsequent nodes are arguments).
1297 *
1298 * Returns NULL if node is a simple token with no expansion.
1299 *
1300 * Otherwise, returns the token list that results from the expansion
1301 * and sets *last to the last node in the list that was consumed by
Carl Worthe1acbfc2010-07-20 16:44:03 -07001302 * the expansion. Specifically, *last will be set as follows:
Carl Worth681afbc2010-05-28 15:06:02 -07001303 *
1304 * As 'node' in the case of object-like macro expansion.
1305 *
1306 * As the token of the closing right parenthesis in the case of
1307 * function-like macro expansion.
1308 */
1309static token_list_t *
1310_glcpp_parser_expand_node (glcpp_parser_t *parser,
1311 token_node_t *node,
1312 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001313{
Carl Worth681afbc2010-05-28 15:06:02 -07001314 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001315 const char *identifier;
1316 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001317
1318 /* We only expand identifiers */
1319 if (token->type != IDENTIFIER) {
1320 /* We change any COMMA into a COMMA_FINAL to prevent
1321 * it being mistaken for an argument separator
1322 * later. */
1323 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001324 token->type = COMMA_FINAL;
1325 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001326 }
Carl Worth681afbc2010-05-28 15:06:02 -07001327
1328 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001329 }
1330
1331 /* Look up this identifier in the hash table. */
1332 identifier = token->value.str;
1333 macro = hash_table_find (parser->defines, identifier);
1334
Carl Worth681afbc2010-05-28 15:06:02 -07001335 /* Not a macro, so no expansion needed. */
1336 if (macro == NULL)
1337 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001338
1339 /* Finally, don't expand this macro if we're already actively
1340 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001341 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001342 /* We change the token type here from IDENTIFIER to
1343 * OTHER to prevent any future expansion of this
1344 * unexpanded token. */
1345 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001346 token_list_t *expansion;
1347 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001348
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001349 str = talloc_strdup (parser, token->value.str);
Carl Worth681afbc2010-05-28 15:06:02 -07001350 final = _token_create_str (parser, OTHER, str);
1351 expansion = _token_list_create (parser);
1352 _token_list_append (expansion, final);
1353 *last = node;
1354 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001355 }
1356
Carl Worth681afbc2010-05-28 15:06:02 -07001357 if (! macro->is_function)
1358 {
1359 *last = node;
1360
Carl Worthe1acbfc2010-07-20 16:44:03 -07001361 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth681afbc2010-05-28 15:06:02 -07001362 if (macro->replacements == NULL)
Carl Worthe1acbfc2010-07-20 16:44:03 -07001363 return _token_list_create_with_one_space (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001364
Carl Worth22b3ace2010-06-02 15:32:03 -07001365 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001366 }
Carl Worth681afbc2010-05-28 15:06:02 -07001367
1368 return _glcpp_parser_expand_function (parser, node, last);
1369}
1370
Carl Worth22b3ace2010-06-02 15:32:03 -07001371/* Push a new identifier onto the active list, returning the new list.
1372 *
1373 * Here, 'marker' is the token node that appears in the list after the
1374 * expansion of 'identifier'. That is, when the list iterator begins
1375 * examinging 'marker', then it is time to pop this node from the
1376 * active stack.
1377 */
1378active_list_t *
1379_active_list_push (active_list_t *list,
1380 const char *identifier,
1381 token_node_t *marker)
1382{
1383 active_list_t *node;
1384
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001385 node = talloc (list, active_list_t);
1386 node->identifier = talloc_strdup (node, identifier);
Carl Worth22b3ace2010-06-02 15:32:03 -07001387 node->marker = marker;
1388 node->next = list;
1389
1390 return node;
1391}
1392
1393active_list_t *
1394_active_list_pop (active_list_t *list)
1395{
1396 active_list_t *node = list;
1397
1398 if (node == NULL)
1399 return NULL;
1400
1401 node = list->next;
1402 talloc_free (list);
1403
1404 return node;
1405}
1406
1407int
1408_active_list_contains (active_list_t *list, const char *identifier)
1409{
1410 active_list_t *node;
1411
1412 if (list == NULL)
1413 return 0;
1414
1415 for (node = list; node; node = node->next)
1416 if (strcmp (node->identifier, identifier) == 0)
1417 return 1;
1418
1419 return 0;
1420}
1421
Carl Worth681afbc2010-05-28 15:06:02 -07001422/* Walk over the token list replacing nodes with their expansion.
1423 * Whenever nodes are expanded the walking will walk over the new
1424 * nodes, continuing to expand as necessary. The results are placed in
1425 * 'list' itself;
1426 */
1427static void
1428_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1429 token_list_t *list)
1430{
1431 token_node_t *node_prev;
Carl Worthc42e6402010-06-22 15:51:34 -07001432 token_node_t *node, *last = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001433 token_list_t *expansion;
1434
1435 if (list == NULL)
1436 return;
1437
1438 _token_list_trim_trailing_space (list);
1439
1440 node_prev = NULL;
1441 node = list->head;
1442
1443 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001444
1445 while (parser->active && parser->active->marker == node)
1446 parser->active = _active_list_pop (parser->active);
1447
Carl Worth681afbc2010-05-28 15:06:02 -07001448 /* Find the expansion for node, which will replace all
1449 * nodes from node to last, inclusive. */
1450 expansion = _glcpp_parser_expand_node (parser, node, &last);
1451 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001452 token_node_t *n;
1453
1454 for (n = node; n != last->next; n = n->next)
1455 while (parser->active &&
1456 parser->active->marker == n)
1457 {
1458 parser->active = _active_list_pop (parser->active);
1459 }
1460
1461 parser->active = _active_list_push (parser->active,
1462 node->token->value.str,
1463 last->next);
1464
Carl Worth681afbc2010-05-28 15:06:02 -07001465 /* Splice expansion into list, supporting a
1466 * simple deletion if the expansion is
1467 * empty. */
1468 if (expansion->head) {
1469 if (node_prev)
1470 node_prev->next = expansion->head;
1471 else
1472 list->head = expansion->head;
1473 expansion->tail->next = last->next;
1474 if (last == list->tail)
1475 list->tail = expansion->tail;
1476 } else {
1477 if (node_prev)
1478 node_prev->next = last->next;
1479 else
1480 list->head = last->next;
1481 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001482 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001483 }
1484 } else {
1485 node_prev = node;
1486 }
1487 node = node_prev ? node_prev->next : list->head;
1488 }
1489
Carl Worth22b3ace2010-06-02 15:32:03 -07001490 while (parser->active)
1491 parser->active = _active_list_pop (parser->active);
1492
Carl Worth681afbc2010-05-28 15:06:02 -07001493 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001494}
1495
Carl Worthae6517f2010-05-25 15:24:59 -07001496void
1497_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1498 token_list_t *list)
1499{
Carl Worthae6517f2010-05-25 15:24:59 -07001500 if (list == NULL)
1501 return;
1502
Carl Worth681afbc2010-05-28 15:06:02 -07001503 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001504
Carl Worth681afbc2010-05-28 15:06:02 -07001505 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001506
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001507 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001508}
1509
Carl Worthd80dcaf2010-07-20 15:55:21 -07001510static void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001511_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1512 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001513{
1514 /* According to the GLSL specification, macro names starting with "__"
1515 * or "GL_" are reserved for future use. So, don't allow them.
1516 */
1517 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001518 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001519 }
1520 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001521 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001522 }
1523}
1524
1525void
Carl Worthfcbbb462010-05-13 09:36:23 -07001526_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001527 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001528 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001529 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001530{
1531 macro_t *macro;
1532
Ian Romanick2d122362010-06-30 16:03:19 -07001533 if (loc != NULL)
1534 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001535
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001536 macro = talloc (parser, macro_t);
Carl Worthfcbbb462010-05-13 09:36:23 -07001537
1538 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001539 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001540 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001541 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001542
1543 hash_table_insert (parser->defines, macro, identifier);
1544}
1545
1546void
1547_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001548 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001549 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001550 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001551 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001552{
1553 macro_t *macro;
1554
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001555 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001556
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001557 macro = talloc (parser, macro_t);
Carl Worthfcbbb462010-05-13 09:36:23 -07001558
1559 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001560 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001561 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001562 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001563
1564 hash_table_insert (parser->defines, macro, identifier);
1565}
1566
Carl Worth8f38aff2010-05-19 10:01:29 -07001567static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001568glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001569{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001570 token_node_t *node;
1571 int ret;
1572
Carl Worth95951ea2010-05-26 15:57:10 -07001573 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001574 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001575
1576 /* XXX: This ugly block of code exists for the sole
1577 * purpose of converting a NEWLINE token into a SPACE
1578 * token, but only in the case where we have seen a
1579 * function-like macro name, but have not yet seen its
1580 * closing parenthesis.
1581 *
1582 * There's perhaps a more compact way to do this with
1583 * mid-rule actions in the grammar.
1584 *
1585 * I'm definitely not pleased with the complexity of
1586 * this code here.
1587 */
1588 if (parser->newline_as_space)
1589 {
1590 if (ret == '(') {
1591 parser->paren_count++;
1592 } else if (ret == ')') {
1593 parser->paren_count--;
1594 if (parser->paren_count == 0)
1595 parser->newline_as_space = 0;
1596 } else if (ret == NEWLINE) {
1597 ret = SPACE;
1598 } else if (ret != SPACE) {
1599 if (parser->paren_count == 0)
1600 parser->newline_as_space = 0;
1601 }
1602 }
1603 else if (parser->in_control_line)
1604 {
1605 if (ret == NEWLINE)
1606 parser->in_control_line = 0;
1607 }
1608 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1609 ret == HASH_UNDEF || ret == HASH_IF ||
1610 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1611 ret == HASH_ELIF || ret == HASH_ELSE ||
1612 ret == HASH_ENDIF || ret == HASH)
1613 {
1614 parser->in_control_line = 1;
1615 }
1616 else if (ret == IDENTIFIER)
1617 {
1618 macro_t *macro;
1619 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001620 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001621 if (macro && macro->is_function) {
1622 parser->newline_as_space = 1;
1623 parser->paren_count = 0;
1624 }
1625 }
1626
1627 return ret;
1628 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001629
1630 node = parser->lex_from_node;
1631
1632 if (node == NULL) {
1633 talloc_free (parser->lex_from_list);
1634 parser->lex_from_list = NULL;
1635 return NEWLINE;
1636 }
1637
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001638 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001639 ret = node->token->type;
1640
1641 parser->lex_from_node = node->next;
1642
1643 return ret;
1644}
1645
1646static void
1647glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1648{
1649 token_node_t *node;
1650
1651 assert (parser->lex_from_list == NULL);
1652
1653 /* Copy list, eliminating any space tokens. */
1654 parser->lex_from_list = _token_list_create (parser);
1655
1656 for (node = list->head; node; node = node->next) {
1657 if (node->token->type == SPACE)
1658 continue;
1659 _token_list_append (parser->lex_from_list, node->token);
1660 }
1661
1662 talloc_free (list);
1663
1664 parser->lex_from_node = parser->lex_from_list->head;
1665
1666 /* It's possible the list consisted of nothing but whitespace. */
1667 if (parser->lex_from_node == NULL) {
1668 talloc_free (parser->lex_from_list);
1669 parser->lex_from_list = NULL;
1670 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001671}
Carl Worthb20d33c2010-05-20 22:27:07 -07001672
1673static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001674_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1675 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001676{
1677 skip_type_t current = SKIP_NO_SKIP;
1678 skip_node_t *node;
1679
1680 if (parser->skip_stack)
1681 current = parser->skip_stack->type;
1682
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001683 node = talloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001684 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001685
1686 if (current == SKIP_NO_SKIP) {
1687 if (condition)
1688 node->type = SKIP_NO_SKIP;
1689 else
1690 node->type = SKIP_TO_ELSE;
1691 } else {
1692 node->type = SKIP_TO_ENDIF;
1693 }
1694
1695 node->next = parser->skip_stack;
1696 parser->skip_stack = node;
1697}
1698
1699static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001700_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1701 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001702{
1703 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001704 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001705 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001706 }
1707
1708 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1709 if (condition)
1710 parser->skip_stack->type = SKIP_NO_SKIP;
1711 } else {
1712 parser->skip_stack->type = SKIP_TO_ENDIF;
1713 }
1714}
Carl Worth80dc60b2010-05-25 14:42:00 -07001715
Carl Worthb20d33c2010-05-20 22:27:07 -07001716static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001717_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001718{
1719 skip_node_t *node;
1720
1721 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001722 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001723 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001724 }
1725
1726 node = parser->skip_stack;
1727 parser->skip_stack = node->next;
1728 talloc_free (node);
1729}