📄 parse.y
字号:
/* Source code parsing and tree node generation for the GNU compiler for the Java(TM) language. Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc. Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)This file is part of GNU CC.GNU CC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU CC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING. If not, write tothe Free Software Foundation, 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA.Java and all Java-based marks are trademarks or registered trademarksof Sun Microsystems, Inc. in the United States and other countries.The Free Software Foundation is independent of Sun Microsystems, Inc. *//* This file parses java source code and issues a tree node imagesuitable for code generation (byte code and targeted CPU assemblylanguage).The grammar conforms to the Java grammar described in "The Java(TM)Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley1996, ISBN 0-201-63451-1"The following modifications were brought to the original grammar:method_body: added the rule '| block SC_TK'static_initializer: added the rule 'static block SC_TK'. Note: All the extra rules described above should go away when the empty_statement rule will work.statement_nsi: 'nsi' should be read no_short_if.Some rules have been modified to support JDK1.1 inner classesdefinitions and other extensions. */%{#include "config.h"#include "system.h"#include <dirent.h>#include "tree.h"#include "rtl.h"#include "obstack.h"#include "toplev.h"#include "flags.h"#include "java-tree.h"#include "jcf.h"#include "lex.h"#include "parse.h"#include "zipfile.h"#include "convert.h"#include "buffer.h"#include "xref.h"#include "except.h"#ifndef DIR_SEPARATOR#define DIR_SEPARATOR '/'#endif/* Local function prototypes */static char *java_accstring_lookup PROTO ((int));static void classitf_redefinition_error PROTO ((char *,tree, tree, tree));static void variable_redefinition_error PROTO ((tree, tree, tree, int));static void check_modifiers PROTO ((char *, int, int));static tree create_class PROTO ((int, tree, tree, tree));static tree create_interface PROTO ((int, tree, tree));static tree find_field PROTO ((tree, tree));static tree lookup_field_wrapper PROTO ((tree, tree));static int duplicate_declaration_error_p PROTO ((tree, tree, tree));static void register_fields PROTO ((int, tree, tree));static tree parser_qualified_classname PROTO ((tree));static int parser_check_super PROTO ((tree, tree, tree));static int parser_check_super_interface PROTO ((tree, tree, tree));static void check_modifiers_consistency PROTO ((int));static tree lookup_cl PROTO ((tree));static tree lookup_java_method2 PROTO ((tree, tree, int));static tree method_header PROTO ((int, tree, tree, tree));static void fix_method_argument_names PROTO ((tree ,tree));static tree method_declarator PROTO ((tree, tree));static void parse_warning_context PVPROTO ((tree cl, const char *msg, ...)) ATTRIBUTE_PRINTF_2;static void issue_warning_error_from_context PROTO ((tree, const char *msg, va_list));static tree parse_jdk1_1_error PROTO ((char *));static void complete_class_report_errors PROTO ((jdep *));static int process_imports PROTO ((void));static void read_import_dir PROTO ((tree));static int find_in_imports_on_demand PROTO ((tree));static int find_in_imports PROTO ((tree));static int check_pkg_class_access PROTO ((tree, tree));static tree resolve_package PROTO ((tree, tree *));static tree lookup_package_type PROTO ((char *, int));static tree lookup_package_type_and_set_next PROTO ((char *, int, tree *));static tree resolve_class PROTO ((tree, tree, tree));static void declare_local_variables PROTO ((int, tree, tree));static void source_start_java_method PROTO ((tree));static void source_end_java_method PROTO ((void));static void expand_start_java_method PROTO ((tree));static tree find_name_in_single_imports PROTO ((tree));static void check_abstract_method_header PROTO ((tree));static tree lookup_java_interface_method2 PROTO ((tree, tree));static tree resolve_expression_name PROTO ((tree, tree *));static tree maybe_create_class_interface_decl PROTO ((tree, tree, tree));static int check_class_interface_creation PROTO ((int, int, tree, tree, tree, tree));static tree patch_method_invocation PROTO ((tree, tree, tree, int *, tree *));static int breakdown_qualified PROTO ((tree *, tree *, tree));static tree resolve_and_layout PROTO ((tree, tree));static tree resolve_no_layout PROTO ((tree, tree));static int invocation_mode PROTO ((tree, int));static tree find_applicable_accessible_methods_list PROTO ((int, tree, tree, tree));static void search_applicable_methods_list PROTO ((int, tree, tree, tree, tree *, tree *));static tree find_most_specific_methods_list PROTO ((tree));static int argument_types_convertible PROTO ((tree, tree));static tree patch_invoke PROTO ((tree, tree, tree));static tree lookup_method_invoke PROTO ((int, tree, tree, tree, tree));static tree register_incomplete_type PROTO ((int, tree, tree, tree));static tree obtain_incomplete_type PROTO ((tree));static tree java_complete_lhs PROTO ((tree));static tree java_complete_tree PROTO ((tree));static void java_complete_expand_method PROTO ((tree));static int unresolved_type_p PROTO ((tree, tree *));static void create_jdep_list PROTO ((struct parser_ctxt *));static tree build_expr_block PROTO ((tree, tree));static tree enter_block PROTO ((void));static tree enter_a_block PROTO ((tree));static tree exit_block PROTO ((void));static tree lookup_name_in_blocks PROTO ((tree));static void maybe_absorb_scoping_blocks PROTO ((void));static tree build_method_invocation PROTO ((tree, tree));static tree build_new_invocation PROTO ((tree, tree));static tree build_assignment PROTO ((int, int, tree, tree));static tree build_binop PROTO ((enum tree_code, int, tree, tree));static int check_final_assignment PROTO ((tree ,tree));static tree patch_assignment PROTO ((tree, tree, tree ));static tree patch_binop PROTO ((tree, tree, tree));static tree build_unaryop PROTO ((int, int, tree));static tree build_incdec PROTO ((int, int, tree, int));static tree patch_unaryop PROTO ((tree, tree));static tree build_cast PROTO ((int, tree, tree));static tree build_null_of_type PROTO ((tree));static tree patch_cast PROTO ((tree, tree));static int valid_ref_assignconv_cast_p PROTO ((tree, tree, int));static int valid_builtin_assignconv_identity_widening_p PROTO ((tree, tree));static int valid_cast_to_p PROTO ((tree, tree));static int valid_method_invocation_conversion_p PROTO ((tree, tree));static tree try_builtin_assignconv PROTO ((tree, tree, tree));static tree try_reference_assignconv PROTO ((tree, tree));static tree build_unresolved_array_type PROTO ((tree));static tree build_array_from_name PROTO ((tree, tree, tree, tree *));static tree build_array_ref PROTO ((int, tree, tree));static tree patch_array_ref PROTO ((tree));static tree make_qualified_name PROTO ((tree, tree, int));static tree merge_qualified_name PROTO ((tree, tree));static tree make_qualified_primary PROTO ((tree, tree, int));static int resolve_qualified_expression_name PROTO ((tree, tree *, tree *, tree *));static void qualify_ambiguous_name PROTO ((tree));static void maybe_generate_clinit PROTO ((void));static tree resolve_field_access PROTO ((tree, tree *, tree *));static tree build_newarray_node PROTO ((tree, tree, int));static tree patch_newarray PROTO ((tree));static tree resolve_type_during_patch PROTO ((tree));static tree build_this PROTO ((int));static tree build_return PROTO ((int, tree));static tree patch_return PROTO ((tree));static tree maybe_access_field PROTO ((tree, tree, tree));static int complete_function_arguments PROTO ((tree));static int check_for_static_method_reference PROTO ((tree, tree, tree, tree, tree));static int not_accessible_p PROTO ((tree, tree, int));static void check_deprecation PROTO ((tree, tree));static int class_in_current_package PROTO ((tree));static tree build_if_else_statement PROTO ((int, tree, tree, tree));static tree patch_if_else_statement PROTO ((tree));static tree add_stmt_to_compound PROTO ((tree, tree, tree));static tree add_stmt_to_block PROTO ((tree, tree, tree));static tree patch_exit_expr PROTO ((tree));static tree build_labeled_block PROTO ((int, tree));static tree finish_labeled_statement PROTO ((tree, tree));static tree build_bc_statement PROTO ((int, int, tree));static tree patch_bc_statement PROTO ((tree));static tree patch_loop_statement PROTO ((tree));static tree build_new_loop PROTO ((tree));static tree build_loop_body PROTO ((int, tree, int));static tree finish_loop_body PROTO ((int, tree, tree, int));static tree build_debugable_stmt PROTO ((int, tree));static tree finish_for_loop PROTO ((int, tree, tree, tree));static tree patch_switch_statement PROTO ((tree));static tree string_constant_concatenation PROTO ((tree, tree));static tree build_string_concatenation PROTO ((tree, tree));static tree patch_string_cst PROTO ((tree));static tree patch_string PROTO ((tree));static tree build_try_statement PROTO ((int, tree, tree));static tree build_try_finally_statement PROTO ((int, tree, tree));static tree patch_try_statement PROTO ((tree));static tree patch_synchronized_statement PROTO ((tree, tree));static tree patch_throw_statement PROTO ((tree, tree));static void check_thrown_exceptions PROTO ((int, tree));static int check_thrown_exceptions_do PROTO ((tree));static void purge_unchecked_exceptions PROTO ((tree));static void check_throws_clauses PROTO ((tree, tree, tree));static void finish_method_declaration PROTO ((tree));static tree build_super_invocation PROTO (());static int verify_constructor_circularity PROTO ((tree, tree));static char *constructor_circularity_msg PROTO ((tree, tree));static tree build_this_super_qualified_invocation PROTO ((int, tree, tree, int, int));static char *get_printable_method_name PROTO ((tree));static tree patch_conditional_expr PROTO ((tree, tree, tree));static void maybe_generate_finit PROTO (());static void fix_constructors PROTO ((tree));static int verify_constructor_super PROTO (());static tree create_artificial_method PROTO ((tree, int, tree, tree, tree));static void start_artificial_method_body PROTO ((tree));static void end_artificial_method_body PROTO ((tree));static int check_method_redefinition PROTO ((tree, tree));static int reset_method_name PROTO ((tree));static void java_check_regular_methods PROTO ((tree));static void java_check_abstract_methods PROTO ((tree));static tree maybe_build_primttype_type_ref PROTO ((tree, tree));static void unreachable_stmt_error PROTO ((tree));static tree find_expr_with_wfl PROTO ((tree));static void missing_return_error PROTO ((tree));static tree build_new_array_init PROTO ((int, tree));static tree patch_new_array_init PROTO ((tree, tree));static tree maybe_build_array_element_wfl PROTO ((tree));static int array_constructor_check_entry PROTO ((tree, tree));static char *purify_type_name PROTO ((char *));static tree fold_constant_for_init PROTO ((tree, tree));static tree strip_out_static_field_access_decl PROTO ((tree));static jdeplist *reverse_jdep_list PROTO ((struct parser_ctxt *));static void static_ref_err PROTO ((tree, tree, tree));/* Number of error found so far. */int java_error_count; /* Number of warning found so far. */int java_warning_count;/* Tell when not to fold, when doing xrefs */int do_not_fold;/* The current parser context */struct parser_ctxt *ctxp;/* List of things that were analyzed for which code will be generated */static struct parser_ctxt *ctxp_for_generation = NULL;/* binop_lookup maps token to tree_code. It is used where binary operations are involved and required by the parser. RDIV_EXPR covers both integral/floating point division. The code is changed once the type of both operator is worked out. */static enum tree_code binop_lookup[19] = { PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR, LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR, BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR, TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR, EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR, };#define BINOP_LOOKUP(VALUE) \ binop_lookup [((VALUE) - PLUS_TK)% \ (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]/* Fake WFL used to report error message. It is initialized once if needed and reused with it's location information is overriden. */tree wfl_operator = NULL_TREE;/* The "$L" identifier we use to create labels. */static tree label_id = NULL_TREE;/* The "StringBuffer" identifier used for the String `+' operator. */static tree wfl_string_buffer = NULL_TREE; /* The "append" identifier used for String `+' operator. */static tree wfl_append = NULL_TREE;/* The "toString" identifier used for String `+' operator. */static tree wfl_to_string = NULL_TREE;/* The "java.lang" import qualified name. */static tree java_lang_id = NULL_TREE;/* The "java.lang.Cloneable" qualified name. */static tree java_lang_cloneable = NULL_TREE;/* Context and flag for static blocks */static tree current_static_block = NULL_TREE;%}%union { tree node; int sub_token; struct { int token; int location; } operator; int value;}%{#include "lex.c"%}%pure_parser/* Things defined here have to match the order of what's in the binop_lookup table. */%token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK%token LS_TK SRS_TK ZRS_TK%token AND_TK XOR_TK OR_TK%token BOOL_AND_TK BOOL_OR_TK %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK/* This maps to the same binop_lookup entry than the token above */%token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK%token REM_ASSIGN_TK %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK%token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK/* Modifier TOKEN have to be kept in this order. Don't scramble it */%token PUBLIC_TK PRIVATE_TK PROTECTED_TK%token STATIC_TK FINAL_TK SYNCHRONIZED_TK%token VOLATILE_TK TRANSIENT_TK NATIVE_TK%token PAD_TK ABSTRACT_TK MODIFIER_TK/* Keep those two in order, too */%token DECR_TK INCR_TK/* From now one, things can be in any order */%token DEFAULT_TK IF_TK THROW_TK%token BOOLEAN_TK DO_TK IMPLEMENTS_TK%token THROWS_TK BREAK_TK IMPORT_TK %token ELSE_TK INSTANCEOF_TK RETURN_TK%token VOID_TK CATCH_TK INTERFACE_TK%token CASE_TK EXTENDS_TK FINALLY_TK%token SUPER_TK WHILE_TK CLASS_TK%token SWITCH_TK CONST_TK TRY_TK%token FOR_TK NEW_TK CONTINUE_TK%token GOTO_TK PACKAGE_TK THIS_TK%token BYTE_TK SHORT_TK INT_TK LONG_TK%token CHAR_TK INTEGRAL_TK%token FLOAT_TK DOUBLE_TK FP_TK%token ID_TK%token REL_QM_TK REL_CL_TK NOT_TK NEG_TK%token ASSIGN_ANY_TK ASSIGN_TK%token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK%token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK%token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK%type <value> modifiers MODIFIER_TK%type <node> super ID_TK identifier%type <node> name simple_name qualified_name%type <node> class_declaration type_declaration compilation_unit field_declaration method_declaration extends_interfaces interfaces interface_type_list interface_declaration class_member_declaration import_declarations package_declaration type_declarations interface_body interface_member_declaration constant_declaration interface_member_declarations interface_type abstract_method_declaration interface_type_list%type <node> class_body_declaration class_member_declaration static_initializer constructor_declaration block%type <node> class_body_declarations constructor_header%type <node> class_or_interface_type class_type class_type_list constructor_declarator explicit_constructor_invocation%type <node> dim_expr dim_exprs this_or_super throws%type <node> variable_declarator_id variable_declarator variable_declarators variable_initializer variable_initializers constructor_body array_initializer%type <node> class_body block_end constructor_block_end%type <node> statement statement_without_trailing_substatement labeled_statement if_then_statement label_decl if_then_else_statement while_statement for_statement statement_nsi labeled_statement_nsi do_statement if_then_else_statement_nsi while_statement_nsi for_statement_nsi statement_expression_list for_init for_update statement_expression expression_statement primary_no_new_array expression primary array_creation_expression array_type class_instance_creation_expression field_access method_invocation array_access something_dot_new argument_list postfix_expression while_expression post_increment_expression post_decrement_expression unary_expression_not_plus_minus unary_expression pre_increment_expression pre_decrement_expression unary_expression_not_plus_minus cast_expression multiplicative_expression additive_expression shift_expression relational_expression equality_expression and_expression exclusive_or_expression inclusive_or_expression conditional_and_expression conditional_or_expression conditional_expression assignment_expression left_hand_side assignment for_header for_begin constant_expression do_statement_begin empty_statement switch_statement synchronized_statement throw_statement try_statement switch_expression switch_block catches catch_clause catch_clause_parameter finally%type <node> return_statement break_statement continue_statement%type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK%type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK%type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK%type <operator> ASSIGN_ANY_TK assignment_operator%token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK%token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK%token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK CCB_TK%token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK%type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK%type <node> method_body %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK STRING_LIT_TK NULL_TK VOID_TK
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -