blob: b1669fa331cf8f6a7cd0c3d56010858814427a41 [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
Carl Worthb20d33c2010-05-20 22:27:07 -0700126_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
127
128static void
129_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
130 int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700131
Carl Worthb20d33c2010-05-20 22:27:07 -0700132static void
133_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
134
Carl Worth0293b2e2010-05-19 10:05:40 -0700135#define yylex glcpp_parser_lex
136
Carl Worth8f38aff2010-05-19 10:01:29 -0700137static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700138glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700139
Carl Worth8e82fcb2010-05-26 11:15:21 -0700140static void
141glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
142
Carl Worth3a37b872010-05-10 11:44:09 -0700143%}
144
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700145%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700146%error-verbose
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700147%locations
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700148
Carl Worth0b27b5f2010-05-10 16:16:06 -0700149%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700150%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700151
Carl Worth050e3de2010-05-27 14:36:29 -0700152%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 -0700153%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700154%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700155%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700156%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700157%type <token> preprocessing_token
158%type <token_list> pp_tokens replacement_list text_line
Carl Worth8fed1cd2010-05-26 09:32:12 -0700159%left OR
160%left AND
161%left '|'
162%left '^'
163%left '&'
164%left EQUAL NOT_EQUAL
165%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
166%left LEFT_SHIFT RIGHT_SHIFT
167%left '+' '-'
168%left '*' '/' '%'
169%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700170
171%%
172
Carl Worth33cc4002010-05-12 12:17:10 -0700173input:
Carl Worth3ff81672010-05-25 13:09:03 -0700174 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700175| input line
176;
177
178line:
179 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700180 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700181 }
Carl Worth808401f2010-05-25 14:52:43 -0700182| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700183 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700184 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700185 talloc_free ($1);
186 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700187| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700188| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700189;
190
Carl Worth8e82fcb2010-05-26 11:15:21 -0700191expanded_line:
192 IF_EXPANDED expression NEWLINE {
193 _glcpp_parser_skip_stack_push_if (parser, $2);
194 }
195| ELIF_EXPANDED expression NEWLINE {
196 _glcpp_parser_skip_stack_change_if (parser, "elif", $2);
197 }
198;
199
Carl Worth3ff81672010-05-25 13:09:03 -0700200control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700201 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700202 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700203 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700204| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700205 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700206 }
207| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700208 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700209 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700210| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700211 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700212 if (macro) {
213 /* XXX: Need hash table to support a real way
214 * to remove an element rather than prefixing
215 * a new node with data of NULL like this. */
216 hash_table_insert (parser->defines, NULL, $2);
217 talloc_free (macro);
218 }
219 talloc_free ($2);
220 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700221| HASH_IF pp_tokens NEWLINE {
222 token_list_t *expanded;
223 token_t *token;
224
225 expanded = _token_list_create (parser);
226 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
227 _token_list_append (expanded, token);
228 talloc_unlink (parser, token);
229 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700230 _glcpp_parser_expand_token_list (parser, $2);
231 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700232 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700233 }
234| HASH_IFDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700235 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700236 talloc_free ($2);
237 _glcpp_parser_skip_stack_push_if (parser, macro != NULL);
238 }
239| HASH_IFNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700240 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700241 talloc_free ($2);
242 _glcpp_parser_skip_stack_push_if (parser, macro == NULL);
243 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700244| HASH_ELIF pp_tokens NEWLINE {
245 token_list_t *expanded;
246 token_t *token;
247
248 expanded = _token_list_create (parser);
249 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
250 _token_list_append (expanded, token);
251 talloc_unlink (parser, token);
252 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700253 _glcpp_parser_expand_token_list (parser, $2);
254 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700255 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700256 }
257| HASH_ELSE NEWLINE {
258 _glcpp_parser_skip_stack_change_if (parser, "else", 1);
259 }
260| HASH_ENDIF NEWLINE {
261 _glcpp_parser_skip_stack_pop (parser);
262 }
Carl Worth3ff81672010-05-25 13:09:03 -0700263| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700264;
265
Carl Worth8fed1cd2010-05-26 09:32:12 -0700266expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700267 INTEGER_STRING {
268 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
269 $$ = strtoll ($1 + 2, NULL, 16);
270 } else if ($1[0] == '0') {
271 $$ = strtoll ($1, NULL, 8);
272 } else {
273 $$ = strtoll ($1, NULL, 10);
274 }
275 }
276| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700277 $$ = $1;
278 }
279| expression OR expression {
280 $$ = $1 || $3;
281 }
282| expression AND expression {
283 $$ = $1 && $3;
284 }
285| expression '|' expression {
286 $$ = $1 | $3;
287 }
288| expression '^' expression {
289 $$ = $1 ^ $3;
290 }
291| expression '&' expression {
292 $$ = $1 & $3;
293 }
294| expression NOT_EQUAL expression {
295 $$ = $1 != $3;
296 }
297| expression EQUAL expression {
298 $$ = $1 == $3;
299 }
300| expression GREATER_OR_EQUAL expression {
301 $$ = $1 >= $3;
302 }
303| expression LESS_OR_EQUAL expression {
304 $$ = $1 <= $3;
305 }
306| expression '>' expression {
307 $$ = $1 > $3;
308 }
309| expression '<' expression {
310 $$ = $1 < $3;
311 }
312| expression RIGHT_SHIFT expression {
313 $$ = $1 >> $3;
314 }
315| expression LEFT_SHIFT expression {
316 $$ = $1 << $3;
317 }
318| expression '-' expression {
319 $$ = $1 - $3;
320 }
321| expression '+' expression {
322 $$ = $1 + $3;
323 }
324| expression '%' expression {
325 $$ = $1 % $3;
326 }
327| expression '/' expression {
328 $$ = $1 / $3;
329 }
330| expression '*' expression {
331 $$ = $1 * $3;
332 }
333| '!' expression %prec UNARY {
334 $$ = ! $2;
335 }
336| '~' expression %prec UNARY {
337 $$ = ~ $2;
338 }
339| '-' expression %prec UNARY {
340 $$ = - $2;
341 }
342| '+' expression %prec UNARY {
343 $$ = + $2;
344 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700345| '(' expression ')' {
346 $$ = $2;
347 }
348;
349
Carl Worth3ff81672010-05-25 13:09:03 -0700350identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700351 IDENTIFIER {
352 $$ = _string_list_create (parser);
353 _string_list_append_item ($$, $1);
354 talloc_steal ($$, $1);
355 }
356| identifier_list ',' IDENTIFIER {
357 $$ = $1;
358 _string_list_append_item ($$, $3);
359 talloc_steal ($$, $3);
360 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700361;
362
Carl Worth3ff81672010-05-25 13:09:03 -0700363text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700364 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700365| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700366;
367
Carl Worth3ff81672010-05-25 13:09:03 -0700368non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700369 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700370 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700371 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700372;
373
Carl Worthaaa9acb2010-05-19 13:28:24 -0700374replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700375 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700376| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700377;
378
Carl Worthaaa9acb2010-05-19 13:28:24 -0700379pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700380 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700381 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700382 $$ = _token_list_create (parser);
383 _token_list_append ($$, $1);
384 talloc_unlink (parser, $1);
385 }
386| pp_tokens preprocessing_token {
387 $$ = $1;
388 _token_list_append ($$, $2);
389 talloc_unlink (parser, $2);
390 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700391;
392
Carl Worth3ff81672010-05-25 13:09:03 -0700393preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700394 IDENTIFIER {
395 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700396 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700397 }
Carl Worth050e3de2010-05-27 14:36:29 -0700398| INTEGER_STRING {
399 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700400 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700401 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700402| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700403 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700404 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700405 }
406| OTHER {
407 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700408 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700409 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700410| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700411 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700412 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700413 }
Carl Worth3ff81672010-05-25 13:09:03 -0700414;
415
Carl Worth8e82fcb2010-05-26 11:15:21 -0700416operator:
Carl Worth808401f2010-05-25 14:52:43 -0700417 '[' { $$ = '['; }
418| ']' { $$ = ']'; }
419| '(' { $$ = '('; }
420| ')' { $$ = ')'; }
421| '{' { $$ = '{'; }
422| '}' { $$ = '}'; }
423| '.' { $$ = '.'; }
424| '&' { $$ = '&'; }
425| '*' { $$ = '*'; }
426| '+' { $$ = '+'; }
427| '-' { $$ = '-'; }
428| '~' { $$ = '~'; }
429| '!' { $$ = '!'; }
430| '/' { $$ = '/'; }
431| '%' { $$ = '%'; }
432| LEFT_SHIFT { $$ = LEFT_SHIFT; }
433| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
434| '<' { $$ = '<'; }
435| '>' { $$ = '>'; }
436| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
437| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
438| EQUAL { $$ = EQUAL; }
439| NOT_EQUAL { $$ = NOT_EQUAL; }
440| '^' { $$ = '^'; }
441| '|' { $$ = '|'; }
442| AND { $$ = AND; }
443| OR { $$ = OR; }
444| ';' { $$ = ';'; }
445| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700446| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700447| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700448| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700449;
450
Carl Worth33cc4002010-05-12 12:17:10 -0700451%%
452
Carl Worth610053b2010-05-14 10:05:11 -0700453string_list_t *
454_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700455{
Carl Worth610053b2010-05-14 10:05:11 -0700456 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700457
Carl Worth610053b2010-05-14 10:05:11 -0700458 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700459 list->head = NULL;
460 list->tail = NULL;
461
462 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700463}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700464
Carl Worth33cc4002010-05-12 12:17:10 -0700465void
Carl Worth610053b2010-05-14 10:05:11 -0700466_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700467{
468 if (list->head == NULL) {
469 list->head = tail->head;
470 } else {
471 list->tail->next = tail->head;
472 }
473
474 list->tail = tail->tail;
475}
476
477void
Carl Worth610053b2010-05-14 10:05:11 -0700478_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700479{
Carl Worth610053b2010-05-14 10:05:11 -0700480 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700481
Carl Worth610053b2010-05-14 10:05:11 -0700482 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700483 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700484
Carl Worth33cc4002010-05-12 12:17:10 -0700485 node->next = NULL;
486
487 if (list->head == NULL) {
488 list->head = node;
489 } else {
490 list->tail->next = node;
491 }
492
493 list->tail = node;
494}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700495
496int
Carl Worth610053b2010-05-14 10:05:11 -0700497_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700498{
Carl Worth610053b2010-05-14 10:05:11 -0700499 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700500 int i;
501
502 if (list == NULL)
503 return 0;
504
505 for (i = 0, node = list->head; node; i++, node = node->next) {
506 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700507 if (index)
508 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700509 return 1;
510 }
511 }
512
513 return 0;
514}
515
516int
Carl Worth610053b2010-05-14 10:05:11 -0700517_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700518{
519 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700520 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700521
522 if (list == NULL)
523 return 0;
524
525 for (node = list->head; node; node = node->next)
526 length++;
527
528 return length;
529}
530
Carl Worth8f6a8282010-05-14 10:44:19 -0700531argument_list_t *
532_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700533{
Carl Worth8f6a8282010-05-14 10:44:19 -0700534 argument_list_t *list;
535
536 list = xtalloc (ctx, argument_list_t);
537 list->head = NULL;
538 list->tail = NULL;
539
540 return list;
541}
542
543void
Carl Worth47252442010-05-19 13:54:37 -0700544_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700545{
546 argument_node_t *node;
547
Carl Worth8f6a8282010-05-14 10:44:19 -0700548 node = xtalloc (list, argument_node_t);
549 node->argument = argument;
550
551 node->next = NULL;
552
553 if (list->head == NULL) {
554 list->head = node;
555 } else {
556 list->tail->next = node;
557 }
558
559 list->tail = node;
560}
561
562int
563_argument_list_length (argument_list_t *list)
564{
565 int length = 0;
566 argument_node_t *node;
567
568 if (list == NULL)
569 return 0;
570
571 for (node = list->head; node; node = node->next)
572 length++;
573
574 return length;
575}
576
Carl Worth47252442010-05-19 13:54:37 -0700577token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700578_argument_list_member_at (argument_list_t *list, int index)
579{
580 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700581 int i;
582
583 if (list == NULL)
584 return NULL;
585
586 node = list->head;
587 for (i = 0; i < index; i++) {
588 node = node->next;
589 if (node == NULL)
590 break;
591 }
592
593 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700594 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700595
596 return NULL;
597}
Carl Worth47252442010-05-19 13:54:37 -0700598
Carl Worth808401f2010-05-25 14:52:43 -0700599/* Note: This function talloc_steal()s the str pointer. */
600token_t *
601_token_create_str (void *ctx, int type, char *str)
602{
603 token_t *token;
604
605 token = xtalloc (ctx, token_t);
606 token->type = type;
607 token->value.str = talloc_steal (token, str);
608
609 return token;
610}
611
612token_t *
613_token_create_ival (void *ctx, int type, int ival)
614{
615 token_t *token;
616
617 token = xtalloc (ctx, token_t);
618 token->type = type;
619 token->value.ival = ival;
620
621 return token;
622}
623
Carl Worth47252442010-05-19 13:54:37 -0700624token_list_t *
625_token_list_create (void *ctx)
626{
627 token_list_t *list;
628
629 list = xtalloc (ctx, token_list_t);
630 list->head = NULL;
631 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700632 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700633
634 return list;
635}
636
637void
Carl Worth808401f2010-05-25 14:52:43 -0700638_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700639{
640 token_node_t *node;
641
642 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700643 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700644
645 node->next = NULL;
646
647 if (list->head == NULL) {
648 list->head = node;
649 } else {
650 list->tail->next = node;
651 }
652
653 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700654 if (token->type != SPACE)
655 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700656}
657
658void
659_token_list_append_list (token_list_t *list, token_list_t *tail)
660{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700661 if (tail == NULL || tail->head == NULL)
662 return;
663
Carl Worth47252442010-05-19 13:54:37 -0700664 if (list->head == NULL) {
665 list->head = tail->head;
666 } else {
667 list->tail->next = tail->head;
668 }
669
670 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700671 list->non_space_tail = tail->non_space_tail;
672}
673
Carl Worth681afbc2010-05-28 15:06:02 -0700674token_list_t *
675_token_list_copy (void *ctx, token_list_t *other)
676{
677 token_list_t *copy;
678 token_node_t *node;
679
680 if (other == NULL)
681 return NULL;
682
683 copy = _token_list_create (ctx);
684 for (node = other->head; node; node = node->next)
685 _token_list_append (copy, node->token);
686
687 return copy;
688}
689
Carl Worth10ae4382010-05-25 20:35:01 -0700690void
691_token_list_trim_trailing_space (token_list_t *list)
692{
693 token_node_t *tail, *next;
694
695 if (list->non_space_tail) {
696 tail = list->non_space_tail->next;
697 list->non_space_tail->next = NULL;
698 list->tail = list->non_space_tail;
699
700 while (tail) {
701 next = tail->next;
702 talloc_free (tail);
703 tail = next;
704 }
705 }
Carl Worth47252442010-05-19 13:54:37 -0700706}
Carl Worth80dc60b2010-05-25 14:42:00 -0700707
Carl Worth22b3ace2010-06-02 15:32:03 -0700708static int
709_token_list_length (token_list_t *list)
710{
711 int length = 0;
712 token_node_t *node;
713
714 if (list == NULL)
715 return 0;
716
717 for (node = list->head; node; node = node->next)
718 length++;
719
720 return length;
721}
722
Carl Worth0197e9b2010-05-26 08:05:19 -0700723static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700724_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700725{
726 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700727 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700728 return;
729 }
730
731 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700732 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700733 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700734 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700735 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700736 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700737 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700738 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700739 break;
740 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700741 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700742 break;
743 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700744 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700745 break;
746 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700747 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700748 break;
749 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700750 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700751 break;
752 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700753 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700754 break;
755 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700756 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700757 break;
758 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700759 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700760 break;
761 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700762 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700763 break;
764 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700765 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700766 break;
767 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700768 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700769 break;
Carl Worthdd749002010-05-27 10:12:33 -0700770 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700771 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700772 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700773 case PLACEHOLDER:
774 /* Nothing to print. */
775 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700776 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700777 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700778 break;
779 }
780}
781
Carl Worthb06096e2010-05-29 05:54:19 -0700782/* Return a new token (talloc()ed off of 'token') formed by pasting
783 * 'token' and 'other'. Note that this function may return 'token' or
784 * 'other' directly rather than allocating anything new.
785 *
786 * Caution: Only very cursory error-checking is performed to see if
787 * the final result is a valid single token. */
788static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700789_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700790{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700791 token_t *combined = NULL;
792
Carl Worth85b50e82010-05-27 14:01:18 -0700793 /* Pasting a placeholder onto anything makes no change. */
794 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700795 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700796
Carl Worthb06096e2010-05-29 05:54:19 -0700797 /* When 'token' is a placeholder, just return 'other'. */
798 if (token->type == PLACEHOLDER)
799 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700800
Carl Worthad0dee62010-05-26 09:04:50 -0700801 /* A very few single-character punctuators can be combined
802 * with another to form a multi-character punctuator. */
803 switch (token->type) {
804 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700805 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700806 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700807 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700808 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700809 break;
810 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700811 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700812 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700813 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700814 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700815 break;
816 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700817 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700818 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700819 break;
820 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700821 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700822 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700823 break;
824 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700825 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700826 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700827 break;
828 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700829 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700830 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700831 break;
832 }
833
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700834 if (combined != NULL) {
835 /* Inherit the location from the first token */
836 combined->location = token->location;
837 return combined;
838 }
839
Carl Worthad0dee62010-05-26 09:04:50 -0700840 /* Two string-valued tokens can usually just be mashed
841 * together.
842 *
Carl Worth050e3de2010-05-27 14:36:29 -0700843 * XXX: This isn't actually legitimate. Several things here
844 * should result in a diagnostic since the result cannot be a
845 * valid, single pre-processing token. For example, pasting
846 * "123" and "abc" is not legal, but we don't catch that
847 * here. */
848 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
849 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700850 {
Carl Worthb06096e2010-05-29 05:54:19 -0700851 char *str;
852
853 str = xtalloc_asprintf (token, "%s%s",
854 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700855 combined = _token_create_str (token, token->type, str);
856 combined->location = token->location;
857 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700858 }
859
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700860 glcpp_error (&token->location, parser, "");
861 glcpp_print (parser->errors, "Pasting \"");
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700862 _token_print (&parser->errors, token);
863 glcpp_print (parser->errors, "\" and \"");
864 _token_print (&parser->errors, other);
865 glcpp_print (parser->errors, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700866
867 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700868}
869
Carl Worth0197e9b2010-05-26 08:05:19 -0700870static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700871_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700872{
873 token_node_t *node;
874
875 if (list == NULL)
876 return;
877
878 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700879 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700880}
881
Carl Worth3a37b872010-05-10 11:44:09 -0700882void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700883yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700884{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700885 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700886}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700887
Carl Worth33cc4002010-05-12 12:17:10 -0700888glcpp_parser_t *
889glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700890{
Carl Worth33cc4002010-05-12 12:17:10 -0700891 glcpp_parser_t *parser;
892
Carl Worth5070a202010-05-12 12:45:33 -0700893 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700894
Carl Worth8f38aff2010-05-19 10:01:29 -0700895 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700896 parser->defines = hash_table_ctor (32, hash_table_string_hash,
897 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700898 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700899 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700900 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700901 parser->newline_as_space = 0;
902 parser->in_control_line = 0;
903 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700904
Carl Worthb20d33c2010-05-20 22:27:07 -0700905 parser->skip_stack = NULL;
906
Carl Worth8e82fcb2010-05-26 11:15:21 -0700907 parser->lex_from_list = NULL;
908 parser->lex_from_node = NULL;
909
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700910 parser->output = talloc_strdup(parser, "");
911 parser->errors = talloc_strdup(parser, "");
912
Carl Worth33cc4002010-05-12 12:17:10 -0700913 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700914}
915
916int
917glcpp_parser_parse (glcpp_parser_t *parser)
918{
919 return yyparse (parser);
920}
921
922void
Carl Worth33cc4002010-05-12 12:17:10 -0700923glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700924{
Carl Worthb20d33c2010-05-20 22:27:07 -0700925 if (parser->skip_stack)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700926 glcpp_print (parser->errors, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700927 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700928 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700929 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700930}
Carl Worthc6d5af32010-05-11 12:30:09 -0700931
Carl Worth8e82fcb2010-05-26 11:15:21 -0700932/* Replace any occurences of DEFINED tokens in 'list' with either a
933 * '0' or '1' INTEGER token depending on whether the next token in the
934 * list is defined or not. */
935static void
936_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
937 token_list_t *list)
938{
939 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700940 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700941
942 if (list == NULL)
943 return;
944
945 for (node = list->head; node; node = node->next) {
946 if (node->token->type != DEFINED)
947 continue;
948 next = node->next;
949 while (next && next->token->type == SPACE)
950 next = next->next;
951 if (next == NULL || next->token->type != IDENTIFIER) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700952 yyerror (&node->token->location, parser, "operator \"defined\" requires an identifier\n");
Carl Worth8e82fcb2010-05-26 11:15:21 -0700953 exit (1);
954 }
955 macro = hash_table_find (parser->defines,
956 next->token->value.str);
957
958 node->token->type = INTEGER;
959 node->token->value.ival = (macro != NULL);
960 node->next = next->next;
961 }
962}
963
Carl Worthb1854fd2010-05-25 16:28:26 -0700964typedef enum function_status
965{
966 FUNCTION_STATUS_SUCCESS,
967 FUNCTION_NOT_A_FUNCTION,
968 FUNCTION_UNBALANCED_PARENTHESES
969} function_status_t;
970
971/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700972 * balanced set of parentheses.
973 *
974 * When called, 'node' should be the opening-parenthesis token, (or
975 * perhaps preceeding SPACE tokens). Upon successful return *last will
976 * be the last consumed node, (corresponding to the closing right
977 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700978 *
979 * Return values:
980 *
981 * FUNCTION_STATUS_SUCCESS:
982 *
983 * Successfully parsed a set of function arguments.
984 *
985 * FUNCTION_NOT_A_FUNCTION:
986 *
987 * Macro name not followed by a '('. This is not an error, but
988 * simply that the macro name should be treated as a non-macro.
989 *
Carl Worth14c98a52010-06-02 15:49:54 -0700990 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -0700991 *
992 * Macro name is not followed by a balanced set of parentheses.
993 */
994static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700995_arguments_parse (argument_list_t *arguments,
996 token_node_t *node,
997 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700998{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700999 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -07001000 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -07001001
Carl Worthb1854fd2010-05-25 16:28:26 -07001002 node = node->next;
1003
1004 /* Ignore whitespace before first parenthesis. */
1005 while (node && node->token->type == SPACE)
1006 node = node->next;
1007
1008 if (node == NULL || node->token->type != '(')
1009 return FUNCTION_NOT_A_FUNCTION;
1010
Carl Worth652fa272010-05-25 17:45:22 -07001011 node = node->next;
1012
Carl Wortha19297b2010-05-27 13:29:19 -07001013 argument = _token_list_create (arguments);
1014 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001015
Carl Worth681afbc2010-05-28 15:06:02 -07001016 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001017 if (node->token->type == '(')
1018 {
1019 paren_count++;
1020 }
1021 else if (node->token->type == ')')
1022 {
1023 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001024 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001025 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001026 }
Carl Worth652fa272010-05-25 17:45:22 -07001027
1028 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001029 paren_count == 1)
1030 {
Carl Wortha19297b2010-05-27 13:29:19 -07001031 _token_list_trim_trailing_space (argument);
1032 argument = _token_list_create (arguments);
1033 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001034 }
1035 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001036 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001037 /* Don't treat initial whitespace as
1038 * part of the arguement. */
1039 if (node->token->type == SPACE)
1040 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001041 }
1042 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001043 }
Carl Worthc7581c22010-05-25 17:41:07 -07001044 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001045
Carl Worth681afbc2010-05-28 15:06:02 -07001046 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001047 return FUNCTION_UNBALANCED_PARENTHESES;
1048
Carl Worth681afbc2010-05-28 15:06:02 -07001049 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001050
1051 return FUNCTION_STATUS_SUCCESS;
1052}
1053
Carl Worth681afbc2010-05-28 15:06:02 -07001054/* This is a helper function that's essentially part of the
1055 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1056 * except for by that function.
1057 *
1058 * Returns NULL if node is a simple token with no expansion, (that is,
1059 * although 'node' corresponds to an identifier defined as a
1060 * function-like macro, it is not followed with a parenthesized
1061 * argument list).
1062 *
1063 * Compute the complete expansion of node (which is a function-like
1064 * macro) and subsequent nodes which are arguments.
1065 *
1066 * Returns the token list that results from the expansion and sets
1067 * *last to the last node in the list that was consumed by the
1068 * expansion. Specificallty, *last will be set as follows: as the
1069 * token of the closing right parenthesis.
1070 */
1071static token_list_t *
1072_glcpp_parser_expand_function (glcpp_parser_t *parser,
1073 token_node_t *node,
1074 token_node_t **last)
1075
Carl Worthb1854fd2010-05-25 16:28:26 -07001076{
1077 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001078 const char *identifier;
1079 argument_list_t *arguments;
1080 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001081 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001082 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001083
Carl Worthb1854fd2010-05-25 16:28:26 -07001084 identifier = node->token->value.str;
1085
1086 macro = hash_table_find (parser->defines, identifier);
1087
1088 assert (macro->is_function);
1089
Carl Worth9ce18cf2010-05-25 17:32:21 -07001090 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001091 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001092
1093 switch (status) {
1094 case FUNCTION_STATUS_SUCCESS:
1095 break;
1096 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001097 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001098 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001099 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth14c98a52010-06-02 15:49:54 -07001100 exit (1);
Carl Worth681afbc2010-05-28 15:06:02 -07001101 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001102 }
1103
Carl Worth9ce18cf2010-05-25 17:32:21 -07001104 if (macro->replacements == NULL) {
1105 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001106 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001107 }
1108
Carl Wortha19297b2010-05-27 13:29:19 -07001109 if (! ((_argument_list_length (arguments) ==
1110 _string_list_length (macro->parameters)) ||
1111 (_string_list_length (macro->parameters) == 0 &&
1112 _argument_list_length (arguments) == 1 &&
1113 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001114 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001115 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001116 "Error: macro %s invoked with %d arguments (expected %d)\n",
1117 identifier,
1118 _argument_list_length (arguments),
1119 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001120 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001121 }
1122
Carl Worth0197e9b2010-05-26 08:05:19 -07001123 /* Perform argument substitution on the replacement list. */
1124 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001125
Carl Worthce540f22010-05-26 08:25:44 -07001126 for (node = macro->replacements->head; node; node = node->next)
1127 {
1128 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001129 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001130 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001131 &parameter_index))
1132 {
1133 token_list_t *argument;
1134 argument = _argument_list_member_at (arguments,
1135 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001136 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001137 * tokens, or append a placeholder token for
1138 * an empty argument. */
1139 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001140 _glcpp_parser_expand_token_list (parser,
1141 argument);
1142 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001143 } else {
1144 token_t *new_token;
1145
1146 new_token = _token_create_ival (substituted,
1147 PLACEHOLDER,
1148 PLACEHOLDER);
1149 _token_list_append (substituted, new_token);
1150 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001151 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001152 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001153 }
1154 }
1155
Carl Worthad0dee62010-05-26 09:04:50 -07001156 /* After argument substitution, and before further expansion
1157 * below, implement token pasting. */
1158
Carl Worthb06096e2010-05-29 05:54:19 -07001159 _token_list_trim_trailing_space (substituted);
1160
Carl Worthad0dee62010-05-26 09:04:50 -07001161 node = substituted->head;
1162 while (node)
1163 {
1164 token_node_t *next_non_space;
1165
1166 /* Look ahead for a PASTE token, skipping space. */
1167 next_non_space = node->next;
1168 while (next_non_space && next_non_space->token->type == SPACE)
1169 next_non_space = next_non_space->next;
1170
1171 if (next_non_space == NULL)
1172 break;
1173
1174 if (next_non_space->token->type != PASTE) {
1175 node = next_non_space;
1176 continue;
1177 }
1178
1179 /* Now find the next non-space token after the PASTE. */
1180 next_non_space = next_non_space->next;
1181 while (next_non_space && next_non_space->token->type == SPACE)
1182 next_non_space = next_non_space->next;
1183
1184 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001185 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001186 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001187 }
1188
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001189 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001190 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001191 if (next_non_space == substituted->tail)
1192 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001193
1194 node = node->next;
1195 }
1196
Carl Worthb06096e2010-05-29 05:54:19 -07001197 substituted->non_space_tail = substituted->tail;
1198
Carl Worth681afbc2010-05-28 15:06:02 -07001199 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001200}
1201
Carl Worth681afbc2010-05-28 15:06:02 -07001202/* Compute the complete expansion of node, (and subsequent nodes after
1203 * 'node' in the case that 'node' is a function-like macro and
1204 * subsequent nodes are arguments).
1205 *
1206 * Returns NULL if node is a simple token with no expansion.
1207 *
1208 * Otherwise, returns the token list that results from the expansion
1209 * and sets *last to the last node in the list that was consumed by
1210 * the expansion. Specificallty, *last will be set as follows:
1211 *
1212 * As 'node' in the case of object-like macro expansion.
1213 *
1214 * As the token of the closing right parenthesis in the case of
1215 * function-like macro expansion.
1216 */
1217static token_list_t *
1218_glcpp_parser_expand_node (glcpp_parser_t *parser,
1219 token_node_t *node,
1220 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001221{
Carl Worth681afbc2010-05-28 15:06:02 -07001222 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001223 const char *identifier;
1224 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001225
1226 /* We only expand identifiers */
1227 if (token->type != IDENTIFIER) {
1228 /* We change any COMMA into a COMMA_FINAL to prevent
1229 * it being mistaken for an argument separator
1230 * later. */
1231 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001232 token->type = COMMA_FINAL;
1233 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001234 }
Carl Worth681afbc2010-05-28 15:06:02 -07001235
1236 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001237 }
1238
1239 /* Look up this identifier in the hash table. */
1240 identifier = token->value.str;
1241 macro = hash_table_find (parser->defines, identifier);
1242
Carl Worth681afbc2010-05-28 15:06:02 -07001243 /* Not a macro, so no expansion needed. */
1244 if (macro == NULL)
1245 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001246
1247 /* Finally, don't expand this macro if we're already actively
1248 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001249 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001250 /* We change the token type here from IDENTIFIER to
1251 * OTHER to prevent any future expansion of this
1252 * unexpanded token. */
1253 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001254 token_list_t *expansion;
1255 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001256
Carl Worth681afbc2010-05-28 15:06:02 -07001257 str = xtalloc_strdup (parser, token->value.str);
1258 final = _token_create_str (parser, OTHER, str);
1259 expansion = _token_list_create (parser);
1260 _token_list_append (expansion, final);
1261 *last = node;
1262 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001263 }
1264
Carl Worth681afbc2010-05-28 15:06:02 -07001265 if (! macro->is_function)
1266 {
1267 *last = node;
1268
1269 if (macro->replacements == NULL)
1270 return _token_list_create (parser);
1271
Carl Worth22b3ace2010-06-02 15:32:03 -07001272 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001273 }
Carl Worth681afbc2010-05-28 15:06:02 -07001274
1275 return _glcpp_parser_expand_function (parser, node, last);
1276}
1277
Carl Worth22b3ace2010-06-02 15:32:03 -07001278/* Push a new identifier onto the active list, returning the new list.
1279 *
1280 * Here, 'marker' is the token node that appears in the list after the
1281 * expansion of 'identifier'. That is, when the list iterator begins
1282 * examinging 'marker', then it is time to pop this node from the
1283 * active stack.
1284 */
1285active_list_t *
1286_active_list_push (active_list_t *list,
1287 const char *identifier,
1288 token_node_t *marker)
1289{
1290 active_list_t *node;
1291
1292 node = xtalloc (list, active_list_t);
1293 node->identifier = xtalloc_strdup (node, identifier);
1294 node->marker = marker;
1295 node->next = list;
1296
1297 return node;
1298}
1299
1300active_list_t *
1301_active_list_pop (active_list_t *list)
1302{
1303 active_list_t *node = list;
1304
1305 if (node == NULL)
1306 return NULL;
1307
1308 node = list->next;
1309 talloc_free (list);
1310
1311 return node;
1312}
1313
1314int
1315_active_list_contains (active_list_t *list, const char *identifier)
1316{
1317 active_list_t *node;
1318
1319 if (list == NULL)
1320 return 0;
1321
1322 for (node = list; node; node = node->next)
1323 if (strcmp (node->identifier, identifier) == 0)
1324 return 1;
1325
1326 return 0;
1327}
1328
Carl Worth681afbc2010-05-28 15:06:02 -07001329/* Walk over the token list replacing nodes with their expansion.
1330 * Whenever nodes are expanded the walking will walk over the new
1331 * nodes, continuing to expand as necessary. The results are placed in
1332 * 'list' itself;
1333 */
1334static void
1335_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1336 token_list_t *list)
1337{
1338 token_node_t *node_prev;
1339 token_node_t *node, *last;
1340 token_list_t *expansion;
1341
1342 if (list == NULL)
1343 return;
1344
1345 _token_list_trim_trailing_space (list);
1346
1347 node_prev = NULL;
1348 node = list->head;
1349
1350 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001351
1352 while (parser->active && parser->active->marker == node)
1353 parser->active = _active_list_pop (parser->active);
1354
Carl Worth681afbc2010-05-28 15:06:02 -07001355 /* Find the expansion for node, which will replace all
1356 * nodes from node to last, inclusive. */
1357 expansion = _glcpp_parser_expand_node (parser, node, &last);
1358 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001359 token_node_t *n;
1360
1361 for (n = node; n != last->next; n = n->next)
1362 while (parser->active &&
1363 parser->active->marker == n)
1364 {
1365 parser->active = _active_list_pop (parser->active);
1366 }
1367
1368 parser->active = _active_list_push (parser->active,
1369 node->token->value.str,
1370 last->next);
1371
Carl Worth681afbc2010-05-28 15:06:02 -07001372 /* Splice expansion into list, supporting a
1373 * simple deletion if the expansion is
1374 * empty. */
1375 if (expansion->head) {
1376 if (node_prev)
1377 node_prev->next = expansion->head;
1378 else
1379 list->head = expansion->head;
1380 expansion->tail->next = last->next;
1381 if (last == list->tail)
1382 list->tail = expansion->tail;
1383 } else {
1384 if (node_prev)
1385 node_prev->next = last->next;
1386 else
1387 list->head = last->next;
1388 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001389 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001390 }
1391 } else {
1392 node_prev = node;
1393 }
1394 node = node_prev ? node_prev->next : list->head;
1395 }
1396
Carl Worth22b3ace2010-06-02 15:32:03 -07001397 while (parser->active)
1398 parser->active = _active_list_pop (parser->active);
1399
Carl Worth681afbc2010-05-28 15:06:02 -07001400 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001401}
1402
Carl Worthae6517f2010-05-25 15:24:59 -07001403void
1404_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1405 token_list_t *list)
1406{
Carl Worthae6517f2010-05-25 15:24:59 -07001407 if (list == NULL)
1408 return;
1409
Carl Worth681afbc2010-05-28 15:06:02 -07001410 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001411
Carl Worth681afbc2010-05-28 15:06:02 -07001412 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001413
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001414 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001415}
1416
1417void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001418_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1419 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001420{
1421 /* According to the GLSL specification, macro names starting with "__"
1422 * or "GL_" are reserved for future use. So, don't allow them.
1423 */
1424 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001425 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001426 exit(1);
1427 }
1428 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001429 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001430 exit(1);
1431 }
1432}
1433
1434void
Carl Worthfcbbb462010-05-13 09:36:23 -07001435_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001436 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001437 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001438 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001439{
1440 macro_t *macro;
1441
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001442 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001443
Carl Worthfcbbb462010-05-13 09:36:23 -07001444 macro = xtalloc (parser, macro_t);
1445
1446 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001447 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001448 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001449 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001450
1451 hash_table_insert (parser->defines, macro, identifier);
1452}
1453
1454void
1455_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001456 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001457 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001458 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001459 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001460{
1461 macro_t *macro;
1462
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001463 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001464
Carl Worthfcbbb462010-05-13 09:36:23 -07001465 macro = xtalloc (parser, macro_t);
1466
1467 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001468 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001469 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001470 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001471
1472 hash_table_insert (parser->defines, macro, identifier);
1473}
1474
Carl Worth8f38aff2010-05-19 10:01:29 -07001475static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001476glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001477{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001478 token_node_t *node;
1479 int ret;
1480
Carl Worth95951ea2010-05-26 15:57:10 -07001481 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001482 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001483
1484 /* XXX: This ugly block of code exists for the sole
1485 * purpose of converting a NEWLINE token into a SPACE
1486 * token, but only in the case where we have seen a
1487 * function-like macro name, but have not yet seen its
1488 * closing parenthesis.
1489 *
1490 * There's perhaps a more compact way to do this with
1491 * mid-rule actions in the grammar.
1492 *
1493 * I'm definitely not pleased with the complexity of
1494 * this code here.
1495 */
1496 if (parser->newline_as_space)
1497 {
1498 if (ret == '(') {
1499 parser->paren_count++;
1500 } else if (ret == ')') {
1501 parser->paren_count--;
1502 if (parser->paren_count == 0)
1503 parser->newline_as_space = 0;
1504 } else if (ret == NEWLINE) {
1505 ret = SPACE;
1506 } else if (ret != SPACE) {
1507 if (parser->paren_count == 0)
1508 parser->newline_as_space = 0;
1509 }
1510 }
1511 else if (parser->in_control_line)
1512 {
1513 if (ret == NEWLINE)
1514 parser->in_control_line = 0;
1515 }
1516 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1517 ret == HASH_UNDEF || ret == HASH_IF ||
1518 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1519 ret == HASH_ELIF || ret == HASH_ELSE ||
1520 ret == HASH_ENDIF || ret == HASH)
1521 {
1522 parser->in_control_line = 1;
1523 }
1524 else if (ret == IDENTIFIER)
1525 {
1526 macro_t *macro;
1527 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001528 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001529 if (macro && macro->is_function) {
1530 parser->newline_as_space = 1;
1531 parser->paren_count = 0;
1532 }
1533 }
1534
1535 return ret;
1536 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001537
1538 node = parser->lex_from_node;
1539
1540 if (node == NULL) {
1541 talloc_free (parser->lex_from_list);
1542 parser->lex_from_list = NULL;
1543 return NEWLINE;
1544 }
1545
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001546 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001547 ret = node->token->type;
1548
1549 parser->lex_from_node = node->next;
1550
1551 return ret;
1552}
1553
1554static void
1555glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1556{
1557 token_node_t *node;
1558
1559 assert (parser->lex_from_list == NULL);
1560
1561 /* Copy list, eliminating any space tokens. */
1562 parser->lex_from_list = _token_list_create (parser);
1563
1564 for (node = list->head; node; node = node->next) {
1565 if (node->token->type == SPACE)
1566 continue;
1567 _token_list_append (parser->lex_from_list, node->token);
1568 }
1569
1570 talloc_free (list);
1571
1572 parser->lex_from_node = parser->lex_from_list->head;
1573
1574 /* It's possible the list consisted of nothing but whitespace. */
1575 if (parser->lex_from_node == NULL) {
1576 talloc_free (parser->lex_from_list);
1577 parser->lex_from_list = NULL;
1578 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001579}
Carl Worthb20d33c2010-05-20 22:27:07 -07001580
1581static void
1582_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1583{
1584 skip_type_t current = SKIP_NO_SKIP;
1585 skip_node_t *node;
1586
1587 if (parser->skip_stack)
1588 current = parser->skip_stack->type;
1589
1590 node = xtalloc (parser, skip_node_t);
1591
1592 if (current == SKIP_NO_SKIP) {
1593 if (condition)
1594 node->type = SKIP_NO_SKIP;
1595 else
1596 node->type = SKIP_TO_ELSE;
1597 } else {
1598 node->type = SKIP_TO_ENDIF;
1599 }
1600
1601 node->next = parser->skip_stack;
1602 parser->skip_stack = node;
1603}
1604
1605static void
1606_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1607 int condition)
1608{
1609 if (parser->skip_stack == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001610 glcpp_printf (parser->errors, "Error: %s without #if\n", type);
Carl Worthb20d33c2010-05-20 22:27:07 -07001611 exit (1);
1612 }
1613
1614 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1615 if (condition)
1616 parser->skip_stack->type = SKIP_NO_SKIP;
1617 } else {
1618 parser->skip_stack->type = SKIP_TO_ENDIF;
1619 }
1620}
Carl Worth80dc60b2010-05-25 14:42:00 -07001621
Carl Worthb20d33c2010-05-20 22:27:07 -07001622static void
1623_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1624{
1625 skip_node_t *node;
1626
1627 if (parser->skip_stack == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001628 glcpp_print (parser->errors, "Error: #endif without #if\n");
Carl Worthb20d33c2010-05-20 22:27:07 -07001629 exit (1);
1630 }
1631
1632 node = parser->skip_stack;
1633 parser->skip_stack = node->next;
1634 talloc_free (node);
1635}