Merged gcj-eclipse to the gcj-eclipse-jmx branch:
[official-gcc.git] / gcc / java / decl.c
blobd8051ed65b2eaf8b384cdb873c4f8f2813d0829b
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, 2006
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 /* Copy the value in decl into every live alias in the same local
164 variable slot. Some of these will be dead stores removed by the
165 optimizer. */
167 void
168 update_aliases (tree decl, int index, int pc)
170 tree decl_type = TREE_TYPE (decl);
171 tree tmp;
173 gcc_assert (! debug_variable_p (decl));
175 for (tmp = TREE_VEC_ELT (decl_map, index);
176 tmp != NULL_TREE;
177 tmp = DECL_LOCAL_SLOT_CHAIN (tmp))
179 tree tmp_type = TREE_TYPE (tmp);
180 if (tmp != decl
181 && LOCAL_SLOT_P (tmp) == 0
182 && (pc == -1
183 || (pc >= DECL_LOCAL_START_PC (tmp)
184 && pc < DECL_LOCAL_END_PC (tmp)))
185 /* This test is < (rather than <=) because there's no point
186 updating an alias that's about to die at the end of this
187 instruction. */
188 && (tmp_type == decl_type
189 || (INTEGRAL_TYPE_P (tmp_type)
190 && INTEGRAL_TYPE_P (decl_type)
191 && TYPE_PRECISION (decl_type) <= 32
192 && TYPE_PRECISION (tmp_type) <= 32)
193 || (TREE_CODE (tmp_type) == POINTER_TYPE
194 && TREE_CODE (decl_type) == POINTER_TYPE)))
196 tree src = build1 (NOP_EXPR, tmp_type, decl);
197 gcc_assert (! LOCAL_VAR_OUT_OF_SCOPE_P (tmp));
198 java_add_stmt (build2 (MODIFY_EXPR, tmp_type, tmp, src));
203 static tree
204 push_jvm_slot (int index, tree decl)
206 DECL_CONTEXT (decl) = current_function_decl;
207 layout_decl (decl, 0);
209 /* Now link the decl into the decl_map. */
210 if (DECL_LANG_SPECIFIC (decl) == NULL)
212 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
213 DECL_LOCAL_START_PC (decl) = 0;
214 DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
215 DECL_LOCAL_SLOT_NUMBER (decl) = index;
217 DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
218 TREE_VEC_ELT (decl_map, index) = decl;
220 return decl;
223 /* At the point of its creation a local variable decl inherits
224 whatever is already in the same slot. In the case of a local
225 variable that is declared but unused, we won't find anything. */
227 static void
228 initialize_local_variable (tree decl, int index)
230 tree decl_type = TREE_TYPE (decl);
231 if (TREE_CODE (decl_type) == POINTER_TYPE)
233 tree tmp = TREE_VEC_ELT (base_decl_map, index);
235 if (tmp)
237 /* At the point of its creation this decl inherits whatever
238 is in the slot. */
239 tree src = build1 (NOP_EXPR, decl_type, tmp);
240 java_add_stmt (build2 (MODIFY_EXPR, decl_type, decl, src));
243 else
245 tree tmp;
247 for (tmp = TREE_VEC_ELT (decl_map, index);
248 tmp != NULL_TREE;
249 tmp = DECL_LOCAL_SLOT_CHAIN (tmp))
251 tree tmp_type = TREE_TYPE (tmp);
252 if (tmp != decl
253 && ! debug_variable_p (tmp)
254 && (tmp_type == decl_type
255 || (INTEGRAL_TYPE_P (tmp_type)
256 && INTEGRAL_TYPE_P (decl_type)
257 && TYPE_PRECISION (decl_type) <= 32
258 && TYPE_PRECISION (tmp_type) <= 32
259 && TYPE_PRECISION (tmp_type)
260 >= TYPE_PRECISION (decl_type))))
262 java_add_stmt (build2 (MODIFY_EXPR, decl_type, decl, tmp));
263 return;
269 /* Find the best declaration based upon type. If 'decl' fits 'type' better
270 than 'best', return 'decl'. Otherwise return 'best'. */
272 static tree
273 check_local_unnamed_variable (tree best, tree decl, tree type)
275 tree decl_type = TREE_TYPE (decl);
277 gcc_assert (! LOCAL_VAR_OUT_OF_SCOPE_P (decl));
279 /* Use the same decl for all integer types <= 32 bits. This is
280 necessary because sometimes a value is stored as (for example)
281 boolean but loaded as int. */
282 if (decl_type == type
283 || (INTEGRAL_TYPE_P (decl_type)
284 && INTEGRAL_TYPE_P (type)
285 && TYPE_PRECISION (decl_type) <= 32
286 && TYPE_PRECISION (type) <= 32
287 && TYPE_PRECISION (decl_type) >= TYPE_PRECISION (type))
288 /* ptr_type_node is used for null pointers, which are
289 assignment compatible with everything. */
290 || (TREE_CODE (decl_type) == POINTER_TYPE
291 && type == ptr_type_node)
292 /* Whenever anyone wants to use a slot that is initially
293 occupied by a PARM_DECL of pointer type they must get that
294 decl, even if they asked for a pointer to a different type.
295 However, if someone wants a scalar variable in a slot that
296 initially held a pointer arg -- or vice versa -- we create a
297 new VAR_DECL.
299 ???: As long as verification is correct, this will be a
300 compatible type. But maybe we should create a dummy variable
301 and replace all references to it with the DECL and a
302 NOP_EXPR.
304 || (TREE_CODE (decl_type) == POINTER_TYPE
305 && TREE_CODE (decl) == PARM_DECL
306 && TREE_CODE (type) == POINTER_TYPE))
308 if (best == NULL_TREE
309 || (decl_type == type && TREE_TYPE (best) != type))
310 return decl;
313 return best;
317 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
318 that is valid at PC (or -1 if any pc).
319 If there is no existing matching decl, allocate one. */
321 tree
322 find_local_variable (int index, tree type, int pc ATTRIBUTE_UNUSED)
324 tree tmp = TREE_VEC_ELT (decl_map, index);
325 tree decl = NULL_TREE;
327 /* Scan through every declaration that has been created in this
328 slot. We're only looking for variables that correspond to local
329 index declarations and PARM_DECLs, not named variables: such
330 local variables are used only for debugging information. */
331 while (tmp != NULL_TREE)
333 if (! debug_variable_p (tmp))
334 decl = check_local_unnamed_variable (decl, tmp, type);
335 tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
338 /* gcj has a function called promote_type(), which is used by both
339 the bytecode compiler and the source compiler. Unfortunately,
340 the type systems for the Java VM and the Java language are not
341 the same: a boolean in the VM promotes to an int, not to a wide
342 boolean. If our caller wants something to hold a boolean, that
343 had better be an int, because that slot might be re-used
344 later in integer context. */
345 if (TREE_CODE (type) == BOOLEAN_TYPE)
346 type = integer_type_node;
348 /* If we don't find a match, create one with the type passed in.
349 The name of the variable is #n#m, which n is the variable index
350 in the local variable area and m is a dummy identifier for
351 uniqueness -- multiple variables may share the same local
352 variable index. We don't call pushdecl() to push pointer types
353 into a binding expr because they'll all be replaced by a single
354 variable that is used for every reference in that local variable
355 slot. */
356 if (! decl)
358 char buf[64];
359 tree name;
360 sprintf (buf, "#slot#%d#%d", index, uniq++);
361 name = get_identifier (buf);
362 decl = build_decl (VAR_DECL, name, type);
363 DECL_IGNORED_P (decl) = 1;
364 DECL_ARTIFICIAL (decl) = 1;
365 decl = push_jvm_slot (index, decl);
366 LOCAL_SLOT_P (decl) = 1;
368 if (TREE_CODE (type) != POINTER_TYPE)
369 pushdecl_function_level (decl);
372 /* As well as creating a local variable that matches the type, we
373 also create a base variable (of ptr_type) that will hold all its
374 aliases. */
375 if (TREE_CODE (type) == POINTER_TYPE
376 && ! TREE_VEC_ELT (base_decl_map, index))
378 char buf[64];
379 tree name;
380 tree base_decl;
381 sprintf (buf, "#ref#%d#%d", index, uniq++);
382 name = get_identifier (buf);
383 base_decl
384 = TREE_VEC_ELT (base_decl_map, index)
385 = build_decl (VAR_DECL, name, ptr_type_node);
386 pushdecl_function_level (base_decl);
387 DECL_IGNORED_P (base_decl) = 1;
388 DECL_ARTIFICIAL (base_decl) = 1;
391 return decl;
394 /* Called during gimplification for every variable. If the variable
395 is a temporary of pointer type, replace it with a common variable
396 thath is used to hold all pointer types that are ever stored in
397 that slot. Set WANT_LVALUE if you want a variable that is to be
398 written to. */
400 tree
401 java_replace_reference (tree var_decl, bool want_lvalue)
403 tree decl_type;
405 if (! base_decl_map)
406 return var_decl;
408 decl_type = TREE_TYPE (var_decl);
410 if (TREE_CODE (decl_type) == POINTER_TYPE)
412 if (DECL_LANG_SPECIFIC (var_decl)
413 && LOCAL_SLOT_P (var_decl))
415 int index = DECL_LOCAL_SLOT_NUMBER (var_decl);
416 tree base_decl = TREE_VEC_ELT (base_decl_map, index);
418 gcc_assert (base_decl);
419 if (! want_lvalue)
420 base_decl = build1 (NOP_EXPR, decl_type, base_decl);
422 return base_decl;
426 return var_decl;
430 /* Same as find_local_index, except that INDEX is a stack index. */
432 tree
433 find_stack_slot (int index, tree type)
435 return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
436 type, -1);
439 struct binding_level GTY(())
441 /* A chain of _DECL nodes for all variables, constants, functions,
442 * and typedef types. These are in the reverse of the order supplied.
444 tree names;
446 /* For each level, a list of shadowed outer-level local definitions
447 to be restored when this level is popped.
448 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
449 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
450 tree shadowed;
452 /* For each level (except not the global one),
453 a chain of BLOCK nodes for all the levels
454 that were entered and exited one level down. */
455 tree blocks;
457 /* The binding level which this one is contained in (inherits from). */
458 struct binding_level *level_chain;
460 /* The bytecode PC that marks the end of this level. */
461 int end_pc;
462 /* The bytecode PC that marks the start of this level. */
463 int start_pc;
465 /* The statements in this binding level. */
466 tree stmts;
468 /* An exception range associated with this binding level. */
469 struct eh_range * GTY((skip (""))) exception_range;
471 /* Binding depth at which this level began. Used only for debugging. */
472 unsigned binding_depth;
475 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
477 /* The binding level currently in effect. */
479 static GTY(()) struct binding_level *current_binding_level;
481 /* A chain of binding_level structures awaiting reuse. */
483 static GTY(()) struct binding_level *free_binding_level;
485 /* The outermost binding level, for names of file scope.
486 This is created when the compiler is started and exists
487 through the entire run. */
489 static GTY(()) struct binding_level *global_binding_level;
491 /* The binding level that holds variables declared at the outermost
492 level within a function body. */
494 static struct binding_level *function_binding_level;
496 /* A PC value bigger than any PC value we may ever may encounter. */
498 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
500 /* Binding level structures are initialized by copying this one. */
502 static const struct binding_level clear_binding_level
504 NULL_TREE, /* names */
505 NULL_TREE, /* shadowed */
506 NULL_TREE, /* blocks */
507 NULL_BINDING_LEVEL, /* level_chain */
508 LARGEST_PC, /* end_pc */
509 0, /* start_pc */
510 NULL, /* stmts */
511 NULL, /* exception_range */
512 0, /* binding_depth */
515 #if 0
516 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
517 that have names. Here so we can clear out their names' definitions
518 at the end of the function. */
520 static tree named_labels;
522 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
524 static tree shadowed_labels;
525 #endif
527 tree java_global_trees[JTI_MAX];
529 /* Build (and pushdecl) a "promoted type" for all standard
530 types shorter than int. */
532 static tree
533 push_promoted_type (const char *name, tree actual_type)
535 tree type = make_node (TREE_CODE (actual_type));
536 #if 1
537 tree in_min = TYPE_MIN_VALUE (int_type_node);
538 tree in_max = TYPE_MAX_VALUE (int_type_node);
539 #else
540 tree in_min = TYPE_MIN_VALUE (actual_type);
541 tree in_max = TYPE_MAX_VALUE (actual_type);
542 #endif
543 TYPE_MIN_VALUE (type) = copy_node (in_min);
544 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
545 TYPE_MAX_VALUE (type) = copy_node (in_max);
546 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
547 TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
548 TYPE_STRING_FLAG (type) = TYPE_STRING_FLAG (actual_type);
549 layout_type (type);
550 pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
551 return type;
554 /* Return a definition for a builtin function named NAME and whose data type
555 is TYPE. TYPE should be a function type with argument types.
556 FUNCTION_CODE tells later passes how to compile calls to this function.
557 See tree.h for its possible values.
559 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
560 the name to be called if we can't opencode the function. If
561 ATTRS is nonzero, use that for the function's attribute list. */
563 tree
564 builtin_function (const char *name,
565 tree type,
566 int function_code,
567 enum built_in_class cl,
568 const char *library_name,
569 tree ARG_UNUSED (attrs))
571 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
572 DECL_EXTERNAL (decl) = 1;
573 TREE_PUBLIC (decl) = 1;
574 if (library_name)
575 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
576 pushdecl (decl);
577 DECL_BUILT_IN_CLASS (decl) = cl;
578 DECL_FUNCTION_CODE (decl) = function_code;
579 return decl;
582 /* Return tree that represents a vtable for a primitive array. */
583 static tree
584 create_primitive_vtable (const char *name)
586 tree r;
587 char buf[50];
589 sprintf (buf, "_Jv_%sVTable", name);
590 r = build_decl (VAR_DECL, get_identifier (buf), ptr_type_node);
591 DECL_EXTERNAL (r) = 1;
592 return r;
595 static tree
596 do_nothing (tree t)
598 return t;
601 /* Parse the version string and compute the ABI version number. */
602 static void
603 parse_version (void)
605 const char *p = version_string;
606 unsigned int major = 0, minor = 0;
607 unsigned int abi_version;
609 /* Skip leading junk. */
610 while (*p && !ISDIGIT (*p))
611 ++p;
612 gcc_assert (*p);
614 /* Extract major version. */
615 while (ISDIGIT (*p))
617 major = major * 10 + *p - '0';
618 ++p;
621 gcc_assert (*p == '.' && ISDIGIT (p[1]));
622 ++p;
624 /* Extract minor version. */
625 while (ISDIGIT (*p))
627 minor = minor * 10 + *p - '0';
628 ++p;
631 if (flag_indirect_dispatch)
633 abi_version = GCJ_CURRENT_BC_ABI_VERSION;
634 abi_version |= FLAG_BINARYCOMPAT_ABI;
636 else /* C++ ABI */
638 /* Implicit in this computation is the idea that we won't break the
639 old-style binary ABI in a sub-minor release (e.g., from 4.0.0 to
640 4.0.1). */
641 abi_version = 100000 * major + 1000 * minor;
643 if (flag_bootstrap_classes)
644 abi_version |= FLAG_BOOTSTRAP_LOADER;
646 gcj_abi_version = build_int_cstu (ptr_type_node, abi_version);
649 void
650 java_init_decl_processing (void)
652 tree endlink;
653 tree field = NULL_TREE;
654 tree t;
656 init_class_processing ();
658 current_function_decl = NULL;
659 current_binding_level = NULL_BINDING_LEVEL;
660 free_binding_level = NULL_BINDING_LEVEL;
661 pushlevel (0); /* make the binding_level structure for global names */
662 global_binding_level = current_binding_level;
664 /* The code here must be similar to build_common_tree_nodes{,_2} in
665 tree.c, especially as to the order of initializing common nodes. */
666 error_mark_node = make_node (ERROR_MARK);
667 TREE_TYPE (error_mark_node) = error_mark_node;
669 /* Create sizetype first - needed for other types. */
670 initialize_sizetypes (false);
672 byte_type_node = make_signed_type (8);
673 pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
674 short_type_node = make_signed_type (16);
675 pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
676 int_type_node = make_signed_type (32);
677 pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
678 long_type_node = make_signed_type (64);
679 pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));
681 unsigned_byte_type_node = make_unsigned_type (8);
682 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
683 unsigned_byte_type_node));
684 unsigned_short_type_node = make_unsigned_type (16);
685 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
686 unsigned_short_type_node));
687 unsigned_int_type_node = make_unsigned_type (32);
688 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
689 unsigned_int_type_node));
690 unsigned_long_type_node = make_unsigned_type (64);
691 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
692 unsigned_long_type_node));
694 /* This is not a java type, however tree-dfa requires a definition for
695 size_type_node. */
696 size_type_node = make_unsigned_type (POINTER_SIZE);
697 set_sizetype (size_type_node);
699 /* Define these next since types below may used them. */
700 integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
701 integer_zero_node = build_int_cst (NULL_TREE, 0);
702 integer_one_node = build_int_cst (NULL_TREE, 1);
703 integer_two_node = build_int_cst (NULL_TREE, 2);
704 integer_four_node = build_int_cst (NULL_TREE, 4);
705 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
707 /* A few values used for range checking in the lexer. */
708 decimal_int_max = build_int_cstu (unsigned_int_type_node, 0x80000000);
709 #if HOST_BITS_PER_WIDE_INT == 64
710 decimal_long_max = build_int_cstu (unsigned_long_type_node,
711 0x8000000000000000LL);
712 #elif HOST_BITS_PER_WIDE_INT == 32
713 decimal_long_max = build_int_cst_wide (unsigned_long_type_node,
714 0, 0x80000000);
715 #else
716 #error "unsupported size"
717 #endif
719 size_zero_node = size_int (0);
720 size_one_node = size_int (1);
721 bitsize_zero_node = bitsize_int (0);
722 bitsize_one_node = bitsize_int (1);
723 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
725 long_zero_node = build_int_cst (long_type_node, 0);
727 void_type_node = make_node (VOID_TYPE);
728 pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
729 layout_type (void_type_node); /* Uses size_zero_node */
731 ptr_type_node = build_pointer_type (void_type_node);
732 const_ptr_type_node
733 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
735 t = make_node (VOID_TYPE);
736 layout_type (t); /* Uses size_zero_node */
737 return_address_type_node = build_pointer_type (t);
739 null_pointer_node = build_int_cst (ptr_type_node, 0);
741 #if 0
742 /* Make a type to be the domain of a few array types
743 whose domains don't really matter.
744 200 is small enough that it always fits in size_t
745 and large enough that it can hold most function names for the
746 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
747 short_array_type_node = build_prim_array_type (short_type_node, 200);
748 #endif
749 char_type_node = make_node (INTEGER_TYPE);
750 TYPE_STRING_FLAG (char_type_node) = 1;
751 TYPE_PRECISION (char_type_node) = 16;
752 fixup_unsigned_type (char_type_node);
753 pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));
755 boolean_type_node = make_node (BOOLEAN_TYPE);
756 TYPE_PRECISION (boolean_type_node) = 1;
757 fixup_unsigned_type (boolean_type_node);
758 pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
759 boolean_type_node));
760 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
761 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
763 promoted_byte_type_node
764 = push_promoted_type ("promoted_byte", byte_type_node);
765 promoted_short_type_node
766 = push_promoted_type ("promoted_short", short_type_node);
767 promoted_char_type_node
768 = push_promoted_type ("promoted_char", char_type_node);
769 promoted_boolean_type_node
770 = push_promoted_type ("promoted_boolean", boolean_type_node);
772 float_type_node = make_node (REAL_TYPE);
773 TYPE_PRECISION (float_type_node) = 32;
774 pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
775 float_type_node));
776 layout_type (float_type_node);
778 double_type_node = make_node (REAL_TYPE);
779 TYPE_PRECISION (double_type_node) = 64;
780 pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
781 double_type_node));
782 layout_type (double_type_node);
784 float_zero_node = build_real (float_type_node, dconst0);
785 double_zero_node = build_real (double_type_node, dconst0);
787 /* These are the vtables for arrays of primitives. */
788 boolean_array_vtable = create_primitive_vtable ("boolean");
789 byte_array_vtable = create_primitive_vtable ("byte");
790 char_array_vtable = create_primitive_vtable ("char");
791 short_array_vtable = create_primitive_vtable ("short");
792 int_array_vtable = create_primitive_vtable ("int");
793 long_array_vtable = create_primitive_vtable ("long");
794 float_array_vtable = create_primitive_vtable ("float");
795 double_array_vtable = create_primitive_vtable ("double");
797 one_elt_array_domain_type = build_index_type (integer_one_node);
798 utf8const_type = make_node (RECORD_TYPE);
799 PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
800 PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
801 FINISH_RECORD (utf8const_type);
802 utf8const_ptr_type = build_pointer_type (utf8const_type);
804 atable_type = build_array_type (ptr_type_node,
805 one_elt_array_domain_type);
806 TYPE_NONALIASED_COMPONENT (atable_type) = 1;
807 atable_ptr_type = build_pointer_type (atable_type);
809 itable_type = build_array_type (ptr_type_node,
810 one_elt_array_domain_type);
811 TYPE_NONALIASED_COMPONENT (itable_type) = 1;
812 itable_ptr_type = build_pointer_type (itable_type);
814 symbol_type = make_node (RECORD_TYPE);
815 PUSH_FIELD (symbol_type, field, "clname", utf8const_ptr_type);
816 PUSH_FIELD (symbol_type, field, "name", utf8const_ptr_type);
817 PUSH_FIELD (symbol_type, field, "signature", utf8const_ptr_type);
818 FINISH_RECORD (symbol_type);
820 symbols_array_type = build_array_type (symbol_type,
821 one_elt_array_domain_type);
822 symbols_array_ptr_type = build_pointer_type (symbols_array_type);
824 assertion_entry_type = make_node (RECORD_TYPE);
825 PUSH_FIELD (assertion_entry_type, field, "assertion_code", integer_type_node);
826 PUSH_FIELD (assertion_entry_type, field, "op1", utf8const_ptr_type);
827 PUSH_FIELD (assertion_entry_type, field, "op2", utf8const_ptr_type);
828 FINISH_RECORD (assertion_entry_type);
830 assertion_table_type = build_array_type (assertion_entry_type,
831 one_elt_array_domain_type);
833 /* As you're adding items here, please update the code right after
834 this section, so that the filename containing the source code of
835 the pre-defined class gets registered correctly. */
836 unqualified_object_id_node = get_identifier ("Object");
837 object_type_node = lookup_class (get_identifier ("java.lang.Object"));
838 object_ptr_type_node = promote_type (object_type_node);
839 string_type_node = lookup_class (get_identifier ("java.lang.String"));
840 string_ptr_type_node = promote_type (string_type_node);
841 class_type_node = lookup_class (get_identifier ("java.lang.Class"));
842 throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
843 exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
844 runtime_exception_type_node =
845 lookup_class (get_identifier ("java.lang.RuntimeException"));
846 error_exception_type_node =
847 lookup_class (get_identifier ("java.lang.Error"));
849 rawdata_ptr_type_node
850 = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
852 add_predefined_file (get_identifier ("java/lang/Class.java"));
853 add_predefined_file (get_identifier ("java/lang/Error.java"));
854 add_predefined_file (get_identifier ("java/lang/Object.java"));
855 add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
856 add_predefined_file (get_identifier ("java/lang/String.java"));
857 add_predefined_file (get_identifier ("java/lang/Throwable.java"));
858 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
859 add_predefined_file (get_identifier ("java/lang/Exception.java"));
860 add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
861 add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
863 methodtable_type = make_node (RECORD_TYPE);
864 layout_type (methodtable_type);
865 build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
866 methodtable_ptr_type = build_pointer_type (methodtable_type);
868 TYPE_identifier_node = get_identifier ("TYPE");
869 init_identifier_node = get_identifier ("<init>");
870 clinit_identifier_node = get_identifier ("<clinit>");
871 finit_identifier_node = get_identifier ("finit$");
872 instinit_identifier_node = get_identifier ("instinit$");
873 void_signature_node = get_identifier ("()V");
874 length_identifier_node = get_identifier ("length");
875 finalize_identifier_node = get_identifier ("finalize");
876 this_identifier_node = get_identifier ("this");
877 super_identifier_node = get_identifier ("super");
878 continue_identifier_node = get_identifier ("continue");
879 access0_identifier_node = get_identifier ("access$0");
880 classdollar_identifier_node = get_identifier ("class$");
882 java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
883 java_io_serializable_identifier_node =
884 get_identifier ("java.io.Serializable");
886 /* for lack of a better place to put this stub call */
887 init_expr_processing();
889 constants_type_node = make_node (RECORD_TYPE);
890 PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
891 PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
892 PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
893 constants_data_field_decl_node = field;
894 FINISH_RECORD (constants_type_node);
895 build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
897 access_flags_type_node = unsigned_short_type_node;
899 dtable_type = make_node (RECORD_TYPE);
900 dtable_ptr_type = build_pointer_type (dtable_type);
902 otable_type = build_array_type (integer_type_node,
903 one_elt_array_domain_type);
904 TYPE_NONALIASED_COMPONENT (otable_type) = 1;
905 otable_ptr_type = build_pointer_type (otable_type);
907 PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
908 DECL_FCONTEXT (field) = object_type_node;
909 TYPE_VFIELD (object_type_node) = field;
911 /* This isn't exactly true, but it is what we have in the source.
912 There is an unresolved issue here, which is whether the vtable
913 should be marked by the GC. */
914 if (! flag_hash_synchronization)
915 PUSH_FIELD (object_type_node, field, "sync_info",
916 build_pointer_type (object_type_node));
917 for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
918 FIELD_PRIVATE (t) = 1;
919 FINISH_RECORD (object_type_node);
921 field_type_node = make_node (RECORD_TYPE);
922 field_ptr_type_node = build_pointer_type (field_type_node);
923 method_type_node = make_node (RECORD_TYPE);
924 method_ptr_type_node = build_pointer_type (method_type_node);
926 set_super_info (0, class_type_node, object_type_node, 0);
927 set_super_info (0, string_type_node, object_type_node, 0);
928 class_ptr_type = build_pointer_type (class_type_node);
930 PUSH_FIELD (class_type_node, field, "next_or_version", class_ptr_type);
931 PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
932 PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
933 PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
934 PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
935 constants_field_decl_node = field;
936 PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
937 PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
938 PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
939 PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
940 PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
941 PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
942 PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
943 PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
944 PUSH_FIELD (class_type_node, field, "otable", otable_ptr_type);
945 PUSH_FIELD (class_type_node, field, "otable_syms",
946 symbols_array_ptr_type);
947 PUSH_FIELD (class_type_node, field, "atable", atable_ptr_type);
948 PUSH_FIELD (class_type_node, field, "atable_syms",
949 symbols_array_ptr_type);
950 PUSH_FIELD (class_type_node, field, "itable", itable_ptr_type);
951 PUSH_FIELD (class_type_node, field, "itable_syms",
952 symbols_array_ptr_type);
953 PUSH_FIELD (class_type_node, field, "catch_classes", ptr_type_node);
954 PUSH_FIELD (class_type_node, field, "interfaces",
955 build_pointer_type (class_ptr_type));
956 PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
957 PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
958 PUSH_FIELD (class_type_node, field, "state", byte_type_node);
959 PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
960 PUSH_FIELD (class_type_node, field, "depth", short_type_node);
961 PUSH_FIELD (class_type_node, field, "ancestors", ptr_type_node);
962 PUSH_FIELD (class_type_node, field, "idt", ptr_type_node);
963 PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);
964 PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
965 PUSH_FIELD (class_type_node, field, "assertion_table", ptr_type_node);
966 PUSH_FIELD (class_type_node, field, "hack_signers", ptr_type_node);
967 PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
968 PUSH_FIELD (class_type_node, field, "aux_info", ptr_type_node);
969 PUSH_FIELD (class_type_node, field, "engine", ptr_type_node);
970 PUSH_FIELD (class_type_node, field, "reflection_data", ptr_type_node);
971 for (t = TYPE_FIELDS (class_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
972 FIELD_PRIVATE (t) = 1;
973 push_super_field (class_type_node, object_type_node);
975 FINISH_RECORD (class_type_node);
976 build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
978 field_info_union_node = make_node (UNION_TYPE);
979 PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
980 PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
981 #if 0
982 PUSH_FIELD (field_info_union_node, field, "idx", unsigned_short_type_node);
983 #endif
984 layout_type (field_info_union_node);
986 PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
987 PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
988 PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
989 PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
990 PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
991 FINISH_RECORD (field_type_node);
992 build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
994 nativecode_ptr_array_type_node
995 = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
997 PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
998 PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
999 FINISH_RECORD (dtable_type);
1000 build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
1002 jexception_type = make_node (RECORD_TYPE);
1003 PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
1004 PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
1005 PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
1006 PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
1007 FINISH_RECORD (jexception_type);
1008 build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
1009 jexception_ptr_type = build_pointer_type (jexception_type);
1011 lineNumberEntry_type = make_node (RECORD_TYPE);
1012 PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
1013 PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
1014 FINISH_RECORD (lineNumberEntry_type);
1016 lineNumbers_type = make_node (RECORD_TYPE);
1017 PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
1018 FINISH_RECORD (lineNumbers_type);
1020 PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
1021 PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
1022 PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
1023 PUSH_FIELD (method_type_node, field, "index", unsigned_short_type_node);
1024 PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
1025 PUSH_FIELD (method_type_node, field, "throws", ptr_type_node);
1026 FINISH_RECORD (method_type_node);
1027 build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
1029 endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
1031 t = tree_cons (NULL_TREE, class_ptr_type, endlink);
1032 alloc_object_node = builtin_function ("_Jv_AllocObject",
1033 build_function_type (ptr_type_node, t),
1034 0, NOT_BUILT_IN, NULL, NULL_TREE);
1035 DECL_IS_MALLOC (alloc_object_node) = 1;
1036 alloc_no_finalizer_node =
1037 builtin_function ("_Jv_AllocObjectNoFinalizer",
1038 build_function_type (ptr_type_node, t),
1039 0, NOT_BUILT_IN, NULL, NULL_TREE);
1040 DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
1042 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1043 soft_initclass_node = builtin_function ("_Jv_InitClass",
1044 build_function_type (void_type_node,
1046 0, NOT_BUILT_IN, NULL, NULL_TREE);
1047 t = tree_cons (NULL_TREE, class_ptr_type,
1048 tree_cons (NULL_TREE, int_type_node, endlink));
1049 soft_resolvepoolentry_node
1050 = builtin_function ("_Jv_ResolvePoolEntry",
1051 build_function_type (ptr_type_node, t),
1052 0,NOT_BUILT_IN, NULL, NULL_TREE);
1053 DECL_IS_PURE (soft_resolvepoolentry_node) = 1;
1054 throw_node = builtin_function ("_Jv_Throw",
1055 build_function_type (void_type_node, t),
1056 0, NOT_BUILT_IN, NULL, NULL_TREE);
1057 /* Mark throw_nodes as `noreturn' functions with side effects. */
1058 TREE_THIS_VOLATILE (throw_node) = 1;
1059 TREE_SIDE_EFFECTS (throw_node) = 1;
1061 t = build_function_type (void_type_node, tree_cons (NULL_TREE, ptr_type_node,
1062 endlink));
1063 soft_monitorenter_node
1064 = builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
1065 NULL, NULL_TREE);
1066 soft_monitorexit_node
1067 = builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
1068 NULL, NULL_TREE);
1070 t = tree_cons (NULL_TREE, ptr_type_node,
1071 tree_cons (NULL_TREE, int_type_node, endlink));
1072 soft_newarray_node
1073 = builtin_function ("_Jv_NewPrimArray",
1074 build_function_type (ptr_type_node, t),
1075 0, NOT_BUILT_IN, NULL, NULL_TREE);
1076 DECL_IS_MALLOC (soft_newarray_node) = 1;
1078 t = tree_cons (NULL_TREE, int_type_node,
1079 tree_cons (NULL_TREE, class_ptr_type,
1080 tree_cons (NULL_TREE, object_ptr_type_node,
1081 endlink)));
1082 soft_anewarray_node
1083 = builtin_function ("_Jv_NewObjectArray",
1084 build_function_type (ptr_type_node, t),
1085 0, NOT_BUILT_IN, NULL, NULL_TREE);
1086 DECL_IS_MALLOC (soft_anewarray_node) = 1;
1088 /* There is no endlink here because _Jv_NewMultiArray is a varargs
1089 function. */
1090 t = tree_cons (NULL_TREE, ptr_type_node,
1091 tree_cons (NULL_TREE, int_type_node, NULL_TREE));
1092 soft_multianewarray_node
1093 = builtin_function ("_Jv_NewMultiArray",
1094 build_function_type (ptr_type_node, t),
1095 0, NOT_BUILT_IN, NULL, NULL_TREE);
1096 DECL_IS_MALLOC (soft_multianewarray_node) = 1;
1098 t = build_function_type (void_type_node,
1099 tree_cons (NULL_TREE, int_type_node, endlink));
1100 soft_badarrayindex_node
1101 = builtin_function ("_Jv_ThrowBadArrayIndex", t,
1102 0, NOT_BUILT_IN, NULL, NULL_TREE);
1103 /* Mark soft_badarrayindex_node as a `noreturn' function with side
1104 effects. */
1105 TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
1106 TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
1108 soft_nullpointer_node
1109 = builtin_function ("_Jv_ThrowNullPointerException",
1110 build_function_type (void_type_node, endlink),
1111 0, NOT_BUILT_IN, NULL, NULL_TREE);
1112 /* Mark soft_nullpointer_node as a `noreturn' function with side
1113 effects. */
1114 TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
1115 TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
1117 soft_abstractmethod_node
1118 = builtin_function ("_Jv_ThrowAbstractMethodError",
1119 build_function_type (void_type_node, endlink),
1120 0, NOT_BUILT_IN, NULL, NULL_TREE);
1121 /* Mark soft_abstractmethod_node as a `noreturn' function with side
1122 effects. */
1123 TREE_THIS_VOLATILE (soft_abstractmethod_node) = 1;
1124 TREE_SIDE_EFFECTS (soft_abstractmethod_node) = 1;
1126 soft_nosuchfield_node
1127 = builtin_function ("_Jv_ThrowNoSuchFieldError",
1128 build_function_type (void_type_node, endlink),
1129 0, NOT_BUILT_IN, NULL, NULL_TREE);
1130 /* Mark soft_nosuchfield_node as a `noreturn' function with side
1131 effects. */
1132 TREE_THIS_VOLATILE (soft_nosuchfield_node) = 1;
1133 TREE_SIDE_EFFECTS (soft_nosuchfield_node) = 1;
1135 t = tree_cons (NULL_TREE, class_ptr_type,
1136 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1137 soft_checkcast_node
1138 = builtin_function ("_Jv_CheckCast",
1139 build_function_type (ptr_type_node, t),
1140 0, NOT_BUILT_IN, NULL, NULL_TREE);
1141 t = tree_cons (NULL_TREE, object_ptr_type_node,
1142 tree_cons (NULL_TREE, class_ptr_type, endlink));
1143 soft_instanceof_node
1144 = builtin_function ("_Jv_IsInstanceOf",
1145 build_function_type (boolean_type_node, t),
1146 0, NOT_BUILT_IN, NULL, NULL_TREE);
1147 DECL_IS_PURE (soft_instanceof_node) = 1;
1148 t = tree_cons (NULL_TREE, object_ptr_type_node,
1149 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1150 soft_checkarraystore_node
1151 = builtin_function ("_Jv_CheckArrayStore",
1152 build_function_type (void_type_node, t),
1153 0, NOT_BUILT_IN, NULL, NULL_TREE);
1154 t = tree_cons (NULL_TREE, ptr_type_node,
1155 tree_cons (NULL_TREE, ptr_type_node,
1156 tree_cons (NULL_TREE, int_type_node, endlink)));
1157 soft_lookupinterfacemethod_node
1158 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
1159 build_function_type (ptr_type_node, t),
1160 0, NOT_BUILT_IN, NULL, NULL_TREE);
1161 DECL_IS_PURE (soft_lookupinterfacemethod_node) = 1;
1162 t = tree_cons (NULL_TREE, ptr_type_node,
1163 tree_cons (NULL_TREE, ptr_type_node,
1164 tree_cons (NULL_TREE, ptr_type_node, endlink)));
1165 soft_lookupinterfacemethodbyname_node
1166 = builtin_function ("_Jv_LookupInterfaceMethod",
1167 build_function_type (ptr_type_node, t),
1168 0, NOT_BUILT_IN, NULL, NULL_TREE);
1169 t = tree_cons (NULL_TREE, object_ptr_type_node,
1170 tree_cons (NULL_TREE, ptr_type_node,
1171 tree_cons (NULL_TREE, ptr_type_node,
1172 tree_cons (NULL_TREE, int_type_node,
1173 endlink))));
1174 soft_lookupjnimethod_node
1175 = builtin_function ("_Jv_LookupJNIMethod",
1176 build_function_type (ptr_type_node, t),
1177 0, NOT_BUILT_IN, NULL, NULL_TREE);
1178 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1179 soft_getjnienvnewframe_node
1180 = builtin_function ("_Jv_GetJNIEnvNewFrame",
1181 build_function_type (ptr_type_node, t),
1182 0, NOT_BUILT_IN, NULL, NULL_TREE);
1183 soft_jnipopsystemframe_node
1184 = builtin_function ("_Jv_JNI_PopSystemFrame",
1185 build_function_type (void_type_node, t),
1186 0, NOT_BUILT_IN, NULL, NULL_TREE);
1188 t = tree_cons (NULL_TREE, object_ptr_type_node, endlink);
1189 soft_unwrapjni_node
1190 = builtin_function ("_Jv_UnwrapJNIweakReference",
1191 build_function_type (object_ptr_type_node, t),
1192 0, NOT_BUILT_IN, NULL, NULL_TREE);
1194 t = tree_cons (NULL_TREE, int_type_node,
1195 tree_cons (NULL_TREE, int_type_node, endlink));
1196 soft_idiv_node
1197 = builtin_function ("_Jv_divI",
1198 build_function_type (int_type_node, t),
1199 0, NOT_BUILT_IN, NULL, NULL_TREE);
1201 soft_irem_node
1202 = builtin_function ("_Jv_remI",
1203 build_function_type (int_type_node, t),
1204 0, NOT_BUILT_IN, NULL, NULL_TREE);
1206 t = tree_cons (NULL_TREE, long_type_node,
1207 tree_cons (NULL_TREE, long_type_node, endlink));
1208 soft_ldiv_node
1209 = builtin_function ("_Jv_divJ",
1210 build_function_type (long_type_node, t),
1211 0, NOT_BUILT_IN, NULL, NULL_TREE);
1213 soft_lrem_node
1214 = builtin_function ("_Jv_remJ",
1215 build_function_type (long_type_node, t),
1216 0, NOT_BUILT_IN, NULL, NULL_TREE);
1218 /* Initialize variables for except.c. */
1219 eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
1220 ? "__gcj_personality_sj0"
1221 : "__gcj_personality_v0");
1222 default_init_unwind_resume_libfunc ();
1224 lang_eh_runtime_type = do_nothing;
1226 init_jcf_parse ();
1228 initialize_builtins ();
1229 soft_fmod_node = built_in_decls[BUILT_IN_FMOD];
1230 #if 0
1231 soft_fmodf_node = built_in_decls[BUILT_IN_FMODF];
1232 #endif
1234 parse_version ();
1238 /* Look up NAME in the current binding level and its superiors
1239 in the namespace of variables, functions and typedefs.
1240 Return a ..._DECL node of some kind representing its definition,
1241 or return 0 if it is undefined. */
1243 tree
1244 lookup_name (tree name)
1246 tree val;
1247 if (current_binding_level != global_binding_level
1248 && IDENTIFIER_LOCAL_VALUE (name))
1249 val = IDENTIFIER_LOCAL_VALUE (name);
1250 else
1251 val = IDENTIFIER_GLOBAL_VALUE (name);
1252 return val;
1255 /* Similar to `lookup_name' but look only at current binding level and
1256 the previous one if it's the parameter level. */
1258 static tree
1259 lookup_name_current_level (tree name)
1261 tree t;
1263 if (current_binding_level == global_binding_level)
1264 return IDENTIFIER_GLOBAL_VALUE (name);
1266 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
1267 return 0;
1269 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
1270 if (DECL_NAME (t) == name)
1271 break;
1273 return t;
1276 /* Use a binding level to record a labeled block declaration */
1278 void
1279 push_labeled_block (tree lb)
1281 tree name = DECL_NAME (LABELED_BLOCK_LABEL (lb));
1282 struct binding_level *b = current_binding_level;
1283 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1284 if (oldlocal != 0)
1285 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1286 TREE_CHAIN (lb) = b->names;
1287 b->names = lb;
1288 IDENTIFIER_LOCAL_VALUE (name) = lb;
1291 /* Pop the current binding level, reinstalling values for the previous
1292 labeled block */
1294 void
1295 pop_labeled_block (void)
1297 struct binding_level *b = current_binding_level;
1298 tree label = b->names;
1299 IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label))) =
1300 NULL_TREE;
1301 if (b->shadowed)
1302 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b->shadowed)) =
1303 TREE_VALUE (b->shadowed);
1305 /* Pop the current level, and free the structure for reuse. */
1306 current_binding_level = current_binding_level->level_chain;
1307 b->level_chain = free_binding_level;
1308 free_binding_level = b;
1311 /* Record a decl-node X as belonging to the current lexical scope.
1312 Check for errors (such as an incompatible declaration for the same
1313 name already seen in the same scope).
1315 Returns either X or an old decl for the same name.
1316 If an old decl is returned, it may have been smashed
1317 to agree with what X says. */
1319 tree
1320 pushdecl (tree x)
1322 tree t;
1323 tree name = DECL_NAME (x);
1324 struct binding_level *b = current_binding_level;
1326 if (TREE_CODE (x) != TYPE_DECL)
1327 DECL_CONTEXT (x) = current_function_decl;
1328 if (name)
1330 t = lookup_name_current_level (name);
1331 if (t != 0 && t == error_mark_node)
1332 /* error_mark_node is 0 for a while during initialization! */
1334 t = 0;
1335 error ("%q+D used prior to declaration", x);
1338 /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1339 to point to the TYPE_DECL.
1340 Since Java does not have typedefs, a type can only have
1341 one (true) name, given by a class, interface, or builtin. */
1342 if (TREE_CODE (x) == TYPE_DECL
1343 && TYPE_NAME (TREE_TYPE (x)) == 0
1344 && TREE_TYPE (x) != error_mark_node)
1346 TYPE_NAME (TREE_TYPE (x)) = x;
1347 TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1350 /* This name is new in its binding level.
1351 Install the new declaration and return it. */
1352 if (b == global_binding_level)
1354 /* Install a global value. */
1356 IDENTIFIER_GLOBAL_VALUE (name) = x;
1358 else
1360 /* Here to install a non-global value. */
1361 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1362 IDENTIFIER_LOCAL_VALUE (name) = x;
1364 #if 0
1365 /* Warn if shadowing an argument at the top level of the body. */
1366 if (oldlocal != 0 && !DECL_EXTERNAL (x)
1367 /* This warning doesn't apply to the parms of a nested fcn. */
1368 && ! current_binding_level->parm_flag
1369 /* Check that this is one level down from the parms. */
1370 && current_binding_level->level_chain->parm_flag
1371 /* Check that the decl being shadowed
1372 comes from the parm level, one level up. */
1373 && chain_member (oldlocal, current_binding_level->level_chain->names))
1375 if (TREE_CODE (oldlocal) == PARM_DECL)
1376 pedwarn ("declaration of %qs shadows a parameter",
1377 IDENTIFIER_POINTER (name));
1378 else
1379 pedwarn ("declaration of %qs shadows a symbol from the parameter list",
1380 IDENTIFIER_POINTER (name));
1383 /* Maybe warn if shadowing something else. */
1384 else if (warn_shadow && !DECL_EXTERNAL (x)
1385 /* No shadow warnings for internally generated vars. */
1386 && DECL_SOURCE_LINE (x) != 0
1387 /* No shadow warnings for vars made for inlining. */
1388 && ! DECL_FROM_INLINE (x))
1390 const char *warnstring = 0;
1392 if (TREE_CODE (x) == PARM_DECL
1393 && current_binding_level->level_chain->parm_flag)
1394 /* Don't warn about the parm names in function declarator
1395 within a function declarator.
1396 It would be nice to avoid warning in any function
1397 declarator in a declaration, as opposed to a definition,
1398 but there is no way to tell it's not a definition. */
1400 else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
1401 warnstring = "declaration of %qs shadows a parameter";
1402 else if (oldlocal != 0)
1403 warnstring = "declaration of %qs shadows previous local";
1404 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1405 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1406 warnstring = "declaration of %qs shadows global declaration";
1408 if (warnstring)
1409 warning (0, warnstring, IDENTIFIER_POINTER (name));
1411 #endif
1413 /* If storing a local value, there may already be one (inherited).
1414 If so, record it for restoration when this binding level ends. */
1415 if (oldlocal != 0)
1416 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1420 /* Put decls on list in reverse order.
1421 We will reverse them later if necessary. */
1422 TREE_CHAIN (x) = b->names;
1423 b->names = x;
1425 return x;
1428 void
1429 pushdecl_force_head (tree x)
1431 current_binding_level->names = x;
1434 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1436 tree
1437 pushdecl_top_level (tree x)
1439 tree t;
1440 struct binding_level *b = current_binding_level;
1442 current_binding_level = global_binding_level;
1443 t = pushdecl (x);
1444 current_binding_level = b;
1445 return t;
1448 /* Like pushdecl, only it places X in FUNCTION_BINDING_LEVEL, if appropriate. */
1450 tree
1451 pushdecl_function_level (tree x)
1453 tree t;
1454 struct binding_level *b = current_binding_level;
1456 current_binding_level = function_binding_level;
1457 t = pushdecl (x);
1458 current_binding_level = b;
1459 return t;
1462 /* Nonzero if we are currently in the global binding level. */
1465 global_bindings_p (void)
1467 return current_binding_level == global_binding_level;
1470 /* Return the list of declarations of the current level.
1471 Note that this list is in reverse order unless/until
1472 you nreverse it; and when you do nreverse it, you must
1473 store the result back using `storedecls' or you will lose. */
1475 tree
1476 getdecls (void)
1478 return current_binding_level->names;
1481 /* Create a new `struct binding_level'. */
1483 static struct binding_level *
1484 make_binding_level (void)
1486 /* NOSTRICT */
1487 return ggc_alloc_cleared (sizeof (struct binding_level));
1490 void
1491 pushlevel (int unused ATTRIBUTE_UNUSED)
1493 struct binding_level *newlevel = NULL_BINDING_LEVEL;
1495 #if 0
1496 /* If this is the top level of a function,
1497 just make sure that NAMED_LABELS is 0. */
1499 if (current_binding_level == global_binding_level)
1500 named_labels = 0;
1501 #endif
1503 /* Reuse or create a struct for this binding level. */
1505 if (free_binding_level)
1507 newlevel = free_binding_level;
1508 free_binding_level = free_binding_level->level_chain;
1510 else
1512 newlevel = make_binding_level ();
1515 /* Add this level to the front of the chain (stack) of levels that
1516 are active. */
1518 *newlevel = clear_binding_level;
1519 newlevel->level_chain = current_binding_level;
1520 current_binding_level = newlevel;
1521 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1522 newlevel->binding_depth = binding_depth;
1523 indent ();
1524 fprintf (stderr, "push %s level %p pc %d\n",
1525 (is_class_level) ? "class" : "block", newlevel, current_pc);
1526 is_class_level = 0;
1527 binding_depth++;
1528 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1531 /* Exit a binding level.
1532 Pop the level off, and restore the state of the identifier-decl mappings
1533 that were in effect when this level was entered.
1535 If KEEP is nonzero, this level had explicit declarations, so
1536 and create a "block" (a BLOCK node) for the level
1537 to record its declarations and subblocks for symbol table output.
1539 If FUNCTIONBODY is nonzero, this level is the body of a function,
1540 so create a block as if KEEP were set and also clear out all
1541 label names.
1543 If REVERSE is nonzero, reverse the order of decls before putting
1544 them into the BLOCK. */
1546 tree
1547 poplevel (int keep, int reverse, int functionbody)
1549 tree link;
1550 /* The chain of decls was accumulated in reverse order.
1551 Put it into forward order, just for cleanliness. */
1552 tree decls;
1553 tree subblocks = current_binding_level->blocks;
1554 tree block = 0;
1555 tree decl;
1556 tree bind = 0;
1558 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1559 binding_depth--;
1560 indent ();
1561 if (current_binding_level->end_pc != LARGEST_PC)
1562 fprintf (stderr, "pop %s level %p pc %d (end pc %d)\n",
1563 (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1564 current_binding_level->end_pc);
1565 else
1566 fprintf (stderr, "pop %s level %p pc %d\n",
1567 (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1568 #if 0
1569 if (is_class_level != (current_binding_level == class_binding_level))
1571 indent ();
1572 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
1574 is_class_level = 0;
1575 #endif
1576 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1578 /* Get the decls in the order they were written.
1579 Usually current_binding_level->names is in reverse order.
1580 But parameter decls were previously put in forward order. */
1582 if (reverse)
1583 current_binding_level->names
1584 = decls = nreverse (current_binding_level->names);
1585 else
1586 decls = current_binding_level->names;
1588 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1589 if (TREE_CODE (decl) == VAR_DECL
1590 && DECL_LANG_SPECIFIC (decl) != NULL
1591 && DECL_LOCAL_SLOT_NUMBER (decl))
1592 LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
1594 /* If there were any declarations in that level,
1595 or if this level is a function body,
1596 create a BLOCK to record them for the life of this function. */
1598 block = 0;
1599 if (keep || functionbody)
1601 block = make_node (BLOCK);
1602 TREE_TYPE (block) = void_type_node;
1605 if (current_binding_level->exception_range)
1606 expand_end_java_handler (current_binding_level->exception_range);
1608 if (block != 0)
1610 /* If any statements have been generated at this level, create a
1611 BIND_EXPR to hold them and copy the variables to it. This
1612 only applies to the bytecode compiler. */
1613 if (current_binding_level->stmts)
1615 tree decl = decls;
1616 tree *var = &BLOCK_VARS (block);
1618 /* Copy decls from names list, ignoring labels. */
1619 while (decl)
1621 tree next = TREE_CHAIN (decl);
1622 if (TREE_CODE (decl) != LABEL_DECL)
1624 *var = decl;
1625 var = &TREE_CHAIN (decl);
1627 decl = next;
1629 *var = NULL;
1631 bind = build3 (BIND_EXPR, TREE_TYPE (block), BLOCK_VARS (block),
1632 BLOCK_EXPR_BODY (block), block);
1633 BIND_EXPR_BODY (bind) = current_binding_level->stmts;
1635 if (BIND_EXPR_BODY (bind)
1636 && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
1637 TREE_SIDE_EFFECTS (bind) = 1;
1639 /* FIXME: gimplifier brain damage. */
1640 if (BIND_EXPR_BODY (bind) == NULL)
1641 BIND_EXPR_BODY (bind) = build_java_empty_stmt ();
1643 current_binding_level->stmts = NULL;
1645 else
1647 BLOCK_VARS (block) = decls;
1649 BLOCK_SUBBLOCKS (block) = subblocks;
1652 /* In each subblock, record that this is its superior. */
1654 for (link = subblocks; link; link = TREE_CHAIN (link))
1655 BLOCK_SUPERCONTEXT (link) = block;
1657 /* Clear out the meanings of the local variables of this level. */
1659 for (link = decls; link; link = TREE_CHAIN (link))
1661 tree name = DECL_NAME (link);
1662 if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1664 /* If the ident. was used or addressed via a local extern decl,
1665 don't forget that fact. */
1666 if (DECL_EXTERNAL (link))
1668 if (TREE_USED (link))
1669 TREE_USED (name) = 1;
1670 if (TREE_ADDRESSABLE (link))
1671 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1673 IDENTIFIER_LOCAL_VALUE (name) = 0;
1677 /* Restore all name-meanings of the outer levels
1678 that were shadowed by this level. */
1680 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1681 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1683 /* If the level being exited is the top level of a function,
1684 check over all the labels, and clear out the current
1685 (function local) meanings of their names. */
1687 if (functionbody)
1689 /* If this is the top level block of a function,
1690 the vars are the function's parameters.
1691 Don't leave them in the BLOCK because they are
1692 found in the FUNCTION_DECL instead. */
1694 BLOCK_VARS (block) = 0;
1696 /* Clear out the definitions of all label names,
1697 since their scopes end here,
1698 and add them to BLOCK_VARS. */
1700 #if 0
1701 for (link = named_labels; link; link = TREE_CHAIN (link))
1703 tree label = TREE_VALUE (link);
1705 if (DECL_INITIAL (label) == 0)
1707 error ("label %q+D used but not defined", label);
1708 /* Avoid crashing later. */
1709 define_label (input_location, DECL_NAME (label));
1711 else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
1712 warning (0, "label %q+D defined but not used", label);
1713 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1715 /* Put the labels into the "variables" of the
1716 top-level block, so debugger can see them. */
1717 TREE_CHAIN (label) = BLOCK_VARS (block);
1718 BLOCK_VARS (block) = label;
1720 #endif
1723 /* Pop the current level, and free the structure for reuse. */
1726 struct binding_level *level = current_binding_level;
1727 current_binding_level = current_binding_level->level_chain;
1729 level->level_chain = free_binding_level;
1730 free_binding_level = level;
1733 /* Dispose of the block that we just made inside some higher level. */
1734 if (functionbody)
1736 DECL_INITIAL (current_function_decl) = block;
1737 DECL_SAVED_TREE (current_function_decl) = bind;
1739 else
1741 if (block)
1743 current_binding_level->blocks
1744 = chainon (current_binding_level->blocks, block);
1746 /* If we did not make a block for the level just exited,
1747 any blocks made for inner levels
1748 (since they cannot be recorded as subblocks in that level)
1749 must be carried forward so they will later become subblocks
1750 of something else. */
1751 else if (subblocks)
1752 current_binding_level->blocks
1753 = chainon (current_binding_level->blocks, subblocks);
1755 if (bind)
1756 java_add_stmt (bind);
1759 if (block)
1760 TREE_USED (block) = 1;
1761 return block;
1764 void
1765 maybe_pushlevels (int pc)
1767 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1768 current_pc = pc;
1769 #endif
1771 while (pending_local_decls != NULL_TREE &&
1772 DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1774 tree *ptr = &pending_local_decls;
1775 tree decl = *ptr, next;
1776 int end_pc = DECL_LOCAL_END_PC (decl);
1778 while (*ptr != NULL_TREE
1779 && DECL_LOCAL_START_PC (*ptr) <= pc
1780 && DECL_LOCAL_END_PC (*ptr) == end_pc)
1781 ptr = &TREE_CHAIN (*ptr);
1782 pending_local_decls = *ptr;
1783 *ptr = NULL_TREE;
1785 /* Force non-nested range to be nested in current range by
1786 truncating variable lifetimes. */
1787 if (end_pc > current_binding_level->end_pc)
1789 tree t;
1790 end_pc = current_binding_level->end_pc;
1791 for (t = decl; t != NULL_TREE; t = TREE_CHAIN (t))
1792 DECL_LOCAL_END_PC (t) = end_pc;
1795 maybe_start_try (pc, end_pc);
1797 pushlevel (1);
1799 current_binding_level->end_pc = end_pc;
1800 current_binding_level->start_pc = pc;
1801 current_binding_level->names = NULL;
1802 for ( ; decl != NULL_TREE; decl = next)
1804 next = TREE_CHAIN (decl);
1805 push_jvm_slot (DECL_LOCAL_SLOT_NUMBER (decl), decl);
1806 pushdecl (decl);
1807 initialize_local_variable (decl, DECL_LOCAL_SLOT_NUMBER (decl));
1811 maybe_start_try (pc, 0);
1814 void
1815 maybe_poplevels (int pc)
1817 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1818 current_pc = pc;
1819 #endif
1821 /* FIXME: I'm pretty sure that this is wrong. Variable scopes are
1822 inclusive, so a variable is live if pc == end_pc. Here, we
1823 terminate a range if the current pc is equal to the end of the
1824 range, and this is *before* we have generated code for the
1825 instruction at end_pc. We're closing a binding level one
1826 instruction too early.*/
1827 while (current_binding_level->end_pc <= pc)
1828 poplevel (1, 0, 0);
1831 /* Terminate any binding which began during the range beginning at
1832 start_pc. This tidies up improperly nested local variable ranges
1833 and exception handlers; a variable declared within an exception
1834 range is forcibly terminated when that exception ends. */
1836 void
1837 force_poplevels (int start_pc)
1839 while (current_binding_level->start_pc > start_pc)
1841 if (pedantic && current_binding_level->start_pc > start_pc)
1842 warning (0, "In %+D: overlapped variable and exception ranges at %d",
1843 current_function_decl,
1844 current_binding_level->start_pc);
1845 poplevel (1, 0, 0);
1849 /* Insert BLOCK at the end of the list of subblocks of the
1850 current binding level. This is used when a BIND_EXPR is expanded,
1851 to handle the BLOCK node inside the BIND_EXPR. */
1853 void
1854 insert_block (tree block)
1856 TREE_USED (block) = 1;
1857 current_binding_level->blocks
1858 = chainon (current_binding_level->blocks, block);
1861 /* integrate_decl_tree calls this function. */
1863 void
1864 java_dup_lang_specific_decl (tree node)
1866 int lang_decl_size;
1867 struct lang_decl *x;
1869 if (!DECL_LANG_SPECIFIC (node))
1870 return;
1872 lang_decl_size = sizeof (struct lang_decl);
1873 x = ggc_alloc (lang_decl_size);
1874 memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1875 DECL_LANG_SPECIFIC (node) = x;
1878 void
1879 give_name_to_locals (JCF *jcf)
1881 int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1882 int code_offset = DECL_CODE_OFFSET (current_function_decl);
1883 tree parm;
1884 pending_local_decls = NULL_TREE;
1885 if (n == 0)
1886 return;
1887 JCF_SEEK (jcf, n);
1888 n = JCF_readu2 (jcf);
1889 for (i = 0; i < n; i++)
1891 int start_pc = JCF_readu2 (jcf);
1892 int length = JCF_readu2 (jcf);
1893 int name_index = JCF_readu2 (jcf);
1894 int signature_index = JCF_readu2 (jcf);
1895 int slot = JCF_readu2 (jcf);
1896 tree name = get_name_constant (jcf, name_index);
1897 tree type = parse_signature (jcf, signature_index);
1898 if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1899 && start_pc == 0
1900 && length == DECL_CODE_LENGTH (current_function_decl))
1902 tree decl = TREE_VEC_ELT (decl_map, slot);
1903 DECL_NAME (decl) = name;
1904 if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1905 warning (0, "bad type in parameter debug info");
1907 else
1909 tree *ptr;
1910 int end_pc = start_pc + length;
1911 tree decl = build_decl (VAR_DECL, name, type);
1912 if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1914 warning (0, "bad PC range for debug info for local %q+D",
1915 decl);
1916 end_pc = DECL_CODE_LENGTH (current_function_decl);
1919 /* Adjust start_pc if necessary so that the local's first
1920 store operation will use the relevant DECL as a
1921 destination. Fore more information, read the leading
1922 comments for expr.c:maybe_adjust_start_pc. */
1923 start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1925 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1926 DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1927 DECL_LOCAL_START_PC (decl) = start_pc;
1928 #if 0
1929 /* FIXME: The range used internally for exceptions and local
1930 variable ranges, is a half-open interval:
1931 start_pc <= pc < end_pc. However, the range used in the
1932 Java VM spec is inclusive at both ends:
1933 start_pc <= pc <= end_pc. */
1934 end_pc++;
1935 #endif
1936 DECL_LOCAL_END_PC (decl) = end_pc;
1938 /* Now insert the new decl in the proper place in
1939 pending_local_decls. We are essentially doing an insertion sort,
1940 which works fine, since the list input will normally already
1941 be sorted. */
1942 ptr = &pending_local_decls;
1943 while (*ptr != NULL_TREE
1944 && (DECL_LOCAL_START_PC (*ptr) > start_pc
1945 || (DECL_LOCAL_START_PC (*ptr) == start_pc
1946 && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1947 ptr = &TREE_CHAIN (*ptr);
1948 TREE_CHAIN (decl) = *ptr;
1949 *ptr = decl;
1953 pending_local_decls = nreverse (pending_local_decls);
1955 /* Fill in default names for the parameters. */
1956 for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1957 parm != NULL_TREE; parm = TREE_CHAIN (parm), i++)
1959 if (DECL_NAME (parm) == NULL_TREE)
1961 int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1962 if (arg_i == 0)
1963 DECL_NAME (parm) = get_identifier ("this");
1964 else
1966 char buffer[12];
1967 sprintf (buffer, "ARG_%d", arg_i);
1968 DECL_NAME (parm) = get_identifier (buffer);
1974 tree
1975 build_result_decl (tree fndecl)
1977 tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1978 tree result = DECL_RESULT (fndecl);
1979 if (! result)
1981 /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1982 if (INTEGRAL_TYPE_P (restype)
1983 && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
1984 restype = integer_type_node;
1985 result = build_decl (RESULT_DECL, NULL_TREE, restype);
1986 DECL_ARTIFICIAL (result) = 1;
1987 DECL_IGNORED_P (result) = 1;
1988 DECL_CONTEXT (result) = fndecl;
1989 DECL_RESULT (fndecl) = result;
1991 return result;
1994 void
1995 start_java_method (tree fndecl)
1997 tree tem, *ptr;
1998 int i;
2000 uniq = 0;
2002 current_function_decl = fndecl;
2003 announce_function (fndecl);
2005 i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
2006 decl_map = make_tree_vec (i);
2007 base_decl_map = make_tree_vec (i);
2008 type_map = xrealloc (type_map, i * sizeof (tree));
2010 #if defined(DEBUG_JAVA_BINDING_LEVELS)
2011 fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
2012 current_pc = 0;
2013 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
2014 pushlevel (1); /* Push parameters. */
2016 ptr = &DECL_ARGUMENTS (fndecl);
2017 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
2018 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
2020 tree parm_name = NULL_TREE, parm_decl;
2021 tree parm_type = TREE_VALUE (tem);
2022 gcc_assert (i < DECL_MAX_LOCALS (fndecl));
2024 parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
2025 DECL_CONTEXT (parm_decl) = fndecl;
2026 if (targetm.calls.promote_prototypes (parm_type)
2027 && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
2028 && INTEGRAL_TYPE_P (parm_type))
2029 parm_type = integer_type_node;
2030 DECL_ARG_TYPE (parm_decl) = parm_type;
2032 *ptr = parm_decl;
2033 ptr = &TREE_CHAIN (parm_decl);
2035 /* Add parm_decl to the decl_map. */
2036 push_jvm_slot (i, parm_decl);
2038 type_map[i] = TREE_TYPE (parm_decl);
2039 if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
2041 i++;
2042 type_map[i] = void_type_node;
2045 *ptr = NULL_TREE;
2046 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
2048 while (i < DECL_MAX_LOCALS(fndecl))
2049 type_map[i++] = NULL_TREE;
2051 build_result_decl (fndecl);
2053 /* Push local variables. */
2054 pushlevel (2);
2056 function_binding_level = current_binding_level;
2059 void
2060 end_java_method (void)
2062 tree fndecl = current_function_decl;
2064 /* pop out of function */
2065 poplevel (1, 1, 0);
2067 /* pop out of its parameters */
2068 poplevel (1, 0, 1);
2070 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
2072 if (DECL_SAVED_TREE (fndecl))
2074 tree fbody, block_body;
2075 /* Before we check initialization, attached all class initialization
2076 variable to the block_body */
2077 fbody = DECL_SAVED_TREE (fndecl);
2078 block_body = BIND_EXPR_BODY (fbody);
2079 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
2080 attach_init_test_initialization_flags, block_body);
2083 finish_method (fndecl);
2085 if (! flag_unit_at_a_time)
2087 /* Nulling these fields when we no longer need them saves
2088 memory. */
2089 DECL_SAVED_TREE (fndecl) = NULL;
2090 DECL_STRUCT_FUNCTION (fndecl) = NULL;
2091 DECL_INITIAL (fndecl) = NULL_TREE;
2093 current_function_decl = NULL_TREE;
2096 /* Prepare a method for expansion. */
2098 void
2099 finish_method (tree fndecl)
2101 tree *tp = &DECL_SAVED_TREE (fndecl);
2103 /* Wrap body of synchronized methods in a monitorenter,
2104 plus monitorexit cleanup. */
2105 if (METHOD_SYNCHRONIZED (fndecl))
2107 tree enter, exit, lock;
2108 if (METHOD_STATIC (fndecl))
2109 lock = build_class_ref (DECL_CONTEXT (fndecl));
2110 else
2111 lock = DECL_ARGUMENTS (fndecl);
2112 BUILD_MONITOR_ENTER (enter, lock);
2113 BUILD_MONITOR_EXIT (exit, lock);
2114 *tp = build2 (COMPOUND_EXPR, void_type_node, enter,
2115 build2 (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
2118 /* Prepend class initialization for static methods reachable from
2119 other classes. */
2120 if (METHOD_STATIC (fndecl)
2121 && (! METHOD_PRIVATE (fndecl)
2122 || INNER_CLASS_P (DECL_CONTEXT (fndecl)))
2123 && ! DECL_CLINIT_P (fndecl)
2124 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
2126 tree clas = DECL_CONTEXT (fndecl);
2127 tree init = build3 (CALL_EXPR, void_type_node,
2128 build_address_of (soft_initclass_node),
2129 build_tree_list (NULL_TREE, build_class_ref (clas)),
2130 NULL_TREE);
2131 *tp = build2 (COMPOUND_EXPR, TREE_TYPE (*tp), init, *tp);
2134 /* Convert function tree to GENERIC prior to inlining. */
2135 java_genericize (fndecl);
2137 /* Store the end of the function, so that we get good line number
2138 info for the epilogue. */
2139 if (DECL_STRUCT_FUNCTION (fndecl))
2140 cfun = DECL_STRUCT_FUNCTION (fndecl);
2141 else
2142 allocate_struct_function (fndecl);
2143 #ifdef USE_MAPPED_LOCATION
2144 cfun->function_end_locus = DECL_FUNCTION_LAST_LINE (fndecl);
2145 #else
2146 cfun->function_end_locus.file = DECL_SOURCE_FILE (fndecl);
2147 cfun->function_end_locus.line = DECL_FUNCTION_LAST_LINE (fndecl);
2148 #endif
2150 /* Defer inlining and expansion to the cgraph optimizers. */
2151 cgraph_finalize_function (fndecl, false);
2154 /* Optimize and expand a function's entire body. */
2156 void
2157 java_expand_body (tree fndecl)
2159 tree_rest_of_compilation (fndecl);
2162 /* We pessimistically marked all methods and fields external until we
2163 knew what set of classes we were planning to compile. Now mark those
2164 associated with CLASS to be generated locally as not external. */
2166 static void
2167 java_mark_decl_local (tree decl)
2169 DECL_EXTERNAL (decl) = 0;
2171 /* If we've already constructed DECL_RTL, give encode_section_info
2172 a second chance, now that we've changed the flags. */
2173 /* ??? Ideally, we'd have flag_unit_at_a_time set, and not have done
2174 anything that would have referenced DECL_RTL so far. But at the
2175 moment we force flag_unit_at_a_time off due to excessive memory
2176 consumption when compiling large jar files. Which probably means
2177 that we need to re-order how we process jar files... */
2178 if (DECL_RTL_SET_P (decl))
2179 make_decl_rtl (decl);
2182 /* Given appropriate target support, G++ will emit hidden aliases for native
2183 methods. Using this hidden name is required for proper operation of
2184 _Jv_Method::ncode, but it doesn't hurt to use it everywhere. Look for
2185 proper target support, then mark the method for aliasing. */
2187 static void
2188 java_mark_cni_decl_local (tree decl)
2190 /* Setting DECL_LOCAL_CNI_METHOD_P changes the behavior of the mangler.
2191 We expect that we should not yet have referenced this decl in a
2192 context that requires it. Check this invariant even if we don't have
2193 support for hidden aliases. */
2194 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
2196 #if !defined(HAVE_GAS_HIDDEN) || !defined(ASM_OUTPUT_DEF)
2197 return;
2198 #endif
2200 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2201 DECL_LOCAL_CNI_METHOD_P (decl) = 1;
2204 /* Use the preceding two functions and mark all members of the class. */
2206 void
2207 java_mark_class_local (tree class)
2209 tree t;
2211 for (t = TYPE_FIELDS (class); t ; t = TREE_CHAIN (t))
2212 if (FIELD_STATIC (t))
2213 java_mark_decl_local (t);
2215 for (t = TYPE_METHODS (class); t ; t = TREE_CHAIN (t))
2216 if (!METHOD_ABSTRACT (t))
2218 if (METHOD_NATIVE (t) && !flag_jni)
2219 java_mark_cni_decl_local (t);
2220 else
2221 java_mark_decl_local (t);
2225 /* Add a statement to a compound_expr. */
2227 tree
2228 add_stmt_to_compound (tree existing, tree type, tree stmt)
2230 if (!stmt)
2231 return existing;
2232 else if (existing)
2234 tree expr = build2 (COMPOUND_EXPR, type, existing, stmt);
2235 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (existing)
2236 | TREE_SIDE_EFFECTS (stmt);
2237 return expr;
2239 else
2240 return stmt;
2243 /* Add a statement to the statement_list currently being constructed.
2244 If the statement_list is null, we don't create a singleton list.
2245 This is necessary because poplevel() assumes that adding a
2246 statement to a null statement_list returns the statement. */
2248 tree
2249 java_add_stmt (tree new_stmt)
2251 tree stmts = current_binding_level->stmts;
2252 tree_stmt_iterator i;
2254 if (input_filename)
2255 SET_EXPR_LOCATION (new_stmt, input_location);
2257 if (stmts == NULL)
2258 return current_binding_level->stmts = new_stmt;
2260 /* Force STMTS to be a statement_list. */
2261 if (TREE_CODE (stmts) != STATEMENT_LIST)
2263 tree t = make_node (STATEMENT_LIST);
2264 i = tsi_last (t);
2265 tsi_link_after (&i, stmts, TSI_CONTINUE_LINKING);
2266 stmts = t;
2269 i = tsi_last (stmts);
2270 tsi_link_after (&i, new_stmt, TSI_CONTINUE_LINKING);
2272 return current_binding_level->stmts = stmts;
2275 /* Add a variable to the current scope. */
2277 tree
2278 java_add_local_var (tree decl)
2280 tree *vars = &current_binding_level->names;
2281 tree next = *vars;
2282 TREE_CHAIN (decl) = next;
2283 *vars = decl;
2284 DECL_CONTEXT (decl) = current_function_decl;
2285 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2286 return decl;
2289 /* Return a pointer to the compound_expr currently being
2290 constructed. */
2292 tree *
2293 get_stmts (void)
2295 return &current_binding_level->stmts;
2298 /* Register an exception range as belonging to the current binding
2299 level. There may only be one: if there are more, we'll create more
2300 binding levels. However, each range can have multiple handlers,
2301 and these are expanded when we call expand_end_java_handler(). */
2303 void
2304 register_exception_range (struct eh_range *range, int pc, int end_pc)
2306 gcc_assert (! current_binding_level->exception_range);
2307 current_binding_level->exception_range = range;
2308 current_binding_level->end_pc = end_pc;
2309 current_binding_level->start_pc = pc;
2312 #include "gt-java-decl.h"