blob: c5c03b68fbd6d166cbe2933927a910a86cd42111 [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
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700144%error-verbose
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700145
Carl Worth0b27b5f2010-05-10 16:16:06 -0700146%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700147%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700148
Carl Worth050e3de2010-05-27 14:36:29 -0700149%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 -0700150%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700151%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700152%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700153%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700154%type <token> preprocessing_token
155%type <token_list> pp_tokens replacement_list text_line
Carl Worth8fed1cd2010-05-26 09:32:12 -0700156%left OR
157%left AND
158%left '|'
159%left '^'
160%left '&'
161%left EQUAL NOT_EQUAL
162%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
163%left LEFT_SHIFT RIGHT_SHIFT
164%left '+' '-'
165%left '*' '/' '%'
166%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700167
168%%
169
Carl Worth33cc4002010-05-12 12:17:10 -0700170input:
Carl Worth3ff81672010-05-25 13:09:03 -0700171 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700172| input line
173;
174
175line:
176 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700177 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700178 }
Carl Worth808401f2010-05-25 14:52:43 -0700179| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700180 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700181 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700182 talloc_free ($1);
183 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700184| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700185| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700186;
187
Carl Worth8e82fcb2010-05-26 11:15:21 -0700188expanded_line:
189 IF_EXPANDED expression NEWLINE {
190 _glcpp_parser_skip_stack_push_if (parser, $2);
191 }
192| ELIF_EXPANDED expression NEWLINE {
193 _glcpp_parser_skip_stack_change_if (parser, "elif", $2);
194 }
195;
196
Carl Worth3ff81672010-05-25 13:09:03 -0700197control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700198 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Carl Worthae6517f2010-05-25 15:24:59 -0700199 _define_object_macro (parser, $2, $3);
200 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700201| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
202 _define_function_macro (parser, $2, NULL, $5);
203 }
204| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
205 _define_function_macro (parser, $2, $4, $6);
206 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700207| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700208 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700209 if (macro) {
210 /* XXX: Need hash table to support a real way
211 * to remove an element rather than prefixing
212 * a new node with data of NULL like this. */
213 hash_table_insert (parser->defines, NULL, $2);
214 talloc_free (macro);
215 }
216 talloc_free ($2);
217 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700218| HASH_IF pp_tokens NEWLINE {
219 token_list_t *expanded;
220 token_t *token;
221
222 expanded = _token_list_create (parser);
223 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
224 _token_list_append (expanded, token);
225 talloc_unlink (parser, token);
226 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700227 _glcpp_parser_expand_token_list (parser, $2);
228 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700229 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700230 }
231| HASH_IFDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700232 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700233 talloc_free ($2);
234 _glcpp_parser_skip_stack_push_if (parser, macro != NULL);
235 }
236| HASH_IFNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700237 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700238 talloc_free ($2);
239 _glcpp_parser_skip_stack_push_if (parser, macro == NULL);
240 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700241| HASH_ELIF pp_tokens NEWLINE {
242 token_list_t *expanded;
243 token_t *token;
244
245 expanded = _token_list_create (parser);
246 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
247 _token_list_append (expanded, token);
248 talloc_unlink (parser, token);
249 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700250 _glcpp_parser_expand_token_list (parser, $2);
251 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700252 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700253 }
254| HASH_ELSE NEWLINE {
255 _glcpp_parser_skip_stack_change_if (parser, "else", 1);
256 }
257| HASH_ENDIF NEWLINE {
258 _glcpp_parser_skip_stack_pop (parser);
259 }
Carl Worth3ff81672010-05-25 13:09:03 -0700260| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700261;
262
Carl Worth8fed1cd2010-05-26 09:32:12 -0700263expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700264 INTEGER_STRING {
265 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
266 $$ = strtoll ($1 + 2, NULL, 16);
267 } else if ($1[0] == '0') {
268 $$ = strtoll ($1, NULL, 8);
269 } else {
270 $$ = strtoll ($1, NULL, 10);
271 }
272 }
273| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700274 $$ = $1;
275 }
276| expression OR expression {
277 $$ = $1 || $3;
278 }
279| expression AND expression {
280 $$ = $1 && $3;
281 }
282| expression '|' expression {
283 $$ = $1 | $3;
284 }
285| expression '^' expression {
286 $$ = $1 ^ $3;
287 }
288| expression '&' expression {
289 $$ = $1 & $3;
290 }
291| expression NOT_EQUAL expression {
292 $$ = $1 != $3;
293 }
294| expression EQUAL expression {
295 $$ = $1 == $3;
296 }
297| expression GREATER_OR_EQUAL expression {
298 $$ = $1 >= $3;
299 }
300| expression LESS_OR_EQUAL expression {
301 $$ = $1 <= $3;
302 }
303| expression '>' expression {
304 $$ = $1 > $3;
305 }
306| expression '<' expression {
307 $$ = $1 < $3;
308 }
309| expression RIGHT_SHIFT expression {
310 $$ = $1 >> $3;
311 }
312| expression LEFT_SHIFT expression {
313 $$ = $1 << $3;
314 }
315| expression '-' expression {
316 $$ = $1 - $3;
317 }
318| expression '+' expression {
319 $$ = $1 + $3;
320 }
321| expression '%' expression {
322 $$ = $1 % $3;
323 }
324| expression '/' expression {
325 $$ = $1 / $3;
326 }
327| expression '*' expression {
328 $$ = $1 * $3;
329 }
330| '!' expression %prec UNARY {
331 $$ = ! $2;
332 }
333| '~' expression %prec UNARY {
334 $$ = ~ $2;
335 }
336| '-' expression %prec UNARY {
337 $$ = - $2;
338 }
339| '+' expression %prec UNARY {
340 $$ = + $2;
341 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700342| '(' expression ')' {
343 $$ = $2;
344 }
345;
346
Carl Worth3ff81672010-05-25 13:09:03 -0700347identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700348 IDENTIFIER {
349 $$ = _string_list_create (parser);
350 _string_list_append_item ($$, $1);
351 talloc_steal ($$, $1);
352 }
353| identifier_list ',' IDENTIFIER {
354 $$ = $1;
355 _string_list_append_item ($$, $3);
356 talloc_steal ($$, $3);
357 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700358;
359
Carl Worth3ff81672010-05-25 13:09:03 -0700360text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700361 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700362| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700363;
364
Carl Worth3ff81672010-05-25 13:09:03 -0700365non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700366 pp_tokens NEWLINE {
367 yyerror (parser, "Invalid tokens after #");
368 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700369;
370
Carl Worthaaa9acb2010-05-19 13:28:24 -0700371replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700372 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700373| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700374;
375
Carl Worthaaa9acb2010-05-19 13:28:24 -0700376pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700377 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700378 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700379 $$ = _token_list_create (parser);
380 _token_list_append ($$, $1);
381 talloc_unlink (parser, $1);
382 }
383| pp_tokens preprocessing_token {
384 $$ = $1;
385 _token_list_append ($$, $2);
386 talloc_unlink (parser, $2);
387 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700388;
389
Carl Worth3ff81672010-05-25 13:09:03 -0700390preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700391 IDENTIFIER {
392 $$ = _token_create_str (parser, IDENTIFIER, $1);
393 }
Carl Worth050e3de2010-05-27 14:36:29 -0700394| INTEGER_STRING {
395 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700396 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700397| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700398 $$ = _token_create_ival (parser, $1, $1);
399 }
400| OTHER {
401 $$ = _token_create_str (parser, OTHER, $1);
402 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700403| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700404 $$ = _token_create_ival (parser, SPACE, SPACE);
Carl Worthb1854fd2010-05-25 16:28:26 -0700405 }
Carl Worth3ff81672010-05-25 13:09:03 -0700406;
407
Carl Worth8e82fcb2010-05-26 11:15:21 -0700408operator:
Carl Worth808401f2010-05-25 14:52:43 -0700409 '[' { $$ = '['; }
410| ']' { $$ = ']'; }
411| '(' { $$ = '('; }
412| ')' { $$ = ')'; }
413| '{' { $$ = '{'; }
414| '}' { $$ = '}'; }
415| '.' { $$ = '.'; }
416| '&' { $$ = '&'; }
417| '*' { $$ = '*'; }
418| '+' { $$ = '+'; }
419| '-' { $$ = '-'; }
420| '~' { $$ = '~'; }
421| '!' { $$ = '!'; }
422| '/' { $$ = '/'; }
423| '%' { $$ = '%'; }
424| LEFT_SHIFT { $$ = LEFT_SHIFT; }
425| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
426| '<' { $$ = '<'; }
427| '>' { $$ = '>'; }
428| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
429| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
430| EQUAL { $$ = EQUAL; }
431| NOT_EQUAL { $$ = NOT_EQUAL; }
432| '^' { $$ = '^'; }
433| '|' { $$ = '|'; }
434| AND { $$ = AND; }
435| OR { $$ = OR; }
436| ';' { $$ = ';'; }
437| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700438| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700439| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700440| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700441;
442
Carl Worth33cc4002010-05-12 12:17:10 -0700443%%
444
Carl Worth610053b2010-05-14 10:05:11 -0700445string_list_t *
446_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700447{
Carl Worth610053b2010-05-14 10:05:11 -0700448 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700449
Carl Worth610053b2010-05-14 10:05:11 -0700450 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700451 list->head = NULL;
452 list->tail = NULL;
453
454 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700455}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700456
Carl Worth33cc4002010-05-12 12:17:10 -0700457void
Carl Worth610053b2010-05-14 10:05:11 -0700458_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700459{
460 if (list->head == NULL) {
461 list->head = tail->head;
462 } else {
463 list->tail->next = tail->head;
464 }
465
466 list->tail = tail->tail;
467}
468
469void
Carl Worth610053b2010-05-14 10:05:11 -0700470_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700471{
Carl Worth610053b2010-05-14 10:05:11 -0700472 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700473
Carl Worth610053b2010-05-14 10:05:11 -0700474 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700475 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700476
Carl Worth33cc4002010-05-12 12:17:10 -0700477 node->next = NULL;
478
479 if (list->head == NULL) {
480 list->head = node;
481 } else {
482 list->tail->next = node;
483 }
484
485 list->tail = node;
486}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700487
488int
Carl Worth610053b2010-05-14 10:05:11 -0700489_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700490{
Carl Worth610053b2010-05-14 10:05:11 -0700491 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700492 int i;
493
494 if (list == NULL)
495 return 0;
496
497 for (i = 0, node = list->head; node; i++, node = node->next) {
498 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700499 if (index)
500 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700501 return 1;
502 }
503 }
504
505 return 0;
506}
507
508int
Carl Worth610053b2010-05-14 10:05:11 -0700509_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700510{
511 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700512 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700513
514 if (list == NULL)
515 return 0;
516
517 for (node = list->head; node; node = node->next)
518 length++;
519
520 return length;
521}
522
Carl Worth8f6a8282010-05-14 10:44:19 -0700523argument_list_t *
524_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700525{
Carl Worth8f6a8282010-05-14 10:44:19 -0700526 argument_list_t *list;
527
528 list = xtalloc (ctx, argument_list_t);
529 list->head = NULL;
530 list->tail = NULL;
531
532 return list;
533}
534
535void
Carl Worth47252442010-05-19 13:54:37 -0700536_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700537{
538 argument_node_t *node;
539
Carl Worth8f6a8282010-05-14 10:44:19 -0700540 node = xtalloc (list, argument_node_t);
541 node->argument = argument;
542
543 node->next = NULL;
544
545 if (list->head == NULL) {
546 list->head = node;
547 } else {
548 list->tail->next = node;
549 }
550
551 list->tail = node;
552}
553
554int
555_argument_list_length (argument_list_t *list)
556{
557 int length = 0;
558 argument_node_t *node;
559
560 if (list == NULL)
561 return 0;
562
563 for (node = list->head; node; node = node->next)
564 length++;
565
566 return length;
567}
568
Carl Worth47252442010-05-19 13:54:37 -0700569token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700570_argument_list_member_at (argument_list_t *list, int index)
571{
572 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700573 int i;
574
575 if (list == NULL)
576 return NULL;
577
578 node = list->head;
579 for (i = 0; i < index; i++) {
580 node = node->next;
581 if (node == NULL)
582 break;
583 }
584
585 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700586 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700587
588 return NULL;
589}
Carl Worth47252442010-05-19 13:54:37 -0700590
Carl Worth808401f2010-05-25 14:52:43 -0700591/* Note: This function talloc_steal()s the str pointer. */
592token_t *
593_token_create_str (void *ctx, int type, char *str)
594{
595 token_t *token;
596
597 token = xtalloc (ctx, token_t);
598 token->type = type;
599 token->value.str = talloc_steal (token, str);
600
601 return token;
602}
603
604token_t *
605_token_create_ival (void *ctx, int type, int ival)
606{
607 token_t *token;
608
609 token = xtalloc (ctx, token_t);
610 token->type = type;
611 token->value.ival = ival;
612
613 return token;
614}
615
Carl Worth47252442010-05-19 13:54:37 -0700616token_list_t *
617_token_list_create (void *ctx)
618{
619 token_list_t *list;
620
621 list = xtalloc (ctx, token_list_t);
622 list->head = NULL;
623 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700624 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700625
626 return list;
627}
628
629void
Carl Worth808401f2010-05-25 14:52:43 -0700630_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700631{
632 token_node_t *node;
633
634 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700635 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700636
637 node->next = NULL;
638
639 if (list->head == NULL) {
640 list->head = node;
641 } else {
642 list->tail->next = node;
643 }
644
645 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700646 if (token->type != SPACE)
647 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700648}
649
650void
651_token_list_append_list (token_list_t *list, token_list_t *tail)
652{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700653 if (tail == NULL || tail->head == NULL)
654 return;
655
Carl Worth47252442010-05-19 13:54:37 -0700656 if (list->head == NULL) {
657 list->head = tail->head;
658 } else {
659 list->tail->next = tail->head;
660 }
661
662 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700663 list->non_space_tail = tail->non_space_tail;
664}
665
Carl Worth681afbc2010-05-28 15:06:02 -0700666token_list_t *
667_token_list_copy (void *ctx, token_list_t *other)
668{
669 token_list_t *copy;
670 token_node_t *node;
671
672 if (other == NULL)
673 return NULL;
674
675 copy = _token_list_create (ctx);
676 for (node = other->head; node; node = node->next)
677 _token_list_append (copy, node->token);
678
679 return copy;
680}
681
Carl Worth10ae4382010-05-25 20:35:01 -0700682void
683_token_list_trim_trailing_space (token_list_t *list)
684{
685 token_node_t *tail, *next;
686
687 if (list->non_space_tail) {
688 tail = list->non_space_tail->next;
689 list->non_space_tail->next = NULL;
690 list->tail = list->non_space_tail;
691
692 while (tail) {
693 next = tail->next;
694 talloc_free (tail);
695 tail = next;
696 }
697 }
Carl Worth47252442010-05-19 13:54:37 -0700698}
Carl Worth80dc60b2010-05-25 14:42:00 -0700699
Carl Worth22b3ace2010-06-02 15:32:03 -0700700static int
701_token_list_length (token_list_t *list)
702{
703 int length = 0;
704 token_node_t *node;
705
706 if (list == NULL)
707 return 0;
708
709 for (node = list->head; node; node = node->next)
710 length++;
711
712 return length;
713}
714
Carl Worth0197e9b2010-05-26 08:05:19 -0700715static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700716_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700717{
718 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700719 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700720 return;
721 }
722
723 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700724 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700725 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700726 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700727 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700728 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700729 case OTHER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700730 glcpp_printf (*out, "%s", token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700731 break;
732 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700733 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700734 break;
735 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700736 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700737 break;
738 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700739 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700740 break;
741 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700742 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700743 break;
744 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700745 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700746 break;
747 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700748 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700749 break;
750 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700751 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700752 break;
753 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700754 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700755 break;
756 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700757 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700758 break;
759 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700760 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700761 break;
Carl Worthdd749002010-05-27 10:12:33 -0700762 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700763 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700764 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700765 case PLACEHOLDER:
766 /* Nothing to print. */
767 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700768 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700769 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700770 break;
771 }
772}
773
Carl Worthb06096e2010-05-29 05:54:19 -0700774/* Return a new token (talloc()ed off of 'token') formed by pasting
775 * 'token' and 'other'. Note that this function may return 'token' or
776 * 'other' directly rather than allocating anything new.
777 *
778 * Caution: Only very cursory error-checking is performed to see if
779 * the final result is a valid single token. */
780static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700781_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700782{
Carl Worth85b50e82010-05-27 14:01:18 -0700783 /* Pasting a placeholder onto anything makes no change. */
784 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700785 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700786
Carl Worthb06096e2010-05-29 05:54:19 -0700787 /* When 'token' is a placeholder, just return 'other'. */
788 if (token->type == PLACEHOLDER)
789 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700790
Carl Worthad0dee62010-05-26 09:04:50 -0700791 /* A very few single-character punctuators can be combined
792 * with another to form a multi-character punctuator. */
793 switch (token->type) {
794 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700795 if (other->type == '<')
796 return _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
797 else if (other->type == '=')
798 return _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700799 break;
800 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700801 if (other->type == '>')
802 return _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
803 else if (other->type == '=')
804 return _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700805 break;
806 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700807 if (other->type == '=')
808 return _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700809 break;
810 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700811 if (other->type == '=')
812 return _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700813 break;
814 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700815 if (other->type == '&')
816 return _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700817 break;
818 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700819 if (other->type == '|')
820 return _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700821 break;
822 }
823
824 /* Two string-valued tokens can usually just be mashed
825 * together.
826 *
Carl Worth050e3de2010-05-27 14:36:29 -0700827 * XXX: This isn't actually legitimate. Several things here
828 * should result in a diagnostic since the result cannot be a
829 * valid, single pre-processing token. For example, pasting
830 * "123" and "abc" is not legal, but we don't catch that
831 * here. */
832 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
833 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700834 {
Carl Worthb06096e2010-05-29 05:54:19 -0700835 char *str;
836
837 str = xtalloc_asprintf (token, "%s%s",
838 token->value.str, other->value.str);
839 return _token_create_str (token, token->type, str);
Carl Worthad0dee62010-05-26 09:04:50 -0700840 }
841
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700842 glcpp_print (parser->errors, "Error: Pasting \"");
843 _token_print (&parser->errors, token);
844 glcpp_print (parser->errors, "\" and \"");
845 _token_print (&parser->errors, other);
846 glcpp_print (parser->errors, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700847
848 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700849}
850
Carl Worth0197e9b2010-05-26 08:05:19 -0700851static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700852_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700853{
854 token_node_t *node;
855
856 if (list == NULL)
857 return;
858
859 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700860 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700861}
862
Carl Worth3a37b872010-05-10 11:44:09 -0700863void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700864yyerror (glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700865{
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700866 glcpp_printf(parser->errors, "Parse error: %s\n", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700867}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700868
Carl Worth33cc4002010-05-12 12:17:10 -0700869glcpp_parser_t *
870glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700871{
Carl Worth33cc4002010-05-12 12:17:10 -0700872 glcpp_parser_t *parser;
873
Carl Worth5070a202010-05-12 12:45:33 -0700874 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700875
Carl Worth8f38aff2010-05-19 10:01:29 -0700876 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700877 parser->defines = hash_table_ctor (32, hash_table_string_hash,
878 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700879 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700880 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700881 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700882 parser->newline_as_space = 0;
883 parser->in_control_line = 0;
884 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700885
Carl Worthb20d33c2010-05-20 22:27:07 -0700886 parser->skip_stack = NULL;
887
Carl Worth8e82fcb2010-05-26 11:15:21 -0700888 parser->lex_from_list = NULL;
889 parser->lex_from_node = NULL;
890
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700891 parser->output = talloc_strdup(parser, "");
892 parser->errors = talloc_strdup(parser, "");
893
Carl Worth33cc4002010-05-12 12:17:10 -0700894 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700895}
896
897int
898glcpp_parser_parse (glcpp_parser_t *parser)
899{
900 return yyparse (parser);
901}
902
903void
Carl Worth33cc4002010-05-12 12:17:10 -0700904glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700905{
Carl Worthb20d33c2010-05-20 22:27:07 -0700906 if (parser->skip_stack)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700907 glcpp_print (parser->errors, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700908 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700909 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700910 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700911}
Carl Worthc6d5af32010-05-11 12:30:09 -0700912
Carl Worth8e82fcb2010-05-26 11:15:21 -0700913/* Replace any occurences of DEFINED tokens in 'list' with either a
914 * '0' or '1' INTEGER token depending on whether the next token in the
915 * list is defined or not. */
916static void
917_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
918 token_list_t *list)
919{
920 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700921 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700922
923 if (list == NULL)
924 return;
925
926 for (node = list->head; node; node = node->next) {
927 if (node->token->type != DEFINED)
928 continue;
929 next = node->next;
930 while (next && next->token->type == SPACE)
931 next = next->next;
932 if (next == NULL || next->token->type != IDENTIFIER) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700933 glcpp_print (parser->errors, "Error: operator \"defined\" requires an identifier\n");
Carl Worth8e82fcb2010-05-26 11:15:21 -0700934 exit (1);
935 }
936 macro = hash_table_find (parser->defines,
937 next->token->value.str);
938
939 node->token->type = INTEGER;
940 node->token->value.ival = (macro != NULL);
941 node->next = next->next;
942 }
943}
944
Carl Worthb1854fd2010-05-25 16:28:26 -0700945typedef enum function_status
946{
947 FUNCTION_STATUS_SUCCESS,
948 FUNCTION_NOT_A_FUNCTION,
949 FUNCTION_UNBALANCED_PARENTHESES
950} function_status_t;
951
952/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700953 * balanced set of parentheses.
954 *
955 * When called, 'node' should be the opening-parenthesis token, (or
956 * perhaps preceeding SPACE tokens). Upon successful return *last will
957 * be the last consumed node, (corresponding to the closing right
958 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700959 *
960 * Return values:
961 *
962 * FUNCTION_STATUS_SUCCESS:
963 *
964 * Successfully parsed a set of function arguments.
965 *
966 * FUNCTION_NOT_A_FUNCTION:
967 *
968 * Macro name not followed by a '('. This is not an error, but
969 * simply that the macro name should be treated as a non-macro.
970 *
Carl Worth14c98a52010-06-02 15:49:54 -0700971 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -0700972 *
973 * Macro name is not followed by a balanced set of parentheses.
974 */
975static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700976_arguments_parse (argument_list_t *arguments,
977 token_node_t *node,
978 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700979{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700980 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700981 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700982
Carl Worthb1854fd2010-05-25 16:28:26 -0700983 node = node->next;
984
985 /* Ignore whitespace before first parenthesis. */
986 while (node && node->token->type == SPACE)
987 node = node->next;
988
989 if (node == NULL || node->token->type != '(')
990 return FUNCTION_NOT_A_FUNCTION;
991
Carl Worth652fa272010-05-25 17:45:22 -0700992 node = node->next;
993
Carl Wortha19297b2010-05-27 13:29:19 -0700994 argument = _token_list_create (arguments);
995 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -0700996
Carl Worth681afbc2010-05-28 15:06:02 -0700997 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -0700998 if (node->token->type == '(')
999 {
1000 paren_count++;
1001 }
1002 else if (node->token->type == ')')
1003 {
1004 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001005 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001006 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001007 }
Carl Worth652fa272010-05-25 17:45:22 -07001008
1009 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001010 paren_count == 1)
1011 {
Carl Wortha19297b2010-05-27 13:29:19 -07001012 _token_list_trim_trailing_space (argument);
1013 argument = _token_list_create (arguments);
1014 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001015 }
1016 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001017 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001018 /* Don't treat initial whitespace as
1019 * part of the arguement. */
1020 if (node->token->type == SPACE)
1021 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001022 }
1023 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001024 }
Carl Worthc7581c22010-05-25 17:41:07 -07001025 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001026
Carl Worth681afbc2010-05-28 15:06:02 -07001027 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001028 return FUNCTION_UNBALANCED_PARENTHESES;
1029
Carl Worth681afbc2010-05-28 15:06:02 -07001030 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001031
1032 return FUNCTION_STATUS_SUCCESS;
1033}
1034
Carl Worth681afbc2010-05-28 15:06:02 -07001035/* This is a helper function that's essentially part of the
1036 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1037 * except for by that function.
1038 *
1039 * Returns NULL if node is a simple token with no expansion, (that is,
1040 * although 'node' corresponds to an identifier defined as a
1041 * function-like macro, it is not followed with a parenthesized
1042 * argument list).
1043 *
1044 * Compute the complete expansion of node (which is a function-like
1045 * macro) and subsequent nodes which are arguments.
1046 *
1047 * Returns the token list that results from the expansion and sets
1048 * *last to the last node in the list that was consumed by the
1049 * expansion. Specificallty, *last will be set as follows: as the
1050 * token of the closing right parenthesis.
1051 */
1052static token_list_t *
1053_glcpp_parser_expand_function (glcpp_parser_t *parser,
1054 token_node_t *node,
1055 token_node_t **last)
1056
Carl Worthb1854fd2010-05-25 16:28:26 -07001057{
1058 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001059 const char *identifier;
1060 argument_list_t *arguments;
1061 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001062 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001063 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001064
Carl Worthb1854fd2010-05-25 16:28:26 -07001065 identifier = node->token->value.str;
1066
1067 macro = hash_table_find (parser->defines, identifier);
1068
1069 assert (macro->is_function);
1070
Carl Worth9ce18cf2010-05-25 17:32:21 -07001071 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001072 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001073
1074 switch (status) {
1075 case FUNCTION_STATUS_SUCCESS:
1076 break;
1077 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001078 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001079 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001080 glcpp_printf (parser->errors, "Error: Macro %s call has unbalanced parentheses\n",
1081 identifier);
Carl Worth14c98a52010-06-02 15:49:54 -07001082 exit (1);
Carl Worth681afbc2010-05-28 15:06:02 -07001083 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001084 }
1085
Carl Worth9ce18cf2010-05-25 17:32:21 -07001086 if (macro->replacements == NULL) {
1087 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001088 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001089 }
1090
Carl Wortha19297b2010-05-27 13:29:19 -07001091 if (! ((_argument_list_length (arguments) ==
1092 _string_list_length (macro->parameters)) ||
1093 (_string_list_length (macro->parameters) == 0 &&
1094 _argument_list_length (arguments) == 1 &&
1095 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001096 {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001097 glcpp_printf (parser->errors,
1098 "Error: macro %s invoked with %d arguments (expected %d)\n",
1099 identifier,
1100 _argument_list_length (arguments),
1101 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001102 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001103 }
1104
Carl Worth0197e9b2010-05-26 08:05:19 -07001105 /* Perform argument substitution on the replacement list. */
1106 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001107
Carl Worthce540f22010-05-26 08:25:44 -07001108 for (node = macro->replacements->head; node; node = node->next)
1109 {
1110 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001111 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001112 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001113 &parameter_index))
1114 {
1115 token_list_t *argument;
1116 argument = _argument_list_member_at (arguments,
1117 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001118 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001119 * tokens, or append a placeholder token for
1120 * an empty argument. */
1121 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001122 _glcpp_parser_expand_token_list (parser,
1123 argument);
1124 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001125 } else {
1126 token_t *new_token;
1127
1128 new_token = _token_create_ival (substituted,
1129 PLACEHOLDER,
1130 PLACEHOLDER);
1131 _token_list_append (substituted, new_token);
1132 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001133 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001134 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001135 }
1136 }
1137
Carl Worthad0dee62010-05-26 09:04:50 -07001138 /* After argument substitution, and before further expansion
1139 * below, implement token pasting. */
1140
Carl Worthb06096e2010-05-29 05:54:19 -07001141 _token_list_trim_trailing_space (substituted);
1142
Carl Worthad0dee62010-05-26 09:04:50 -07001143 node = substituted->head;
1144 while (node)
1145 {
1146 token_node_t *next_non_space;
1147
1148 /* Look ahead for a PASTE token, skipping space. */
1149 next_non_space = node->next;
1150 while (next_non_space && next_non_space->token->type == SPACE)
1151 next_non_space = next_non_space->next;
1152
1153 if (next_non_space == NULL)
1154 break;
1155
1156 if (next_non_space->token->type != PASTE) {
1157 node = next_non_space;
1158 continue;
1159 }
1160
1161 /* Now find the next non-space token after the PASTE. */
1162 next_non_space = next_non_space->next;
1163 while (next_non_space && next_non_space->token->type == SPACE)
1164 next_non_space = next_non_space->next;
1165
1166 if (next_non_space == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001167 glcpp_print (parser->errors, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001168 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001169 }
1170
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001171 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001172 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001173 if (next_non_space == substituted->tail)
1174 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001175
1176 node = node->next;
1177 }
1178
Carl Worthb06096e2010-05-29 05:54:19 -07001179 substituted->non_space_tail = substituted->tail;
1180
Carl Worth681afbc2010-05-28 15:06:02 -07001181 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001182}
1183
Carl Worth681afbc2010-05-28 15:06:02 -07001184/* Compute the complete expansion of node, (and subsequent nodes after
1185 * 'node' in the case that 'node' is a function-like macro and
1186 * subsequent nodes are arguments).
1187 *
1188 * Returns NULL if node is a simple token with no expansion.
1189 *
1190 * Otherwise, returns the token list that results from the expansion
1191 * and sets *last to the last node in the list that was consumed by
1192 * the expansion. Specificallty, *last will be set as follows:
1193 *
1194 * As 'node' in the case of object-like macro expansion.
1195 *
1196 * As the token of the closing right parenthesis in the case of
1197 * function-like macro expansion.
1198 */
1199static token_list_t *
1200_glcpp_parser_expand_node (glcpp_parser_t *parser,
1201 token_node_t *node,
1202 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001203{
Carl Worth681afbc2010-05-28 15:06:02 -07001204 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001205 const char *identifier;
1206 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001207
1208 /* We only expand identifiers */
1209 if (token->type != IDENTIFIER) {
1210 /* We change any COMMA into a COMMA_FINAL to prevent
1211 * it being mistaken for an argument separator
1212 * later. */
1213 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001214 token->type = COMMA_FINAL;
1215 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001216 }
Carl Worth681afbc2010-05-28 15:06:02 -07001217
1218 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001219 }
1220
1221 /* Look up this identifier in the hash table. */
1222 identifier = token->value.str;
1223 macro = hash_table_find (parser->defines, identifier);
1224
Carl Worth681afbc2010-05-28 15:06:02 -07001225 /* Not a macro, so no expansion needed. */
1226 if (macro == NULL)
1227 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001228
1229 /* Finally, don't expand this macro if we're already actively
1230 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001231 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001232 /* We change the token type here from IDENTIFIER to
1233 * OTHER to prevent any future expansion of this
1234 * unexpanded token. */
1235 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001236 token_list_t *expansion;
1237 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001238
Carl Worth681afbc2010-05-28 15:06:02 -07001239 str = xtalloc_strdup (parser, token->value.str);
1240 final = _token_create_str (parser, OTHER, str);
1241 expansion = _token_list_create (parser);
1242 _token_list_append (expansion, final);
1243 *last = node;
1244 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001245 }
1246
Carl Worth681afbc2010-05-28 15:06:02 -07001247 if (! macro->is_function)
1248 {
1249 *last = node;
1250
1251 if (macro->replacements == NULL)
1252 return _token_list_create (parser);
1253
Carl Worth22b3ace2010-06-02 15:32:03 -07001254 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001255 }
Carl Worth681afbc2010-05-28 15:06:02 -07001256
1257 return _glcpp_parser_expand_function (parser, node, last);
1258}
1259
Carl Worth22b3ace2010-06-02 15:32:03 -07001260/* Push a new identifier onto the active list, returning the new list.
1261 *
1262 * Here, 'marker' is the token node that appears in the list after the
1263 * expansion of 'identifier'. That is, when the list iterator begins
1264 * examinging 'marker', then it is time to pop this node from the
1265 * active stack.
1266 */
1267active_list_t *
1268_active_list_push (active_list_t *list,
1269 const char *identifier,
1270 token_node_t *marker)
1271{
1272 active_list_t *node;
1273
1274 node = xtalloc (list, active_list_t);
1275 node->identifier = xtalloc_strdup (node, identifier);
1276 node->marker = marker;
1277 node->next = list;
1278
1279 return node;
1280}
1281
1282active_list_t *
1283_active_list_pop (active_list_t *list)
1284{
1285 active_list_t *node = list;
1286
1287 if (node == NULL)
1288 return NULL;
1289
1290 node = list->next;
1291 talloc_free (list);
1292
1293 return node;
1294}
1295
1296int
1297_active_list_contains (active_list_t *list, const char *identifier)
1298{
1299 active_list_t *node;
1300
1301 if (list == NULL)
1302 return 0;
1303
1304 for (node = list; node; node = node->next)
1305 if (strcmp (node->identifier, identifier) == 0)
1306 return 1;
1307
1308 return 0;
1309}
1310
Carl Worth681afbc2010-05-28 15:06:02 -07001311/* Walk over the token list replacing nodes with their expansion.
1312 * Whenever nodes are expanded the walking will walk over the new
1313 * nodes, continuing to expand as necessary. The results are placed in
1314 * 'list' itself;
1315 */
1316static void
1317_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1318 token_list_t *list)
1319{
1320 token_node_t *node_prev;
1321 token_node_t *node, *last;
1322 token_list_t *expansion;
1323
1324 if (list == NULL)
1325 return;
1326
1327 _token_list_trim_trailing_space (list);
1328
1329 node_prev = NULL;
1330 node = list->head;
1331
1332 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001333
1334 while (parser->active && parser->active->marker == node)
1335 parser->active = _active_list_pop (parser->active);
1336
Carl Worth681afbc2010-05-28 15:06:02 -07001337 /* Find the expansion for node, which will replace all
1338 * nodes from node to last, inclusive. */
1339 expansion = _glcpp_parser_expand_node (parser, node, &last);
1340 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001341 token_node_t *n;
1342
1343 for (n = node; n != last->next; n = n->next)
1344 while (parser->active &&
1345 parser->active->marker == n)
1346 {
1347 parser->active = _active_list_pop (parser->active);
1348 }
1349
1350 parser->active = _active_list_push (parser->active,
1351 node->token->value.str,
1352 last->next);
1353
Carl Worth681afbc2010-05-28 15:06:02 -07001354 /* Splice expansion into list, supporting a
1355 * simple deletion if the expansion is
1356 * empty. */
1357 if (expansion->head) {
1358 if (node_prev)
1359 node_prev->next = expansion->head;
1360 else
1361 list->head = expansion->head;
1362 expansion->tail->next = last->next;
1363 if (last == list->tail)
1364 list->tail = expansion->tail;
1365 } else {
1366 if (node_prev)
1367 node_prev->next = last->next;
1368 else
1369 list->head = last->next;
1370 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001371 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001372 }
1373 } else {
1374 node_prev = node;
1375 }
1376 node = node_prev ? node_prev->next : list->head;
1377 }
1378
Carl Worth22b3ace2010-06-02 15:32:03 -07001379 while (parser->active)
1380 parser->active = _active_list_pop (parser->active);
1381
Carl Worth681afbc2010-05-28 15:06:02 -07001382 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001383}
1384
Carl Worthae6517f2010-05-25 15:24:59 -07001385void
1386_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1387 token_list_t *list)
1388{
Carl Worthae6517f2010-05-25 15:24:59 -07001389 if (list == NULL)
1390 return;
1391
Carl Worth681afbc2010-05-28 15:06:02 -07001392 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001393
Carl Worth681afbc2010-05-28 15:06:02 -07001394 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001395
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001396 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001397}
1398
1399void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001400_check_for_reserved_macro_name (glcpp_parser_t *parser, const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001401{
1402 /* According to the GLSL specification, macro names starting with "__"
1403 * or "GL_" are reserved for future use. So, don't allow them.
1404 */
1405 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001406 glcpp_print (parser->errors, "Error: Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001407 exit(1);
1408 }
1409 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001410 glcpp_print (parser->errors, "Error: Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001411 exit(1);
1412 }
1413}
1414
1415void
Carl Worthfcbbb462010-05-13 09:36:23 -07001416_define_object_macro (glcpp_parser_t *parser,
1417 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001418 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001419{
1420 macro_t *macro;
1421
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001422 _check_for_reserved_macro_name(parser, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001423
Carl Worthfcbbb462010-05-13 09:36:23 -07001424 macro = xtalloc (parser, macro_t);
1425
1426 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001427 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001428 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001429 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001430
1431 hash_table_insert (parser->defines, macro, identifier);
1432}
1433
1434void
1435_define_function_macro (glcpp_parser_t *parser,
1436 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001437 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001438 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001439{
1440 macro_t *macro;
1441
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001442 _check_for_reserved_macro_name(parser, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001443
Carl Worthfcbbb462010-05-13 09:36:23 -07001444 macro = xtalloc (parser, macro_t);
1445
1446 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001447 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001448 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001449 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001450
1451 hash_table_insert (parser->defines, macro, identifier);
1452}
1453
Carl Worth8f38aff2010-05-19 10:01:29 -07001454static int
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001455glcpp_parser_lex (YYSTYPE *yylval, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001456{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001457 token_node_t *node;
1458 int ret;
1459
Carl Worth95951ea2010-05-26 15:57:10 -07001460 if (parser->lex_from_list == NULL) {
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001461 ret = glcpp_lex (yylval, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001462
1463 /* XXX: This ugly block of code exists for the sole
1464 * purpose of converting a NEWLINE token into a SPACE
1465 * token, but only in the case where we have seen a
1466 * function-like macro name, but have not yet seen its
1467 * closing parenthesis.
1468 *
1469 * There's perhaps a more compact way to do this with
1470 * mid-rule actions in the grammar.
1471 *
1472 * I'm definitely not pleased with the complexity of
1473 * this code here.
1474 */
1475 if (parser->newline_as_space)
1476 {
1477 if (ret == '(') {
1478 parser->paren_count++;
1479 } else if (ret == ')') {
1480 parser->paren_count--;
1481 if (parser->paren_count == 0)
1482 parser->newline_as_space = 0;
1483 } else if (ret == NEWLINE) {
1484 ret = SPACE;
1485 } else if (ret != SPACE) {
1486 if (parser->paren_count == 0)
1487 parser->newline_as_space = 0;
1488 }
1489 }
1490 else if (parser->in_control_line)
1491 {
1492 if (ret == NEWLINE)
1493 parser->in_control_line = 0;
1494 }
1495 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1496 ret == HASH_UNDEF || ret == HASH_IF ||
1497 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1498 ret == HASH_ELIF || ret == HASH_ELSE ||
1499 ret == HASH_ENDIF || ret == HASH)
1500 {
1501 parser->in_control_line = 1;
1502 }
1503 else if (ret == IDENTIFIER)
1504 {
1505 macro_t *macro;
1506 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001507 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001508 if (macro && macro->is_function) {
1509 parser->newline_as_space = 1;
1510 parser->paren_count = 0;
1511 }
1512 }
1513
1514 return ret;
1515 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001516
1517 node = parser->lex_from_node;
1518
1519 if (node == NULL) {
1520 talloc_free (parser->lex_from_list);
1521 parser->lex_from_list = NULL;
1522 return NEWLINE;
1523 }
1524
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001525 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001526 ret = node->token->type;
1527
1528 parser->lex_from_node = node->next;
1529
1530 return ret;
1531}
1532
1533static void
1534glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1535{
1536 token_node_t *node;
1537
1538 assert (parser->lex_from_list == NULL);
1539
1540 /* Copy list, eliminating any space tokens. */
1541 parser->lex_from_list = _token_list_create (parser);
1542
1543 for (node = list->head; node; node = node->next) {
1544 if (node->token->type == SPACE)
1545 continue;
1546 _token_list_append (parser->lex_from_list, node->token);
1547 }
1548
1549 talloc_free (list);
1550
1551 parser->lex_from_node = parser->lex_from_list->head;
1552
1553 /* It's possible the list consisted of nothing but whitespace. */
1554 if (parser->lex_from_node == NULL) {
1555 talloc_free (parser->lex_from_list);
1556 parser->lex_from_list = NULL;
1557 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001558}
Carl Worthb20d33c2010-05-20 22:27:07 -07001559
1560static void
1561_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1562{
1563 skip_type_t current = SKIP_NO_SKIP;
1564 skip_node_t *node;
1565
1566 if (parser->skip_stack)
1567 current = parser->skip_stack->type;
1568
1569 node = xtalloc (parser, skip_node_t);
1570
1571 if (current == SKIP_NO_SKIP) {
1572 if (condition)
1573 node->type = SKIP_NO_SKIP;
1574 else
1575 node->type = SKIP_TO_ELSE;
1576 } else {
1577 node->type = SKIP_TO_ENDIF;
1578 }
1579
1580 node->next = parser->skip_stack;
1581 parser->skip_stack = node;
1582}
1583
1584static void
1585_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1586 int condition)
1587{
1588 if (parser->skip_stack == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001589 glcpp_printf (parser->errors, "Error: %s without #if\n", type);
Carl Worthb20d33c2010-05-20 22:27:07 -07001590 exit (1);
1591 }
1592
1593 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1594 if (condition)
1595 parser->skip_stack->type = SKIP_NO_SKIP;
1596 } else {
1597 parser->skip_stack->type = SKIP_TO_ENDIF;
1598 }
1599}
Carl Worth80dc60b2010-05-25 14:42:00 -07001600
Carl Worthb20d33c2010-05-20 22:27:07 -07001601static void
1602_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1603{
1604 skip_node_t *node;
1605
1606 if (parser->skip_stack == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001607 glcpp_print (parser->errors, "Error: #endif without #if\n");
Carl Worthb20d33c2010-05-20 22:27:07 -07001608 exit (1);
1609 }
1610
1611 node = parser->skip_stack;
1612 parser->skip_stack = node->next;
1613 talloc_free (node);
1614}