ChangeLog:
[official-gcc.git] / gcc / java / decl.c
blob4fcc6c06ee3ddecd31ca2b164b47c9f45e943074
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"
53 #if defined (DEBUG_JAVA_BINDING_LEVELS)
54 extern void indent (void);
55 #endif
57 static tree push_jvm_slot (int, tree);
58 static tree lookup_name_current_level (tree);
59 static tree push_promoted_type (const char *, tree);
60 static struct binding_level *make_binding_level (void);
61 static tree create_primitive_vtable (const char *);
62 static tree check_local_unnamed_variable (tree, tree, tree);
63 static void parse_version (void);
66 /* The following ABI flags are used in the high-order bits of the version
67 ID field. The version ID number itself should never be larger than
68 0xfffff, so it should be safe to use top 12 bits for these flags. */
70 #define FLAG_BINARYCOMPAT_ABI (1<<31) /* Class is built with the BC-ABI. */
72 #define FLAG_BOOTSTRAP_LOADER (1<<30) /* Used when defining a class that
73 should be loaded by the bootstrap
74 loader. */
76 /* If an ABI change is made within a GCC release series, rendering current
77 binaries incompatible with the old runtimes, this number can be set to
78 enforce the compatibility rules. */
79 #define MINOR_BINARYCOMPAT_ABI_VERSION 0
81 /* The runtime may recognize a variety of BC ABIs (objects generated by
82 different version of gcj), but will probably always require strict
83 matching for the ordinary (C++) ABI. */
85 /* The version ID of the BC ABI that we generate. This must be kept in
86 sync with parse_version(), libgcj, and reality (if the BC format changes,
87 this must change). */
88 #define GCJ_CURRENT_BC_ABI_VERSION \
89 (4 * 100000 + 0 * 1000 + MINOR_BINARYCOMPAT_ABI_VERSION)
91 /* The ABI version number. */
92 tree gcj_abi_version;
94 /* Name of the Cloneable class. */
95 tree java_lang_cloneable_identifier_node;
97 /* Name of the Serializable class. */
98 tree java_io_serializable_identifier_node;
100 /* The DECL_MAP is a mapping from (index, type) to a decl node.
101 If index < max_locals, it is the index of a local variable.
102 if index >= max_locals, then index-max_locals is a stack slot.
103 The DECL_MAP mapping is represented as a TREE_VEC whose elements
104 are a list of decls (VAR_DECL or PARM_DECL) chained by
105 DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
106 we search the chain for a decl with a matching TREE_TYPE. */
108 static GTY(()) tree decl_map;
110 /* The base_decl_map is contains one variable of ptr_type: this is
111 used to contain every variable of reference type that is ever
112 stored in a local variable slot. */
114 static GTY(()) tree base_decl_map;
116 /* An index used to make temporary identifiers unique. */
117 static int uniq;
119 /* A list of local variables VAR_DECLs for this method that we have seen
120 debug information, but we have not reached their starting (byte) PC yet. */
122 static GTY(()) tree pending_local_decls;
124 /* The decl for "_Jv_ResolvePoolEntry". */
125 tree soft_resolvepoolentry_node;
127 /* The decl for the .constants field of an instance of Class. */
128 tree constants_field_decl_node;
130 /* The decl for the .data field of an instance of Class. */
131 tree constants_data_field_decl_node;
133 #if defined(DEBUG_JAVA_BINDING_LEVELS)
134 int binding_depth = 0;
135 int is_class_level = 0;
136 int current_pc;
138 void
139 indent (void)
141 int i;
143 for (i = 0; i < binding_depth*2; i++)
144 putc (' ', stderr);
146 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
148 /* True if decl is a named local variable, i.e. if it is an alias
149 that's used only for debugging purposes. */
151 static bool
152 debug_variable_p (tree decl)
154 if (TREE_CODE (decl) == PARM_DECL)
155 return false;
157 if (LOCAL_SLOT_P (decl))
158 return false;
160 return true;
163 static tree
164 push_jvm_slot (int index, tree decl)
166 DECL_CONTEXT (decl) = current_function_decl;
167 layout_decl (decl, 0);
169 /* Now link the decl into the decl_map. */
170 if (DECL_LANG_SPECIFIC (decl) == NULL)
172 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
173 DECL_LOCAL_START_PC (decl) = 0;
174 DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
175 DECL_LOCAL_SLOT_NUMBER (decl) = index;
177 DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
178 TREE_VEC_ELT (decl_map, index) = decl;
180 return decl;
183 /* Find the best declaration based upon type. If 'decl' fits 'type' better
184 than 'best', return 'decl'. Otherwise return 'best'. */
186 static tree
187 check_local_unnamed_variable (tree best, tree decl, tree type)
189 tree decl_type = TREE_TYPE (decl);
191 gcc_assert (! LOCAL_VAR_OUT_OF_SCOPE_P (decl));
193 /* Use the same decl for all integer types <= 32 bits. This is
194 necessary because sometimes a value is stored as (for example)
195 boolean but loaded as int. */
196 if (decl_type == type
197 || (INTEGRAL_TYPE_P (decl_type)
198 && INTEGRAL_TYPE_P (type)
199 && TYPE_PRECISION (decl_type) <= 32
200 && TYPE_PRECISION (type) <= 32
201 && TYPE_PRECISION (decl_type) >= TYPE_PRECISION (type))
202 /* ptr_type_node is used for null pointers, which are
203 assignment compatible with everything. */
204 || (TREE_CODE (decl_type) == POINTER_TYPE
205 && type == ptr_type_node)
206 /* Whenever anyone wants to use a slot that is initially
207 occupied by a PARM_DECL of pointer type they must get that
208 decl, even if they asked for a pointer to a different type.
209 However, if someone wants a scalar variable in a slot that
210 initially held a pointer arg -- or vice versa -- we create a
211 new VAR_DECL.
213 ???: As long as verification is correct, this will be a
214 compatible type. But maybe we should create a dummy variable
215 and replace all references to it with the DECL and a
216 NOP_EXPR.
218 || (TREE_CODE (decl_type) == POINTER_TYPE
219 && TREE_CODE (decl) == PARM_DECL
220 && TREE_CODE (type) == POINTER_TYPE))
222 if (best == NULL_TREE
223 || (decl_type == type && TREE_TYPE (best) != type))
224 return decl;
227 return best;
231 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
232 that is valid at PC (or -1 if any pc).
233 If there is no existing matching decl, allocate one. */
235 tree
236 find_local_variable (int index, tree type, int pc ATTRIBUTE_UNUSED)
238 tree tmp = TREE_VEC_ELT (decl_map, index);
239 tree decl = NULL_TREE;
241 /* Scan through every declaration that has been created in this
242 slot. We're only looking for variables that correspond to local
243 index declarations and PARM_DECLs, not named variables: such
244 local variables are used only for debugging information. */
245 while (tmp != NULL_TREE)
247 if (! debug_variable_p (tmp))
248 decl = check_local_unnamed_variable (decl, tmp, type);
249 tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
252 /* gcj has a function called promote_type(), which is used by both
253 the bytecode compiler and the source compiler. Unfortunately,
254 the type systems for the Java VM and the Java language are not
255 the same: a boolean in the VM promotes to an int, not to a wide
256 boolean. If our caller wants something to hold a boolean, that
257 had better be an int, because that slot might be re-used
258 later in integer context. */
259 if (TREE_CODE (type) == BOOLEAN_TYPE)
260 type = integer_type_node;
262 /* If we don't find a match, create one with the type passed in.
263 The name of the variable is #n#m, which n is the variable index
264 in the local variable area and m is a dummy identifier for
265 uniqueness -- multiple variables may share the same local
266 variable index. We don't call pushdecl() to push pointer types
267 into a binding expr because they'll all be replaced by a single
268 variable that is used for every reference in that local variable
269 slot. */
270 if (! decl)
272 char buf[64];
273 tree name;
274 sprintf (buf, "#slot#%d#%d", index, uniq++);
275 name = get_identifier (buf);
276 decl = build_decl (VAR_DECL, name, type);
277 DECL_IGNORED_P (decl) = 1;
278 DECL_ARTIFICIAL (decl) = 1;
279 decl = push_jvm_slot (index, decl);
280 LOCAL_SLOT_P (decl) = 1;
282 if (TREE_CODE (type) != POINTER_TYPE)
283 pushdecl_function_level (decl);
286 /* As well as creating a local variable that matches the type, we
287 also create a base variable (of ptr_type) that will hold all its
288 aliases. */
289 if (TREE_CODE (type) == POINTER_TYPE
290 && ! TREE_VEC_ELT (base_decl_map, index))
292 char buf[64];
293 tree name;
294 tree base_decl;
295 sprintf (buf, "#ref#%d#%d", index, uniq++);
296 name = get_identifier (buf);
297 base_decl
298 = TREE_VEC_ELT (base_decl_map, index)
299 = build_decl (VAR_DECL, name, ptr_type_node);
300 pushdecl_function_level (base_decl);
301 DECL_IGNORED_P (base_decl) = 1;
302 DECL_ARTIFICIAL (base_decl) = 1;
305 return decl;
308 /* Called during gimplification for every variable. If the variable
309 is a temporary of pointer type, replace it with a common variable
310 thath is used to hold all pointer types that are ever stored in
311 that slot. Set WANT_LVALUE if you want a variable that is to be
312 written to. */
314 tree
315 java_replace_reference (tree var_decl, bool want_lvalue)
317 tree decl_type;
319 if (! base_decl_map)
320 return var_decl;
322 decl_type = TREE_TYPE (var_decl);
324 if (TREE_CODE (decl_type) == POINTER_TYPE)
326 if (DECL_LANG_SPECIFIC (var_decl)
327 && LOCAL_SLOT_P (var_decl))
329 int index = DECL_LOCAL_SLOT_NUMBER (var_decl);
330 tree base_decl = TREE_VEC_ELT (base_decl_map, index);
332 gcc_assert (base_decl);
333 if (! want_lvalue)
334 base_decl = build1 (NOP_EXPR, decl_type, base_decl);
336 return base_decl;
340 return var_decl;
344 /* Same as find_local_index, except that INDEX is a stack index. */
346 tree
347 find_stack_slot (int index, tree type)
349 return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
350 type, -1);
353 struct binding_level GTY(())
355 /* A chain of _DECL nodes for all variables, constants, functions,
356 * and typedef types. These are in the reverse of the order supplied.
358 tree names;
360 /* For each level, a list of shadowed outer-level local definitions
361 to be restored when this level is popped.
362 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
363 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
364 tree shadowed;
366 /* For each level (except not the global one),
367 a chain of BLOCK nodes for all the levels
368 that were entered and exited one level down. */
369 tree blocks;
371 /* The binding level which this one is contained in (inherits from). */
372 struct binding_level *level_chain;
374 /* The bytecode PC that marks the end of this level. */
375 int end_pc;
376 /* The bytecode PC that marks the start of this level. */
377 int start_pc;
379 /* The statements in this binding level. */
380 tree stmts;
382 /* An exception range associated with this binding level. */
383 struct eh_range * GTY((skip (""))) exception_range;
385 /* Binding depth at which this level began. Used only for debugging. */
386 unsigned binding_depth;
389 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
391 /* The binding level currently in effect. */
393 static GTY(()) struct binding_level *current_binding_level;
395 /* A chain of binding_level structures awaiting reuse. */
397 static GTY(()) struct binding_level *free_binding_level;
399 /* The outermost binding level, for names of file scope.
400 This is created when the compiler is started and exists
401 through the entire run. */
403 static GTY(()) struct binding_level *global_binding_level;
405 /* The binding level that holds variables declared at the outermost
406 level within a function body. */
408 static struct binding_level *function_binding_level;
410 /* A PC value bigger than any PC value we may ever may encounter. */
412 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
414 /* Binding level structures are initialized by copying this one. */
416 static const struct binding_level clear_binding_level
418 NULL_TREE, /* names */
419 NULL_TREE, /* shadowed */
420 NULL_TREE, /* blocks */
421 NULL_BINDING_LEVEL, /* level_chain */
422 LARGEST_PC, /* end_pc */
423 0, /* start_pc */
424 NULL, /* stmts */
425 NULL, /* exception_range */
426 0, /* binding_depth */
429 #if 0
430 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
431 that have names. Here so we can clear out their names' definitions
432 at the end of the function. */
434 static tree named_labels;
436 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
438 static tree shadowed_labels;
439 #endif
441 tree java_global_trees[JTI_MAX];
443 /* Build (and pushdecl) a "promoted type" for all standard
444 types shorter than int. */
446 static tree
447 push_promoted_type (const char *name, tree actual_type)
449 tree type = make_node (TREE_CODE (actual_type));
450 #if 1
451 tree in_min = TYPE_MIN_VALUE (int_type_node);
452 tree in_max = TYPE_MAX_VALUE (int_type_node);
453 #else
454 tree in_min = TYPE_MIN_VALUE (actual_type);
455 tree in_max = TYPE_MAX_VALUE (actual_type);
456 #endif
457 TYPE_MIN_VALUE (type) = copy_node (in_min);
458 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
459 TYPE_MAX_VALUE (type) = copy_node (in_max);
460 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
461 TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
462 TYPE_STRING_FLAG (type) = TYPE_STRING_FLAG (actual_type);
463 layout_type (type);
464 pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
465 return type;
468 /* Return a definition for a builtin function named NAME and whose data type
469 is TYPE. TYPE should be a function type with argument types.
470 FUNCTION_CODE tells later passes how to compile calls to this function.
471 See tree.h for its possible values.
473 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
474 the name to be called if we can't opencode the function. If
475 ATTRS is nonzero, use that for the function's attribute list. */
477 tree
478 builtin_function (const char *name,
479 tree type,
480 int function_code,
481 enum built_in_class cl,
482 const char *library_name,
483 tree ARG_UNUSED (attrs))
485 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
486 DECL_EXTERNAL (decl) = 1;
487 TREE_PUBLIC (decl) = 1;
488 if (library_name)
489 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
490 pushdecl (decl);
491 DECL_BUILT_IN_CLASS (decl) = cl;
492 DECL_FUNCTION_CODE (decl) = function_code;
493 return decl;
496 /* Return tree that represents a vtable for a primitive array. */
497 static tree
498 create_primitive_vtable (const char *name)
500 tree r;
501 char buf[50];
503 sprintf (buf, "_Jv_%sVTable", name);
504 r = build_decl (VAR_DECL, get_identifier (buf), ptr_type_node);
505 DECL_EXTERNAL (r) = 1;
506 return r;
509 static tree
510 do_nothing (tree t)
512 return t;
515 /* Parse the version string and compute the ABI version number. */
516 static void
517 parse_version (void)
519 const char *p = version_string;
520 unsigned int major = 0, minor = 0;
521 unsigned int abi_version;
523 /* Skip leading junk. */
524 while (*p && !ISDIGIT (*p))
525 ++p;
526 gcc_assert (*p);
528 /* Extract major version. */
529 while (ISDIGIT (*p))
531 major = major * 10 + *p - '0';
532 ++p;
535 gcc_assert (*p == '.' && ISDIGIT (p[1]));
536 ++p;
538 /* Extract minor version. */
539 while (ISDIGIT (*p))
541 minor = minor * 10 + *p - '0';
542 ++p;
545 if (flag_indirect_dispatch)
547 abi_version = GCJ_CURRENT_BC_ABI_VERSION;
548 abi_version |= FLAG_BINARYCOMPAT_ABI;
550 else /* C++ ABI */
552 /* Implicit in this computation is the idea that we won't break the
553 old-style binary ABI in a sub-minor release (e.g., from 4.0.0 to
554 4.0.1). */
555 abi_version = 100000 * major + 1000 * minor;
557 if (flag_bootstrap_classes)
558 abi_version |= FLAG_BOOTSTRAP_LOADER;
560 gcj_abi_version = build_int_cstu (ptr_type_node, abi_version);
563 void
564 java_init_decl_processing (void)
566 tree endlink;
567 tree field = NULL_TREE;
568 tree t;
570 init_class_processing ();
572 current_function_decl = NULL;
573 current_binding_level = NULL_BINDING_LEVEL;
574 free_binding_level = NULL_BINDING_LEVEL;
575 pushlevel (0); /* make the binding_level structure for global names */
576 global_binding_level = current_binding_level;
578 /* The code here must be similar to build_common_tree_nodes{,_2} in
579 tree.c, especially as to the order of initializing common nodes. */
580 error_mark_node = make_node (ERROR_MARK);
581 TREE_TYPE (error_mark_node) = error_mark_node;
583 /* Create sizetype first - needed for other types. */
584 initialize_sizetypes (false);
586 byte_type_node = make_signed_type (8);
587 pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
588 short_type_node = make_signed_type (16);
589 pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
590 int_type_node = make_signed_type (32);
591 pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
592 long_type_node = make_signed_type (64);
593 pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));
595 unsigned_byte_type_node = make_unsigned_type (8);
596 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
597 unsigned_byte_type_node));
598 unsigned_short_type_node = make_unsigned_type (16);
599 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
600 unsigned_short_type_node));
601 unsigned_int_type_node = make_unsigned_type (32);
602 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
603 unsigned_int_type_node));
604 unsigned_long_type_node = make_unsigned_type (64);
605 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
606 unsigned_long_type_node));
608 /* This is not a java type, however tree-dfa requires a definition for
609 size_type_node. */
610 size_type_node = make_unsigned_type (POINTER_SIZE);
611 set_sizetype (size_type_node);
613 /* Define these next since types below may used them. */
614 integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
615 integer_zero_node = build_int_cst (NULL_TREE, 0);
616 integer_one_node = build_int_cst (NULL_TREE, 1);
617 integer_two_node = build_int_cst (NULL_TREE, 2);
618 integer_four_node = build_int_cst (NULL_TREE, 4);
619 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
621 /* A few values used for range checking in the lexer. */
622 decimal_int_max = build_int_cstu (unsigned_int_type_node, 0x80000000);
623 #if HOST_BITS_PER_WIDE_INT == 64
624 decimal_long_max = build_int_cstu (unsigned_long_type_node,
625 0x8000000000000000LL);
626 #elif HOST_BITS_PER_WIDE_INT == 32
627 decimal_long_max = build_int_cst_wide (unsigned_long_type_node,
628 0, 0x80000000);
629 #else
630 #error "unsupported size"
631 #endif
633 size_zero_node = size_int (0);
634 size_one_node = size_int (1);
635 bitsize_zero_node = bitsize_int (0);
636 bitsize_one_node = bitsize_int (1);
637 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
639 long_zero_node = build_int_cst (long_type_node, 0);
641 void_type_node = make_node (VOID_TYPE);
642 pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
643 layout_type (void_type_node); /* Uses size_zero_node */
645 ptr_type_node = build_pointer_type (void_type_node);
646 const_ptr_type_node
647 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
649 t = make_node (VOID_TYPE);
650 layout_type (t); /* Uses size_zero_node */
651 return_address_type_node = build_pointer_type (t);
653 null_pointer_node = build_int_cst (ptr_type_node, 0);
655 #if 0
656 /* Make a type to be the domain of a few array types
657 whose domains don't really matter.
658 200 is small enough that it always fits in size_t
659 and large enough that it can hold most function names for the
660 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
661 short_array_type_node = build_prim_array_type (short_type_node, 200);
662 #endif
663 char_type_node = make_node (INTEGER_TYPE);
664 TYPE_STRING_FLAG (char_type_node) = 1;
665 TYPE_PRECISION (char_type_node) = 16;
666 fixup_unsigned_type (char_type_node);
667 pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));
669 boolean_type_node = make_node (BOOLEAN_TYPE);
670 TYPE_PRECISION (boolean_type_node) = 1;
671 fixup_unsigned_type (boolean_type_node);
672 pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
673 boolean_type_node));
674 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
675 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
677 promoted_byte_type_node
678 = push_promoted_type ("promoted_byte", byte_type_node);
679 promoted_short_type_node
680 = push_promoted_type ("promoted_short", short_type_node);
681 promoted_char_type_node
682 = push_promoted_type ("promoted_char", char_type_node);
683 promoted_boolean_type_node
684 = push_promoted_type ("promoted_boolean", boolean_type_node);
686 float_type_node = make_node (REAL_TYPE);
687 TYPE_PRECISION (float_type_node) = 32;
688 pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
689 float_type_node));
690 layout_type (float_type_node);
692 double_type_node = make_node (REAL_TYPE);
693 TYPE_PRECISION (double_type_node) = 64;
694 pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
695 double_type_node));
696 layout_type (double_type_node);
698 float_zero_node = build_real (float_type_node, dconst0);
699 double_zero_node = build_real (double_type_node, dconst0);
701 /* These are the vtables for arrays of primitives. */
702 boolean_array_vtable = create_primitive_vtable ("boolean");
703 byte_array_vtable = create_primitive_vtable ("byte");
704 char_array_vtable = create_primitive_vtable ("char");
705 short_array_vtable = create_primitive_vtable ("short");
706 int_array_vtable = create_primitive_vtable ("int");
707 long_array_vtable = create_primitive_vtable ("long");
708 float_array_vtable = create_primitive_vtable ("float");
709 double_array_vtable = create_primitive_vtable ("double");
711 one_elt_array_domain_type = build_index_type (integer_one_node);
712 utf8const_type = make_node (RECORD_TYPE);
713 PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
714 PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
715 FINISH_RECORD (utf8const_type);
716 utf8const_ptr_type = build_pointer_type (utf8const_type);
718 atable_type = build_array_type (ptr_type_node,
719 one_elt_array_domain_type);
720 TYPE_NONALIASED_COMPONENT (atable_type) = 1;
721 atable_ptr_type = build_pointer_type (atable_type);
723 itable_type = build_array_type (ptr_type_node,
724 one_elt_array_domain_type);
725 TYPE_NONALIASED_COMPONENT (itable_type) = 1;
726 itable_ptr_type = build_pointer_type (itable_type);
728 symbol_type = make_node (RECORD_TYPE);
729 PUSH_FIELD (symbol_type, field, "clname", utf8const_ptr_type);
730 PUSH_FIELD (symbol_type, field, "name", utf8const_ptr_type);
731 PUSH_FIELD (symbol_type, field, "signature", utf8const_ptr_type);
732 FINISH_RECORD (symbol_type);
734 symbols_array_type = build_array_type (symbol_type,
735 one_elt_array_domain_type);
736 symbols_array_ptr_type = build_pointer_type (symbols_array_type);
738 assertion_entry_type = make_node (RECORD_TYPE);
739 PUSH_FIELD (assertion_entry_type, field, "assertion_code", integer_type_node);
740 PUSH_FIELD (assertion_entry_type, field, "op1", utf8const_ptr_type);
741 PUSH_FIELD (assertion_entry_type, field, "op2", utf8const_ptr_type);
742 FINISH_RECORD (assertion_entry_type);
744 assertion_table_type = build_array_type (assertion_entry_type,
745 one_elt_array_domain_type);
747 /* As you're adding items here, please update the code right after
748 this section, so that the filename containing the source code of
749 the pre-defined class gets registered correctly. */
750 unqualified_object_id_node = get_identifier ("Object");
751 object_type_node = lookup_class (get_identifier ("java.lang.Object"));
752 object_ptr_type_node = promote_type (object_type_node);
753 string_type_node = lookup_class (get_identifier ("java.lang.String"));
754 string_ptr_type_node = promote_type (string_type_node);
755 class_type_node = lookup_class (get_identifier ("java.lang.Class"));
756 throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
757 exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
758 runtime_exception_type_node =
759 lookup_class (get_identifier ("java.lang.RuntimeException"));
760 error_exception_type_node =
761 lookup_class (get_identifier ("java.lang.Error"));
763 rawdata_ptr_type_node
764 = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
766 add_predefined_file (get_identifier ("java/lang/Class.java"));
767 add_predefined_file (get_identifier ("java/lang/Error.java"));
768 add_predefined_file (get_identifier ("java/lang/Object.java"));
769 add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
770 add_predefined_file (get_identifier ("java/lang/String.java"));
771 add_predefined_file (get_identifier ("java/lang/Throwable.java"));
772 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
773 add_predefined_file (get_identifier ("java/lang/Exception.java"));
774 add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
775 add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
777 methodtable_type = make_node (RECORD_TYPE);
778 layout_type (methodtable_type);
779 build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
780 methodtable_ptr_type = build_pointer_type (methodtable_type);
782 TYPE_identifier_node = get_identifier ("TYPE");
783 init_identifier_node = get_identifier ("<init>");
784 clinit_identifier_node = get_identifier ("<clinit>");
785 finit_identifier_node = get_identifier ("finit$");
786 instinit_identifier_node = get_identifier ("instinit$");
787 void_signature_node = get_identifier ("()V");
788 length_identifier_node = get_identifier ("length");
789 finalize_identifier_node = get_identifier ("finalize");
790 this_identifier_node = get_identifier ("this");
791 super_identifier_node = get_identifier ("super");
792 continue_identifier_node = get_identifier ("continue");
793 access0_identifier_node = get_identifier ("access$0");
794 classdollar_identifier_node = get_identifier ("class$");
796 java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
797 java_io_serializable_identifier_node =
798 get_identifier ("java.io.Serializable");
800 /* for lack of a better place to put this stub call */
801 init_expr_processing();
803 constants_type_node = make_node (RECORD_TYPE);
804 PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
805 PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
806 PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
807 constants_data_field_decl_node = field;
808 FINISH_RECORD (constants_type_node);
809 build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
811 access_flags_type_node = unsigned_short_type_node;
813 dtable_type = make_node (RECORD_TYPE);
814 dtable_ptr_type = build_pointer_type (dtable_type);
816 otable_type = build_array_type (integer_type_node,
817 one_elt_array_domain_type);
818 TYPE_NONALIASED_COMPONENT (otable_type) = 1;
819 otable_ptr_type = build_pointer_type (otable_type);
821 PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
822 DECL_FCONTEXT (field) = object_type_node;
823 TYPE_VFIELD (object_type_node) = field;
825 /* This isn't exactly true, but it is what we have in the source.
826 There is an unresolved issue here, which is whether the vtable
827 should be marked by the GC. */
828 if (! flag_hash_synchronization)
829 PUSH_FIELD (object_type_node, field, "sync_info",
830 build_pointer_type (object_type_node));
831 for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
832 FIELD_PRIVATE (t) = 1;
833 FINISH_RECORD (object_type_node);
835 field_type_node = make_node (RECORD_TYPE);
836 field_ptr_type_node = build_pointer_type (field_type_node);
837 method_type_node = make_node (RECORD_TYPE);
838 method_ptr_type_node = build_pointer_type (method_type_node);
840 set_super_info (0, class_type_node, object_type_node, 0);
841 set_super_info (0, string_type_node, object_type_node, 0);
842 class_ptr_type = build_pointer_type (class_type_node);
844 PUSH_FIELD (class_type_node, field, "next_or_version", class_ptr_type);
845 PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
846 PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
847 PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
848 PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
849 constants_field_decl_node = field;
850 PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
851 PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
852 PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
853 PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
854 PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
855 PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
856 PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
857 PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
858 PUSH_FIELD (class_type_node, field, "otable", otable_ptr_type);
859 PUSH_FIELD (class_type_node, field, "otable_syms",
860 symbols_array_ptr_type);
861 PUSH_FIELD (class_type_node, field, "atable", atable_ptr_type);
862 PUSH_FIELD (class_type_node, field, "atable_syms",
863 symbols_array_ptr_type);
864 PUSH_FIELD (class_type_node, field, "itable", itable_ptr_type);
865 PUSH_FIELD (class_type_node, field, "itable_syms",
866 symbols_array_ptr_type);
867 PUSH_FIELD (class_type_node, field, "catch_classes", ptr_type_node);
868 PUSH_FIELD (class_type_node, field, "interfaces",
869 build_pointer_type (class_ptr_type));
870 PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
871 PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
872 PUSH_FIELD (class_type_node, field, "state", byte_type_node);
873 PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
874 PUSH_FIELD (class_type_node, field, "depth", short_type_node);
875 PUSH_FIELD (class_type_node, field, "ancestors", ptr_type_node);
876 PUSH_FIELD (class_type_node, field, "idt", ptr_type_node);
877 PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);
878 PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
879 PUSH_FIELD (class_type_node, field, "assertion_table", ptr_type_node);
880 PUSH_FIELD (class_type_node, field, "hack_signers", ptr_type_node);
881 PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
882 PUSH_FIELD (class_type_node, field, "aux_info", ptr_type_node);
883 PUSH_FIELD (class_type_node, field, "engine", ptr_type_node);
884 for (t = TYPE_FIELDS (class_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
885 FIELD_PRIVATE (t) = 1;
886 push_super_field (class_type_node, object_type_node);
888 FINISH_RECORD (class_type_node);
889 build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
891 field_info_union_node = make_node (UNION_TYPE);
892 PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
893 PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
894 #if 0
895 PUSH_FIELD (field_info_union_node, field, "idx", unsigned_short_type_node);
896 #endif
897 layout_type (field_info_union_node);
899 PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
900 PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
901 PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
902 PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
903 PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
904 FINISH_RECORD (field_type_node);
905 build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
907 nativecode_ptr_array_type_node
908 = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
910 PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
911 PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
912 FINISH_RECORD (dtable_type);
913 build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
915 jexception_type = make_node (RECORD_TYPE);
916 PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
917 PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
918 PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
919 PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
920 FINISH_RECORD (jexception_type);
921 build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
922 jexception_ptr_type = build_pointer_type (jexception_type);
924 lineNumberEntry_type = make_node (RECORD_TYPE);
925 PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
926 PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
927 FINISH_RECORD (lineNumberEntry_type);
929 lineNumbers_type = make_node (RECORD_TYPE);
930 PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
931 FINISH_RECORD (lineNumbers_type);
933 PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
934 PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
935 PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
936 PUSH_FIELD (method_type_node, field, "index", unsigned_short_type_node);
937 PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
938 PUSH_FIELD (method_type_node, field, "throws", ptr_type_node);
939 FINISH_RECORD (method_type_node);
940 build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
942 endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
944 t = tree_cons (NULL_TREE, class_ptr_type, endlink);
945 alloc_object_node = builtin_function ("_Jv_AllocObject",
946 build_function_type (ptr_type_node, t),
947 0, NOT_BUILT_IN, NULL, NULL_TREE);
948 DECL_IS_MALLOC (alloc_object_node) = 1;
949 alloc_no_finalizer_node =
950 builtin_function ("_Jv_AllocObjectNoFinalizer",
951 build_function_type (ptr_type_node, t),
952 0, NOT_BUILT_IN, NULL, NULL_TREE);
953 DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
955 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
956 soft_initclass_node = builtin_function ("_Jv_InitClass",
957 build_function_type (void_type_node,
959 0, NOT_BUILT_IN, NULL, NULL_TREE);
960 t = tree_cons (NULL_TREE, class_ptr_type,
961 tree_cons (NULL_TREE, int_type_node, endlink));
962 soft_resolvepoolentry_node
963 = builtin_function ("_Jv_ResolvePoolEntry",
964 build_function_type (ptr_type_node, t),
965 0,NOT_BUILT_IN, NULL, NULL_TREE);
966 DECL_IS_PURE (soft_resolvepoolentry_node) = 1;
967 throw_node = builtin_function ("_Jv_Throw",
968 build_function_type (void_type_node, t),
969 0, NOT_BUILT_IN, NULL, NULL_TREE);
970 /* Mark throw_nodes as `noreturn' functions with side effects. */
971 TREE_THIS_VOLATILE (throw_node) = 1;
972 TREE_SIDE_EFFECTS (throw_node) = 1;
974 t = build_function_type (void_type_node, tree_cons (NULL_TREE, ptr_type_node,
975 endlink));
976 soft_monitorenter_node
977 = builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
978 NULL, NULL_TREE);
979 soft_monitorexit_node
980 = builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
981 NULL, NULL_TREE);
983 t = tree_cons (NULL_TREE, ptr_type_node,
984 tree_cons (NULL_TREE, int_type_node, endlink));
985 soft_newarray_node
986 = builtin_function ("_Jv_NewPrimArray",
987 build_function_type (ptr_type_node, t),
988 0, NOT_BUILT_IN, NULL, NULL_TREE);
989 DECL_IS_MALLOC (soft_newarray_node) = 1;
991 t = tree_cons (NULL_TREE, int_type_node,
992 tree_cons (NULL_TREE, class_ptr_type,
993 tree_cons (NULL_TREE, object_ptr_type_node,
994 endlink)));
995 soft_anewarray_node
996 = builtin_function ("_Jv_NewObjectArray",
997 build_function_type (ptr_type_node, t),
998 0, NOT_BUILT_IN, NULL, NULL_TREE);
999 DECL_IS_MALLOC (soft_anewarray_node) = 1;
1001 /* There is no endlink here because _Jv_NewMultiArray is a varargs
1002 function. */
1003 t = tree_cons (NULL_TREE, ptr_type_node,
1004 tree_cons (NULL_TREE, int_type_node, NULL_TREE));
1005 soft_multianewarray_node
1006 = builtin_function ("_Jv_NewMultiArray",
1007 build_function_type (ptr_type_node, t),
1008 0, NOT_BUILT_IN, NULL, NULL_TREE);
1009 DECL_IS_MALLOC (soft_multianewarray_node) = 1;
1011 t = build_function_type (void_type_node,
1012 tree_cons (NULL_TREE, int_type_node, endlink));
1013 soft_badarrayindex_node
1014 = builtin_function ("_Jv_ThrowBadArrayIndex", t,
1015 0, NOT_BUILT_IN, NULL, NULL_TREE);
1016 /* Mark soft_badarrayindex_node as a `noreturn' function with side
1017 effects. */
1018 TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
1019 TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
1021 soft_nullpointer_node
1022 = builtin_function ("_Jv_ThrowNullPointerException",
1023 build_function_type (void_type_node, endlink),
1024 0, NOT_BUILT_IN, NULL, NULL_TREE);
1025 /* Mark soft_nullpointer_node as a `noreturn' function with side
1026 effects. */
1027 TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
1028 TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
1030 soft_abstractmethod_node
1031 = builtin_function ("_Jv_ThrowAbstractMethodError",
1032 build_function_type (void_type_node, endlink),
1033 0, NOT_BUILT_IN, NULL, NULL_TREE);
1034 /* Mark soft_abstractmethod_node as a `noreturn' function with side
1035 effects. */
1036 TREE_THIS_VOLATILE (soft_abstractmethod_node) = 1;
1037 TREE_SIDE_EFFECTS (soft_abstractmethod_node) = 1;
1039 soft_nosuchfield_node
1040 = builtin_function ("_Jv_ThrowNoSuchFieldError",
1041 build_function_type (void_type_node, endlink),
1042 0, NOT_BUILT_IN, NULL, NULL_TREE);
1043 /* Mark soft_nosuchfield_node as a `noreturn' function with side
1044 effects. */
1045 TREE_THIS_VOLATILE (soft_nosuchfield_node) = 1;
1046 TREE_SIDE_EFFECTS (soft_nosuchfield_node) = 1;
1048 t = tree_cons (NULL_TREE, class_ptr_type,
1049 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1050 soft_checkcast_node
1051 = builtin_function ("_Jv_CheckCast",
1052 build_function_type (ptr_type_node, t),
1053 0, NOT_BUILT_IN, NULL, NULL_TREE);
1054 t = tree_cons (NULL_TREE, object_ptr_type_node,
1055 tree_cons (NULL_TREE, class_ptr_type, endlink));
1056 soft_instanceof_node
1057 = builtin_function ("_Jv_IsInstanceOf",
1058 build_function_type (boolean_type_node, t),
1059 0, NOT_BUILT_IN, NULL, NULL_TREE);
1060 DECL_IS_PURE (soft_instanceof_node) = 1;
1061 t = tree_cons (NULL_TREE, object_ptr_type_node,
1062 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1063 soft_checkarraystore_node
1064 = builtin_function ("_Jv_CheckArrayStore",
1065 build_function_type (void_type_node, t),
1066 0, NOT_BUILT_IN, NULL, NULL_TREE);
1067 t = tree_cons (NULL_TREE, ptr_type_node,
1068 tree_cons (NULL_TREE, ptr_type_node,
1069 tree_cons (NULL_TREE, int_type_node, endlink)));
1070 soft_lookupinterfacemethod_node
1071 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
1072 build_function_type (ptr_type_node, t),
1073 0, NOT_BUILT_IN, NULL, NULL_TREE);
1074 DECL_IS_PURE (soft_lookupinterfacemethod_node) = 1;
1075 t = tree_cons (NULL_TREE, ptr_type_node,
1076 tree_cons (NULL_TREE, ptr_type_node,
1077 tree_cons (NULL_TREE, ptr_type_node, endlink)));
1078 soft_lookupinterfacemethodbyname_node
1079 = builtin_function ("_Jv_LookupInterfaceMethod",
1080 build_function_type (ptr_type_node, t),
1081 0, NOT_BUILT_IN, NULL, NULL_TREE);
1082 t = tree_cons (NULL_TREE, object_ptr_type_node,
1083 tree_cons (NULL_TREE, ptr_type_node,
1084 tree_cons (NULL_TREE, ptr_type_node,
1085 tree_cons (NULL_TREE, int_type_node,
1086 endlink))));
1087 soft_lookupjnimethod_node
1088 = builtin_function ("_Jv_LookupJNIMethod",
1089 build_function_type (ptr_type_node, t),
1090 0, NOT_BUILT_IN, NULL, NULL_TREE);
1091 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1092 soft_getjnienvnewframe_node
1093 = builtin_function ("_Jv_GetJNIEnvNewFrame",
1094 build_function_type (ptr_type_node, t),
1095 0, NOT_BUILT_IN, NULL, NULL_TREE);
1096 soft_jnipopsystemframe_node
1097 = builtin_function ("_Jv_JNI_PopSystemFrame",
1098 build_function_type (void_type_node, t),
1099 0, NOT_BUILT_IN, NULL, NULL_TREE);
1101 t = tree_cons (NULL_TREE, object_ptr_type_node, endlink);
1102 soft_unwrapjni_node
1103 = builtin_function ("_Jv_UnwrapJNIweakReference",
1104 build_function_type (object_ptr_type_node, t),
1105 0, NOT_BUILT_IN, NULL, NULL_TREE);
1107 t = tree_cons (NULL_TREE, int_type_node,
1108 tree_cons (NULL_TREE, int_type_node, endlink));
1109 soft_idiv_node
1110 = builtin_function ("_Jv_divI",
1111 build_function_type (int_type_node, t),
1112 0, NOT_BUILT_IN, NULL, NULL_TREE);
1114 soft_irem_node
1115 = builtin_function ("_Jv_remI",
1116 build_function_type (int_type_node, t),
1117 0, NOT_BUILT_IN, NULL, NULL_TREE);
1119 t = tree_cons (NULL_TREE, long_type_node,
1120 tree_cons (NULL_TREE, long_type_node, endlink));
1121 soft_ldiv_node
1122 = builtin_function ("_Jv_divJ",
1123 build_function_type (long_type_node, t),
1124 0, NOT_BUILT_IN, NULL, NULL_TREE);
1126 soft_lrem_node
1127 = builtin_function ("_Jv_remJ",
1128 build_function_type (long_type_node, t),
1129 0, NOT_BUILT_IN, NULL, NULL_TREE);
1131 /* Initialize variables for except.c. */
1132 eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
1133 ? "__gcj_personality_sj0"
1134 : "__gcj_personality_v0");
1135 default_init_unwind_resume_libfunc ();
1137 lang_eh_runtime_type = do_nothing;
1139 init_jcf_parse ();
1141 initialize_builtins ();
1142 soft_fmod_node = built_in_decls[BUILT_IN_FMOD];
1143 #if 0
1144 soft_fmodf_node = built_in_decls[BUILT_IN_FMODF];
1145 #endif
1147 parse_version ();
1151 /* Look up NAME in the current binding level and its superiors
1152 in the namespace of variables, functions and typedefs.
1153 Return a ..._DECL node of some kind representing its definition,
1154 or return 0 if it is undefined. */
1156 tree
1157 lookup_name (tree name)
1159 tree val;
1160 if (current_binding_level != global_binding_level
1161 && IDENTIFIER_LOCAL_VALUE (name))
1162 val = IDENTIFIER_LOCAL_VALUE (name);
1163 else
1164 val = IDENTIFIER_GLOBAL_VALUE (name);
1165 return val;
1168 /* Similar to `lookup_name' but look only at current binding level and
1169 the previous one if it's the parameter level. */
1171 static tree
1172 lookup_name_current_level (tree name)
1174 tree t;
1176 if (current_binding_level == global_binding_level)
1177 return IDENTIFIER_GLOBAL_VALUE (name);
1179 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
1180 return 0;
1182 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
1183 if (DECL_NAME (t) == name)
1184 break;
1186 return t;
1189 /* Use a binding level to record a labeled block declaration */
1191 void
1192 push_labeled_block (tree lb)
1194 tree name = DECL_NAME (LABELED_BLOCK_LABEL (lb));
1195 struct binding_level *b = current_binding_level;
1196 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1197 if (oldlocal != 0)
1198 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1199 TREE_CHAIN (lb) = b->names;
1200 b->names = lb;
1201 IDENTIFIER_LOCAL_VALUE (name) = lb;
1204 /* Pop the current binding level, reinstalling values for the previous
1205 labeled block */
1207 void
1208 pop_labeled_block (void)
1210 struct binding_level *b = current_binding_level;
1211 tree label = b->names;
1212 IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label))) =
1213 NULL_TREE;
1214 if (b->shadowed)
1215 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b->shadowed)) =
1216 TREE_VALUE (b->shadowed);
1218 /* Pop the current level, and free the structure for reuse. */
1219 current_binding_level = current_binding_level->level_chain;
1220 b->level_chain = free_binding_level;
1221 free_binding_level = b;
1224 /* Record a decl-node X as belonging to the current lexical scope.
1225 Check for errors (such as an incompatible declaration for the same
1226 name already seen in the same scope).
1228 Returns either X or an old decl for the same name.
1229 If an old decl is returned, it may have been smashed
1230 to agree with what X says. */
1232 tree
1233 pushdecl (tree x)
1235 tree t;
1236 tree name = DECL_NAME (x);
1237 struct binding_level *b = current_binding_level;
1239 if (TREE_CODE (x) != TYPE_DECL)
1240 DECL_CONTEXT (x) = current_function_decl;
1241 if (name)
1243 t = lookup_name_current_level (name);
1244 if (t != 0 && t == error_mark_node)
1245 /* error_mark_node is 0 for a while during initialization! */
1247 t = 0;
1248 error ("%q+D used prior to declaration", x);
1251 /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1252 to point to the TYPE_DECL.
1253 Since Java does not have typedefs, a type can only have
1254 one (true) name, given by a class, interface, or builtin. */
1255 if (TREE_CODE (x) == TYPE_DECL
1256 && TYPE_NAME (TREE_TYPE (x)) == 0
1257 && TREE_TYPE (x) != error_mark_node)
1259 TYPE_NAME (TREE_TYPE (x)) = x;
1260 TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1263 /* This name is new in its binding level.
1264 Install the new declaration and return it. */
1265 if (b == global_binding_level)
1267 /* Install a global value. */
1269 IDENTIFIER_GLOBAL_VALUE (name) = x;
1271 else
1273 /* Here to install a non-global value. */
1274 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1275 IDENTIFIER_LOCAL_VALUE (name) = x;
1277 #if 0
1278 /* Warn if shadowing an argument at the top level of the body. */
1279 if (oldlocal != 0 && !DECL_EXTERNAL (x)
1280 /* This warning doesn't apply to the parms of a nested fcn. */
1281 && ! current_binding_level->parm_flag
1282 /* Check that this is one level down from the parms. */
1283 && current_binding_level->level_chain->parm_flag
1284 /* Check that the decl being shadowed
1285 comes from the parm level, one level up. */
1286 && chain_member (oldlocal, current_binding_level->level_chain->names))
1288 if (TREE_CODE (oldlocal) == PARM_DECL)
1289 pedwarn ("declaration of %qs shadows a parameter",
1290 IDENTIFIER_POINTER (name));
1291 else
1292 pedwarn ("declaration of %qs shadows a symbol from the parameter list",
1293 IDENTIFIER_POINTER (name));
1296 /* Maybe warn if shadowing something else. */
1297 else if (warn_shadow && !DECL_EXTERNAL (x)
1298 /* No shadow warnings for internally generated vars. */
1299 && DECL_SOURCE_LINE (x) != 0
1300 /* No shadow warnings for vars made for inlining. */
1301 && ! DECL_FROM_INLINE (x))
1303 const char *warnstring = 0;
1305 if (TREE_CODE (x) == PARM_DECL
1306 && current_binding_level->level_chain->parm_flag)
1307 /* Don't warn about the parm names in function declarator
1308 within a function declarator.
1309 It would be nice to avoid warning in any function
1310 declarator in a declaration, as opposed to a definition,
1311 but there is no way to tell it's not a definition. */
1313 else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
1314 warnstring = "declaration of %qs shadows a parameter";
1315 else if (oldlocal != 0)
1316 warnstring = "declaration of %qs shadows previous local";
1317 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1318 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1319 warnstring = "declaration of %qs shadows global declaration";
1321 if (warnstring)
1322 warning (0, warnstring, IDENTIFIER_POINTER (name));
1324 #endif
1326 /* If storing a local value, there may already be one (inherited).
1327 If so, record it for restoration when this binding level ends. */
1328 if (oldlocal != 0)
1329 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1333 /* Put decls on list in reverse order.
1334 We will reverse them later if necessary. */
1335 TREE_CHAIN (x) = b->names;
1336 b->names = x;
1338 return x;
1341 void
1342 pushdecl_force_head (tree x)
1344 current_binding_level->names = x;
1347 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1349 tree
1350 pushdecl_top_level (tree x)
1352 tree t;
1353 struct binding_level *b = current_binding_level;
1355 current_binding_level = global_binding_level;
1356 t = pushdecl (x);
1357 current_binding_level = b;
1358 return t;
1361 /* Like pushdecl, only it places X in FUNCTION_BINDING_LEVEL, if appropriate. */
1363 tree
1364 pushdecl_function_level (tree x)
1366 tree t;
1367 struct binding_level *b = current_binding_level;
1369 current_binding_level = function_binding_level;
1370 t = pushdecl (x);
1371 current_binding_level = b;
1372 return t;
1375 /* Nonzero if we are currently in the global binding level. */
1378 global_bindings_p (void)
1380 return current_binding_level == global_binding_level;
1383 /* Return the list of declarations of the current level.
1384 Note that this list is in reverse order unless/until
1385 you nreverse it; and when you do nreverse it, you must
1386 store the result back using `storedecls' or you will lose. */
1388 tree
1389 getdecls (void)
1391 return current_binding_level->names;
1394 /* Create a new `struct binding_level'. */
1396 static struct binding_level *
1397 make_binding_level (void)
1399 /* NOSTRICT */
1400 return ggc_alloc_cleared (sizeof (struct binding_level));
1403 void
1404 pushlevel (int unused ATTRIBUTE_UNUSED)
1406 struct binding_level *newlevel = NULL_BINDING_LEVEL;
1408 #if 0
1409 /* If this is the top level of a function,
1410 just make sure that NAMED_LABELS is 0. */
1412 if (current_binding_level == global_binding_level)
1413 named_labels = 0;
1414 #endif
1416 /* Reuse or create a struct for this binding level. */
1418 if (free_binding_level)
1420 newlevel = free_binding_level;
1421 free_binding_level = free_binding_level->level_chain;
1423 else
1425 newlevel = make_binding_level ();
1428 /* Add this level to the front of the chain (stack) of levels that
1429 are active. */
1431 *newlevel = clear_binding_level;
1432 newlevel->level_chain = current_binding_level;
1433 current_binding_level = newlevel;
1434 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1435 newlevel->binding_depth = binding_depth;
1436 indent ();
1437 fprintf (stderr, "push %s level %p pc %d\n",
1438 (is_class_level) ? "class" : "block", newlevel, current_pc);
1439 is_class_level = 0;
1440 binding_depth++;
1441 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1444 /* Exit a binding level.
1445 Pop the level off, and restore the state of the identifier-decl mappings
1446 that were in effect when this level was entered.
1448 If KEEP is nonzero, this level had explicit declarations, so
1449 and create a "block" (a BLOCK node) for the level
1450 to record its declarations and subblocks for symbol table output.
1452 If FUNCTIONBODY is nonzero, this level is the body of a function,
1453 so create a block as if KEEP were set and also clear out all
1454 label names.
1456 If REVERSE is nonzero, reverse the order of decls before putting
1457 them into the BLOCK. */
1459 tree
1460 poplevel (int keep, int reverse, int functionbody)
1462 tree link;
1463 /* The chain of decls was accumulated in reverse order.
1464 Put it into forward order, just for cleanliness. */
1465 tree decls;
1466 tree subblocks = current_binding_level->blocks;
1467 tree block = 0;
1468 tree decl;
1469 tree bind = 0;
1471 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1472 binding_depth--;
1473 indent ();
1474 if (current_binding_level->end_pc != LARGEST_PC)
1475 fprintf (stderr, "pop %s level %p pc %d (end pc %d)\n",
1476 (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1477 current_binding_level->end_pc);
1478 else
1479 fprintf (stderr, "pop %s level %p pc %d\n",
1480 (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1481 #if 0
1482 if (is_class_level != (current_binding_level == class_binding_level))
1484 indent ();
1485 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
1487 is_class_level = 0;
1488 #endif
1489 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1491 /* Get the decls in the order they were written.
1492 Usually current_binding_level->names is in reverse order.
1493 But parameter decls were previously put in forward order. */
1495 if (reverse)
1496 current_binding_level->names
1497 = decls = nreverse (current_binding_level->names);
1498 else
1499 decls = current_binding_level->names;
1501 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1502 if (TREE_CODE (decl) == VAR_DECL
1503 && DECL_LANG_SPECIFIC (decl) != NULL
1504 && DECL_LOCAL_SLOT_NUMBER (decl))
1505 LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
1507 /* If there were any declarations in that level,
1508 or if this level is a function body,
1509 create a BLOCK to record them for the life of this function. */
1511 block = 0;
1512 if (keep || functionbody)
1514 block = make_node (BLOCK);
1515 TREE_TYPE (block) = void_type_node;
1518 if (current_binding_level->exception_range)
1519 expand_end_java_handler (current_binding_level->exception_range);
1521 if (block != 0)
1523 /* If any statements have been generated at this level, create a
1524 BIND_EXPR to hold them and copy the variables to it. This
1525 only applies to the bytecode compiler. */
1526 if (current_binding_level->stmts)
1528 tree decl = decls;
1529 tree *var = &BLOCK_VARS (block);
1531 /* Copy decls from names list, ignoring labels. */
1532 while (decl)
1534 tree next = TREE_CHAIN (decl);
1535 if (TREE_CODE (decl) != LABEL_DECL)
1537 *var = decl;
1538 var = &TREE_CHAIN (decl);
1540 decl = next;
1542 *var = NULL;
1544 bind = build3 (BIND_EXPR, TREE_TYPE (block), BLOCK_VARS (block),
1545 BLOCK_EXPR_BODY (block), block);
1546 BIND_EXPR_BODY (bind) = current_binding_level->stmts;
1548 if (BIND_EXPR_BODY (bind)
1549 && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
1550 TREE_SIDE_EFFECTS (bind) = 1;
1552 /* FIXME: gimplifier brain damage. */
1553 if (BIND_EXPR_BODY (bind) == NULL)
1554 BIND_EXPR_BODY (bind) = build_java_empty_stmt ();
1556 current_binding_level->stmts = NULL;
1558 else
1560 BLOCK_VARS (block) = decls;
1562 BLOCK_SUBBLOCKS (block) = subblocks;
1565 /* In each subblock, record that this is its superior. */
1567 for (link = subblocks; link; link = TREE_CHAIN (link))
1568 BLOCK_SUPERCONTEXT (link) = block;
1570 /* Clear out the meanings of the local variables of this level. */
1572 for (link = decls; link; link = TREE_CHAIN (link))
1574 tree name = DECL_NAME (link);
1575 if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1577 /* If the ident. was used or addressed via a local extern decl,
1578 don't forget that fact. */
1579 if (DECL_EXTERNAL (link))
1581 if (TREE_USED (link))
1582 TREE_USED (name) = 1;
1583 if (TREE_ADDRESSABLE (link))
1584 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1586 IDENTIFIER_LOCAL_VALUE (name) = 0;
1590 /* Restore all name-meanings of the outer levels
1591 that were shadowed by this level. */
1593 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1594 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1596 /* If the level being exited is the top level of a function,
1597 check over all the labels, and clear out the current
1598 (function local) meanings of their names. */
1600 if (functionbody)
1602 /* If this is the top level block of a function,
1603 the vars are the function's parameters.
1604 Don't leave them in the BLOCK because they are
1605 found in the FUNCTION_DECL instead. */
1607 BLOCK_VARS (block) = 0;
1609 /* Clear out the definitions of all label names,
1610 since their scopes end here,
1611 and add them to BLOCK_VARS. */
1613 #if 0
1614 for (link = named_labels; link; link = TREE_CHAIN (link))
1616 tree label = TREE_VALUE (link);
1618 if (DECL_INITIAL (label) == 0)
1620 error ("label %q+D used but not defined", label);
1621 /* Avoid crashing later. */
1622 define_label (input_location, DECL_NAME (label));
1624 else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
1625 warning (0, "label %q+D defined but not used", label);
1626 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1628 /* Put the labels into the "variables" of the
1629 top-level block, so debugger can see them. */
1630 TREE_CHAIN (label) = BLOCK_VARS (block);
1631 BLOCK_VARS (block) = label;
1633 #endif
1636 /* Pop the current level, and free the structure for reuse. */
1639 struct binding_level *level = current_binding_level;
1640 current_binding_level = current_binding_level->level_chain;
1642 level->level_chain = free_binding_level;
1643 free_binding_level = level;
1646 /* Dispose of the block that we just made inside some higher level. */
1647 if (functionbody)
1649 DECL_INITIAL (current_function_decl) = block;
1650 DECL_SAVED_TREE (current_function_decl) = bind;
1652 else
1654 if (block)
1656 current_binding_level->blocks
1657 = chainon (current_binding_level->blocks, block);
1659 /* If we did not make a block for the level just exited,
1660 any blocks made for inner levels
1661 (since they cannot be recorded as subblocks in that level)
1662 must be carried forward so they will later become subblocks
1663 of something else. */
1664 else if (subblocks)
1665 current_binding_level->blocks
1666 = chainon (current_binding_level->blocks, subblocks);
1668 if (bind)
1669 java_add_stmt (bind);
1672 if (block)
1673 TREE_USED (block) = 1;
1674 return block;
1677 void
1678 maybe_pushlevels (int pc)
1680 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1681 current_pc = pc;
1682 #endif
1684 while (pending_local_decls != NULL_TREE &&
1685 DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1687 tree *ptr = &pending_local_decls;
1688 tree decl = *ptr, next;
1689 int end_pc = DECL_LOCAL_END_PC (decl);
1691 while (*ptr != NULL_TREE
1692 && DECL_LOCAL_START_PC (*ptr) <= pc
1693 && DECL_LOCAL_END_PC (*ptr) == end_pc)
1694 ptr = &TREE_CHAIN (*ptr);
1695 pending_local_decls = *ptr;
1696 *ptr = NULL_TREE;
1698 /* Force non-nested range to be nested in current range by
1699 truncating variable lifetimes. */
1700 if (end_pc > current_binding_level->end_pc)
1702 tree t;
1703 end_pc = current_binding_level->end_pc;
1704 for (t = decl; t != NULL_TREE; t = TREE_CHAIN (t))
1705 DECL_LOCAL_END_PC (t) = end_pc;
1708 maybe_start_try (pc, end_pc);
1710 pushlevel (1);
1712 current_binding_level->end_pc = end_pc;
1713 current_binding_level->start_pc = pc;
1714 current_binding_level->names = NULL;
1715 for ( ; decl != NULL_TREE; decl = next)
1717 int index = DECL_LOCAL_SLOT_NUMBER (decl);
1718 tree base_decl;
1719 next = TREE_CHAIN (decl);
1720 push_jvm_slot (index, decl);
1721 pushdecl (decl);
1722 base_decl
1723 = find_local_variable (index, TREE_TYPE (decl), pc);
1724 if (TREE_CODE (TREE_TYPE (base_decl)) == POINTER_TYPE)
1725 base_decl = TREE_VEC_ELT (base_decl_map, index);
1726 SET_DECL_VALUE_EXPR (decl, base_decl);
1727 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1731 maybe_start_try (pc, 0);
1734 void
1735 maybe_poplevels (int pc)
1737 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1738 current_pc = pc;
1739 #endif
1741 /* FIXME: I'm pretty sure that this is wrong. Variable scopes are
1742 inclusive, so a variable is live if pc == end_pc. Here, we
1743 terminate a range if the current pc is equal to the end of the
1744 range, and this is *before* we have generated code for the
1745 instruction at end_pc. We're closing a binding level one
1746 instruction too early.*/
1747 while (current_binding_level->end_pc <= pc)
1748 poplevel (1, 0, 0);
1751 /* Terminate any binding which began during the range beginning at
1752 start_pc. This tidies up improperly nested local variable ranges
1753 and exception handlers; a variable declared within an exception
1754 range is forcibly terminated when that exception ends. */
1756 void
1757 force_poplevels (int start_pc)
1759 while (current_binding_level->start_pc > start_pc)
1761 if (pedantic && current_binding_level->start_pc > start_pc)
1762 warning (0, "In %+D: overlapped variable and exception ranges at %d",
1763 current_function_decl,
1764 current_binding_level->start_pc);
1765 poplevel (1, 0, 0);
1769 /* Insert BLOCK at the end of the list of subblocks of the
1770 current binding level. This is used when a BIND_EXPR is expanded,
1771 to handle the BLOCK node inside the BIND_EXPR. */
1773 void
1774 insert_block (tree block)
1776 TREE_USED (block) = 1;
1777 current_binding_level->blocks
1778 = chainon (current_binding_level->blocks, block);
1781 /* integrate_decl_tree calls this function. */
1783 void
1784 java_dup_lang_specific_decl (tree node)
1786 int lang_decl_size;
1787 struct lang_decl *x;
1789 if (!DECL_LANG_SPECIFIC (node))
1790 return;
1792 lang_decl_size = sizeof (struct lang_decl);
1793 x = ggc_alloc (lang_decl_size);
1794 memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1795 DECL_LANG_SPECIFIC (node) = x;
1798 void
1799 give_name_to_locals (JCF *jcf)
1801 int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1802 int code_offset = DECL_CODE_OFFSET (current_function_decl);
1803 tree parm;
1804 pending_local_decls = NULL_TREE;
1805 if (n == 0)
1806 return;
1807 JCF_SEEK (jcf, n);
1808 n = JCF_readu2 (jcf);
1809 for (i = 0; i < n; i++)
1811 int start_pc = JCF_readu2 (jcf);
1812 int length = JCF_readu2 (jcf);
1813 int name_index = JCF_readu2 (jcf);
1814 int signature_index = JCF_readu2 (jcf);
1815 int slot = JCF_readu2 (jcf);
1816 tree name = get_name_constant (jcf, name_index);
1817 tree type = parse_signature (jcf, signature_index);
1818 if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1819 && start_pc == 0
1820 && length == DECL_CODE_LENGTH (current_function_decl))
1822 tree decl = TREE_VEC_ELT (decl_map, slot);
1823 DECL_NAME (decl) = name;
1824 if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1825 warning (0, "bad type in parameter debug info");
1827 else
1829 tree *ptr;
1830 int end_pc = start_pc + length;
1831 tree decl = build_decl (VAR_DECL, name, type);
1832 if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1834 warning (0, "bad PC range for debug info for local %q+D",
1835 decl);
1836 end_pc = DECL_CODE_LENGTH (current_function_decl);
1839 /* Adjust start_pc if necessary so that the local's first
1840 store operation will use the relevant DECL as a
1841 destination. Fore more information, read the leading
1842 comments for expr.c:maybe_adjust_start_pc. */
1843 start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1845 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1846 DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1847 DECL_LOCAL_START_PC (decl) = start_pc;
1848 #if 0
1849 /* FIXME: The range used internally for exceptions and local
1850 variable ranges, is a half-open interval:
1851 start_pc <= pc < end_pc. However, the range used in the
1852 Java VM spec is inclusive at both ends:
1853 start_pc <= pc <= end_pc. */
1854 end_pc++;
1855 #endif
1856 DECL_LOCAL_END_PC (decl) = end_pc;
1858 /* Now insert the new decl in the proper place in
1859 pending_local_decls. We are essentially doing an insertion sort,
1860 which works fine, since the list input will normally already
1861 be sorted. */
1862 ptr = &pending_local_decls;
1863 while (*ptr != NULL_TREE
1864 && (DECL_LOCAL_START_PC (*ptr) > start_pc
1865 || (DECL_LOCAL_START_PC (*ptr) == start_pc
1866 && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1867 ptr = &TREE_CHAIN (*ptr);
1868 TREE_CHAIN (decl) = *ptr;
1869 *ptr = decl;
1873 pending_local_decls = nreverse (pending_local_decls);
1875 /* Fill in default names for the parameters. */
1876 for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1877 parm != NULL_TREE; parm = TREE_CHAIN (parm), i++)
1879 if (DECL_NAME (parm) == NULL_TREE)
1881 int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1882 if (arg_i == 0)
1883 DECL_NAME (parm) = get_identifier ("this");
1884 else
1886 char buffer[12];
1887 sprintf (buffer, "ARG_%d", arg_i);
1888 DECL_NAME (parm) = get_identifier (buffer);
1894 tree
1895 build_result_decl (tree fndecl)
1897 tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1898 tree result = DECL_RESULT (fndecl);
1899 if (! result)
1901 /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1902 if (INTEGRAL_TYPE_P (restype)
1903 && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
1904 restype = integer_type_node;
1905 result = build_decl (RESULT_DECL, NULL_TREE, restype);
1906 DECL_ARTIFICIAL (result) = 1;
1907 DECL_IGNORED_P (result) = 1;
1908 DECL_CONTEXT (result) = fndecl;
1909 DECL_RESULT (fndecl) = result;
1911 return result;
1914 void
1915 start_java_method (tree fndecl)
1917 tree tem, *ptr;
1918 int i;
1920 uniq = 0;
1922 current_function_decl = fndecl;
1923 announce_function (fndecl);
1925 i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1926 decl_map = make_tree_vec (i);
1927 base_decl_map = make_tree_vec (i);
1928 type_map = xrealloc (type_map, i * sizeof (tree));
1930 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1931 fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
1932 current_pc = 0;
1933 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1934 pushlevel (1); /* Push parameters. */
1936 ptr = &DECL_ARGUMENTS (fndecl);
1937 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1938 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
1940 tree parm_name = NULL_TREE, parm_decl;
1941 tree parm_type = TREE_VALUE (tem);
1942 gcc_assert (i < DECL_MAX_LOCALS (fndecl));
1944 parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
1945 DECL_CONTEXT (parm_decl) = fndecl;
1946 if (targetm.calls.promote_prototypes (parm_type)
1947 && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
1948 && INTEGRAL_TYPE_P (parm_type))
1949 parm_type = integer_type_node;
1950 DECL_ARG_TYPE (parm_decl) = parm_type;
1952 *ptr = parm_decl;
1953 ptr = &TREE_CHAIN (parm_decl);
1955 /* Add parm_decl to the decl_map. */
1956 push_jvm_slot (i, parm_decl);
1958 type_map[i] = TREE_TYPE (parm_decl);
1959 if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
1961 i++;
1962 type_map[i] = void_type_node;
1965 *ptr = NULL_TREE;
1966 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
1968 while (i < DECL_MAX_LOCALS(fndecl))
1969 type_map[i++] = NULL_TREE;
1971 build_result_decl (fndecl);
1973 /* Push local variables. */
1974 pushlevel (2);
1976 function_binding_level = current_binding_level;
1979 void
1980 end_java_method (void)
1982 tree fndecl = current_function_decl;
1984 /* pop out of function */
1985 poplevel (1, 1, 0);
1987 /* pop out of its parameters */
1988 poplevel (1, 0, 1);
1990 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
1992 if (DECL_SAVED_TREE (fndecl))
1994 tree fbody, block_body;
1995 /* Before we check initialization, attached all class initialization
1996 variable to the block_body */
1997 fbody = DECL_SAVED_TREE (fndecl);
1998 block_body = BIND_EXPR_BODY (fbody);
1999 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
2000 attach_init_test_initialization_flags, block_body);
2003 finish_method (fndecl);
2005 if (! flag_unit_at_a_time)
2007 /* Nulling these fields when we no longer need them saves
2008 memory. */
2009 DECL_SAVED_TREE (fndecl) = NULL;
2010 DECL_STRUCT_FUNCTION (fndecl) = NULL;
2011 DECL_INITIAL (fndecl) = NULL_TREE;
2013 current_function_decl = NULL_TREE;
2016 /* Prepare a method for expansion. */
2018 void
2019 finish_method (tree fndecl)
2021 tree *tp = &DECL_SAVED_TREE (fndecl);
2023 /* Wrap body of synchronized methods in a monitorenter,
2024 plus monitorexit cleanup. */
2025 if (METHOD_SYNCHRONIZED (fndecl))
2027 tree enter, exit, lock;
2028 if (METHOD_STATIC (fndecl))
2029 lock = build_class_ref (DECL_CONTEXT (fndecl));
2030 else
2031 lock = DECL_ARGUMENTS (fndecl);
2032 BUILD_MONITOR_ENTER (enter, lock);
2033 BUILD_MONITOR_EXIT (exit, lock);
2034 *tp = build2 (COMPOUND_EXPR, void_type_node, enter,
2035 build2 (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
2038 /* Prepend class initialization for static methods reachable from
2039 other classes. */
2040 if (METHOD_STATIC (fndecl)
2041 && (! METHOD_PRIVATE (fndecl)
2042 || INNER_CLASS_P (DECL_CONTEXT (fndecl)))
2043 && ! DECL_CLINIT_P (fndecl)
2044 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
2046 tree clas = DECL_CONTEXT (fndecl);
2047 tree init = build3 (CALL_EXPR, void_type_node,
2048 build_address_of (soft_initclass_node),
2049 build_tree_list (NULL_TREE, build_class_ref (clas)),
2050 NULL_TREE);
2051 *tp = build2 (COMPOUND_EXPR, TREE_TYPE (*tp), init, *tp);
2054 /* Convert function tree to GENERIC prior to inlining. */
2055 java_genericize (fndecl);
2057 /* Store the end of the function, so that we get good line number
2058 info for the epilogue. */
2059 if (DECL_STRUCT_FUNCTION (fndecl))
2060 cfun = DECL_STRUCT_FUNCTION (fndecl);
2061 else
2062 allocate_struct_function (fndecl);
2063 #ifdef USE_MAPPED_LOCATION
2064 cfun->function_end_locus = DECL_FUNCTION_LAST_LINE (fndecl);
2065 #else
2066 cfun->function_end_locus.file = DECL_SOURCE_FILE (fndecl);
2067 cfun->function_end_locus.line = DECL_FUNCTION_LAST_LINE (fndecl);
2068 #endif
2070 /* Defer inlining and expansion to the cgraph optimizers. */
2071 cgraph_finalize_function (fndecl, false);
2074 /* Optimize and expand a function's entire body. */
2076 void
2077 java_expand_body (tree fndecl)
2079 tree_rest_of_compilation (fndecl);
2082 /* We pessimistically marked all methods and fields external until we
2083 knew what set of classes we were planning to compile. Now mark those
2084 associated with CLASS to be generated locally as not external. */
2086 static void
2087 java_mark_decl_local (tree decl)
2089 DECL_EXTERNAL (decl) = 0;
2091 /* If we've already constructed DECL_RTL, give encode_section_info
2092 a second chance, now that we've changed the flags. */
2093 /* ??? Ideally, we'd have flag_unit_at_a_time set, and not have done
2094 anything that would have referenced DECL_RTL so far. But at the
2095 moment we force flag_unit_at_a_time off due to excessive memory
2096 consumption when compiling large jar files. Which probably means
2097 that we need to re-order how we process jar files... */
2098 if (DECL_RTL_SET_P (decl))
2099 make_decl_rtl (decl);
2102 /* Given appropriate target support, G++ will emit hidden aliases for native
2103 methods. Using this hidden name is required for proper operation of
2104 _Jv_Method::ncode, but it doesn't hurt to use it everywhere. Look for
2105 proper target support, then mark the method for aliasing. */
2107 static void
2108 java_mark_cni_decl_local (tree decl)
2110 /* Setting DECL_LOCAL_CNI_METHOD_P changes the behavior of the mangler.
2111 We expect that we should not yet have referenced this decl in a
2112 context that requires it. Check this invariant even if we don't have
2113 support for hidden aliases. */
2114 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
2116 #if !defined(HAVE_GAS_HIDDEN) || !defined(ASM_OUTPUT_DEF)
2117 return;
2118 #endif
2120 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2121 DECL_LOCAL_CNI_METHOD_P (decl) = 1;
2124 /* Use the preceding two functions and mark all members of the class. */
2126 void
2127 java_mark_class_local (tree class)
2129 tree t;
2131 for (t = TYPE_FIELDS (class); t ; t = TREE_CHAIN (t))
2132 if (FIELD_STATIC (t))
2133 java_mark_decl_local (t);
2135 for (t = TYPE_METHODS (class); t ; t = TREE_CHAIN (t))
2136 if (!METHOD_ABSTRACT (t))
2138 if (METHOD_NATIVE (t) && !flag_jni)
2139 java_mark_cni_decl_local (t);
2140 else
2141 java_mark_decl_local (t);
2145 /* Add a statement to a compound_expr. */
2147 tree
2148 add_stmt_to_compound (tree existing, tree type, tree stmt)
2150 if (!stmt)
2151 return existing;
2152 else if (existing)
2154 tree expr = build2 (COMPOUND_EXPR, type, existing, stmt);
2155 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (existing)
2156 | TREE_SIDE_EFFECTS (stmt);
2157 return expr;
2159 else
2160 return stmt;
2163 /* Add a statement to the statement_list currently being constructed.
2164 If the statement_list is null, we don't create a singleton list.
2165 This is necessary because poplevel() assumes that adding a
2166 statement to a null statement_list returns the statement. */
2168 tree
2169 java_add_stmt (tree new_stmt)
2171 tree stmts = current_binding_level->stmts;
2172 tree_stmt_iterator i;
2174 if (input_filename)
2175 SET_EXPR_LOCATION (new_stmt, input_location);
2177 if (stmts == NULL)
2178 return current_binding_level->stmts = new_stmt;
2180 /* Force STMTS to be a statement_list. */
2181 if (TREE_CODE (stmts) != STATEMENT_LIST)
2183 tree t = make_node (STATEMENT_LIST);
2184 i = tsi_last (t);
2185 tsi_link_after (&i, stmts, TSI_CONTINUE_LINKING);
2186 stmts = t;
2189 i = tsi_last (stmts);
2190 tsi_link_after (&i, new_stmt, TSI_CONTINUE_LINKING);
2191 TREE_TYPE (stmts) = void_type_node;
2193 return current_binding_level->stmts = stmts;
2196 /* Add a variable to the current scope. */
2198 tree
2199 java_add_local_var (tree decl)
2201 tree *vars = &current_binding_level->names;
2202 tree next = *vars;
2203 TREE_CHAIN (decl) = next;
2204 *vars = decl;
2205 DECL_CONTEXT (decl) = current_function_decl;
2206 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2207 return decl;
2210 /* Return a pointer to the compound_expr currently being
2211 constructed. */
2213 tree *
2214 get_stmts (void)
2216 return &current_binding_level->stmts;
2219 /* Register an exception range as belonging to the current binding
2220 level. There may only be one: if there are more, we'll create more
2221 binding levels. However, each range can have multiple handlers,
2222 and these are expanded when we call expand_end_java_handler(). */
2224 void
2225 register_exception_range (struct eh_range *range, int pc, int end_pc)
2227 gcc_assert (! current_binding_level->exception_range);
2228 current_binding_level->exception_range = range;
2229 current_binding_level->end_pc = end_pc;
2230 current_binding_level->start_pc = pc;
2233 #include "gt-java-decl.h"