Commit 22eed1e6 by Alexandre Petit-Bianco Committed by Alexandre Petit-Bianco

class.c (layout_class): Don't mangle <finit>, produce __finit<class> instead.

Wed Oct 28 08:03:31 1998  Alexandre Petit-Bianco  <apbianco@cygnus.com>
	* class.c (layout_class): Don't mangle <finit>, produce
 	__finit<class> instead. Don't verify artificial methods.
	* decl.c (finit_identifier_node): New declared global.
	(init_decl_processing): finit_identifier_node initialized.
	* java-tree.def (CONDITIONAL_EXPR): New Java tree code.
	* java-tree.h (finit_identifier_node): Declared as extern.
	(struct lang_decl): New field called_constructor.
	(DECL_CONSTRUCTOR_CALLS): Access macro to called_constructor.
	(CLASS_HAS_FINIT_P): New macro.
	(CALL_CONSTRUCTOR_P): Leading comment changed. Macro now checks
 	explicit constructor invocation.
	(CALL_EXPLICIT_CONSTRUCTOR_P, CALL_THIS_CONSTRUCTOR_P,
 	CALL_SUPER_CONSTRUCTOR_P): New macros.
	(write_classfile): Added prototype.
	* jcf-parse.c (jcf_parse_source): Parse and remember for
 	generation if the file was seen on the command line.
	(parse_source_file): Don't write the class file here.
	(yyparse): Loop on files rewritten. Set current_jcf.
	(parse_zip_file_entries): Parse class file only if it was found.
	* lang.c (init_parse): Don't open command line provided filename
 	here.
	(lang_parse): Don't set main_jcf anymore.
	* parse.h (ABSTRAC_CHECK): Capitalized arguments.
	(JCONSTRUCTOR_CHECK): New macro.
	(JBSC_TYPE_P): New macro.
	(IN_TRY_BLOCK_P, EXCEPTIONS_P): Fixed leading comment.
	(COMPLETE_CHECK_OP_2): New macro.
	(struct parse_ctxt): New field explicit_constructor_p.
	(check_class_interface_creation): Fixed prototype indentation.
	(patch_method_invocation_stmt): Prototype reflects added argument.
	(patch_invoke): Likewise.
	(complete_method_declaration, build_super_invocation,
 	verify_constructor_circularity,
 	build_this_super_qualified_invocation, get_printable_method_name,
 	patch_conditional_expr, maybe_generate_finit, fix_constructors,
 	verify_constructor_super, create_artificial_method,
 	start_artificial_method_body, end_artificial_method_body,
 	generate_field_initialization_code): New function prototypes.
	* parse.y: Fixed leading comment
	(constructor_header:, constructor_body:, block_end:): Rules tagged
 	<node>.
	(type_declaration:): Call maybe_generate_finit.
	(method_declaration:): Action for method_body: placed in new
 	function complete_method_declaration, called here.
	(constructor_declaration:): Defined actions. Removed leading
 	FIXME.
	(constructor_header:): New rule with action.
	(constructor_body:): Rule rewritten using block_begin: and
 	block_end:. Defined actions.
	(constructor_declarator:, explicit_constructor_invocation:):
 	Defined actions.
	(block:): Use new rules block_begin: block_end:.
	(block_begin:, block_end:): New rules and actions.
	(block_statements:): Fixed error message for explicit
 	constructors.
	(method_invocation:): Call build_this_super_qualified_invocation
 	if primary is `this' or `super' was seen.
	(conditional_expression:): Action defined.
	(extra_ctxp_pushed_p): New static global flag.
	(java_parser_context_save_global): Create parser context if
 	necessary. Use extra_ctxp_pushed_p to remember it.
	(java_parser_context_restore_global): Pop extra parser context if
 	one exists.
	(build_array_from_name): Array on primitive types are marked
 	loaded.
	(register_fields): Restore new name in field initializer
 	expression if type was altered. Non static fields initialized upon
 	declaration marked initialized.
	(maybe_generate_finit): New function.
	(maybe_generate_clinit): Use create_artificial_method,
 	start_artificial_method_body, end_artificial_method_body. Generate
 	debug info for enclosed initialization statements.
	(method_header): Fixed leading comment. Check constructor
 	flags. Detect constructor declarations and set DECL_CONSTRUCTOR_P
 	accordingly.
	(complete_method_declaration, constructor_circularity_msg,
 	verify_constructor_circularity): New functions.
	(get_printable_method_name): New function.
	(check_method_redefinition): Don't rename <finit> methods. Fix
 	declared constructor names. Error message for
 	constructors modified.
	(java_check_regular_methods): Local variable seen_constructor
 	renamed saw_constructor. Skip verification on constructors. Create
 	default constructor with create_artificial_method.
	(java_check_methods): Removed unnecessary empty line.
	(create_artificial_method, start_artificial_method_body,
 	end_artificial_method_body): New functions.
	(java_layout_classes): Changed leading comment. Reverse fields
 	list if necessary. Always layout java.lang.Object if being
 	defined.
	(java_complete_expand_methods): Verify constructor circularity.
	(java_complete_expand_method): Call fix_constructor on
 	constructors.  Local variable no_ac_found removed. Restore
 	bindings if method body expansion failed.
	(fix_constructors, verify_constructor_super,
 	generate_field_initialization_code): New function.
	(java_expand_classes): Fixed leading comment. Write class file
 	here.
	(resolve_expression_name): Check for illegal instance variable
 	usage within the argument scope of an explicit constructor
 	invocation.
	(resolve_qualified_expression_name): Pass extra from_super flag
 	when invoking patch_method_invocation_stmt. New case for
 	conditional expression when used as a primary. Check for error
 	when acquiring super.
	(patch_method_invocation_stmt): Added extra argument super. New
 	local variable is_static_flag. Set class_to_search according to
 	the nature of the constructor invocation. Don't add `this'
 	argument when expanding NEW_CLASS_EXPR. Check for illegal method
 	invocation within the argument scope of explicit constructor
 	invocation. Set is_static according to is_static_flag. Provide
 	extra `super' argument to patch_invoke invocation.
	(patch_invoke): New argument from_super. Loop on arguments
 	indentation fixed. Pass from_super to invocation_mode. New switch
 	case INVOKE_SUPER. Fixed error message in switch default case.
  	Don't use CALL_CONSTRUCTOR_P but rather a test on the tree node
 	value.
	(invocation_mode): Return INVOKE_SUPER mode when appropriate.
	(lookup_method_invoke): Fixed prototypes in candidates list. Error
 	message takes constructors into account.
	(find_applicable_accessible_methods_list): Fixed indentation.
	(qualify_ambiguous_name): Take explicit constructor invocation
 	into account. Deal with a conditional expression as a primary to
 	a method call.
	(java_complete_tree): Added local wfl_op3. New CONDITIONAL_EXPR
 	case. Added extra argument to patch_method_invocation_stmt.
 	Register calls made to explicit constructor `this'. Don't call
 	save_expr in ARRAY_REF case when emitting class files. Check for
 	illegal use of this when expanding explicit constructor invocation
 	arguments.
	(complete_function_arguments): Set and reset parser context
 	explicit_constructor_p field value when appropriate.
	(build_super_invocation, build_this_super_qualified_invocation):
 	New functions.
	(patch_assignment): Fixed typo.
	(patch_unaryop): Check on final fields occurs only when a decl
 	exits.
	(patch_return): Take constructors into account.
	(patch_conditional_expr): New function.
	* typeck.c (build_java_signature): Removed unnecessary empty line.
This patch implements the conditional operator, fixes the super
invokation mode, implements most of what is required for constructors
and changes the way source files are handled by the front-end.

