blob: c314e9556cedbf858adb016eccf931afc61a71ed [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 void
Carl Worth610053b2010-05-14 10:05:11 -070059_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070060
Carl Worth5aa7ea02010-05-25 18:39:43 -070061static int
Carl Worth610053b2010-05-14 10:05:11 -070062_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070063
Carl Worth5aa7ea02010-05-25 18:39:43 -070064static int
Carl Worth610053b2010-05-14 10:05:11 -070065_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070066
Carl Worth5aa7ea02010-05-25 18:39:43 -070067static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070068_argument_list_create (void *ctx);
69
Carl Worth5aa7ea02010-05-25 18:39:43 -070070static void
Carl Worth47252442010-05-19 13:54:37 -070071_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070072
Carl Worth5aa7ea02010-05-25 18:39:43 -070073static int
Carl Worth8f6a8282010-05-14 10:44:19 -070074_argument_list_length (argument_list_t *list);
75
Carl Worth5aa7ea02010-05-25 18:39:43 -070076static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070077_argument_list_member_at (argument_list_t *list, int index);
78
Carl Worth808401f2010-05-25 14:52:43 -070079/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070080static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070081_token_create_str (void *ctx, int type, char *str);
82
Carl Worth5aa7ea02010-05-25 18:39:43 -070083static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070084_token_create_ival (void *ctx, int type, int ival);
85
Carl Worth5aa7ea02010-05-25 18:39:43 -070086static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070087_token_list_create (void *ctx);
88
Carl Worthb1ae61a2010-05-26 08:10:38 -070089/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070090 *
91 * You may want to talloc_unlink any current reference if you no
92 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070093static void
Carl Worth808401f2010-05-25 14:52:43 -070094_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070095
Carl Worth5aa7ea02010-05-25 18:39:43 -070096static void
Carl Worth47252442010-05-19 13:54:37 -070097_token_list_append_list (token_list_t *list, token_list_t *tail);
98
Carl Worth22b3ace2010-06-02 15:32:03 -070099static int
100_token_list_length (token_list_t *list);
101
102static active_list_t *
103_active_list_push (active_list_t *list,
104 const char *identifier,
105 token_node_t *marker);
106
107static active_list_t *
108_active_list_pop (active_list_t *list);
109
110int
111_active_list_contains (active_list_t *list, const char *identifier);
112
Carl Worth5aa7ea02010-05-25 18:39:43 -0700113static void
Carl Worth681afbc2010-05-28 15:06:02 -0700114_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
115 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700116
Carl Worthaaa9acb2010-05-19 13:28:24 -0700117static void
Carl Worth681afbc2010-05-28 15:06:02 -0700118_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
119 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700120
121static void
Kenneth Graunke07745232010-06-17 12:41:46 -0700122_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
123 int condition);
Carl Worthb20d33c2010-05-20 22:27:07 -0700124
125static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700126_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
127 const char *type, int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700128
Carl Worthb20d33c2010-05-20 22:27:07 -0700129static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700130_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
Carl Worthb20d33c2010-05-20 22:27:07 -0700131
Carl Worth0293b2e2010-05-19 10:05:40 -0700132#define yylex glcpp_parser_lex
133
Carl Worth8f38aff2010-05-19 10:01:29 -0700134static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700135glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700136
Carl Worth8e82fcb2010-05-26 11:15:21 -0700137static void
138glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
139
Carl Worth3a37b872010-05-10 11:44:09 -0700140%}
141
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700142%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700143%error-verbose
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700144%locations
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700145
Carl Worth0b27b5f2010-05-10 16:16:06 -0700146%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700147%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700148
Carl Worth050e3de2010-05-27 14:36:29 -0700149%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 -0700150%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700151%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700152%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700153%type <string_list> identifier_list
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700154%type <token> preprocessing_token conditional_token
155%type <token_list> pp_tokens replacement_list text_line conditional_tokens
Carl Worth8fed1cd2010-05-26 09:32:12 -0700156%left OR
157%left AND
158%left '|'
159%left '^'
160%left '&'
161%left EQUAL NOT_EQUAL
162%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
163%left LEFT_SHIFT RIGHT_SHIFT
164%left '+' '-'
165%left '*' '/' '%'
166%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700167
168%%
169
Carl Worth33cc4002010-05-12 12:17:10 -0700170input:
Carl Worth3ff81672010-05-25 13:09:03 -0700171 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700172| input line
173;
174
175line:
176 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700177 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700178 }
Carl Worth808401f2010-05-25 14:52:43 -0700179| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700180 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700181 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700182 talloc_free ($1);
183 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700184| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700185| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700186;
187
Carl Worth8e82fcb2010-05-26 11:15:21 -0700188expanded_line:
189 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700190 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700191 }
192| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700193 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700194 }
195;
196
Carl Worth3ff81672010-05-25 13:09:03 -0700197control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700198 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700199 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700200 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700201| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700202 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700203 }
204| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700205 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700206 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700207| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700208 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700209 if (macro) {
210 /* XXX: Need hash table to support a real way
211 * to remove an element rather than prefixing
212 * a new node with data of NULL like this. */
213 hash_table_insert (parser->defines, NULL, $2);
214 talloc_free (macro);
215 }
216 talloc_free ($2);
217 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700218| HASH_IF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700219 token_list_t *expanded;
220 token_t *token;
221
222 expanded = _token_list_create (parser);
223 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
224 _token_list_append (expanded, token);
225 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700226 _glcpp_parser_expand_token_list (parser, $2);
227 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700228 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700229 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700230| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700231 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700232 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700233 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700234 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700235| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700236 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700237 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700238 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700239 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700240| HASH_ELIF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700241 token_list_t *expanded;
242 token_t *token;
243
244 expanded = _token_list_create (parser);
245 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
246 _token_list_append (expanded, token);
247 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700248 _glcpp_parser_expand_token_list (parser, $2);
249 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700250 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700251 }
Kenneth Graunke332fc472010-06-21 12:20:22 -0700252| HASH_ELIF NEWLINE {
253 /* #elif without an expression results in a warning if the
254 * condition doesn't matter (we just handled #if 1 or such)
255 * but an error otherwise. */
256 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
257 parser->skip_stack->type = SKIP_TO_ENDIF;
258 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
259 } else {
260 glcpp_error(& @1, parser, "#elif needs an expression");
261 }
262 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700263| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700264 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700265 }
266| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700267 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700268 }
Carl Worth3ff81672010-05-25 13:09:03 -0700269| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700270;
271
Carl Worth8fed1cd2010-05-26 09:32:12 -0700272expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700273 INTEGER_STRING {
274 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
275 $$ = strtoll ($1 + 2, NULL, 16);
276 } else if ($1[0] == '0') {
277 $$ = strtoll ($1, NULL, 8);
278 } else {
279 $$ = strtoll ($1, NULL, 10);
280 }
281 }
282| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700283 $$ = $1;
284 }
285| expression OR expression {
286 $$ = $1 || $3;
287 }
288| expression AND expression {
289 $$ = $1 && $3;
290 }
291| expression '|' expression {
292 $$ = $1 | $3;
293 }
294| expression '^' expression {
295 $$ = $1 ^ $3;
296 }
297| expression '&' expression {
298 $$ = $1 & $3;
299 }
300| expression NOT_EQUAL expression {
301 $$ = $1 != $3;
302 }
303| expression EQUAL expression {
304 $$ = $1 == $3;
305 }
306| expression GREATER_OR_EQUAL expression {
307 $$ = $1 >= $3;
308 }
309| expression LESS_OR_EQUAL expression {
310 $$ = $1 <= $3;
311 }
312| expression '>' expression {
313 $$ = $1 > $3;
314 }
315| expression '<' expression {
316 $$ = $1 < $3;
317 }
318| expression RIGHT_SHIFT expression {
319 $$ = $1 >> $3;
320 }
321| expression LEFT_SHIFT 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 '/' expression {
334 $$ = $1 / $3;
335 }
336| expression '*' expression {
337 $$ = $1 * $3;
338 }
339| '!' expression %prec UNARY {
340 $$ = ! $2;
341 }
342| '~' expression %prec UNARY {
343 $$ = ~ $2;
344 }
345| '-' expression %prec UNARY {
346 $$ = - $2;
347 }
348| '+' expression %prec UNARY {
349 $$ = + $2;
350 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700351| '(' expression ')' {
352 $$ = $2;
353 }
354;
355
Carl Worth3ff81672010-05-25 13:09:03 -0700356identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700357 IDENTIFIER {
358 $$ = _string_list_create (parser);
359 _string_list_append_item ($$, $1);
360 talloc_steal ($$, $1);
361 }
362| identifier_list ',' IDENTIFIER {
363 $$ = $1;
364 _string_list_append_item ($$, $3);
365 talloc_steal ($$, $3);
366 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700367;
368
Carl Worth3ff81672010-05-25 13:09:03 -0700369text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700370 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700371| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700372;
373
Carl Worth3ff81672010-05-25 13:09:03 -0700374non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700375 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700376 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700377 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700378;
379
Carl Worthaaa9acb2010-05-19 13:28:24 -0700380replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700381 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700382| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700383;
384
Kenneth Graunke65875742010-06-18 20:08:15 -0700385junk:
386 /* empty */
387| pp_tokens {
388 glcpp_warning(&@1, parser, "extra tokens at end of directive");
389 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700390;
391
392conditional_token:
393 /* Handle "defined" operator */
394 DEFINED IDENTIFIER {
395 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
396 $$ = _token_create_ival (parser, INTEGER, v);
397 }
398| DEFINED '(' IDENTIFIER ')' {
399 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
400 $$ = _token_create_ival (parser, INTEGER, v);
401 }
402| preprocessing_token
403;
404
405conditional_tokens:
406 /* Exactly the same as pp_tokens, but using conditional_token */
407 conditional_token {
408 parser->space_tokens = 1;
409 $$ = _token_list_create (parser);
410 _token_list_append ($$, $1);
411 talloc_unlink (parser, $1);
412 }
413| conditional_tokens conditional_token {
414 $$ = $1;
415 _token_list_append ($$, $2);
416 talloc_unlink (parser, $2);
417 }
418;
Kenneth Graunke65875742010-06-18 20:08:15 -0700419
Carl Worthaaa9acb2010-05-19 13:28:24 -0700420pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700421 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700422 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700423 $$ = _token_list_create (parser);
424 _token_list_append ($$, $1);
425 talloc_unlink (parser, $1);
426 }
427| pp_tokens preprocessing_token {
428 $$ = $1;
429 _token_list_append ($$, $2);
430 talloc_unlink (parser, $2);
431 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700432;
433
Carl Worth3ff81672010-05-25 13:09:03 -0700434preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700435 IDENTIFIER {
436 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700437 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700438 }
Carl Worth050e3de2010-05-27 14:36:29 -0700439| INTEGER_STRING {
440 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700441 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700442 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700443| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700444 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700445 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700446 }
447| OTHER {
448 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700449 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700450 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700451| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700452 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700453 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700454 }
Carl Worth3ff81672010-05-25 13:09:03 -0700455;
456
Carl Worth8e82fcb2010-05-26 11:15:21 -0700457operator:
Carl Worth808401f2010-05-25 14:52:43 -0700458 '[' { $$ = '['; }
459| ']' { $$ = ']'; }
460| '(' { $$ = '('; }
461| ')' { $$ = ')'; }
462| '{' { $$ = '{'; }
463| '}' { $$ = '}'; }
464| '.' { $$ = '.'; }
465| '&' { $$ = '&'; }
466| '*' { $$ = '*'; }
467| '+' { $$ = '+'; }
468| '-' { $$ = '-'; }
469| '~' { $$ = '~'; }
470| '!' { $$ = '!'; }
471| '/' { $$ = '/'; }
472| '%' { $$ = '%'; }
473| LEFT_SHIFT { $$ = LEFT_SHIFT; }
474| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
475| '<' { $$ = '<'; }
476| '>' { $$ = '>'; }
477| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
478| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
479| EQUAL { $$ = EQUAL; }
480| NOT_EQUAL { $$ = NOT_EQUAL; }
481| '^' { $$ = '^'; }
482| '|' { $$ = '|'; }
483| AND { $$ = AND; }
484| OR { $$ = OR; }
485| ';' { $$ = ';'; }
486| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700487| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700488| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700489| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700490;
491
Carl Worth33cc4002010-05-12 12:17:10 -0700492%%
493
Carl Worth610053b2010-05-14 10:05:11 -0700494string_list_t *
495_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700496{
Carl Worth610053b2010-05-14 10:05:11 -0700497 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700498
Carl Worth610053b2010-05-14 10:05:11 -0700499 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700500 list->head = NULL;
501 list->tail = NULL;
502
503 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700504}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700505
Carl Worth33cc4002010-05-12 12:17:10 -0700506void
Carl Worth610053b2010-05-14 10:05:11 -0700507_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700508{
509 if (list->head == NULL) {
510 list->head = tail->head;
511 } else {
512 list->tail->next = tail->head;
513 }
514
515 list->tail = tail->tail;
516}
517
518void
Carl Worth610053b2010-05-14 10:05:11 -0700519_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700520{
Carl Worth610053b2010-05-14 10:05:11 -0700521 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700522
Carl Worth610053b2010-05-14 10:05:11 -0700523 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700524 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700525
Carl Worth33cc4002010-05-12 12:17:10 -0700526 node->next = NULL;
527
528 if (list->head == NULL) {
529 list->head = node;
530 } else {
531 list->tail->next = node;
532 }
533
534 list->tail = node;
535}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700536
537int
Carl Worth610053b2010-05-14 10:05:11 -0700538_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700539{
Carl Worth610053b2010-05-14 10:05:11 -0700540 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700541 int i;
542
543 if (list == NULL)
544 return 0;
545
546 for (i = 0, node = list->head; node; i++, node = node->next) {
547 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700548 if (index)
549 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700550 return 1;
551 }
552 }
553
554 return 0;
555}
556
557int
Carl Worth610053b2010-05-14 10:05:11 -0700558_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700559{
560 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700561 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700562
563 if (list == NULL)
564 return 0;
565
566 for (node = list->head; node; node = node->next)
567 length++;
568
569 return length;
570}
571
Carl Worth8f6a8282010-05-14 10:44:19 -0700572argument_list_t *
573_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700574{
Carl Worth8f6a8282010-05-14 10:44:19 -0700575 argument_list_t *list;
576
577 list = xtalloc (ctx, argument_list_t);
578 list->head = NULL;
579 list->tail = NULL;
580
581 return list;
582}
583
584void
Carl Worth47252442010-05-19 13:54:37 -0700585_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700586{
587 argument_node_t *node;
588
Carl Worth8f6a8282010-05-14 10:44:19 -0700589 node = xtalloc (list, argument_node_t);
590 node->argument = argument;
591
592 node->next = NULL;
593
594 if (list->head == NULL) {
595 list->head = node;
596 } else {
597 list->tail->next = node;
598 }
599
600 list->tail = node;
601}
602
603int
604_argument_list_length (argument_list_t *list)
605{
606 int length = 0;
607 argument_node_t *node;
608
609 if (list == NULL)
610 return 0;
611
612 for (node = list->head; node; node = node->next)
613 length++;
614
615 return length;
616}
617
Carl Worth47252442010-05-19 13:54:37 -0700618token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700619_argument_list_member_at (argument_list_t *list, int index)
620{
621 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700622 int i;
623
624 if (list == NULL)
625 return NULL;
626
627 node = list->head;
628 for (i = 0; i < index; i++) {
629 node = node->next;
630 if (node == NULL)
631 break;
632 }
633
634 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700635 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700636
637 return NULL;
638}
Carl Worth47252442010-05-19 13:54:37 -0700639
Carl Worth808401f2010-05-25 14:52:43 -0700640/* Note: This function talloc_steal()s the str pointer. */
641token_t *
642_token_create_str (void *ctx, int type, char *str)
643{
644 token_t *token;
645
646 token = xtalloc (ctx, token_t);
647 token->type = type;
648 token->value.str = talloc_steal (token, str);
649
650 return token;
651}
652
653token_t *
654_token_create_ival (void *ctx, int type, int ival)
655{
656 token_t *token;
657
658 token = xtalloc (ctx, token_t);
659 token->type = type;
660 token->value.ival = ival;
661
662 return token;
663}
664
Carl Worth47252442010-05-19 13:54:37 -0700665token_list_t *
666_token_list_create (void *ctx)
667{
668 token_list_t *list;
669
670 list = xtalloc (ctx, token_list_t);
671 list->head = NULL;
672 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700673 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700674
675 return list;
676}
677
678void
Carl Worth808401f2010-05-25 14:52:43 -0700679_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700680{
681 token_node_t *node;
682
683 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700684 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700685
686 node->next = NULL;
687
688 if (list->head == NULL) {
689 list->head = node;
690 } else {
691 list->tail->next = node;
692 }
693
694 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700695 if (token->type != SPACE)
696 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700697}
698
699void
700_token_list_append_list (token_list_t *list, token_list_t *tail)
701{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700702 if (tail == NULL || tail->head == NULL)
703 return;
704
Carl Worth47252442010-05-19 13:54:37 -0700705 if (list->head == NULL) {
706 list->head = tail->head;
707 } else {
708 list->tail->next = tail->head;
709 }
710
711 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700712 list->non_space_tail = tail->non_space_tail;
713}
714
Carl Worth681afbc2010-05-28 15:06:02 -0700715token_list_t *
716_token_list_copy (void *ctx, token_list_t *other)
717{
718 token_list_t *copy;
719 token_node_t *node;
720
721 if (other == NULL)
722 return NULL;
723
724 copy = _token_list_create (ctx);
725 for (node = other->head; node; node = node->next)
726 _token_list_append (copy, node->token);
727
728 return copy;
729}
730
Carl Worth10ae4382010-05-25 20:35:01 -0700731void
732_token_list_trim_trailing_space (token_list_t *list)
733{
734 token_node_t *tail, *next;
735
736 if (list->non_space_tail) {
737 tail = list->non_space_tail->next;
738 list->non_space_tail->next = NULL;
739 list->tail = list->non_space_tail;
740
741 while (tail) {
742 next = tail->next;
743 talloc_free (tail);
744 tail = next;
745 }
746 }
Carl Worth47252442010-05-19 13:54:37 -0700747}
Carl Worth80dc60b2010-05-25 14:42:00 -0700748
Carl Worth22b3ace2010-06-02 15:32:03 -0700749static int
750_token_list_length (token_list_t *list)
751{
752 int length = 0;
753 token_node_t *node;
754
755 if (list == NULL)
756 return 0;
757
758 for (node = list->head; node; node = node->next)
759 length++;
760
761 return length;
762}
763
Carl Worth0197e9b2010-05-26 08:05:19 -0700764static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700765_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700766{
767 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700768 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700769 return;
770 }
771
772 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700773 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700774 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700775 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700776 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700777 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700778 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700779 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700780 break;
781 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700782 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700783 break;
784 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700785 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700786 break;
787 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700788 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700789 break;
790 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700791 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700792 break;
793 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700794 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700795 break;
796 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700797 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700798 break;
799 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700800 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700801 break;
802 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700803 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700804 break;
805 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700806 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700807 break;
808 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700809 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700810 break;
Carl Worthdd749002010-05-27 10:12:33 -0700811 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700812 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700813 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700814 case PLACEHOLDER:
815 /* Nothing to print. */
816 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700817 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700818 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700819 break;
820 }
821}
822
Carl Worthb06096e2010-05-29 05:54:19 -0700823/* Return a new token (talloc()ed off of 'token') formed by pasting
824 * 'token' and 'other'. Note that this function may return 'token' or
825 * 'other' directly rather than allocating anything new.
826 *
827 * Caution: Only very cursory error-checking is performed to see if
828 * the final result is a valid single token. */
829static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700830_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700831{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700832 token_t *combined = NULL;
833
Carl Worth85b50e82010-05-27 14:01:18 -0700834 /* Pasting a placeholder onto anything makes no change. */
835 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700836 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700837
Carl Worthb06096e2010-05-29 05:54:19 -0700838 /* When 'token' is a placeholder, just return 'other'. */
839 if (token->type == PLACEHOLDER)
840 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700841
Carl Worthad0dee62010-05-26 09:04:50 -0700842 /* A very few single-character punctuators can be combined
843 * with another to form a multi-character punctuator. */
844 switch (token->type) {
845 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700846 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700847 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700848 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700849 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700850 break;
851 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700852 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700853 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700854 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700855 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700856 break;
857 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700858 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700859 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700860 break;
861 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700862 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700863 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700864 break;
865 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700866 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700867 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700868 break;
869 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700870 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700871 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700872 break;
873 }
874
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700875 if (combined != NULL) {
876 /* Inherit the location from the first token */
877 combined->location = token->location;
878 return combined;
879 }
880
Carl Worthad0dee62010-05-26 09:04:50 -0700881 /* Two string-valued tokens can usually just be mashed
882 * together.
883 *
Carl Worth050e3de2010-05-27 14:36:29 -0700884 * XXX: This isn't actually legitimate. Several things here
885 * should result in a diagnostic since the result cannot be a
886 * valid, single pre-processing token. For example, pasting
887 * "123" and "abc" is not legal, but we don't catch that
888 * here. */
889 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
890 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700891 {
Carl Worthb06096e2010-05-29 05:54:19 -0700892 char *str;
893
894 str = xtalloc_asprintf (token, "%s%s",
895 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700896 combined = _token_create_str (token, token->type, str);
897 combined->location = token->location;
898 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700899 }
900
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700901 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700902 glcpp_print (parser->info_log, "Pasting \"");
903 _token_print (&parser->info_log, token);
904 glcpp_print (parser->info_log, "\" and \"");
905 _token_print (&parser->info_log, other);
906 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700907
908 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700909}
910
Carl Worth0197e9b2010-05-26 08:05:19 -0700911static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700912_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700913{
914 token_node_t *node;
915
916 if (list == NULL)
917 return;
918
919 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700920 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700921}
922
Carl Worth3a37b872010-05-10 11:44:09 -0700923void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700924yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700925{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700926 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700927}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700928
Carl Worth33cc4002010-05-12 12:17:10 -0700929glcpp_parser_t *
930glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700931{
Carl Worth33cc4002010-05-12 12:17:10 -0700932 glcpp_parser_t *parser;
933
Carl Worth5070a202010-05-12 12:45:33 -0700934 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700935
Carl Worth8f38aff2010-05-19 10:01:29 -0700936 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700937 parser->defines = hash_table_ctor (32, hash_table_string_hash,
938 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700939 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700940 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700941 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700942 parser->newline_as_space = 0;
943 parser->in_control_line = 0;
944 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700945
Carl Worthb20d33c2010-05-20 22:27:07 -0700946 parser->skip_stack = NULL;
947
Carl Worth8e82fcb2010-05-26 11:15:21 -0700948 parser->lex_from_list = NULL;
949 parser->lex_from_node = NULL;
950
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700951 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700952 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700953
Carl Worth33cc4002010-05-12 12:17:10 -0700954 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700955}
956
957int
958glcpp_parser_parse (glcpp_parser_t *parser)
959{
960 return yyparse (parser);
961}
962
963void
Carl Worth33cc4002010-05-12 12:17:10 -0700964glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700965{
Carl Worthb20d33c2010-05-20 22:27:07 -0700966 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700967 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700968 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700969 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700970 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700971}
Carl Worthc6d5af32010-05-11 12:30:09 -0700972
Carl Worthb1854fd2010-05-25 16:28:26 -0700973typedef enum function_status
974{
975 FUNCTION_STATUS_SUCCESS,
976 FUNCTION_NOT_A_FUNCTION,
977 FUNCTION_UNBALANCED_PARENTHESES
978} function_status_t;
979
980/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700981 * balanced set of parentheses.
982 *
983 * When called, 'node' should be the opening-parenthesis token, (or
984 * perhaps preceeding SPACE tokens). Upon successful return *last will
985 * be the last consumed node, (corresponding to the closing right
986 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700987 *
988 * Return values:
989 *
990 * FUNCTION_STATUS_SUCCESS:
991 *
992 * Successfully parsed a set of function arguments.
993 *
994 * FUNCTION_NOT_A_FUNCTION:
995 *
996 * Macro name not followed by a '('. This is not an error, but
997 * simply that the macro name should be treated as a non-macro.
998 *
Carl Worth14c98a52010-06-02 15:49:54 -0700999 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -07001000 *
1001 * Macro name is not followed by a balanced set of parentheses.
1002 */
1003static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -07001004_arguments_parse (argument_list_t *arguments,
1005 token_node_t *node,
1006 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001007{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001008 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001009 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001010
Carl Worthb1854fd2010-05-25 16:28:26 -07001011 node = node->next;
1012
1013 /* Ignore whitespace before first parenthesis. */
1014 while (node && node->token->type == SPACE)
1015 node = node->next;
1016
1017 if (node == NULL || node->token->type != '(')
1018 return FUNCTION_NOT_A_FUNCTION;
1019
Carl Worth652fa272010-05-25 17:45:22 -07001020 node = node->next;
1021
Carl Wortha19297b2010-05-27 13:29:19 -07001022 argument = _token_list_create (arguments);
1023 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001024
Carl Worth681afbc2010-05-28 15:06:02 -07001025 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001026 if (node->token->type == '(')
1027 {
1028 paren_count++;
1029 }
1030 else if (node->token->type == ')')
1031 {
1032 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001033 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001034 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001035 }
Carl Worth652fa272010-05-25 17:45:22 -07001036
1037 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001038 paren_count == 1)
1039 {
Carl Wortha19297b2010-05-27 13:29:19 -07001040 _token_list_trim_trailing_space (argument);
1041 argument = _token_list_create (arguments);
1042 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001043 }
1044 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001045 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001046 /* Don't treat initial whitespace as
1047 * part of the arguement. */
1048 if (node->token->type == SPACE)
1049 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001050 }
1051 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001052 }
Carl Worthc7581c22010-05-25 17:41:07 -07001053 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001054
Carl Worth681afbc2010-05-28 15:06:02 -07001055 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001056 return FUNCTION_UNBALANCED_PARENTHESES;
1057
Carl Worth681afbc2010-05-28 15:06:02 -07001058 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001059
1060 return FUNCTION_STATUS_SUCCESS;
1061}
1062
Carl Worth681afbc2010-05-28 15:06:02 -07001063/* This is a helper function that's essentially part of the
1064 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1065 * except for by that function.
1066 *
1067 * Returns NULL if node is a simple token with no expansion, (that is,
1068 * although 'node' corresponds to an identifier defined as a
1069 * function-like macro, it is not followed with a parenthesized
1070 * argument list).
1071 *
1072 * Compute the complete expansion of node (which is a function-like
1073 * macro) and subsequent nodes which are arguments.
1074 *
1075 * Returns the token list that results from the expansion and sets
1076 * *last to the last node in the list that was consumed by the
1077 * expansion. Specificallty, *last will be set as follows: as the
1078 * token of the closing right parenthesis.
1079 */
1080static token_list_t *
1081_glcpp_parser_expand_function (glcpp_parser_t *parser,
1082 token_node_t *node,
1083 token_node_t **last)
1084
Carl Worthb1854fd2010-05-25 16:28:26 -07001085{
1086 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001087 const char *identifier;
1088 argument_list_t *arguments;
1089 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001090 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001091 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001092
Carl Worthb1854fd2010-05-25 16:28:26 -07001093 identifier = node->token->value.str;
1094
1095 macro = hash_table_find (parser->defines, identifier);
1096
1097 assert (macro->is_function);
1098
Carl Worth9ce18cf2010-05-25 17:32:21 -07001099 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001100 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001101
1102 switch (status) {
1103 case FUNCTION_STATUS_SUCCESS:
1104 break;
1105 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001106 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001107 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001108 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001109 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001110 }
1111
Carl Worth9ce18cf2010-05-25 17:32:21 -07001112 if (macro->replacements == NULL) {
1113 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001114 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001115 }
1116
Carl Wortha19297b2010-05-27 13:29:19 -07001117 if (! ((_argument_list_length (arguments) ==
1118 _string_list_length (macro->parameters)) ||
1119 (_string_list_length (macro->parameters) == 0 &&
1120 _argument_list_length (arguments) == 1 &&
1121 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001122 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001123 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001124 "Error: macro %s invoked with %d arguments (expected %d)\n",
1125 identifier,
1126 _argument_list_length (arguments),
1127 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001128 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001129 }
1130
Carl Worth0197e9b2010-05-26 08:05:19 -07001131 /* Perform argument substitution on the replacement list. */
1132 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001133
Carl Worthce540f22010-05-26 08:25:44 -07001134 for (node = macro->replacements->head; node; node = node->next)
1135 {
1136 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001137 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001138 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001139 &parameter_index))
1140 {
1141 token_list_t *argument;
1142 argument = _argument_list_member_at (arguments,
1143 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001144 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001145 * tokens, or append a placeholder token for
1146 * an empty argument. */
1147 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001148 _glcpp_parser_expand_token_list (parser,
1149 argument);
1150 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001151 } else {
1152 token_t *new_token;
1153
1154 new_token = _token_create_ival (substituted,
1155 PLACEHOLDER,
1156 PLACEHOLDER);
1157 _token_list_append (substituted, new_token);
1158 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001159 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001160 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001161 }
1162 }
1163
Carl Worthad0dee62010-05-26 09:04:50 -07001164 /* After argument substitution, and before further expansion
1165 * below, implement token pasting. */
1166
Carl Worthb06096e2010-05-29 05:54:19 -07001167 _token_list_trim_trailing_space (substituted);
1168
Carl Worthad0dee62010-05-26 09:04:50 -07001169 node = substituted->head;
1170 while (node)
1171 {
1172 token_node_t *next_non_space;
1173
1174 /* Look ahead for a PASTE token, skipping space. */
1175 next_non_space = node->next;
1176 while (next_non_space && next_non_space->token->type == SPACE)
1177 next_non_space = next_non_space->next;
1178
1179 if (next_non_space == NULL)
1180 break;
1181
1182 if (next_non_space->token->type != PASTE) {
1183 node = next_non_space;
1184 continue;
1185 }
1186
1187 /* Now find the next non-space token after the PASTE. */
1188 next_non_space = next_non_space->next;
1189 while (next_non_space && next_non_space->token->type == SPACE)
1190 next_non_space = next_non_space->next;
1191
1192 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001193 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001194 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001195 }
1196
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001197 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001198 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001199 if (next_non_space == substituted->tail)
1200 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001201
1202 node = node->next;
1203 }
1204
Carl Worthb06096e2010-05-29 05:54:19 -07001205 substituted->non_space_tail = substituted->tail;
1206
Carl Worth681afbc2010-05-28 15:06:02 -07001207 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001208}
1209
Carl Worth681afbc2010-05-28 15:06:02 -07001210/* Compute the complete expansion of node, (and subsequent nodes after
1211 * 'node' in the case that 'node' is a function-like macro and
1212 * subsequent nodes are arguments).
1213 *
1214 * Returns NULL if node is a simple token with no expansion.
1215 *
1216 * Otherwise, returns the token list that results from the expansion
1217 * and sets *last to the last node in the list that was consumed by
1218 * the expansion. Specificallty, *last will be set as follows:
1219 *
1220 * As 'node' in the case of object-like macro expansion.
1221 *
1222 * As the token of the closing right parenthesis in the case of
1223 * function-like macro expansion.
1224 */
1225static token_list_t *
1226_glcpp_parser_expand_node (glcpp_parser_t *parser,
1227 token_node_t *node,
1228 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001229{
Carl Worth681afbc2010-05-28 15:06:02 -07001230 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001231 const char *identifier;
1232 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001233
1234 /* We only expand identifiers */
1235 if (token->type != IDENTIFIER) {
1236 /* We change any COMMA into a COMMA_FINAL to prevent
1237 * it being mistaken for an argument separator
1238 * later. */
1239 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001240 token->type = COMMA_FINAL;
1241 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001242 }
Carl Worth681afbc2010-05-28 15:06:02 -07001243
1244 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001245 }
1246
1247 /* Look up this identifier in the hash table. */
1248 identifier = token->value.str;
1249 macro = hash_table_find (parser->defines, identifier);
1250
Carl Worth681afbc2010-05-28 15:06:02 -07001251 /* Not a macro, so no expansion needed. */
1252 if (macro == NULL)
1253 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001254
1255 /* Finally, don't expand this macro if we're already actively
1256 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001257 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001258 /* We change the token type here from IDENTIFIER to
1259 * OTHER to prevent any future expansion of this
1260 * unexpanded token. */
1261 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001262 token_list_t *expansion;
1263 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001264
Carl Worth681afbc2010-05-28 15:06:02 -07001265 str = xtalloc_strdup (parser, token->value.str);
1266 final = _token_create_str (parser, OTHER, str);
1267 expansion = _token_list_create (parser);
1268 _token_list_append (expansion, final);
1269 *last = node;
1270 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001271 }
1272
Carl Worth681afbc2010-05-28 15:06:02 -07001273 if (! macro->is_function)
1274 {
1275 *last = node;
1276
1277 if (macro->replacements == NULL)
1278 return _token_list_create (parser);
1279
Carl Worth22b3ace2010-06-02 15:32:03 -07001280 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001281 }
Carl Worth681afbc2010-05-28 15:06:02 -07001282
1283 return _glcpp_parser_expand_function (parser, node, last);
1284}
1285
Carl Worth22b3ace2010-06-02 15:32:03 -07001286/* Push a new identifier onto the active list, returning the new list.
1287 *
1288 * Here, 'marker' is the token node that appears in the list after the
1289 * expansion of 'identifier'. That is, when the list iterator begins
1290 * examinging 'marker', then it is time to pop this node from the
1291 * active stack.
1292 */
1293active_list_t *
1294_active_list_push (active_list_t *list,
1295 const char *identifier,
1296 token_node_t *marker)
1297{
1298 active_list_t *node;
1299
1300 node = xtalloc (list, active_list_t);
1301 node->identifier = xtalloc_strdup (node, identifier);
1302 node->marker = marker;
1303 node->next = list;
1304
1305 return node;
1306}
1307
1308active_list_t *
1309_active_list_pop (active_list_t *list)
1310{
1311 active_list_t *node = list;
1312
1313 if (node == NULL)
1314 return NULL;
1315
1316 node = list->next;
1317 talloc_free (list);
1318
1319 return node;
1320}
1321
1322int
1323_active_list_contains (active_list_t *list, const char *identifier)
1324{
1325 active_list_t *node;
1326
1327 if (list == NULL)
1328 return 0;
1329
1330 for (node = list; node; node = node->next)
1331 if (strcmp (node->identifier, identifier) == 0)
1332 return 1;
1333
1334 return 0;
1335}
1336
Carl Worth681afbc2010-05-28 15:06:02 -07001337/* Walk over the token list replacing nodes with their expansion.
1338 * Whenever nodes are expanded the walking will walk over the new
1339 * nodes, continuing to expand as necessary. The results are placed in
1340 * 'list' itself;
1341 */
1342static void
1343_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1344 token_list_t *list)
1345{
1346 token_node_t *node_prev;
1347 token_node_t *node, *last;
1348 token_list_t *expansion;
1349
1350 if (list == NULL)
1351 return;
1352
1353 _token_list_trim_trailing_space (list);
1354
1355 node_prev = NULL;
1356 node = list->head;
1357
1358 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001359
1360 while (parser->active && parser->active->marker == node)
1361 parser->active = _active_list_pop (parser->active);
1362
Carl Worth681afbc2010-05-28 15:06:02 -07001363 /* Find the expansion for node, which will replace all
1364 * nodes from node to last, inclusive. */
1365 expansion = _glcpp_parser_expand_node (parser, node, &last);
1366 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001367 token_node_t *n;
1368
1369 for (n = node; n != last->next; n = n->next)
1370 while (parser->active &&
1371 parser->active->marker == n)
1372 {
1373 parser->active = _active_list_pop (parser->active);
1374 }
1375
1376 parser->active = _active_list_push (parser->active,
1377 node->token->value.str,
1378 last->next);
1379
Carl Worth681afbc2010-05-28 15:06:02 -07001380 /* Splice expansion into list, supporting a
1381 * simple deletion if the expansion is
1382 * empty. */
1383 if (expansion->head) {
1384 if (node_prev)
1385 node_prev->next = expansion->head;
1386 else
1387 list->head = expansion->head;
1388 expansion->tail->next = last->next;
1389 if (last == list->tail)
1390 list->tail = expansion->tail;
1391 } else {
1392 if (node_prev)
1393 node_prev->next = last->next;
1394 else
1395 list->head = last->next;
1396 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001397 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001398 }
1399 } else {
1400 node_prev = node;
1401 }
1402 node = node_prev ? node_prev->next : list->head;
1403 }
1404
Carl Worth22b3ace2010-06-02 15:32:03 -07001405 while (parser->active)
1406 parser->active = _active_list_pop (parser->active);
1407
Carl Worth681afbc2010-05-28 15:06:02 -07001408 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001409}
1410
Carl Worthae6517f2010-05-25 15:24:59 -07001411void
1412_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1413 token_list_t *list)
1414{
Carl Worthae6517f2010-05-25 15:24:59 -07001415 if (list == NULL)
1416 return;
1417
Carl Worth681afbc2010-05-28 15:06:02 -07001418 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001419
Carl Worth681afbc2010-05-28 15:06:02 -07001420 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001421
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001422 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001423}
1424
1425void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001426_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1427 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001428{
1429 /* According to the GLSL specification, macro names starting with "__"
1430 * or "GL_" are reserved for future use. So, don't allow them.
1431 */
1432 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001433 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001434 }
1435 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001436 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001437 }
1438}
1439
1440void
Carl Worthfcbbb462010-05-13 09:36:23 -07001441_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001442 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001443 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001444 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001445{
1446 macro_t *macro;
1447
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001448 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001449
Carl Worthfcbbb462010-05-13 09:36:23 -07001450 macro = xtalloc (parser, macro_t);
1451
1452 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001453 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001454 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001455 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001456
1457 hash_table_insert (parser->defines, macro, identifier);
1458}
1459
1460void
1461_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001462 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001463 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001464 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001465 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001466{
1467 macro_t *macro;
1468
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001469 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001470
Carl Worthfcbbb462010-05-13 09:36:23 -07001471 macro = xtalloc (parser, macro_t);
1472
1473 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001474 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001475 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001476 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001477
1478 hash_table_insert (parser->defines, macro, identifier);
1479}
1480
Carl Worth8f38aff2010-05-19 10:01:29 -07001481static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001482glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001483{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001484 token_node_t *node;
1485 int ret;
1486
Carl Worth95951ea2010-05-26 15:57:10 -07001487 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001488 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001489
1490 /* XXX: This ugly block of code exists for the sole
1491 * purpose of converting a NEWLINE token into a SPACE
1492 * token, but only in the case where we have seen a
1493 * function-like macro name, but have not yet seen its
1494 * closing parenthesis.
1495 *
1496 * There's perhaps a more compact way to do this with
1497 * mid-rule actions in the grammar.
1498 *
1499 * I'm definitely not pleased with the complexity of
1500 * this code here.
1501 */
1502 if (parser->newline_as_space)
1503 {
1504 if (ret == '(') {
1505 parser->paren_count++;
1506 } else if (ret == ')') {
1507 parser->paren_count--;
1508 if (parser->paren_count == 0)
1509 parser->newline_as_space = 0;
1510 } else if (ret == NEWLINE) {
1511 ret = SPACE;
1512 } else if (ret != SPACE) {
1513 if (parser->paren_count == 0)
1514 parser->newline_as_space = 0;
1515 }
1516 }
1517 else if (parser->in_control_line)
1518 {
1519 if (ret == NEWLINE)
1520 parser->in_control_line = 0;
1521 }
1522 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1523 ret == HASH_UNDEF || ret == HASH_IF ||
1524 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1525 ret == HASH_ELIF || ret == HASH_ELSE ||
1526 ret == HASH_ENDIF || ret == HASH)
1527 {
1528 parser->in_control_line = 1;
1529 }
1530 else if (ret == IDENTIFIER)
1531 {
1532 macro_t *macro;
1533 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001534 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001535 if (macro && macro->is_function) {
1536 parser->newline_as_space = 1;
1537 parser->paren_count = 0;
1538 }
1539 }
1540
1541 return ret;
1542 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001543
1544 node = parser->lex_from_node;
1545
1546 if (node == NULL) {
1547 talloc_free (parser->lex_from_list);
1548 parser->lex_from_list = NULL;
1549 return NEWLINE;
1550 }
1551
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001552 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001553 ret = node->token->type;
1554
1555 parser->lex_from_node = node->next;
1556
1557 return ret;
1558}
1559
1560static void
1561glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1562{
1563 token_node_t *node;
1564
1565 assert (parser->lex_from_list == NULL);
1566
1567 /* Copy list, eliminating any space tokens. */
1568 parser->lex_from_list = _token_list_create (parser);
1569
1570 for (node = list->head; node; node = node->next) {
1571 if (node->token->type == SPACE)
1572 continue;
1573 _token_list_append (parser->lex_from_list, node->token);
1574 }
1575
1576 talloc_free (list);
1577
1578 parser->lex_from_node = parser->lex_from_list->head;
1579
1580 /* It's possible the list consisted of nothing but whitespace. */
1581 if (parser->lex_from_node == NULL) {
1582 talloc_free (parser->lex_from_list);
1583 parser->lex_from_list = NULL;
1584 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001585}
Carl Worthb20d33c2010-05-20 22:27:07 -07001586
1587static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001588_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1589 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001590{
1591 skip_type_t current = SKIP_NO_SKIP;
1592 skip_node_t *node;
1593
1594 if (parser->skip_stack)
1595 current = parser->skip_stack->type;
1596
1597 node = xtalloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001598 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001599
1600 if (current == SKIP_NO_SKIP) {
1601 if (condition)
1602 node->type = SKIP_NO_SKIP;
1603 else
1604 node->type = SKIP_TO_ELSE;
1605 } else {
1606 node->type = SKIP_TO_ENDIF;
1607 }
1608
1609 node->next = parser->skip_stack;
1610 parser->skip_stack = node;
1611}
1612
1613static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001614_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1615 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001616{
1617 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001618 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001619 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001620 }
1621
1622 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1623 if (condition)
1624 parser->skip_stack->type = SKIP_NO_SKIP;
1625 } else {
1626 parser->skip_stack->type = SKIP_TO_ENDIF;
1627 }
1628}
Carl Worth80dc60b2010-05-25 14:42:00 -07001629
Carl Worthb20d33c2010-05-20 22:27:07 -07001630static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001631_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001632{
1633 skip_node_t *node;
1634
1635 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001636 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001637 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001638 }
1639
1640 node = parser->skip_stack;
1641 parser->skip_stack = node->next;
1642 talloc_free (node);
1643}