blob: 795030ecfe758ead5d2bbd540bff50897b013f1f [file] [log] [blame]
Carl Worth3a37b872010-05-10 11:44:09 -07001%{
2/*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
Carl Worthfcbbb462010-05-13 09:36:23 -070027#include <assert.h>
Carl Worth8fed1cd2010-05-26 09:32:12 -070028#include <inttypes.h>
Carl Worth3a37b872010-05-10 11:44:09 -070029
Carl Wortha1e32bc2010-05-10 13:17:25 -070030#include "glcpp.h"
Ian Romanick06143ea2010-06-30 16:27:22 -070031#include "main/mtypes.h"
Carl Wortha1e32bc2010-05-10 13:17:25 -070032
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -070033#define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
34#define glcpp_printf(stream, fmt, args...) \
35 stream = talloc_asprintf_append(stream, fmt, args)
36
Carl Worth5aa7ea02010-05-25 18:39:43 -070037static void
Kenneth Graunke465e03e2010-06-16 16:35:57 -070038yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070039
Carl Worth5aa7ea02010-05-25 18:39:43 -070040static void
Carl Worthfcbbb462010-05-13 09:36:23 -070041_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070042 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070043 const char *macro,
Carl Worth47252442010-05-19 13:54:37 -070044 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070045
Carl Worth5aa7ea02010-05-25 18:39:43 -070046static void
Carl Worthfcbbb462010-05-13 09:36:23 -070047_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070048 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070049 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070050 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070051 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070052
Carl Worth5aa7ea02010-05-25 18:39:43 -070053static string_list_t *
Carl Worth610053b2010-05-14 10:05:11 -070054_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070055
Carl Worth5aa7ea02010-05-25 18:39:43 -070056static void
Carl Worth610053b2010-05-14 10:05:11 -070057_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070058
Carl Worth5aa7ea02010-05-25 18:39:43 -070059static int
Carl Worth610053b2010-05-14 10:05:11 -070060_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070061
Carl Worth5aa7ea02010-05-25 18:39:43 -070062static int
Carl Worth610053b2010-05-14 10:05:11 -070063_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070064
Carl Worth5aa7ea02010-05-25 18:39:43 -070065static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070066_argument_list_create (void *ctx);
67
Carl Worth5aa7ea02010-05-25 18:39:43 -070068static void
Carl Worth47252442010-05-19 13:54:37 -070069_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070070
Carl Worth5aa7ea02010-05-25 18:39:43 -070071static int
Carl Worth8f6a8282010-05-14 10:44:19 -070072_argument_list_length (argument_list_t *list);
73
Carl Worth5aa7ea02010-05-25 18:39:43 -070074static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070075_argument_list_member_at (argument_list_t *list, int index);
76
Carl Worth808401f2010-05-25 14:52:43 -070077/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070078static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070079_token_create_str (void *ctx, int type, char *str);
80
Carl Worth5aa7ea02010-05-25 18:39:43 -070081static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070082_token_create_ival (void *ctx, int type, int ival);
83
Carl Worth5aa7ea02010-05-25 18:39:43 -070084static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070085_token_list_create (void *ctx);
86
Carl Worthb1ae61a2010-05-26 08:10:38 -070087/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070088 *
89 * You may want to talloc_unlink any current reference if you no
90 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070091static void
Carl Worth808401f2010-05-25 14:52:43 -070092_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070093
Carl Worth5aa7ea02010-05-25 18:39:43 -070094static void
Carl Worth47252442010-05-19 13:54:37 -070095_token_list_append_list (token_list_t *list, token_list_t *tail);
96
Carl Worth22b3ace2010-06-02 15:32:03 -070097static active_list_t *
98_active_list_push (active_list_t *list,
99 const char *identifier,
100 token_node_t *marker);
101
102static active_list_t *
103_active_list_pop (active_list_t *list);
104
105int
106_active_list_contains (active_list_t *list, const char *identifier);
107
Carl Worth5aa7ea02010-05-25 18:39:43 -0700108static void
Kenneth Graunke16b4eed2010-08-04 16:10:03 -0700109_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list);
110
111static void
Carl Worth681afbc2010-05-28 15:06:02 -0700112_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
113 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700114
Carl Worthaaa9acb2010-05-19 13:28:24 -0700115static void
Carl Worth681afbc2010-05-28 15:06:02 -0700116_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
117 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700118
119static void
Kenneth Graunke07745232010-06-17 12:41:46 -0700120_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
121 int condition);
Carl Worthb20d33c2010-05-20 22:27:07 -0700122
123static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700124_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
125 const char *type, int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700126
Carl Worthb20d33c2010-05-20 22:27:07 -0700127static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700128_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
Carl Worthb20d33c2010-05-20 22:27:07 -0700129
Carl Worth0293b2e2010-05-19 10:05:40 -0700130#define yylex glcpp_parser_lex
131
Carl Worth8f38aff2010-05-19 10:01:29 -0700132static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700133glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700134
Carl Worth8e82fcb2010-05-26 11:15:21 -0700135static void
136glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
137
Eric Anholtd4a04f32010-07-28 16:58:39 -0700138static void
139add_builtin_define(glcpp_parser_t *parser, const char *name, int value);
140
Carl Worth3a37b872010-05-10 11:44:09 -0700141%}
142
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700143%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700144%error-verbose
Carl Worth485f84d2010-08-10 16:58:28 -0700145
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700146%locations
Carl Worth485f84d2010-08-10 16:58:28 -0700147%initial-action {
148 @$.first_line = 1;
149 @$.first_column = 1;
150 @$.last_line = 1;
151 @$.last_column = 1;
152 @$.source = 0;
153}
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700154
Carl Worth0b27b5f2010-05-10 16:16:06 -0700155%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700156%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700157
Carl Worthefef9502010-07-28 11:10:52 -0700158%expect 0
Eric Anholtd4a04f32010-07-28 16:58:39 -0700159%token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_UNDEF HASH_VERSION IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE
Carl Worth8fed1cd2010-05-26 09:32:12 -0700160%token PASTE
Eric Anholtd4a04f32010-07-28 16:58:39 -0700161%type <ival> expression INTEGER operator SPACE integer_constant
Carl Worth050e3de2010-05-27 14:36:29 -0700162%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700163%type <string_list> identifier_list
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700164%type <token> preprocessing_token conditional_token
165%type <token_list> pp_tokens replacement_list text_line conditional_tokens
Carl Worth8fed1cd2010-05-26 09:32:12 -0700166%left OR
167%left AND
168%left '|'
169%left '^'
170%left '&'
171%left EQUAL NOT_EQUAL
172%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
173%left LEFT_SHIFT RIGHT_SHIFT
174%left '+' '-'
175%left '*' '/' '%'
176%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700177
178%%
179
Carl Worth33cc4002010-05-12 12:17:10 -0700180input:
Carl Worth3ff81672010-05-25 13:09:03 -0700181 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700182| input line
183;
184
185line:
186 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700187 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700188 }
Carl Worth808401f2010-05-25 14:52:43 -0700189| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700190 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700191 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700192 talloc_free ($1);
193 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700194| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700195| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700196;
197
Carl Worth8e82fcb2010-05-26 11:15:21 -0700198expanded_line:
199 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700200 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700201 }
202| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700203 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700204 }
205;
206
Carl Worth3ff81672010-05-25 13:09:03 -0700207control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700208 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700209 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700210 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700211| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700212 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700213 }
214| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700215 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700216 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700217| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700218 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700219 if (macro) {
Carl Worth61ebc012010-07-19 18:02:12 -0700220 hash_table_remove (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700221 talloc_free (macro);
222 }
223 talloc_free ($2);
224 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700225| HASH_IF conditional_tokens NEWLINE {
Carl Worth48ba0582010-08-11 12:43:44 -0700226 /* Be careful to only evaluate the 'if' expression if
227 * we are not skipping. When we are skipping, we
228 * simply push a new 0-valued 'if' onto the skip
229 * stack.
230 *
231 * This avoids generating diagnostics for invalid
232 * expressions that are being skipped. */
233 if (parser->skip_stack == NULL ||
234 parser->skip_stack->type == SKIP_NO_SKIP)
235 {
236 _glcpp_parser_expand_if (parser, IF_EXPANDED, $2);
237 }
238 else
239 {
Kenneth Graunkef4239872010-08-04 16:24:39 -0700240 _glcpp_parser_skip_stack_push_if (parser, & @1, 0);
241 parser->skip_stack->type = SKIP_TO_ENDIF;
Kenneth Graunkef4239872010-08-04 16:24:39 -0700242 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700243 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700244| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700245 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700246 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700247 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700248 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700249| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700250 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700251 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700252 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700253 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700254| HASH_ELIF conditional_tokens NEWLINE {
Carl Worth48ba0582010-08-11 12:43:44 -0700255 /* Be careful to only evaluate the 'elif' expression
256 * if we are not skipping. When we are skipping, we
257 * simply change to a 0-valued 'elif' on the skip
258 * stack.
259 *
260 * This avoids generating diagnostics for invalid
261 * expressions that are being skipped. */
262 if (parser->skip_stack &&
263 parser->skip_stack->type == SKIP_TO_ELSE)
264 {
Kenneth Graunkef4239872010-08-04 16:24:39 -0700265 _glcpp_parser_expand_if (parser, ELIF_EXPANDED, $2);
Carl Worth48ba0582010-08-11 12:43:44 -0700266 }
267 else
268 {
269 _glcpp_parser_skip_stack_change_if (parser, & @1,
270 "elif", 0);
271 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700272 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700273| HASH_ELIF NEWLINE {
Carl Worth48ba0582010-08-11 12:43:44 -0700274 /* #elif without an expression is an error unless we
275 * are skipping. */
276 if (parser->skip_stack &&
277 parser->skip_stack->type == SKIP_TO_ELSE)
278 {
Carl Worth624dd582010-08-11 13:50:51 -0700279 glcpp_error(& @1, parser, "#elif with no expression");
Kenneth Graunke332fc472010-06-21 12:20:22 -0700280 }
Carl Worth48ba0582010-08-11 12:43:44 -0700281 else
282 {
283 _glcpp_parser_skip_stack_change_if (parser, & @1,
284 "elif", 0);
285 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
286 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700287 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700288| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700289 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700290 }
291| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700292 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700293 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700294| HASH_VERSION integer_constant NEWLINE {
295 macro_t *macro = hash_table_find (parser->defines, "__VERSION__");
296 if (macro) {
297 hash_table_remove (parser->defines, "__VERSION__");
298 talloc_free (macro);
299 }
300 add_builtin_define (parser, "__VERSION__", $2);
301 glcpp_printf(parser->output, "#version %" PRIiMAX "\n", $2);
302 }
Carl Worth3ff81672010-05-25 13:09:03 -0700303| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700304;
305
Eric Anholtd4a04f32010-07-28 16:58:39 -0700306integer_constant:
Carl Worth050e3de2010-05-27 14:36:29 -0700307 INTEGER_STRING {
308 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
309 $$ = strtoll ($1 + 2, NULL, 16);
310 } else if ($1[0] == '0') {
311 $$ = strtoll ($1, NULL, 8);
312 } else {
313 $$ = strtoll ($1, NULL, 10);
314 }
315 }
316| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700317 $$ = $1;
318 }
Eric Anholtd4a04f32010-07-28 16:58:39 -0700319
320expression:
321 integer_constant
Carl Worth8fed1cd2010-05-26 09:32:12 -0700322| expression OR expression {
323 $$ = $1 || $3;
324 }
325| expression AND expression {
326 $$ = $1 && $3;
327 }
328| expression '|' expression {
329 $$ = $1 | $3;
330 }
331| expression '^' expression {
332 $$ = $1 ^ $3;
333 }
334| expression '&' expression {
335 $$ = $1 & $3;
336 }
337| expression NOT_EQUAL expression {
338 $$ = $1 != $3;
339 }
340| expression EQUAL expression {
341 $$ = $1 == $3;
342 }
343| expression GREATER_OR_EQUAL expression {
344 $$ = $1 >= $3;
345 }
346| expression LESS_OR_EQUAL expression {
347 $$ = $1 <= $3;
348 }
349| expression '>' expression {
350 $$ = $1 > $3;
351 }
352| expression '<' expression {
353 $$ = $1 < $3;
354 }
355| expression RIGHT_SHIFT expression {
356 $$ = $1 >> $3;
357 }
358| expression LEFT_SHIFT expression {
359 $$ = $1 << $3;
360 }
361| expression '-' expression {
362 $$ = $1 - $3;
363 }
364| expression '+' expression {
365 $$ = $1 + $3;
366 }
367| expression '%' expression {
368 $$ = $1 % $3;
369 }
370| expression '/' expression {
371 $$ = $1 / $3;
372 }
373| expression '*' expression {
374 $$ = $1 * $3;
375 }
376| '!' expression %prec UNARY {
377 $$ = ! $2;
378 }
379| '~' expression %prec UNARY {
380 $$ = ~ $2;
381 }
382| '-' expression %prec UNARY {
383 $$ = - $2;
384 }
385| '+' expression %prec UNARY {
386 $$ = + $2;
387 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700388| '(' expression ')' {
389 $$ = $2;
390 }
391;
392
Carl Worth3ff81672010-05-25 13:09:03 -0700393identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700394 IDENTIFIER {
395 $$ = _string_list_create (parser);
396 _string_list_append_item ($$, $1);
397 talloc_steal ($$, $1);
398 }
399| identifier_list ',' IDENTIFIER {
400 $$ = $1;
401 _string_list_append_item ($$, $3);
402 talloc_steal ($$, $3);
403 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700404;
405
Carl Worth3ff81672010-05-25 13:09:03 -0700406text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700407 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700408| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700409;
410
Carl Worth3ff81672010-05-25 13:09:03 -0700411non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700412 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700413 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700414 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700415;
416
Carl Worthaaa9acb2010-05-19 13:28:24 -0700417replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700418 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700419| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700420;
421
Kenneth Graunke65875742010-06-18 20:08:15 -0700422junk:
423 /* empty */
424| pp_tokens {
425 glcpp_warning(&@1, parser, "extra tokens at end of directive");
426 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700427;
428
429conditional_token:
430 /* Handle "defined" operator */
431 DEFINED IDENTIFIER {
432 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
433 $$ = _token_create_ival (parser, INTEGER, v);
434 }
435| DEFINED '(' IDENTIFIER ')' {
436 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
437 $$ = _token_create_ival (parser, INTEGER, v);
438 }
439| preprocessing_token
440;
441
442conditional_tokens:
443 /* Exactly the same as pp_tokens, but using conditional_token */
444 conditional_token {
445 parser->space_tokens = 1;
446 $$ = _token_list_create (parser);
447 _token_list_append ($$, $1);
448 talloc_unlink (parser, $1);
449 }
450| conditional_tokens conditional_token {
451 $$ = $1;
452 _token_list_append ($$, $2);
453 talloc_unlink (parser, $2);
454 }
455;
Kenneth Graunke65875742010-06-18 20:08:15 -0700456
Carl Worthaaa9acb2010-05-19 13:28:24 -0700457pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700458 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700459 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700460 $$ = _token_list_create (parser);
461 _token_list_append ($$, $1);
462 talloc_unlink (parser, $1);
463 }
464| pp_tokens preprocessing_token {
465 $$ = $1;
466 _token_list_append ($$, $2);
467 talloc_unlink (parser, $2);
468 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700469;
470
Carl Worth3ff81672010-05-25 13:09:03 -0700471preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700472 IDENTIFIER {
473 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700474 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700475 }
Carl Worth050e3de2010-05-27 14:36:29 -0700476| INTEGER_STRING {
477 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700478 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700479 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700480| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700481 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700482 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700483 }
484| OTHER {
485 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700486 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700487 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700488| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700489 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700490 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700491 }
Carl Worth3ff81672010-05-25 13:09:03 -0700492;
493
Carl Worth8e82fcb2010-05-26 11:15:21 -0700494operator:
Carl Worth808401f2010-05-25 14:52:43 -0700495 '[' { $$ = '['; }
496| ']' { $$ = ']'; }
497| '(' { $$ = '('; }
498| ')' { $$ = ')'; }
499| '{' { $$ = '{'; }
500| '}' { $$ = '}'; }
501| '.' { $$ = '.'; }
502| '&' { $$ = '&'; }
503| '*' { $$ = '*'; }
504| '+' { $$ = '+'; }
505| '-' { $$ = '-'; }
506| '~' { $$ = '~'; }
507| '!' { $$ = '!'; }
508| '/' { $$ = '/'; }
509| '%' { $$ = '%'; }
510| LEFT_SHIFT { $$ = LEFT_SHIFT; }
511| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
512| '<' { $$ = '<'; }
513| '>' { $$ = '>'; }
514| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
515| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
516| EQUAL { $$ = EQUAL; }
517| NOT_EQUAL { $$ = NOT_EQUAL; }
518| '^' { $$ = '^'; }
519| '|' { $$ = '|'; }
520| AND { $$ = AND; }
521| OR { $$ = OR; }
522| ';' { $$ = ';'; }
523| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700524| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700525| PASTE { $$ = PASTE; }
Carl Worth3ff81672010-05-25 13:09:03 -0700526;
527
Carl Worth33cc4002010-05-12 12:17:10 -0700528%%
529
Carl Worth610053b2010-05-14 10:05:11 -0700530string_list_t *
531_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700532{
Carl Worth610053b2010-05-14 10:05:11 -0700533 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700534
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700535 list = talloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700536 list->head = NULL;
537 list->tail = NULL;
538
539 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700540}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700541
Carl Worth33cc4002010-05-12 12:17:10 -0700542void
Carl Worth610053b2010-05-14 10:05:11 -0700543_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700544{
Carl Worth610053b2010-05-14 10:05:11 -0700545 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700546
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700547 node = talloc (list, string_node_t);
548 node->str = talloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700549
Carl Worth33cc4002010-05-12 12:17:10 -0700550 node->next = NULL;
551
552 if (list->head == NULL) {
553 list->head = node;
554 } else {
555 list->tail->next = node;
556 }
557
558 list->tail = node;
559}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700560
561int
Carl Worth610053b2010-05-14 10:05:11 -0700562_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700563{
Carl Worth610053b2010-05-14 10:05:11 -0700564 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700565 int i;
566
567 if (list == NULL)
568 return 0;
569
570 for (i = 0, node = list->head; node; i++, node = node->next) {
571 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700572 if (index)
573 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700574 return 1;
575 }
576 }
577
578 return 0;
579}
580
581int
Carl Worth610053b2010-05-14 10:05:11 -0700582_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700583{
584 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700585 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700586
587 if (list == NULL)
588 return 0;
589
590 for (node = list->head; node; node = node->next)
591 length++;
592
593 return length;
594}
595
Carl Worth8f6a8282010-05-14 10:44:19 -0700596argument_list_t *
597_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700598{
Carl Worth8f6a8282010-05-14 10:44:19 -0700599 argument_list_t *list;
600
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700601 list = talloc (ctx, argument_list_t);
Carl Worth8f6a8282010-05-14 10:44:19 -0700602 list->head = NULL;
603 list->tail = NULL;
604
605 return list;
606}
607
608void
Carl Worth47252442010-05-19 13:54:37 -0700609_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700610{
611 argument_node_t *node;
612
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700613 node = talloc (list, argument_node_t);
Carl Worth8f6a8282010-05-14 10:44:19 -0700614 node->argument = argument;
615
616 node->next = NULL;
617
618 if (list->head == NULL) {
619 list->head = node;
620 } else {
621 list->tail->next = node;
622 }
623
624 list->tail = node;
625}
626
627int
628_argument_list_length (argument_list_t *list)
629{
630 int length = 0;
631 argument_node_t *node;
632
633 if (list == NULL)
634 return 0;
635
636 for (node = list->head; node; node = node->next)
637 length++;
638
639 return length;
640}
641
Carl Worth47252442010-05-19 13:54:37 -0700642token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700643_argument_list_member_at (argument_list_t *list, int index)
644{
645 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700646 int i;
647
648 if (list == NULL)
649 return NULL;
650
651 node = list->head;
652 for (i = 0; i < index; i++) {
653 node = node->next;
654 if (node == NULL)
655 break;
656 }
657
658 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700659 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700660
661 return NULL;
662}
Carl Worth47252442010-05-19 13:54:37 -0700663
Carl Worth808401f2010-05-25 14:52:43 -0700664/* Note: This function talloc_steal()s the str pointer. */
665token_t *
666_token_create_str (void *ctx, int type, char *str)
667{
668 token_t *token;
669
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700670 token = talloc (ctx, token_t);
Carl Worth808401f2010-05-25 14:52:43 -0700671 token->type = type;
672 token->value.str = talloc_steal (token, str);
673
674 return token;
675}
676
677token_t *
678_token_create_ival (void *ctx, int type, int ival)
679{
680 token_t *token;
681
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700682 token = talloc (ctx, token_t);
Carl Worth808401f2010-05-25 14:52:43 -0700683 token->type = type;
684 token->value.ival = ival;
685
686 return token;
687}
688
Carl Worth47252442010-05-19 13:54:37 -0700689token_list_t *
690_token_list_create (void *ctx)
691{
692 token_list_t *list;
693
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700694 list = talloc (ctx, token_list_t);
Carl Worth47252442010-05-19 13:54:37 -0700695 list->head = NULL;
696 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700697 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700698
699 return list;
700}
701
702void
Carl Worth808401f2010-05-25 14:52:43 -0700703_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700704{
705 token_node_t *node;
706
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700707 node = talloc (list, token_node_t);
708 node->token = talloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700709
710 node->next = NULL;
711
712 if (list->head == NULL) {
713 list->head = node;
714 } else {
715 list->tail->next = node;
716 }
717
718 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700719 if (token->type != SPACE)
720 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700721}
722
723void
724_token_list_append_list (token_list_t *list, token_list_t *tail)
725{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700726 if (tail == NULL || tail->head == NULL)
727 return;
728
Carl Worth47252442010-05-19 13:54:37 -0700729 if (list->head == NULL) {
730 list->head = tail->head;
731 } else {
732 list->tail->next = tail->head;
733 }
734
735 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700736 list->non_space_tail = tail->non_space_tail;
737}
738
Carl Worthd80dcaf2010-07-20 15:55:21 -0700739static token_list_t *
Carl Worth681afbc2010-05-28 15:06:02 -0700740_token_list_copy (void *ctx, token_list_t *other)
741{
742 token_list_t *copy;
743 token_node_t *node;
744
745 if (other == NULL)
746 return NULL;
747
748 copy = _token_list_create (ctx);
749 for (node = other->head; node; node = node->next)
750 _token_list_append (copy, node->token);
751
752 return copy;
753}
754
Carl Worthd80dcaf2010-07-20 15:55:21 -0700755static void
Carl Worth10ae4382010-05-25 20:35:01 -0700756_token_list_trim_trailing_space (token_list_t *list)
757{
758 token_node_t *tail, *next;
759
760 if (list->non_space_tail) {
761 tail = list->non_space_tail->next;
762 list->non_space_tail->next = NULL;
763 list->tail = list->non_space_tail;
764
765 while (tail) {
766 next = tail->next;
767 talloc_free (tail);
768 tail = next;
769 }
770 }
Carl Worth47252442010-05-19 13:54:37 -0700771}
Carl Worth80dc60b2010-05-25 14:42:00 -0700772
Carl Worth0197e9b2010-05-26 08:05:19 -0700773static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700774_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700775{
776 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700777 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700778 return;
779 }
780
781 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700782 case INTEGER:
Eric Anholt8605c292010-07-28 16:53:51 -0700783 glcpp_printf (*out, "%" PRIiMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700784 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700785 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700786 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700787 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700788 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700789 break;
790 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700791 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700792 break;
793 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700794 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700795 break;
796 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700797 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700798 break;
799 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700800 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700801 break;
802 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700803 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700804 break;
805 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700806 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700807 break;
808 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700809 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700810 break;
811 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700812 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700813 break;
814 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700815 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700816 break;
817 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700818 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700819 break;
Carl Worthdd749002010-05-27 10:12:33 -0700820 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700821 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700822 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700823 case PLACEHOLDER:
824 /* Nothing to print. */
825 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700826 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700827 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700828 break;
829 }
830}
831
Carl Worthb06096e2010-05-29 05:54:19 -0700832/* Return a new token (talloc()ed off of 'token') formed by pasting
833 * 'token' and 'other'. Note that this function may return 'token' or
834 * 'other' directly rather than allocating anything new.
835 *
836 * Caution: Only very cursory error-checking is performed to see if
837 * the final result is a valid single token. */
838static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700839_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700840{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700841 token_t *combined = NULL;
842
Carl Worth85b50e82010-05-27 14:01:18 -0700843 /* Pasting a placeholder onto anything makes no change. */
844 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700845 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700846
Carl Worthb06096e2010-05-29 05:54:19 -0700847 /* When 'token' is a placeholder, just return 'other'. */
848 if (token->type == PLACEHOLDER)
849 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700850
Carl Worthad0dee62010-05-26 09:04:50 -0700851 /* A very few single-character punctuators can be combined
852 * with another to form a multi-character punctuator. */
853 switch (token->type) {
854 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700855 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700856 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700857 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700858 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700859 break;
860 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700861 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700862 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700863 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700864 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700865 break;
866 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700867 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700868 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700869 break;
870 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700871 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700872 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700873 break;
874 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700875 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700876 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700877 break;
878 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700879 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700880 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700881 break;
882 }
883
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700884 if (combined != NULL) {
885 /* Inherit the location from the first token */
886 combined->location = token->location;
887 return combined;
888 }
889
Carl Worthad0dee62010-05-26 09:04:50 -0700890 /* Two string-valued tokens can usually just be mashed
891 * together.
892 *
Carl Worth050e3de2010-05-27 14:36:29 -0700893 * XXX: This isn't actually legitimate. Several things here
894 * should result in a diagnostic since the result cannot be a
895 * valid, single pre-processing token. For example, pasting
896 * "123" and "abc" is not legal, but we don't catch that
897 * here. */
898 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
899 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700900 {
Carl Worthb06096e2010-05-29 05:54:19 -0700901 char *str;
902
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700903 str = talloc_asprintf (token, "%s%s", token->value.str,
904 other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700905 combined = _token_create_str (token, token->type, str);
906 combined->location = token->location;
907 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700908 }
909
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700910 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700911 glcpp_print (parser->info_log, "Pasting \"");
912 _token_print (&parser->info_log, token);
913 glcpp_print (parser->info_log, "\" and \"");
914 _token_print (&parser->info_log, other);
915 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700916
917 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700918}
919
Carl Worth0197e9b2010-05-26 08:05:19 -0700920static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700921_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700922{
923 token_node_t *node;
924
925 if (list == NULL)
926 return;
927
928 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700929 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700930}
931
Carl Worth3a37b872010-05-10 11:44:09 -0700932void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700933yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700934{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700935 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700936}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700937
Eric Anholtd4a04f32010-07-28 16:58:39 -0700938static void add_builtin_define(glcpp_parser_t *parser,
939 const char *name, int value)
940{
941 token_t *tok;
942 token_list_t *list;
943
944 tok = _token_create_ival (parser, INTEGER, value);
945
946 list = _token_list_create(parser);
947 _token_list_append(list, tok);
948 _define_object_macro(parser, NULL, name, list);
949
950 talloc_unlink(parser, tok);
951}
952
Carl Worth33cc4002010-05-12 12:17:10 -0700953glcpp_parser_t *
Ian Romanick06143ea2010-06-30 16:27:22 -0700954glcpp_parser_create (const struct gl_extensions *extensions)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700955{
Carl Worth33cc4002010-05-12 12:17:10 -0700956 glcpp_parser_t *parser;
Eric Anholtd4a04f32010-07-28 16:58:39 -0700957 int language_version;
Carl Worth33cc4002010-05-12 12:17:10 -0700958
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -0700959 parser = talloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700960
Carl Worth8f38aff2010-05-19 10:01:29 -0700961 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700962 parser->defines = hash_table_ctor (32, hash_table_string_hash,
963 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700964 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700965 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700966 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700967 parser->newline_as_space = 0;
968 parser->in_control_line = 0;
969 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700970
Carl Worthb20d33c2010-05-20 22:27:07 -0700971 parser->skip_stack = NULL;
972
Carl Worth8e82fcb2010-05-26 11:15:21 -0700973 parser->lex_from_list = NULL;
974 parser->lex_from_node = NULL;
975
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700976 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700977 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke1b85c462010-06-21 13:55:12 -0700978 parser->error = 0;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700979
Ian Romanick2d122362010-06-30 16:03:19 -0700980 /* Add pre-defined macros. */
Eric Anholtd4a04f32010-07-28 16:58:39 -0700981 add_builtin_define(parser, "GL_ARB_draw_buffers", 1);
982 add_builtin_define(parser, "GL_ARB_texture_rectangle", 1);
Ian Romanick2d122362010-06-30 16:03:19 -0700983
Eric Anholtd4a04f32010-07-28 16:58:39 -0700984 if (extensions != NULL) {
985 if (extensions->EXT_texture_array) {
986 add_builtin_define(parser, "GL_EXT_texture_array", 1);
987 }
Ian Romanick2d122362010-06-30 16:03:19 -0700988
Eric Anholtd4a04f32010-07-28 16:58:39 -0700989 if (extensions->ARB_fragment_coord_conventions)
990 add_builtin_define(parser, "GL_ARB_fragment_coord_conventions",
991 1);
Ian Romanick06143ea2010-06-30 16:27:22 -0700992 }
993
Eric Anholtd4a04f32010-07-28 16:58:39 -0700994 language_version = 110;
Eric Anholtd4a04f32010-07-28 16:58:39 -0700995 add_builtin_define(parser, "__VERSION__", language_version);
Ian Romanick2d122362010-06-30 16:03:19 -0700996
Carl Worth33cc4002010-05-12 12:17:10 -0700997 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700998}
999
1000int
1001glcpp_parser_parse (glcpp_parser_t *parser)
1002{
1003 return yyparse (parser);
1004}
1005
1006void
Carl Worth33cc4002010-05-12 12:17:10 -07001007glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -07001008{
Carl Worthb20d33c2010-05-20 22:27:07 -07001009 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -07001010 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -07001011 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -07001012 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -07001013 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -07001014}
Carl Worthc6d5af32010-05-11 12:30:09 -07001015
Carl Worthb1854fd2010-05-25 16:28:26 -07001016typedef enum function_status
1017{
1018 FUNCTION_STATUS_SUCCESS,
1019 FUNCTION_NOT_A_FUNCTION,
1020 FUNCTION_UNBALANCED_PARENTHESES
1021} function_status_t;
1022
1023/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -07001024 * balanced set of parentheses.
1025 *
1026 * When called, 'node' should be the opening-parenthesis token, (or
1027 * perhaps preceeding SPACE tokens). Upon successful return *last will
1028 * be the last consumed node, (corresponding to the closing right
1029 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -07001030 *
1031 * Return values:
1032 *
1033 * FUNCTION_STATUS_SUCCESS:
1034 *
1035 * Successfully parsed a set of function arguments.
1036 *
1037 * FUNCTION_NOT_A_FUNCTION:
1038 *
1039 * Macro name not followed by a '('. This is not an error, but
1040 * simply that the macro name should be treated as a non-macro.
1041 *
Carl Worth14c98a52010-06-02 15:49:54 -07001042 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -07001043 *
1044 * Macro name is not followed by a balanced set of parentheses.
1045 */
1046static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -07001047_arguments_parse (argument_list_t *arguments,
1048 token_node_t *node,
1049 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001050{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001051 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001052 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001053
Carl Worthb1854fd2010-05-25 16:28:26 -07001054 node = node->next;
1055
1056 /* Ignore whitespace before first parenthesis. */
1057 while (node && node->token->type == SPACE)
1058 node = node->next;
1059
1060 if (node == NULL || node->token->type != '(')
1061 return FUNCTION_NOT_A_FUNCTION;
1062
Carl Worth652fa272010-05-25 17:45:22 -07001063 node = node->next;
1064
Carl Wortha19297b2010-05-27 13:29:19 -07001065 argument = _token_list_create (arguments);
1066 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001067
Carl Worth681afbc2010-05-28 15:06:02 -07001068 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001069 if (node->token->type == '(')
1070 {
1071 paren_count++;
1072 }
1073 else if (node->token->type == ')')
1074 {
1075 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001076 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001077 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001078 }
Carl Worth652fa272010-05-25 17:45:22 -07001079
1080 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001081 paren_count == 1)
1082 {
Carl Wortha19297b2010-05-27 13:29:19 -07001083 _token_list_trim_trailing_space (argument);
1084 argument = _token_list_create (arguments);
1085 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001086 }
1087 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001088 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001089 /* Don't treat initial whitespace as
1090 * part of the arguement. */
1091 if (node->token->type == SPACE)
1092 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001093 }
1094 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001095 }
Carl Worthc7581c22010-05-25 17:41:07 -07001096 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001097
Carl Worth681afbc2010-05-28 15:06:02 -07001098 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001099 return FUNCTION_UNBALANCED_PARENTHESES;
1100
Carl Worth681afbc2010-05-28 15:06:02 -07001101 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001102
1103 return FUNCTION_STATUS_SUCCESS;
1104}
1105
Carl Worthe1acbfc2010-07-20 16:44:03 -07001106static token_list_t *
1107_token_list_create_with_one_space (void *ctx)
1108{
1109 token_list_t *list;
1110 token_t *space;
1111
1112 list = _token_list_create (ctx);
1113 space = _token_create_ival (list, SPACE, SPACE);
1114 _token_list_append (list, space);
1115
1116 return list;
1117}
1118
Kenneth Graunke16b4eed2010-08-04 16:10:03 -07001119static void
1120_glcpp_parser_expand_if (glcpp_parser_t *parser, int type, token_list_t *list)
1121{
1122 token_list_t *expanded;
1123 token_t *token;
1124
1125 expanded = _token_list_create (parser);
1126 token = _token_create_ival (parser, type, type);
1127 _token_list_append (expanded, token);
1128 _glcpp_parser_expand_token_list (parser, list);
1129 _token_list_append_list (expanded, list);
1130 glcpp_parser_lex_from (parser, expanded);
1131}
1132
Carl Worth681afbc2010-05-28 15:06:02 -07001133/* This is a helper function that's essentially part of the
1134 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1135 * except for by that function.
1136 *
1137 * Returns NULL if node is a simple token with no expansion, (that is,
1138 * although 'node' corresponds to an identifier defined as a
1139 * function-like macro, it is not followed with a parenthesized
1140 * argument list).
1141 *
1142 * Compute the complete expansion of node (which is a function-like
1143 * macro) and subsequent nodes which are arguments.
1144 *
1145 * Returns the token list that results from the expansion and sets
1146 * *last to the last node in the list that was consumed by the
Carl Worthfbe42402010-07-22 16:36:04 -07001147 * expansion. Specifically, *last will be set as follows: as the
Carl Worth681afbc2010-05-28 15:06:02 -07001148 * token of the closing right parenthesis.
1149 */
1150static token_list_t *
1151_glcpp_parser_expand_function (glcpp_parser_t *parser,
1152 token_node_t *node,
1153 token_node_t **last)
1154
Carl Worthb1854fd2010-05-25 16:28:26 -07001155{
1156 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001157 const char *identifier;
1158 argument_list_t *arguments;
1159 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001160 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001161 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001162
Carl Worthb1854fd2010-05-25 16:28:26 -07001163 identifier = node->token->value.str;
1164
1165 macro = hash_table_find (parser->defines, identifier);
1166
1167 assert (macro->is_function);
1168
Carl Worth9ce18cf2010-05-25 17:32:21 -07001169 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001170 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001171
1172 switch (status) {
1173 case FUNCTION_STATUS_SUCCESS:
1174 break;
1175 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001176 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001177 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001178 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001179 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001180 }
1181
Carl Worthe1acbfc2010-07-20 16:44:03 -07001182 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth9ce18cf2010-05-25 17:32:21 -07001183 if (macro->replacements == NULL) {
1184 talloc_free (arguments);
Carl Worthe1acbfc2010-07-20 16:44:03 -07001185 return _token_list_create_with_one_space (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001186 }
1187
Carl Wortha19297b2010-05-27 13:29:19 -07001188 if (! ((_argument_list_length (arguments) ==
1189 _string_list_length (macro->parameters)) ||
1190 (_string_list_length (macro->parameters) == 0 &&
1191 _argument_list_length (arguments) == 1 &&
1192 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001193 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001194 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001195 "Error: macro %s invoked with %d arguments (expected %d)\n",
1196 identifier,
1197 _argument_list_length (arguments),
1198 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001199 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001200 }
1201
Carl Worth0197e9b2010-05-26 08:05:19 -07001202 /* Perform argument substitution on the replacement list. */
1203 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001204
Carl Worthce540f22010-05-26 08:25:44 -07001205 for (node = macro->replacements->head; node; node = node->next)
1206 {
1207 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001208 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001209 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001210 &parameter_index))
1211 {
1212 token_list_t *argument;
1213 argument = _argument_list_member_at (arguments,
1214 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001215 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001216 * tokens, or append a placeholder token for
1217 * an empty argument. */
1218 if (argument->head) {
Carl Worthfbe42402010-07-22 16:36:04 -07001219 token_list_t *expanded_argument;
1220 expanded_argument = _token_list_copy (parser,
1221 argument);
Carl Worth681afbc2010-05-28 15:06:02 -07001222 _glcpp_parser_expand_token_list (parser,
Carl Worthfbe42402010-07-22 16:36:04 -07001223 expanded_argument);
1224 _token_list_append_list (substituted,
1225 expanded_argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001226 } else {
1227 token_t *new_token;
1228
1229 new_token = _token_create_ival (substituted,
1230 PLACEHOLDER,
1231 PLACEHOLDER);
1232 _token_list_append (substituted, new_token);
1233 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001234 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001235 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001236 }
1237 }
1238
Carl Worthad0dee62010-05-26 09:04:50 -07001239 /* After argument substitution, and before further expansion
1240 * below, implement token pasting. */
1241
Carl Worthb06096e2010-05-29 05:54:19 -07001242 _token_list_trim_trailing_space (substituted);
1243
Carl Worthad0dee62010-05-26 09:04:50 -07001244 node = substituted->head;
1245 while (node)
1246 {
1247 token_node_t *next_non_space;
1248
1249 /* Look ahead for a PASTE token, skipping space. */
1250 next_non_space = node->next;
1251 while (next_non_space && next_non_space->token->type == SPACE)
1252 next_non_space = next_non_space->next;
1253
1254 if (next_non_space == NULL)
1255 break;
1256
1257 if (next_non_space->token->type != PASTE) {
1258 node = next_non_space;
1259 continue;
1260 }
1261
1262 /* Now find the next non-space token after the PASTE. */
1263 next_non_space = next_non_space->next;
1264 while (next_non_space && next_non_space->token->type == SPACE)
1265 next_non_space = next_non_space->next;
1266
1267 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001268 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001269 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001270 }
1271
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001272 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001273 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001274 if (next_non_space == substituted->tail)
1275 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001276
1277 node = node->next;
1278 }
1279
Carl Worthb06096e2010-05-29 05:54:19 -07001280 substituted->non_space_tail = substituted->tail;
1281
Carl Worth681afbc2010-05-28 15:06:02 -07001282 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001283}
1284
Carl Worth681afbc2010-05-28 15:06:02 -07001285/* Compute the complete expansion of node, (and subsequent nodes after
1286 * 'node' in the case that 'node' is a function-like macro and
1287 * subsequent nodes are arguments).
1288 *
1289 * Returns NULL if node is a simple token with no expansion.
1290 *
1291 * Otherwise, returns the token list that results from the expansion
1292 * and sets *last to the last node in the list that was consumed by
Carl Worthe1acbfc2010-07-20 16:44:03 -07001293 * the expansion. Specifically, *last will be set as follows:
Carl Worth681afbc2010-05-28 15:06:02 -07001294 *
1295 * As 'node' in the case of object-like macro expansion.
1296 *
1297 * As the token of the closing right parenthesis in the case of
1298 * function-like macro expansion.
1299 */
1300static token_list_t *
1301_glcpp_parser_expand_node (glcpp_parser_t *parser,
1302 token_node_t *node,
1303 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001304{
Carl Worth681afbc2010-05-28 15:06:02 -07001305 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001306 const char *identifier;
1307 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001308
1309 /* We only expand identifiers */
1310 if (token->type != IDENTIFIER) {
1311 /* We change any COMMA into a COMMA_FINAL to prevent
1312 * it being mistaken for an argument separator
1313 * later. */
1314 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001315 token->type = COMMA_FINAL;
1316 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001317 }
Carl Worth681afbc2010-05-28 15:06:02 -07001318
1319 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001320 }
1321
1322 /* Look up this identifier in the hash table. */
1323 identifier = token->value.str;
1324 macro = hash_table_find (parser->defines, identifier);
1325
Carl Worth681afbc2010-05-28 15:06:02 -07001326 /* Not a macro, so no expansion needed. */
1327 if (macro == NULL)
1328 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001329
1330 /* Finally, don't expand this macro if we're already actively
1331 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001332 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001333 /* We change the token type here from IDENTIFIER to
1334 * OTHER to prevent any future expansion of this
1335 * unexpanded token. */
1336 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001337 token_list_t *expansion;
1338 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001339
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001340 str = talloc_strdup (parser, token->value.str);
Carl Worth681afbc2010-05-28 15:06:02 -07001341 final = _token_create_str (parser, OTHER, str);
1342 expansion = _token_list_create (parser);
1343 _token_list_append (expansion, final);
1344 *last = node;
1345 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001346 }
1347
Carl Worth681afbc2010-05-28 15:06:02 -07001348 if (! macro->is_function)
1349 {
1350 *last = node;
1351
Carl Worthe1acbfc2010-07-20 16:44:03 -07001352 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth681afbc2010-05-28 15:06:02 -07001353 if (macro->replacements == NULL)
Carl Worthe1acbfc2010-07-20 16:44:03 -07001354 return _token_list_create_with_one_space (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001355
Carl Worth22b3ace2010-06-02 15:32:03 -07001356 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001357 }
Carl Worth681afbc2010-05-28 15:06:02 -07001358
1359 return _glcpp_parser_expand_function (parser, node, last);
1360}
1361
Carl Worth22b3ace2010-06-02 15:32:03 -07001362/* Push a new identifier onto the active list, returning the new list.
1363 *
1364 * Here, 'marker' is the token node that appears in the list after the
1365 * expansion of 'identifier'. That is, when the list iterator begins
1366 * examinging 'marker', then it is time to pop this node from the
1367 * active stack.
1368 */
1369active_list_t *
1370_active_list_push (active_list_t *list,
1371 const char *identifier,
1372 token_node_t *marker)
1373{
1374 active_list_t *node;
1375
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001376 node = talloc (list, active_list_t);
1377 node->identifier = talloc_strdup (node, identifier);
Carl Worth22b3ace2010-06-02 15:32:03 -07001378 node->marker = marker;
1379 node->next = list;
1380
1381 return node;
1382}
1383
1384active_list_t *
1385_active_list_pop (active_list_t *list)
1386{
1387 active_list_t *node = list;
1388
1389 if (node == NULL)
1390 return NULL;
1391
1392 node = list->next;
1393 talloc_free (list);
1394
1395 return node;
1396}
1397
1398int
1399_active_list_contains (active_list_t *list, const char *identifier)
1400{
1401 active_list_t *node;
1402
1403 if (list == NULL)
1404 return 0;
1405
1406 for (node = list; node; node = node->next)
1407 if (strcmp (node->identifier, identifier) == 0)
1408 return 1;
1409
1410 return 0;
1411}
1412
Carl Worth681afbc2010-05-28 15:06:02 -07001413/* Walk over the token list replacing nodes with their expansion.
1414 * Whenever nodes are expanded the walking will walk over the new
1415 * nodes, continuing to expand as necessary. The results are placed in
1416 * 'list' itself;
1417 */
1418static void
1419_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1420 token_list_t *list)
1421{
1422 token_node_t *node_prev;
Carl Worthc42e6402010-06-22 15:51:34 -07001423 token_node_t *node, *last = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001424 token_list_t *expansion;
1425
1426 if (list == NULL)
1427 return;
1428
1429 _token_list_trim_trailing_space (list);
1430
1431 node_prev = NULL;
1432 node = list->head;
1433
1434 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001435
1436 while (parser->active && parser->active->marker == node)
1437 parser->active = _active_list_pop (parser->active);
1438
Carl Worth681afbc2010-05-28 15:06:02 -07001439 /* Find the expansion for node, which will replace all
1440 * nodes from node to last, inclusive. */
1441 expansion = _glcpp_parser_expand_node (parser, node, &last);
1442 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001443 token_node_t *n;
1444
1445 for (n = node; n != last->next; n = n->next)
1446 while (parser->active &&
1447 parser->active->marker == n)
1448 {
1449 parser->active = _active_list_pop (parser->active);
1450 }
1451
1452 parser->active = _active_list_push (parser->active,
1453 node->token->value.str,
1454 last->next);
1455
Carl Worth681afbc2010-05-28 15:06:02 -07001456 /* Splice expansion into list, supporting a
1457 * simple deletion if the expansion is
1458 * empty. */
1459 if (expansion->head) {
1460 if (node_prev)
1461 node_prev->next = expansion->head;
1462 else
1463 list->head = expansion->head;
1464 expansion->tail->next = last->next;
1465 if (last == list->tail)
1466 list->tail = expansion->tail;
1467 } else {
1468 if (node_prev)
1469 node_prev->next = last->next;
1470 else
1471 list->head = last->next;
1472 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001473 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001474 }
1475 } else {
1476 node_prev = node;
1477 }
1478 node = node_prev ? node_prev->next : list->head;
1479 }
1480
Carl Worth22b3ace2010-06-02 15:32:03 -07001481 while (parser->active)
1482 parser->active = _active_list_pop (parser->active);
1483
Carl Worth681afbc2010-05-28 15:06:02 -07001484 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001485}
1486
Carl Worthae6517f2010-05-25 15:24:59 -07001487void
1488_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1489 token_list_t *list)
1490{
Carl Worthae6517f2010-05-25 15:24:59 -07001491 if (list == NULL)
1492 return;
1493
Carl Worth681afbc2010-05-28 15:06:02 -07001494 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001495
Carl Worth681afbc2010-05-28 15:06:02 -07001496 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001497
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001498 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001499}
1500
Carl Worthd80dcaf2010-07-20 15:55:21 -07001501static void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001502_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1503 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001504{
1505 /* According to the GLSL specification, macro names starting with "__"
1506 * or "GL_" are reserved for future use. So, don't allow them.
1507 */
1508 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001509 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001510 }
1511 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001512 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001513 }
1514}
1515
1516void
Carl Worthfcbbb462010-05-13 09:36:23 -07001517_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001518 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001519 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001520 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001521{
1522 macro_t *macro;
1523
Ian Romanick2d122362010-06-30 16:03:19 -07001524 if (loc != NULL)
1525 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001526
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001527 macro = talloc (parser, macro_t);
Carl Worthfcbbb462010-05-13 09:36:23 -07001528
1529 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001530 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001531 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001532 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001533
1534 hash_table_insert (parser->defines, macro, identifier);
1535}
1536
1537void
1538_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001539 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001540 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001541 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001542 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001543{
1544 macro_t *macro;
1545
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001546 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001547
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001548 macro = talloc (parser, macro_t);
Carl Worthfcbbb462010-05-13 09:36:23 -07001549
1550 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001551 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001552 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001553 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001554
1555 hash_table_insert (parser->defines, macro, identifier);
1556}
1557
Carl Worth8f38aff2010-05-19 10:01:29 -07001558static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001559glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001560{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001561 token_node_t *node;
1562 int ret;
1563
Carl Worth95951ea2010-05-26 15:57:10 -07001564 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001565 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001566
1567 /* XXX: This ugly block of code exists for the sole
1568 * purpose of converting a NEWLINE token into a SPACE
1569 * token, but only in the case where we have seen a
1570 * function-like macro name, but have not yet seen its
1571 * closing parenthesis.
1572 *
1573 * There's perhaps a more compact way to do this with
1574 * mid-rule actions in the grammar.
1575 *
1576 * I'm definitely not pleased with the complexity of
1577 * this code here.
1578 */
1579 if (parser->newline_as_space)
1580 {
1581 if (ret == '(') {
1582 parser->paren_count++;
1583 } else if (ret == ')') {
1584 parser->paren_count--;
1585 if (parser->paren_count == 0)
1586 parser->newline_as_space = 0;
1587 } else if (ret == NEWLINE) {
1588 ret = SPACE;
1589 } else if (ret != SPACE) {
1590 if (parser->paren_count == 0)
1591 parser->newline_as_space = 0;
1592 }
1593 }
1594 else if (parser->in_control_line)
1595 {
1596 if (ret == NEWLINE)
1597 parser->in_control_line = 0;
1598 }
1599 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1600 ret == HASH_UNDEF || ret == HASH_IF ||
1601 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1602 ret == HASH_ELIF || ret == HASH_ELSE ||
1603 ret == HASH_ENDIF || ret == HASH)
1604 {
1605 parser->in_control_line = 1;
1606 }
1607 else if (ret == IDENTIFIER)
1608 {
1609 macro_t *macro;
1610 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001611 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001612 if (macro && macro->is_function) {
1613 parser->newline_as_space = 1;
1614 parser->paren_count = 0;
1615 }
1616 }
1617
1618 return ret;
1619 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001620
1621 node = parser->lex_from_node;
1622
1623 if (node == NULL) {
1624 talloc_free (parser->lex_from_list);
1625 parser->lex_from_list = NULL;
1626 return NEWLINE;
1627 }
1628
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001629 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001630 ret = node->token->type;
1631
1632 parser->lex_from_node = node->next;
1633
1634 return ret;
1635}
1636
1637static void
1638glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1639{
1640 token_node_t *node;
1641
1642 assert (parser->lex_from_list == NULL);
1643
1644 /* Copy list, eliminating any space tokens. */
1645 parser->lex_from_list = _token_list_create (parser);
1646
1647 for (node = list->head; node; node = node->next) {
1648 if (node->token->type == SPACE)
1649 continue;
1650 _token_list_append (parser->lex_from_list, node->token);
1651 }
1652
1653 talloc_free (list);
1654
1655 parser->lex_from_node = parser->lex_from_list->head;
1656
1657 /* It's possible the list consisted of nothing but whitespace. */
1658 if (parser->lex_from_node == NULL) {
1659 talloc_free (parser->lex_from_list);
1660 parser->lex_from_list = NULL;
1661 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001662}
Carl Worthb20d33c2010-05-20 22:27:07 -07001663
1664static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001665_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1666 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001667{
1668 skip_type_t current = SKIP_NO_SKIP;
1669 skip_node_t *node;
1670
1671 if (parser->skip_stack)
1672 current = parser->skip_stack->type;
1673
Kenneth Graunke1ffc1cd2010-08-03 20:21:52 -07001674 node = talloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001675 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001676
1677 if (current == SKIP_NO_SKIP) {
1678 if (condition)
1679 node->type = SKIP_NO_SKIP;
1680 else
1681 node->type = SKIP_TO_ELSE;
1682 } else {
1683 node->type = SKIP_TO_ENDIF;
1684 }
1685
1686 node->next = parser->skip_stack;
1687 parser->skip_stack = node;
1688}
1689
1690static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001691_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1692 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001693{
1694 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001695 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001696 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001697 }
1698
1699 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1700 if (condition)
1701 parser->skip_stack->type = SKIP_NO_SKIP;
1702 } else {
1703 parser->skip_stack->type = SKIP_TO_ENDIF;
1704 }
1705}
Carl Worth80dc60b2010-05-25 14:42:00 -07001706
Carl Worthb20d33c2010-05-20 22:27:07 -07001707static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001708_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001709{
1710 skip_node_t *node;
1711
1712 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001713 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001714 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001715 }
1716
1717 node = parser->skip_stack;
1718 parser->skip_stack = node->next;
1719 talloc_free (node);
1720}