From-SVN: r23403
parent a05273d4
Wed Oct 28 08:03:31 1998 Alexandre Petit-Bianco <apbianco@cygnus.com>
* class.c (layout_class): Don't mangle <finit>, produce
__finit<class> instead. Don't verify artificial methods.
* decl.c (finit_identifier_node): New declared global.
(init_decl_processing): finit_identifier_node initialized.
* java-tree.def (CONDITIONAL_EXPR): New Java tree code.
* java-tree.h (finit_identifier_node): Declared as extern.
(struct lang_decl): New field called_constructor.
(DECL_CONSTRUCTOR_CALLS): Access macro to called_constructor.
(CLASS_HAS_FINIT_P): New macro.
(CALL_CONSTRUCTOR_P): Leading comment changed. Macro now checks
explicit constructor invocation.
(CALL_EXPLICIT_CONSTRUCTOR_P, CALL_THIS_CONSTRUCTOR_P,
CALL_SUPER_CONSTRUCTOR_P): New macros.
(write_classfile): Added prototype.
* jcf-parse.c (jcf_parse_source): Parse and remember for
generation if the file was seen on the command line.
(parse_source_file): Don't write the class file here.
(yyparse): Loop on files rewritten. Set current_jcf.
(parse_zip_file_entries): Parse class file only if it was found.
* lang.c (init_parse): Don't open command line provided filename
here.
(lang_parse): Don't set main_jcf anymore.
* parse.h (ABSTRAC_CHECK): Capitalized arguments.
(JCONSTRUCTOR_CHECK): New macro.
(JBSC_TYPE_P): New macro.
(IN_TRY_BLOCK_P, EXCEPTIONS_P): Fixed leading comment.
(COMPLETE_CHECK_OP_2): New macro.
(struct parse_ctxt): New field explicit_constructor_p.
(check_class_interface_creation): Fixed prototype indentation.
(patch_method_invocation_stmt): Prototype reflects added argument.
(patch_invoke): Likewise.
(complete_method_declaration, build_super_invocation,
verify_constructor_circularity,
build_this_super_qualified_invocation, get_printable_method_name,
patch_conditional_expr, maybe_generate_finit, fix_constructors,
verify_constructor_super, create_artificial_method,
start_artificial_method_body, end_artificial_method_body,
generate_field_initialization_code): New function prototypes.
* parse.y: Fixed leading comment
(constructor_header:, constructor_body:, block_end:): Rules tagged
<node>.
(type_declaration:): Call maybe_generate_finit.
(method_declaration:): Action for method_body: placed in new
function complete_method_declaration, called here.
(constructor_declaration:): Defined actions. Removed leading
FIXME.
(constructor_header:): New rule with action.
(constructor_body:): Rule rewritten using block_begin: and
block_end:. Defined actions.
(constructor_declarator:, explicit_constructor_invocation:):
Defined actions.
(block:): Use new rules block_begin: block_end:.
(block_begin:, block_end:): New rules and actions.
(block_statements:): Fixed error message for explicit
constructors.
(method_invocation:): Call build_this_super_qualified_invocation
if primary is `this' or `super' was seen.
(conditional_expression:): Action defined.
(extra_ctxp_pushed_p): New static global flag.
(java_parser_context_save_global): Create parser context if
necessary. Use extra_ctxp_pushed_p to remember it.
(java_parser_context_restore_global): Pop extra parser context if
one exists.
(build_array_from_name): Array on primitive types are marked
loaded.
(register_fields): Restore new name in field initializer
expression if type was altered. Non static fields initialized upon
declaration marked initialized.
(maybe_generate_finit): New function.
(maybe_generate_clinit): Use create_artificial_method,
start_artificial_method_body, end_artificial_method_body. Generate
debug info for enclosed initialization statements.
(method_header): Fixed leading comment. Check constructor
flags. Detect constructor declarations and set DECL_CONSTRUCTOR_P
accordingly.
(complete_method_declaration, constructor_circularity_msg,
verify_constructor_circularity): New functions.
(get_printable_method_name): New function.
(check_method_redefinition): Don't rename <finit> methods. Fix
declared constructor names. Error message for
constructors modified.
(java_check_regular_methods): Local variable seen_constructor
renamed saw_constructor. Skip verification on constructors. Create
default constructor with create_artificial_method.
(java_check_methods): Removed unnecessary empty line.
(create_artificial_method, start_artificial_method_body,
end_artificial_method_body): New functions.
(java_layout_classes): Changed leading comment. Reverse fields
list if necessary. Always layout java.lang.Object if being
defined.
(java_complete_expand_methods): Verify constructor circularity.
(java_complete_expand_method): Call fix_constructor on
constructors. Local variable no_ac_found removed. Restore
bindings if method body expansion failed.
(fix_constructors, verify_constructor_super,
generate_field_initialization_code): New function.
(java_expand_classes): Fixed leading comment. Write class file
here.
(resolve_expression_name): Check for illegal instance variable
usage within the argument scope of an explicit constructor
invocation.
(resolve_qualified_expression_name): Pass extra from_super flag
when invoking patch_method_invocation_stmt. New case for
conditional expression when used as a primary. Check for error
when acquiring super.
(patch_method_invocation_stmt): Added extra argument super. New
local variable is_static_flag. Set class_to_search according to
the nature of the constructor invocation. Don't add `this'
argument when expanding NEW_CLASS_EXPR. Check for illegal method
invocation within the argument scope of explicit constructor
invocation. Set is_static according to is_static_flag. Provide
extra `super' argument to patch_invoke invocation.
(patch_invoke): New argument from_super. Loop on arguments
indentation fixed. Pass from_super to invocation_mode. New switch
case INVOKE_SUPER. Fixed error message in switch default case.
Don't use CALL_CONSTRUCTOR_P but rather a test on the tree node
value.
(invocation_mode): Return INVOKE_SUPER mode when appropriate.
(lookup_method_invoke): Fixed prototypes in candidates list. Error
message takes constructors into account.
(find_applicable_accessible_methods_list): Fixed indentation.
(qualify_ambiguous_name): Take explicit constructor invocation
into account. Deal with a conditional expression as a primary to
a method call.
(java_complete_tree): Added local wfl_op3. New CONDITIONAL_EXPR
case. Added extra argument to patch_method_invocation_stmt.
Register calls made to explicit constructor `this'. Don't call
save_expr in ARRAY_REF case when emitting class files. Check for
illegal use of this when expanding explicit constructor invocation
arguments.
(complete_function_arguments): Set and reset parser context
explicit_constructor_p field value when appropriate.
(build_super_invocation, build_this_super_qualified_invocation):
New functions.
(patch_assignment): Fixed typo.
(patch_unaryop): Check on final fields occurs only when a decl
exits.
(patch_return): Take constructors into account.
(patch_conditional_expr): New function.
* typeck.c (build_java_signature): Removed unnecessary empty line.
Wed Oct 28 00:46:15 1998 Jeffrey A Law (law@cygnus.com)
* Makefile.in (jcf-dump, gcjh): Link in $(LIBS) too.
......
......@@ -1400,7 +1400,8 @@ layout_class (this_class)
{
int len; tree arg, arglist, t;
int method_name_needs_escapes = 0;
if (method_name != init_identifier_node)
if (method_name != init_identifier_node
&& method_name != finit_identifier_node)
{
int encoded_len
= unicode_mangling_length (IDENTIFIER_POINTER (method_name),
......@@ -1421,6 +1422,8 @@ layout_class (this_class)
}
obstack_grow (&temporary_obstack, "__", 2);
if (method_name == finit_identifier_node)
obstack_grow (&temporary_obstack, "finit", 5);
append_gpp_mangled_type (&temporary_obstack, this_class);
TREE_PUBLIC (method_decl) = 1;
......@@ -1492,7 +1495,7 @@ layout_class (this_class)
DECL_NAME (method_decl) = get_identifier (p);
DECL_CONSTRUCTOR_P (method_decl) = 1;
}
else if (! METHOD_STATIC (method_decl))
else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
{
tree method_sig = build_java_argument_signature (TREE_TYPE (method_decl));
tree super_method = lookup_argument_method (super_class, method_name,
......
......@@ -325,6 +325,7 @@ tree boolean_true_node, boolean_false_node;
tree TYPE_identifier_node;
tree init_identifier_node;
tree clinit_identifier_node;
tree finit_identifier_node;
tree void_signature_node;
tree length_identifier_node;
tree this_identifier_node;
......@@ -533,6 +534,7 @@ init_decl_processing ()
TYPE_identifier_node = get_identifier ("TYPE");
init_identifier_node = get_identifier ("<init>");
clinit_identifier_node = get_identifier ("<clinit>");
finit_identifier_node = get_identifier ("<finit>");
void_signature_node = get_identifier ("()V");
length_identifier_node = get_identifier ("length");
this_identifier_node = get_identifier ("this");
......
......@@ -64,3 +64,9 @@ DEFTREECODE (SYNCHRONIZED_EXPR, "synchronized", 'e', 2)
/* Throw statement.
Operand 0 is the throw expresion. */
DEFTREECODE (THROW_EXPR, "throw", '1', 1)
/* Conditional operator.
Operand 0 is the condition expression
Operand 1 is the then-value
Operand 2 is the else-value. */
DEFTREECODE (CONDITIONAL_EXPR, "?:", 'e', 3)
......@@ -61,6 +61,7 @@ struct JCF;
3: CLASS_FROM_SOURCE_P (in RECORD_TYPE).
4: CLASS_P (in RECORD_TYPE).
5: CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (in RECORD_TYPE)
6: CLASS_HAS_FINIT_P (in RECORD_TYPE)
Usage of DECL_LANG_FLAG_?:
1: METHOD_PUBLIC (in FUNCTION_DECL).
......@@ -195,6 +196,7 @@ extern tree string_array_type_node;
extern tree TYPE_identifier_node; /* "TYPE" */
extern tree init_identifier_node; /* "<init>" */
extern tree clinit_identifier_node; /* "<clinit>" */
extern tree finit_identifier_node; /* "<finit>" */
extern tree void_signature_node; /* "()V" */
extern tree length_identifier_node; /* "length" */
extern tree this_identifier_node; /* "this" */
......@@ -324,6 +326,10 @@ struct lang_identifier
/* List of checked thrown exceptions, as specified with the `throws'
keyword */
#define DECL_FUNCTION_THROWS(DECL) (DECL_LANG_SPECIFIC(DECL)->throws_list)
/* List of other constructors of the same class that this constructor
calls */
#define DECL_CONSTRUCTOR_CALLS(DECL) \
(DECL_LANG_SPECIFIC(DECL)->called_constructor)
/* Pointer to the function's current's COMPOUND_EXPR tree (while
completing its body) or the function's block */
#define DECL_FUNCTION_BODY(DECL) (DECL_LANG_SPECIFIC(DECL)->function_decl_body)
......@@ -403,6 +409,8 @@ struct lang_decl
int max_locals, max_stack, arg_slot_count;
tree throws_list; /* Exception specified by `throws' */
tree function_decl_body; /* Hold all function's statements */
tree called_constructor; /* When decl is a constructor, the
list of other constructor it calls. */
};
/* DECL_LANG_SPECIFIC for VAR_DECL and PARM_DECL. */
......@@ -524,6 +532,7 @@ extern void register_class PROTO (());
extern int alloc_name_constant PROTO ((int, tree));
extern void emit_register_classes PROTO (());
extern void lang_init_source PROTO ((int));
extern void write_classfile PROTO ((tree));
/* Access flags etc for a method (a FUNCTION_DECL): */
......@@ -661,6 +670,9 @@ extern tree *type_map;
#define CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P(TYPE) \
TYPE_LANG_FLAG_5 (TYPE)
/* True if class TYPE has a field initializer <finit> function */
#define CLASS_HAS_FINIT_P(TYPE) TYPE_LANG_FLAG_6 (TYPE)
/* True if identifier ID was seen while processing a single type import stmt */
#define IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P(ID) TREE_LANG_FLAG_0 (ID)
......@@ -754,8 +766,20 @@ extern tree *type_map;
#define FINISH_RECORD_CONSTRUCTOR(CONS) \
CONSTRUCTOR_ELTS(CONS) = nreverse (CONSTRUCTOR_ELTS(CONS))
/* Macro(s) using the definitions above */
#define CALL_CONSTRUCTOR_P(NODE) (TREE_CODE (NODE) == NEW_CLASS_EXPR)
/* Macros on constructors invocations. */
#define CALL_CONSTRUCTOR_P(NODE) \
(TREE_CODE (NODE) == NEW_CLASS_EXPR || CALL_EXPLICIT_CONSTRUCTOR_P (NODE))
#define CALL_EXPLICIT_CONSTRUCTOR_P(NODE) \
(CALL_THIS_CONSTRUCTOR_P (NODE) || CALL_SUPER_CONSTRUCTOR_P (NODE))
#define CALL_THIS_CONSTRUCTOR_P(NODE) \
(TREE_CODE (NODE) == CALL_EXPR \
&& EXPR_WFL_NODE (TREE_OPERAND (NODE, 0)) == this_identifier_node)
#define CALL_SUPER_CONSTRUCTOR_P(NODE) \
(TREE_CODE (NODE) == CALL_EXPR \
&& EXPR_WFL_NODE (TREE_OPERAND (NODE, 0)) == super_identifier_node)
/* Using a FINALLY_EXPR node */
#define FINALLY_EXPR_LABEL(NODE) TREE_OPERAND ((NODE), 0)
......
......@@ -506,13 +506,14 @@ int
jcf_parse_source (jcf)
JCF *jcf;
{
tree filename = get_identifier (input_filename);
java_parser_context_save_global ();
input_filename = current_jcf->filename;
if (!(finput = fopen (input_filename, "r")))
fatal ("input file `%s' just disappeared - jcf_parse_source",
input_filename);
parse_source_file (1); /* Parse only */
parse_source_file (IS_A_COMMAND_LINE_FILENAME_P (filename));
java_parser_context_restore_global ();
}
......@@ -686,9 +687,6 @@ parse_source_file (parse_only)
java_layout_classes ();
java_parse_abort_on_error ();
if (flag_emit_class_files)
write_classfile (current_class);
/* If only parsing, make sure that the currently parsed file isn't
also present in the argument list. If it's the case, remember
that we should generate it. */
......@@ -737,33 +735,35 @@ yyparse ()
}
while (next);
current_jcf = main_jcf;
current_file_list = nreverse (current_file_list);
for (node = current_file_list; node; node = TREE_CHAIN (node))
{
/* Don't substitute if INPUT_FILENAME doesn't feature the &
separator: we have only one file to deal with, we're fine */
if (several_files)
{
tree name = TREE_VALUE (node);
tree name = TREE_VALUE (node);
/* Skip already parsed files */
if (HAS_BEEN_ALREADY_PARSED_P (name))
continue;
/* Close previous descriptor, if any */
if (main_jcf->read_state && fclose (main_jcf->read_state))
fatal ("failed to close input file `%s' - yyparse",
(main_jcf->filename ? main_jcf->filename : "<unknown>"));
/* Open new file */
main_jcf->read_state = fopen (IDENTIFIER_POINTER (name), "r");
if (main_jcf->read_state == NULL)
pfatal_with_name (IDENTIFIER_POINTER (name));
/* Set new input_filename and finput */
input_filename = IDENTIFIER_POINTER (name);
finput = main_jcf->read_state;
}
/* Skip already parsed files */
if (HAS_BEEN_ALREADY_PARSED_P (name))
continue;
/* Close previous descriptor, if any */
if (main_jcf->read_state && fclose (main_jcf->read_state))
fatal ("failed to close input file `%s' - yyparse",
(main_jcf->filename ? main_jcf->filename : "<unknown>"));
/* Set jcf up and open a new file */
JCF_ZERO (main_jcf);
main_jcf->read_state = fopen (IDENTIFIER_POINTER (name), "r");
if (main_jcf->read_state == NULL)
pfatal_with_name (IDENTIFIER_POINTER (name));
/* Set new input_filename and finput */
finput = main_jcf->read_state;
#ifdef IO_BUFFER_SIZE
setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
_IOFBF, IO_BUFFER_SIZE);
#endif
input_filename = IDENTIFIER_POINTER (name);
main_jcf->filbuf = jcf_filbuf_from_stdio;
switch (jcf_figure_file_type (current_jcf))
{
......@@ -813,14 +813,16 @@ parse_zip_file_entries (void)
jcf_parse (current_jcf);
}
input_filename = current_jcf->filename;
parse_class_file ();
FREE (current_jcf->buffer); /* No longer necessary */
/* Note: there is a way to free this buffer right after a class seen
in a zip file has been parsed. The idea is the set its jcf in such
a way that buffer will be reallocated the time the code for the class
will be generated. FIXME. */
if (TYPE_SIZE (current_class) != error_mark_node)
{
input_filename = current_jcf->filename;
parse_class_file ();
FREE (current_jcf->buffer); /* No longer necessary */
/* Note: there is a way to free this buffer right after a
class seen in a zip file has been parsed. The idea is the
set its jcf in such a way that buffer will be reallocated
the time the code for the class will be generated. FIXME. */
}
}
}
......
......@@ -259,15 +259,7 @@ init_parse (filename)
free (buf);
}
}
finput = fopen (filename, "r");
}
if (finput == 0)
pfatal_with_name (filename);
#ifdef IO_BUFFER_SIZE
setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE), _IOFBF, IO_BUFFER_SIZE);
#endif
init_lex ();
return filename;
......@@ -454,11 +446,6 @@ lang_init ()
print_error_function = lang_print_error;
lang_expand_expr = java_lang_expand_expr;
JCF_ZERO (main_jcf);
main_jcf->read_state = finput;
main_jcf->filbuf = jcf_filbuf_from_stdio;
current_jcf = main_jcf;
flag_exceptions = 1;
/* Append to Gcc tree node definition arrays */
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -99,10 +99,14 @@ extern tree stabilize_reference PROTO ((tree));
count++; \
}
#define ABSTRACT_CHECK(flag, v, cl, s) \
if ((flag) & (v)) \
parse_error_context (cl, s " method can't be abstract");
#define ABSTRACT_CHECK(FLAG, V, CL, S) \
if ((FLAG) & (V)) \
parse_error_context ((CL), S " method can't be abstract");
#define JCONSTRUCTOR_CHECK(FLAG, V, CL, S) \
if ((FLAG) & (V)) \
parse_error_context ((CL), "Constructor can't be %s", (S)); \
/* Misc. */
#define exit_java_complete_class() \
{ \
......@@ -159,6 +163,10 @@ extern tree stabilize_reference PROTO ((tree));
&& (JNUMERIC_TYPE_P ((TYPE)) \
|| TREE_CODE ((TYPE)) == BOOLEAN_TYPE))
#define JBSC_TYPE_P(TYPE) ((TYPE) && (((TYPE) == byte_type_node) \
|| ((TYPE) == short_type_node) \
|| ((TYPE) == char_type_node)))
/* Not defined in the LRM */
#define JSTRING_TYPE_P(TYPE) ((TYPE) \
&& ((TYPE) == string_type_node || \
......@@ -269,17 +277,16 @@ extern tree stabilize_reference PROTO ((tree));
#define POP_EXCEPTIONS() \
currently_caught_type_list = TREE_CHAIN (currently_caught_type_list)
/* Check that we're inside a try block */
/* Check that we're inside a try block. */
#define IN_TRY_BLOCK_P() \
(currently_caught_type_list \
&& ((TREE_VALUE (currently_caught_type_list) != \
DECL_FUNCTION_THROWS (current_function_decl)) \
|| TREE_CHAIN (currently_caught_type_list)))
/* Check that we have exceptions in E */
/* Check that we have exceptions in E. */
#define EXCEPTIONS_P(E) ((E) ? TREE_VALUE (E) : NULL_TREE)
/* Invocation modes, as returned by invocation_mode (). */
enum {
INVOKE_STATIC,
......@@ -462,6 +469,7 @@ static jdeplist *reverse_jdep_list ();
}
#define COMPLETE_CHECK_OP_0(NODE) COMPLETE_CHECK_OP(NODE, 0)
#define COMPLETE_CHECK_OP_1(NODE) COMPLETE_CHECK_OP(NODE, 1)
#define COMPLETE_CHECK_OP_2(NODE) COMPLETE_CHECK_OP(NODE, 2)
/* Building invocations: append(ARG) and StringBuffer(ARG) */
#define BUILD_APPEND(ARG) \
......@@ -567,6 +575,12 @@ struct parser_ctxt {
labeled blocks. */
int pending_block; /* Pending block to close */
int explicit_constructor_p; /* True when processing an
explicit constructor. This flag is
used to trap illegal argument usage
during an explicit constructor
invocation. */
#endif /* JC1_LITE */
};
......@@ -611,8 +625,10 @@ static void check_abstract_method_header PROTO ((tree));
static tree lookup_java_interface_method2 PROTO ((tree, tree));
static tree resolve_expression_name PROTO ((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_stmt PROTO ((tree, tree, tree, int *, tree *));
static int check_class_interface_creation PROTO ((int, int, tree,
tree, tree, tree));
static tree patch_method_invocation_stmt PROTO ((tree, tree, tree,
int *, tree *, int));
static int breakdown_qualified PROTO ((tree *, tree *, tree));
static tree resolve_and_layout PROTO ((tree, tree));
static tree resolve_no_layout PROTO ((tree, tree));
......@@ -620,7 +636,7 @@ static int invocation_mode PROTO ((tree, int));
static tree find_applicable_accessible_methods_list PROTO ((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 patch_invoke PROTO ((tree, tree, tree, int));
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));
......@@ -704,6 +720,21 @@ 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 complete_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 tree generate_field_initialization_code PROTO ((tree));
void safe_layout_class PROTO ((tree));
void java_complete_class PROTO ((void));
......
......@@ -35,7 +35,6 @@ Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
The following modifications were brought to the original grammar:
method_body: added the rule '| block SC_TK'
constructor_declaration: added two rules to accept SC_TK.
static_initializer: added the rule 'static block SC_TK'.
Note: All the extra rules described above should go away when the
......@@ -196,16 +195,16 @@ static tree wfl_to_string = NULL_TREE;
abstract_method_declaration interface_type_list
%type <node> class_body_declaration class_member_declaration
static_initializer constructor_declaration block
%type <node> class_body_declarations
%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
variable_initializers constructor_body
%type <node> class_body
%type <node> class_body block_end
%type <node> block_statement local_variable_declaration_statement
block_statements local_variable_declaration
%type <node> statement statement_without_trailing_substatement
......@@ -448,6 +447,7 @@ type_declaration:
class_declaration
{
maybe_generate_clinit ();
maybe_generate_finit ();
$$ = $1;
}
| interface_declaration
......@@ -644,18 +644,7 @@ method_declaration:
source_start_java_method (current_function_decl);
}
method_body
{
BLOCK_EXPR_BODY
(DECL_FUNCTION_BODY (current_function_decl)) = $3;
maybe_absorb_scoping_blocks ();
exit_block (); /* Exit function's body. */
/* Merge last line of the function with first line,
directly in the function decl. It will be used to
emit correct debug info. */
DECL_SOURCE_LINE_MERGE (current_function_decl,
ctxp->last_ccb_indent1);
}
{ complete_method_declaration ($3); }
| method_header error
{YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
;
......@@ -774,51 +763,61 @@ static: /* Test lval.sub_token here */
;
/* 19.8.5 Productions from 8.6: Constructor Declarations */
/* NOTE FOR FURTHER WORK ON CONSTRUCTORS:
- If a forbidded modifier is found, the the error is either the use of
a forbidded modifier for a constructor OR bogus attempt to declare a
method without having specified the return type. FIXME */
constructor_declaration:
constructor_declarator throws constructor_body
{
RULE ("CONSTRUCTOR_DECLARATION");
}
| modifiers constructor_declarator throws constructor_body
constructor_header
{
SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
RULE ("CONSTRUCTOR_DECLARATION (modifier)");
}
/* extra SC_TK, FIXME */
| constructor_declarator throws constructor_body SC_TK
{
RULE ("CONSTRUCTOR_DECLARATION");
}
/* extra SC_TK, FIXME */
| modifiers constructor_declarator throws constructor_body SC_TK
{
SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
RULE ("CONSTRUCTOR_DECLARATION (modifier)");
current_function_decl = $1;
source_start_java_method (current_function_decl);
}
/* I'm not happy with the SC_TK addition. It isn't in the grammer and should
probably be matched by and empty statement. But it doesn't work. FIXME */
constructor_body
{ complete_method_declaration ($3); }
;
constructor_header:
constructor_declarator throws
{ $$ = method_header (0, NULL_TREE, $1, $2); }
| modifiers constructor_declarator throws
{ $$ = method_header ($1, NULL_TREE, $2, $3); }
;
constructor_declarator:
simple_name OP_TK CP_TK
{ $$ = method_declarator ($1, NULL_TREE); }
| simple_name OP_TK formal_parameter_list CP_TK
{ $$ = method_declarator ($1, $3); }
;
constructor_body:
OCB_TK CCB_TK
| OCB_TK explicit_constructor_invocation CCB_TK
| OCB_TK block_statements CCB_TK
| OCB_TK explicit_constructor_invocation block_statements CCB_TK
/* Unlike regular method, we always need a complete (empty)
body so we can safely perform all the required code
addition (super invocation and field initialization) */
block_begin block_end
{
BLOCK_EXPR_BODY ($2) = size_zero_node;
$$ = $2;
}
| block_begin explicit_constructor_invocation block_end
{ $$ = $3; }
| block_begin block_statements block_end
{ $$ = $3; }
| block_begin explicit_constructor_invocation block_statements block_end
{ $$ = $4; }
;
/* Error recovery for that rule moved down expression_statement: rule. */
explicit_constructor_invocation:
this_or_super OP_TK CP_TK SC_TK
{
$$ = build_method_invocation ($1, NULL_TREE);
$$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
$$ = java_method_add_stmt (current_function_decl, $$);
}
| this_or_super OP_TK argument_list CP_TK SC_TK
{
$$ = build_method_invocation ($1, $3);
$$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
$$ = java_method_add_stmt (current_function_decl, $$);
}
/* Added, JDK1.1 inner classes. Modified because the rule
'primary' couldn't work. */
| name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
......@@ -960,9 +959,16 @@ variable_initializers:
block:
OCB_TK CCB_TK
{ $$ = size_zero_node; }
| OCB_TK
| block_begin block_statements block_end
{ $$ = $3; }
;
block_begin:
OCB_TK
{ enter_block (); }
block_statements
;
block_end:
CCB_TK
{
maybe_absorb_scoping_blocks ();
......@@ -1101,11 +1107,19 @@ expression_statement:
| this_or_super OP_TK error
{yyerror ("')' expected"); RECOVER;}
| this_or_super OP_TK CP_TK error
{yyerror ("';' expected"); RECOVER;}
{
yyerror ("Constructor invocation must be first "
"thing in a constructor");
RECOVER;
}
| this_or_super OP_TK argument_list error
{yyerror ("')' expected"); RECOVER;}
| this_or_super OP_TK argument_list CP_TK error
{yyerror ("';' expected"); RECOVER;}
{
yyerror ("Constructor invocation must be first "
"thing in a constructor");
RECOVER;
}
| name DOT_TK SUPER_TK error
{yyerror ("'(' expected"); RECOVER;}
| name DOT_TK SUPER_TK OP_TK error
......@@ -1666,31 +1680,35 @@ method_invocation:
{ $$ = build_method_invocation ($1, $3); }
| primary DOT_TK identifier OP_TK CP_TK
{
tree invok = build_method_invocation ($3, NULL_TREE);
$$ = make_qualified_primary ($1, invok, $2.location);
if (TREE_CODE ($1) == THIS_EXPR)
$$ = build_this_super_qualified_invocation
(1, $3, NULL_TREE, 0, $2.location);
else
{
tree invok = build_method_invocation ($3, NULL_TREE);
$$ = make_qualified_primary ($1, invok, $2.location);
}
}
| primary DOT_TK identifier OP_TK argument_list CP_TK
{
tree invok = build_method_invocation ($3, $5);
$$ = make_qualified_primary ($1, invok, $2.location);
if (TREE_CODE ($1) == THIS_EXPR)
$$ = build_this_super_qualified_invocation
(1, $3, $5, 0, $2.location);
else
{
tree invok = build_method_invocation ($3, $5);
$$ = make_qualified_primary ($1, invok, $2.location);
}
}
| SUPER_TK DOT_TK identifier OP_TK CP_TK
{
tree invok;
tree wfl = build_wfl_node (super_identifier_node,
input_filename, 0, 0);
EXPR_WFL_LINECOL (wfl) = $1.location;
invok = build_method_invocation ($3, NULL_TREE);
$$ = make_qualified_primary (wfl, invok, $2.location);
{
$$ = build_this_super_qualified_invocation
(0, $3, NULL_TREE, $1.location, $2.location);
}
| SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
{
tree invok;
tree wfl = build_wfl_node (super_identifier_node,
input_filename, 0, 0);
EXPR_WFL_LINECOL (wfl) = $1.location;
invok = build_method_invocation ($3, $5);
$$ = make_qualified_primary (wfl, invok, $2.location);
$$ = build_this_super_qualified_invocation
(0, $3, $5, $1.location, $2.location);
}
/* Screws up thing. I let it here until I'm convinced it can
be removed. FIXME
......@@ -2006,6 +2024,10 @@ conditional_or_expression:
conditional_expression: /* Error handling here is weak */
conditional_or_expression
| conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
{
$$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
EXPR_WFL_LINECOL ($$) = $2.location;
}
| conditional_or_expression REL_QM_TK REL_CL_TK error
{
YYERROR_NOW;
......@@ -2079,9 +2101,20 @@ java_push_parser_context ()
ctxp->incomplete_class = ctxp->next->incomplete_class;
}
/* If the first file of a file list was a class file, no context
exists for a source file to be parsed. This boolean remembers that
java_parser_context_save_global might have created a dummy one, so
that java_parser_context_restore_global can pop it. */
static int extra_ctxp_pushed_p = 0;
void
java_parser_context_save_global ()
{
if (!ctxp)
{
java_push_parser_context ();
extra_ctxp_pushed_p = 1;
}
ctxp->finput = finput;
ctxp->lineno = lineno;
ctxp->current_class = current_class;
......@@ -2097,6 +2130,11 @@ java_parser_context_restore_global ()
current_class = ctxp->current_class;
input_filename = ctxp->filename;
current_function_decl = ctxp->current_function_decl;
if (extra_ctxp_pushed_p)
{
java_pop_parser_context (0);
extra_ctxp_pushed_p = 0;
}
}
void
......@@ -2375,6 +2413,7 @@ build_array_from_name (type, type_wfl, name, ret_name)
if (JPRIMITIVE_TYPE_P (type))
{
type = build_java_array_type (type, -1);
CLASS_LOADED_P (type) = 1;
more_dims--;
}
/* Otherwise, if we have a WFL for this type, use it (the type
......@@ -2804,10 +2843,15 @@ register_fields (flags, type, variable_list)
/* Type adjustment. We may have just readjusted TYPE because
the variable specified more dimensions. Make sure we have
a reference if we can and don't have one already. */
if (type != saved_type && !must_chain
&& (TREE_CODE (type) == RECORD_TYPE))
type = promote_type (type);
a reference if we can and don't have one already. Also
change the name if we have an init. */
if (type != saved_type)
{
if (!must_chain && (TREE_CODE (type) == RECORD_TYPE))
type = promote_type (type);
if (init)
EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
}
/* Set lineno to the line the field was found and create a
declaration for it */
......@@ -2848,6 +2892,7 @@ register_fields (flags, type, variable_list)
time of the generation of <init>. */
else
{
INITIALIZED_P (field_decl) = 1;
TREE_CHAIN (init) = ctxp->non_static_initialized;
ctxp->non_static_initialized = init;
}
......@@ -2856,36 +2901,49 @@ register_fields (flags, type, variable_list)
lineno = saved_lineno;
}
/* Generate the method <finit> that initializes fields initialized
upon declaration. */
static void
maybe_generate_finit ()
{
tree mdecl, current;
if (!ctxp->non_static_initialized || java_error_count)
return;
mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
ACC_PRIVATE|ACC_FINAL, void_type_node,
finit_identifier_node, NULL_TREE);
start_artificial_method_body (mdecl);
ctxp->non_static_initialized = nreverse (ctxp->non_static_initialized);
for (current = ctxp->non_static_initialized; current;
current = TREE_CHAIN (current))
java_method_add_stmt (mdecl,
build_debugable_stmt (EXPR_WFL_LINECOL (current),
current));
end_artificial_method_body (mdecl);
CLASS_HAS_FINIT_P (TREE_TYPE (ctxp->current_parsed_class)) = 1;
ctxp->non_static_initialized = NULL_TREE;
}
/* Check whether it is necessary to generate a <clinit> for the class
we just parsed. */
static void
maybe_generate_clinit ()
{
int saved_lineno;
tree meth, mdecl, c;
tree cclass, class_wfl;
tree mdecl, c;
if (!ctxp->static_initialized || java_error_count)
return;
cclass = TREE_TYPE (ctxp->current_parsed_class);
class_wfl = build_expr_wfl (DECL_NAME (TYPE_NAME (cclass)),
input_filename, 0, 0);
saved_lineno = lineno;
lineno = 0;
meth = make_node (FUNCTION_TYPE);
TREE_TYPE (meth) = void_type_node;
TYPE_ARG_TYPES (meth) = NULL_TREE;
mdecl = add_method (cclass, ACC_STATIC, clinit_identifier_node,
build_java_signature (meth));
lineno = saved_lineno;
DECL_SOURCE_LINE (mdecl) = 1;
DECL_SOURCE_LINE_MERGE (mdecl, 1);
source_start_java_method (mdecl);
enter_block ();
mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
ACC_STATIC, void_type_node,
clinit_identifier_node, NULL_TREE);
start_artificial_method_body (mdecl);
/* Keep initialization in order to enforce 8.5 */
ctxp->static_initialized = nreverse (ctxp->static_initialized);
......@@ -2898,11 +2956,11 @@ maybe_generate_clinit ()
/* We build the assignment expression that will initialize the
field to its value. There are strict rules on static
initializers (8.5). FIXME */
java_method_add_stmt (mdecl, c);
java_method_add_stmt (mdecl,
build_debugable_stmt (EXPR_WFL_LINECOL (c), c));
}
BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
exit_block ();
end_artificial_method_body (mdecl);
ctxp->static_initialized = NULL_TREE;
}
......@@ -2917,7 +2975,8 @@ static int patch_stage;
/* Check the method declaration and add the method to its current
class. If the argument list is known to contain incomplete types,
the method is partially added and the registration will be resume
once the method arguments resolved */
once the method arguments resolved. If TYPE is NULL, we're dealing
with a constructor. */
static tree
method_header (flags, type, mdecl, throws)
......@@ -2929,12 +2988,13 @@ method_header (flags, type, mdecl, throws)
tree this_class = TREE_TYPE (ctxp->current_parsed_class);
tree meth_name, returned_type, current;
int saved_lineno;
int constructor_ok = 0;
check_modifiers_consistency (flags);
/* There are some forbidden modifiers for an abstract method and its
class must be abstract as well. */
if (flags & ACC_ABSTRACT)
if (type && (flags & ACC_ABSTRACT))
{
ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
......@@ -2948,7 +3008,30 @@ method_header (flags, type, mdecl, throws)
IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
}
/* Things to be checked when declaring a constructor */
if (!type)
{
int ec = java_error_count;
/* 8.6: Constructor declarations: we might be trying to define a
method without specifying a return type. */
if (EXPR_WFL_NODE (id) != DECL_NAME (ctxp->current_parsed_class))
parse_error_context
(id, "Invalid method declaration, return type required");
/* 8.6.3: Constructor modifiers */
else
{
JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
}
/* If we found error here, we don't consider it's OK to tread
the method definition as a constructor, for the rest of this
function */
if (ec == java_error_count)
constructor_ok = 1;
}
/* Method declared within the scope of an interface are implicitly
abstract and public. Conflicts with other erroneously provided
......@@ -2970,7 +3053,17 @@ method_header (flags, type, mdecl, throws)
/* Modifiers context reset moved up, so abstract method declaration
modifiers can be later checked. */
meth_name = EXPR_WFL_NODE (id);
/* Set constructor returned type to void and method name to <init>,
unless we found an error identifier the constructor (in which
case we retain the original name) */
if (!type)
{
type = void_type_node;
if (constructor_ok)
meth_name = init_identifier_node;
}
else
meth_name = EXPR_WFL_NODE (id);
if (unresolved_type_p (type, &returned_type))
{
......@@ -3055,12 +3148,92 @@ method_header (flags, type, mdecl, throws)
redefinition error accurately. When method are verified,
DECL_NAME is reinstalled properly (using the content of the
WFL node ID) (see check_method_redefinition). We don't do that
when Object is being defined. */
when Object is being defined. Constructor <init> names will be
reinstalled the same way. */
if (TREE_TYPE (ctxp->current_parsed_class) != object_type_node)
DECL_NAME (meth) = id;
/* Set the flag if we correctly processed a constructor */
if (constructor_ok)
DECL_CONSTRUCTOR_P (meth) = 1;
return meth;
}
/* Complete the method declaration with METHOD_BODY. */
static void
complete_method_declaration (method_body)
tree method_body;
{
BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
maybe_absorb_scoping_blocks ();
/* Exit function's body */
exit_block ();
/* Merge last line of the function with first line, directly in the
function decl. It will be used to emit correct debug info. */
DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
}
/* Build a an error message for constructor circularity errors. */
static char *
constructor_circularity_msg (from, to)
tree from, to;
{
static char string [4096];
char *t = strdup (lang_printable_name (from, 0));
sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
free (t);
return string;
}
/* Verify a circular call to METH. Return 1 if an error is found, 0
otherwise. */
static int
verify_constructor_circularity (meth, current)
tree meth, current;
{
static tree list = NULL_TREE;
tree c;
for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
{
if (TREE_VALUE (c) == meth)
{
char *t;
if (list)
{
tree liste;
list = nreverse (list);
for (liste = list; liste; liste = TREE_CHAIN (liste))
{
parse_error_context
(TREE_PURPOSE (TREE_PURPOSE (liste)),
constructor_circularity_msg
(TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
java_error_count--;
}
}
t = strdup (lang_printable_name (meth, 0));
parse_error_context (TREE_PURPOSE (c),
"%s: recursive invocation of constructor `%s'",
constructor_circularity_msg (current, meth), t);
free (t);
list = NULL_TREE;
return 1;
}
}
for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
{
list = tree_cons (c, current, list);
if (verify_constructor_circularity (meth, TREE_VALUE (c)))
return 1;
list = TREE_CHAIN (list);
}
return 0;
}
/* Check modifiers that can be declared but exclusively */
static void
......@@ -3797,6 +3970,36 @@ java_check_final ()
{
}
/* Return a static string containing the DECL prototype string. If
DECL is a constructor, use the class name instead of the form
<init> */
static char *
get_printable_method_name (decl)
tree decl;
{
char *to_return;
tree name;
if (DECL_CONSTRUCTOR_P (decl))
{
name = DECL_NAME (decl);
DECL_NAME (decl) =
DECL_NAME (ctxp->current_parsed_class ?
ctxp->current_parsed_class : current_class);
}
to_return = lang_printable_name (decl, 0);
if (DECL_CONSTRUCTOR_P (decl))
DECL_NAME (decl) = name;
return to_return;
}
/* Track method being redefined inside the same class. As a side
effect, set DECL_NAME to an IDENTIFIER (prior entering this
function it's a FWL, so we can track errors more accurately */
static int
check_method_redefinition (class, method)
tree class, method;
......@@ -3804,14 +4007,17 @@ check_method_redefinition (class, method)
tree redef, name;
tree cl = DECL_NAME (method);
tree sig = TYPE_LANG_SPECIFIC (TREE_TYPE (method))->signature;
/* decl name of generated <clinit> doesn't need to be fixed and
/* decl name of artificial <clinit> and <finit> doesn't need to be fixed and
checked */
if (DECL_NAME (method) != clinit_identifier_node)
if (DECL_NAME (method) != clinit_identifier_node
&& DECL_NAME (method) != finit_identifier_node)
{
/* NAME is just the plain name when Object is being defined */
if (class != object_type_node)
name = DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
else
name = DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
init_identifier_node :
EXPR_WFL_NODE (DECL_NAME (method)));
else
name = DECL_NAME (method);
}
else
......@@ -3825,7 +4031,10 @@ check_method_redefinition (class, method)
break;
if (DECL_NAME (redef) == name && sig == t->signature)
{
parse_error_context (cl, "Duplicate method declaration");
parse_error_context
(cl, "Duplicate %s declaration `%s'",
(DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
get_printable_method_name (redef));
return 1;
}
}
......@@ -3843,7 +4052,7 @@ java_check_regular_methods (class_decl)
tree method;
tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
tree super_class = CLASSTYPE_SUPER (class);
int seen_constructor = 0;
int saw_constructor = 0;
TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
......@@ -3854,13 +4063,19 @@ java_check_regular_methods (class_decl)
tree method_wfl = DECL_NAME (method);
int aflags;
if (DECL_CONSTRUCTOR_P (method))
seen_constructor = 1;
/* Check for redefinitions */
if (check_method_redefinition (class, method))
continue;
/* If we see one constructor a mark so we don't generate the
default one. Also skip other verifications: constructors
can't be inherited hence hiden or overriden */
if (DECL_CONSTRUCTOR_P (method))
{
saw_constructor = 1;
continue;
}
sig = build_java_argument_signature (TREE_TYPE (method));
found = lookup_argument_method (super_class, DECL_NAME (method), sig);
......@@ -3951,19 +4166,20 @@ java_check_regular_methods (class_decl)
TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
if (!seen_constructor)
if (!saw_constructor)
{
/* No constructor seen, we craft one, at line 0 */
int saved_lineno = lineno;
tree meth, decl;
lineno = 0;
meth = make_node (FUNCTION_TYPE);
TREE_TYPE (meth) = void_type_node;
TYPE_ARG_TYPES (meth) = NULL_TREE;
decl = add_method (class, 0, init_identifier_node,
build_java_signature (meth));
int flags;
tree decl;
/* If the class is declared PUBLIC, the default constructor is
PUBLIC otherwise it has default access implied by no access
modifiers. */
flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
ACC_PUBLIC : 0);
decl = create_artificial_method (class, flags, void_type_node,
init_identifier_node, NULL_TREE);
DECL_CONSTRUCTOR_P (decl) = 1;
lineno = saved_lineno;
}
}
......@@ -4075,7 +4291,6 @@ java_check_abstract_methods (interface)
void
java_check_methods ()
{
tree current;
for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
if (CLASS_FROM_SOURCE_P (TREE_TYPE (current)))
......@@ -4633,6 +4848,47 @@ source_start_java_method (fndecl)
DECL_ARG_SLOT_COUNT (current_function_decl) = i;
}
/* Called during parsing. Creates an artificial method declaration. */
static tree
create_artificial_method (class, flags, type, name, args)
tree class;
int flags;
tree type, name, args;
{
int saved_lineno = lineno;
tree mdecl;
lineno = 0;
mdecl = make_node (FUNCTION_TYPE);
TREE_TYPE (mdecl) = type;
TYPE_ARG_TYPES (mdecl) = args;
mdecl = add_method (class, flags, name, build_java_signature (mdecl));
lineno = saved_lineno;
DECL_ARTIFICIAL (mdecl) = 1;
return mdecl;
}
/* Starts the body if an artifical method. */
static void
start_artificial_method_body (mdecl)
tree mdecl;
{
DECL_SOURCE_LINE (mdecl) = 1;
DECL_SOURCE_LINE_MERGE (mdecl, 1);
source_start_java_method (mdecl);
enter_block ();
}
static void
end_artificial_method_body (mdecl)
tree mdecl;
{
BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
exit_block ();
}
/* Called during expansion. Push decls formerly built from argument
list so they're usable during expansion. */
......@@ -4765,7 +5021,8 @@ add_stmt_to_compound (existing, type, stmt)
/* Hold THIS for the scope of the current public method decl. */
static tree current_this;
/* Layout all class found during parsing */
/* Layout all class found during parsing. Also fixes the order of
several field related lists. */
void
java_layout_classes ()
......@@ -4774,8 +5031,16 @@ java_layout_classes ()
for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
{
current_class = TREE_TYPE (current);
TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
if (!TYPE_SIZE (current_class))
/* Reverse the fields if it's necessary (they've already
reversed if the dummy field has been inserted at the
beginning of the list */
if (TYPE_FIELDS (current_class)
&& !DECL_IGNORED_P (TYPE_FIELDS (current_class)))
TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
/* Do a layout if necessary */
if (!TYPE_SIZE (current_class) || (current_class == object_type_node))
safe_layout_class (current_class);
}
}
......@@ -4819,6 +5084,14 @@ java_complete_expand_methods ()
java_complete_expand_method (decl);
}
/* Now verify constructor circularity (stop after the first one
we find) */
if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
if (DECL_CONSTRUCTOR_P (decl) &&
verify_constructor_circularity (decl, decl))
break;
/* Make the class data, register it and run the rest of decl
compilation on it */
if (!java_error_count && ! flag_emit_class_files)
......@@ -4841,51 +5114,11 @@ static void
java_complete_expand_method (mdecl)
tree mdecl;
{
int no_ac_found = 1;
/* We generate some code for an empty constructor */
if (DECL_CONSTRUCTOR_P (mdecl) && !DECL_FUNCTION_BODY (mdecl))
{
tree arg_list, func, call;
tree method_type = TREE_TYPE (mdecl);
tree class_type = CLASS_TO_HANDLE_TYPE (current_class);
tree self_type = (CLASSTYPE_SUPER (class_type) ?
CLASSTYPE_SUPER (class_type) : class_type);
tree method_signature =
TYPE_LANG_SPECIFIC (method_type)->signature;
tree method =
lookup_java_constructor (CLASS_TO_HANDLE_TYPE (self_type),
method_signature);
tree block, compound;
/* Fixe the begining/ending lines of the method so that with
no_line_numbers set to 1 it doesn't generate debug info at
line 1 for this artificial constructor. */
DECL_SOURCE_LINE (mdecl) = 1;
DECL_SOURCE_LINE_MERGE (mdecl, 1);
source_start_java_method (mdecl);
arg_list = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl));
enter_block ();
func = build_known_method_ref (method, method_type, self_type,
method_signature, arg_list);
if (! flag_emit_class_files)
func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
call = build (CALL_EXPR, TREE_TYPE (method_type), func,
build_tree_list (NULL_TREE, arg_list), NULL_TREE);
TREE_SIDE_EFFECTS (call) = 1;
call = build_class_init (self_type, call);
compound = java_method_add_stmt (mdecl, call);
block = exit_block ();
BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = block;
/* The function decl, its block and the compound statement
within this block are all of void type. */
TREE_TYPE (block) = TREE_TYPE (compound) =
TREE_TYPE (DECL_FUNCTION_BODY (mdecl)) = void_type_node;
exit_block ();
no_ac_found = 0;
}
/* Fix constructors before expanding them */
if (DECL_CONSTRUCTOR_P (mdecl))
fix_constructors (mdecl);
/* Expand functions that have a body */
if (DECL_FUNCTION_BODY (mdecl))
{
expand_start_java_method (mdecl);
......@@ -4900,12 +5133,17 @@ java_complete_expand_method (mdecl)
/* Install exceptions thrown with `throws' */
PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) && no_ac_found)
if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)))
java_complete_tree (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)));
/* Don't go any further if we've found error(s) during the
expansion */
if (!java_error_count)
source_end_java_method ();
else
{
pushdecl_force_head (DECL_ARGUMENTS (mdecl));
poplevel (1, 0, 1);
}
/* Pop the exceptions and sanity check */
POP_EXCEPTIONS();
......@@ -4914,6 +5152,136 @@ java_complete_expand_method (mdecl)
}
}
/* Craft a body for default constructor. Patch existing constructor
bodies with call to super() and field initialization statements if
necessary. */
static void
fix_constructors (mdecl)
tree mdecl;
{
tree body = DECL_FUNCTION_BODY (mdecl);
tree field_init;
/* The constructor body must be crafted by hand. It's the
constructor we defined when we realize we didn't have the
CLASSNAME() constructor */
if (!body)
{
tree compound;
/* It is an error for the compiler to generate a default
constructor if the superclass doesn't have a constructor that
takes no argument */
if (verify_constructor_super ())
{
tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (current_class));
char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
parse_error_context (lookup_cl (TYPE_NAME (current_class)),
"No constructor matching `%s()' found in "
"class `%s'", n, n);
}
start_artificial_method_body (mdecl);
/* We don't generate a super constructor invocation if we're
compiling java.lang.Object. build_super_invocation takes care
of that. */
compound = java_method_add_stmt (mdecl, build_super_invocation ());
/* Takes care of non static field initialization */
field_init = generate_field_initialization_code (current_class);
if (field_init)
compound = java_method_add_stmt (mdecl, field_init);
end_artificial_method_body (mdecl);
}
/* Search for an explicit constructor invocation */
else
{
int found = 0;
tree main_block = BLOCK_EXPR_BODY (body);
tree compound = NULL_TREE;
while (body)
switch (TREE_CODE (body))
{
case CALL_EXPR:
found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
body = NULL_TREE;
break;
case COMPOUND_EXPR:
case EXPR_WITH_FILE_LOCATION:
body = TREE_OPERAND (body, 0);
break;
case BLOCK:
body = BLOCK_EXPR_BODY (body);
break;
default:
found = 0;
body = NULL_TREE;
}
/* The constructor is missing an invocation of super() */
if (!found)
compound = add_stmt_to_compound (compound, NULL_TREE,
build_super_invocation ());
/* Also fix its initialized fields initialization */
field_init = generate_field_initialization_code (current_class);
if (field_init)
compound = add_stmt_to_compound (compound, NULL_TREE, field_init);
/* Fix the constructor main block if we're adding extra stmts */
if (compound)
{
compound = add_stmt_to_compound (compound, NULL_TREE,
BLOCK_EXPR_BODY (main_block));
BLOCK_EXPR_BODY (main_block) = compound;
}
}
}
/* Browse constructors in the super class, searching for a constructor
that doesn't take any argument. Return 0 if one is found, 1
otherwise. */
static int
verify_constructor_super ()
{
tree class = CLASSTYPE_SUPER (current_class);
if (!class)
return 0;
if (class)
{
tree mdecl;
for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
{
if (DECL_CONSTRUCTOR_P (mdecl)
&& !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (mdecl))))
return 0;
}
}
return 1;
}
/* Generate the code used to initialize field declared with an
initialization statement. For now, it returns a call the the
artificial function <finit>, if required. Always returns NULL if
nothing needs to be generated. */
static tree
generate_field_initialization_code (class)
tree class;
{
if (CLASS_HAS_FINIT_P (class))
return build_method_invocation (build_expr_wfl (finit_identifier_node,
input_filename, 0, 0),
NULL_TREE);
else
return NULL_TREE;
}
/* Expand finals. */
void
......@@ -4921,7 +5289,7 @@ java_expand_finals ()
{
}
/* Generate code for all context remembered for code generation */
/* Generate code for all context remembered for code generation. */
void
java_expand_classes ()
......@@ -4936,6 +5304,8 @@ java_expand_classes ()
java_parse_abort_on_error ();
java_check_final (); /* Check unitialized final */
java_parse_abort_on_error ();
if (flag_emit_class_files)
write_classfile (current_class);
}
}
......@@ -5089,6 +5459,15 @@ resolve_expression_name (id)
(TYPE_NAME (current_class))));
return error_mark_node;
}
/* Instance variables can't appear as an argument of
an explicit constructor invocation */
if (!fs && ctxp->explicit_constructor_p)
{
parse_error_context
(id, "Can't reference `%s' before the superclass "
"constructor has been called", IDENTIFIER_POINTER (name));
return error_mark_node;
}
decl = build_field_ref ((fs ? NULL_TREE : current_this),
current_class, name);
return (fs ? build_class_init (current_class, decl) : decl);
......@@ -5210,7 +5589,7 @@ resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
return 1;
*where_found =
patch_method_invocation_stmt (qual_wfl, decl, type,
&is_static, NULL);
&is_static, NULL, from_super);
if (*where_found == error_mark_node)
return 1;
*type_found = type = QUAL_DECL_TYPE (*where_found);
......@@ -5239,6 +5618,13 @@ resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
from_cast = 1;
continue;
case CONDITIONAL_EXPR:
*where_found = decl = java_complete_tree (qual_wfl);
if (decl == error_mark_node)
return 1;
*type_found = type = QUAL_DECL_TYPE (decl);
continue;
case ARRAY_REF:
/* If the access to the function call is a non static field,
build the code to access it. */
......@@ -5295,6 +5681,8 @@ resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
CLASSTYPE_SUPER (current_class),
build_this (EXPR_WFL_LINECOL (qual_wfl)));
*where_found = decl = java_complete_tree (node);
if (decl == error_mark_node)
return 1;
*type_found = type = QUAL_DECL_TYPE (decl);
from_super = from_type = 1;
continue;
......@@ -5560,15 +5948,17 @@ maybe_access_field (decl, where, type)
used. IS_STATIC is set to 1 if the invoked function is static. */
static tree
patch_method_invocation_stmt (patch, primary, where, is_static, ret_decl)
patch_method_invocation_stmt (patch, primary, where, is_static, ret_decl, super)
tree patch, primary, where;
int *is_static;
tree *ret_decl;
int super;
{
tree wfl = TREE_OPERAND (patch, 0);
tree args = TREE_OPERAND (patch, 1);
tree name = EXPR_WFL_NODE (wfl);
tree list, class_type;
int is_static_flag = 0;
/* Should be overriden if everything goes well. Otherwise, if
something fails, it should keep this value. It stop the
......@@ -5678,24 +6068,49 @@ patch_method_invocation_stmt (patch, primary, where, is_static, ret_decl)
/* We search constructor in their target class */
if (CALL_CONSTRUCTOR_P (patch))
{
class_to_search = resolve_no_layout (EXPR_WFL_NODE (wfl), NULL_TREE);
if (!class_to_search)
if (TREE_CODE (patch) == NEW_CLASS_EXPR)
class_to_search = EXPR_WFL_NODE (wfl);
else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
this_identifier_node)
class_to_search = NULL_TREE;
else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
super_identifier_node)
{
parse_error_context
(wfl, "Class `%s' not found in type declaration",
IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
PATCH_METHOD_RETURN_ERROR ();
if (CLASSTYPE_SUPER (current_class))
class_to_search =
DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
else
{
parse_error_context (wfl, "Can't invoke super constructor "
"on java.lang.Object");
PATCH_METHOD_RETURN_ERROR ();
}
}
/* Can't instantiate an abstract class */
if (CLASS_ABSTRACT (class_to_search))
/* Class to search is NULL if we're searching the current one */
if (class_to_search)
{
parse_error_context
(wfl, "Class `%s' is an abstract class. It can't be "
"instantiated", IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
PATCH_METHOD_RETURN_ERROR ();
class_to_search = resolve_no_layout (class_to_search, NULL_TREE);
if (!class_to_search)
{
parse_error_context
(wfl, "Class `%s' not found in type declaration",
IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
PATCH_METHOD_RETURN_ERROR ();
}
/* Can't instantiate an abstract class */
if (CLASS_ABSTRACT (class_to_search))
{
parse_error_context
(wfl, "Class `%s' is an abstract class. It can't be "
"instantiated", IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
PATCH_METHOD_RETURN_ERROR ();
}
class_to_search = TREE_TYPE (class_to_search);
}
class_to_search = TREE_TYPE (class_to_search);
else
class_to_search = current_class;
lc = 1;
}
/* This is a regular search in the local class, unless an
......@@ -5721,11 +6136,12 @@ patch_method_invocation_stmt (patch, primary, where, is_static, ret_decl)
class_to_search, primary))
PATCH_METHOD_RETURN_ERROR ();
/* Non static/constructor methods are called with the current
object extra argument. If method is resolved as a primary,
use the primary otherwise use the current THIS. */
/* Non static methods are called with the current object extra
argument. If patch a `new TYPE()', the argument is the value
returned by the object allocator. If method is resolved as a
primary, use the primary otherwise use the current THIS. */
args = nreverse (args);
if (!CALL_CONSTRUCTOR_P (patch) && !METHOD_STATIC (list))
if (!METHOD_STATIC (list) && TREE_CODE (patch) != NEW_CLASS_EXPR)
args = tree_cons (NULL_TREE, primary ? primary : current_this, args);
class_type = class_to_search;
......@@ -5750,15 +6166,27 @@ patch_method_invocation_stmt (patch, primary, where, is_static, ret_decl)
free (fct_name);
PATCH_METHOD_RETURN_ERROR ();
}
if (is_static)
*is_static = METHOD_STATIC (list);
is_static_flag = METHOD_STATIC (list);
/* In the context of an explicit constructor invocation, we can't invoke
any method relying on `this' */
if (ctxp->explicit_constructor_p
&& !is_static_flag && (!primary || primary == current_this))
{
parse_error_context
(wfl, "Can't reference `this' before the superclass constructor has "
"been called");
PATCH_METHOD_RETURN_ERROR ();
}
java_parser_context_restore_global ();
if (is_static)
*is_static = is_static_flag;
/* Sometimes, we want the decl of the selected method. Such as for
EH checking */
if (ret_decl)
*ret_decl = list;
return patch_invoke (patch, list, args);
return patch_invoke (patch, list, args, super);
}
/* Check that we're not trying to do a static reference to a method in
......@@ -5786,35 +6214,37 @@ check_for_static_method_reference (wfl, node, method, where, primary)
mode. */
static tree
patch_invoke (patch, method, args)
patch_invoke (patch, method, args, from_super)
tree patch, method, args;
int from_super;
{
int im;
tree dtable, func;
tree signature = build_java_signature (TREE_TYPE (method));
tree original_call, t, ta;
/* Last step for args: convert build-in types */
for (t = TYPE_ARG_TYPES (TREE_TYPE (method)),
ta = args; t && ta; t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
/* Last step for args: convert build-in types. */
for (t = TYPE_ARG_TYPES (TREE_TYPE (method)), ta = args;
t && ta; t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
switch ((im = invocation_mode (method, 0)))
switch ((im = invocation_mode (method, from_super)))
{
case INVOKE_VIRTUAL:
dtable = invoke_build_dtable (0, args);
func = build_invokevirtual (dtable, method);
break;
case INVOKE_SUPER:
case INVOKE_STATIC:
func = build_known_method_ref (method, TREE_TYPE (method),
DECL_CONTEXT (method), signature, args);
break;
default:
fatal ("Unknown invocation mode - build_invoke");
fatal ("Unknown invocation mode `%d' - build_invoke", im);
return NULL_TREE;
}
......@@ -5825,11 +6255,12 @@ patch_invoke (patch, method, args)
TREE_OPERAND (patch, 1) = args;
original_call = patch;
/* We're calling a constructor. New is called an its returned value
is an argument to the constructor. We build a COMPOUND_EXPR and
use saved expression so that the overall NEW expression value is
a pointer to a newly created and initialized class. */
if (CALL_CONSTRUCTOR_P (original_call))
/* We're processing a `new TYPE ()' form. New is called an its
returned value is the first argument to the constructor. We build
a COMPOUND_EXPR and use saved expression so that the overall NEW
expression value is a pointer to a newly created and initialized
class. */
if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
{
tree class = DECL_CONTEXT (method);
tree c1, saved_new, size, new;
......@@ -5859,21 +6290,21 @@ invocation_mode (method, super)
{
int access = get_access_flags_from_decl (method);
if (super)
return INVOKE_SUPER;
if (access & ACC_STATIC || access & ACC_FINAL)
return INVOKE_STATIC;
if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
return INVOKE_STATIC;
if (super)
return INVOKE_SUPER;
if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
return INVOKE_INTERFACE;
if (DECL_CONSTRUCTOR_P (method))
return INVOKE_STATIC;
return INVOKE_VIRTUAL;
}
......@@ -5932,11 +6363,9 @@ lookup_method_invoke (lc, cl, class, name, arg_list)
char string [4096];
if (!cm || not_accessible_p (class, cm, 0))
continue;
signature = build_java_argument_signature (TREE_TYPE (cm));
sprintf
(string, " `%s(%s)' in `%s'%s",
IDENTIFIER_POINTER (name),
IDENTIFIER_POINTER (signature),
(string, " `%s' in `%s'%s",
get_printable_method_name (cm),
IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
(TREE_CHAIN (current) ? "\n" : ""));
obstack_grow (&temporary_obstack, string, strlen (string));
......@@ -5946,8 +6375,11 @@ lookup_method_invoke (lc, cl, class, name, arg_list)
}
/* Issue the error message */
signature = build_java_argument_signature (method);
parse_error_context (cl, "Can't find method `%s(%s)' in class `%s'%s",
IDENTIFIER_POINTER (name),
parse_error_context (cl, "Can't find %s `%s(%s)' in class `%s'%s",
(lc ? "constructor" : "method"),
(lc ?
IDENTIFIER_POINTER(DECL_NAME (TYPE_NAME (class))) :
IDENTIFIER_POINTER (name)),
IDENTIFIER_POINTER (signature),
IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))),
(candidates ? candidates : ""));
......@@ -5969,8 +6401,7 @@ find_applicable_accessible_methods_list (class, name, arglist)
method != NULL_TREE; method = TREE_CHAIN (method))
{
/* Names have to match and we're not looking for constructor */
if (DECL_NAME (method) != name
|| DECL_CONSTRUCTOR_P (method))
if (DECL_NAME (method) != name || DECL_CONSTRUCTOR_P (method))
continue;
if (argument_types_convertible (method, arglist))
......@@ -6145,7 +6576,10 @@ qualify_ambiguous_name (id)
{
qual = TREE_CHAIN (qual);
qual_wfl = QUAL_WFL (qual);
name = EXPR_WFL_NODE (qual_wfl);
if (TREE_CODE (qual_wfl) == CALL_EXPR)
again = 1;
else
name = EXPR_WFL_NODE (qual_wfl);
this_found = 1;
}
/* If we have a SUPER, we set the context accordingly */
......@@ -6164,6 +6598,13 @@ qualify_ambiguous_name (id)
/* Do one more interation to set things up */
super_found = again = 1;
}
/* Loop one more time if we're dealing with ?: up front */
if (TREE_CODE (qual_wfl) == CONDITIONAL_EXPR)
{
qual = TREE_CHAIN (qual);
qual_wfl = QUAL_WFL (qual);
again = 1;
}
} while (again);
/* If name appears within the scope of a location variable
......@@ -6283,7 +6724,7 @@ static tree
java_complete_tree (node)
tree node;
{
tree nn, cn, wfl_op1, wfl_op2;
tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
int flag;
/* CONVERT_EXPR always has its type set, even though it needs to be
......@@ -6394,6 +6835,16 @@ java_complete_tree (node)
return patch_if_else_statement (node);
break;
case CONDITIONAL_EXPR:
/* Condition */
wfl_op1 = TREE_OPERAND (node, 0);
COMPLETE_CHECK_OP_0 (node);
wfl_op2 = TREE_OPERAND (node, 1);
COMPLETE_CHECK_OP_1 (node);
wfl_op3 = TREE_OPERAND (node, 2);
COMPLETE_CHECK_OP_2 (node);
return patch_conditional_expr (node, wfl_op1, wfl_op2);
/* 3- Expression section */
case COMPOUND_EXPR:
TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
......@@ -6459,11 +6910,21 @@ java_complete_tree (node)
return error_mark_node;
else
{
tree decl;
tree decl, wfl = TREE_OPERAND (node, 0);
int in_this = CALL_THIS_CONSTRUCTOR_P (node);
node = patch_method_invocation_stmt (node, NULL_TREE,
NULL_TREE, 0, &decl);
NULL_TREE, 0, &decl, 0);
if (node != error_mark_node)
check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
{
check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
/* If we call this(...), register signature and positions */
if (in_this)
DECL_CONSTRUCTOR_CALLS (current_function_decl) =
tree_cons (wfl, decl,
DECL_CONSTRUCTOR_CALLS (current_function_decl));
}
return node;
}
......@@ -6585,7 +7046,8 @@ java_complete_tree (node)
TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
if (TREE_OPERAND (node, 1) == error_mark_node)
return error_mark_node;
TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
if (!flag_emit_class_files)
TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
return patch_array_ref (node, wfl_op1, wfl_op2);
case THIS_EXPR:
......@@ -6598,6 +7060,15 @@ java_complete_tree (node)
TREE_TYPE (node) = error_mark_node;
return error_mark_node;
}
if (ctxp->explicit_constructor_p)
{
EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
parse_error_context
(wfl_operator, "Can't reference `this' or `super' before the "
"superclass constructor has been called");
TREE_TYPE (node) = error_mark_node;
return error_mark_node;
}
return current_this;
default:
......@@ -6622,6 +7093,7 @@ complete_function_arguments (node)
int flag = 0;
tree cn;
ctxp->explicit_constructor_p += (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
{
tree wfl = TREE_VALUE (cn), parm, temp;
......@@ -6644,6 +7116,7 @@ complete_function_arguments (node)
INITIALIZED_P (parm) = 1;
}
}
ctxp->explicit_constructor_p -= (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
return flag;
}
......@@ -6769,6 +7242,40 @@ maybe_absorb_scoping_blocks ()
are building incomplete tree nodes and the patch_* functions that
are completing them. */
/* Build a super() constructor invocation. Returns size_zero_node if
we're currently dealing with the class java.lang.Object. */
static tree
build_super_invocation ()
{
if (current_class == object_type_node)
return size_zero_node;
else
{
tree super_wfl = build_wfl_node (super_identifier_node,
input_filename, 0, 0);
return build_method_invocation (super_wfl, NULL_TREE);
}
}
/* Build a SUPER/THIS qualified method invocation. */
static tree
build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
int use_this;
tree name, args;
int lloc, rloc;
{
tree invok;
tree wfl =
build_wfl_node ((use_this ? this_identifier_node : super_identifier_node),
input_filename, 0, 0);
EXPR_WFL_LINECOL (wfl) = lloc;
invok = build_method_invocation (name, args);
return make_qualified_primary (wfl, invok, rloc);
}
/* Build an incomplete CALL_EXPR node. */
static tree
......@@ -6962,7 +7469,7 @@ patch_assignment (node, wfl_op1, wfl_op2)
assignment into an array element, return it here. */
if (TREE_CODE (node) == COMPOUND_EXPR)
return node;
TREE_OPERAND (node, 0) = lvalue;
TREE_OPERAND (node, 1) = new_rhs;
TREE_TYPE (node) = lhs_type;
......@@ -7845,7 +8352,7 @@ patch_unaryop (node, wfl_op)
TREE_TYPE (node) = error_mark_node;
error_found = 1;
}
else if (FIELD_FINAL (op))
else if (DECL_P (op) && FIELD_FINAL (op))
{
parse_error_context
(wfl_op, "Can't assign a value to the final variable `%s'",
......@@ -8315,11 +8822,19 @@ patch_return (node)
if (error_found)
{
char *t = strdup (lang_printable_name (mtype, 0));
parse_error_context (wfl_operator, "`return' with%s value from `%s %s'",
(error_found == 1 ? "" : "out"), t,
lang_printable_name (meth, 0));
free (t);
if (!DECL_CONSTRUCTOR_P (meth))
{
char *t = strdup (lang_printable_name (mtype, 0));
parse_error_context (wfl_operator,
"`return' with%s value from `%s %s'",
(error_found == 1 ? "" : "out"),
t, lang_printable_name (meth, 0));
free (t);
}
else
parse_error_context (wfl_operator,
"`return' with value from constructor `%s'",
lang_printable_name (meth, 0));
return error_mark_node;
}
......@@ -9364,3 +9879,107 @@ purge_unchecked_exceptions (mdecl)
/* List is inverted here, but it doesn't matter */
DECL_FUNCTION_THROWS (mdecl) = new;
}
/* 15.24 Conditional Operator ?: */
static tree
patch_conditional_expr (node, wfl_cond, wfl_op1)
tree node, wfl_cond, wfl_op1;
{
tree cond = TREE_OPERAND (node, 0);
tree op1 = TREE_OPERAND (node, 1);
tree op2 = TREE_OPERAND (node, 2);
tree t1 = TREE_TYPE (op1);
tree t2 = TREE_TYPE (op2);
tree resulting_type = NULL_TREE;
int error_found = 0;
/* The first expression must be a boolean */
if (TREE_TYPE (cond) != boolean_type_node)
{
SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
"convert `%s' to `boolean'",
lang_printable_name (TREE_TYPE (cond), 0));
error_found = 1;
}
/* Second and third can be numeric, boolean (i.e. primitive),
references or null. Anything else results in an error */
if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
|| ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
&& (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
|| (t1 == boolean_type_node && t2 == boolean_type_node)))
error_found = 1;
/* Determine the type of the conditional expression. Same types are
easy to deal with */
else if (t1 == t2)
resulting_type = t1;
/* There are different rules for numeric types */
else if (JNUMERIC_TYPE_P (t1))
{
/* if byte/short found, the resulting type is short */
if ((t1 == byte_type_node && t2 == short_type_node)
|| (t1 == short_type_node && t2 == byte_type_node))
resulting_type = short_type_node;
/* If t1 is a constant int and t2 is of type byte, short or char
and t1's value fits in t2, then the resulting type is t2 */
else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
&& JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
resulting_type = t2;
/* If t2 is a constant int and t1 is of type byte, short or char
and t2's value fits in t1, then the resulting type is t1 */
else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
&& JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
resulting_type = t1;
/* Otherwise, binary numeric promotion is applied and the
resulting type is the promoted type of operand 1 and 2 */
else
resulting_type = binary_numeric_promotion (t2, t2,
&TREE_OPERAND (node, 1),
&TREE_OPERAND (node, 2));
}
/* Cases of a reference and a null type */
else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
resulting_type = t1;
else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
resulting_type = t2;
/* Last case: different reference types. If a type can be converted
into the other one by assignment conversion, the latter
determines the type of the expression */
else if ((resulting_type = try_reference_assignconv (t1, op2)))
resulting_type = promote_type (t1);
else if ((resulting_type = try_reference_assignconv (t2, op1)))
resulting_type = promote_type (t2);
/* If we don't have any resulting type, we're in trouble */
if (!resulting_type)
{
char *t = strdup (lang_printable_name (t1, 0));
SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
"convert `%s' to `%s'", t,
lang_printable_name (t2, 0));
free (t);
error_found = 1;
}
if (error_found)
{
TREE_TYPE (node) = error_mark_node;
return error_mark_node;
}
TREE_TYPE (node) = resulting_type;
TREE_SET_CODE (node, COND_EXPR);
return node;
}
......@@ -546,7 +546,6 @@ build_java_signature (type)
{
TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
perm_calloc (1, sizeof (struct lang_type));
}
sig = TYPE_LANG_SPECIFIC (type)->signature;
if (sig == NULL_TREE)
......@@ -600,7 +599,7 @@ build_java_signature (type)
obstack_grow (&temporary_obstack,
IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
obstack_1grow (&temporary_obstack, ')');
t = build_java_signature (TREE_TYPE (type));
obstack_grow0 (&temporary_obstack,
IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment