blob: 855448ff20d91d446965b887f783b49e9a44779b [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
Carl Worth681afbc2010-05-28 15:06:02 -0700109_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
110 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700111
Carl Worthaaa9acb2010-05-19 13:28:24 -0700112static void
Carl Worth681afbc2010-05-28 15:06:02 -0700113_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
114 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700115
116static void
Kenneth Graunke07745232010-06-17 12:41:46 -0700117_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
118 int condition);
Carl Worthb20d33c2010-05-20 22:27:07 -0700119
120static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700121_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
122 const char *type, int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700123
Carl Worthb20d33c2010-05-20 22:27:07 -0700124static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700125_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
Carl Worthb20d33c2010-05-20 22:27:07 -0700126
Carl Worth0293b2e2010-05-19 10:05:40 -0700127#define yylex glcpp_parser_lex
128
Carl Worth8f38aff2010-05-19 10:01:29 -0700129static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700130glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700131
Carl Worth8e82fcb2010-05-26 11:15:21 -0700132static void
133glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
134
Eric Anholtd4a04f32010-07-28 16:58:39 -0700135static void
136add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
137
Carl Worth3a37b872010-05-10 11:44:09 -0700138%}
139
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700140%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700141%error-verbose
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700142%locations
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700143
Carl Worth0b27b5f2010-05-10 16:16:06 -0700144%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700145%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700146
Carl Worthefef9502010-07-28 11:10:52 -0700147%expect 0
Eric Anholtd4a04f32010-07-28 16:58:39 -0700148%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 -0700149%token PASTE
Eric Anholtd4a04f32010-07-28 16:58:39 -0700150%type <ival> expression INTEGER operator SPACE integer_constant
Carl Worth050e3de2010-05-27 14:36:29 -0700151%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700152%type <string_list> identifier_list
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700153%type <token> preprocessing_token conditional_token
154%type <token_list> pp_tokens replacement_list text_line conditional_tokens
Carl Worth8fed1cd2010-05-26 09:32:12 -0700155%left OR
156%left AND
157%left '|'
158%left '^'
159%left '&'
160%left EQUAL NOT_EQUAL
161%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
162%left LEFT_SHIFT RIGHT_SHIFT
163%left '+' '-'
164%left '*' '/' '%'
165%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700166
167%%
168
Carl Worth33cc4002010-05-12 12:17:10 -0700169input:
Carl Worth3ff81672010-05-25 13:09:03 -0700170 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700171| input line
172;
173
174line:
175 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700176 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700177 }
Carl Worth808401f2010-05-25 14:52:43 -0700178| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700179 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700180 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700181 talloc_free ($1);
182 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700183| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700184| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700185;
186
Carl Worth8e82fcb2010-05-26 11:15:21 -0700187expanded_line:
188 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700189 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700190 }
191| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700192 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700193 }
194;
195
Carl Worth3ff81672010-05-25 13:09:03 -0700196control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700197 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700198 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700199 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700200| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700201 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700202 }
203| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700204 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700205 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700206| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700207 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700208 if (macro) {
Carl Worth61ebc012010-07-19 18:02:12 -0700209 hash_table_remove (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700210 talloc_free (macro);
211 }
212 talloc_free ($2);
213 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700214| HASH_IF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700215 token_list_t *expanded;
216 token_t *token;
217
218 expanded = _token_list_create (parser);
219 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
220 _token_list_append (expanded, token);
221 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700222 _glcpp_parser_expand_token_list (parser, $2);
223 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700224 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700225 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700226| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700227 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700228 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700229 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700230 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700231| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700232 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700233 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700234 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700235 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700236| HASH_ELIF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700237 token_list_t *expanded;
238 token_t *token;
239
240 expanded = _token_list_create (parser);
241 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
242 _token_list_append (expanded, token);
243 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700244 _glcpp_parser_expand_token_list (parser, $2);
245 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700246 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700247 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700248| HASH_ELIF NEWLINE {
249 /* #elif without an expression results in a warning if the
250 * condition doesn't matter (we just handled #if 1 or such)
251 * but an error otherwise. */
252 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
253 parser->skip_stack->type = SKIP_TO_ENDIF;
254 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
255 } else {
256 glcpp_error(& @1, parser, "#elif needs an expression");
257 }
258 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700259| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700260 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700261 }
262| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700263 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700264 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700265| HASH_VERSION integer_constant NEWLINE {
266 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
267 if (macro) {
268 hash_table_remove (parser->defines, "__VERSION__");
269 talloc_free (macro);
270 }
271 add_builtin_define (parser, "__VERSION__", $2);
272 glcpp_printf(parser->output, "#version %" PRIiMAX "\n", $2);
273 }
Carl Worth3ff81672010-05-25 13:09:03 -0700274| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700275;
276
Eric Anholtd4a04f32010-07-28 16:58:39 -0700277integer_constant:
Carl Worth050e3de2010-05-27 14:36:29 -0700278 INTEGER_STRING {
279 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
280 $$ = strtoll ($1 + 2, NULL, 16);
281 } else if ($1[0] == '0') {
282 $$ = strtoll ($1, NULL, 8);
283 } else {
284 $$ = strtoll ($1, NULL, 10);
285 }
286 }
287| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700288 $$ = $1;
289 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700290
291expression:
292 integer_constant
Carl Worth8fed1cd2010-05-26 09:32:12 -0700293| expression OR expression {
294 $$ = $1 || $3;
295 }
296| expression AND expression {
297 $$ = $1 && $3;
298 }
299| expression '|' expression {
300 $$ = $1 | $3;
301 }
302| expression '^' expression {
303 $$ = $1 ^ $3;
304 }
305| expression '&' expression {
306 $$ = $1 & $3;
307 }
308| expression NOT_EQUAL expression {
309 $$ = $1 != $3;
310 }
311| expression EQUAL expression {
312 $$ = $1 == $3;
313 }
314| expression GREATER_OR_EQUAL expression {
315 $$ = $1 >= $3;
316 }
317| expression LESS_OR_EQUAL expression {
318 $$ = $1 <= $3;
319 }
320| expression '>' expression {
321 $$ = $1 > $3;
322 }
323| expression '<' expression {
324 $$ = $1 < $3;
325 }
326| expression RIGHT_SHIFT expression {
327 $$ = $1 >> $3;
328 }
329| expression LEFT_SHIFT expression {
330 $$ = $1 << $3;
331 }
332| expression '-' expression {
333 $$ = $1 - $3;
334 }
335| expression '+' expression {
336 $$ = $1 + $3;
337 }
338| expression '%' expression {
339 $$ = $1 % $3;
340 }
341| expression '/' expression {
342 $$ = $1 / $3;
343 }
344| expression '*' expression {
345 $$ = $1 * $3;
346 }
347| '!' expression %prec UNARY {
348 $$ = ! $2;
349 }
350| '~' expression %prec UNARY {
351 $$ = ~ $2;
352 }
353| '-' expression %prec UNARY {
354 $$ = - $2;
355 }
356| '+' expression %prec UNARY {
357 $$ = + $2;
358 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700359| '(' expression ')' {
360 $$ = $2;
361 }
362;
363
Carl Worth3ff81672010-05-25 13:09:03 -0700364identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700365 IDENTIFIER {
366 $$ = _string_list_create (parser);
367 _string_list_append_item ($$, $1);
368 talloc_steal ($$, $1);
369 }
370| identifier_list ',' IDENTIFIER {
371 $$ = $1;
372 _string_list_append_item ($$, $3);
373 talloc_steal ($$, $3);
374 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700375;
376
Carl Worth3ff81672010-05-25 13:09:03 -0700377text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700378 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700379| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700380;
381
Carl Worth3ff81672010-05-25 13:09:03 -0700382non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700383 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700384 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700385 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700386;
387
Carl Worthaaa9acb2010-05-19 13:28:24 -0700388replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700389 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700390| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700391;
392
Kenneth Graunke65875742010-06-18 20:08:15 -0700393junk:
394 /* empty */
395| pp_tokens {
396 glcpp_warning(&@1, parser, "extra tokens at end of directive");
397 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700398;
399
400conditional_token:
401 /* Handle "defined" operator */
402 DEFINED IDENTIFIER {
403 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
404 $$ = _token_create_ival (parser, INTEGER, v);
405 }
406| DEFINED '(' IDENTIFIER ')' {
407 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
408 $$ = _token_create_ival (parser, INTEGER, v);
409 }
410| preprocessing_token
411;
412
413conditional_tokens:
414 /* Exactly the same as pp_tokens, but using conditional_token */
415 conditional_token {
416 parser->space_tokens = 1;
417 $$ = _token_list_create (parser);
418 _token_list_append ($$, $1);
419 talloc_unlink (parser, $1);
420 }
421| conditional_tokens conditional_token {
422 $$ = $1;
423 _token_list_append ($$, $2);
424 talloc_unlink (parser, $2);
425 }
426;
Kenneth Graunke65875742010-06-18 20:08:15 -0700427
Carl Worthaaa9acb2010-05-19 13:28:24 -0700428pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700429 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700430 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700431 $$ = _token_list_create (parser);
432 _token_list_append ($$, $1);
433 talloc_unlink (parser, $1);
434 }
435| pp_tokens preprocessing_token {
436 $$ = $1;
437 _token_list_append ($$, $2);
438 talloc_unlink (parser, $2);
439 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700440;
441
Carl Worth3ff81672010-05-25 13:09:03 -0700442preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700443 IDENTIFIER {
444 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700445 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700446 }
Carl Worth050e3de2010-05-27 14:36:29 -0700447| INTEGER_STRING {
448 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700449 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700450 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700451| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700452 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700453 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700454 }
455| OTHER {
456 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700457 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700458 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700459| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700460 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700461 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700462 }
Carl Worth3ff81672010-05-25 13:09:03 -0700463;
464
Carl Worth8e82fcb2010-05-26 11:15:21 -0700465operator:
Carl Worth808401f2010-05-25 14:52:43 -0700466 '[' { $$ = '['; }
467| ']' { $$ = ']'; }
468| '(' { $$ = '('; }
469| ')' { $$ = ')'; }
470| '{' { $$ = '{'; }
471| '}' { $$ = '}'; }
472| '.' { $$ = '.'; }
473| '&' { $$ = '&'; }
474| '*' { $$ = '*'; }
475| '+' { $$ = '+'; }
476| '-' { $$ = '-'; }
477| '~' { $$ = '~'; }
478| '!' { $$ = '!'; }
479| '/' { $$ = '/'; }
480| '%' { $$ = '%'; }
481| LEFT_SHIFT { $$ = LEFT_SHIFT; }
482| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
483| '<' { $$ = '<'; }
484| '>' { $$ = '>'; }
485| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
486| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
487| EQUAL { $$ = EQUAL; }
488| NOT_EQUAL { $$ = NOT_EQUAL; }
489| '^' { $$ = '^'; }
490| '|' { $$ = '|'; }
491| AND { $$ = AND; }
492| OR { $$ = OR; }
493| ';' { $$ = ';'; }
494| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700495| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700496| PASTE { $$ = PASTE; }
Carl Worth3ff81672010-05-25 13:09:03 -0700497;
498
Carl Worth33cc4002010-05-12 12:17:10 -0700499%%
500
Carl Worth610053b2010-05-14 10:05:11 -0700501string_list_t *
502_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700503{
Carl Worth610053b2010-05-14 10:05:11 -0700504 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700505
Carl Worth610053b2010-05-14 10:05:11 -0700506 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700507 list->head = NULL;
508 list->tail = NULL;
509
510 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700511}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700512
Carl Worth33cc4002010-05-12 12:17:10 -0700513void
Carl Worth610053b2010-05-14 10:05:11 -0700514_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700515{
Carl Worth610053b2010-05-14 10:05:11 -0700516 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700517
Carl Worth610053b2010-05-14 10:05:11 -0700518 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700519 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700520
Carl Worth33cc4002010-05-12 12:17:10 -0700521 node->next = NULL;
522
523 if (list->head == NULL) {
524 list->head = node;
525 } else {
526 list->tail->next = node;
527 }
528
529 list->tail = node;
530}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700531
532int
Carl Worth610053b2010-05-14 10:05:11 -0700533_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700534{
Carl Worth610053b2010-05-14 10:05:11 -0700535 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700536 int i;
537
538 if (list == NULL)
539 return 0;
540
541 for (i = 0, node = list->head; node; i++, node = node->next) {
542 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700543 if (index)
544 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700545 return 1;
546 }
547 }
548
549 return 0;
550}
551
552int
Carl Worth610053b2010-05-14 10:05:11 -0700553_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700554{
555 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700556 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700557
558 if (list == NULL)
559 return 0;
560
561 for (node = list->head; node; node = node->next)
562 length++;
563
564 return length;
565}
566
Carl Worth8f6a8282010-05-14 10:44:19 -0700567argument_list_t *
568_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700569{
Carl Worth8f6a8282010-05-14 10:44:19 -0700570 argument_list_t *list;
571
572 list = xtalloc (ctx, argument_list_t);
573 list->head = NULL;
574 list->tail = NULL;
575
576 return list;
577}
578
579void
Carl Worth47252442010-05-19 13:54:37 -0700580_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700581{
582 argument_node_t *node;
583
Carl Worth8f6a8282010-05-14 10:44:19 -0700584 node = xtalloc (list, argument_node_t);
585 node->argument = argument;
586
587 node->next = NULL;
588
589 if (list->head == NULL) {
590 list->head = node;
591 } else {
592 list->tail->next = node;
593 }
594
595 list->tail = node;
596}
597
598int
599_argument_list_length (argument_list_t *list)
600{
601 int length = 0;
602 argument_node_t *node;
603
604 if (list == NULL)
605 return 0;
606
607 for (node = list->head; node; node = node->next)
608 length++;
609
610 return length;
611}
612
Carl Worth47252442010-05-19 13:54:37 -0700613token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700614_argument_list_member_at (argument_list_t *list, int index)
615{
616 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700617 int i;
618
619 if (list == NULL)
620 return NULL;
621
622 node = list->head;
623 for (i = 0; i < index; i++) {
624 node = node->next;
625 if (node == NULL)
626 break;
627 }
628
629 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700630 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700631
632 return NULL;
633}
Carl Worth47252442010-05-19 13:54:37 -0700634
Carl Worth808401f2010-05-25 14:52:43 -0700635/* Note: This function talloc_steal()s the str pointer. */
636token_t *
637_token_create_str (void *ctx, int type, char *str)
638{
639 token_t *token;
640
641 token = xtalloc (ctx, token_t);
642 token->type = type;
643 token->value.str = talloc_steal (token, str);
644
645 return token;
646}
647
648token_t *
649_token_create_ival (void *ctx, int type, int ival)
650{
651 token_t *token;
652
653 token = xtalloc (ctx, token_t);
654 token->type = type;
655 token->value.ival = ival;
656
657 return token;
658}
659
Carl Worth47252442010-05-19 13:54:37 -0700660token_list_t *
661_token_list_create (void *ctx)
662{
663 token_list_t *list;
664
665 list = xtalloc (ctx, token_list_t);
666 list->head = NULL;
667 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700668 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700669
670 return list;
671}
672
673void
Carl Worth808401f2010-05-25 14:52:43 -0700674_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700675{
676 token_node_t *node;
677
678 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700679 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700680
681 node->next = NULL;
682
683 if (list->head == NULL) {
684 list->head = node;
685 } else {
686 list->tail->next = node;
687 }
688
689 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700690 if (token->type != SPACE)
691 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700692}
693
694void
695_token_list_append_list (token_list_t *list, token_list_t *tail)
696{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700697 if (tail == NULL || tail->head == NULL)
698 return;
699
Carl Worth47252442010-05-19 13:54:37 -0700700 if (list->head == NULL) {
701 list->head = tail->head;
702 } else {
703 list->tail->next = tail->head;
704 }
705
706 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700707 list->non_space_tail = tail->non_space_tail;
708}
709
Carl Worthd80dcaf2010-07-20 15:55:21 -0700710static token_list_t *
Carl Worth681afbc2010-05-28 15:06:02 -0700711_token_list_copy (void *ctx, token_list_t *other)
712{
713 token_list_t *copy;
714 token_node_t *node;
715
716 if (other == NULL)
717 return NULL;
718
719 copy = _token_list_create (ctx);
720 for (node = other->head; node; node = node->next)
721 _token_list_append (copy, node->token);
722
723 return copy;
724}
725
Carl Worthd80dcaf2010-07-20 15:55:21 -0700726static void
Carl Worth10ae4382010-05-25 20:35:01 -0700727_token_list_trim_trailing_space (token_list_t *list)
728{
729 token_node_t *tail, *next;
730
731 if (list->non_space_tail) {
732 tail = list->non_space_tail->next;
733 list->non_space_tail->next = NULL;
734 list->tail = list->non_space_tail;
735
736 while (tail) {
737 next = tail->next;
738 talloc_free (tail);
739 tail = next;
740 }
741 }
Carl Worth47252442010-05-19 13:54:37 -0700742}
Carl Worth80dc60b2010-05-25 14:42:00 -0700743
Carl Worth0197e9b2010-05-26 08:05:19 -0700744static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700745_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700746{
747 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700748 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700749 return;
750 }
751
752 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700753 case INTEGER:
Eric Anholt8605c292010-07-28 16:53:51 -0700754 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700755 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700756 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700757 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700758 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700759 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700760 break;
761 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700762 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700763 break;
764 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700765 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700766 break;
767 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700768 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700769 break;
770 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700771 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700772 break;
773 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700774 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700775 break;
776 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700777 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700778 break;
779 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700780 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700781 break;
782 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700783 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700784 break;
785 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700786 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700787 break;
788 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700789 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700790 break;
Carl Worthdd749002010-05-27 10:12:33 -0700791 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700792 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700793 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700794 case PLACEHOLDER:
795 /* Nothing to print. */
796 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700797 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700798 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700799 break;
800 }
801}
802
Carl Worthb06096e2010-05-29 05:54:19 -0700803/* Return a new token (talloc()ed off of 'token') formed by pasting
804 * 'token' and 'other'. Note that this function may return 'token' or
805 * 'other' directly rather than allocating anything new.
806 *
807 * Caution: Only very cursory error-checking is performed to see if
808 * the final result is a valid single token. */
809static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700810_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700811{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700812 token_t *combined = NULL;
813
Carl Worth85b50e82010-05-27 14:01:18 -0700814 /* Pasting a placeholder onto anything makes no change. */
815 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700816 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700817
Carl Worthb06096e2010-05-29 05:54:19 -0700818 /* When 'token' is a placeholder, just return 'other'. */
819 if (token->type == PLACEHOLDER)
820 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700821
Carl Worthad0dee62010-05-26 09:04:50 -0700822 /* A very few single-character punctuators can be combined
823 * with another to form a multi-character punctuator. */
824 switch (token->type) {
825 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700826 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700827 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700828 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700829 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700830 break;
831 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700832 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700833 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700834 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700835 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700836 break;
837 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700838 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700839 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700840 break;
841 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700842 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700843 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700844 break;
845 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700846 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700847 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700848 break;
849 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700850 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700851 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700852 break;
853 }
854
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700855 if (combined != NULL) {
856 /* Inherit the location from the first token */
857 combined->location = token->location;
858 return combined;
859 }
860
Carl Worthad0dee62010-05-26 09:04:50 -0700861 /* Two string-valued tokens can usually just be mashed
862 * together.
863 *
Carl Worth050e3de2010-05-27 14:36:29 -0700864 * XXX: This isn't actually legitimate. Several things here
865 * should result in a diagnostic since the result cannot be a
866 * valid, single pre-processing token. For example, pasting
867 * "123" and "abc" is not legal, but we don't catch that
868 * here. */
869 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
870 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700871 {
Carl Worthb06096e2010-05-29 05:54:19 -0700872 char *str;
873
874 str = xtalloc_asprintf (token, "%s%s",
875 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700876 combined = _token_create_str (token, token->type, str);
877 combined->location = token->location;
878 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700879 }
880
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700881 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700882 glcpp_print (parser->info_log, "Pasting \"");
883 _token_print (&parser->info_log, token);
884 glcpp_print (parser->info_log, "\" and \"");
885 _token_print (&parser->info_log, other);
886 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700887
888 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700889}
890
Carl Worth0197e9b2010-05-26 08:05:19 -0700891static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700892_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700893{
894 token_node_t *node;
895
896 if (list == NULL)
897 return;
898
899 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700900 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700901}
902
Carl Worth3a37b872010-05-10 11:44:09 -0700903void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700904yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700905{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700906 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700907}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700908
Eric Anholtd4a04f32010-07-28 16:58:39 -0700909static void add_builtin_define(glcpp_parser_t *parser,
910 const char *name, int value)
911{
912 token_t *tok;
913 token_list_t *list;
914
915 tok = _token_create_ival (parser, INTEGER, value);
916
917 list = _token_list_create(parser);
918 _token_list_append(list, tok);
919 _define_object_macro(parser, NULL, name, list);
920
921 talloc_unlink(parser, tok);
922}
923
Carl Worth33cc4002010-05-12 12:17:10 -0700924glcpp_parser_t *
Ian Romanick06143ea2010-06-30 16:27:22 -0700925glcpp_parser_create (const struct gl_extensions *extensions)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700926{
Carl Worth33cc4002010-05-12 12:17:10 -0700927 glcpp_parser_t *parser;
Eric Anholtd4a04f32010-07-28 16:58:39 -0700928 int language_version;
Carl Worth33cc4002010-05-12 12:17:10 -0700929
Carl Worth5070a202010-05-12 12:45:33 -0700930 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700931
Carl Worth8f38aff2010-05-19 10:01:29 -0700932 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700933 parser->defines = hash_table_ctor (32, hash_table_string_hash,
934 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700935 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700936 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700937 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700938 parser->newline_as_space = 0;
939 parser->in_control_line = 0;
940 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700941
Carl Worthb20d33c2010-05-20 22:27:07 -0700942 parser->skip_stack = NULL;
943
Carl Worth8e82fcb2010-05-26 11:15:21 -0700944 parser->lex_from_list = NULL;
945 parser->lex_from_node = NULL;
946
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700947 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700948 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke1b85c462010-06-21 13:55:12 -0700949 parser->error = 0;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700950
Ian Romanick2d122362010-06-30 16:03:19 -0700951 /* Add pre-defined macros. */
Eric Anholtd4a04f32010-07-28 16:58:39 -0700952 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
953 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
Ian Romanick2d122362010-06-30 16:03:19 -0700954
Eric Anholtd4a04f32010-07-28 16:58:39 -0700955 if (extensions != NULL) {
956 if (extensions->EXT_texture_array) {
957 add_builtin_define(parser, "GL_EXT_texture_array", 1);
958 }
Ian Romanick2d122362010-06-30 16:03:19 -0700959
Eric Anholtd4a04f32010-07-28 16:58:39 -0700960 if (extensions->ARB_fragment_coord_conventions)
961 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
962 1);
Ian Romanick06143ea2010-06-30 16:27:22 -0700963 }
964
Eric Anholtd4a04f32010-07-28 16:58:39 -0700965 language_version = 110;
Eric Anholtd4a04f32010-07-28 16:58:39 -0700966 add_builtin_define(parser, "__VERSION__", language_version);
Ian Romanick2d122362010-06-30 16:03:19 -0700967
Carl Worth33cc4002010-05-12 12:17:10 -0700968 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700969}
970
971int
972glcpp_parser_parse (glcpp_parser_t *parser)
973{
974 return yyparse (parser);
975}
976
977void
Carl Worth33cc4002010-05-12 12:17:10 -0700978glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700979{
Carl Worthb20d33c2010-05-20 22:27:07 -0700980 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700981 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700982 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700983 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700984 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700985}
Carl Worthc6d5af32010-05-11 12:30:09 -0700986
Carl Worthb1854fd2010-05-25 16:28:26 -0700987typedef enum function_status
988{
989 FUNCTION_STATUS_SUCCESS,
990 FUNCTION_NOT_A_FUNCTION,
991 FUNCTION_UNBALANCED_PARENTHESES
992} function_status_t;
993
994/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700995 * balanced set of parentheses.
996 *
997 * When called, 'node' should be the opening-parenthesis token, (or
998 * perhaps preceeding SPACE tokens). Upon successful return *last will
999 * be the last consumed node, (corresponding to the closing right
1000 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -07001001 *
1002 * Return values:
1003 *
1004 * FUNCTION_STATUS_SUCCESS:
1005 *
1006 * Successfully parsed a set of function arguments.
1007 *
1008 * FUNCTION_NOT_A_FUNCTION:
1009 *
1010 * Macro name not followed by a '('. This is not an error, but
1011 * simply that the macro name should be treated as a non-macro.
1012 *
Carl Worth14c98a52010-06-02 15:49:54 -07001013 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -07001014 *
1015 * Macro name is not followed by a balanced set of parentheses.
1016 */
1017static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -07001018_arguments_parse (argument_list_t *arguments,
1019 token_node_t *node,
1020 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001021{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001022 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001023 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001024
Carl Worthb1854fd2010-05-25 16:28:26 -07001025 node = node->next;
1026
1027 /* Ignore whitespace before first parenthesis. */
1028 while (node && node->token->type == SPACE)
1029 node = node->next;
1030
1031 if (node == NULL || node->token->type != '(')
1032 return FUNCTION_NOT_A_FUNCTION;
1033
Carl Worth652fa272010-05-25 17:45:22 -07001034 node = node->next;
1035
Carl Wortha19297b2010-05-27 13:29:19 -07001036 argument = _token_list_create (arguments);
1037 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001038
Carl Worth681afbc2010-05-28 15:06:02 -07001039 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001040 if (node->token->type == '(')
1041 {
1042 paren_count++;
1043 }
1044 else if (node->token->type == ')')
1045 {
1046 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001047 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001048 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001049 }
Carl Worth652fa272010-05-25 17:45:22 -07001050
1051 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001052 paren_count == 1)
1053 {
Carl Wortha19297b2010-05-27 13:29:19 -07001054 _token_list_trim_trailing_space (argument);
1055 argument = _token_list_create (arguments);
1056 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001057 }
1058 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001059 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001060 /* Don't treat initial whitespace as
1061 * part of the arguement. */
1062 if (node->token->type == SPACE)
1063 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001064 }
1065 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001066 }
Carl Worthc7581c22010-05-25 17:41:07 -07001067 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001068
Carl Worth681afbc2010-05-28 15:06:02 -07001069 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001070 return FUNCTION_UNBALANCED_PARENTHESES;
1071
Carl Worth681afbc2010-05-28 15:06:02 -07001072 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001073
1074 return FUNCTION_STATUS_SUCCESS;
1075}
1076
Carl Worthe1acbfc2010-07-20 16:44:03 -07001077static token_list_t *
1078_token_list_create_with_one_space (void *ctx)
1079{
1080 token_list_t *list;
1081 token_t *space;
1082
1083 list = _token_list_create (ctx);
1084 space = _token_create_ival (list, SPACE, SPACE);
1085 _token_list_append (list, space);
1086
1087 return list;
1088}
1089
Carl Worth681afbc2010-05-28 15:06:02 -07001090/* This is a helper function that's essentially part of the
1091 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1092 * except for by that function.
1093 *
1094 * Returns NULL if node is a simple token with no expansion, (that is,
1095 * although 'node' corresponds to an identifier defined as a
1096 * function-like macro, it is not followed with a parenthesized
1097 * argument list).
1098 *
1099 * Compute the complete expansion of node (which is a function-like
1100 * macro) and subsequent nodes which are arguments.
1101 *
1102 * Returns the token list that results from the expansion and sets
1103 * *last to the last node in the list that was consumed by the
Carl Worthfbe42402010-07-22 16:36:04 -07001104 * expansion. Specifically, *last will be set as follows: as the
Carl Worth681afbc2010-05-28 15:06:02 -07001105 * token of the closing right parenthesis.
1106 */
1107static token_list_t *
1108_glcpp_parser_expand_function (glcpp_parser_t *parser,
1109 token_node_t *node,
1110 token_node_t **last)
1111
Carl Worthb1854fd2010-05-25 16:28:26 -07001112{
1113 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001114 const char *identifier;
1115 argument_list_t *arguments;
1116 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001117 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001118 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001119
Carl Worthb1854fd2010-05-25 16:28:26 -07001120 identifier = node->token->value.str;
1121
1122 macro = hash_table_find (parser->defines, identifier);
1123
1124 assert (macro->is_function);
1125
Carl Worth9ce18cf2010-05-25 17:32:21 -07001126 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001127 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001128
1129 switch (status) {
1130 case FUNCTION_STATUS_SUCCESS:
1131 break;
1132 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001133 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001134 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001135 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001136 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001137 }
1138
Carl Worthe1acbfc2010-07-20 16:44:03 -07001139 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth9ce18cf2010-05-25 17:32:21 -07001140 if (macro->replacements == NULL) {
1141 talloc_free (arguments);
Carl Worthe1acbfc2010-07-20 16:44:03 -07001142 return _token_list_create_with_one_space (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001143 }
1144
Carl Wortha19297b2010-05-27 13:29:19 -07001145 if (! ((_argument_list_length (arguments) ==
1146 _string_list_length (macro->parameters)) ||
1147 (_string_list_length (macro->parameters) == 0 &&
1148 _argument_list_length (arguments) == 1 &&
1149 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001150 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001151 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001152 "Error: macro %s invoked with %d arguments (expected %d)\n",
1153 identifier,
1154 _argument_list_length (arguments),
1155 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001156 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001157 }
1158
Carl Worth0197e9b2010-05-26 08:05:19 -07001159 /* Perform argument substitution on the replacement list. */
1160 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001161
Carl Worthce540f22010-05-26 08:25:44 -07001162 for (node = macro->replacements->head; node; node = node->next)
1163 {
1164 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001165 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001166 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001167 &parameter_index))
1168 {
1169 token_list_t *argument;
1170 argument = _argument_list_member_at (arguments,
1171 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001172 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001173 * tokens, or append a placeholder token for
1174 * an empty argument. */
1175 if (argument->head) {
Carl Worthfbe42402010-07-22 16:36:04 -07001176 token_list_t *expanded_argument;
1177 expanded_argument = _token_list_copy (parser,
1178 argument);
Carl Worth681afbc2010-05-28 15:06:02 -07001179 _glcpp_parser_expand_token_list (parser,
Carl Worthfbe42402010-07-22 16:36:04 -07001180 expanded_argument);
1181 _token_list_append_list (substituted,
1182 expanded_argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001183 } else {
1184 token_t *new_token;
1185
1186 new_token = _token_create_ival (substituted,
1187 PLACEHOLDER,
1188 PLACEHOLDER);
1189 _token_list_append (substituted, new_token);
1190 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001191 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001192 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001193 }
1194 }
1195
Carl Worthad0dee62010-05-26 09:04:50 -07001196 /* After argument substitution, and before further expansion
1197 * below, implement token pasting. */
1198
Carl Worthb06096e2010-05-29 05:54:19 -07001199 _token_list_trim_trailing_space (substituted);
1200
Carl Worthad0dee62010-05-26 09:04:50 -07001201 node = substituted->head;
1202 while (node)
1203 {
1204 token_node_t *next_non_space;
1205
1206 /* Look ahead for a PASTE token, skipping space. */
1207 next_non_space = node->next;
1208 while (next_non_space && next_non_space->token->type == SPACE)
1209 next_non_space = next_non_space->next;
1210
1211 if (next_non_space == NULL)
1212 break;
1213
1214 if (next_non_space->token->type != PASTE) {
1215 node = next_non_space;
1216 continue;
1217 }
1218
1219 /* Now find the next non-space token after the PASTE. */
1220 next_non_space = next_non_space->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) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001225 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001226 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001227 }
1228
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001229 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001230 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001231 if (next_non_space == substituted->tail)
1232 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001233
1234 node = node->next;
1235 }
1236
Carl Worthb06096e2010-05-29 05:54:19 -07001237 substituted->non_space_tail = substituted->tail;
1238
Carl Worth681afbc2010-05-28 15:06:02 -07001239 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001240}
1241
Carl Worth681afbc2010-05-28 15:06:02 -07001242/* Compute the complete expansion of node, (and subsequent nodes after
1243 * 'node' in the case that 'node' is a function-like macro and
1244 * subsequent nodes are arguments).
1245 *
1246 * Returns NULL if node is a simple token with no expansion.
1247 *
1248 * Otherwise, returns the token list that results from the expansion
1249 * and sets *last to the last node in the list that was consumed by
Carl Worthe1acbfc2010-07-20 16:44:03 -07001250 * the expansion. Specifically, *last will be set as follows:
Carl Worth681afbc2010-05-28 15:06:02 -07001251 *
1252 * As 'node' in the case of object-like macro expansion.
1253 *
1254 * As the token of the closing right parenthesis in the case of
1255 * function-like macro expansion.
1256 */
1257static token_list_t *
1258_glcpp_parser_expand_node (glcpp_parser_t *parser,
1259 token_node_t *node,
1260 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001261{
Carl Worth681afbc2010-05-28 15:06:02 -07001262 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001263 const char *identifier;
1264 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001265
1266 /* We only expand identifiers */
1267 if (token->type != IDENTIFIER) {
1268 /* We change any COMMA into a COMMA_FINAL to prevent
1269 * it being mistaken for an argument separator
1270 * later. */
1271 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001272 token->type = COMMA_FINAL;
1273 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001274 }
Carl Worth681afbc2010-05-28 15:06:02 -07001275
1276 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001277 }
1278
1279 /* Look up this identifier in the hash table. */
1280 identifier = token->value.str;
1281 macro = hash_table_find (parser->defines, identifier);
1282
Carl Worth681afbc2010-05-28 15:06:02 -07001283 /* Not a macro, so no expansion needed. */
1284 if (macro == NULL)
1285 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001286
1287 /* Finally, don't expand this macro if we're already actively
1288 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001289 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001290 /* We change the token type here from IDENTIFIER to
1291 * OTHER to prevent any future expansion of this
1292 * unexpanded token. */
1293 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001294 token_list_t *expansion;
1295 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001296
Carl Worth681afbc2010-05-28 15:06:02 -07001297 str = xtalloc_strdup (parser, token->value.str);
1298 final = _token_create_str (parser, OTHER, str);
1299 expansion = _token_list_create (parser);
1300 _token_list_append (expansion, final);
1301 *last = node;
1302 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001303 }
1304
Carl Worth681afbc2010-05-28 15:06:02 -07001305 if (! macro->is_function)
1306 {
1307 *last = node;
1308
Carl Worthe1acbfc2010-07-20 16:44:03 -07001309 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth681afbc2010-05-28 15:06:02 -07001310 if (macro->replacements == NULL)
Carl Worthe1acbfc2010-07-20 16:44:03 -07001311 return _token_list_create_with_one_space (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001312
Carl Worth22b3ace2010-06-02 15:32:03 -07001313 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001314 }
Carl Worth681afbc2010-05-28 15:06:02 -07001315
1316 return _glcpp_parser_expand_function (parser, node, last);
1317}
1318
Carl Worth22b3ace2010-06-02 15:32:03 -07001319/* Push a new identifier onto the active list, returning the new list.
1320 *
1321 * Here, 'marker' is the token node that appears in the list after the
1322 * expansion of 'identifier'. That is, when the list iterator begins
1323 * examinging 'marker', then it is time to pop this node from the
1324 * active stack.
1325 */
1326active_list_t *
1327_active_list_push (active_list_t *list,
1328 const char *identifier,
1329 token_node_t *marker)
1330{
1331 active_list_t *node;
1332
1333 node = xtalloc (list, active_list_t);
1334 node->identifier = xtalloc_strdup (node, identifier);
1335 node->marker = marker;
1336 node->next = list;
1337
1338 return node;
1339}
1340
1341active_list_t *
1342_active_list_pop (active_list_t *list)
1343{
1344 active_list_t *node = list;
1345
1346 if (node == NULL)
1347 return NULL;
1348
1349 node = list->next;
1350 talloc_free (list);
1351
1352 return node;
1353}
1354
1355int
1356_active_list_contains (active_list_t *list, const char *identifier)
1357{
1358 active_list_t *node;
1359
1360 if (list == NULL)
1361 return 0;
1362
1363 for (node = list; node; node = node->next)
1364 if (strcmp (node->identifier, identifier) == 0)
1365 return 1;
1366
1367 return 0;
1368}
1369
Carl Worth681afbc2010-05-28 15:06:02 -07001370/* Walk over the token list replacing nodes with their expansion.
1371 * Whenever nodes are expanded the walking will walk over the new
1372 * nodes, continuing to expand as necessary. The results are placed in
1373 * 'list' itself;
1374 */
1375static void
1376_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1377 token_list_t *list)
1378{
1379 token_node_t *node_prev;
Carl Worthc42e6402010-06-22 15:51:34 -07001380 token_node_t *node, *last = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001381 token_list_t *expansion;
1382
1383 if (list == NULL)
1384 return;
1385
1386 _token_list_trim_trailing_space (list);
1387
1388 node_prev = NULL;
1389 node = list->head;
1390
1391 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001392
1393 while (parser->active && parser->active->marker == node)
1394 parser->active = _active_list_pop (parser->active);
1395
Carl Worth681afbc2010-05-28 15:06:02 -07001396 /* Find the expansion for node, which will replace all
1397 * nodes from node to last, inclusive. */
1398 expansion = _glcpp_parser_expand_node (parser, node, &last);
1399 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001400 token_node_t *n;
1401
1402 for (n = node; n != last->next; n = n->next)
1403 while (parser->active &&
1404 parser->active->marker == n)
1405 {
1406 parser->active = _active_list_pop (parser->active);
1407 }
1408
1409 parser->active = _active_list_push (parser->active,
1410 node->token->value.str,
1411 last->next);
1412
Carl Worth681afbc2010-05-28 15:06:02 -07001413 /* Splice expansion into list, supporting a
1414 * simple deletion if the expansion is
1415 * empty. */
1416 if (expansion->head) {
1417 if (node_prev)
1418 node_prev->next = expansion->head;
1419 else
1420 list->head = expansion->head;
1421 expansion->tail->next = last->next;
1422 if (last == list->tail)
1423 list->tail = expansion->tail;
1424 } else {
1425 if (node_prev)
1426 node_prev->next = last->next;
1427 else
1428 list->head = last->next;
1429 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001430 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001431 }
1432 } else {
1433 node_prev = node;
1434 }
1435 node = node_prev ? node_prev->next : list->head;
1436 }
1437
Carl Worth22b3ace2010-06-02 15:32:03 -07001438 while (parser->active)
1439 parser->active = _active_list_pop (parser->active);
1440
Carl Worth681afbc2010-05-28 15:06:02 -07001441 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001442}
1443
Carl Worthae6517f2010-05-25 15:24:59 -07001444void
1445_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1446 token_list_t *list)
1447{
Carl Worthae6517f2010-05-25 15:24:59 -07001448 if (list == NULL)
1449 return;
1450
Carl Worth681afbc2010-05-28 15:06:02 -07001451 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001452
Carl Worth681afbc2010-05-28 15:06:02 -07001453 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001454
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001455 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001456}
1457
Carl Worthd80dcaf2010-07-20 15:55:21 -07001458static void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001459_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1460 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001461{
1462 /* According to the GLSL specification, macro names starting with "__"
1463 * or "GL_" are reserved for future use. So, don't allow them.
1464 */
1465 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001466 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001467 }
1468 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001469 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001470 }
1471}
1472
1473void
Carl Worthfcbbb462010-05-13 09:36:23 -07001474_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001475 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001476 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001477 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001478{
1479 macro_t *macro;
1480
Ian Romanick2d122362010-06-30 16:03:19 -07001481 if (loc != NULL)
1482 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001483
Carl Worthfcbbb462010-05-13 09:36:23 -07001484 macro = xtalloc (parser, macro_t);
1485
1486 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001487 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001488 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001489 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001490
1491 hash_table_insert (parser->defines, macro, identifier);
1492}
1493
1494void
1495_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001496 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001497 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001498 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001499 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001500{
1501 macro_t *macro;
1502
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001503 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001504
Carl Worthfcbbb462010-05-13 09:36:23 -07001505 macro = xtalloc (parser, macro_t);
1506
1507 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001508 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001509 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001510 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001511
1512 hash_table_insert (parser->defines, macro, identifier);
1513}
1514
Carl Worth8f38aff2010-05-19 10:01:29 -07001515static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001516glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001517{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001518 token_node_t *node;
1519 int ret;
1520
Carl Worth95951ea2010-05-26 15:57:10 -07001521 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001522 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001523
1524 /* XXX: This ugly block of code exists for the sole
1525 * purpose of converting a NEWLINE token into a SPACE
1526 * token, but only in the case where we have seen a
1527 * function-like macro name, but have not yet seen its
1528 * closing parenthesis.
1529 *
1530 * There's perhaps a more compact way to do this with
1531 * mid-rule actions in the grammar.
1532 *
1533 * I'm definitely not pleased with the complexity of
1534 * this code here.
1535 */
1536 if (parser->newline_as_space)
1537 {
1538 if (ret == '(') {
1539 parser->paren_count++;
1540 } else if (ret == ')') {
1541 parser->paren_count--;
1542 if (parser->paren_count == 0)
1543 parser->newline_as_space = 0;
1544 } else if (ret == NEWLINE) {
1545 ret = SPACE;
1546 } else if (ret != SPACE) {
1547 if (parser->paren_count == 0)
1548 parser->newline_as_space = 0;
1549 }
1550 }
1551 else if (parser->in_control_line)
1552 {
1553 if (ret == NEWLINE)
1554 parser->in_control_line = 0;
1555 }
1556 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1557 ret == HASH_UNDEF || ret == HASH_IF ||
1558 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1559 ret == HASH_ELIF || ret == HASH_ELSE ||
1560 ret == HASH_ENDIF || ret == HASH)
1561 {
1562 parser->in_control_line = 1;
1563 }
1564 else if (ret == IDENTIFIER)
1565 {
1566 macro_t *macro;
1567 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001568 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001569 if (macro && macro->is_function) {
1570 parser->newline_as_space = 1;
1571 parser->paren_count = 0;
1572 }
1573 }
1574
1575 return ret;
1576 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001577
1578 node = parser->lex_from_node;
1579
1580 if (node == NULL) {
1581 talloc_free (parser->lex_from_list);
1582 parser->lex_from_list = NULL;
1583 return NEWLINE;
1584 }
1585
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001586 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001587 ret = node->token->type;
1588
1589 parser->lex_from_node = node->next;
1590
1591 return ret;
1592}
1593
1594static void
1595glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1596{
1597 token_node_t *node;
1598
1599 assert (parser->lex_from_list == NULL);
1600
1601 /* Copy list, eliminating any space tokens. */
1602 parser->lex_from_list = _token_list_create (parser);
1603
1604 for (node = list->head; node; node = node->next) {
1605 if (node->token->type == SPACE)
1606 continue;
1607 _token_list_append (parser->lex_from_list, node->token);
1608 }
1609
1610 talloc_free (list);
1611
1612 parser->lex_from_node = parser->lex_from_list->head;
1613
1614 /* It's possible the list consisted of nothing but whitespace. */
1615 if (parser->lex_from_node == NULL) {
1616 talloc_free (parser->lex_from_list);
1617 parser->lex_from_list = NULL;
1618 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001619}
Carl Worthb20d33c2010-05-20 22:27:07 -07001620
1621static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001622_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1623 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001624{
1625 skip_type_t current = SKIP_NO_SKIP;
1626 skip_node_t *node;
1627
1628 if (parser->skip_stack)
1629 current = parser->skip_stack->type;
1630
1631 node = xtalloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001632 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001633
1634 if (current == SKIP_NO_SKIP) {
1635 if (condition)
1636 node->type = SKIP_NO_SKIP;
1637 else
1638 node->type = SKIP_TO_ELSE;
1639 } else {
1640 node->type = SKIP_TO_ENDIF;
1641 }
1642
1643 node->next = parser->skip_stack;
1644 parser->skip_stack = node;
1645}
1646
1647static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001648_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1649 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001650{
1651 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001652 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001653 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001654 }
1655
1656 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1657 if (condition)
1658 parser->skip_stack->type = SKIP_NO_SKIP;
1659 } else {
1660 parser->skip_stack->type = SKIP_TO_ENDIF;
1661 }
1662}
Carl Worth80dc60b2010-05-25 14:42:00 -07001663
Carl Worthb20d33c2010-05-20 22:27:07 -07001664static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001665_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001666{
1667 skip_node_t *node;
1668
1669 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001670 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001671 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001672 }
1673
1674 node = parser->skip_stack;
1675 parser->skip_stack = node->next;
1676 talloc_free (node);
1677}