glsl2: Move the compiler to the subdirectory it will live in in Mesa.
[mesa.git] / src / glsl / glcpp / glcpp-parse.y
1 %{
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>
27 #include <assert.h>
28 #include <inttypes.h>
29
30 #include "glcpp.h"
31
32 #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
36 static void
37 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error);
38
39 static void
40 _define_object_macro (glcpp_parser_t *parser,
41 YYLTYPE *loc,
42 const char *macro,
43 token_list_t *replacements);
44
45 static void
46 _define_function_macro (glcpp_parser_t *parser,
47 YYLTYPE *loc,
48 const char *macro,
49 string_list_t *parameters,
50 token_list_t *replacements);
51
52 static string_list_t *
53 _string_list_create (void *ctx);
54
55 static void
56 _string_list_append_item (string_list_t *list, const char *str);
57
58 static int
59 _string_list_contains (string_list_t *list, const char *member, int *index);
60
61 static int
62 _string_list_length (string_list_t *list);
63
64 static argument_list_t *
65 _argument_list_create (void *ctx);
66
67 static void
68 _argument_list_append (argument_list_t *list, token_list_t *argument);
69
70 static int
71 _argument_list_length (argument_list_t *list);
72
73 static token_list_t *
74 _argument_list_member_at (argument_list_t *list, int index);
75
76 /* Note: This function talloc_steal()s the str pointer. */
77 static token_t *
78 _token_create_str (void *ctx, int type, char *str);
79
80 static token_t *
81 _token_create_ival (void *ctx, int type, int ival);
82
83 static token_list_t *
84 _token_list_create (void *ctx);
85
86 /* Note: This function adds a talloc_reference() to token.
87 *
88 * You may want to talloc_unlink any current reference if you no
89 * longer need it. */
90 static void
91 _token_list_append (token_list_t *list, token_t *token);
92
93 static void
94 _token_list_append_list (token_list_t *list, token_list_t *tail);
95
96 static active_list_t *
97 _active_list_push (active_list_t *list,
98 const char *identifier,
99 token_node_t *marker);
100
101 static active_list_t *
102 _active_list_pop (active_list_t *list);
103
104 int
105 _active_list_contains (active_list_t *list, const char *identifier);
106
107 static void
108 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
109 token_list_t *list);
110
111 static void
112 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
113 token_list_t *list);
114
115 static void
116 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
117 int condition);
118
119 static void
120 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
121 const char *type, int condition);
122
123 static void
124 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc);
125
126 #define yylex glcpp_parser_lex
127
128 static int
129 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser);
130
131 static void
132 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list);
133
134 %}
135
136 %pure-parser
137 %error-verbose
138 %locations
139
140 %parse-param {glcpp_parser_t *parser}
141 %lex-param {glcpp_parser_t *parser}
142
143 %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
144 %token PASTE
145 %type <ival> expression INTEGER operator SPACE
146 %type <str> IDENTIFIER INTEGER_STRING OTHER
147 %type <string_list> identifier_list
148 %type <token> preprocessing_token conditional_token
149 %type <token_list> pp_tokens replacement_list text_line conditional_tokens
150 %left OR
151 %left AND
152 %left '|'
153 %left '^'
154 %left '&'
155 %left EQUAL NOT_EQUAL
156 %left '<' '>' LESS_OR_EQUAL GREATER_OR_EQUAL
157 %left LEFT_SHIFT RIGHT_SHIFT
158 %left '+' '-'
159 %left '*' '/' '%'
160 %right UNARY
161
162 %%
163
164 input:
165 /* empty */
166 | input line
167 ;
168
169 line:
170 control_line {
171 glcpp_print(parser->output, "\n");
172 }
173 | text_line {
174 _glcpp_parser_print_expanded_token_list (parser, $1);
175 glcpp_print(parser->output, "\n");
176 talloc_free ($1);
177 }
178 | expanded_line
179 | HASH non_directive
180 ;
181
182 expanded_line:
183 IF_EXPANDED expression NEWLINE {
184 _glcpp_parser_skip_stack_push_if (parser, & @1, $2);
185 }
186 | ELIF_EXPANDED expression NEWLINE {
187 _glcpp_parser_skip_stack_change_if (parser, & @1, "elif", $2);
188 }
189 ;
190
191 control_line:
192 HASH_DEFINE_OBJ IDENTIFIER replacement_list NEWLINE {
193 _define_object_macro (parser, & @2, $2, $3);
194 }
195 | HASH_DEFINE_FUNC IDENTIFIER '(' ')' replacement_list NEWLINE {
196 _define_function_macro (parser, & @2, $2, NULL, $5);
197 }
198 | HASH_DEFINE_FUNC IDENTIFIER '(' identifier_list ')' replacement_list NEWLINE {
199 _define_function_macro (parser, & @2, $2, $4, $6);
200 }
201 | HASH_UNDEF IDENTIFIER NEWLINE {
202 macro_t *macro = hash_table_find (parser->defines, $2);
203 if (macro) {
204 /* XXX: Need hash table to support a real way
205 * to remove an element rather than prefixing
206 * a new node with data of NULL like this. */
207 hash_table_insert (parser->defines, NULL, $2);
208 talloc_free (macro);
209 }
210 talloc_free ($2);
211 }
212 | HASH_IF conditional_tokens NEWLINE {
213 token_list_t *expanded;
214 token_t *token;
215
216 expanded = _token_list_create (parser);
217 token = _token_create_ival (parser, IF_EXPANDED, IF_EXPANDED);
218 _token_list_append (expanded, token);
219 talloc_unlink (parser, token);
220 _glcpp_parser_expand_token_list (parser, $2);
221 _token_list_append_list (expanded, $2);
222 glcpp_parser_lex_from (parser, expanded);
223 }
224 | HASH_IFDEF IDENTIFIER junk NEWLINE {
225 macro_t *macro = hash_table_find (parser->defines, $2);
226 talloc_free ($2);
227 _glcpp_parser_skip_stack_push_if (parser, & @1, macro != NULL);
228 }
229 | HASH_IFNDEF IDENTIFIER junk NEWLINE {
230 macro_t *macro = hash_table_find (parser->defines, $2);
231 talloc_free ($2);
232 _glcpp_parser_skip_stack_push_if (parser, & @1, macro == NULL);
233 }
234 | HASH_ELIF conditional_tokens NEWLINE {
235 token_list_t *expanded;
236 token_t *token;
237
238 expanded = _token_list_create (parser);
239 token = _token_create_ival (parser, ELIF_EXPANDED, ELIF_EXPANDED);
240 _token_list_append (expanded, token);
241 talloc_unlink (parser, token);
242 _glcpp_parser_expand_token_list (parser, $2);
243 _token_list_append_list (expanded, $2);
244 glcpp_parser_lex_from (parser, expanded);
245 }
246 | HASH_ELIF NEWLINE {
247 /* #elif without an expression results in a warning if the
248 * condition doesn't matter (we just handled #if 1 or such)
249 * but an error otherwise. */
250 if (parser->skip_stack != NULL && parser->skip_stack->type == SKIP_NO_SKIP) {
251 parser->skip_stack->type = SKIP_TO_ENDIF;
252 glcpp_warning(& @1, parser, "ignoring illegal #elif without expression");
253 } else {
254 glcpp_error(& @1, parser, "#elif needs an expression");
255 }
256 }
257 | HASH_ELSE NEWLINE {
258 _glcpp_parser_skip_stack_change_if (parser, & @1, "else", 1);
259 }
260 | HASH_ENDIF NEWLINE {
261 _glcpp_parser_skip_stack_pop (parser, & @1);
262 }
263 | HASH NEWLINE
264 ;
265
266 expression:
267 INTEGER_STRING {
268 if (strlen ($1) >= 3 && strncmp ($1, "0x", 2) == 0) {
269 $$ = strtoll ($1 + 2, NULL, 16);
270 } else if ($1[0] == '0') {
271 $$ = strtoll ($1, NULL, 8);
272 } else {
273 $$ = strtoll ($1, NULL, 10);
274 }
275 }
276 | INTEGER {
277 $$ = $1;
278 }
279 | expression OR expression {
280 $$ = $1 || $3;
281 }
282 | expression AND expression {
283 $$ = $1 && $3;
284 }
285 | expression '|' expression {
286 $$ = $1 | $3;
287 }
288 | expression '^' expression {
289 $$ = $1 ^ $3;
290 }
291 | expression '&' expression {
292 $$ = $1 & $3;
293 }
294 | expression NOT_EQUAL expression {
295 $$ = $1 != $3;
296 }
297 | expression EQUAL expression {
298 $$ = $1 == $3;
299 }
300 | expression GREATER_OR_EQUAL expression {
301 $$ = $1 >= $3;
302 }
303 | expression LESS_OR_EQUAL expression {
304 $$ = $1 <= $3;
305 }
306 | expression '>' expression {
307 $$ = $1 > $3;
308 }
309 | expression '<' expression {
310 $$ = $1 < $3;
311 }
312 | expression RIGHT_SHIFT expression {
313 $$ = $1 >> $3;
314 }
315 | expression LEFT_SHIFT expression {
316 $$ = $1 << $3;
317 }
318 | expression '-' expression {
319 $$ = $1 - $3;
320 }
321 | expression '+' expression {
322 $$ = $1 + $3;
323 }
324 | expression '%' expression {
325 $$ = $1 % $3;
326 }
327 | expression '/' expression {
328 $$ = $1 / $3;
329 }
330 | expression '*' expression {
331 $$ = $1 * $3;
332 }
333 | '!' expression %prec UNARY {
334 $$ = ! $2;
335 }
336 | '~' expression %prec UNARY {
337 $$ = ~ $2;
338 }
339 | '-' expression %prec UNARY {
340 $$ = - $2;
341 }
342 | '+' expression %prec UNARY {
343 $$ = + $2;
344 }
345 | '(' expression ')' {
346 $$ = $2;
347 }
348 ;
349
350 identifier_list:
351 IDENTIFIER {
352 $$ = _string_list_create (parser);
353 _string_list_append_item ($$, $1);
354 talloc_steal ($$, $1);
355 }
356 | identifier_list ',' IDENTIFIER {
357 $$ = $1;
358 _string_list_append_item ($$, $3);
359 talloc_steal ($$, $3);
360 }
361 ;
362
363 text_line:
364 NEWLINE { $$ = NULL; }
365 | pp_tokens NEWLINE
366 ;
367
368 non_directive:
369 pp_tokens NEWLINE {
370 yyerror (& @1, parser, "Invalid tokens after #");
371 }
372 ;
373
374 replacement_list:
375 /* empty */ { $$ = NULL; }
376 | pp_tokens
377 ;
378
379 junk:
380 /* empty */
381 | pp_tokens {
382 glcpp_warning(&@1, parser, "extra tokens at end of directive");
383 }
384 ;
385
386 conditional_token:
387 /* Handle "defined" operator */
388 DEFINED IDENTIFIER {
389 int v = hash_table_find (parser->defines, $2) ? 1 : 0;
390 $$ = _token_create_ival (parser, INTEGER, v);
391 }
392 | DEFINED '(' IDENTIFIER ')' {
393 int v = hash_table_find (parser->defines, $3) ? 1 : 0;
394 $$ = _token_create_ival (parser, INTEGER, v);
395 }
396 | preprocessing_token
397 ;
398
399 conditional_tokens:
400 /* Exactly the same as pp_tokens, but using conditional_token */
401 conditional_token {
402 parser->space_tokens = 1;
403 $$ = _token_list_create (parser);
404 _token_list_append ($$, $1);
405 talloc_unlink (parser, $1);
406 }
407 | conditional_tokens conditional_token {
408 $$ = $1;
409 _token_list_append ($$, $2);
410 talloc_unlink (parser, $2);
411 }
412 ;
413
414 pp_tokens:
415 preprocessing_token {
416 parser->space_tokens = 1;
417 $$ = _token_list_create (parser);
418 _token_list_append ($$, $1);
419 talloc_unlink (parser, $1);
420 }
421 | pp_tokens preprocessing_token {
422 $$ = $1;
423 _token_list_append ($$, $2);
424 talloc_unlink (parser, $2);
425 }
426 ;
427
428 preprocessing_token:
429 IDENTIFIER {
430 $$ = _token_create_str (parser, IDENTIFIER, $1);
431 $$->location = yylloc;
432 }
433 | INTEGER_STRING {
434 $$ = _token_create_str (parser, INTEGER_STRING, $1);
435 $$->location = yylloc;
436 }
437 | operator {
438 $$ = _token_create_ival (parser, $1, $1);
439 $$->location = yylloc;
440 }
441 | OTHER {
442 $$ = _token_create_str (parser, OTHER, $1);
443 $$->location = yylloc;
444 }
445 | SPACE {
446 $$ = _token_create_ival (parser, SPACE, SPACE);
447 $$->location = yylloc;
448 }
449 ;
450
451 operator:
452 '[' { $$ = '['; }
453 | ']' { $$ = ']'; }
454 | '(' { $$ = '('; }
455 | ')' { $$ = ')'; }
456 | '{' { $$ = '{'; }
457 | '}' { $$ = '}'; }
458 | '.' { $$ = '.'; }
459 | '&' { $$ = '&'; }
460 | '*' { $$ = '*'; }
461 | '+' { $$ = '+'; }
462 | '-' { $$ = '-'; }
463 | '~' { $$ = '~'; }
464 | '!' { $$ = '!'; }
465 | '/' { $$ = '/'; }
466 | '%' { $$ = '%'; }
467 | LEFT_SHIFT { $$ = LEFT_SHIFT; }
468 | RIGHT_SHIFT { $$ = RIGHT_SHIFT; }
469 | '<' { $$ = '<'; }
470 | '>' { $$ = '>'; }
471 | LESS_OR_EQUAL { $$ = LESS_OR_EQUAL; }
472 | GREATER_OR_EQUAL { $$ = GREATER_OR_EQUAL; }
473 | EQUAL { $$ = EQUAL; }
474 | NOT_EQUAL { $$ = NOT_EQUAL; }
475 | '^' { $$ = '^'; }
476 | '|' { $$ = '|'; }
477 | AND { $$ = AND; }
478 | OR { $$ = OR; }
479 | ';' { $$ = ';'; }
480 | ',' { $$ = ','; }
481 | '=' { $$ = '='; }
482 | PASTE { $$ = PASTE; }
483 | DEFINED { $$ = DEFINED; }
484 ;
485
486 %%
487
488 string_list_t *
489 _string_list_create (void *ctx)
490 {
491 string_list_t *list;
492
493 list = xtalloc (ctx, string_list_t);
494 list->head = NULL;
495 list->tail = NULL;
496
497 return list;
498 }
499
500 void
501 _string_list_append_item (string_list_t *list, const char *str)
502 {
503 string_node_t *node;
504
505 node = xtalloc (list, string_node_t);
506 node->str = xtalloc_strdup (node, str);
507
508 node->next = NULL;
509
510 if (list->head == NULL) {
511 list->head = node;
512 } else {
513 list->tail->next = node;
514 }
515
516 list->tail = node;
517 }
518
519 int
520 _string_list_contains (string_list_t *list, const char *member, int *index)
521 {
522 string_node_t *node;
523 int i;
524
525 if (list == NULL)
526 return 0;
527
528 for (i = 0, node = list->head; node; i++, node = node->next) {
529 if (strcmp (node->str, member) == 0) {
530 if (index)
531 *index = i;
532 return 1;
533 }
534 }
535
536 return 0;
537 }
538
539 int
540 _string_list_length (string_list_t *list)
541 {
542 int length = 0;
543 string_node_t *node;
544
545 if (list == NULL)
546 return 0;
547
548 for (node = list->head; node; node = node->next)
549 length++;
550
551 return length;
552 }
553
554 argument_list_t *
555 _argument_list_create (void *ctx)
556 {
557 argument_list_t *list;
558
559 list = xtalloc (ctx, argument_list_t);
560 list->head = NULL;
561 list->tail = NULL;
562
563 return list;
564 }
565
566 void
567 _argument_list_append (argument_list_t *list, token_list_t *argument)
568 {
569 argument_node_t *node;
570
571 node = xtalloc (list, argument_node_t);
572 node->argument = argument;
573
574 node->next = NULL;
575
576 if (list->head == NULL) {
577 list->head = node;
578 } else {
579 list->tail->next = node;
580 }
581
582 list->tail = node;
583 }
584
585 int
586 _argument_list_length (argument_list_t *list)
587 {
588 int length = 0;
589 argument_node_t *node;
590
591 if (list == NULL)
592 return 0;
593
594 for (node = list->head; node; node = node->next)
595 length++;
596
597 return length;
598 }
599
600 token_list_t *
601 _argument_list_member_at (argument_list_t *list, int index)
602 {
603 argument_node_t *node;
604 int i;
605
606 if (list == NULL)
607 return NULL;
608
609 node = list->head;
610 for (i = 0; i < index; i++) {
611 node = node->next;
612 if (node == NULL)
613 break;
614 }
615
616 if (node)
617 return node->argument;
618
619 return NULL;
620 }
621
622 /* Note: This function talloc_steal()s the str pointer. */
623 token_t *
624 _token_create_str (void *ctx, int type, char *str)
625 {
626 token_t *token;
627
628 token = xtalloc (ctx, token_t);
629 token->type = type;
630 token->value.str = talloc_steal (token, str);
631
632 return token;
633 }
634
635 token_t *
636 _token_create_ival (void *ctx, int type, int ival)
637 {
638 token_t *token;
639
640 token = xtalloc (ctx, token_t);
641 token->type = type;
642 token->value.ival = ival;
643
644 return token;
645 }
646
647 token_list_t *
648 _token_list_create (void *ctx)
649 {
650 token_list_t *list;
651
652 list = xtalloc (ctx, token_list_t);
653 list->head = NULL;
654 list->tail = NULL;
655 list->non_space_tail = NULL;
656
657 return list;
658 }
659
660 void
661 _token_list_append (token_list_t *list, token_t *token)
662 {
663 token_node_t *node;
664
665 node = xtalloc (list, token_node_t);
666 node->token = xtalloc_reference (list, token);
667
668 node->next = NULL;
669
670 if (list->head == NULL) {
671 list->head = node;
672 } else {
673 list->tail->next = node;
674 }
675
676 list->tail = node;
677 if (token->type != SPACE)
678 list->non_space_tail = node;
679 }
680
681 void
682 _token_list_append_list (token_list_t *list, token_list_t *tail)
683 {
684 if (tail == NULL || tail->head == NULL)
685 return;
686
687 if (list->head == NULL) {
688 list->head = tail->head;
689 } else {
690 list->tail->next = tail->head;
691 }
692
693 list->tail = tail->tail;
694 list->non_space_tail = tail->non_space_tail;
695 }
696
697 token_list_t *
698 _token_list_copy (void *ctx, token_list_t *other)
699 {
700 token_list_t *copy;
701 token_node_t *node;
702
703 if (other == NULL)
704 return NULL;
705
706 copy = _token_list_create (ctx);
707 for (node = other->head; node; node = node->next)
708 _token_list_append (copy, node->token);
709
710 return copy;
711 }
712
713 void
714 _token_list_trim_trailing_space (token_list_t *list)
715 {
716 token_node_t *tail, *next;
717
718 if (list->non_space_tail) {
719 tail = list->non_space_tail->next;
720 list->non_space_tail->next = NULL;
721 list->tail = list->non_space_tail;
722
723 while (tail) {
724 next = tail->next;
725 talloc_free (tail);
726 tail = next;
727 }
728 }
729 }
730
731 static void
732 _token_print (char **out, token_t *token)
733 {
734 if (token->type < 256) {
735 glcpp_printf (*out, "%c", token->type);
736 return;
737 }
738
739 switch (token->type) {
740 case INTEGER:
741 glcpp_printf (*out, "%" PRIxMAX, token->value.ival);
742 break;
743 case IDENTIFIER:
744 case INTEGER_STRING:
745 case OTHER:
746 glcpp_print (*out, token->value.str);
747 break;
748 case SPACE:
749 glcpp_print (*out, " ");
750 break;
751 case LEFT_SHIFT:
752 glcpp_print (*out, "<<");
753 break;
754 case RIGHT_SHIFT:
755 glcpp_print (*out, ">>");
756 break;
757 case LESS_OR_EQUAL:
758 glcpp_print (*out, "<=");
759 break;
760 case GREATER_OR_EQUAL:
761 glcpp_print (*out, ">=");
762 break;
763 case EQUAL:
764 glcpp_print (*out, "==");
765 break;
766 case NOT_EQUAL:
767 glcpp_print (*out, "!=");
768 break;
769 case AND:
770 glcpp_print (*out, "&&");
771 break;
772 case OR:
773 glcpp_print (*out, "||");
774 break;
775 case PASTE:
776 glcpp_print (*out, "##");
777 break;
778 case COMMA_FINAL:
779 glcpp_print (*out, ",");
780 break;
781 case PLACEHOLDER:
782 /* Nothing to print. */
783 break;
784 default:
785 assert(!"Error: Don't know how to print token.");
786 break;
787 }
788 }
789
790 /* Return a new token (talloc()ed off of 'token') formed by pasting
791 * 'token' and 'other'. Note that this function may return 'token' or
792 * 'other' directly rather than allocating anything new.
793 *
794 * Caution: Only very cursory error-checking is performed to see if
795 * the final result is a valid single token. */
796 static token_t *
797 _token_paste (glcpp_parser_t *parser, token_t *token, token_t *other)
798 {
799 token_t *combined = NULL;
800
801 /* Pasting a placeholder onto anything makes no change. */
802 if (other->type == PLACEHOLDER)
803 return token;
804
805 /* When 'token' is a placeholder, just return 'other'. */
806 if (token->type == PLACEHOLDER)
807 return other;
808
809 /* A very few single-character punctuators can be combined
810 * with another to form a multi-character punctuator. */
811 switch (token->type) {
812 case '<':
813 if (other->type == '<')
814 combined = _token_create_ival (token, LEFT_SHIFT, LEFT_SHIFT);
815 else if (other->type == '=')
816 combined = _token_create_ival (token, LESS_OR_EQUAL, LESS_OR_EQUAL);
817 break;
818 case '>':
819 if (other->type == '>')
820 combined = _token_create_ival (token, RIGHT_SHIFT, RIGHT_SHIFT);
821 else if (other->type == '=')
822 combined = _token_create_ival (token, GREATER_OR_EQUAL, GREATER_OR_EQUAL);
823 break;
824 case '=':
825 if (other->type == '=')
826 combined = _token_create_ival (token, EQUAL, EQUAL);
827 break;
828 case '!':
829 if (other->type == '=')
830 combined = _token_create_ival (token, NOT_EQUAL, NOT_EQUAL);
831 break;
832 case '&':
833 if (other->type == '&')
834 combined = _token_create_ival (token, AND, AND);
835 break;
836 case '|':
837 if (other->type == '|')
838 combined = _token_create_ival (token, OR, OR);
839 break;
840 }
841
842 if (combined != NULL) {
843 /* Inherit the location from the first token */
844 combined->location = token->location;
845 return combined;
846 }
847
848 /* Two string-valued tokens can usually just be mashed
849 * together.
850 *
851 * XXX: This isn't actually legitimate. Several things here
852 * should result in a diagnostic since the result cannot be a
853 * valid, single pre-processing token. For example, pasting
854 * "123" and "abc" is not legal, but we don't catch that
855 * here. */
856 if ((token->type == IDENTIFIER || token->type == OTHER || token->type == INTEGER_STRING) &&
857 (other->type == IDENTIFIER || other->type == OTHER || other->type == INTEGER_STRING))
858 {
859 char *str;
860
861 str = xtalloc_asprintf (token, "%s%s",
862 token->value.str, other->value.str);
863 combined = _token_create_str (token, token->type, str);
864 combined->location = token->location;
865 return combined;
866 }
867
868 glcpp_error (&token->location, parser, "");
869 glcpp_print (parser->info_log, "Pasting \"");
870 _token_print (&parser->info_log, token);
871 glcpp_print (parser->info_log, "\" and \"");
872 _token_print (&parser->info_log, other);
873 glcpp_print (parser->info_log, "\" does not give a valid preprocessing token.\n");
874
875 return token;
876 }
877
878 static void
879 _token_list_print (glcpp_parser_t *parser, token_list_t *list)
880 {
881 token_node_t *node;
882
883 if (list == NULL)
884 return;
885
886 for (node = list->head; node; node = node->next)
887 _token_print (&parser->output, node->token);
888 }
889
890 void
891 yyerror (YYLTYPE *locp, glcpp_parser_t *parser, const char *error)
892 {
893 glcpp_error(locp, parser, "%s", error);
894 }
895
896 glcpp_parser_t *
897 glcpp_parser_create (void)
898 {
899 glcpp_parser_t *parser;
900
901 parser = xtalloc (NULL, glcpp_parser_t);
902
903 glcpp_lex_init_extra (parser, &parser->scanner);
904 parser->defines = hash_table_ctor (32, hash_table_string_hash,
905 hash_table_string_compare);
906 parser->active = NULL;
907 parser->lexing_if = 0;
908 parser->space_tokens = 1;
909 parser->newline_as_space = 0;
910 parser->in_control_line = 0;
911 parser->paren_count = 0;
912
913 parser->skip_stack = NULL;
914
915 parser->lex_from_list = NULL;
916 parser->lex_from_node = NULL;
917
918 parser->output = talloc_strdup(parser, "");
919 parser->info_log = talloc_strdup(parser, "");
920 parser->error = 0;
921
922 return parser;
923 }
924
925 int
926 glcpp_parser_parse (glcpp_parser_t *parser)
927 {
928 return yyparse (parser);
929 }
930
931 void
932 glcpp_parser_destroy (glcpp_parser_t *parser)
933 {
934 if (parser->skip_stack)
935 glcpp_error (&parser->skip_stack->loc, parser, "Unterminated #if\n");
936 glcpp_lex_destroy (parser->scanner);
937 hash_table_dtor (parser->defines);
938 talloc_free (parser);
939 }
940
941 typedef enum function_status
942 {
943 FUNCTION_STATUS_SUCCESS,
944 FUNCTION_NOT_A_FUNCTION,
945 FUNCTION_UNBALANCED_PARENTHESES
946 } function_status_t;
947
948 /* Find a set of function-like macro arguments by looking for a
949 * balanced set of parentheses.
950 *
951 * When called, 'node' should be the opening-parenthesis token, (or
952 * perhaps preceeding SPACE tokens). Upon successful return *last will
953 * be the last consumed node, (corresponding to the closing right
954 * parenthesis).
955 *
956 * Return values:
957 *
958 * FUNCTION_STATUS_SUCCESS:
959 *
960 * Successfully parsed a set of function arguments.
961 *
962 * FUNCTION_NOT_A_FUNCTION:
963 *
964 * Macro name not followed by a '('. This is not an error, but
965 * simply that the macro name should be treated as a non-macro.
966 *
967 * FUNCTION_UNBALANCED_PARENTHESES
968 *
969 * Macro name is not followed by a balanced set of parentheses.
970 */
971 static function_status_t
972 _arguments_parse (argument_list_t *arguments,
973 token_node_t *node,
974 token_node_t **last)
975 {
976 token_list_t *argument;
977 int paren_count;
978
979 node = node->next;
980
981 /* Ignore whitespace before first parenthesis. */
982 while (node && node->token->type == SPACE)
983 node = node->next;
984
985 if (node == NULL || node->token->type != '(')
986 return FUNCTION_NOT_A_FUNCTION;
987
988 node = node->next;
989
990 argument = _token_list_create (arguments);
991 _argument_list_append (arguments, argument);
992
993 for (paren_count = 1; node; node = node->next) {
994 if (node->token->type == '(')
995 {
996 paren_count++;
997 }
998 else if (node->token->type == ')')
999 {
1000 paren_count--;
1001 if (paren_count == 0)
1002 break;
1003 }
1004
1005 if (node->token->type == ',' &&
1006 paren_count == 1)
1007 {
1008 _token_list_trim_trailing_space (argument);
1009 argument = _token_list_create (arguments);
1010 _argument_list_append (arguments, argument);
1011 }
1012 else {
1013 if (argument->head == NULL) {
1014 /* Don't treat initial whitespace as
1015 * part of the arguement. */
1016 if (node->token->type == SPACE)
1017 continue;
1018 }
1019 _token_list_append (argument, node->token);
1020 }
1021 }
1022
1023 if (paren_count)
1024 return FUNCTION_UNBALANCED_PARENTHESES;
1025
1026 *last = node;
1027
1028 return FUNCTION_STATUS_SUCCESS;
1029 }
1030
1031 /* This is a helper function that's essentially part of the
1032 * implementation of _glcpp_parser_expand_node. It shouldn't be called
1033 * except for by that function.
1034 *
1035 * Returns NULL if node is a simple token with no expansion, (that is,
1036 * although 'node' corresponds to an identifier defined as a
1037 * function-like macro, it is not followed with a parenthesized
1038 * argument list).
1039 *
1040 * Compute the complete expansion of node (which is a function-like
1041 * macro) and subsequent nodes which are arguments.
1042 *
1043 * Returns the token list that results from the expansion and sets
1044 * *last to the last node in the list that was consumed by the
1045 * expansion. Specificallty, *last will be set as follows: as the
1046 * token of the closing right parenthesis.
1047 */
1048 static token_list_t *
1049 _glcpp_parser_expand_function (glcpp_parser_t *parser,
1050 token_node_t *node,
1051 token_node_t **last)
1052
1053 {
1054 macro_t *macro;
1055 const char *identifier;
1056 argument_list_t *arguments;
1057 function_status_t status;
1058 token_list_t *substituted;
1059 int parameter_index;
1060
1061 identifier = node->token->value.str;
1062
1063 macro = hash_table_find (parser->defines, identifier);
1064
1065 assert (macro->is_function);
1066
1067 arguments = _argument_list_create (parser);
1068 status = _arguments_parse (arguments, node, last);
1069
1070 switch (status) {
1071 case FUNCTION_STATUS_SUCCESS:
1072 break;
1073 case FUNCTION_NOT_A_FUNCTION:
1074 return NULL;
1075 case FUNCTION_UNBALANCED_PARENTHESES:
1076 glcpp_error (&node->token->location, parser, "Macro %s call has unbalanced parentheses\n", identifier);
1077 return NULL;
1078 }
1079
1080 if (macro->replacements == NULL) {
1081 talloc_free (arguments);
1082 return _token_list_create (parser);
1083 }
1084
1085 if (! ((_argument_list_length (arguments) ==
1086 _string_list_length (macro->parameters)) ||
1087 (_string_list_length (macro->parameters) == 0 &&
1088 _argument_list_length (arguments) == 1 &&
1089 arguments->head->argument->head == NULL)))
1090 {
1091 glcpp_error (&node->token->location, parser,
1092 "Error: macro %s invoked with %d arguments (expected %d)\n",
1093 identifier,
1094 _argument_list_length (arguments),
1095 _string_list_length (macro->parameters));
1096 return NULL;
1097 }
1098
1099 /* Perform argument substitution on the replacement list. */
1100 substituted = _token_list_create (arguments);
1101
1102 for (node = macro->replacements->head; node; node = node->next)
1103 {
1104 if (node->token->type == IDENTIFIER &&
1105 _string_list_contains (macro->parameters,
1106 node->token->value.str,
1107 &parameter_index))
1108 {
1109 token_list_t *argument;
1110 argument = _argument_list_member_at (arguments,
1111 parameter_index);
1112 /* Before substituting, we expand the argument
1113 * tokens, or append a placeholder token for
1114 * an empty argument. */
1115 if (argument->head) {
1116 _glcpp_parser_expand_token_list (parser,
1117 argument);
1118 _token_list_append_list (substituted, argument);
1119 } else {
1120 token_t *new_token;
1121
1122 new_token = _token_create_ival (substituted,
1123 PLACEHOLDER,
1124 PLACEHOLDER);
1125 _token_list_append (substituted, new_token);
1126 }
1127 } else {
1128 _token_list_append (substituted, node->token);
1129 }
1130 }
1131
1132 /* After argument substitution, and before further expansion
1133 * below, implement token pasting. */
1134
1135 _token_list_trim_trailing_space (substituted);
1136
1137 node = substituted->head;
1138 while (node)
1139 {
1140 token_node_t *next_non_space;
1141
1142 /* Look ahead for a PASTE token, skipping space. */
1143 next_non_space = node->next;
1144 while (next_non_space && next_non_space->token->type == SPACE)
1145 next_non_space = next_non_space->next;
1146
1147 if (next_non_space == NULL)
1148 break;
1149
1150 if (next_non_space->token->type != PASTE) {
1151 node = next_non_space;
1152 continue;
1153 }
1154
1155 /* Now find the next non-space token after the PASTE. */
1156 next_non_space = next_non_space->next;
1157 while (next_non_space && next_non_space->token->type == SPACE)
1158 next_non_space = next_non_space->next;
1159
1160 if (next_non_space == NULL) {
1161 yyerror (&node->token->location, parser, "'##' cannot appear at either end of a macro expansion\n");
1162 return NULL;
1163 }
1164
1165 node->token = _token_paste (parser, node->token, next_non_space->token);
1166 node->next = next_non_space->next;
1167 if (next_non_space == substituted->tail)
1168 substituted->tail = node;
1169
1170 node = node->next;
1171 }
1172
1173 substituted->non_space_tail = substituted->tail;
1174
1175 return substituted;
1176 }
1177
1178 /* Compute the complete expansion of node, (and subsequent nodes after
1179 * 'node' in the case that 'node' is a function-like macro and
1180 * subsequent nodes are arguments).
1181 *
1182 * Returns NULL if node is a simple token with no expansion.
1183 *
1184 * Otherwise, returns the token list that results from the expansion
1185 * and sets *last to the last node in the list that was consumed by
1186 * the expansion. Specificallty, *last will be set as follows:
1187 *
1188 * As 'node' in the case of object-like macro expansion.
1189 *
1190 * As the token of the closing right parenthesis in the case of
1191 * function-like macro expansion.
1192 */
1193 static token_list_t *
1194 _glcpp_parser_expand_node (glcpp_parser_t *parser,
1195 token_node_t *node,
1196 token_node_t **last)
1197 {
1198 token_t *token = node->token;
1199 const char *identifier;
1200 macro_t *macro;
1201
1202 /* We only expand identifiers */
1203 if (token->type != IDENTIFIER) {
1204 /* We change any COMMA into a COMMA_FINAL to prevent
1205 * it being mistaken for an argument separator
1206 * later. */
1207 if (token->type == ',') {
1208 token->type = COMMA_FINAL;
1209 token->value.ival = COMMA_FINAL;
1210 }
1211
1212 return NULL;
1213 }
1214
1215 /* Look up this identifier in the hash table. */
1216 identifier = token->value.str;
1217 macro = hash_table_find (parser->defines, identifier);
1218
1219 /* Not a macro, so no expansion needed. */
1220 if (macro == NULL)
1221 return NULL;
1222
1223 /* Finally, don't expand this macro if we're already actively
1224 * expanding it, (to avoid infinite recursion). */
1225 if (_active_list_contains (parser->active, identifier)) {
1226 /* We change the token type here from IDENTIFIER to
1227 * OTHER to prevent any future expansion of this
1228 * unexpanded token. */
1229 char *str;
1230 token_list_t *expansion;
1231 token_t *final;
1232
1233 str = xtalloc_strdup (parser, token->value.str);
1234 final = _token_create_str (parser, OTHER, str);
1235 expansion = _token_list_create (parser);
1236 _token_list_append (expansion, final);
1237 *last = node;
1238 return expansion;
1239 }
1240
1241 if (! macro->is_function)
1242 {
1243 *last = node;
1244
1245 if (macro->replacements == NULL)
1246 return _token_list_create (parser);
1247
1248 return _token_list_copy (parser, macro->replacements);
1249 }
1250
1251 return _glcpp_parser_expand_function (parser, node, last);
1252 }
1253
1254 /* Push a new identifier onto the active list, returning the new list.
1255 *
1256 * Here, 'marker' is the token node that appears in the list after the
1257 * expansion of 'identifier'. That is, when the list iterator begins
1258 * examinging 'marker', then it is time to pop this node from the
1259 * active stack.
1260 */
1261 active_list_t *
1262 _active_list_push (active_list_t *list,
1263 const char *identifier,
1264 token_node_t *marker)
1265 {
1266 active_list_t *node;
1267
1268 node = xtalloc (list, active_list_t);
1269 node->identifier = xtalloc_strdup (node, identifier);
1270 node->marker = marker;
1271 node->next = list;
1272
1273 return node;
1274 }
1275
1276 active_list_t *
1277 _active_list_pop (active_list_t *list)
1278 {
1279 active_list_t *node = list;
1280
1281 if (node == NULL)
1282 return NULL;
1283
1284 node = list->next;
1285 talloc_free (list);
1286
1287 return node;
1288 }
1289
1290 int
1291 _active_list_contains (active_list_t *list, const char *identifier)
1292 {
1293 active_list_t *node;
1294
1295 if (list == NULL)
1296 return 0;
1297
1298 for (node = list; node; node = node->next)
1299 if (strcmp (node->identifier, identifier) == 0)
1300 return 1;
1301
1302 return 0;
1303 }
1304
1305 /* Walk over the token list replacing nodes with their expansion.
1306 * Whenever nodes are expanded the walking will walk over the new
1307 * nodes, continuing to expand as necessary. The results are placed in
1308 * 'list' itself;
1309 */
1310 static void
1311 _glcpp_parser_expand_token_list (glcpp_parser_t *parser,
1312 token_list_t *list)
1313 {
1314 token_node_t *node_prev;
1315 token_node_t *node, *last = NULL;
1316 token_list_t *expansion;
1317
1318 if (list == NULL)
1319 return;
1320
1321 _token_list_trim_trailing_space (list);
1322
1323 node_prev = NULL;
1324 node = list->head;
1325
1326 while (node) {
1327
1328 while (parser->active && parser->active->marker == node)
1329 parser->active = _active_list_pop (parser->active);
1330
1331 /* Find the expansion for node, which will replace all
1332 * nodes from node to last, inclusive. */
1333 expansion = _glcpp_parser_expand_node (parser, node, &last);
1334 if (expansion) {
1335 token_node_t *n;
1336
1337 for (n = node; n != last->next; n = n->next)
1338 while (parser->active &&
1339 parser->active->marker == n)
1340 {
1341 parser->active = _active_list_pop (parser->active);
1342 }
1343
1344 parser->active = _active_list_push (parser->active,
1345 node->token->value.str,
1346 last->next);
1347
1348 /* Splice expansion into list, supporting a
1349 * simple deletion if the expansion is
1350 * empty. */
1351 if (expansion->head) {
1352 if (node_prev)
1353 node_prev->next = expansion->head;
1354 else
1355 list->head = expansion->head;
1356 expansion->tail->next = last->next;
1357 if (last == list->tail)
1358 list->tail = expansion->tail;
1359 } else {
1360 if (node_prev)
1361 node_prev->next = last->next;
1362 else
1363 list->head = last->next;
1364 if (last == list->tail)
1365 list->tail = NULL;
1366 }
1367 } else {
1368 node_prev = node;
1369 }
1370 node = node_prev ? node_prev->next : list->head;
1371 }
1372
1373 while (parser->active)
1374 parser->active = _active_list_pop (parser->active);
1375
1376 list->non_space_tail = list->tail;
1377 }
1378
1379 void
1380 _glcpp_parser_print_expanded_token_list (glcpp_parser_t *parser,
1381 token_list_t *list)
1382 {
1383 if (list == NULL)
1384 return;
1385
1386 _glcpp_parser_expand_token_list (parser, list);
1387
1388 _token_list_trim_trailing_space (list);
1389
1390 _token_list_print (parser, list);
1391 }
1392
1393 void
1394 _check_for_reserved_macro_name (glcpp_parser_t *parser, YYLTYPE *loc,
1395 const char *identifier)
1396 {
1397 /* According to the GLSL specification, macro names starting with "__"
1398 * or "GL_" are reserved for future use. So, don't allow them.
1399 */
1400 if (strncmp(identifier, "__", 2) == 0) {
1401 glcpp_error (loc, parser, "Macro names starting with \"__\" are reserved.\n");
1402 }
1403 if (strncmp(identifier, "GL_", 3) == 0) {
1404 glcpp_error (loc, parser, "Macro names starting with \"GL_\" are reserved.\n");
1405 }
1406 }
1407
1408 void
1409 _define_object_macro (glcpp_parser_t *parser,
1410 YYLTYPE *loc,
1411 const char *identifier,
1412 token_list_t *replacements)
1413 {
1414 macro_t *macro;
1415
1416 _check_for_reserved_macro_name(parser, loc, identifier);
1417
1418 macro = xtalloc (parser, macro_t);
1419
1420 macro->is_function = 0;
1421 macro->parameters = NULL;
1422 macro->identifier = talloc_strdup (macro, identifier);
1423 macro->replacements = talloc_steal (macro, replacements);
1424
1425 hash_table_insert (parser->defines, macro, identifier);
1426 }
1427
1428 void
1429 _define_function_macro (glcpp_parser_t *parser,
1430 YYLTYPE *loc,
1431 const char *identifier,
1432 string_list_t *parameters,
1433 token_list_t *replacements)
1434 {
1435 macro_t *macro;
1436
1437 _check_for_reserved_macro_name(parser, loc, identifier);
1438
1439 macro = xtalloc (parser, macro_t);
1440
1441 macro->is_function = 1;
1442 macro->parameters = talloc_steal (macro, parameters);
1443 macro->identifier = talloc_strdup (macro, identifier);
1444 macro->replacements = talloc_steal (macro, replacements);
1445
1446 hash_table_insert (parser->defines, macro, identifier);
1447 }
1448
1449 static int
1450 glcpp_parser_lex (YYSTYPE *yylval, YYLTYPE *yylloc, glcpp_parser_t *parser)
1451 {
1452 token_node_t *node;
1453 int ret;
1454
1455 if (parser->lex_from_list == NULL) {
1456 ret = glcpp_lex (yylval, yylloc, parser->scanner);
1457
1458 /* XXX: This ugly block of code exists for the sole
1459 * purpose of converting a NEWLINE token into a SPACE
1460 * token, but only in the case where we have seen a
1461 * function-like macro name, but have not yet seen its
1462 * closing parenthesis.
1463 *
1464 * There's perhaps a more compact way to do this with
1465 * mid-rule actions in the grammar.
1466 *
1467 * I'm definitely not pleased with the complexity of
1468 * this code here.
1469 */
1470 if (parser->newline_as_space)
1471 {
1472 if (ret == '(') {
1473 parser->paren_count++;
1474 } else if (ret == ')') {
1475 parser->paren_count--;
1476 if (parser->paren_count == 0)
1477 parser->newline_as_space = 0;
1478 } else if (ret == NEWLINE) {
1479 ret = SPACE;
1480 } else if (ret != SPACE) {
1481 if (parser->paren_count == 0)
1482 parser->newline_as_space = 0;
1483 }
1484 }
1485 else if (parser->in_control_line)
1486 {
1487 if (ret == NEWLINE)
1488 parser->in_control_line = 0;
1489 }
1490 else if (ret == HASH_DEFINE_OBJ || ret == HASH_DEFINE_FUNC ||
1491 ret == HASH_UNDEF || ret == HASH_IF ||
1492 ret == HASH_IFDEF || ret == HASH_IFNDEF ||
1493 ret == HASH_ELIF || ret == HASH_ELSE ||
1494 ret == HASH_ENDIF || ret == HASH)
1495 {
1496 parser->in_control_line = 1;
1497 }
1498 else if (ret == IDENTIFIER)
1499 {
1500 macro_t *macro;
1501 macro = hash_table_find (parser->defines,
1502 yylval->str);
1503 if (macro && macro->is_function) {
1504 parser->newline_as_space = 1;
1505 parser->paren_count = 0;
1506 }
1507 }
1508
1509 return ret;
1510 }
1511
1512 node = parser->lex_from_node;
1513
1514 if (node == NULL) {
1515 talloc_free (parser->lex_from_list);
1516 parser->lex_from_list = NULL;
1517 return NEWLINE;
1518 }
1519
1520 *yylval = node->token->value;
1521 ret = node->token->type;
1522
1523 parser->lex_from_node = node->next;
1524
1525 return ret;
1526 }
1527
1528 static void
1529 glcpp_parser_lex_from (glcpp_parser_t *parser, token_list_t *list)
1530 {
1531 token_node_t *node;
1532
1533 assert (parser->lex_from_list == NULL);
1534
1535 /* Copy list, eliminating any space tokens. */
1536 parser->lex_from_list = _token_list_create (parser);
1537
1538 for (node = list->head; node; node = node->next) {
1539 if (node->token->type == SPACE)
1540 continue;
1541 _token_list_append (parser->lex_from_list, node->token);
1542 }
1543
1544 talloc_free (list);
1545
1546 parser->lex_from_node = parser->lex_from_list->head;
1547
1548 /* It's possible the list consisted of nothing but whitespace. */
1549 if (parser->lex_from_node == NULL) {
1550 talloc_free (parser->lex_from_list);
1551 parser->lex_from_list = NULL;
1552 }
1553 }
1554
1555 static void
1556 _glcpp_parser_skip_stack_push_if (glcpp_parser_t *parser, YYLTYPE *loc,
1557 int condition)
1558 {
1559 skip_type_t current = SKIP_NO_SKIP;
1560 skip_node_t *node;
1561
1562 if (parser->skip_stack)
1563 current = parser->skip_stack->type;
1564
1565 node = xtalloc (parser, skip_node_t);
1566 node->loc = *loc;
1567
1568 if (current == SKIP_NO_SKIP) {
1569 if (condition)
1570 node->type = SKIP_NO_SKIP;
1571 else
1572 node->type = SKIP_TO_ELSE;
1573 } else {
1574 node->type = SKIP_TO_ENDIF;
1575 }
1576
1577 node->next = parser->skip_stack;
1578 parser->skip_stack = node;
1579 }
1580
1581 static void
1582 _glcpp_parser_skip_stack_change_if (glcpp_parser_t *parser, YYLTYPE *loc,
1583 const char *type, int condition)
1584 {
1585 if (parser->skip_stack == NULL) {
1586 glcpp_error (loc, parser, "%s without #if\n", type);
1587 return;
1588 }
1589
1590 if (parser->skip_stack->type == SKIP_TO_ELSE) {
1591 if (condition)
1592 parser->skip_stack->type = SKIP_NO_SKIP;
1593 } else {
1594 parser->skip_stack->type = SKIP_TO_ENDIF;
1595 }
1596 }
1597
1598 static void
1599 _glcpp_parser_skip_stack_pop (glcpp_parser_t *parser, YYLTYPE *loc)
1600 {
1601 skip_node_t *node;
1602
1603 if (parser->skip_stack == NULL) {
1604 glcpp_error (loc, parser, "#endif without #if\n");
1605 return;
1606 }
1607
1608 node = parser->skip_stack;
1609 parser->skip_stack = node->next;
1610 talloc_free (node);
1611 }