1 /* Parser grammar for quick source code scan of Java(TM) language programs.
2 Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc. */
27 /* This file parses Java source code. Action can be further completed
28 to achieve a desired behavior. This file isn't part of the Java
29 language gcc front end.
31 The grammar conforms to the Java grammar described in "The Java(TM)
32 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
33 1996, ISBN 0-201-63451-1"
35 Some rules have been modified to support JDK1.1 inner classes
36 definitions and other extensions. */
43 #include "coretypes.h"
49 extern FILE *finput, *out;
51 /* Current position in real source file. */
53 location_t input_location;
55 /* Obstack for the lexer. */
56 struct obstack temporary_obstack;
58 /* The current parser context. */
59 struct parser_ctxt *ctxp;
61 /* Error and warning counts, because they're used elsewhere */
63 int java_warning_count;
65 /* Tweak default rules when necessary. */
67 #define USE_ABSORBER absorber = 0
69 /* Keep track of the current package name. */
70 static const char *package_name;
72 /* Keep track of whether things have be listed before. */
73 static int previous_output;
75 /* Record modifier uses */
76 static int modifier_value;
78 /* Record (almost) cyclomatic complexity. */
79 static int complexity;
81 /* Keeps track of number of bracket pairs after a variable declarator
83 static int bracket_count;
85 /* Numbers anonymous classes */
86 static int anonymous_count;
88 /* This is used to record the current class context. */
92 struct class_context *next;
95 /* The global class context. */
96 static struct class_context *current_class_context;
98 /* A special constant used to represent an anonymous context. */
99 static const char *anonymous_context = "ANONYMOUS";
101 /* Count of method depth. */
102 static int method_depth;
104 /* Record a method declaration */
105 struct method_declarator {
106 const char *method_name;
109 #define NEW_METHOD_DECLARATOR(D,N,A) \
111 (D) = xmalloc (sizeof (struct method_declarator)); \
112 (D)->method_name = (N); \
116 /* Two actions for this grammar */
117 static int make_class_name_recursive (struct obstack *stack,
118 struct class_context *ctx);
119 static char *get_class_name (void);
120 static void report_class_declaration (const char *);
121 static void report_main_declaration (struct method_declarator *);
122 static void push_class_context (const char *);
123 static void pop_class_context (void);
133 struct method_declarator *declarator;
134 int value; /* For modifiers */
138 extern int flag_assert;
145 /* Things defined here have to match the order of what's in the
146 binop_lookup table. */
148 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
149 %token LS_TK SRS_TK ZRS_TK
150 %token AND_TK XOR_TK OR_TK
151 %token BOOL_AND_TK BOOL_OR_TK
152 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
154 /* This maps to the same binop_lookup entry than the token above */
156 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
158 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
159 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
162 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
164 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
165 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
166 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
167 %token PAD_TK ABSTRACT_TK MODIFIER_TK
170 /* Keep those two in order, too */
171 %token DECR_TK INCR_TK
173 /* From now one, things can be in any order */
175 %token DEFAULT_TK IF_TK THROW_TK
176 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
177 %token THROWS_TK BREAK_TK IMPORT_TK
178 %token ELSE_TK INSTANCEOF_TK RETURN_TK
179 %token VOID_TK CATCH_TK INTERFACE_TK
180 %token CASE_TK EXTENDS_TK FINALLY_TK
181 %token SUPER_TK WHILE_TK CLASS_TK
182 %token SWITCH_TK CONST_TK TRY_TK
183 %token FOR_TK NEW_TK CONTINUE_TK
184 %token GOTO_TK PACKAGE_TK THIS_TK
187 %token BYTE_TK SHORT_TK INT_TK LONG_TK
188 %token CHAR_TK INTEGRAL_TK
190 %token FLOAT_TK DOUBLE_TK FP_TK
194 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
196 %token ASSIGN_ANY_TK ASSIGN_TK
197 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
199 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
200 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
202 %type <node> ID_TK identifier name simple_name qualified_name type
203 primitive_type reference_type array_type formal_parameter_list
204 formal_parameter class_or_interface_type class_type interface_type
205 %type <declarator> method_declarator
206 %type <value> MODIFIER_TK
209 /* 19.2 Production from 2.3: The Syntactic Grammar */
214 /* 19.3 Productions from 3: Lexical structure */
224 /* 19.4 Productions from 4: Types, Values and Variables */
233 /* use preset global here. FIXME */
234 $$ = xstrdup ("int");
238 /* use preset global here. FIXME */
239 $$ = xstrdup ("double");
243 /* use preset global here. FIXME */
244 $$ = xstrdup ("boolean");
249 class_or_interface_type
253 class_or_interface_type:
258 class_or_interface_type /* Default rule */
262 class_or_interface_type
268 while (bracket_count-- > 0)
269 $$ = concat ("[", $1, NULL);
273 while (bracket_count-- > 0)
274 $$ = concat ("[", $1, NULL);
278 /* 19.5 Productions from 6: Names */
280 simple_name /* Default rule */
281 | qualified_name /* Default rule */
285 identifier /* Default rule */
289 name DOT_TK identifier
291 $$ = concat ($1, ".", $3, NULL);
299 /* 19.6: Production from 7: Packages */
301 | package_declaration
302 | import_declarations
304 | package_declaration import_declarations
305 | package_declaration type_declarations
306 | import_declarations type_declarations
307 | package_declaration import_declarations type_declarations
312 | import_declarations import_declaration
317 | type_declarations type_declaration
321 PACKAGE_TK name SC_TK
322 { package_name = $2; }
326 single_type_import_declaration
327 | type_import_on_demand_declaration
330 single_type_import_declaration:
334 type_import_on_demand_declaration:
335 IMPORT_TK name DOT_TK MULT_TK SC_TK
340 | interface_declaration
344 /* 19.7 Shortened from the original:
345 modifiers: modifier | modifiers modifier
346 modifier: any of public... */
356 | modifiers MODIFIER_TK
366 /* 19.8.1 Production from $8.1: Class Declaration */
368 modifiers CLASS_TK identifier super interfaces
370 report_class_declaration($3);
374 | CLASS_TK identifier super interfaces
375 { report_class_declaration($2); }
380 | EXTENDS_TK class_type
384 | IMPLEMENTS_TK interface_type_list
390 | interface_type_list C_TK interface_type
396 { pop_class_context (); }
397 | OCB_TK class_body_declarations CCB_TK
398 { pop_class_context (); }
401 class_body_declarations:
402 class_body_declaration
403 | class_body_declarations class_body_declaration
406 class_body_declaration:
407 class_member_declaration
409 | constructor_declaration
410 | block /* Added, JDK1.1, instance initializer */
413 class_member_declaration:
416 | class_declaration /* Added, JDK1.1 inner classes */
417 | interface_declaration /* Added, JDK1.1 inner classes */
421 /* 19.8.2 Productions from 8.3: Field Declarations */
423 type variable_declarators SC_TK
425 | modifiers type variable_declarators SC_TK
426 { modifier_value = 0; }
429 variable_declarators:
430 /* Should we use build_decl_list () instead ? FIXME */
431 variable_declarator /* Default rule */
432 | variable_declarators C_TK variable_declarator
436 variable_declarator_id
437 | variable_declarator_id ASSIGN_TK variable_initializer
440 variable_declarator_id:
442 { bracket_count = 0; USE_ABSORBER; }
443 | variable_declarator_id OSB_TK CSB_TK
447 variable_initializer:
452 /* 19.8.3 Productions from 8.4: Method Declarations */
461 type method_declarator throws
463 | VOID_TK method_declarator throws
464 | modifiers type method_declarator throws
465 { modifier_value = 0; }
466 | modifiers VOID_TK method_declarator throws
468 report_main_declaration ($3);
474 identifier OP_TK CP_TK
476 struct method_declarator *d;
477 NEW_METHOD_DECLARATOR (d, $1, NULL);
480 | identifier OP_TK formal_parameter_list CP_TK
482 struct method_declarator *d;
483 NEW_METHOD_DECLARATOR (d, $1, $3);
486 | method_declarator OSB_TK CSB_TK
489 formal_parameter_list:
491 | formal_parameter_list C_TK formal_parameter
493 $$ = concat ($1, ",", $3, NULL);
498 type variable_declarator_id
504 char *n = xmalloc (bracket_count + 1 + strlen ($$));
505 for (i = 0; i < bracket_count; ++i)
507 strcpy (n + bracket_count, $$);
513 | modifiers type variable_declarator_id /* Added, JDK1.1 final locals */
518 char *n = xmalloc (bracket_count + 1 + strlen ($$));
519 for (i = 0; i < bracket_count; ++i)
521 strcpy (n + bracket_count, $$);
530 | THROWS_TK class_type_list
536 | class_type_list C_TK class_type
545 /* 19.8.4 Productions from 8.5: Static Initializers */
550 static: /* Test lval.sub_token here */
555 /* 19.8.5 Productions from 8.6: Constructor Declarations */
556 /* NOTE FOR FURTHER WORK ON CONSTRUCTORS:
557 - If a forbidden modifier is found, the error is either the use of
558 a forbidden modifier for a constructor OR bogus attempt to declare a
559 method without having specified the return type. FIXME */
560 constructor_declaration:
561 constructor_declarator throws constructor_body
562 | modifiers constructor_declarator throws constructor_body
563 { modifier_value = 0; }
564 /* extra SC_TK, FIXME */
565 | constructor_declarator throws constructor_body SC_TK
566 /* extra SC_TK, FIXME */
567 | modifiers constructor_declarator throws constructor_body SC_TK
568 { modifier_value = 0; }
569 /* I'm not happy with the SC_TK addition. It isn't in the grammar and should
570 probably be matched by and empty statement. But it doesn't work. FIXME */
573 constructor_declarator:
574 simple_name OP_TK CP_TK
576 | simple_name OP_TK formal_parameter_list CP_TK
582 | OCB_TK explicit_constructor_invocation CCB_TK
583 | OCB_TK block_statements CCB_TK
584 | OCB_TK explicit_constructor_invocation block_statements CCB_TK
587 /* Error recovery for that rule moved down expression_statement: rule. */
588 explicit_constructor_invocation:
589 this_or_super OP_TK CP_TK SC_TK
590 | this_or_super OP_TK argument_list CP_TK SC_TK
591 /* Added, JDK1.1 inner classes. Modified because the rule
592 'primary' couldn't work. */
593 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
595 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
599 this_or_super: /* Added, simplifies error diagnostics */
604 /* 19.9 Productions from 9: Interfaces */
605 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
606 interface_declaration:
607 INTERFACE_TK identifier
608 { report_class_declaration ($2); modifier_value = 0; }
610 | modifiers INTERFACE_TK identifier
611 { report_class_declaration ($3); modifier_value = 0; }
613 | INTERFACE_TK identifier extends_interfaces
614 { report_class_declaration ($2); modifier_value = 0; }
616 | modifiers INTERFACE_TK identifier extends_interfaces
617 { report_class_declaration ($3); modifier_value = 0; }
622 EXTENDS_TK interface_type
623 | extends_interfaces C_TK interface_type
628 { pop_class_context (); }
629 | OCB_TK interface_member_declarations CCB_TK
630 { pop_class_context (); }
633 interface_member_declarations:
634 interface_member_declaration
635 | interface_member_declarations interface_member_declaration
638 interface_member_declaration:
640 | abstract_method_declaration
641 | class_declaration /* Added, JDK1.1 inner classes */
642 | interface_declaration /* Added, JDK1.1 inner classes */
645 constant_declaration:
649 abstract_method_declaration:
653 /* 19.10 Productions from 10: Arrays */
656 | OCB_TK variable_initializers CCB_TK
658 | OCB_TK variable_initializers C_TK CCB_TK
661 variable_initializers:
663 | variable_initializers C_TK variable_initializer
666 /* 19.11 Production from 14: Blocks and Statements */
669 | OCB_TK block_statements CCB_TK
674 | block_statements block_statement
678 local_variable_declaration_statement
680 | class_declaration /* Added, JDK1.1 inner classes */
683 local_variable_declaration_statement:
684 local_variable_declaration SC_TK /* Can't catch missing ';' here */
687 local_variable_declaration:
688 type variable_declarators
690 | modifiers type variable_declarators /* Added, JDK1.1 final locals */
691 { modifier_value = 0; }
695 statement_without_trailing_substatement
698 | if_then_else_statement
704 statement_without_trailing_substatement
705 | labeled_statement_nsi
706 | if_then_else_statement_nsi
707 | while_statement_nsi
711 statement_without_trailing_substatement:
714 | expression_statement
720 | synchronized_statement
739 labeled_statement_nsi:
740 label_decl statement_nsi
743 /* We concentrate here a bunch of error handling rules that we couldn't write
744 earlier, because expression_statement catches a missing ';'. */
745 expression_statement:
746 statement_expression SC_TK
749 statement_expression:
751 | pre_increment_expression
752 | pre_decrement_expression
753 | post_increment_expression
754 | post_decrement_expression
756 | class_instance_creation_expression
760 IF_TK OP_TK expression CP_TK statement { ++complexity; }
763 if_then_else_statement:
764 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
768 if_then_else_statement_nsi:
769 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
774 SWITCH_TK OP_TK expression CP_TK switch_block
779 | OCB_TK switch_labels CCB_TK
780 | OCB_TK switch_block_statement_groups CCB_TK
781 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
784 switch_block_statement_groups:
785 switch_block_statement_group
786 | switch_block_statement_groups switch_block_statement_group
789 switch_block_statement_group:
790 switch_labels block_statements { ++complexity; }
796 | switch_labels switch_label
800 CASE_TK constant_expression REL_CL_TK
801 | DEFAULT_TK REL_CL_TK
805 WHILE_TK OP_TK expression CP_TK { ++complexity; }
809 while_expression statement
813 while_expression statement_nsi
821 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
826 for_begin SC_TK expression SC_TK for_update CP_TK statement
827 | for_begin SC_TK SC_TK for_update CP_TK statement
831 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
832 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
840 for_header for_init { ++complexity; }
842 for_init: /* Can be empty */
843 | statement_expression_list
844 | local_variable_declaration
847 for_update: /* Can be empty */
848 | statement_expression_list
851 statement_expression_list:
853 | statement_expression_list C_TK statement_expression
858 | BREAK_TK identifier SC_TK
861 /* `continue' with a label is considered for complexity but ordinary
865 | CONTINUE_TK identifier SC_TK { ++complexity; }
870 | RETURN_TK expression SC_TK
874 THROW_TK expression SC_TK { ++complexity; }
878 ASSERT_TK expression REL_CL_TK expression SC_TK
879 | ASSERT_TK expression SC_TK
881 {yyerror ("Missing term"); RECOVER;}
882 | ASSERT_TK expression error
883 {yyerror ("';' expected"); RECOVER;}
885 synchronized_statement:
886 synchronized OP_TK expression CP_TK block
887 | synchronized OP_TK expression CP_TK error
890 synchronized: /* Test lval.sub_token here */
897 | TRY_TK block finally
898 | TRY_TK block catches finally
903 | catches catch_clause
907 CATCH_TK OP_TK formal_parameter CP_TK block { ++complexity; }
911 FINALLY_TK block { ++complexity; }
914 /* 19.12 Production from 15: Expressions */
917 | array_creation_expression
920 primary_no_new_array:
923 | OP_TK expression CP_TK
924 | class_instance_creation_expression
929 /* Added, JDK1.1 inner classes. Documentation is wrong
930 referring to a 'ClassName' (class_name) rule that doesn't
931 exist. Used name instead. */
932 | name DOT_TK THIS_TK
939 | array_type DOT_TK CLASS_TK
941 | primitive_type DOT_TK CLASS_TK
943 | VOID_TK DOT_TK CLASS_TK
947 class_instance_creation_expression:
948 NEW_TK class_type OP_TK argument_list CP_TK
949 | NEW_TK class_type OP_TK CP_TK
950 | anonymous_class_creation
951 | something_dot_new identifier OP_TK CP_TK
952 | something_dot_new identifier OP_TK CP_TK class_body
953 | something_dot_new identifier OP_TK argument_list CP_TK
954 | something_dot_new identifier OP_TK argument_list CP_TK class_body
957 anonymous_class_creation:
958 NEW_TK class_type OP_TK CP_TK
959 { report_class_declaration (anonymous_context); }
961 | NEW_TK class_type OP_TK argument_list CP_TK
962 { report_class_declaration (anonymous_context); }
966 something_dot_new: /* Added, not part of the specs. */
969 | primary DOT_TK NEW_TK
974 | argument_list C_TK expression
975 | argument_list C_TK error
978 array_creation_expression:
979 NEW_TK primitive_type dim_exprs
980 | NEW_TK class_or_interface_type dim_exprs
981 | NEW_TK primitive_type dim_exprs dims
982 | NEW_TK class_or_interface_type dim_exprs dims
983 /* Added, JDK1.1 anonymous array. Initial documentation rule
985 | NEW_TK class_or_interface_type dims array_initializer
986 | NEW_TK primitive_type dims array_initializer
995 OSB_TK expression CSB_TK
1000 { bracket_count = 1; }
1001 | dims OSB_TK CSB_TK
1002 { bracket_count++; }
1006 primary DOT_TK identifier
1007 | SUPER_TK DOT_TK identifier
1010 /* We include method invocation in the complexity measure on the
1011 theory that most method calls are virtual and therefore involve a
1015 { USE_ABSORBER; ++complexity; }
1016 | name OP_TK argument_list CP_TK
1017 { USE_ABSORBER; ++complexity; }
1018 | primary DOT_TK identifier OP_TK CP_TK { ++complexity; }
1019 | primary DOT_TK identifier OP_TK argument_list CP_TK { ++complexity; }
1020 | SUPER_TK DOT_TK identifier OP_TK CP_TK { ++complexity; }
1021 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK { ++complexity; }
1025 name OSB_TK expression CSB_TK
1027 | primary_no_new_array OSB_TK expression CSB_TK
1034 | post_increment_expression
1035 | post_decrement_expression
1038 post_increment_expression:
1039 postfix_expression INCR_TK
1042 post_decrement_expression:
1043 postfix_expression DECR_TK
1047 pre_increment_expression
1048 | pre_decrement_expression
1049 | PLUS_TK unary_expression
1050 | MINUS_TK unary_expression
1051 | unary_expression_not_plus_minus
1054 pre_increment_expression:
1055 INCR_TK unary_expression
1058 pre_decrement_expression:
1059 DECR_TK unary_expression
1062 unary_expression_not_plus_minus:
1064 | NOT_TK unary_expression
1065 | NEG_TK unary_expression
1069 cast_expression: /* Error handling here is potentially weak */
1070 OP_TK primitive_type dims CP_TK unary_expression
1071 | OP_TK primitive_type CP_TK unary_expression
1072 | OP_TK expression CP_TK unary_expression_not_plus_minus
1073 | OP_TK name dims CP_TK unary_expression_not_plus_minus
1076 multiplicative_expression:
1078 | multiplicative_expression MULT_TK unary_expression
1079 | multiplicative_expression DIV_TK unary_expression
1080 | multiplicative_expression REM_TK unary_expression
1083 additive_expression:
1084 multiplicative_expression
1085 | additive_expression PLUS_TK multiplicative_expression
1086 | additive_expression MINUS_TK multiplicative_expression
1091 | shift_expression LS_TK additive_expression
1092 | shift_expression SRS_TK additive_expression
1093 | shift_expression ZRS_TK additive_expression
1096 relational_expression:
1098 | relational_expression LT_TK shift_expression
1099 | relational_expression GT_TK shift_expression
1100 | relational_expression LTE_TK shift_expression
1101 | relational_expression GTE_TK shift_expression
1102 | relational_expression INSTANCEOF_TK reference_type
1105 equality_expression:
1106 relational_expression
1107 | equality_expression EQ_TK relational_expression
1108 | equality_expression NEQ_TK relational_expression
1113 | and_expression AND_TK equality_expression
1116 exclusive_or_expression:
1118 | exclusive_or_expression XOR_TK and_expression
1121 inclusive_or_expression:
1122 exclusive_or_expression
1123 | inclusive_or_expression OR_TK exclusive_or_expression
1126 conditional_and_expression:
1127 inclusive_or_expression
1128 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
1132 conditional_or_expression:
1133 conditional_and_expression
1134 | conditional_or_expression BOOL_OR_TK conditional_and_expression
1138 conditional_expression: /* Error handling here is weak */
1139 conditional_or_expression
1140 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
1144 assignment_expression:
1145 conditional_expression
1150 left_hand_side assignment_operator assignment_expression
1160 assignment_operator:
1166 assignment_expression
1169 constant_expression:
1175 /* Create a new parser context */
1178 java_push_parser_context (void)
1180 struct parser_ctxt *new = xcalloc (1, sizeof (struct parser_ctxt));
1187 push_class_context (const char *name)
1189 struct class_context *ctx;
1191 ctx = xmalloc (sizeof (struct class_context));
1192 ctx->name = (char *) name;
1193 ctx->next = current_class_context;
1194 current_class_context = ctx;
1198 pop_class_context (void)
1200 struct class_context *ctx;
1202 if (current_class_context == NULL)
1205 ctx = current_class_context->next;
1206 if (current_class_context->name != anonymous_context)
1207 free (current_class_context->name);
1208 free (current_class_context);
1210 current_class_context = ctx;
1211 if (current_class_context == NULL)
1212 anonymous_count = 0;
1215 /* Recursively construct the class name. This is just a helper
1216 function for get_class_name(). */
1218 make_class_name_recursive (struct obstack *stack, struct class_context *ctx)
1223 make_class_name_recursive (stack, ctx->next);
1225 /* Replace an anonymous context with the appropriate counter value. */
1226 if (ctx->name == anonymous_context)
1230 sprintf (buf, "%d", anonymous_count);
1231 ctx->name = xstrdup (buf);
1234 obstack_grow (stack, ctx->name, strlen (ctx->name));
1235 obstack_1grow (stack, '$');
1237 return ISDIGIT (ctx->name[0]);
1240 /* Return a newly allocated string holding the name of the class. */
1242 get_class_name (void)
1246 struct obstack name_stack;
1248 obstack_init (&name_stack);
1250 /* Duplicate the logic of parse.y:maybe_make_nested_class_name(). */
1251 last_was_digit = make_class_name_recursive (&name_stack,
1252 current_class_context->next);
1254 if (! last_was_digit
1256 && current_class_context->name != anonymous_context)
1260 sprintf (buf, "%d", anonymous_count);
1261 obstack_grow (&name_stack, buf, strlen (buf));
1262 obstack_1grow (&name_stack, '$');
1265 if (current_class_context->name == anonymous_context)
1269 sprintf (buf, "%d", anonymous_count);
1270 current_class_context->name = xstrdup (buf);
1271 obstack_grow0 (&name_stack, buf, strlen (buf));
1274 obstack_grow0 (&name_stack, current_class_context->name,
1275 strlen (current_class_context->name));
1277 result = xstrdup (obstack_finish (&name_stack));
1278 obstack_free (&name_stack, NULL);
1283 /* Actions defined here */
1286 report_class_declaration (const char * name)
1288 extern int flag_dump_class, flag_list_filename;
1290 push_class_context (name);
1291 if (flag_dump_class)
1293 char *name = get_class_name ();
1295 if (!previous_output)
1297 if (flag_list_filename)
1298 fprintf (out, "%s: ", input_filename);
1299 previous_output = 1;
1303 fprintf (out, "%s.%s ", package_name, name);
1305 fprintf (out, "%s ", name);
1312 report_main_declaration (struct method_declarator *declarator)
1314 extern int flag_find_main;
1317 && modifier_value == 2
1318 && !strcmp (declarator->method_name, "main")
1320 && declarator->args [0] == '['
1321 && (! strcmp (declarator->args+1, "String")
1322 || ! strcmp (declarator->args + 1, "java.lang.String"))
1323 && current_class_context)
1325 if (!previous_output)
1327 char *name = get_class_name ();
1329 fprintf (out, "%s.%s ", package_name, name);
1331 fprintf (out, "%s", name);
1333 previous_output = 1;
1341 extern int flag_complexity;
1342 if (flag_complexity)
1343 fprintf (out, "%s %d\n", input_filename, complexity);
1346 /* Reset global status used by the report functions. */
1351 previous_output = 0;
1352 package_name = NULL;
1353 current_class_context = NULL;
1358 yyerror (const char *msg ATTRIBUTE_UNUSED)
1360 fprintf (stderr, "%s: %d: %s\n", input_filename, input_line, msg);
1365 /* Depending on the version of Bison used to compile this grammar,
1366 it may issue generic diagnostics spelled "syntax error" or
1367 "parse error". To prevent this from changing the translation
1368 template randomly, we list all the variants of this particular
1369 diagnostic here. Translators: there is no fine distinction
1370 between diagnostics with "syntax error" in them, and diagnostics
1371 with "parse error" in them. It's okay to give them both the same
1373 const char d1[] = N_("syntax error");
1374 const char d2[] = N_("parse error");
1375 const char d3[] = N_("syntax error; also virtual memory exhausted");
1376 const char d4[] = N_("parse error; also virtual memory exhausted");
1377 const char d5[] = N_("syntax error: cannot back up");
1378 const char d6[] = N_("parse error: cannot back up");