stl_bvector.h (swap(_Bit_reference,_Bit_reference)): Move/rename...
[official-gcc.git] / gcc / treelang / parse.y
blobf0c0cefa7ad5877b99d5b413f7f2aeabdbd9317d
1 %{ /* -*- c -*- emacs mode c */
2 /*
4 TREELANG Compiler parser.
6 ---------------------------------------------------------------------
8 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
10 This program is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option) any
13 later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
25 In other words, you are welcome to use, share and improve this program.
26 You are forbidden to forbid anyone else to use, share and improve
27 what you give them. Help stamp out software-hoarding!
29 ---------------------------------------------------------------------
31 Written by Tim Josling 1999-2001, based in part on other parts of
32 the GCC compiler.
36 /*
38 Grammar Conflicts
39 *****************
41 There are no conflicts in this grammar. Please keep it that way.
45 #undef IN_GCC
47 typedef void *tree;
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
53 #include "ansidecl.h"
54 #include "config.h"
55 #include "system.h"
56 #include "diagnostic.h"
58 #include "treelang.h"
59 #include "treetree.h"
61 #define YYDEBUG 1
62 #define YYPRINT(file, type, value) print_token (file, type, value)
63 #define YYERROR_VERBOSE YES
66 extern int option_parser_trace;
68 /* Local prototypes. */
70 static void yyerror (const char *error_message);
71 int yylex (void);
72 int yyparse (void);
73 void print_token (FILE * file, unsigned int type ATTRIBUTE_UNUSED, YYSTYPE value);
74 static struct production *reverse_prod_list (struct production *old_first);
75 static void ensure_not_void (unsigned int type, struct token* name);
76 static int check_type_match (int type_num, struct production *exp);
77 static int get_common_type (struct production *type1, struct production *type2);
78 static struct production *make_integer_constant (struct token* value);
79 static void set_storage (struct production *prod);
81 /* File global variables. */
83 static struct production *current_function=NULL;
87 /* Not %raw - seems to have bugs. */
88 %token_table
90 /* Punctuation. */
91 %token RIGHT_BRACE
92 %token LEFT_BRACE
93 %token RIGHT_SQUARE_BRACKET
94 %token LEFT_SQUARE_BRACKET
95 %token RIGHT_PARENTHESIS
96 %token LEFT_PARENTHESIS
97 %token SEMICOLON
98 %token ASTERISK
99 %token COMMA
100 %right EQUALS
101 %right ASSIGN
102 %left PLUS
103 %left MINUS
105 /* Literals. */
106 %token INTEGER
108 /* Keywords. */
109 %token IF
110 %token ELSE
111 %token RETURN
112 %token CHAR
113 %token INT
114 %token UNSIGNED
115 %token VOID
116 %token TYPEDEF
117 %token NAME
118 %token STATIC
119 %token AUTOMATIC
120 %token EXTERNAL_DEFINITION
121 %token EXTERNAL_REFERENCE
123 /* Tokens not passed to parser. */
124 %token WHITESPACE
125 %token COMMENT
127 /* Pseudo tokens - productions. */
128 %token PROD_VARIABLE_NAME
129 %token PROD_TYPE_NAME
130 %token PROD_FUNCTION_NAME
131 %token PROD_INTEGER_CONSTANT
132 %token PROD_PLUS_EXPRESSION
133 %token PROD_MINUS_EXPRESSION
134 %token PROD_ASSIGN_EXPRESSION
135 %token PROD_VARIABLE_REFERENCE_EXPRESSION
136 %token PROD_PARAMETER
137 %token PROD_FUNCTION_INVOCATION
138 %expect 0
141 file:
142 /* Nil. */ {
143 /* Nothing to do. */
145 |declarations {
146 /* Nothing to do. */
151 declarations:
152 declaration {
153 /* Nothing to do. */
155 | declarations declaration {
156 /* Nothing to do. */
160 declaration:
161 variable_def {
162 /* Nothing to do. */
164 |function_prototype {
165 /* Nothing to do. */
167 |function {
168 /* Nothing to do. */
172 variable_def:
173 storage typename NAME init_opt SEMICOLON {
174 struct token* tok;
175 struct production *prod;
176 tok = $3;
177 prod = make_production (PROD_VARIABLE_NAME, tok);
178 SYMBOL_TABLE_NAME (prod) = tok;
179 EXPRESSION_TYPE (prod) = $2;
180 VAR_INIT (prod) = $4;
181 NUMERIC_TYPE (prod) = NUMERIC_TYPE (( (struct production*)EXPRESSION_TYPE (prod)));
182 ensure_not_void (NUMERIC_TYPE (prod), tok);
183 if (insert_tree_name (prod))
185 YYERROR;
187 STORAGE_CLASS_TOKEN (prod) = $1;
188 set_storage (prod);
190 if (VAR_INIT (prod))
192 if (! ((struct production*)VAR_INIT (prod))->code)
193 abort ();
194 if (STORAGE_CLASS (prod) == EXTERNAL_REFERENCE_STORAGE)
196 fprintf (stderr, "%s:%i:%i: External reference variables may not have initial value\n", in_fname,
197 tok->lineno, tok->charno);
198 print_token (stderr, 0, tok);
199 errorcount++;
200 YYERROR;
203 prod->code = tree_code_create_variable
204 (STORAGE_CLASS (prod),
205 ((struct token*)SYMBOL_TABLE_NAME (prod))->chars,
206 ((struct token*)SYMBOL_TABLE_NAME (prod))->length,
207 NUMERIC_TYPE (prod),
208 VAR_INIT (prod)? ((struct production*)VAR_INIT (prod))->code:NULL,
209 in_fname,
210 tok->lineno);
211 if (!prod->code)
212 abort ();
216 storage:
217 STATIC
218 |AUTOMATIC
219 |EXTERNAL_DEFINITION
220 |EXTERNAL_REFERENCE
223 parameter:
224 typename NAME {
225 struct token* tok;
226 struct production *prod;
227 struct production *prod2;
228 tok = $2;
229 prod = make_production (PROD_VARIABLE_NAME, tok);
230 SYMBOL_TABLE_NAME (prod) = $2;
231 EXPRESSION_TYPE (prod) = $1;
232 NUMERIC_TYPE (prod) = NUMERIC_TYPE (( (struct production*)EXPRESSION_TYPE (prod)));
233 ensure_not_void (NUMERIC_TYPE (prod), tok);
234 if (insert_tree_name (prod))
236 YYERROR;
238 prod2 = make_production (PROD_PARAMETER, tok);
239 VARIABLE (prod2) = prod;
240 $$ = prod2;
244 function_prototype:
245 storage typename NAME LEFT_PARENTHESIS parameters RIGHT_PARENTHESIS SEMICOLON {
246 struct token* tok;
247 struct production *prod;
248 struct production *type;
249 struct tree_parameter_list* first_parms;
250 struct tree_parameter_list* last_parms;
251 struct tree_parameter_list* this_parms;
252 struct production *this_parm;
253 struct production *this_parm_var;
254 tok = $3;
255 prod = make_production (PROD_FUNCTION_NAME, $3);
256 SYMBOL_TABLE_NAME (prod) = $3;
257 EXPRESSION_TYPE (prod) = $2;
258 NUMERIC_TYPE (prod) = NUMERIC_TYPE (( (struct production*)EXPRESSION_TYPE (prod)));
259 PARAMETERS (prod) = reverse_prod_list ($5);
260 insert_tree_name (prod);
261 STORAGE_CLASS_TOKEN (prod) = $1;
262 set_storage (prod);
263 switch (STORAGE_CLASS (prod))
265 case STATIC_STORAGE:
266 case EXTERNAL_DEFINITION_STORAGE:
267 break;
269 case AUTOMATIC_STORAGE:
270 fprintf (stderr, "%s:%i:%i: A function cannot be automatic\n", in_fname,
271 tok->lineno, tok->charno);
272 print_token (stderr, 0, tok);
273 errorcount++;
274 YYERROR;
275 break;
277 default:
278 abort ();
280 type = EXPRESSION_TYPE (prod);
281 /* Create a parameter list in a non-front end specific format. */
282 for (first_parms = NULL, last_parms = NULL, this_parm = PARAMETERS (prod);
283 this_parm;
284 this_parm = this_parm->next)
286 if (this_parm->category != production_category)
287 abort ();
288 this_parm_var = VARIABLE (this_parm);
289 if (!this_parm_var)
290 abort ();
291 if (this_parm_var->category != production_category)
292 abort ();
293 this_parms = my_malloc (sizeof (struct tree_parameter_list));
294 if (!this_parm_var->main_token)
295 abort ();
296 this_parms->variable_name = this_parm_var->main_token->chars;
297 this_parms->type = NUMERIC_TYPE (( (struct production*)EXPRESSION_TYPE (this_parm_var)));
298 if (last_parms)
300 last_parms->next = this_parms;
301 last_parms = this_parms;
303 else
305 first_parms = this_parms;
306 last_parms = this_parms;
308 this_parms->where_to_put_var_tree = & (( (struct production*)VARIABLE (this_parm))->code);
310 FIRST_PARMS (prod) = first_parms;
312 prod->code = tree_code_create_function_prototype
313 (tok->chars, STORAGE_CLASS (prod), NUMERIC_TYPE (type),
314 first_parms, in_fname, tok->lineno);
319 function:
320 NAME LEFT_BRACE {
321 struct production *proto;
322 struct production search_prod;
323 struct token* tok;
324 struct production *this_parm;
325 tok = $1;
326 SYMBOL_TABLE_NAME ((&search_prod)) = tok;
327 current_function = proto = lookup_tree_name (&search_prod);
328 if (!proto)
330 fprintf (stderr, "%s:%i:%i: Function prototype not found\n", in_fname,
331 tok->lineno, tok->charno);
332 print_token (stderr, 0, tok);
333 errorcount++;
334 YYERROR;
336 if (!proto->code)
337 abort ();
338 tree_code_create_function_initial
339 (proto->code, in_fname, tok->lineno,
340 FIRST_PARMS (current_function));
342 /* Check all the parameters have code. */
343 for (this_parm = PARAMETERS (proto);
344 this_parm;
345 this_parm = this_parm->next)
347 if (! (struct production*)VARIABLE (this_parm))
348 abort ();
349 if (! (( (struct production*)VARIABLE (this_parm))->code))
350 abort ();
353 variable_defs_opt statements_opt RIGHT_BRACE {
354 struct token* tok;
355 tok = $1;
356 tree_code_create_function_wrapup (in_fname, tok->lineno);
357 current_function = NULL;
361 variable_defs_opt:
362 /* Nil. */ {
363 $$ = 0;
365 |variable_defs {
366 $$ = $1;
370 statements_opt:
371 /* Nil. */ {
372 $$ = 0;
374 |statements {
375 $$ = $1;
379 variable_defs:
380 variable_def {
381 /* Nothing to do. */
383 |variable_defs variable_def {
384 /* Nothing to do. */
388 typename:
389 INT {
390 struct token* tok;
391 struct production *prod;
392 tok = $1;
393 prod = make_production (PROD_TYPE_NAME, tok);
394 NUMERIC_TYPE (prod) = SIGNED_INT;
395 prod->code = tree_code_get_type (NUMERIC_TYPE (prod));
396 $$ = prod;
398 |UNSIGNED INT {
399 struct token* tok;
400 struct production *prod;
401 tok = $1;
402 prod = make_production (PROD_TYPE_NAME, tok);
403 NUMERIC_TYPE (prod) = UNSIGNED_INT;
404 prod->code = tree_code_get_type (NUMERIC_TYPE (prod));
405 $$ = prod;
407 |CHAR {
408 struct token* tok;
409 struct production *prod;
410 tok = $1;
411 prod = make_production (PROD_TYPE_NAME, tok);
412 NUMERIC_TYPE (prod) = SIGNED_CHAR;
413 prod->code = tree_code_get_type (NUMERIC_TYPE (prod));
414 $$ = prod;
416 |UNSIGNED CHAR {
417 struct token* tok;
418 struct production *prod;
419 tok = $1;
420 prod = make_production (PROD_TYPE_NAME, tok);
421 NUMERIC_TYPE (prod) = UNSIGNED_CHAR;
422 prod->code = tree_code_get_type (NUMERIC_TYPE (prod));
423 $$ = prod;
425 |VOID {
426 struct token* tok;
427 struct production *prod;
428 tok = $1;
429 prod = make_production (PROD_TYPE_NAME, tok);
430 NUMERIC_TYPE (prod) = VOID_TYPE;
431 prod->code = tree_code_get_type (NUMERIC_TYPE (prod));
432 $$ = prod;
436 parameters:
437 parameter {
438 /* Nothing to do. */
439 $$ = $1;
441 |parameters COMMA parameter {
442 struct production *prod1;
443 prod1 = $3;
444 prod1->next = $1; /* Insert in reverse order. */
445 $$ = prod1;
449 statements:
450 statement {
451 /* Nothing to do. */
453 |statements statement {
454 /* Nothing to do. */
458 statement:
459 expression SEMICOLON {
460 struct production *exp;
461 exp = $1;
462 tree_code_output_expression_statement (exp->code, in_fname, exp->main_token->lineno);
464 |return SEMICOLON {
465 /* Nothing to do. */
467 |if_statement {
468 /* Nothing to do. */
472 if_statement:
473 IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS {
474 struct token* tok;
475 struct production *exp;
476 tok = $1;
477 exp = $3;
478 ensure_not_void (NUMERIC_TYPE (exp), exp->main_token);
479 tree_code_if_start (exp->code, in_fname, tok->lineno);
481 LEFT_BRACE statements_opt RIGHT_BRACE {
482 /* Just let the statements flow. */
484 ELSE {
485 struct token* tok;
486 tok = $1;
487 tree_code_if_else (in_fname, tok->lineno);
489 LEFT_BRACE statements_opt RIGHT_BRACE {
490 struct token* tok;
491 tok = $12;
492 tree_code_if_end (in_fname, tok->lineno);
497 return:
498 RETURN expression_opt {
499 struct production *type_prod;
500 struct token* ret_tok;
501 ret_tok = $1;
502 type_prod = EXPRESSION_TYPE (current_function);
503 if (NUMERIC_TYPE (type_prod) == VOID)
504 if ($2 == NULL)
505 tree_code_generate_return (type_prod->code, NULL);
506 else
508 fprintf (stderr, "%s:%i:%i: Redundant expression in return\n", in_fname,
509 ret_tok->lineno, ret_tok->charno);
510 print_token (stderr, 0, ret_tok);
511 errorcount++;
512 tree_code_generate_return (type_prod->code, NULL);
514 else
515 if ($2 == NULL)
517 fprintf (stderr, "%s:%i:%i: Expression missing in return\n", in_fname,
518 ret_tok->lineno, ret_tok->charno);
519 print_token (stderr, 0, ret_tok);
520 errorcount++;
522 else
524 struct production *exp;
525 exp = $2;
526 /* Check same type. */
527 if (check_type_match (NUMERIC_TYPE (type_prod), $2))
529 if (!type_prod->code)
530 abort ();
531 if (!exp->code)
532 abort ();
533 /* Generate the code. */
534 tree_code_generate_return (type_prod->code, exp->code);
540 expression_opt:
541 /* Nil. */ {
542 $$ = 0;
544 |expression {
545 struct production *exp;
546 exp = $1;
547 if (!exp->code)
548 abort ();
550 $$ = $1;
554 expression:
555 INTEGER {
556 $$ = make_integer_constant ($1);
558 |variable_ref {
559 $$ = $1;
561 |expression PLUS expression {
562 struct token* tok;
563 struct production *prod;
564 struct production *op1;
565 struct production *op2;
566 tree type;
568 op1 = $1;
569 op2 = $3;
570 tok = $2;
571 ensure_not_void (NUMERIC_TYPE (op1), op1->main_token);
572 ensure_not_void (NUMERIC_TYPE (op2), op2->main_token);
573 prod = make_production (PROD_PLUS_EXPRESSION, tok);
574 NUMERIC_TYPE (prod) = get_common_type (op1, op2);
575 if (!NUMERIC_TYPE (prod))
576 YYERROR;
577 else
579 type = get_type_for_numeric_type (NUMERIC_TYPE (prod));
580 if (!type)
581 abort ();
582 OP1 (prod) = $1;
583 OP2 (prod) = $3;
585 prod->code = tree_code_get_expression
586 (EXP_PLUS, type, op1->code, op2->code, NULL);
588 $$ = prod;
590 |expression MINUS expression %prec PLUS {
591 struct token* tok;
592 struct production *prod;
593 struct production *op1;
594 struct production *op2;
595 tree type;
597 op1 = $1;
598 op2 = $3;
599 ensure_not_void (NUMERIC_TYPE (op1), op1->main_token);
600 ensure_not_void (NUMERIC_TYPE (op2), op2->main_token);
601 tok = $2;
602 prod = make_production (PROD_PLUS_EXPRESSION, tok);
603 NUMERIC_TYPE (prod) = get_common_type (op1, op2);
604 if (!NUMERIC_TYPE (prod))
605 YYERROR;
606 else
608 type = get_type_for_numeric_type (NUMERIC_TYPE (prod));
609 if (!type)
610 abort ();
611 OP1 (prod) = $1;
612 OP2 (prod) = $3;
614 prod->code = tree_code_get_expression (EXP_MINUS,
615 type, op1->code, op2->code, NULL);
617 $$ = prod;
619 |expression EQUALS expression {
620 struct token* tok;
621 struct production *prod;
622 struct production *op1;
623 struct production *op2;
624 tree type;
626 op1 = $1;
627 op2 = $3;
628 ensure_not_void (NUMERIC_TYPE (op1), op1->main_token);
629 ensure_not_void (NUMERIC_TYPE (op2), op2->main_token);
630 tok = $2;
631 prod = make_production (PROD_PLUS_EXPRESSION, tok);
632 NUMERIC_TYPE (prod) = SIGNED_INT;
633 if (!NUMERIC_TYPE (prod))
634 YYERROR;
635 else
637 type = get_type_for_numeric_type (NUMERIC_TYPE (prod));
638 if (!type)
639 abort ();
640 OP1 (prod) = $1;
641 OP2 (prod) = $3;
643 prod->code = tree_code_get_expression (EXP_EQUALS,
644 type, op1->code, op2->code, NULL);
646 $$ = prod;
648 |variable_ref ASSIGN expression {
649 struct token* tok;
650 struct production *prod;
651 struct production *op1;
652 struct production *op2;
653 tree type;
655 op1 = $1;
656 op2 = $3;
657 tok = $2;
658 ensure_not_void (NUMERIC_TYPE (op2), op2->main_token);
659 prod = make_production (PROD_ASSIGN_EXPRESSION, tok);
660 NUMERIC_TYPE (prod) = NUMERIC_TYPE (op1);
661 if (!NUMERIC_TYPE (prod))
662 YYERROR;
663 else
665 type = get_type_for_numeric_type (NUMERIC_TYPE (prod));
666 if (!type)
667 abort ();
668 OP1 (prod) = $1;
669 OP2 (prod) = $3;
670 prod->code = tree_code_get_expression (EXP_ASSIGN,
671 type, op1->code, op2->code, NULL);
673 $$ = prod;
675 |function_invocation {
676 $$ = $1;
680 function_invocation:
681 NAME LEFT_PARENTHESIS expressions_with_commas RIGHT_PARENTHESIS {
682 struct production *prod;
683 struct token* tok;
684 struct production search_prod;
685 struct production *proto;
686 struct production *exp;
687 struct production *exp_proto;
688 struct production *var;
689 int exp_proto_count;
690 int exp_count;
691 tree parms;
692 tree type;
694 tok = $1;
695 prod = make_production (PROD_FUNCTION_INVOCATION, tok);
696 SYMBOL_TABLE_NAME (prod) = tok;
697 PARAMETERS (prod) = reverse_prod_list ($3);
698 SYMBOL_TABLE_NAME ((&search_prod)) = tok;
699 proto = lookup_tree_name (&search_prod);
700 if (!proto)
702 fprintf (stderr, "%s:%i:%i: Function prototype not found\n", in_fname,
703 tok->lineno, tok->charno);
704 print_token (stderr, 0, tok);
705 errorcount++;
706 YYERROR;
708 EXPRESSION_TYPE (prod) = EXPRESSION_TYPE (proto);
709 NUMERIC_TYPE (prod) = NUMERIC_TYPE (proto);
710 /* Count the expressions and ensure they match the prototype. */
711 for (exp_proto_count = 0, exp_proto = PARAMETERS (proto);
712 exp_proto; exp_proto = exp_proto->next)
713 exp_proto_count++;
715 for (exp_count = 0, exp = PARAMETERS (prod); exp; exp = exp->next)
716 exp_count++;
718 if (exp_count != exp_proto_count)
720 fprintf (stderr, "%s:%i:%i: expression count mismatch with prototype\n", in_fname,
721 tok->lineno, tok->charno);
722 print_token (stderr, 0, tok);
723 errorcount++;
724 YYERROR;
726 parms = tree_code_init_parameters ();
727 for (exp_proto = PARAMETERS (proto), exp = PARAMETERS (prod);
728 exp_proto;
729 exp = exp->next, exp_proto = exp_proto->next)
731 if (!exp)
732 abort ();
733 if (!exp_proto)
734 abort ();
735 if (!exp->code)
736 abort ();
737 var = VARIABLE (exp_proto);
738 if (!var)
739 abort ();
740 if (!var->code)
741 abort ();
742 parms = tree_code_add_parameter (parms, var->code, exp->code);
744 type = get_type_for_numeric_type (NUMERIC_TYPE (prod));
745 prod->code = tree_code_get_expression
746 (EXP_FUNCTION_INVOCATION, type, proto->code, parms, NULL);
747 $$ = prod;
751 expressions_with_commas:
752 expression {
753 struct production *exp;
754 exp = $1;
755 ensure_not_void (NUMERIC_TYPE (exp), exp->main_token);
756 $$ = $1;
758 |expressions_with_commas COMMA expression {
759 struct production *exp;
760 exp = $3;
761 ensure_not_void (NUMERIC_TYPE (exp), exp->main_token);
762 exp->next = $1; /* Reverse order. */
763 $$ = exp;
767 variable_ref:
768 NAME {
769 struct production search_prod;
770 struct production *prod;
771 struct production *symbol_table_entry;
772 struct token* tok;
773 tree type;
775 tok = $1;
776 SYMBOL_TABLE_NAME ((&search_prod)) = tok;
777 symbol_table_entry = lookup_tree_name (&search_prod);
778 if (!symbol_table_entry)
780 fprintf (stderr, "%s:%i:%i: Variable referred to but not defined\n", in_fname,
781 tok->lineno, tok->charno);
782 print_token (stderr, 0, tok);
783 errorcount++;
784 YYERROR;
787 prod = make_production (PROD_VARIABLE_REFERENCE_EXPRESSION, tok);
788 NUMERIC_TYPE (prod) = NUMERIC_TYPE (symbol_table_entry);
789 type = get_type_for_numeric_type (NUMERIC_TYPE (prod));
790 if (!NUMERIC_TYPE (prod))
791 YYERROR;
792 OP1 (prod) = $1;
794 prod->code = tree_code_get_expression (EXP_REFERENCE, type,
795 symbol_table_entry->code, NULL, NULL);
796 $$ = prod;
800 init_opt:
801 /* Nil. */ {
802 $$ = 0;
804 |init {
805 /* Nothing to do. */
808 init:
809 ASSIGN init_element {
813 init_element:
814 INTEGER {
815 $$ = make_integer_constant ($1);
821 /* Print a token VALUE to file FILE. Ignore TYPE which is the token
822 type. */
824 void
825 print_token (FILE * file, unsigned int type ATTRIBUTE_UNUSED, YYSTYPE value)
827 struct token *tok;
828 unsigned int ix;
830 tok = value;
831 fprintf (file, "%d \"", tok->lineno);
832 for (ix = 0; ix < tok->length; ix++)
833 fprintf (file, "%c", tok->chars[ix]);
834 fprintf (file, "\"");
837 /* Output a message ERROR_MESSAGE from the parser. */
838 void
839 yyerror (const char *error_message)
841 struct token *tok;
843 tok = yylval;
844 if (tok)
846 fprintf (stderr, "%s:%i:%i: %s\n", in_fname, tok->lineno, tok->charno, error_message);
847 print_token (stderr, 0, tok);
849 else
850 fprintf (stderr, "%s\n", error_message);
852 errorcount++;
856 /* Reverse the order of a token list, linked by parse_next, old first
857 token is OLD_FIRST. */
859 static struct production*
860 reverse_prod_list (struct production *old_first)
862 struct production *current;
863 struct production *next;
864 struct production *prev = NULL;
866 current = old_first;
867 prev = NULL;
869 while (current)
871 if (current->category != production_category)
872 abort ();
873 next = current->next;
874 current->next = prev;
875 prev = current;
876 current = next;
878 return prev;
881 /* Ensure TYPE is not VOID. Use NAME as the token for the error location. */
883 static void
884 ensure_not_void (unsigned int type, struct token* name)
886 if (type == VOID)
888 fprintf (stderr, "%s:%i:%i: Type must not be void in this context\n", in_fname,
889 name->lineno, name->charno);
890 print_token (stderr, 0, name);
891 errorcount++;
895 /* Check TYPE1 and TYPE2 which are integral types. Return the lowest
896 common type (min is signed int). */
898 static int
899 get_common_type (struct production *type1, struct production *type2)
901 if (NUMERIC_TYPE (type1) == UNSIGNED_INT)
902 return UNSIGNED_INT;
903 if (NUMERIC_TYPE (type2) == UNSIGNED_INT)
904 return UNSIGNED_INT;
906 return SIGNED_INT;
909 /* Check type (TYPE_NUM) and expression (EXP) match. Return the 1 if
910 OK else 0. Must be exact match - same name unless it is an
911 integral type. */
913 static int
914 check_type_match (int type_num, struct production *exp)
916 switch (type_num)
918 case SIGNED_INT:
919 case UNSIGNED_INT:
920 case SIGNED_CHAR:
921 case UNSIGNED_CHAR:
922 switch (NUMERIC_TYPE (exp))
924 case SIGNED_INT:
925 case UNSIGNED_INT:
926 case SIGNED_CHAR:
927 case UNSIGNED_CHAR:
928 return 1;
930 case VOID:
931 abort ();
933 default:
934 abort ();
936 break;
938 case VOID:
939 abort ();
941 default:
942 abort ();
947 /* Make a production for an integer constant VALUE. */
949 static struct production *
950 make_integer_constant (struct token* value)
952 struct token* tok;
953 struct production *prod;
954 tok = value;
955 prod = make_production (PROD_INTEGER_CONSTANT, tok);
956 if ((tok->chars[0] == (unsigned char)'-')|| (tok->chars[0] == (unsigned char)'+'))
957 NUMERIC_TYPE (prod) = SIGNED_INT;
958 else
959 NUMERIC_TYPE (prod) = UNSIGNED_INT;
960 prod->code = tree_code_get_integer_value (tok->chars, tok->length);
961 return prod;
964 /* Set STORAGE_CLASS in PROD according to CLASS_TOKEN. */
966 static void
967 set_storage (struct production *prod)
969 struct token* stg_class;
970 stg_class = STORAGE_CLASS_TOKEN (prod);
971 switch (stg_class->type)
973 case STATIC:
974 STORAGE_CLASS (prod) = STATIC_STORAGE;
975 break;
977 case AUTOMATIC:
978 STORAGE_CLASS (prod) = AUTOMATIC_STORAGE;
979 break;
981 case EXTERNAL_DEFINITION:
982 STORAGE_CLASS (prod) = EXTERNAL_DEFINITION_STORAGE;
983 break;
985 case EXTERNAL_REFERENCE:
986 STORAGE_CLASS (prod) = EXTERNAL_REFERENCE_STORAGE;
987 break;
989 default:
990 abort ();
994 /* Set parse trace. */
996 void
997 treelang_debug (void)
999 if (option_parser_trace)
1000 yydebug = 1;