blob: d4cb006bbcdaf94478e35bad5ee771ff05bed822 [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"
31
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -070032#define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
33#define glcpp_printf(stream, fmt, args...) \
34 stream = talloc_asprintf_append(stream, fmt, args)
35
Carl Worth5aa7ea02010-05-25 18:39:43 -070036static void
Kenneth Graunke465e03e2010-06-16 16:35:57 -070037yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070038
Carl Worth5aa7ea02010-05-25 18:39:43 -070039static void
Carl Worthfcbbb462010-05-13 09:36:23 -070040_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070041 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070042 const char *macro,
Carl Worth47252442010-05-19 13:54:37 -070043 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070044
Carl Worth5aa7ea02010-05-25 18:39:43 -070045static void
Carl Worthfcbbb462010-05-13 09:36:23 -070046_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070047 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070048 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070049 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070050 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070051
Carl Worth5aa7ea02010-05-25 18:39:43 -070052static string_list_t *
Carl Worth610053b2010-05-14 10:05:11 -070053_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070054
Carl Worth5aa7ea02010-05-25 18:39:43 -070055static void
Carl Worth610053b2010-05-14 10:05:11 -070056_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070057
Carl Worth5aa7ea02010-05-25 18:39:43 -070058static int
Carl Worth610053b2010-05-14 10:05:11 -070059_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070060
Carl Worth5aa7ea02010-05-25 18:39:43 -070061static int
Carl Worth610053b2010-05-14 10:05:11 -070062_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070063
Carl Worth5aa7ea02010-05-25 18:39:43 -070064static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070065_argument_list_create (void *ctx);
66
Carl Worth5aa7ea02010-05-25 18:39:43 -070067static void
Carl Worth47252442010-05-19 13:54:37 -070068_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070069
Carl Worth5aa7ea02010-05-25 18:39:43 -070070static int
Carl Worth8f6a8282010-05-14 10:44:19 -070071_argument_list_length (argument_list_t *list);
72
Carl Worth5aa7ea02010-05-25 18:39:43 -070073static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070074_argument_list_member_at (argument_list_t *list, int index);
75
Carl Worth808401f2010-05-25 14:52:43 -070076/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070077static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070078_token_create_str (void *ctx, int type, char *str);
79
Carl Worth5aa7ea02010-05-25 18:39:43 -070080static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070081_token_create_ival (void *ctx, int type, int ival);
82
Carl Worth5aa7ea02010-05-25 18:39:43 -070083static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070084_token_list_create (void *ctx);
85
Carl Worthb1ae61a2010-05-26 08:10:38 -070086/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070087 *
88 * You may want to talloc_unlink any current reference if you no
89 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070090static void
Carl Worth808401f2010-05-25 14:52:43 -070091_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070092
Carl Worth5aa7ea02010-05-25 18:39:43 -070093static void
Carl Worth47252442010-05-19 13:54:37 -070094_token_list_append_list (token_list_t *list, token_list_t *tail);
95
Carl Worth22b3ace2010-06-02 15:32:03 -070096static active_list_t *
97_active_list_push (active_list_t *list,
98 const char *identifier,
99 token_node_t *marker);
100
101static active_list_t *
102_active_list_pop (active_list_t *list);
103
104int
105_active_list_contains (active_list_t *list, const char *identifier);
106
Carl Worth5aa7ea02010-05-25 18:39:43 -0700107static void
Carl Worth681afbc2010-05-28 15:06:02 -0700108_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
109 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700110
Carl Worthaaa9acb2010-05-19 13:28:24 -0700111static void
Carl Worth681afbc2010-05-28 15:06:02 -0700112_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
113 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700114
115static void
Kenneth Graunke07745232010-06-17 12:41:46 -0700116_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
117 int condition);
Carl Worthb20d33c2010-05-20 22:27:07 -0700118
119static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700120_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
121 const char *type, int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700122
Carl Worthb20d33c2010-05-20 22:27:07 -0700123static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700124_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
Carl Worthb20d33c2010-05-20 22:27:07 -0700125
Carl Worth0293b2e2010-05-19 10:05:40 -0700126#define yylex glcpp_parser_lex
127
Carl Worth8f38aff2010-05-19 10:01:29 -0700128static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700129glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700130
Carl Worth8e82fcb2010-05-26 11:15:21 -0700131static void
132glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
133
Carl Worth3a37b872010-05-10 11:44:09 -0700134%}
135
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700136%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700137%error-verbose
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700138%locations
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700139
Carl Worth0b27b5f2010-05-10 16:16:06 -0700140%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700141%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700142
Carl Worth050e3de2010-05-27 14:36:29 -0700143%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 -0700144%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700145%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700146%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700147%type <string_list> identifier_list
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700148%type <token> preprocessing_token conditional_token
149%type <token_list> pp_tokens replacement_list text_line conditional_tokens
Carl Worth8fed1cd2010-05-26 09:32:12 -0700150%left OR
151%left AND
152%left '|'
153%left '^'
154%left '&'
155%left EQUAL NOT_EQUAL
156%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
157%left LEFT_SHIFT RIGHT_SHIFT
158%left '+' '-'
159%left '*' '/' '%'
160%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700161
162%%
163
Carl Worth33cc4002010-05-12 12:17:10 -0700164input:
Carl Worth3ff81672010-05-25 13:09:03 -0700165 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700166| input line
167;
168
169line:
170 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700171 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700172 }
Carl Worth808401f2010-05-25 14:52:43 -0700173| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700174 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700175 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700176 talloc_free ($1);
177 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700178| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700179| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700180;
181
Carl Worth8e82fcb2010-05-26 11:15:21 -0700182expanded_line:
183 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700184 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700185 }
186| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700187 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700188 }
189;
190
Carl Worth3ff81672010-05-25 13:09:03 -0700191control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700192 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700193 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700194 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700195| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700196 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700197 }
198| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700199 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700200 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700201| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700202 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700203 if (macro) {
204 /* XXX: Need hash table to support a real way
205 * to remove an element rather than prefixing
206 * a new node with data of NULL like this. */
207 hash_table_insert (parser->defines, NULL, $2);
208 talloc_free (macro);
209 }
210 talloc_free ($2);
211 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700212| HASH_IF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700213 token_list_t *expanded;
214 token_t *token;
215
216 expanded = _token_list_create (parser);
217 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
218 _token_list_append (expanded, token);
219 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700220 _glcpp_parser_expand_token_list (parser, $2);
221 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700222 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700223 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700224| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700225 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700226 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700227 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700228 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700229| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700230 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700231 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700232 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700233 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700234| HASH_ELIF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700235 token_list_t *expanded;
236 token_t *token;
237
238 expanded = _token_list_create (parser);
239 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
240 _token_list_append (expanded, token);
241 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700242 _glcpp_parser_expand_token_list (parser, $2);
243 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700244 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700245 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700246| HASH_ELIF NEWLINE {
247 /* #elif without an expression results in a warning if the
248 * condition doesn't matter (we just handled #if 1 or such)
249 * but an error otherwise. */
250 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
251 parser->skip_stack->type = SKIP_TO_ENDIF;
252 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
253 } else {
254 glcpp_error(& @1, parser, "#elif needs an expression");
255 }
256 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700257| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700258 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700259 }
260| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700261 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700262 }
Carl Worth3ff81672010-05-25 13:09:03 -0700263| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700264;
265
Carl Worth8fed1cd2010-05-26 09:32:12 -0700266expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700267 INTEGER_STRING {
268 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
269 $$ = strtoll ($1 + 2, NULL, 16);
270 } else if ($1[0] == '0') {
271 $$ = strtoll ($1, NULL, 8);
272 } else {
273 $$ = strtoll ($1, NULL, 10);
274 }
275 }
276| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700277 $$ = $1;
278 }
279| expression OR expression {
280 $$ = $1 || $3;
281 }
282| expression AND expression {
283 $$ = $1 && $3;
284 }
285| expression '|' expression {
286 $$ = $1 | $3;
287 }
288| expression '^' expression {
289 $$ = $1 ^ $3;
290 }
291| expression '&' expression {
292 $$ = $1 & $3;
293 }
294| expression NOT_EQUAL expression {
295 $$ = $1 != $3;
296 }
297| expression EQUAL expression {
298 $$ = $1 == $3;
299 }
300| expression GREATER_OR_EQUAL expression {
301 $$ = $1 >= $3;
302 }
303| expression LESS_OR_EQUAL expression {
304 $$ = $1 <= $3;
305 }
306| expression '>' expression {
307 $$ = $1 > $3;
308 }
309| expression '<' expression {
310 $$ = $1 < $3;
311 }
312| expression RIGHT_SHIFT expression {
313 $$ = $1 >> $3;
314 }
315| expression LEFT_SHIFT expression {
316 $$ = $1 << $3;
317 }
318| expression '-' expression {
319 $$ = $1 - $3;
320 }
321| expression '+' expression {
322 $$ = $1 + $3;
323 }
324| expression '%' expression {
325 $$ = $1 % $3;
326 }
327| expression '/' expression {
328 $$ = $1 / $3;
329 }
330| expression '*' expression {
331 $$ = $1 * $3;
332 }
333| '!' expression %prec UNARY {
334 $$ = ! $2;
335 }
336| '~' expression %prec UNARY {
337 $$ = ~ $2;
338 }
339| '-' expression %prec UNARY {
340 $$ = - $2;
341 }
342| '+' expression %prec UNARY {
343 $$ = + $2;
344 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700345| '(' expression ')' {
346 $$ = $2;
347 }
348;
349
Carl Worth3ff81672010-05-25 13:09:03 -0700350identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700351 IDENTIFIER {
352 $$ = _string_list_create (parser);
353 _string_list_append_item ($$, $1);
354 talloc_steal ($$, $1);
355 }
356| identifier_list ',' IDENTIFIER {
357 $$ = $1;
358 _string_list_append_item ($$, $3);
359 talloc_steal ($$, $3);
360 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700361;
362
Carl Worth3ff81672010-05-25 13:09:03 -0700363text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700364 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700365| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700366;
367
Carl Worth3ff81672010-05-25 13:09:03 -0700368non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700369 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700370 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700371 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700372;
373
Carl Worthaaa9acb2010-05-19 13:28:24 -0700374replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700375 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700376| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700377;
378
Kenneth Graunke65875742010-06-18 20:08:15 -0700379junk:
380 /* empty */
381| pp_tokens {
382 glcpp_warning(&@1, parser, "extra tokens at end of directive");
383 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700384;
385
386conditional_token:
387 /* Handle "defined" operator */
388 DEFINED IDENTIFIER {
389 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
390 $$ = _token_create_ival (parser, INTEGER, v);
391 }
392| DEFINED '(' IDENTIFIER ')' {
393 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
394 $$ = _token_create_ival (parser, INTEGER, v);
395 }
396| preprocessing_token
397;
398
399conditional_tokens:
400 /* Exactly the same as pp_tokens, but using conditional_token */
401 conditional_token {
402 parser->space_tokens = 1;
403 $$ = _token_list_create (parser);
404 _token_list_append ($$, $1);
405 talloc_unlink (parser, $1);
406 }
407| conditional_tokens conditional_token {
408 $$ = $1;
409 _token_list_append ($$, $2);
410 talloc_unlink (parser, $2);
411 }
412;
Kenneth Graunke65875742010-06-18 20:08:15 -0700413
Carl Worthaaa9acb2010-05-19 13:28:24 -0700414pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700415 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700416 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700417 $$ = _token_list_create (parser);
418 _token_list_append ($$, $1);
419 talloc_unlink (parser, $1);
420 }
421| pp_tokens preprocessing_token {
422 $$ = $1;
423 _token_list_append ($$, $2);
424 talloc_unlink (parser, $2);
425 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700426;
427
Carl Worth3ff81672010-05-25 13:09:03 -0700428preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700429 IDENTIFIER {
430 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700431 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700432 }
Carl Worth050e3de2010-05-27 14:36:29 -0700433| INTEGER_STRING {
434 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700435 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700436 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700437| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700438 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700439 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700440 }
441| OTHER {
442 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700443 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700444 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700445| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700446 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700447 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700448 }
Carl Worth3ff81672010-05-25 13:09:03 -0700449;
450
Carl Worth8e82fcb2010-05-26 11:15:21 -0700451operator:
Carl Worth808401f2010-05-25 14:52:43 -0700452 '[' { $$ = '['; }
453| ']' { $$ = ']'; }
454| '(' { $$ = '('; }
455| ')' { $$ = ')'; }
456| '{' { $$ = '{'; }
457| '}' { $$ = '}'; }
458| '.' { $$ = '.'; }
459| '&' { $$ = '&'; }
460| '*' { $$ = '*'; }
461| '+' { $$ = '+'; }
462| '-' { $$ = '-'; }
463| '~' { $$ = '~'; }
464| '!' { $$ = '!'; }
465| '/' { $$ = '/'; }
466| '%' { $$ = '%'; }
467| LEFT_SHIFT { $$ = LEFT_SHIFT; }
468| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
469| '<' { $$ = '<'; }
470| '>' { $$ = '>'; }
471| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
472| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
473| EQUAL { $$ = EQUAL; }
474| NOT_EQUAL { $$ = NOT_EQUAL; }
475| '^' { $$ = '^'; }
476| '|' { $$ = '|'; }
477| AND { $$ = AND; }
478| OR { $$ = OR; }
479| ';' { $$ = ';'; }
480| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700481| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700482| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700483| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700484;
485
Carl Worth33cc4002010-05-12 12:17:10 -0700486%%
487
Carl Worth610053b2010-05-14 10:05:11 -0700488string_list_t *
489_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700490{
Carl Worth610053b2010-05-14 10:05:11 -0700491 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700492
Carl Worth610053b2010-05-14 10:05:11 -0700493 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700494 list->head = NULL;
495 list->tail = NULL;
496
497 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700498}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700499
Carl Worth33cc4002010-05-12 12:17:10 -0700500void
Carl Worth610053b2010-05-14 10:05:11 -0700501_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700502{
Carl Worth610053b2010-05-14 10:05:11 -0700503 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700504
Carl Worth610053b2010-05-14 10:05:11 -0700505 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700506 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700507
Carl Worth33cc4002010-05-12 12:17:10 -0700508 node->next = NULL;
509
510 if (list->head == NULL) {
511 list->head = node;
512 } else {
513 list->tail->next = node;
514 }
515
516 list->tail = node;
517}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700518
519int
Carl Worth610053b2010-05-14 10:05:11 -0700520_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700521{
Carl Worth610053b2010-05-14 10:05:11 -0700522 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700523 int i;
524
525 if (list == NULL)
526 return 0;
527
528 for (i = 0, node = list->head; node; i++, node = node->next) {
529 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700530 if (index)
531 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700532 return 1;
533 }
534 }
535
536 return 0;
537}
538
539int
Carl Worth610053b2010-05-14 10:05:11 -0700540_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700541{
542 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700543 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700544
545 if (list == NULL)
546 return 0;
547
548 for (node = list->head; node; node = node->next)
549 length++;
550
551 return length;
552}
553
Carl Worth8f6a8282010-05-14 10:44:19 -0700554argument_list_t *
555_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700556{
Carl Worth8f6a8282010-05-14 10:44:19 -0700557 argument_list_t *list;
558
559 list = xtalloc (ctx, argument_list_t);
560 list->head = NULL;
561 list->tail = NULL;
562
563 return list;
564}
565
566void
Carl Worth47252442010-05-19 13:54:37 -0700567_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700568{
569 argument_node_t *node;
570
Carl Worth8f6a8282010-05-14 10:44:19 -0700571 node = xtalloc (list, argument_node_t);
572 node->argument = argument;
573
574 node->next = NULL;
575
576 if (list->head == NULL) {
577 list->head = node;
578 } else {
579 list->tail->next = node;
580 }
581
582 list->tail = node;
583}
584
585int
586_argument_list_length (argument_list_t *list)
587{
588 int length = 0;
589 argument_node_t *node;
590
591 if (list == NULL)
592 return 0;
593
594 for (node = list->head; node; node = node->next)
595 length++;
596
597 return length;
598}
599
Carl Worth47252442010-05-19 13:54:37 -0700600token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700601_argument_list_member_at (argument_list_t *list, int index)
602{
603 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700604 int i;
605
606 if (list == NULL)
607 return NULL;
608
609 node = list->head;
610 for (i = 0; i < index; i++) {
611 node = node->next;
612 if (node == NULL)
613 break;
614 }
615
616 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700617 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700618
619 return NULL;
620}
Carl Worth47252442010-05-19 13:54:37 -0700621
Carl Worth808401f2010-05-25 14:52:43 -0700622/* Note: This function talloc_steal()s the str pointer. */
623token_t *
624_token_create_str (void *ctx, int type, char *str)
625{
626 token_t *token;
627
628 token = xtalloc (ctx, token_t);
629 token->type = type;
630 token->value.str = talloc_steal (token, str);
631
632 return token;
633}
634
635token_t *
636_token_create_ival (void *ctx, int type, int ival)
637{
638 token_t *token;
639
640 token = xtalloc (ctx, token_t);
641 token->type = type;
642 token->value.ival = ival;
643
644 return token;
645}
646
Carl Worth47252442010-05-19 13:54:37 -0700647token_list_t *
648_token_list_create (void *ctx)
649{
650 token_list_t *list;
651
652 list = xtalloc (ctx, token_list_t);
653 list->head = NULL;
654 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700655 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700656
657 return list;
658}
659
660void
Carl Worth808401f2010-05-25 14:52:43 -0700661_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700662{
663 token_node_t *node;
664
665 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700666 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700667
668 node->next = NULL;
669
670 if (list->head == NULL) {
671 list->head = node;
672 } else {
673 list->tail->next = node;
674 }
675
676 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700677 if (token->type != SPACE)
678 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700679}
680
681void
682_token_list_append_list (token_list_t *list, token_list_t *tail)
683{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700684 if (tail == NULL || tail->head == NULL)
685 return;
686
Carl Worth47252442010-05-19 13:54:37 -0700687 if (list->head == NULL) {
688 list->head = tail->head;
689 } else {
690 list->tail->next = tail->head;
691 }
692
693 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700694 list->non_space_tail = tail->non_space_tail;
695}
696
Carl Worth681afbc2010-05-28 15:06:02 -0700697token_list_t *
698_token_list_copy (void *ctx, token_list_t *other)
699{
700 token_list_t *copy;
701 token_node_t *node;
702
703 if (other == NULL)
704 return NULL;
705
706 copy = _token_list_create (ctx);
707 for (node = other->head; node; node = node->next)
708 _token_list_append (copy, node->token);
709
710 return copy;
711}
712
Carl Worth10ae4382010-05-25 20:35:01 -0700713void
714_token_list_trim_trailing_space (token_list_t *list)
715{
716 token_node_t *tail, *next;
717
718 if (list->non_space_tail) {
719 tail = list->non_space_tail->next;
720 list->non_space_tail->next = NULL;
721 list->tail = list->non_space_tail;
722
723 while (tail) {
724 next = tail->next;
725 talloc_free (tail);
726 tail = next;
727 }
728 }
Carl Worth47252442010-05-19 13:54:37 -0700729}
Carl Worth80dc60b2010-05-25 14:42:00 -0700730
Carl Worth0197e9b2010-05-26 08:05:19 -0700731static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700732_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700733{
734 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700735 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700736 return;
737 }
738
739 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700740 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700741 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700742 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700743 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700744 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700745 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700746 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700747 break;
748 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700749 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700750 break;
751 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700752 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700753 break;
754 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700755 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700756 break;
757 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700758 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700759 break;
760 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700761 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700762 break;
763 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700764 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700765 break;
766 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700767 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700768 break;
769 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700770 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700771 break;
772 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700773 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700774 break;
775 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700776 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700777 break;
Carl Worthdd749002010-05-27 10:12:33 -0700778 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700779 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700780 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700781 case PLACEHOLDER:
782 /* Nothing to print. */
783 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700784 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700785 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700786 break;
787 }
788}
789
Carl Worthb06096e2010-05-29 05:54:19 -0700790/* Return a new token (talloc()ed off of 'token') formed by pasting
791 * 'token' and 'other'. Note that this function may return 'token' or
792 * 'other' directly rather than allocating anything new.
793 *
794 * Caution: Only very cursory error-checking is performed to see if
795 * the final result is a valid single token. */
796static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700797_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700798{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700799 token_t *combined = NULL;
800
Carl Worth85b50e82010-05-27 14:01:18 -0700801 /* Pasting a placeholder onto anything makes no change. */
802 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700803 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700804
Carl Worthb06096e2010-05-29 05:54:19 -0700805 /* When 'token' is a placeholder, just return 'other'. */
806 if (token->type == PLACEHOLDER)
807 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700808
Carl Worthad0dee62010-05-26 09:04:50 -0700809 /* A very few single-character punctuators can be combined
810 * with another to form a multi-character punctuator. */
811 switch (token->type) {
812 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700813 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700814 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700815 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700816 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700817 break;
818 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700819 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700820 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700821 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700822 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700823 break;
824 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700825 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700826 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700827 break;
828 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700829 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700830 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700831 break;
832 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700833 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700834 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700835 break;
836 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700837 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700838 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700839 break;
840 }
841
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700842 if (combined != NULL) {
843 /* Inherit the location from the first token */
844 combined->location = token->location;
845 return combined;
846 }
847
Carl Worthad0dee62010-05-26 09:04:50 -0700848 /* Two string-valued tokens can usually just be mashed
849 * together.
850 *
Carl Worth050e3de2010-05-27 14:36:29 -0700851 * XXX: This isn't actually legitimate. Several things here
852 * should result in a diagnostic since the result cannot be a
853 * valid, single pre-processing token. For example, pasting
854 * "123" and "abc" is not legal, but we don't catch that
855 * here. */
856 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
857 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700858 {
Carl Worthb06096e2010-05-29 05:54:19 -0700859 char *str;
860
861 str = xtalloc_asprintf (token, "%s%s",
862 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700863 combined = _token_create_str (token, token->type, str);
864 combined->location = token->location;
865 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700866 }
867
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700868 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700869 glcpp_print (parser->info_log, "Pasting \"");
870 _token_print (&parser->info_log, token);
871 glcpp_print (parser->info_log, "\" and \"");
872 _token_print (&parser->info_log, other);
873 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700874
875 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700876}
877
Carl Worth0197e9b2010-05-26 08:05:19 -0700878static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700879_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700880{
881 token_node_t *node;
882
883 if (list == NULL)
884 return;
885
886 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700887 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700888}
889
Carl Worth3a37b872010-05-10 11:44:09 -0700890void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700891yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700892{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700893 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700894}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700895
Carl Worth33cc4002010-05-12 12:17:10 -0700896glcpp_parser_t *
897glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700898{
Carl Worth33cc4002010-05-12 12:17:10 -0700899 glcpp_parser_t *parser;
Ian Romanick2d122362010-06-30 16:03:19 -0700900 token_t *tok;
901 token_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700902
Carl Worth5070a202010-05-12 12:45:33 -0700903 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700904
Carl Worth8f38aff2010-05-19 10:01:29 -0700905 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700906 parser->defines = hash_table_ctor (32, hash_table_string_hash,
907 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700908 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700909 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700910 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700911 parser->newline_as_space = 0;
912 parser->in_control_line = 0;
913 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700914
Carl Worthb20d33c2010-05-20 22:27:07 -0700915 parser->skip_stack = NULL;
916
Carl Worth8e82fcb2010-05-26 11:15:21 -0700917 parser->lex_from_list = NULL;
918 parser->lex_from_node = NULL;
919
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700920 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700921 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke1b85c462010-06-21 13:55:12 -0700922 parser->error = 0;
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700923
Ian Romanick2d122362010-06-30 16:03:19 -0700924 /* Add pre-defined macros. */
925 tok = _token_create_ival (parser, INTEGER, 1);
926
927 list = _token_list_create(parser);
928 _token_list_append(list, tok);
929 _define_object_macro(parser, NULL, "GL_ARB_draw_buffers", list);
930
931 list = _token_list_create(parser);
932 _token_list_append(list, tok);
933 _define_object_macro(parser, NULL, "GL_ARB_texture_rectangle", list);
934
935 talloc_unlink(parser, tok);
936
Carl Worth33cc4002010-05-12 12:17:10 -0700937 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700938}
939
940int
941glcpp_parser_parse (glcpp_parser_t *parser)
942{
943 return yyparse (parser);
944}
945
946void
Carl Worth33cc4002010-05-12 12:17:10 -0700947glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700948{
Carl Worthb20d33c2010-05-20 22:27:07 -0700949 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700950 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700951 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700952 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700953 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700954}
Carl Worthc6d5af32010-05-11 12:30:09 -0700955
Carl Worthb1854fd2010-05-25 16:28:26 -0700956typedef enum function_status
957{
958 FUNCTION_STATUS_SUCCESS,
959 FUNCTION_NOT_A_FUNCTION,
960 FUNCTION_UNBALANCED_PARENTHESES
961} function_status_t;
962
963/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700964 * balanced set of parentheses.
965 *
966 * When called, 'node' should be the opening-parenthesis token, (or
967 * perhaps preceeding SPACE tokens). Upon successful return *last will
968 * be the last consumed node, (corresponding to the closing right
969 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700970 *
971 * Return values:
972 *
973 * FUNCTION_STATUS_SUCCESS:
974 *
975 * Successfully parsed a set of function arguments.
976 *
977 * FUNCTION_NOT_A_FUNCTION:
978 *
979 * Macro name not followed by a '('. This is not an error, but
980 * simply that the macro name should be treated as a non-macro.
981 *
Carl Worth14c98a52010-06-02 15:49:54 -0700982 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -0700983 *
984 * Macro name is not followed by a balanced set of parentheses.
985 */
986static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700987_arguments_parse (argument_list_t *arguments,
988 token_node_t *node,
989 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700990{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700991 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700992 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700993
Carl Worthb1854fd2010-05-25 16:28:26 -0700994 node = node->next;
995
996 /* Ignore whitespace before first parenthesis. */
997 while (node && node->token->type == SPACE)
998 node = node->next;
999
1000 if (node == NULL || node->token->type != '(')
1001 return FUNCTION_NOT_A_FUNCTION;
1002
Carl Worth652fa272010-05-25 17:45:22 -07001003 node = node->next;
1004
Carl Wortha19297b2010-05-27 13:29:19 -07001005 argument = _token_list_create (arguments);
1006 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001007
Carl Worth681afbc2010-05-28 15:06:02 -07001008 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001009 if (node->token->type == '(')
1010 {
1011 paren_count++;
1012 }
1013 else if (node->token->type == ')')
1014 {
1015 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001016 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001017 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001018 }
Carl Worth652fa272010-05-25 17:45:22 -07001019
1020 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001021 paren_count == 1)
1022 {
Carl Wortha19297b2010-05-27 13:29:19 -07001023 _token_list_trim_trailing_space (argument);
1024 argument = _token_list_create (arguments);
1025 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001026 }
1027 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001028 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001029 /* Don't treat initial whitespace as
1030 * part of the arguement. */
1031 if (node->token->type == SPACE)
1032 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001033 }
1034 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001035 }
Carl Worthc7581c22010-05-25 17:41:07 -07001036 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001037
Carl Worth681afbc2010-05-28 15:06:02 -07001038 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001039 return FUNCTION_UNBALANCED_PARENTHESES;
1040
Carl Worth681afbc2010-05-28 15:06:02 -07001041 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001042
1043 return FUNCTION_STATUS_SUCCESS;
1044}
1045
Carl Worth681afbc2010-05-28 15:06:02 -07001046/* This is a helper function that's essentially part of the
1047 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1048 * except for by that function.
1049 *
1050 * Returns NULL if node is a simple token with no expansion, (that is,
1051 * although 'node' corresponds to an identifier defined as a
1052 * function-like macro, it is not followed with a parenthesized
1053 * argument list).
1054 *
1055 * Compute the complete expansion of node (which is a function-like
1056 * macro) and subsequent nodes which are arguments.
1057 *
1058 * Returns the token list that results from the expansion and sets
1059 * *last to the last node in the list that was consumed by the
1060 * expansion. Specificallty, *last will be set as follows: as the
1061 * token of the closing right parenthesis.
1062 */
1063static token_list_t *
1064_glcpp_parser_expand_function (glcpp_parser_t *parser,
1065 token_node_t *node,
1066 token_node_t **last)
1067
Carl Worthb1854fd2010-05-25 16:28:26 -07001068{
1069 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001070 const char *identifier;
1071 argument_list_t *arguments;
1072 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001073 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001074 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001075
Carl Worthb1854fd2010-05-25 16:28:26 -07001076 identifier = node->token->value.str;
1077
1078 macro = hash_table_find (parser->defines, identifier);
1079
1080 assert (macro->is_function);
1081
Carl Worth9ce18cf2010-05-25 17:32:21 -07001082 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001083 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001084
1085 switch (status) {
1086 case FUNCTION_STATUS_SUCCESS:
1087 break;
1088 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001089 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001090 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001091 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001092 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001093 }
1094
Carl Worth9ce18cf2010-05-25 17:32:21 -07001095 if (macro->replacements == NULL) {
1096 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001097 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001098 }
1099
Carl Wortha19297b2010-05-27 13:29:19 -07001100 if (! ((_argument_list_length (arguments) ==
1101 _string_list_length (macro->parameters)) ||
1102 (_string_list_length (macro->parameters) == 0 &&
1103 _argument_list_length (arguments) == 1 &&
1104 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001105 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001106 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001107 "Error: macro %s invoked with %d arguments (expected %d)\n",
1108 identifier,
1109 _argument_list_length (arguments),
1110 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001111 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001112 }
1113
Carl Worth0197e9b2010-05-26 08:05:19 -07001114 /* Perform argument substitution on the replacement list. */
1115 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001116
Carl Worthce540f22010-05-26 08:25:44 -07001117 for (node = macro->replacements->head; node; node = node->next)
1118 {
1119 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001120 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001121 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001122 &parameter_index))
1123 {
1124 token_list_t *argument;
1125 argument = _argument_list_member_at (arguments,
1126 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001127 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001128 * tokens, or append a placeholder token for
1129 * an empty argument. */
1130 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001131 _glcpp_parser_expand_token_list (parser,
1132 argument);
1133 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001134 } else {
1135 token_t *new_token;
1136
1137 new_token = _token_create_ival (substituted,
1138 PLACEHOLDER,
1139 PLACEHOLDER);
1140 _token_list_append (substituted, new_token);
1141 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001142 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001143 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001144 }
1145 }
1146
Carl Worthad0dee62010-05-26 09:04:50 -07001147 /* After argument substitution, and before further expansion
1148 * below, implement token pasting. */
1149
Carl Worthb06096e2010-05-29 05:54:19 -07001150 _token_list_trim_trailing_space (substituted);
1151
Carl Worthad0dee62010-05-26 09:04:50 -07001152 node = substituted->head;
1153 while (node)
1154 {
1155 token_node_t *next_non_space;
1156
1157 /* Look ahead for a PASTE token, skipping space. */
1158 next_non_space = node->next;
1159 while (next_non_space && next_non_space->token->type == SPACE)
1160 next_non_space = next_non_space->next;
1161
1162 if (next_non_space == NULL)
1163 break;
1164
1165 if (next_non_space->token->type != PASTE) {
1166 node = next_non_space;
1167 continue;
1168 }
1169
1170 /* Now find the next non-space token after the PASTE. */
1171 next_non_space = next_non_space->next;
1172 while (next_non_space && next_non_space->token->type == SPACE)
1173 next_non_space = next_non_space->next;
1174
1175 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001176 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001177 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001178 }
1179
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001180 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001181 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001182 if (next_non_space == substituted->tail)
1183 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001184
1185 node = node->next;
1186 }
1187
Carl Worthb06096e2010-05-29 05:54:19 -07001188 substituted->non_space_tail = substituted->tail;
1189
Carl Worth681afbc2010-05-28 15:06:02 -07001190 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001191}
1192
Carl Worth681afbc2010-05-28 15:06:02 -07001193/* Compute the complete expansion of node, (and subsequent nodes after
1194 * 'node' in the case that 'node' is a function-like macro and
1195 * subsequent nodes are arguments).
1196 *
1197 * Returns NULL if node is a simple token with no expansion.
1198 *
1199 * Otherwise, returns the token list that results from the expansion
1200 * and sets *last to the last node in the list that was consumed by
1201 * the expansion. Specificallty, *last will be set as follows:
1202 *
1203 * As 'node' in the case of object-like macro expansion.
1204 *
1205 * As the token of the closing right parenthesis in the case of
1206 * function-like macro expansion.
1207 */
1208static token_list_t *
1209_glcpp_parser_expand_node (glcpp_parser_t *parser,
1210 token_node_t *node,
1211 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001212{
Carl Worth681afbc2010-05-28 15:06:02 -07001213 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001214 const char *identifier;
1215 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001216
1217 /* We only expand identifiers */
1218 if (token->type != IDENTIFIER) {
1219 /* We change any COMMA into a COMMA_FINAL to prevent
1220 * it being mistaken for an argument separator
1221 * later. */
1222 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001223 token->type = COMMA_FINAL;
1224 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001225 }
Carl Worth681afbc2010-05-28 15:06:02 -07001226
1227 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001228 }
1229
1230 /* Look up this identifier in the hash table. */
1231 identifier = token->value.str;
1232 macro = hash_table_find (parser->defines, identifier);
1233
Carl Worth681afbc2010-05-28 15:06:02 -07001234 /* Not a macro, so no expansion needed. */
1235 if (macro == NULL)
1236 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001237
1238 /* Finally, don't expand this macro if we're already actively
1239 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001240 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001241 /* We change the token type here from IDENTIFIER to
1242 * OTHER to prevent any future expansion of this
1243 * unexpanded token. */
1244 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001245 token_list_t *expansion;
1246 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001247
Carl Worth681afbc2010-05-28 15:06:02 -07001248 str = xtalloc_strdup (parser, token->value.str);
1249 final = _token_create_str (parser, OTHER, str);
1250 expansion = _token_list_create (parser);
1251 _token_list_append (expansion, final);
1252 *last = node;
1253 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001254 }
1255
Carl Worth681afbc2010-05-28 15:06:02 -07001256 if (! macro->is_function)
1257 {
1258 *last = node;
1259
1260 if (macro->replacements == NULL)
1261 return _token_list_create (parser);
1262
Carl Worth22b3ace2010-06-02 15:32:03 -07001263 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001264 }
Carl Worth681afbc2010-05-28 15:06:02 -07001265
1266 return _glcpp_parser_expand_function (parser, node, last);
1267}
1268
Carl Worth22b3ace2010-06-02 15:32:03 -07001269/* Push a new identifier onto the active list, returning the new list.
1270 *
1271 * Here, 'marker' is the token node that appears in the list after the
1272 * expansion of 'identifier'. That is, when the list iterator begins
1273 * examinging 'marker', then it is time to pop this node from the
1274 * active stack.
1275 */
1276active_list_t *
1277_active_list_push (active_list_t *list,
1278 const char *identifier,
1279 token_node_t *marker)
1280{
1281 active_list_t *node;
1282
1283 node = xtalloc (list, active_list_t);
1284 node->identifier = xtalloc_strdup (node, identifier);
1285 node->marker = marker;
1286 node->next = list;
1287
1288 return node;
1289}
1290
1291active_list_t *
1292_active_list_pop (active_list_t *list)
1293{
1294 active_list_t *node = list;
1295
1296 if (node == NULL)
1297 return NULL;
1298
1299 node = list->next;
1300 talloc_free (list);
1301
1302 return node;
1303}
1304
1305int
1306_active_list_contains (active_list_t *list, const char *identifier)
1307{
1308 active_list_t *node;
1309
1310 if (list == NULL)
1311 return 0;
1312
1313 for (node = list; node; node = node->next)
1314 if (strcmp (node->identifier, identifier) == 0)
1315 return 1;
1316
1317 return 0;
1318}
1319
Carl Worth681afbc2010-05-28 15:06:02 -07001320/* Walk over the token list replacing nodes with their expansion.
1321 * Whenever nodes are expanded the walking will walk over the new
1322 * nodes, continuing to expand as necessary. The results are placed in
1323 * 'list' itself;
1324 */
1325static void
1326_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1327 token_list_t *list)
1328{
1329 token_node_t *node_prev;
Carl Worthc42e6402010-06-22 15:51:34 -07001330 token_node_t *node, *last = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001331 token_list_t *expansion;
1332
1333 if (list == NULL)
1334 return;
1335
1336 _token_list_trim_trailing_space (list);
1337
1338 node_prev = NULL;
1339 node = list->head;
1340
1341 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001342
1343 while (parser->active && parser->active->marker == node)
1344 parser->active = _active_list_pop (parser->active);
1345
Carl Worth681afbc2010-05-28 15:06:02 -07001346 /* Find the expansion for node, which will replace all
1347 * nodes from node to last, inclusive. */
1348 expansion = _glcpp_parser_expand_node (parser, node, &last);
1349 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001350 token_node_t *n;
1351
1352 for (n = node; n != last->next; n = n->next)
1353 while (parser->active &&
1354 parser->active->marker == n)
1355 {
1356 parser->active = _active_list_pop (parser->active);
1357 }
1358
1359 parser->active = _active_list_push (parser->active,
1360 node->token->value.str,
1361 last->next);
1362
Carl Worth681afbc2010-05-28 15:06:02 -07001363 /* Splice expansion into list, supporting a
1364 * simple deletion if the expansion is
1365 * empty. */
1366 if (expansion->head) {
1367 if (node_prev)
1368 node_prev->next = expansion->head;
1369 else
1370 list->head = expansion->head;
1371 expansion->tail->next = last->next;
1372 if (last == list->tail)
1373 list->tail = expansion->tail;
1374 } else {
1375 if (node_prev)
1376 node_prev->next = last->next;
1377 else
1378 list->head = last->next;
1379 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001380 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001381 }
1382 } else {
1383 node_prev = node;
1384 }
1385 node = node_prev ? node_prev->next : list->head;
1386 }
1387
Carl Worth22b3ace2010-06-02 15:32:03 -07001388 while (parser->active)
1389 parser->active = _active_list_pop (parser->active);
1390
Carl Worth681afbc2010-05-28 15:06:02 -07001391 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001392}
1393
Carl Worthae6517f2010-05-25 15:24:59 -07001394void
1395_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1396 token_list_t *list)
1397{
Carl Worthae6517f2010-05-25 15:24:59 -07001398 if (list == NULL)
1399 return;
1400
Carl Worth681afbc2010-05-28 15:06:02 -07001401 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001402
Carl Worth681afbc2010-05-28 15:06:02 -07001403 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001404
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001405 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001406}
1407
1408void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001409_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1410 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001411{
1412 /* According to the GLSL specification, macro names starting with "__"
1413 * or "GL_" are reserved for future use. So, don't allow them.
1414 */
1415 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001416 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001417 }
1418 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001419 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001420 }
1421}
1422
1423void
Carl Worthfcbbb462010-05-13 09:36:23 -07001424_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001425 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001426 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001427 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001428{
1429 macro_t *macro;
1430
Ian Romanick2d122362010-06-30 16:03:19 -07001431 if (loc != NULL)
1432 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001433
Carl Worthfcbbb462010-05-13 09:36:23 -07001434 macro = xtalloc (parser, macro_t);
1435
1436 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001437 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001438 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001439 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001440
1441 hash_table_insert (parser->defines, macro, identifier);
1442}
1443
1444void
1445_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001446 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001447 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001448 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001449 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001450{
1451 macro_t *macro;
1452
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001453 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001454
Carl Worthfcbbb462010-05-13 09:36:23 -07001455 macro = xtalloc (parser, macro_t);
1456
1457 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001458 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001459 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001460 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001461
1462 hash_table_insert (parser->defines, macro, identifier);
1463}
1464
Carl Worth8f38aff2010-05-19 10:01:29 -07001465static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001466glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001467{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001468 token_node_t *node;
1469 int ret;
1470
Carl Worth95951ea2010-05-26 15:57:10 -07001471 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001472 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001473
1474 /* XXX: This ugly block of code exists for the sole
1475 * purpose of converting a NEWLINE token into a SPACE
1476 * token, but only in the case where we have seen a
1477 * function-like macro name, but have not yet seen its
1478 * closing parenthesis.
1479 *
1480 * There's perhaps a more compact way to do this with
1481 * mid-rule actions in the grammar.
1482 *
1483 * I'm definitely not pleased with the complexity of
1484 * this code here.
1485 */
1486 if (parser->newline_as_space)
1487 {
1488 if (ret == '(') {
1489 parser->paren_count++;
1490 } else if (ret == ')') {
1491 parser->paren_count--;
1492 if (parser->paren_count == 0)
1493 parser->newline_as_space = 0;
1494 } else if (ret == NEWLINE) {
1495 ret = SPACE;
1496 } else if (ret != SPACE) {
1497 if (parser->paren_count == 0)
1498 parser->newline_as_space = 0;
1499 }
1500 }
1501 else if (parser->in_control_line)
1502 {
1503 if (ret == NEWLINE)
1504 parser->in_control_line = 0;
1505 }
1506 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1507 ret == HASH_UNDEF || ret == HASH_IF ||
1508 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1509 ret == HASH_ELIF || ret == HASH_ELSE ||
1510 ret == HASH_ENDIF || ret == HASH)
1511 {
1512 parser->in_control_line = 1;
1513 }
1514 else if (ret == IDENTIFIER)
1515 {
1516 macro_t *macro;
1517 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001518 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001519 if (macro && macro->is_function) {
1520 parser->newline_as_space = 1;
1521 parser->paren_count = 0;
1522 }
1523 }
1524
1525 return ret;
1526 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001527
1528 node = parser->lex_from_node;
1529
1530 if (node == NULL) {
1531 talloc_free (parser->lex_from_list);
1532 parser->lex_from_list = NULL;
1533 return NEWLINE;
1534 }
1535
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001536 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001537 ret = node->token->type;
1538
1539 parser->lex_from_node = node->next;
1540
1541 return ret;
1542}
1543
1544static void
1545glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1546{
1547 token_node_t *node;
1548
1549 assert (parser->lex_from_list == NULL);
1550
1551 /* Copy list, eliminating any space tokens. */
1552 parser->lex_from_list = _token_list_create (parser);
1553
1554 for (node = list->head; node; node = node->next) {
1555 if (node->token->type == SPACE)
1556 continue;
1557 _token_list_append (parser->lex_from_list, node->token);
1558 }
1559
1560 talloc_free (list);
1561
1562 parser->lex_from_node = parser->lex_from_list->head;
1563
1564 /* It's possible the list consisted of nothing but whitespace. */
1565 if (parser->lex_from_node == NULL) {
1566 talloc_free (parser->lex_from_list);
1567 parser->lex_from_list = NULL;
1568 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001569}
Carl Worthb20d33c2010-05-20 22:27:07 -07001570
1571static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001572_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1573 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001574{
1575 skip_type_t current = SKIP_NO_SKIP;
1576 skip_node_t *node;
1577
1578 if (parser->skip_stack)
1579 current = parser->skip_stack->type;
1580
1581 node = xtalloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001582 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001583
1584 if (current == SKIP_NO_SKIP) {
1585 if (condition)
1586 node->type = SKIP_NO_SKIP;
1587 else
1588 node->type = SKIP_TO_ELSE;
1589 } else {
1590 node->type = SKIP_TO_ENDIF;
1591 }
1592
1593 node->next = parser->skip_stack;
1594 parser->skip_stack = node;
1595}
1596
1597static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001598_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1599 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001600{
1601 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001602 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001603 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001604 }
1605
1606 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1607 if (condition)
1608 parser->skip_stack->type = SKIP_NO_SKIP;
1609 } else {
1610 parser->skip_stack->type = SKIP_TO_ENDIF;
1611 }
1612}
Carl Worth80dc60b2010-05-25 14:42:00 -07001613
Carl Worthb20d33c2010-05-20 22:27:07 -07001614static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001615_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001616{
1617 skip_node_t *node;
1618
1619 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001620 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001621 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001622 }
1623
1624 node = parser->skip_stack;
1625 parser->skip_stack = node->next;
1626 talloc_free (node);
1627}