1 /* Process declarations and variables for the GNU compiler for the
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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)
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, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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. */
31 #include "coretypes.h"
38 #include "java-tree.h"
44 #include "java-except.h"
48 #include "tree-inline.h"
51 #if defined (DEBUG_JAVA_BINDING_LEVELS)
52 extern void indent (void);
55 static tree push_jvm_slot (int, tree);
56 static tree lookup_name_current_level (tree);
57 static tree push_promoted_type (const char *, tree);
58 static struct binding_level *make_binding_level (void);
59 static tree create_primitive_vtable (const char *);
60 static tree check_local_unnamed_variable (tree, tree, tree);
62 /* Name of the Cloneable class. */
63 tree java_lang_cloneable_identifier_node;
65 /* Name of the Serializable class. */
66 tree java_io_serializable_identifier_node;
68 /* The DECL_MAP is a mapping from (index, type) to a decl node.
69 If index < max_locals, it is the index of a local variable.
70 if index >= max_locals, then index-max_locals is a stack slot.
71 The DECL_MAP mapping is represented as a TREE_VEC whose elements
72 are a list of decls (VAR_DECL or PARM_DECL) chained by
73 DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
74 we search the chain for a decl with a matching TREE_TYPE. */
76 static GTY(()) tree decl_map;
78 /* The base_decl_map is contains one variable of ptr_type: this is
79 used to contain every variable of reference type that is ever
80 stored in a local variable slot. */
82 static GTY(()) tree base_decl_map;
84 /* An index used to make temporary identifiers unique. */
87 /* A list of local variables VAR_DECLs for this method that we have seen
88 debug information, but we have not reached their starting (byte) PC yet. */
90 static GTY(()) tree pending_local_decls;
92 #if defined(DEBUG_JAVA_BINDING_LEVELS)
93 int binding_depth = 0;
94 int is_class_level = 0;
102 for (i = 0; i < binding_depth*2; i++)
105 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
107 /* True if decl is a named local variable, i.e. if it is an alias
108 that's used only for debugging purposes. */
111 debug_variable_p (tree decl)
113 if (TREE_CODE (decl) == PARM_DECL)
116 if (LOCAL_SLOT_P (decl))
122 /* Copy the value in decl into every live alias in the same local
123 variable slot. Some of these will be dead stores removed by the
127 update_aliases (tree decl, int index, int pc)
129 tree decl_type = TREE_TYPE (decl);
132 if (debug_variable_p (decl))
135 for (tmp = TREE_VEC_ELT (decl_map, index);
137 tmp = DECL_LOCAL_SLOT_CHAIN (tmp))
139 tree tmp_type = TREE_TYPE (tmp);
141 && LOCAL_SLOT_P (tmp) == 0
143 || (pc >= DECL_LOCAL_START_PC (tmp)
144 && pc <= DECL_LOCAL_END_PC (tmp)))
145 && (tmp_type == decl_type
146 || (INTEGRAL_TYPE_P (tmp_type)
147 && INTEGRAL_TYPE_P (decl_type)
148 && TYPE_PRECISION (decl_type) <= 32
149 && TYPE_PRECISION (tmp_type) <= 32)
150 || (TREE_CODE (tmp_type) == POINTER_TYPE
151 && TREE_CODE (decl_type) == POINTER_TYPE)))
153 tree src = build1 (NOP_EXPR, tmp_type, decl);
154 if (LOCAL_VAR_OUT_OF_SCOPE_P (tmp))
156 java_add_stmt (build2 (MODIFY_EXPR, tmp_type, tmp, src));
162 push_jvm_slot (int index, tree decl)
164 DECL_CONTEXT (decl) = current_function_decl;
165 layout_decl (decl, 0);
167 /* Now link the decl into the decl_map. */
168 if (DECL_LANG_SPECIFIC (decl) == NULL)
170 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
171 DECL_LOCAL_START_PC (decl) = 0;
172 DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
173 DECL_LOCAL_SLOT_NUMBER (decl) = index;
175 DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
176 TREE_VEC_ELT (decl_map, index) = decl;
181 /* At the point of its creation a local variable decl inherits
182 whatever is already in the same slot. In the case of a local
183 variable that is declared but unused, we won't find anything. */
186 initialize_local_variable (tree decl, int index)
188 tree decl_type = TREE_TYPE (decl);
189 if (TREE_CODE (decl_type) == POINTER_TYPE)
191 tree tmp = TREE_VEC_ELT (base_decl_map, index);
195 /* At the point of its creation this decl inherits whatever
197 tree src = build1 (NOP_EXPR, decl_type, tmp);
198 java_add_stmt (build2 (MODIFY_EXPR, decl_type, decl, src));
205 for (tmp = TREE_VEC_ELT (decl_map, index);
207 tmp = DECL_LOCAL_SLOT_CHAIN (tmp))
209 tree tmp_type = TREE_TYPE (tmp);
211 && ! debug_variable_p (tmp)
212 && (tmp_type == decl_type
213 || (INTEGRAL_TYPE_P (tmp_type)
214 && INTEGRAL_TYPE_P (decl_type)
215 && TYPE_PRECISION (decl_type) <= 32
216 && TYPE_PRECISION (tmp_type) <= 32
217 && TYPE_PRECISION (tmp_type)
218 >= TYPE_PRECISION (decl_type))))
220 java_add_stmt (build2 (MODIFY_EXPR, decl_type, decl, tmp));
227 /* Find the best declaration based upon type. If 'decl' fits 'type' better
228 than 'best', return 'decl'. Otherwise return 'best'. */
231 check_local_unnamed_variable (tree best, tree decl, tree type)
233 tree decl_type = TREE_TYPE (decl);
235 if (LOCAL_VAR_OUT_OF_SCOPE_P (decl))
238 /* Use the same decl for all integer types <= 32 bits. This is
239 necessary because sometimes a value is stored as (for example)
240 boolean but loaded as int. */
241 if (decl_type == type
242 || (INTEGRAL_TYPE_P (decl_type)
243 && INTEGRAL_TYPE_P (type)
244 && TYPE_PRECISION (decl_type) <= 32
245 && TYPE_PRECISION (type) <= 32
246 && TYPE_PRECISION (decl_type) >= TYPE_PRECISION (type))
247 || (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
248 && type == ptr_type_node))
250 if (best == NULL_TREE
251 || (decl_type == type && TREE_TYPE (best) != type))
259 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
260 that is valid at PC (or -1 if any pc).
261 If there is no existing matching decl, allocate one. */
264 find_local_variable (int index, tree type, int pc ATTRIBUTE_UNUSED)
266 tree tmp = TREE_VEC_ELT (decl_map, index);
267 tree decl = NULL_TREE;
269 /* Scan through every declaration that has been created in this
270 slot. We're only looking for variables that correspond to local
271 index declarations and PARM_DECLs, not named variables: such
272 local variables are used only for debugging information. */
273 while (tmp != NULL_TREE)
275 if (! debug_variable_p (tmp))
276 decl = check_local_unnamed_variable (decl, tmp, type);
277 tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
280 /* If we don't find a match, create one with the type passed in.
281 The name of the variable is #n#m, which n is the variable index
282 in the local variable area and m is a dummy identifier for
283 uniqueness -- multiple variables may share the same local
284 variable index. We don't call pushdecl() to push pointer types
285 into a binding expr because they'll all be replaced by a single
286 variable that is used for every reference in that local variable
292 sprintf (buf, "#slot#%d#%d", index, uniq++);
293 name = get_identifier (buf);
294 decl = build_decl (VAR_DECL, name, type);
295 DECL_IGNORED_P (decl) = 1;
296 DECL_ARTIFICIAL (decl) = 1;
297 decl = push_jvm_slot (index, decl);
298 LOCAL_SLOT_P (decl) = 1;
300 if (TREE_CODE (type) != POINTER_TYPE)
301 pushdecl_function_level (decl);
304 /* As well as creating a local variable that matches the type, we
305 also create a base variable (of ptr_type) that will hold all its
307 if (TREE_CODE (type) == POINTER_TYPE
308 && ! TREE_VEC_ELT (base_decl_map, index))
313 sprintf (buf, "#ref#%d#%d", index, uniq++);
314 name = get_identifier (buf);
316 = TREE_VEC_ELT (base_decl_map, index)
317 = build_decl (VAR_DECL, name, ptr_type_node);
318 pushdecl_function_level (base_decl);
319 DECL_IGNORED_P (base_decl) = 1;
320 DECL_ARTIFICIAL (base_decl) = 1;
326 /* Called during gimplification for every variable. If the variable
327 is a temporary of pointer type, replace it with a common variable
328 thath is used to hold all pointer types that are ever stored in
329 that slot. Set WANT_LVALUE if you want a variable that is to be
333 java_replace_reference (tree var_decl, bool want_lvalue)
340 decl_type = TREE_TYPE (var_decl);
342 if (TREE_CODE (decl_type) == POINTER_TYPE)
344 if (DECL_LANG_SPECIFIC (var_decl)
345 && LOCAL_SLOT_P (var_decl))
347 int index = DECL_LOCAL_SLOT_NUMBER (var_decl);
348 tree base_decl = TREE_VEC_ELT (base_decl_map, index);
354 base_decl = build1 (NOP_EXPR, decl_type, base_decl);
364 /* Same as find_local_index, except that INDEX is a stack index. */
367 find_stack_slot (int index, tree type)
369 return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
373 struct binding_level GTY(())
375 /* A chain of _DECL nodes for all variables, constants, functions,
376 * and typedef types. These are in the reverse of the order supplied.
380 /* For each level, a list of shadowed outer-level local definitions
381 to be restored when this level is popped.
382 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
383 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
386 /* For each level (except not the global one),
387 a chain of BLOCK nodes for all the levels
388 that were entered and exited one level down. */
391 /* The binding level which this one is contained in (inherits from). */
392 struct binding_level *level_chain;
394 /* The bytecode PC that marks the end of this level. */
396 /* The bytecode PC that marks the start of this level. */
399 /* The statements in this binding level. */
402 /* An exception range associated with this binding level. */
403 struct eh_range * GTY((skip (""))) exception_range;
405 /* Binding depth at which this level began. Used only for debugging. */
406 unsigned binding_depth;
409 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
411 /* The binding level currently in effect. */
413 static GTY(()) struct binding_level *current_binding_level;
415 /* A chain of binding_level structures awaiting reuse. */
417 static GTY(()) struct binding_level *free_binding_level;
419 /* The outermost binding level, for names of file scope.
420 This is created when the compiler is started and exists
421 through the entire run. */
423 static GTY(()) struct binding_level *global_binding_level;
425 /* The binding level that holds variables declared at the outermost
426 level within a function body. */
428 static struct binding_level *function_binding_level;
430 /* A PC value bigger than any PC value we may ever may encounter. */
432 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
434 /* Binding level structures are initialized by copying this one. */
436 static const struct binding_level clear_binding_level
438 NULL_TREE, /* names */
439 NULL_TREE, /* shadowed */
440 NULL_TREE, /* blocks */
441 NULL_BINDING_LEVEL, /* level_chain */
442 LARGEST_PC, /* end_pc */
445 NULL, /* exception_range */
446 0, /* binding_depth */
450 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
451 that have names. Here so we can clear out their names' definitions
452 at the end of the function. */
454 static tree named_labels;
456 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
458 static tree shadowed_labels;
461 tree java_global_trees[JTI_MAX];
463 /* Build (and pushdecl) a "promoted type" for all standard
464 types shorter than int. */
467 push_promoted_type (const char *name, tree actual_type)
469 tree type = make_node (TREE_CODE (actual_type));
471 tree in_min = TYPE_MIN_VALUE (int_type_node);
472 tree in_max = TYPE_MAX_VALUE (int_type_node);
474 tree in_min = TYPE_MIN_VALUE (actual_type);
475 tree in_max = TYPE_MAX_VALUE (actual_type);
477 TYPE_MIN_VALUE (type) = copy_node (in_min);
478 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
479 TYPE_MAX_VALUE (type) = copy_node (in_max);
480 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
481 TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
483 pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
487 /* Return a definition for a builtin function named NAME and whose data type
488 is TYPE. TYPE should be a function type with argument types.
489 FUNCTION_CODE tells later passes how to compile calls to this function.
490 See tree.h for its possible values.
492 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
493 the name to be called if we can't opencode the function. If
494 ATTRS is nonzero, use that for the function's attribute list. */
497 builtin_function (const char *name,
500 enum built_in_class cl,
501 const char *library_name,
502 tree ARG_UNUSED (attrs))
504 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
505 DECL_EXTERNAL (decl) = 1;
506 TREE_PUBLIC (decl) = 1;
508 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
509 make_decl_rtl (decl);
511 DECL_BUILT_IN_CLASS (decl) = cl;
512 DECL_FUNCTION_CODE (decl) = function_code;
516 /* Return tree that represents a vtable for a primitive array. */
518 create_primitive_vtable (const char *name)
523 sprintf (buf, "_Jv_%sVTable", name);
524 r = build_decl (VAR_DECL, get_identifier (buf), ptr_type_node);
525 DECL_EXTERNAL (r) = 1;
537 java_init_decl_processing (void)
540 tree field = NULL_TREE;
543 init_class_processing ();
545 current_function_decl = NULL;
546 current_binding_level = NULL_BINDING_LEVEL;
547 free_binding_level = NULL_BINDING_LEVEL;
548 pushlevel (0); /* make the binding_level structure for global names */
549 global_binding_level = current_binding_level;
551 /* The code here must be similar to build_common_tree_nodes{,_2} in
552 tree.c, especially as to the order of initializing common nodes. */
553 error_mark_node = make_node (ERROR_MARK);
554 TREE_TYPE (error_mark_node) = error_mark_node;
556 /* Create sizetype first - needed for other types. */
557 initialize_sizetypes (false);
559 byte_type_node = make_signed_type (8);
560 pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
561 short_type_node = make_signed_type (16);
562 pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
563 int_type_node = make_signed_type (32);
564 pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
565 long_type_node = make_signed_type (64);
566 pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));
568 unsigned_byte_type_node = make_unsigned_type (8);
569 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
570 unsigned_byte_type_node));
571 unsigned_short_type_node = make_unsigned_type (16);
572 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
573 unsigned_short_type_node));
574 unsigned_int_type_node = make_unsigned_type (32);
575 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
576 unsigned_int_type_node));
577 unsigned_long_type_node = make_unsigned_type (64);
578 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
579 unsigned_long_type_node));
581 /* This is not a java type, however tree-dfa requires a definition for
583 size_type_node = make_unsigned_type (POINTER_SIZE);
584 set_sizetype (size_type_node);
586 /* Define these next since types below may used them. */
587 integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
588 integer_zero_node = build_int_cst (NULL_TREE, 0);
589 integer_one_node = build_int_cst (NULL_TREE, 1);
590 integer_two_node = build_int_cst (NULL_TREE, 2);
591 integer_four_node = build_int_cst (NULL_TREE, 4);
592 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
594 /* A few values used for range checking in the lexer. */
595 decimal_int_max = build_int_cstu (unsigned_int_type_node, 0x80000000);
596 #if HOST_BITS_PER_WIDE_INT == 64
597 decimal_long_max = build_int_cstu (unsigned_long_type_node,
598 0x8000000000000000LL);
599 #elif HOST_BITS_PER_WIDE_INT == 32
600 decimal_long_max = build_int_cst_wide (unsigned_long_type_node,
603 #error "unsupported size"
606 size_zero_node = size_int (0);
607 size_one_node = size_int (1);
608 bitsize_zero_node = bitsize_int (0);
609 bitsize_one_node = bitsize_int (1);
610 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
612 long_zero_node = build_int_cst (long_type_node, 0);
614 void_type_node = make_node (VOID_TYPE);
615 pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
616 layout_type (void_type_node); /* Uses size_zero_node */
617 ptr_type_node = build_pointer_type (void_type_node);
618 t = make_node (VOID_TYPE);
619 layout_type (t); /* Uses size_zero_node */
620 return_address_type_node = build_pointer_type (t);
622 null_pointer_node = build_int_cst (ptr_type_node, 0);
625 /* Make a type to be the domain of a few array types
626 whose domains don't really matter.
627 200 is small enough that it always fits in size_t
628 and large enough that it can hold most function names for the
629 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
630 short_array_type_node = build_prim_array_type (short_type_node, 200);
632 char_type_node = make_node (CHAR_TYPE);
633 TYPE_PRECISION (char_type_node) = 16;
634 fixup_unsigned_type (char_type_node);
635 pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));
637 boolean_type_node = make_node (BOOLEAN_TYPE);
638 TYPE_PRECISION (boolean_type_node) = 1;
639 fixup_unsigned_type (boolean_type_node);
640 pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
642 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
643 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
645 promoted_byte_type_node
646 = push_promoted_type ("promoted_byte", byte_type_node);
647 promoted_short_type_node
648 = push_promoted_type ("promoted_short", short_type_node);
649 promoted_char_type_node
650 = push_promoted_type ("promoted_char", char_type_node);
651 promoted_boolean_type_node
652 = push_promoted_type ("promoted_boolean", boolean_type_node);
654 float_type_node = make_node (REAL_TYPE);
655 TYPE_PRECISION (float_type_node) = 32;
656 pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
658 layout_type (float_type_node);
660 double_type_node = make_node (REAL_TYPE);
661 TYPE_PRECISION (double_type_node) = 64;
662 pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
664 layout_type (double_type_node);
666 float_zero_node = build_real (float_type_node, dconst0);
667 double_zero_node = build_real (double_type_node, dconst0);
669 /* These are the vtables for arrays of primitives. */
670 boolean_array_vtable = create_primitive_vtable ("boolean");
671 byte_array_vtable = create_primitive_vtable ("byte");
672 char_array_vtable = create_primitive_vtable ("char");
673 short_array_vtable = create_primitive_vtable ("short");
674 int_array_vtable = create_primitive_vtable ("int");
675 long_array_vtable = create_primitive_vtable ("long");
676 float_array_vtable = create_primitive_vtable ("float");
677 double_array_vtable = create_primitive_vtable ("double");
679 one_elt_array_domain_type = build_index_type (integer_one_node);
680 utf8const_type = make_node (RECORD_TYPE);
681 PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
682 PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
683 FINISH_RECORD (utf8const_type);
684 utf8const_ptr_type = build_pointer_type (utf8const_type);
686 atable_type = build_array_type (ptr_type_node,
687 one_elt_array_domain_type);
688 TYPE_NONALIASED_COMPONENT (atable_type) = 1;
689 atable_ptr_type = build_pointer_type (atable_type);
691 symbol_type = make_node (RECORD_TYPE);
692 PUSH_FIELD (symbol_type, field, "clname", utf8const_ptr_type);
693 PUSH_FIELD (symbol_type, field, "name", utf8const_ptr_type);
694 PUSH_FIELD (symbol_type, field, "signature", utf8const_ptr_type);
695 FINISH_RECORD (symbol_type);
697 symbols_array_type = build_array_type (symbol_type,
698 one_elt_array_domain_type);
699 symbols_array_ptr_type = build_pointer_type (symbols_array_type);
701 /* As you're adding items here, please update the code right after
702 this section, so that the filename containing the source code of
703 the pre-defined class gets registered correctly. */
704 unqualified_object_id_node = get_identifier ("Object");
705 object_type_node = lookup_class (get_identifier ("java.lang.Object"));
706 object_ptr_type_node = promote_type (object_type_node);
707 string_type_node = lookup_class (get_identifier ("java.lang.String"));
708 string_ptr_type_node = promote_type (string_type_node);
709 class_type_node = lookup_class (get_identifier ("java.lang.Class"));
710 throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
711 exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
712 runtime_exception_type_node =
713 lookup_class (get_identifier ("java.lang.RuntimeException"));
714 error_exception_type_node =
715 lookup_class (get_identifier ("java.lang.Error"));
717 rawdata_ptr_type_node
718 = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
720 add_predefined_file (get_identifier ("java/lang/Class.java"));
721 add_predefined_file (get_identifier ("java/lang/Error.java"));
722 add_predefined_file (get_identifier ("java/lang/Object.java"));
723 add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
724 add_predefined_file (get_identifier ("java/lang/String.java"));
725 add_predefined_file (get_identifier ("java/lang/Throwable.java"));
726 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
727 add_predefined_file (get_identifier ("java/lang/Exception.java"));
728 add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
729 add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
731 methodtable_type = make_node (RECORD_TYPE);
732 layout_type (methodtable_type);
733 build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
734 methodtable_ptr_type = build_pointer_type (methodtable_type);
736 TYPE_identifier_node = get_identifier ("TYPE");
737 init_identifier_node = get_identifier ("<init>");
738 clinit_identifier_node = get_identifier ("<clinit>");
739 finit_identifier_node = get_identifier ("finit$");
740 instinit_identifier_node = get_identifier ("instinit$");
741 void_signature_node = get_identifier ("()V");
742 length_identifier_node = get_identifier ("length");
743 finalize_identifier_node = get_identifier ("finalize");
744 this_identifier_node = get_identifier ("this");
745 super_identifier_node = get_identifier ("super");
746 continue_identifier_node = get_identifier ("continue");
747 access0_identifier_node = get_identifier ("access$0");
748 classdollar_identifier_node = get_identifier ("class$");
750 java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
751 java_io_serializable_identifier_node =
752 get_identifier ("java.io.Serializable");
754 /* for lack of a better place to put this stub call */
755 init_expr_processing();
757 constants_type_node = make_node (RECORD_TYPE);
758 PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
759 PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
760 PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
761 FINISH_RECORD (constants_type_node);
762 build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
764 access_flags_type_node = unsigned_short_type_node;
766 dtable_type = make_node (RECORD_TYPE);
767 dtable_ptr_type = build_pointer_type (dtable_type);
769 otable_type = build_array_type (integer_type_node,
770 one_elt_array_domain_type);
771 TYPE_NONALIASED_COMPONENT (otable_type) = 1;
772 otable_ptr_type = build_pointer_type (otable_type);
774 PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
775 DECL_FCONTEXT (field) = object_type_node;
776 TYPE_VFIELD (object_type_node) = field;
778 /* This isn't exactly true, but it is what we have in the source.
779 There is an unresolved issue here, which is whether the vtable
780 should be marked by the GC. */
781 if (! flag_hash_synchronization)
782 PUSH_FIELD (object_type_node, field, "sync_info",
783 build_pointer_type (object_type_node));
784 for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
785 FIELD_PRIVATE (t) = 1;
786 FINISH_RECORD (object_type_node);
788 field_type_node = make_node (RECORD_TYPE);
789 field_ptr_type_node = build_pointer_type (field_type_node);
790 method_type_node = make_node (RECORD_TYPE);
791 method_ptr_type_node = build_pointer_type (method_type_node);
793 set_super_info (0, class_type_node, object_type_node, 0);
794 set_super_info (0, string_type_node, object_type_node, 0);
795 class_ptr_type = build_pointer_type (class_type_node);
797 PUSH_FIELD (class_type_node, field, "next", class_ptr_type);
798 PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
799 PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
800 PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
801 PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
802 PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
803 PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
804 PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
805 PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
806 PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
807 PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
808 PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
809 PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
810 PUSH_FIELD (class_type_node, field, "otable", otable_ptr_type);
811 PUSH_FIELD (class_type_node, field, "otable_syms",
812 symbols_array_ptr_type);
813 PUSH_FIELD (class_type_node, field, "atable", atable_ptr_type);
814 PUSH_FIELD (class_type_node, field, "atable_syms",
815 symbols_array_ptr_type);
816 PUSH_FIELD (class_type_node, field, "catch_classes", ptr_type_node);
817 PUSH_FIELD (class_type_node, field, "interfaces",
818 build_pointer_type (class_ptr_type));
819 PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
820 PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
821 PUSH_FIELD (class_type_node, field, "state", byte_type_node);
822 PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
823 PUSH_FIELD (class_type_node, field, "depth", short_type_node);
824 PUSH_FIELD (class_type_node, field, "ancestors", ptr_type_node);
825 PUSH_FIELD (class_type_node, field, "idt", ptr_type_node);
826 PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);
827 PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
828 PUSH_FIELD (class_type_node, field, "hack_signers", ptr_type_node);
829 PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
830 PUSH_FIELD (class_type_node, field, "aux_info", ptr_type_node);
831 for (t = TYPE_FIELDS (class_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
832 FIELD_PRIVATE (t) = 1;
833 push_super_field (class_type_node, object_type_node);
835 FINISH_RECORD (class_type_node);
836 build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
838 field_info_union_node = make_node (UNION_TYPE);
839 PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
840 PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
842 PUSH_FIELD (field_info_union_node, field, "idx", unsigned_short_type_node);
844 layout_type (field_info_union_node);
846 PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
847 PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
848 PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
849 PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
850 PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
851 FINISH_RECORD (field_type_node);
852 build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
854 nativecode_ptr_array_type_node
855 = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
857 PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
858 PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
859 FINISH_RECORD (dtable_type);
860 build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
862 #define jint_type int_type_node
863 #define jint_ptr_type ptr_type_node
865 jexception_type = make_node (RECORD_TYPE);
866 PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
867 PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
868 PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
869 PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
870 FINISH_RECORD (jexception_type);
871 build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
872 jexception_ptr_type = build_pointer_type (jexception_type);
874 lineNumberEntry_type = make_node (RECORD_TYPE);
875 PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
876 PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
877 FINISH_RECORD (lineNumberEntry_type);
879 lineNumbers_type = make_node (RECORD_TYPE);
880 PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
881 FINISH_RECORD (lineNumbers_type);
883 #define instn_ptr_type_node ptr_type_node /* XXX JH */
885 #define lineNumbers_ptr_type_node build_pointer_type(lineNumbers_type)
887 PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
888 PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
889 PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
890 PUSH_FIELD (method_type_node, field, "index", unsigned_short_type_node);
891 PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
892 PUSH_FIELD (method_type_node, field, "throws", ptr_type_node);
893 FINISH_RECORD (method_type_node);
894 build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
896 endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
898 t = tree_cons (NULL_TREE, class_ptr_type,
899 tree_cons (NULL_TREE, int_type_node, endlink));
900 alloc_object_node = builtin_function ("_Jv_AllocObject",
901 build_function_type (ptr_type_node, t),
902 0, NOT_BUILT_IN, NULL, NULL_TREE);
903 DECL_IS_MALLOC (alloc_object_node) = 1;
904 alloc_no_finalizer_node =
905 builtin_function ("_Jv_AllocObjectNoFinalizer",
906 build_function_type (ptr_type_node, t),
907 0, NOT_BUILT_IN, NULL, NULL_TREE);
908 DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
910 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
911 soft_initclass_node = builtin_function ("_Jv_InitClass",
912 build_function_type (void_type_node,
914 0, NOT_BUILT_IN, NULL, NULL_TREE);
916 throw_node = builtin_function ("_Jv_Throw",
917 build_function_type (ptr_type_node, t),
918 0, NOT_BUILT_IN, NULL, NULL_TREE);
919 /* Mark throw_nodes as `noreturn' functions with side effects. */
920 TREE_THIS_VOLATILE (throw_node) = 1;
921 TREE_SIDE_EFFECTS (throw_node) = 1;
923 t = build_function_type (int_type_node, endlink);
924 soft_monitorenter_node
925 = builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
927 soft_monitorexit_node
928 = builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
931 t = tree_cons (NULL_TREE, int_type_node,
932 tree_cons (NULL_TREE, int_type_node, endlink));
934 = builtin_function ("_Jv_NewPrimArray",
935 build_function_type(ptr_type_node, t),
936 0, NOT_BUILT_IN, NULL, NULL_TREE);
937 DECL_IS_MALLOC (soft_newarray_node) = 1;
939 t = tree_cons (NULL_TREE, int_type_node,
940 tree_cons (NULL_TREE, class_ptr_type,
941 tree_cons (NULL_TREE, object_ptr_type_node, endlink)));
943 = builtin_function ("_Jv_NewObjectArray",
944 build_function_type (ptr_type_node, t),
945 0, NOT_BUILT_IN, NULL, NULL_TREE);
946 DECL_IS_MALLOC (soft_anewarray_node) = 1;
948 /* There is no endlink here because _Jv_NewMultiArray is a varargs
950 t = tree_cons (NULL_TREE, ptr_type_node,
951 tree_cons (NULL_TREE, int_type_node, NULL_TREE));
952 soft_multianewarray_node
953 = builtin_function ("_Jv_NewMultiArray",
954 build_function_type (ptr_type_node, t),
955 0, NOT_BUILT_IN, NULL, NULL_TREE);
956 DECL_IS_MALLOC (soft_multianewarray_node) = 1;
958 t = build_function_type (void_type_node,
959 tree_cons (NULL_TREE, int_type_node, endlink));
960 soft_badarrayindex_node
961 = builtin_function ("_Jv_ThrowBadArrayIndex", t,
962 0, NOT_BUILT_IN, NULL, NULL_TREE);
963 /* Mark soft_badarrayindex_node as a `noreturn' function with side
965 TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
966 TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
968 soft_nullpointer_node
969 = builtin_function ("_Jv_ThrowNullPointerException",
970 build_function_type (void_type_node, endlink),
971 0, NOT_BUILT_IN, NULL, NULL_TREE);
972 /* Mark soft_nullpointer_node as a `noreturn' function with side
974 TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
975 TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
977 t = tree_cons (NULL_TREE, class_ptr_type,
978 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
980 = builtin_function ("_Jv_CheckCast",
981 build_function_type (ptr_type_node, t),
982 0, NOT_BUILT_IN, NULL, NULL_TREE);
983 t = tree_cons (NULL_TREE, object_ptr_type_node,
984 tree_cons (NULL_TREE, class_ptr_type, endlink));
986 = builtin_function ("_Jv_IsInstanceOf",
987 build_function_type (boolean_type_node, t),
988 0, NOT_BUILT_IN, NULL, NULL_TREE);
989 DECL_IS_PURE (soft_instanceof_node) = 1;
990 t = tree_cons (NULL_TREE, object_ptr_type_node,
991 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
992 soft_checkarraystore_node
993 = builtin_function ("_Jv_CheckArrayStore",
994 build_function_type (void_type_node, t),
995 0, NOT_BUILT_IN, NULL, NULL_TREE);
996 t = tree_cons (NULL_TREE, ptr_type_node,
997 tree_cons (NULL_TREE, ptr_type_node,
998 tree_cons (NULL_TREE, int_type_node, endlink)));
999 soft_lookupinterfacemethod_node
1000 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
1001 build_function_type (ptr_type_node, t),
1002 0, NOT_BUILT_IN, NULL, NULL_TREE);
1004 DECL_IS_PURE (soft_lookupinterfacemethod_node) = 1;
1005 t = tree_cons (NULL_TREE, object_ptr_type_node,
1006 tree_cons (NULL_TREE, ptr_type_node,
1007 tree_cons (NULL_TREE, ptr_type_node,
1008 tree_cons (NULL_TREE, int_type_node,
1010 soft_lookupjnimethod_node
1011 = builtin_function ("_Jv_LookupJNIMethod",
1012 build_function_type (ptr_type_node, t),
1013 0, NOT_BUILT_IN, NULL, NULL_TREE);
1014 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1015 soft_getjnienvnewframe_node
1016 = builtin_function ("_Jv_GetJNIEnvNewFrame",
1017 build_function_type (ptr_type_node, t),
1018 0, NOT_BUILT_IN, NULL, NULL_TREE);
1019 soft_jnipopsystemframe_node
1020 = builtin_function ("_Jv_JNI_PopSystemFrame",
1021 build_function_type (ptr_type_node, t),
1022 0, NOT_BUILT_IN, NULL, NULL_TREE);
1025 = builtin_function ("_Jv_divI",
1026 build_function_type (int_type_node, t),
1027 0, NOT_BUILT_IN, NULL, NULL_TREE);
1030 = builtin_function ("_Jv_remI",
1031 build_function_type (int_type_node, t),
1032 0, NOT_BUILT_IN, NULL, NULL_TREE);
1035 = builtin_function ("_Jv_divJ",
1036 build_function_type (long_type_node, t),
1037 0, NOT_BUILT_IN, NULL, NULL_TREE);
1040 = builtin_function ("_Jv_remJ",
1041 build_function_type (long_type_node, t),
1042 0, NOT_BUILT_IN, NULL, NULL_TREE);
1044 /* Initialize variables for except.c. */
1045 eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
1046 ? "__gcj_personality_sj0"
1047 : "__gcj_personality_v0");
1049 lang_eh_runtime_type = do_nothing;
1053 initialize_builtins ();
1054 soft_fmod_node = built_in_decls[BUILT_IN_FMOD];
1056 soft_fmodf_node = built_in_decls[BUILT_IN_FMODF];
1061 /* Look up NAME in the current binding level and its superiors
1062 in the namespace of variables, functions and typedefs.
1063 Return a ..._DECL node of some kind representing its definition,
1064 or return 0 if it is undefined. */
1067 lookup_name (tree name)
1070 if (current_binding_level != global_binding_level
1071 && IDENTIFIER_LOCAL_VALUE (name))
1072 val = IDENTIFIER_LOCAL_VALUE (name);
1074 val = IDENTIFIER_GLOBAL_VALUE (name);
1078 /* Similar to `lookup_name' but look only at current binding level and
1079 the previous one if its the parameter level. */
1082 lookup_name_current_level (tree name)
1086 if (current_binding_level == global_binding_level)
1087 return IDENTIFIER_GLOBAL_VALUE (name);
1089 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
1092 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
1093 if (DECL_NAME (t) == name)
1099 /* Use a binding level to record a labeled block declaration */
1102 push_labeled_block (tree lb)
1104 tree name = DECL_NAME (LABELED_BLOCK_LABEL (lb));
1105 struct binding_level *b = current_binding_level;
1106 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1108 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1109 TREE_CHAIN (lb) = b->names;
1111 IDENTIFIER_LOCAL_VALUE (name) = lb;
1114 /* Pop the current binding level, reinstalling values for the previous
1118 pop_labeled_block (void)
1120 struct binding_level *b = current_binding_level;
1121 tree label = b->names;
1122 IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label))) =
1125 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b->shadowed)) =
1126 TREE_VALUE (b->shadowed);
1128 /* Pop the current level, and free the structure for reuse. */
1129 current_binding_level = current_binding_level->level_chain;
1130 b->level_chain = free_binding_level;
1131 free_binding_level = b;
1134 /* Record a decl-node X as belonging to the current lexical scope.
1135 Check for errors (such as an incompatible declaration for the same
1136 name already seen in the same scope).
1138 Returns either X or an old decl for the same name.
1139 If an old decl is returned, it may have been smashed
1140 to agree with what X says. */
1146 tree name = DECL_NAME (x);
1147 struct binding_level *b = current_binding_level;
1149 if (TREE_CODE (x) != TYPE_DECL)
1150 DECL_CONTEXT (x) = current_function_decl;
1153 t = lookup_name_current_level (name);
1154 if (t != 0 && t == error_mark_node)
1155 /* error_mark_node is 0 for a while during initialization! */
1158 error ("%J'%D' used prior to declaration", x, x);
1161 /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1162 to point to the TYPE_DECL.
1163 Since Java does not have typedefs, a type can only have
1164 one (true) name, given by a class, interface, or builtin. */
1165 if (TREE_CODE (x) == TYPE_DECL
1166 && TYPE_NAME (TREE_TYPE (x)) == 0
1167 && TREE_TYPE (x) != error_mark_node)
1169 TYPE_NAME (TREE_TYPE (x)) = x;
1170 TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1173 /* This name is new in its binding level.
1174 Install the new declaration and return it. */
1175 if (b == global_binding_level)
1177 /* Install a global value. */
1179 IDENTIFIER_GLOBAL_VALUE (name) = x;
1183 /* Here to install a non-global value. */
1184 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1185 IDENTIFIER_LOCAL_VALUE (name) = x;
1188 /* Warn if shadowing an argument at the top level of the body. */
1189 if (oldlocal != 0 && !DECL_EXTERNAL (x)
1190 /* This warning doesn't apply to the parms of a nested fcn. */
1191 && ! current_binding_level->parm_flag
1192 /* Check that this is one level down from the parms. */
1193 && current_binding_level->level_chain->parm_flag
1194 /* Check that the decl being shadowed
1195 comes from the parm level, one level up. */
1196 && chain_member (oldlocal, current_binding_level->level_chain->names))
1198 if (TREE_CODE (oldlocal) == PARM_DECL)
1199 pedwarn ("declaration of `%s' shadows a parameter",
1200 IDENTIFIER_POINTER (name));
1202 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
1203 IDENTIFIER_POINTER (name));
1206 /* Maybe warn if shadowing something else. */
1207 else if (warn_shadow && !DECL_EXTERNAL (x)
1208 /* No shadow warnings for internally generated vars. */
1209 && DECL_SOURCE_LINE (x) != 0
1210 /* No shadow warnings for vars made for inlining. */
1211 && ! DECL_FROM_INLINE (x))
1213 const char *warnstring = 0;
1215 if (TREE_CODE (x) == PARM_DECL
1216 && current_binding_level->level_chain->parm_flag)
1217 /* Don't warn about the parm names in function declarator
1218 within a function declarator.
1219 It would be nice to avoid warning in any function
1220 declarator in a declaration, as opposed to a definition,
1221 but there is no way to tell it's not a definition. */
1223 else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
1224 warnstring = "declaration of `%s' shadows a parameter";
1225 else if (oldlocal != 0)
1226 warnstring = "declaration of `%s' shadows previous local";
1227 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1228 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1229 warnstring = "declaration of `%s' shadows global declaration";
1232 warning (warnstring, IDENTIFIER_POINTER (name));
1236 /* If storing a local value, there may already be one (inherited).
1237 If so, record it for restoration when this binding level ends. */
1239 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1243 /* Put decls on list in reverse order.
1244 We will reverse them later if necessary. */
1245 TREE_CHAIN (x) = b->names;
1252 pushdecl_force_head (tree x)
1254 current_binding_level->names = x;
1257 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1260 pushdecl_top_level (tree x)
1263 struct binding_level *b = current_binding_level;
1265 current_binding_level = global_binding_level;
1267 current_binding_level = b;
1271 /* Like pushdecl, only it places X in FUNCTION_BINDING_LEVEL, if appropriate. */
1274 pushdecl_function_level (tree x)
1277 struct binding_level *b = current_binding_level;
1279 current_binding_level = function_binding_level;
1281 current_binding_level = b;
1285 /* Nonzero if we are currently in the global binding level. */
1288 global_bindings_p (void)
1290 return current_binding_level == global_binding_level;
1293 /* Return the list of declarations of the current level.
1294 Note that this list is in reverse order unless/until
1295 you nreverse it; and when you do nreverse it, you must
1296 store the result back using `storedecls' or you will lose. */
1301 return current_binding_level->names;
1304 /* Create a new `struct binding_level'. */
1306 static struct binding_level *
1307 make_binding_level (void)
1310 return ggc_alloc_cleared (sizeof (struct binding_level));
1314 pushlevel (int unused ATTRIBUTE_UNUSED)
1316 struct binding_level *newlevel = NULL_BINDING_LEVEL;
1319 /* If this is the top level of a function,
1320 just make sure that NAMED_LABELS is 0. */
1322 if (current_binding_level == global_binding_level)
1326 /* Reuse or create a struct for this binding level. */
1328 if (free_binding_level)
1330 newlevel = free_binding_level;
1331 free_binding_level = free_binding_level->level_chain;
1335 newlevel = make_binding_level ();
1338 /* Add this level to the front of the chain (stack) of levels that
1341 *newlevel = clear_binding_level;
1342 newlevel->level_chain = current_binding_level;
1343 current_binding_level = newlevel;
1344 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1345 newlevel->binding_depth = binding_depth;
1347 fprintf (stderr, "push %s level %p pc %d\n",
1348 (is_class_level) ? "class" : "block", newlevel, current_pc);
1351 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1354 /* Exit a binding level.
1355 Pop the level off, and restore the state of the identifier-decl mappings
1356 that were in effect when this level was entered.
1358 If KEEP is nonzero, this level had explicit declarations, so
1359 and create a "block" (a BLOCK node) for the level
1360 to record its declarations and subblocks for symbol table output.
1362 If FUNCTIONBODY is nonzero, this level is the body of a function,
1363 so create a block as if KEEP were set and also clear out all
1366 If REVERSE is nonzero, reverse the order of decls before putting
1367 them into the BLOCK. */
1370 poplevel (int keep, int reverse, int functionbody)
1373 /* The chain of decls was accumulated in reverse order.
1374 Put it into forward order, just for cleanliness. */
1376 tree subblocks = current_binding_level->blocks;
1381 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1384 if (current_binding_level->end_pc != LARGEST_PC)
1385 fprintf (stderr, "pop %s level %p pc %d (end pc %d)\n",
1386 (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1387 current_binding_level->end_pc);
1389 fprintf (stderr, "pop %s level %p pc %d\n",
1390 (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1392 if (is_class_level != (current_binding_level == class_binding_level))
1395 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
1399 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1401 /* Get the decls in the order they were written.
1402 Usually current_binding_level->names is in reverse order.
1403 But parameter decls were previously put in forward order. */
1406 current_binding_level->names
1407 = decls = nreverse (current_binding_level->names);
1409 decls = current_binding_level->names;
1411 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1412 if (TREE_CODE (decl) == VAR_DECL
1413 && DECL_LANG_SPECIFIC (decl) != NULL
1414 && DECL_LOCAL_SLOT_NUMBER (decl))
1415 LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
1417 /* If there were any declarations in that level,
1418 or if this level is a function body,
1419 create a BLOCK to record them for the life of this function. */
1422 if (keep || functionbody)
1424 block = make_node (BLOCK);
1425 TREE_TYPE (block) = void_type_node;
1428 if (current_binding_level->exception_range)
1429 expand_end_java_handler (current_binding_level->exception_range);
1433 /* If any statements have been generated at this level, create a
1434 BIND_EXPR to hold them and copy the variables to it. This
1435 only applies to the bytecode compiler. */
1436 if (current_binding_level->stmts)
1439 tree *var = &BLOCK_VARS (block);
1441 /* Copy decls from names list, ignoring labels. */
1444 tree next = TREE_CHAIN (decl);
1445 if (TREE_CODE (decl) != LABEL_DECL)
1448 var = &TREE_CHAIN (decl);
1454 bind = build3 (BIND_EXPR, TREE_TYPE (block), BLOCK_VARS (block),
1455 BLOCK_EXPR_BODY (block), block);
1456 BIND_EXPR_BODY (bind) = current_binding_level->stmts;
1458 if (BIND_EXPR_BODY (bind)
1459 && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
1460 TREE_SIDE_EFFECTS (bind) = 1;
1462 /* FIXME: gimplifier brain damage. */
1463 if (BIND_EXPR_BODY (bind) == NULL)
1464 BIND_EXPR_BODY (bind) = build_java_empty_stmt ();
1466 current_binding_level->stmts = NULL;
1470 BLOCK_VARS (block) = decls;
1472 BLOCK_SUBBLOCKS (block) = subblocks;
1475 /* In each subblock, record that this is its superior. */
1477 for (link = subblocks; link; link = TREE_CHAIN (link))
1478 BLOCK_SUPERCONTEXT (link) = block;
1480 /* Clear out the meanings of the local variables of this level. */
1482 for (link = decls; link; link = TREE_CHAIN (link))
1484 tree name = DECL_NAME (link);
1485 if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1487 /* If the ident. was used or addressed via a local extern decl,
1488 don't forget that fact. */
1489 if (DECL_EXTERNAL (link))
1491 if (TREE_USED (link))
1492 TREE_USED (name) = 1;
1493 if (TREE_ADDRESSABLE (link))
1494 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1496 IDENTIFIER_LOCAL_VALUE (name) = 0;
1500 /* Restore all name-meanings of the outer levels
1501 that were shadowed by this level. */
1503 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1504 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1506 /* If the level being exited is the top level of a function,
1507 check over all the labels, and clear out the current
1508 (function local) meanings of their names. */
1512 /* If this is the top level block of a function,
1513 the vars are the function's parameters.
1514 Don't leave them in the BLOCK because they are
1515 found in the FUNCTION_DECL instead. */
1517 BLOCK_VARS (block) = 0;
1519 /* Clear out the definitions of all label names,
1520 since their scopes end here,
1521 and add them to BLOCK_VARS. */
1524 for (link = named_labels; link; link = TREE_CHAIN (link))
1526 tree label = TREE_VALUE (link);
1528 if (DECL_INITIAL (label) == 0)
1530 error ("%Jlabel '%D' used but not defined", label, label);
1531 /* Avoid crashing later. */
1532 define_label (input_location, DECL_NAME (label));
1534 else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
1535 warning ("%Jlabel '%D' defined but not used", label, label);
1536 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1538 /* Put the labels into the "variables" of the
1539 top-level block, so debugger can see them. */
1540 TREE_CHAIN (label) = BLOCK_VARS (block);
1541 BLOCK_VARS (block) = label;
1546 /* Pop the current level, and free the structure for reuse. */
1549 struct binding_level *level = current_binding_level;
1550 current_binding_level = current_binding_level->level_chain;
1552 level->level_chain = free_binding_level;
1553 free_binding_level = level;
1556 /* Dispose of the block that we just made inside some higher level. */
1559 DECL_INITIAL (current_function_decl) = block;
1560 DECL_SAVED_TREE (current_function_decl) = bind;
1566 current_binding_level->blocks
1567 = chainon (current_binding_level->blocks, block);
1569 /* If we did not make a block for the level just exited,
1570 any blocks made for inner levels
1571 (since they cannot be recorded as subblocks in that level)
1572 must be carried forward so they will later become subblocks
1573 of something else. */
1575 current_binding_level->blocks
1576 = chainon (current_binding_level->blocks, subblocks);
1579 java_add_stmt (bind);
1583 TREE_USED (block) = 1;
1588 maybe_pushlevels (int pc)
1590 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1594 while (pending_local_decls != NULL_TREE &&
1595 DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1597 tree *ptr = &pending_local_decls;
1598 tree decl = *ptr, next;
1599 int end_pc = DECL_LOCAL_END_PC (decl);
1601 while (*ptr != NULL_TREE
1602 && DECL_LOCAL_START_PC (*ptr) <= pc
1603 && DECL_LOCAL_END_PC (*ptr) == end_pc)
1604 ptr = &TREE_CHAIN (*ptr);
1605 pending_local_decls = *ptr;
1608 /* Force non-nested range to be nested in current range by
1609 truncating variable lifetimes. */
1610 if (end_pc > current_binding_level->end_pc)
1612 end_pc = current_binding_level->end_pc;
1613 DECL_LOCAL_END_PC (decl) = end_pc;
1616 maybe_start_try (pc, end_pc);
1620 current_binding_level->end_pc = end_pc;
1621 current_binding_level->start_pc = pc;
1622 current_binding_level->names = NULL;
1623 for ( ; decl != NULL_TREE; decl = next)
1625 next = TREE_CHAIN (decl);
1626 push_jvm_slot (DECL_LOCAL_SLOT_NUMBER (decl), decl);
1628 initialize_local_variable (decl, DECL_LOCAL_SLOT_NUMBER (decl));
1632 maybe_start_try (pc, 0);
1636 maybe_poplevels (int pc)
1638 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1642 while (current_binding_level->end_pc <= pc)
1646 /* Terminate any binding which began during the range beginning at
1647 start_pc. This tidies up improperly nested local variable ranges
1648 and exception handlers; a variable declared within an exception
1649 range is forcibly terminated when that exception ends. */
1652 force_poplevels (int start_pc)
1654 while (current_binding_level->start_pc > start_pc)
1656 if (pedantic && current_binding_level->start_pc > start_pc)
1657 warning ("%JIn %D: overlapped variable and exception ranges at %d",
1658 current_function_decl, current_function_decl,
1659 current_binding_level->start_pc);
1664 /* Insert BLOCK at the end of the list of subblocks of the
1665 current binding level. This is used when a BIND_EXPR is expanded,
1666 to handle the BLOCK node inside the BIND_EXPR. */
1669 insert_block (tree block)
1671 TREE_USED (block) = 1;
1672 current_binding_level->blocks
1673 = chainon (current_binding_level->blocks, block);
1676 /* integrate_decl_tree calls this function. */
1679 java_dup_lang_specific_decl (tree node)
1682 struct lang_decl *x;
1684 if (!DECL_LANG_SPECIFIC (node))
1687 lang_decl_size = sizeof (struct lang_decl);
1688 x = ggc_alloc (lang_decl_size);
1689 memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1690 DECL_LANG_SPECIFIC (node) = x;
1694 give_name_to_locals (JCF *jcf)
1696 int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1697 int code_offset = DECL_CODE_OFFSET (current_function_decl);
1699 pending_local_decls = NULL_TREE;
1703 n = JCF_readu2 (jcf);
1704 for (i = 0; i < n; i++)
1706 int start_pc = JCF_readu2 (jcf);
1707 int length = JCF_readu2 (jcf);
1708 int name_index = JCF_readu2 (jcf);
1709 int signature_index = JCF_readu2 (jcf);
1710 int slot = JCF_readu2 (jcf);
1711 tree name = get_name_constant (jcf, name_index);
1712 tree type = parse_signature (jcf, signature_index);
1713 if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1715 && length == DECL_CODE_LENGTH (current_function_decl))
1717 tree decl = TREE_VEC_ELT (decl_map, slot);
1718 DECL_NAME (decl) = name;
1719 SET_DECL_ASSEMBLER_NAME (decl, name);
1720 if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1721 warning ("bad type in parameter debug info");
1726 int end_pc = start_pc + length;
1727 tree decl = build_decl (VAR_DECL, name, type);
1728 if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1730 warning ("%Jbad PC range for debug info for local '%D'",
1732 end_pc = DECL_CODE_LENGTH (current_function_decl);
1735 /* Adjust start_pc if necessary so that the local's first
1736 store operation will use the relevant DECL as a
1737 destination. Fore more information, read the leading
1738 comments for expr.c:maybe_adjust_start_pc. */
1739 start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1741 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1742 DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1743 DECL_LOCAL_START_PC (decl) = start_pc;
1745 /* FIXME: The range used internally for exceptions and local
1746 variable ranges, is a half-open interval:
1747 start_pc <= pc < end_pc. However, the range used in the
1748 Java VM spec is inclusive at both ends:
1749 start_pc <= pc <= end_pc. */
1752 DECL_LOCAL_END_PC (decl) = end_pc;
1754 /* Now insert the new decl in the proper place in
1755 pending_local_decls. We are essentially doing an insertion sort,
1756 which works fine, since the list input will normally already
1758 ptr = &pending_local_decls;
1759 while (*ptr != NULL_TREE
1760 && (DECL_LOCAL_START_PC (*ptr) > start_pc
1761 || (DECL_LOCAL_START_PC (*ptr) == start_pc
1762 && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1763 ptr = &TREE_CHAIN (*ptr);
1764 TREE_CHAIN (decl) = *ptr;
1769 pending_local_decls = nreverse (pending_local_decls);
1771 /* Fill in default names for the parameters. */
1772 for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1773 parm != NULL_TREE; parm = TREE_CHAIN (parm), i++)
1775 if (DECL_NAME (parm) == NULL_TREE)
1777 int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1779 DECL_NAME (parm) = get_identifier ("this");
1783 sprintf (buffer, "ARG_%d", arg_i);
1784 DECL_NAME (parm) = get_identifier (buffer);
1786 SET_DECL_ASSEMBLER_NAME (parm, DECL_NAME (parm));
1792 build_result_decl (tree fndecl)
1794 tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1795 tree result = DECL_RESULT (fndecl);
1798 /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1799 if (INTEGRAL_TYPE_P (restype)
1800 && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
1801 restype = integer_type_node;
1802 result = build_decl (RESULT_DECL, NULL_TREE, restype);
1803 DECL_ARTIFICIAL (result) = 1;
1804 DECL_IGNORED_P (result) = 1;
1805 DECL_CONTEXT (result) = fndecl;
1806 DECL_RESULT (fndecl) = result;
1812 start_java_method (tree fndecl)
1819 current_function_decl = fndecl;
1820 announce_function (fndecl);
1822 i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1823 decl_map = make_tree_vec (i);
1824 base_decl_map = make_tree_vec (i);
1825 type_map = xrealloc (type_map, i * sizeof (tree));
1827 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1828 fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
1830 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1831 pushlevel (1); /* Push parameters. */
1833 ptr = &DECL_ARGUMENTS (fndecl);
1834 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1835 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
1837 tree parm_name = NULL_TREE, parm_decl;
1838 tree parm_type = TREE_VALUE (tem);
1839 if (i >= DECL_MAX_LOCALS (fndecl))
1842 parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
1843 DECL_CONTEXT (parm_decl) = fndecl;
1844 if (targetm.calls.promote_prototypes (parm_type)
1845 && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
1846 && INTEGRAL_TYPE_P (parm_type))
1847 parm_type = integer_type_node;
1848 DECL_ARG_TYPE (parm_decl) = parm_type;
1851 ptr = &TREE_CHAIN (parm_decl);
1853 /* Add parm_decl to the decl_map. */
1854 push_jvm_slot (i, parm_decl);
1856 type_map[i] = TREE_TYPE (parm_decl);
1857 if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
1860 type_map[i] = void_type_node;
1864 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
1866 while (i < DECL_MAX_LOCALS(fndecl))
1867 type_map[i++] = NULL_TREE;
1869 build_result_decl (fndecl);
1871 /* Push local variables. */
1874 function_binding_level = current_binding_level;
1878 end_java_method (void)
1880 tree fndecl = current_function_decl;
1882 /* pop out of function */
1885 /* pop out of its parameters */
1888 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
1890 flag_unit_at_a_time = 0;
1891 finish_method (fndecl);
1893 if (! flag_unit_at_a_time)
1895 /* Nulling these fields when we no longer need them saves
1897 DECL_SAVED_TREE (fndecl) = NULL;
1898 DECL_STRUCT_FUNCTION (fndecl) = NULL;
1899 DECL_INITIAL (fndecl) = NULL_TREE;
1901 if (! flag_unit_at_a_time)
1903 /* Nulling these fields when we no longer need them saves
1905 DECL_SAVED_TREE (fndecl) = NULL;
1906 DECL_STRUCT_FUNCTION (fndecl) = NULL;
1907 DECL_INITIAL (fndecl) = NULL_TREE;
1909 current_function_decl = NULL_TREE;
1912 /* Prepare a method for expansion. */
1915 finish_method (tree fndecl)
1917 tree *tp = &DECL_SAVED_TREE (fndecl);
1919 /* Wrap body of synchronized methods in a monitorenter,
1920 plus monitorexit cleanup. */
1921 if (METHOD_SYNCHRONIZED (fndecl))
1923 tree enter, exit, lock;
1924 if (METHOD_STATIC (fndecl))
1925 lock = build_class_ref (DECL_CONTEXT (fndecl));
1927 lock = DECL_ARGUMENTS (fndecl);
1928 BUILD_MONITOR_ENTER (enter, lock);
1929 BUILD_MONITOR_EXIT (exit, lock);
1930 *tp = build2 (COMPOUND_EXPR, void_type_node, enter,
1931 build2 (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
1934 /* Prepend class initialization for static methods reachable from
1936 if (METHOD_STATIC (fndecl) && ! METHOD_PRIVATE (fndecl)
1937 && ! DECL_CLINIT_P (fndecl)
1938 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
1940 tree clas = DECL_CONTEXT (fndecl);
1941 tree init = build3 (CALL_EXPR, void_type_node,
1942 build_address_of (soft_initclass_node),
1943 build_tree_list (NULL_TREE, build_class_ref (clas)),
1945 *tp = build2 (COMPOUND_EXPR, TREE_TYPE (*tp), init, *tp);
1948 /* Convert function tree to GENERIC prior to inlining. */
1949 java_genericize (fndecl);
1951 /* Store the end of the function, so that we get good line number
1952 info for the epilogue. */
1953 if (DECL_STRUCT_FUNCTION (fndecl))
1954 cfun = DECL_STRUCT_FUNCTION (fndecl);
1956 allocate_struct_function (fndecl);
1957 cfun->function_end_locus.file = DECL_SOURCE_FILE (fndecl);
1958 cfun->function_end_locus.line = DECL_FUNCTION_LAST_LINE (fndecl);
1960 /* Defer inlining and expansion to the cgraph optimizers. */
1961 cgraph_finalize_function (fndecl, false);
1964 /* Optimize and expand a function's entire body. */
1967 java_expand_body (tree fndecl)
1969 tree_rest_of_compilation (fndecl, 0);
1972 /* We pessimistically marked all methods and fields external until we
1973 knew what set of classes we were planning to compile. Now mark those
1974 associated with CLASS to be generated locally as not external. */
1977 java_mark_decl_local (tree decl)
1979 DECL_EXTERNAL (decl) = 0;
1981 /* If we've already constructed DECL_RTL, give encode_section_info
1982 a second chance, now that we've changed the flags. */
1983 if (DECL_RTL_SET_P (decl))
1984 make_decl_rtl (decl);
1988 java_mark_class_local (tree class)
1992 for (t = TYPE_FIELDS (class); t ; t = TREE_CHAIN (t))
1993 if (FIELD_STATIC (t))
1994 java_mark_decl_local (t);
1996 for (t = TYPE_METHODS (class); t ; t = TREE_CHAIN (t))
1997 if (!METHOD_ABSTRACT (t) && (!METHOD_NATIVE (t) || flag_jni))
1998 java_mark_decl_local (t);
2001 /* Add a statement to a compound_expr. */
2004 add_stmt_to_compound (tree existing, tree type, tree stmt)
2010 tree expr = build2 (COMPOUND_EXPR, type, existing, stmt);
2011 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (existing)
2012 | TREE_SIDE_EFFECTS (stmt);
2019 /* Add a statement to the compound_expr currently being
2023 java_add_stmt (tree stmt)
2026 annotate_with_locus (stmt, input_location);
2028 return current_binding_level->stmts
2029 = add_stmt_to_compound (current_binding_level->stmts,
2030 TREE_TYPE (stmt), stmt);
2033 /* Add a variable to the current scope. */
2036 java_add_local_var (tree decl)
2038 tree *vars = ¤t_binding_level->names;
2040 TREE_CHAIN (decl) = next;
2042 DECL_CONTEXT (decl) = current_function_decl;
2043 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2047 /* Return a pointer to the compound_expr currently being
2053 return ¤t_binding_level->stmts;
2056 /* Register an exception range as belonging to the current binding
2057 level. There may only be one: if there are more, we'll create more
2058 binding levels. However, each range can have multiple handlers,
2059 and these are expanded when we call expand_end_java_handler(). */
2062 register_exception_range (struct eh_range *range, int pc, int end_pc)
2064 if (current_binding_level->exception_range)
2066 current_binding_level->exception_range = range;
2067 current_binding_level->end_pc = end_pc;
2068 current_binding_level->start_pc = pc;
2071 #include "gt-java-decl.h"