1 /* Parser grammar for quick source code scan of Java(TM) language programs.
2 Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2005
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, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, 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"
48 extern
FILE *finput
, *out
;
50 const char *main_input_filename
;
52 /* Obstack for the lexer. */
53 struct obstack temporary_obstack
;
55 /* The current parser context. */
56 struct parser_ctxt
*ctxp
;
58 /* Error and warning counts, because they're used elsewhere */
60 int java_warning_count
;
62 /* Tweak default rules when necessary. */
64 #define USE_ABSORBER absorber = 0
66 /* Keep track of the current package name. */
67 static const char *package_name
;
69 /* Keep track of whether things have be listed before. */
70 static int previous_output
;
72 /* Record modifier uses */
73 static int modifier_value
;
75 /* Record (almost) cyclomatic complexity. */
76 static int complexity
;
78 /* Keeps track of number of bracket pairs after a variable declarator
80 static int bracket_count
;
82 /* Numbers anonymous classes */
83 static int anonymous_count
;
85 /* This is used to record the current class context. */
89 struct class_context
*next
;
92 /* The global class context. */
93 static struct class_context
*current_class_context
;
95 /* A special constant used to represent an anonymous context. */
96 static const char *anonymous_context
= "ANONYMOUS";
98 /* Count of method depth. */
99 static int method_depth
;
101 /* Record a method declaration */
102 struct method_declarator
{
103 const char *method_name
;
106 #define NEW_METHOD_DECLARATOR(D,N,A) \
108 (D
) = XNEW
(struct method_declarator
); \
109 (D
)->method_name
= (N
); \
113 /* Two actions for this grammar */
114 static int make_class_name_recursive
(struct obstack
*stack
,
115 struct class_context
*ctx
);
116 static char *get_class_name
(void);
117 static void report_class_declaration
(const char *);
118 static void report_main_declaration
(struct method_declarator
*);
119 static void push_class_context
(const char *);
120 static void pop_class_context
(void);
130 struct method_declarator
*declarator
;
131 int value
; /* For modifiers */
135 extern
int flag_assert
;
142 /* Things defined here have to match the order of what's in the
143 binop_lookup table. */
145 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
146 %token LS_TK SRS_TK ZRS_TK
147 %token AND_TK XOR_TK OR_TK
148 %token BOOL_AND_TK BOOL_OR_TK
149 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
151 /* This maps to the same binop_lookup entry than the token above */
153 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
155 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
156 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
159 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
161 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
162 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
163 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
164 %token PAD_TK ABSTRACT_TK MODIFIER_TK
167 /* Keep those two in order, too */
168 %token DECR_TK INCR_TK
170 /* From now one, things can be in any order */
172 %token DEFAULT_TK IF_TK THROW_TK
173 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
174 %token THROWS_TK BREAK_TK IMPORT_TK
175 %token ELSE_TK INSTANCEOF_TK RETURN_TK
176 %token VOID_TK CATCH_TK INTERFACE_TK
177 %token CASE_TK EXTENDS_TK FINALLY_TK
178 %token SUPER_TK WHILE_TK CLASS_TK
179 %token SWITCH_TK CONST_TK TRY_TK
180 %token FOR_TK NEW_TK CONTINUE_TK
181 %token GOTO_TK PACKAGE_TK THIS_TK
184 %token BYTE_TK SHORT_TK INT_TK LONG_TK
185 %token CHAR_TK INTEGRAL_TK
187 %token FLOAT_TK DOUBLE_TK FP_TK
191 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
193 %token ASSIGN_ANY_TK ASSIGN_TK
194 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
196 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
197 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
199 %type
<node
> ID_TK identifier name simple_name qualified_name type
200 primitive_type reference_type array_type formal_parameter_list
201 formal_parameter class_or_interface_type class_type interface_type
202 %type
<declarator
> method_declarator
203 %type
<value
> MODIFIER_TK
206 /* 19.2 Production from 2.3: The Syntactic Grammar */
211 /* 19.3 Productions from 3: Lexical structure */
221 /* 19.4 Productions from 4: Types, Values and Variables */
230 /* use preset global here. FIXME */
231 $$
= xstrdup
("int");
235 /* use preset global here. FIXME */
236 $$
= xstrdup
("double");
240 /* use preset global here. FIXME */
241 $$
= xstrdup
("boolean");
246 class_or_interface_type
250 class_or_interface_type:
255 class_or_interface_type
/* Default rule */
259 class_or_interface_type
265 while
(bracket_count
-- > 0)
266 $$
= concat
("[", $1, NULL
);
270 while
(bracket_count
-- > 0)
271 $$
= concat
("[", $1, NULL
);
275 /* 19.5 Productions from 6: Names */
277 simple_name
/* Default rule */
278 | qualified_name
/* Default rule */
282 identifier
/* Default rule */
286 name DOT_TK identifier
288 $$
= concat
($1, ".", $3, NULL
);
296 /* 19.6: Production from 7: Packages */
298 | package_declaration
299 | import_declarations
301 | package_declaration import_declarations
302 | package_declaration type_declarations
303 | import_declarations type_declarations
304 | package_declaration import_declarations type_declarations
309 | import_declarations import_declaration
314 | type_declarations type_declaration
318 PACKAGE_TK name SC_TK
319 { package_name
= $2; }
323 single_type_import_declaration
324 | type_import_on_demand_declaration
327 single_type_import_declaration:
331 type_import_on_demand_declaration:
332 IMPORT_TK name DOT_TK MULT_TK SC_TK
337 | interface_declaration
341 /* 19.7 Shortened from the original:
342 modifiers: modifier | modifiers modifier
343 modifier: any of public... */
353 | modifiers MODIFIER_TK
363 /* 19.8.1 Production from $8.1: Class Declaration */
365 modifiers CLASS_TK identifier super interfaces
367 report_class_declaration
($3);
371 | CLASS_TK identifier super interfaces
372 { report_class_declaration
($2); }
377 | EXTENDS_TK class_type
381 | IMPLEMENTS_TK interface_type_list
387 | interface_type_list C_TK interface_type
393 { pop_class_context
(); }
394 | OCB_TK class_body_declarations CCB_TK
395 { pop_class_context
(); }
398 class_body_declarations:
399 class_body_declaration
400 | class_body_declarations class_body_declaration
403 class_body_declaration:
404 class_member_declaration
406 | constructor_declaration
407 | block
/* Added, JDK1.1, instance initializer */
410 class_member_declaration:
413 | class_declaration
/* Added, JDK1.1 inner classes */
414 | interface_declaration
/* Added, JDK1.1 inner classes */
418 /* 19.8.2 Productions from 8.3: Field Declarations */
420 type variable_declarators SC_TK
422 | modifiers type variable_declarators SC_TK
423 { modifier_value
= 0; }
426 variable_declarators:
427 /* Should we use build_decl_list () instead ? FIXME */
428 variable_declarator
/* Default rule */
429 | variable_declarators C_TK variable_declarator
433 variable_declarator_id
434 | variable_declarator_id ASSIGN_TK variable_initializer
437 variable_declarator_id:
439 { bracket_count
= 0; USE_ABSORBER
; }
440 | variable_declarator_id OSB_TK CSB_TK
444 variable_initializer:
449 /* 19.8.3 Productions from 8.4: Method Declarations */
458 type method_declarator throws
460 | VOID_TK method_declarator throws
461 | modifiers type method_declarator throws
462 { modifier_value
= 0; }
463 | modifiers VOID_TK method_declarator throws
465 report_main_declaration
($3);
471 identifier OP_TK CP_TK
473 struct method_declarator
*d
;
474 NEW_METHOD_DECLARATOR
(d
, $1, NULL
);
477 | identifier OP_TK formal_parameter_list CP_TK
479 struct method_declarator
*d
;
480 NEW_METHOD_DECLARATOR
(d
, $1, $3);
483 | method_declarator OSB_TK CSB_TK
486 formal_parameter_list:
488 | formal_parameter_list C_TK formal_parameter
490 $$
= concat
($1, ",", $3, NULL
);
495 type variable_declarator_id
501 char *n
= XNEWVEC
(char, bracket_count
+ 1 + strlen
($$
));
502 for
(i
= 0; i
< bracket_count
; ++i
)
504 strcpy
(n
+ bracket_count
, $$
);
510 | modifiers type variable_declarator_id
/* Added, JDK1.1 final locals */
515 char *n
= XNEWVEC
(char, bracket_count
+ 1 + strlen
($2));
516 for
(i
= 0; i
< bracket_count
; ++i
)
518 strcpy
(n
+ bracket_count
, $2);
527 | THROWS_TK class_type_list
533 | class_type_list C_TK class_type
542 /* 19.8.4 Productions from 8.5: Static Initializers */
547 static: /* Test lval.sub_token here */
552 /* 19.8.5 Productions from 8.6: Constructor Declarations */
553 /* NOTE FOR FURTHER WORK ON CONSTRUCTORS:
554 - If a forbidden modifier is found, the error is either the use of
555 a forbidden modifier for a constructor OR bogus attempt to declare a
556 method without having specified the return type. FIXME */
557 constructor_declaration:
558 constructor_declarator throws constructor_body
559 | modifiers constructor_declarator throws constructor_body
560 { modifier_value
= 0; }
561 /* extra SC_TK, FIXME */
562 | constructor_declarator throws constructor_body SC_TK
563 /* extra SC_TK, FIXME */
564 | modifiers constructor_declarator throws constructor_body SC_TK
565 { modifier_value
= 0; }
566 /* I'm not happy with the SC_TK addition. It isn't in the grammar and should
567 probably be matched by and empty statement. But it doesn't work. FIXME */
570 constructor_declarator:
571 simple_name OP_TK CP_TK
573 | simple_name OP_TK formal_parameter_list CP_TK
579 | OCB_TK explicit_constructor_invocation CCB_TK
580 | OCB_TK block_statements CCB_TK
581 | OCB_TK explicit_constructor_invocation block_statements CCB_TK
584 /* Error recovery for that rule moved down expression_statement: rule. */
585 explicit_constructor_invocation:
586 this_or_super OP_TK CP_TK SC_TK
587 | this_or_super OP_TK argument_list CP_TK SC_TK
588 /* Added, JDK1.1 inner classes. Modified because the rule
589 'primary' couldn't work. */
590 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
592 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
596 this_or_super: /* Added, simplifies error diagnostics */
601 /* 19.9 Productions from 9: Interfaces */
602 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
603 interface_declaration:
604 INTERFACE_TK identifier
605 { report_class_declaration
($2); modifier_value
= 0; }
607 | modifiers INTERFACE_TK identifier
608 { report_class_declaration
($3); modifier_value
= 0; }
610 | INTERFACE_TK identifier extends_interfaces
611 { report_class_declaration
($2); modifier_value
= 0; }
613 | modifiers INTERFACE_TK identifier extends_interfaces
614 { report_class_declaration
($3); modifier_value
= 0; }
619 EXTENDS_TK interface_type
620 | extends_interfaces C_TK interface_type
625 { pop_class_context
(); }
626 | OCB_TK interface_member_declarations CCB_TK
627 { pop_class_context
(); }
630 interface_member_declarations:
631 interface_member_declaration
632 | interface_member_declarations interface_member_declaration
635 interface_member_declaration:
637 | abstract_method_declaration
638 | class_declaration
/* Added, JDK1.1 inner classes */
639 | interface_declaration
/* Added, JDK1.1 inner classes */
643 constant_declaration:
647 abstract_method_declaration:
651 /* 19.10 Productions from 10: Arrays */
654 | OCB_TK variable_initializers CCB_TK
656 | OCB_TK variable_initializers C_TK CCB_TK
659 variable_initializers:
661 | variable_initializers C_TK variable_initializer
664 /* 19.11 Production from 14: Blocks and Statements */
667 | OCB_TK block_statements CCB_TK
672 | block_statements block_statement
676 local_variable_declaration_statement
678 | class_declaration
/* Added, JDK1.1 inner classes */
681 local_variable_declaration_statement:
682 local_variable_declaration SC_TK
/* Can't catch missing ';' here */
685 local_variable_declaration:
686 type variable_declarators
688 | modifiers type variable_declarators
/* Added, JDK1.1 final locals */
689 { modifier_value
= 0; }
693 statement_without_trailing_substatement
696 | if_then_else_statement
702 statement_without_trailing_substatement
703 | labeled_statement_nsi
704 | if_then_else_statement_nsi
705 | while_statement_nsi
709 statement_without_trailing_substatement:
712 | expression_statement
718 | synchronized_statement
737 labeled_statement_nsi:
738 label_decl statement_nsi
741 /* We concentrate here a bunch of error handling rules that we couldn't write
742 earlier, because expression_statement catches a missing ';'. */
743 expression_statement:
744 statement_expression SC_TK
747 statement_expression:
749 | pre_increment_expression
750 | pre_decrement_expression
751 | post_increment_expression
752 | post_decrement_expression
754 | class_instance_creation_expression
758 IF_TK OP_TK expression CP_TK statement
{ ++complexity
; }
761 if_then_else_statement:
762 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
766 if_then_else_statement_nsi:
767 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
772 SWITCH_TK OP_TK expression CP_TK switch_block
777 | OCB_TK switch_labels CCB_TK
778 | OCB_TK switch_block_statement_groups CCB_TK
779 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
782 switch_block_statement_groups:
783 switch_block_statement_group
784 | switch_block_statement_groups switch_block_statement_group
787 switch_block_statement_group:
788 switch_labels block_statements
{ ++complexity
; }
794 | switch_labels switch_label
798 CASE_TK constant_expression REL_CL_TK
799 | DEFAULT_TK REL_CL_TK
803 WHILE_TK OP_TK expression CP_TK
{ ++complexity
; }
807 while_expression statement
811 while_expression statement_nsi
819 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
824 for_begin SC_TK expression SC_TK for_update CP_TK statement
825 | for_begin SC_TK SC_TK for_update CP_TK statement
829 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
830 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
838 for_header for_init
{ ++complexity
; }
840 for_init: /* Can be empty */
841 | statement_expression_list
842 | local_variable_declaration
845 for_update: /* Can be empty */
846 | statement_expression_list
849 statement_expression_list:
851 | statement_expression_list C_TK statement_expression
856 | BREAK_TK identifier SC_TK
859 /* `continue' with a label is considered for complexity but ordinary
863 | CONTINUE_TK identifier SC_TK
{ ++complexity
; }
868 | RETURN_TK expression SC_TK
872 THROW_TK expression SC_TK
{ ++complexity
; }
876 ASSERT_TK expression REL_CL_TK expression SC_TK
877 | ASSERT_TK expression SC_TK
879 {yyerror ("Missing term"); RECOVER
;}
880 | ASSERT_TK expression
error
881 {yyerror ("';' expected"); RECOVER
;}
883 synchronized_statement:
884 synchronized OP_TK expression CP_TK block
885 | synchronized OP_TK expression CP_TK
error
888 synchronized: /* Test lval.sub_token here */
895 | TRY_TK block finally
896 | TRY_TK block catches finally
901 | catches catch_clause
905 CATCH_TK OP_TK formal_parameter CP_TK block
{ ++complexity
; }
909 FINALLY_TK block
{ ++complexity
; }
912 /* 19.12 Production from 15: Expressions */
915 | array_creation_expression
918 primary_no_new_array:
921 | OP_TK expression CP_TK
922 | class_instance_creation_expression
927 /* Added, JDK1.1 inner classes. Documentation is wrong
928 referring to a 'ClassName' (class_name) rule that doesn't
929 exist. Used name instead. */
930 | name DOT_TK THIS_TK
937 | array_type DOT_TK CLASS_TK
939 | primitive_type DOT_TK CLASS_TK
941 | VOID_TK DOT_TK CLASS_TK
945 class_instance_creation_expression:
946 NEW_TK class_type OP_TK argument_list CP_TK
947 | NEW_TK class_type OP_TK CP_TK
948 | anonymous_class_creation
949 | something_dot_new identifier OP_TK CP_TK
950 | something_dot_new identifier OP_TK CP_TK class_body
951 | something_dot_new identifier OP_TK argument_list CP_TK
952 | something_dot_new identifier OP_TK argument_list CP_TK class_body
955 anonymous_class_creation:
956 NEW_TK class_type OP_TK CP_TK
957 { report_class_declaration
(anonymous_context
); }
959 | NEW_TK class_type OP_TK argument_list CP_TK
960 { report_class_declaration
(anonymous_context
); }
964 something_dot_new: /* Added, not part of the specs. */
967 | primary DOT_TK NEW_TK
972 | argument_list C_TK expression
973 | argument_list C_TK
error
976 array_creation_expression:
977 NEW_TK primitive_type dim_exprs
978 | NEW_TK class_or_interface_type dim_exprs
979 | NEW_TK primitive_type dim_exprs dims
980 | NEW_TK class_or_interface_type dim_exprs dims
981 /* Added, JDK1.1 anonymous array. Initial documentation rule
983 | NEW_TK class_or_interface_type dims array_initializer
984 | NEW_TK primitive_type dims array_initializer
993 OSB_TK expression CSB_TK
998 { bracket_count
= 1; }
1000 { bracket_count
++; }
1004 primary DOT_TK identifier
1005 | SUPER_TK DOT_TK identifier
1008 /* We include method invocation in the complexity measure on the
1009 theory that most method calls are virtual and therefore involve a
1013 { USE_ABSORBER
; ++complexity
; }
1014 | name OP_TK argument_list CP_TK
1015 { USE_ABSORBER
; ++complexity
; }
1016 | primary DOT_TK identifier OP_TK CP_TK
{ ++complexity
; }
1017 | primary DOT_TK identifier OP_TK argument_list CP_TK
{ ++complexity
; }
1018 | SUPER_TK DOT_TK identifier OP_TK CP_TK
{ ++complexity
; }
1019 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
{ ++complexity
; }
1023 name OSB_TK expression CSB_TK
1025 | primary_no_new_array OSB_TK expression CSB_TK
1032 | post_increment_expression
1033 | post_decrement_expression
1036 post_increment_expression:
1037 postfix_expression INCR_TK
1040 post_decrement_expression:
1041 postfix_expression DECR_TK
1045 pre_increment_expression
1046 | pre_decrement_expression
1047 | PLUS_TK unary_expression
1048 | MINUS_TK unary_expression
1049 | unary_expression_not_plus_minus
1052 pre_increment_expression:
1053 INCR_TK unary_expression
1056 pre_decrement_expression:
1057 DECR_TK unary_expression
1060 unary_expression_not_plus_minus:
1062 | NOT_TK unary_expression
1063 | NEG_TK unary_expression
1067 cast_expression: /* Error handling here is potentially weak */
1068 OP_TK primitive_type dims CP_TK unary_expression
1069 | OP_TK primitive_type CP_TK unary_expression
1070 | OP_TK expression CP_TK unary_expression_not_plus_minus
1071 | OP_TK name dims CP_TK unary_expression_not_plus_minus
1074 multiplicative_expression:
1076 | multiplicative_expression MULT_TK unary_expression
1077 | multiplicative_expression DIV_TK unary_expression
1078 | multiplicative_expression REM_TK unary_expression
1081 additive_expression:
1082 multiplicative_expression
1083 | additive_expression PLUS_TK multiplicative_expression
1084 | additive_expression MINUS_TK multiplicative_expression
1089 | shift_expression LS_TK additive_expression
1090 | shift_expression SRS_TK additive_expression
1091 | shift_expression ZRS_TK additive_expression
1094 relational_expression:
1096 | relational_expression LT_TK shift_expression
1097 | relational_expression GT_TK shift_expression
1098 | relational_expression LTE_TK shift_expression
1099 | relational_expression GTE_TK shift_expression
1100 | relational_expression INSTANCEOF_TK reference_type
1103 equality_expression:
1104 relational_expression
1105 | equality_expression EQ_TK relational_expression
1106 | equality_expression NEQ_TK relational_expression
1111 | and_expression AND_TK equality_expression
1114 exclusive_or_expression:
1116 | exclusive_or_expression XOR_TK and_expression
1119 inclusive_or_expression:
1120 exclusive_or_expression
1121 | inclusive_or_expression OR_TK exclusive_or_expression
1124 conditional_and_expression:
1125 inclusive_or_expression
1126 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
1130 conditional_or_expression:
1131 conditional_and_expression
1132 | conditional_or_expression BOOL_OR_TK conditional_and_expression
1136 conditional_expression: /* Error handling here is weak */
1137 conditional_or_expression
1138 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
1142 assignment_expression:
1143 conditional_expression
1148 left_hand_side assignment_operator assignment_expression
1158 assignment_operator:
1164 assignment_expression
1167 constant_expression:
1173 /* Create a new parser context */
1176 java_push_parser_context
(void)
1178 struct parser_ctxt
*tmp
= XCNEW
(struct parser_ctxt
);
1185 push_class_context
(const char *name
)
1187 struct class_context
*ctx
;
1189 ctx
= XNEW
(struct class_context
);
1190 ctx
->name
= (char *) name
;
1191 ctx
->next
= current_class_context
;
1192 current_class_context
= ctx
;
1196 pop_class_context
(void)
1198 struct class_context
*ctx
;
1200 if
(current_class_context
== NULL
)
1203 ctx
= current_class_context
->next
;
1204 if
(current_class_context
->name
!= anonymous_context
)
1205 free
(current_class_context
->name
);
1206 free
(current_class_context
);
1208 current_class_context
= ctx
;
1209 if
(current_class_context
== NULL
)
1210 anonymous_count
= 0;
1213 /* Recursively construct the class name. This is just a helper
1214 function for get_class_name(). */
1216 make_class_name_recursive
(struct obstack
*stack
, struct class_context
*ctx
)
1221 make_class_name_recursive
(stack
, ctx
->next
);
1223 /* Replace an anonymous context with the appropriate counter value. */
1224 if
(ctx
->name
== anonymous_context
)
1228 sprintf
(buf
, "%d", anonymous_count
);
1229 ctx
->name
= xstrdup
(buf
);
1232 obstack_grow
(stack
, ctx
->name
, strlen
(ctx
->name
));
1233 obstack_1grow
(stack
, '$');
1235 return ISDIGIT
(ctx
->name
[0]);
1238 /* Return a newly allocated string holding the name of the class. */
1240 get_class_name
(void)
1244 struct obstack name_stack
;
1246 obstack_init
(&name_stack
);
1248 /* Duplicate the logic of parse.y:maybe_make_nested_class_name(). */
1249 last_was_digit
= make_class_name_recursive
(&name_stack
,
1250 current_class_context
->next
);
1252 if
(! last_was_digit
1254 && current_class_context
->name
!= anonymous_context
)
1258 sprintf
(buf
, "%d", anonymous_count
);
1259 obstack_grow
(&name_stack
, buf
, strlen
(buf
));
1260 obstack_1grow
(&name_stack
, '$');
1263 if
(current_class_context
->name
== anonymous_context
)
1267 sprintf
(buf
, "%d", anonymous_count
);
1268 current_class_context
->name
= xstrdup
(buf
);
1269 obstack_grow0
(&name_stack
, buf
, strlen
(buf
));
1272 obstack_grow0
(&name_stack
, current_class_context
->name
,
1273 strlen
(current_class_context
->name
));
1275 result
= xstrdup
(obstack_finish
(&name_stack
));
1276 obstack_free
(&name_stack
, NULL
);
1281 /* Actions defined here */
1284 report_class_declaration
(const char * name
)
1286 extern
int flag_dump_class
, flag_list_filename
;
1288 push_class_context
(name
);
1289 if
(flag_dump_class
)
1291 char *name
= get_class_name
();
1293 if
(!previous_output
)
1295 if
(flag_list_filename
)
1296 fprintf
(out
, "%s: ", main_input_filename
);
1297 previous_output
= 1;
1301 fprintf
(out
, "%s.%s ", package_name
, name
);
1303 fprintf
(out
, "%s ", name
);
1310 report_main_declaration
(struct method_declarator
*declarator
)
1312 extern
int flag_find_main
;
1315 && modifier_value
== 2
1316 && !strcmp
(declarator
->method_name
, "main")
1318 && declarator
->args
[0] == '['
1319 && (! strcmp
(declarator
->args
+1, "String")
1320 ||
! strcmp
(declarator
->args
+ 1, "java.lang.String"))
1321 && current_class_context
)
1323 if
(!previous_output
)
1325 char *name
= get_class_name
();
1327 fprintf
(out
, "%s.%s ", package_name
, name
);
1329 fprintf
(out
, "%s", name
);
1331 previous_output
= 1;
1339 extern
int flag_complexity
;
1340 if
(flag_complexity
)
1341 fprintf
(out
, "%s %d\n", main_input_filename
, complexity
);
1344 /* Reset global status used by the report functions. */
1349 previous_output
= 0;
1350 package_name
= NULL
;
1351 current_class_context
= NULL
;
1356 yyerror (const char *msg ATTRIBUTE_UNUSED
)
1358 fprintf
(stderr
, "%s: %s\n", main_input_filename
, msg
);
1363 /* Depending on the version of Bison used to compile this grammar,
1364 it may issue generic diagnostics spelled "syntax error" or
1365 "parse error". To prevent this from changing the translation
1366 template randomly, we list all the variants of this particular
1367 diagnostic here. Translators: there is no fine distinction
1368 between diagnostics with "syntax error" in them, and diagnostics
1369 with "parse error" in them. It's okay to give them both the same
1371 const char d1
[] = N_
("syntax error");
1372 const char d2
[] = N_
("parse error");
1373 const char d3
[] = N_
("syntax error; also virtual memory exhausted");
1374 const char d4
[] = N_
("parse error; also virtual memory exhausted");
1375 const char d5
[] = N_
("syntax error: cannot back up");
1376 const char d6
[] = N_
("parse error: cannot back up");