blob: 3322db06ed374341dc04898222ac880f4e07a572 [file] [log] [blame]
Carl Worth3a37b872010-05-10 11:44:09 -07001%{
2/*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
Carl Worthfcbbb462010-05-13 09:36:23 -070027#include <assert.h>
Carl Worth8fed1cd2010-05-26 09:32:12 -070028#include <inttypes.h>
Carl Worth3a37b872010-05-10 11:44:09 -070029
Carl Wortha1e32bc2010-05-10 13:17:25 -070030#include "glcpp.h"
Ian Romanick06143ea2010-06-30 16:27:22 -070031#include "main/mtypes.h"
Carl Wortha1e32bc2010-05-10 13:17:25 -070032
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -070033#define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
34#define glcpp_printf(stream, fmt, args...) \
35 stream = talloc_asprintf_append(stream, fmt, args)
36
Carl Worth5aa7ea02010-05-25 18:39:43 -070037static void
Kenneth Graunke465e03e2010-06-16 16:35:57 -070038yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070039
Carl Worth5aa7ea02010-05-25 18:39:43 -070040static void
Carl Worthfcbbb462010-05-13 09:36:23 -070041_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070042 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070043 const char *macro,
Carl Worth47252442010-05-19 13:54:37 -070044 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070045
Carl Worth5aa7ea02010-05-25 18:39:43 -070046static void
Carl Worthfcbbb462010-05-13 09:36:23 -070047_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070048 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070049 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070050 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070051 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070052
Carl Worth5aa7ea02010-05-25 18:39:43 -070053static string_list_t *
Carl Worth610053b2010-05-14 10:05:11 -070054_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070055
Carl Worth5aa7ea02010-05-25 18:39:43 -070056static void
Carl Worth610053b2010-05-14 10:05:11 -070057_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070058
Carl Worth5aa7ea02010-05-25 18:39:43 -070059static int
Carl Worth610053b2010-05-14 10:05:11 -070060_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070061
Carl Worth5aa7ea02010-05-25 18:39:43 -070062static int
Carl Worth610053b2010-05-14 10:05:11 -070063_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070064
Carl Worth5aa7ea02010-05-25 18:39:43 -070065static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070066_argument_list_create (void *ctx);
67
Carl Worth5aa7ea02010-05-25 18:39:43 -070068static void
Carl Worth47252442010-05-19 13:54:37 -070069_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070070
Carl Worth5aa7ea02010-05-25 18:39:43 -070071static int
Carl Worth8f6a8282010-05-14 10:44:19 -070072_argument_list_length (argument_list_t *list);
73
Carl Worth5aa7ea02010-05-25 18:39:43 -070074static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070075_argument_list_member_at (argument_list_t *list, int index);
76
Carl Worth808401f2010-05-25 14:52:43 -070077/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070078static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070079_token_create_str (void *ctx, int type, char *str);
80
Carl Worth5aa7ea02010-05-25 18:39:43 -070081static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070082_token_create_ival (void *ctx, int type, int ival);
83
Carl Worth5aa7ea02010-05-25 18:39:43 -070084static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070085_token_list_create (void *ctx);
86
Carl Worthb1ae61a2010-05-26 08:10:38 -070087/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070088 *
89 * You may want to talloc_unlink any current reference if you no
90 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070091static void
Carl Worth808401f2010-05-25 14:52:43 -070092_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070093
Carl Worth5aa7ea02010-05-25 18:39:43 -070094static void
Carl Worth47252442010-05-19 13:54:37 -070095_token_list_append_list (token_list_t *list, token_list_t *tail);
96
Carl Worth22b3ace2010-06-02 15:32:03 -070097static active_list_t *
98_active_list_push (active_list_t *list,
99 const char *identifier,
100 token_node_t *marker);
101
102static active_list_t *
103_active_list_pop (active_list_t *list);
104
105int
106_active_list_contains (active_list_t *list, const char *identifier);
107
Carl Worth5aa7ea02010-05-25 18:39:43 -0700108static void
Carl Worth681afbc2010-05-28 15:06:02 -0700109_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
110 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700111
Carl Worthaaa9acb2010-05-19 13:28:24 -0700112static void
Carl Worth681afbc2010-05-28 15:06:02 -0700113_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
114 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700115
116static void
Kenneth Graunke07745232010-06-17 12:41:46 -0700117_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
118 int condition);
Carl Worthb20d33c2010-05-20 22:27:07 -0700119
120static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700121_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
122 const char *type, int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700123
Carl Worthb20d33c2010-05-20 22:27:07 -0700124static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700125_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
Carl Worthb20d33c2010-05-20 22:27:07 -0700126
Carl Worth0293b2e2010-05-19 10:05:40 -0700127#define yylex glcpp_parser_lex
128
Carl Worth8f38aff2010-05-19 10:01:29 -0700129static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700130glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700131
Carl Worth8e82fcb2010-05-26 11:15:21 -0700132static void
133glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
134
Carl Worth3a37b872010-05-10 11:44:09 -0700135%}
136
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700137%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700138%error-verbose
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700139%locations
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700140
Carl Worth0b27b5f2010-05-10 16:16:06 -0700141%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700142%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700143
Carl Worth050e3de2010-05-27 14:36:29 -0700144%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 IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE
Carl Worth8fed1cd2010-05-26 09:32:12 -0700145%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700146%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700147%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700148%type <string_list> identifier_list
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700149%type <token> preprocessing_token conditional_token
150%type <token_list> pp_tokens replacement_list text_line conditional_tokens
Carl Worth8fed1cd2010-05-26 09:32:12 -0700151%left OR
152%left AND
153%left '|'
154%left '^'
155%left '&'
156%left EQUAL NOT_EQUAL
157%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
158%left LEFT_SHIFT RIGHT_SHIFT
159%left '+' '-'
160%left '*' '/' '%'
161%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700162
163%%
164
Carl Worth33cc4002010-05-12 12:17:10 -0700165input:
Carl Worth3ff81672010-05-25 13:09:03 -0700166 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700167| input line
168;
169
170line:
171 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700172 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700173 }
Carl Worth808401f2010-05-25 14:52:43 -0700174| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700175 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700176 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700177 talloc_free ($1);
178 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700179| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700180| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700181;
182
Carl Worth8e82fcb2010-05-26 11:15:21 -0700183expanded_line:
184 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700185 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700186 }
187| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700188 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700189 }
190;
191
Carl Worth3ff81672010-05-25 13:09:03 -0700192control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700193 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700194 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700195 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700196| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700197 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700198 }
199| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700200 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700201 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700202| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700203 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700204 if (macro) {
Carl Worth61ebc012010-07-19 18:02:12 -0700205 hash_table_remove (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700206 talloc_free (macro);
207 }
208 talloc_free ($2);
209 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700210| HASH_IF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700211 token_list_t *expanded;
212 token_t *token;
213
214 expanded = _token_list_create (parser);
215 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
216 _token_list_append (expanded, token);
217 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700218 _glcpp_parser_expand_token_list (parser, $2);
219 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700220 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700221 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700222| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700223 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700224 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700225 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700226 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700227| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700228 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700229 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700230 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700231 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700232| HASH_ELIF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700233 token_list_t *expanded;
234 token_t *token;
235
236 expanded = _token_list_create (parser);
237 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
238 _token_list_append (expanded, token);
239 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700240 _glcpp_parser_expand_token_list (parser, $2);
241 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700242 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700243 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700244| HASH_ELIF NEWLINE {
245 /* #elif without an expression results in a warning if the
246 * condition doesn't matter (we just handled #if 1 or such)
247 * but an error otherwise. */
248 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
249 parser->skip_stack->type = SKIP_TO_ENDIF;
250 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
251 } else {
252 glcpp_error(& @1, parser, "#elif needs an expression");
253 }
254 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700255| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700256 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700257 }
258| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700259 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700260 }
Carl Worth3ff81672010-05-25 13:09:03 -0700261| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700262;
263
Carl Worth8fed1cd2010-05-26 09:32:12 -0700264expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700265 INTEGER_STRING {
266 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
267 $$ = strtoll ($1 + 2, NULL, 16);
268 } else if ($1[0] == '0') {
269 $$ = strtoll ($1, NULL, 8);
270 } else {
271 $$ = strtoll ($1, NULL, 10);
272 }
273 }
274| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700275 $$ = $1;
276 }
277| expression OR expression {
278 $$ = $1 || $3;
279 }
280| expression AND expression {
281 $$ = $1 && $3;
282 }
283| expression '|' expression {
284 $$ = $1 | $3;
285 }
286| expression '^' expression {
287 $$ = $1 ^ $3;
288 }
289| expression '&' expression {
290 $$ = $1 & $3;
291 }
292| expression NOT_EQUAL expression {
293 $$ = $1 != $3;
294 }
295| expression EQUAL expression {
296 $$ = $1 == $3;
297 }
298| expression GREATER_OR_EQUAL expression {
299 $$ = $1 >= $3;
300 }
301| expression LESS_OR_EQUAL expression {
302 $$ = $1 <= $3;
303 }
304| expression '>' expression {
305 $$ = $1 > $3;
306 }
307| expression '<' expression {
308 $$ = $1 < $3;
309 }
310| expression RIGHT_SHIFT expression {
311 $$ = $1 >> $3;
312 }
313| expression LEFT_SHIFT expression {
314 $$ = $1 << $3;
315 }
316| expression '-' expression {
317 $$ = $1 - $3;
318 }
319| expression '+' expression {
320 $$ = $1 + $3;
321 }
322| expression '%' expression {
323 $$ = $1 % $3;
324 }
325| expression '/' expression {
326 $$ = $1 / $3;
327 }
328| expression '*' expression {
329 $$ = $1 * $3;
330 }
331| '!' expression %prec UNARY {
332 $$ = ! $2;
333 }
334| '~' expression %prec UNARY {
335 $$ = ~ $2;
336 }
337| '-' expression %prec UNARY {
338 $$ = - $2;
339 }
340| '+' expression %prec UNARY {
341 $$ = + $2;
342 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700343| '(' expression ')' {
344 $$ = $2;
345 }
346;
347
Carl Worth3ff81672010-05-25 13:09:03 -0700348identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700349 IDENTIFIER {
350 $$ = _string_list_create (parser);
351 _string_list_append_item ($$, $1);
352 talloc_steal ($$, $1);
353 }
354| identifier_list ',' IDENTIFIER {
355 $$ = $1;
356 _string_list_append_item ($$, $3);
357 talloc_steal ($$, $3);
358 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700359;
360
Carl Worth3ff81672010-05-25 13:09:03 -0700361text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700362 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700363| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700364;
365
Carl Worth3ff81672010-05-25 13:09:03 -0700366non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700367 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700368 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700369 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700370;
371
Carl Worthaaa9acb2010-05-19 13:28:24 -0700372replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700373 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700374| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700375;
376
Kenneth Graunke65875742010-06-18 20:08:15 -0700377junk:
378 /* empty */
379| pp_tokens {
380 glcpp_warning(&@1, parser, "extra tokens at end of directive");
381 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700382;
383
384conditional_token:
385 /* Handle "defined" operator */
386 DEFINED IDENTIFIER {
387 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
388 $$ = _token_create_ival (parser, INTEGER, v);
389 }
390| DEFINED '(' IDENTIFIER ')' {
391 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
392 $$ = _token_create_ival (parser, INTEGER, v);
393 }
394| preprocessing_token
395;
396
397conditional_tokens:
398 /* Exactly the same as pp_tokens, but using conditional_token */
399 conditional_token {
400 parser->space_tokens = 1;
401 $$ = _token_list_create (parser);
402 _token_list_append ($$, $1);
403 talloc_unlink (parser, $1);
404 }
405| conditional_tokens conditional_token {
406 $$ = $1;
407 _token_list_append ($$, $2);
408 talloc_unlink (parser, $2);
409 }
410;
Kenneth Graunke65875742010-06-18 20:08:15 -0700411
Carl Worthaaa9acb2010-05-19 13:28:24 -0700412pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700413 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700414 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700415 $$ = _token_list_create (parser);
416 _token_list_append ($$, $1);
417 talloc_unlink (parser, $1);
418 }
419| pp_tokens preprocessing_token {
420 $$ = $1;
421 _token_list_append ($$, $2);
422 talloc_unlink (parser, $2);
423 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700424;
425
Carl Worth3ff81672010-05-25 13:09:03 -0700426preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700427 IDENTIFIER {
428 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700429 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700430 }
Carl Worth050e3de2010-05-27 14:36:29 -0700431| INTEGER_STRING {
432 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700433 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700434 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700435| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700436 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700437 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700438 }
439| OTHER {
440 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700441 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700442 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700443| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700444 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700445 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700446 }
Carl Worth3ff81672010-05-25 13:09:03 -0700447;
448
Carl Worth8e82fcb2010-05-26 11:15:21 -0700449operator:
Carl Worth808401f2010-05-25 14:52:43 -0700450 '[' { $$ = '['; }
451| ']' { $$ = ']'; }
452| '(' { $$ = '('; }
453| ')' { $$ = ')'; }
454| '{' { $$ = '{'; }
455| '}' { $$ = '}'; }
456| '.' { $$ = '.'; }
457| '&' { $$ = '&'; }
458| '*' { $$ = '*'; }
459| '+' { $$ = '+'; }
460| '-' { $$ = '-'; }
461| '~' { $$ = '~'; }
462| '!' { $$ = '!'; }
463| '/' { $$ = '/'; }
464| '%' { $$ = '%'; }
465| LEFT_SHIFT { $$ = LEFT_SHIFT; }
466| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
467| '<' { $$ = '<'; }
468| '>' { $$ = '>'; }
469| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
470| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
471| EQUAL { $$ = EQUAL; }
472| NOT_EQUAL { $$ = NOT_EQUAL; }
473| '^' { $$ = '^'; }
474| '|' { $$ = '|'; }
475| AND { $$ = AND; }
476| OR { $$ = OR; }
477| ';' { $$ = ';'; }
478| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700479| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700480| PASTE { $$ = PASTE; }
Carl Worth3ff81672010-05-25 13:09:03 -0700481;
482
Carl Worth33cc4002010-05-12 12:17:10 -0700483%%
484
Carl Worth610053b2010-05-14 10:05:11 -0700485string_list_t *
486_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700487{
Carl Worth610053b2010-05-14 10:05:11 -0700488 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700489
Carl Worth610053b2010-05-14 10:05:11 -0700490 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700491 list->head = NULL;
492 list->tail = NULL;
493
494 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700495}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700496
Carl Worth33cc4002010-05-12 12:17:10 -0700497void
Carl Worth610053b2010-05-14 10:05:11 -0700498_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700499{
Carl Worth610053b2010-05-14 10:05:11 -0700500 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700501
Carl Worth610053b2010-05-14 10:05:11 -0700502 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700503 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700504
Carl Worth33cc4002010-05-12 12:17:10 -0700505 node->next = NULL;
506
507 if (list->head == NULL) {
508 list->head = node;
509 } else {
510 list->tail->next = node;
511 }
512
513 list->tail = node;
514}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700515
516int
Carl Worth610053b2010-05-14 10:05:11 -0700517_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700518{
Carl Worth610053b2010-05-14 10:05:11 -0700519 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700520 int i;
521
522 if (list == NULL)
523 return 0;
524
525 for (i = 0, node = list->head; node; i++, node = node->next) {
526 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700527 if (index)
528 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700529 return 1;
530 }
531 }
532
533 return 0;
534}
535
536int
Carl Worth610053b2010-05-14 10:05:11 -0700537_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700538{
539 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700540 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700541
542 if (list == NULL)
543 return 0;
544
545 for (node = list->head; node; node = node->next)
546 length++;
547
548 return length;
549}
550
Carl Worth8f6a8282010-05-14 10:44:19 -0700551argument_list_t *
552_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700553{
Carl Worth8f6a8282010-05-14 10:44:19 -0700554 argument_list_t *list;
555
556 list = xtalloc (ctx, argument_list_t);
557 list->head = NULL;
558 list->tail = NULL;
559
560 return list;
561}
562
563void
Carl Worth47252442010-05-19 13:54:37 -0700564_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700565{
566 argument_node_t *node;
567
Carl Worth8f6a8282010-05-14 10:44:19 -0700568 node = xtalloc (list, argument_node_t);
569 node->argument = argument;
570
571 node->next = NULL;
572
573 if (list->head == NULL) {
574 list->head = node;
575 } else {
576 list->tail->next = node;
577 }
578
579 list->tail = node;
580}
581
582int
583_argument_list_length (argument_list_t *list)
584{
585 int length = 0;
586 argument_node_t *node;
587
588 if (list == NULL)
589 return 0;
590
591 for (node = list->head; node; node = node->next)
592 length++;
593
594 return length;
595}
596
Carl Worth47252442010-05-19 13:54:37 -0700597token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700598_argument_list_member_at (argument_list_t *list, int index)
599{
600 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700601 int i;
602
603 if (list == NULL)
604 return NULL;
605
606 node = list->head;
607 for (i = 0; i < index; i++) {
608 node = node->next;
609 if (node == NULL)
610 break;
611 }
612
613 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700614 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700615
616 return NULL;
617}
Carl Worth47252442010-05-19 13:54:37 -0700618
Carl Worth808401f2010-05-25 14:52:43 -0700619/* Note: This function talloc_steal()s the str pointer. */
620token_t *
621_token_create_str (void *ctx, int type, char *str)
622{
623 token_t *token;
624
625 token = xtalloc (ctx, token_t);
626 token->type = type;
627 token->value.str = talloc_steal (token, str);
628
629 return token;
630}
631
632token_t *
633_token_create_ival (void *ctx, int type, int ival)
634{
635 token_t *token;
636
637 token = xtalloc (ctx, token_t);
638 token->type = type;
639 token->value.ival = ival;
640
641 return token;
642}
643
Carl Worth47252442010-05-19 13:54:37 -0700644token_list_t *
645_token_list_create (void *ctx)
646{
647 token_list_t *list;
648
649 list = xtalloc (ctx, token_list_t);
650 list->head = NULL;
651 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700652 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700653
654 return list;
655}
656
657void
Carl Worth808401f2010-05-25 14:52:43 -0700658_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700659{
660 token_node_t *node;
661
662 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700663 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700664
665 node->next = NULL;
666
667 if (list->head == NULL) {
668 list->head = node;
669 } else {
670 list->tail->next = node;
671 }
672
673 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700674 if (token->type != SPACE)
675 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700676}
677
678void
679_token_list_append_list (token_list_t *list, token_list_t *tail)
680{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700681 if (tail == NULL || tail->head == NULL)
682 return;
683
Carl Worth47252442010-05-19 13:54:37 -0700684 if (list->head == NULL) {
685 list->head = tail->head;
686 } else {
687 list->tail->next = tail->head;
688 }
689
690 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700691 list->non_space_tail = tail->non_space_tail;
692}
693
Carl Worthd80dcaf2010-07-20 15:55:21 -0700694static token_list_t *
Carl Worth681afbc2010-05-28 15:06:02 -0700695_token_list_copy (void *ctx, token_list_t *other)
696{
697 token_list_t *copy;
698 token_node_t *node;
699
700 if (other == NULL)
701 return NULL;
702
703 copy = _token_list_create (ctx);
704 for (node = other->head; node; node = node->next)
705 _token_list_append (copy, node->token);
706
707 return copy;
708}
709
Carl Worthd80dcaf2010-07-20 15:55:21 -0700710static void
Carl Worth10ae4382010-05-25 20:35:01 -0700711_token_list_trim_trailing_space (token_list_t *list)
712{
713 token_node_t *tail, *next;
714
715 if (list->non_space_tail) {
716 tail = list->non_space_tail->next;
717 list->non_space_tail->next = NULL;
718 list->tail = list->non_space_tail;
719
720 while (tail) {
721 next = tail->next;
722 talloc_free (tail);
723 tail = next;
724 }
725 }
Carl Worth47252442010-05-19 13:54:37 -0700726}
Carl Worth80dc60b2010-05-25 14:42:00 -0700727
Carl Worth0197e9b2010-05-26 08:05:19 -0700728static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700729_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700730{
731 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700732 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700733 return;
734 }
735
736 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700737 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700738 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700739 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700740 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700741 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700742 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700743 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700744 break;
745 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700746 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700747 break;
748 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700749 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700750 break;
751 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700752 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700753 break;
754 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700755 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700756 break;
757 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700758 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700759 break;
760 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700761 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700762 break;
763 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700764 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700765 break;
766 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700767 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700768 break;
769 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700770 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700771 break;
772 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700773 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700774 break;
Carl Worthdd749002010-05-27 10:12:33 -0700775 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700776 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700777 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700778 case PLACEHOLDER:
779 /* Nothing to print. */
780 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700781 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700782 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700783 break;
784 }
785}
786
Carl Worthb06096e2010-05-29 05:54:19 -0700787/* Return a new token (talloc()ed off of 'token') formed by pasting
788 * 'token' and 'other'. Note that this function may return 'token' or
789 * 'other' directly rather than allocating anything new.
790 *
791 * Caution: Only very cursory error-checking is performed to see if
792 * the final result is a valid single token. */
793static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700794_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700795{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700796 token_t *combined = NULL;
797
Carl Worth85b50e82010-05-27 14:01:18 -0700798 /* Pasting a placeholder onto anything makes no change. */
799 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700800 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700801
Carl Worthb06096e2010-05-29 05:54:19 -0700802 /* When 'token' is a placeholder, just return 'other'. */
803 if (token->type == PLACEHOLDER)
804 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700805
Carl Worthad0dee62010-05-26 09:04:50 -0700806 /* A very few single-character punctuators can be combined
807 * with another to form a multi-character punctuator. */
808 switch (token->type) {
809 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700810 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700811 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700812 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700813 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700814 break;
815 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700816 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700817 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700818 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700819 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700820 break;
821 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700822 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700823 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700824 break;
825 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700826 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700827 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700828 break;
829 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700830 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700831 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700832 break;
833 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700834 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700835 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700836 break;
837 }
838
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700839 if (combined != NULL) {
840 /* Inherit the location from the first token */
841 combined->location = token->location;
842 return combined;
843 }
844
Carl Worthad0dee62010-05-26 09:04:50 -0700845 /* Two string-valued tokens can usually just be mashed
846 * together.
847 *
Carl Worth050e3de2010-05-27 14:36:29 -0700848 * XXX: This isn't actually legitimate. Several things here
849 * should result in a diagnostic since the result cannot be a
850 * valid, single pre-processing token. For example, pasting
851 * "123" and "abc" is not legal, but we don't catch that
852 * here. */
853 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
854 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700855 {
Carl Worthb06096e2010-05-29 05:54:19 -0700856 char *str;
857
858 str = xtalloc_asprintf (token, "%s%s",
859 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700860 combined = _token_create_str (token, token->type, str);
861 combined->location = token->location;
862 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700863 }
864
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700865 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700866 glcpp_print (parser->info_log, "Pasting \"");
867 _token_print (&parser->info_log, token);
868 glcpp_print (parser->info_log, "\" and \"");
869 _token_print (&parser->info_log, other);
870 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700871
872 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700873}
874
Carl Worth0197e9b2010-05-26 08:05:19 -0700875static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700876_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700877{
878 token_node_t *node;
879
880 if (list == NULL)
881 return;
882
883 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700884 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700885}
886
Carl Worth3a37b872010-05-10 11:44:09 -0700887void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700888yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700889{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700890 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700891}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700892
Carl Worth33cc4002010-05-12 12:17:10 -0700893glcpp_parser_t *
Ian Romanick06143ea2010-06-30 16:27:22 -0700894glcpp_parser_create (const struct gl_extensions *extensions)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700895{
Carl Worth33cc4002010-05-12 12:17:10 -0700896 glcpp_parser_t *parser;
Ian Romanick2d122362010-06-30 16:03:19 -0700897 token_t *tok;
898 token_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700899
Carl Worth5070a202010-05-12 12:45:33 -0700900 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700901
Carl Worth8f38aff2010-05-19 10:01:29 -0700902 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700903 parser->defines = hash_table_ctor (32, hash_table_string_hash,
904 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700905 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700906 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700907 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700908 parser->newline_as_space = 0;
909 parser->in_control_line = 0;
910 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700911
Carl Worthb20d33c2010-05-20 22:27:07 -0700912 parser->skip_stack = NULL;
913
Carl Worth8e82fcb2010-05-26 11:15:21 -0700914 parser->lex_from_list = NULL;
915 parser->lex_from_node = NULL;
916
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700917 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700918 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke1b85c462010-06-21 13:55:12 -0700919 parser->error = 0;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700920
Ian Romanick2d122362010-06-30 16:03:19 -0700921 /* Add pre-defined macros. */
922 tok = _token_create_ival (parser, INTEGER, 1);
923
924 list = _token_list_create(parser);
925 _token_list_append(list, tok);
926 _define_object_macro(parser, NULL, "GL_ARB_draw_buffers", list);
927
928 list = _token_list_create(parser);
929 _token_list_append(list, tok);
930 _define_object_macro(parser, NULL, "GL_ARB_texture_rectangle", list);
931
Ian Romanick06143ea2010-06-30 16:27:22 -0700932 if ((extensions != NULL) && extensions->EXT_texture_array) {
933 list = _token_list_create(parser);
934 _token_list_append(list, tok);
935 _define_object_macro(parser, NULL,
936 "GL_EXT_texture_array", list);
937 }
938
Ian Romanick2d122362010-06-30 16:03:19 -0700939 talloc_unlink(parser, tok);
940
Carl Worth33cc4002010-05-12 12:17:10 -0700941 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700942}
943
944int
945glcpp_parser_parse (glcpp_parser_t *parser)
946{
947 return yyparse (parser);
948}
949
950void
Carl Worth33cc4002010-05-12 12:17:10 -0700951glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700952{
Carl Worthb20d33c2010-05-20 22:27:07 -0700953 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700954 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700955 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700956 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700957 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700958}
Carl Worthc6d5af32010-05-11 12:30:09 -0700959
Carl Worthb1854fd2010-05-25 16:28:26 -0700960typedef enum function_status
961{
962 FUNCTION_STATUS_SUCCESS,
963 FUNCTION_NOT_A_FUNCTION,
964 FUNCTION_UNBALANCED_PARENTHESES
965} function_status_t;
966
967/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700968 * balanced set of parentheses.
969 *
970 * When called, 'node' should be the opening-parenthesis token, (or
971 * perhaps preceeding SPACE tokens). Upon successful return *last will
972 * be the last consumed node, (corresponding to the closing right
973 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700974 *
975 * Return values:
976 *
977 * FUNCTION_STATUS_SUCCESS:
978 *
979 * Successfully parsed a set of function arguments.
980 *
981 * FUNCTION_NOT_A_FUNCTION:
982 *
983 * Macro name not followed by a '('. This is not an error, but
984 * simply that the macro name should be treated as a non-macro.
985 *
Carl Worth14c98a52010-06-02 15:49:54 -0700986 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -0700987 *
988 * Macro name is not followed by a balanced set of parentheses.
989 */
990static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700991_arguments_parse (argument_list_t *arguments,
992 token_node_t *node,
993 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700994{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700995 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700996 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700997
Carl Worthb1854fd2010-05-25 16:28:26 -0700998 node = node->next;
999
1000 /* Ignore whitespace before first parenthesis. */
1001 while (node && node->token->type == SPACE)
1002 node = node->next;
1003
1004 if (node == NULL || node->token->type != '(')
1005 return FUNCTION_NOT_A_FUNCTION;
1006
Carl Worth652fa272010-05-25 17:45:22 -07001007 node = node->next;
1008
Carl Wortha19297b2010-05-27 13:29:19 -07001009 argument = _token_list_create (arguments);
1010 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001011
Carl Worth681afbc2010-05-28 15:06:02 -07001012 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001013 if (node->token->type == '(')
1014 {
1015 paren_count++;
1016 }
1017 else if (node->token->type == ')')
1018 {
1019 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001020 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001021 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001022 }
Carl Worth652fa272010-05-25 17:45:22 -07001023
1024 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001025 paren_count == 1)
1026 {
Carl Wortha19297b2010-05-27 13:29:19 -07001027 _token_list_trim_trailing_space (argument);
1028 argument = _token_list_create (arguments);
1029 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001030 }
1031 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001032 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001033 /* Don't treat initial whitespace as
1034 * part of the arguement. */
1035 if (node->token->type == SPACE)
1036 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001037 }
1038 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001039 }
Carl Worthc7581c22010-05-25 17:41:07 -07001040 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001041
Carl Worth681afbc2010-05-28 15:06:02 -07001042 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001043 return FUNCTION_UNBALANCED_PARENTHESES;
1044
Carl Worth681afbc2010-05-28 15:06:02 -07001045 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001046
1047 return FUNCTION_STATUS_SUCCESS;
1048}
1049
Carl Worthe1acbfc2010-07-20 16:44:03 -07001050static token_list_t *
1051_token_list_create_with_one_space (void *ctx)
1052{
1053 token_list_t *list;
1054 token_t *space;
1055
1056 list = _token_list_create (ctx);
1057 space = _token_create_ival (list, SPACE, SPACE);
1058 _token_list_append (list, space);
1059
1060 return list;
1061}
1062
Carl Worth681afbc2010-05-28 15:06:02 -07001063/* This is a helper function that's essentially part of the
1064 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1065 * except for by that function.
1066 *
1067 * Returns NULL if node is a simple token with no expansion, (that is,
1068 * although 'node' corresponds to an identifier defined as a
1069 * function-like macro, it is not followed with a parenthesized
1070 * argument list).
1071 *
1072 * Compute the complete expansion of node (which is a function-like
1073 * macro) and subsequent nodes which are arguments.
1074 *
1075 * Returns the token list that results from the expansion and sets
1076 * *last to the last node in the list that was consumed by the
Carl Worthfbe42402010-07-22 16:36:04 -07001077 * expansion. Specifically, *last will be set as follows: as the
Carl Worth681afbc2010-05-28 15:06:02 -07001078 * token of the closing right parenthesis.
1079 */
1080static token_list_t *
1081_glcpp_parser_expand_function (glcpp_parser_t *parser,
1082 token_node_t *node,
1083 token_node_t **last)
1084
Carl Worthb1854fd2010-05-25 16:28:26 -07001085{
1086 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001087 const char *identifier;
1088 argument_list_t *arguments;
1089 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001090 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001091 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001092
Carl Worthb1854fd2010-05-25 16:28:26 -07001093 identifier = node->token->value.str;
1094
1095 macro = hash_table_find (parser->defines, identifier);
1096
1097 assert (macro->is_function);
1098
Carl Worth9ce18cf2010-05-25 17:32:21 -07001099 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001100 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001101
1102 switch (status) {
1103 case FUNCTION_STATUS_SUCCESS:
1104 break;
1105 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001106 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001107 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001108 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001109 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001110 }
1111
Carl Worthe1acbfc2010-07-20 16:44:03 -07001112 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth9ce18cf2010-05-25 17:32:21 -07001113 if (macro->replacements == NULL) {
1114 talloc_free (arguments);
Carl Worthe1acbfc2010-07-20 16:44:03 -07001115 return _token_list_create_with_one_space (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001116 }
1117
Carl Wortha19297b2010-05-27 13:29:19 -07001118 if (! ((_argument_list_length (arguments) ==
1119 _string_list_length (macro->parameters)) ||
1120 (_string_list_length (macro->parameters) == 0 &&
1121 _argument_list_length (arguments) == 1 &&
1122 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001123 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001124 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001125 "Error: macro %s invoked with %d arguments (expected %d)\n",
1126 identifier,
1127 _argument_list_length (arguments),
1128 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001129 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001130 }
1131
Carl Worth0197e9b2010-05-26 08:05:19 -07001132 /* Perform argument substitution on the replacement list. */
1133 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001134
Carl Worthce540f22010-05-26 08:25:44 -07001135 for (node = macro->replacements->head; node; node = node->next)
1136 {
1137 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001138 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001139 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001140 &parameter_index))
1141 {
1142 token_list_t *argument;
1143 argument = _argument_list_member_at (arguments,
1144 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001145 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001146 * tokens, or append a placeholder token for
1147 * an empty argument. */
1148 if (argument->head) {
Carl Worthfbe42402010-07-22 16:36:04 -07001149 token_list_t *expanded_argument;
1150 expanded_argument = _token_list_copy (parser,
1151 argument);
Carl Worth681afbc2010-05-28 15:06:02 -07001152 _glcpp_parser_expand_token_list (parser,
Carl Worthfbe42402010-07-22 16:36:04 -07001153 expanded_argument);
1154 _token_list_append_list (substituted,
1155 expanded_argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001156 } else {
1157 token_t *new_token;
1158
1159 new_token = _token_create_ival (substituted,
1160 PLACEHOLDER,
1161 PLACEHOLDER);
1162 _token_list_append (substituted, new_token);
1163 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001164 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001165 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001166 }
1167 }
1168
Carl Worthad0dee62010-05-26 09:04:50 -07001169 /* After argument substitution, and before further expansion
1170 * below, implement token pasting. */
1171
Carl Worthb06096e2010-05-29 05:54:19 -07001172 _token_list_trim_trailing_space (substituted);
1173
Carl Worthad0dee62010-05-26 09:04:50 -07001174 node = substituted->head;
1175 while (node)
1176 {
1177 token_node_t *next_non_space;
1178
1179 /* Look ahead for a PASTE token, skipping space. */
1180 next_non_space = node->next;
1181 while (next_non_space && next_non_space->token->type == SPACE)
1182 next_non_space = next_non_space->next;
1183
1184 if (next_non_space == NULL)
1185 break;
1186
1187 if (next_non_space->token->type != PASTE) {
1188 node = next_non_space;
1189 continue;
1190 }
1191
1192 /* Now find the next non-space token after the PASTE. */
1193 next_non_space = next_non_space->next;
1194 while (next_non_space && next_non_space->token->type == SPACE)
1195 next_non_space = next_non_space->next;
1196
1197 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001198 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001199 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001200 }
1201
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001202 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001203 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001204 if (next_non_space == substituted->tail)
1205 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001206
1207 node = node->next;
1208 }
1209
Carl Worthb06096e2010-05-29 05:54:19 -07001210 substituted->non_space_tail = substituted->tail;
1211
Carl Worth681afbc2010-05-28 15:06:02 -07001212 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001213}
1214
Carl Worth681afbc2010-05-28 15:06:02 -07001215/* Compute the complete expansion of node, (and subsequent nodes after
1216 * 'node' in the case that 'node' is a function-like macro and
1217 * subsequent nodes are arguments).
1218 *
1219 * Returns NULL if node is a simple token with no expansion.
1220 *
1221 * Otherwise, returns the token list that results from the expansion
1222 * and sets *last to the last node in the list that was consumed by
Carl Worthe1acbfc2010-07-20 16:44:03 -07001223 * the expansion. Specifically, *last will be set as follows:
Carl Worth681afbc2010-05-28 15:06:02 -07001224 *
1225 * As 'node' in the case of object-like macro expansion.
1226 *
1227 * As the token of the closing right parenthesis in the case of
1228 * function-like macro expansion.
1229 */
1230static token_list_t *
1231_glcpp_parser_expand_node (glcpp_parser_t *parser,
1232 token_node_t *node,
1233 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001234{
Carl Worth681afbc2010-05-28 15:06:02 -07001235 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001236 const char *identifier;
1237 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001238
1239 /* We only expand identifiers */
1240 if (token->type != IDENTIFIER) {
1241 /* We change any COMMA into a COMMA_FINAL to prevent
1242 * it being mistaken for an argument separator
1243 * later. */
1244 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001245 token->type = COMMA_FINAL;
1246 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001247 }
Carl Worth681afbc2010-05-28 15:06:02 -07001248
1249 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001250 }
1251
1252 /* Look up this identifier in the hash table. */
1253 identifier = token->value.str;
1254 macro = hash_table_find (parser->defines, identifier);
1255
Carl Worth681afbc2010-05-28 15:06:02 -07001256 /* Not a macro, so no expansion needed. */
1257 if (macro == NULL)
1258 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001259
1260 /* Finally, don't expand this macro if we're already actively
1261 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001262 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001263 /* We change the token type here from IDENTIFIER to
1264 * OTHER to prevent any future expansion of this
1265 * unexpanded token. */
1266 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001267 token_list_t *expansion;
1268 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001269
Carl Worth681afbc2010-05-28 15:06:02 -07001270 str = xtalloc_strdup (parser, token->value.str);
1271 final = _token_create_str (parser, OTHER, str);
1272 expansion = _token_list_create (parser);
1273 _token_list_append (expansion, final);
1274 *last = node;
1275 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001276 }
1277
Carl Worth681afbc2010-05-28 15:06:02 -07001278 if (! macro->is_function)
1279 {
1280 *last = node;
1281
Carl Worthe1acbfc2010-07-20 16:44:03 -07001282 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth681afbc2010-05-28 15:06:02 -07001283 if (macro->replacements == NULL)
Carl Worthe1acbfc2010-07-20 16:44:03 -07001284 return _token_list_create_with_one_space (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001285
Carl Worth22b3ace2010-06-02 15:32:03 -07001286 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001287 }
Carl Worth681afbc2010-05-28 15:06:02 -07001288
1289 return _glcpp_parser_expand_function (parser, node, last);
1290}
1291
Carl Worth22b3ace2010-06-02 15:32:03 -07001292/* Push a new identifier onto the active list, returning the new list.
1293 *
1294 * Here, 'marker' is the token node that appears in the list after the
1295 * expansion of 'identifier'. That is, when the list iterator begins
1296 * examinging 'marker', then it is time to pop this node from the
1297 * active stack.
1298 */
1299active_list_t *
1300_active_list_push (active_list_t *list,
1301 const char *identifier,
1302 token_node_t *marker)
1303{
1304 active_list_t *node;
1305
1306 node = xtalloc (list, active_list_t);
1307 node->identifier = xtalloc_strdup (node, identifier);
1308 node->marker = marker;
1309 node->next = list;
1310
1311 return node;
1312}
1313
1314active_list_t *
1315_active_list_pop (active_list_t *list)
1316{
1317 active_list_t *node = list;
1318
1319 if (node == NULL)
1320 return NULL;
1321
1322 node = list->next;
1323 talloc_free (list);
1324
1325 return node;
1326}
1327
1328int
1329_active_list_contains (active_list_t *list, const char *identifier)
1330{
1331 active_list_t *node;
1332
1333 if (list == NULL)
1334 return 0;
1335
1336 for (node = list; node; node = node->next)
1337 if (strcmp (node->identifier, identifier) == 0)
1338 return 1;
1339
1340 return 0;
1341}
1342
Carl Worth681afbc2010-05-28 15:06:02 -07001343/* Walk over the token list replacing nodes with their expansion.
1344 * Whenever nodes are expanded the walking will walk over the new
1345 * nodes, continuing to expand as necessary. The results are placed in
1346 * 'list' itself;
1347 */
1348static void
1349_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1350 token_list_t *list)
1351{
1352 token_node_t *node_prev;
Carl Worthc42e6402010-06-22 15:51:34 -07001353 token_node_t *node, *last = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001354 token_list_t *expansion;
1355
1356 if (list == NULL)
1357 return;
1358
1359 _token_list_trim_trailing_space (list);
1360
1361 node_prev = NULL;
1362 node = list->head;
1363
1364 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001365
1366 while (parser->active && parser->active->marker == node)
1367 parser->active = _active_list_pop (parser->active);
1368
Carl Worth681afbc2010-05-28 15:06:02 -07001369 /* Find the expansion for node, which will replace all
1370 * nodes from node to last, inclusive. */
1371 expansion = _glcpp_parser_expand_node (parser, node, &last);
1372 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001373 token_node_t *n;
1374
1375 for (n = node; n != last->next; n = n->next)
1376 while (parser->active &&
1377 parser->active->marker == n)
1378 {
1379 parser->active = _active_list_pop (parser->active);
1380 }
1381
1382 parser->active = _active_list_push (parser->active,
1383 node->token->value.str,
1384 last->next);
1385
Carl Worth681afbc2010-05-28 15:06:02 -07001386 /* Splice expansion into list, supporting a
1387 * simple deletion if the expansion is
1388 * empty. */
1389 if (expansion->head) {
1390 if (node_prev)
1391 node_prev->next = expansion->head;
1392 else
1393 list->head = expansion->head;
1394 expansion->tail->next = last->next;
1395 if (last == list->tail)
1396 list->tail = expansion->tail;
1397 } else {
1398 if (node_prev)
1399 node_prev->next = last->next;
1400 else
1401 list->head = last->next;
1402 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001403 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001404 }
1405 } else {
1406 node_prev = node;
1407 }
1408 node = node_prev ? node_prev->next : list->head;
1409 }
1410
Carl Worth22b3ace2010-06-02 15:32:03 -07001411 while (parser->active)
1412 parser->active = _active_list_pop (parser->active);
1413
Carl Worth681afbc2010-05-28 15:06:02 -07001414 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001415}
1416
Carl Worthae6517f2010-05-25 15:24:59 -07001417void
1418_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1419 token_list_t *list)
1420{
Carl Worthae6517f2010-05-25 15:24:59 -07001421 if (list == NULL)
1422 return;
1423
Carl Worth681afbc2010-05-28 15:06:02 -07001424 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001425
Carl Worth681afbc2010-05-28 15:06:02 -07001426 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001427
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001428 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001429}
1430
Carl Worthd80dcaf2010-07-20 15:55:21 -07001431static void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001432_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1433 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001434{
1435 /* According to the GLSL specification, macro names starting with "__"
1436 * or "GL_" are reserved for future use. So, don't allow them.
1437 */
1438 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001439 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001440 }
1441 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001442 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001443 }
1444}
1445
1446void
Carl Worthfcbbb462010-05-13 09:36:23 -07001447_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001448 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001449 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001450 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001451{
1452 macro_t *macro;
1453
Ian Romanick2d122362010-06-30 16:03:19 -07001454 if (loc != NULL)
1455 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001456
Carl Worthfcbbb462010-05-13 09:36:23 -07001457 macro = xtalloc (parser, macro_t);
1458
1459 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001460 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001461 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001462 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001463
1464 hash_table_insert (parser->defines, macro, identifier);
1465}
1466
1467void
1468_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001469 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001470 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001471 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001472 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001473{
1474 macro_t *macro;
1475
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001476 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001477
Carl Worthfcbbb462010-05-13 09:36:23 -07001478 macro = xtalloc (parser, macro_t);
1479
1480 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001481 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001482 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001483 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001484
1485 hash_table_insert (parser->defines, macro, identifier);
1486}
1487
Carl Worth8f38aff2010-05-19 10:01:29 -07001488static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001489glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001490{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001491 token_node_t *node;
1492 int ret;
1493
Carl Worth95951ea2010-05-26 15:57:10 -07001494 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001495 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001496
1497 /* XXX: This ugly block of code exists for the sole
1498 * purpose of converting a NEWLINE token into a SPACE
1499 * token, but only in the case where we have seen a
1500 * function-like macro name, but have not yet seen its
1501 * closing parenthesis.
1502 *
1503 * There's perhaps a more compact way to do this with
1504 * mid-rule actions in the grammar.
1505 *
1506 * I'm definitely not pleased with the complexity of
1507 * this code here.
1508 */
1509 if (parser->newline_as_space)
1510 {
1511 if (ret == '(') {
1512 parser->paren_count++;
1513 } else if (ret == ')') {
1514 parser->paren_count--;
1515 if (parser->paren_count == 0)
1516 parser->newline_as_space = 0;
1517 } else if (ret == NEWLINE) {
1518 ret = SPACE;
1519 } else if (ret != SPACE) {
1520 if (parser->paren_count == 0)
1521 parser->newline_as_space = 0;
1522 }
1523 }
1524 else if (parser->in_control_line)
1525 {
1526 if (ret == NEWLINE)
1527 parser->in_control_line = 0;
1528 }
1529 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1530 ret == HASH_UNDEF || ret == HASH_IF ||
1531 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1532 ret == HASH_ELIF || ret == HASH_ELSE ||
1533 ret == HASH_ENDIF || ret == HASH)
1534 {
1535 parser->in_control_line = 1;
1536 }
1537 else if (ret == IDENTIFIER)
1538 {
1539 macro_t *macro;
1540 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001541 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001542 if (macro && macro->is_function) {
1543 parser->newline_as_space = 1;
1544 parser->paren_count = 0;
1545 }
1546 }
1547
1548 return ret;
1549 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001550
1551 node = parser->lex_from_node;
1552
1553 if (node == NULL) {
1554 talloc_free (parser->lex_from_list);
1555 parser->lex_from_list = NULL;
1556 return NEWLINE;
1557 }
1558
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001559 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001560 ret = node->token->type;
1561
1562 parser->lex_from_node = node->next;
1563
1564 return ret;
1565}
1566
1567static void
1568glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1569{
1570 token_node_t *node;
1571
1572 assert (parser->lex_from_list == NULL);
1573
1574 /* Copy list, eliminating any space tokens. */
1575 parser->lex_from_list = _token_list_create (parser);
1576
1577 for (node = list->head; node; node = node->next) {
1578 if (node->token->type == SPACE)
1579 continue;
1580 _token_list_append (parser->lex_from_list, node->token);
1581 }
1582
1583 talloc_free (list);
1584
1585 parser->lex_from_node = parser->lex_from_list->head;
1586
1587 /* It's possible the list consisted of nothing but whitespace. */
1588 if (parser->lex_from_node == NULL) {
1589 talloc_free (parser->lex_from_list);
1590 parser->lex_from_list = NULL;
1591 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001592}
Carl Worthb20d33c2010-05-20 22:27:07 -07001593
1594static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001595_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1596 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001597{
1598 skip_type_t current = SKIP_NO_SKIP;
1599 skip_node_t *node;
1600
1601 if (parser->skip_stack)
1602 current = parser->skip_stack->type;
1603
1604 node = xtalloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001605 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001606
1607 if (current == SKIP_NO_SKIP) {
1608 if (condition)
1609 node->type = SKIP_NO_SKIP;
1610 else
1611 node->type = SKIP_TO_ELSE;
1612 } else {
1613 node->type = SKIP_TO_ENDIF;
1614 }
1615
1616 node->next = parser->skip_stack;
1617 parser->skip_stack = node;
1618}
1619
1620static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001621_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1622 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001623{
1624 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001625 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001626 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001627 }
1628
1629 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1630 if (condition)
1631 parser->skip_stack->type = SKIP_NO_SKIP;
1632 } else {
1633 parser->skip_stack->type = SKIP_TO_ENDIF;
1634 }
1635}
Carl Worth80dc60b2010-05-25 14:42:00 -07001636
Carl Worthb20d33c2010-05-20 22:27:07 -07001637static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001638_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001639{
1640 skip_node_t *node;
1641
1642 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001643 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001644 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001645 }
1646
1647 node = parser->skip_stack;
1648 parser->skip_stack = node->next;
1649 talloc_free (node);
1650}