blob: e5544fe29b8bbcef596d666289f43b6fc5221d2e [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) {
205 /* XXX: Need hash table to support a real way
206 * to remove an element rather than prefixing
207 * a new node with data of NULL like this. */
208 hash_table_insert (parser->defines, NULL, $2);
209 talloc_free (macro);
210 }
211 talloc_free ($2);
212 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700213| HASH_IF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700214 token_list_t *expanded;
215 token_t *token;
216
217 expanded = _token_list_create (parser);
218 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
219 _token_list_append (expanded, token);
220 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700221 _glcpp_parser_expand_token_list (parser, $2);
222 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700223 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700224 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700225| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700226 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700227 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700228 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700229 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700230| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700231 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700232 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700233 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700234 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700235| HASH_ELIF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700236 token_list_t *expanded;
237 token_t *token;
238
239 expanded = _token_list_create (parser);
240 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
241 _token_list_append (expanded, token);
242 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700243 _glcpp_parser_expand_token_list (parser, $2);
244 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700245 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700246 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700247| HASH_ELIF NEWLINE {
248 /* #elif without an expression results in a warning if the
249 * condition doesn't matter (we just handled #if 1 or such)
250 * but an error otherwise. */
251 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
252 parser->skip_stack->type = SKIP_TO_ENDIF;
253 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
254 } else {
255 glcpp_error(& @1, parser, "#elif needs an expression");
256 }
257 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700258| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700259 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700260 }
261| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700262 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700263 }
Carl Worth3ff81672010-05-25 13:09:03 -0700264| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700265;
266
Carl Worth8fed1cd2010-05-26 09:32:12 -0700267expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700268 INTEGER_STRING {
269 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
270 $$ = strtoll ($1 + 2, NULL, 16);
271 } else if ($1[0] == '0') {
272 $$ = strtoll ($1, NULL, 8);
273 } else {
274 $$ = strtoll ($1, NULL, 10);
275 }
276 }
277| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700278 $$ = $1;
279 }
280| expression OR expression {
281 $$ = $1 || $3;
282 }
283| expression AND expression {
284 $$ = $1 && $3;
285 }
286| expression '|' expression {
287 $$ = $1 | $3;
288 }
289| expression '^' expression {
290 $$ = $1 ^ $3;
291 }
292| expression '&' expression {
293 $$ = $1 & $3;
294 }
295| expression NOT_EQUAL expression {
296 $$ = $1 != $3;
297 }
298| expression EQUAL expression {
299 $$ = $1 == $3;
300 }
301| expression GREATER_OR_EQUAL expression {
302 $$ = $1 >= $3;
303 }
304| expression LESS_OR_EQUAL expression {
305 $$ = $1 <= $3;
306 }
307| expression '>' expression {
308 $$ = $1 > $3;
309 }
310| expression '<' expression {
311 $$ = $1 < $3;
312 }
313| expression RIGHT_SHIFT expression {
314 $$ = $1 >> $3;
315 }
316| expression LEFT_SHIFT 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 '*' expression {
332 $$ = $1 * $3;
333 }
334| '!' expression %prec UNARY {
335 $$ = ! $2;
336 }
337| '~' expression %prec UNARY {
338 $$ = ~ $2;
339 }
340| '-' expression %prec UNARY {
341 $$ = - $2;
342 }
343| '+' expression %prec UNARY {
344 $$ = + $2;
345 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700346| '(' expression ')' {
347 $$ = $2;
348 }
349;
350
Carl Worth3ff81672010-05-25 13:09:03 -0700351identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700352 IDENTIFIER {
353 $$ = _string_list_create (parser);
354 _string_list_append_item ($$, $1);
355 talloc_steal ($$, $1);
356 }
357| identifier_list ',' IDENTIFIER {
358 $$ = $1;
359 _string_list_append_item ($$, $3);
360 talloc_steal ($$, $3);
361 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700362;
363
Carl Worth3ff81672010-05-25 13:09:03 -0700364text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700365 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700366| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700367;
368
Carl Worth3ff81672010-05-25 13:09:03 -0700369non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700370 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700371 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700372 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700373;
374
Carl Worthaaa9acb2010-05-19 13:28:24 -0700375replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700376 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700377| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700378;
379
Kenneth Graunke65875742010-06-18 20:08:15 -0700380junk:
381 /* empty */
382| pp_tokens {
383 glcpp_warning(&@1, parser, "extra tokens at end of directive");
384 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700385;
386
387conditional_token:
388 /* Handle "defined" operator */
389 DEFINED IDENTIFIER {
390 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
391 $$ = _token_create_ival (parser, INTEGER, v);
392 }
393| DEFINED '(' IDENTIFIER ')' {
394 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
395 $$ = _token_create_ival (parser, INTEGER, v);
396 }
397| preprocessing_token
398;
399
400conditional_tokens:
401 /* Exactly the same as pp_tokens, but using conditional_token */
402 conditional_token {
403 parser->space_tokens = 1;
404 $$ = _token_list_create (parser);
405 _token_list_append ($$, $1);
406 talloc_unlink (parser, $1);
407 }
408| conditional_tokens conditional_token {
409 $$ = $1;
410 _token_list_append ($$, $2);
411 talloc_unlink (parser, $2);
412 }
413;
Kenneth Graunke65875742010-06-18 20:08:15 -0700414
Carl Worthaaa9acb2010-05-19 13:28:24 -0700415pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700416 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700417 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700418 $$ = _token_list_create (parser);
419 _token_list_append ($$, $1);
420 talloc_unlink (parser, $1);
421 }
422| pp_tokens preprocessing_token {
423 $$ = $1;
424 _token_list_append ($$, $2);
425 talloc_unlink (parser, $2);
426 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700427;
428
Carl Worth3ff81672010-05-25 13:09:03 -0700429preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700430 IDENTIFIER {
431 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700432 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700433 }
Carl Worth050e3de2010-05-27 14:36:29 -0700434| INTEGER_STRING {
435 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700436 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700437 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700438| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700439 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700440 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700441 }
442| OTHER {
443 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700444 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700445 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700446| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700447 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700448 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700449 }
Carl Worth3ff81672010-05-25 13:09:03 -0700450;
451
Carl Worth8e82fcb2010-05-26 11:15:21 -0700452operator:
Carl Worth808401f2010-05-25 14:52:43 -0700453 '[' { $$ = '['; }
454| ']' { $$ = ']'; }
455| '(' { $$ = '('; }
456| ')' { $$ = ')'; }
457| '{' { $$ = '{'; }
458| '}' { $$ = '}'; }
459| '.' { $$ = '.'; }
460| '&' { $$ = '&'; }
461| '*' { $$ = '*'; }
462| '+' { $$ = '+'; }
463| '-' { $$ = '-'; }
464| '~' { $$ = '~'; }
465| '!' { $$ = '!'; }
466| '/' { $$ = '/'; }
467| '%' { $$ = '%'; }
468| LEFT_SHIFT { $$ = LEFT_SHIFT; }
469| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
470| '<' { $$ = '<'; }
471| '>' { $$ = '>'; }
472| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
473| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
474| EQUAL { $$ = EQUAL; }
475| NOT_EQUAL { $$ = NOT_EQUAL; }
476| '^' { $$ = '^'; }
477| '|' { $$ = '|'; }
478| AND { $$ = AND; }
479| OR { $$ = OR; }
480| ';' { $$ = ';'; }
481| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700482| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700483| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700484| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700485;
486
Carl Worth33cc4002010-05-12 12:17:10 -0700487%%
488
Carl Worth610053b2010-05-14 10:05:11 -0700489string_list_t *
490_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700491{
Carl Worth610053b2010-05-14 10:05:11 -0700492 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700493
Carl Worth610053b2010-05-14 10:05:11 -0700494 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700495 list->head = NULL;
496 list->tail = NULL;
497
498 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700499}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700500
Carl Worth33cc4002010-05-12 12:17:10 -0700501void
Carl Worth610053b2010-05-14 10:05:11 -0700502_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700503{
Carl Worth610053b2010-05-14 10:05:11 -0700504 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700505
Carl Worth610053b2010-05-14 10:05:11 -0700506 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700507 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700508
Carl Worth33cc4002010-05-12 12:17:10 -0700509 node->next = NULL;
510
511 if (list->head == NULL) {
512 list->head = node;
513 } else {
514 list->tail->next = node;
515 }
516
517 list->tail = node;
518}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700519
520int
Carl Worth610053b2010-05-14 10:05:11 -0700521_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700522{
Carl Worth610053b2010-05-14 10:05:11 -0700523 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700524 int i;
525
526 if (list == NULL)
527 return 0;
528
529 for (i = 0, node = list->head; node; i++, node = node->next) {
530 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700531 if (index)
532 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700533 return 1;
534 }
535 }
536
537 return 0;
538}
539
540int
Carl Worth610053b2010-05-14 10:05:11 -0700541_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700542{
543 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700544 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700545
546 if (list == NULL)
547 return 0;
548
549 for (node = list->head; node; node = node->next)
550 length++;
551
552 return length;
553}
554
Carl Worth8f6a8282010-05-14 10:44:19 -0700555argument_list_t *
556_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700557{
Carl Worth8f6a8282010-05-14 10:44:19 -0700558 argument_list_t *list;
559
560 list = xtalloc (ctx, argument_list_t);
561 list->head = NULL;
562 list->tail = NULL;
563
564 return list;
565}
566
567void
Carl Worth47252442010-05-19 13:54:37 -0700568_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700569{
570 argument_node_t *node;
571
Carl Worth8f6a8282010-05-14 10:44:19 -0700572 node = xtalloc (list, argument_node_t);
573 node->argument = argument;
574
575 node->next = NULL;
576
577 if (list->head == NULL) {
578 list->head = node;
579 } else {
580 list->tail->next = node;
581 }
582
583 list->tail = node;
584}
585
586int
587_argument_list_length (argument_list_t *list)
588{
589 int length = 0;
590 argument_node_t *node;
591
592 if (list == NULL)
593 return 0;
594
595 for (node = list->head; node; node = node->next)
596 length++;
597
598 return length;
599}
600
Carl Worth47252442010-05-19 13:54:37 -0700601token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700602_argument_list_member_at (argument_list_t *list, int index)
603{
604 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700605 int i;
606
607 if (list == NULL)
608 return NULL;
609
610 node = list->head;
611 for (i = 0; i < index; i++) {
612 node = node->next;
613 if (node == NULL)
614 break;
615 }
616
617 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700618 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700619
620 return NULL;
621}
Carl Worth47252442010-05-19 13:54:37 -0700622
Carl Worth808401f2010-05-25 14:52:43 -0700623/* Note: This function talloc_steal()s the str pointer. */
624token_t *
625_token_create_str (void *ctx, int type, char *str)
626{
627 token_t *token;
628
629 token = xtalloc (ctx, token_t);
630 token->type = type;
631 token->value.str = talloc_steal (token, str);
632
633 return token;
634}
635
636token_t *
637_token_create_ival (void *ctx, int type, int ival)
638{
639 token_t *token;
640
641 token = xtalloc (ctx, token_t);
642 token->type = type;
643 token->value.ival = ival;
644
645 return token;
646}
647
Carl Worth47252442010-05-19 13:54:37 -0700648token_list_t *
649_token_list_create (void *ctx)
650{
651 token_list_t *list;
652
653 list = xtalloc (ctx, token_list_t);
654 list->head = NULL;
655 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700656 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700657
658 return list;
659}
660
661void
Carl Worth808401f2010-05-25 14:52:43 -0700662_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700663{
664 token_node_t *node;
665
666 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700667 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700668
669 node->next = NULL;
670
671 if (list->head == NULL) {
672 list->head = node;
673 } else {
674 list->tail->next = node;
675 }
676
677 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700678 if (token->type != SPACE)
679 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700680}
681
682void
683_token_list_append_list (token_list_t *list, token_list_t *tail)
684{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700685 if (tail == NULL || tail->head == NULL)
686 return;
687
Carl Worth47252442010-05-19 13:54:37 -0700688 if (list->head == NULL) {
689 list->head = tail->head;
690 } else {
691 list->tail->next = tail->head;
692 }
693
694 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700695 list->non_space_tail = tail->non_space_tail;
696}
697
Carl Worth681afbc2010-05-28 15:06:02 -0700698token_list_t *
699_token_list_copy (void *ctx, token_list_t *other)
700{
701 token_list_t *copy;
702 token_node_t *node;
703
704 if (other == NULL)
705 return NULL;
706
707 copy = _token_list_create (ctx);
708 for (node = other->head; node; node = node->next)
709 _token_list_append (copy, node->token);
710
711 return copy;
712}
713
Carl Worth10ae4382010-05-25 20:35:01 -0700714void
715_token_list_trim_trailing_space (token_list_t *list)
716{
717 token_node_t *tail, *next;
718
719 if (list->non_space_tail) {
720 tail = list->non_space_tail->next;
721 list->non_space_tail->next = NULL;
722 list->tail = list->non_space_tail;
723
724 while (tail) {
725 next = tail->next;
726 talloc_free (tail);
727 tail = next;
728 }
729 }
Carl Worth47252442010-05-19 13:54:37 -0700730}
Carl Worth80dc60b2010-05-25 14:42:00 -0700731
Carl Worth0197e9b2010-05-26 08:05:19 -0700732static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700733_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700734{
735 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700736 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700737 return;
738 }
739
740 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700741 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700742 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700743 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700744 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700745 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700746 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700747 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700748 break;
749 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700750 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700751 break;
752 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700753 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700754 break;
755 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700756 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700757 break;
758 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700759 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700760 break;
761 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700762 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700763 break;
764 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700765 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700766 break;
767 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700768 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700769 break;
770 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700771 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700772 break;
773 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700774 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700775 break;
776 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700777 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700778 break;
Carl Worthdd749002010-05-27 10:12:33 -0700779 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700780 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700781 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700782 case PLACEHOLDER:
783 /* Nothing to print. */
784 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700785 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700786 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700787 break;
788 }
789}
790
Carl Worthb06096e2010-05-29 05:54:19 -0700791/* Return a new token (talloc()ed off of 'token') formed by pasting
792 * 'token' and 'other'. Note that this function may return 'token' or
793 * 'other' directly rather than allocating anything new.
794 *
795 * Caution: Only very cursory error-checking is performed to see if
796 * the final result is a valid single token. */
797static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700798_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700799{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700800 token_t *combined = NULL;
801
Carl Worth85b50e82010-05-27 14:01:18 -0700802 /* Pasting a placeholder onto anything makes no change. */
803 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700804 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700805
Carl Worthb06096e2010-05-29 05:54:19 -0700806 /* When 'token' is a placeholder, just return 'other'. */
807 if (token->type == PLACEHOLDER)
808 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700809
Carl Worthad0dee62010-05-26 09:04:50 -0700810 /* A very few single-character punctuators can be combined
811 * with another to form a multi-character punctuator. */
812 switch (token->type) {
813 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700814 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700815 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700816 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700817 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700818 break;
819 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700820 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700821 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700822 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700823 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_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, EQUAL, 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, NOT_EQUAL, NOT_EQUAL);
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, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700836 break;
837 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700838 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700839 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700840 break;
841 }
842
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700843 if (combined != NULL) {
844 /* Inherit the location from the first token */
845 combined->location = token->location;
846 return combined;
847 }
848
Carl Worthad0dee62010-05-26 09:04:50 -0700849 /* Two string-valued tokens can usually just be mashed
850 * together.
851 *
Carl Worth050e3de2010-05-27 14:36:29 -0700852 * XXX: This isn't actually legitimate. Several things here
853 * should result in a diagnostic since the result cannot be a
854 * valid, single pre-processing token. For example, pasting
855 * "123" and "abc" is not legal, but we don't catch that
856 * here. */
857 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
858 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700859 {
Carl Worthb06096e2010-05-29 05:54:19 -0700860 char *str;
861
862 str = xtalloc_asprintf (token, "%s%s",
863 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700864 combined = _token_create_str (token, token->type, str);
865 combined->location = token->location;
866 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700867 }
868
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700869 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700870 glcpp_print (parser->info_log, "Pasting \"");
871 _token_print (&parser->info_log, token);
872 glcpp_print (parser->info_log, "\" and \"");
873 _token_print (&parser->info_log, other);
874 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700875
876 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700877}
878
Carl Worth0197e9b2010-05-26 08:05:19 -0700879static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700880_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700881{
882 token_node_t *node;
883
884 if (list == NULL)
885 return;
886
887 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700888 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700889}
890
Carl Worth3a37b872010-05-10 11:44:09 -0700891void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700892yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700893{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700894 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700895}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700896
Carl Worth33cc4002010-05-12 12:17:10 -0700897glcpp_parser_t *
Ian Romanick06143ea2010-06-30 16:27:22 -0700898glcpp_parser_create (const struct gl_extensions *extensions)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700899{
Carl Worth33cc4002010-05-12 12:17:10 -0700900 glcpp_parser_t *parser;
Ian Romanick2d122362010-06-30 16:03:19 -0700901 token_t *tok;
902 token_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700903
Carl Worth5070a202010-05-12 12:45:33 -0700904 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700905
Carl Worth8f38aff2010-05-19 10:01:29 -0700906 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700907 parser->defines = hash_table_ctor (32, hash_table_string_hash,
908 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700909 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700910 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700911 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700912 parser->newline_as_space = 0;
913 parser->in_control_line = 0;
914 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700915
Carl Worthb20d33c2010-05-20 22:27:07 -0700916 parser->skip_stack = NULL;
917
Carl Worth8e82fcb2010-05-26 11:15:21 -0700918 parser->lex_from_list = NULL;
919 parser->lex_from_node = NULL;
920
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700921 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700922 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke1b85c462010-06-21 13:55:12 -0700923 parser->error = 0;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700924
Ian Romanick2d122362010-06-30 16:03:19 -0700925 /* Add pre-defined macros. */
926 tok = _token_create_ival (parser, INTEGER, 1);
927
928 list = _token_list_create(parser);
929 _token_list_append(list, tok);
930 _define_object_macro(parser, NULL, "GL_ARB_draw_buffers", list);
931
932 list = _token_list_create(parser);
933 _token_list_append(list, tok);
934 _define_object_macro(parser, NULL, "GL_ARB_texture_rectangle", list);
935
Ian Romanick06143ea2010-06-30 16:27:22 -0700936 if ((extensions != NULL) && extensions->EXT_texture_array) {
937 list = _token_list_create(parser);
938 _token_list_append(list, tok);
939 _define_object_macro(parser, NULL,
940 "GL_EXT_texture_array", list);
941 }
942
Ian Romanick2d122362010-06-30 16:03:19 -0700943 talloc_unlink(parser, tok);
944
Carl Worth33cc4002010-05-12 12:17:10 -0700945 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700946}
947
948int
949glcpp_parser_parse (glcpp_parser_t *parser)
950{
951 return yyparse (parser);
952}
953
954void
Carl Worth33cc4002010-05-12 12:17:10 -0700955glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700956{
Carl Worthb20d33c2010-05-20 22:27:07 -0700957 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700958 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700959 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700960 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700961 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700962}
Carl Worthc6d5af32010-05-11 12:30:09 -0700963
Carl Worthb1854fd2010-05-25 16:28:26 -0700964typedef enum function_status
965{
966 FUNCTION_STATUS_SUCCESS,
967 FUNCTION_NOT_A_FUNCTION,
968 FUNCTION_UNBALANCED_PARENTHESES
969} function_status_t;
970
971/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700972 * balanced set of parentheses.
973 *
974 * When called, 'node' should be the opening-parenthesis token, (or
975 * perhaps preceeding SPACE tokens). Upon successful return *last will
976 * be the last consumed node, (corresponding to the closing right
977 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700978 *
979 * Return values:
980 *
981 * FUNCTION_STATUS_SUCCESS:
982 *
983 * Successfully parsed a set of function arguments.
984 *
985 * FUNCTION_NOT_A_FUNCTION:
986 *
987 * Macro name not followed by a '('. This is not an error, but
988 * simply that the macro name should be treated as a non-macro.
989 *
Carl Worth14c98a52010-06-02 15:49:54 -0700990 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -0700991 *
992 * Macro name is not followed by a balanced set of parentheses.
993 */
994static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700995_arguments_parse (argument_list_t *arguments,
996 token_node_t *node,
997 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700998{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700999 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001000 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001001
Carl Worthb1854fd2010-05-25 16:28:26 -07001002 node = node->next;
1003
1004 /* Ignore whitespace before first parenthesis. */
1005 while (node && node->token->type == SPACE)
1006 node = node->next;
1007
1008 if (node == NULL || node->token->type != '(')
1009 return FUNCTION_NOT_A_FUNCTION;
1010
Carl Worth652fa272010-05-25 17:45:22 -07001011 node = node->next;
1012
Carl Wortha19297b2010-05-27 13:29:19 -07001013 argument = _token_list_create (arguments);
1014 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001015
Carl Worth681afbc2010-05-28 15:06:02 -07001016 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001017 if (node->token->type == '(')
1018 {
1019 paren_count++;
1020 }
1021 else if (node->token->type == ')')
1022 {
1023 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001024 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001025 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001026 }
Carl Worth652fa272010-05-25 17:45:22 -07001027
1028 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001029 paren_count == 1)
1030 {
Carl Wortha19297b2010-05-27 13:29:19 -07001031 _token_list_trim_trailing_space (argument);
1032 argument = _token_list_create (arguments);
1033 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001034 }
1035 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001036 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001037 /* Don't treat initial whitespace as
1038 * part of the arguement. */
1039 if (node->token->type == SPACE)
1040 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001041 }
1042 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001043 }
Carl Worthc7581c22010-05-25 17:41:07 -07001044 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001045
Carl Worth681afbc2010-05-28 15:06:02 -07001046 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001047 return FUNCTION_UNBALANCED_PARENTHESES;
1048
Carl Worth681afbc2010-05-28 15:06:02 -07001049 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001050
1051 return FUNCTION_STATUS_SUCCESS;
1052}
1053
Carl Worth681afbc2010-05-28 15:06:02 -07001054/* This is a helper function that's essentially part of the
1055 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1056 * except for by that function.
1057 *
1058 * Returns NULL if node is a simple token with no expansion, (that is,
1059 * although 'node' corresponds to an identifier defined as a
1060 * function-like macro, it is not followed with a parenthesized
1061 * argument list).
1062 *
1063 * Compute the complete expansion of node (which is a function-like
1064 * macro) and subsequent nodes which are arguments.
1065 *
1066 * Returns the token list that results from the expansion and sets
1067 * *last to the last node in the list that was consumed by the
1068 * expansion. Specificallty, *last will be set as follows: as the
1069 * token of the closing right parenthesis.
1070 */
1071static token_list_t *
1072_glcpp_parser_expand_function (glcpp_parser_t *parser,
1073 token_node_t *node,
1074 token_node_t **last)
1075
Carl Worthb1854fd2010-05-25 16:28:26 -07001076{
1077 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001078 const char *identifier;
1079 argument_list_t *arguments;
1080 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001081 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001082 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001083
Carl Worthb1854fd2010-05-25 16:28:26 -07001084 identifier = node->token->value.str;
1085
1086 macro = hash_table_find (parser->defines, identifier);
1087
1088 assert (macro->is_function);
1089
Carl Worth9ce18cf2010-05-25 17:32:21 -07001090 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001091 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001092
1093 switch (status) {
1094 case FUNCTION_STATUS_SUCCESS:
1095 break;
1096 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001097 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001098 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001099 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001100 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001101 }
1102
Carl Worth9ce18cf2010-05-25 17:32:21 -07001103 if (macro->replacements == NULL) {
1104 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001105 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001106 }
1107
Carl Wortha19297b2010-05-27 13:29:19 -07001108 if (! ((_argument_list_length (arguments) ==
1109 _string_list_length (macro->parameters)) ||
1110 (_string_list_length (macro->parameters) == 0 &&
1111 _argument_list_length (arguments) == 1 &&
1112 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001113 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001114 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001115 "Error: macro %s invoked with %d arguments (expected %d)\n",
1116 identifier,
1117 _argument_list_length (arguments),
1118 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001119 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001120 }
1121
Carl Worth0197e9b2010-05-26 08:05:19 -07001122 /* Perform argument substitution on the replacement list. */
1123 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001124
Carl Worthce540f22010-05-26 08:25:44 -07001125 for (node = macro->replacements->head; node; node = node->next)
1126 {
1127 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001128 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001129 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001130 &parameter_index))
1131 {
1132 token_list_t *argument;
1133 argument = _argument_list_member_at (arguments,
1134 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001135 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001136 * tokens, or append a placeholder token for
1137 * an empty argument. */
1138 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001139 _glcpp_parser_expand_token_list (parser,
1140 argument);
1141 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001142 } else {
1143 token_t *new_token;
1144
1145 new_token = _token_create_ival (substituted,
1146 PLACEHOLDER,
1147 PLACEHOLDER);
1148 _token_list_append (substituted, new_token);
1149 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001150 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001151 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001152 }
1153 }
1154
Carl Worthad0dee62010-05-26 09:04:50 -07001155 /* After argument substitution, and before further expansion
1156 * below, implement token pasting. */
1157
Carl Worthb06096e2010-05-29 05:54:19 -07001158 _token_list_trim_trailing_space (substituted);
1159
Carl Worthad0dee62010-05-26 09:04:50 -07001160 node = substituted->head;
1161 while (node)
1162 {
1163 token_node_t *next_non_space;
1164
1165 /* Look ahead for a PASTE token, skipping space. */
1166 next_non_space = node->next;
1167 while (next_non_space && next_non_space->token->type == SPACE)
1168 next_non_space = next_non_space->next;
1169
1170 if (next_non_space == NULL)
1171 break;
1172
1173 if (next_non_space->token->type != PASTE) {
1174 node = next_non_space;
1175 continue;
1176 }
1177
1178 /* Now find the next non-space token after the PASTE. */
1179 next_non_space = next_non_space->next;
1180 while (next_non_space && next_non_space->token->type == SPACE)
1181 next_non_space = next_non_space->next;
1182
1183 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001184 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001185 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001186 }
1187
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001188 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001189 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001190 if (next_non_space == substituted->tail)
1191 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001192
1193 node = node->next;
1194 }
1195
Carl Worthb06096e2010-05-29 05:54:19 -07001196 substituted->non_space_tail = substituted->tail;
1197
Carl Worth681afbc2010-05-28 15:06:02 -07001198 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001199}
1200
Carl Worth681afbc2010-05-28 15:06:02 -07001201/* Compute the complete expansion of node, (and subsequent nodes after
1202 * 'node' in the case that 'node' is a function-like macro and
1203 * subsequent nodes are arguments).
1204 *
1205 * Returns NULL if node is a simple token with no expansion.
1206 *
1207 * Otherwise, returns the token list that results from the expansion
1208 * and sets *last to the last node in the list that was consumed by
1209 * the expansion. Specificallty, *last will be set as follows:
1210 *
1211 * As 'node' in the case of object-like macro expansion.
1212 *
1213 * As the token of the closing right parenthesis in the case of
1214 * function-like macro expansion.
1215 */
1216static token_list_t *
1217_glcpp_parser_expand_node (glcpp_parser_t *parser,
1218 token_node_t *node,
1219 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001220{
Carl Worth681afbc2010-05-28 15:06:02 -07001221 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001222 const char *identifier;
1223 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001224
1225 /* We only expand identifiers */
1226 if (token->type != IDENTIFIER) {
1227 /* We change any COMMA into a COMMA_FINAL to prevent
1228 * it being mistaken for an argument separator
1229 * later. */
1230 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001231 token->type = COMMA_FINAL;
1232 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001233 }
Carl Worth681afbc2010-05-28 15:06:02 -07001234
1235 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001236 }
1237
1238 /* Look up this identifier in the hash table. */
1239 identifier = token->value.str;
1240 macro = hash_table_find (parser->defines, identifier);
1241
Carl Worth681afbc2010-05-28 15:06:02 -07001242 /* Not a macro, so no expansion needed. */
1243 if (macro == NULL)
1244 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001245
1246 /* Finally, don't expand this macro if we're already actively
1247 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001248 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001249 /* We change the token type here from IDENTIFIER to
1250 * OTHER to prevent any future expansion of this
1251 * unexpanded token. */
1252 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001253 token_list_t *expansion;
1254 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001255
Carl Worth681afbc2010-05-28 15:06:02 -07001256 str = xtalloc_strdup (parser, token->value.str);
1257 final = _token_create_str (parser, OTHER, str);
1258 expansion = _token_list_create (parser);
1259 _token_list_append (expansion, final);
1260 *last = node;
1261 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001262 }
1263
Carl Worth681afbc2010-05-28 15:06:02 -07001264 if (! macro->is_function)
1265 {
1266 *last = node;
1267
1268 if (macro->replacements == NULL)
1269 return _token_list_create (parser);
1270
Carl Worth22b3ace2010-06-02 15:32:03 -07001271 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001272 }
Carl Worth681afbc2010-05-28 15:06:02 -07001273
1274 return _glcpp_parser_expand_function (parser, node, last);
1275}
1276
Carl Worth22b3ace2010-06-02 15:32:03 -07001277/* Push a new identifier onto the active list, returning the new list.
1278 *
1279 * Here, 'marker' is the token node that appears in the list after the
1280 * expansion of 'identifier'. That is, when the list iterator begins
1281 * examinging 'marker', then it is time to pop this node from the
1282 * active stack.
1283 */
1284active_list_t *
1285_active_list_push (active_list_t *list,
1286 const char *identifier,
1287 token_node_t *marker)
1288{
1289 active_list_t *node;
1290
1291 node = xtalloc (list, active_list_t);
1292 node->identifier = xtalloc_strdup (node, identifier);
1293 node->marker = marker;
1294 node->next = list;
1295
1296 return node;
1297}
1298
1299active_list_t *
1300_active_list_pop (active_list_t *list)
1301{
1302 active_list_t *node = list;
1303
1304 if (node == NULL)
1305 return NULL;
1306
1307 node = list->next;
1308 talloc_free (list);
1309
1310 return node;
1311}
1312
1313int
1314_active_list_contains (active_list_t *list, const char *identifier)
1315{
1316 active_list_t *node;
1317
1318 if (list == NULL)
1319 return 0;
1320
1321 for (node = list; node; node = node->next)
1322 if (strcmp (node->identifier, identifier) == 0)
1323 return 1;
1324
1325 return 0;
1326}
1327
Carl Worth681afbc2010-05-28 15:06:02 -07001328/* Walk over the token list replacing nodes with their expansion.
1329 * Whenever nodes are expanded the walking will walk over the new
1330 * nodes, continuing to expand as necessary. The results are placed in
1331 * 'list' itself;
1332 */
1333static void
1334_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1335 token_list_t *list)
1336{
1337 token_node_t *node_prev;
Carl Worthc42e6402010-06-22 15:51:34 -07001338 token_node_t *node, *last = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001339 token_list_t *expansion;
1340
1341 if (list == NULL)
1342 return;
1343
1344 _token_list_trim_trailing_space (list);
1345
1346 node_prev = NULL;
1347 node = list->head;
1348
1349 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001350
1351 while (parser->active && parser->active->marker == node)
1352 parser->active = _active_list_pop (parser->active);
1353
Carl Worth681afbc2010-05-28 15:06:02 -07001354 /* Find the expansion for node, which will replace all
1355 * nodes from node to last, inclusive. */
1356 expansion = _glcpp_parser_expand_node (parser, node, &last);
1357 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001358 token_node_t *n;
1359
1360 for (n = node; n != last->next; n = n->next)
1361 while (parser->active &&
1362 parser->active->marker == n)
1363 {
1364 parser->active = _active_list_pop (parser->active);
1365 }
1366
1367 parser->active = _active_list_push (parser->active,
1368 node->token->value.str,
1369 last->next);
1370
Carl Worth681afbc2010-05-28 15:06:02 -07001371 /* Splice expansion into list, supporting a
1372 * simple deletion if the expansion is
1373 * empty. */
1374 if (expansion->head) {
1375 if (node_prev)
1376 node_prev->next = expansion->head;
1377 else
1378 list->head = expansion->head;
1379 expansion->tail->next = last->next;
1380 if (last == list->tail)
1381 list->tail = expansion->tail;
1382 } else {
1383 if (node_prev)
1384 node_prev->next = last->next;
1385 else
1386 list->head = last->next;
1387 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001388 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001389 }
1390 } else {
1391 node_prev = node;
1392 }
1393 node = node_prev ? node_prev->next : list->head;
1394 }
1395
Carl Worth22b3ace2010-06-02 15:32:03 -07001396 while (parser->active)
1397 parser->active = _active_list_pop (parser->active);
1398
Carl Worth681afbc2010-05-28 15:06:02 -07001399 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001400}
1401
Carl Worthae6517f2010-05-25 15:24:59 -07001402void
1403_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1404 token_list_t *list)
1405{
Carl Worthae6517f2010-05-25 15:24:59 -07001406 if (list == NULL)
1407 return;
1408
Carl Worth681afbc2010-05-28 15:06:02 -07001409 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001410
Carl Worth681afbc2010-05-28 15:06:02 -07001411 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001412
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001413 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001414}
1415
1416void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001417_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1418 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001419{
1420 /* According to the GLSL specification, macro names starting with "__"
1421 * or "GL_" are reserved for future use. So, don't allow them.
1422 */
1423 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001424 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001425 }
1426 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001427 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001428 }
1429}
1430
1431void
Carl Worthfcbbb462010-05-13 09:36:23 -07001432_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001433 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001434 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001435 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001436{
1437 macro_t *macro;
1438
Ian Romanick2d122362010-06-30 16:03:19 -07001439 if (loc != NULL)
1440 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001441
Carl Worthfcbbb462010-05-13 09:36:23 -07001442 macro = xtalloc (parser, macro_t);
1443
1444 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001445 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001446 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001447 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001448
1449 hash_table_insert (parser->defines, macro, identifier);
1450}
1451
1452void
1453_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001454 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001455 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001456 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001457 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001458{
1459 macro_t *macro;
1460
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001461 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001462
Carl Worthfcbbb462010-05-13 09:36:23 -07001463 macro = xtalloc (parser, macro_t);
1464
1465 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001466 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001467 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001468 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001469
1470 hash_table_insert (parser->defines, macro, identifier);
1471}
1472
Carl Worth8f38aff2010-05-19 10:01:29 -07001473static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001474glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001475{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001476 token_node_t *node;
1477 int ret;
1478
Carl Worth95951ea2010-05-26 15:57:10 -07001479 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001480 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001481
1482 /* XXX: This ugly block of code exists for the sole
1483 * purpose of converting a NEWLINE token into a SPACE
1484 * token, but only in the case where we have seen a
1485 * function-like macro name, but have not yet seen its
1486 * closing parenthesis.
1487 *
1488 * There's perhaps a more compact way to do this with
1489 * mid-rule actions in the grammar.
1490 *
1491 * I'm definitely not pleased with the complexity of
1492 * this code here.
1493 */
1494 if (parser->newline_as_space)
1495 {
1496 if (ret == '(') {
1497 parser->paren_count++;
1498 } else if (ret == ')') {
1499 parser->paren_count--;
1500 if (parser->paren_count == 0)
1501 parser->newline_as_space = 0;
1502 } else if (ret == NEWLINE) {
1503 ret = SPACE;
1504 } else if (ret != SPACE) {
1505 if (parser->paren_count == 0)
1506 parser->newline_as_space = 0;
1507 }
1508 }
1509 else if (parser->in_control_line)
1510 {
1511 if (ret == NEWLINE)
1512 parser->in_control_line = 0;
1513 }
1514 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1515 ret == HASH_UNDEF || ret == HASH_IF ||
1516 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1517 ret == HASH_ELIF || ret == HASH_ELSE ||
1518 ret == HASH_ENDIF || ret == HASH)
1519 {
1520 parser->in_control_line = 1;
1521 }
1522 else if (ret == IDENTIFIER)
1523 {
1524 macro_t *macro;
1525 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001526 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001527 if (macro && macro->is_function) {
1528 parser->newline_as_space = 1;
1529 parser->paren_count = 0;
1530 }
1531 }
1532
1533 return ret;
1534 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001535
1536 node = parser->lex_from_node;
1537
1538 if (node == NULL) {
1539 talloc_free (parser->lex_from_list);
1540 parser->lex_from_list = NULL;
1541 return NEWLINE;
1542 }
1543
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001544 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001545 ret = node->token->type;
1546
1547 parser->lex_from_node = node->next;
1548
1549 return ret;
1550}
1551
1552static void
1553glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1554{
1555 token_node_t *node;
1556
1557 assert (parser->lex_from_list == NULL);
1558
1559 /* Copy list, eliminating any space tokens. */
1560 parser->lex_from_list = _token_list_create (parser);
1561
1562 for (node = list->head; node; node = node->next) {
1563 if (node->token->type == SPACE)
1564 continue;
1565 _token_list_append (parser->lex_from_list, node->token);
1566 }
1567
1568 talloc_free (list);
1569
1570 parser->lex_from_node = parser->lex_from_list->head;
1571
1572 /* It's possible the list consisted of nothing but whitespace. */
1573 if (parser->lex_from_node == NULL) {
1574 talloc_free (parser->lex_from_list);
1575 parser->lex_from_list = NULL;
1576 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001577}
Carl Worthb20d33c2010-05-20 22:27:07 -07001578
1579static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001580_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1581 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001582{
1583 skip_type_t current = SKIP_NO_SKIP;
1584 skip_node_t *node;
1585
1586 if (parser->skip_stack)
1587 current = parser->skip_stack->type;
1588
1589 node = xtalloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001590 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001591
1592 if (current == SKIP_NO_SKIP) {
1593 if (condition)
1594 node->type = SKIP_NO_SKIP;
1595 else
1596 node->type = SKIP_TO_ELSE;
1597 } else {
1598 node->type = SKIP_TO_ENDIF;
1599 }
1600
1601 node->next = parser->skip_stack;
1602 parser->skip_stack = node;
1603}
1604
1605static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001606_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1607 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001608{
1609 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001610 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001611 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001612 }
1613
1614 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1615 if (condition)
1616 parser->skip_stack->type = SKIP_NO_SKIP;
1617 } else {
1618 parser->skip_stack->type = SKIP_TO_ENDIF;
1619 }
1620}
Carl Worth80dc60b2010-05-25 14:42:00 -07001621
Carl Worthb20d33c2010-05-20 22:27:07 -07001622static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001623_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001624{
1625 skip_node_t *node;
1626
1627 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001628 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001629 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001630 }
1631
1632 node = parser->skip_stack;
1633 parser->skip_stack = node->next;
1634 talloc_free (node);
1635}