* configure.in: When removing Makefiles to force a reconfigure, also
[official-gcc.git] / gcc / java / decl.c
blob912f85479fa91dc94f07e21d7643cf0930e6cf9f
1 /* Process declarations and variables for the GNU compiler for the
2 Java(TM) language.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc. */
27 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "real.h"
36 #include "toplev.h"
37 #include "flags.h"
38 #include "java-tree.h"
39 #include "jcf.h"
40 #include "function.h"
41 #include "expr.h"
42 #include "libfuncs.h"
43 #include "except.h"
44 #include "java-except.h"
45 #include "ggc.h"
46 #include "timevar.h"
47 #include "cgraph.h"
48 #include "tree-inline.h"
49 #include "target.h"
50 #include "version.h"
51 #include "tree-iterator.h"
52 #include "langhooks.h"
54 #if defined (DEBUG_JAVA_BINDING_LEVELS)
55 extern void indent (void);
56 #endif
58 static tree push_jvm_slot (int, tree);
59 static tree lookup_name_current_level (tree);
60 static tree push_promoted_type (const char *, tree);
61 static struct binding_level *make_binding_level (void);
62 static tree create_primitive_vtable (const char *);
63 static tree check_local_unnamed_variable (tree, tree, tree);
64 static void parse_version (void);
67 /* The following ABI flags are used in the high-order bits of the version
68 ID field. The version ID number itself should never be larger than
69 0xfffff, so it should be safe to use top 12 bits for these flags. */
71 #define FLAG_BINARYCOMPAT_ABI (1<<31) /* Class is built with the BC-ABI. */
73 #define FLAG_BOOTSTRAP_LOADER (1<<30) /* Used when defining a class that
74 should be loaded by the bootstrap
75 loader. */
77 /* If an ABI change is made within a GCC release series, rendering current
78 binaries incompatible with the old runtimes, this number can be set to
79 enforce the compatibility rules. */
80 #define MINOR_BINARYCOMPAT_ABI_VERSION 0
82 /* The runtime may recognize a variety of BC ABIs (objects generated by
83 different version of gcj), but will probably always require strict
84 matching for the ordinary (C++) ABI. */
86 /* The version ID of the BC ABI that we generate. This must be kept in
87 sync with parse_version(), libgcj, and reality (if the BC format changes,
88 this must change). */
89 #define GCJ_CURRENT_BC_ABI_VERSION \
90 (4 * 100000 + 0 * 1000 + MINOR_BINARYCOMPAT_ABI_VERSION)
92 /* The ABI version number. */
93 tree gcj_abi_version;
95 /* Name of the Cloneable class. */
96 tree java_lang_cloneable_identifier_node;
98 /* Name of the Serializable class. */
99 tree java_io_serializable_identifier_node;
101 /* The DECL_MAP is a mapping from (index, type) to a decl node.
102 If index < max_locals, it is the index of a local variable.
103 if index >= max_locals, then index-max_locals is a stack slot.
104 The DECL_MAP mapping is represented as a TREE_VEC whose elements
105 are a list of decls (VAR_DECL or PARM_DECL) chained by
106 DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
107 we search the chain for a decl with a matching TREE_TYPE. */
109 static GTY(()) tree decl_map;
111 /* The base_decl_map is contains one variable of ptr_type: this is
112 used to contain every variable of reference type that is ever
113 stored in a local variable slot. */
115 static GTY(()) tree base_decl_map;
117 /* An index used to make temporary identifiers unique. */
118 static int uniq;
120 /* A list of local variables VAR_DECLs for this method that we have seen
121 debug information, but we have not reached their starting (byte) PC yet. */
123 static GTY(()) tree pending_local_decls;
125 /* The decl for "_Jv_ResolvePoolEntry". */
126 tree soft_resolvepoolentry_node;
128 /* The decl for the .constants field of an instance of Class. */
129 tree constants_field_decl_node;
131 /* The decl for the .data field of an instance of Class. */
132 tree constants_data_field_decl_node;
134 #if defined(DEBUG_JAVA_BINDING_LEVELS)
135 int binding_depth = 0;
136 int is_class_level = 0;
137 int current_pc;
139 void
140 indent (void)
142 int i;
144 for (i = 0; i < binding_depth*2; i++)
145 putc (' ', stderr);
147 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
149 /* True if decl is a named local variable, i.e. if it is an alias
150 that's used only for debugging purposes. */
152 static bool
153 debug_variable_p (tree decl)
155 if (TREE_CODE (decl) == PARM_DECL)
156 return false;
158 if (LOCAL_SLOT_P (decl))
159 return false;
161 return true;
164 static tree
165 push_jvm_slot (int index, tree decl)
167 DECL_CONTEXT (decl) = current_function_decl;
168 layout_decl (decl, 0);
170 /* Now link the decl into the decl_map. */
171 if (DECL_LANG_SPECIFIC (decl) == NULL)
173 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
174 DECL_LOCAL_START_PC (decl) = 0;
175 DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
176 DECL_LOCAL_SLOT_NUMBER (decl) = index;
178 DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
179 TREE_VEC_ELT (decl_map, index) = decl;
181 return decl;
184 /* Find the best declaration based upon type. If 'decl' fits 'type' better
185 than 'best', return 'decl'. Otherwise return 'best'. */
187 static tree
188 check_local_unnamed_variable (tree best, tree decl, tree type)
190 tree decl_type = TREE_TYPE (decl);
192 gcc_assert (! LOCAL_VAR_OUT_OF_SCOPE_P (decl));
194 /* Use the same decl for all integer types <= 32 bits. This is
195 necessary because sometimes a value is stored as (for example)
196 boolean but loaded as int. */
197 if (decl_type == type
198 || (INTEGRAL_TYPE_P (decl_type)
199 && INTEGRAL_TYPE_P (type)
200 && TYPE_PRECISION (decl_type) <= 32
201 && TYPE_PRECISION (type) <= 32
202 && TYPE_PRECISION (decl_type) >= TYPE_PRECISION (type))
203 /* ptr_type_node is used for null pointers, which are
204 assignment compatible with everything. */
205 || (TREE_CODE (decl_type) == POINTER_TYPE
206 && type == ptr_type_node)
207 /* Whenever anyone wants to use a slot that is initially
208 occupied by a PARM_DECL of pointer type they must get that
209 decl, even if they asked for a pointer to a different type.
210 However, if someone wants a scalar variable in a slot that
211 initially held a pointer arg -- or vice versa -- we create a
212 new VAR_DECL.
214 ???: As long as verification is correct, this will be a
215 compatible type. But maybe we should create a dummy variable
216 and replace all references to it with the DECL and a
217 NOP_EXPR.
219 || (TREE_CODE (decl_type) == POINTER_TYPE
220 && TREE_CODE (decl) == PARM_DECL
221 && TREE_CODE (type) == POINTER_TYPE))
223 if (best == NULL_TREE
224 || (decl_type == type && TREE_TYPE (best) != type))
225 return decl;
228 return best;
232 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
233 that is valid at PC (or -1 if any pc).
234 If there is no existing matching decl, allocate one. */
236 tree
237 find_local_variable (int index, tree type, int pc ATTRIBUTE_UNUSED)
239 tree tmp = TREE_VEC_ELT (decl_map, index);
240 tree decl = NULL_TREE;
242 /* Scan through every declaration that has been created in this
243 slot. We're only looking for variables that correspond to local
244 index declarations and PARM_DECLs, not named variables: such
245 local variables are used only for debugging information. */
246 while (tmp != NULL_TREE)
248 if (! debug_variable_p (tmp))
249 decl = check_local_unnamed_variable (decl, tmp, type);
250 tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
253 /* gcj has a function called promote_type(), which is used by both
254 the bytecode compiler and the source compiler. Unfortunately,
255 the type systems for the Java VM and the Java language are not
256 the same: a boolean in the VM promotes to an int, not to a wide
257 boolean. If our caller wants something to hold a boolean, that
258 had better be an int, because that slot might be re-used
259 later in integer context. */
260 if (TREE_CODE (type) == BOOLEAN_TYPE)
261 type = integer_type_node;
263 /* If we don't find a match, create one with the type passed in.
264 The name of the variable is #n#m, which n is the variable index
265 in the local variable area and m is a dummy identifier for
266 uniqueness -- multiple variables may share the same local
267 variable index. We don't call pushdecl() to push pointer types
268 into a binding expr because they'll all be replaced by a single
269 variable that is used for every reference in that local variable
270 slot. */
271 if (! decl)
273 char buf[64];
274 tree name;
275 sprintf (buf, "#slot#%d#%d", index, uniq++);
276 name = get_identifier (buf);
277 decl = build_decl (VAR_DECL, name, type);
278 DECL_IGNORED_P (decl) = 1;
279 DECL_ARTIFICIAL (decl) = 1;
280 decl = push_jvm_slot (index, decl);
281 LOCAL_SLOT_P (decl) = 1;
283 if (TREE_CODE (type) != POINTER_TYPE)
284 pushdecl_function_level (decl);
287 /* As well as creating a local variable that matches the type, we
288 also create a base variable (of ptr_type) that will hold all its
289 aliases. */
290 if (TREE_CODE (type) == POINTER_TYPE
291 && ! TREE_VEC_ELT (base_decl_map, index))
293 char buf[64];
294 tree name;
295 tree base_decl;
296 sprintf (buf, "#ref#%d#%d", index, uniq++);
297 name = get_identifier (buf);
298 base_decl
299 = TREE_VEC_ELT (base_decl_map, index)
300 = build_decl (VAR_DECL, name, ptr_type_node);
301 pushdecl_function_level (base_decl);
302 DECL_IGNORED_P (base_decl) = 1;
303 DECL_ARTIFICIAL (base_decl) = 1;
306 return decl;
309 /* Called during gimplification for every variable. If the variable
310 is a temporary of pointer type, replace it with a common variable
311 thath is used to hold all pointer types that are ever stored in
312 that slot. Set WANT_LVALUE if you want a variable that is to be
313 written to. */
315 tree
316 java_replace_reference (tree var_decl, bool want_lvalue)
318 tree decl_type;
320 if (! base_decl_map)
321 return var_decl;
323 decl_type = TREE_TYPE (var_decl);
325 if (TREE_CODE (decl_type) == POINTER_TYPE)
327 if (DECL_LANG_SPECIFIC (var_decl)
328 && LOCAL_SLOT_P (var_decl))
330 int index = DECL_LOCAL_SLOT_NUMBER (var_decl);
331 tree base_decl = TREE_VEC_ELT (base_decl_map, index);
333 gcc_assert (base_decl);
334 if (! want_lvalue)
335 base_decl = build1 (NOP_EXPR, decl_type, base_decl);
337 return base_decl;
341 return var_decl;
345 /* Same as find_local_index, except that INDEX is a stack index. */
347 tree
348 find_stack_slot (int index, tree type)
350 return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
351 type, -1);
354 struct binding_level GTY(())
356 /* A chain of _DECL nodes for all variables, constants, functions,
357 * and typedef types. These are in the reverse of the order supplied.
359 tree names;
361 /* For each level, a list of shadowed outer-level local definitions
362 to be restored when this level is popped.
363 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
364 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
365 tree shadowed;
367 /* For each level (except not the global one),
368 a chain of BLOCK nodes for all the levels
369 that were entered and exited one level down. */
370 tree blocks;
372 /* The binding level which this one is contained in (inherits from). */
373 struct binding_level *level_chain;
375 /* The bytecode PC that marks the end of this level. */
376 int end_pc;
377 /* The bytecode PC that marks the start of this level. */
378 int start_pc;
380 /* The statements in this binding level. */
381 tree stmts;
383 /* An exception range associated with this binding level. */
384 struct eh_range * GTY((skip (""))) exception_range;
386 /* Binding depth at which this level began. Used only for debugging. */
387 unsigned binding_depth;
390 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
392 /* The binding level currently in effect. */
394 static GTY(()) struct binding_level *current_binding_level;
396 /* A chain of binding_level structures awaiting reuse. */
398 static GTY(()) struct binding_level *free_binding_level;
400 /* The outermost binding level, for names of file scope.
401 This is created when the compiler is started and exists
402 through the entire run. */
404 static GTY(()) struct binding_level *global_binding_level;
406 /* The binding level that holds variables declared at the outermost
407 level within a function body. */
409 static struct binding_level *function_binding_level;
411 /* A PC value bigger than any PC value we may ever may encounter. */
413 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
415 /* Binding level structures are initialized by copying this one. */
417 static const struct binding_level clear_binding_level
419 NULL_TREE, /* names */
420 NULL_TREE, /* shadowed */
421 NULL_TREE, /* blocks */
422 NULL_BINDING_LEVEL, /* level_chain */
423 LARGEST_PC, /* end_pc */
424 0, /* start_pc */
425 NULL, /* stmts */
426 NULL, /* exception_range */
427 0, /* binding_depth */
430 #if 0
431 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
432 that have names. Here so we can clear out their names' definitions
433 at the end of the function. */
435 static tree named_labels;
437 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
439 static tree shadowed_labels;
440 #endif
442 tree java_global_trees[JTI_MAX];
444 /* Build (and pushdecl) a "promoted type" for all standard
445 types shorter than int. */
447 static tree
448 push_promoted_type (const char *name, tree actual_type)
450 tree type = make_node (TREE_CODE (actual_type));
451 #if 1
452 tree in_min = TYPE_MIN_VALUE (int_type_node);
453 tree in_max = TYPE_MAX_VALUE (int_type_node);
454 #else
455 tree in_min = TYPE_MIN_VALUE (actual_type);
456 tree in_max = TYPE_MAX_VALUE (actual_type);
457 #endif
458 TYPE_MIN_VALUE (type) = copy_node (in_min);
459 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
460 TYPE_MAX_VALUE (type) = copy_node (in_max);
461 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
462 TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
463 TYPE_STRING_FLAG (type) = TYPE_STRING_FLAG (actual_type);
464 layout_type (type);
465 pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
466 return type;
469 /* Return tree that represents a vtable for a primitive array. */
470 static tree
471 create_primitive_vtable (const char *name)
473 tree r;
474 char buf[50];
476 sprintf (buf, "_Jv_%sVTable", name);
477 r = build_decl (VAR_DECL, get_identifier (buf), ptr_type_node);
478 DECL_EXTERNAL (r) = 1;
479 return r;
482 static tree
483 do_nothing (tree t)
485 return t;
488 /* Parse the version string and compute the ABI version number. */
489 static void
490 parse_version (void)
492 const char *p = version_string;
493 unsigned int major = 0, minor = 0;
494 unsigned int abi_version;
496 /* Skip leading junk. */
497 while (*p && !ISDIGIT (*p))
498 ++p;
499 gcc_assert (*p);
501 /* Extract major version. */
502 while (ISDIGIT (*p))
504 major = major * 10 + *p - '0';
505 ++p;
508 gcc_assert (*p == '.' && ISDIGIT (p[1]));
509 ++p;
511 /* Extract minor version. */
512 while (ISDIGIT (*p))
514 minor = minor * 10 + *p - '0';
515 ++p;
518 if (flag_indirect_dispatch)
520 abi_version = GCJ_CURRENT_BC_ABI_VERSION;
521 abi_version |= FLAG_BINARYCOMPAT_ABI;
523 else /* C++ ABI */
525 /* Implicit in this computation is the idea that we won't break the
526 old-style binary ABI in a sub-minor release (e.g., from 4.0.0 to
527 4.0.1). */
528 abi_version = 100000 * major + 1000 * minor;
530 if (flag_bootstrap_classes)
531 abi_version |= FLAG_BOOTSTRAP_LOADER;
533 gcj_abi_version = build_int_cstu (ptr_type_node, abi_version);
536 void
537 java_init_decl_processing (void)
539 tree endlink;
540 tree field = NULL_TREE;
541 tree t;
543 init_class_processing ();
545 current_function_decl = NULL;
546 current_binding_level = NULL_BINDING_LEVEL;
547 free_binding_level = NULL_BINDING_LEVEL;
548 pushlevel (0); /* make the binding_level structure for global names */
549 global_binding_level = current_binding_level;
551 /* The code here must be similar to build_common_tree_nodes{,_2} in
552 tree.c, especially as to the order of initializing common nodes. */
553 error_mark_node = make_node (ERROR_MARK);
554 TREE_TYPE (error_mark_node) = error_mark_node;
556 /* Create sizetype first - needed for other types. */
557 initialize_sizetypes (false);
559 byte_type_node = make_signed_type (8);
560 pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
561 short_type_node = make_signed_type (16);
562 pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
563 int_type_node = make_signed_type (32);
564 pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
565 long_type_node = make_signed_type (64);
566 pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));
568 unsigned_byte_type_node = make_unsigned_type (8);
569 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
570 unsigned_byte_type_node));
571 unsigned_short_type_node = make_unsigned_type (16);
572 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
573 unsigned_short_type_node));
574 unsigned_int_type_node = make_unsigned_type (32);
575 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
576 unsigned_int_type_node));
577 unsigned_long_type_node = make_unsigned_type (64);
578 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
579 unsigned_long_type_node));
581 /* This is not a java type, however tree-dfa requires a definition for
582 size_type_node. */
583 size_type_node = make_unsigned_type (POINTER_SIZE);
584 set_sizetype (size_type_node);
586 /* Define these next since types below may used them. */
587 integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
588 integer_zero_node = build_int_cst (NULL_TREE, 0);
589 integer_one_node = build_int_cst (NULL_TREE, 1);
590 integer_two_node = build_int_cst (NULL_TREE, 2);
591 integer_four_node = build_int_cst (NULL_TREE, 4);
592 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
594 /* A few values used for range checking in the lexer. */
595 decimal_int_max = build_int_cstu (unsigned_int_type_node, 0x80000000);
596 #if HOST_BITS_PER_WIDE_INT == 64
597 decimal_long_max = build_int_cstu (unsigned_long_type_node,
598 0x8000000000000000LL);
599 #elif HOST_BITS_PER_WIDE_INT == 32
600 decimal_long_max = build_int_cst_wide (unsigned_long_type_node,
601 0, 0x80000000);
602 #else
603 #error "unsupported size"
604 #endif
606 size_zero_node = size_int (0);
607 size_one_node = size_int (1);
608 bitsize_zero_node = bitsize_int (0);
609 bitsize_one_node = bitsize_int (1);
610 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
612 long_zero_node = build_int_cst (long_type_node, 0);
614 void_type_node = make_node (VOID_TYPE);
615 pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
616 layout_type (void_type_node); /* Uses size_zero_node */
618 ptr_type_node = build_pointer_type (void_type_node);
619 const_ptr_type_node
620 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
622 t = make_node (VOID_TYPE);
623 layout_type (t); /* Uses size_zero_node */
624 return_address_type_node = build_pointer_type (t);
626 null_pointer_node = build_int_cst (ptr_type_node, 0);
628 #if 0
629 /* Make a type to be the domain of a few array types
630 whose domains don't really matter.
631 200 is small enough that it always fits in size_t
632 and large enough that it can hold most function names for the
633 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
634 short_array_type_node = build_prim_array_type (short_type_node, 200);
635 #endif
636 char_type_node = make_node (INTEGER_TYPE);
637 TYPE_STRING_FLAG (char_type_node) = 1;
638 TYPE_PRECISION (char_type_node) = 16;
639 fixup_unsigned_type (char_type_node);
640 pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));
642 boolean_type_node = make_node (BOOLEAN_TYPE);
643 TYPE_PRECISION (boolean_type_node) = 1;
644 fixup_unsigned_type (boolean_type_node);
645 pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
646 boolean_type_node));
647 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
648 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
650 promoted_byte_type_node
651 = push_promoted_type ("promoted_byte", byte_type_node);
652 promoted_short_type_node
653 = push_promoted_type ("promoted_short", short_type_node);
654 promoted_char_type_node
655 = push_promoted_type ("promoted_char", char_type_node);
656 promoted_boolean_type_node
657 = push_promoted_type ("promoted_boolean", boolean_type_node);
659 float_type_node = make_node (REAL_TYPE);
660 TYPE_PRECISION (float_type_node) = 32;
661 pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
662 float_type_node));
663 layout_type (float_type_node);
665 double_type_node = make_node (REAL_TYPE);
666 TYPE_PRECISION (double_type_node) = 64;
667 pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
668 double_type_node));
669 layout_type (double_type_node);
671 float_zero_node = build_real (float_type_node, dconst0);
672 double_zero_node = build_real (double_type_node, dconst0);
674 /* These are the vtables for arrays of primitives. */
675 boolean_array_vtable = create_primitive_vtable ("boolean");
676 byte_array_vtable = create_primitive_vtable ("byte");
677 char_array_vtable = create_primitive_vtable ("char");
678 short_array_vtable = create_primitive_vtable ("short");
679 int_array_vtable = create_primitive_vtable ("int");
680 long_array_vtable = create_primitive_vtable ("long");
681 float_array_vtable = create_primitive_vtable ("float");
682 double_array_vtable = create_primitive_vtable ("double");
684 one_elt_array_domain_type = build_index_type (integer_one_node);
685 utf8const_type = make_node (RECORD_TYPE);
686 PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
687 PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
688 FINISH_RECORD (utf8const_type);
689 utf8const_ptr_type = build_pointer_type (utf8const_type);
691 atable_type = build_array_type (ptr_type_node,
692 one_elt_array_domain_type);
693 TYPE_NONALIASED_COMPONENT (atable_type) = 1;
694 atable_ptr_type = build_pointer_type (atable_type);
696 itable_type = build_array_type (ptr_type_node,
697 one_elt_array_domain_type);
698 TYPE_NONALIASED_COMPONENT (itable_type) = 1;
699 itable_ptr_type = build_pointer_type (itable_type);
701 symbol_type = make_node (RECORD_TYPE);
702 PUSH_FIELD (symbol_type, field, "clname", utf8const_ptr_type);
703 PUSH_FIELD (symbol_type, field, "name", utf8const_ptr_type);
704 PUSH_FIELD (symbol_type, field, "signature", utf8const_ptr_type);
705 FINISH_RECORD (symbol_type);
707 symbols_array_type = build_array_type (symbol_type,
708 one_elt_array_domain_type);
709 symbols_array_ptr_type = build_pointer_type (symbols_array_type);
711 assertion_entry_type = make_node (RECORD_TYPE);
712 PUSH_FIELD (assertion_entry_type, field, "assertion_code", integer_type_node);
713 PUSH_FIELD (assertion_entry_type, field, "op1", utf8const_ptr_type);
714 PUSH_FIELD (assertion_entry_type, field, "op2", utf8const_ptr_type);
715 FINISH_RECORD (assertion_entry_type);
717 assertion_table_type = build_array_type (assertion_entry_type,
718 one_elt_array_domain_type);
720 /* As you're adding items here, please update the code right after
721 this section, so that the filename containing the source code of
722 the pre-defined class gets registered correctly. */
723 unqualified_object_id_node = get_identifier ("Object");
724 object_type_node = lookup_class (get_identifier ("java.lang.Object"));
725 object_ptr_type_node = promote_type (object_type_node);
726 string_type_node = lookup_class (get_identifier ("java.lang.String"));
727 string_ptr_type_node = promote_type (string_type_node);
728 class_type_node = lookup_class (get_identifier ("java.lang.Class"));
729 throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
730 exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
731 runtime_exception_type_node =
732 lookup_class (get_identifier ("java.lang.RuntimeException"));
733 error_exception_type_node =
734 lookup_class (get_identifier ("java.lang.Error"));
736 rawdata_ptr_type_node
737 = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
739 add_predefined_file (get_identifier ("java/lang/Class.java"));
740 add_predefined_file (get_identifier ("java/lang/Error.java"));
741 add_predefined_file (get_identifier ("java/lang/Object.java"));
742 add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
743 add_predefined_file (get_identifier ("java/lang/String.java"));
744 add_predefined_file (get_identifier ("java/lang/Throwable.java"));
745 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
746 add_predefined_file (get_identifier ("java/lang/Exception.java"));
747 add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
748 add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
750 methodtable_type = make_node (RECORD_TYPE);
751 layout_type (methodtable_type);
752 build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
753 methodtable_ptr_type = build_pointer_type (methodtable_type);
755 TYPE_identifier_node = get_identifier ("TYPE");
756 init_identifier_node = get_identifier ("<init>");
757 clinit_identifier_node = get_identifier ("<clinit>");
758 finit_identifier_node = get_identifier ("finit$");
759 instinit_identifier_node = get_identifier ("instinit$");
760 void_signature_node = get_identifier ("()V");
761 length_identifier_node = get_identifier ("length");
762 finalize_identifier_node = get_identifier ("finalize");
763 this_identifier_node = get_identifier ("this");
764 super_identifier_node = get_identifier ("super");
765 continue_identifier_node = get_identifier ("continue");
766 access0_identifier_node = get_identifier ("access$0");
767 classdollar_identifier_node = get_identifier ("class$");
769 java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
770 java_io_serializable_identifier_node =
771 get_identifier ("java.io.Serializable");
773 /* for lack of a better place to put this stub call */
774 init_expr_processing();
776 constants_type_node = make_node (RECORD_TYPE);
777 PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
778 PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
779 PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
780 constants_data_field_decl_node = field;
781 FINISH_RECORD (constants_type_node);
782 build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
784 access_flags_type_node = unsigned_short_type_node;
786 dtable_type = make_node (RECORD_TYPE);
787 dtable_ptr_type = build_pointer_type (dtable_type);
789 otable_type = build_array_type (integer_type_node,
790 one_elt_array_domain_type);
791 TYPE_NONALIASED_COMPONENT (otable_type) = 1;
792 otable_ptr_type = build_pointer_type (otable_type);
794 PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
795 DECL_FCONTEXT (field) = object_type_node;
796 TYPE_VFIELD (object_type_node) = field;
798 /* This isn't exactly true, but it is what we have in the source.
799 There is an unresolved issue here, which is whether the vtable
800 should be marked by the GC. */
801 if (! flag_hash_synchronization)
802 PUSH_FIELD (object_type_node, field, "sync_info",
803 build_pointer_type (object_type_node));
804 for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
805 FIELD_PRIVATE (t) = 1;
806 FINISH_RECORD (object_type_node);
808 field_type_node = make_node (RECORD_TYPE);
809 field_ptr_type_node = build_pointer_type (field_type_node);
810 method_type_node = make_node (RECORD_TYPE);
811 method_ptr_type_node = build_pointer_type (method_type_node);
813 set_super_info (0, class_type_node, object_type_node, 0);
814 set_super_info (0, string_type_node, object_type_node, 0);
815 class_ptr_type = build_pointer_type (class_type_node);
817 PUSH_FIELD (class_type_node, field, "next_or_version", class_ptr_type);
818 PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
819 PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
820 PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
821 PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
822 constants_field_decl_node = field;
823 PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
824 PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
825 PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
826 PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
827 PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
828 PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
829 PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
830 PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
831 PUSH_FIELD (class_type_node, field, "otable", otable_ptr_type);
832 PUSH_FIELD (class_type_node, field, "otable_syms",
833 symbols_array_ptr_type);
834 PUSH_FIELD (class_type_node, field, "atable", atable_ptr_type);
835 PUSH_FIELD (class_type_node, field, "atable_syms",
836 symbols_array_ptr_type);
837 PUSH_FIELD (class_type_node, field, "itable", itable_ptr_type);
838 PUSH_FIELD (class_type_node, field, "itable_syms",
839 symbols_array_ptr_type);
840 PUSH_FIELD (class_type_node, field, "catch_classes", ptr_type_node);
841 PUSH_FIELD (class_type_node, field, "interfaces",
842 build_pointer_type (class_ptr_type));
843 PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
844 PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
845 PUSH_FIELD (class_type_node, field, "state", byte_type_node);
846 PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
847 PUSH_FIELD (class_type_node, field, "depth", short_type_node);
848 PUSH_FIELD (class_type_node, field, "ancestors", ptr_type_node);
849 PUSH_FIELD (class_type_node, field, "idt", ptr_type_node);
850 PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);
851 PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
852 PUSH_FIELD (class_type_node, field, "assertion_table", ptr_type_node);
853 PUSH_FIELD (class_type_node, field, "hack_signers", ptr_type_node);
854 PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
855 PUSH_FIELD (class_type_node, field, "aux_info", ptr_type_node);
856 PUSH_FIELD (class_type_node, field, "engine", ptr_type_node);
857 for (t = TYPE_FIELDS (class_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
858 FIELD_PRIVATE (t) = 1;
859 push_super_field (class_type_node, object_type_node);
861 FINISH_RECORD (class_type_node);
862 build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
864 field_info_union_node = make_node (UNION_TYPE);
865 PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
866 PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
867 #if 0
868 PUSH_FIELD (field_info_union_node, field, "idx", unsigned_short_type_node);
869 #endif
870 layout_type (field_info_union_node);
872 PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
873 PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
874 PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
875 PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
876 PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
877 FINISH_RECORD (field_type_node);
878 build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
880 nativecode_ptr_array_type_node
881 = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
883 PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
884 PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
885 FINISH_RECORD (dtable_type);
886 build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
888 jexception_type = make_node (RECORD_TYPE);
889 PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
890 PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
891 PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
892 PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
893 FINISH_RECORD (jexception_type);
894 build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
895 jexception_ptr_type = build_pointer_type (jexception_type);
897 lineNumberEntry_type = make_node (RECORD_TYPE);
898 PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
899 PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
900 FINISH_RECORD (lineNumberEntry_type);
902 lineNumbers_type = make_node (RECORD_TYPE);
903 PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
904 FINISH_RECORD (lineNumbers_type);
906 PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
907 PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
908 PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
909 PUSH_FIELD (method_type_node, field, "index", unsigned_short_type_node);
910 PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
911 PUSH_FIELD (method_type_node, field, "throws", ptr_type_node);
912 FINISH_RECORD (method_type_node);
913 build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
915 endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
917 t = tree_cons (NULL_TREE, class_ptr_type, endlink);
918 alloc_object_node = add_builtin_function ("_Jv_AllocObject",
919 build_function_type (ptr_type_node, t),
920 0, NOT_BUILT_IN, NULL, NULL_TREE);
921 DECL_IS_MALLOC (alloc_object_node) = 1;
922 alloc_no_finalizer_node =
923 add_builtin_function ("_Jv_AllocObjectNoFinalizer",
924 build_function_type (ptr_type_node, t),
925 0, NOT_BUILT_IN, NULL, NULL_TREE);
926 DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
928 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
929 soft_initclass_node = add_builtin_function ("_Jv_InitClass",
930 build_function_type (void_type_node,
932 0, NOT_BUILT_IN, NULL, NULL_TREE);
933 t = tree_cons (NULL_TREE, class_ptr_type,
934 tree_cons (NULL_TREE, int_type_node, endlink));
935 soft_resolvepoolentry_node
936 = add_builtin_function ("_Jv_ResolvePoolEntry",
937 build_function_type (ptr_type_node, t),
938 0,NOT_BUILT_IN, NULL, NULL_TREE);
939 DECL_IS_PURE (soft_resolvepoolentry_node) = 1;
940 throw_node = add_builtin_function ("_Jv_Throw",
941 build_function_type (void_type_node, t),
942 0, NOT_BUILT_IN, NULL, NULL_TREE);
943 /* Mark throw_nodes as `noreturn' functions with side effects. */
944 TREE_THIS_VOLATILE (throw_node) = 1;
945 TREE_SIDE_EFFECTS (throw_node) = 1;
947 t = build_function_type (void_type_node, tree_cons (NULL_TREE, ptr_type_node,
948 endlink));
949 soft_monitorenter_node
950 = add_builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
951 NULL, NULL_TREE);
952 soft_monitorexit_node
953 = add_builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
954 NULL, NULL_TREE);
956 t = tree_cons (NULL_TREE, ptr_type_node,
957 tree_cons (NULL_TREE, int_type_node, endlink));
958 soft_newarray_node
959 = add_builtin_function ("_Jv_NewPrimArray",
960 build_function_type (ptr_type_node, t),
961 0, NOT_BUILT_IN, NULL, NULL_TREE);
962 DECL_IS_MALLOC (soft_newarray_node) = 1;
964 t = tree_cons (NULL_TREE, int_type_node,
965 tree_cons (NULL_TREE, class_ptr_type,
966 tree_cons (NULL_TREE, object_ptr_type_node,
967 endlink)));
968 soft_anewarray_node
969 = add_builtin_function ("_Jv_NewObjectArray",
970 build_function_type (ptr_type_node, t),
971 0, NOT_BUILT_IN, NULL, NULL_TREE);
972 DECL_IS_MALLOC (soft_anewarray_node) = 1;
974 /* There is no endlink here because _Jv_NewMultiArray is a varargs
975 function. */
976 t = tree_cons (NULL_TREE, ptr_type_node,
977 tree_cons (NULL_TREE, int_type_node, NULL_TREE));
978 soft_multianewarray_node
979 = add_builtin_function ("_Jv_NewMultiArray",
980 build_function_type (ptr_type_node, t),
981 0, NOT_BUILT_IN, NULL, NULL_TREE);
982 DECL_IS_MALLOC (soft_multianewarray_node) = 1;
984 t = build_function_type (void_type_node,
985 tree_cons (NULL_TREE, int_type_node, endlink));
986 soft_badarrayindex_node
987 = add_builtin_function ("_Jv_ThrowBadArrayIndex", t,
988 0, NOT_BUILT_IN, NULL, NULL_TREE);
989 /* Mark soft_badarrayindex_node as a `noreturn' function with side
990 effects. */
991 TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
992 TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
994 soft_nullpointer_node
995 = add_builtin_function ("_Jv_ThrowNullPointerException",
996 build_function_type (void_type_node, endlink),
997 0, NOT_BUILT_IN, NULL, NULL_TREE);
998 /* Mark soft_nullpointer_node as a `noreturn' function with side
999 effects. */
1000 TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
1001 TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
1003 soft_abstractmethod_node
1004 = add_builtin_function ("_Jv_ThrowAbstractMethodError",
1005 build_function_type (void_type_node, endlink),
1006 0, NOT_BUILT_IN, NULL, NULL_TREE);
1007 /* Mark soft_abstractmethod_node as a `noreturn' function with side
1008 effects. */
1009 TREE_THIS_VOLATILE (soft_abstractmethod_node) = 1;
1010 TREE_SIDE_EFFECTS (soft_abstractmethod_node) = 1;
1012 soft_nosuchfield_node
1013 = add_builtin_function ("_Jv_ThrowNoSuchFieldError",
1014 build_function_type (void_type_node, endlink),
1015 0, NOT_BUILT_IN, NULL, NULL_TREE);
1016 /* Mark soft_nosuchfield_node as a `noreturn' function with side
1017 effects. */
1018 TREE_THIS_VOLATILE (soft_nosuchfield_node) = 1;
1019 TREE_SIDE_EFFECTS (soft_nosuchfield_node) = 1;
1021 t = tree_cons (NULL_TREE, class_ptr_type,
1022 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1023 soft_checkcast_node
1024 = add_builtin_function ("_Jv_CheckCast",
1025 build_function_type (ptr_type_node, t),
1026 0, NOT_BUILT_IN, NULL, NULL_TREE);
1027 t = tree_cons (NULL_TREE, object_ptr_type_node,
1028 tree_cons (NULL_TREE, class_ptr_type, endlink));
1029 soft_instanceof_node
1030 = add_builtin_function ("_Jv_IsInstanceOf",
1031 build_function_type (boolean_type_node, t),
1032 0, NOT_BUILT_IN, NULL, NULL_TREE);
1033 DECL_IS_PURE (soft_instanceof_node) = 1;
1034 t = tree_cons (NULL_TREE, object_ptr_type_node,
1035 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1036 soft_checkarraystore_node
1037 = add_builtin_function ("_Jv_CheckArrayStore",
1038 build_function_type (void_type_node, t),
1039 0, NOT_BUILT_IN, NULL, NULL_TREE);
1040 t = tree_cons (NULL_TREE, ptr_type_node,
1041 tree_cons (NULL_TREE, ptr_type_node,
1042 tree_cons (NULL_TREE, int_type_node, endlink)));
1043 soft_lookupinterfacemethod_node
1044 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx",
1045 build_function_type (ptr_type_node, t),
1046 0, NOT_BUILT_IN, NULL, NULL_TREE);
1047 DECL_IS_PURE (soft_lookupinterfacemethod_node) = 1;
1048 t = tree_cons (NULL_TREE, ptr_type_node,
1049 tree_cons (NULL_TREE, ptr_type_node,
1050 tree_cons (NULL_TREE, ptr_type_node, endlink)));
1051 soft_lookupinterfacemethodbyname_node
1052 = add_builtin_function ("_Jv_LookupInterfaceMethod",
1053 build_function_type (ptr_type_node, t),
1054 0, NOT_BUILT_IN, NULL, NULL_TREE);
1055 t = tree_cons (NULL_TREE, object_ptr_type_node,
1056 tree_cons (NULL_TREE, ptr_type_node,
1057 tree_cons (NULL_TREE, ptr_type_node,
1058 tree_cons (NULL_TREE, int_type_node,
1059 endlink))));
1060 soft_lookupjnimethod_node
1061 = add_builtin_function ("_Jv_LookupJNIMethod",
1062 build_function_type (ptr_type_node, t),
1063 0, NOT_BUILT_IN, NULL, NULL_TREE);
1064 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1065 soft_getjnienvnewframe_node
1066 = add_builtin_function ("_Jv_GetJNIEnvNewFrame",
1067 build_function_type (ptr_type_node, t),
1068 0, NOT_BUILT_IN, NULL, NULL_TREE);
1069 soft_jnipopsystemframe_node
1070 = add_builtin_function ("_Jv_JNI_PopSystemFrame",
1071 build_function_type (void_type_node, t),
1072 0, NOT_BUILT_IN, NULL, NULL_TREE);
1074 t = tree_cons (NULL_TREE, object_ptr_type_node, endlink);
1075 soft_unwrapjni_node
1076 = add_builtin_function ("_Jv_UnwrapJNIweakReference",
1077 build_function_type (object_ptr_type_node, t),
1078 0, NOT_BUILT_IN, NULL, NULL_TREE);
1080 t = tree_cons (NULL_TREE, int_type_node,
1081 tree_cons (NULL_TREE, int_type_node, endlink));
1082 soft_idiv_node
1083 = add_builtin_function ("_Jv_divI",
1084 build_function_type (int_type_node, t),
1085 0, NOT_BUILT_IN, NULL, NULL_TREE);
1087 soft_irem_node
1088 = add_builtin_function ("_Jv_remI",
1089 build_function_type (int_type_node, t),
1090 0, NOT_BUILT_IN, NULL, NULL_TREE);
1092 t = tree_cons (NULL_TREE, long_type_node,
1093 tree_cons (NULL_TREE, long_type_node, endlink));
1094 soft_ldiv_node
1095 = add_builtin_function ("_Jv_divJ",
1096 build_function_type (long_type_node, t),
1097 0, NOT_BUILT_IN, NULL, NULL_TREE);
1099 soft_lrem_node
1100 = add_builtin_function ("_Jv_remJ",
1101 build_function_type (long_type_node, t),
1102 0, NOT_BUILT_IN, NULL, NULL_TREE);
1104 /* Initialize variables for except.c. */
1105 eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
1106 ? "__gcj_personality_sj0"
1107 : "__gcj_personality_v0");
1108 default_init_unwind_resume_libfunc ();
1110 lang_eh_runtime_type = do_nothing;
1112 init_jcf_parse ();
1114 initialize_builtins ();
1115 soft_fmod_node = built_in_decls[BUILT_IN_FMOD];
1116 #if 0
1117 soft_fmodf_node = built_in_decls[BUILT_IN_FMODF];
1118 #endif
1120 parse_version ();
1124 /* Look up NAME in the current binding level and its superiors
1125 in the namespace of variables, functions and typedefs.
1126 Return a ..._DECL node of some kind representing its definition,
1127 or return 0 if it is undefined. */
1129 tree
1130 lookup_name (tree name)
1132 tree val;
1133 if (current_binding_level != global_binding_level
1134 && IDENTIFIER_LOCAL_VALUE (name))
1135 val = IDENTIFIER_LOCAL_VALUE (name);
1136 else
1137 val = IDENTIFIER_GLOBAL_VALUE (name);
1138 return val;
1141 /* Similar to `lookup_name' but look only at current binding level and
1142 the previous one if it's the parameter level. */
1144 static tree
1145 lookup_name_current_level (tree name)
1147 tree t;
1149 if (current_binding_level == global_binding_level)
1150 return IDENTIFIER_GLOBAL_VALUE (name);
1152 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
1153 return 0;
1155 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
1156 if (DECL_NAME (t) == name)
1157 break;
1159 return t;
1162 /* Use a binding level to record a labeled block declaration */
1164 void
1165 push_labeled_block (tree lb)
1167 tree name = DECL_NAME (LABELED_BLOCK_LABEL (lb));
1168 struct binding_level *b = current_binding_level;
1169 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1170 if (oldlocal != 0)
1171 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1172 TREE_CHAIN (lb) = b->names;
1173 b->names = lb;
1174 IDENTIFIER_LOCAL_VALUE (name) = lb;
1177 /* Pop the current binding level, reinstalling values for the previous
1178 labeled block */
1180 void
1181 pop_labeled_block (void)
1183 struct binding_level *b = current_binding_level;
1184 tree label = b->names;
1185 IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label))) =
1186 NULL_TREE;
1187 if (b->shadowed)
1188 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b->shadowed)) =
1189 TREE_VALUE (b->shadowed);
1191 /* Pop the current level, and free the structure for reuse. */
1192 current_binding_level = current_binding_level->level_chain;
1193 b->level_chain = free_binding_level;
1194 free_binding_level = b;
1197 /* Record a decl-node X as belonging to the current lexical scope.
1198 Check for errors (such as an incompatible declaration for the same
1199 name already seen in the same scope).
1201 Returns either X or an old decl for the same name.
1202 If an old decl is returned, it may have been smashed
1203 to agree with what X says. */
1205 tree
1206 pushdecl (tree x)
1208 tree t;
1209 tree name = DECL_NAME (x);
1210 struct binding_level *b = current_binding_level;
1212 if (TREE_CODE (x) != TYPE_DECL)
1213 DECL_CONTEXT (x) = current_function_decl;
1214 if (name)
1216 t = lookup_name_current_level (name);
1217 if (t != 0 && t == error_mark_node)
1218 /* error_mark_node is 0 for a while during initialization! */
1220 t = 0;
1221 error ("%q+D used prior to declaration", x);
1224 /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1225 to point to the TYPE_DECL.
1226 Since Java does not have typedefs, a type can only have
1227 one (true) name, given by a class, interface, or builtin. */
1228 if (TREE_CODE (x) == TYPE_DECL
1229 && TYPE_NAME (TREE_TYPE (x)) == 0
1230 && TREE_TYPE (x) != error_mark_node)
1232 TYPE_NAME (TREE_TYPE (x)) = x;
1233 TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1236 /* This name is new in its binding level.
1237 Install the new declaration and return it. */
1238 if (b == global_binding_level)
1240 /* Install a global value. */
1242 IDENTIFIER_GLOBAL_VALUE (name) = x;
1244 else
1246 /* Here to install a non-global value. */
1247 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1248 IDENTIFIER_LOCAL_VALUE (name) = x;
1250 #if 0
1251 /* Warn if shadowing an argument at the top level of the body. */
1252 if (oldlocal != 0 && !DECL_EXTERNAL (x)
1253 /* This warning doesn't apply to the parms of a nested fcn. */
1254 && ! current_binding_level->parm_flag
1255 /* Check that this is one level down from the parms. */
1256 && current_binding_level->level_chain->parm_flag
1257 /* Check that the decl being shadowed
1258 comes from the parm level, one level up. */
1259 && chain_member (oldlocal, current_binding_level->level_chain->names))
1261 if (TREE_CODE (oldlocal) == PARM_DECL)
1262 pedwarn ("declaration of %qs shadows a parameter",
1263 IDENTIFIER_POINTER (name));
1264 else
1265 pedwarn ("declaration of %qs shadows a symbol from the parameter list",
1266 IDENTIFIER_POINTER (name));
1269 /* Maybe warn if shadowing something else. */
1270 else if (warn_shadow && !DECL_EXTERNAL (x)
1271 /* No shadow warnings for internally generated vars. */
1272 && DECL_SOURCE_LINE (x) != 0
1273 /* No shadow warnings for vars made for inlining. */
1274 && ! DECL_FROM_INLINE (x))
1276 const char *warnstring = 0;
1278 if (TREE_CODE (x) == PARM_DECL
1279 && current_binding_level->level_chain->parm_flag)
1280 /* Don't warn about the parm names in function declarator
1281 within a function declarator.
1282 It would be nice to avoid warning in any function
1283 declarator in a declaration, as opposed to a definition,
1284 but there is no way to tell it's not a definition. */
1286 else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
1287 warnstring = "declaration of %qs shadows a parameter";
1288 else if (oldlocal != 0)
1289 warnstring = "declaration of %qs shadows previous local";
1290 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1291 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1292 warnstring = "declaration of %qs shadows global declaration";
1294 if (warnstring)
1295 warning (0, warnstring, IDENTIFIER_POINTER (name));
1297 #endif
1299 /* If storing a local value, there may already be one (inherited).
1300 If so, record it for restoration when this binding level ends. */
1301 if (oldlocal != 0)
1302 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1306 /* Put decls on list in reverse order.
1307 We will reverse them later if necessary. */
1308 TREE_CHAIN (x) = b->names;
1309 b->names = x;
1311 return x;
1314 void
1315 pushdecl_force_head (tree x)
1317 current_binding_level->names = x;
1320 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1322 tree
1323 pushdecl_top_level (tree x)
1325 tree t;
1326 struct binding_level *b = current_binding_level;
1328 current_binding_level = global_binding_level;
1329 t = pushdecl (x);
1330 current_binding_level = b;
1331 return t;
1334 /* Like pushdecl, only it places X in FUNCTION_BINDING_LEVEL, if appropriate. */
1336 tree
1337 pushdecl_function_level (tree x)
1339 tree t;
1340 struct binding_level *b = current_binding_level;
1342 current_binding_level = function_binding_level;
1343 t = pushdecl (x);
1344 current_binding_level = b;
1345 return t;
1348 /* Nonzero if we are currently in the global binding level. */
1351 global_bindings_p (void)
1353 return current_binding_level == global_binding_level;
1356 /* Return the list of declarations of the current level.
1357 Note that this list is in reverse order unless/until
1358 you nreverse it; and when you do nreverse it, you must
1359 store the result back using `storedecls' or you will lose. */
1361 tree
1362 getdecls (void)
1364 return current_binding_level->names;
1367 /* Create a new `struct binding_level'. */
1369 static struct binding_level *
1370 make_binding_level (void)
1372 /* NOSTRICT */
1373 return ggc_alloc_cleared (sizeof (struct binding_level));
1376 void
1377 pushlevel (int unused ATTRIBUTE_UNUSED)
1379 struct binding_level *newlevel = NULL_BINDING_LEVEL;
1381 #if 0
1382 /* If this is the top level of a function,
1383 just make sure that NAMED_LABELS is 0. */
1385 if (current_binding_level == global_binding_level)
1386 named_labels = 0;
1387 #endif
1389 /* Reuse or create a struct for this binding level. */
1391 if (free_binding_level)
1393 newlevel = free_binding_level;
1394 free_binding_level = free_binding_level->level_chain;
1396 else
1398 newlevel = make_binding_level ();
1401 /* Add this level to the front of the chain (stack) of levels that
1402 are active. */
1404 *newlevel = clear_binding_level;
1405 newlevel->level_chain = current_binding_level;
1406 current_binding_level = newlevel;
1407 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1408 newlevel->binding_depth = binding_depth;
1409 indent ();
1410 fprintf (stderr, "push %s level %p pc %d\n",
1411 (is_class_level) ? "class" : "block", newlevel, current_pc);
1412 is_class_level = 0;
1413 binding_depth++;
1414 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1417 /* Exit a binding level.
1418 Pop the level off, and restore the state of the identifier-decl mappings
1419 that were in effect when this level was entered.
1421 If KEEP is nonzero, this level had explicit declarations, so
1422 and create a "block" (a BLOCK node) for the level
1423 to record its declarations and subblocks for symbol table output.
1425 If FUNCTIONBODY is nonzero, this level is the body of a function,
1426 so create a block as if KEEP were set and also clear out all
1427 label names.
1429 If REVERSE is nonzero, reverse the order of decls before putting
1430 them into the BLOCK. */
1432 tree
1433 poplevel (int keep, int reverse, int functionbody)
1435 tree link;
1436 /* The chain of decls was accumulated in reverse order.
1437 Put it into forward order, just for cleanliness. */
1438 tree decls;
1439 tree subblocks = current_binding_level->blocks;
1440 tree block = 0;
1441 tree decl;
1442 tree bind = 0;
1444 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1445 binding_depth--;
1446 indent ();
1447 if (current_binding_level->end_pc != LARGEST_PC)
1448 fprintf (stderr, "pop %s level %p pc %d (end pc %d)\n",
1449 (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1450 current_binding_level->end_pc);
1451 else
1452 fprintf (stderr, "pop %s level %p pc %d\n",
1453 (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1454 #if 0
1455 if (is_class_level != (current_binding_level == class_binding_level))
1457 indent ();
1458 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
1460 is_class_level = 0;
1461 #endif
1462 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1464 /* Get the decls in the order they were written.
1465 Usually current_binding_level->names is in reverse order.
1466 But parameter decls were previously put in forward order. */
1468 if (reverse)
1469 current_binding_level->names
1470 = decls = nreverse (current_binding_level->names);
1471 else
1472 decls = current_binding_level->names;
1474 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1475 if (TREE_CODE (decl) == VAR_DECL
1476 && DECL_LANG_SPECIFIC (decl) != NULL
1477 && DECL_LOCAL_SLOT_NUMBER (decl))
1478 LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
1480 /* If there were any declarations in that level,
1481 or if this level is a function body,
1482 create a BLOCK to record them for the life of this function. */
1484 block = 0;
1485 if (keep || functionbody)
1487 block = make_node (BLOCK);
1488 TREE_TYPE (block) = void_type_node;
1491 if (current_binding_level->exception_range)
1492 expand_end_java_handler (current_binding_level->exception_range);
1494 if (block != 0)
1496 /* If any statements have been generated at this level, create a
1497 BIND_EXPR to hold them and copy the variables to it. This
1498 only applies to the bytecode compiler. */
1499 if (current_binding_level->stmts)
1501 tree decl = decls;
1502 tree *var = &BLOCK_VARS (block);
1504 /* Copy decls from names list, ignoring labels. */
1505 while (decl)
1507 tree next = TREE_CHAIN (decl);
1508 if (TREE_CODE (decl) != LABEL_DECL)
1510 *var = decl;
1511 var = &TREE_CHAIN (decl);
1513 decl = next;
1515 *var = NULL;
1517 bind = build3 (BIND_EXPR, TREE_TYPE (block), BLOCK_VARS (block),
1518 BLOCK_EXPR_BODY (block), block);
1519 BIND_EXPR_BODY (bind) = current_binding_level->stmts;
1521 if (BIND_EXPR_BODY (bind)
1522 && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
1523 TREE_SIDE_EFFECTS (bind) = 1;
1525 /* FIXME: gimplifier brain damage. */
1526 if (BIND_EXPR_BODY (bind) == NULL)
1527 BIND_EXPR_BODY (bind) = build_java_empty_stmt ();
1529 current_binding_level->stmts = NULL;
1531 else
1533 BLOCK_VARS (block) = decls;
1535 BLOCK_SUBBLOCKS (block) = subblocks;
1538 /* In each subblock, record that this is its superior. */
1540 for (link = subblocks; link; link = TREE_CHAIN (link))
1541 BLOCK_SUPERCONTEXT (link) = block;
1543 /* Clear out the meanings of the local variables of this level. */
1545 for (link = decls; link; link = TREE_CHAIN (link))
1547 tree name = DECL_NAME (link);
1548 if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1550 /* If the ident. was used or addressed via a local extern decl,
1551 don't forget that fact. */
1552 if (DECL_EXTERNAL (link))
1554 if (TREE_USED (link))
1555 TREE_USED (name) = 1;
1556 if (TREE_ADDRESSABLE (link))
1557 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1559 IDENTIFIER_LOCAL_VALUE (name) = 0;
1563 /* Restore all name-meanings of the outer levels
1564 that were shadowed by this level. */
1566 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1567 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1569 /* If the level being exited is the top level of a function,
1570 check over all the labels, and clear out the current
1571 (function local) meanings of their names. */
1573 if (functionbody)
1575 /* If this is the top level block of a function,
1576 the vars are the function's parameters.
1577 Don't leave them in the BLOCK because they are
1578 found in the FUNCTION_DECL instead. */
1580 BLOCK_VARS (block) = 0;
1582 /* Clear out the definitions of all label names,
1583 since their scopes end here,
1584 and add them to BLOCK_VARS. */
1586 #if 0
1587 for (link = named_labels; link; link = TREE_CHAIN (link))
1589 tree label = TREE_VALUE (link);
1591 if (DECL_INITIAL (label) == 0)
1593 error ("label %q+D used but not defined", label);
1594 /* Avoid crashing later. */
1595 define_label (input_location, DECL_NAME (label));
1597 else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
1598 warning (0, "label %q+D defined but not used", label);
1599 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1601 /* Put the labels into the "variables" of the
1602 top-level block, so debugger can see them. */
1603 TREE_CHAIN (label) = BLOCK_VARS (block);
1604 BLOCK_VARS (block) = label;
1606 #endif
1609 /* Pop the current level, and free the structure for reuse. */
1612 struct binding_level *level = current_binding_level;
1613 current_binding_level = current_binding_level->level_chain;
1615 level->level_chain = free_binding_level;
1616 free_binding_level = level;
1619 /* Dispose of the block that we just made inside some higher level. */
1620 if (functionbody)
1622 DECL_INITIAL (current_function_decl) = block;
1623 DECL_SAVED_TREE (current_function_decl) = bind;
1625 else
1627 if (block)
1629 current_binding_level->blocks
1630 = chainon (current_binding_level->blocks, block);
1632 /* If we did not make a block for the level just exited,
1633 any blocks made for inner levels
1634 (since they cannot be recorded as subblocks in that level)
1635 must be carried forward so they will later become subblocks
1636 of something else. */
1637 else if (subblocks)
1638 current_binding_level->blocks
1639 = chainon (current_binding_level->blocks, subblocks);
1641 if (bind)
1642 java_add_stmt (bind);
1645 if (block)
1646 TREE_USED (block) = 1;
1647 return block;
1650 void
1651 maybe_pushlevels (int pc)
1653 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1654 current_pc = pc;
1655 #endif
1657 while (pending_local_decls != NULL_TREE &&
1658 DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1660 tree *ptr = &pending_local_decls;
1661 tree decl = *ptr, next;
1662 int end_pc = DECL_LOCAL_END_PC (decl);
1664 while (*ptr != NULL_TREE
1665 && DECL_LOCAL_START_PC (*ptr) <= pc
1666 && DECL_LOCAL_END_PC (*ptr) == end_pc)
1667 ptr = &TREE_CHAIN (*ptr);
1668 pending_local_decls = *ptr;
1669 *ptr = NULL_TREE;
1671 /* Force non-nested range to be nested in current range by
1672 truncating variable lifetimes. */
1673 if (end_pc > current_binding_level->end_pc)
1675 tree t;
1676 end_pc = current_binding_level->end_pc;
1677 for (t = decl; t != NULL_TREE; t = TREE_CHAIN (t))
1678 DECL_LOCAL_END_PC (t) = end_pc;
1681 maybe_start_try (pc, end_pc);
1683 pushlevel (1);
1685 current_binding_level->end_pc = end_pc;
1686 current_binding_level->start_pc = pc;
1687 current_binding_level->names = NULL;
1688 for ( ; decl != NULL_TREE; decl = next)
1690 int index = DECL_LOCAL_SLOT_NUMBER (decl);
1691 tree base_decl;
1692 next = TREE_CHAIN (decl);
1693 push_jvm_slot (index, decl);
1694 pushdecl (decl);
1695 base_decl
1696 = find_local_variable (index, TREE_TYPE (decl), pc);
1697 if (TREE_CODE (TREE_TYPE (base_decl)) == POINTER_TYPE)
1698 base_decl = TREE_VEC_ELT (base_decl_map, index);
1699 SET_DECL_VALUE_EXPR (decl, base_decl);
1700 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1704 maybe_start_try (pc, 0);
1707 void
1708 maybe_poplevels (int pc)
1710 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1711 current_pc = pc;
1712 #endif
1714 /* FIXME: I'm pretty sure that this is wrong. Variable scopes are
1715 inclusive, so a variable is live if pc == end_pc. Here, we
1716 terminate a range if the current pc is equal to the end of the
1717 range, and this is *before* we have generated code for the
1718 instruction at end_pc. We're closing a binding level one
1719 instruction too early.*/
1720 while (current_binding_level->end_pc <= pc)
1721 poplevel (1, 0, 0);
1724 /* Terminate any binding which began during the range beginning at
1725 start_pc. This tidies up improperly nested local variable ranges
1726 and exception handlers; a variable declared within an exception
1727 range is forcibly terminated when that exception ends. */
1729 void
1730 force_poplevels (int start_pc)
1732 while (current_binding_level->start_pc > start_pc)
1734 if (pedantic && current_binding_level->start_pc > start_pc)
1735 warning (0, "In %+D: overlapped variable and exception ranges at %d",
1736 current_function_decl,
1737 current_binding_level->start_pc);
1738 poplevel (1, 0, 0);
1742 /* Insert BLOCK at the end of the list of subblocks of the
1743 current binding level. This is used when a BIND_EXPR is expanded,
1744 to handle the BLOCK node inside the BIND_EXPR. */
1746 void
1747 insert_block (tree block)
1749 TREE_USED (block) = 1;
1750 current_binding_level->blocks
1751 = chainon (current_binding_level->blocks, block);
1754 /* integrate_decl_tree calls this function. */
1756 void
1757 java_dup_lang_specific_decl (tree node)
1759 int lang_decl_size;
1760 struct lang_decl *x;
1762 if (!DECL_LANG_SPECIFIC (node))
1763 return;
1765 lang_decl_size = sizeof (struct lang_decl);
1766 x = ggc_alloc (lang_decl_size);
1767 memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1768 DECL_LANG_SPECIFIC (node) = x;
1771 void
1772 give_name_to_locals (JCF *jcf)
1774 int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1775 int code_offset = DECL_CODE_OFFSET (current_function_decl);
1776 tree parm;
1777 pending_local_decls = NULL_TREE;
1778 if (n == 0)
1779 return;
1780 JCF_SEEK (jcf, n);
1781 n = JCF_readu2 (jcf);
1782 for (i = 0; i < n; i++)
1784 int start_pc = JCF_readu2 (jcf);
1785 int length = JCF_readu2 (jcf);
1786 int name_index = JCF_readu2 (jcf);
1787 int signature_index = JCF_readu2 (jcf);
1788 int slot = JCF_readu2 (jcf);
1789 tree name = get_name_constant (jcf, name_index);
1790 tree type = parse_signature (jcf, signature_index);
1791 if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1792 && start_pc == 0
1793 && length == DECL_CODE_LENGTH (current_function_decl))
1795 tree decl = TREE_VEC_ELT (decl_map, slot);
1796 DECL_NAME (decl) = name;
1797 if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1798 warning (0, "bad type in parameter debug info");
1800 else
1802 tree *ptr;
1803 int end_pc = start_pc + length;
1804 tree decl = build_decl (VAR_DECL, name, type);
1805 if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1807 warning (0, "bad PC range for debug info for local %q+D",
1808 decl);
1809 end_pc = DECL_CODE_LENGTH (current_function_decl);
1812 /* Adjust start_pc if necessary so that the local's first
1813 store operation will use the relevant DECL as a
1814 destination. Fore more information, read the leading
1815 comments for expr.c:maybe_adjust_start_pc. */
1816 start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1818 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1819 DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1820 DECL_LOCAL_START_PC (decl) = start_pc;
1821 #if 0
1822 /* FIXME: The range used internally for exceptions and local
1823 variable ranges, is a half-open interval:
1824 start_pc <= pc < end_pc. However, the range used in the
1825 Java VM spec is inclusive at both ends:
1826 start_pc <= pc <= end_pc. */
1827 end_pc++;
1828 #endif
1829 DECL_LOCAL_END_PC (decl) = end_pc;
1831 /* Now insert the new decl in the proper place in
1832 pending_local_decls. We are essentially doing an insertion sort,
1833 which works fine, since the list input will normally already
1834 be sorted. */
1835 ptr = &pending_local_decls;
1836 while (*ptr != NULL_TREE
1837 && (DECL_LOCAL_START_PC (*ptr) > start_pc
1838 || (DECL_LOCAL_START_PC (*ptr) == start_pc
1839 && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1840 ptr = &TREE_CHAIN (*ptr);
1841 TREE_CHAIN (decl) = *ptr;
1842 *ptr = decl;
1846 pending_local_decls = nreverse (pending_local_decls);
1848 /* Fill in default names for the parameters. */
1849 for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1850 parm != NULL_TREE; parm = TREE_CHAIN (parm), i++)
1852 if (DECL_NAME (parm) == NULL_TREE)
1854 int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1855 if (arg_i == 0)
1856 DECL_NAME (parm) = get_identifier ("this");
1857 else
1859 char buffer[12];
1860 sprintf (buffer, "ARG_%d", arg_i);
1861 DECL_NAME (parm) = get_identifier (buffer);
1867 tree
1868 build_result_decl (tree fndecl)
1870 tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1871 tree result = DECL_RESULT (fndecl);
1872 if (! result)
1874 /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1875 if (INTEGRAL_TYPE_P (restype)
1876 && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
1877 restype = integer_type_node;
1878 result = build_decl (RESULT_DECL, NULL_TREE, restype);
1879 DECL_ARTIFICIAL (result) = 1;
1880 DECL_IGNORED_P (result) = 1;
1881 DECL_CONTEXT (result) = fndecl;
1882 DECL_RESULT (fndecl) = result;
1884 return result;
1887 void
1888 start_java_method (tree fndecl)
1890 tree tem, *ptr;
1891 int i;
1893 uniq = 0;
1895 current_function_decl = fndecl;
1896 announce_function (fndecl);
1898 i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1899 decl_map = make_tree_vec (i);
1900 base_decl_map = make_tree_vec (i);
1901 type_map = xrealloc (type_map, i * sizeof (tree));
1903 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1904 fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
1905 current_pc = 0;
1906 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1907 pushlevel (1); /* Push parameters. */
1909 ptr = &DECL_ARGUMENTS (fndecl);
1910 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1911 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
1913 tree parm_name = NULL_TREE, parm_decl;
1914 tree parm_type = TREE_VALUE (tem);
1915 gcc_assert (i < DECL_MAX_LOCALS (fndecl));
1917 parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
1918 DECL_CONTEXT (parm_decl) = fndecl;
1919 if (targetm.calls.promote_prototypes (parm_type)
1920 && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
1921 && INTEGRAL_TYPE_P (parm_type))
1922 parm_type = integer_type_node;
1923 DECL_ARG_TYPE (parm_decl) = parm_type;
1925 *ptr = parm_decl;
1926 ptr = &TREE_CHAIN (parm_decl);
1928 /* Add parm_decl to the decl_map. */
1929 push_jvm_slot (i, parm_decl);
1931 type_map[i] = TREE_TYPE (parm_decl);
1932 if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
1934 i++;
1935 type_map[i] = void_type_node;
1938 *ptr = NULL_TREE;
1939 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
1941 while (i < DECL_MAX_LOCALS(fndecl))
1942 type_map[i++] = NULL_TREE;
1944 build_result_decl (fndecl);
1946 /* Push local variables. */
1947 pushlevel (2);
1949 function_binding_level = current_binding_level;
1952 void
1953 end_java_method (void)
1955 tree fndecl = current_function_decl;
1957 /* pop out of function */
1958 poplevel (1, 1, 0);
1960 /* pop out of its parameters */
1961 poplevel (1, 0, 1);
1963 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
1965 if (DECL_SAVED_TREE (fndecl))
1967 tree fbody, block_body;
1968 /* Before we check initialization, attached all class initialization
1969 variable to the block_body */
1970 fbody = DECL_SAVED_TREE (fndecl);
1971 block_body = BIND_EXPR_BODY (fbody);
1972 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
1973 attach_init_test_initialization_flags, block_body);
1976 finish_method (fndecl);
1978 if (! flag_unit_at_a_time)
1980 /* Nulling these fields when we no longer need them saves
1981 memory. */
1982 DECL_SAVED_TREE (fndecl) = NULL;
1983 DECL_STRUCT_FUNCTION (fndecl) = NULL;
1984 DECL_INITIAL (fndecl) = NULL_TREE;
1986 current_function_decl = NULL_TREE;
1989 /* Prepare a method for expansion. */
1991 void
1992 finish_method (tree fndecl)
1994 tree *tp = &DECL_SAVED_TREE (fndecl);
1996 /* Wrap body of synchronized methods in a monitorenter,
1997 plus monitorexit cleanup. */
1998 if (METHOD_SYNCHRONIZED (fndecl))
2000 tree enter, exit, lock;
2001 if (METHOD_STATIC (fndecl))
2002 lock = build_class_ref (DECL_CONTEXT (fndecl));
2003 else
2004 lock = DECL_ARGUMENTS (fndecl);
2005 BUILD_MONITOR_ENTER (enter, lock);
2006 BUILD_MONITOR_EXIT (exit, lock);
2007 *tp = build2 (COMPOUND_EXPR, void_type_node, enter,
2008 build2 (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
2011 /* Prepend class initialization for static methods reachable from
2012 other classes. */
2013 if (METHOD_STATIC (fndecl)
2014 && (! METHOD_PRIVATE (fndecl)
2015 || INNER_CLASS_P (DECL_CONTEXT (fndecl)))
2016 && ! DECL_CLINIT_P (fndecl)
2017 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
2019 tree clas = DECL_CONTEXT (fndecl);
2020 tree init = build3 (CALL_EXPR, void_type_node,
2021 build_address_of (soft_initclass_node),
2022 build_tree_list (NULL_TREE, build_class_ref (clas)),
2023 NULL_TREE);
2024 *tp = build2 (COMPOUND_EXPR, TREE_TYPE (*tp), init, *tp);
2027 /* Convert function tree to GENERIC prior to inlining. */
2028 java_genericize (fndecl);
2030 /* Store the end of the function, so that we get good line number
2031 info for the epilogue. */
2032 if (DECL_STRUCT_FUNCTION (fndecl))
2033 cfun = DECL_STRUCT_FUNCTION (fndecl);
2034 else
2035 allocate_struct_function (fndecl);
2036 #ifdef USE_MAPPED_LOCATION
2037 cfun->function_end_locus = DECL_FUNCTION_LAST_LINE (fndecl);
2038 #else
2039 cfun->function_end_locus.file = DECL_SOURCE_FILE (fndecl);
2040 cfun->function_end_locus.line = DECL_FUNCTION_LAST_LINE (fndecl);
2041 #endif
2043 /* Defer inlining and expansion to the cgraph optimizers. */
2044 cgraph_finalize_function (fndecl, false);
2047 /* Optimize and expand a function's entire body. */
2049 void
2050 java_expand_body (tree fndecl)
2052 tree_rest_of_compilation (fndecl);
2055 /* We pessimistically marked all methods and fields external until we
2056 knew what set of classes we were planning to compile. Now mark those
2057 associated with CLASS to be generated locally as not external. */
2059 static void
2060 java_mark_decl_local (tree decl)
2062 DECL_EXTERNAL (decl) = 0;
2064 /* If we've already constructed DECL_RTL, give encode_section_info
2065 a second chance, now that we've changed the flags. */
2066 /* ??? Ideally, we'd have flag_unit_at_a_time set, and not have done
2067 anything that would have referenced DECL_RTL so far. But at the
2068 moment we force flag_unit_at_a_time off due to excessive memory
2069 consumption when compiling large jar files. Which probably means
2070 that we need to re-order how we process jar files... */
2071 if (DECL_RTL_SET_P (decl))
2072 make_decl_rtl (decl);
2075 /* Given appropriate target support, G++ will emit hidden aliases for native
2076 methods. Using this hidden name is required for proper operation of
2077 _Jv_Method::ncode, but it doesn't hurt to use it everywhere. Look for
2078 proper target support, then mark the method for aliasing. */
2080 static void
2081 java_mark_cni_decl_local (tree decl)
2083 /* Setting DECL_LOCAL_CNI_METHOD_P changes the behavior of the mangler.
2084 We expect that we should not yet have referenced this decl in a
2085 context that requires it. Check this invariant even if we don't have
2086 support for hidden aliases. */
2087 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
2089 #if !defined(HAVE_GAS_HIDDEN) || !defined(ASM_OUTPUT_DEF)
2090 return;
2091 #endif
2093 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2094 DECL_LOCAL_CNI_METHOD_P (decl) = 1;
2097 /* Use the preceding two functions and mark all members of the class. */
2099 void
2100 java_mark_class_local (tree class)
2102 tree t;
2104 for (t = TYPE_FIELDS (class); t ; t = TREE_CHAIN (t))
2105 if (FIELD_STATIC (t))
2106 java_mark_decl_local (t);
2108 for (t = TYPE_METHODS (class); t ; t = TREE_CHAIN (t))
2109 if (!METHOD_ABSTRACT (t))
2111 if (METHOD_NATIVE (t) && !flag_jni)
2112 java_mark_cni_decl_local (t);
2113 else
2114 java_mark_decl_local (t);
2118 /* Add a statement to a compound_expr. */
2120 tree
2121 add_stmt_to_compound (tree existing, tree type, tree stmt)
2123 if (!stmt)
2124 return existing;
2125 else if (existing)
2127 tree expr = build2 (COMPOUND_EXPR, type, existing, stmt);
2128 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (existing)
2129 | TREE_SIDE_EFFECTS (stmt);
2130 return expr;
2132 else
2133 return stmt;
2136 /* Add a statement to the statement_list currently being constructed.
2137 If the statement_list is null, we don't create a singleton list.
2138 This is necessary because poplevel() assumes that adding a
2139 statement to a null statement_list returns the statement. */
2141 tree
2142 java_add_stmt (tree new_stmt)
2144 tree stmts = current_binding_level->stmts;
2145 tree_stmt_iterator i;
2147 if (input_filename)
2148 SET_EXPR_LOCATION (new_stmt, input_location);
2150 if (stmts == NULL)
2151 return current_binding_level->stmts = new_stmt;
2153 /* Force STMTS to be a statement_list. */
2154 if (TREE_CODE (stmts) != STATEMENT_LIST)
2156 tree t = make_node (STATEMENT_LIST);
2157 i = tsi_last (t);
2158 tsi_link_after (&i, stmts, TSI_CONTINUE_LINKING);
2159 stmts = t;
2162 i = tsi_last (stmts);
2163 tsi_link_after (&i, new_stmt, TSI_CONTINUE_LINKING);
2164 TREE_TYPE (stmts) = void_type_node;
2166 return current_binding_level->stmts = stmts;
2169 /* Add a variable to the current scope. */
2171 tree
2172 java_add_local_var (tree decl)
2174 tree *vars = &current_binding_level->names;
2175 tree next = *vars;
2176 TREE_CHAIN (decl) = next;
2177 *vars = decl;
2178 DECL_CONTEXT (decl) = current_function_decl;
2179 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2180 return decl;
2183 /* Return a pointer to the compound_expr currently being
2184 constructed. */
2186 tree *
2187 get_stmts (void)
2189 return &current_binding_level->stmts;
2192 /* Register an exception range as belonging to the current binding
2193 level. There may only be one: if there are more, we'll create more
2194 binding levels. However, each range can have multiple handlers,
2195 and these are expanded when we call expand_end_java_handler(). */
2197 void
2198 register_exception_range (struct eh_range *range, int pc, int end_pc)
2200 gcc_assert (! current_binding_level->exception_range);
2201 current_binding_level->exception_range = range;
2202 current_binding_level->end_pc = end_pc;
2203 current_binding_level->start_pc = pc;
2206 #include "gt-java-decl.h"