blob: c6ff32e544c661e96e6f232b139d529005f7043a [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 Worthefef9502010-07-28 11:10:52 -0700144%expect 0
Carl Worth050e3de2010-05-27 14:36:29 -0700145%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 -0700146%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700147%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700148%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700149%type <string_list> identifier_list
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700150%type <token> preprocessing_token conditional_token
151%type <token_list> pp_tokens replacement_list text_line conditional_tokens
Carl Worth8fed1cd2010-05-26 09:32:12 -0700152%left OR
153%left AND
154%left '|'
155%left '^'
156%left '&'
157%left EQUAL NOT_EQUAL
158%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
159%left LEFT_SHIFT RIGHT_SHIFT
160%left '+' '-'
161%left '*' '/' '%'
162%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700163
164%%
165
Carl Worth33cc4002010-05-12 12:17:10 -0700166input:
Carl Worth3ff81672010-05-25 13:09:03 -0700167 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700168| input line
169;
170
171line:
172 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700173 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700174 }
Carl Worth808401f2010-05-25 14:52:43 -0700175| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700176 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700177 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700178 talloc_free ($1);
179 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700180| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700181| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700182;
183
Carl Worth8e82fcb2010-05-26 11:15:21 -0700184expanded_line:
185 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700186 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700187 }
188| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700189 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700190 }
191;
192
Carl Worth3ff81672010-05-25 13:09:03 -0700193control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700194 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700195 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700196 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700197| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700198 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700199 }
200| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700201 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700202 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700203| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700204 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700205 if (macro) {
Carl Worth61ebc012010-07-19 18:02:12 -0700206 hash_table_remove (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700207 talloc_free (macro);
208 }
209 talloc_free ($2);
210 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700211| HASH_IF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700212 token_list_t *expanded;
213 token_t *token;
214
215 expanded = _token_list_create (parser);
216 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
217 _token_list_append (expanded, token);
218 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700219 _glcpp_parser_expand_token_list (parser, $2);
220 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700221 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700222 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700223| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700224 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700225 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700226 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700227 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700228| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700229 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700230 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700231 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700232 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700233| HASH_ELIF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700234 token_list_t *expanded;
235 token_t *token;
236
237 expanded = _token_list_create (parser);
238 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
239 _token_list_append (expanded, token);
240 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700241 _glcpp_parser_expand_token_list (parser, $2);
242 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700243 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700244 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700245| HASH_ELIF NEWLINE {
246 /* #elif without an expression results in a warning if the
247 * condition doesn't matter (we just handled #if 1 or such)
248 * but an error otherwise. */
249 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
250 parser->skip_stack->type = SKIP_TO_ENDIF;
251 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
252 } else {
253 glcpp_error(& @1, parser, "#elif needs an expression");
254 }
255 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700256| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700257 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700258 }
259| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700260 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700261 }
Carl Worth3ff81672010-05-25 13:09:03 -0700262| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700263;
264
Carl Worth8fed1cd2010-05-26 09:32:12 -0700265expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700266 INTEGER_STRING {
267 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
268 $$ = strtoll ($1 + 2, NULL, 16);
269 } else if ($1[0] == '0') {
270 $$ = strtoll ($1, NULL, 8);
271 } else {
272 $$ = strtoll ($1, NULL, 10);
273 }
274 }
275| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700276 $$ = $1;
277 }
278| expression OR expression {
279 $$ = $1 || $3;
280 }
281| expression AND expression {
282 $$ = $1 && $3;
283 }
284| expression '|' expression {
285 $$ = $1 | $3;
286 }
287| expression '^' expression {
288 $$ = $1 ^ $3;
289 }
290| expression '&' expression {
291 $$ = $1 & $3;
292 }
293| expression NOT_EQUAL expression {
294 $$ = $1 != $3;
295 }
296| expression EQUAL expression {
297 $$ = $1 == $3;
298 }
299| expression GREATER_OR_EQUAL expression {
300 $$ = $1 >= $3;
301 }
302| expression LESS_OR_EQUAL expression {
303 $$ = $1 <= $3;
304 }
305| expression '>' expression {
306 $$ = $1 > $3;
307 }
308| expression '<' expression {
309 $$ = $1 < $3;
310 }
311| expression RIGHT_SHIFT expression {
312 $$ = $1 >> $3;
313 }
314| expression LEFT_SHIFT expression {
315 $$ = $1 << $3;
316 }
317| expression '-' expression {
318 $$ = $1 - $3;
319 }
320| expression '+' expression {
321 $$ = $1 + $3;
322 }
323| expression '%' expression {
324 $$ = $1 % $3;
325 }
326| expression '/' expression {
327 $$ = $1 / $3;
328 }
329| expression '*' expression {
330 $$ = $1 * $3;
331 }
332| '!' expression %prec UNARY {
333 $$ = ! $2;
334 }
335| '~' expression %prec UNARY {
336 $$ = ~ $2;
337 }
338| '-' expression %prec UNARY {
339 $$ = - $2;
340 }
341| '+' expression %prec UNARY {
342 $$ = + $2;
343 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700344| '(' expression ')' {
345 $$ = $2;
346 }
347;
348
Carl Worth3ff81672010-05-25 13:09:03 -0700349identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700350 IDENTIFIER {
351 $$ = _string_list_create (parser);
352 _string_list_append_item ($$, $1);
353 talloc_steal ($$, $1);
354 }
355| identifier_list ',' IDENTIFIER {
356 $$ = $1;
357 _string_list_append_item ($$, $3);
358 talloc_steal ($$, $3);
359 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700360;
361
Carl Worth3ff81672010-05-25 13:09:03 -0700362text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700363 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700364| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700365;
366
Carl Worth3ff81672010-05-25 13:09:03 -0700367non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700368 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700369 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700370 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700371;
372
Carl Worthaaa9acb2010-05-19 13:28:24 -0700373replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700374 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700375| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700376;
377
Kenneth Graunke65875742010-06-18 20:08:15 -0700378junk:
379 /* empty */
380| pp_tokens {
381 glcpp_warning(&@1, parser, "extra tokens at end of directive");
382 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700383;
384
385conditional_token:
386 /* Handle "defined" operator */
387 DEFINED IDENTIFIER {
388 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
389 $$ = _token_create_ival (parser, INTEGER, v);
390 }
391| DEFINED '(' IDENTIFIER ')' {
392 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
393 $$ = _token_create_ival (parser, INTEGER, v);
394 }
395| preprocessing_token
396;
397
398conditional_tokens:
399 /* Exactly the same as pp_tokens, but using conditional_token */
400 conditional_token {
401 parser->space_tokens = 1;
402 $$ = _token_list_create (parser);
403 _token_list_append ($$, $1);
404 talloc_unlink (parser, $1);
405 }
406| conditional_tokens conditional_token {
407 $$ = $1;
408 _token_list_append ($$, $2);
409 talloc_unlink (parser, $2);
410 }
411;
Kenneth Graunke65875742010-06-18 20:08:15 -0700412
Carl Worthaaa9acb2010-05-19 13:28:24 -0700413pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700414 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700415 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700416 $$ = _token_list_create (parser);
417 _token_list_append ($$, $1);
418 talloc_unlink (parser, $1);
419 }
420| pp_tokens preprocessing_token {
421 $$ = $1;
422 _token_list_append ($$, $2);
423 talloc_unlink (parser, $2);
424 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700425;
426
Carl Worth3ff81672010-05-25 13:09:03 -0700427preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700428 IDENTIFIER {
429 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700430 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700431 }
Carl Worth050e3de2010-05-27 14:36:29 -0700432| INTEGER_STRING {
433 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700434 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700435 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700436| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700437 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700438 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700439 }
440| OTHER {
441 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700442 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700443 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700444| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700445 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700446 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700447 }
Carl Worth3ff81672010-05-25 13:09:03 -0700448;
449
Carl Worth8e82fcb2010-05-26 11:15:21 -0700450operator:
Carl Worth808401f2010-05-25 14:52:43 -0700451 '[' { $$ = '['; }
452| ']' { $$ = ']'; }
453| '(' { $$ = '('; }
454| ')' { $$ = ')'; }
455| '{' { $$ = '{'; }
456| '}' { $$ = '}'; }
457| '.' { $$ = '.'; }
458| '&' { $$ = '&'; }
459| '*' { $$ = '*'; }
460| '+' { $$ = '+'; }
461| '-' { $$ = '-'; }
462| '~' { $$ = '~'; }
463| '!' { $$ = '!'; }
464| '/' { $$ = '/'; }
465| '%' { $$ = '%'; }
466| LEFT_SHIFT { $$ = LEFT_SHIFT; }
467| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
468| '<' { $$ = '<'; }
469| '>' { $$ = '>'; }
470| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
471| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
472| EQUAL { $$ = EQUAL; }
473| NOT_EQUAL { $$ = NOT_EQUAL; }
474| '^' { $$ = '^'; }
475| '|' { $$ = '|'; }
476| AND { $$ = AND; }
477| OR { $$ = OR; }
478| ';' { $$ = ';'; }
479| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700480| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700481| PASTE { $$ = PASTE; }
Carl Worth3ff81672010-05-25 13:09:03 -0700482;
483
Carl Worth33cc4002010-05-12 12:17:10 -0700484%%
485
Carl Worth610053b2010-05-14 10:05:11 -0700486string_list_t *
487_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700488{
Carl Worth610053b2010-05-14 10:05:11 -0700489 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700490
Carl Worth610053b2010-05-14 10:05:11 -0700491 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700492 list->head = NULL;
493 list->tail = NULL;
494
495 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700496}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700497
Carl Worth33cc4002010-05-12 12:17:10 -0700498void
Carl Worth610053b2010-05-14 10:05:11 -0700499_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700500{
Carl Worth610053b2010-05-14 10:05:11 -0700501 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700502
Carl Worth610053b2010-05-14 10:05:11 -0700503 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700504 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700505
Carl Worth33cc4002010-05-12 12:17:10 -0700506 node->next = NULL;
507
508 if (list->head == NULL) {
509 list->head = node;
510 } else {
511 list->tail->next = node;
512 }
513
514 list->tail = node;
515}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700516
517int
Carl Worth610053b2010-05-14 10:05:11 -0700518_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700519{
Carl Worth610053b2010-05-14 10:05:11 -0700520 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700521 int i;
522
523 if (list == NULL)
524 return 0;
525
526 for (i = 0, node = list->head; node; i++, node = node->next) {
527 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700528 if (index)
529 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700530 return 1;
531 }
532 }
533
534 return 0;
535}
536
537int
Carl Worth610053b2010-05-14 10:05:11 -0700538_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700539{
540 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700541 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700542
543 if (list == NULL)
544 return 0;
545
546 for (node = list->head; node; node = node->next)
547 length++;
548
549 return length;
550}
551
Carl Worth8f6a8282010-05-14 10:44:19 -0700552argument_list_t *
553_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700554{
Carl Worth8f6a8282010-05-14 10:44:19 -0700555 argument_list_t *list;
556
557 list = xtalloc (ctx, argument_list_t);
558 list->head = NULL;
559 list->tail = NULL;
560
561 return list;
562}
563
564void
Carl Worth47252442010-05-19 13:54:37 -0700565_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700566{
567 argument_node_t *node;
568
Carl Worth8f6a8282010-05-14 10:44:19 -0700569 node = xtalloc (list, argument_node_t);
570 node->argument = argument;
571
572 node->next = NULL;
573
574 if (list->head == NULL) {
575 list->head = node;
576 } else {
577 list->tail->next = node;
578 }
579
580 list->tail = node;
581}
582
583int
584_argument_list_length (argument_list_t *list)
585{
586 int length = 0;
587 argument_node_t *node;
588
589 if (list == NULL)
590 return 0;
591
592 for (node = list->head; node; node = node->next)
593 length++;
594
595 return length;
596}
597
Carl Worth47252442010-05-19 13:54:37 -0700598token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700599_argument_list_member_at (argument_list_t *list, int index)
600{
601 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700602 int i;
603
604 if (list == NULL)
605 return NULL;
606
607 node = list->head;
608 for (i = 0; i < index; i++) {
609 node = node->next;
610 if (node == NULL)
611 break;
612 }
613
614 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700615 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700616
617 return NULL;
618}
Carl Worth47252442010-05-19 13:54:37 -0700619
Carl Worth808401f2010-05-25 14:52:43 -0700620/* Note: This function talloc_steal()s the str pointer. */
621token_t *
622_token_create_str (void *ctx, int type, char *str)
623{
624 token_t *token;
625
626 token = xtalloc (ctx, token_t);
627 token->type = type;
628 token->value.str = talloc_steal (token, str);
629
630 return token;
631}
632
633token_t *
634_token_create_ival (void *ctx, int type, int ival)
635{
636 token_t *token;
637
638 token = xtalloc (ctx, token_t);
639 token->type = type;
640 token->value.ival = ival;
641
642 return token;
643}
644
Carl Worth47252442010-05-19 13:54:37 -0700645token_list_t *
646_token_list_create (void *ctx)
647{
648 token_list_t *list;
649
650 list = xtalloc (ctx, token_list_t);
651 list->head = NULL;
652 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700653 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700654
655 return list;
656}
657
658void
Carl Worth808401f2010-05-25 14:52:43 -0700659_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700660{
661 token_node_t *node;
662
663 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700664 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700665
666 node->next = NULL;
667
668 if (list->head == NULL) {
669 list->head = node;
670 } else {
671 list->tail->next = node;
672 }
673
674 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700675 if (token->type != SPACE)
676 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700677}
678
679void
680_token_list_append_list (token_list_t *list, token_list_t *tail)
681{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700682 if (tail == NULL || tail->head == NULL)
683 return;
684
Carl Worth47252442010-05-19 13:54:37 -0700685 if (list->head == NULL) {
686 list->head = tail->head;
687 } else {
688 list->tail->next = tail->head;
689 }
690
691 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700692 list->non_space_tail = tail->non_space_tail;
693}
694
Carl Worthd80dcaf2010-07-20 15:55:21 -0700695static token_list_t *
Carl Worth681afbc2010-05-28 15:06:02 -0700696_token_list_copy (void *ctx, token_list_t *other)
697{
698 token_list_t *copy;
699 token_node_t *node;
700
701 if (other == NULL)
702 return NULL;
703
704 copy = _token_list_create (ctx);
705 for (node = other->head; node; node = node->next)
706 _token_list_append (copy, node->token);
707
708 return copy;
709}
710
Carl Worthd80dcaf2010-07-20 15:55:21 -0700711static void
Carl Worth10ae4382010-05-25 20:35:01 -0700712_token_list_trim_trailing_space (token_list_t *list)
713{
714 token_node_t *tail, *next;
715
716 if (list->non_space_tail) {
717 tail = list->non_space_tail->next;
718 list->non_space_tail->next = NULL;
719 list->tail = list->non_space_tail;
720
721 while (tail) {
722 next = tail->next;
723 talloc_free (tail);
724 tail = next;
725 }
726 }
Carl Worth47252442010-05-19 13:54:37 -0700727}
Carl Worth80dc60b2010-05-25 14:42:00 -0700728
Carl Worth0197e9b2010-05-26 08:05:19 -0700729static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700730_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700731{
732 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700733 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700734 return;
735 }
736
737 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700738 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700739 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700740 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700741 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700742 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700743 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700744 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700745 break;
746 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700747 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700748 break;
749 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700750 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700751 break;
752 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700753 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700754 break;
755 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700756 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700757 break;
758 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700759 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700760 break;
761 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700762 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700763 break;
764 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700765 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700766 break;
767 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700768 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700769 break;
770 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700771 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700772 break;
773 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700774 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700775 break;
Carl Worthdd749002010-05-27 10:12:33 -0700776 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700777 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700778 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700779 case PLACEHOLDER:
780 /* Nothing to print. */
781 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700782 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700783 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700784 break;
785 }
786}
787
Carl Worthb06096e2010-05-29 05:54:19 -0700788/* Return a new token (talloc()ed off of 'token') formed by pasting
789 * 'token' and 'other'. Note that this function may return 'token' or
790 * 'other' directly rather than allocating anything new.
791 *
792 * Caution: Only very cursory error-checking is performed to see if
793 * the final result is a valid single token. */
794static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700795_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700796{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700797 token_t *combined = NULL;
798
Carl Worth85b50e82010-05-27 14:01:18 -0700799 /* Pasting a placeholder onto anything makes no change. */
800 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700801 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700802
Carl Worthb06096e2010-05-29 05:54:19 -0700803 /* When 'token' is a placeholder, just return 'other'. */
804 if (token->type == PLACEHOLDER)
805 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700806
Carl Worthad0dee62010-05-26 09:04:50 -0700807 /* A very few single-character punctuators can be combined
808 * with another to form a multi-character punctuator. */
809 switch (token->type) {
810 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700811 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700812 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700813 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700814 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700815 break;
816 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700817 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700818 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700819 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700820 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700821 break;
822 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700823 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700824 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700825 break;
826 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700827 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700828 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700829 break;
830 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700831 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700832 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700833 break;
834 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700835 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700836 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700837 break;
838 }
839
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700840 if (combined != NULL) {
841 /* Inherit the location from the first token */
842 combined->location = token->location;
843 return combined;
844 }
845
Carl Worthad0dee62010-05-26 09:04:50 -0700846 /* Two string-valued tokens can usually just be mashed
847 * together.
848 *
Carl Worth050e3de2010-05-27 14:36:29 -0700849 * XXX: This isn't actually legitimate. Several things here
850 * should result in a diagnostic since the result cannot be a
851 * valid, single pre-processing token. For example, pasting
852 * "123" and "abc" is not legal, but we don't catch that
853 * here. */
854 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
855 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700856 {
Carl Worthb06096e2010-05-29 05:54:19 -0700857 char *str;
858
859 str = xtalloc_asprintf (token, "%s%s",
860 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700861 combined = _token_create_str (token, token->type, str);
862 combined->location = token->location;
863 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700864 }
865
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700866 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700867 glcpp_print (parser->info_log, "Pasting \"");
868 _token_print (&parser->info_log, token);
869 glcpp_print (parser->info_log, "\" and \"");
870 _token_print (&parser->info_log, other);
871 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700872
873 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700874}
875
Carl Worth0197e9b2010-05-26 08:05:19 -0700876static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700877_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700878{
879 token_node_t *node;
880
881 if (list == NULL)
882 return;
883
884 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700885 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700886}
887
Carl Worth3a37b872010-05-10 11:44:09 -0700888void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700889yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700890{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700891 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700892}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700893
Carl Worth33cc4002010-05-12 12:17:10 -0700894glcpp_parser_t *
Ian Romanick06143ea2010-06-30 16:27:22 -0700895glcpp_parser_create (const struct gl_extensions *extensions)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700896{
Carl Worth33cc4002010-05-12 12:17:10 -0700897 glcpp_parser_t *parser;
Ian Romanick2d122362010-06-30 16:03:19 -0700898 token_t *tok;
899 token_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700900
Carl Worth5070a202010-05-12 12:45:33 -0700901 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700902
Carl Worth8f38aff2010-05-19 10:01:29 -0700903 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700904 parser->defines = hash_table_ctor (32, hash_table_string_hash,
905 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700906 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700907 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700908 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700909 parser->newline_as_space = 0;
910 parser->in_control_line = 0;
911 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700912
Carl Worthb20d33c2010-05-20 22:27:07 -0700913 parser->skip_stack = NULL;
914
Carl Worth8e82fcb2010-05-26 11:15:21 -0700915 parser->lex_from_list = NULL;
916 parser->lex_from_node = NULL;
917
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700918 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700919 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke1b85c462010-06-21 13:55:12 -0700920 parser->error = 0;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700921
Ian Romanick2d122362010-06-30 16:03:19 -0700922 /* Add pre-defined macros. */
923 tok = _token_create_ival (parser, INTEGER, 1);
924
925 list = _token_list_create(parser);
926 _token_list_append(list, tok);
927 _define_object_macro(parser, NULL, "GL_ARB_draw_buffers", list);
928
929 list = _token_list_create(parser);
930 _token_list_append(list, tok);
931 _define_object_macro(parser, NULL, "GL_ARB_texture_rectangle", list);
932
Ian Romanick06143ea2010-06-30 16:27:22 -0700933 if ((extensions != NULL) && extensions->EXT_texture_array) {
934 list = _token_list_create(parser);
935 _token_list_append(list, tok);
936 _define_object_macro(parser, NULL,
937 "GL_EXT_texture_array", list);
938 }
939
Eric Anholt0c7b37c2010-07-28 14:58:31 -0700940 if ((extensions != NULL) &&
941 extensions->ARB_fragment_coord_conventions) {
942 list = _token_list_create(parser);
943 _token_list_append(list, tok);
944 _define_object_macro(parser, NULL,
945 "GL_ARB_fragment_coord_conventions", list);
946 }
947
Ian Romanick2d122362010-06-30 16:03:19 -0700948 talloc_unlink(parser, tok);
949
Carl Worth33cc4002010-05-12 12:17:10 -0700950 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700951}
952
953int
954glcpp_parser_parse (glcpp_parser_t *parser)
955{
956 return yyparse (parser);
957}
958
959void
Carl Worth33cc4002010-05-12 12:17:10 -0700960glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700961{
Carl Worthb20d33c2010-05-20 22:27:07 -0700962 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700963 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700964 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700965 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700966 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700967}
Carl Worthc6d5af32010-05-11 12:30:09 -0700968
Carl Worthb1854fd2010-05-25 16:28:26 -0700969typedef enum function_status
970{
971 FUNCTION_STATUS_SUCCESS,
972 FUNCTION_NOT_A_FUNCTION,
973 FUNCTION_UNBALANCED_PARENTHESES
974} function_status_t;
975
976/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700977 * balanced set of parentheses.
978 *
979 * When called, 'node' should be the opening-parenthesis token, (or
980 * perhaps preceeding SPACE tokens). Upon successful return *last will
981 * be the last consumed node, (corresponding to the closing right
982 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700983 *
984 * Return values:
985 *
986 * FUNCTION_STATUS_SUCCESS:
987 *
988 * Successfully parsed a set of function arguments.
989 *
990 * FUNCTION_NOT_A_FUNCTION:
991 *
992 * Macro name not followed by a '('. This is not an error, but
993 * simply that the macro name should be treated as a non-macro.
994 *
Carl Worth14c98a52010-06-02 15:49:54 -0700995 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -0700996 *
997 * Macro name is not followed by a balanced set of parentheses.
998 */
999static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -07001000_arguments_parse (argument_list_t *arguments,
1001 token_node_t *node,
1002 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001003{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001004 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001005 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001006
Carl Worthb1854fd2010-05-25 16:28:26 -07001007 node = node->next;
1008
1009 /* Ignore whitespace before first parenthesis. */
1010 while (node && node->token->type == SPACE)
1011 node = node->next;
1012
1013 if (node == NULL || node->token->type != '(')
1014 return FUNCTION_NOT_A_FUNCTION;
1015
Carl Worth652fa272010-05-25 17:45:22 -07001016 node = node->next;
1017
Carl Wortha19297b2010-05-27 13:29:19 -07001018 argument = _token_list_create (arguments);
1019 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001020
Carl Worth681afbc2010-05-28 15:06:02 -07001021 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001022 if (node->token->type == '(')
1023 {
1024 paren_count++;
1025 }
1026 else if (node->token->type == ')')
1027 {
1028 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001029 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001030 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001031 }
Carl Worth652fa272010-05-25 17:45:22 -07001032
1033 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001034 paren_count == 1)
1035 {
Carl Wortha19297b2010-05-27 13:29:19 -07001036 _token_list_trim_trailing_space (argument);
1037 argument = _token_list_create (arguments);
1038 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001039 }
1040 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001041 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001042 /* Don't treat initial whitespace as
1043 * part of the arguement. */
1044 if (node->token->type == SPACE)
1045 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001046 }
1047 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001048 }
Carl Worthc7581c22010-05-25 17:41:07 -07001049 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001050
Carl Worth681afbc2010-05-28 15:06:02 -07001051 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001052 return FUNCTION_UNBALANCED_PARENTHESES;
1053
Carl Worth681afbc2010-05-28 15:06:02 -07001054 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001055
1056 return FUNCTION_STATUS_SUCCESS;
1057}
1058
Carl Worthe1acbfc2010-07-20 16:44:03 -07001059static token_list_t *
1060_token_list_create_with_one_space (void *ctx)
1061{
1062 token_list_t *list;
1063 token_t *space;
1064
1065 list = _token_list_create (ctx);
1066 space = _token_create_ival (list, SPACE, SPACE);
1067 _token_list_append (list, space);
1068
1069 return list;
1070}
1071
Carl Worth681afbc2010-05-28 15:06:02 -07001072/* This is a helper function that's essentially part of the
1073 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1074 * except for by that function.
1075 *
1076 * Returns NULL if node is a simple token with no expansion, (that is,
1077 * although 'node' corresponds to an identifier defined as a
1078 * function-like macro, it is not followed with a parenthesized
1079 * argument list).
1080 *
1081 * Compute the complete expansion of node (which is a function-like
1082 * macro) and subsequent nodes which are arguments.
1083 *
1084 * Returns the token list that results from the expansion and sets
1085 * *last to the last node in the list that was consumed by the
Carl Worthfbe42402010-07-22 16:36:04 -07001086 * expansion. Specifically, *last will be set as follows: as the
Carl Worth681afbc2010-05-28 15:06:02 -07001087 * token of the closing right parenthesis.
1088 */
1089static token_list_t *
1090_glcpp_parser_expand_function (glcpp_parser_t *parser,
1091 token_node_t *node,
1092 token_node_t **last)
1093
Carl Worthb1854fd2010-05-25 16:28:26 -07001094{
1095 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001096 const char *identifier;
1097 argument_list_t *arguments;
1098 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001099 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001100 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001101
Carl Worthb1854fd2010-05-25 16:28:26 -07001102 identifier = node->token->value.str;
1103
1104 macro = hash_table_find (parser->defines, identifier);
1105
1106 assert (macro->is_function);
1107
Carl Worth9ce18cf2010-05-25 17:32:21 -07001108 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001109 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001110
1111 switch (status) {
1112 case FUNCTION_STATUS_SUCCESS:
1113 break;
1114 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001115 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001116 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001117 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001118 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001119 }
1120
Carl Worthe1acbfc2010-07-20 16:44:03 -07001121 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth9ce18cf2010-05-25 17:32:21 -07001122 if (macro->replacements == NULL) {
1123 talloc_free (arguments);
Carl Worthe1acbfc2010-07-20 16:44:03 -07001124 return _token_list_create_with_one_space (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001125 }
1126
Carl Wortha19297b2010-05-27 13:29:19 -07001127 if (! ((_argument_list_length (arguments) ==
1128 _string_list_length (macro->parameters)) ||
1129 (_string_list_length (macro->parameters) == 0 &&
1130 _argument_list_length (arguments) == 1 &&
1131 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001132 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001133 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001134 "Error: macro %s invoked with %d arguments (expected %d)\n",
1135 identifier,
1136 _argument_list_length (arguments),
1137 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001138 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001139 }
1140
Carl Worth0197e9b2010-05-26 08:05:19 -07001141 /* Perform argument substitution on the replacement list. */
1142 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001143
Carl Worthce540f22010-05-26 08:25:44 -07001144 for (node = macro->replacements->head; node; node = node->next)
1145 {
1146 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001147 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001148 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001149 &parameter_index))
1150 {
1151 token_list_t *argument;
1152 argument = _argument_list_member_at (arguments,
1153 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001154 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001155 * tokens, or append a placeholder token for
1156 * an empty argument. */
1157 if (argument->head) {
Carl Worthfbe42402010-07-22 16:36:04 -07001158 token_list_t *expanded_argument;
1159 expanded_argument = _token_list_copy (parser,
1160 argument);
Carl Worth681afbc2010-05-28 15:06:02 -07001161 _glcpp_parser_expand_token_list (parser,
Carl Worthfbe42402010-07-22 16:36:04 -07001162 expanded_argument);
1163 _token_list_append_list (substituted,
1164 expanded_argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001165 } else {
1166 token_t *new_token;
1167
1168 new_token = _token_create_ival (substituted,
1169 PLACEHOLDER,
1170 PLACEHOLDER);
1171 _token_list_append (substituted, new_token);
1172 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001173 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001174 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001175 }
1176 }
1177
Carl Worthad0dee62010-05-26 09:04:50 -07001178 /* After argument substitution, and before further expansion
1179 * below, implement token pasting. */
1180
Carl Worthb06096e2010-05-29 05:54:19 -07001181 _token_list_trim_trailing_space (substituted);
1182
Carl Worthad0dee62010-05-26 09:04:50 -07001183 node = substituted->head;
1184 while (node)
1185 {
1186 token_node_t *next_non_space;
1187
1188 /* Look ahead for a PASTE token, skipping space. */
1189 next_non_space = node->next;
1190 while (next_non_space && next_non_space->token->type == SPACE)
1191 next_non_space = next_non_space->next;
1192
1193 if (next_non_space == NULL)
1194 break;
1195
1196 if (next_non_space->token->type != PASTE) {
1197 node = next_non_space;
1198 continue;
1199 }
1200
1201 /* Now find the next non-space token after the PASTE. */
1202 next_non_space = next_non_space->next;
1203 while (next_non_space && next_non_space->token->type == SPACE)
1204 next_non_space = next_non_space->next;
1205
1206 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001207 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001208 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001209 }
1210
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001211 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001212 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001213 if (next_non_space == substituted->tail)
1214 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001215
1216 node = node->next;
1217 }
1218
Carl Worthb06096e2010-05-29 05:54:19 -07001219 substituted->non_space_tail = substituted->tail;
1220
Carl Worth681afbc2010-05-28 15:06:02 -07001221 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001222}
1223
Carl Worth681afbc2010-05-28 15:06:02 -07001224/* Compute the complete expansion of node, (and subsequent nodes after
1225 * 'node' in the case that 'node' is a function-like macro and
1226 * subsequent nodes are arguments).
1227 *
1228 * Returns NULL if node is a simple token with no expansion.
1229 *
1230 * Otherwise, returns the token list that results from the expansion
1231 * and sets *last to the last node in the list that was consumed by
Carl Worthe1acbfc2010-07-20 16:44:03 -07001232 * the expansion. Specifically, *last will be set as follows:
Carl Worth681afbc2010-05-28 15:06:02 -07001233 *
1234 * As 'node' in the case of object-like macro expansion.
1235 *
1236 * As the token of the closing right parenthesis in the case of
1237 * function-like macro expansion.
1238 */
1239static token_list_t *
1240_glcpp_parser_expand_node (glcpp_parser_t *parser,
1241 token_node_t *node,
1242 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001243{
Carl Worth681afbc2010-05-28 15:06:02 -07001244 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001245 const char *identifier;
1246 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001247
1248 /* We only expand identifiers */
1249 if (token->type != IDENTIFIER) {
1250 /* We change any COMMA into a COMMA_FINAL to prevent
1251 * it being mistaken for an argument separator
1252 * later. */
1253 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001254 token->type = COMMA_FINAL;
1255 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001256 }
Carl Worth681afbc2010-05-28 15:06:02 -07001257
1258 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001259 }
1260
1261 /* Look up this identifier in the hash table. */
1262 identifier = token->value.str;
1263 macro = hash_table_find (parser->defines, identifier);
1264
Carl Worth681afbc2010-05-28 15:06:02 -07001265 /* Not a macro, so no expansion needed. */
1266 if (macro == NULL)
1267 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001268
1269 /* Finally, don't expand this macro if we're already actively
1270 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001271 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001272 /* We change the token type here from IDENTIFIER to
1273 * OTHER to prevent any future expansion of this
1274 * unexpanded token. */
1275 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001276 token_list_t *expansion;
1277 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001278
Carl Worth681afbc2010-05-28 15:06:02 -07001279 str = xtalloc_strdup (parser, token->value.str);
1280 final = _token_create_str (parser, OTHER, str);
1281 expansion = _token_list_create (parser);
1282 _token_list_append (expansion, final);
1283 *last = node;
1284 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001285 }
1286
Carl Worth681afbc2010-05-28 15:06:02 -07001287 if (! macro->is_function)
1288 {
1289 *last = node;
1290
Carl Worthe1acbfc2010-07-20 16:44:03 -07001291 /* Replace a macro defined as empty with a SPACE token. */
Carl Worth681afbc2010-05-28 15:06:02 -07001292 if (macro->replacements == NULL)
Carl Worthe1acbfc2010-07-20 16:44:03 -07001293 return _token_list_create_with_one_space (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001294
Carl Worth22b3ace2010-06-02 15:32:03 -07001295 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001296 }
Carl Worth681afbc2010-05-28 15:06:02 -07001297
1298 return _glcpp_parser_expand_function (parser, node, last);
1299}
1300
Carl Worth22b3ace2010-06-02 15:32:03 -07001301/* Push a new identifier onto the active list, returning the new list.
1302 *
1303 * Here, 'marker' is the token node that appears in the list after the
1304 * expansion of 'identifier'. That is, when the list iterator begins
1305 * examinging 'marker', then it is time to pop this node from the
1306 * active stack.
1307 */
1308active_list_t *
1309_active_list_push (active_list_t *list,
1310 const char *identifier,
1311 token_node_t *marker)
1312{
1313 active_list_t *node;
1314
1315 node = xtalloc (list, active_list_t);
1316 node->identifier = xtalloc_strdup (node, identifier);
1317 node->marker = marker;
1318 node->next = list;
1319
1320 return node;
1321}
1322
1323active_list_t *
1324_active_list_pop (active_list_t *list)
1325{
1326 active_list_t *node = list;
1327
1328 if (node == NULL)
1329 return NULL;
1330
1331 node = list->next;
1332 talloc_free (list);
1333
1334 return node;
1335}
1336
1337int
1338_active_list_contains (active_list_t *list, const char *identifier)
1339{
1340 active_list_t *node;
1341
1342 if (list == NULL)
1343 return 0;
1344
1345 for (node = list; node; node = node->next)
1346 if (strcmp (node->identifier, identifier) == 0)
1347 return 1;
1348
1349 return 0;
1350}
1351
Carl Worth681afbc2010-05-28 15:06:02 -07001352/* Walk over the token list replacing nodes with their expansion.
1353 * Whenever nodes are expanded the walking will walk over the new
1354 * nodes, continuing to expand as necessary. The results are placed in
1355 * 'list' itself;
1356 */
1357static void
1358_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1359 token_list_t *list)
1360{
1361 token_node_t *node_prev;
Carl Worthc42e6402010-06-22 15:51:34 -07001362 token_node_t *node, *last = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001363 token_list_t *expansion;
1364
1365 if (list == NULL)
1366 return;
1367
1368 _token_list_trim_trailing_space (list);
1369
1370 node_prev = NULL;
1371 node = list->head;
1372
1373 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001374
1375 while (parser->active && parser->active->marker == node)
1376 parser->active = _active_list_pop (parser->active);
1377
Carl Worth681afbc2010-05-28 15:06:02 -07001378 /* Find the expansion for node, which will replace all
1379 * nodes from node to last, inclusive. */
1380 expansion = _glcpp_parser_expand_node (parser, node, &last);
1381 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001382 token_node_t *n;
1383
1384 for (n = node; n != last->next; n = n->next)
1385 while (parser->active &&
1386 parser->active->marker == n)
1387 {
1388 parser->active = _active_list_pop (parser->active);
1389 }
1390
1391 parser->active = _active_list_push (parser->active,
1392 node->token->value.str,
1393 last->next);
1394
Carl Worth681afbc2010-05-28 15:06:02 -07001395 /* Splice expansion into list, supporting a
1396 * simple deletion if the expansion is
1397 * empty. */
1398 if (expansion->head) {
1399 if (node_prev)
1400 node_prev->next = expansion->head;
1401 else
1402 list->head = expansion->head;
1403 expansion->tail->next = last->next;
1404 if (last == list->tail)
1405 list->tail = expansion->tail;
1406 } else {
1407 if (node_prev)
1408 node_prev->next = last->next;
1409 else
1410 list->head = last->next;
1411 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001412 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001413 }
1414 } else {
1415 node_prev = node;
1416 }
1417 node = node_prev ? node_prev->next : list->head;
1418 }
1419
Carl Worth22b3ace2010-06-02 15:32:03 -07001420 while (parser->active)
1421 parser->active = _active_list_pop (parser->active);
1422
Carl Worth681afbc2010-05-28 15:06:02 -07001423 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001424}
1425
Carl Worthae6517f2010-05-25 15:24:59 -07001426void
1427_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1428 token_list_t *list)
1429{
Carl Worthae6517f2010-05-25 15:24:59 -07001430 if (list == NULL)
1431 return;
1432
Carl Worth681afbc2010-05-28 15:06:02 -07001433 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001434
Carl Worth681afbc2010-05-28 15:06:02 -07001435 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001436
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001437 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001438}
1439
Carl Worthd80dcaf2010-07-20 15:55:21 -07001440static void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001441_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1442 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001443{
1444 /* According to the GLSL specification, macro names starting with "__"
1445 * or "GL_" are reserved for future use. So, don't allow them.
1446 */
1447 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001448 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001449 }
1450 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001451 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001452 }
1453}
1454
1455void
Carl Worthfcbbb462010-05-13 09:36:23 -07001456_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001457 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001458 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001459 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001460{
1461 macro_t *macro;
1462
Ian Romanick2d122362010-06-30 16:03:19 -07001463 if (loc != NULL)
1464 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001465
Carl Worthfcbbb462010-05-13 09:36:23 -07001466 macro = xtalloc (parser, macro_t);
1467
1468 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001469 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001470 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001471 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001472
1473 hash_table_insert (parser->defines, macro, identifier);
1474}
1475
1476void
1477_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001478 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001479 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001480 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001481 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001482{
1483 macro_t *macro;
1484
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001485 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001486
Carl Worthfcbbb462010-05-13 09:36:23 -07001487 macro = xtalloc (parser, macro_t);
1488
1489 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001490 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001491 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001492 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001493
1494 hash_table_insert (parser->defines, macro, identifier);
1495}
1496
Carl Worth8f38aff2010-05-19 10:01:29 -07001497static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001498glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001499{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001500 token_node_t *node;
1501 int ret;
1502
Carl Worth95951ea2010-05-26 15:57:10 -07001503 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001504 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001505
1506 /* XXX: This ugly block of code exists for the sole
1507 * purpose of converting a NEWLINE token into a SPACE
1508 * token, but only in the case where we have seen a
1509 * function-like macro name, but have not yet seen its
1510 * closing parenthesis.
1511 *
1512 * There's perhaps a more compact way to do this with
1513 * mid-rule actions in the grammar.
1514 *
1515 * I'm definitely not pleased with the complexity of
1516 * this code here.
1517 */
1518 if (parser->newline_as_space)
1519 {
1520 if (ret == '(') {
1521 parser->paren_count++;
1522 } else if (ret == ')') {
1523 parser->paren_count--;
1524 if (parser->paren_count == 0)
1525 parser->newline_as_space = 0;
1526 } else if (ret == NEWLINE) {
1527 ret = SPACE;
1528 } else if (ret != SPACE) {
1529 if (parser->paren_count == 0)
1530 parser->newline_as_space = 0;
1531 }
1532 }
1533 else if (parser->in_control_line)
1534 {
1535 if (ret == NEWLINE)
1536 parser->in_control_line = 0;
1537 }
1538 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1539 ret == HASH_UNDEF || ret == HASH_IF ||
1540 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1541 ret == HASH_ELIF || ret == HASH_ELSE ||
1542 ret == HASH_ENDIF || ret == HASH)
1543 {
1544 parser->in_control_line = 1;
1545 }
1546 else if (ret == IDENTIFIER)
1547 {
1548 macro_t *macro;
1549 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001550 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001551 if (macro && macro->is_function) {
1552 parser->newline_as_space = 1;
1553 parser->paren_count = 0;
1554 }
1555 }
1556
1557 return ret;
1558 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001559
1560 node = parser->lex_from_node;
1561
1562 if (node == NULL) {
1563 talloc_free (parser->lex_from_list);
1564 parser->lex_from_list = NULL;
1565 return NEWLINE;
1566 }
1567
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001568 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001569 ret = node->token->type;
1570
1571 parser->lex_from_node = node->next;
1572
1573 return ret;
1574}
1575
1576static void
1577glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1578{
1579 token_node_t *node;
1580
1581 assert (parser->lex_from_list == NULL);
1582
1583 /* Copy list, eliminating any space tokens. */
1584 parser->lex_from_list = _token_list_create (parser);
1585
1586 for (node = list->head; node; node = node->next) {
1587 if (node->token->type == SPACE)
1588 continue;
1589 _token_list_append (parser->lex_from_list, node->token);
1590 }
1591
1592 talloc_free (list);
1593
1594 parser->lex_from_node = parser->lex_from_list->head;
1595
1596 /* It's possible the list consisted of nothing but whitespace. */
1597 if (parser->lex_from_node == NULL) {
1598 talloc_free (parser->lex_from_list);
1599 parser->lex_from_list = NULL;
1600 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001601}
Carl Worthb20d33c2010-05-20 22:27:07 -07001602
1603static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001604_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1605 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001606{
1607 skip_type_t current = SKIP_NO_SKIP;
1608 skip_node_t *node;
1609
1610 if (parser->skip_stack)
1611 current = parser->skip_stack->type;
1612
1613 node = xtalloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001614 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001615
1616 if (current == SKIP_NO_SKIP) {
1617 if (condition)
1618 node->type = SKIP_NO_SKIP;
1619 else
1620 node->type = SKIP_TO_ELSE;
1621 } else {
1622 node->type = SKIP_TO_ENDIF;
1623 }
1624
1625 node->next = parser->skip_stack;
1626 parser->skip_stack = node;
1627}
1628
1629static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001630_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1631 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001632{
1633 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001634 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001635 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001636 }
1637
1638 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1639 if (condition)
1640 parser->skip_stack->type = SKIP_NO_SKIP;
1641 } else {
1642 parser->skip_stack->type = SKIP_TO_ENDIF;
1643 }
1644}
Carl Worth80dc60b2010-05-25 14:42:00 -07001645
Carl Worthb20d33c2010-05-20 22:27:07 -07001646static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001647_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001648{
1649 skip_node_t *node;
1650
1651 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001652 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001653 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001654 }
1655
1656 node = parser->skip_stack;
1657 parser->skip_stack = node->next;
1658 talloc_free (node);
1659}