Add x prefix to v850e case for handling --with-cpu=v850e.
[official-gcc.git] / gcc / java / parse-scan.y
blob977bbce226d77b0b3e0a24b764e9f895b8f60959
1 /* Parser grammar for quick source code scan of Java(TM) language programs.
2 Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
3 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* This file parses Java source code. Action can be further completed
27 to achieve a desired behavior. This file isn't part of the Java
28 language gcc front end.
30 The grammar conforms to the Java grammar described in "The Java(TM)
31 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
32 1996, ISBN 0-201-63451-1"
34 Some rules have been modified to support JDK1.1 inner classes
35 definitions and other extensions. */
38 #define JC1_LITE
40 #include "config.h"
41 #include "system.h"
43 #include "obstack.h"
44 #include "toplev.h"
46 #define obstack_chunk_alloc xmalloc
47 #define obstack_chunk_free free
49 extern char *input_filename;
50 extern FILE *finput, *out;
52 /* Obstack for the lexer. */
53 struct obstack temporary_obstack;
55 /* The current parser context. */
56 static struct parser_ctxt *ctxp;
58 /* Error and warning counts, current line number, because they're used
59 elsewhere */
60 int java_error_count;
61 int java_warning_count;
62 int lineno;
64 /* Tweak default rules when necessary. */
65 static int absorber;
66 #define USE_ABSORBER absorber = 0
68 /* Keep track of the current package name. */
69 static const char *package_name;
71 /* Keep track of whether things have be listed before. */
72 static int previous_output;
74 /* Record modifier uses */
75 static int modifier_value;
77 /* Record (almost) cyclomatic complexity. */
78 static int complexity;
80 /* Keeps track of number of bracket pairs after a variable declarator
81 id. */
82 static int bracket_count;
84 /* Numbers anonymous classes */
85 static int anonymous_count;
87 /* This is used to record the current class context. */
88 struct class_context
90 char *name;
91 struct class_context *next;
94 /* The global class context. */
95 static struct class_context *current_class_context;
97 /* A special constant used to represent an anonymous context. */
98 static const char *anonymous_context = "ANONYMOUS";
100 /* Count of method depth. */
101 static int method_depth;
103 /* Record a method declaration */
104 struct method_declarator {
105 const char *method_name;
106 const char *args;
108 #define NEW_METHOD_DECLARATOR(D,N,A) \
110 (D) = \
111 (struct method_declarator *)xmalloc (sizeof (struct method_declarator)); \
112 (D)->method_name = (N); \
113 (D)->args = (A); \
116 /* Two actions for this grammar */
117 static int make_class_name_recursive PARAMS ((struct obstack *stack,
118 struct class_context *ctx));
119 static char *get_class_name PARAMS ((void));
120 static void report_class_declaration PARAMS ((const char *));
121 static void report_main_declaration PARAMS ((struct method_declarator *));
122 static void push_class_context PARAMS ((const char *));
123 static void pop_class_context PARAMS ((void));
125 void report PARAMS ((void));
127 #include "lex.h"
128 #include "parse.h"
131 %union {
132 char *node;
133 struct method_declarator *declarator;
134 int value; /* For modifiers */
138 #include "lex.c"
141 %pure_parser
143 /* Things defined here have to match the order of what's in the
144 binop_lookup table. */
146 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
147 %token LS_TK SRS_TK ZRS_TK
148 %token AND_TK XOR_TK OR_TK
149 %token BOOL_AND_TK BOOL_OR_TK
150 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
152 /* This maps to the same binop_lookup entry than the token above */
154 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
155 %token REM_ASSIGN_TK
156 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
157 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
160 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
162 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
163 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
164 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
165 %token PAD_TK ABSTRACT_TK MODIFIER_TK
166 %token STRICT_TK
168 /* Keep those two in order, too */
169 %token DECR_TK INCR_TK
171 /* From now one, things can be in any order */
173 %token DEFAULT_TK IF_TK THROW_TK
174 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
175 %token THROWS_TK BREAK_TK IMPORT_TK
176 %token ELSE_TK INSTANCEOF_TK RETURN_TK
177 %token VOID_TK CATCH_TK INTERFACE_TK
178 %token CASE_TK EXTENDS_TK FINALLY_TK
179 %token SUPER_TK WHILE_TK CLASS_TK
180 %token SWITCH_TK CONST_TK TRY_TK
181 %token FOR_TK NEW_TK CONTINUE_TK
182 %token GOTO_TK PACKAGE_TK THIS_TK
183 %token ASSERT_TK
185 %token BYTE_TK SHORT_TK INT_TK LONG_TK
186 %token CHAR_TK INTEGRAL_TK
188 %token FLOAT_TK DOUBLE_TK FP_TK
190 %token ID_TK
192 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
194 %token ASSIGN_ANY_TK ASSIGN_TK
195 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
197 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
198 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
200 %type <node> ID_TK identifier name simple_name qualified_name type
201 primitive_type reference_type array_type formal_parameter_list
202 formal_parameter class_or_interface_type class_type interface_type
203 %type <declarator> method_declarator
204 %type <value> MODIFIER_TK
207 /* 19.2 Production from 2.3: The Syntactic Grammar */
208 goal:
209 compilation_unit
212 /* 19.3 Productions from 3: Lexical structure */
213 literal:
214 INT_LIT_TK
215 | FP_LIT_TK
216 | BOOL_LIT_TK
217 | CHAR_LIT_TK
218 | STRING_LIT_TK
219 | NULL_TK
222 /* 19.4 Productions from 4: Types, Values and Variables */
223 type:
224 primitive_type
225 | reference_type
228 primitive_type:
229 INTEGRAL_TK
231 /* use preset global here. FIXME */
232 $$ = xstrdup ("int");
234 | FP_TK
236 /* use preset global here. FIXME */
237 $$ = xstrdup ("double");
239 | BOOLEAN_TK
241 /* use preset global here. FIXME */
242 $$ = xstrdup ("boolean");
246 reference_type:
247 class_or_interface_type
248 | array_type
251 class_or_interface_type:
252 name
255 class_type:
256 class_or_interface_type /* Default rule */
259 interface_type:
260 class_or_interface_type
263 array_type:
264 primitive_type dims
266 while (bracket_count-- > 0)
267 $$ = concat ("[", $1, NULL);
269 | name dims
271 while (bracket_count-- > 0)
272 $$ = concat ("[", $1, NULL);
276 /* 19.5 Productions from 6: Names */
277 name:
278 simple_name /* Default rule */
279 | qualified_name /* Default rule */
282 simple_name:
283 identifier /* Default rule */
286 qualified_name:
287 name DOT_TK identifier
289 $$ = concat ($1, ".", $3, NULL);
293 identifier:
294 ID_TK
297 /* 19.6: Production from 7: Packages */
298 compilation_unit:
299 | package_declaration
300 | import_declarations
301 | type_declarations
302 | package_declaration import_declarations
303 | package_declaration type_declarations
304 | import_declarations type_declarations
305 | package_declaration import_declarations type_declarations
308 import_declarations:
309 import_declaration
310 | import_declarations import_declaration
313 type_declarations:
314 type_declaration
315 | type_declarations type_declaration
318 package_declaration:
319 PACKAGE_TK name SC_TK
320 { package_name = $2; }
323 import_declaration:
324 single_type_import_declaration
325 | type_import_on_demand_declaration
328 single_type_import_declaration:
329 IMPORT_TK name SC_TK
332 type_import_on_demand_declaration:
333 IMPORT_TK name DOT_TK MULT_TK SC_TK
336 type_declaration:
337 class_declaration
338 | interface_declaration
339 | empty_statement
342 /* 19.7 Shortened from the original:
343 modifiers: modifier | modifiers modifier
344 modifier: any of public... */
345 modifiers:
346 MODIFIER_TK
348 if ($1 == PUBLIC_TK)
349 modifier_value++;
350 if ($1 == STATIC_TK)
351 modifier_value++;
352 USE_ABSORBER;
354 | modifiers MODIFIER_TK
356 if ($2 == PUBLIC_TK)
357 modifier_value++;
358 if ($2 == STATIC_TK)
359 modifier_value++;
360 USE_ABSORBER;
364 /* 19.8.1 Production from $8.1: Class Declaration */
365 class_declaration:
366 modifiers CLASS_TK identifier super interfaces
368 report_class_declaration($3);
369 modifier_value = 0;
371 class_body
372 | CLASS_TK identifier super interfaces
373 { report_class_declaration($2); }
374 class_body
377 super:
378 | EXTENDS_TK class_type
381 interfaces:
382 | IMPLEMENTS_TK interface_type_list
385 interface_type_list:
386 interface_type
387 { USE_ABSORBER; }
388 | interface_type_list C_TK interface_type
389 { USE_ABSORBER; }
392 class_body:
393 OCB_TK CCB_TK
394 { pop_class_context (); }
395 | OCB_TK class_body_declarations CCB_TK
396 { pop_class_context (); }
399 class_body_declarations:
400 class_body_declaration
401 | class_body_declarations class_body_declaration
404 class_body_declaration:
405 class_member_declaration
406 | static_initializer
407 | constructor_declaration
408 | block /* Added, JDK1.1, instance initializer */
411 class_member_declaration:
412 field_declaration
413 | method_declaration
414 | class_declaration /* Added, JDK1.1 inner classes */
415 | interface_declaration /* Added, JDK1.1 inner classes */
416 | empty_statement
419 /* 19.8.2 Productions from 8.3: Field Declarations */
420 field_declaration:
421 type variable_declarators SC_TK
422 { USE_ABSORBER; }
423 | modifiers type variable_declarators SC_TK
424 { modifier_value = 0; }
427 variable_declarators:
428 /* Should we use build_decl_list () instead ? FIXME */
429 variable_declarator /* Default rule */
430 | variable_declarators C_TK variable_declarator
433 variable_declarator:
434 variable_declarator_id
435 | variable_declarator_id ASSIGN_TK variable_initializer
438 variable_declarator_id:
439 identifier
440 { bracket_count = 0; USE_ABSORBER; }
441 | variable_declarator_id OSB_TK CSB_TK
442 { ++bracket_count; }
445 variable_initializer:
446 expression
447 | array_initializer
450 /* 19.8.3 Productions from 8.4: Method Declarations */
451 method_declaration:
452 method_header
453 { ++method_depth; }
454 method_body
455 { --method_depth; }
458 method_header:
459 type method_declarator throws
460 { USE_ABSORBER; }
461 | VOID_TK method_declarator throws
462 | modifiers type method_declarator throws
463 { modifier_value = 0; }
464 | modifiers VOID_TK method_declarator throws
466 report_main_declaration ($3);
467 modifier_value = 0;
471 method_declarator:
472 identifier OP_TK CP_TK
474 struct method_declarator *d;
475 NEW_METHOD_DECLARATOR (d, $1, NULL);
476 $$ = d;
478 | identifier OP_TK formal_parameter_list CP_TK
480 struct method_declarator *d;
481 NEW_METHOD_DECLARATOR (d, $1, $3);
482 $$ = d;
484 | method_declarator OSB_TK CSB_TK
487 formal_parameter_list:
488 formal_parameter
489 | formal_parameter_list C_TK formal_parameter
491 $$ = concat ($1, ",", $3, NULL);
495 formal_parameter:
496 type variable_declarator_id
498 USE_ABSORBER;
499 if (bracket_count)
501 int i;
502 char *n = xmalloc (bracket_count + 1 + strlen ($$));
503 for (i = 0; i < bracket_count; ++i)
504 n[i] = '[';
505 strcpy (n + bracket_count, $$);
506 $$ = n;
508 else
509 $$ = $1;
511 | modifiers type variable_declarator_id /* Added, JDK1.1 final locals */
513 if (bracket_count)
515 int i;
516 char *n = xmalloc (bracket_count + 1 + strlen ($$));
517 for (i = 0; i < bracket_count; ++i)
518 n[i] = '[';
519 strcpy (n + bracket_count, $$);
520 $$ = n;
522 else
523 $$ = $2;
527 throws:
528 | THROWS_TK class_type_list
531 class_type_list:
532 class_type
533 { USE_ABSORBER; }
534 | class_type_list C_TK class_type
535 { USE_ABSORBER; }
538 method_body:
539 block
540 | SC_TK
543 /* 19.8.4 Productions from 8.5: Static Initializers */
544 static_initializer:
545 static block
548 static: /* Test lval.sub_token here */
549 MODIFIER_TK
550 { USE_ABSORBER; }
553 /* 19.8.5 Productions from 8.6: Constructor Declarations */
554 /* NOTE FOR FURTHER WORK ON CONSTRUCTORS:
555 - If a forbidded modifier is found, the the error is either the use of
556 a forbidded modifier for a constructor OR bogus attempt to declare a
557 method without having specified the return type. FIXME */
558 constructor_declaration:
559 constructor_declarator throws constructor_body
560 | modifiers constructor_declarator throws constructor_body
561 { modifier_value = 0; }
562 /* extra SC_TK, FIXME */
563 | constructor_declarator throws constructor_body SC_TK
564 /* extra SC_TK, FIXME */
565 | modifiers constructor_declarator throws constructor_body SC_TK
566 { modifier_value = 0; }
567 /* I'm not happy with the SC_TK addition. It isn't in the grammer and should
568 probably be matched by and empty statement. But it doesn't work. FIXME */
571 constructor_declarator:
572 simple_name OP_TK CP_TK
573 { USE_ABSORBER; }
574 | simple_name OP_TK formal_parameter_list CP_TK
575 { USE_ABSORBER; }
578 constructor_body:
579 OCB_TK CCB_TK
580 | OCB_TK explicit_constructor_invocation CCB_TK
581 | OCB_TK block_statements CCB_TK
582 | OCB_TK explicit_constructor_invocation block_statements CCB_TK
585 /* Error recovery for that rule moved down expression_statement: rule. */
586 explicit_constructor_invocation:
587 this_or_super OP_TK CP_TK SC_TK
588 | this_or_super OP_TK argument_list CP_TK SC_TK
589 /* Added, JDK1.1 inner classes. Modified because the rule
590 'primary' couldn't work. */
591 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
592 { USE_ABSORBER; }
593 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
594 { USE_ABSORBER; }
597 this_or_super: /* Added, simplifies error diagnostics */
598 THIS_TK
599 | SUPER_TK
602 /* 19.9 Productions from 9: Interfaces */
603 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
604 interface_declaration:
605 INTERFACE_TK identifier
606 { report_class_declaration ($2); modifier_value = 0; }
607 interface_body
608 | modifiers INTERFACE_TK identifier
609 { report_class_declaration ($3); modifier_value = 0; }
610 interface_body
611 | INTERFACE_TK identifier extends_interfaces
612 { report_class_declaration ($2); modifier_value = 0; }
613 interface_body
614 | modifiers INTERFACE_TK identifier extends_interfaces
615 { report_class_declaration ($3); modifier_value = 0; }
616 interface_body
619 extends_interfaces:
620 EXTENDS_TK interface_type
621 | extends_interfaces C_TK interface_type
624 interface_body:
625 OCB_TK CCB_TK
626 { pop_class_context (); }
627 | OCB_TK interface_member_declarations CCB_TK
628 { pop_class_context (); }
631 interface_member_declarations:
632 interface_member_declaration
633 | interface_member_declarations interface_member_declaration
636 interface_member_declaration:
637 constant_declaration
638 | abstract_method_declaration
639 | class_declaration /* Added, JDK1.1 inner classes */
640 | interface_declaration /* Added, JDK1.1 inner classes */
643 constant_declaration:
644 field_declaration
647 abstract_method_declaration:
648 method_header SC_TK
651 /* 19.10 Productions from 10: Arrays */
652 array_initializer:
653 OCB_TK CCB_TK
654 | OCB_TK variable_initializers CCB_TK
655 | OCB_TK C_TK CCB_TK
656 | OCB_TK variable_initializers C_TK CCB_TK
659 variable_initializers:
660 variable_initializer
661 | variable_initializers C_TK variable_initializer
664 /* 19.11 Production from 14: Blocks and Statements */
665 block:
666 OCB_TK CCB_TK
667 | OCB_TK block_statements CCB_TK
670 block_statements:
671 block_statement
672 | block_statements block_statement
675 block_statement:
676 local_variable_declaration_statement
677 | 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
687 { USE_ABSORBER; }
688 | modifiers type variable_declarators /* Added, JDK1.1 final locals */
689 { modifier_value = 0; }
692 statement:
693 statement_without_trailing_substatement
694 | labeled_statement
695 | if_then_statement
696 | if_then_else_statement
697 | while_statement
698 | for_statement
701 statement_nsi:
702 statement_without_trailing_substatement
703 | labeled_statement_nsi
704 | if_then_else_statement_nsi
705 | while_statement_nsi
706 | for_statement_nsi
709 statement_without_trailing_substatement:
710 block
711 | empty_statement
712 | expression_statement
713 | switch_statement
714 | do_statement
715 | break_statement
716 | continue_statement
717 | return_statement
718 | synchronized_statement
719 | throw_statement
720 | try_statement
721 | assert_statement
724 empty_statement:
725 SC_TK
728 label_decl:
729 identifier REL_CL_TK
730 { USE_ABSORBER; }
733 labeled_statement:
734 label_decl 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:
748 assignment
749 | pre_increment_expression
750 | pre_decrement_expression
751 | post_increment_expression
752 | post_decrement_expression
753 | method_invocation
754 | class_instance_creation_expression
757 if_then_statement:
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
763 { ++complexity; }
766 if_then_else_statement_nsi:
767 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
768 { ++complexity; }
771 switch_statement:
772 SWITCH_TK OP_TK expression CP_TK switch_block
775 switch_block:
776 OCB_TK CCB_TK
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; }
792 switch_labels:
793 switch_label
794 | switch_labels switch_label
797 switch_label:
798 CASE_TK constant_expression REL_CL_TK
799 | DEFAULT_TK REL_CL_TK
802 while_expression:
803 WHILE_TK OP_TK expression CP_TK { ++complexity; }
806 while_statement:
807 while_expression statement
810 while_statement_nsi:
811 while_expression statement_nsi
814 do_statement_begin:
815 DO_TK
818 do_statement:
819 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
820 { ++complexity; }
823 for_statement:
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
828 for_statement_nsi:
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
833 for_header:
834 FOR_TK OP_TK
837 for_begin:
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:
850 statement_expression
851 | statement_expression_list C_TK statement_expression
854 break_statement:
855 BREAK_TK SC_TK
856 | BREAK_TK identifier SC_TK
859 /* `continue' with a label is considered for complexity but ordinary
860 continue is not. */
861 continue_statement:
862 CONTINUE_TK SC_TK
863 | CONTINUE_TK identifier SC_TK { ++complexity; }
866 return_statement:
867 RETURN_TK SC_TK
868 | RETURN_TK expression SC_TK
871 throw_statement:
872 THROW_TK expression SC_TK { ++complexity; }
875 assert_statement:
876 ASSERT_TK expression REL_CL_TK expression SC_TK
877 | ASSERT_TK expression SC_TK
878 | ASSERT_TK error
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 */
889 MODIFIER_TK
890 { USE_ABSORBER; }
893 try_statement:
894 TRY_TK block catches
895 | TRY_TK block finally
896 | TRY_TK block catches finally
899 catches:
900 catch_clause
901 | catches catch_clause
904 catch_clause:
905 CATCH_TK OP_TK formal_parameter CP_TK block { ++complexity; }
908 finally:
909 FINALLY_TK block { ++complexity; }
912 /* 19.12 Production from 15: Expressions */
913 primary:
914 primary_no_new_array
915 | array_creation_expression
918 primary_no_new_array:
919 literal
920 | THIS_TK
921 | OP_TK expression CP_TK
922 | class_instance_creation_expression
923 | field_access
924 | method_invocation
925 | array_access
926 | type_literals
927 /* Added, JDK1.1 inner classes. Documentation is wrong
928 refering to a 'ClassName' (class_name) rule that doesn't
929 exist. Used name instead. */
930 | name DOT_TK THIS_TK
931 { USE_ABSORBER; }
934 type_literals:
935 name DOT_TK CLASS_TK
936 { USE_ABSORBER; }
937 | array_type DOT_TK CLASS_TK
938 { USE_ABSORBER; }
939 | primitive_type DOT_TK CLASS_TK
940 { USE_ABSORBER; }
941 | VOID_TK DOT_TK CLASS_TK
942 { USE_ABSORBER; }
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); }
958 class_body
959 | NEW_TK class_type OP_TK argument_list CP_TK
960 { report_class_declaration (anonymous_context); }
961 class_body
964 something_dot_new: /* Added, not part of the specs. */
965 name DOT_TK NEW_TK
966 { USE_ABSORBER; }
967 | primary DOT_TK NEW_TK
970 argument_list:
971 expression
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
982 modified */
983 | NEW_TK class_or_interface_type dims array_initializer
984 | NEW_TK primitive_type dims array_initializer
987 dim_exprs:
988 dim_expr
989 | dim_exprs dim_expr
992 dim_expr:
993 OSB_TK expression CSB_TK
996 dims:
997 OSB_TK CSB_TK
998 { bracket_count = 1; }
999 | dims OSB_TK CSB_TK
1000 { bracket_count++; }
1003 field_access:
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
1010 decision point. */
1011 method_invocation:
1012 name OP_TK CP_TK
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; }
1022 array_access:
1023 name OSB_TK expression CSB_TK
1024 { USE_ABSORBER; }
1025 | primary_no_new_array OSB_TK expression CSB_TK
1028 postfix_expression:
1029 primary
1030 | name
1031 { USE_ABSORBER; }
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
1044 unary_expression:
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:
1061 postfix_expression
1062 | NOT_TK unary_expression
1063 | NEG_TK unary_expression
1064 | cast_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:
1075 unary_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
1087 shift_expression:
1088 additive_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:
1095 shift_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
1109 and_expression:
1110 equality_expression
1111 | and_expression AND_TK equality_expression
1114 exclusive_or_expression:
1115 and_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
1127 { ++complexity; }
1130 conditional_or_expression:
1131 conditional_and_expression
1132 | conditional_or_expression BOOL_OR_TK conditional_and_expression
1133 { ++complexity; }
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
1139 { ++complexity; }
1142 assignment_expression:
1143 conditional_expression
1144 | assignment
1147 assignment:
1148 left_hand_side assignment_operator assignment_expression
1151 left_hand_side:
1152 name
1153 { USE_ABSORBER; }
1154 | field_access
1155 | array_access
1158 assignment_operator:
1159 ASSIGN_ANY_TK
1160 | ASSIGN_TK
1163 expression:
1164 assignment_expression
1167 constant_expression:
1168 expression
1173 /* Create a new parser context */
1175 void
1176 java_push_parser_context ()
1178 struct parser_ctxt *new =
1179 (struct parser_ctxt *) xcalloc (1, sizeof (struct parser_ctxt));
1181 new->next = ctxp;
1182 ctxp = new;
1185 static void
1186 push_class_context (name)
1187 const char *name;
1189 struct class_context *ctx;
1191 ctx = (struct class_context *) xmalloc (sizeof (struct class_context));
1192 ctx->name = (char *) name;
1193 ctx->next = current_class_context;
1194 current_class_context = ctx;
1197 static void
1198 pop_class_context ()
1200 struct class_context *ctx;
1202 if (current_class_context == NULL)
1203 return;
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(). */
1217 static int
1218 make_class_name_recursive (stack, ctx)
1219 struct obstack *stack;
1220 struct class_context *ctx;
1222 if (! ctx)
1223 return 0;
1225 make_class_name_recursive (stack, ctx->next);
1227 /* Replace an anonymous context with the appropriate counter value. */
1228 if (ctx->name == anonymous_context)
1230 char buf[50];
1231 ++anonymous_count;
1232 sprintf (buf, "%d", anonymous_count);
1233 ctx->name = xstrdup (buf);
1236 obstack_grow (stack, ctx->name, strlen (ctx->name));
1237 obstack_1grow (stack, '$');
1239 return ISDIGIT (ctx->name[0]);
1242 /* Return a newly allocated string holding the name of the class. */
1243 static char *
1244 get_class_name ()
1246 char *result;
1247 int last_was_digit;
1248 struct obstack name_stack;
1250 obstack_init (&name_stack);
1252 /* Duplicate the logic of parse.y:maybe_make_nested_class_name(). */
1253 last_was_digit = make_class_name_recursive (&name_stack,
1254 current_class_context->next);
1256 if (! last_was_digit
1257 && method_depth
1258 && current_class_context->name != anonymous_context)
1260 char buf[50];
1261 ++anonymous_count;
1262 sprintf (buf, "%d", anonymous_count);
1263 obstack_grow (&name_stack, buf, strlen (buf));
1264 obstack_1grow (&name_stack, '$');
1267 if (current_class_context->name == anonymous_context)
1269 char buf[50];
1270 ++anonymous_count;
1271 sprintf (buf, "%d", anonymous_count);
1272 current_class_context->name = xstrdup (buf);
1273 obstack_grow0 (&name_stack, buf, strlen (buf));
1275 else
1276 obstack_grow0 (&name_stack, current_class_context->name,
1277 strlen (current_class_context->name));
1279 result = xstrdup (obstack_finish (&name_stack));
1280 obstack_free (&name_stack, NULL);
1282 return result;
1285 /* Actions defined here */
1287 static void
1288 report_class_declaration (name)
1289 const char * name;
1291 extern int flag_dump_class, flag_list_filename;
1293 push_class_context (name);
1294 if (flag_dump_class)
1296 char *name = get_class_name ();
1298 if (!previous_output)
1300 if (flag_list_filename)
1301 fprintf (out, "%s: ", input_filename);
1302 previous_output = 1;
1305 if (package_name)
1306 fprintf (out, "%s.%s ", package_name, name);
1307 else
1308 fprintf (out, "%s ", name);
1310 free (name);
1314 static void
1315 report_main_declaration (declarator)
1316 struct method_declarator *declarator;
1318 extern int flag_find_main;
1320 if (flag_find_main
1321 && modifier_value == 2
1322 && !strcmp (declarator->method_name, "main")
1323 && declarator->args
1324 && declarator->args [0] == '['
1325 && (! strcmp (declarator->args+1, "String")
1326 || ! strcmp (declarator->args + 1, "java.lang.String"))
1327 && current_class_context)
1329 if (!previous_output)
1331 char *name = get_class_name ();
1332 if (package_name)
1333 fprintf (out, "%s.%s ", package_name, name);
1334 else
1335 fprintf (out, "%s", name);
1336 free (name);
1337 previous_output = 1;
1342 void
1343 report ()
1345 extern int flag_complexity;
1346 if (flag_complexity)
1347 fprintf (out, "%s %d\n", input_filename, complexity);
1350 /* Reset global status used by the report functions. */
1352 void reset_report ()
1354 previous_output = 0;
1355 package_name = NULL;
1356 current_class_context = NULL;
1357 complexity = 0;
1360 void
1361 yyerror (msg)
1362 const char *msg ATTRIBUTE_UNUSED;
1364 fprintf (stderr, "%s: %d: %s\n", input_filename, lineno, msg);
1365 exit (1);