blob: 035a5c83f0ee7933dffa8e7e75423cf8f04935c2 [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 Worth8e82fcb2010-05-26 11:15:21 -0700114_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
115 token_list_t *list);
116
117static void
Carl Worth681afbc2010-05-28 15:06:02 -0700118_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
119 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700120
Carl Worthaaa9acb2010-05-19 13:28:24 -0700121static void
Carl Worth681afbc2010-05-28 15:06:02 -0700122_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
123 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700124
125static void
Kenneth Graunke07745232010-06-17 12:41:46 -0700126_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
127 int condition);
Carl Worthb20d33c2010-05-20 22:27:07 -0700128
129static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700130_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
131 const char *type, int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700132
Carl Worthb20d33c2010-05-20 22:27:07 -0700133static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700134_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
Carl Worthb20d33c2010-05-20 22:27:07 -0700135
Carl Worth0293b2e2010-05-19 10:05:40 -0700136#define yylex glcpp_parser_lex
137
Carl Worth8f38aff2010-05-19 10:01:29 -0700138static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700139glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700140
Carl Worth8e82fcb2010-05-26 11:15:21 -0700141static void
142glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
143
Carl Worth3a37b872010-05-10 11:44:09 -0700144%}
145
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700146%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700147%error-verbose
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700148%locations
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700149
Carl Worth0b27b5f2010-05-10 16:16:06 -0700150%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700151%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700152
Carl Worth050e3de2010-05-27 14:36:29 -0700153%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 -0700154%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700155%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700156%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700157%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700158%type <token> preprocessing_token
159%type <token_list> pp_tokens replacement_list text_line
Carl Worth8fed1cd2010-05-26 09:32:12 -0700160%left OR
161%left AND
162%left '|'
163%left '^'
164%left '&'
165%left EQUAL NOT_EQUAL
166%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
167%left LEFT_SHIFT RIGHT_SHIFT
168%left '+' '-'
169%left '*' '/' '%'
170%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700171
172%%
173
Carl Worth33cc4002010-05-12 12:17:10 -0700174input:
Carl Worth3ff81672010-05-25 13:09:03 -0700175 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700176| input line
177;
178
179line:
180 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700181 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700182 }
Carl Worth808401f2010-05-25 14:52:43 -0700183| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700184 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700185 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700186 talloc_free ($1);
187 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700188| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700189| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700190;
191
Carl Worth8e82fcb2010-05-26 11:15:21 -0700192expanded_line:
193 IF_EXPANDED expression NEWLINE {
Kenneth Graunke07745232010-06-17 12:41:46 -0700194 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700195 }
196| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700197 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700198 }
199;
200
Carl Worth3ff81672010-05-25 13:09:03 -0700201control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700202 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700203 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700204 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700205| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700206 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700207 }
208| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700209 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700210 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700211| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700212 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700213 if (macro) {
214 /* XXX: Need hash table to support a real way
215 * to remove an element rather than prefixing
216 * a new node with data of NULL like this. */
217 hash_table_insert (parser->defines, NULL, $2);
218 talloc_free (macro);
219 }
220 talloc_free ($2);
221 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700222| HASH_IF pp_tokens NEWLINE {
223 token_list_t *expanded;
224 token_t *token;
225
226 expanded = _token_list_create (parser);
227 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
228 _token_list_append (expanded, token);
229 talloc_unlink (parser, token);
230 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700231 _glcpp_parser_expand_token_list (parser, $2);
232 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700233 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700234 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700235| HASH_IFDEF 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 Graunke65875742010-06-18 20:08:15 -0700240| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700241 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700242 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700243 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700244 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700245| HASH_ELIF pp_tokens NEWLINE {
246 token_list_t *expanded;
247 token_t *token;
248
249 expanded = _token_list_create (parser);
250 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
251 _token_list_append (expanded, token);
252 talloc_unlink (parser, token);
253 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700254 _glcpp_parser_expand_token_list (parser, $2);
255 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700256 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700257 }
258| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700259 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700260 }
261| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700262 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700263 }
Carl Worth3ff81672010-05-25 13:09:03 -0700264| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700265;
266
Carl Worth8fed1cd2010-05-26 09:32:12 -0700267expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700268 INTEGER_STRING {
269 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
270 $$ = strtoll ($1 + 2, NULL, 16);
271 } else if ($1[0] == '0') {
272 $$ = strtoll ($1, NULL, 8);
273 } else {
274 $$ = strtoll ($1, NULL, 10);
275 }
276 }
277| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700278 $$ = $1;
279 }
280| expression OR expression {
281 $$ = $1 || $3;
282 }
283| expression AND expression {
284 $$ = $1 && $3;
285 }
286| expression '|' expression {
287 $$ = $1 | $3;
288 }
289| expression '^' expression {
290 $$ = $1 ^ $3;
291 }
292| expression '&' expression {
293 $$ = $1 & $3;
294 }
295| expression NOT_EQUAL expression {
296 $$ = $1 != $3;
297 }
298| expression EQUAL expression {
299 $$ = $1 == $3;
300 }
301| expression GREATER_OR_EQUAL expression {
302 $$ = $1 >= $3;
303 }
304| expression LESS_OR_EQUAL expression {
305 $$ = $1 <= $3;
306 }
307| expression '>' expression {
308 $$ = $1 > $3;
309 }
310| expression '<' expression {
311 $$ = $1 < $3;
312 }
313| expression RIGHT_SHIFT expression {
314 $$ = $1 >> $3;
315 }
316| expression LEFT_SHIFT expression {
317 $$ = $1 << $3;
318 }
319| expression '-' expression {
320 $$ = $1 - $3;
321 }
322| expression '+' expression {
323 $$ = $1 + $3;
324 }
325| expression '%' expression {
326 $$ = $1 % $3;
327 }
328| expression '/' expression {
329 $$ = $1 / $3;
330 }
331| expression '*' expression {
332 $$ = $1 * $3;
333 }
334| '!' expression %prec UNARY {
335 $$ = ! $2;
336 }
337| '~' expression %prec UNARY {
338 $$ = ~ $2;
339 }
340| '-' expression %prec UNARY {
341 $$ = - $2;
342 }
343| '+' expression %prec UNARY {
344 $$ = + $2;
345 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700346| '(' expression ')' {
347 $$ = $2;
348 }
349;
350
Carl Worth3ff81672010-05-25 13:09:03 -0700351identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700352 IDENTIFIER {
353 $$ = _string_list_create (parser);
354 _string_list_append_item ($$, $1);
355 talloc_steal ($$, $1);
356 }
357| identifier_list ',' IDENTIFIER {
358 $$ = $1;
359 _string_list_append_item ($$, $3);
360 talloc_steal ($$, $3);
361 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700362;
363
Carl Worth3ff81672010-05-25 13:09:03 -0700364text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700365 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700366| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700367;
368
Carl Worth3ff81672010-05-25 13:09:03 -0700369non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700370 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700371 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700372 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700373;
374
Carl Worthaaa9acb2010-05-19 13:28:24 -0700375replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700376 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700377| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700378;
379
Kenneth Graunke65875742010-06-18 20:08:15 -0700380junk:
381 /* empty */
382| pp_tokens {
383 glcpp_warning(&@1, parser, "extra tokens at end of directive");
384 }
385
Carl Worthaaa9acb2010-05-19 13:28:24 -0700386pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700387 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700388 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700389 $$ = _token_list_create (parser);
390 _token_list_append ($$, $1);
391 talloc_unlink (parser, $1);
392 }
393| pp_tokens preprocessing_token {
394 $$ = $1;
395 _token_list_append ($$, $2);
396 talloc_unlink (parser, $2);
397 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700398;
399
Carl Worth3ff81672010-05-25 13:09:03 -0700400preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700401 IDENTIFIER {
402 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700403 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700404 }
Carl Worth050e3de2010-05-27 14:36:29 -0700405| INTEGER_STRING {
406 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700407 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700408 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700409| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700410 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700411 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700412 }
413| OTHER {
414 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700415 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700416 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700417| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700418 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700419 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700420 }
Carl Worth3ff81672010-05-25 13:09:03 -0700421;
422
Carl Worth8e82fcb2010-05-26 11:15:21 -0700423operator:
Carl Worth808401f2010-05-25 14:52:43 -0700424 '[' { $$ = '['; }
425| ']' { $$ = ']'; }
426| '(' { $$ = '('; }
427| ')' { $$ = ')'; }
428| '{' { $$ = '{'; }
429| '}' { $$ = '}'; }
430| '.' { $$ = '.'; }
431| '&' { $$ = '&'; }
432| '*' { $$ = '*'; }
433| '+' { $$ = '+'; }
434| '-' { $$ = '-'; }
435| '~' { $$ = '~'; }
436| '!' { $$ = '!'; }
437| '/' { $$ = '/'; }
438| '%' { $$ = '%'; }
439| LEFT_SHIFT { $$ = LEFT_SHIFT; }
440| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
441| '<' { $$ = '<'; }
442| '>' { $$ = '>'; }
443| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
444| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
445| EQUAL { $$ = EQUAL; }
446| NOT_EQUAL { $$ = NOT_EQUAL; }
447| '^' { $$ = '^'; }
448| '|' { $$ = '|'; }
449| AND { $$ = AND; }
450| OR { $$ = OR; }
451| ';' { $$ = ';'; }
452| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700453| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700454| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700455| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700456;
457
Carl Worth33cc4002010-05-12 12:17:10 -0700458%%
459
Carl Worth610053b2010-05-14 10:05:11 -0700460string_list_t *
461_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700462{
Carl Worth610053b2010-05-14 10:05:11 -0700463 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700464
Carl Worth610053b2010-05-14 10:05:11 -0700465 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700466 list->head = NULL;
467 list->tail = NULL;
468
469 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700470}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700471
Carl Worth33cc4002010-05-12 12:17:10 -0700472void
Carl Worth610053b2010-05-14 10:05:11 -0700473_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700474{
475 if (list->head == NULL) {
476 list->head = tail->head;
477 } else {
478 list->tail->next = tail->head;
479 }
480
481 list->tail = tail->tail;
482}
483
484void
Carl Worth610053b2010-05-14 10:05:11 -0700485_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700486{
Carl Worth610053b2010-05-14 10:05:11 -0700487 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700488
Carl Worth610053b2010-05-14 10:05:11 -0700489 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700490 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700491
Carl Worth33cc4002010-05-12 12:17:10 -0700492 node->next = NULL;
493
494 if (list->head == NULL) {
495 list->head = node;
496 } else {
497 list->tail->next = node;
498 }
499
500 list->tail = node;
501}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700502
503int
Carl Worth610053b2010-05-14 10:05:11 -0700504_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700505{
Carl Worth610053b2010-05-14 10:05:11 -0700506 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700507 int i;
508
509 if (list == NULL)
510 return 0;
511
512 for (i = 0, node = list->head; node; i++, node = node->next) {
513 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700514 if (index)
515 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700516 return 1;
517 }
518 }
519
520 return 0;
521}
522
523int
Carl Worth610053b2010-05-14 10:05:11 -0700524_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700525{
526 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700527 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700528
529 if (list == NULL)
530 return 0;
531
532 for (node = list->head; node; node = node->next)
533 length++;
534
535 return length;
536}
537
Carl Worth8f6a8282010-05-14 10:44:19 -0700538argument_list_t *
539_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700540{
Carl Worth8f6a8282010-05-14 10:44:19 -0700541 argument_list_t *list;
542
543 list = xtalloc (ctx, argument_list_t);
544 list->head = NULL;
545 list->tail = NULL;
546
547 return list;
548}
549
550void
Carl Worth47252442010-05-19 13:54:37 -0700551_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700552{
553 argument_node_t *node;
554
Carl Worth8f6a8282010-05-14 10:44:19 -0700555 node = xtalloc (list, argument_node_t);
556 node->argument = argument;
557
558 node->next = NULL;
559
560 if (list->head == NULL) {
561 list->head = node;
562 } else {
563 list->tail->next = node;
564 }
565
566 list->tail = node;
567}
568
569int
570_argument_list_length (argument_list_t *list)
571{
572 int length = 0;
573 argument_node_t *node;
574
575 if (list == NULL)
576 return 0;
577
578 for (node = list->head; node; node = node->next)
579 length++;
580
581 return length;
582}
583
Carl Worth47252442010-05-19 13:54:37 -0700584token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700585_argument_list_member_at (argument_list_t *list, int index)
586{
587 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700588 int i;
589
590 if (list == NULL)
591 return NULL;
592
593 node = list->head;
594 for (i = 0; i < index; i++) {
595 node = node->next;
596 if (node == NULL)
597 break;
598 }
599
600 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700601 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700602
603 return NULL;
604}
Carl Worth47252442010-05-19 13:54:37 -0700605
Carl Worth808401f2010-05-25 14:52:43 -0700606/* Note: This function talloc_steal()s the str pointer. */
607token_t *
608_token_create_str (void *ctx, int type, char *str)
609{
610 token_t *token;
611
612 token = xtalloc (ctx, token_t);
613 token->type = type;
614 token->value.str = talloc_steal (token, str);
615
616 return token;
617}
618
619token_t *
620_token_create_ival (void *ctx, int type, int ival)
621{
622 token_t *token;
623
624 token = xtalloc (ctx, token_t);
625 token->type = type;
626 token->value.ival = ival;
627
628 return token;
629}
630
Carl Worth47252442010-05-19 13:54:37 -0700631token_list_t *
632_token_list_create (void *ctx)
633{
634 token_list_t *list;
635
636 list = xtalloc (ctx, token_list_t);
637 list->head = NULL;
638 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700639 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700640
641 return list;
642}
643
644void
Carl Worth808401f2010-05-25 14:52:43 -0700645_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700646{
647 token_node_t *node;
648
649 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700650 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700651
652 node->next = NULL;
653
654 if (list->head == NULL) {
655 list->head = node;
656 } else {
657 list->tail->next = node;
658 }
659
660 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700661 if (token->type != SPACE)
662 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700663}
664
665void
666_token_list_append_list (token_list_t *list, token_list_t *tail)
667{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700668 if (tail == NULL || tail->head == NULL)
669 return;
670
Carl Worth47252442010-05-19 13:54:37 -0700671 if (list->head == NULL) {
672 list->head = tail->head;
673 } else {
674 list->tail->next = tail->head;
675 }
676
677 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700678 list->non_space_tail = tail->non_space_tail;
679}
680
Carl Worth681afbc2010-05-28 15:06:02 -0700681token_list_t *
682_token_list_copy (void *ctx, token_list_t *other)
683{
684 token_list_t *copy;
685 token_node_t *node;
686
687 if (other == NULL)
688 return NULL;
689
690 copy = _token_list_create (ctx);
691 for (node = other->head; node; node = node->next)
692 _token_list_append (copy, node->token);
693
694 return copy;
695}
696
Carl Worth10ae4382010-05-25 20:35:01 -0700697void
698_token_list_trim_trailing_space (token_list_t *list)
699{
700 token_node_t *tail, *next;
701
702 if (list->non_space_tail) {
703 tail = list->non_space_tail->next;
704 list->non_space_tail->next = NULL;
705 list->tail = list->non_space_tail;
706
707 while (tail) {
708 next = tail->next;
709 talloc_free (tail);
710 tail = next;
711 }
712 }
Carl Worth47252442010-05-19 13:54:37 -0700713}
Carl Worth80dc60b2010-05-25 14:42:00 -0700714
Carl Worth22b3ace2010-06-02 15:32:03 -0700715static int
716_token_list_length (token_list_t *list)
717{
718 int length = 0;
719 token_node_t *node;
720
721 if (list == NULL)
722 return 0;
723
724 for (node = list->head; node; node = node->next)
725 length++;
726
727 return length;
728}
729
Carl Worth0197e9b2010-05-26 08:05:19 -0700730static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700731_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700732{
733 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700734 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700735 return;
736 }
737
738 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700739 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700740 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700741 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700742 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700743 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700744 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700745 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700746 break;
747 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700748 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700749 break;
750 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700751 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700752 break;
753 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700754 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700755 break;
756 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700757 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700758 break;
759 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700760 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700761 break;
762 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700763 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700764 break;
765 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700766 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700767 break;
768 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700769 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700770 break;
771 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700772 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700773 break;
774 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700775 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700776 break;
Carl Worthdd749002010-05-27 10:12:33 -0700777 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700778 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700779 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700780 case PLACEHOLDER:
781 /* Nothing to print. */
782 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700783 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700784 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700785 break;
786 }
787}
788
Carl Worthb06096e2010-05-29 05:54:19 -0700789/* Return a new token (talloc()ed off of 'token') formed by pasting
790 * 'token' and 'other'. Note that this function may return 'token' or
791 * 'other' directly rather than allocating anything new.
792 *
793 * Caution: Only very cursory error-checking is performed to see if
794 * the final result is a valid single token. */
795static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700796_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700797{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700798 token_t *combined = NULL;
799
Carl Worth85b50e82010-05-27 14:01:18 -0700800 /* Pasting a placeholder onto anything makes no change. */
801 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700802 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700803
Carl Worthb06096e2010-05-29 05:54:19 -0700804 /* When 'token' is a placeholder, just return 'other'. */
805 if (token->type == PLACEHOLDER)
806 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700807
Carl Worthad0dee62010-05-26 09:04:50 -0700808 /* A very few single-character punctuators can be combined
809 * with another to form a multi-character punctuator. */
810 switch (token->type) {
811 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700812 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700813 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700814 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700815 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700816 break;
817 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700818 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700819 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700820 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700821 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700822 break;
823 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700824 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700825 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700826 break;
827 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700828 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700829 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700830 break;
831 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700832 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700833 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700834 break;
835 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700836 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700837 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700838 break;
839 }
840
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700841 if (combined != NULL) {
842 /* Inherit the location from the first token */
843 combined->location = token->location;
844 return combined;
845 }
846
Carl Worthad0dee62010-05-26 09:04:50 -0700847 /* Two string-valued tokens can usually just be mashed
848 * together.
849 *
Carl Worth050e3de2010-05-27 14:36:29 -0700850 * XXX: This isn't actually legitimate. Several things here
851 * should result in a diagnostic since the result cannot be a
852 * valid, single pre-processing token. For example, pasting
853 * "123" and "abc" is not legal, but we don't catch that
854 * here. */
855 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
856 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700857 {
Carl Worthb06096e2010-05-29 05:54:19 -0700858 char *str;
859
860 str = xtalloc_asprintf (token, "%s%s",
861 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700862 combined = _token_create_str (token, token->type, str);
863 combined->location = token->location;
864 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700865 }
866
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700867 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700868 glcpp_print (parser->info_log, "Pasting \"");
869 _token_print (&parser->info_log, token);
870 glcpp_print (parser->info_log, "\" and \"");
871 _token_print (&parser->info_log, other);
872 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700873
874 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700875}
876
Carl Worth0197e9b2010-05-26 08:05:19 -0700877static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700878_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700879{
880 token_node_t *node;
881
882 if (list == NULL)
883 return;
884
885 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700886 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700887}
888
Carl Worth3a37b872010-05-10 11:44:09 -0700889void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700890yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700891{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700892 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700893}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700894
Carl Worth33cc4002010-05-12 12:17:10 -0700895glcpp_parser_t *
896glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700897{
Carl Worth33cc4002010-05-12 12:17:10 -0700898 glcpp_parser_t *parser;
899
Carl Worth5070a202010-05-12 12:45:33 -0700900 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700901
Carl Worth8f38aff2010-05-19 10:01:29 -0700902 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700903 parser->defines = hash_table_ctor (32, hash_table_string_hash,
904 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700905 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700906 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700907 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700908 parser->newline_as_space = 0;
909 parser->in_control_line = 0;
910 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700911
Carl Worthb20d33c2010-05-20 22:27:07 -0700912 parser->skip_stack = NULL;
913
Carl Worth8e82fcb2010-05-26 11:15:21 -0700914 parser->lex_from_list = NULL;
915 parser->lex_from_node = NULL;
916
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700917 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700918 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700919
Carl Worth33cc4002010-05-12 12:17:10 -0700920 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700921}
922
923int
924glcpp_parser_parse (glcpp_parser_t *parser)
925{
926 return yyparse (parser);
927}
928
929void
Carl Worth33cc4002010-05-12 12:17:10 -0700930glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700931{
Carl Worthb20d33c2010-05-20 22:27:07 -0700932 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700933 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700934 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700935 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700936 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700937}
Carl Worthc6d5af32010-05-11 12:30:09 -0700938
Carl Worth8e82fcb2010-05-26 11:15:21 -0700939/* Replace any occurences of DEFINED tokens in 'list' with either a
940 * '0' or '1' INTEGER token depending on whether the next token in the
941 * list is defined or not. */
942static void
943_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
944 token_list_t *list)
945{
946 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700947 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700948
949 if (list == NULL)
950 return;
951
952 for (node = list->head; node; node = node->next) {
953 if (node->token->type != DEFINED)
954 continue;
955 next = node->next;
956 while (next && next->token->type == SPACE)
957 next = next->next;
958 if (next == NULL || next->token->type != IDENTIFIER) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700959 yyerror (&node->token->location, parser, "operator \"defined\" requires an identifier\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -0700960 /* Already flagged an error; fake it. */
961 node->token->type = INTEGER;
962 node->token->value.ival = 0;
963 return;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700964 }
965 macro = hash_table_find (parser->defines,
966 next->token->value.str);
967
968 node->token->type = INTEGER;
969 node->token->value.ival = (macro != NULL);
970 node->next = next->next;
971 }
972}
973
Carl Worthb1854fd2010-05-25 16:28:26 -0700974typedef enum function_status
975{
976 FUNCTION_STATUS_SUCCESS,
977 FUNCTION_NOT_A_FUNCTION,
978 FUNCTION_UNBALANCED_PARENTHESES
979} function_status_t;
980
981/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700982 * balanced set of parentheses.
983 *
984 * When called, 'node' should be the opening-parenthesis token, (or
985 * perhaps preceeding SPACE tokens). Upon successful return *last will
986 * be the last consumed node, (corresponding to the closing right
987 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700988 *
989 * Return values:
990 *
991 * FUNCTION_STATUS_SUCCESS:
992 *
993 * Successfully parsed a set of function arguments.
994 *
995 * FUNCTION_NOT_A_FUNCTION:
996 *
997 * Macro name not followed by a '('. This is not an error, but
998 * simply that the macro name should be treated as a non-macro.
999 *
Carl Worth14c98a52010-06-02 15:49:54 -07001000 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -07001001 *
1002 * Macro name is not followed by a balanced set of parentheses.
1003 */
1004static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -07001005_arguments_parse (argument_list_t *arguments,
1006 token_node_t *node,
1007 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001008{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001009 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001010 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001011
Carl Worthb1854fd2010-05-25 16:28:26 -07001012 node = node->next;
1013
1014 /* Ignore whitespace before first parenthesis. */
1015 while (node && node->token->type == SPACE)
1016 node = node->next;
1017
1018 if (node == NULL || node->token->type != '(')
1019 return FUNCTION_NOT_A_FUNCTION;
1020
Carl Worth652fa272010-05-25 17:45:22 -07001021 node = node->next;
1022
Carl Wortha19297b2010-05-27 13:29:19 -07001023 argument = _token_list_create (arguments);
1024 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001025
Carl Worth681afbc2010-05-28 15:06:02 -07001026 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001027 if (node->token->type == '(')
1028 {
1029 paren_count++;
1030 }
1031 else if (node->token->type == ')')
1032 {
1033 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001034 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001035 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001036 }
Carl Worth652fa272010-05-25 17:45:22 -07001037
1038 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001039 paren_count == 1)
1040 {
Carl Wortha19297b2010-05-27 13:29:19 -07001041 _token_list_trim_trailing_space (argument);
1042 argument = _token_list_create (arguments);
1043 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001044 }
1045 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001046 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001047 /* Don't treat initial whitespace as
1048 * part of the arguement. */
1049 if (node->token->type == SPACE)
1050 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001051 }
1052 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001053 }
Carl Worthc7581c22010-05-25 17:41:07 -07001054 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001055
Carl Worth681afbc2010-05-28 15:06:02 -07001056 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001057 return FUNCTION_UNBALANCED_PARENTHESES;
1058
Carl Worth681afbc2010-05-28 15:06:02 -07001059 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001060
1061 return FUNCTION_STATUS_SUCCESS;
1062}
1063
Carl Worth681afbc2010-05-28 15:06:02 -07001064/* This is a helper function that's essentially part of the
1065 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1066 * except for by that function.
1067 *
1068 * Returns NULL if node is a simple token with no expansion, (that is,
1069 * although 'node' corresponds to an identifier defined as a
1070 * function-like macro, it is not followed with a parenthesized
1071 * argument list).
1072 *
1073 * Compute the complete expansion of node (which is a function-like
1074 * macro) and subsequent nodes which are arguments.
1075 *
1076 * Returns the token list that results from the expansion and sets
1077 * *last to the last node in the list that was consumed by the
1078 * expansion. Specificallty, *last will be set as follows: as the
1079 * token of the closing right parenthesis.
1080 */
1081static token_list_t *
1082_glcpp_parser_expand_function (glcpp_parser_t *parser,
1083 token_node_t *node,
1084 token_node_t **last)
1085
Carl Worthb1854fd2010-05-25 16:28:26 -07001086{
1087 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001088 const char *identifier;
1089 argument_list_t *arguments;
1090 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001091 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001092 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001093
Carl Worthb1854fd2010-05-25 16:28:26 -07001094 identifier = node->token->value.str;
1095
1096 macro = hash_table_find (parser->defines, identifier);
1097
1098 assert (macro->is_function);
1099
Carl Worth9ce18cf2010-05-25 17:32:21 -07001100 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001101 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001102
1103 switch (status) {
1104 case FUNCTION_STATUS_SUCCESS:
1105 break;
1106 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001107 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001108 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001109 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001110 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001111 }
1112
Carl Worth9ce18cf2010-05-25 17:32:21 -07001113 if (macro->replacements == NULL) {
1114 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001115 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001116 }
1117
Carl Wortha19297b2010-05-27 13:29:19 -07001118 if (! ((_argument_list_length (arguments) ==
1119 _string_list_length (macro->parameters)) ||
1120 (_string_list_length (macro->parameters) == 0 &&
1121 _argument_list_length (arguments) == 1 &&
1122 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001123 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001124 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001125 "Error: macro %s invoked with %d arguments (expected %d)\n",
1126 identifier,
1127 _argument_list_length (arguments),
1128 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001129 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001130 }
1131
Carl Worth0197e9b2010-05-26 08:05:19 -07001132 /* Perform argument substitution on the replacement list. */
1133 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001134
Carl Worthce540f22010-05-26 08:25:44 -07001135 for (node = macro->replacements->head; node; node = node->next)
1136 {
1137 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001138 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001139 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001140 &parameter_index))
1141 {
1142 token_list_t *argument;
1143 argument = _argument_list_member_at (arguments,
1144 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001145 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001146 * tokens, or append a placeholder token for
1147 * an empty argument. */
1148 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001149 _glcpp_parser_expand_token_list (parser,
1150 argument);
1151 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001152 } else {
1153 token_t *new_token;
1154
1155 new_token = _token_create_ival (substituted,
1156 PLACEHOLDER,
1157 PLACEHOLDER);
1158 _token_list_append (substituted, new_token);
1159 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001160 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001161 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001162 }
1163 }
1164
Carl Worthad0dee62010-05-26 09:04:50 -07001165 /* After argument substitution, and before further expansion
1166 * below, implement token pasting. */
1167
Carl Worthb06096e2010-05-29 05:54:19 -07001168 _token_list_trim_trailing_space (substituted);
1169
Carl Worthad0dee62010-05-26 09:04:50 -07001170 node = substituted->head;
1171 while (node)
1172 {
1173 token_node_t *next_non_space;
1174
1175 /* Look ahead for a PASTE token, skipping space. */
1176 next_non_space = node->next;
1177 while (next_non_space && next_non_space->token->type == SPACE)
1178 next_non_space = next_non_space->next;
1179
1180 if (next_non_space == NULL)
1181 break;
1182
1183 if (next_non_space->token->type != PASTE) {
1184 node = next_non_space;
1185 continue;
1186 }
1187
1188 /* Now find the next non-space token after the PASTE. */
1189 next_non_space = next_non_space->next;
1190 while (next_non_space && next_non_space->token->type == SPACE)
1191 next_non_space = next_non_space->next;
1192
1193 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001194 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001195 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001196 }
1197
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001198 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001199 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001200 if (next_non_space == substituted->tail)
1201 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001202
1203 node = node->next;
1204 }
1205
Carl Worthb06096e2010-05-29 05:54:19 -07001206 substituted->non_space_tail = substituted->tail;
1207
Carl Worth681afbc2010-05-28 15:06:02 -07001208 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001209}
1210
Carl Worth681afbc2010-05-28 15:06:02 -07001211/* Compute the complete expansion of node, (and subsequent nodes after
1212 * 'node' in the case that 'node' is a function-like macro and
1213 * subsequent nodes are arguments).
1214 *
1215 * Returns NULL if node is a simple token with no expansion.
1216 *
1217 * Otherwise, returns the token list that results from the expansion
1218 * and sets *last to the last node in the list that was consumed by
1219 * the expansion. Specificallty, *last will be set as follows:
1220 *
1221 * As 'node' in the case of object-like macro expansion.
1222 *
1223 * As the token of the closing right parenthesis in the case of
1224 * function-like macro expansion.
1225 */
1226static token_list_t *
1227_glcpp_parser_expand_node (glcpp_parser_t *parser,
1228 token_node_t *node,
1229 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001230{
Carl Worth681afbc2010-05-28 15:06:02 -07001231 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001232 const char *identifier;
1233 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001234
1235 /* We only expand identifiers */
1236 if (token->type != IDENTIFIER) {
1237 /* We change any COMMA into a COMMA_FINAL to prevent
1238 * it being mistaken for an argument separator
1239 * later. */
1240 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001241 token->type = COMMA_FINAL;
1242 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001243 }
Carl Worth681afbc2010-05-28 15:06:02 -07001244
1245 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001246 }
1247
1248 /* Look up this identifier in the hash table. */
1249 identifier = token->value.str;
1250 macro = hash_table_find (parser->defines, identifier);
1251
Carl Worth681afbc2010-05-28 15:06:02 -07001252 /* Not a macro, so no expansion needed. */
1253 if (macro == NULL)
1254 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001255
1256 /* Finally, don't expand this macro if we're already actively
1257 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001258 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001259 /* We change the token type here from IDENTIFIER to
1260 * OTHER to prevent any future expansion of this
1261 * unexpanded token. */
1262 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001263 token_list_t *expansion;
1264 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001265
Carl Worth681afbc2010-05-28 15:06:02 -07001266 str = xtalloc_strdup (parser, token->value.str);
1267 final = _token_create_str (parser, OTHER, str);
1268 expansion = _token_list_create (parser);
1269 _token_list_append (expansion, final);
1270 *last = node;
1271 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001272 }
1273
Carl Worth681afbc2010-05-28 15:06:02 -07001274 if (! macro->is_function)
1275 {
1276 *last = node;
1277
1278 if (macro->replacements == NULL)
1279 return _token_list_create (parser);
1280
Carl Worth22b3ace2010-06-02 15:32:03 -07001281 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001282 }
Carl Worth681afbc2010-05-28 15:06:02 -07001283
1284 return _glcpp_parser_expand_function (parser, node, last);
1285}
1286
Carl Worth22b3ace2010-06-02 15:32:03 -07001287/* Push a new identifier onto the active list, returning the new list.
1288 *
1289 * Here, 'marker' is the token node that appears in the list after the
1290 * expansion of 'identifier'. That is, when the list iterator begins
1291 * examinging 'marker', then it is time to pop this node from the
1292 * active stack.
1293 */
1294active_list_t *
1295_active_list_push (active_list_t *list,
1296 const char *identifier,
1297 token_node_t *marker)
1298{
1299 active_list_t *node;
1300
1301 node = xtalloc (list, active_list_t);
1302 node->identifier = xtalloc_strdup (node, identifier);
1303 node->marker = marker;
1304 node->next = list;
1305
1306 return node;
1307}
1308
1309active_list_t *
1310_active_list_pop (active_list_t *list)
1311{
1312 active_list_t *node = list;
1313
1314 if (node == NULL)
1315 return NULL;
1316
1317 node = list->next;
1318 talloc_free (list);
1319
1320 return node;
1321}
1322
1323int
1324_active_list_contains (active_list_t *list, const char *identifier)
1325{
1326 active_list_t *node;
1327
1328 if (list == NULL)
1329 return 0;
1330
1331 for (node = list; node; node = node->next)
1332 if (strcmp (node->identifier, identifier) == 0)
1333 return 1;
1334
1335 return 0;
1336}
1337
Carl Worth681afbc2010-05-28 15:06:02 -07001338/* Walk over the token list replacing nodes with their expansion.
1339 * Whenever nodes are expanded the walking will walk over the new
1340 * nodes, continuing to expand as necessary. The results are placed in
1341 * 'list' itself;
1342 */
1343static void
1344_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1345 token_list_t *list)
1346{
1347 token_node_t *node_prev;
1348 token_node_t *node, *last;
1349 token_list_t *expansion;
1350
1351 if (list == NULL)
1352 return;
1353
1354 _token_list_trim_trailing_space (list);
1355
1356 node_prev = NULL;
1357 node = list->head;
1358
1359 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001360
1361 while (parser->active && parser->active->marker == node)
1362 parser->active = _active_list_pop (parser->active);
1363
Carl Worth681afbc2010-05-28 15:06:02 -07001364 /* Find the expansion for node, which will replace all
1365 * nodes from node to last, inclusive. */
1366 expansion = _glcpp_parser_expand_node (parser, node, &last);
1367 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001368 token_node_t *n;
1369
1370 for (n = node; n != last->next; n = n->next)
1371 while (parser->active &&
1372 parser->active->marker == n)
1373 {
1374 parser->active = _active_list_pop (parser->active);
1375 }
1376
1377 parser->active = _active_list_push (parser->active,
1378 node->token->value.str,
1379 last->next);
1380
Carl Worth681afbc2010-05-28 15:06:02 -07001381 /* Splice expansion into list, supporting a
1382 * simple deletion if the expansion is
1383 * empty. */
1384 if (expansion->head) {
1385 if (node_prev)
1386 node_prev->next = expansion->head;
1387 else
1388 list->head = expansion->head;
1389 expansion->tail->next = last->next;
1390 if (last == list->tail)
1391 list->tail = expansion->tail;
1392 } else {
1393 if (node_prev)
1394 node_prev->next = last->next;
1395 else
1396 list->head = last->next;
1397 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001398 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001399 }
1400 } else {
1401 node_prev = node;
1402 }
1403 node = node_prev ? node_prev->next : list->head;
1404 }
1405
Carl Worth22b3ace2010-06-02 15:32:03 -07001406 while (parser->active)
1407 parser->active = _active_list_pop (parser->active);
1408
Carl Worth681afbc2010-05-28 15:06:02 -07001409 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001410}
1411
Carl Worthae6517f2010-05-25 15:24:59 -07001412void
1413_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1414 token_list_t *list)
1415{
Carl Worthae6517f2010-05-25 15:24:59 -07001416 if (list == NULL)
1417 return;
1418
Carl Worth681afbc2010-05-28 15:06:02 -07001419 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001420
Carl Worth681afbc2010-05-28 15:06:02 -07001421 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001422
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001423 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001424}
1425
1426void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001427_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1428 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001429{
1430 /* According to the GLSL specification, macro names starting with "__"
1431 * or "GL_" are reserved for future use. So, don't allow them.
1432 */
1433 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001434 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001435 }
1436 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001437 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001438 }
1439}
1440
1441void
Carl Worthfcbbb462010-05-13 09:36:23 -07001442_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001443 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001444 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001445 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001446{
1447 macro_t *macro;
1448
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001449 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001450
Carl Worthfcbbb462010-05-13 09:36:23 -07001451 macro = xtalloc (parser, macro_t);
1452
1453 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001454 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001455 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001456 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001457
1458 hash_table_insert (parser->defines, macro, identifier);
1459}
1460
1461void
1462_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001463 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001464 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001465 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001466 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001467{
1468 macro_t *macro;
1469
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001470 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001471
Carl Worthfcbbb462010-05-13 09:36:23 -07001472 macro = xtalloc (parser, macro_t);
1473
1474 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001475 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001476 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001477 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001478
1479 hash_table_insert (parser->defines, macro, identifier);
1480}
1481
Carl Worth8f38aff2010-05-19 10:01:29 -07001482static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001483glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001484{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001485 token_node_t *node;
1486 int ret;
1487
Carl Worth95951ea2010-05-26 15:57:10 -07001488 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001489 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001490
1491 /* XXX: This ugly block of code exists for the sole
1492 * purpose of converting a NEWLINE token into a SPACE
1493 * token, but only in the case where we have seen a
1494 * function-like macro name, but have not yet seen its
1495 * closing parenthesis.
1496 *
1497 * There's perhaps a more compact way to do this with
1498 * mid-rule actions in the grammar.
1499 *
1500 * I'm definitely not pleased with the complexity of
1501 * this code here.
1502 */
1503 if (parser->newline_as_space)
1504 {
1505 if (ret == '(') {
1506 parser->paren_count++;
1507 } else if (ret == ')') {
1508 parser->paren_count--;
1509 if (parser->paren_count == 0)
1510 parser->newline_as_space = 0;
1511 } else if (ret == NEWLINE) {
1512 ret = SPACE;
1513 } else if (ret != SPACE) {
1514 if (parser->paren_count == 0)
1515 parser->newline_as_space = 0;
1516 }
1517 }
1518 else if (parser->in_control_line)
1519 {
1520 if (ret == NEWLINE)
1521 parser->in_control_line = 0;
1522 }
1523 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1524 ret == HASH_UNDEF || ret == HASH_IF ||
1525 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1526 ret == HASH_ELIF || ret == HASH_ELSE ||
1527 ret == HASH_ENDIF || ret == HASH)
1528 {
1529 parser->in_control_line = 1;
1530 }
1531 else if (ret == IDENTIFIER)
1532 {
1533 macro_t *macro;
1534 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001535 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001536 if (macro && macro->is_function) {
1537 parser->newline_as_space = 1;
1538 parser->paren_count = 0;
1539 }
1540 }
1541
1542 return ret;
1543 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001544
1545 node = parser->lex_from_node;
1546
1547 if (node == NULL) {
1548 talloc_free (parser->lex_from_list);
1549 parser->lex_from_list = NULL;
1550 return NEWLINE;
1551 }
1552
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001553 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001554 ret = node->token->type;
1555
1556 parser->lex_from_node = node->next;
1557
1558 return ret;
1559}
1560
1561static void
1562glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1563{
1564 token_node_t *node;
1565
1566 assert (parser->lex_from_list == NULL);
1567
1568 /* Copy list, eliminating any space tokens. */
1569 parser->lex_from_list = _token_list_create (parser);
1570
1571 for (node = list->head; node; node = node->next) {
1572 if (node->token->type == SPACE)
1573 continue;
1574 _token_list_append (parser->lex_from_list, node->token);
1575 }
1576
1577 talloc_free (list);
1578
1579 parser->lex_from_node = parser->lex_from_list->head;
1580
1581 /* It's possible the list consisted of nothing but whitespace. */
1582 if (parser->lex_from_node == NULL) {
1583 talloc_free (parser->lex_from_list);
1584 parser->lex_from_list = NULL;
1585 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001586}
Carl Worthb20d33c2010-05-20 22:27:07 -07001587
1588static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001589_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1590 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001591{
1592 skip_type_t current = SKIP_NO_SKIP;
1593 skip_node_t *node;
1594
1595 if (parser->skip_stack)
1596 current = parser->skip_stack->type;
1597
1598 node = xtalloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001599 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001600
1601 if (current == SKIP_NO_SKIP) {
1602 if (condition)
1603 node->type = SKIP_NO_SKIP;
1604 else
1605 node->type = SKIP_TO_ELSE;
1606 } else {
1607 node->type = SKIP_TO_ENDIF;
1608 }
1609
1610 node->next = parser->skip_stack;
1611 parser->skip_stack = node;
1612}
1613
1614static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001615_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1616 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001617{
1618 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001619 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001620 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001621 }
1622
1623 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1624 if (condition)
1625 parser->skip_stack->type = SKIP_NO_SKIP;
1626 } else {
1627 parser->skip_stack->type = SKIP_TO_ENDIF;
1628 }
1629}
Carl Worth80dc60b2010-05-25 14:42:00 -07001630
Carl Worthb20d33c2010-05-20 22:27:07 -07001631static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001632_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001633{
1634 skip_node_t *node;
1635
1636 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001637 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001638 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001639 }
1640
1641 node = parser->skip_stack;
1642 parser->skip_stack = node->next;
1643 talloc_free (node);
1644}