blob: 8119de3aa5ca4557867df83084bea671013e939b [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 Graunke4c8a1af2010-06-16 11:57:48 -070037yyerror (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 Graunkee0e429f2010-06-16 16:26:28 -0700136glcpp_parser_lex (YYSTYPE *yylval, 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
144
Carl Worth0b27b5f2010-05-10 16:16:06 -0700145%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700146%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700147
Carl Worth050e3de2010-05-27 14:36:29 -0700148%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 -0700149%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700150%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700151%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700152%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700153%type <token> preprocessing_token
154%type <token_list> pp_tokens replacement_list text_line
Carl Worth8fed1cd2010-05-26 09:32:12 -0700155%left OR
156%left AND
157%left '|'
158%left '^'
159%left '&'
160%left EQUAL NOT_EQUAL
161%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
162%left LEFT_SHIFT RIGHT_SHIFT
163%left '+' '-'
164%left '*' '/' '%'
165%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700166
167%%
168
Carl Worth33cc4002010-05-12 12:17:10 -0700169input:
Carl Worth3ff81672010-05-25 13:09:03 -0700170 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700171| input line
172;
173
174line:
175 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700176 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700177 }
Carl Worth808401f2010-05-25 14:52:43 -0700178| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700179 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700180 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700181 talloc_free ($1);
182 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700183| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700184| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700185;
186
Carl Worth8e82fcb2010-05-26 11:15:21 -0700187expanded_line:
188 IF_EXPANDED expression NEWLINE {
189 _glcpp_parser_skip_stack_push_if (parser, $2);
190 }
191| ELIF_EXPANDED expression NEWLINE {
192 _glcpp_parser_skip_stack_change_if (parser, "elif", $2);
193 }
194;
195
Carl Worth3ff81672010-05-25 13:09:03 -0700196control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700197 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Carl Worthae6517f2010-05-25 15:24:59 -0700198 _define_object_macro (parser, $2, $3);
199 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700200| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
201 _define_function_macro (parser, $2, NULL, $5);
202 }
203| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
204 _define_function_macro (parser, $2, $4, $6);
205 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700206| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700207 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700208 if (macro) {
209 /* XXX: Need hash table to support a real way
210 * to remove an element rather than prefixing
211 * a new node with data of NULL like this. */
212 hash_table_insert (parser->defines, NULL, $2);
213 talloc_free (macro);
214 }
215 talloc_free ($2);
216 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700217| HASH_IF pp_tokens NEWLINE {
218 token_list_t *expanded;
219 token_t *token;
220
221 expanded = _token_list_create (parser);
222 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
223 _token_list_append (expanded, token);
224 talloc_unlink (parser, token);
225 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700226 _glcpp_parser_expand_token_list (parser, $2);
227 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700228 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700229 }
230| HASH_IFDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700231 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700232 talloc_free ($2);
233 _glcpp_parser_skip_stack_push_if (parser, macro != NULL);
234 }
235| HASH_IFNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700236 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700237 talloc_free ($2);
238 _glcpp_parser_skip_stack_push_if (parser, macro == NULL);
239 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700240| HASH_ELIF pp_tokens NEWLINE {
241 token_list_t *expanded;
242 token_t *token;
243
244 expanded = _token_list_create (parser);
245 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
246 _token_list_append (expanded, token);
247 talloc_unlink (parser, token);
248 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700249 _glcpp_parser_expand_token_list (parser, $2);
250 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700251 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700252 }
253| HASH_ELSE NEWLINE {
254 _glcpp_parser_skip_stack_change_if (parser, "else", 1);
255 }
256| HASH_ENDIF NEWLINE {
257 _glcpp_parser_skip_stack_pop (parser);
258 }
Carl Worth3ff81672010-05-25 13:09:03 -0700259| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700260;
261
Carl Worth8fed1cd2010-05-26 09:32:12 -0700262expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700263 INTEGER_STRING {
264 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
265 $$ = strtoll ($1 + 2, NULL, 16);
266 } else if ($1[0] == '0') {
267 $$ = strtoll ($1, NULL, 8);
268 } else {
269 $$ = strtoll ($1, NULL, 10);
270 }
271 }
272| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700273 $$ = $1;
274 }
275| expression OR expression {
276 $$ = $1 || $3;
277 }
278| expression AND expression {
279 $$ = $1 && $3;
280 }
281| expression '|' expression {
282 $$ = $1 | $3;
283 }
284| expression '^' expression {
285 $$ = $1 ^ $3;
286 }
287| expression '&' expression {
288 $$ = $1 & $3;
289 }
290| expression NOT_EQUAL expression {
291 $$ = $1 != $3;
292 }
293| expression EQUAL expression {
294 $$ = $1 == $3;
295 }
296| expression GREATER_OR_EQUAL expression {
297 $$ = $1 >= $3;
298 }
299| expression LESS_OR_EQUAL expression {
300 $$ = $1 <= $3;
301 }
302| expression '>' expression {
303 $$ = $1 > $3;
304 }
305| expression '<' expression {
306 $$ = $1 < $3;
307 }
308| expression RIGHT_SHIFT expression {
309 $$ = $1 >> $3;
310 }
311| expression LEFT_SHIFT expression {
312 $$ = $1 << $3;
313 }
314| expression '-' expression {
315 $$ = $1 - $3;
316 }
317| expression '+' expression {
318 $$ = $1 + $3;
319 }
320| expression '%' expression {
321 $$ = $1 % $3;
322 }
323| expression '/' expression {
324 $$ = $1 / $3;
325 }
326| expression '*' expression {
327 $$ = $1 * $3;
328 }
329| '!' expression %prec UNARY {
330 $$ = ! $2;
331 }
332| '~' expression %prec UNARY {
333 $$ = ~ $2;
334 }
335| '-' expression %prec UNARY {
336 $$ = - $2;
337 }
338| '+' expression %prec UNARY {
339 $$ = + $2;
340 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700341| '(' expression ')' {
342 $$ = $2;
343 }
344;
345
Carl Worth3ff81672010-05-25 13:09:03 -0700346identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700347 IDENTIFIER {
348 $$ = _string_list_create (parser);
349 _string_list_append_item ($$, $1);
350 talloc_steal ($$, $1);
351 }
352| identifier_list ',' IDENTIFIER {
353 $$ = $1;
354 _string_list_append_item ($$, $3);
355 talloc_steal ($$, $3);
356 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700357;
358
Carl Worth3ff81672010-05-25 13:09:03 -0700359text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700360 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700361| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700362;
363
Carl Worth3ff81672010-05-25 13:09:03 -0700364non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700365 pp_tokens NEWLINE {
366 yyerror (parser, "Invalid tokens after #");
367 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700368;
369
Carl Worthaaa9acb2010-05-19 13:28:24 -0700370replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700371 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700372| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700373;
374
Carl Worthaaa9acb2010-05-19 13:28:24 -0700375pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700376 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700377 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700378 $$ = _token_list_create (parser);
379 _token_list_append ($$, $1);
380 talloc_unlink (parser, $1);
381 }
382| pp_tokens preprocessing_token {
383 $$ = $1;
384 _token_list_append ($$, $2);
385 talloc_unlink (parser, $2);
386 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700387;
388
Carl Worth3ff81672010-05-25 13:09:03 -0700389preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700390 IDENTIFIER {
391 $$ = _token_create_str (parser, IDENTIFIER, $1);
392 }
Carl Worth050e3de2010-05-27 14:36:29 -0700393| INTEGER_STRING {
394 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700395 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700396| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700397 $$ = _token_create_ival (parser, $1, $1);
398 }
399| OTHER {
400 $$ = _token_create_str (parser, OTHER, $1);
401 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700402| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700403 $$ = _token_create_ival (parser, SPACE, SPACE);
Carl Worthb1854fd2010-05-25 16:28:26 -0700404 }
Carl Worth3ff81672010-05-25 13:09:03 -0700405;
406
Carl Worth8e82fcb2010-05-26 11:15:21 -0700407operator:
Carl Worth808401f2010-05-25 14:52:43 -0700408 '[' { $$ = '['; }
409| ']' { $$ = ']'; }
410| '(' { $$ = '('; }
411| ')' { $$ = ')'; }
412| '{' { $$ = '{'; }
413| '}' { $$ = '}'; }
414| '.' { $$ = '.'; }
415| '&' { $$ = '&'; }
416| '*' { $$ = '*'; }
417| '+' { $$ = '+'; }
418| '-' { $$ = '-'; }
419| '~' { $$ = '~'; }
420| '!' { $$ = '!'; }
421| '/' { $$ = '/'; }
422| '%' { $$ = '%'; }
423| LEFT_SHIFT { $$ = LEFT_SHIFT; }
424| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
425| '<' { $$ = '<'; }
426| '>' { $$ = '>'; }
427| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
428| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
429| EQUAL { $$ = EQUAL; }
430| NOT_EQUAL { $$ = NOT_EQUAL; }
431| '^' { $$ = '^'; }
432| '|' { $$ = '|'; }
433| AND { $$ = AND; }
434| OR { $$ = OR; }
435| ';' { $$ = ';'; }
436| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700437| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700438| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700439| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700440;
441
Carl Worth33cc4002010-05-12 12:17:10 -0700442%%
443
Carl Worth610053b2010-05-14 10:05:11 -0700444string_list_t *
445_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700446{
Carl Worth610053b2010-05-14 10:05:11 -0700447 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700448
Carl Worth610053b2010-05-14 10:05:11 -0700449 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700450 list->head = NULL;
451 list->tail = NULL;
452
453 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700454}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700455
Carl Worth33cc4002010-05-12 12:17:10 -0700456void
Carl Worth610053b2010-05-14 10:05:11 -0700457_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700458{
459 if (list->head == NULL) {
460 list->head = tail->head;
461 } else {
462 list->tail->next = tail->head;
463 }
464
465 list->tail = tail->tail;
466}
467
468void
Carl Worth610053b2010-05-14 10:05:11 -0700469_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700470{
Carl Worth610053b2010-05-14 10:05:11 -0700471 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700472
Carl Worth610053b2010-05-14 10:05:11 -0700473 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700474 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700475
Carl Worth33cc4002010-05-12 12:17:10 -0700476 node->next = NULL;
477
478 if (list->head == NULL) {
479 list->head = node;
480 } else {
481 list->tail->next = node;
482 }
483
484 list->tail = node;
485}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700486
487int
Carl Worth610053b2010-05-14 10:05:11 -0700488_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700489{
Carl Worth610053b2010-05-14 10:05:11 -0700490 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700491 int i;
492
493 if (list == NULL)
494 return 0;
495
496 for (i = 0, node = list->head; node; i++, node = node->next) {
497 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700498 if (index)
499 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700500 return 1;
501 }
502 }
503
504 return 0;
505}
506
507int
Carl Worth610053b2010-05-14 10:05:11 -0700508_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700509{
510 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700511 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700512
513 if (list == NULL)
514 return 0;
515
516 for (node = list->head; node; node = node->next)
517 length++;
518
519 return length;
520}
521
Carl Worth8f6a8282010-05-14 10:44:19 -0700522argument_list_t *
523_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700524{
Carl Worth8f6a8282010-05-14 10:44:19 -0700525 argument_list_t *list;
526
527 list = xtalloc (ctx, argument_list_t);
528 list->head = NULL;
529 list->tail = NULL;
530
531 return list;
532}
533
534void
Carl Worth47252442010-05-19 13:54:37 -0700535_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700536{
537 argument_node_t *node;
538
Carl Worth8f6a8282010-05-14 10:44:19 -0700539 node = xtalloc (list, argument_node_t);
540 node->argument = argument;
541
542 node->next = NULL;
543
544 if (list->head == NULL) {
545 list->head = node;
546 } else {
547 list->tail->next = node;
548 }
549
550 list->tail = node;
551}
552
553int
554_argument_list_length (argument_list_t *list)
555{
556 int length = 0;
557 argument_node_t *node;
558
559 if (list == NULL)
560 return 0;
561
562 for (node = list->head; node; node = node->next)
563 length++;
564
565 return length;
566}
567
Carl Worth47252442010-05-19 13:54:37 -0700568token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700569_argument_list_member_at (argument_list_t *list, int index)
570{
571 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700572 int i;
573
574 if (list == NULL)
575 return NULL;
576
577 node = list->head;
578 for (i = 0; i < index; i++) {
579 node = node->next;
580 if (node == NULL)
581 break;
582 }
583
584 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700585 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700586
587 return NULL;
588}
Carl Worth47252442010-05-19 13:54:37 -0700589
Carl Worth808401f2010-05-25 14:52:43 -0700590/* Note: This function talloc_steal()s the str pointer. */
591token_t *
592_token_create_str (void *ctx, int type, char *str)
593{
594 token_t *token;
595
596 token = xtalloc (ctx, token_t);
597 token->type = type;
598 token->value.str = talloc_steal (token, str);
599
600 return token;
601}
602
603token_t *
604_token_create_ival (void *ctx, int type, int ival)
605{
606 token_t *token;
607
608 token = xtalloc (ctx, token_t);
609 token->type = type;
610 token->value.ival = ival;
611
612 return token;
613}
614
Carl Worth47252442010-05-19 13:54:37 -0700615token_list_t *
616_token_list_create (void *ctx)
617{
618 token_list_t *list;
619
620 list = xtalloc (ctx, token_list_t);
621 list->head = NULL;
622 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700623 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700624
625 return list;
626}
627
628void
Carl Worth808401f2010-05-25 14:52:43 -0700629_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700630{
631 token_node_t *node;
632
633 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700634 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700635
636 node->next = NULL;
637
638 if (list->head == NULL) {
639 list->head = node;
640 } else {
641 list->tail->next = node;
642 }
643
644 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700645 if (token->type != SPACE)
646 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700647}
648
649void
650_token_list_append_list (token_list_t *list, token_list_t *tail)
651{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700652 if (tail == NULL || tail->head == NULL)
653 return;
654
Carl Worth47252442010-05-19 13:54:37 -0700655 if (list->head == NULL) {
656 list->head = tail->head;
657 } else {
658 list->tail->next = tail->head;
659 }
660
661 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700662 list->non_space_tail = tail->non_space_tail;
663}
664
Carl Worth681afbc2010-05-28 15:06:02 -0700665token_list_t *
666_token_list_copy (void *ctx, token_list_t *other)
667{
668 token_list_t *copy;
669 token_node_t *node;
670
671 if (other == NULL)
672 return NULL;
673
674 copy = _token_list_create (ctx);
675 for (node = other->head; node; node = node->next)
676 _token_list_append (copy, node->token);
677
678 return copy;
679}
680
Carl Worth10ae4382010-05-25 20:35:01 -0700681void
682_token_list_trim_trailing_space (token_list_t *list)
683{
684 token_node_t *tail, *next;
685
686 if (list->non_space_tail) {
687 tail = list->non_space_tail->next;
688 list->non_space_tail->next = NULL;
689 list->tail = list->non_space_tail;
690
691 while (tail) {
692 next = tail->next;
693 talloc_free (tail);
694 tail = next;
695 }
696 }
Carl Worth47252442010-05-19 13:54:37 -0700697}
Carl Worth80dc60b2010-05-25 14:42:00 -0700698
Carl Worth22b3ace2010-06-02 15:32:03 -0700699static int
700_token_list_length (token_list_t *list)
701{
702 int length = 0;
703 token_node_t *node;
704
705 if (list == NULL)
706 return 0;
707
708 for (node = list->head; node; node = node->next)
709 length++;
710
711 return length;
712}
713
Carl Worth0197e9b2010-05-26 08:05:19 -0700714static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700715_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700716{
717 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700718 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700719 return;
720 }
721
722 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700723 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700724 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700725 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700726 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700727 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700728 case OTHER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700729 glcpp_printf (*out, "%s", token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700730 break;
731 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700732 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700733 break;
734 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700735 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700736 break;
737 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700738 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700739 break;
740 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700741 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700742 break;
743 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700744 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700745 break;
746 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700747 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700748 break;
749 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700750 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700751 break;
752 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700753 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700754 break;
755 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700756 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700757 break;
758 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700759 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700760 break;
Carl Worthdd749002010-05-27 10:12:33 -0700761 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700762 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700763 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700764 case PLACEHOLDER:
765 /* Nothing to print. */
766 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700767 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700768 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700769 break;
770 }
771}
772
Carl Worthb06096e2010-05-29 05:54:19 -0700773/* Return a new token (talloc()ed off of 'token') formed by pasting
774 * 'token' and 'other'. Note that this function may return 'token' or
775 * 'other' directly rather than allocating anything new.
776 *
777 * Caution: Only very cursory error-checking is performed to see if
778 * the final result is a valid single token. */
779static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700780_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700781{
Carl Worth85b50e82010-05-27 14:01:18 -0700782 /* Pasting a placeholder onto anything makes no change. */
783 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700784 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700785
Carl Worthb06096e2010-05-29 05:54:19 -0700786 /* When 'token' is a placeholder, just return 'other'. */
787 if (token->type == PLACEHOLDER)
788 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700789
Carl Worthad0dee62010-05-26 09:04:50 -0700790 /* A very few single-character punctuators can be combined
791 * with another to form a multi-character punctuator. */
792 switch (token->type) {
793 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700794 if (other->type == '<')
795 return _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
796 else if (other->type == '=')
797 return _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700798 break;
799 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700800 if (other->type == '>')
801 return _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
802 else if (other->type == '=')
803 return _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700804 break;
805 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700806 if (other->type == '=')
807 return _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700808 break;
809 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700810 if (other->type == '=')
811 return _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700812 break;
813 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700814 if (other->type == '&')
815 return _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700816 break;
817 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700818 if (other->type == '|')
819 return _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700820 break;
821 }
822
823 /* Two string-valued tokens can usually just be mashed
824 * together.
825 *
Carl Worth050e3de2010-05-27 14:36:29 -0700826 * XXX: This isn't actually legitimate. Several things here
827 * should result in a diagnostic since the result cannot be a
828 * valid, single pre-processing token. For example, pasting
829 * "123" and "abc" is not legal, but we don't catch that
830 * here. */
831 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
832 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700833 {
Carl Worthb06096e2010-05-29 05:54:19 -0700834 char *str;
835
836 str = xtalloc_asprintf (token, "%s%s",
837 token->value.str, other->value.str);
838 return _token_create_str (token, token->type, str);
Carl Worthad0dee62010-05-26 09:04:50 -0700839 }
840
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700841 glcpp_print (parser->errors, "Error: Pasting \"");
842 _token_print (&parser->errors, token);
843 glcpp_print (parser->errors, "\" and \"");
844 _token_print (&parser->errors, other);
845 glcpp_print (parser->errors, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700846
847 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700848}
849
Carl Worth0197e9b2010-05-26 08:05:19 -0700850static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700851_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700852{
853 token_node_t *node;
854
855 if (list == NULL)
856 return;
857
858 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700859 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700860}
861
Carl Worth3a37b872010-05-10 11:44:09 -0700862void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700863yyerror (glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700864{
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700865 glcpp_printf(parser->errors, "Parse error: %s\n", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700866}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700867
Carl Worth33cc4002010-05-12 12:17:10 -0700868glcpp_parser_t *
869glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700870{
Carl Worth33cc4002010-05-12 12:17:10 -0700871 glcpp_parser_t *parser;
872
Carl Worth5070a202010-05-12 12:45:33 -0700873 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700874
Carl Worth8f38aff2010-05-19 10:01:29 -0700875 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700876 parser->defines = hash_table_ctor (32, hash_table_string_hash,
877 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700878 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700879 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700880 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700881 parser->newline_as_space = 0;
882 parser->in_control_line = 0;
883 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700884
Carl Worthb20d33c2010-05-20 22:27:07 -0700885 parser->skip_stack = NULL;
886
Carl Worth8e82fcb2010-05-26 11:15:21 -0700887 parser->lex_from_list = NULL;
888 parser->lex_from_node = NULL;
889
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700890 parser->output = talloc_strdup(parser, "");
891 parser->errors = talloc_strdup(parser, "");
892
Carl Worth33cc4002010-05-12 12:17:10 -0700893 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700894}
895
896int
897glcpp_parser_parse (glcpp_parser_t *parser)
898{
899 return yyparse (parser);
900}
901
902void
Carl Worth33cc4002010-05-12 12:17:10 -0700903glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700904{
Carl Worthb20d33c2010-05-20 22:27:07 -0700905 if (parser->skip_stack)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700906 glcpp_print (parser->errors, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700907 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700908 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700909 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700910}
Carl Worthc6d5af32010-05-11 12:30:09 -0700911
Carl Worth8e82fcb2010-05-26 11:15:21 -0700912/* Replace any occurences of DEFINED tokens in 'list' with either a
913 * '0' or '1' INTEGER token depending on whether the next token in the
914 * list is defined or not. */
915static void
916_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
917 token_list_t *list)
918{
919 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700920 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700921
922 if (list == NULL)
923 return;
924
925 for (node = list->head; node; node = node->next) {
926 if (node->token->type != DEFINED)
927 continue;
928 next = node->next;
929 while (next && next->token->type == SPACE)
930 next = next->next;
931 if (next == NULL || next->token->type != IDENTIFIER) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700932 glcpp_print (parser->errors, "Error: operator \"defined\" requires an identifier\n");
Carl Worth8e82fcb2010-05-26 11:15:21 -0700933 exit (1);
934 }
935 macro = hash_table_find (parser->defines,
936 next->token->value.str);
937
938 node->token->type = INTEGER;
939 node->token->value.ival = (macro != NULL);
940 node->next = next->next;
941 }
942}
943
Carl Worthb1854fd2010-05-25 16:28:26 -0700944typedef enum function_status
945{
946 FUNCTION_STATUS_SUCCESS,
947 FUNCTION_NOT_A_FUNCTION,
948 FUNCTION_UNBALANCED_PARENTHESES
949} function_status_t;
950
951/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700952 * balanced set of parentheses.
953 *
954 * When called, 'node' should be the opening-parenthesis token, (or
955 * perhaps preceeding SPACE tokens). Upon successful return *last will
956 * be the last consumed node, (corresponding to the closing right
957 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700958 *
959 * Return values:
960 *
961 * FUNCTION_STATUS_SUCCESS:
962 *
963 * Successfully parsed a set of function arguments.
964 *
965 * FUNCTION_NOT_A_FUNCTION:
966 *
967 * Macro name not followed by a '('. This is not an error, but
968 * simply that the macro name should be treated as a non-macro.
969 *
Carl Worth14c98a52010-06-02 15:49:54 -0700970 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -0700971 *
972 * Macro name is not followed by a balanced set of parentheses.
973 */
974static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700975_arguments_parse (argument_list_t *arguments,
976 token_node_t *node,
977 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700978{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700979 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700980 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700981
Carl Worthb1854fd2010-05-25 16:28:26 -0700982 node = node->next;
983
984 /* Ignore whitespace before first parenthesis. */
985 while (node && node->token->type == SPACE)
986 node = node->next;
987
988 if (node == NULL || node->token->type != '(')
989 return FUNCTION_NOT_A_FUNCTION;
990
Carl Worth652fa272010-05-25 17:45:22 -0700991 node = node->next;
992
Carl Wortha19297b2010-05-27 13:29:19 -0700993 argument = _token_list_create (arguments);
994 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -0700995
Carl Worth681afbc2010-05-28 15:06:02 -0700996 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -0700997 if (node->token->type == '(')
998 {
999 paren_count++;
1000 }
1001 else if (node->token->type == ')')
1002 {
1003 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001004 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001005 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001006 }
Carl Worth652fa272010-05-25 17:45:22 -07001007
1008 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001009 paren_count == 1)
1010 {
Carl Wortha19297b2010-05-27 13:29:19 -07001011 _token_list_trim_trailing_space (argument);
1012 argument = _token_list_create (arguments);
1013 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001014 }
1015 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001016 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001017 /* Don't treat initial whitespace as
1018 * part of the arguement. */
1019 if (node->token->type == SPACE)
1020 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001021 }
1022 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001023 }
Carl Worthc7581c22010-05-25 17:41:07 -07001024 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001025
Carl Worth681afbc2010-05-28 15:06:02 -07001026 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001027 return FUNCTION_UNBALANCED_PARENTHESES;
1028
Carl Worth681afbc2010-05-28 15:06:02 -07001029 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001030
1031 return FUNCTION_STATUS_SUCCESS;
1032}
1033
Carl Worth681afbc2010-05-28 15:06:02 -07001034/* This is a helper function that's essentially part of the
1035 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1036 * except for by that function.
1037 *
1038 * Returns NULL if node is a simple token with no expansion, (that is,
1039 * although 'node' corresponds to an identifier defined as a
1040 * function-like macro, it is not followed with a parenthesized
1041 * argument list).
1042 *
1043 * Compute the complete expansion of node (which is a function-like
1044 * macro) and subsequent nodes which are arguments.
1045 *
1046 * Returns the token list that results from the expansion and sets
1047 * *last to the last node in the list that was consumed by the
1048 * expansion. Specificallty, *last will be set as follows: as the
1049 * token of the closing right parenthesis.
1050 */
1051static token_list_t *
1052_glcpp_parser_expand_function (glcpp_parser_t *parser,
1053 token_node_t *node,
1054 token_node_t **last)
1055
Carl Worthb1854fd2010-05-25 16:28:26 -07001056{
1057 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001058 const char *identifier;
1059 argument_list_t *arguments;
1060 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001061 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001062 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001063
Carl Worthb1854fd2010-05-25 16:28:26 -07001064 identifier = node->token->value.str;
1065
1066 macro = hash_table_find (parser->defines, identifier);
1067
1068 assert (macro->is_function);
1069
Carl Worth9ce18cf2010-05-25 17:32:21 -07001070 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001071 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001072
1073 switch (status) {
1074 case FUNCTION_STATUS_SUCCESS:
1075 break;
1076 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001077 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001078 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001079 glcpp_printf (parser->errors, "Error: Macro %s call has unbalanced parentheses\n",
1080 identifier);
Carl Worth14c98a52010-06-02 15:49:54 -07001081 exit (1);
Carl Worth681afbc2010-05-28 15:06:02 -07001082 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001083 }
1084
Carl Worth9ce18cf2010-05-25 17:32:21 -07001085 if (macro->replacements == NULL) {
1086 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001087 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001088 }
1089
Carl Wortha19297b2010-05-27 13:29:19 -07001090 if (! ((_argument_list_length (arguments) ==
1091 _string_list_length (macro->parameters)) ||
1092 (_string_list_length (macro->parameters) == 0 &&
1093 _argument_list_length (arguments) == 1 &&
1094 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001095 {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001096 glcpp_printf (parser->errors,
1097 "Error: macro %s invoked with %d arguments (expected %d)\n",
1098 identifier,
1099 _argument_list_length (arguments),
1100 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001101 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001102 }
1103
Carl Worth0197e9b2010-05-26 08:05:19 -07001104 /* Perform argument substitution on the replacement list. */
1105 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001106
Carl Worthce540f22010-05-26 08:25:44 -07001107 for (node = macro->replacements->head; node; node = node->next)
1108 {
1109 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001110 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001111 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001112 &parameter_index))
1113 {
1114 token_list_t *argument;
1115 argument = _argument_list_member_at (arguments,
1116 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001117 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001118 * tokens, or append a placeholder token for
1119 * an empty argument. */
1120 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001121 _glcpp_parser_expand_token_list (parser,
1122 argument);
1123 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001124 } else {
1125 token_t *new_token;
1126
1127 new_token = _token_create_ival (substituted,
1128 PLACEHOLDER,
1129 PLACEHOLDER);
1130 _token_list_append (substituted, new_token);
1131 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001132 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001133 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001134 }
1135 }
1136
Carl Worthad0dee62010-05-26 09:04:50 -07001137 /* After argument substitution, and before further expansion
1138 * below, implement token pasting. */
1139
Carl Worthb06096e2010-05-29 05:54:19 -07001140 _token_list_trim_trailing_space (substituted);
1141
Carl Worthad0dee62010-05-26 09:04:50 -07001142 node = substituted->head;
1143 while (node)
1144 {
1145 token_node_t *next_non_space;
1146
1147 /* Look ahead for a PASTE token, skipping space. */
1148 next_non_space = node->next;
1149 while (next_non_space && next_non_space->token->type == SPACE)
1150 next_non_space = next_non_space->next;
1151
1152 if (next_non_space == NULL)
1153 break;
1154
1155 if (next_non_space->token->type != PASTE) {
1156 node = next_non_space;
1157 continue;
1158 }
1159
1160 /* Now find the next non-space token after the PASTE. */
1161 next_non_space = next_non_space->next;
1162 while (next_non_space && next_non_space->token->type == SPACE)
1163 next_non_space = next_non_space->next;
1164
1165 if (next_non_space == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001166 glcpp_print (parser->errors, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001167 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001168 }
1169
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001170 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001171 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001172 if (next_non_space == substituted->tail)
1173 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001174
1175 node = node->next;
1176 }
1177
Carl Worthb06096e2010-05-29 05:54:19 -07001178 substituted->non_space_tail = substituted->tail;
1179
Carl Worth681afbc2010-05-28 15:06:02 -07001180 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001181}
1182
Carl Worth681afbc2010-05-28 15:06:02 -07001183/* Compute the complete expansion of node, (and subsequent nodes after
1184 * 'node' in the case that 'node' is a function-like macro and
1185 * subsequent nodes are arguments).
1186 *
1187 * Returns NULL if node is a simple token with no expansion.
1188 *
1189 * Otherwise, returns the token list that results from the expansion
1190 * and sets *last to the last node in the list that was consumed by
1191 * the expansion. Specificallty, *last will be set as follows:
1192 *
1193 * As 'node' in the case of object-like macro expansion.
1194 *
1195 * As the token of the closing right parenthesis in the case of
1196 * function-like macro expansion.
1197 */
1198static token_list_t *
1199_glcpp_parser_expand_node (glcpp_parser_t *parser,
1200 token_node_t *node,
1201 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001202{
Carl Worth681afbc2010-05-28 15:06:02 -07001203 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001204 const char *identifier;
1205 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001206
1207 /* We only expand identifiers */
1208 if (token->type != IDENTIFIER) {
1209 /* We change any COMMA into a COMMA_FINAL to prevent
1210 * it being mistaken for an argument separator
1211 * later. */
1212 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001213 token->type = COMMA_FINAL;
1214 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001215 }
Carl Worth681afbc2010-05-28 15:06:02 -07001216
1217 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001218 }
1219
1220 /* Look up this identifier in the hash table. */
1221 identifier = token->value.str;
1222 macro = hash_table_find (parser->defines, identifier);
1223
Carl Worth681afbc2010-05-28 15:06:02 -07001224 /* Not a macro, so no expansion needed. */
1225 if (macro == NULL)
1226 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001227
1228 /* Finally, don't expand this macro if we're already actively
1229 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001230 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001231 /* We change the token type here from IDENTIFIER to
1232 * OTHER to prevent any future expansion of this
1233 * unexpanded token. */
1234 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001235 token_list_t *expansion;
1236 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001237
Carl Worth681afbc2010-05-28 15:06:02 -07001238 str = xtalloc_strdup (parser, token->value.str);
1239 final = _token_create_str (parser, OTHER, str);
1240 expansion = _token_list_create (parser);
1241 _token_list_append (expansion, final);
1242 *last = node;
1243 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001244 }
1245
Carl Worth681afbc2010-05-28 15:06:02 -07001246 if (! macro->is_function)
1247 {
1248 *last = node;
1249
1250 if (macro->replacements == NULL)
1251 return _token_list_create (parser);
1252
Carl Worth22b3ace2010-06-02 15:32:03 -07001253 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001254 }
Carl Worth681afbc2010-05-28 15:06:02 -07001255
1256 return _glcpp_parser_expand_function (parser, node, last);
1257}
1258
Carl Worth22b3ace2010-06-02 15:32:03 -07001259/* Push a new identifier onto the active list, returning the new list.
1260 *
1261 * Here, 'marker' is the token node that appears in the list after the
1262 * expansion of 'identifier'. That is, when the list iterator begins
1263 * examinging 'marker', then it is time to pop this node from the
1264 * active stack.
1265 */
1266active_list_t *
1267_active_list_push (active_list_t *list,
1268 const char *identifier,
1269 token_node_t *marker)
1270{
1271 active_list_t *node;
1272
1273 node = xtalloc (list, active_list_t);
1274 node->identifier = xtalloc_strdup (node, identifier);
1275 node->marker = marker;
1276 node->next = list;
1277
1278 return node;
1279}
1280
1281active_list_t *
1282_active_list_pop (active_list_t *list)
1283{
1284 active_list_t *node = list;
1285
1286 if (node == NULL)
1287 return NULL;
1288
1289 node = list->next;
1290 talloc_free (list);
1291
1292 return node;
1293}
1294
1295int
1296_active_list_contains (active_list_t *list, const char *identifier)
1297{
1298 active_list_t *node;
1299
1300 if (list == NULL)
1301 return 0;
1302
1303 for (node = list; node; node = node->next)
1304 if (strcmp (node->identifier, identifier) == 0)
1305 return 1;
1306
1307 return 0;
1308}
1309
Carl Worth681afbc2010-05-28 15:06:02 -07001310/* Walk over the token list replacing nodes with their expansion.
1311 * Whenever nodes are expanded the walking will walk over the new
1312 * nodes, continuing to expand as necessary. The results are placed in
1313 * 'list' itself;
1314 */
1315static void
1316_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1317 token_list_t *list)
1318{
1319 token_node_t *node_prev;
1320 token_node_t *node, *last;
1321 token_list_t *expansion;
1322
1323 if (list == NULL)
1324 return;
1325
1326 _token_list_trim_trailing_space (list);
1327
1328 node_prev = NULL;
1329 node = list->head;
1330
1331 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001332
1333 while (parser->active && parser->active->marker == node)
1334 parser->active = _active_list_pop (parser->active);
1335
Carl Worth681afbc2010-05-28 15:06:02 -07001336 /* Find the expansion for node, which will replace all
1337 * nodes from node to last, inclusive. */
1338 expansion = _glcpp_parser_expand_node (parser, node, &last);
1339 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001340 token_node_t *n;
1341
1342 for (n = node; n != last->next; n = n->next)
1343 while (parser->active &&
1344 parser->active->marker == n)
1345 {
1346 parser->active = _active_list_pop (parser->active);
1347 }
1348
1349 parser->active = _active_list_push (parser->active,
1350 node->token->value.str,
1351 last->next);
1352
Carl Worth681afbc2010-05-28 15:06:02 -07001353 /* Splice expansion into list, supporting a
1354 * simple deletion if the expansion is
1355 * empty. */
1356 if (expansion->head) {
1357 if (node_prev)
1358 node_prev->next = expansion->head;
1359 else
1360 list->head = expansion->head;
1361 expansion->tail->next = last->next;
1362 if (last == list->tail)
1363 list->tail = expansion->tail;
1364 } else {
1365 if (node_prev)
1366 node_prev->next = last->next;
1367 else
1368 list->head = last->next;
1369 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001370 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001371 }
1372 } else {
1373 node_prev = node;
1374 }
1375 node = node_prev ? node_prev->next : list->head;
1376 }
1377
Carl Worth22b3ace2010-06-02 15:32:03 -07001378 while (parser->active)
1379 parser->active = _active_list_pop (parser->active);
1380
Carl Worth681afbc2010-05-28 15:06:02 -07001381 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001382}
1383
Carl Worthae6517f2010-05-25 15:24:59 -07001384void
1385_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1386 token_list_t *list)
1387{
Carl Worthae6517f2010-05-25 15:24:59 -07001388 if (list == NULL)
1389 return;
1390
Carl Worth681afbc2010-05-28 15:06:02 -07001391 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001392
Carl Worth681afbc2010-05-28 15:06:02 -07001393 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001394
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001395 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001396}
1397
1398void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001399_check_for_reserved_macro_name (glcpp_parser_t *parser, const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001400{
1401 /* According to the GLSL specification, macro names starting with "__"
1402 * or "GL_" are reserved for future use. So, don't allow them.
1403 */
1404 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001405 glcpp_print (parser->errors, "Error: Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001406 exit(1);
1407 }
1408 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001409 glcpp_print (parser->errors, "Error: Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001410 exit(1);
1411 }
1412}
1413
1414void
Carl Worthfcbbb462010-05-13 09:36:23 -07001415_define_object_macro (glcpp_parser_t *parser,
1416 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001417 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001418{
1419 macro_t *macro;
1420
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001421 _check_for_reserved_macro_name(parser, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001422
Carl Worthfcbbb462010-05-13 09:36:23 -07001423 macro = xtalloc (parser, macro_t);
1424
1425 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001426 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001427 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001428 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001429
1430 hash_table_insert (parser->defines, macro, identifier);
1431}
1432
1433void
1434_define_function_macro (glcpp_parser_t *parser,
1435 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001436 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001437 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001438{
1439 macro_t *macro;
1440
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001441 _check_for_reserved_macro_name(parser, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001442
Carl Worthfcbbb462010-05-13 09:36:23 -07001443 macro = xtalloc (parser, macro_t);
1444
1445 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001446 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001447 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001448 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001449
1450 hash_table_insert (parser->defines, macro, identifier);
1451}
1452
Carl Worth8f38aff2010-05-19 10:01:29 -07001453static int
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001454glcpp_parser_lex (YYSTYPE *yylval, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001455{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001456 token_node_t *node;
1457 int ret;
1458
Carl Worth95951ea2010-05-26 15:57:10 -07001459 if (parser->lex_from_list == NULL) {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001460 ret = glcpp_lex (yylval, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001461
1462 /* XXX: This ugly block of code exists for the sole
1463 * purpose of converting a NEWLINE token into a SPACE
1464 * token, but only in the case where we have seen a
1465 * function-like macro name, but have not yet seen its
1466 * closing parenthesis.
1467 *
1468 * There's perhaps a more compact way to do this with
1469 * mid-rule actions in the grammar.
1470 *
1471 * I'm definitely not pleased with the complexity of
1472 * this code here.
1473 */
1474 if (parser->newline_as_space)
1475 {
1476 if (ret == '(') {
1477 parser->paren_count++;
1478 } else if (ret == ')') {
1479 parser->paren_count--;
1480 if (parser->paren_count == 0)
1481 parser->newline_as_space = 0;
1482 } else if (ret == NEWLINE) {
1483 ret = SPACE;
1484 } else if (ret != SPACE) {
1485 if (parser->paren_count == 0)
1486 parser->newline_as_space = 0;
1487 }
1488 }
1489 else if (parser->in_control_line)
1490 {
1491 if (ret == NEWLINE)
1492 parser->in_control_line = 0;
1493 }
1494 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1495 ret == HASH_UNDEF || ret == HASH_IF ||
1496 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1497 ret == HASH_ELIF || ret == HASH_ELSE ||
1498 ret == HASH_ENDIF || ret == HASH)
1499 {
1500 parser->in_control_line = 1;
1501 }
1502 else if (ret == IDENTIFIER)
1503 {
1504 macro_t *macro;
1505 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001506 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001507 if (macro && macro->is_function) {
1508 parser->newline_as_space = 1;
1509 parser->paren_count = 0;
1510 }
1511 }
1512
1513 return ret;
1514 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001515
1516 node = parser->lex_from_node;
1517
1518 if (node == NULL) {
1519 talloc_free (parser->lex_from_list);
1520 parser->lex_from_list = NULL;
1521 return NEWLINE;
1522 }
1523
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001524 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001525 ret = node->token->type;
1526
1527 parser->lex_from_node = node->next;
1528
1529 return ret;
1530}
1531
1532static void
1533glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1534{
1535 token_node_t *node;
1536
1537 assert (parser->lex_from_list == NULL);
1538
1539 /* Copy list, eliminating any space tokens. */
1540 parser->lex_from_list = _token_list_create (parser);
1541
1542 for (node = list->head; node; node = node->next) {
1543 if (node->token->type == SPACE)
1544 continue;
1545 _token_list_append (parser->lex_from_list, node->token);
1546 }
1547
1548 talloc_free (list);
1549
1550 parser->lex_from_node = parser->lex_from_list->head;
1551
1552 /* It's possible the list consisted of nothing but whitespace. */
1553 if (parser->lex_from_node == NULL) {
1554 talloc_free (parser->lex_from_list);
1555 parser->lex_from_list = NULL;
1556 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001557}
Carl Worthb20d33c2010-05-20 22:27:07 -07001558
1559static void
1560_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1561{
1562 skip_type_t current = SKIP_NO_SKIP;
1563 skip_node_t *node;
1564
1565 if (parser->skip_stack)
1566 current = parser->skip_stack->type;
1567
1568 node = xtalloc (parser, skip_node_t);
1569
1570 if (current == SKIP_NO_SKIP) {
1571 if (condition)
1572 node->type = SKIP_NO_SKIP;
1573 else
1574 node->type = SKIP_TO_ELSE;
1575 } else {
1576 node->type = SKIP_TO_ENDIF;
1577 }
1578
1579 node->next = parser->skip_stack;
1580 parser->skip_stack = node;
1581}
1582
1583static void
1584_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1585 int condition)
1586{
1587 if (parser->skip_stack == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001588 glcpp_printf (parser->errors, "Error: %s without #if\n", type);
Carl Worthb20d33c2010-05-20 22:27:07 -07001589 exit (1);
1590 }
1591
1592 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1593 if (condition)
1594 parser->skip_stack->type = SKIP_NO_SKIP;
1595 } else {
1596 parser->skip_stack->type = SKIP_TO_ENDIF;
1597 }
1598}
Carl Worth80dc60b2010-05-25 14:42:00 -07001599
Carl Worthb20d33c2010-05-20 22:27:07 -07001600static void
1601_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1602{
1603 skip_node_t *node;
1604
1605 if (parser->skip_stack == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001606 glcpp_print (parser->errors, "Error: #endif without #if\n");
Carl Worthb20d33c2010-05-20 22:27:07 -07001607 exit (1);
1608 }
1609
1610 node = parser->skip_stack;
1611 parser->skip_stack = node->next;
1612 talloc_free (node);
1613}