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_named_variable (tree, tree, int, int *);
61 static tree check_local_unnamed_variable (tree, tree, tree);
63 /* Name of the Cloneable class. */
64 tree java_lang_cloneable_identifier_node;
66 /* Name of the Serializable class. */
67 tree java_io_serializable_identifier_node;
69 /* The DECL_MAP is a mapping from (index, type) to a decl node.
70 If index < max_locals, it is the index of a local variable.
71 if index >= max_locals, then index-max_locals is a stack slot.
72 The DECL_MAP mapping is represented as a TREE_VEC whose elements
73 are a list of decls (VAR_DECL or PARM_DECL) chained by
74 DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
75 we search the chain for a decl with a matching TREE_TYPE. */
77 static GTY(()) tree decl_map;
79 /* A list of local variables VAR_DECLs for this method that we have seen
80 debug information, but we have not reached their starting (byte) PC yet. */
82 static GTY(()) tree pending_local_decls;
84 /* Push a local variable or stack slot into the decl_map,
85 and assign it an rtl. */
87 #if defined(DEBUG_JAVA_BINDING_LEVELS)
88 int binding_depth = 0;
89 int is_class_level = 0;
97 for (i = 0; i < binding_depth*2; i++)
100 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
102 /* Copy the value in decl into every alias in the same local variable
105 update_aliases (tree decl, int index)
107 tree tmp = TREE_VEC_ELT (decl_map, index);
108 tree type = TREE_TYPE (decl);
109 while (tmp != NULL_TREE)
112 && ! LOCAL_VAR_OUT_OF_SCOPE_P (tmp)
113 && TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (tmp)))
115 tree src = build1 (NOP_EXPR, TREE_TYPE (tmp), decl);
117 (build (MODIFY_EXPR, TREE_TYPE (tmp), tmp, src));
119 tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
124 push_jvm_slot (int index, tree decl)
126 tree type = TREE_TYPE (decl);
129 DECL_CONTEXT (decl) = current_function_decl;
130 layout_decl (decl, 0);
132 /* Look for another variable of the same mode in this slot. */
133 tmp = TREE_VEC_ELT (decl_map, index);
134 while (tmp != NULL_TREE)
136 if (! LOCAL_VAR_OUT_OF_SCOPE_P (tmp)
137 && TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (tmp)))
139 /* At the point of its creation this decl inherits whatever
141 tree src = build1 (NOP_EXPR, TREE_TYPE (decl), tmp);
143 (build (MODIFY_EXPR, TREE_TYPE (decl), decl, src));
146 tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
149 /* Now link the decl into the decl_map. */
150 if (DECL_LANG_SPECIFIC (decl) == NULL)
152 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
153 DECL_LOCAL_START_PC (decl) = 0;
154 DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
155 DECL_LOCAL_SLOT_NUMBER (decl) = index;
157 DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
158 TREE_VEC_ELT (decl_map, index) = decl;
160 if (TREE_CODE (decl) != PARM_DECL)
165 /* Find out if 'decl' passed in fits the defined PC location better than
166 'best'. Return decl if it does, return best if it doesn't. If decl
167 is returned, then updated is set to true. */
170 check_local_named_variable (tree best, tree decl, int pc, int *updated)
172 if (pc >= DECL_LOCAL_START_PC (decl)
173 && pc < DECL_LOCAL_END_PC (decl))
175 if (best == NULL_TREE
176 || (DECL_LOCAL_START_PC (decl) > DECL_LOCAL_START_PC (best)
177 && DECL_LOCAL_END_PC (decl) < DECL_LOCAL_END_PC (best)))
187 /* Find the best declaration based upon type. If 'decl' fits 'type' better
188 than 'best', return 'decl'. Otherwise return 'best'. */
191 check_local_unnamed_variable (tree best, tree decl, tree type)
193 if (TREE_TYPE (decl) == type
194 || (TREE_CODE (TREE_TYPE (decl)) == TREE_CODE (type)
195 && TYPE_PRECISION (TREE_TYPE (decl)) <= 32
196 && TYPE_PRECISION (type) <= 32
197 && TREE_CODE (type) != POINTER_TYPE)
198 || (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
199 && type == ptr_type_node))
201 if (best == NULL_TREE
202 || (TREE_TYPE (decl) == type && TREE_TYPE (best) != type))
210 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
211 that is valid at PC (or -1 if any pc).
212 If there is no existing matching decl, allocate one. */
215 find_local_variable (int index, tree type, int pc)
217 tree decl = TREE_VEC_ELT (decl_map, index);
218 tree best = NULL_TREE;
219 int found_scoped_var = 0;
221 /* Scan through every declaration that has been created in this slot. */
222 while (decl != NULL_TREE)
224 bool has_name = false;
225 tree name = DECL_NAME (decl);
226 if (name && IDENTIFIER_POINTER (name))
227 has_name = IDENTIFIER_POINTER (name)[0] != '#';
229 /* Variables created in give_name_to_locals() have a name and have
230 a specified scope, so we can handle them specifically. We want
231 to use the specific decls created for those so they are assigned
232 the right variables in the debugging information. */
235 /* This is a variable we have a name for, so it has a scope
236 supplied in the class file. But it only matters when we
237 actually have a PC to use. If pc<0, then we are asking
238 for a stack slot and this decl won't be one of those. */
240 best = check_local_named_variable (best, decl, pc,
243 /* We scan for type information unless we found a variable in the
244 proper scope already. */
245 else if (!found_scoped_var)
247 /* If we don't have scoping information for a variable, we use
248 a different method to look it up. */
249 best = check_local_unnamed_variable (best, decl, type);
252 decl = DECL_LOCAL_SLOT_CHAIN (decl);
255 if (best != NULL_TREE)
258 /* If we don't find a match, create one with the type passed in.
259 Ths name of the variable is #n#m, which n is the variable index
260 in the local variable area and m is a dummy identifier for
261 uniqueness -- multiple variables may share the same local
267 sprintf (buf, "#%d#%d", index, uniq++);
268 name = get_identifier (buf);
270 return push_jvm_slot (index, build_decl (VAR_DECL, name, type));
275 /* Same as find_local_index, except that INDEX is a stack index. */
278 find_stack_slot (int index, tree type)
280 return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
284 struct binding_level GTY(())
286 /* A chain of _DECL nodes for all variables, constants, functions,
287 * and typedef types. These are in the reverse of the order supplied.
291 /* For each level, a list of shadowed outer-level local definitions
292 to be restored when this level is popped.
293 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
294 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
297 /* For each level (except not the global one),
298 a chain of BLOCK nodes for all the levels
299 that were entered and exited one level down. */
302 /* The binding level which this one is contained in (inherits from). */
303 struct binding_level *level_chain;
305 /* The bytecode PC that marks the end of this level. */
307 /* The bytecode PC that marks the start of this level. */
310 /* The statements in this binding level. */
313 /* An exception range associated with this binding level. */
314 struct eh_range * GTY((skip (""))) exception_range;
316 /* Binding depth at which this level began. Used only for debugging. */
317 unsigned binding_depth;
320 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
322 /* The binding level currently in effect. */
324 static GTY(()) struct binding_level *current_binding_level;
326 /* A chain of binding_level structures awaiting reuse. */
328 static GTY(()) struct binding_level *free_binding_level;
330 /* The outermost binding level, for names of file scope.
331 This is created when the compiler is started and exists
332 through the entire run. */
334 static GTY(()) struct binding_level *global_binding_level;
336 /* A PC value bigger than any PC value we may ever may encounter. */
338 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
340 /* Binding level structures are initialized by copying this one. */
342 static const struct binding_level clear_binding_level
344 NULL_TREE, /* names */
345 NULL_TREE, /* shadowed */
346 NULL_TREE, /* blocks */
347 NULL_BINDING_LEVEL, /* level_chain */
348 LARGEST_PC, /* end_pc */
351 NULL, /* exception_range */
352 0, /* binding_depth */
356 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
357 that have names. Here so we can clear out their names' definitions
358 at the end of the function. */
360 static tree named_labels;
362 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
364 static tree shadowed_labels;
367 tree java_global_trees[JTI_MAX];
369 /* Build (and pushdecl) a "promoted type" for all standard
370 types shorter than int. */
373 push_promoted_type (const char *name, tree actual_type)
375 tree type = make_node (TREE_CODE (actual_type));
377 tree in_min = TYPE_MIN_VALUE (int_type_node);
378 tree in_max = TYPE_MAX_VALUE (int_type_node);
380 tree in_min = TYPE_MIN_VALUE (actual_type);
381 tree in_max = TYPE_MAX_VALUE (actual_type);
383 TYPE_MIN_VALUE (type) = copy_node (in_min);
384 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
385 TYPE_MAX_VALUE (type) = copy_node (in_max);
386 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
387 TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
389 pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
393 /* Return a definition for a builtin function named NAME and whose data type
394 is TYPE. TYPE should be a function type with argument types.
395 FUNCTION_CODE tells later passes how to compile calls to this function.
396 See tree.h for its possible values.
398 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
399 the name to be called if we can't opencode the function. If
400 ATTRS is nonzero, use that for the function's attribute list. */
403 builtin_function (const char *name,
406 enum built_in_class class,
407 const char *library_name,
408 tree attrs ATTRIBUTE_UNUSED)
410 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
411 DECL_EXTERNAL (decl) = 1;
412 TREE_PUBLIC (decl) = 1;
414 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
415 make_decl_rtl (decl, NULL);
417 DECL_BUILT_IN_CLASS (decl) = class;
418 DECL_FUNCTION_CODE (decl) = function_code;
422 /* Return tree that represents a vtable for a primitive array. */
424 create_primitive_vtable (const char *name)
429 sprintf (buf, "_Jv_%sVTable", name);
430 r = build_decl (VAR_DECL, get_identifier (buf), ptr_type_node);
431 DECL_EXTERNAL (r) = 1;
443 java_init_decl_processing (void)
446 tree field = NULL_TREE;
449 init_class_processing ();
451 current_function_decl = NULL;
452 current_binding_level = NULL_BINDING_LEVEL;
453 free_binding_level = NULL_BINDING_LEVEL;
454 pushlevel (0); /* make the binding_level structure for global names */
455 global_binding_level = current_binding_level;
457 /* The code here must be similar to build_common_tree_nodes{,_2} in
458 tree.c, especially as to the order of initializing common nodes. */
459 error_mark_node = make_node (ERROR_MARK);
460 TREE_TYPE (error_mark_node) = error_mark_node;
462 /* Create sizetype first - needed for other types. */
463 initialize_sizetypes ();
465 byte_type_node = make_signed_type (8);
466 pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
467 short_type_node = make_signed_type (16);
468 pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
469 int_type_node = make_signed_type (32);
470 pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
471 long_type_node = make_signed_type (64);
472 pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));
474 unsigned_byte_type_node = make_unsigned_type (8);
475 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
476 unsigned_byte_type_node));
477 unsigned_short_type_node = make_unsigned_type (16);
478 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
479 unsigned_short_type_node));
480 unsigned_int_type_node = make_unsigned_type (32);
481 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
482 unsigned_int_type_node));
483 unsigned_long_type_node = make_unsigned_type (64);
484 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
485 unsigned_long_type_node));
487 /* This is not a java type, however tree-dfa requires a definition for
489 size_type_node = make_unsigned_type (POINTER_SIZE);
490 set_sizetype (size_type_node);
492 /* Define these next since types below may used them. */
493 integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
494 integer_zero_node = build_int_2 (0, 0);
495 integer_one_node = build_int_2 (1, 0);
496 integer_two_node = build_int_2 (2, 0);
497 integer_four_node = build_int_2 (4, 0);
498 integer_minus_one_node = build_int_2 (-1, -1);
500 /* A few values used for range checking in the lexer. */
501 decimal_int_max = build_int_2 (0x80000000, 0);
502 TREE_TYPE (decimal_int_max) = unsigned_int_type_node;
503 #if HOST_BITS_PER_WIDE_INT == 64
504 decimal_long_max = build_int_2 (0x8000000000000000LL, 0);
506 #if HOST_BITS_PER_WIDE_INT == 32
507 decimal_long_max = build_int_2 (0, 0x80000000);
509 #error "unsupported size"
512 TREE_TYPE (decimal_long_max) = unsigned_long_type_node;
514 size_zero_node = size_int (0);
515 size_one_node = size_int (1);
516 bitsize_zero_node = bitsize_int (0);
517 bitsize_one_node = bitsize_int (1);
518 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
520 long_zero_node = build_int_2 (0, 0);
521 TREE_TYPE (long_zero_node) = long_type_node;
523 void_type_node = make_node (VOID_TYPE);
524 pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
525 layout_type (void_type_node); /* Uses size_zero_node */
526 ptr_type_node = build_pointer_type (void_type_node);
527 t = make_node (VOID_TYPE);
528 layout_type (t); /* Uses size_zero_node */
529 return_address_type_node = build_pointer_type (t);
531 null_pointer_node = build_int_2 (0, 0);
532 TREE_TYPE (null_pointer_node) = ptr_type_node;
535 /* Make a type to be the domain of a few array types
536 whose domains don't really matter.
537 200 is small enough that it always fits in size_t
538 and large enough that it can hold most function names for the
539 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
540 short_array_type_node = build_prim_array_type (short_type_node, 200);
542 char_type_node = make_node (CHAR_TYPE);
543 TYPE_PRECISION (char_type_node) = 16;
544 fixup_unsigned_type (char_type_node);
545 pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));
547 boolean_type_node = make_node (BOOLEAN_TYPE);
548 TYPE_PRECISION (boolean_type_node) = 1;
549 fixup_unsigned_type (boolean_type_node);
550 pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
552 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
553 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
555 promoted_byte_type_node
556 = push_promoted_type ("promoted_byte", byte_type_node);
557 promoted_short_type_node
558 = push_promoted_type ("promoted_short", short_type_node);
559 promoted_char_type_node
560 = push_promoted_type ("promoted_char", char_type_node);
561 promoted_boolean_type_node
562 = push_promoted_type ("promoted_boolean", boolean_type_node);
564 float_type_node = make_node (REAL_TYPE);
565 TYPE_PRECISION (float_type_node) = 32;
566 pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
568 layout_type (float_type_node);
570 double_type_node = make_node (REAL_TYPE);
571 TYPE_PRECISION (double_type_node) = 64;
572 pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
574 layout_type (double_type_node);
576 float_zero_node = build_real (float_type_node, dconst0);
577 double_zero_node = build_real (double_type_node, dconst0);
579 /* These are the vtables for arrays of primitives. */
580 boolean_array_vtable = create_primitive_vtable ("boolean");
581 byte_array_vtable = create_primitive_vtable ("byte");
582 char_array_vtable = create_primitive_vtable ("char");
583 short_array_vtable = create_primitive_vtable ("short");
584 int_array_vtable = create_primitive_vtable ("int");
585 long_array_vtable = create_primitive_vtable ("long");
586 float_array_vtable = create_primitive_vtable ("float");
587 double_array_vtable = create_primitive_vtable ("double");
589 one_elt_array_domain_type = build_index_type (integer_one_node);
590 utf8const_type = make_node (RECORD_TYPE);
591 PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
592 PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
593 FINISH_RECORD (utf8const_type);
594 utf8const_ptr_type = build_pointer_type (utf8const_type);
596 atable_type = build_array_type (ptr_type_node,
597 one_elt_array_domain_type);
598 TYPE_NONALIASED_COMPONENT (atable_type) = 1;
599 atable_ptr_type = build_pointer_type (atable_type);
601 symbol_type = make_node (RECORD_TYPE);
602 PUSH_FIELD (symbol_type, field, "clname", utf8const_ptr_type);
603 PUSH_FIELD (symbol_type, field, "name", utf8const_ptr_type);
604 PUSH_FIELD (symbol_type, field, "signature", utf8const_ptr_type);
605 FINISH_RECORD (symbol_type);
607 symbols_array_type = build_array_type (symbol_type,
608 one_elt_array_domain_type);
609 symbols_array_ptr_type = build_pointer_type (symbols_array_type);
611 /* As you're adding items here, please update the code right after
612 this section, so that the filename containing the source code of
613 the pre-defined class gets registered correctly. */
614 unqualified_object_id_node = get_identifier ("Object");
615 object_type_node = lookup_class (get_identifier ("java.lang.Object"));
616 object_ptr_type_node = promote_type (object_type_node);
617 string_type_node = lookup_class (get_identifier ("java.lang.String"));
618 string_ptr_type_node = promote_type (string_type_node);
619 class_type_node = lookup_class (get_identifier ("java.lang.Class"));
620 throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
621 exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
622 runtime_exception_type_node =
623 lookup_class (get_identifier ("java.lang.RuntimeException"));
624 error_exception_type_node =
625 lookup_class (get_identifier ("java.lang.Error"));
627 rawdata_ptr_type_node
628 = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
630 add_predefined_file (get_identifier ("java/lang/Class.java"));
631 add_predefined_file (get_identifier ("java/lang/Error.java"));
632 add_predefined_file (get_identifier ("java/lang/Object.java"));
633 add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
634 add_predefined_file (get_identifier ("java/lang/String.java"));
635 add_predefined_file (get_identifier ("java/lang/Throwable.java"));
636 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
637 add_predefined_file (get_identifier ("java/lang/Exception.java"));
638 add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
639 add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
641 methodtable_type = make_node (RECORD_TYPE);
642 layout_type (methodtable_type);
643 build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
644 methodtable_ptr_type = build_pointer_type (methodtable_type);
646 TYPE_identifier_node = get_identifier ("TYPE");
647 init_identifier_node = get_identifier ("<init>");
648 clinit_identifier_node = get_identifier ("<clinit>");
649 finit_identifier_node = get_identifier ("finit$");
650 instinit_identifier_node = get_identifier ("instinit$");
651 void_signature_node = get_identifier ("()V");
652 length_identifier_node = get_identifier ("length");
653 finalize_identifier_node = get_identifier ("finalize");
654 this_identifier_node = get_identifier ("this");
655 super_identifier_node = get_identifier ("super");
656 continue_identifier_node = get_identifier ("continue");
657 access0_identifier_node = get_identifier ("access$0");
658 classdollar_identifier_node = get_identifier ("class$");
660 java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
661 java_io_serializable_identifier_node =
662 get_identifier ("java.io.Serializable");
664 /* for lack of a better place to put this stub call */
665 init_expr_processing();
667 constants_type_node = make_node (RECORD_TYPE);
668 PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
669 PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
670 PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
671 FINISH_RECORD (constants_type_node);
672 build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
674 access_flags_type_node = unsigned_short_type_node;
676 dtable_type = make_node (RECORD_TYPE);
677 dtable_ptr_type = build_pointer_type (dtable_type);
679 otable_type = build_array_type (integer_type_node,
680 one_elt_array_domain_type);
681 TYPE_NONALIASED_COMPONENT (otable_type) = 1;
682 otable_ptr_type = build_pointer_type (otable_type);
684 PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
685 DECL_FCONTEXT (field) = object_type_node;
686 TYPE_VFIELD (object_type_node) = field;
688 /* This isn't exactly true, but it is what we have in the source.
689 There is an unresolved issue here, which is whether the vtable
690 should be marked by the GC. */
691 if (! flag_hash_synchronization)
692 PUSH_FIELD (object_type_node, field, "sync_info",
693 build_pointer_type (object_type_node));
694 for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
695 FIELD_PRIVATE (t) = 1;
696 FINISH_RECORD (object_type_node);
698 field_type_node = make_node (RECORD_TYPE);
699 field_ptr_type_node = build_pointer_type (field_type_node);
700 method_type_node = make_node (RECORD_TYPE);
701 method_ptr_type_node = build_pointer_type (method_type_node);
703 set_super_info (0, class_type_node, object_type_node, 0);
704 set_super_info (0, string_type_node, object_type_node, 0);
705 class_ptr_type = build_pointer_type (class_type_node);
707 PUSH_FIELD (class_type_node, field, "next", class_ptr_type);
708 PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
709 PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
710 PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
711 PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
712 PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
713 PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
714 PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
715 PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
716 PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
717 PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
718 PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
719 PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
720 PUSH_FIELD (class_type_node, field, "otable", otable_ptr_type);
721 PUSH_FIELD (class_type_node, field, "otable_syms",
722 symbols_array_ptr_type);
723 PUSH_FIELD (class_type_node, field, "atable", atable_ptr_type);
724 PUSH_FIELD (class_type_node, field, "atable_syms",
725 symbols_array_ptr_type);
726 PUSH_FIELD (class_type_node, field, "catch_classes", ptr_type_node);
727 PUSH_FIELD (class_type_node, field, "interfaces",
728 build_pointer_type (class_ptr_type));
729 PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
730 PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
731 PUSH_FIELD (class_type_node, field, "state", byte_type_node);
732 PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
733 PUSH_FIELD (class_type_node, field, "depth", short_type_node);
734 PUSH_FIELD (class_type_node, field, "ancestors", ptr_type_node);
735 PUSH_FIELD (class_type_node, field, "idt", ptr_type_node);
736 PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);
737 PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
738 PUSH_FIELD (class_type_node, field, "hack_signers", ptr_type_node);
739 PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
740 PUSH_FIELD (class_type_node, field, "aux_info", ptr_type_node);
741 for (t = TYPE_FIELDS (class_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
742 FIELD_PRIVATE (t) = 1;
743 push_super_field (class_type_node, object_type_node);
745 FINISH_RECORD (class_type_node);
746 build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
748 field_info_union_node = make_node (UNION_TYPE);
749 PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
750 PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
752 PUSH_FIELD (field_info_union_node, field, "idx", unsigned_short_type_node);
754 layout_type (field_info_union_node);
756 PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
757 PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
758 PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
759 PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
760 PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
761 FINISH_RECORD (field_type_node);
762 build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
764 nativecode_ptr_array_type_node
765 = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
767 PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
768 PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
769 FINISH_RECORD (dtable_type);
770 build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
772 #define jint_type int_type_node
773 #define jint_ptr_type ptr_type_node
775 jexception_type = make_node (RECORD_TYPE);
776 PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
777 PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
778 PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
779 PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
780 FINISH_RECORD (jexception_type);
781 build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
782 jexception_ptr_type = build_pointer_type (jexception_type);
784 lineNumberEntry_type = make_node (RECORD_TYPE);
785 PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
786 PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
787 FINISH_RECORD (lineNumberEntry_type);
789 lineNumbers_type = make_node (RECORD_TYPE);
790 PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
791 FINISH_RECORD (lineNumbers_type);
793 #define instn_ptr_type_node ptr_type_node /* XXX JH */
795 #define lineNumbers_ptr_type_node build_pointer_type(lineNumbers_type)
797 PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
798 PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
799 PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
800 PUSH_FIELD (method_type_node, field, "index", unsigned_short_type_node);
801 PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
802 PUSH_FIELD (method_type_node, field, "throws", ptr_type_node);
803 FINISH_RECORD (method_type_node);
804 build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
806 endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
808 t = tree_cons (NULL_TREE, class_ptr_type,
809 tree_cons (NULL_TREE, int_type_node, endlink));
810 alloc_object_node = builtin_function ("_Jv_AllocObject",
811 build_function_type (ptr_type_node, t),
812 0, NOT_BUILT_IN, NULL, NULL_TREE);
813 DECL_IS_MALLOC (alloc_object_node) = 1;
814 alloc_no_finalizer_node =
815 builtin_function ("_Jv_AllocObjectNoFinalizer",
816 build_function_type (ptr_type_node, t),
817 0, NOT_BUILT_IN, NULL, NULL_TREE);
818 DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
820 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
821 soft_initclass_node = builtin_function ("_Jv_InitClass",
822 build_function_type (void_type_node,
824 0, NOT_BUILT_IN, NULL, NULL_TREE);
826 throw_node = builtin_function ("_Jv_Throw",
827 build_function_type (ptr_type_node, t),
828 0, NOT_BUILT_IN, NULL, NULL_TREE);
829 /* Mark throw_nodes as `noreturn' functions with side effects. */
830 TREE_THIS_VOLATILE (throw_node) = 1;
831 TREE_SIDE_EFFECTS (throw_node) = 1;
833 t = build_function_type (int_type_node, endlink);
834 soft_monitorenter_node
835 = builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
837 soft_monitorexit_node
838 = builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
841 t = tree_cons (NULL_TREE, int_type_node,
842 tree_cons (NULL_TREE, int_type_node, endlink));
844 = builtin_function ("_Jv_NewPrimArray",
845 build_function_type(ptr_type_node, t),
846 0, NOT_BUILT_IN, NULL, NULL_TREE);
847 DECL_IS_MALLOC (soft_newarray_node) = 1;
849 t = tree_cons (NULL_TREE, int_type_node,
850 tree_cons (NULL_TREE, class_ptr_type,
851 tree_cons (NULL_TREE, object_ptr_type_node, endlink)));
853 = builtin_function ("_Jv_NewObjectArray",
854 build_function_type (ptr_type_node, t),
855 0, NOT_BUILT_IN, NULL, NULL_TREE);
856 DECL_IS_MALLOC (soft_anewarray_node) = 1;
858 /* There is no endlink here because _Jv_NewMultiArray is a varargs
860 t = tree_cons (NULL_TREE, ptr_type_node,
861 tree_cons (NULL_TREE, int_type_node, NULL_TREE));
862 soft_multianewarray_node
863 = builtin_function ("_Jv_NewMultiArray",
864 build_function_type (ptr_type_node, t),
865 0, NOT_BUILT_IN, NULL, NULL_TREE);
866 DECL_IS_MALLOC (soft_multianewarray_node) = 1;
868 t = build_function_type (void_type_node,
869 tree_cons (NULL_TREE, int_type_node, endlink));
870 soft_badarrayindex_node
871 = builtin_function ("_Jv_ThrowBadArrayIndex", t,
872 0, NOT_BUILT_IN, NULL, NULL_TREE);
873 /* Mark soft_badarrayindex_node as a `noreturn' function with side
875 TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
876 TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
878 soft_nullpointer_node
879 = builtin_function ("_Jv_ThrowNullPointerException",
880 build_function_type (void_type_node, endlink),
881 0, NOT_BUILT_IN, NULL, NULL_TREE);
882 /* Mark soft_nullpointer_node as a `noreturn' function with side
884 TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
885 TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
887 t = tree_cons (NULL_TREE, class_ptr_type,
888 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
890 = builtin_function ("_Jv_CheckCast",
891 build_function_type (ptr_type_node, t),
892 0, NOT_BUILT_IN, NULL, NULL_TREE);
893 t = tree_cons (NULL_TREE, object_ptr_type_node,
894 tree_cons (NULL_TREE, class_ptr_type, endlink));
896 = builtin_function ("_Jv_IsInstanceOf",
897 build_function_type (boolean_type_node, t),
898 0, NOT_BUILT_IN, NULL, NULL_TREE);
899 t = tree_cons (NULL_TREE, object_ptr_type_node,
900 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
901 soft_checkarraystore_node
902 = builtin_function ("_Jv_CheckArrayStore",
903 build_function_type (void_type_node, t),
904 0, NOT_BUILT_IN, NULL, NULL_TREE);
905 t = tree_cons (NULL_TREE, ptr_type_node,
906 tree_cons (NULL_TREE, ptr_type_node,
907 tree_cons (NULL_TREE, int_type_node, endlink)));
908 soft_lookupinterfacemethod_node
909 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
910 build_function_type (ptr_type_node, t),
911 0, NOT_BUILT_IN, NULL, NULL_TREE);
913 t = tree_cons (NULL_TREE, object_ptr_type_node,
914 tree_cons (NULL_TREE, ptr_type_node,
915 tree_cons (NULL_TREE, ptr_type_node,
916 tree_cons (NULL_TREE, int_type_node,
918 soft_lookupjnimethod_node
919 = builtin_function ("_Jv_LookupJNIMethod",
920 build_function_type (ptr_type_node, t),
921 0, NOT_BUILT_IN, NULL, NULL_TREE);
922 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
923 soft_getjnienvnewframe_node
924 = builtin_function ("_Jv_GetJNIEnvNewFrame",
925 build_function_type (ptr_type_node, t),
926 0, NOT_BUILT_IN, NULL, NULL_TREE);
927 soft_jnipopsystemframe_node
928 = builtin_function ("_Jv_JNI_PopSystemFrame",
929 build_function_type (ptr_type_node, t),
930 0, NOT_BUILT_IN, NULL, NULL_TREE);
933 = builtin_function ("_Jv_divI",
934 build_function_type (int_type_node, t),
935 0, NOT_BUILT_IN, NULL, NULL_TREE);
938 = builtin_function ("_Jv_remI",
939 build_function_type (int_type_node, t),
940 0, NOT_BUILT_IN, NULL, NULL_TREE);
943 = builtin_function ("_Jv_divJ",
944 build_function_type (long_type_node, t),
945 0, NOT_BUILT_IN, NULL, NULL_TREE);
948 = builtin_function ("_Jv_remJ",
949 build_function_type (long_type_node, t),
950 0, NOT_BUILT_IN, NULL, NULL_TREE);
952 /* Initialize variables for except.c. */
953 eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
954 ? "__gcj_personality_sj0"
955 : "__gcj_personality_v0");
957 lang_eh_runtime_type = do_nothing;
961 initialize_builtins ();
962 soft_fmod_node = built_in_decls[BUILT_IN_FMOD];
964 soft_fmodf_node = built_in_decls[BUILT_IN_FMODF];
969 /* Look up NAME in the current binding level and its superiors
970 in the namespace of variables, functions and typedefs.
971 Return a ..._DECL node of some kind representing its definition,
972 or return 0 if it is undefined. */
975 lookup_name (tree name)
978 if (current_binding_level != global_binding_level
979 && IDENTIFIER_LOCAL_VALUE (name))
980 val = IDENTIFIER_LOCAL_VALUE (name);
982 val = IDENTIFIER_GLOBAL_VALUE (name);
986 /* Similar to `lookup_name' but look only at current binding level and
987 the previous one if its the parameter level. */
990 lookup_name_current_level (tree name)
994 if (current_binding_level == global_binding_level)
995 return IDENTIFIER_GLOBAL_VALUE (name);
997 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
1000 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
1001 if (DECL_NAME (t) == name)
1007 /* Use a binding level to record a labeled block declaration */
1010 push_labeled_block (tree lb)
1012 tree name = DECL_NAME (LABELED_BLOCK_LABEL (lb));
1013 struct binding_level *b = current_binding_level;
1014 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1016 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1017 TREE_CHAIN (lb) = b->names;
1019 IDENTIFIER_LOCAL_VALUE (name) = lb;
1022 /* Pop the current binding level, reinstalling values for the previous
1026 pop_labeled_block (void)
1028 struct binding_level *b = current_binding_level;
1029 tree label = b->names;
1030 IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label))) =
1033 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b->shadowed)) =
1034 TREE_VALUE (b->shadowed);
1036 /* Pop the current level, and free the structure for reuse. */
1037 current_binding_level = current_binding_level->level_chain;
1038 b->level_chain = free_binding_level;
1039 free_binding_level = b;
1042 /* Record a decl-node X as belonging to the current lexical scope.
1043 Check for errors (such as an incompatible declaration for the same
1044 name already seen in the same scope).
1046 Returns either X or an old decl for the same name.
1047 If an old decl is returned, it may have been smashed
1048 to agree with what X says. */
1054 tree name = DECL_NAME (x);
1055 struct binding_level *b = current_binding_level;
1057 if (TREE_CODE (x) != TYPE_DECL)
1058 DECL_CONTEXT (x) = current_function_decl;
1061 t = lookup_name_current_level (name);
1062 if (t != 0 && t == error_mark_node)
1063 /* error_mark_node is 0 for a while during initialization! */
1066 error ("%J'%D' used prior to declaration", x, x);
1069 /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1070 to point to the TYPE_DECL.
1071 Since Java does not have typedefs, a type can only have
1072 one (true) name, given by a class, interface, or builtin. */
1073 if (TREE_CODE (x) == TYPE_DECL
1074 && TYPE_NAME (TREE_TYPE (x)) == 0
1075 && TREE_TYPE (x) != error_mark_node)
1077 TYPE_NAME (TREE_TYPE (x)) = x;
1078 TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1081 /* This name is new in its binding level.
1082 Install the new declaration and return it. */
1083 if (b == global_binding_level)
1085 /* Install a global value. */
1087 IDENTIFIER_GLOBAL_VALUE (name) = x;
1091 /* Here to install a non-global value. */
1092 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1093 IDENTIFIER_LOCAL_VALUE (name) = x;
1096 /* Warn if shadowing an argument at the top level of the body. */
1097 if (oldlocal != 0 && !DECL_EXTERNAL (x)
1098 /* This warning doesn't apply to the parms of a nested fcn. */
1099 && ! current_binding_level->parm_flag
1100 /* Check that this is one level down from the parms. */
1101 && current_binding_level->level_chain->parm_flag
1102 /* Check that the decl being shadowed
1103 comes from the parm level, one level up. */
1104 && chain_member (oldlocal, current_binding_level->level_chain->names))
1106 if (TREE_CODE (oldlocal) == PARM_DECL)
1107 pedwarn ("declaration of `%s' shadows a parameter",
1108 IDENTIFIER_POINTER (name));
1110 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
1111 IDENTIFIER_POINTER (name));
1114 /* Maybe warn if shadowing something else. */
1115 else if (warn_shadow && !DECL_EXTERNAL (x)
1116 /* No shadow warnings for internally generated vars. */
1117 && DECL_SOURCE_LINE (x) != 0
1118 /* No shadow warnings for vars made for inlining. */
1119 && ! DECL_FROM_INLINE (x))
1121 const char *warnstring = 0;
1123 if (TREE_CODE (x) == PARM_DECL
1124 && current_binding_level->level_chain->parm_flag)
1125 /* Don't warn about the parm names in function declarator
1126 within a function declarator.
1127 It would be nice to avoid warning in any function
1128 declarator in a declaration, as opposed to a definition,
1129 but there is no way to tell it's not a definition. */
1131 else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
1132 warnstring = "declaration of `%s' shadows a parameter";
1133 else if (oldlocal != 0)
1134 warnstring = "declaration of `%s' shadows previous local";
1135 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1136 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1137 warnstring = "declaration of `%s' shadows global declaration";
1140 warning (warnstring, IDENTIFIER_POINTER (name));
1144 /* If storing a local value, there may already be one (inherited).
1145 If so, record it for restoration when this binding level ends. */
1147 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1151 /* Put decls on list in reverse order.
1152 We will reverse them later if necessary. */
1153 TREE_CHAIN (x) = b->names;
1160 pushdecl_force_head (tree x)
1162 current_binding_level->names = x;
1165 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1168 pushdecl_top_level (tree x)
1171 struct binding_level *b = current_binding_level;
1173 current_binding_level = global_binding_level;
1175 current_binding_level = b;
1179 /* Nonzero if we are currently in the global binding level. */
1182 global_bindings_p (void)
1184 return current_binding_level == global_binding_level;
1187 /* Return the list of declarations of the current level.
1188 Note that this list is in reverse order unless/until
1189 you nreverse it; and when you do nreverse it, you must
1190 store the result back using `storedecls' or you will lose. */
1195 return current_binding_level->names;
1198 /* Create a new `struct binding_level'. */
1200 static struct binding_level *
1201 make_binding_level (void)
1204 return ggc_alloc_cleared (sizeof (struct binding_level));
1208 pushlevel (int unused ATTRIBUTE_UNUSED)
1210 struct binding_level *newlevel = NULL_BINDING_LEVEL;
1213 /* If this is the top level of a function,
1214 just make sure that NAMED_LABELS is 0. */
1216 if (current_binding_level == global_binding_level)
1220 /* Reuse or create a struct for this binding level. */
1222 if (free_binding_level)
1224 newlevel = free_binding_level;
1225 free_binding_level = free_binding_level->level_chain;
1229 newlevel = make_binding_level ();
1232 /* Add this level to the front of the chain (stack) of levels that
1235 *newlevel = clear_binding_level;
1236 newlevel->level_chain = current_binding_level;
1237 current_binding_level = newlevel;
1238 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1239 newlevel->binding_depth = binding_depth;
1241 fprintf (stderr, "push %s level %p pc %d\n",
1242 (is_class_level) ? "class" : "block", newlevel, current_pc);
1245 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1248 /* Exit a binding level.
1249 Pop the level off, and restore the state of the identifier-decl mappings
1250 that were in effect when this level was entered.
1252 If KEEP is nonzero, this level had explicit declarations, so
1253 and create a "block" (a BLOCK node) for the level
1254 to record its declarations and subblocks for symbol table output.
1256 If FUNCTIONBODY is nonzero, this level is the body of a function,
1257 so create a block as if KEEP were set and also clear out all
1260 If REVERSE is nonzero, reverse the order of decls before putting
1261 them into the BLOCK. */
1264 poplevel (int keep, int reverse, int functionbody)
1267 /* The chain of decls was accumulated in reverse order.
1268 Put it into forward order, just for cleanliness. */
1270 tree subblocks = current_binding_level->blocks;
1275 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1278 if (current_binding_level->end_pc != LARGEST_PC)
1279 fprintf (stderr, "pop %s level %p pc %d (end pc %d)\n",
1280 (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1281 current_binding_level->end_pc);
1283 fprintf (stderr, "pop %s level %p pc %d\n",
1284 (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1286 if (is_class_level != (current_binding_level == class_binding_level))
1289 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
1293 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1295 /* Get the decls in the order they were written.
1296 Usually current_binding_level->names is in reverse order.
1297 But parameter decls were previously put in forward order. */
1300 current_binding_level->names
1301 = decls = nreverse (current_binding_level->names);
1303 decls = current_binding_level->names;
1305 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1306 if (TREE_CODE (decl) == VAR_DECL
1307 && DECL_LANG_SPECIFIC (decl) != NULL
1308 && DECL_LOCAL_SLOT_NUMBER (decl))
1309 LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
1311 /* If there were any declarations in that level,
1312 or if this level is a function body,
1313 create a BLOCK to record them for the life of this function. */
1316 if (keep || functionbody)
1318 block = make_node (BLOCK);
1319 TREE_TYPE (block) = void_type_node;
1322 if (current_binding_level->exception_range)
1323 expand_end_java_handler (current_binding_level->exception_range);
1327 /* If any statements have been generated at this level, create a
1328 BIND_EXPR to hold them and copy the variables to it. This
1329 only applies to the bytecode compiler. */
1330 if (current_binding_level->stmts)
1333 tree *var = &BLOCK_VARS (block);
1335 /* Copy decls from names list, ignoring labels. */
1338 tree next = TREE_CHAIN (decl);
1339 if (TREE_CODE (decl) != LABEL_DECL)
1342 var = &TREE_CHAIN (decl);
1348 bind = build (BIND_EXPR, TREE_TYPE (block), BLOCK_VARS (block),
1349 BLOCK_EXPR_BODY (block), block);
1350 BIND_EXPR_BODY (bind) = current_binding_level->stmts;
1352 if (BIND_EXPR_BODY (bind)
1353 && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
1354 TREE_SIDE_EFFECTS (bind) = 1;
1356 /* FIXME: gimplifier brain damage. */
1357 if (BIND_EXPR_BODY (bind) == NULL)
1358 BIND_EXPR_BODY (bind) = build_java_empty_stmt ();
1360 current_binding_level->stmts = NULL;
1364 BLOCK_VARS (block) = decls;
1366 BLOCK_SUBBLOCKS (block) = subblocks;
1369 /* In each subblock, record that this is its superior. */
1371 for (link = subblocks; link; link = TREE_CHAIN (link))
1372 BLOCK_SUPERCONTEXT (link) = block;
1374 /* Clear out the meanings of the local variables of this level. */
1376 for (link = decls; link; link = TREE_CHAIN (link))
1378 tree name = DECL_NAME (link);
1379 if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1381 /* If the ident. was used or addressed via a local extern decl,
1382 don't forget that fact. */
1383 if (DECL_EXTERNAL (link))
1385 if (TREE_USED (link))
1386 TREE_USED (name) = 1;
1387 if (TREE_ADDRESSABLE (link))
1388 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1390 IDENTIFIER_LOCAL_VALUE (name) = 0;
1394 /* Restore all name-meanings of the outer levels
1395 that were shadowed by this level. */
1397 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1398 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1400 /* If the level being exited is the top level of a function,
1401 check over all the labels, and clear out the current
1402 (function local) meanings of their names. */
1406 /* If this is the top level block of a function,
1407 the vars are the function's parameters.
1408 Don't leave them in the BLOCK because they are
1409 found in the FUNCTION_DECL instead. */
1411 BLOCK_VARS (block) = 0;
1413 /* Clear out the definitions of all label names,
1414 since their scopes end here,
1415 and add them to BLOCK_VARS. */
1418 for (link = named_labels; link; link = TREE_CHAIN (link))
1420 tree label = TREE_VALUE (link);
1422 if (DECL_INITIAL (label) == 0)
1424 error ("%Jlabel '%D' used but not defined", label, label);
1425 /* Avoid crashing later. */
1426 define_label (input_location, DECL_NAME (label));
1428 else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
1429 warning ("%Jlabel '%D' defined but not used", label, label);
1430 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1432 /* Put the labels into the "variables" of the
1433 top-level block, so debugger can see them. */
1434 TREE_CHAIN (label) = BLOCK_VARS (block);
1435 BLOCK_VARS (block) = label;
1440 /* Pop the current level, and free the structure for reuse. */
1443 struct binding_level *level = current_binding_level;
1444 current_binding_level = current_binding_level->level_chain;
1446 level->level_chain = free_binding_level;
1447 free_binding_level = level;
1450 /* Dispose of the block that we just made inside some higher level. */
1453 DECL_INITIAL (current_function_decl) = block;
1454 DECL_SAVED_TREE (current_function_decl) = bind;
1460 current_binding_level->blocks
1461 = chainon (current_binding_level->blocks, block);
1463 /* If we did not make a block for the level just exited,
1464 any blocks made for inner levels
1465 (since they cannot be recorded as subblocks in that level)
1466 must be carried forward so they will later become subblocks
1467 of something else. */
1469 current_binding_level->blocks
1470 = chainon (current_binding_level->blocks, subblocks);
1473 java_add_stmt (bind);
1477 TREE_USED (block) = 1;
1482 maybe_pushlevels (int pc)
1484 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1488 while (pending_local_decls != NULL_TREE &&
1489 DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1491 tree *ptr = &pending_local_decls;
1492 tree decl = *ptr, next;
1493 int end_pc = DECL_LOCAL_END_PC (decl);
1495 while (*ptr != NULL_TREE
1496 && DECL_LOCAL_START_PC (*ptr) <= pc
1497 && DECL_LOCAL_END_PC (*ptr) == end_pc)
1498 ptr = &TREE_CHAIN (*ptr);
1499 pending_local_decls = *ptr;
1502 /* Force non-nested range to be nested in current range. */
1503 if (end_pc > current_binding_level->end_pc)
1504 end_pc = current_binding_level->end_pc;
1506 maybe_start_try (pc, end_pc);
1510 current_binding_level->end_pc = end_pc;
1511 current_binding_level->start_pc = pc;
1512 current_binding_level->names = NULL;
1513 for ( ; decl != NULL_TREE; decl = next)
1515 next = TREE_CHAIN (decl);
1516 push_jvm_slot (DECL_LOCAL_SLOT_NUMBER (decl), decl);
1520 maybe_start_try (pc, 0);
1524 maybe_poplevels (int pc)
1526 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1530 while (current_binding_level->end_pc <= pc)
1534 /* Terminate any binding which began during the range beginning at
1535 start_pc. This tidies up improperly nested local variable ranges
1536 and exception handlers; a variable declared within an exception
1537 range is forcibly terminated when that exception ends. */
1540 force_poplevels (int start_pc)
1542 while (current_binding_level->start_pc > start_pc)
1544 if (pedantic && current_binding_level->start_pc > start_pc)
1545 warning ("%JIn %D: overlapped variable and exception ranges at %d",
1546 current_function_decl, current_function_decl,
1547 current_binding_level->start_pc);
1552 /* Insert BLOCK at the end of the list of subblocks of the
1553 current binding level. This is used when a BIND_EXPR is expanded,
1554 to handle the BLOCK node inside the BIND_EXPR. */
1557 insert_block (tree block)
1559 TREE_USED (block) = 1;
1560 current_binding_level->blocks
1561 = chainon (current_binding_level->blocks, block);
1564 /* integrate_decl_tree calls this function. */
1567 java_dup_lang_specific_decl (tree node)
1570 struct lang_decl *x;
1572 if (!DECL_LANG_SPECIFIC (node))
1575 lang_decl_size = sizeof (struct lang_decl);
1576 x = ggc_alloc (lang_decl_size);
1577 memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1578 DECL_LANG_SPECIFIC (node) = x;
1582 give_name_to_locals (JCF *jcf)
1584 int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1585 int code_offset = DECL_CODE_OFFSET (current_function_decl);
1587 pending_local_decls = NULL_TREE;
1591 n = JCF_readu2 (jcf);
1592 for (i = 0; i < n; i++)
1594 int start_pc = JCF_readu2 (jcf);
1595 int length = JCF_readu2 (jcf);
1596 int name_index = JCF_readu2 (jcf);
1597 int signature_index = JCF_readu2 (jcf);
1598 int slot = JCF_readu2 (jcf);
1599 tree name = get_name_constant (jcf, name_index);
1600 tree type = parse_signature (jcf, signature_index);
1601 if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1603 && length == DECL_CODE_LENGTH (current_function_decl))
1605 tree decl = TREE_VEC_ELT (decl_map, slot);
1606 DECL_NAME (decl) = name;
1607 SET_DECL_ASSEMBLER_NAME (decl, name);
1608 if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1609 warning ("bad type in parameter debug info");
1614 int end_pc = start_pc + length;
1615 tree decl = build_decl (VAR_DECL, name, type);
1616 if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1618 warning ("%Jbad PC range for debug info for local '%D'",
1620 end_pc = DECL_CODE_LENGTH (current_function_decl);
1623 /* Adjust start_pc if necessary so that the local's first
1624 store operation will use the relevant DECL as a
1625 destination. Fore more information, read the leading
1626 comments for expr.c:maybe_adjust_start_pc. */
1627 start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1629 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1630 DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1631 DECL_LOCAL_START_PC (decl) = start_pc;
1633 /* FIXME: The range used internally for exceptions and local
1634 variable ranges, is a half-open interval:
1635 start_pc <= pc < end_pc. However, the range used in the
1636 Java VM spec is inclusive at both ends:
1637 start_pc <= pc <= end_pc. */
1640 DECL_LOCAL_END_PC (decl) = end_pc;
1642 /* Now insert the new decl in the proper place in
1643 pending_local_decls. We are essentially doing an insertion sort,
1644 which works fine, since the list input will normally already
1646 ptr = &pending_local_decls;
1647 while (*ptr != NULL_TREE
1648 && (DECL_LOCAL_START_PC (*ptr) > start_pc
1649 || (DECL_LOCAL_START_PC (*ptr) == start_pc
1650 && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1651 ptr = &TREE_CHAIN (*ptr);
1652 TREE_CHAIN (decl) = *ptr;
1657 pending_local_decls = nreverse (pending_local_decls);
1659 /* Fill in default names for the parameters. */
1660 for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1661 parm != NULL_TREE; parm = TREE_CHAIN (parm), i++)
1663 if (DECL_NAME (parm) == NULL_TREE)
1665 int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1667 DECL_NAME (parm) = get_identifier ("this");
1671 sprintf (buffer, "ARG_%d", arg_i);
1672 DECL_NAME (parm) = get_identifier (buffer);
1674 SET_DECL_ASSEMBLER_NAME (parm, DECL_NAME (parm));
1680 build_result_decl (tree fndecl)
1682 tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1683 tree result = DECL_RESULT (fndecl);
1686 /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1687 if (INTEGRAL_TYPE_P (restype)
1688 && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
1689 restype = integer_type_node;
1690 result = build_decl (RESULT_DECL, NULL_TREE, restype);
1691 DECL_CONTEXT (result) = fndecl;
1692 DECL_RESULT (fndecl) = result;
1698 start_java_method (tree fndecl)
1703 current_function_decl = fndecl;
1704 announce_function (fndecl);
1706 i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1707 decl_map = make_tree_vec (i);
1708 type_map = xrealloc (type_map, i * sizeof (tree));
1710 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1711 fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
1713 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1714 pushlevel (1); /* Push parameters. */
1716 ptr = &DECL_ARGUMENTS (fndecl);
1717 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1718 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
1720 tree parm_name = NULL_TREE, parm_decl;
1721 tree parm_type = TREE_VALUE (tem);
1722 if (i >= DECL_MAX_LOCALS (fndecl))
1725 parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
1726 DECL_CONTEXT (parm_decl) = fndecl;
1727 if (targetm.calls.promote_prototypes (parm_type)
1728 && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
1729 && INTEGRAL_TYPE_P (parm_type))
1730 parm_type = integer_type_node;
1731 DECL_ARG_TYPE (parm_decl) = parm_type;
1734 ptr = &TREE_CHAIN (parm_decl);
1736 /* Add parm_decl to the decl_map. */
1737 push_jvm_slot (i, parm_decl);
1739 type_map[i] = TREE_TYPE (parm_decl);
1740 if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
1743 type_map[i] = void_type_node;
1747 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
1749 while (i < DECL_MAX_LOCALS(fndecl))
1750 type_map[i++] = NULL_TREE;
1752 build_result_decl (fndecl);
1754 /* Push local variables. */
1759 end_java_method (void)
1761 tree fndecl = current_function_decl;
1763 /* pop out of function */
1766 /* pop out of its parameters */
1769 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
1771 flag_unit_at_a_time = 0;
1772 finish_method (fndecl);
1774 if (! flag_unit_at_a_time)
1776 /* Nulling these fields when we no longer need them saves
1778 DECL_SAVED_TREE (fndecl) = NULL;
1779 DECL_STRUCT_FUNCTION (fndecl) = NULL;
1780 DECL_INITIAL (fndecl) = NULL_TREE;
1782 current_function_decl = NULL_TREE;
1785 /* Prepare a method for expansion. */
1788 finish_method (tree fndecl)
1790 tree *tp = &DECL_SAVED_TREE (fndecl);
1792 /* Wrap body of synchronized methods in a monitorenter,
1793 plus monitorexit cleanup. */
1794 if (METHOD_SYNCHRONIZED (fndecl))
1796 tree enter, exit, lock;
1797 if (METHOD_STATIC (fndecl))
1798 lock = build_class_ref (DECL_CONTEXT (fndecl));
1800 lock = DECL_ARGUMENTS (fndecl);
1801 BUILD_MONITOR_ENTER (enter, lock);
1802 BUILD_MONITOR_EXIT (exit, lock);
1803 *tp = build (COMPOUND_EXPR, void_type_node,
1805 build (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
1808 /* Prepend class initialization for static methods reachable from
1810 if (METHOD_STATIC (fndecl) && ! METHOD_PRIVATE (fndecl)
1811 && ! DECL_CLINIT_P (fndecl)
1812 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
1814 tree clas = DECL_CONTEXT (fndecl);
1815 tree init = build (CALL_EXPR, void_type_node,
1816 build_address_of (soft_initclass_node),
1817 build_tree_list (NULL_TREE, build_class_ref (clas)),
1819 *tp = build (COMPOUND_EXPR, TREE_TYPE (*tp), init, *tp);
1822 /* Convert function tree to GENERIC prior to inlining. */
1823 java_genericize (fndecl);
1825 /* Store the end of the function, so that we get good line number
1826 info for the epilogue. */
1827 if (DECL_STRUCT_FUNCTION (fndecl))
1828 cfun = DECL_STRUCT_FUNCTION (fndecl);
1830 allocate_struct_function (fndecl);
1831 cfun->function_end_locus.file = DECL_SOURCE_FILE (fndecl);
1832 cfun->function_end_locus.line = DECL_FUNCTION_LAST_LINE (fndecl);
1834 /* Defer inlining and expansion to the cgraph optimizers. */
1835 cgraph_finalize_function (fndecl, false);
1838 /* Optimize and expand a function's entire body. */
1841 java_expand_body (tree fndecl)
1843 tree_rest_of_compilation (fndecl, 0);
1846 /* We pessimistically marked all methods and fields external until we
1847 knew what set of classes we were planning to compile. Now mark those
1848 associated with CLASS to be generated locally as not external. */
1851 java_mark_decl_local (tree decl)
1853 DECL_EXTERNAL (decl) = 0;
1855 /* If we've already constructed DECL_RTL, give encode_section_info
1856 a second chance, now that we've changed the flags. */
1857 if (DECL_RTL_SET_P (decl))
1858 make_decl_rtl (decl, NULL);
1862 java_mark_class_local (tree class)
1866 for (t = TYPE_FIELDS (class); t ; t = TREE_CHAIN (t))
1867 if (FIELD_STATIC (t))
1868 java_mark_decl_local (t);
1870 for (t = TYPE_METHODS (class); t ; t = TREE_CHAIN (t))
1871 if (!METHOD_ABSTRACT (t) && (!METHOD_NATIVE (t) || flag_jni))
1872 java_mark_decl_local (t);
1875 /* Add a statement to a compound_expr. */
1878 add_stmt_to_compound (tree existing, tree type, tree stmt)
1884 tree expr = build (COMPOUND_EXPR, type, existing, stmt);
1885 TREE_SIDE_EFFECTS (expr)
1886 = TREE_SIDE_EFFECTS (existing) | TREE_SIDE_EFFECTS (stmt);
1893 /* Add a statement to the compound_expr currently being
1897 java_add_stmt (tree stmt)
1900 annotate_with_locus (stmt, input_location);
1902 return current_binding_level->stmts
1903 = add_stmt_to_compound (current_binding_level->stmts,
1904 TREE_TYPE (stmt), stmt);
1907 /* Add a variable to the current scope. */
1910 java_add_local_var (tree decl)
1912 tree *vars = ¤t_binding_level->names;
1914 TREE_CHAIN (decl) = next;
1916 DECL_CONTEXT (decl) = current_function_decl;
1917 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1921 /* Return a pointer to the compound_expr currently being
1927 return ¤t_binding_level->stmts;
1930 /* Register an exception range as belonging to the current binding
1931 level. There may only be one: if there are more, we'll create more
1932 binding levels. However, each range can have multiple handlers,
1933 and these are expanded when we call expand_end_java_handler(). */
1936 register_exception_range (struct eh_range *range, int pc, int end_pc)
1938 if (current_binding_level->exception_range)
1940 current_binding_level->exception_range = range;
1941 current_binding_level->end_pc = end_pc;
1942 current_binding_level->start_pc = pc;
1945 #include "gt-java-decl.h"