blob: 37c2f97c23d058c2891b96f4e2eeb04abd543574 [file] [log] [blame]
Carl Worth3a37b872010-05-10 11:44:09 -07001%{
2/*
3 * Copyright © 2010 Intel Corporation
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
Carl Worthfcbbb462010-05-13 09:36:23 -070027#include <assert.h>
Carl Worth8fed1cd2010-05-26 09:32:12 -070028#include <inttypes.h>
Carl Worth3a37b872010-05-10 11:44:09 -070029
Carl Wortha1e32bc2010-05-10 13:17:25 -070030#include "glcpp.h"
31
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -070032#define glcpp_print(stream, str) stream = talloc_strdup_append(stream, str)
33#define glcpp_printf(stream, fmt, args...) \
34 stream = talloc_asprintf_append(stream, fmt, args)
35
Carl Worth5aa7ea02010-05-25 18:39:43 -070036static void
Kenneth Graunke465e03e2010-06-16 16:35:57 -070037yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
Carl Worth3a37b872010-05-10 11:44:09 -070038
Carl Worth5aa7ea02010-05-25 18:39:43 -070039static void
Carl Worthfcbbb462010-05-13 09:36:23 -070040_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070041 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070042 const char *macro,
Carl Worth47252442010-05-19 13:54:37 -070043 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070044
Carl Worth5aa7ea02010-05-25 18:39:43 -070045static void
Carl Worthfcbbb462010-05-13 09:36:23 -070046_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -070047 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -070048 const char *macro,
Carl Worthc5e98552010-05-14 10:12:21 -070049 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -070050 token_list_t *replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -070051
Carl Worth5aa7ea02010-05-25 18:39:43 -070052static string_list_t *
Carl Worth610053b2010-05-14 10:05:11 -070053_string_list_create (void *ctx);
Carl Worth33cc4002010-05-12 12:17:10 -070054
Carl Worth5aa7ea02010-05-25 18:39:43 -070055static void
Carl Worth610053b2010-05-14 10:05:11 -070056_string_list_append_item (string_list_t *list, const char *str);
Carl Worthfcbbb462010-05-13 09:36:23 -070057
Carl Worth5aa7ea02010-05-25 18:39:43 -070058static void
Carl Worth610053b2010-05-14 10:05:11 -070059_string_list_append_list (string_list_t *list, string_list_t *tail);
Carl Worthc6d5af32010-05-11 12:30:09 -070060
Carl Worth5aa7ea02010-05-25 18:39:43 -070061static int
Carl Worth610053b2010-05-14 10:05:11 -070062_string_list_contains (string_list_t *list, const char *member, int *index);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070063
Carl Worth5aa7ea02010-05-25 18:39:43 -070064static int
Carl Worth610053b2010-05-14 10:05:11 -070065_string_list_length (string_list_t *list);
Carl Worthdcc2ecd2010-05-13 12:56:42 -070066
Carl Worth5aa7ea02010-05-25 18:39:43 -070067static argument_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070068_argument_list_create (void *ctx);
69
Carl Worth5aa7ea02010-05-25 18:39:43 -070070static void
Carl Worth47252442010-05-19 13:54:37 -070071_argument_list_append (argument_list_t *list, token_list_t *argument);
Carl Worth8f6a8282010-05-14 10:44:19 -070072
Carl Worth5aa7ea02010-05-25 18:39:43 -070073static int
Carl Worth8f6a8282010-05-14 10:44:19 -070074_argument_list_length (argument_list_t *list);
75
Carl Worth5aa7ea02010-05-25 18:39:43 -070076static token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -070077_argument_list_member_at (argument_list_t *list, int index);
78
Carl Worth808401f2010-05-25 14:52:43 -070079/* Note: This function talloc_steal()s the str pointer. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070080static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070081_token_create_str (void *ctx, int type, char *str);
82
Carl Worth5aa7ea02010-05-25 18:39:43 -070083static token_t *
Carl Worth808401f2010-05-25 14:52:43 -070084_token_create_ival (void *ctx, int type, int ival);
85
Carl Worth5aa7ea02010-05-25 18:39:43 -070086static token_list_t *
Carl Worth47252442010-05-19 13:54:37 -070087_token_list_create (void *ctx);
88
Carl Worthb1ae61a2010-05-26 08:10:38 -070089/* Note: This function adds a talloc_reference() to token.
Carl Worth808401f2010-05-25 14:52:43 -070090 *
91 * You may want to talloc_unlink any current reference if you no
92 * longer need it. */
Carl Worth5aa7ea02010-05-25 18:39:43 -070093static void
Carl Worth808401f2010-05-25 14:52:43 -070094_token_list_append (token_list_t *list, token_t *token);
Carl Worth47252442010-05-19 13:54:37 -070095
Carl Worth5aa7ea02010-05-25 18:39:43 -070096static void
Carl Worth47252442010-05-19 13:54:37 -070097_token_list_append_list (token_list_t *list, token_list_t *tail);
98
Carl Worth22b3ace2010-06-02 15:32:03 -070099static int
100_token_list_length (token_list_t *list);
101
102static active_list_t *
103_active_list_push (active_list_t *list,
104 const char *identifier,
105 token_node_t *marker);
106
107static active_list_t *
108_active_list_pop (active_list_t *list);
109
110int
111_active_list_contains (active_list_t *list, const char *identifier);
112
Carl Worth5aa7ea02010-05-25 18:39:43 -0700113static void
Carl Worth681afbc2010-05-28 15:06:02 -0700114_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
115 token_list_t *list);
Carl Worth808401f2010-05-25 14:52:43 -0700116
Carl Worthaaa9acb2010-05-19 13:28:24 -0700117static void
Carl Worth681afbc2010-05-28 15:06:02 -0700118_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
119 token_list_t *list);
Carl Worth0197e9b2010-05-26 08:05:19 -0700120
121static void
Kenneth Graunke07745232010-06-17 12:41:46 -0700122_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
123 int condition);
Carl Worthb20d33c2010-05-20 22:27:07 -0700124
125static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700126_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
127 const char *type, int condition);
Carl Worth80dc60b2010-05-25 14:42:00 -0700128
Carl Worthb20d33c2010-05-20 22:27:07 -0700129static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700130_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
Carl Worthb20d33c2010-05-20 22:27:07 -0700131
Carl Worth0293b2e2010-05-19 10:05:40 -0700132#define yylex glcpp_parser_lex
133
Carl Worth8f38aff2010-05-19 10:01:29 -0700134static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700135glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
Carl Worth8f38aff2010-05-19 10:01:29 -0700136
Carl Worth8e82fcb2010-05-26 11:15:21 -0700137static void
138glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
139
Carl Worth3a37b872010-05-10 11:44:09 -0700140%}
141
Kenneth Graunkee0e429f2010-06-16 16:26:28 -0700142%pure-parser
Kenneth Graunkef70f6072010-06-17 13:07:13 -0700143%error-verbose
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700144%locations
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
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700154%type <token> preprocessing_token conditional_token
155%type <token_list> pp_tokens replacement_list text_line conditional_tokens
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 {
Kenneth Graunke07745232010-06-17 12:41:46 -0700190 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700191 }
192| ELIF_EXPANDED expression NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700193 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700194 }
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 {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700199 _define_object_macro (parser, & @2, $2, $3);
Carl Worthae6517f2010-05-25 15:24:59 -0700200 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700201| HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700202 _define_function_macro (parser, & @2, $2, NULL, $5);
Carl Worthb1854fd2010-05-25 16:28:26 -0700203 }
204| HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -0700205 _define_function_macro (parser, & @2, $2, $4, $6);
Carl Worthb1854fd2010-05-25 16:28:26 -0700206 }
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 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700218| HASH_IF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700219 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);
Carl Worth681afbc2010-05-28 15:06:02 -0700226 _glcpp_parser_expand_token_list (parser, $2);
227 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700228 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700229 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700230| HASH_IFDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700231 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700232 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700233 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700234 }
Kenneth Graunke65875742010-06-18 20:08:15 -0700235| HASH_IFNDEF IDENTIFIER junk NEWLINE {
Carl Worth0324cad2010-05-26 15:53:05 -0700236 macro_t *macro = hash_table_find (parser->defines, $2);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700237 talloc_free ($2);
Kenneth Graunke07745232010-06-17 12:41:46 -0700238 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700239 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700240| HASH_ELIF conditional_tokens NEWLINE {
Carl Worth8e82fcb2010-05-26 11:15:21 -0700241 token_list_t *expanded;
242 token_t *token;
243
244 expanded = _token_list_create (parser);
245 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
246 _token_list_append (expanded, token);
247 talloc_unlink (parser, token);
Carl Worth681afbc2010-05-28 15:06:02 -0700248 _glcpp_parser_expand_token_list (parser, $2);
249 _token_list_append_list (expanded, $2);
Carl Worth8e82fcb2010-05-26 11:15:21 -0700250 glcpp_parser_lex_from (parser, expanded);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700251 }
252| HASH_ELSE NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700253 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700254 }
255| HASH_ENDIF NEWLINE {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -0700256 _glcpp_parser_skip_stack_pop (parser, & @1);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700257 }
Carl Worth3ff81672010-05-25 13:09:03 -0700258| HASH NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700259;
260
Carl Worth8fed1cd2010-05-26 09:32:12 -0700261expression:
Carl Worth050e3de2010-05-27 14:36:29 -0700262 INTEGER_STRING {
263 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
264 $$ = strtoll ($1 + 2, NULL, 16);
265 } else if ($1[0] == '0') {
266 $$ = strtoll ($1, NULL, 8);
267 } else {
268 $$ = strtoll ($1, NULL, 10);
269 }
270 }
271| INTEGER {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700272 $$ = $1;
273 }
274| expression OR expression {
275 $$ = $1 || $3;
276 }
277| expression AND expression {
278 $$ = $1 && $3;
279 }
280| expression '|' expression {
281 $$ = $1 | $3;
282 }
283| expression '^' expression {
284 $$ = $1 ^ $3;
285 }
286| expression '&' expression {
287 $$ = $1 & $3;
288 }
289| expression NOT_EQUAL expression {
290 $$ = $1 != $3;
291 }
292| expression EQUAL expression {
293 $$ = $1 == $3;
294 }
295| expression GREATER_OR_EQUAL expression {
296 $$ = $1 >= $3;
297 }
298| expression LESS_OR_EQUAL expression {
299 $$ = $1 <= $3;
300 }
301| expression '>' expression {
302 $$ = $1 > $3;
303 }
304| expression '<' expression {
305 $$ = $1 < $3;
306 }
307| expression RIGHT_SHIFT expression {
308 $$ = $1 >> $3;
309 }
310| expression LEFT_SHIFT expression {
311 $$ = $1 << $3;
312 }
313| expression '-' 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 %prec UNARY {
329 $$ = ! $2;
330 }
331| '~' expression %prec UNARY {
332 $$ = ~ $2;
333 }
334| '-' expression %prec UNARY {
335 $$ = - $2;
336 }
337| '+' expression %prec UNARY {
338 $$ = + $2;
339 }
Carl Worth8fed1cd2010-05-26 09:32:12 -0700340| '(' expression ')' {
341 $$ = $2;
342 }
343;
344
Carl Worth3ff81672010-05-25 13:09:03 -0700345identifier_list:
Carl Worthb1854fd2010-05-25 16:28:26 -0700346 IDENTIFIER {
347 $$ = _string_list_create (parser);
348 _string_list_append_item ($$, $1);
349 talloc_steal ($$, $1);
350 }
351| identifier_list ',' IDENTIFIER {
352 $$ = $1;
353 _string_list_append_item ($$, $3);
354 talloc_steal ($$, $3);
355 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700356;
357
Carl Worth3ff81672010-05-25 13:09:03 -0700358text_line:
Carl Worth808401f2010-05-25 14:52:43 -0700359 NEWLINE { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700360| pp_tokens NEWLINE
Carl Worthfcbbb462010-05-13 09:36:23 -0700361;
362
Carl Worth3ff81672010-05-25 13:09:03 -0700363non_directive:
Kenneth Graunke739ba062010-06-16 12:41:37 -0700364 pp_tokens NEWLINE {
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700365 yyerror (& @1, parser, "Invalid tokens after #");
Kenneth Graunke739ba062010-06-16 12:41:37 -0700366 }
Carl Worthfcbbb462010-05-13 09:36:23 -0700367;
368
Carl Worthaaa9acb2010-05-19 13:28:24 -0700369replacement_list:
Carl Worth808401f2010-05-25 14:52:43 -0700370 /* empty */ { $$ = NULL; }
Carl Worth3ff81672010-05-25 13:09:03 -0700371| pp_tokens
Carl Worthaaa9acb2010-05-19 13:28:24 -0700372;
373
Kenneth Graunke65875742010-06-18 20:08:15 -0700374junk:
375 /* empty */
376| pp_tokens {
377 glcpp_warning(&@1, parser, "extra tokens at end of directive");
378 }
Kenneth Graunke26e761e2010-06-18 23:06:54 -0700379;
380
381conditional_token:
382 /* Handle "defined" operator */
383 DEFINED IDENTIFIER {
384 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
385 $$ = _token_create_ival (parser, INTEGER, v);
386 }
387| DEFINED '(' IDENTIFIER ')' {
388 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
389 $$ = _token_create_ival (parser, INTEGER, v);
390 }
391| preprocessing_token
392;
393
394conditional_tokens:
395 /* Exactly the same as pp_tokens, but using conditional_token */
396 conditional_token {
397 parser->space_tokens = 1;
398 $$ = _token_list_create (parser);
399 _token_list_append ($$, $1);
400 talloc_unlink (parser, $1);
401 }
402| conditional_tokens conditional_token {
403 $$ = $1;
404 _token_list_append ($$, $2);
405 talloc_unlink (parser, $2);
406 }
407;
Kenneth Graunke65875742010-06-18 20:08:15 -0700408
Carl Worthaaa9acb2010-05-19 13:28:24 -0700409pp_tokens:
Carl Worth808401f2010-05-25 14:52:43 -0700410 preprocessing_token {
Carl Worthf34a0002010-05-25 16:59:02 -0700411 parser->space_tokens = 1;
Carl Worth808401f2010-05-25 14:52:43 -0700412 $$ = _token_list_create (parser);
413 _token_list_append ($$, $1);
414 talloc_unlink (parser, $1);
415 }
416| pp_tokens preprocessing_token {
417 $$ = $1;
418 _token_list_append ($$, $2);
419 talloc_unlink (parser, $2);
420 }
Carl Worthaaa9acb2010-05-19 13:28:24 -0700421;
422
Carl Worth3ff81672010-05-25 13:09:03 -0700423preprocessing_token:
Carl Worth808401f2010-05-25 14:52:43 -0700424 IDENTIFIER {
425 $$ = _token_create_str (parser, IDENTIFIER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700426 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700427 }
Carl Worth050e3de2010-05-27 14:36:29 -0700428| INTEGER_STRING {
429 $$ = _token_create_str (parser, INTEGER_STRING, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700430 $$->location = yylloc;
Carl Worth8fed1cd2010-05-26 09:32:12 -0700431 }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700432| operator {
Carl Worth808401f2010-05-25 14:52:43 -0700433 $$ = _token_create_ival (parser, $1, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700434 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700435 }
436| OTHER {
437 $$ = _token_create_str (parser, OTHER, $1);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700438 $$->location = yylloc;
Carl Worth808401f2010-05-25 14:52:43 -0700439 }
Carl Worthb1854fd2010-05-25 16:28:26 -0700440| SPACE {
Carl Worthe9397862010-05-25 17:08:07 -0700441 $$ = _token_create_ival (parser, SPACE, SPACE);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700442 $$->location = yylloc;
Carl Worthb1854fd2010-05-25 16:28:26 -0700443 }
Carl Worth3ff81672010-05-25 13:09:03 -0700444;
445
Carl Worth8e82fcb2010-05-26 11:15:21 -0700446operator:
Carl Worth808401f2010-05-25 14:52:43 -0700447 '[' { $$ = '['; }
448| ']' { $$ = ']'; }
449| '(' { $$ = '('; }
450| ')' { $$ = ')'; }
451| '{' { $$ = '{'; }
452| '}' { $$ = '}'; }
453| '.' { $$ = '.'; }
454| '&' { $$ = '&'; }
455| '*' { $$ = '*'; }
456| '+' { $$ = '+'; }
457| '-' { $$ = '-'; }
458| '~' { $$ = '~'; }
459| '!' { $$ = '!'; }
460| '/' { $$ = '/'; }
461| '%' { $$ = '%'; }
462| LEFT_SHIFT { $$ = LEFT_SHIFT; }
463| RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
464| '<' { $$ = '<'; }
465| '>' { $$ = '>'; }
466| LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
467| GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
468| EQUAL { $$ = EQUAL; }
469| NOT_EQUAL { $$ = NOT_EQUAL; }
470| '^' { $$ = '^'; }
471| '|' { $$ = '|'; }
472| AND { $$ = AND; }
473| OR { $$ = OR; }
474| ';' { $$ = ';'; }
475| ',' { $$ = ','; }
Carl Worth63101692010-05-29 05:07:24 -0700476| '=' { $$ = '='; }
Carl Worth808401f2010-05-25 14:52:43 -0700477| PASTE { $$ = PASTE; }
Carl Worth8e82fcb2010-05-26 11:15:21 -0700478| DEFINED { $$ = DEFINED; }
Carl Worth3ff81672010-05-25 13:09:03 -0700479;
480
Carl Worth33cc4002010-05-12 12:17:10 -0700481%%
482
Carl Worth610053b2010-05-14 10:05:11 -0700483string_list_t *
484_string_list_create (void *ctx)
Carl Worth33cc4002010-05-12 12:17:10 -0700485{
Carl Worth610053b2010-05-14 10:05:11 -0700486 string_list_t *list;
Carl Worth33cc4002010-05-12 12:17:10 -0700487
Carl Worth610053b2010-05-14 10:05:11 -0700488 list = xtalloc (ctx, string_list_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700489 list->head = NULL;
490 list->tail = NULL;
491
492 return list;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700493}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700494
Carl Worth33cc4002010-05-12 12:17:10 -0700495void
Carl Worth610053b2010-05-14 10:05:11 -0700496_string_list_append_list (string_list_t *list, string_list_t *tail)
Carl Worthfcbbb462010-05-13 09:36:23 -0700497{
498 if (list->head == NULL) {
499 list->head = tail->head;
500 } else {
501 list->tail->next = tail->head;
502 }
503
504 list->tail = tail->tail;
505}
506
507void
Carl Worth610053b2010-05-14 10:05:11 -0700508_string_list_append_item (string_list_t *list, const char *str)
Carl Worth33cc4002010-05-12 12:17:10 -0700509{
Carl Worth610053b2010-05-14 10:05:11 -0700510 string_node_t *node;
Carl Worth3a37b872010-05-10 11:44:09 -0700511
Carl Worth610053b2010-05-14 10:05:11 -0700512 node = xtalloc (list, string_node_t);
Carl Worth5070a202010-05-12 12:45:33 -0700513 node->str = xtalloc_strdup (node, str);
Carl Worth80dc60b2010-05-25 14:42:00 -0700514
Carl Worth33cc4002010-05-12 12:17:10 -0700515 node->next = NULL;
516
517 if (list->head == NULL) {
518 list->head = node;
519 } else {
520 list->tail->next = node;
521 }
522
523 list->tail = node;
524}
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700525
526int
Carl Worth610053b2010-05-14 10:05:11 -0700527_string_list_contains (string_list_t *list, const char *member, int *index)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700528{
Carl Worth610053b2010-05-14 10:05:11 -0700529 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700530 int i;
531
532 if (list == NULL)
533 return 0;
534
535 for (i = 0, node = list->head; node; i++, node = node->next) {
536 if (strcmp (node->str, member) == 0) {
Carl Worth420d05a2010-05-17 10:15:23 -0700537 if (index)
538 *index = i;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700539 return 1;
540 }
541 }
542
543 return 0;
544}
545
546int
Carl Worth610053b2010-05-14 10:05:11 -0700547_string_list_length (string_list_t *list)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700548{
549 int length = 0;
Carl Worth610053b2010-05-14 10:05:11 -0700550 string_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700551
552 if (list == NULL)
553 return 0;
554
555 for (node = list->head; node; node = node->next)
556 length++;
557
558 return length;
559}
560
Carl Worth8f6a8282010-05-14 10:44:19 -0700561argument_list_t *
562_argument_list_create (void *ctx)
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700563{
Carl Worth8f6a8282010-05-14 10:44:19 -0700564 argument_list_t *list;
565
566 list = xtalloc (ctx, argument_list_t);
567 list->head = NULL;
568 list->tail = NULL;
569
570 return list;
571}
572
573void
Carl Worth47252442010-05-19 13:54:37 -0700574_argument_list_append (argument_list_t *list, token_list_t *argument)
Carl Worth8f6a8282010-05-14 10:44:19 -0700575{
576 argument_node_t *node;
577
Carl Worth8f6a8282010-05-14 10:44:19 -0700578 node = xtalloc (list, argument_node_t);
579 node->argument = argument;
580
581 node->next = NULL;
582
583 if (list->head == NULL) {
584 list->head = node;
585 } else {
586 list->tail->next = node;
587 }
588
589 list->tail = node;
590}
591
592int
593_argument_list_length (argument_list_t *list)
594{
595 int length = 0;
596 argument_node_t *node;
597
598 if (list == NULL)
599 return 0;
600
601 for (node = list->head; node; node = node->next)
602 length++;
603
604 return length;
605}
606
Carl Worth47252442010-05-19 13:54:37 -0700607token_list_t *
Carl Worth8f6a8282010-05-14 10:44:19 -0700608_argument_list_member_at (argument_list_t *list, int index)
609{
610 argument_node_t *node;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700611 int i;
612
613 if (list == NULL)
614 return NULL;
615
616 node = list->head;
617 for (i = 0; i < index; i++) {
618 node = node->next;
619 if (node == NULL)
620 break;
621 }
622
623 if (node)
Carl Worth8f6a8282010-05-14 10:44:19 -0700624 return node->argument;
Carl Worthdcc2ecd2010-05-13 12:56:42 -0700625
626 return NULL;
627}
Carl Worth47252442010-05-19 13:54:37 -0700628
Carl Worth808401f2010-05-25 14:52:43 -0700629/* Note: This function talloc_steal()s the str pointer. */
630token_t *
631_token_create_str (void *ctx, int type, char *str)
632{
633 token_t *token;
634
635 token = xtalloc (ctx, token_t);
636 token->type = type;
637 token->value.str = talloc_steal (token, str);
638
639 return token;
640}
641
642token_t *
643_token_create_ival (void *ctx, int type, int ival)
644{
645 token_t *token;
646
647 token = xtalloc (ctx, token_t);
648 token->type = type;
649 token->value.ival = ival;
650
651 return token;
652}
653
Carl Worth47252442010-05-19 13:54:37 -0700654token_list_t *
655_token_list_create (void *ctx)
656{
657 token_list_t *list;
658
659 list = xtalloc (ctx, token_list_t);
660 list->head = NULL;
661 list->tail = NULL;
Carl Worth10ae4382010-05-25 20:35:01 -0700662 list->non_space_tail = NULL;
Carl Worth47252442010-05-19 13:54:37 -0700663
664 return list;
665}
666
667void
Carl Worth808401f2010-05-25 14:52:43 -0700668_token_list_append (token_list_t *list, token_t *token)
Carl Worth47252442010-05-19 13:54:37 -0700669{
670 token_node_t *node;
671
672 node = xtalloc (list, token_node_t);
Carl Worth808401f2010-05-25 14:52:43 -0700673 node->token = xtalloc_reference (list, token);
Carl Worth47252442010-05-19 13:54:37 -0700674
675 node->next = NULL;
676
677 if (list->head == NULL) {
678 list->head = node;
679 } else {
680 list->tail->next = node;
681 }
682
683 list->tail = node;
Carl Worth10ae4382010-05-25 20:35:01 -0700684 if (token->type != SPACE)
685 list->non_space_tail = node;
Carl Worth47252442010-05-19 13:54:37 -0700686}
687
688void
689_token_list_append_list (token_list_t *list, token_list_t *tail)
690{
Carl Wortha65cf7b2010-05-27 11:55:36 -0700691 if (tail == NULL || tail->head == NULL)
692 return;
693
Carl Worth47252442010-05-19 13:54:37 -0700694 if (list->head == NULL) {
695 list->head = tail->head;
696 } else {
697 list->tail->next = tail->head;
698 }
699
700 list->tail = tail->tail;
Carl Worth10ae4382010-05-25 20:35:01 -0700701 list->non_space_tail = tail->non_space_tail;
702}
703
Carl Worth681afbc2010-05-28 15:06:02 -0700704token_list_t *
705_token_list_copy (void *ctx, token_list_t *other)
706{
707 token_list_t *copy;
708 token_node_t *node;
709
710 if (other == NULL)
711 return NULL;
712
713 copy = _token_list_create (ctx);
714 for (node = other->head; node; node = node->next)
715 _token_list_append (copy, node->token);
716
717 return copy;
718}
719
Carl Worth10ae4382010-05-25 20:35:01 -0700720void
721_token_list_trim_trailing_space (token_list_t *list)
722{
723 token_node_t *tail, *next;
724
725 if (list->non_space_tail) {
726 tail = list->non_space_tail->next;
727 list->non_space_tail->next = NULL;
728 list->tail = list->non_space_tail;
729
730 while (tail) {
731 next = tail->next;
732 talloc_free (tail);
733 tail = next;
734 }
735 }
Carl Worth47252442010-05-19 13:54:37 -0700736}
Carl Worth80dc60b2010-05-25 14:42:00 -0700737
Carl Worth22b3ace2010-06-02 15:32:03 -0700738static int
739_token_list_length (token_list_t *list)
740{
741 int length = 0;
742 token_node_t *node;
743
744 if (list == NULL)
745 return 0;
746
747 for (node = list->head; node; node = node->next)
748 length++;
749
750 return length;
751}
752
Carl Worth0197e9b2010-05-26 08:05:19 -0700753static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700754_token_print (char **out, token_t *token)
Carl Worth0197e9b2010-05-26 08:05:19 -0700755{
756 if (token->type < 256) {
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700757 glcpp_printf (*out, "%c", token->type);
Carl Worth0197e9b2010-05-26 08:05:19 -0700758 return;
759 }
760
761 switch (token->type) {
Carl Worth8fed1cd2010-05-26 09:32:12 -0700762 case INTEGER:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700763 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
Carl Worth8fed1cd2010-05-26 09:32:12 -0700764 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700765 case IDENTIFIER:
Carl Worth050e3de2010-05-27 14:36:29 -0700766 case INTEGER_STRING:
Carl Worth0197e9b2010-05-26 08:05:19 -0700767 case OTHER:
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700768 glcpp_print (*out, token->value.str);
Carl Worth0197e9b2010-05-26 08:05:19 -0700769 break;
770 case SPACE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700771 glcpp_print (*out, " ");
Carl Worth0197e9b2010-05-26 08:05:19 -0700772 break;
773 case LEFT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700774 glcpp_print (*out, "<<");
Carl Worth0197e9b2010-05-26 08:05:19 -0700775 break;
776 case RIGHT_SHIFT:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700777 glcpp_print (*out, ">>");
Carl Worth0197e9b2010-05-26 08:05:19 -0700778 break;
779 case LESS_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700780 glcpp_print (*out, "<=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700781 break;
782 case GREATER_OR_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700783 glcpp_print (*out, ">=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700784 break;
785 case EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700786 glcpp_print (*out, "==");
Carl Worth0197e9b2010-05-26 08:05:19 -0700787 break;
788 case NOT_EQUAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700789 glcpp_print (*out, "!=");
Carl Worth0197e9b2010-05-26 08:05:19 -0700790 break;
791 case AND:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700792 glcpp_print (*out, "&&");
Carl Worth0197e9b2010-05-26 08:05:19 -0700793 break;
794 case OR:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700795 glcpp_print (*out, "||");
Carl Worth0197e9b2010-05-26 08:05:19 -0700796 break;
797 case PASTE:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700798 glcpp_print (*out, "##");
Carl Worth0197e9b2010-05-26 08:05:19 -0700799 break;
Carl Worthdd749002010-05-27 10:12:33 -0700800 case COMMA_FINAL:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700801 glcpp_print (*out, ",");
Carl Worthdd749002010-05-27 10:12:33 -0700802 break;
Carl Worth85b50e82010-05-27 14:01:18 -0700803 case PLACEHOLDER:
804 /* Nothing to print. */
805 break;
Carl Worth0197e9b2010-05-26 08:05:19 -0700806 default:
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700807 assert(!"Error: Don't know how to print token.");
Carl Worth0197e9b2010-05-26 08:05:19 -0700808 break;
809 }
810}
811
Carl Worthb06096e2010-05-29 05:54:19 -0700812/* Return a new token (talloc()ed off of 'token') formed by pasting
813 * 'token' and 'other'. Note that this function may return 'token' or
814 * 'other' directly rather than allocating anything new.
815 *
816 * Caution: Only very cursory error-checking is performed to see if
817 * the final result is a valid single token. */
818static token_t *
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700819_token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
Carl Worthad0dee62010-05-26 09:04:50 -0700820{
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700821 token_t *combined = NULL;
822
Carl Worth85b50e82010-05-27 14:01:18 -0700823 /* Pasting a placeholder onto anything makes no change. */
824 if (other->type == PLACEHOLDER)
Carl Worthb06096e2010-05-29 05:54:19 -0700825 return token;
Carl Worth85b50e82010-05-27 14:01:18 -0700826
Carl Worthb06096e2010-05-29 05:54:19 -0700827 /* When 'token' is a placeholder, just return 'other'. */
828 if (token->type == PLACEHOLDER)
829 return other;
Carl Worth85b50e82010-05-27 14:01:18 -0700830
Carl Worthad0dee62010-05-26 09:04:50 -0700831 /* A very few single-character punctuators can be combined
832 * with another to form a multi-character punctuator. */
833 switch (token->type) {
834 case '<':
Carl Worthb06096e2010-05-29 05:54:19 -0700835 if (other->type == '<')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700836 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700837 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700838 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700839 break;
840 case '>':
Carl Worthb06096e2010-05-29 05:54:19 -0700841 if (other->type == '>')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700842 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
Carl Worthb06096e2010-05-29 05:54:19 -0700843 else if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700844 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700845 break;
846 case '=':
Carl Worthb06096e2010-05-29 05:54:19 -0700847 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700848 combined = _token_create_ival (token, EQUAL, EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700849 break;
850 case '!':
Carl Worthb06096e2010-05-29 05:54:19 -0700851 if (other->type == '=')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700852 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
Carl Worthad0dee62010-05-26 09:04:50 -0700853 break;
854 case '&':
Carl Worthb06096e2010-05-29 05:54:19 -0700855 if (other->type == '&')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700856 combined = _token_create_ival (token, AND, AND);
Carl Worthad0dee62010-05-26 09:04:50 -0700857 break;
858 case '|':
Carl Worthb06096e2010-05-29 05:54:19 -0700859 if (other->type == '|')
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700860 combined = _token_create_ival (token, OR, OR);
Carl Worthad0dee62010-05-26 09:04:50 -0700861 break;
862 }
863
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700864 if (combined != NULL) {
865 /* Inherit the location from the first token */
866 combined->location = token->location;
867 return combined;
868 }
869
Carl Worthad0dee62010-05-26 09:04:50 -0700870 /* Two string-valued tokens can usually just be mashed
871 * together.
872 *
Carl Worth050e3de2010-05-27 14:36:29 -0700873 * XXX: This isn't actually legitimate. Several things here
874 * should result in a diagnostic since the result cannot be a
875 * valid, single pre-processing token. For example, pasting
876 * "123" and "abc" is not legal, but we don't catch that
877 * here. */
878 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
879 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
Carl Worthad0dee62010-05-26 09:04:50 -0700880 {
Carl Worthb06096e2010-05-29 05:54:19 -0700881 char *str;
882
883 str = xtalloc_asprintf (token, "%s%s",
884 token->value.str, other->value.str);
Kenneth Graunkeb78c9dd2010-06-16 16:58:31 -0700885 combined = _token_create_str (token, token->type, str);
886 combined->location = token->location;
887 return combined;
Carl Worthad0dee62010-05-26 09:04:50 -0700888 }
889
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -0700890 glcpp_error (&token->location, parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700891 glcpp_print (parser->info_log, "Pasting \"");
892 _token_print (&parser->info_log, token);
893 glcpp_print (parser->info_log, "\" and \"");
894 _token_print (&parser->info_log, other);
895 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
Carl Worthb06096e2010-05-29 05:54:19 -0700896
897 return token;
Carl Worthad0dee62010-05-26 09:04:50 -0700898}
899
Carl Worth0197e9b2010-05-26 08:05:19 -0700900static void
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700901_token_list_print (glcpp_parser_t *parser, token_list_t *list)
Carl Worth0197e9b2010-05-26 08:05:19 -0700902{
903 token_node_t *node;
904
905 if (list == NULL)
906 return;
907
908 for (node = list->head; node; node = node->next)
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700909 _token_print (&parser->output, node->token);
Carl Worth0197e9b2010-05-26 08:05:19 -0700910}
911
Carl Worth3a37b872010-05-10 11:44:09 -0700912void
Kenneth Graunke465e03e2010-06-16 16:35:57 -0700913yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
Carl Worth3a37b872010-05-10 11:44:09 -0700914{
Kenneth Graunkef1e6c062010-06-17 12:03:25 -0700915 glcpp_error(locp, parser, "%s", error);
Carl Worth3a37b872010-05-10 11:44:09 -0700916}
Carl Worth0b27b5f2010-05-10 16:16:06 -0700917
Carl Worth33cc4002010-05-12 12:17:10 -0700918glcpp_parser_t *
919glcpp_parser_create (void)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700920{
Carl Worth33cc4002010-05-12 12:17:10 -0700921 glcpp_parser_t *parser;
922
Carl Worth5070a202010-05-12 12:45:33 -0700923 parser = xtalloc (NULL, glcpp_parser_t);
Carl Worth33cc4002010-05-12 12:17:10 -0700924
Carl Worth8f38aff2010-05-19 10:01:29 -0700925 glcpp_lex_init_extra (parser, &parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700926 parser->defines = hash_table_ctor (32, hash_table_string_hash,
927 hash_table_string_compare);
Carl Worth22b3ace2010-06-02 15:32:03 -0700928 parser->active = NULL;
Carl Wortha771a402010-06-01 11:20:18 -0700929 parser->lexing_if = 0;
Carl Worthf34a0002010-05-25 16:59:02 -0700930 parser->space_tokens = 1;
Carl Worth95951ea2010-05-26 15:57:10 -0700931 parser->newline_as_space = 0;
932 parser->in_control_line = 0;
933 parser->paren_count = 0;
Carl Worth5a6b9a22010-05-20 14:29:43 -0700934
Carl Worthb20d33c2010-05-20 22:27:07 -0700935 parser->skip_stack = NULL;
936
Carl Worth8e82fcb2010-05-26 11:15:21 -0700937 parser->lex_from_list = NULL;
938 parser->lex_from_node = NULL;
939
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700940 parser->output = talloc_strdup(parser, "");
Kenneth Graunke33eaa3e2010-06-18 19:52:36 -0700941 parser->info_log = talloc_strdup(parser, "");
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -0700942
Carl Worth33cc4002010-05-12 12:17:10 -0700943 return parser;
Carl Worth0b27b5f2010-05-10 16:16:06 -0700944}
945
946int
947glcpp_parser_parse (glcpp_parser_t *parser)
948{
949 return yyparse (parser);
950}
951
952void
Carl Worth33cc4002010-05-12 12:17:10 -0700953glcpp_parser_destroy (glcpp_parser_t *parser)
Carl Worth0b27b5f2010-05-10 16:16:06 -0700954{
Carl Worthb20d33c2010-05-20 22:27:07 -0700955 if (parser->skip_stack)
Kenneth Graunke07745232010-06-17 12:41:46 -0700956 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
Carl Worth8f38aff2010-05-19 10:01:29 -0700957 glcpp_lex_destroy (parser->scanner);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700958 hash_table_dtor (parser->defines);
Carl Worth33cc4002010-05-12 12:17:10 -0700959 talloc_free (parser);
Carl Worth0b27b5f2010-05-10 16:16:06 -0700960}
Carl Worthc6d5af32010-05-11 12:30:09 -0700961
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 Graunkeca9e5fc2010-06-16 17:31:50 -07001097 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
Carl Worth681afbc2010-05-28 15:06:02 -07001098 return NULL;
Carl Worthae6517f2010-05-25 15:24:59 -07001099 }
1100
Carl Worth9ce18cf2010-05-25 17:32:21 -07001101 if (macro->replacements == NULL) {
1102 talloc_free (arguments);
Carl Worth681afbc2010-05-28 15:06:02 -07001103 return _token_list_create (parser);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001104 }
1105
Carl Wortha19297b2010-05-27 13:29:19 -07001106 if (! ((_argument_list_length (arguments) ==
1107 _string_list_length (macro->parameters)) ||
1108 (_string_list_length (macro->parameters) == 0 &&
1109 _argument_list_length (arguments) == 1 &&
1110 arguments->head->argument->head == NULL)))
Carl Worth9ce18cf2010-05-25 17:32:21 -07001111 {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001112 glcpp_error (&node->token->location, parser,
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001113 "Error: macro %s invoked with %d arguments (expected %d)\n",
1114 identifier,
1115 _argument_list_length (arguments),
1116 _string_list_length (macro->parameters));
Carl Worth681afbc2010-05-28 15:06:02 -07001117 return NULL;
Carl Worth9ce18cf2010-05-25 17:32:21 -07001118 }
1119
Carl Worth0197e9b2010-05-26 08:05:19 -07001120 /* Perform argument substitution on the replacement list. */
1121 substituted = _token_list_create (arguments);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001122
Carl Worthce540f22010-05-26 08:25:44 -07001123 for (node = macro->replacements->head; node; node = node->next)
1124 {
1125 if (node->token->type == IDENTIFIER &&
Carl Worth9ce18cf2010-05-25 17:32:21 -07001126 _string_list_contains (macro->parameters,
Carl Worthce540f22010-05-26 08:25:44 -07001127 node->token->value.str,
Carl Worth9ce18cf2010-05-25 17:32:21 -07001128 &parameter_index))
1129 {
1130 token_list_t *argument;
1131 argument = _argument_list_member_at (arguments,
1132 parameter_index);
Carl Worthd5cd4032010-05-26 08:09:29 -07001133 /* Before substituting, we expand the argument
Carl Worth85b50e82010-05-27 14:01:18 -07001134 * tokens, or append a placeholder token for
1135 * an empty argument. */
1136 if (argument->head) {
Carl Worth681afbc2010-05-28 15:06:02 -07001137 _glcpp_parser_expand_token_list (parser,
1138 argument);
1139 _token_list_append_list (substituted, argument);
Carl Worth85b50e82010-05-27 14:01:18 -07001140 } else {
1141 token_t *new_token;
1142
1143 new_token = _token_create_ival (substituted,
1144 PLACEHOLDER,
1145 PLACEHOLDER);
1146 _token_list_append (substituted, new_token);
1147 }
Carl Worth9ce18cf2010-05-25 17:32:21 -07001148 } else {
Carl Worthce540f22010-05-26 08:25:44 -07001149 _token_list_append (substituted, node->token);
Carl Worth9ce18cf2010-05-25 17:32:21 -07001150 }
1151 }
1152
Carl Worthad0dee62010-05-26 09:04:50 -07001153 /* After argument substitution, and before further expansion
1154 * below, implement token pasting. */
1155
Carl Worthb06096e2010-05-29 05:54:19 -07001156 _token_list_trim_trailing_space (substituted);
1157
Carl Worthad0dee62010-05-26 09:04:50 -07001158 node = substituted->head;
1159 while (node)
1160 {
1161 token_node_t *next_non_space;
1162
1163 /* Look ahead for a PASTE token, skipping space. */
1164 next_non_space = node->next;
1165 while (next_non_space && next_non_space->token->type == SPACE)
1166 next_non_space = next_non_space->next;
1167
1168 if (next_non_space == NULL)
1169 break;
1170
1171 if (next_non_space->token->type != PASTE) {
1172 node = next_non_space;
1173 continue;
1174 }
1175
1176 /* Now find the next non-space token after the PASTE. */
1177 next_non_space = next_non_space->next;
1178 while (next_non_space && next_non_space->token->type == SPACE)
1179 next_non_space = next_non_space->next;
1180
1181 if (next_non_space == NULL) {
Kenneth Graunkeca9e5fc2010-06-16 17:31:50 -07001182 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
Carl Worth681afbc2010-05-28 15:06:02 -07001183 return NULL;
Carl Worthad0dee62010-05-26 09:04:50 -07001184 }
1185
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001186 node->token = _token_paste (parser, node->token, next_non_space->token);
Carl Worthad0dee62010-05-26 09:04:50 -07001187 node->next = next_non_space->next;
Carl Worthb06096e2010-05-29 05:54:19 -07001188 if (next_non_space == substituted->tail)
1189 substituted->tail = node;
Carl Worthad0dee62010-05-26 09:04:50 -07001190
1191 node = node->next;
1192 }
1193
Carl Worthb06096e2010-05-29 05:54:19 -07001194 substituted->non_space_tail = substituted->tail;
1195
Carl Worth681afbc2010-05-28 15:06:02 -07001196 return substituted;
Carl Worthae6517f2010-05-25 15:24:59 -07001197}
1198
Carl Worth681afbc2010-05-28 15:06:02 -07001199/* Compute the complete expansion of node, (and subsequent nodes after
1200 * 'node' in the case that 'node' is a function-like macro and
1201 * subsequent nodes are arguments).
1202 *
1203 * Returns NULL if node is a simple token with no expansion.
1204 *
1205 * Otherwise, returns the token list that results from the expansion
1206 * and sets *last to the last node in the list that was consumed by
1207 * the expansion. Specificallty, *last will be set as follows:
1208 *
1209 * As 'node' in the case of object-like macro expansion.
1210 *
1211 * As the token of the closing right parenthesis in the case of
1212 * function-like macro expansion.
1213 */
1214static token_list_t *
1215_glcpp_parser_expand_node (glcpp_parser_t *parser,
1216 token_node_t *node,
1217 token_node_t **last)
Carl Worth3c93d392010-05-28 08:17:46 -07001218{
Carl Worth681afbc2010-05-28 15:06:02 -07001219 token_t *token = node->token;
Carl Worth3c93d392010-05-28 08:17:46 -07001220 const char *identifier;
1221 macro_t *macro;
Carl Worth3c93d392010-05-28 08:17:46 -07001222
1223 /* We only expand identifiers */
1224 if (token->type != IDENTIFIER) {
1225 /* We change any COMMA into a COMMA_FINAL to prevent
1226 * it being mistaken for an argument separator
1227 * later. */
1228 if (token->type == ',') {
Carl Worth681afbc2010-05-28 15:06:02 -07001229 token->type = COMMA_FINAL;
1230 token->value.ival = COMMA_FINAL;
Carl Worth3c93d392010-05-28 08:17:46 -07001231 }
Carl Worth681afbc2010-05-28 15:06:02 -07001232
1233 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001234 }
1235
1236 /* Look up this identifier in the hash table. */
1237 identifier = token->value.str;
1238 macro = hash_table_find (parser->defines, identifier);
1239
Carl Worth681afbc2010-05-28 15:06:02 -07001240 /* Not a macro, so no expansion needed. */
1241 if (macro == NULL)
1242 return NULL;
Carl Worth3c93d392010-05-28 08:17:46 -07001243
1244 /* Finally, don't expand this macro if we're already actively
1245 * expanding it, (to avoid infinite recursion). */
Carl Worth22b3ace2010-06-02 15:32:03 -07001246 if (_active_list_contains (parser->active, identifier)) {
Carl Worth3c93d392010-05-28 08:17:46 -07001247 /* We change the token type here from IDENTIFIER to
1248 * OTHER to prevent any future expansion of this
1249 * unexpanded token. */
1250 char *str;
Carl Worth681afbc2010-05-28 15:06:02 -07001251 token_list_t *expansion;
1252 token_t *final;
Carl Worth3c93d392010-05-28 08:17:46 -07001253
Carl Worth681afbc2010-05-28 15:06:02 -07001254 str = xtalloc_strdup (parser, token->value.str);
1255 final = _token_create_str (parser, OTHER, str);
1256 expansion = _token_list_create (parser);
1257 _token_list_append (expansion, final);
1258 *last = node;
1259 return expansion;
Carl Worth3c93d392010-05-28 08:17:46 -07001260 }
1261
Carl Worth681afbc2010-05-28 15:06:02 -07001262 if (! macro->is_function)
1263 {
1264 *last = node;
1265
1266 if (macro->replacements == NULL)
1267 return _token_list_create (parser);
1268
Carl Worth22b3ace2010-06-02 15:32:03 -07001269 return _token_list_copy (parser, macro->replacements);
Carl Worth3c93d392010-05-28 08:17:46 -07001270 }
Carl Worth681afbc2010-05-28 15:06:02 -07001271
1272 return _glcpp_parser_expand_function (parser, node, last);
1273}
1274
Carl Worth22b3ace2010-06-02 15:32:03 -07001275/* Push a new identifier onto the active list, returning the new list.
1276 *
1277 * Here, 'marker' is the token node that appears in the list after the
1278 * expansion of 'identifier'. That is, when the list iterator begins
1279 * examinging 'marker', then it is time to pop this node from the
1280 * active stack.
1281 */
1282active_list_t *
1283_active_list_push (active_list_t *list,
1284 const char *identifier,
1285 token_node_t *marker)
1286{
1287 active_list_t *node;
1288
1289 node = xtalloc (list, active_list_t);
1290 node->identifier = xtalloc_strdup (node, identifier);
1291 node->marker = marker;
1292 node->next = list;
1293
1294 return node;
1295}
1296
1297active_list_t *
1298_active_list_pop (active_list_t *list)
1299{
1300 active_list_t *node = list;
1301
1302 if (node == NULL)
1303 return NULL;
1304
1305 node = list->next;
1306 talloc_free (list);
1307
1308 return node;
1309}
1310
1311int
1312_active_list_contains (active_list_t *list, const char *identifier)
1313{
1314 active_list_t *node;
1315
1316 if (list == NULL)
1317 return 0;
1318
1319 for (node = list; node; node = node->next)
1320 if (strcmp (node->identifier, identifier) == 0)
1321 return 1;
1322
1323 return 0;
1324}
1325
Carl Worth681afbc2010-05-28 15:06:02 -07001326/* Walk over the token list replacing nodes with their expansion.
1327 * Whenever nodes are expanded the walking will walk over the new
1328 * nodes, continuing to expand as necessary. The results are placed in
1329 * 'list' itself;
1330 */
1331static void
1332_glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1333 token_list_t *list)
1334{
1335 token_node_t *node_prev;
1336 token_node_t *node, *last;
1337 token_list_t *expansion;
1338
1339 if (list == NULL)
1340 return;
1341
1342 _token_list_trim_trailing_space (list);
1343
1344 node_prev = NULL;
1345 node = list->head;
1346
1347 while (node) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001348
1349 while (parser->active && parser->active->marker == node)
1350 parser->active = _active_list_pop (parser->active);
1351
Carl Worth681afbc2010-05-28 15:06:02 -07001352 /* Find the expansion for node, which will replace all
1353 * nodes from node to last, inclusive. */
1354 expansion = _glcpp_parser_expand_node (parser, node, &last);
1355 if (expansion) {
Carl Worth22b3ace2010-06-02 15:32:03 -07001356 token_node_t *n;
1357
1358 for (n = node; n != last->next; n = n->next)
1359 while (parser->active &&
1360 parser->active->marker == n)
1361 {
1362 parser->active = _active_list_pop (parser->active);
1363 }
1364
1365 parser->active = _active_list_push (parser->active,
1366 node->token->value.str,
1367 last->next);
1368
Carl Worth681afbc2010-05-28 15:06:02 -07001369 /* Splice expansion into list, supporting a
1370 * simple deletion if the expansion is
1371 * empty. */
1372 if (expansion->head) {
1373 if (node_prev)
1374 node_prev->next = expansion->head;
1375 else
1376 list->head = expansion->head;
1377 expansion->tail->next = last->next;
1378 if (last == list->tail)
1379 list->tail = expansion->tail;
1380 } else {
1381 if (node_prev)
1382 node_prev->next = last->next;
1383 else
1384 list->head = last->next;
1385 if (last == list->tail)
Kenneth Graunke0656f6b2010-06-16 11:56:36 -07001386 list->tail = NULL;
Carl Worth681afbc2010-05-28 15:06:02 -07001387 }
1388 } else {
1389 node_prev = node;
1390 }
1391 node = node_prev ? node_prev->next : list->head;
1392 }
1393
Carl Worth22b3ace2010-06-02 15:32:03 -07001394 while (parser->active)
1395 parser->active = _active_list_pop (parser->active);
1396
Carl Worth681afbc2010-05-28 15:06:02 -07001397 list->non_space_tail = list->tail;
Carl Worth3c93d392010-05-28 08:17:46 -07001398}
1399
Carl Worthae6517f2010-05-25 15:24:59 -07001400void
1401_glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1402 token_list_t *list)
1403{
Carl Worthae6517f2010-05-25 15:24:59 -07001404 if (list == NULL)
1405 return;
1406
Carl Worth681afbc2010-05-28 15:06:02 -07001407 _glcpp_parser_expand_token_list (parser, list);
Carl Worth10ae4382010-05-25 20:35:01 -07001408
Carl Worth681afbc2010-05-28 15:06:02 -07001409 _token_list_trim_trailing_space (list);
Carl Worth0197e9b2010-05-26 08:05:19 -07001410
Kenneth Graunke4c8a1af2010-06-16 11:57:48 -07001411 _token_list_print (parser, list);
Carl Worthae6517f2010-05-25 15:24:59 -07001412}
1413
1414void
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001415_check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1416 const char *identifier)
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001417{
1418 /* According to the GLSL specification, macro names starting with "__"
1419 * or "GL_" are reserved for future use. So, don't allow them.
1420 */
1421 if (strncmp(identifier, "__", 2) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001422 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001423 }
1424 if (strncmp(identifier, "GL_", 3) == 0) {
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001425 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001426 }
1427}
1428
1429void
Carl Worthfcbbb462010-05-13 09:36:23 -07001430_define_object_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001431 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001432 const char *identifier,
Carl Worth47252442010-05-19 13:54:37 -07001433 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001434{
1435 macro_t *macro;
1436
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001437 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001438
Carl Worthfcbbb462010-05-13 09:36:23 -07001439 macro = xtalloc (parser, macro_t);
1440
1441 macro->is_function = 0;
Carl Worthc5e98552010-05-14 10:12:21 -07001442 macro->parameters = NULL;
Carl Wortha807fb72010-05-18 22:10:04 -07001443 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001444 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001445
1446 hash_table_insert (parser->defines, macro, identifier);
1447}
1448
1449void
1450_define_function_macro (glcpp_parser_t *parser,
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001451 YYLTYPE *loc,
Carl Worthfcbbb462010-05-13 09:36:23 -07001452 const char *identifier,
Carl Worthc5e98552010-05-14 10:12:21 -07001453 string_list_t *parameters,
Carl Worth47252442010-05-19 13:54:37 -07001454 token_list_t *replacements)
Carl Worthfcbbb462010-05-13 09:36:23 -07001455{
1456 macro_t *macro;
1457
Kenneth Graunkedcdf62f2010-06-17 12:21:53 -07001458 _check_for_reserved_macro_name(parser, loc, identifier);
Kenneth Graunke2ab0b132010-06-04 14:53:58 -07001459
Carl Worthfcbbb462010-05-13 09:36:23 -07001460 macro = xtalloc (parser, macro_t);
1461
1462 macro->is_function = 1;
Carl Worthc5e98552010-05-14 10:12:21 -07001463 macro->parameters = talloc_steal (macro, parameters);
Carl Wortha807fb72010-05-18 22:10:04 -07001464 macro->identifier = talloc_strdup (macro, identifier);
Carl Worthaaa9acb2010-05-19 13:28:24 -07001465 macro->replacements = talloc_steal (macro, replacements);
Carl Worthfcbbb462010-05-13 09:36:23 -07001466
1467 hash_table_insert (parser->defines, macro, identifier);
1468}
1469
Carl Worth8f38aff2010-05-19 10:01:29 -07001470static int
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001471glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
Carl Worth8f38aff2010-05-19 10:01:29 -07001472{
Carl Worth8e82fcb2010-05-26 11:15:21 -07001473 token_node_t *node;
1474 int ret;
1475
Carl Worth95951ea2010-05-26 15:57:10 -07001476 if (parser->lex_from_list == NULL) {
Kenneth Graunke465e03e2010-06-16 16:35:57 -07001477 ret = glcpp_lex (yylval, yylloc, parser->scanner);
Carl Worth95951ea2010-05-26 15:57:10 -07001478
1479 /* XXX: This ugly block of code exists for the sole
1480 * purpose of converting a NEWLINE token into a SPACE
1481 * token, but only in the case where we have seen a
1482 * function-like macro name, but have not yet seen its
1483 * closing parenthesis.
1484 *
1485 * There's perhaps a more compact way to do this with
1486 * mid-rule actions in the grammar.
1487 *
1488 * I'm definitely not pleased with the complexity of
1489 * this code here.
1490 */
1491 if (parser->newline_as_space)
1492 {
1493 if (ret == '(') {
1494 parser->paren_count++;
1495 } else if (ret == ')') {
1496 parser->paren_count--;
1497 if (parser->paren_count == 0)
1498 parser->newline_as_space = 0;
1499 } else if (ret == NEWLINE) {
1500 ret = SPACE;
1501 } else if (ret != SPACE) {
1502 if (parser->paren_count == 0)
1503 parser->newline_as_space = 0;
1504 }
1505 }
1506 else if (parser->in_control_line)
1507 {
1508 if (ret == NEWLINE)
1509 parser->in_control_line = 0;
1510 }
1511 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1512 ret == HASH_UNDEF || ret == HASH_IF ||
1513 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1514 ret == HASH_ELIF || ret == HASH_ELSE ||
1515 ret == HASH_ENDIF || ret == HASH)
1516 {
1517 parser->in_control_line = 1;
1518 }
1519 else if (ret == IDENTIFIER)
1520 {
1521 macro_t *macro;
1522 macro = hash_table_find (parser->defines,
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001523 yylval->str);
Carl Worth95951ea2010-05-26 15:57:10 -07001524 if (macro && macro->is_function) {
1525 parser->newline_as_space = 1;
1526 parser->paren_count = 0;
1527 }
1528 }
1529
1530 return ret;
1531 }
Carl Worth8e82fcb2010-05-26 11:15:21 -07001532
1533 node = parser->lex_from_node;
1534
1535 if (node == NULL) {
1536 talloc_free (parser->lex_from_list);
1537 parser->lex_from_list = NULL;
1538 return NEWLINE;
1539 }
1540
Kenneth Graunkee0e429f2010-06-16 16:26:28 -07001541 *yylval = node->token->value;
Carl Worth8e82fcb2010-05-26 11:15:21 -07001542 ret = node->token->type;
1543
1544 parser->lex_from_node = node->next;
1545
1546 return ret;
1547}
1548
1549static void
1550glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1551{
1552 token_node_t *node;
1553
1554 assert (parser->lex_from_list == NULL);
1555
1556 /* Copy list, eliminating any space tokens. */
1557 parser->lex_from_list = _token_list_create (parser);
1558
1559 for (node = list->head; node; node = node->next) {
1560 if (node->token->type == SPACE)
1561 continue;
1562 _token_list_append (parser->lex_from_list, node->token);
1563 }
1564
1565 talloc_free (list);
1566
1567 parser->lex_from_node = parser->lex_from_list->head;
1568
1569 /* It's possible the list consisted of nothing but whitespace. */
1570 if (parser->lex_from_node == NULL) {
1571 talloc_free (parser->lex_from_list);
1572 parser->lex_from_list = NULL;
1573 }
Carl Worth8f38aff2010-05-19 10:01:29 -07001574}
Carl Worthb20d33c2010-05-20 22:27:07 -07001575
1576static void
Kenneth Graunke07745232010-06-17 12:41:46 -07001577_glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1578 int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001579{
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);
Kenneth Graunke07745232010-06-17 12:41:46 -07001587 node->loc = *loc;
Carl Worthb20d33c2010-05-20 22:27:07 -07001588
1589 if (current == SKIP_NO_SKIP) {
1590 if (condition)
1591 node->type = SKIP_NO_SKIP;
1592 else
1593 node->type = SKIP_TO_ELSE;
1594 } else {
1595 node->type = SKIP_TO_ENDIF;
1596 }
1597
1598 node->next = parser->skip_stack;
1599 parser->skip_stack = node;
1600}
1601
1602static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001603_glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1604 const char *type, int condition)
Carl Worthb20d33c2010-05-20 22:27:07 -07001605{
1606 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001607 glcpp_error (loc, parser, "%s without #if\n", type);
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001608 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001609 }
1610
1611 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1612 if (condition)
1613 parser->skip_stack->type = SKIP_NO_SKIP;
1614 } else {
1615 parser->skip_stack->type = SKIP_TO_ENDIF;
1616 }
1617}
Carl Worth80dc60b2010-05-25 14:42:00 -07001618
Carl Worthb20d33c2010-05-20 22:27:07 -07001619static void
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001620_glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
Carl Worthb20d33c2010-05-20 22:27:07 -07001621{
1622 skip_node_t *node;
1623
1624 if (parser->skip_stack == NULL) {
Kenneth Graunke8a132aa2010-06-17 12:30:57 -07001625 glcpp_error (loc, parser, "#endif without #if\n");
Kenneth Graunkee8e93a42010-06-17 12:58:54 -07001626 return;
Carl Worthb20d33c2010-05-20 22:27:07 -07001627 }
1628
1629 node = parser->skip_stack;
1630 parser->skip_stack = node->next;
1631 talloc_free (node);
1632}