blob: c70370a6c322ab846dde35f63ed5fabb0f9b2683 [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 }
235| HASH_IFDEF IDENTIFIER 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 }
240| HASH_IFNDEF IDENTIFIER 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
Carl Worthaaa9acb2010-05-19 13:28:24 -0700380pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700381 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700382 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700383 $$ = _token_list_create (parser);
384 _token_list_append ($$, $1);
385 talloc_unlink (parser, $1);
386 }
387| pp_tokens preprocessing_token {
388 $$ = $1;
389 _token_list_append ($$, $2);
390 talloc_unlink (parser, $2);
391 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700392;
393
Carl Worth3ff81672010-05-25 13:09:03 -0700394preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700395 IDENTIFIER {
396 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700397 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700398 }
Carl Worth050e3de2010-05-27 14:36:29 -0700399| INTEGER_STRING {
400 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700401 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700402 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700403| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700404 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700405 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700406 }
407| OTHER {
408 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700409 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700410 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700411| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700412 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700413 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700414 }
Carl Worth3ff81672010-05-25 13:09:03 -0700415;
416
Carl Worth8e82fcb2010-05-26 11:15:21 -0700417operator:
Carl Worth808401f2010-05-25 14:52:43 -0700418 '[' { $$ = '['; }
419| ']' { $$ = ']'; }
420| '(' { $$ = '('; }
421| ')' { $$ = ')'; }
422| '{' { $$ = '{'; }
423| '}' { $$ = '}'; }
424| '.' { $$ = '.'; }
425| '&' { $$ = '&'; }
426| '*' { $$ = '*'; }
427| '+' { $$ = '+'; }
428| '-' { $$ = '-'; }
429| '~' { $$ = '~'; }
430| '!' { $$ = '!'; }
431| '/' { $$ = '/'; }
432| '%' { $$ = '%'; }
433| LEFT_SHIFT { $$ = LEFT_SHIFT; }
434| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
435| '<' { $$ = '<'; }
436| '>' { $$ = '>'; }
437| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
438| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
439| EQUAL { $$ = EQUAL; }
440| NOT_EQUAL { $$ = NOT_EQUAL; }
441| '^' { $$ = '^'; }
442| '|' { $$ = '|'; }
443| AND { $$ = AND; }
444| OR { $$ = OR; }
445| ';' { $$ = ';'; }
446| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700447| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700448| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700449| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700450;
451
Carl Worth33cc4002010-05-12 12:17:10 -0700452%%
453
Carl Worth610053b2010-05-14 10:05:11 -0700454string_list_t *
455_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700456{
Carl Worth610053b2010-05-14 10:05:11 -0700457 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700458
Carl Worth610053b2010-05-14 10:05:11 -0700459 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700460 list->head = NULL;
461 list->tail = NULL;
462
463 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700464}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700465
Carl Worth33cc4002010-05-12 12:17:10 -0700466void
Carl Worth610053b2010-05-14 10:05:11 -0700467_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700468{
469 if (list->head == NULL) {
470 list->head = tail->head;
471 } else {
472 list->tail->next = tail->head;
473 }
474
475 list->tail = tail->tail;
476}
477
478void
Carl Worth610053b2010-05-14 10:05:11 -0700479_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700480{
Carl Worth610053b2010-05-14 10:05:11 -0700481 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700482
Carl Worth610053b2010-05-14 10:05:11 -0700483 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700484 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700485
Carl Worth33cc4002010-05-12 12:17:10 -0700486 node->next = NULL;
487
488 if (list->head == NULL) {
489 list->head = node;
490 } else {
491 list->tail->next = node;
492 }
493
494 list->tail = node;
495}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700496
497int
Carl Worth610053b2010-05-14 10:05:11 -0700498_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700499{
Carl Worth610053b2010-05-14 10:05:11 -0700500 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700501 int i;
502
503 if (list == NULL)
504 return 0;
505
506 for (i = 0, node = list->head; node; i++, node = node->next) {
507 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700508 if (index)
509 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700510 return 1;
511 }
512 }
513
514 return 0;
515}
516
517int
Carl Worth610053b2010-05-14 10:05:11 -0700518_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700519{
520 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700521 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700522
523 if (list == NULL)
524 return 0;
525
526 for (node = list->head; node; node = node->next)
527 length++;
528
529 return length;
530}
531
Carl Worth8f6a8282010-05-14 10:44:19 -0700532argument_list_t *
533_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700534{
Carl Worth8f6a8282010-05-14 10:44:19 -0700535 argument_list_t *list;
536
537 list = xtalloc (ctx, argument_list_t);
538 list->head = NULL;
539 list->tail = NULL;
540
541 return list;
542}
543
544void
Carl Worth47252442010-05-19 13:54:37 -0700545_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700546{
547 argument_node_t *node;
548
Carl Worth8f6a8282010-05-14 10:44:19 -0700549 node = xtalloc (list, argument_node_t);
550 node->argument = argument;
551
552 node->next = NULL;
553
554 if (list->head == NULL) {
555 list->head = node;
556 } else {
557 list->tail->next = node;
558 }
559
560 list->tail = node;
561}
562
563int
564_argument_list_length (argument_list_t *list)
565{
566 int length = 0;
567 argument_node_t *node;
568
569 if (list == NULL)
570 return 0;
571
572 for (node = list->head; node; node = node->next)
573 length++;
574
575 return length;
576}
577
Carl Worth47252442010-05-19 13:54:37 -0700578token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700579_argument_list_member_at (argument_list_t *list, int index)
580{
581 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700582 int i;
583
584 if (list == NULL)
585 return NULL;
586
587 node = list->head;
588 for (i = 0; i < index; i++) {
589 node = node->next;
590 if (node == NULL)
591 break;
592 }
593
594 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700595 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700596
597 return NULL;
598}
Carl Worth47252442010-05-19 13:54:37 -0700599
Carl Worth808401f2010-05-25 14:52:43 -0700600/* Note: This function talloc_steal()s the str pointer. */
601token_t *
602_token_create_str (void *ctx, int type, char *str)
603{
604 token_t *token;
605
606 token = xtalloc (ctx, token_t);
607 token->type = type;
608 token->value.str = talloc_steal (token, str);
609
610 return token;
611}
612
613token_t *
614_token_create_ival (void *ctx, int type, int ival)
615{
616 token_t *token;
617
618 token = xtalloc (ctx, token_t);
619 token->type = type;
620 token->value.ival = ival;
621
622 return token;
623}
624
Carl Worth47252442010-05-19 13:54:37 -0700625token_list_t *
626_token_list_create (void *ctx)
627{
628 token_list_t *list;
629
630 list = xtalloc (ctx, token_list_t);
631 list->head = NULL;
632 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700633 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700634
635 return list;
636}
637
638void
Carl Worth808401f2010-05-25 14:52:43 -0700639_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700640{
641 token_node_t *node;
642
643 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700644 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700645
646 node->next = NULL;
647
648 if (list->head == NULL) {
649 list->head = node;
650 } else {
651 list->tail->next = node;
652 }
653
654 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700655 if (token->type != SPACE)
656 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700657}
658
659void
660_token_list_append_list (token_list_t *list, token_list_t *tail)
661{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700662 if (tail == NULL || tail->head == NULL)
663 return;
664
Carl Worth47252442010-05-19 13:54:37 -0700665 if (list->head == NULL) {
666 list->head = tail->head;
667 } else {
668 list->tail->next = tail->head;
669 }
670
671 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700672 list->non_space_tail = tail->non_space_tail;
673}
674
Carl Worth681afbc2010-05-28 15:06:02 -0700675token_list_t *
676_token_list_copy (void *ctx, token_list_t *other)
677{
678 token_list_t *copy;
679 token_node_t *node;
680
681 if (other == NULL)
682 return NULL;
683
684 copy = _token_list_create (ctx);
685 for (node = other->head; node; node = node->next)
686 _token_list_append (copy, node->token);
687
688 return copy;
689}
690
Carl Worth10ae4382010-05-25 20:35:01 -0700691void
692_token_list_trim_trailing_space (token_list_t *list)
693{
694 token_node_t *tail, *next;
695
696 if (list->non_space_tail) {
697 tail = list->non_space_tail->next;
698 list->non_space_tail->next = NULL;
699 list->tail = list->non_space_tail;
700
701 while (tail) {
702 next = tail->next;
703 talloc_free (tail);
704 tail = next;
705 }
706 }
Carl Worth47252442010-05-19 13:54:37 -0700707}
Carl Worth80dc60b2010-05-25 14:42:00 -0700708
Carl Worth22b3ace2010-06-02 15:32:03 -0700709static int
710_token_list_length (token_list_t *list)
711{
712 int length = 0;
713 token_node_t *node;
714
715 if (list == NULL)
716 return 0;
717
718 for (node = list->head; node; node = node->next)
719 length++;
720
721 return length;
722}
723
Carl Worth0197e9b2010-05-26 08:05:19 -0700724static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700725_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700726{
727 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700728 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700729 return;
730 }
731
732 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700733 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700734 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700735 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700736 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700737 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700738 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700739 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700740 break;
741 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700742 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700743 break;
744 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700745 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700746 break;
747 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700748 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700749 break;
750 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700751 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700752 break;
753 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700754 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700755 break;
756 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700757 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700758 break;
759 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700760 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700761 break;
762 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700763 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700764 break;
765 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700766 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700767 break;
768 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700769 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700770 break;
Carl Worthdd749002010-05-27 10:12:33 -0700771 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700772 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700773 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700774 case PLACEHOLDER:
775 /* Nothing to print. */
776 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700777 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700778 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700779 break;
780 }
781}
782
Carl Worthb06096e2010-05-29 05:54:19 -0700783/* Return a new token (talloc()ed off of 'token') formed by pasting
784 * 'token' and 'other'. Note that this function may return 'token' or
785 * 'other' directly rather than allocating anything new.
786 *
787 * Caution: Only very cursory error-checking is performed to see if
788 * the final result is a valid single token. */
789static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700790_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700791{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700792 token_t *combined = NULL;
793
Carl Worth85b50e82010-05-27 14:01:18 -0700794 /* Pasting a placeholder onto anything makes no change. */
795 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700796 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700797
Carl Worthb06096e2010-05-29 05:54:19 -0700798 /* When 'token' is a placeholder, just return 'other'. */
799 if (token->type == PLACEHOLDER)
800 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700801
Carl Worthad0dee62010-05-26 09:04:50 -0700802 /* A very few single-character punctuators can be combined
803 * with another to form a multi-character punctuator. */
804 switch (token->type) {
805 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700806 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700807 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700808 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700809 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700810 break;
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, RIGHT_SHIFT, RIGHT_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, GREATER_OR_EQUAL, GREATER_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, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700820 break;
821 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700822 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700823 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700824 break;
825 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700826 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700827 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700828 break;
829 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700830 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700831 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700832 break;
833 }
834
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700835 if (combined != NULL) {
836 /* Inherit the location from the first token */
837 combined->location = token->location;
838 return combined;
839 }
840
Carl Worthad0dee62010-05-26 09:04:50 -0700841 /* Two string-valued tokens can usually just be mashed
842 * together.
843 *
Carl Worth050e3de2010-05-27 14:36:29 -0700844 * XXX: This isn't actually legitimate. Several things here
845 * should result in a diagnostic since the result cannot be a
846 * valid, single pre-processing token. For example, pasting
847 * "123" and "abc" is not legal, but we don't catch that
848 * here. */
849 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
850 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700851 {
Carl Worthb06096e2010-05-29 05:54:19 -0700852 char *str;
853
854 str = xtalloc_asprintf (token, "%s%s",
855 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700856 combined = _token_create_str (token, token->type, str);
857 combined->location = token->location;
858 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700859 }
860
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700861 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700862 glcpp_print (parser->info_log, "Pasting \"");
863 _token_print (&parser->info_log, token);
864 glcpp_print (parser->info_log, "\" and \"");
865 _token_print (&parser->info_log, other);
866 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700867
868 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700869}
870
Carl Worth0197e9b2010-05-26 08:05:19 -0700871static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700872_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700873{
874 token_node_t *node;
875
876 if (list == NULL)
877 return;
878
879 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700880 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700881}
882
Carl Worth3a37b872010-05-10 11:44:09 -0700883void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700884yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700885{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700886 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700887}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700888
Carl Worth33cc4002010-05-12 12:17:10 -0700889glcpp_parser_t *
890glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700891{
Carl Worth33cc4002010-05-12 12:17:10 -0700892 glcpp_parser_t *parser;
893
Carl Worth5070a202010-05-12 12:45:33 -0700894 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700895
Carl Worth8f38aff2010-05-19 10:01:29 -0700896 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700897 parser->defines = hash_table_ctor (32, hash_table_string_hash,
898 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700899 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700900 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700901 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700902 parser->newline_as_space = 0;
903 parser->in_control_line = 0;
904 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700905
Carl Worthb20d33c2010-05-20 22:27:07 -0700906 parser->skip_stack = NULL;
907
Carl Worth8e82fcb2010-05-26 11:15:21 -0700908 parser->lex_from_list = NULL;
909 parser->lex_from_node = NULL;
910
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700911 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700912 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700913
Carl Worth33cc4002010-05-12 12:17:10 -0700914 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700915}
916
917int
918glcpp_parser_parse (glcpp_parser_t *parser)
919{
920 return yyparse (parser);
921}
922
923void
Carl Worth33cc4002010-05-12 12:17:10 -0700924glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700925{
Carl Worthb20d33c2010-05-20 22:27:07 -0700926 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700927 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700928 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700929 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700930 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700931}
Carl Worthc6d5af32010-05-11 12:30:09 -0700932
Carl Worth8e82fcb2010-05-26 11:15:21 -0700933/* Replace any occurences of DEFINED tokens in 'list' with either a
934 * '0' or '1' INTEGER token depending on whether the next token in the
935 * list is defined or not. */
936static void
937_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
938 token_list_t *list)
939{
940 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700941 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700942
943 if (list == NULL)
944 return;
945
946 for (node = list->head; node; node = node->next) {
947 if (node->token->type != DEFINED)
948 continue;
949 next = node->next;
950 while (next && next->token->type == SPACE)
951 next = next->next;
952 if (next == NULL || next->token->type != IDENTIFIER) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700953 yyerror (&node->token->location, parser, "operator \"defined\" requires an identifier\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -0700954 /* Already flagged an error; fake it. */
955 node->token->type = INTEGER;
956 node->token->value.ival = 0;
957 return;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700958 }
959 macro = hash_table_find (parser->defines,
960 next->token->value.str);
961
962 node->token->type = INTEGER;
963 node->token->value.ival = (macro != NULL);
964 node->next = next->next;
965 }
966}
967
Carl Worthb1854fd2010-05-25 16:28:26 -0700968typedef enum function_status
969{
970 FUNCTION_STATUS_SUCCESS,
971 FUNCTION_NOT_A_FUNCTION,
972 FUNCTION_UNBALANCED_PARENTHESES
973} function_status_t;
974
975/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700976 * balanced set of parentheses.
977 *
978 * When called, 'node' should be the opening-parenthesis token, (or
979 * perhaps preceeding SPACE tokens). Upon successful return *last will
980 * be the last consumed node, (corresponding to the closing right
981 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700982 *
983 * Return values:
984 *
985 * FUNCTION_STATUS_SUCCESS:
986 *
987 * Successfully parsed a set of function arguments.
988 *
989 * FUNCTION_NOT_A_FUNCTION:
990 *
991 * Macro name not followed by a '('. This is not an error, but
992 * simply that the macro name should be treated as a non-macro.
993 *
Carl Worth14c98a52010-06-02 15:49:54 -0700994 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -0700995 *
996 * Macro name is not followed by a balanced set of parentheses.
997 */
998static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700999_arguments_parse (argument_list_t *arguments,
1000 token_node_t *node,
1001 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -07001002{
Carl Worth9ce18cf2010-05-25 17:32:21 -07001003 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001004 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001005
Carl Worthb1854fd2010-05-25 16:28:26 -07001006 node = node->next;
1007
1008 /* Ignore whitespace before first parenthesis. */
1009 while (node && node->token->type == SPACE)
1010 node = node->next;
1011
1012 if (node == NULL || node->token->type != '(')
1013 return FUNCTION_NOT_A_FUNCTION;
1014
Carl Worth652fa272010-05-25 17:45:22 -07001015 node = node->next;
1016
Carl Wortha19297b2010-05-27 13:29:19 -07001017 argument = _token_list_create (arguments);
1018 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001019
Carl Worth681afbc2010-05-28 15:06:02 -07001020 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001021 if (node->token->type == '(')
1022 {
1023 paren_count++;
1024 }
1025 else if (node->token->type == ')')
1026 {
1027 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001028 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001029 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001030 }
Carl Worth652fa272010-05-25 17:45:22 -07001031
1032 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001033 paren_count == 1)
1034 {
Carl Wortha19297b2010-05-27 13:29:19 -07001035 _token_list_trim_trailing_space (argument);
1036 argument = _token_list_create (arguments);
1037 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001038 }
1039 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001040 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001041 /* Don't treat initial whitespace as
1042 * part of the arguement. */
1043 if (node->token->type == SPACE)
1044 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001045 }
1046 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001047 }
Carl Worthc7581c22010-05-25 17:41:07 -07001048 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001049
Carl Worth681afbc2010-05-28 15:06:02 -07001050 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001051 return FUNCTION_UNBALANCED_PARENTHESES;
1052
Carl Worth681afbc2010-05-28 15:06:02 -07001053 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001054
1055 return FUNCTION_STATUS_SUCCESS;
1056}
1057
Carl Worth681afbc2010-05-28 15:06:02 -07001058/* This is a helper function that's essentially part of the
1059 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1060 * except for by that function.
1061 *
1062 * Returns NULL if node is a simple token with no expansion, (that is,
1063 * although 'node' corresponds to an identifier defined as a
1064 * function-like macro, it is not followed with a parenthesized
1065 * argument list).
1066 *
1067 * Compute the complete expansion of node (which is a function-like
1068 * macro) and subsequent nodes which are arguments.
1069 *
1070 * Returns the token list that results from the expansion and sets
1071 * *last to the last node in the list that was consumed by the
1072 * expansion. Specificallty, *last will be set as follows: as the
1073 * token of the closing right parenthesis.
1074 */
1075static token_list_t *
1076_glcpp_parser_expand_function (glcpp_parser_t *parser,
1077 token_node_t *node,
1078 token_node_t **last)
1079
Carl Worthb1854fd2010-05-25 16:28:26 -07001080{
1081 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001082 const char *identifier;
1083 argument_list_t *arguments;
1084 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001085 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001086 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001087
Carl Worthb1854fd2010-05-25 16:28:26 -07001088 identifier = node->token->value.str;
1089
1090 macro = hash_table_find (parser->defines, identifier);
1091
1092 assert (macro->is_function);
1093
Carl Worth9ce18cf2010-05-25 17:32:21 -07001094 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001095 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001096
1097 switch (status) {
1098 case FUNCTION_STATUS_SUCCESS:
1099 break;
1100 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001101 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001102 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001103 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001104 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001105 }
1106
Carl Worth9ce18cf2010-05-25 17:32:21 -07001107 if (macro->replacements == NULL) {
1108 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001109 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001110 }
1111
Carl Wortha19297b2010-05-27 13:29:19 -07001112 if (! ((_argument_list_length (arguments) ==
1113 _string_list_length (macro->parameters)) ||
1114 (_string_list_length (macro->parameters) == 0 &&
1115 _argument_list_length (arguments) == 1 &&
1116 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001117 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001118 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001119 "Error: macro %s invoked with %d arguments (expected %d)\n",
1120 identifier,
1121 _argument_list_length (arguments),
1122 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001123 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001124 }
1125
Carl Worth0197e9b2010-05-26 08:05:19 -07001126 /* Perform argument substitution on the replacement list. */
1127 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001128
Carl Worthce540f22010-05-26 08:25:44 -07001129 for (node = macro->replacements->head; node; node = node->next)
1130 {
1131 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001132 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001133 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001134 &parameter_index))
1135 {
1136 token_list_t *argument;
1137 argument = _argument_list_member_at (arguments,
1138 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001139 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001140 * tokens, or append a placeholder token for
1141 * an empty argument. */
1142 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001143 _glcpp_parser_expand_token_list (parser,
1144 argument);
1145 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001146 } else {
1147 token_t *new_token;
1148
1149 new_token = _token_create_ival (substituted,
1150 PLACEHOLDER,
1151 PLACEHOLDER);
1152 _token_list_append (substituted, new_token);
1153 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001154 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001155 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001156 }
1157 }
1158
Carl Worthad0dee62010-05-26 09:04:50 -07001159 /* After argument substitution, and before further expansion
1160 * below, implement token pasting. */
1161
Carl Worthb06096e2010-05-29 05:54:19 -07001162 _token_list_trim_trailing_space (substituted);
1163
Carl Worthad0dee62010-05-26 09:04:50 -07001164 node = substituted->head;
1165 while (node)
1166 {
1167 token_node_t *next_non_space;
1168
1169 /* Look ahead for a PASTE token, skipping space. */
1170 next_non_space = node->next;
1171 while (next_non_space && next_non_space->token->type == SPACE)
1172 next_non_space = next_non_space->next;
1173
1174 if (next_non_space == NULL)
1175 break;
1176
1177 if (next_non_space->token->type != PASTE) {
1178 node = next_non_space;
1179 continue;
1180 }
1181
1182 /* Now find the next non-space token after the PASTE. */
1183 next_non_space = next_non_space->next;
1184 while (next_non_space && next_non_space->token->type == SPACE)
1185 next_non_space = next_non_space->next;
1186
1187 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001188 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001189 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001190 }
1191
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001192 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001193 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001194 if (next_non_space == substituted->tail)
1195 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001196
1197 node = node->next;
1198 }
1199
Carl Worthb06096e2010-05-29 05:54:19 -07001200 substituted->non_space_tail = substituted->tail;
1201
Carl Worth681afbc2010-05-28 15:06:02 -07001202 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001203}
1204
Carl Worth681afbc2010-05-28 15:06:02 -07001205/* Compute the complete expansion of node, (and subsequent nodes after
1206 * 'node' in the case that 'node' is a function-like macro and
1207 * subsequent nodes are arguments).
1208 *
1209 * Returns NULL if node is a simple token with no expansion.
1210 *
1211 * Otherwise, returns the token list that results from the expansion
1212 * and sets *last to the last node in the list that was consumed by
1213 * the expansion. Specificallty, *last will be set as follows:
1214 *
1215 * As 'node' in the case of object-like macro expansion.
1216 *
1217 * As the token of the closing right parenthesis in the case of
1218 * function-like macro expansion.
1219 */
1220static token_list_t *
1221_glcpp_parser_expand_node (glcpp_parser_t *parser,
1222 token_node_t *node,
1223 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001224{
Carl Worth681afbc2010-05-28 15:06:02 -07001225 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001226 const char *identifier;
1227 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001228
1229 /* We only expand identifiers */
1230 if (token->type != IDENTIFIER) {
1231 /* We change any COMMA into a COMMA_FINAL to prevent
1232 * it being mistaken for an argument separator
1233 * later. */
1234 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001235 token->type = COMMA_FINAL;
1236 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001237 }
Carl Worth681afbc2010-05-28 15:06:02 -07001238
1239 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001240 }
1241
1242 /* Look up this identifier in the hash table. */
1243 identifier = token->value.str;
1244 macro = hash_table_find (parser->defines, identifier);
1245
Carl Worth681afbc2010-05-28 15:06:02 -07001246 /* Not a macro, so no expansion needed. */
1247 if (macro == NULL)
1248 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001249
1250 /* Finally, don't expand this macro if we're already actively
1251 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001252 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001253 /* We change the token type here from IDENTIFIER to
1254 * OTHER to prevent any future expansion of this
1255 * unexpanded token. */
1256 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001257 token_list_t *expansion;
1258 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001259
Carl Worth681afbc2010-05-28 15:06:02 -07001260 str = xtalloc_strdup (parser, token->value.str);
1261 final = _token_create_str (parser, OTHER, str);
1262 expansion = _token_list_create (parser);
1263 _token_list_append (expansion, final);
1264 *last = node;
1265 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001266 }
1267
Carl Worth681afbc2010-05-28 15:06:02 -07001268 if (! macro->is_function)
1269 {
1270 *last = node;
1271
1272 if (macro->replacements == NULL)
1273 return _token_list_create (parser);
1274
Carl Worth22b3ace2010-06-02 15:32:03 -07001275 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001276 }
Carl Worth681afbc2010-05-28 15:06:02 -07001277
1278 return _glcpp_parser_expand_function (parser, node, last);
1279}
1280
Carl Worth22b3ace2010-06-02 15:32:03 -07001281/* Push a new identifier onto the active list, returning the new list.
1282 *
1283 * Here, 'marker' is the token node that appears in the list after the
1284 * expansion of 'identifier'. That is, when the list iterator begins
1285 * examinging 'marker', then it is time to pop this node from the
1286 * active stack.
1287 */
1288active_list_t *
1289_active_list_push (active_list_t *list,
1290 const char *identifier,
1291 token_node_t *marker)
1292{
1293 active_list_t *node;
1294
1295 node = xtalloc (list, active_list_t);
1296 node->identifier = xtalloc_strdup (node, identifier);
1297 node->marker = marker;
1298 node->next = list;
1299
1300 return node;
1301}
1302
1303active_list_t *
1304_active_list_pop (active_list_t *list)
1305{
1306 active_list_t *node = list;
1307
1308 if (node == NULL)
1309 return NULL;
1310
1311 node = list->next;
1312 talloc_free (list);
1313
1314 return node;
1315}
1316
1317int
1318_active_list_contains (active_list_t *list, const char *identifier)
1319{
1320 active_list_t *node;
1321
1322 if (list == NULL)
1323 return 0;
1324
1325 for (node = list; node; node = node->next)
1326 if (strcmp (node->identifier, identifier) == 0)
1327 return 1;
1328
1329 return 0;
1330}
1331
Carl Worth681afbc2010-05-28 15:06:02 -07001332/* Walk over the token list replacing nodes with their expansion.
1333 * Whenever nodes are expanded the walking will walk over the new
1334 * nodes, continuing to expand as necessary. The results are placed in
1335 * 'list' itself;
1336 */
1337static void
1338_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1339 token_list_t *list)
1340{
1341 token_node_t *node_prev;
1342 token_node_t *node, *last;
1343 token_list_t *expansion;
1344
1345 if (list == NULL)
1346 return;
1347
1348 _token_list_trim_trailing_space (list);
1349
1350 node_prev = NULL;
1351 node = list->head;
1352
1353 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001354
1355 while (parser->active && parser->active->marker == node)
1356 parser->active = _active_list_pop (parser->active);
1357
Carl Worth681afbc2010-05-28 15:06:02 -07001358 /* Find the expansion for node, which will replace all
1359 * nodes from node to last, inclusive. */
1360 expansion = _glcpp_parser_expand_node (parser, node, &last);
1361 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001362 token_node_t *n;
1363
1364 for (n = node; n != last->next; n = n->next)
1365 while (parser->active &&
1366 parser->active->marker == n)
1367 {
1368 parser->active = _active_list_pop (parser->active);
1369 }
1370
1371 parser->active = _active_list_push (parser->active,
1372 node->token->value.str,
1373 last->next);
1374
Carl Worth681afbc2010-05-28 15:06:02 -07001375 /* Splice expansion into list, supporting a
1376 * simple deletion if the expansion is
1377 * empty. */
1378 if (expansion->head) {
1379 if (node_prev)
1380 node_prev->next = expansion->head;
1381 else
1382 list->head = expansion->head;
1383 expansion->tail->next = last->next;
1384 if (last == list->tail)
1385 list->tail = expansion->tail;
1386 } else {
1387 if (node_prev)
1388 node_prev->next = last->next;
1389 else
1390 list->head = last->next;
1391 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001392 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001393 }
1394 } else {
1395 node_prev = node;
1396 }
1397 node = node_prev ? node_prev->next : list->head;
1398 }
1399
Carl Worth22b3ace2010-06-02 15:32:03 -07001400 while (parser->active)
1401 parser->active = _active_list_pop (parser->active);
1402
Carl Worth681afbc2010-05-28 15:06:02 -07001403 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001404}
1405
Carl Worthae6517f2010-05-25 15:24:59 -07001406void
1407_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1408 token_list_t *list)
1409{
Carl Worthae6517f2010-05-25 15:24:59 -07001410 if (list == NULL)
1411 return;
1412
Carl Worth681afbc2010-05-28 15:06:02 -07001413 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001414
Carl Worth681afbc2010-05-28 15:06:02 -07001415 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001416
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001417 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001418}
1419
1420void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001421_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1422 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001423{
1424 /* According to the GLSL specification, macro names starting with "__"
1425 * or "GL_" are reserved for future use. So, don't allow them.
1426 */
1427 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001428 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001429 }
1430 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001431 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001432 }
1433}
1434
1435void
Carl Worthfcbbb462010-05-13 09:36:23 -07001436_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001437 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001438 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001439 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001440{
1441 macro_t *macro;
1442
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001443 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001444
Carl Worthfcbbb462010-05-13 09:36:23 -07001445 macro = xtalloc (parser, macro_t);
1446
1447 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001448 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001449 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001450 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001451
1452 hash_table_insert (parser->defines, macro, identifier);
1453}
1454
1455void
1456_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001457 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001458 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001459 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001460 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001461{
1462 macro_t *macro;
1463
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001464 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001465
Carl Worthfcbbb462010-05-13 09:36:23 -07001466 macro = xtalloc (parser, macro_t);
1467
1468 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001469 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001470 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001471 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001472
1473 hash_table_insert (parser->defines, macro, identifier);
1474}
1475
Carl Worth8f38aff2010-05-19 10:01:29 -07001476static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001477glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001478{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001479 token_node_t *node;
1480 int ret;
1481
Carl Worth95951ea2010-05-26 15:57:10 -07001482 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001483 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001484
1485 /* XXX: This ugly block of code exists for the sole
1486 * purpose of converting a NEWLINE token into a SPACE
1487 * token, but only in the case where we have seen a
1488 * function-like macro name, but have not yet seen its
1489 * closing parenthesis.
1490 *
1491 * There's perhaps a more compact way to do this with
1492 * mid-rule actions in the grammar.
1493 *
1494 * I'm definitely not pleased with the complexity of
1495 * this code here.
1496 */
1497 if (parser->newline_as_space)
1498 {
1499 if (ret == '(') {
1500 parser->paren_count++;
1501 } else if (ret == ')') {
1502 parser->paren_count--;
1503 if (parser->paren_count == 0)
1504 parser->newline_as_space = 0;
1505 } else if (ret == NEWLINE) {
1506 ret = SPACE;
1507 } else if (ret != SPACE) {
1508 if (parser->paren_count == 0)
1509 parser->newline_as_space = 0;
1510 }
1511 }
1512 else if (parser->in_control_line)
1513 {
1514 if (ret == NEWLINE)
1515 parser->in_control_line = 0;
1516 }
1517 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1518 ret == HASH_UNDEF || ret == HASH_IF ||
1519 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1520 ret == HASH_ELIF || ret == HASH_ELSE ||
1521 ret == HASH_ENDIF || ret == HASH)
1522 {
1523 parser->in_control_line = 1;
1524 }
1525 else if (ret == IDENTIFIER)
1526 {
1527 macro_t *macro;
1528 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001529 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001530 if (macro && macro->is_function) {
1531 parser->newline_as_space = 1;
1532 parser->paren_count = 0;
1533 }
1534 }
1535
1536 return ret;
1537 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001538
1539 node = parser->lex_from_node;
1540
1541 if (node == NULL) {
1542 talloc_free (parser->lex_from_list);
1543 parser->lex_from_list = NULL;
1544 return NEWLINE;
1545 }
1546
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001547 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001548 ret = node->token->type;
1549
1550 parser->lex_from_node = node->next;
1551
1552 return ret;
1553}
1554
1555static void
1556glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1557{
1558 token_node_t *node;
1559
1560 assert (parser->lex_from_list == NULL);
1561
1562 /* Copy list, eliminating any space tokens. */
1563 parser->lex_from_list = _token_list_create (parser);
1564
1565 for (node = list->head; node; node = node->next) {
1566 if (node->token->type == SPACE)
1567 continue;
1568 _token_list_append (parser->lex_from_list, node->token);
1569 }
1570
1571 talloc_free (list);
1572
1573 parser->lex_from_node = parser->lex_from_list->head;
1574
1575 /* It's possible the list consisted of nothing but whitespace. */
1576 if (parser->lex_from_node == NULL) {
1577 talloc_free (parser->lex_from_list);
1578 parser->lex_from_list = NULL;
1579 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001580}
Carl Worthb20d33c2010-05-20 22:27:07 -07001581
1582static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001583_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1584 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001585{
1586 skip_type_t current = SKIP_NO_SKIP;
1587 skip_node_t *node;
1588
1589 if (parser->skip_stack)
1590 current = parser->skip_stack->type;
1591
1592 node = xtalloc (parser, skip_node_t);
Kenneth Graunke07745232010-06-17 12:41:46 -07001593 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001594
1595 if (current == SKIP_NO_SKIP) {
1596 if (condition)
1597 node->type = SKIP_NO_SKIP;
1598 else
1599 node->type = SKIP_TO_ELSE;
1600 } else {
1601 node->type = SKIP_TO_ENDIF;
1602 }
1603
1604 node->next = parser->skip_stack;
1605 parser->skip_stack = node;
1606}
1607
1608static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001609_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1610 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001611{
1612 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001613 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001614 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001615 }
1616
1617 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1618 if (condition)
1619 parser->skip_stack->type = SKIP_NO_SKIP;
1620 } else {
1621 parser->skip_stack->type = SKIP_TO_ENDIF;
1622 }
1623}
Carl Worth80dc60b2010-05-25 14:42:00 -07001624
Carl Worthb20d33c2010-05-20 22:27:07 -07001625static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001626_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001627{
1628 skip_node_t *node;
1629
1630 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001631 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001632 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001633 }
1634
1635 node = parser->skip_stack;
1636 parser->skip_stack = node->next;
1637 talloc_free (node);
1638}