blob: f26dd9a0dbc4718295d34ae9fb5169d62be6d888 [file] [log] [blame]
Carl Worth3a37b872010-05-10 11:44:09 -07001%{
2/*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
Carl Worthfcbbb462010-05-13 09:36:23 -070027#include <assert.h>
Carl Worth8fed1cd2010-05-26 09:32:12 -070028#include <inttypes.h>
Carl Worth3a37b872010-05-10 11:44:09 -070029
Carl Wortha1e32bc2010-05-10 13:17:25 -070030#include "glcpp.h"
31
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -070032#define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
33#define glcpp_printf(stream, fmt, args...) \
34 stream = talloc_asprintf_append(stream, fmt, args)
35
Carl Worth5aa7ea02010-05-25 18:39:43 -070036static void
Kenneth Graunke465e03e2010-06-16 16:35:57 -070037yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070038
Carl Worth5aa7ea02010-05-25 18:39:43 -070039static void
Carl Worthfcbbb462010-05-13 09:36:23 -070040_define_object_macro (glcpp_parser_t *parser,
41 const char *macro,
Carl Worth47252442010-05-19 13:54:37 -070042 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070043
Carl Worth5aa7ea02010-05-25 18:39:43 -070044static void
Carl Worthfcbbb462010-05-13 09:36:23 -070045_define_function_macro (glcpp_parser_t *parser,
46 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070047 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070048 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070049
Carl Worth5aa7ea02010-05-25 18:39:43 -070050static string_list_t *
Carl Worth610053b2010-05-14 10:05:11 -070051_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070052
Carl Worth5aa7ea02010-05-25 18:39:43 -070053static void
Carl Worth610053b2010-05-14 10:05:11 -070054_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070055
Carl Worth5aa7ea02010-05-25 18:39:43 -070056static void
Carl Worth610053b2010-05-14 10:05:11 -070057_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070058
Carl Worth5aa7ea02010-05-25 18:39:43 -070059static int
Carl Worth610053b2010-05-14 10:05:11 -070060_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070061
Carl Worth5aa7ea02010-05-25 18:39:43 -070062static int
Carl Worth610053b2010-05-14 10:05:11 -070063_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070064
Carl Worth5aa7ea02010-05-25 18:39:43 -070065static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070066_argument_list_create (void *ctx);
67
Carl Worth5aa7ea02010-05-25 18:39:43 -070068static void
Carl Worth47252442010-05-19 13:54:37 -070069_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070070
Carl Worth5aa7ea02010-05-25 18:39:43 -070071static int
Carl Worth8f6a8282010-05-14 10:44:19 -070072_argument_list_length (argument_list_t *list);
73
Carl Worth5aa7ea02010-05-25 18:39:43 -070074static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070075_argument_list_member_at (argument_list_t *list, int index);
76
Carl Worth808401f2010-05-25 14:52:43 -070077/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070078static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070079_token_create_str (void *ctx, int type, char *str);
80
Carl Worth5aa7ea02010-05-25 18:39:43 -070081static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070082_token_create_ival (void *ctx, int type, int ival);
83
Carl Worth5aa7ea02010-05-25 18:39:43 -070084static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070085_token_list_create (void *ctx);
86
Carl Worthb1ae61a2010-05-26 08:10:38 -070087/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070088 *
89 * You may want to talloc_unlink any current reference if you no
90 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070091static void
Carl Worth808401f2010-05-25 14:52:43 -070092_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070093
Carl Worth5aa7ea02010-05-25 18:39:43 -070094static void
Carl Worth47252442010-05-19 13:54:37 -070095_token_list_append_list (token_list_t *list, token_list_t *tail);
96
Carl Worth22b3ace2010-06-02 15:32:03 -070097static int
98_token_list_length (token_list_t *list);
99
100static active_list_t *
101_active_list_push (active_list_t *list,
102 const char *identifier,
103 token_node_t *marker);
104
105static active_list_t *
106_active_list_pop (active_list_t *list);
107
108int
109_active_list_contains (active_list_t *list, const char *identifier);
110
Carl Worth5aa7ea02010-05-25 18:39:43 -0700111static void
Carl Worth8e82fcb2010-05-26 11:15:21 -0700112_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
113 token_list_t *list);
114
115static void
Carl Worth681afbc2010-05-28 15:06:02 -0700116_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
117 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700118
Carl Worthaaa9acb2010-05-19 13:28:24 -0700119static void
Carl Worth681afbc2010-05-28 15:06:02 -0700120_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
121 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700122
123static void
Carl Worthb20d33c2010-05-20 22:27:07 -0700124_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition);
125
126static void
127_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
128 int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700129
Carl Worthb20d33c2010-05-20 22:27:07 -0700130static void
131_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser);
132
Carl Worth0293b2e2010-05-19 10:05:40 -0700133#define yylex glcpp_parser_lex
134
Carl Worth8f38aff2010-05-19 10:01:29 -0700135static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700136glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700137
Carl Worth8e82fcb2010-05-26 11:15:21 -0700138static void
139glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
140
Carl Worth3a37b872010-05-10 11:44:09 -0700141%}
142
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700143%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700144%error-verbose
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700145%locations
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700146
Carl Worth0b27b5f2010-05-10 16:16:06 -0700147%parse-param {glcpp_parser_t *parser}
Carl Worth0293b2e2010-05-19 10:05:40 -0700148%lex-param {glcpp_parser_t *parser}
Carl Worth38aa8352010-05-10 11:52:29 -0700149
Carl Worth050e3de2010-05-27 14:36:29 -0700150%token COMMA_FINAL DEFINED ELIF_EXPANDED HASH HASH_DEFINE_FUNC HASH_DEFINE_OBJ HASH_ELIF HASH_ELSE HASH_ENDIF HASH_IF HASH_IFDEF HASH_IFNDEF HASH_UNDEF IDENTIFIER IF_EXPANDED INTEGER INTEGER_STRING NEWLINE OTHER PLACEHOLDER SPACE
Carl Worth8fed1cd2010-05-26 09:32:12 -0700151%token PASTE
Carl Worth8e82fcb2010-05-26 11:15:21 -0700152%type <ival> expression INTEGER operator SPACE
Carl Worth050e3de2010-05-27 14:36:29 -0700153%type <str> IDENTIFIER INTEGER_STRING OTHER
Carl Worthb1854fd2010-05-25 16:28:26 -0700154%type <string_list> identifier_list
Carl Worth808401f2010-05-25 14:52:43 -0700155%type <token> preprocessing_token
156%type <token_list> pp_tokens replacement_list text_line
Carl Worth8fed1cd2010-05-26 09:32:12 -0700157%left OR
158%left AND
159%left '|'
160%left '^'
161%left '&'
162%left EQUAL NOT_EQUAL
163%left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
164%left LEFT_SHIFT RIGHT_SHIFT
165%left '+' '-'
166%left '*' '/' '%'
167%right UNARY
Carl Worth3a37b872010-05-10 11:44:09 -0700168
169%%
170
Carl Worth33cc4002010-05-12 12:17:10 -0700171input:
Carl Worth3ff81672010-05-25 13:09:03 -0700172 /* empty */
Carl Worth8e82fcb2010-05-26 11:15:21 -0700173| input line
174;
175
176line:
177 control_line {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700178 glcpp_print(parser->output, "\n");
Carl Worthae6517f2010-05-25 15:24:59 -0700179 }
Carl Worth808401f2010-05-25 14:52:43 -0700180| text_line {
Carl Wortha771a402010-06-01 11:20:18 -0700181 _glcpp_parser_print_expanded_token_list (parser, $1);
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700182 glcpp_print(parser->output, "\n");
Carl Worth808401f2010-05-25 14:52:43 -0700183 talloc_free ($1);
184 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700185| expanded_line
Carl Worth3ff81672010-05-25 13:09:03 -0700186| HASH non_directive
Carl Worthcd27e642010-05-12 13:11:50 -0700187;
188
Carl Worth8e82fcb2010-05-26 11:15:21 -0700189expanded_line:
190 IF_EXPANDED expression NEWLINE {
191 _glcpp_parser_skip_stack_push_if (parser, $2);
192 }
193| ELIF_EXPANDED expression NEWLINE {
194 _glcpp_parser_skip_stack_change_if (parser, "elif", $2);
195 }
196;
197
Carl Worth3ff81672010-05-25 13:09:03 -0700198control_line:
Carl Worthf34a0002010-05-25 16:59:02 -0700199 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
Carl Worthae6517f2010-05-25 15:24:59 -0700200 _define_object_macro (parser, $2, $3);
201 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700202| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
203 _define_function_macro (parser, $2, NULL, $5);
204 }
205| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
206 _define_function_macro (parser, $2, $4, $6);
207 }
Carl Worthe6fb7822010-05-25 15:28:58 -0700208| HASH_UNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700209 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worthe6fb7822010-05-25 15:28:58 -0700210 if (macro) {
211 /* XXX: Need hash table to support a real way
212 * to remove an element rather than prefixing
213 * a new node with data of NULL like this. */
214 hash_table_insert (parser->defines, NULL, $2);
215 talloc_free (macro);
216 }
217 talloc_free ($2);
218 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700219| HASH_IF pp_tokens NEWLINE {
220 token_list_t *expanded;
221 token_t *token;
222
223 expanded = _token_list_create (parser);
224 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
225 _token_list_append (expanded, token);
226 talloc_unlink (parser, token);
227 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700228 _glcpp_parser_expand_token_list (parser, $2);
229 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700230 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700231 }
232| HASH_IFDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700233 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700234 talloc_free ($2);
235 _glcpp_parser_skip_stack_push_if (parser, macro != NULL);
236 }
237| HASH_IFNDEF IDENTIFIER NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700238 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700239 talloc_free ($2);
240 _glcpp_parser_skip_stack_push_if (parser, macro == NULL);
241 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700242| HASH_ELIF pp_tokens NEWLINE {
243 token_list_t *expanded;
244 token_t *token;
245
246 expanded = _token_list_create (parser);
247 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
248 _token_list_append (expanded, token);
249 talloc_unlink (parser, token);
250 _glcpp_parser_evaluate_defined (parser, $2);
Carl Worth681afbc2010-05-28 15:06:02 -0700251 _glcpp_parser_expand_token_list (parser, $2);
252 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700253 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700254 }
255| HASH_ELSE NEWLINE {
256 _glcpp_parser_skip_stack_change_if (parser, "else", 1);
257 }
258| HASH_ENDIF NEWLINE {
259 _glcpp_parser_skip_stack_pop (parser);
260 }
Carl Worth3ff81672010-05-25 13:09:03 -0700261| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700262;
263
Carl Worth8fed1cd2010-05-26 09:32:12 -0700264expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700265 INTEGER_STRING {
266 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
267 $$ = strtoll ($1 + 2, NULL, 16);
268 } else if ($1[0] == '0') {
269 $$ = strtoll ($1, NULL, 8);
270 } else {
271 $$ = strtoll ($1, NULL, 10);
272 }
273 }
274| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700275 $$ = $1;
276 }
277| expression OR expression {
278 $$ = $1 || $3;
279 }
280| expression AND expression {
281 $$ = $1 && $3;
282 }
283| expression '|' expression {
284 $$ = $1 | $3;
285 }
286| expression '^' expression {
287 $$ = $1 ^ $3;
288 }
289| expression '&' expression {
290 $$ = $1 & $3;
291 }
292| expression NOT_EQUAL expression {
293 $$ = $1 != $3;
294 }
295| expression EQUAL expression {
296 $$ = $1 == $3;
297 }
298| expression GREATER_OR_EQUAL expression {
299 $$ = $1 >= $3;
300 }
301| expression LESS_OR_EQUAL expression {
302 $$ = $1 <= $3;
303 }
304| expression '>' expression {
305 $$ = $1 > $3;
306 }
307| expression '<' expression {
308 $$ = $1 < $3;
309 }
310| expression RIGHT_SHIFT expression {
311 $$ = $1 >> $3;
312 }
313| expression LEFT_SHIFT expression {
314 $$ = $1 << $3;
315 }
316| expression '-' expression {
317 $$ = $1 - $3;
318 }
319| expression '+' expression {
320 $$ = $1 + $3;
321 }
322| expression '%' expression {
323 $$ = $1 % $3;
324 }
325| expression '/' expression {
326 $$ = $1 / $3;
327 }
328| expression '*' expression {
329 $$ = $1 * $3;
330 }
331| '!' expression %prec UNARY {
332 $$ = ! $2;
333 }
334| '~' expression %prec UNARY {
335 $$ = ~ $2;
336 }
337| '-' expression %prec UNARY {
338 $$ = - $2;
339 }
340| '+' expression %prec UNARY {
341 $$ = + $2;
342 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700343| '(' expression ')' {
344 $$ = $2;
345 }
346;
347
Carl Worth3ff81672010-05-25 13:09:03 -0700348identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700349 IDENTIFIER {
350 $$ = _string_list_create (parser);
351 _string_list_append_item ($$, $1);
352 talloc_steal ($$, $1);
353 }
354| identifier_list ',' IDENTIFIER {
355 $$ = $1;
356 _string_list_append_item ($$, $3);
357 talloc_steal ($$, $3);
358 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700359;
360
Carl Worth3ff81672010-05-25 13:09:03 -0700361text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700362 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700363| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700364;
365
Carl Worth3ff81672010-05-25 13:09:03 -0700366non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700367 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700368 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700369 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700370;
371
Carl Worthaaa9acb2010-05-19 13:28:24 -0700372replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700373 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700374| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700375;
376
Carl Worthaaa9acb2010-05-19 13:28:24 -0700377pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700378 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700379 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700380 $$ = _token_list_create (parser);
381 _token_list_append ($$, $1);
382 talloc_unlink (parser, $1);
383 }
384| pp_tokens preprocessing_token {
385 $$ = $1;
386 _token_list_append ($$, $2);
387 talloc_unlink (parser, $2);
388 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700389;
390
Carl Worth3ff81672010-05-25 13:09:03 -0700391preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700392 IDENTIFIER {
393 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700394 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700395 }
Carl Worth050e3de2010-05-27 14:36:29 -0700396| INTEGER_STRING {
397 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700398 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700399 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700400| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700401 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700402 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700403 }
404| OTHER {
405 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700406 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700407 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700408| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700409 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700410 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700411 }
Carl Worth3ff81672010-05-25 13:09:03 -0700412;
413
Carl Worth8e82fcb2010-05-26 11:15:21 -0700414operator:
Carl Worth808401f2010-05-25 14:52:43 -0700415 '[' { $$ = '['; }
416| ']' { $$ = ']'; }
417| '(' { $$ = '('; }
418| ')' { $$ = ')'; }
419| '{' { $$ = '{'; }
420| '}' { $$ = '}'; }
421| '.' { $$ = '.'; }
422| '&' { $$ = '&'; }
423| '*' { $$ = '*'; }
424| '+' { $$ = '+'; }
425| '-' { $$ = '-'; }
426| '~' { $$ = '~'; }
427| '!' { $$ = '!'; }
428| '/' { $$ = '/'; }
429| '%' { $$ = '%'; }
430| LEFT_SHIFT { $$ = LEFT_SHIFT; }
431| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
432| '<' { $$ = '<'; }
433| '>' { $$ = '>'; }
434| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
435| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
436| EQUAL { $$ = EQUAL; }
437| NOT_EQUAL { $$ = NOT_EQUAL; }
438| '^' { $$ = '^'; }
439| '|' { $$ = '|'; }
440| AND { $$ = AND; }
441| OR { $$ = OR; }
442| ';' { $$ = ';'; }
443| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700444| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700445| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700446| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700447;
448
Carl Worth33cc4002010-05-12 12:17:10 -0700449%%
450
Carl Worth610053b2010-05-14 10:05:11 -0700451string_list_t *
452_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700453{
Carl Worth610053b2010-05-14 10:05:11 -0700454 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700455
Carl Worth610053b2010-05-14 10:05:11 -0700456 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700457 list->head = NULL;
458 list->tail = NULL;
459
460 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700461}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700462
Carl Worth33cc4002010-05-12 12:17:10 -0700463void
Carl Worth610053b2010-05-14 10:05:11 -0700464_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700465{
466 if (list->head == NULL) {
467 list->head = tail->head;
468 } else {
469 list->tail->next = tail->head;
470 }
471
472 list->tail = tail->tail;
473}
474
475void
Carl Worth610053b2010-05-14 10:05:11 -0700476_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700477{
Carl Worth610053b2010-05-14 10:05:11 -0700478 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700479
Carl Worth610053b2010-05-14 10:05:11 -0700480 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700481 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700482
Carl Worth33cc4002010-05-12 12:17:10 -0700483 node->next = NULL;
484
485 if (list->head == NULL) {
486 list->head = node;
487 } else {
488 list->tail->next = node;
489 }
490
491 list->tail = node;
492}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700493
494int
Carl Worth610053b2010-05-14 10:05:11 -0700495_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700496{
Carl Worth610053b2010-05-14 10:05:11 -0700497 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700498 int i;
499
500 if (list == NULL)
501 return 0;
502
503 for (i = 0, node = list->head; node; i++, node = node->next) {
504 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700505 if (index)
506 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700507 return 1;
508 }
509 }
510
511 return 0;
512}
513
514int
Carl Worth610053b2010-05-14 10:05:11 -0700515_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700516{
517 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700518 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700519
520 if (list == NULL)
521 return 0;
522
523 for (node = list->head; node; node = node->next)
524 length++;
525
526 return length;
527}
528
Carl Worth8f6a8282010-05-14 10:44:19 -0700529argument_list_t *
530_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700531{
Carl Worth8f6a8282010-05-14 10:44:19 -0700532 argument_list_t *list;
533
534 list = xtalloc (ctx, argument_list_t);
535 list->head = NULL;
536 list->tail = NULL;
537
538 return list;
539}
540
541void
Carl Worth47252442010-05-19 13:54:37 -0700542_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700543{
544 argument_node_t *node;
545
Carl Worth8f6a8282010-05-14 10:44:19 -0700546 node = xtalloc (list, argument_node_t);
547 node->argument = argument;
548
549 node->next = NULL;
550
551 if (list->head == NULL) {
552 list->head = node;
553 } else {
554 list->tail->next = node;
555 }
556
557 list->tail = node;
558}
559
560int
561_argument_list_length (argument_list_t *list)
562{
563 int length = 0;
564 argument_node_t *node;
565
566 if (list == NULL)
567 return 0;
568
569 for (node = list->head; node; node = node->next)
570 length++;
571
572 return length;
573}
574
Carl Worth47252442010-05-19 13:54:37 -0700575token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700576_argument_list_member_at (argument_list_t *list, int index)
577{
578 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700579 int i;
580
581 if (list == NULL)
582 return NULL;
583
584 node = list->head;
585 for (i = 0; i < index; i++) {
586 node = node->next;
587 if (node == NULL)
588 break;
589 }
590
591 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700592 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700593
594 return NULL;
595}
Carl Worth47252442010-05-19 13:54:37 -0700596
Carl Worth808401f2010-05-25 14:52:43 -0700597/* Note: This function talloc_steal()s the str pointer. */
598token_t *
599_token_create_str (void *ctx, int type, char *str)
600{
601 token_t *token;
602
603 token = xtalloc (ctx, token_t);
604 token->type = type;
605 token->value.str = talloc_steal (token, str);
606
607 return token;
608}
609
610token_t *
611_token_create_ival (void *ctx, int type, int ival)
612{
613 token_t *token;
614
615 token = xtalloc (ctx, token_t);
616 token->type = type;
617 token->value.ival = ival;
618
619 return token;
620}
621
Carl Worth47252442010-05-19 13:54:37 -0700622token_list_t *
623_token_list_create (void *ctx)
624{
625 token_list_t *list;
626
627 list = xtalloc (ctx, token_list_t);
628 list->head = NULL;
629 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700630 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700631
632 return list;
633}
634
635void
Carl Worth808401f2010-05-25 14:52:43 -0700636_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700637{
638 token_node_t *node;
639
640 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700641 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700642
643 node->next = NULL;
644
645 if (list->head == NULL) {
646 list->head = node;
647 } else {
648 list->tail->next = node;
649 }
650
651 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700652 if (token->type != SPACE)
653 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700654}
655
656void
657_token_list_append_list (token_list_t *list, token_list_t *tail)
658{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700659 if (tail == NULL || tail->head == NULL)
660 return;
661
Carl Worth47252442010-05-19 13:54:37 -0700662 if (list->head == NULL) {
663 list->head = tail->head;
664 } else {
665 list->tail->next = tail->head;
666 }
667
668 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700669 list->non_space_tail = tail->non_space_tail;
670}
671
Carl Worth681afbc2010-05-28 15:06:02 -0700672token_list_t *
673_token_list_copy (void *ctx, token_list_t *other)
674{
675 token_list_t *copy;
676 token_node_t *node;
677
678 if (other == NULL)
679 return NULL;
680
681 copy = _token_list_create (ctx);
682 for (node = other->head; node; node = node->next)
683 _token_list_append (copy, node->token);
684
685 return copy;
686}
687
Carl Worth10ae4382010-05-25 20:35:01 -0700688void
689_token_list_trim_trailing_space (token_list_t *list)
690{
691 token_node_t *tail, *next;
692
693 if (list->non_space_tail) {
694 tail = list->non_space_tail->next;
695 list->non_space_tail->next = NULL;
696 list->tail = list->non_space_tail;
697
698 while (tail) {
699 next = tail->next;
700 talloc_free (tail);
701 tail = next;
702 }
703 }
Carl Worth47252442010-05-19 13:54:37 -0700704}
Carl Worth80dc60b2010-05-25 14:42:00 -0700705
Carl Worth22b3ace2010-06-02 15:32:03 -0700706static int
707_token_list_length (token_list_t *list)
708{
709 int length = 0;
710 token_node_t *node;
711
712 if (list == NULL)
713 return 0;
714
715 for (node = list->head; node; node = node->next)
716 length++;
717
718 return length;
719}
720
Carl Worth0197e9b2010-05-26 08:05:19 -0700721static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700722_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700723{
724 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700725 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700726 return;
727 }
728
729 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700730 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700731 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700732 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700733 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700734 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700735 case OTHER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700736 glcpp_printf (*out, "%s", token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700737 break;
738 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700739 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700740 break;
741 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700742 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700743 break;
744 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700745 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700746 break;
747 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700748 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700749 break;
750 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700751 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700752 break;
753 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700754 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700755 break;
756 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700757 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700758 break;
759 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700760 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700761 break;
762 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700763 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700764 break;
765 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700766 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700767 break;
Carl Worthdd749002010-05-27 10:12:33 -0700768 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700769 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700770 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700771 case PLACEHOLDER:
772 /* Nothing to print. */
773 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700774 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700775 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700776 break;
777 }
778}
779
Carl Worthb06096e2010-05-29 05:54:19 -0700780/* Return a new token (talloc()ed off of 'token') formed by pasting
781 * 'token' and 'other'. Note that this function may return 'token' or
782 * 'other' directly rather than allocating anything new.
783 *
784 * Caution: Only very cursory error-checking is performed to see if
785 * the final result is a valid single token. */
786static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700787_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700788{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700789 token_t *combined = NULL;
790
Carl Worth85b50e82010-05-27 14:01:18 -0700791 /* Pasting a placeholder onto anything makes no change. */
792 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700793 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700794
Carl Worthb06096e2010-05-29 05:54:19 -0700795 /* When 'token' is a placeholder, just return 'other'. */
796 if (token->type == PLACEHOLDER)
797 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700798
Carl Worthad0dee62010-05-26 09:04:50 -0700799 /* A very few single-character punctuators can be combined
800 * with another to form a multi-character punctuator. */
801 switch (token->type) {
802 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700803 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700804 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700805 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700806 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700807 break;
808 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700809 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700810 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700811 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700812 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700813 break;
814 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700815 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700816 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700817 break;
818 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700819 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700820 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700821 break;
822 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700823 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700824 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700825 break;
826 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700827 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700828 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700829 break;
830 }
831
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700832 if (combined != NULL) {
833 /* Inherit the location from the first token */
834 combined->location = token->location;
835 return combined;
836 }
837
Carl Worthad0dee62010-05-26 09:04:50 -0700838 /* Two string-valued tokens can usually just be mashed
839 * together.
840 *
Carl Worth050e3de2010-05-27 14:36:29 -0700841 * XXX: This isn't actually legitimate. Several things here
842 * should result in a diagnostic since the result cannot be a
843 * valid, single pre-processing token. For example, pasting
844 * "123" and "abc" is not legal, but we don't catch that
845 * here. */
846 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
847 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700848 {
Carl Worthb06096e2010-05-29 05:54:19 -0700849 char *str;
850
851 str = xtalloc_asprintf (token, "%s%s",
852 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700853 combined = _token_create_str (token, token->type, str);
854 combined->location = token->location;
855 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700856 }
857
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700858 glcpp_print (parser->errors, "Error: Pasting \"");
859 _token_print (&parser->errors, token);
860 glcpp_print (parser->errors, "\" and \"");
861 _token_print (&parser->errors, other);
862 glcpp_print (parser->errors, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700863
864 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700865}
866
Carl Worth0197e9b2010-05-26 08:05:19 -0700867static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700868_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700869{
870 token_node_t *node;
871
872 if (list == NULL)
873 return;
874
875 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700876 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700877}
878
Carl Worth3a37b872010-05-10 11:44:09 -0700879void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700880yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700881{
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700882 glcpp_printf(parser->errors, "%u:%u(%u): preprocessor error: %s\n",
883 locp->source, locp->first_line, locp->first_column, error);
Carl Worth3a37b872010-05-10 11:44:09 -0700884}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700885
Carl Worth33cc4002010-05-12 12:17:10 -0700886glcpp_parser_t *
887glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700888{
Carl Worth33cc4002010-05-12 12:17:10 -0700889 glcpp_parser_t *parser;
890
Carl Worth5070a202010-05-12 12:45:33 -0700891 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700892
Carl Worth8f38aff2010-05-19 10:01:29 -0700893 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700894 parser->defines = hash_table_ctor (32, hash_table_string_hash,
895 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700896 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700897 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700898 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700899 parser->newline_as_space = 0;
900 parser->in_control_line = 0;
901 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700902
Carl Worthb20d33c2010-05-20 22:27:07 -0700903 parser->skip_stack = NULL;
904
Carl Worth8e82fcb2010-05-26 11:15:21 -0700905 parser->lex_from_list = NULL;
906 parser->lex_from_node = NULL;
907
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700908 parser->output = talloc_strdup(parser, "");
909 parser->errors = talloc_strdup(parser, "");
910
Carl Worth33cc4002010-05-12 12:17:10 -0700911 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700912}
913
914int
915glcpp_parser_parse (glcpp_parser_t *parser)
916{
917 return yyparse (parser);
918}
919
920void
Carl Worth33cc4002010-05-12 12:17:10 -0700921glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700922{
Carl Worthb20d33c2010-05-20 22:27:07 -0700923 if (parser->skip_stack)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700924 glcpp_print (parser->errors, "Error: Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700925 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700926 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700927 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700928}
Carl Worthc6d5af32010-05-11 12:30:09 -0700929
Carl Worth8e82fcb2010-05-26 11:15:21 -0700930/* Replace any occurences of DEFINED tokens in 'list' with either a
931 * '0' or '1' INTEGER token depending on whether the next token in the
932 * list is defined or not. */
933static void
934_glcpp_parser_evaluate_defined (glcpp_parser_t *parser,
935 token_list_t *list)
936{
937 token_node_t *node, *next;
Carl Worth0324cad2010-05-26 15:53:05 -0700938 macro_t *macro;
Carl Worth8e82fcb2010-05-26 11:15:21 -0700939
940 if (list == NULL)
941 return;
942
943 for (node = list->head; node; node = node->next) {
944 if (node->token->type != DEFINED)
945 continue;
946 next = node->next;
947 while (next && next->token->type == SPACE)
948 next = next->next;
949 if (next == NULL || next->token->type != IDENTIFIER) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700950 glcpp_print (parser->errors, "Error: operator \"defined\" requires an identifier\n");
Carl Worth8e82fcb2010-05-26 11:15:21 -0700951 exit (1);
952 }
953 macro = hash_table_find (parser->defines,
954 next->token->value.str);
955
956 node->token->type = INTEGER;
957 node->token->value.ival = (macro != NULL);
958 node->next = next->next;
959 }
960}
961
Carl Worthb1854fd2010-05-25 16:28:26 -0700962typedef enum function_status
963{
964 FUNCTION_STATUS_SUCCESS,
965 FUNCTION_NOT_A_FUNCTION,
966 FUNCTION_UNBALANCED_PARENTHESES
967} function_status_t;
968
969/* Find a set of function-like macro arguments by looking for a
Carl Worth681afbc2010-05-28 15:06:02 -0700970 * balanced set of parentheses.
971 *
972 * When called, 'node' should be the opening-parenthesis token, (or
973 * perhaps preceeding SPACE tokens). Upon successful return *last will
974 * be the last consumed node, (corresponding to the closing right
975 * parenthesis).
Carl Worthb1854fd2010-05-25 16:28:26 -0700976 *
977 * Return values:
978 *
979 * FUNCTION_STATUS_SUCCESS:
980 *
981 * Successfully parsed a set of function arguments.
982 *
983 * FUNCTION_NOT_A_FUNCTION:
984 *
985 * Macro name not followed by a '('. This is not an error, but
986 * simply that the macro name should be treated as a non-macro.
987 *
Carl Worth14c98a52010-06-02 15:49:54 -0700988 * FUNCTION_UNBALANCED_PARENTHESES
Carl Worthb1854fd2010-05-25 16:28:26 -0700989 *
990 * Macro name is not followed by a balanced set of parentheses.
991 */
992static function_status_t
Carl Worth681afbc2010-05-28 15:06:02 -0700993_arguments_parse (argument_list_t *arguments,
994 token_node_t *node,
995 token_node_t **last)
Carl Worthb1854fd2010-05-25 16:28:26 -0700996{
Carl Worth9ce18cf2010-05-25 17:32:21 -0700997 token_list_t *argument;
Carl Worthb1854fd2010-05-25 16:28:26 -0700998 int paren_count;
Carl Worthb1854fd2010-05-25 16:28:26 -0700999
Carl Worthb1854fd2010-05-25 16:28:26 -07001000 node = node->next;
1001
1002 /* Ignore whitespace before first parenthesis. */
1003 while (node && node->token->type == SPACE)
1004 node = node->next;
1005
1006 if (node == NULL || node->token->type != '(')
1007 return FUNCTION_NOT_A_FUNCTION;
1008
Carl Worth652fa272010-05-25 17:45:22 -07001009 node = node->next;
1010
Carl Wortha19297b2010-05-27 13:29:19 -07001011 argument = _token_list_create (arguments);
1012 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001013
Carl Worth681afbc2010-05-28 15:06:02 -07001014 for (paren_count = 1; node; node = node->next) {
Carl Worthb1854fd2010-05-25 16:28:26 -07001015 if (node->token->type == '(')
1016 {
1017 paren_count++;
1018 }
1019 else if (node->token->type == ')')
1020 {
1021 paren_count--;
Carl Worth681afbc2010-05-28 15:06:02 -07001022 if (paren_count == 0)
Carl Worthc7581c22010-05-25 17:41:07 -07001023 break;
Carl Worthb1854fd2010-05-25 16:28:26 -07001024 }
Carl Worth652fa272010-05-25 17:45:22 -07001025
1026 if (node->token->type == ',' &&
Carl Worthb1854fd2010-05-25 16:28:26 -07001027 paren_count == 1)
1028 {
Carl Wortha19297b2010-05-27 13:29:19 -07001029 _token_list_trim_trailing_space (argument);
1030 argument = _token_list_create (arguments);
1031 _argument_list_append (arguments, argument);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001032 }
1033 else {
Carl Wortha19297b2010-05-27 13:29:19 -07001034 if (argument->head == NULL) {
Carl Worthc7581c22010-05-25 17:41:07 -07001035 /* Don't treat initial whitespace as
1036 * part of the arguement. */
1037 if (node->token->type == SPACE)
1038 continue;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001039 }
1040 _token_list_append (argument, node->token);
Carl Worthb1854fd2010-05-25 16:28:26 -07001041 }
Carl Worthc7581c22010-05-25 17:41:07 -07001042 }
Carl Worthb1854fd2010-05-25 16:28:26 -07001043
Carl Worth681afbc2010-05-28 15:06:02 -07001044 if (paren_count)
Carl Worthb1854fd2010-05-25 16:28:26 -07001045 return FUNCTION_UNBALANCED_PARENTHESES;
1046
Carl Worth681afbc2010-05-28 15:06:02 -07001047 *last = node;
Carl Worthb1854fd2010-05-25 16:28:26 -07001048
1049 return FUNCTION_STATUS_SUCCESS;
1050}
1051
Carl Worth681afbc2010-05-28 15:06:02 -07001052/* This is a helper function that's essentially part of the
1053 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1054 * except for by that function.
1055 *
1056 * Returns NULL if node is a simple token with no expansion, (that is,
1057 * although 'node' corresponds to an identifier defined as a
1058 * function-like macro, it is not followed with a parenthesized
1059 * argument list).
1060 *
1061 * Compute the complete expansion of node (which is a function-like
1062 * macro) and subsequent nodes which are arguments.
1063 *
1064 * Returns the token list that results from the expansion and sets
1065 * *last to the last node in the list that was consumed by the
1066 * expansion. Specificallty, *last will be set as follows: as the
1067 * token of the closing right parenthesis.
1068 */
1069static token_list_t *
1070_glcpp_parser_expand_function (glcpp_parser_t *parser,
1071 token_node_t *node,
1072 token_node_t **last)
1073
Carl Worthb1854fd2010-05-25 16:28:26 -07001074{
1075 macro_t *macro;
Carl Worthb1854fd2010-05-25 16:28:26 -07001076 const char *identifier;
1077 argument_list_t *arguments;
1078 function_status_t status;
Carl Worth0197e9b2010-05-26 08:05:19 -07001079 token_list_t *substituted;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001080 int parameter_index;
Carl Worthb1854fd2010-05-25 16:28:26 -07001081
Carl Worthb1854fd2010-05-25 16:28:26 -07001082 identifier = node->token->value.str;
1083
1084 macro = hash_table_find (parser->defines, identifier);
1085
1086 assert (macro->is_function);
1087
Carl Worth9ce18cf2010-05-25 17:32:21 -07001088 arguments = _argument_list_create (parser);
Carl Worth681afbc2010-05-28 15:06:02 -07001089 status = _arguments_parse (arguments, node, last);
Carl Worthb1854fd2010-05-25 16:28:26 -07001090
1091 switch (status) {
1092 case FUNCTION_STATUS_SUCCESS:
1093 break;
1094 case FUNCTION_NOT_A_FUNCTION:
Carl Worth681afbc2010-05-28 15:06:02 -07001095 return NULL;
Carl Worthb1854fd2010-05-25 16:28:26 -07001096 case FUNCTION_UNBALANCED_PARENTHESES:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001097 glcpp_printf (parser->errors, "Error: Macro %s call has unbalanced parentheses\n",
1098 identifier);
Carl Worth14c98a52010-06-02 15:49:54 -07001099 exit (1);
Carl Worth681afbc2010-05-28 15:06:02 -07001100 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001101 }
1102
Carl Worth9ce18cf2010-05-25 17:32:21 -07001103 if (macro->replacements == NULL) {
1104 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001105 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001106 }
1107
Carl Wortha19297b2010-05-27 13:29:19 -07001108 if (! ((_argument_list_length (arguments) ==
1109 _string_list_length (macro->parameters)) ||
1110 (_string_list_length (macro->parameters) == 0 &&
1111 _argument_list_length (arguments) == 1 &&
1112 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001113 {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001114 glcpp_printf (parser->errors,
1115 "Error: macro %s invoked with %d arguments (expected %d)\n",
1116 identifier,
1117 _argument_list_length (arguments),
1118 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001119 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001120 }
1121
Carl Worth0197e9b2010-05-26 08:05:19 -07001122 /* Perform argument substitution on the replacement list. */
1123 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001124
Carl Worthce540f22010-05-26 08:25:44 -07001125 for (node = macro->replacements->head; node; node = node->next)
1126 {
1127 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001128 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001129 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001130 &parameter_index))
1131 {
1132 token_list_t *argument;
1133 argument = _argument_list_member_at (arguments,
1134 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001135 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001136 * tokens, or append a placeholder token for
1137 * an empty argument. */
1138 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001139 _glcpp_parser_expand_token_list (parser,
1140 argument);
1141 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001142 } else {
1143 token_t *new_token;
1144
1145 new_token = _token_create_ival (substituted,
1146 PLACEHOLDER,
1147 PLACEHOLDER);
1148 _token_list_append (substituted, new_token);
1149 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001150 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001151 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001152 }
1153 }
1154
Carl Worthad0dee62010-05-26 09:04:50 -07001155 /* After argument substitution, and before further expansion
1156 * below, implement token pasting. */
1157
Carl Worthb06096e2010-05-29 05:54:19 -07001158 _token_list_trim_trailing_space (substituted);
1159
Carl Worthad0dee62010-05-26 09:04:50 -07001160 node = substituted->head;
1161 while (node)
1162 {
1163 token_node_t *next_non_space;
1164
1165 /* Look ahead for a PASTE token, skipping space. */
1166 next_non_space = node->next;
1167 while (next_non_space && next_non_space->token->type == SPACE)
1168 next_non_space = next_non_space->next;
1169
1170 if (next_non_space == NULL)
1171 break;
1172
1173 if (next_non_space->token->type != PASTE) {
1174 node = next_non_space;
1175 continue;
1176 }
1177
1178 /* Now find the next non-space token after the PASTE. */
1179 next_non_space = next_non_space->next;
1180 while (next_non_space && next_non_space->token->type == SPACE)
1181 next_non_space = next_non_space->next;
1182
1183 if (next_non_space == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001184 glcpp_print (parser->errors, "Error: '##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001185 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001186 }
1187
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001188 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001189 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001190 if (next_non_space == substituted->tail)
1191 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001192
1193 node = node->next;
1194 }
1195
Carl Worthb06096e2010-05-29 05:54:19 -07001196 substituted->non_space_tail = substituted->tail;
1197
Carl Worth681afbc2010-05-28 15:06:02 -07001198 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001199}
1200
Carl Worth681afbc2010-05-28 15:06:02 -07001201/* Compute the complete expansion of node, (and subsequent nodes after
1202 * 'node' in the case that 'node' is a function-like macro and
1203 * subsequent nodes are arguments).
1204 *
1205 * Returns NULL if node is a simple token with no expansion.
1206 *
1207 * Otherwise, returns the token list that results from the expansion
1208 * and sets *last to the last node in the list that was consumed by
1209 * the expansion. Specificallty, *last will be set as follows:
1210 *
1211 * As 'node' in the case of object-like macro expansion.
1212 *
1213 * As the token of the closing right parenthesis in the case of
1214 * function-like macro expansion.
1215 */
1216static token_list_t *
1217_glcpp_parser_expand_node (glcpp_parser_t *parser,
1218 token_node_t *node,
1219 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001220{
Carl Worth681afbc2010-05-28 15:06:02 -07001221 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001222 const char *identifier;
1223 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001224
1225 /* We only expand identifiers */
1226 if (token->type != IDENTIFIER) {
1227 /* We change any COMMA into a COMMA_FINAL to prevent
1228 * it being mistaken for an argument separator
1229 * later. */
1230 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001231 token->type = COMMA_FINAL;
1232 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001233 }
Carl Worth681afbc2010-05-28 15:06:02 -07001234
1235 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001236 }
1237
1238 /* Look up this identifier in the hash table. */
1239 identifier = token->value.str;
1240 macro = hash_table_find (parser->defines, identifier);
1241
Carl Worth681afbc2010-05-28 15:06:02 -07001242 /* Not a macro, so no expansion needed. */
1243 if (macro == NULL)
1244 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001245
1246 /* Finally, don't expand this macro if we're already actively
1247 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001248 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001249 /* We change the token type here from IDENTIFIER to
1250 * OTHER to prevent any future expansion of this
1251 * unexpanded token. */
1252 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001253 token_list_t *expansion;
1254 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001255
Carl Worth681afbc2010-05-28 15:06:02 -07001256 str = xtalloc_strdup (parser, token->value.str);
1257 final = _token_create_str (parser, OTHER, str);
1258 expansion = _token_list_create (parser);
1259 _token_list_append (expansion, final);
1260 *last = node;
1261 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001262 }
1263
Carl Worth681afbc2010-05-28 15:06:02 -07001264 if (! macro->is_function)
1265 {
1266 *last = node;
1267
1268 if (macro->replacements == NULL)
1269 return _token_list_create (parser);
1270
Carl Worth22b3ace2010-06-02 15:32:03 -07001271 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001272 }
Carl Worth681afbc2010-05-28 15:06:02 -07001273
1274 return _glcpp_parser_expand_function (parser, node, last);
1275}
1276
Carl Worth22b3ace2010-06-02 15:32:03 -07001277/* Push a new identifier onto the active list, returning the new list.
1278 *
1279 * Here, 'marker' is the token node that appears in the list after the
1280 * expansion of 'identifier'. That is, when the list iterator begins
1281 * examinging 'marker', then it is time to pop this node from the
1282 * active stack.
1283 */
1284active_list_t *
1285_active_list_push (active_list_t *list,
1286 const char *identifier,
1287 token_node_t *marker)
1288{
1289 active_list_t *node;
1290
1291 node = xtalloc (list, active_list_t);
1292 node->identifier = xtalloc_strdup (node, identifier);
1293 node->marker = marker;
1294 node->next = list;
1295
1296 return node;
1297}
1298
1299active_list_t *
1300_active_list_pop (active_list_t *list)
1301{
1302 active_list_t *node = list;
1303
1304 if (node == NULL)
1305 return NULL;
1306
1307 node = list->next;
1308 talloc_free (list);
1309
1310 return node;
1311}
1312
1313int
1314_active_list_contains (active_list_t *list, const char *identifier)
1315{
1316 active_list_t *node;
1317
1318 if (list == NULL)
1319 return 0;
1320
1321 for (node = list; node; node = node->next)
1322 if (strcmp (node->identifier, identifier) == 0)
1323 return 1;
1324
1325 return 0;
1326}
1327
Carl Worth681afbc2010-05-28 15:06:02 -07001328/* Walk over the token list replacing nodes with their expansion.
1329 * Whenever nodes are expanded the walking will walk over the new
1330 * nodes, continuing to expand as necessary. The results are placed in
1331 * 'list' itself;
1332 */
1333static void
1334_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1335 token_list_t *list)
1336{
1337 token_node_t *node_prev;
1338 token_node_t *node, *last;
1339 token_list_t *expansion;
1340
1341 if (list == NULL)
1342 return;
1343
1344 _token_list_trim_trailing_space (list);
1345
1346 node_prev = NULL;
1347 node = list->head;
1348
1349 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001350
1351 while (parser->active && parser->active->marker == node)
1352 parser->active = _active_list_pop (parser->active);
1353
Carl Worth681afbc2010-05-28 15:06:02 -07001354 /* Find the expansion for node, which will replace all
1355 * nodes from node to last, inclusive. */
1356 expansion = _glcpp_parser_expand_node (parser, node, &last);
1357 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001358 token_node_t *n;
1359
1360 for (n = node; n != last->next; n = n->next)
1361 while (parser->active &&
1362 parser->active->marker == n)
1363 {
1364 parser->active = _active_list_pop (parser->active);
1365 }
1366
1367 parser->active = _active_list_push (parser->active,
1368 node->token->value.str,
1369 last->next);
1370
Carl Worth681afbc2010-05-28 15:06:02 -07001371 /* Splice expansion into list, supporting a
1372 * simple deletion if the expansion is
1373 * empty. */
1374 if (expansion->head) {
1375 if (node_prev)
1376 node_prev->next = expansion->head;
1377 else
1378 list->head = expansion->head;
1379 expansion->tail->next = last->next;
1380 if (last == list->tail)
1381 list->tail = expansion->tail;
1382 } else {
1383 if (node_prev)
1384 node_prev->next = last->next;
1385 else
1386 list->head = last->next;
1387 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001388 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001389 }
1390 } else {
1391 node_prev = node;
1392 }
1393 node = node_prev ? node_prev->next : list->head;
1394 }
1395
Carl Worth22b3ace2010-06-02 15:32:03 -07001396 while (parser->active)
1397 parser->active = _active_list_pop (parser->active);
1398
Carl Worth681afbc2010-05-28 15:06:02 -07001399 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001400}
1401
Carl Worthae6517f2010-05-25 15:24:59 -07001402void
1403_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1404 token_list_t *list)
1405{
Carl Worthae6517f2010-05-25 15:24:59 -07001406 if (list == NULL)
1407 return;
1408
Carl Worth681afbc2010-05-28 15:06:02 -07001409 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001410
Carl Worth681afbc2010-05-28 15:06:02 -07001411 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001412
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001413 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001414}
1415
1416void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001417_check_for_reserved_macro_name (glcpp_parser_t *parser, const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001418{
1419 /* According to the GLSL specification, macro names starting with "__"
1420 * or "GL_" are reserved for future use. So, don't allow them.
1421 */
1422 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001423 glcpp_print (parser->errors, "Error: Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001424 exit(1);
1425 }
1426 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001427 glcpp_print (parser->errors, "Error: Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001428 exit(1);
1429 }
1430}
1431
1432void
Carl Worthfcbbb462010-05-13 09:36:23 -07001433_define_object_macro (glcpp_parser_t *parser,
1434 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001435 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001436{
1437 macro_t *macro;
1438
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001439 _check_for_reserved_macro_name(parser, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001440
Carl Worthfcbbb462010-05-13 09:36:23 -07001441 macro = xtalloc (parser, macro_t);
1442
1443 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001444 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001445 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001446 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001447
1448 hash_table_insert (parser->defines, macro, identifier);
1449}
1450
1451void
1452_define_function_macro (glcpp_parser_t *parser,
1453 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001454 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001455 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001456{
1457 macro_t *macro;
1458
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001459 _check_for_reserved_macro_name(parser, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001460
Carl Worthfcbbb462010-05-13 09:36:23 -07001461 macro = xtalloc (parser, macro_t);
1462
1463 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001464 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001465 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001466 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001467
1468 hash_table_insert (parser->defines, macro, identifier);
1469}
1470
Carl Worth8f38aff2010-05-19 10:01:29 -07001471static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001472glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001473{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001474 token_node_t *node;
1475 int ret;
1476
Carl Worth95951ea2010-05-26 15:57:10 -07001477 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001478 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001479
1480 /* XXX: This ugly block of code exists for the sole
1481 * purpose of converting a NEWLINE token into a SPACE
1482 * token, but only in the case where we have seen a
1483 * function-like macro name, but have not yet seen its
1484 * closing parenthesis.
1485 *
1486 * There's perhaps a more compact way to do this with
1487 * mid-rule actions in the grammar.
1488 *
1489 * I'm definitely not pleased with the complexity of
1490 * this code here.
1491 */
1492 if (parser->newline_as_space)
1493 {
1494 if (ret == '(') {
1495 parser->paren_count++;
1496 } else if (ret == ')') {
1497 parser->paren_count--;
1498 if (parser->paren_count == 0)
1499 parser->newline_as_space = 0;
1500 } else if (ret == NEWLINE) {
1501 ret = SPACE;
1502 } else if (ret != SPACE) {
1503 if (parser->paren_count == 0)
1504 parser->newline_as_space = 0;
1505 }
1506 }
1507 else if (parser->in_control_line)
1508 {
1509 if (ret == NEWLINE)
1510 parser->in_control_line = 0;
1511 }
1512 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1513 ret == HASH_UNDEF || ret == HASH_IF ||
1514 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1515 ret == HASH_ELIF || ret == HASH_ELSE ||
1516 ret == HASH_ENDIF || ret == HASH)
1517 {
1518 parser->in_control_line = 1;
1519 }
1520 else if (ret == IDENTIFIER)
1521 {
1522 macro_t *macro;
1523 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001524 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001525 if (macro && macro->is_function) {
1526 parser->newline_as_space = 1;
1527 parser->paren_count = 0;
1528 }
1529 }
1530
1531 return ret;
1532 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001533
1534 node = parser->lex_from_node;
1535
1536 if (node == NULL) {
1537 talloc_free (parser->lex_from_list);
1538 parser->lex_from_list = NULL;
1539 return NEWLINE;
1540 }
1541
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001542 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001543 ret = node->token->type;
1544
1545 parser->lex_from_node = node->next;
1546
1547 return ret;
1548}
1549
1550static void
1551glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1552{
1553 token_node_t *node;
1554
1555 assert (parser->lex_from_list == NULL);
1556
1557 /* Copy list, eliminating any space tokens. */
1558 parser->lex_from_list = _token_list_create (parser);
1559
1560 for (node = list->head; node; node = node->next) {
1561 if (node->token->type == SPACE)
1562 continue;
1563 _token_list_append (parser->lex_from_list, node->token);
1564 }
1565
1566 talloc_free (list);
1567
1568 parser->lex_from_node = parser->lex_from_list->head;
1569
1570 /* It's possible the list consisted of nothing but whitespace. */
1571 if (parser->lex_from_node == NULL) {
1572 talloc_free (parser->lex_from_list);
1573 parser->lex_from_list = NULL;
1574 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001575}
Carl Worthb20d33c2010-05-20 22:27:07 -07001576
1577static void
1578_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, int condition)
1579{
1580 skip_type_t current = SKIP_NO_SKIP;
1581 skip_node_t *node;
1582
1583 if (parser->skip_stack)
1584 current = parser->skip_stack->type;
1585
1586 node = xtalloc (parser, skip_node_t);
1587
1588 if (current == SKIP_NO_SKIP) {
1589 if (condition)
1590 node->type = SKIP_NO_SKIP;
1591 else
1592 node->type = SKIP_TO_ELSE;
1593 } else {
1594 node->type = SKIP_TO_ENDIF;
1595 }
1596
1597 node->next = parser->skip_stack;
1598 parser->skip_stack = node;
1599}
1600
1601static void
1602_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, const char *type,
1603 int condition)
1604{
1605 if (parser->skip_stack == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001606 glcpp_printf (parser->errors, "Error: %s without #if\n", type);
Carl Worthb20d33c2010-05-20 22:27:07 -07001607 exit (1);
1608 }
1609
1610 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1611 if (condition)
1612 parser->skip_stack->type = SKIP_NO_SKIP;
1613 } else {
1614 parser->skip_stack->type = SKIP_TO_ENDIF;
1615 }
1616}
Carl Worth80dc60b2010-05-25 14:42:00 -07001617
Carl Worthb20d33c2010-05-20 22:27:07 -07001618static void
1619_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser)
1620{
1621 skip_node_t *node;
1622
1623 if (parser->skip_stack == NULL) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001624 glcpp_print (parser->errors, "Error: #endif without #if\n");
Carl Worthb20d33c2010-05-20 22:27:07 -07001625 exit (1);
1626 }
1627
1628 node = parser->skip_stack;
1629 parser->skip_stack = node->next;
1630 talloc_free (node);
1631}