Add PR target/20673 to previous changelog.
[official-gcc.git] / gcc / java / parse-scan.y
blobdceacb6cc7f2a6fa9aa800e1727616eae86388bd
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)
11 any later version.
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. */
39 #define JC1_LITE
41 #include "config.h"
42 #include "system.h"
43 #include "coretypes.h"
44 #include "tm.h"
45 #include "obstack.h"
46 #include "toplev.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 */
59 int java_error_count;
60 int java_warning_count;
62 /* Tweak default rules when necessary. */
63 static int absorber;
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
79 id. */
80 static int bracket_count;
82 /* Numbers anonymous classes */
83 static int anonymous_count;
85 /* This is used to record the current class context. */
86 struct class_context
88 char *name;
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;
104 const char *args;
106 #define NEW_METHOD_DECLARATOR(D,N,A) \
108 (D) = xmalloc (sizeof (struct method_declarator)); \
109 (D)->method_name = (N); \
110 (D)->args = (A); \
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);
122 void report (void);
124 #include "lex.h"
125 #include "parse.h"
128 %union {
129 char *node;
130 struct method_declarator *declarator;
131 int value; /* For modifiers */
135 extern int flag_assert;
137 #include "lex.c"
140 %pure_parser
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
154 %token REM_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
165 %token STRICT_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
182 %token ASSERT_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
189 %token ID_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 */
207 goal:
208 compilation_unit
211 /* 19.3 Productions from 3: Lexical structure */
212 literal:
213 INT_LIT_TK
214 | FP_LIT_TK
215 | BOOL_LIT_TK
216 | CHAR_LIT_TK
217 | STRING_LIT_TK
218 | NULL_TK
221 /* 19.4 Productions from 4: Types, Values and Variables */
222 type:
223 primitive_type
224 | reference_type
227 primitive_type:
228 INTEGRAL_TK
230 /* use preset global here. FIXME */
231 $$ = xstrdup ("int");
233 | FP_TK
235 /* use preset global here. FIXME */
236 $$ = xstrdup ("double");
238 | BOOLEAN_TK
240 /* use preset global here. FIXME */
241 $$ = xstrdup ("boolean");
245 reference_type:
246 class_or_interface_type
247 | array_type
250 class_or_interface_type:
251 name
254 class_type:
255 class_or_interface_type /* Default rule */
258 interface_type:
259 class_or_interface_type
262 array_type:
263 primitive_type dims
265 while (bracket_count-- > 0)
266 $$ = concat ("[", $1, NULL);
268 | name dims
270 while (bracket_count-- > 0)
271 $$ = concat ("[", $1, NULL);
275 /* 19.5 Productions from 6: Names */
276 name:
277 simple_name /* Default rule */
278 | qualified_name /* Default rule */
281 simple_name:
282 identifier /* Default rule */
285 qualified_name:
286 name DOT_TK identifier
288 $$ = concat ($1, ".", $3, NULL);
292 identifier:
293 ID_TK
296 /* 19.6: Production from 7: Packages */
297 compilation_unit:
298 | package_declaration
299 | import_declarations
300 | type_declarations
301 | package_declaration import_declarations
302 | package_declaration type_declarations
303 | import_declarations type_declarations
304 | package_declaration import_declarations type_declarations
307 import_declarations:
308 import_declaration
309 | import_declarations import_declaration
312 type_declarations:
313 type_declaration
314 | type_declarations type_declaration
317 package_declaration:
318 PACKAGE_TK name SC_TK
319 { package_name = $2; }
322 import_declaration:
323 single_type_import_declaration
324 | type_import_on_demand_declaration
327 single_type_import_declaration:
328 IMPORT_TK name SC_TK
331 type_import_on_demand_declaration:
332 IMPORT_TK name DOT_TK MULT_TK SC_TK
335 type_declaration:
336 class_declaration
337 | interface_declaration
338 | empty_statement
341 /* 19.7 Shortened from the original:
342 modifiers: modifier | modifiers modifier
343 modifier: any of public... */
344 modifiers:
345 MODIFIER_TK
347 if ($1 == PUBLIC_TK)
348 modifier_value++;
349 if ($1 == STATIC_TK)
350 modifier_value++;
351 USE_ABSORBER;
353 | modifiers MODIFIER_TK
355 if ($2 == PUBLIC_TK)
356 modifier_value++;
357 if ($2 == STATIC_TK)
358 modifier_value++;
359 USE_ABSORBER;
363 /* 19.8.1 Production from $8.1: Class Declaration */
364 class_declaration:
365 modifiers CLASS_TK identifier super interfaces
367 report_class_declaration($3);
368 modifier_value = 0;
370 class_body
371 | CLASS_TK identifier super interfaces
372 { report_class_declaration($2); }
373 class_body
376 super:
377 | EXTENDS_TK class_type
380 interfaces:
381 | IMPLEMENTS_TK interface_type_list
384 interface_type_list:
385 interface_type
386 { USE_ABSORBER; }
387 | interface_type_list C_TK interface_type
388 { USE_ABSORBER; }
391 class_body:
392 OCB_TK CCB_TK
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
405 | static_initializer
406 | constructor_declaration
407 | block /* Added, JDK1.1, instance initializer */
410 class_member_declaration:
411 field_declaration
412 | method_declaration
413 | class_declaration /* Added, JDK1.1 inner classes */
414 | interface_declaration /* Added, JDK1.1 inner classes */
415 | empty_statement
418 /* 19.8.2 Productions from 8.3: Field Declarations */
419 field_declaration:
420 type variable_declarators SC_TK
421 { USE_ABSORBER; }
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
432 variable_declarator:
433 variable_declarator_id
434 | variable_declarator_id ASSIGN_TK variable_initializer
437 variable_declarator_id:
438 identifier
439 { bracket_count = 0; USE_ABSORBER; }
440 | variable_declarator_id OSB_TK CSB_TK
441 { ++bracket_count; }
444 variable_initializer:
445 expression
446 | array_initializer
449 /* 19.8.3 Productions from 8.4: Method Declarations */
450 method_declaration:
451 method_header
452 { ++method_depth; }
453 method_body
454 { --method_depth; }
457 method_header:
458 type method_declarator throws
459 { USE_ABSORBER; }
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);
466 modifier_value = 0;
470 method_declarator:
471 identifier OP_TK CP_TK
473 struct method_declarator *d;
474 NEW_METHOD_DECLARATOR (d, $1, NULL);
475 $$ = d;
477 | identifier OP_TK formal_parameter_list CP_TK
479 struct method_declarator *d;
480 NEW_METHOD_DECLARATOR (d, $1, $3);
481 $$ = d;
483 | method_declarator OSB_TK CSB_TK
486 formal_parameter_list:
487 formal_parameter
488 | formal_parameter_list C_TK formal_parameter
490 $$ = concat ($1, ",", $3, NULL);
494 formal_parameter:
495 type variable_declarator_id
497 USE_ABSORBER;
498 if (bracket_count)
500 int i;
501 char *n = xmalloc (bracket_count + 1 + strlen ($$));
502 for (i = 0; i < bracket_count; ++i)
503 n[i] = '[';
504 strcpy (n + bracket_count, $$);
505 $$ = n;
507 else
508 $$ = $1;
510 | modifiers type variable_declarator_id /* Added, JDK1.1 final locals */
512 if (bracket_count)
514 int i;
515 char *n = xmalloc (bracket_count + 1 + strlen ($2));
516 for (i = 0; i < bracket_count; ++i)
517 n[i] = '[';
518 strcpy (n + bracket_count, $2);
519 $$ = n;
521 else
522 $$ = $2;
526 throws:
527 | THROWS_TK class_type_list
530 class_type_list:
531 class_type
532 { USE_ABSORBER; }
533 | class_type_list C_TK class_type
534 { USE_ABSORBER; }
537 method_body:
538 block
539 | SC_TK
542 /* 19.8.4 Productions from 8.5: Static Initializers */
543 static_initializer:
544 static block
547 static: /* Test lval.sub_token here */
548 MODIFIER_TK
549 { USE_ABSORBER; }
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
572 { USE_ABSORBER; }
573 | simple_name OP_TK formal_parameter_list CP_TK
574 { USE_ABSORBER; }
577 constructor_body:
578 OCB_TK CCB_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
591 { USE_ABSORBER; }
592 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
593 { USE_ABSORBER; }
596 this_or_super: /* Added, simplifies error diagnostics */
597 THIS_TK
598 | SUPER_TK
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; }
606 interface_body
607 | modifiers INTERFACE_TK identifier
608 { report_class_declaration ($3); modifier_value = 0; }
609 interface_body
610 | INTERFACE_TK identifier extends_interfaces
611 { report_class_declaration ($2); modifier_value = 0; }
612 interface_body
613 | modifiers INTERFACE_TK identifier extends_interfaces
614 { report_class_declaration ($3); modifier_value = 0; }
615 interface_body
618 extends_interfaces:
619 EXTENDS_TK interface_type
620 | extends_interfaces C_TK interface_type
623 interface_body:
624 OCB_TK CCB_TK
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:
636 constant_declaration
637 | abstract_method_declaration
638 | class_declaration /* Added, JDK1.1 inner classes */
639 | interface_declaration /* Added, JDK1.1 inner classes */
642 constant_declaration:
643 field_declaration
646 abstract_method_declaration:
647 method_header SC_TK
650 /* 19.10 Productions from 10: Arrays */
651 array_initializer:
652 OCB_TK CCB_TK
653 | OCB_TK variable_initializers CCB_TK
654 | OCB_TK C_TK CCB_TK
655 | OCB_TK variable_initializers C_TK CCB_TK
658 variable_initializers:
659 variable_initializer
660 | variable_initializers C_TK variable_initializer
663 /* 19.11 Production from 14: Blocks and Statements */
664 block:
665 OCB_TK CCB_TK
666 | OCB_TK block_statements CCB_TK
669 block_statements:
670 block_statement
671 | block_statements block_statement
674 block_statement:
675 local_variable_declaration_statement
676 | statement
677 | class_declaration /* Added, JDK1.1 inner classes */
680 local_variable_declaration_statement:
681 local_variable_declaration SC_TK /* Can't catch missing ';' here */
684 local_variable_declaration:
685 type variable_declarators
686 { USE_ABSORBER; }
687 | modifiers type variable_declarators /* Added, JDK1.1 final locals */
688 { modifier_value = 0; }
691 statement:
692 statement_without_trailing_substatement
693 | labeled_statement
694 | if_then_statement
695 | if_then_else_statement
696 | while_statement
697 | for_statement
700 statement_nsi:
701 statement_without_trailing_substatement
702 | labeled_statement_nsi
703 | if_then_else_statement_nsi
704 | while_statement_nsi
705 | for_statement_nsi
708 statement_without_trailing_substatement:
709 block
710 | empty_statement
711 | expression_statement
712 | switch_statement
713 | do_statement
714 | break_statement
715 | continue_statement
716 | return_statement
717 | synchronized_statement
718 | throw_statement
719 | try_statement
720 | assert_statement
723 empty_statement:
724 SC_TK
727 label_decl:
728 identifier REL_CL_TK
729 { USE_ABSORBER; }
732 labeled_statement:
733 label_decl statement
736 labeled_statement_nsi:
737 label_decl statement_nsi
740 /* We concentrate here a bunch of error handling rules that we couldn't write
741 earlier, because expression_statement catches a missing ';'. */
742 expression_statement:
743 statement_expression SC_TK
746 statement_expression:
747 assignment
748 | pre_increment_expression
749 | pre_decrement_expression
750 | post_increment_expression
751 | post_decrement_expression
752 | method_invocation
753 | class_instance_creation_expression
756 if_then_statement:
757 IF_TK OP_TK expression CP_TK statement { ++complexity; }
760 if_then_else_statement:
761 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
762 { ++complexity; }
765 if_then_else_statement_nsi:
766 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
767 { ++complexity; }
770 switch_statement:
771 SWITCH_TK OP_TK expression CP_TK switch_block
774 switch_block:
775 OCB_TK CCB_TK
776 | OCB_TK switch_labels CCB_TK
777 | OCB_TK switch_block_statement_groups CCB_TK
778 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
781 switch_block_statement_groups:
782 switch_block_statement_group
783 | switch_block_statement_groups switch_block_statement_group
786 switch_block_statement_group:
787 switch_labels block_statements { ++complexity; }
791 switch_labels:
792 switch_label
793 | switch_labels switch_label
796 switch_label:
797 CASE_TK constant_expression REL_CL_TK
798 | DEFAULT_TK REL_CL_TK
801 while_expression:
802 WHILE_TK OP_TK expression CP_TK { ++complexity; }
805 while_statement:
806 while_expression statement
809 while_statement_nsi:
810 while_expression statement_nsi
813 do_statement_begin:
814 DO_TK
817 do_statement:
818 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
819 { ++complexity; }
822 for_statement:
823 for_begin SC_TK expression SC_TK for_update CP_TK statement
824 | for_begin SC_TK SC_TK for_update CP_TK statement
827 for_statement_nsi:
828 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
829 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
832 for_header:
833 FOR_TK OP_TK
836 for_begin:
837 for_header for_init { ++complexity; }
839 for_init: /* Can be empty */
840 | statement_expression_list
841 | local_variable_declaration
844 for_update: /* Can be empty */
845 | statement_expression_list
848 statement_expression_list:
849 statement_expression
850 | statement_expression_list C_TK statement_expression
853 break_statement:
854 BREAK_TK SC_TK
855 | BREAK_TK identifier SC_TK
858 /* `continue' with a label is considered for complexity but ordinary
859 continue is not. */
860 continue_statement:
861 CONTINUE_TK SC_TK
862 | CONTINUE_TK identifier SC_TK { ++complexity; }
865 return_statement:
866 RETURN_TK SC_TK
867 | RETURN_TK expression SC_TK
870 throw_statement:
871 THROW_TK expression SC_TK { ++complexity; }
874 assert_statement:
875 ASSERT_TK expression REL_CL_TK expression SC_TK
876 | ASSERT_TK expression SC_TK
877 | ASSERT_TK error
878 {yyerror ("Missing term"); RECOVER;}
879 | ASSERT_TK expression error
880 {yyerror ("';' expected"); RECOVER;}
882 synchronized_statement:
883 synchronized OP_TK expression CP_TK block
884 | synchronized OP_TK expression CP_TK error
887 synchronized: /* Test lval.sub_token here */
888 MODIFIER_TK
889 { USE_ABSORBER; }
892 try_statement:
893 TRY_TK block catches
894 | TRY_TK block finally
895 | TRY_TK block catches finally
898 catches:
899 catch_clause
900 | catches catch_clause
903 catch_clause:
904 CATCH_TK OP_TK formal_parameter CP_TK block { ++complexity; }
907 finally:
908 FINALLY_TK block { ++complexity; }
911 /* 19.12 Production from 15: Expressions */
912 primary:
913 primary_no_new_array
914 | array_creation_expression
917 primary_no_new_array:
918 literal
919 | THIS_TK
920 | OP_TK expression CP_TK
921 | class_instance_creation_expression
922 | field_access
923 | method_invocation
924 | array_access
925 | type_literals
926 /* Added, JDK1.1 inner classes. Documentation is wrong
927 referring to a 'ClassName' (class_name) rule that doesn't
928 exist. Used name instead. */
929 | name DOT_TK THIS_TK
930 { USE_ABSORBER; }
933 type_literals:
934 name DOT_TK CLASS_TK
935 { USE_ABSORBER; }
936 | array_type DOT_TK CLASS_TK
937 { USE_ABSORBER; }
938 | primitive_type DOT_TK CLASS_TK
939 { USE_ABSORBER; }
940 | VOID_TK DOT_TK CLASS_TK
941 { USE_ABSORBER; }
944 class_instance_creation_expression:
945 NEW_TK class_type OP_TK argument_list CP_TK
946 | NEW_TK class_type OP_TK CP_TK
947 | anonymous_class_creation
948 | something_dot_new identifier OP_TK CP_TK
949 | something_dot_new identifier OP_TK CP_TK class_body
950 | something_dot_new identifier OP_TK argument_list CP_TK
951 | something_dot_new identifier OP_TK argument_list CP_TK class_body
954 anonymous_class_creation:
955 NEW_TK class_type OP_TK CP_TK
956 { report_class_declaration (anonymous_context); }
957 class_body
958 | NEW_TK class_type OP_TK argument_list CP_TK
959 { report_class_declaration (anonymous_context); }
960 class_body
963 something_dot_new: /* Added, not part of the specs. */
964 name DOT_TK NEW_TK
965 { USE_ABSORBER; }
966 | primary DOT_TK NEW_TK
969 argument_list:
970 expression
971 | argument_list C_TK expression
972 | argument_list C_TK error
975 array_creation_expression:
976 NEW_TK primitive_type dim_exprs
977 | NEW_TK class_or_interface_type dim_exprs
978 | NEW_TK primitive_type dim_exprs dims
979 | NEW_TK class_or_interface_type dim_exprs dims
980 /* Added, JDK1.1 anonymous array. Initial documentation rule
981 modified */
982 | NEW_TK class_or_interface_type dims array_initializer
983 | NEW_TK primitive_type dims array_initializer
986 dim_exprs:
987 dim_expr
988 | dim_exprs dim_expr
991 dim_expr:
992 OSB_TK expression CSB_TK
995 dims:
996 OSB_TK CSB_TK
997 { bracket_count = 1; }
998 | dims OSB_TK CSB_TK
999 { bracket_count++; }
1002 field_access:
1003 primary DOT_TK identifier
1004 | SUPER_TK DOT_TK identifier
1007 /* We include method invocation in the complexity measure on the
1008 theory that most method calls are virtual and therefore involve a
1009 decision point. */
1010 method_invocation:
1011 name OP_TK CP_TK
1012 { USE_ABSORBER; ++complexity; }
1013 | name OP_TK argument_list CP_TK
1014 { USE_ABSORBER; ++complexity; }
1015 | primary DOT_TK identifier OP_TK CP_TK { ++complexity; }
1016 | primary DOT_TK identifier OP_TK argument_list CP_TK { ++complexity; }
1017 | SUPER_TK DOT_TK identifier OP_TK CP_TK { ++complexity; }
1018 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK { ++complexity; }
1021 array_access:
1022 name OSB_TK expression CSB_TK
1023 { USE_ABSORBER; }
1024 | primary_no_new_array OSB_TK expression CSB_TK
1027 postfix_expression:
1028 primary
1029 | name
1030 { USE_ABSORBER; }
1031 | post_increment_expression
1032 | post_decrement_expression
1035 post_increment_expression:
1036 postfix_expression INCR_TK
1039 post_decrement_expression:
1040 postfix_expression DECR_TK
1043 unary_expression:
1044 pre_increment_expression
1045 | pre_decrement_expression
1046 | PLUS_TK unary_expression
1047 | MINUS_TK unary_expression
1048 | unary_expression_not_plus_minus
1051 pre_increment_expression:
1052 INCR_TK unary_expression
1055 pre_decrement_expression:
1056 DECR_TK unary_expression
1059 unary_expression_not_plus_minus:
1060 postfix_expression
1061 | NOT_TK unary_expression
1062 | NEG_TK unary_expression
1063 | cast_expression
1066 cast_expression: /* Error handling here is potentially weak */
1067 OP_TK primitive_type dims CP_TK unary_expression
1068 | OP_TK primitive_type CP_TK unary_expression
1069 | OP_TK expression CP_TK unary_expression_not_plus_minus
1070 | OP_TK name dims CP_TK unary_expression_not_plus_minus
1073 multiplicative_expression:
1074 unary_expression
1075 | multiplicative_expression MULT_TK unary_expression
1076 | multiplicative_expression DIV_TK unary_expression
1077 | multiplicative_expression REM_TK unary_expression
1080 additive_expression:
1081 multiplicative_expression
1082 | additive_expression PLUS_TK multiplicative_expression
1083 | additive_expression MINUS_TK multiplicative_expression
1086 shift_expression:
1087 additive_expression
1088 | shift_expression LS_TK additive_expression
1089 | shift_expression SRS_TK additive_expression
1090 | shift_expression ZRS_TK additive_expression
1093 relational_expression:
1094 shift_expression
1095 | relational_expression LT_TK shift_expression
1096 | relational_expression GT_TK shift_expression
1097 | relational_expression LTE_TK shift_expression
1098 | relational_expression GTE_TK shift_expression
1099 | relational_expression INSTANCEOF_TK reference_type
1102 equality_expression:
1103 relational_expression
1104 | equality_expression EQ_TK relational_expression
1105 | equality_expression NEQ_TK relational_expression
1108 and_expression:
1109 equality_expression
1110 | and_expression AND_TK equality_expression
1113 exclusive_or_expression:
1114 and_expression
1115 | exclusive_or_expression XOR_TK and_expression
1118 inclusive_or_expression:
1119 exclusive_or_expression
1120 | inclusive_or_expression OR_TK exclusive_or_expression
1123 conditional_and_expression:
1124 inclusive_or_expression
1125 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
1126 { ++complexity; }
1129 conditional_or_expression:
1130 conditional_and_expression
1131 | conditional_or_expression BOOL_OR_TK conditional_and_expression
1132 { ++complexity; }
1135 conditional_expression: /* Error handling here is weak */
1136 conditional_or_expression
1137 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
1138 { ++complexity; }
1141 assignment_expression:
1142 conditional_expression
1143 | assignment
1146 assignment:
1147 left_hand_side assignment_operator assignment_expression
1150 left_hand_side:
1151 name
1152 { USE_ABSORBER; }
1153 | field_access
1154 | array_access
1157 assignment_operator:
1158 ASSIGN_ANY_TK
1159 | ASSIGN_TK
1162 expression:
1163 assignment_expression
1166 constant_expression:
1167 expression
1172 /* Create a new parser context */
1174 void
1175 java_push_parser_context (void)
1177 struct parser_ctxt *new = xcalloc (1, sizeof (struct parser_ctxt));
1179 new->next = ctxp;
1180 ctxp = new;
1183 static void
1184 push_class_context (const char *name)
1186 struct class_context *ctx;
1188 ctx = xmalloc (sizeof (struct class_context));
1189 ctx->name = (char *) name;
1190 ctx->next = current_class_context;
1191 current_class_context = ctx;
1194 static void
1195 pop_class_context (void)
1197 struct class_context *ctx;
1199 if (current_class_context == NULL)
1200 return;
1202 ctx = current_class_context->next;
1203 if (current_class_context->name != anonymous_context)
1204 free (current_class_context->name);
1205 free (current_class_context);
1207 current_class_context = ctx;
1208 if (current_class_context == NULL)
1209 anonymous_count = 0;
1212 /* Recursively construct the class name. This is just a helper
1213 function for get_class_name(). */
1214 static int
1215 make_class_name_recursive (struct obstack *stack, struct class_context *ctx)
1217 if (! ctx)
1218 return 0;
1220 make_class_name_recursive (stack, ctx->next);
1222 /* Replace an anonymous context with the appropriate counter value. */
1223 if (ctx->name == anonymous_context)
1225 char buf[50];
1226 ++anonymous_count;
1227 sprintf (buf, "%d", anonymous_count);
1228 ctx->name = xstrdup (buf);
1231 obstack_grow (stack, ctx->name, strlen (ctx->name));
1232 obstack_1grow (stack, '$');
1234 return ISDIGIT (ctx->name[0]);
1237 /* Return a newly allocated string holding the name of the class. */
1238 static char *
1239 get_class_name (void)
1241 char *result;
1242 int last_was_digit;
1243 struct obstack name_stack;
1245 obstack_init (&name_stack);
1247 /* Duplicate the logic of parse.y:maybe_make_nested_class_name(). */
1248 last_was_digit = make_class_name_recursive (&name_stack,
1249 current_class_context->next);
1251 if (! last_was_digit
1252 && method_depth
1253 && current_class_context->name != anonymous_context)
1255 char buf[50];
1256 ++anonymous_count;
1257 sprintf (buf, "%d", anonymous_count);
1258 obstack_grow (&name_stack, buf, strlen (buf));
1259 obstack_1grow (&name_stack, '$');
1262 if (current_class_context->name == anonymous_context)
1264 char buf[50];
1265 ++anonymous_count;
1266 sprintf (buf, "%d", anonymous_count);
1267 current_class_context->name = xstrdup (buf);
1268 obstack_grow0 (&name_stack, buf, strlen (buf));
1270 else
1271 obstack_grow0 (&name_stack, current_class_context->name,
1272 strlen (current_class_context->name));
1274 result = xstrdup (obstack_finish (&name_stack));
1275 obstack_free (&name_stack, NULL);
1277 return result;
1280 /* Actions defined here */
1282 static void
1283 report_class_declaration (const char * name)
1285 extern int flag_dump_class, flag_list_filename;
1287 push_class_context (name);
1288 if (flag_dump_class)
1290 char *name = get_class_name ();
1292 if (!previous_output)
1294 if (flag_list_filename)
1295 fprintf (out, "%s: ", main_input_filename);
1296 previous_output = 1;
1299 if (package_name)
1300 fprintf (out, "%s.%s ", package_name, name);
1301 else
1302 fprintf (out, "%s ", name);
1304 free (name);
1308 static void
1309 report_main_declaration (struct method_declarator *declarator)
1311 extern int flag_find_main;
1313 if (flag_find_main
1314 && modifier_value == 2
1315 && !strcmp (declarator->method_name, "main")
1316 && declarator->args
1317 && declarator->args [0] == '['
1318 && (! strcmp (declarator->args+1, "String")
1319 || ! strcmp (declarator->args + 1, "java.lang.String"))
1320 && current_class_context)
1322 if (!previous_output)
1324 char *name = get_class_name ();
1325 if (package_name)
1326 fprintf (out, "%s.%s ", package_name, name);
1327 else
1328 fprintf (out, "%s", name);
1329 free (name);
1330 previous_output = 1;
1335 void
1336 report (void)
1338 extern int flag_complexity;
1339 if (flag_complexity)
1340 fprintf (out, "%s %d\n", main_input_filename, complexity);
1343 /* Reset global status used by the report functions. */
1345 void
1346 reset_report (void)
1348 previous_output = 0;
1349 package_name = NULL;
1350 current_class_context = NULL;
1351 complexity = 0;
1354 void
1355 yyerror (const char *msg ATTRIBUTE_UNUSED)
1357 fprintf (stderr, "%s: %s\n", main_input_filename, msg);
1358 exit (1);
1361 #ifdef __XGETTEXT__
1362 /* Depending on the version of Bison used to compile this grammar,
1363 it may issue generic diagnostics spelled "syntax error" or
1364 "parse error". To prevent this from changing the translation
1365 template randomly, we list all the variants of this particular
1366 diagnostic here. Translators: there is no fine distinction
1367 between diagnostics with "syntax error" in them, and diagnostics
1368 with "parse error" in them. It's okay to give them both the same
1369 translation. */
1370 const char d1[] = N_("syntax error");
1371 const char d2[] = N_("parse error");
1372 const char d3[] = N_("syntax error; also virtual memory exhausted");
1373 const char d4[] = N_("parse error; also virtual memory exhausted");
1374 const char d5[] = N_("syntax error: cannot back up");
1375 const char d6[] = N_("parse error: cannot back up");
1376 #endif