blob: 3275496d99acab3b66a4dedbf00ab7b027a0422e [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 Worth3882cf22010-08-17 23:20:58 -070066static int
67_string_list_equal (string_list_t *a, string_list_t *b);
68
Carl Worth5aa7ea02010-05-25 18:39:43 -070069static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070070_argument_list_create (void *ctx);
71
Carl Worth5aa7ea02010-05-25 18:39:43 -070072static void
Carl Worth47252442010-05-19 13:54:37 -070073_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070074
Carl Worth5aa7ea02010-05-25 18:39:43 -070075static int
Carl Worth8f6a8282010-05-14 10:44:19 -070076_argument_list_length (argument_list_t *list);
77
Carl Worth5aa7ea02010-05-25 18:39:43 -070078static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070079_argument_list_member_at (argument_list_t *list, int index);
80
Carl Worth808401f2010-05-25 14:52:43 -070081/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070082static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070083_token_create_str (void *ctx, int type, char *str);
84
Carl Worth5aa7ea02010-05-25 18:39:43 -070085static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070086_token_create_ival (void *ctx, int type, int ival);
87
Carl Worth5aa7ea02010-05-25 18:39:43 -070088static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070089_token_list_create (void *ctx);
90
Carl Worthb1ae61a2010-05-26 08:10:38 -070091/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070092 *
93 * You may want to talloc_unlink any current reference if you no
94 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070095static void
Carl Worth808401f2010-05-25 14:52:43 -070096_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070097
Carl Worth5aa7ea02010-05-25 18:39:43 -070098static void
Carl Worth47252442010-05-19 13:54:37 -070099_token_list_append_list (token_list_t *list, token_list_t *tail);
100
Carl Worth3882cf22010-08-17 23:20:58 -0700101static int
102_token_list_equal_ignoring_space (token_list_t *a, token_list_t *b);
103
Carl Worth22b3ace2010-06-02 15:32:03 -0700104static active_list_t *
105_active_list_push (active_list_t *list,
106 const char *identifier,
107 token_node_t *marker);
108
109static active_list_t *
110_active_list_pop (active_list_t *list);
111
112int
113_active_list_contains (active_list_t *list, const char *identifier);
114
Carl Worth5aa7ea02010-05-25 18:39:43 -0700115static void
Kenneth Graunke16b4eed2010-08-04 16:10:03 -0700116_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list);
117
118static void
Carl Worth681afbc2010-05-28 15:06:02 -0700119_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
120 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700121
Carl Worthaaa9acb2010-05-19 13:28:24 -0700122static void
Carl Worth681afbc2010-05-28 15:06:02 -0700123_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
124 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700125
126static void
Kenneth Graunke07745232010-06-17 12:41:46 -0700127_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
128 int condition);
Carl Worthb20d33c2010-05-20 22:27:07 -0700129
130static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700131_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
132 const char *type, int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700133
Carl Worthb20d33c2010-05-20 22:27:07 -0700134static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700135_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
Carl Worthb20d33c2010-05-20 22:27:07 -0700136
Carl Worth0293b2e2010-05-19 10:05:40 -0700137#define yylex glcpp_parser_lex
138
Carl Worth8f38aff2010-05-19 10:01:29 -0700139static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700140glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700141
Carl Worth8e82fcb2010-05-26 11:15:21 -0700142static void
143glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
144
Eric Anholtd4a04f32010-07-28 16:58:39 -0700145static void
146add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
147
Carl Worth3a37b872010-05-10 11:44:09 -0700148%}
149
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700150%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700151%error-verbose
Carl Worth485f84d2010-08-10 16:58:28 -0700152
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700153%locations
Carl Worth485f84d2010-08-10 16:58:28 -0700154%initial-action {
155 @$.first_line = 1;
156 @$.first_column = 1;
157 @$.last_line = 1;
158 @$.last_column = 1;
159 @$.source = 0;
160}
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700161
Carl Worth0b27b5f2010-05-10 16:16:06 -0700162%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700163%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700164
Carl Worthefef9502010-07-28 11:10:52 -0700165%expect 0
Eric Anholtd4a04f32010-07-28 16:58:39 -0700166%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 -0700167%token PASTE
Eric Anholtd4a04f32010-07-28 16:58:39 -0700168%type <ival> expression INTEGER operator SPACE integer_constant
Carl Worth050e3de2010-05-27 14:36:29 -0700169%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700170%type <string_list> identifier_list
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700171%type <token> preprocessing_token conditional_token
172%type <token_list> pp_tokens replacement_list text_line conditional_tokens
Carl Worth8fed1cd2010-05-26 09:32:12 -0700173%left OR
174%left AND
175%left '|'
176%left '^'
177%left '&'
178%left EQUAL NOT_EQUAL
179%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
180%left LEFT_SHIFT RIGHT_SHIFT
181%left '+' '-'
182%left '*' '/' '%'
183%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700184
185%%
186
Carl Worth33cc4002010-05-12 12:17:10 -0700187input:
Carl Worth3ff81672010-05-25 13:09:03 -0700188 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700189| input line
190;
191
192line:
193 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700194 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700195 }
Carl Worth808401f2010-05-25 14:52:43 -0700196| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700197 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700198 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700199 talloc_free ($1);
200 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700201| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700202| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700203;
204
Carl Worth8e82fcb2010-05-26 11:15:21 -0700205expanded_line:
206 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700207 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700208 }
209| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700210 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700211 }
212;
213
Carl Worth3ff81672010-05-25 13:09:03 -0700214control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700215 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700216 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700217 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700218| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700219 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700220 }
221| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700222 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700223 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700224| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700225 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700226 if (macro) {
Carl Worth61ebc012010-07-19 18:02:12 -0700227 hash_table_remove (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700228 talloc_free (macro);
229 }
230 talloc_free ($2);
231 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700232| HASH_IF conditional_tokens NEWLINE {
Carl Worth48ba0582010-08-11 12:43:44 -0700233 /* Be careful to only evaluate the 'if' expression if
234 * we are not skipping. When we are skipping, we
235 * simply push a new 0-valued 'if' onto the skip
236 * stack.
237 *
238 * This avoids generating diagnostics for invalid
239 * expressions that are being skipped. */
240 if (parser->skip_stack == NULL ||
241 parser->skip_stack->type == SKIP_NO_SKIP)
242 {
243 _glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
244 }
245 else
246 {
Kenneth Graunkef4239872010-08-04 16:24:39 -0700247 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
248 parser->skip_stack->type = SKIP_TO_ENDIF;
Kenneth Graunkef4239872010-08-04 16:24:39 -0700249 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700250 }
Carl Worth253cad32010-08-11 13:59:22 -0700251| HASH_IF NEWLINE {
252 /* #if without an expression is only an error if we
253 * are not skipping */
254 if (parser->skip_stack == NULL ||
255 parser->skip_stack->type == SKIP_NO_SKIP)
256 {
257 glcpp_error(& @1, parser, "#if with no expression");
258 }
259 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
260 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700261| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700262 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700263 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700264 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700265 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700266| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700267 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700268 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700269 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700270 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700271| HASH_ELIF conditional_tokens NEWLINE {
Carl Worth48ba0582010-08-11 12:43:44 -0700272 /* Be careful to only evaluate the 'elif' expression
273 * if we are not skipping. When we are skipping, we
274 * simply change to a 0-valued 'elif' on the skip
275 * stack.
276 *
277 * This avoids generating diagnostics for invalid
278 * expressions that are being skipped. */
279 if (parser->skip_stack &&
280 parser->skip_stack->type == SKIP_TO_ELSE)
281 {
Kenneth Graunkef4239872010-08-04 16:24:39 -0700282 _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
Carl Worth48ba0582010-08-11 12:43:44 -0700283 }
284 else
285 {
286 _glcpp_parser_skip_stack_change_if (parser, & @1,
287 "elif", 0);
288 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700289 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700290| HASH_ELIF NEWLINE {
Carl Worth48ba0582010-08-11 12:43:44 -0700291 /* #elif without an expression is an error unless we
292 * are skipping. */
293 if (parser->skip_stack &&
294 parser->skip_stack->type == SKIP_TO_ELSE)
295 {
Carl Worth624dd582010-08-11 13:50:51 -0700296 glcpp_error(& @1, parser, "#elif with no expression");
Kenneth Graunke332fc472010-06-21 12:20:22 -0700297 }
Carl Worth48ba0582010-08-11 12:43:44 -0700298 else
299 {
300 _glcpp_parser_skip_stack_change_if (parser, & @1,
301 "elif", 0);
302 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
303 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700304 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700305| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700306 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700307 }
308| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700309 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700310 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700311| HASH_VERSION integer_constant NEWLINE {
312 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
313 if (macro) {
314 hash_table_remove (parser->defines, "__VERSION__");
315 talloc_free (macro);
316 }
317 add_builtin_define (parser, "__VERSION__", $2);
Kenneth Graunke6be3a8b2010-08-16 13:42:04 -0700318 glcpp_printf(parser->output, "#version %" PRIiMAX, $2);
Eric Anholtd4a04f32010-07-28 16:58:39 -0700319 }
Carl Worth3ff81672010-05-25 13:09:03 -0700320| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700321;
322
Eric Anholtd4a04f32010-07-28 16:58:39 -0700323integer_constant:
Carl Worth050e3de2010-05-27 14:36:29 -0700324 INTEGER_STRING {
325 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
326 $$ = strtoll ($1 + 2, NULL, 16);
327 } else if ($1[0] == '0') {
328 $$ = strtoll ($1, NULL, 8);
329 } else {
330 $$ = strtoll ($1, NULL, 10);
331 }
332 }
333| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700334 $$ = $1;
335 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700336
337expression:
338 integer_constant
Carl Worth8fed1cd2010-05-26 09:32:12 -0700339| expression OR expression {
340 $$ = $1 || $3;
341 }
342| expression AND expression {
343 $$ = $1 && $3;
344 }
345| expression '|' expression {
346 $$ = $1 | $3;
347 }
348| expression '^' expression {
349 $$ = $1 ^ $3;
350 }
351| expression '&' expression {
352 $$ = $1 & $3;
353 }
354| expression NOT_EQUAL expression {
355 $$ = $1 != $3;
356 }
357| expression EQUAL expression {
358 $$ = $1 == $3;
359 }
360| expression GREATER_OR_EQUAL expression {
361 $$ = $1 >= $3;
362 }
363| expression LESS_OR_EQUAL expression {
364 $$ = $1 <= $3;
365 }
366| expression '>' expression {
367 $$ = $1 > $3;
368 }
369| expression '<' expression {
370 $$ = $1 < $3;
371 }
372| expression RIGHT_SHIFT expression {
373 $$ = $1 >> $3;
374 }
375| expression LEFT_SHIFT 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 '/' expression {
388 $$ = $1 / $3;
389 }
390| expression '*' expression {
391 $$ = $1 * $3;
392 }
393| '!' expression %prec UNARY {
394 $$ = ! $2;
395 }
396| '~' expression %prec UNARY {
397 $$ = ~ $2;
398 }
399| '-' expression %prec UNARY {
400 $$ = - $2;
401 }
402| '+' expression %prec UNARY {
403 $$ = + $2;
404 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700405| '(' expression ')' {
406 $$ = $2;
407 }
408;
409
Carl Worth3ff81672010-05-25 13:09:03 -0700410identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700411 IDENTIFIER {
412 $$ = _string_list_create (parser);
413 _string_list_append_item ($$, $1);
414 talloc_steal ($$, $1);
415 }
416| identifier_list ',' IDENTIFIER {
417 $$ = $1;
418 _string_list_append_item ($$, $3);
419 talloc_steal ($$, $3);
420 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700421;
422
Carl Worth3ff81672010-05-25 13:09:03 -0700423text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700424 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700425| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700426;
427
Carl Worth3ff81672010-05-25 13:09:03 -0700428non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700429 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700430 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700431 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700432;
433
Carl Worthaaa9acb2010-05-19 13:28:24 -0700434replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700435 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700436| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700437;
438
Kenneth Graunke65875742010-06-18 20:08:15 -0700439junk:
440 /* empty */
441| pp_tokens {
442 glcpp_warning(&@1, parser, "extra tokens at end of directive");
443 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700444;
445
446conditional_token:
447 /* Handle "defined" operator */
448 DEFINED IDENTIFIER {
449 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
450 $$ = _token_create_ival (parser, INTEGER, v);
451 }
452| DEFINED '(' IDENTIFIER ')' {
453 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
454 $$ = _token_create_ival (parser, INTEGER, v);
455 }
456| preprocessing_token
457;
458
459conditional_tokens:
460 /* Exactly the same as pp_tokens, but using conditional_token */
461 conditional_token {
462 parser->space_tokens = 1;
463 $$ = _token_list_create (parser);
464 _token_list_append ($$, $1);
465 talloc_unlink (parser, $1);
466 }
467| conditional_tokens conditional_token {
468 $$ = $1;
469 _token_list_append ($$, $2);
470 talloc_unlink (parser, $2);
471 }
472;
Kenneth Graunke65875742010-06-18 20:08:15 -0700473
Carl Worthaaa9acb2010-05-19 13:28:24 -0700474pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700475 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700476 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700477 $$ = _token_list_create (parser);
478 _token_list_append ($$, $1);
479 talloc_unlink (parser, $1);
480 }
481| pp_tokens preprocessing_token {
482 $$ = $1;
483 _token_list_append ($$, $2);
484 talloc_unlink (parser, $2);
485 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700486;
487
Carl Worth3ff81672010-05-25 13:09:03 -0700488preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700489 IDENTIFIER {
490 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700491 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700492 }
Carl Worth050e3de2010-05-27 14:36:29 -0700493| INTEGER_STRING {
494 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700495 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700496 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700497| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700498 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700499 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700500 }
501| OTHER {
502 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700503 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700504 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700505| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700506 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700507 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700508 }
Carl Worth3ff81672010-05-25 13:09:03 -0700509;
510
Carl Worth8e82fcb2010-05-26 11:15:21 -0700511operator:
Carl Worth808401f2010-05-25 14:52:43 -0700512 '[' { $$ = '['; }
513| ']' { $$ = ']'; }
514| '(' { $$ = '('; }
515| ')' { $$ = ')'; }
516| '{' { $$ = '{'; }
517| '}' { $$ = '}'; }
518| '.' { $$ = '.'; }
519| '&' { $$ = '&'; }
520| '*' { $$ = '*'; }
521| '+' { $$ = '+'; }
522| '-' { $$ = '-'; }
523| '~' { $$ = '~'; }
524| '!' { $$ = '!'; }
525| '/' { $$ = '/'; }
526| '%' { $$ = '%'; }
527| LEFT_SHIFT { $$ = LEFT_SHIFT; }
528| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
529| '<' { $$ = '<'; }
530| '>' { $$ = '>'; }
531| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
532| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
533| EQUAL { $$ = EQUAL; }
534| NOT_EQUAL { $$ = NOT_EQUAL; }
535| '^' { $$ = '^'; }
536| '|' { $$ = '|'; }
537| AND { $$ = AND; }
538| OR { $$ = OR; }
539| ';' { $$ = ';'; }
540| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700541| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700542| PASTE { $$ = PASTE; }
Carl Worth3ff81672010-05-25 13:09:03 -0700543;
544
Carl Worth33cc4002010-05-12 12:17:10 -0700545%%
546
Carl Worth610053b2010-05-14 10:05:11 -0700547string_list_t *
548_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700549{
Carl Worth610053b2010-05-14 10:05:11 -0700550 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700551
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700552 list = talloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700553 list->head = NULL;
554 list->tail = NULL;
555
556 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700557}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700558
Carl Worth33cc4002010-05-12 12:17:10 -0700559void
Carl Worth610053b2010-05-14 10:05:11 -0700560_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700561{
Carl Worth610053b2010-05-14 10:05:11 -0700562 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700563
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700564 node = talloc (list, string_node_t);
565 node->str = talloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700566
Carl Worth33cc4002010-05-12 12:17:10 -0700567 node->next = NULL;
568
569 if (list->head == NULL) {
570 list->head = node;
571 } else {
572 list->tail->next = node;
573 }
574
575 list->tail = node;
576}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700577
578int
Carl Worth610053b2010-05-14 10:05:11 -0700579_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700580{
Carl Worth610053b2010-05-14 10:05:11 -0700581 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700582 int i;
583
584 if (list == NULL)
585 return 0;
586
587 for (i = 0, node = list->head; node; i++, node = node->next) {
588 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700589 if (index)
590 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700591 return 1;
592 }
593 }
594
595 return 0;
596}
597
598int
Carl Worth610053b2010-05-14 10:05:11 -0700599_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700600{
601 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700602 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700603
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 Worth3882cf22010-08-17 23:20:58 -0700613int
614_string_list_equal (string_list_t *a, string_list_t *b)
615{
616 string_node_t *node_a, *node_b;
617
618 if (a == NULL && b == NULL)
619 return 1;
620
621 if (a == NULL || b == NULL)
622 return 0;
623
624 for (node_a = a->head, node_b = b->head;
625 node_a && node_b;
626 node_a = node_a->next, node_b = node_b->next)
627 {
628 if (strcmp (node_a->str, node_b->str))
629 return 0;
630 }
631
632 /* Catch the case of lists being different lengths, (which
633 * would cause the loop above to terminate after the shorter
634 * list). */
635 return node_a == node_b;
636}
637
Carl Worth8f6a8282010-05-14 10:44:19 -0700638argument_list_t *
639_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700640{
Carl Worth8f6a8282010-05-14 10:44:19 -0700641 argument_list_t *list;
642
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700643 list = talloc (ctx, argument_list_t);
Carl Worth8f6a8282010-05-14 10:44:19 -0700644 list->head = NULL;
645 list->tail = NULL;
646
647 return list;
648}
649
650void
Carl Worth47252442010-05-19 13:54:37 -0700651_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700652{
653 argument_node_t *node;
654
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700655 node = talloc (list, argument_node_t);
Carl Worth8f6a8282010-05-14 10:44:19 -0700656 node->argument = argument;
657
658 node->next = NULL;
659
660 if (list->head == NULL) {
661 list->head = node;
662 } else {
663 list->tail->next = node;
664 }
665
666 list->tail = node;
667}
668
669int
670_argument_list_length (argument_list_t *list)
671{
672 int length = 0;
673 argument_node_t *node;
674
675 if (list == NULL)
676 return 0;
677
678 for (node = list->head; node; node = node->next)
679 length++;
680
681 return length;
682}
683
Carl Worth47252442010-05-19 13:54:37 -0700684token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700685_argument_list_member_at (argument_list_t *list, int index)
686{
687 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700688 int i;
689
690 if (list == NULL)
691 return NULL;
692
693 node = list->head;
694 for (i = 0; i < index; i++) {
695 node = node->next;
696 if (node == NULL)
697 break;
698 }
699
700 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700701 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700702
703 return NULL;
704}
Carl Worth47252442010-05-19 13:54:37 -0700705
Carl Worth808401f2010-05-25 14:52:43 -0700706/* Note: This function talloc_steal()s the str pointer. */
707token_t *
708_token_create_str (void *ctx, int type, char *str)
709{
710 token_t *token;
711
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700712 token = talloc (ctx, token_t);
Carl Worth808401f2010-05-25 14:52:43 -0700713 token->type = type;
714 token->value.str = talloc_steal (token, str);
715
716 return token;
717}
718
719token_t *
720_token_create_ival (void *ctx, int type, int ival)
721{
722 token_t *token;
723
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700724 token = talloc (ctx, token_t);
Carl Worth808401f2010-05-25 14:52:43 -0700725 token->type = type;
726 token->value.ival = ival;
727
728 return token;
729}
730
Carl Worth47252442010-05-19 13:54:37 -0700731token_list_t *
732_token_list_create (void *ctx)
733{
734 token_list_t *list;
735
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700736 list = talloc (ctx, token_list_t);
Carl Worth47252442010-05-19 13:54:37 -0700737 list->head = NULL;
738 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700739 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700740
741 return list;
742}
743
744void
Carl Worth808401f2010-05-25 14:52:43 -0700745_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700746{
747 token_node_t *node;
748
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700749 node = talloc (list, token_node_t);
750 node->token = talloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700751
752 node->next = NULL;
753
754 if (list->head == NULL) {
755 list->head = node;
756 } else {
757 list->tail->next = node;
758 }
759
760 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700761 if (token->type != SPACE)
762 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700763}
764
765void
766_token_list_append_list (token_list_t *list, token_list_t *tail)
767{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700768 if (tail == NULL || tail->head == NULL)
769 return;
770
Carl Worth47252442010-05-19 13:54:37 -0700771 if (list->head == NULL) {
772 list->head = tail->head;
773 } else {
774 list->tail->next = tail->head;
775 }
776
777 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700778 list->non_space_tail = tail->non_space_tail;
779}
780
Carl Worthd80dcaf2010-07-20 15:55:21 -0700781static token_list_t *
Carl Worth681afbc2010-05-28 15:06:02 -0700782_token_list_copy (void *ctx, token_list_t *other)
783{
784 token_list_t *copy;
785 token_node_t *node;
786
787 if (other == NULL)
788 return NULL;
789
790 copy = _token_list_create (ctx);
791 for (node = other->head; node; node = node->next)
792 _token_list_append (copy, node->token);
793
794 return copy;
795}
796
Carl Worthd80dcaf2010-07-20 15:55:21 -0700797static void
Carl Worth10ae4382010-05-25 20:35:01 -0700798_token_list_trim_trailing_space (token_list_t *list)
799{
800 token_node_t *tail, *next;
801
802 if (list->non_space_tail) {
803 tail = list->non_space_tail->next;
804 list->non_space_tail->next = NULL;
805 list->tail = list->non_space_tail;
806
807 while (tail) {
808 next = tail->next;
809 talloc_free (tail);
810 tail = next;
811 }
812 }
Carl Worth47252442010-05-19 13:54:37 -0700813}
Carl Worth80dc60b2010-05-25 14:42:00 -0700814
Carl Worth3882cf22010-08-17 23:20:58 -0700815int
816_token_list_equal_ignoring_space (token_list_t *a, token_list_t *b)
817{
818 token_node_t *node_a, *node_b;
819
820 node_a = a->head;
821 node_b = b->head;
822
823 while (1)
824 {
825 if (node_a == NULL && node_b == NULL)
826 break;
827
828 if (node_a == NULL || node_b == NULL)
829 return 0;
830
831 if (node_a->token->type == SPACE) {
832 node_a = node_a->next;
833 continue;
834 }
835
836 if (node_b->token->type == SPACE) {
837 node_b = node_b->next;
838 continue;
839 }
840
841 if (node_a->token->type != node_b->token->type)
842 return 0;
843
844 switch (node_a->token->type) {
845 case INTEGER:
846 if (node_a->token->value.ival !=
847 node_b->token->value.ival)
848 {
849 return 0;
850 }
851 break;
852 case IDENTIFIER:
853 case INTEGER_STRING:
854 case OTHER:
855 if (strcmp (node_a->token->value.str,
856 node_b->token->value.str))
857 {
858 return 0;
859 }
860 break;
861 }
862
863 node_a = node_a->next;
864 node_b = node_b->next;
865 }
866
867 return 1;
868}
869
Carl Worth0197e9b2010-05-26 08:05:19 -0700870static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700871_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700872{
873 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700874 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700875 return;
876 }
877
878 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700879 case INTEGER:
Eric Anholt8605c292010-07-28 16:53:51 -0700880 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700881 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700882 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700883 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700884 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700885 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700886 break;
887 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700888 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700889 break;
890 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700891 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700892 break;
893 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700894 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700895 break;
896 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700897 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700898 break;
899 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700900 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700901 break;
902 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700903 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700904 break;
905 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700906 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700907 break;
908 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700909 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700910 break;
911 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700912 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700913 break;
914 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700915 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700916 break;
Carl Worthdd749002010-05-27 10:12:33 -0700917 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700918 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700919 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700920 case PLACEHOLDER:
921 /* Nothing to print. */
922 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700923 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700924 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700925 break;
926 }
927}
928
Carl Worthb06096e2010-05-29 05:54:19 -0700929/* Return a new token (talloc()ed off of 'token') formed by pasting
930 * 'token' and 'other'. Note that this function may return 'token' or
931 * 'other' directly rather than allocating anything new.
932 *
933 * Caution: Only very cursory error-checking is performed to see if
934 * the final result is a valid single token. */
935static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700936_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700937{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700938 token_t *combined = NULL;
939
Carl Worth85b50e82010-05-27 14:01:18 -0700940 /* Pasting a placeholder onto anything makes no change. */
941 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700942 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700943
Carl Worthb06096e2010-05-29 05:54:19 -0700944 /* When 'token' is a placeholder, just return 'other'. */
945 if (token->type == PLACEHOLDER)
946 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700947
Carl Worthad0dee62010-05-26 09:04:50 -0700948 /* A very few single-character punctuators can be combined
949 * with another to form a multi-character punctuator. */
950 switch (token->type) {
951 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700952 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700953 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700954 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700955 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700956 break;
957 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700958 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700959 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700960 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700961 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700962 break;
963 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700964 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700965 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700966 break;
967 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700968 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700969 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700970 break;
971 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700972 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700973 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700974 break;
975 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700976 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700977 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700978 break;
979 }
980
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700981 if (combined != NULL) {
982 /* Inherit the location from the first token */
983 combined->location = token->location;
984 return combined;
985 }
986
Carl Worthad0dee62010-05-26 09:04:50 -0700987 /* Two string-valued tokens can usually just be mashed
988 * together.
989 *
Carl Worth050e3de2010-05-27 14:36:29 -0700990 * XXX: This isn't actually legitimate. Several things here
991 * should result in a diagnostic since the result cannot be a
992 * valid, single pre-processing token. For example, pasting
993 * "123" and "abc" is not legal, but we don't catch that
994 * here. */
995 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
996 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700997 {
Carl Worthb06096e2010-05-29 05:54:19 -0700998 char *str;
999
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001000 str = talloc_asprintf (token, "%s%s", token->value.str,
1001 other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -07001002 combined = _token_create_str (token, token->type, str);
1003 combined->location = token->location;
1004 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -07001005 }
1006
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001007 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -07001008 glcpp_print (parser->info_log, "Pasting \"");
1009 _token_print (&parser->info_log, token);
1010 glcpp_print (parser->info_log, "\" and \"");
1011 _token_print (&parser->info_log, other);
1012 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -07001013
1014 return token;
Carl Worthad0dee62010-05-26 09:04:50 -07001015}
1016
Carl Worth0197e9b2010-05-26 08:05:19 -07001017static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001018_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -07001019{
1020 token_node_t *node;
1021
1022 if (list == NULL)
1023 return;
1024
1025 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001026 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -07001027}
1028
Carl Worth3a37b872010-05-10 11:44:09 -07001029void
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001030yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -07001031{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -07001032 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -07001033}
Carl Worth0b27b5f2010-05-10 16:16:06 -07001034
Eric Anholtd4a04f32010-07-28 16:58:39 -07001035static void add_builtin_define(glcpp_parser_t *parser,
1036 const char *name, int value)
1037{
1038 token_t *tok;
1039 token_list_t *list;
1040
1041 tok = _token_create_ival (parser, INTEGER, value);
1042
1043 list = _token_list_create(parser);
1044 _token_list_append(list, tok);
1045 _define_object_macro(parser, NULL, name, list);
1046
1047 talloc_unlink(parser, tok);
1048}
1049
Carl Worth33cc4002010-05-12 12:17:10 -07001050glcpp_parser_t *
Ian Romanick06143ea2010-06-30 16:27:22 -07001051glcpp_parser_create (const struct gl_extensions *extensions)
Carl Worth0b27b5f2010-05-10 16:16:06 -07001052{
Carl Worth33cc4002010-05-12 12:17:10 -07001053 glcpp_parser_t *parser;
Eric Anholtd4a04f32010-07-28 16:58:39 -07001054 int language_version;
Carl Worth33cc4002010-05-12 12:17:10 -07001055
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001056 parser = talloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -07001057
Carl Worth8f38aff2010-05-19 10:01:29 -07001058 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -07001059 parser->defines = hash_table_ctor (32, hash_table_string_hash,
1060 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -07001061 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -07001062 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -07001063 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -07001064 parser->newline_as_space = 0;
1065 parser->in_control_line = 0;
1066 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -07001067
Carl Worthb20d33c2010-05-20 22:27:07 -07001068 parser->skip_stack = NULL;
1069
Carl Worth8e82fcb2010-05-26 11:15:21 -07001070 parser->lex_from_list = NULL;
1071 parser->lex_from_node = NULL;
1072
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001073 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -07001074 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke1b85c462010-06-21 13:55:12 -07001075 parser->error = 0;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001076
Ian Romanick2d122362010-06-30 16:03:19 -07001077 /* Add pre-defined macros. */
Eric Anholtd4a04f32010-07-28 16:58:39 -07001078 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
1079 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
Ian Romanick2d122362010-06-30 16:03:19 -07001080
Eric Anholtd4a04f32010-07-28 16:58:39 -07001081 if (extensions != NULL) {
1082 if (extensions->EXT_texture_array) {
1083 add_builtin_define(parser, "GL_EXT_texture_array", 1);
1084 }
Ian Romanick2d122362010-06-30 16:03:19 -07001085
Eric Anholtd4a04f32010-07-28 16:58:39 -07001086 if (extensions->ARB_fragment_coord_conventions)
1087 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
1088 1);
Ian Romanick06143ea2010-06-30 16:27:22 -07001089 }
1090
Eric Anholtd4a04f32010-07-28 16:58:39 -07001091 language_version = 110;
Eric Anholtd4a04f32010-07-28 16:58:39 -07001092 add_builtin_define(parser, "__VERSION__", language_version);
Ian Romanick2d122362010-06-30 16:03:19 -07001093
Carl Worth33cc4002010-05-12 12:17:10 -07001094 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -07001095}
1096
1097int
1098glcpp_parser_parse (glcpp_parser_t *parser)
1099{
1100 return yyparse (parser);
1101}
1102
1103void
Carl Worth33cc4002010-05-12 12:17:10 -07001104glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -07001105{
Carl Worth8f38aff2010-05-19 10:01:29 -07001106 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -07001107 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -07001108 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -07001109}
Carl Worthc6d5af32010-05-11 12:30:09 -07001110
Carl Worthb1854fd2010-05-25 16:28:26 -07001111typedef enum function_status
1112{
1113 FUNCTION_STATUS_SUCCESS,
1114 FUNCTION_NOT_A_FUNCTION,
1115 FUNCTION_UNBALANCED_PARENTHESES
1116} function_status_t;
1117
1118/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -07001119 * balanced set of parentheses.
1120 *
1121 * When called, 'node' should be the opening-parenthesis token, (or
1122 * perhaps preceeding SPACE tokens). Upon successful return *last will
1123 * be the last consumed node, (corresponding to the closing right
1124 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -07001125 *
1126 * Return values:
1127 *
1128 * FUNCTION_STATUS_SUCCESS:
1129 *
1130 * Successfully parsed a set of function arguments.
1131 *
1132 * FUNCTION_NOT_A_FUNCTION:
1133 *
1134 * Macro name not followed by a '('. This is not an error, but
1135 * simply that the macro name should be treated as a non-macro.
1136 *
Carl Worth14c98a52010-06-02 15:49:54 -07001137 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -07001138 *
1139 * Macro name is not followed by a balanced set of parentheses.
1140 */
1141static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -07001142_arguments_parse (argument_list_t *arguments,
1143 token_node_t *node,
1144 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001145{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001146 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001147 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001148
Carl Worthb1854fd2010-05-25 16:28:26 -07001149 node = node->next;
1150
1151 /* Ignore whitespace before first parenthesis. */
1152 while (node && node->token->type == SPACE)
1153 node = node->next;
1154
1155 if (node == NULL || node->token->type != '(')
1156 return FUNCTION_NOT_A_FUNCTION;
1157
Carl Worth652fa272010-05-25 17:45:22 -07001158 node = node->next;
1159
Carl Wortha19297b2010-05-27 13:29:19 -07001160 argument = _token_list_create (arguments);
1161 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001162
Carl Worth681afbc2010-05-28 15:06:02 -07001163 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001164 if (node->token->type == '(')
1165 {
1166 paren_count++;
1167 }
1168 else if (node->token->type == ')')
1169 {
1170 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001171 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001172 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001173 }
Carl Worth652fa272010-05-25 17:45:22 -07001174
1175 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001176 paren_count == 1)
1177 {
Carl Wortha19297b2010-05-27 13:29:19 -07001178 _token_list_trim_trailing_space (argument);
1179 argument = _token_list_create (arguments);
1180 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001181 }
1182 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001183 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001184 /* Don't treat initial whitespace as
1185 * part of the arguement. */
1186 if (node->token->type == SPACE)
1187 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001188 }
1189 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001190 }
Carl Worthc7581c22010-05-25 17:41:07 -07001191 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001192
Carl Worth681afbc2010-05-28 15:06:02 -07001193 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001194 return FUNCTION_UNBALANCED_PARENTHESES;
1195
Carl Worth681afbc2010-05-28 15:06:02 -07001196 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001197
1198 return FUNCTION_STATUS_SUCCESS;
1199}
1200
Carl Worthe1acbfc2010-07-20 16:44:03 -07001201static token_list_t *
1202_token_list_create_with_one_space (void *ctx)
1203{
1204 token_list_t *list;
1205 token_t *space;
1206
1207 list = _token_list_create (ctx);
1208 space = _token_create_ival (list, SPACE, SPACE);
1209 _token_list_append (list, space);
1210
1211 return list;
1212}
1213
Kenneth Graunke16b4eed2010-08-04 16:10:03 -07001214static void
1215_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1216{
1217 token_list_t *expanded;
1218 token_t *token;
1219
1220 expanded = _token_list_create (parser);
1221 token = _token_create_ival (parser, type, type);
1222 _token_list_append (expanded, token);
1223 _glcpp_parser_expand_token_list (parser, list);
1224 _token_list_append_list (expanded, list);
1225 glcpp_parser_lex_from (parser, expanded);
1226}
1227
Carl Worth681afbc2010-05-28 15:06:02 -07001228/* This is a helper function that's essentially part of the
1229 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1230 * except for by that function.
1231 *
1232 * Returns NULL if node is a simple token with no expansion, (that is,
1233 * although 'node' corresponds to an identifier defined as a
1234 * function-like macro, it is not followed with a parenthesized
1235 * argument list).
1236 *
1237 * Compute the complete expansion of node (which is a function-like
1238 * macro) and subsequent nodes which are arguments.
1239 *
1240 * Returns the token list that results from the expansion and sets
1241 * *last to the last node in the list that was consumed by the
Carl Worthfbe42402010-07-22 16:36:04 -07001242 * expansion. Specifically, *last will be set as follows: as the
Carl Worth681afbc2010-05-28 15:06:02 -07001243 * token of the closing right parenthesis.
1244 */
1245static token_list_t *
1246_glcpp_parser_expand_function (glcpp_parser_t *parser,
1247 token_node_t *node,
1248 token_node_t **last)
1249
Carl Worthb1854fd2010-05-25 16:28:26 -07001250{
1251 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001252 const char *identifier;
1253 argument_list_t *arguments;
1254 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001255 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001256 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001257
Carl Worthb1854fd2010-05-25 16:28:26 -07001258 identifier = node->token->value.str;
1259
1260 macro = hash_table_find (parser->defines, identifier);
1261
1262 assert (macro->is_function);
1263
Carl Worth9ce18cf2010-05-25 17:32:21 -07001264 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001265 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001266
1267 switch (status) {
1268 case FUNCTION_STATUS_SUCCESS:
1269 break;
1270 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001271 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001272 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001273 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001274 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001275 }
1276
Carl Worthe1acbfc2010-07-20 16:44:03 -07001277 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth9ce18cf2010-05-25 17:32:21 -07001278 if (macro->replacements == NULL) {
1279 talloc_free (arguments);
Carl Worthe1acbfc2010-07-20 16:44:03 -07001280 return _token_list_create_with_one_space (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001281 }
1282
Carl Wortha19297b2010-05-27 13:29:19 -07001283 if (! ((_argument_list_length (arguments) ==
1284 _string_list_length (macro->parameters)) ||
1285 (_string_list_length (macro->parameters) == 0 &&
1286 _argument_list_length (arguments) == 1 &&
1287 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001288 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001289 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001290 "Error: macro %s invoked with %d arguments (expected %d)\n",
1291 identifier,
1292 _argument_list_length (arguments),
1293 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001294 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001295 }
1296
Carl Worth0197e9b2010-05-26 08:05:19 -07001297 /* Perform argument substitution on the replacement list. */
1298 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001299
Carl Worthce540f22010-05-26 08:25:44 -07001300 for (node = macro->replacements->head; node; node = node->next)
1301 {
1302 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001303 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001304 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001305 &parameter_index))
1306 {
1307 token_list_t *argument;
1308 argument = _argument_list_member_at (arguments,
1309 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001310 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001311 * tokens, or append a placeholder token for
1312 * an empty argument. */
1313 if (argument->head) {
Carl Worthfbe42402010-07-22 16:36:04 -07001314 token_list_t *expanded_argument;
1315 expanded_argument = _token_list_copy (parser,
1316 argument);
Carl Worth681afbc2010-05-28 15:06:02 -07001317 _glcpp_parser_expand_token_list (parser,
Carl Worthfbe42402010-07-22 16:36:04 -07001318 expanded_argument);
1319 _token_list_append_list (substituted,
1320 expanded_argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001321 } else {
1322 token_t *new_token;
1323
1324 new_token = _token_create_ival (substituted,
1325 PLACEHOLDER,
1326 PLACEHOLDER);
1327 _token_list_append (substituted, new_token);
1328 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001329 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001330 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001331 }
1332 }
1333
Carl Worthad0dee62010-05-26 09:04:50 -07001334 /* After argument substitution, and before further expansion
1335 * below, implement token pasting. */
1336
Carl Worthb06096e2010-05-29 05:54:19 -07001337 _token_list_trim_trailing_space (substituted);
1338
Carl Worthad0dee62010-05-26 09:04:50 -07001339 node = substituted->head;
1340 while (node)
1341 {
1342 token_node_t *next_non_space;
1343
1344 /* Look ahead for a PASTE token, skipping space. */
1345 next_non_space = node->next;
1346 while (next_non_space && next_non_space->token->type == SPACE)
1347 next_non_space = next_non_space->next;
1348
1349 if (next_non_space == NULL)
1350 break;
1351
1352 if (next_non_space->token->type != PASTE) {
1353 node = next_non_space;
1354 continue;
1355 }
1356
1357 /* Now find the next non-space token after the PASTE. */
1358 next_non_space = next_non_space->next;
1359 while (next_non_space && next_non_space->token->type == SPACE)
1360 next_non_space = next_non_space->next;
1361
1362 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001363 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001364 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001365 }
1366
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001367 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001368 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001369 if (next_non_space == substituted->tail)
1370 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001371
1372 node = node->next;
1373 }
1374
Carl Worthb06096e2010-05-29 05:54:19 -07001375 substituted->non_space_tail = substituted->tail;
1376
Carl Worth681afbc2010-05-28 15:06:02 -07001377 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001378}
1379
Carl Worth681afbc2010-05-28 15:06:02 -07001380/* Compute the complete expansion of node, (and subsequent nodes after
1381 * 'node' in the case that 'node' is a function-like macro and
1382 * subsequent nodes are arguments).
1383 *
1384 * Returns NULL if node is a simple token with no expansion.
1385 *
1386 * Otherwise, returns the token list that results from the expansion
1387 * and sets *last to the last node in the list that was consumed by
Carl Worthe1acbfc2010-07-20 16:44:03 -07001388 * the expansion. Specifically, *last will be set as follows:
Carl Worth681afbc2010-05-28 15:06:02 -07001389 *
1390 * As 'node' in the case of object-like macro expansion.
1391 *
1392 * As the token of the closing right parenthesis in the case of
1393 * function-like macro expansion.
1394 */
1395static token_list_t *
1396_glcpp_parser_expand_node (glcpp_parser_t *parser,
1397 token_node_t *node,
1398 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001399{
Carl Worth681afbc2010-05-28 15:06:02 -07001400 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001401 const char *identifier;
1402 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001403
1404 /* We only expand identifiers */
1405 if (token->type != IDENTIFIER) {
1406 /* We change any COMMA into a COMMA_FINAL to prevent
1407 * it being mistaken for an argument separator
1408 * later. */
1409 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001410 token->type = COMMA_FINAL;
1411 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001412 }
Carl Worth681afbc2010-05-28 15:06:02 -07001413
1414 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001415 }
1416
1417 /* Look up this identifier in the hash table. */
1418 identifier = token->value.str;
1419 macro = hash_table_find (parser->defines, identifier);
1420
Carl Worth681afbc2010-05-28 15:06:02 -07001421 /* Not a macro, so no expansion needed. */
1422 if (macro == NULL)
1423 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001424
1425 /* Finally, don't expand this macro if we're already actively
1426 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001427 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001428 /* We change the token type here from IDENTIFIER to
1429 * OTHER to prevent any future expansion of this
1430 * unexpanded token. */
1431 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001432 token_list_t *expansion;
1433 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001434
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001435 str = talloc_strdup (parser, token->value.str);
Carl Worth681afbc2010-05-28 15:06:02 -07001436 final = _token_create_str (parser, OTHER, str);
1437 expansion = _token_list_create (parser);
1438 _token_list_append (expansion, final);
1439 *last = node;
1440 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001441 }
1442
Carl Worth681afbc2010-05-28 15:06:02 -07001443 if (! macro->is_function)
1444 {
1445 *last = node;
1446
Carl Worthe1acbfc2010-07-20 16:44:03 -07001447 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth681afbc2010-05-28 15:06:02 -07001448 if (macro->replacements == NULL)
Carl Worthe1acbfc2010-07-20 16:44:03 -07001449 return _token_list_create_with_one_space (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001450
Carl Worth22b3ace2010-06-02 15:32:03 -07001451 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001452 }
Carl Worth681afbc2010-05-28 15:06:02 -07001453
1454 return _glcpp_parser_expand_function (parser, node, last);
1455}
1456
Carl Worth22b3ace2010-06-02 15:32:03 -07001457/* Push a new identifier onto the active list, returning the new list.
1458 *
1459 * Here, 'marker' is the token node that appears in the list after the
1460 * expansion of 'identifier'. That is, when the list iterator begins
1461 * examinging 'marker', then it is time to pop this node from the
1462 * active stack.
1463 */
1464active_list_t *
1465_active_list_push (active_list_t *list,
1466 const char *identifier,
1467 token_node_t *marker)
1468{
1469 active_list_t *node;
1470
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001471 node = talloc (list, active_list_t);
1472 node->identifier = talloc_strdup (node, identifier);
Carl Worth22b3ace2010-06-02 15:32:03 -07001473 node->marker = marker;
1474 node->next = list;
1475
1476 return node;
1477}
1478
1479active_list_t *
1480_active_list_pop (active_list_t *list)
1481{
1482 active_list_t *node = list;
1483
1484 if (node == NULL)
1485 return NULL;
1486
1487 node = list->next;
1488 talloc_free (list);
1489
1490 return node;
1491}
1492
1493int
1494_active_list_contains (active_list_t *list, const char *identifier)
1495{
1496 active_list_t *node;
1497
1498 if (list == NULL)
1499 return 0;
1500
1501 for (node = list; node; node = node->next)
1502 if (strcmp (node->identifier, identifier) == 0)
1503 return 1;
1504
1505 return 0;
1506}
1507
Carl Worth681afbc2010-05-28 15:06:02 -07001508/* Walk over the token list replacing nodes with their expansion.
1509 * Whenever nodes are expanded the walking will walk over the new
1510 * nodes, continuing to expand as necessary. The results are placed in
1511 * 'list' itself;
1512 */
1513static void
1514_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1515 token_list_t *list)
1516{
1517 token_node_t *node_prev;
Carl Worthc42e6402010-06-22 15:51:34 -07001518 token_node_t *node, *last = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001519 token_list_t *expansion;
1520
1521 if (list == NULL)
1522 return;
1523
1524 _token_list_trim_trailing_space (list);
1525
1526 node_prev = NULL;
1527 node = list->head;
1528
1529 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001530
1531 while (parser->active && parser->active->marker == node)
1532 parser->active = _active_list_pop (parser->active);
1533
Carl Worth681afbc2010-05-28 15:06:02 -07001534 /* Find the expansion for node, which will replace all
1535 * nodes from node to last, inclusive. */
1536 expansion = _glcpp_parser_expand_node (parser, node, &last);
1537 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001538 token_node_t *n;
1539
1540 for (n = node; n != last->next; n = n->next)
1541 while (parser->active &&
1542 parser->active->marker == n)
1543 {
1544 parser->active = _active_list_pop (parser->active);
1545 }
1546
1547 parser->active = _active_list_push (parser->active,
1548 node->token->value.str,
1549 last->next);
1550
Carl Worth681afbc2010-05-28 15:06:02 -07001551 /* Splice expansion into list, supporting a
1552 * simple deletion if the expansion is
1553 * empty. */
1554 if (expansion->head) {
1555 if (node_prev)
1556 node_prev->next = expansion->head;
1557 else
1558 list->head = expansion->head;
1559 expansion->tail->next = last->next;
1560 if (last == list->tail)
1561 list->tail = expansion->tail;
1562 } else {
1563 if (node_prev)
1564 node_prev->next = last->next;
1565 else
1566 list->head = last->next;
1567 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001568 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001569 }
1570 } else {
1571 node_prev = node;
1572 }
1573 node = node_prev ? node_prev->next : list->head;
1574 }
1575
Carl Worth22b3ace2010-06-02 15:32:03 -07001576 while (parser->active)
1577 parser->active = _active_list_pop (parser->active);
1578
Carl Worth681afbc2010-05-28 15:06:02 -07001579 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001580}
1581
Carl Worthae6517f2010-05-25 15:24:59 -07001582void
1583_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1584 token_list_t *list)
1585{
Carl Worthae6517f2010-05-25 15:24:59 -07001586 if (list == NULL)
1587 return;
1588
Carl Worth681afbc2010-05-28 15:06:02 -07001589 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001590
Carl Worth681afbc2010-05-28 15:06:02 -07001591 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001592
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001593 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001594}
1595
Carl Worthd80dcaf2010-07-20 15:55:21 -07001596static void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001597_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1598 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001599{
1600 /* According to the GLSL specification, macro names starting with "__"
1601 * or "GL_" are reserved for future use. So, don't allow them.
1602 */
1603 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001604 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001605 }
1606 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001607 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001608 }
1609}
1610
Carl Worth3882cf22010-08-17 23:20:58 -07001611static int
1612_macro_equal (macro_t *a, macro_t *b)
1613{
1614 if (a->is_function != b->is_function)
1615 return 0;
1616
1617 if (a->is_function) {
1618 if (! _string_list_equal (a->parameters, b->parameters))
1619 return 0;
1620 }
1621
1622 return _token_list_equal_ignoring_space (a->replacements,
1623 b->replacements);
1624}
1625
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001626void
Carl Worthfcbbb462010-05-13 09:36:23 -07001627_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001628 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001629 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001630 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001631{
Carl Worth3882cf22010-08-17 23:20:58 -07001632 macro_t *macro, *previous;
Carl Worthfcbbb462010-05-13 09:36:23 -07001633
Ian Romanick2d122362010-06-30 16:03:19 -07001634 if (loc != NULL)
1635 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001636
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001637 macro = talloc (parser, macro_t);
Carl Worthfcbbb462010-05-13 09:36:23 -07001638
1639 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001640 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001641 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001642 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001643
Carl Worth3882cf22010-08-17 23:20:58 -07001644 previous = hash_table_find (parser->defines, identifier);
1645 if (previous) {
1646 if (_macro_equal (macro, previous)) {
1647 talloc_free (macro);
1648 return;
1649 }
1650 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1651 identifier);
1652 }
1653
Carl Worthfcbbb462010-05-13 09:36:23 -07001654 hash_table_insert (parser->defines, macro, identifier);
1655}
1656
1657void
1658_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001659 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001660 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001661 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001662 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001663{
Carl Worth3882cf22010-08-17 23:20:58 -07001664 macro_t *macro, *previous;
Carl Worthfcbbb462010-05-13 09:36:23 -07001665
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001666 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001667
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001668 macro = talloc (parser, macro_t);
Carl Worthfcbbb462010-05-13 09:36:23 -07001669
1670 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001671 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001672 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001673 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001674
Carl Worth3882cf22010-08-17 23:20:58 -07001675 previous = hash_table_find (parser->defines, identifier);
1676 if (previous) {
1677 if (_macro_equal (macro, previous)) {
1678 talloc_free (macro);
1679 return;
1680 }
1681 glcpp_error (loc, parser, "Redefinition of macro %s\n",
1682 identifier);
1683 }
1684
Carl Worthfcbbb462010-05-13 09:36:23 -07001685 hash_table_insert (parser->defines, macro, identifier);
1686}
1687
Carl Worth8f38aff2010-05-19 10:01:29 -07001688static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001689glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001690{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001691 token_node_t *node;
1692 int ret;
1693
Carl Worth95951ea2010-05-26 15:57:10 -07001694 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001695 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001696
1697 /* XXX: This ugly block of code exists for the sole
1698 * purpose of converting a NEWLINE token into a SPACE
1699 * token, but only in the case where we have seen a
1700 * function-like macro name, but have not yet seen its
1701 * closing parenthesis.
1702 *
1703 * There's perhaps a more compact way to do this with
1704 * mid-rule actions in the grammar.
1705 *
1706 * I'm definitely not pleased with the complexity of
1707 * this code here.
1708 */
1709 if (parser->newline_as_space)
1710 {
1711 if (ret == '(') {
1712 parser->paren_count++;
1713 } else if (ret == ')') {
1714 parser->paren_count--;
1715 if (parser->paren_count == 0)
1716 parser->newline_as_space = 0;
1717 } else if (ret == NEWLINE) {
1718 ret = SPACE;
1719 } else if (ret != SPACE) {
1720 if (parser->paren_count == 0)
1721 parser->newline_as_space = 0;
1722 }
1723 }
1724 else if (parser->in_control_line)
1725 {
1726 if (ret == NEWLINE)
1727 parser->in_control_line = 0;
1728 }
1729 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1730 ret == HASH_UNDEF || ret == HASH_IF ||
1731 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1732 ret == HASH_ELIF || ret == HASH_ELSE ||
1733 ret == HASH_ENDIF || ret == HASH)
1734 {
1735 parser->in_control_line = 1;
1736 }
1737 else if (ret == IDENTIFIER)
1738 {
1739 macro_t *macro;
1740 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001741 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001742 if (macro && macro->is_function) {
1743 parser->newline_as_space = 1;
1744 parser->paren_count = 0;
1745 }
1746 }
1747
1748 return ret;
1749 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001750
1751 node = parser->lex_from_node;
1752
1753 if (node == NULL) {
1754 talloc_free (parser->lex_from_list);
1755 parser->lex_from_list = NULL;
1756 return NEWLINE;
1757 }
1758
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001759 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001760 ret = node->token->type;
1761
1762 parser->lex_from_node = node->next;
1763
1764 return ret;
1765}
1766
1767static void
1768glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1769{
1770 token_node_t *node;
1771
1772 assert (parser->lex_from_list == NULL);
1773
1774 /* Copy list, eliminating any space tokens. */
1775 parser->lex_from_list = _token_list_create (parser);
1776
1777 for (node = list->head; node; node = node->next) {
1778 if (node->token->type == SPACE)
1779 continue;
1780 _token_list_append (parser->lex_from_list, node->token);
1781 }
1782
1783 talloc_free (list);
1784
1785 parser->lex_from_node = parser->lex_from_list->head;
1786
1787 /* It's possible the list consisted of nothing but whitespace. */
1788 if (parser->lex_from_node == NULL) {
1789 talloc_free (parser->lex_from_list);
1790 parser->lex_from_list = NULL;
1791 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001792}
Carl Worthb20d33c2010-05-20 22:27:07 -07001793
1794static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001795_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1796 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001797{
1798 skip_type_t current = SKIP_NO_SKIP;
1799 skip_node_t *node;
1800
1801 if (parser->skip_stack)
1802 current = parser->skip_stack->type;
1803
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001804 node = talloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001805 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001806
1807 if (current == SKIP_NO_SKIP) {
1808 if (condition)
1809 node->type = SKIP_NO_SKIP;
1810 else
1811 node->type = SKIP_TO_ELSE;
1812 } else {
1813 node->type = SKIP_TO_ENDIF;
1814 }
1815
1816 node->next = parser->skip_stack;
1817 parser->skip_stack = node;
1818}
1819
1820static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001821_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1822 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001823{
1824 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001825 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001826 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001827 }
1828
1829 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1830 if (condition)
1831 parser->skip_stack->type = SKIP_NO_SKIP;
1832 } else {
1833 parser->skip_stack->type = SKIP_TO_ENDIF;
1834 }
1835}
Carl Worth80dc60b2010-05-25 14:42:00 -07001836
Carl Worthb20d33c2010-05-20 22:27:07 -07001837static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001838_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001839{
1840 skip_node_t *node;
1841
1842 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001843 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001844 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001845 }
1846
1847 node = parser->skip_stack;
1848 parser->skip_stack = node->next;
1849 talloc_free (node);
1850}