parse.y: Changed pointer declaration from "type* var" to "type *var" Removed trailing...
[official-gcc.git] / gcc / treelang / parse.y
blob740237c8b98d83995b412c94606fc39ae452c8b2
1 /* -*- c -*- emacs mode c */
2 /* TREELANG Compiler parser.
4 ---------------------------------------------------------------------
6 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
7 Free Software Foundation, Inc.
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
12 later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
24 In other words, you are welcome to use, share and improve this program.
25 You are forbidden to forbid anyone else to use, share and improve
26 what you give them. Help stamp out software-hoarding!
28 ---------------------------------------------------------------------
30 Written by Tim Josling 1999-2001, based in part on other parts of
31 the GCC compiler. */
33 /* Grammar Conflicts
34 *****************
35 There are no conflicts in this grammar. Please keep it that way. */
38 #include "config.h"
39 #include "system.h"
40 #include "coretypes.h"
41 #include "tm.h"
42 #include "timevar.h"
43 #include "tree.h"
45 #include "treelang.h"
46 #include "treetree.h"
47 #include "toplev.h"
49 #define YYDEBUG 1
50 #define YYPRINT(file, type, value) print_token (file, type, value)
51 #define YYERROR_VERBOSE YES
53 /* My yylex routine used to intercept calls to flex generated code, to
54 record lex time. */
55 int yylex (void);
56 static inline int my_yylex (void);
58 /* Call lex, but ensure time is charged to TV_LEX. */
59 static inline int
60 my_yylex (void)
62 int res;
63 timevar_push (TV_LEX);
64 res = yylex ();
65 timevar_pop (TV_LEX);
66 return res;
68 #define yylex my_yylex
70 extern int option_parser_trace;
72 /* Local prototypes. */
73 static void yyerror (const char *error_message);
74 int yyparse (void);
75 void print_token (FILE *file, unsigned int type ATTRIBUTE_UNUSED,
76 YYSTYPE value);
77 static struct prod_token_parm_item *reverse_prod_list
78 (struct prod_token_parm_item *old_first);
79 static void ensure_not_void (unsigned int type,
80 struct prod_token_parm_item *name);
81 static int check_type_match (int type_num, struct prod_token_parm_item *exp);
82 static int get_common_type (struct prod_token_parm_item *type1,
83 struct prod_token_parm_item *type2);
84 static struct prod_token_parm_item *make_integer_constant
85 (struct prod_token_parm_item *value);
86 static struct prod_token_parm_item *make_plus_expression
87 (struct prod_token_parm_item *tok, struct prod_token_parm_item *op1,
88 struct prod_token_parm_item *op2, int type_code, int prod_code);
89 static void set_storage (struct prod_token_parm_item *prod);
91 /* File global variables. */
92 static struct prod_token_parm_item *current_function = NULL;
95 /* Not %raw - seems to have bugs. */
96 %token_table
98 /* Punctuation. */
99 %token RIGHT_BRACE
100 %token LEFT_BRACE
101 %token RIGHT_SQUARE_BRACKET
102 %token LEFT_SQUARE_BRACKET
103 %token RIGHT_PARENTHESIS
104 %token LEFT_PARENTHESIS
105 %token SEMICOLON
106 %token ASTERISK
107 %token COMMA
108 %right EQUALS
109 %right ASSIGN
110 %left tl_PLUS
111 %left tl_MINUS
113 /* Literals. */
114 %token INTEGER
116 /* Keywords. */
117 %token IF
118 %token ELSE
119 %token tl_RETURN
120 %token CHAR
121 %token INT
122 %token UNSIGNED
123 %token VOID
124 %token TYPEDEF
125 %token NAME
126 %token STATIC
127 %token AUTOMATIC
128 %token EXTERNAL_DEFINITION
129 %token EXTERNAL_REFERENCE
131 /* Tokens not passed to parser. */
132 %token WHITESPACE
133 %token COMMENT
135 /* Pseudo tokens - productions. */
136 %token PROD_VARIABLE_NAME
137 %token PROD_TYPE_NAME
138 %token PROD_FUNCTION_NAME
139 %token PROD_INTEGER_CONSTANT
140 %token PROD_PLUS_EXPRESSION
141 %token PROD_MINUS_EXPRESSION
142 %token PROD_ASSIGN_EXPRESSION
143 %token PROD_VARIABLE_REFERENCE_EXPRESSION
144 %token PROD_PARAMETER
145 %token PROD_FUNCTION_INVOCATION
146 %expect 0
149 file:
150 /* Nil. */ {
151 /* Nothing to do. */
153 |declarations {
154 /* Nothing to do. */
159 declarations:
160 declaration {
161 /* Nothing to do. */
163 | declarations declaration {
164 /* Nothing to do. */
168 declaration:
169 variable_def {
170 /* Nothing to do. */
172 |function_prototype {
173 /* Nothing to do. */
175 |function {
176 /* Nothing to do. */
180 variable_def:
181 storage typename NAME init_opt SEMICOLON {
182 struct prod_token_parm_item *tok;
183 struct prod_token_parm_item *prod;
184 tok = $3;
185 prod = make_production (PROD_VARIABLE_NAME, tok);
186 SYMBOL_TABLE_NAME (prod) = tok;
187 EXPRESSION_TYPE (prod) = $2;
188 VAR_INIT (prod) = $4;
189 NUMERIC_TYPE (prod) =
190 NUMERIC_TYPE (( (struct prod_token_parm_item *)EXPRESSION_TYPE (prod)));
191 ensure_not_void (NUMERIC_TYPE (prod), tok);
192 if (insert_tree_name (prod))
194 YYERROR;
196 STORAGE_CLASS_TOKEN (prod) = $1;
197 set_storage (prod);
199 if (VAR_INIT (prod))
201 gcc_assert (((struct prod_token_parm_item *)
202 VAR_INIT (prod))->tp.pro.code);
203 if (STORAGE_CLASS (prod) == EXTERNAL_REFERENCE_STORAGE)
205 error("%Hexternal reference variable %q.*s has an initial value",
206 &tok->tp.tok.location, tok->tp.tok.length, tok->tp.tok.chars);
207 YYERROR;
208 VAR_INIT (prod) = NULL;
213 prod->tp.pro.code = tree_code_create_variable
214 (STORAGE_CLASS (prod),
215 ((struct prod_token_parm_item *)SYMBOL_TABLE_NAME (prod))->tp.tok.chars,
216 ((struct prod_token_parm_item *)SYMBOL_TABLE_NAME (prod))->tp.tok.length,
217 NUMERIC_TYPE (prod),
218 VAR_INIT (prod) ?
219 ((struct prod_token_parm_item *)VAR_INIT (prod))->tp.pro.code : NULL,
220 tok->tp.tok.location);
221 gcc_assert (prod->tp.pro.code);
225 storage:
226 STATIC
227 |AUTOMATIC
228 |EXTERNAL_DEFINITION
229 |EXTERNAL_REFERENCE
232 parameter:
233 typename NAME {
234 struct prod_token_parm_item *tok;
235 struct prod_token_parm_item *prod;
236 struct prod_token_parm_item *prod2;
237 tok = $2;
238 prod = make_production (PROD_VARIABLE_NAME, tok);
239 SYMBOL_TABLE_NAME (prod) = $2;
240 EXPRESSION_TYPE (prod) = $1;
241 NUMERIC_TYPE (prod) =
242 NUMERIC_TYPE (( (struct prod_token_parm_item *)EXPRESSION_TYPE (prod)));
243 ensure_not_void (NUMERIC_TYPE (prod), tok);
244 if (insert_tree_name (prod))
246 YYERROR;
248 prod2 = make_production (PROD_PARAMETER, tok);
249 VARIABLE (prod2) = prod;
250 $$ = prod2;
254 function_prototype:
255 storage typename NAME LEFT_PARENTHESIS parameters_opt RIGHT_PARENTHESIS SEMICOLON {
256 struct prod_token_parm_item *tok;
257 struct prod_token_parm_item *prod;
258 struct prod_token_parm_item *type;
259 struct prod_token_parm_item *first_parms;
260 struct prod_token_parm_item *last_parms;
261 struct prod_token_parm_item *this_parms;
262 struct prod_token_parm_item *this_parm;
263 struct prod_token_parm_item *this_parm_var;
264 tok = $3;
265 prod = make_production (PROD_FUNCTION_NAME, $3);
266 SYMBOL_TABLE_NAME (prod) = $3;
267 EXPRESSION_TYPE (prod) = $2;
268 NUMERIC_TYPE (prod) =
269 NUMERIC_TYPE (( (struct prod_token_parm_item *)EXPRESSION_TYPE (prod)));
270 PARAMETERS (prod) = reverse_prod_list ($5);
271 insert_tree_name (prod);
272 STORAGE_CLASS_TOKEN (prod) = $1;
273 set_storage (prod);
274 switch (STORAGE_CLASS (prod))
276 case STATIC_STORAGE:
277 case EXTERNAL_DEFINITION_STORAGE:
278 case EXTERNAL_REFERENCE_STORAGE:
279 break;
281 case AUTOMATIC_STORAGE:
282 error ("%Hfunction %q.*s cannot be automatic",
283 &tok->tp.tok.location, tok->tp.tok.length, tok->tp.tok.chars);
284 YYERROR;
285 break;
287 default:
288 gcc_unreachable ();
290 type = EXPRESSION_TYPE (prod);
291 /* Create a parameter list in a non-front end specific format. */
292 for (first_parms = NULL, last_parms = NULL, this_parm = PARAMETERS (prod);
293 this_parm;
294 this_parm = this_parm->tp.pro.next)
296 gcc_assert (this_parm->category == production_category);
297 this_parm_var = VARIABLE (this_parm);
299 gcc_assert (this_parm_var);
300 gcc_assert (this_parm_var->category == production_category);
301 gcc_assert (this_parm_var->tp.pro.main_token);
303 this_parms = my_malloc (sizeof (struct prod_token_parm_item));
305 this_parms->tp.par.variable_name =
306 this_parm_var->tp.pro.main_token->tp.tok.chars;
307 this_parms->category = parameter_category;
308 this_parms->type = NUMERIC_TYPE
309 (( (struct prod_token_parm_item *)EXPRESSION_TYPE (this_parm_var)));
310 if (last_parms)
312 last_parms->tp.par.next = this_parms;
313 last_parms = this_parms;
315 else
317 first_parms = this_parms;
318 last_parms = this_parms;
320 this_parms->tp.par.where_to_put_var_tree =
321 & (((struct prod_token_parm_item *)VARIABLE (this_parm))->tp.pro.code);
323 FIRST_PARMS (prod) = first_parms;
325 prod->tp.pro.code =
326 tree_code_create_function_prototype (tok->tp.tok.chars,
327 STORAGE_CLASS (prod),
328 NUMERIC_TYPE (type),
329 first_parms, tok->tp.tok.location);
331 #ifdef ENABLE_CHECKING
332 /* Check all the parameters have code. */
333 for (this_parm = PARAMETERS (prod);
334 this_parm;
335 this_parm = this_parm->tp.pro.next)
337 gcc_assert ((struct prod_token_parm_item *)VARIABLE (this_parm));
338 gcc_assert (((struct prod_token_parm_item *)
339 VARIABLE (this_parm))->tp.pro.code);
341 #endif
345 function:
346 NAME LEFT_BRACE {
347 struct prod_token_parm_item *proto;
348 struct prod_token_parm_item search_prod;
349 struct prod_token_parm_item *tok;
350 tok = $1;
351 SYMBOL_TABLE_NAME ((&search_prod)) = tok;
352 search_prod.category = token_category;
353 current_function = proto = lookup_tree_name (&search_prod);
354 if (!proto)
356 error ("%Hno prototype found for %q.*s", &tok->tp.tok.location,
357 tok->tp.tok.length, tok->tp.tok.chars);
358 YYERROR;
361 gcc_assert (proto->tp.pro.code);
363 tree_code_create_function_initial (proto->tp.pro.code, tok->tp.tok.location);
366 variable_defs_opt statements_opt RIGHT_BRACE {
367 struct prod_token_parm_item *tok;
368 tok = $1;
369 tree_code_create_function_wrapup (tok->tp.tok.location);
370 current_function = NULL;
374 variable_defs_opt:
375 /* Nil. */ {
376 $$ = 0;
378 |variable_defs {
379 $$ = $1;
383 statements_opt:
384 /* Nil. */ {
385 $$ = 0;
387 |statements {
388 $$ = $1;
392 variable_defs:
393 variable_def {
394 /* Nothing to do. */
396 |variable_defs variable_def {
397 /* Nothing to do. */
401 typename:
402 INT {
403 struct prod_token_parm_item *tok;
404 struct prod_token_parm_item *prod;
405 tok = $1;
406 prod = make_production (PROD_TYPE_NAME, tok);
407 NUMERIC_TYPE (prod) = SIGNED_INT;
408 prod->tp.pro.code = tree_code_get_type (NUMERIC_TYPE (prod));
409 $$ = prod;
411 |UNSIGNED INT {
412 struct prod_token_parm_item *tok;
413 struct prod_token_parm_item *prod;
414 tok = $1;
415 prod = make_production (PROD_TYPE_NAME, tok);
416 NUMERIC_TYPE (prod) = UNSIGNED_INT;
417 prod->tp.pro.code = tree_code_get_type (NUMERIC_TYPE (prod));
418 $$ = prod;
420 |CHAR {
421 struct prod_token_parm_item *tok;
422 struct prod_token_parm_item *prod;
423 tok = $1;
424 prod = make_production (PROD_TYPE_NAME, tok);
425 NUMERIC_TYPE (prod) = SIGNED_CHAR;
426 prod->tp.pro.code = tree_code_get_type (NUMERIC_TYPE (prod));
427 $$ = prod;
429 |UNSIGNED CHAR {
430 struct prod_token_parm_item *tok;
431 struct prod_token_parm_item *prod;
432 tok = $1;
433 prod = make_production (PROD_TYPE_NAME, tok);
434 NUMERIC_TYPE (prod) = UNSIGNED_CHAR;
435 prod->tp.pro.code = tree_code_get_type (NUMERIC_TYPE (prod));
436 $$ = prod;
438 |VOID {
439 struct prod_token_parm_item *tok;
440 struct prod_token_parm_item *prod;
441 tok = $1;
442 prod = make_production (PROD_TYPE_NAME, tok);
443 NUMERIC_TYPE (prod) = VOID_TYPE;
444 prod->tp.pro.code = tree_code_get_type (NUMERIC_TYPE (prod));
445 $$ = prod;
449 parameters_opt:
450 /* Nothing to do. */ {
451 $$ = 0;
453 | parameters {
454 $$ = $1;
458 parameters:
459 parameter {
460 /* Nothing to do. */
461 $$ = $1;
463 |parameters COMMA parameter {
464 struct prod_token_parm_item *prod1;
465 prod1 = $3;
466 prod1->tp.pro.next = $1; /* Insert in reverse order. */
467 $$ = prod1;
471 statements:
472 statement {
473 /* Nothing to do. */
475 |statements statement {
476 /* Nothing to do. */
480 statement:
481 expression SEMICOLON {
482 struct prod_token_parm_item *exp;
483 exp = $1;
484 tree_code_output_expression_statement (exp->tp.pro.code,
485 exp->tp.pro.main_token->tp.tok.location);
487 |return SEMICOLON {
488 /* Nothing to do. */
490 |if_statement {
491 /* Nothing to do. */
495 if_statement:
496 IF LEFT_PARENTHESIS expression RIGHT_PARENTHESIS {
497 struct prod_token_parm_item *tok;
498 struct prod_token_parm_item *exp;
499 tok = $1;
500 exp = $3;
501 ensure_not_void (NUMERIC_TYPE (exp), exp->tp.pro.main_token);
502 tree_code_if_start (exp->tp.pro.code, tok->tp.tok.location);
504 LEFT_BRACE variable_defs_opt statements_opt RIGHT_BRACE {
505 /* Just let the statements flow. */
507 ELSE {
508 struct prod_token_parm_item *tok;
509 tok = $1;
510 tree_code_if_else (tok->tp.tok.location);
512 LEFT_BRACE variable_defs_opt statements_opt RIGHT_BRACE {
513 struct prod_token_parm_item *tok;
514 tok = $1;
515 tree_code_if_end (tok->tp.tok.location);
520 return:
521 tl_RETURN expression_opt {
522 struct prod_token_parm_item *type_prod;
523 struct prod_token_parm_item *ret_tok = $1;
524 struct prod_token_parm_item *exp = $2;
526 type_prod = EXPRESSION_TYPE (current_function);
527 if (NUMERIC_TYPE (type_prod) == VOID_TYPE)
528 if (exp == NULL)
529 tree_code_generate_return (type_prod->tp.pro.code, NULL);
530 else
532 warning (0, "%Hredundant expression in return",
533 &ret_tok->tp.tok.location);
534 tree_code_generate_return (type_prod->tp.pro.code, NULL);
536 else
537 if (exp == NULL)
538 error ("%Hexpression missing in return", &ret_tok->tp.tok.location);
539 else
541 /* Check same type. */
542 if (check_type_match (NUMERIC_TYPE (type_prod), exp))
544 gcc_assert (type_prod->tp.pro.code);
545 gcc_assert (exp->tp.pro.code);
547 /* Generate the code. */
548 tree_code_generate_return (type_prod->tp.pro.code,
549 exp->tp.pro.code);
555 expression_opt:
556 /* Nil. */ {
557 $$ = 0;
559 |expression {
560 struct prod_token_parm_item *exp;
561 exp = $1;
562 gcc_assert (exp->tp.pro.code);
564 $$ = $1;
568 expression:
569 INTEGER {
570 $$ = make_integer_constant ($1);
572 |variable_ref {
573 $$ = $1;
575 |expression tl_PLUS expression {
576 struct prod_token_parm_item *tok = $2;
577 struct prod_token_parm_item *op1 = $1;
578 struct prod_token_parm_item *op2 = $3;
579 int type_code = get_common_type (op1, op2);
580 if (!type_code)
581 YYERROR;
582 $$ = make_plus_expression (tok, op1, op2, type_code, EXP_PLUS);
584 |expression tl_MINUS expression %prec tl_PLUS {
585 struct prod_token_parm_item *tok = $2;
586 struct prod_token_parm_item *op1 = $1;
587 struct prod_token_parm_item *op2 = $3;
588 int type_code = get_common_type (op1, op2);
589 if (!type_code)
590 YYERROR;
591 $$ = make_plus_expression (tok, op1, op2, type_code, EXP_MINUS);
593 |expression EQUALS expression {
594 struct prod_token_parm_item *tok = $2;
595 struct prod_token_parm_item *op1 = $1;
596 struct prod_token_parm_item *op2 = $3;
597 int type_code = NUMERIC_TYPE (op1);
598 if (!type_code)
599 YYERROR;
600 $$ = make_plus_expression
601 (tok, op1, op2, type_code, EXP_EQUALS);
603 |variable_ref ASSIGN expression {
604 struct prod_token_parm_item *tok = $2;
605 struct prod_token_parm_item *op1 = $1;
606 struct prod_token_parm_item *op2 = $3;
607 int type_code = NUMERIC_TYPE (op1);
608 if (!type_code)
609 YYERROR;
610 $$ = make_plus_expression
611 (tok, op1, op2, type_code, EXP_ASSIGN);
613 |function_invocation {
614 $$ = $1;
618 function_invocation:
619 NAME LEFT_PARENTHESIS expressions_with_commas_opt RIGHT_PARENTHESIS {
620 struct prod_token_parm_item *prod;
621 struct prod_token_parm_item *tok;
622 struct prod_token_parm_item search_prod;
623 struct prod_token_parm_item *proto;
624 struct prod_token_parm_item *exp;
625 struct prod_token_parm_item *exp_proto;
626 struct prod_token_parm_item *var;
627 int exp_proto_count;
628 int exp_count;
629 tree parms;
630 tree type;
632 tok = $1;
633 prod = make_production (PROD_FUNCTION_INVOCATION, tok);
634 SYMBOL_TABLE_NAME (prod) = tok;
635 PARAMETERS (prod) = reverse_prod_list ($3);
636 SYMBOL_TABLE_NAME ((&search_prod)) = tok;
637 search_prod.category = token_category;
638 proto = lookup_tree_name (&search_prod);
639 if (!proto)
641 error ("%Hfunction prototype not found for %q.*s",
642 &tok->tp.tok.location, tok->tp.tok.length, tok->tp.tok.chars);
643 YYERROR;
645 EXPRESSION_TYPE (prod) = EXPRESSION_TYPE (proto);
646 NUMERIC_TYPE (prod) = NUMERIC_TYPE (proto);
647 /* Count the expressions and ensure they match the prototype. */
648 for (exp_proto_count = 0, exp_proto = PARAMETERS (proto);
649 exp_proto; exp_proto = exp_proto->tp.pro.next)
650 exp_proto_count++;
652 for (exp_count = 0, exp = PARAMETERS (prod); exp; exp = exp->tp.pro.next)
653 exp_count++;
655 if (exp_count != exp_proto_count)
657 error ("%Hexpression count mismatch %q.*s with prototype",
658 &tok->tp.tok.location, tok->tp.tok.length, tok->tp.tok.chars);
659 YYERROR;
661 parms = tree_code_init_parameters ();
662 for (exp_proto = PARAMETERS (proto), exp = PARAMETERS (prod);
663 exp_proto;
664 exp = exp->tp.pro.next, exp_proto = exp_proto->tp.pro.next)
666 gcc_assert (exp);
667 gcc_assert (exp_proto);
668 gcc_assert (exp->tp.pro.code);
670 var = VARIABLE (exp_proto);
672 gcc_assert (var);
673 gcc_assert (var->tp.pro.code);
675 parms = tree_code_add_parameter (parms, var->tp.pro.code,
676 exp->tp.pro.code);
678 type = tree_code_get_type (NUMERIC_TYPE (prod));
679 prod->tp.pro.code = tree_code_get_expression (EXP_FUNCTION_INVOCATION, type,
680 proto->tp.pro.code,
681 nreverse (parms),
682 NULL, tok->tp.tok.location);
683 $$ = prod;
687 expressions_with_commas_opt:
688 /* Nil. */ {
689 $$ = 0
691 |expressions_with_commas { $$ = $1 }
694 expressions_with_commas:
695 expression {
696 struct prod_token_parm_item *exp;
697 exp = $1;
698 ensure_not_void (NUMERIC_TYPE (exp), exp->tp.pro.main_token);
699 $$ = $1;
701 |expressions_with_commas COMMA expression {
702 struct prod_token_parm_item *exp;
703 exp = $3;
704 ensure_not_void (NUMERIC_TYPE (exp), exp->tp.pro.main_token);
705 exp->tp.pro.next = $1; /* Reverse order. */
706 $$ = exp;
710 variable_ref:
711 NAME {
712 struct prod_token_parm_item search_prod;
713 struct prod_token_parm_item *prod;
714 struct prod_token_parm_item *symbol_table_entry;
715 struct prod_token_parm_item *tok;
716 tree type;
718 tok = $1;
719 SYMBOL_TABLE_NAME ((&search_prod)) = tok;
720 search_prod.category = token_category;
721 symbol_table_entry = lookup_tree_name (&search_prod);
722 if (!symbol_table_entry)
724 error ("%Hvariable %q.*s not defined",
725 &tok->tp.tok.location, tok->tp.tok.length, tok->tp.tok.chars);
726 YYERROR;
729 prod = make_production (PROD_VARIABLE_REFERENCE_EXPRESSION, tok);
730 NUMERIC_TYPE (prod) = NUMERIC_TYPE (symbol_table_entry);
731 type = tree_code_get_type (NUMERIC_TYPE (prod));
732 if (!NUMERIC_TYPE (prod))
733 YYERROR;
734 OP1 (prod) = $1;
736 prod->tp.pro.code =
737 tree_code_get_expression (EXP_REFERENCE, type,
738 symbol_table_entry->tp.pro.code, NULL, NULL,
739 tok->tp.tok.location);
740 $$ = prod;
744 init_opt:
745 /* Nil. */ {
746 $$ = 0;
748 |init {
749 /* Pass the initialization value up. */
750 $$ = $1;
753 init:
754 ASSIGN init_element {
755 $$ = $2;
759 init_element:
760 INTEGER {
761 $$ = make_integer_constant ($1);
767 /* Print a token VALUE to file FILE. Ignore TYPE which is the token
768 type. */
770 void
771 print_token (FILE *file, unsigned int type ATTRIBUTE_UNUSED, YYSTYPE value)
773 struct prod_token_parm_item *tok;
774 unsigned int ix;
776 tok = value;
777 fprintf (file, "%d \"", LOCATION_LINE (tok->tp.tok.location));
778 for (ix = 0; ix < tok->tp.tok.length; ix++)
779 fprintf (file, "%c", tok->tp.tok.chars[ix]);
781 fprintf (file, "\"");
784 /* Output a message ERROR_MESSAGE from the parser. */
785 static void
786 yyerror (const char *error_message)
788 struct prod_token_parm_item *tok;
790 tok = yylval;
791 if (tok)
792 error ("%H%s", &tok->tp.tok.location, error_message);
793 else
794 error ("%s", error_message);
797 /* Reverse the order of a token list, linked by parse_next, old first
798 token is OLD_FIRST. */
800 static struct prod_token_parm_item*
801 reverse_prod_list (struct prod_token_parm_item *old_first)
803 struct prod_token_parm_item *current;
804 struct prod_token_parm_item *next;
805 struct prod_token_parm_item *prev = NULL;
807 current = old_first;
808 prev = NULL;
810 while (current)
812 gcc_assert (current->category == production_category);
814 next = current->tp.pro.next;
815 current->tp.pro.next = prev;
816 prev = current;
817 current = next;
819 return prev;
822 /* Ensure TYPE is not VOID. Use NAME as the token for the error location. */
824 static void
825 ensure_not_void (unsigned int type, struct prod_token_parm_item* name)
827 if (type == VOID_TYPE)
828 error ("%Htype must not be void in this context",
829 &name->tp.tok.location);
832 /* Check TYPE1 and TYPE2 which are integral types. Return the lowest
833 common type (min is signed int). */
835 static int
836 get_common_type (struct prod_token_parm_item *type1,
837 struct prod_token_parm_item *type2)
839 if (NUMERIC_TYPE (type1) == UNSIGNED_INT)
840 return UNSIGNED_INT;
841 if (NUMERIC_TYPE (type2) == UNSIGNED_INT)
842 return UNSIGNED_INT;
844 return SIGNED_INT;
847 /* Check type (TYPE_NUM) and expression (EXP) match. Return the 1 if
848 OK else 0. Must be exact match - same name unless it is an
849 integral type. */
851 static int
852 check_type_match (int type_num, struct prod_token_parm_item *exp)
854 switch (type_num)
856 case SIGNED_INT:
857 case UNSIGNED_INT:
858 case SIGNED_CHAR:
859 case UNSIGNED_CHAR:
860 switch (NUMERIC_TYPE (exp))
862 case SIGNED_INT:
863 case UNSIGNED_INT:
864 case SIGNED_CHAR:
865 case UNSIGNED_CHAR:
866 return 1;
868 case VOID_TYPE:
869 default:
870 gcc_unreachable ();
872 break;
874 case VOID_TYPE:
875 default:
876 gcc_unreachable ();
881 /* Make a production for an integer constant VALUE. */
883 static struct prod_token_parm_item *
884 make_integer_constant (struct prod_token_parm_item* value)
886 struct prod_token_parm_item *tok;
887 struct prod_token_parm_item *prod;
888 tok = value;
889 prod = make_production (PROD_INTEGER_CONSTANT, tok);
890 if ((tok->tp.tok.chars[0] == (unsigned char)'-')
891 || (tok->tp.tok.chars[0] == (unsigned char)'+'))
892 NUMERIC_TYPE (prod) = SIGNED_INT;
893 else
894 NUMERIC_TYPE (prod) = UNSIGNED_INT;
895 prod->tp.pro.code = tree_code_get_integer_value (tok->tp.tok.chars,
896 tok->tp.tok.length);
897 return prod;
901 /* Build a PROD_PLUS_EXPRESSION. This is uses for PLUS, MINUS, ASSIGN
902 and EQUALS expressions. */
904 static struct prod_token_parm_item *
905 make_plus_expression (struct prod_token_parm_item *tok,
906 struct prod_token_parm_item *op1,
907 struct prod_token_parm_item *op2,
908 int type_code, int prod_code)
910 struct prod_token_parm_item *prod;
911 tree type;
913 ensure_not_void (NUMERIC_TYPE (op1), op1->tp.pro.main_token);
914 ensure_not_void (NUMERIC_TYPE (op2), op2->tp.pro.main_token);
916 prod = make_production (PROD_PLUS_EXPRESSION, tok);
918 NUMERIC_TYPE (prod) = type_code;
919 type = tree_code_get_type (type_code);
921 gcc_assert (type);
923 OP1 (prod) = op1;
924 OP2 (prod) = op2;
926 prod->tp.pro.code = tree_code_get_expression (prod_code, type,
927 op1->tp.pro.code,
928 op2->tp.pro.code, NULL,
929 tok->tp.tok.location);
931 return prod;
935 /* Set STORAGE_CLASS in PROD according to CLASS_TOKEN. */
937 static void
938 set_storage (struct prod_token_parm_item *prod)
940 struct prod_token_parm_item *stg_class;
941 stg_class = STORAGE_CLASS_TOKEN (prod);
942 switch (stg_class->type)
944 case STATIC:
945 STORAGE_CLASS (prod) = STATIC_STORAGE;
946 break;
948 case AUTOMATIC:
949 STORAGE_CLASS (prod) = AUTOMATIC_STORAGE;
950 break;
952 case EXTERNAL_DEFINITION:
953 STORAGE_CLASS (prod) = EXTERNAL_DEFINITION_STORAGE;
954 break;
956 case EXTERNAL_REFERENCE:
957 STORAGE_CLASS (prod) = EXTERNAL_REFERENCE_STORAGE;
958 break;
960 default:
961 gcc_unreachable ();
965 /* Set parse trace. */
967 void
968 treelang_debug (void)
970 if (option_parser_trace)
971 yydebug = 1;
974 #ifdef __XGETTEXT__
975 /* Depending on the version of Bison used to compile this grammar,
976 it may issue generic diagnostics spelled "syntax error" or
977 "parse error". To prevent this from changing the translation
978 template randomly, we list all the variants of this particular
979 diagnostic here. Translators: there is no fine distinction
980 between diagnostics with "syntax error" in them, and diagnostics
981 with "parse error" in them. It's okay to give them both the same
982 translation. */
983 const char d1[] = N_("syntax error");
984 const char d2[] = N_("parse error");
985 const char d3[] = N_("syntax error; also virtual memory exhausted");
986 const char d4[] = N_("parse error; also virtual memory exhausted");
987 const char d5[] = N_("syntax error: cannot back up");
988 const char d6[] = N_("parse error: cannot back up");
989 #endif