blob: 52927d83c6cc4975f4b63303a511ae6a85b0bbfb [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,
41 const char *macro,
Carl Worth47252442010-05-19 13:54:37 -070042 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070043
Carl Worth5aa7ea02010-05-25 18:39:43 -070044static void
Carl Worthfcbbb462010-05-13 09:36:23 -070045_define_function_macro (glcpp_parser_t *parser,
46 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070047 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070048 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070049
Carl Worth5aa7ea02010-05-25 18:39:43 -070050static string_list_t *
Carl Worth610053b2010-05-14 10:05:11 -070051_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070052
Carl Worth5aa7ea02010-05-25 18:39:43 -070053static void
Carl Worth610053b2010-05-14 10:05:11 -070054_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070055
Carl Worth5aa7ea02010-05-25 18:39:43 -070056static void
Carl Worth610053b2010-05-14 10:05:11 -070057_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070058
Carl Worth5aa7ea02010-05-25 18:39:43 -070059static int
Carl Worth610053b2010-05-14 10:05:11 -070060_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070061
Carl Worth5aa7ea02010-05-25 18:39:43 -070062static int
Carl Worth610053b2010-05-14 10:05:11 -070063_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070064
Carl Worth5aa7ea02010-05-25 18:39:43 -070065static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070066_argument_list_create (void *ctx);
67
Carl Worth5aa7ea02010-05-25 18:39:43 -070068static void
Carl Worth47252442010-05-19 13:54:37 -070069_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070070
Carl Worth5aa7ea02010-05-25 18:39:43 -070071static int
Carl Worth8f6a8282010-05-14 10:44:19 -070072_argument_list_length (argument_list_t *list);
73
Carl Worth5aa7ea02010-05-25 18:39:43 -070074static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070075_argument_list_member_at (argument_list_t *list, int index);
76
Carl Worth808401f2010-05-25 14:52:43 -070077/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070078static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070079_token_create_str (void *ctx, int type, char *str);
80
Carl Worth5aa7ea02010-05-25 18:39:43 -070081static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070082_token_create_ival (void *ctx, int type, int ival);
83
Carl Worth5aa7ea02010-05-25 18:39:43 -070084static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070085_token_list_create (void *ctx);
86
Carl Worthb1ae61a2010-05-26 08:10:38 -070087/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070088 *
89 * You may want to talloc_unlink any current reference if you no
90 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070091static void
Carl Worth808401f2010-05-25 14:52:43 -070092_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070093
Carl Worth5aa7ea02010-05-25 18:39:43 -070094static void
Carl Worth47252442010-05-19 13:54:37 -070095_token_list_append_list (token_list_t *list, token_list_t *tail);
96
Carl Worth22b3ace2010-06-02 15:32:03 -070097static int
98_token_list_length (token_list_t *list);
99
100static active_list_t *
101_active_list_push (active_list_t *list,
102 const char *identifier,
103 token_node_t *marker);
104
105static active_list_t *
106_active_list_pop (active_list_t *list);
107
108int
109_active_list_contains (active_list_t *list, const char *identifier);
110
Carl Worth5aa7ea02010-05-25 18:39:43 -0700111static void
Carl Worth8e82fcb2010-05-26 11:15:21 -0700112_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
113 token_list_t *list);
114
115static void
Carl Worth681afbc2010-05-28 15:06:02 -0700116_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
117 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700118
Carl Worthaaa9acb2010-05-19 13:28:24 -0700119static void
Carl Worth681afbc2010-05-28 15:06:02 -0700120_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
121 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700122
123static void
Carl Worthb20d33c2010-05-20 22:27:07 -0700124_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
125
126static void
127_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
128 int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700129
Carl Worthb20d33c2010-05-20 22:27:07 -0700130static void
131_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
132
Carl Worth0293b2e2010-05-19 10:05:40 -0700133#define yylex glcpp_parser_lex
134
Carl Worth8f38aff2010-05-19 10:01:29 -0700135static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700136glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700137
Carl Worth8e82fcb2010-05-26 11:15:21 -0700138static void
139glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
140
Carl Worth3a37b872010-05-10 11:44:09 -0700141%}
142
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700143%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700144%error-verbose
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700145%locations
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700146
Carl Worth0b27b5f2010-05-10 16:16:06 -0700147%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700148%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700149
Carl Worth050e3de2010-05-27 14:36:29 -0700150%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 -0700151%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700152%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700153%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700154%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700155%type <token> preprocessing_token
156%type <token_list> pp_tokens replacement_list text_line
Carl Worth8fed1cd2010-05-26 09:32:12 -0700157%left OR
158%left AND
159%left '|'
160%left '^'
161%left '&'
162%left EQUAL NOT_EQUAL
163%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
164%left LEFT_SHIFT RIGHT_SHIFT
165%left '+' '-'
166%left '*' '/' '%'
167%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700168
169%%
170
Carl Worth33cc4002010-05-12 12:17:10 -0700171input:
Carl Worth3ff81672010-05-25 13:09:03 -0700172 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700173| input line
174;
175
176line:
177 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700178 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700179 }
Carl Worth808401f2010-05-25 14:52:43 -0700180| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700181 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700182 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700183 talloc_free ($1);
184 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700185| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700186| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700187;
188
Carl Worth8e82fcb2010-05-26 11:15:21 -0700189expanded_line:
190 IF_EXPANDED expression NEWLINE {
191 _glcpp_parser_skip_stack_push_if (parser, $2);
192 }
193| ELIF_EXPANDED expression NEWLINE {
194 _glcpp_parser_skip_stack_change_if (parser, "elif", $2);
195 }
196;
197
Carl Worth3ff81672010-05-25 13:09:03 -0700198control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700199 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Carl Worthae6517f2010-05-25 15:24:59 -0700200 _define_object_macro (parser, $2, $3);
201 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700202| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
203 _define_function_macro (parser, $2, NULL, $5);
204 }
205| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
206 _define_function_macro (parser, $2, $4, $6);
207 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700208| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700209 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700210 if (macro) {
211 /* XXX: Need hash table to support a real way
212 * to remove an element rather than prefixing
213 * a new node with data of NULL like this. */
214 hash_table_insert (parser->defines, NULL, $2);
215 talloc_free (macro);
216 }
217 talloc_free ($2);
218 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700219| HASH_IF pp_tokens NEWLINE {
220 token_list_t *expanded;
221 token_t *token;
222
223 expanded = _token_list_create (parser);
224 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
225 _token_list_append (expanded, token);
226 talloc_unlink (parser, token);
227 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700228 _glcpp_parser_expand_token_list (parser, $2);
229 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700230 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700231 }
232| HASH_IFDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700233 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700234 talloc_free ($2);
235 _glcpp_parser_skip_stack_push_if (parser, macro != NULL);
236 }
237| HASH_IFNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700238 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700239 talloc_free ($2);
240 _glcpp_parser_skip_stack_push_if (parser, macro == NULL);
241 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700242| HASH_ELIF pp_tokens NEWLINE {
243 token_list_t *expanded;
244 token_t *token;
245
246 expanded = _token_list_create (parser);
247 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
248 _token_list_append (expanded, token);
249 talloc_unlink (parser, token);
250 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700251 _glcpp_parser_expand_token_list (parser, $2);
252 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700253 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700254 }
255| HASH_ELSE NEWLINE {
256 _glcpp_parser_skip_stack_change_if (parser, "else", 1);
257 }
258| HASH_ENDIF NEWLINE {
259 _glcpp_parser_skip_stack_pop (parser);
260 }
Carl Worth3ff81672010-05-25 13:09:03 -0700261| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700262;
263
Carl Worth8fed1cd2010-05-26 09:32:12 -0700264expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700265 INTEGER_STRING {
266 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
267 $$ = strtoll ($1 + 2, NULL, 16);
268 } else if ($1[0] == '0') {
269 $$ = strtoll ($1, NULL, 8);
270 } else {
271 $$ = strtoll ($1, NULL, 10);
272 }
273 }
274| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700275 $$ = $1;
276 }
277| expression OR expression {
278 $$ = $1 || $3;
279 }
280| expression AND expression {
281 $$ = $1 && $3;
282 }
283| expression '|' expression {
284 $$ = $1 | $3;
285 }
286| expression '^' expression {
287 $$ = $1 ^ $3;
288 }
289| expression '&' expression {
290 $$ = $1 & $3;
291 }
292| expression NOT_EQUAL expression {
293 $$ = $1 != $3;
294 }
295| expression EQUAL expression {
296 $$ = $1 == $3;
297 }
298| expression GREATER_OR_EQUAL expression {
299 $$ = $1 >= $3;
300 }
301| expression LESS_OR_EQUAL expression {
302 $$ = $1 <= $3;
303 }
304| expression '>' expression {
305 $$ = $1 > $3;
306 }
307| expression '<' expression {
308 $$ = $1 < $3;
309 }
310| expression RIGHT_SHIFT expression {
311 $$ = $1 >> $3;
312 }
313| expression LEFT_SHIFT expression {
314 $$ = $1 << $3;
315 }
316| expression '-' 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 %prec UNARY {
332 $$ = ! $2;
333 }
334| '~' expression %prec UNARY {
335 $$ = ~ $2;
336 }
337| '-' expression %prec UNARY {
338 $$ = - $2;
339 }
340| '+' expression %prec UNARY {
341 $$ = + $2;
342 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700343| '(' expression ')' {
344 $$ = $2;
345 }
346;
347
Carl Worth3ff81672010-05-25 13:09:03 -0700348identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700349 IDENTIFIER {
350 $$ = _string_list_create (parser);
351 _string_list_append_item ($$, $1);
352 talloc_steal ($$, $1);
353 }
354| identifier_list ',' IDENTIFIER {
355 $$ = $1;
356 _string_list_append_item ($$, $3);
357 talloc_steal ($$, $3);
358 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700359;
360
Carl Worth3ff81672010-05-25 13:09:03 -0700361text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700362 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700363| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700364;
365
Carl Worth3ff81672010-05-25 13:09:03 -0700366non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700367 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700368 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700369 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700370;
371
Carl Worthaaa9acb2010-05-19 13:28:24 -0700372replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700373 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700374| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700375;
376
Carl Worthaaa9acb2010-05-19 13:28:24 -0700377pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700378 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700379 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700380 $$ = _token_list_create (parser);
381 _token_list_append ($$, $1);
382 talloc_unlink (parser, $1);
383 }
384| pp_tokens preprocessing_token {
385 $$ = $1;
386 _token_list_append ($$, $2);
387 talloc_unlink (parser, $2);
388 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700389;
390
Carl Worth3ff81672010-05-25 13:09:03 -0700391preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700392 IDENTIFIER {
393 $$ = _token_create_str (parser, IDENTIFIER, $1);
394 }
Carl Worth050e3de2010-05-27 14:36:29 -0700395| INTEGER_STRING {
396 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700397 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700398| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700399 $$ = _token_create_ival (parser, $1, $1);
400 }
401| OTHER {
402 $$ = _token_create_str (parser, OTHER, $1);
403 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700404| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700405 $$ = _token_create_ival (parser, SPACE, SPACE);
Carl Worthb1854fd2010-05-25 16:28:26 -0700406 }
Carl Worth3ff81672010-05-25 13:09:03 -0700407;
408
Carl Worth8e82fcb2010-05-26 11:15:21 -0700409operator:
Carl Worth808401f2010-05-25 14:52:43 -0700410 '[' { $$ = '['; }
411| ']' { $$ = ']'; }
412| '(' { $$ = '('; }
413| ')' { $$ = ')'; }
414| '{' { $$ = '{'; }
415| '}' { $$ = '}'; }
416| '.' { $$ = '.'; }
417| '&' { $$ = '&'; }
418| '*' { $$ = '*'; }
419| '+' { $$ = '+'; }
420| '-' { $$ = '-'; }
421| '~' { $$ = '~'; }
422| '!' { $$ = '!'; }
423| '/' { $$ = '/'; }
424| '%' { $$ = '%'; }
425| LEFT_SHIFT { $$ = LEFT_SHIFT; }
426| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
427| '<' { $$ = '<'; }
428| '>' { $$ = '>'; }
429| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
430| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
431| EQUAL { $$ = EQUAL; }
432| NOT_EQUAL { $$ = NOT_EQUAL; }
433| '^' { $$ = '^'; }
434| '|' { $$ = '|'; }
435| AND { $$ = AND; }
436| OR { $$ = OR; }
437| ';' { $$ = ';'; }
438| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700439| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700440| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700441| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700442;
443
Carl Worth33cc4002010-05-12 12:17:10 -0700444%%
445
Carl Worth610053b2010-05-14 10:05:11 -0700446string_list_t *
447_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700448{
Carl Worth610053b2010-05-14 10:05:11 -0700449 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700450
Carl Worth610053b2010-05-14 10:05:11 -0700451 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700452 list->head = NULL;
453 list->tail = NULL;
454
455 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700456}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700457
Carl Worth33cc4002010-05-12 12:17:10 -0700458void
Carl Worth610053b2010-05-14 10:05:11 -0700459_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700460{
461 if (list->head == NULL) {
462 list->head = tail->head;
463 } else {
464 list->tail->next = tail->head;
465 }
466
467 list->tail = tail->tail;
468}
469
470void
Carl Worth610053b2010-05-14 10:05:11 -0700471_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700472{
Carl Worth610053b2010-05-14 10:05:11 -0700473 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700474
Carl Worth610053b2010-05-14 10:05:11 -0700475 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700476 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700477
Carl Worth33cc4002010-05-12 12:17:10 -0700478 node->next = NULL;
479
480 if (list->head == NULL) {
481 list->head = node;
482 } else {
483 list->tail->next = node;
484 }
485
486 list->tail = node;
487}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700488
489int
Carl Worth610053b2010-05-14 10:05:11 -0700490_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700491{
Carl Worth610053b2010-05-14 10:05:11 -0700492 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700493 int i;
494
495 if (list == NULL)
496 return 0;
497
498 for (i = 0, node = list->head; node; i++, node = node->next) {
499 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700500 if (index)
501 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700502 return 1;
503 }
504 }
505
506 return 0;
507}
508
509int
Carl Worth610053b2010-05-14 10:05:11 -0700510_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700511{
512 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700513 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700514
515 if (list == NULL)
516 return 0;
517
518 for (node = list->head; node; node = node->next)
519 length++;
520
521 return length;
522}
523
Carl Worth8f6a8282010-05-14 10:44:19 -0700524argument_list_t *
525_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700526{
Carl Worth8f6a8282010-05-14 10:44:19 -0700527 argument_list_t *list;
528
529 list = xtalloc (ctx, argument_list_t);
530 list->head = NULL;
531 list->tail = NULL;
532
533 return list;
534}
535
536void
Carl Worth47252442010-05-19 13:54:37 -0700537_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700538{
539 argument_node_t *node;
540
Carl Worth8f6a8282010-05-14 10:44:19 -0700541 node = xtalloc (list, argument_node_t);
542 node->argument = argument;
543
544 node->next = NULL;
545
546 if (list->head == NULL) {
547 list->head = node;
548 } else {
549 list->tail->next = node;
550 }
551
552 list->tail = node;
553}
554
555int
556_argument_list_length (argument_list_t *list)
557{
558 int length = 0;
559 argument_node_t *node;
560
561 if (list == NULL)
562 return 0;
563
564 for (node = list->head; node; node = node->next)
565 length++;
566
567 return length;
568}
569
Carl Worth47252442010-05-19 13:54:37 -0700570token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700571_argument_list_member_at (argument_list_t *list, int index)
572{
573 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700574 int i;
575
576 if (list == NULL)
577 return NULL;
578
579 node = list->head;
580 for (i = 0; i < index; i++) {
581 node = node->next;
582 if (node == NULL)
583 break;
584 }
585
586 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700587 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700588
589 return NULL;
590}
Carl Worth47252442010-05-19 13:54:37 -0700591
Carl Worth808401f2010-05-25 14:52:43 -0700592/* Note: This function talloc_steal()s the str pointer. */
593token_t *
594_token_create_str (void *ctx, int type, char *str)
595{
596 token_t *token;
597
598 token = xtalloc (ctx, token_t);
599 token->type = type;
600 token->value.str = talloc_steal (token, str);
601
602 return token;
603}
604
605token_t *
606_token_create_ival (void *ctx, int type, int ival)
607{
608 token_t *token;
609
610 token = xtalloc (ctx, token_t);
611 token->type = type;
612 token->value.ival = ival;
613
614 return token;
615}
616
Carl Worth47252442010-05-19 13:54:37 -0700617token_list_t *
618_token_list_create (void *ctx)
619{
620 token_list_t *list;
621
622 list = xtalloc (ctx, token_list_t);
623 list->head = NULL;
624 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700625 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700626
627 return list;
628}
629
630void
Carl Worth808401f2010-05-25 14:52:43 -0700631_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700632{
633 token_node_t *node;
634
635 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700636 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700637
638 node->next = NULL;
639
640 if (list->head == NULL) {
641 list->head = node;
642 } else {
643 list->tail->next = node;
644 }
645
646 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700647 if (token->type != SPACE)
648 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700649}
650
651void
652_token_list_append_list (token_list_t *list, token_list_t *tail)
653{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700654 if (tail == NULL || tail->head == NULL)
655 return;
656
Carl Worth47252442010-05-19 13:54:37 -0700657 if (list->head == NULL) {
658 list->head = tail->head;
659 } else {
660 list->tail->next = tail->head;
661 }
662
663 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700664 list->non_space_tail = tail->non_space_tail;
665}
666
Carl Worth681afbc2010-05-28 15:06:02 -0700667token_list_t *
668_token_list_copy (void *ctx, token_list_t *other)
669{
670 token_list_t *copy;
671 token_node_t *node;
672
673 if (other == NULL)
674 return NULL;
675
676 copy = _token_list_create (ctx);
677 for (node = other->head; node; node = node->next)
678 _token_list_append (copy, node->token);
679
680 return copy;
681}
682
Carl Worth10ae4382010-05-25 20:35:01 -0700683void
684_token_list_trim_trailing_space (token_list_t *list)
685{
686 token_node_t *tail, *next;
687
688 if (list->non_space_tail) {
689 tail = list->non_space_tail->next;
690 list->non_space_tail->next = NULL;
691 list->tail = list->non_space_tail;
692
693 while (tail) {
694 next = tail->next;
695 talloc_free (tail);
696 tail = next;
697 }
698 }
Carl Worth47252442010-05-19 13:54:37 -0700699}
Carl Worth80dc60b2010-05-25 14:42:00 -0700700
Carl Worth22b3ace2010-06-02 15:32:03 -0700701static int
702_token_list_length (token_list_t *list)
703{
704 int length = 0;
705 token_node_t *node;
706
707 if (list == NULL)
708 return 0;
709
710 for (node = list->head; node; node = node->next)
711 length++;
712
713 return length;
714}
715
Carl Worth0197e9b2010-05-26 08:05:19 -0700716static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700717_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700718{
719 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700720 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700721 return;
722 }
723
724 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700725 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700726 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700727 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700728 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700729 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700730 case OTHER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700731 glcpp_printf (*out, "%s", token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700732 break;
733 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700734 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700735 break;
736 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700737 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700738 break;
739 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700740 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700741 break;
742 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700743 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700744 break;
745 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700746 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700747 break;
748 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700749 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700750 break;
751 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700752 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700753 break;
754 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700755 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700756 break;
757 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700758 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700759 break;
760 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700761 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700762 break;
Carl Worthdd749002010-05-27 10:12:33 -0700763 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700764 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700765 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700766 case PLACEHOLDER:
767 /* Nothing to print. */
768 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700769 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700770 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700771 break;
772 }
773}
774
Carl Worthb06096e2010-05-29 05:54:19 -0700775/* Return a new token (talloc()ed off of 'token') formed by pasting
776 * 'token' and 'other'. Note that this function may return 'token' or
777 * 'other' directly rather than allocating anything new.
778 *
779 * Caution: Only very cursory error-checking is performed to see if
780 * the final result is a valid single token. */
781static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700782_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700783{
Carl Worth85b50e82010-05-27 14:01:18 -0700784 /* Pasting a placeholder onto anything makes no change. */
785 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700786 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700787
Carl Worthb06096e2010-05-29 05:54:19 -0700788 /* When 'token' is a placeholder, just return 'other'. */
789 if (token->type == PLACEHOLDER)
790 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700791
Carl Worthad0dee62010-05-26 09:04:50 -0700792 /* A very few single-character punctuators can be combined
793 * with another to form a multi-character punctuator. */
794 switch (token->type) {
795 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700796 if (other->type == '<')
797 return _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
798 else if (other->type == '=')
799 return _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700800 break;
801 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700802 if (other->type == '>')
803 return _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
804 else if (other->type == '=')
805 return _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700806 break;
807 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700808 if (other->type == '=')
809 return _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700810 break;
811 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700812 if (other->type == '=')
813 return _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700814 break;
815 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700816 if (other->type == '&')
817 return _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700818 break;
819 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700820 if (other->type == '|')
821 return _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700822 break;
823 }
824
825 /* Two string-valued tokens can usually just be mashed
826 * together.
827 *
Carl Worth050e3de2010-05-27 14:36:29 -0700828 * XXX: This isn't actually legitimate. Several things here
829 * should result in a diagnostic since the result cannot be a
830 * valid, single pre-processing token. For example, pasting
831 * "123" and "abc" is not legal, but we don't catch that
832 * here. */
833 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
834 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700835 {
Carl Worthb06096e2010-05-29 05:54:19 -0700836 char *str;
837
838 str = xtalloc_asprintf (token, "%s%s",
839 token->value.str, other->value.str);
840 return _token_create_str (token, token->type, str);
Carl Worthad0dee62010-05-26 09:04:50 -0700841 }
842
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700843 glcpp_print (parser->errors, "Error: Pasting \"");
844 _token_print (&parser->errors, token);
845 glcpp_print (parser->errors, "\" and \"");
846 _token_print (&parser->errors, other);
847 glcpp_print (parser->errors, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700848
849 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700850}
851
Carl Worth0197e9b2010-05-26 08:05:19 -0700852static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700853_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700854{
855 token_node_t *node;
856
857 if (list == NULL)
858 return;
859
860 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700861 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700862}
863
Carl Worth3a37b872010-05-10 11:44:09 -0700864void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700865yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700866{
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700867 glcpp_printf(parser->errors, "%u:%u(%u): preprocessor error: %s\n",
868 locp->source, locp->first_line, locp->first_column, error);
Carl Worth3a37b872010-05-10 11:44:09 -0700869}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700870
Carl Worth33cc4002010-05-12 12:17:10 -0700871glcpp_parser_t *
872glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700873{
Carl Worth33cc4002010-05-12 12:17:10 -0700874 glcpp_parser_t *parser;
875
Carl Worth5070a202010-05-12 12:45:33 -0700876 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700877
Carl Worth8f38aff2010-05-19 10:01:29 -0700878 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700879 parser->defines = hash_table_ctor (32, hash_table_string_hash,
880 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700881 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700882 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700883 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700884 parser->newline_as_space = 0;
885 parser->in_control_line = 0;
886 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700887
Carl Worthb20d33c2010-05-20 22:27:07 -0700888 parser->skip_stack = NULL;
889
Carl Worth8e82fcb2010-05-26 11:15:21 -0700890 parser->lex_from_list = NULL;
891 parser->lex_from_node = NULL;
892
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700893 parser->output = talloc_strdup(parser, "");
894 parser->errors = talloc_strdup(parser, "");
895
Carl Worth33cc4002010-05-12 12:17:10 -0700896 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700897}
898
899int
900glcpp_parser_parse (glcpp_parser_t *parser)
901{
902 return yyparse (parser);
903}
904
905void
Carl Worth33cc4002010-05-12 12:17:10 -0700906glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700907{
Carl Worthb20d33c2010-05-20 22:27:07 -0700908 if (parser->skip_stack)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700909 glcpp_print (parser->errors, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700910 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700911 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700912 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700913}
Carl Worthc6d5af32010-05-11 12:30:09 -0700914
Carl Worth8e82fcb2010-05-26 11:15:21 -0700915/* Replace any occurences of DEFINED tokens in 'list' with either a
916 * '0' or '1' INTEGER token depending on whether the next token in the
917 * list is defined or not. */
918static void
919_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
920 token_list_t *list)
921{
922 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700923 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700924
925 if (list == NULL)
926 return;
927
928 for (node = list->head; node; node = node->next) {
929 if (node->token->type != DEFINED)
930 continue;
931 next = node->next;
932 while (next && next->token->type == SPACE)
933 next = next->next;
934 if (next == NULL || next->token->type != IDENTIFIER) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700935 glcpp_print (parser->errors, "Error: operator \"defined\" requires an identifier\n");
Carl Worth8e82fcb2010-05-26 11:15:21 -0700936 exit (1);
937 }
938 macro = hash_table_find (parser->defines,
939 next->token->value.str);
940
941 node->token->type = INTEGER;
942 node->token->value.ival = (macro != NULL);
943 node->next = next->next;
944 }
945}
946
Carl Worthb1854fd2010-05-25 16:28:26 -0700947typedef enum function_status
948{
949 FUNCTION_STATUS_SUCCESS,
950 FUNCTION_NOT_A_FUNCTION,
951 FUNCTION_UNBALANCED_PARENTHESES
952} function_status_t;
953
954/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700955 * balanced set of parentheses.
956 *
957 * When called, 'node' should be the opening-parenthesis token, (or
958 * perhaps preceeding SPACE tokens). Upon successful return *last will
959 * be the last consumed node, (corresponding to the closing right
960 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700961 *
962 * Return values:
963 *
964 * FUNCTION_STATUS_SUCCESS:
965 *
966 * Successfully parsed a set of function arguments.
967 *
968 * FUNCTION_NOT_A_FUNCTION:
969 *
970 * Macro name not followed by a '('. This is not an error, but
971 * simply that the macro name should be treated as a non-macro.
972 *
Carl Worth14c98a52010-06-02 15:49:54 -0700973 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -0700974 *
975 * Macro name is not followed by a balanced set of parentheses.
976 */
977static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700978_arguments_parse (argument_list_t *arguments,
979 token_node_t *node,
980 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700981{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700982 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700983 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700984
Carl Worthb1854fd2010-05-25 16:28:26 -0700985 node = node->next;
986
987 /* Ignore whitespace before first parenthesis. */
988 while (node && node->token->type == SPACE)
989 node = node->next;
990
991 if (node == NULL || node->token->type != '(')
992 return FUNCTION_NOT_A_FUNCTION;
993
Carl Worth652fa272010-05-25 17:45:22 -0700994 node = node->next;
995
Carl Wortha19297b2010-05-27 13:29:19 -0700996 argument = _token_list_create (arguments);
997 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -0700998
Carl Worth681afbc2010-05-28 15:06:02 -0700999 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001000 if (node->token->type == '(')
1001 {
1002 paren_count++;
1003 }
1004 else if (node->token->type == ')')
1005 {
1006 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001007 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001008 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001009 }
Carl Worth652fa272010-05-25 17:45:22 -07001010
1011 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001012 paren_count == 1)
1013 {
Carl Wortha19297b2010-05-27 13:29:19 -07001014 _token_list_trim_trailing_space (argument);
1015 argument = _token_list_create (arguments);
1016 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001017 }
1018 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001019 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001020 /* Don't treat initial whitespace as
1021 * part of the arguement. */
1022 if (node->token->type == SPACE)
1023 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001024 }
1025 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001026 }
Carl Worthc7581c22010-05-25 17:41:07 -07001027 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001028
Carl Worth681afbc2010-05-28 15:06:02 -07001029 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001030 return FUNCTION_UNBALANCED_PARENTHESES;
1031
Carl Worth681afbc2010-05-28 15:06:02 -07001032 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001033
1034 return FUNCTION_STATUS_SUCCESS;
1035}
1036
Carl Worth681afbc2010-05-28 15:06:02 -07001037/* This is a helper function that's essentially part of the
1038 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1039 * except for by that function.
1040 *
1041 * Returns NULL if node is a simple token with no expansion, (that is,
1042 * although 'node' corresponds to an identifier defined as a
1043 * function-like macro, it is not followed with a parenthesized
1044 * argument list).
1045 *
1046 * Compute the complete expansion of node (which is a function-like
1047 * macro) and subsequent nodes which are arguments.
1048 *
1049 * Returns the token list that results from the expansion and sets
1050 * *last to the last node in the list that was consumed by the
1051 * expansion. Specificallty, *last will be set as follows: as the
1052 * token of the closing right parenthesis.
1053 */
1054static token_list_t *
1055_glcpp_parser_expand_function (glcpp_parser_t *parser,
1056 token_node_t *node,
1057 token_node_t **last)
1058
Carl Worthb1854fd2010-05-25 16:28:26 -07001059{
1060 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001061 const char *identifier;
1062 argument_list_t *arguments;
1063 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001064 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001065 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001066
Carl Worthb1854fd2010-05-25 16:28:26 -07001067 identifier = node->token->value.str;
1068
1069 macro = hash_table_find (parser->defines, identifier);
1070
1071 assert (macro->is_function);
1072
Carl Worth9ce18cf2010-05-25 17:32:21 -07001073 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001074 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001075
1076 switch (status) {
1077 case FUNCTION_STATUS_SUCCESS:
1078 break;
1079 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001080 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001081 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001082 glcpp_printf (parser->errors, "Error: Macro %s call has unbalanced parentheses\n",
1083 identifier);
Carl Worth14c98a52010-06-02 15:49:54 -07001084 exit (1);
Carl Worth681afbc2010-05-28 15:06:02 -07001085 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001086 }
1087
Carl Worth9ce18cf2010-05-25 17:32:21 -07001088 if (macro->replacements == NULL) {
1089 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001090 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001091 }
1092
Carl Wortha19297b2010-05-27 13:29:19 -07001093 if (! ((_argument_list_length (arguments) ==
1094 _string_list_length (macro->parameters)) ||
1095 (_string_list_length (macro->parameters) == 0 &&
1096 _argument_list_length (arguments) == 1 &&
1097 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001098 {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001099 glcpp_printf (parser->errors,
1100 "Error: macro %s invoked with %d arguments (expected %d)\n",
1101 identifier,
1102 _argument_list_length (arguments),
1103 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001104 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001105 }
1106
Carl Worth0197e9b2010-05-26 08:05:19 -07001107 /* Perform argument substitution on the replacement list. */
1108 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001109
Carl Worthce540f22010-05-26 08:25:44 -07001110 for (node = macro->replacements->head; node; node = node->next)
1111 {
1112 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001113 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001114 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001115 &parameter_index))
1116 {
1117 token_list_t *argument;
1118 argument = _argument_list_member_at (arguments,
1119 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001120 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001121 * tokens, or append a placeholder token for
1122 * an empty argument. */
1123 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001124 _glcpp_parser_expand_token_list (parser,
1125 argument);
1126 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001127 } else {
1128 token_t *new_token;
1129
1130 new_token = _token_create_ival (substituted,
1131 PLACEHOLDER,
1132 PLACEHOLDER);
1133 _token_list_append (substituted, new_token);
1134 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001135 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001136 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001137 }
1138 }
1139
Carl Worthad0dee62010-05-26 09:04:50 -07001140 /* After argument substitution, and before further expansion
1141 * below, implement token pasting. */
1142
Carl Worthb06096e2010-05-29 05:54:19 -07001143 _token_list_trim_trailing_space (substituted);
1144
Carl Worthad0dee62010-05-26 09:04:50 -07001145 node = substituted->head;
1146 while (node)
1147 {
1148 token_node_t *next_non_space;
1149
1150 /* Look ahead for a PASTE token, skipping space. */
1151 next_non_space = node->next;
1152 while (next_non_space && next_non_space->token->type == SPACE)
1153 next_non_space = next_non_space->next;
1154
1155 if (next_non_space == NULL)
1156 break;
1157
1158 if (next_non_space->token->type != PASTE) {
1159 node = next_non_space;
1160 continue;
1161 }
1162
1163 /* Now find the next non-space token after the PASTE. */
1164 next_non_space = next_non_space->next;
1165 while (next_non_space && next_non_space->token->type == SPACE)
1166 next_non_space = next_non_space->next;
1167
1168 if (next_non_space == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001169 glcpp_print (parser->errors, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001170 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001171 }
1172
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001173 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001174 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001175 if (next_non_space == substituted->tail)
1176 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001177
1178 node = node->next;
1179 }
1180
Carl Worthb06096e2010-05-29 05:54:19 -07001181 substituted->non_space_tail = substituted->tail;
1182
Carl Worth681afbc2010-05-28 15:06:02 -07001183 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001184}
1185
Carl Worth681afbc2010-05-28 15:06:02 -07001186/* Compute the complete expansion of node, (and subsequent nodes after
1187 * 'node' in the case that 'node' is a function-like macro and
1188 * subsequent nodes are arguments).
1189 *
1190 * Returns NULL if node is a simple token with no expansion.
1191 *
1192 * Otherwise, returns the token list that results from the expansion
1193 * and sets *last to the last node in the list that was consumed by
1194 * the expansion. Specificallty, *last will be set as follows:
1195 *
1196 * As 'node' in the case of object-like macro expansion.
1197 *
1198 * As the token of the closing right parenthesis in the case of
1199 * function-like macro expansion.
1200 */
1201static token_list_t *
1202_glcpp_parser_expand_node (glcpp_parser_t *parser,
1203 token_node_t *node,
1204 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001205{
Carl Worth681afbc2010-05-28 15:06:02 -07001206 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001207 const char *identifier;
1208 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001209
1210 /* We only expand identifiers */
1211 if (token->type != IDENTIFIER) {
1212 /* We change any COMMA into a COMMA_FINAL to prevent
1213 * it being mistaken for an argument separator
1214 * later. */
1215 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001216 token->type = COMMA_FINAL;
1217 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001218 }
Carl Worth681afbc2010-05-28 15:06:02 -07001219
1220 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001221 }
1222
1223 /* Look up this identifier in the hash table. */
1224 identifier = token->value.str;
1225 macro = hash_table_find (parser->defines, identifier);
1226
Carl Worth681afbc2010-05-28 15:06:02 -07001227 /* Not a macro, so no expansion needed. */
1228 if (macro == NULL)
1229 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001230
1231 /* Finally, don't expand this macro if we're already actively
1232 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001233 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001234 /* We change the token type here from IDENTIFIER to
1235 * OTHER to prevent any future expansion of this
1236 * unexpanded token. */
1237 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001238 token_list_t *expansion;
1239 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001240
Carl Worth681afbc2010-05-28 15:06:02 -07001241 str = xtalloc_strdup (parser, token->value.str);
1242 final = _token_create_str (parser, OTHER, str);
1243 expansion = _token_list_create (parser);
1244 _token_list_append (expansion, final);
1245 *last = node;
1246 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001247 }
1248
Carl Worth681afbc2010-05-28 15:06:02 -07001249 if (! macro->is_function)
1250 {
1251 *last = node;
1252
1253 if (macro->replacements == NULL)
1254 return _token_list_create (parser);
1255
Carl Worth22b3ace2010-06-02 15:32:03 -07001256 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001257 }
Carl Worth681afbc2010-05-28 15:06:02 -07001258
1259 return _glcpp_parser_expand_function (parser, node, last);
1260}
1261
Carl Worth22b3ace2010-06-02 15:32:03 -07001262/* Push a new identifier onto the active list, returning the new list.
1263 *
1264 * Here, 'marker' is the token node that appears in the list after the
1265 * expansion of 'identifier'. That is, when the list iterator begins
1266 * examinging 'marker', then it is time to pop this node from the
1267 * active stack.
1268 */
1269active_list_t *
1270_active_list_push (active_list_t *list,
1271 const char *identifier,
1272 token_node_t *marker)
1273{
1274 active_list_t *node;
1275
1276 node = xtalloc (list, active_list_t);
1277 node->identifier = xtalloc_strdup (node, identifier);
1278 node->marker = marker;
1279 node->next = list;
1280
1281 return node;
1282}
1283
1284active_list_t *
1285_active_list_pop (active_list_t *list)
1286{
1287 active_list_t *node = list;
1288
1289 if (node == NULL)
1290 return NULL;
1291
1292 node = list->next;
1293 talloc_free (list);
1294
1295 return node;
1296}
1297
1298int
1299_active_list_contains (active_list_t *list, const char *identifier)
1300{
1301 active_list_t *node;
1302
1303 if (list == NULL)
1304 return 0;
1305
1306 for (node = list; node; node = node->next)
1307 if (strcmp (node->identifier, identifier) == 0)
1308 return 1;
1309
1310 return 0;
1311}
1312
Carl Worth681afbc2010-05-28 15:06:02 -07001313/* Walk over the token list replacing nodes with their expansion.
1314 * Whenever nodes are expanded the walking will walk over the new
1315 * nodes, continuing to expand as necessary. The results are placed in
1316 * 'list' itself;
1317 */
1318static void
1319_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1320 token_list_t *list)
1321{
1322 token_node_t *node_prev;
1323 token_node_t *node, *last;
1324 token_list_t *expansion;
1325
1326 if (list == NULL)
1327 return;
1328
1329 _token_list_trim_trailing_space (list);
1330
1331 node_prev = NULL;
1332 node = list->head;
1333
1334 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001335
1336 while (parser->active && parser->active->marker == node)
1337 parser->active = _active_list_pop (parser->active);
1338
Carl Worth681afbc2010-05-28 15:06:02 -07001339 /* Find the expansion for node, which will replace all
1340 * nodes from node to last, inclusive. */
1341 expansion = _glcpp_parser_expand_node (parser, node, &last);
1342 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001343 token_node_t *n;
1344
1345 for (n = node; n != last->next; n = n->next)
1346 while (parser->active &&
1347 parser->active->marker == n)
1348 {
1349 parser->active = _active_list_pop (parser->active);
1350 }
1351
1352 parser->active = _active_list_push (parser->active,
1353 node->token->value.str,
1354 last->next);
1355
Carl Worth681afbc2010-05-28 15:06:02 -07001356 /* Splice expansion into list, supporting a
1357 * simple deletion if the expansion is
1358 * empty. */
1359 if (expansion->head) {
1360 if (node_prev)
1361 node_prev->next = expansion->head;
1362 else
1363 list->head = expansion->head;
1364 expansion->tail->next = last->next;
1365 if (last == list->tail)
1366 list->tail = expansion->tail;
1367 } else {
1368 if (node_prev)
1369 node_prev->next = last->next;
1370 else
1371 list->head = last->next;
1372 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001373 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001374 }
1375 } else {
1376 node_prev = node;
1377 }
1378 node = node_prev ? node_prev->next : list->head;
1379 }
1380
Carl Worth22b3ace2010-06-02 15:32:03 -07001381 while (parser->active)
1382 parser->active = _active_list_pop (parser->active);
1383
Carl Worth681afbc2010-05-28 15:06:02 -07001384 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001385}
1386
Carl Worthae6517f2010-05-25 15:24:59 -07001387void
1388_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1389 token_list_t *list)
1390{
Carl Worthae6517f2010-05-25 15:24:59 -07001391 if (list == NULL)
1392 return;
1393
Carl Worth681afbc2010-05-28 15:06:02 -07001394 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001395
Carl Worth681afbc2010-05-28 15:06:02 -07001396 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001397
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001398 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001399}
1400
1401void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001402_check_for_reserved_macro_name (glcpp_parser_t *parser, const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001403{
1404 /* According to the GLSL specification, macro names starting with "__"
1405 * or "GL_" are reserved for future use. So, don't allow them.
1406 */
1407 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001408 glcpp_print (parser->errors, "Error: Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001409 exit(1);
1410 }
1411 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001412 glcpp_print (parser->errors, "Error: Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001413 exit(1);
1414 }
1415}
1416
1417void
Carl Worthfcbbb462010-05-13 09:36:23 -07001418_define_object_macro (glcpp_parser_t *parser,
1419 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001420 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001421{
1422 macro_t *macro;
1423
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001424 _check_for_reserved_macro_name(parser, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001425
Carl Worthfcbbb462010-05-13 09:36:23 -07001426 macro = xtalloc (parser, macro_t);
1427
1428 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001429 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001430 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001431 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001432
1433 hash_table_insert (parser->defines, macro, identifier);
1434}
1435
1436void
1437_define_function_macro (glcpp_parser_t *parser,
1438 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001439 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001440 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001441{
1442 macro_t *macro;
1443
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001444 _check_for_reserved_macro_name(parser, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001445
Carl Worthfcbbb462010-05-13 09:36:23 -07001446 macro = xtalloc (parser, macro_t);
1447
1448 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001449 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001450 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001451 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001452
1453 hash_table_insert (parser->defines, macro, identifier);
1454}
1455
Carl Worth8f38aff2010-05-19 10:01:29 -07001456static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001457glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001458{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001459 token_node_t *node;
1460 int ret;
1461
Carl Worth95951ea2010-05-26 15:57:10 -07001462 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001463 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001464
1465 /* XXX: This ugly block of code exists for the sole
1466 * purpose of converting a NEWLINE token into a SPACE
1467 * token, but only in the case where we have seen a
1468 * function-like macro name, but have not yet seen its
1469 * closing parenthesis.
1470 *
1471 * There's perhaps a more compact way to do this with
1472 * mid-rule actions in the grammar.
1473 *
1474 * I'm definitely not pleased with the complexity of
1475 * this code here.
1476 */
1477 if (parser->newline_as_space)
1478 {
1479 if (ret == '(') {
1480 parser->paren_count++;
1481 } else if (ret == ')') {
1482 parser->paren_count--;
1483 if (parser->paren_count == 0)
1484 parser->newline_as_space = 0;
1485 } else if (ret == NEWLINE) {
1486 ret = SPACE;
1487 } else if (ret != SPACE) {
1488 if (parser->paren_count == 0)
1489 parser->newline_as_space = 0;
1490 }
1491 }
1492 else if (parser->in_control_line)
1493 {
1494 if (ret == NEWLINE)
1495 parser->in_control_line = 0;
1496 }
1497 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1498 ret == HASH_UNDEF || ret == HASH_IF ||
1499 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1500 ret == HASH_ELIF || ret == HASH_ELSE ||
1501 ret == HASH_ENDIF || ret == HASH)
1502 {
1503 parser->in_control_line = 1;
1504 }
1505 else if (ret == IDENTIFIER)
1506 {
1507 macro_t *macro;
1508 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001509 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001510 if (macro && macro->is_function) {
1511 parser->newline_as_space = 1;
1512 parser->paren_count = 0;
1513 }
1514 }
1515
1516 return ret;
1517 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001518
1519 node = parser->lex_from_node;
1520
1521 if (node == NULL) {
1522 talloc_free (parser->lex_from_list);
1523 parser->lex_from_list = NULL;
1524 return NEWLINE;
1525 }
1526
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001527 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001528 ret = node->token->type;
1529
1530 parser->lex_from_node = node->next;
1531
1532 return ret;
1533}
1534
1535static void
1536glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1537{
1538 token_node_t *node;
1539
1540 assert (parser->lex_from_list == NULL);
1541
1542 /* Copy list, eliminating any space tokens. */
1543 parser->lex_from_list = _token_list_create (parser);
1544
1545 for (node = list->head; node; node = node->next) {
1546 if (node->token->type == SPACE)
1547 continue;
1548 _token_list_append (parser->lex_from_list, node->token);
1549 }
1550
1551 talloc_free (list);
1552
1553 parser->lex_from_node = parser->lex_from_list->head;
1554
1555 /* It's possible the list consisted of nothing but whitespace. */
1556 if (parser->lex_from_node == NULL) {
1557 talloc_free (parser->lex_from_list);
1558 parser->lex_from_list = NULL;
1559 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001560}
Carl Worthb20d33c2010-05-20 22:27:07 -07001561
1562static void
1563_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1564{
1565 skip_type_t current = SKIP_NO_SKIP;
1566 skip_node_t *node;
1567
1568 if (parser->skip_stack)
1569 current = parser->skip_stack->type;
1570
1571 node = xtalloc (parser, skip_node_t);
1572
1573 if (current == SKIP_NO_SKIP) {
1574 if (condition)
1575 node->type = SKIP_NO_SKIP;
1576 else
1577 node->type = SKIP_TO_ELSE;
1578 } else {
1579 node->type = SKIP_TO_ENDIF;
1580 }
1581
1582 node->next = parser->skip_stack;
1583 parser->skip_stack = node;
1584}
1585
1586static void
1587_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1588 int condition)
1589{
1590 if (parser->skip_stack == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001591 glcpp_printf (parser->errors, "Error: %s without #if\n", type);
Carl Worthb20d33c2010-05-20 22:27:07 -07001592 exit (1);
1593 }
1594
1595 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1596 if (condition)
1597 parser->skip_stack->type = SKIP_NO_SKIP;
1598 } else {
1599 parser->skip_stack->type = SKIP_TO_ENDIF;
1600 }
1601}
Carl Worth80dc60b2010-05-25 14:42:00 -07001602
Carl Worthb20d33c2010-05-20 22:27:07 -07001603static void
1604_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1605{
1606 skip_node_t *node;
1607
1608 if (parser->skip_stack == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001609 glcpp_print (parser->errors, "Error: #endif without #if\n");
Carl Worthb20d33c2010-05-20 22:27:07 -07001610 exit (1);
1611 }
1612
1613 node = parser->skip_stack;
1614 parser->skip_stack = node->next;
1615 talloc_free (node);
1616}