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 BLOCK node for this level, if one has been preallocated.
303 If 0, the BLOCK is allocated (if needed) when the level is popped. */
306 /* The binding level which this one is contained in (inherits from). */
307 struct binding_level
*level_chain
;
309 /* The bytecode PC that marks the end of this level. */
311 /* The bytecode PC that marks the start of this level. */
314 /* The statements in this binding level. */
317 /* Binding depth at which this level began. Used only for debugging. */
318 unsigned binding_depth
;
321 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
323 /* The binding level currently in effect. */
325 static GTY(()) struct binding_level
*current_binding_level
;
327 /* A chain of binding_level structures awaiting reuse. */
329 static GTY(()) struct binding_level
*free_binding_level
;
331 /* The outermost binding level, for names of file scope.
332 This is created when the compiler is started and exists
333 through the entire run. */
335 static GTY(()) struct binding_level
*global_binding_level
;
337 /* A PC value bigger than any PC value we may ever may encounter. */
339 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
341 /* Binding level structures are initialized by copying this one. */
343 static const struct binding_level clear_binding_level
344 = {NULL_TREE
, NULL_TREE
, NULL_TREE
, NULL_TREE
,
345 NULL_BINDING_LEVEL
, LARGEST_PC
, 0, NULL_TREE
, 0};
348 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
349 that have names. Here so we can clear out their names' definitions
350 at the end of the function. */
352 static tree named_labels
;
354 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
356 static tree shadowed_labels
;
359 tree java_global_trees
[JTI_MAX
];
361 /* Build (and pushdecl) a "promoted type" for all standard
362 types shorter than int. */
365 push_promoted_type (const char *name
, tree actual_type
)
367 tree type
= make_node (TREE_CODE (actual_type
));
369 tree in_min
= TYPE_MIN_VALUE (int_type_node
);
370 tree in_max
= TYPE_MAX_VALUE (int_type_node
);
372 tree in_min
= TYPE_MIN_VALUE (actual_type
);
373 tree in_max
= TYPE_MAX_VALUE (actual_type
);
375 TYPE_MIN_VALUE (type
) = copy_node (in_min
);
376 TREE_TYPE (TYPE_MIN_VALUE (type
)) = type
;
377 TYPE_MAX_VALUE (type
) = copy_node (in_max
);
378 TREE_TYPE (TYPE_MAX_VALUE (type
)) = type
;
379 TYPE_PRECISION (type
) = TYPE_PRECISION (int_type_node
);
381 pushdecl (build_decl (TYPE_DECL
, get_identifier (name
), type
));
385 /* Return a definition for a builtin function named NAME and whose data type
386 is TYPE. TYPE should be a function type with argument types.
387 FUNCTION_CODE tells later passes how to compile calls to this function.
388 See tree.h for its possible values.
390 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
391 the name to be called if we can't opencode the function. If
392 ATTRS is nonzero, use that for the function's attribute list. */
395 builtin_function (const char *name
,
398 enum built_in_class
class,
399 const char *library_name
,
400 tree attrs ATTRIBUTE_UNUSED
)
402 tree decl
= build_decl (FUNCTION_DECL
, get_identifier (name
), type
);
403 DECL_EXTERNAL (decl
) = 1;
404 TREE_PUBLIC (decl
) = 1;
406 SET_DECL_ASSEMBLER_NAME (decl
, get_identifier (library_name
));
407 make_decl_rtl (decl
, NULL
);
409 DECL_BUILT_IN_CLASS (decl
) = class;
410 DECL_FUNCTION_CODE (decl
) = function_code
;
414 /* Return tree that represents a vtable for a primitive array. */
416 create_primitive_vtable (const char *name
)
421 sprintf (buf
, "_Jv_%sVTable", name
);
422 r
= build_decl (VAR_DECL
, get_identifier (buf
), ptr_type_node
);
423 DECL_EXTERNAL (r
) = 1;
435 java_init_decl_processing (void)
438 tree field
= NULL_TREE
;
441 init_class_processing ();
442 init_resource_processing ();
444 current_function_decl
= NULL
;
445 current_binding_level
= NULL_BINDING_LEVEL
;
446 free_binding_level
= NULL_BINDING_LEVEL
;
447 pushlevel (0); /* make the binding_level structure for global names */
448 global_binding_level
= current_binding_level
;
450 /* The code here must be similar to build_common_tree_nodes{,_2} in
451 tree.c, especially as to the order of initializing common nodes. */
452 error_mark_node
= make_node (ERROR_MARK
);
453 TREE_TYPE (error_mark_node
) = error_mark_node
;
455 /* Create sizetype first - needed for other types. */
456 initialize_sizetypes ();
458 byte_type_node
= make_signed_type (8);
459 pushdecl (build_decl (TYPE_DECL
, get_identifier ("byte"), byte_type_node
));
460 short_type_node
= make_signed_type (16);
461 pushdecl (build_decl (TYPE_DECL
, get_identifier ("short"), short_type_node
));
462 int_type_node
= make_signed_type (32);
463 pushdecl (build_decl (TYPE_DECL
, get_identifier ("int"), int_type_node
));
464 long_type_node
= make_signed_type (64);
465 pushdecl (build_decl (TYPE_DECL
, get_identifier ("long"), long_type_node
));
467 unsigned_byte_type_node
= make_unsigned_type (8);
468 pushdecl (build_decl (TYPE_DECL
, get_identifier ("unsigned byte"),
469 unsigned_byte_type_node
));
470 unsigned_short_type_node
= make_unsigned_type (16);
471 pushdecl (build_decl (TYPE_DECL
, get_identifier ("unsigned short"),
472 unsigned_short_type_node
));
473 unsigned_int_type_node
= make_unsigned_type (32);
474 pushdecl (build_decl (TYPE_DECL
, get_identifier ("unsigned int"),
475 unsigned_int_type_node
));
476 unsigned_long_type_node
= make_unsigned_type (64);
477 pushdecl (build_decl (TYPE_DECL
, get_identifier ("unsigned long"),
478 unsigned_long_type_node
));
480 /* This is not a java type, however tree-dfa requires a definition for
482 size_type_node
= make_unsigned_type (POINTER_SIZE
);
483 set_sizetype (size_type_node
);
485 /* Define these next since types below may used them. */
486 integer_type_node
= java_type_for_size (INT_TYPE_SIZE
, 0);
487 integer_zero_node
= build_int_2 (0, 0);
488 integer_one_node
= build_int_2 (1, 0);
489 integer_two_node
= build_int_2 (2, 0);
490 integer_four_node
= build_int_2 (4, 0);
491 integer_minus_one_node
= build_int_2 (-1, -1);
493 /* A few values used for range checking in the lexer. */
494 decimal_int_max
= build_int_2 (0x80000000, 0);
495 TREE_TYPE (decimal_int_max
) = unsigned_int_type_node
;
496 #if HOST_BITS_PER_WIDE_INT == 64
497 decimal_long_max
= build_int_2 (0x8000000000000000LL
, 0);
499 #if HOST_BITS_PER_WIDE_INT == 32
500 decimal_long_max
= build_int_2 (0, 0x80000000);
502 #error "unsupported size"
505 TREE_TYPE (decimal_long_max
) = unsigned_long_type_node
;
507 size_zero_node
= size_int (0);
508 size_one_node
= size_int (1);
509 bitsize_zero_node
= bitsize_int (0);
510 bitsize_one_node
= bitsize_int (1);
511 bitsize_unit_node
= bitsize_int (BITS_PER_UNIT
);
513 long_zero_node
= build_int_2 (0, 0);
514 TREE_TYPE (long_zero_node
) = long_type_node
;
516 void_type_node
= make_node (VOID_TYPE
);
517 pushdecl (build_decl (TYPE_DECL
, get_identifier ("void"), void_type_node
));
518 layout_type (void_type_node
); /* Uses size_zero_node */
519 ptr_type_node
= build_pointer_type (void_type_node
);
520 t
= make_node (VOID_TYPE
);
521 layout_type (t
); /* Uses size_zero_node */
522 return_address_type_node
= build_pointer_type (t
);
524 null_pointer_node
= build_int_2 (0, 0);
525 TREE_TYPE (null_pointer_node
) = ptr_type_node
;
528 /* Make a type to be the domain of a few array types
529 whose domains don't really matter.
530 200 is small enough that it always fits in size_t
531 and large enough that it can hold most function names for the
532 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
533 short_array_type_node
= build_prim_array_type (short_type_node
, 200);
535 char_type_node
= make_node (CHAR_TYPE
);
536 TYPE_PRECISION (char_type_node
) = 16;
537 fixup_unsigned_type (char_type_node
);
538 pushdecl (build_decl (TYPE_DECL
, get_identifier ("char"), char_type_node
));
540 boolean_type_node
= make_node (BOOLEAN_TYPE
);
541 TYPE_PRECISION (boolean_type_node
) = 1;
542 fixup_unsigned_type (boolean_type_node
);
543 pushdecl (build_decl (TYPE_DECL
, get_identifier ("boolean"),
545 boolean_false_node
= TYPE_MIN_VALUE (boolean_type_node
);
546 boolean_true_node
= TYPE_MAX_VALUE (boolean_type_node
);
548 promoted_byte_type_node
549 = push_promoted_type ("promoted_byte", byte_type_node
);
550 promoted_short_type_node
551 = push_promoted_type ("promoted_short", short_type_node
);
552 promoted_char_type_node
553 = push_promoted_type ("promoted_char", char_type_node
);
554 promoted_boolean_type_node
555 = push_promoted_type ("promoted_boolean", boolean_type_node
);
557 float_type_node
= make_node (REAL_TYPE
);
558 TYPE_PRECISION (float_type_node
) = 32;
559 pushdecl (build_decl (TYPE_DECL
, get_identifier ("float"),
561 layout_type (float_type_node
);
563 double_type_node
= make_node (REAL_TYPE
);
564 TYPE_PRECISION (double_type_node
) = 64;
565 pushdecl (build_decl (TYPE_DECL
, get_identifier ("double"),
567 layout_type (double_type_node
);
569 float_zero_node
= build_real (float_type_node
, dconst0
);
570 double_zero_node
= build_real (double_type_node
, dconst0
);
572 /* These are the vtables for arrays of primitives. */
573 boolean_array_vtable
= create_primitive_vtable ("boolean");
574 byte_array_vtable
= create_primitive_vtable ("byte");
575 char_array_vtable
= create_primitive_vtable ("char");
576 short_array_vtable
= create_primitive_vtable ("short");
577 int_array_vtable
= create_primitive_vtable ("int");
578 long_array_vtable
= create_primitive_vtable ("long");
579 float_array_vtable
= create_primitive_vtable ("float");
580 double_array_vtable
= create_primitive_vtable ("double");
582 one_elt_array_domain_type
= build_index_type (integer_one_node
);
583 utf8const_type
= make_node (RECORD_TYPE
);
584 PUSH_FIELD (utf8const_type
, field
, "hash", unsigned_short_type_node
);
585 PUSH_FIELD (utf8const_type
, field
, "length", unsigned_short_type_node
);
586 FINISH_RECORD (utf8const_type
);
587 utf8const_ptr_type
= build_pointer_type (utf8const_type
);
589 atable_type
= build_array_type (ptr_type_node
,
590 one_elt_array_domain_type
);
591 TYPE_NONALIASED_COMPONENT (atable_type
) = 1;
592 atable_ptr_type
= build_pointer_type (atable_type
);
594 symbol_type
= make_node (RECORD_TYPE
);
595 PUSH_FIELD (symbol_type
, field
, "clname", utf8const_ptr_type
);
596 PUSH_FIELD (symbol_type
, field
, "name", utf8const_ptr_type
);
597 PUSH_FIELD (symbol_type
, field
, "signature", utf8const_ptr_type
);
598 FINISH_RECORD (symbol_type
);
600 symbols_array_type
= build_array_type (symbol_type
,
601 one_elt_array_domain_type
);
602 symbols_array_ptr_type
= build_pointer_type (symbols_array_type
);
604 /* As you're adding items here, please update the code right after
605 this section, so that the filename containing the source code of
606 the pre-defined class gets registered correctly. */
607 unqualified_object_id_node
= get_identifier ("Object");
608 object_type_node
= lookup_class (get_identifier ("java.lang.Object"));
609 object_ptr_type_node
= promote_type (object_type_node
);
610 string_type_node
= lookup_class (get_identifier ("java.lang.String"));
611 string_ptr_type_node
= promote_type (string_type_node
);
612 class_type_node
= lookup_class (get_identifier ("java.lang.Class"));
613 throwable_type_node
= lookup_class (get_identifier ("java.lang.Throwable"));
614 exception_type_node
= lookup_class (get_identifier ("java.lang.Exception"));
615 runtime_exception_type_node
=
616 lookup_class (get_identifier ("java.lang.RuntimeException"));
617 error_exception_type_node
=
618 lookup_class (get_identifier ("java.lang.Error"));
620 rawdata_ptr_type_node
621 = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
623 add_predefined_file (get_identifier ("java/lang/Class.java"));
624 add_predefined_file (get_identifier ("java/lang/Error.java"));
625 add_predefined_file (get_identifier ("java/lang/Object.java"));
626 add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
627 add_predefined_file (get_identifier ("java/lang/String.java"));
628 add_predefined_file (get_identifier ("java/lang/Throwable.java"));
629 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
630 add_predefined_file (get_identifier ("java/lang/Exception.java"));
631 add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
632 add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
634 methodtable_type
= make_node (RECORD_TYPE
);
635 layout_type (methodtable_type
);
636 build_decl (TYPE_DECL
, get_identifier ("methodtable"), methodtable_type
);
637 methodtable_ptr_type
= build_pointer_type (methodtable_type
);
639 TYPE_identifier_node
= get_identifier ("TYPE");
640 init_identifier_node
= get_identifier ("<init>");
641 clinit_identifier_node
= get_identifier ("<clinit>");
642 finit_identifier_node
= get_identifier ("finit$");
643 instinit_identifier_node
= get_identifier ("instinit$");
644 void_signature_node
= get_identifier ("()V");
645 length_identifier_node
= get_identifier ("length");
646 finalize_identifier_node
= get_identifier ("finalize");
647 this_identifier_node
= get_identifier ("this");
648 super_identifier_node
= get_identifier ("super");
649 continue_identifier_node
= get_identifier ("continue");
650 access0_identifier_node
= get_identifier ("access$0");
651 classdollar_identifier_node
= get_identifier ("class$");
653 java_lang_cloneable_identifier_node
= get_identifier ("java.lang.Cloneable");
654 java_io_serializable_identifier_node
=
655 get_identifier ("java.io.Serializable");
657 /* for lack of a better place to put this stub call */
658 init_expr_processing();
660 constants_type_node
= make_node (RECORD_TYPE
);
661 PUSH_FIELD (constants_type_node
, field
, "size", unsigned_int_type_node
);
662 PUSH_FIELD (constants_type_node
, field
, "tags", ptr_type_node
);
663 PUSH_FIELD (constants_type_node
, field
, "data", ptr_type_node
);
664 FINISH_RECORD (constants_type_node
);
665 build_decl (TYPE_DECL
, get_identifier ("constants"), constants_type_node
);
667 access_flags_type_node
= unsigned_short_type_node
;
669 dtable_type
= make_node (RECORD_TYPE
);
670 dtable_ptr_type
= build_pointer_type (dtable_type
);
672 otable_type
= build_array_type (integer_type_node
,
673 one_elt_array_domain_type
);
674 TYPE_NONALIASED_COMPONENT (otable_type
) = 1;
675 otable_ptr_type
= build_pointer_type (otable_type
);
677 PUSH_FIELD (object_type_node
, field
, "vtable", dtable_ptr_type
);
678 DECL_FCONTEXT (field
) = object_type_node
;
679 TYPE_VFIELD (object_type_node
) = field
;
681 /* This isn't exactly true, but it is what we have in the source.
682 There is an unresolved issue here, which is whether the vtable
683 should be marked by the GC. */
684 if (! flag_hash_synchronization
)
685 PUSH_FIELD (object_type_node
, field
, "sync_info",
686 build_pointer_type (object_type_node
));
687 for (t
= TYPE_FIELDS (object_type_node
); t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
688 FIELD_PRIVATE (t
) = 1;
689 FINISH_RECORD (object_type_node
);
691 field_type_node
= make_node (RECORD_TYPE
);
692 field_ptr_type_node
= build_pointer_type (field_type_node
);
693 method_type_node
= make_node (RECORD_TYPE
);
694 method_ptr_type_node
= build_pointer_type (method_type_node
);
696 set_super_info (0, class_type_node
, object_type_node
, 0);
697 set_super_info (0, string_type_node
, object_type_node
, 0);
698 class_ptr_type
= build_pointer_type (class_type_node
);
700 PUSH_FIELD (class_type_node
, field
, "next", class_ptr_type
);
701 PUSH_FIELD (class_type_node
, field
, "name", utf8const_ptr_type
);
702 PUSH_FIELD (class_type_node
, field
, "accflags", access_flags_type_node
);
703 PUSH_FIELD (class_type_node
, field
, "superclass", class_ptr_type
);
704 PUSH_FIELD (class_type_node
, field
, "constants", constants_type_node
);
705 PUSH_FIELD (class_type_node
, field
, "methods", method_ptr_type_node
);
706 PUSH_FIELD (class_type_node
, field
, "method_count", short_type_node
);
707 PUSH_FIELD (class_type_node
, field
, "vtable_method_count", short_type_node
);
708 PUSH_FIELD (class_type_node
, field
, "fields", field_ptr_type_node
);
709 PUSH_FIELD (class_type_node
, field
, "size_in_bytes", int_type_node
);
710 PUSH_FIELD (class_type_node
, field
, "field_count", short_type_node
);
711 PUSH_FIELD (class_type_node
, field
, "static_field_count", short_type_node
);
712 PUSH_FIELD (class_type_node
, field
, "vtable", dtable_ptr_type
);
713 PUSH_FIELD (class_type_node
, field
, "otable", otable_ptr_type
);
714 PUSH_FIELD (class_type_node
, field
, "otable_syms",
715 symbols_array_ptr_type
);
716 PUSH_FIELD (class_type_node
, field
, "atable", atable_ptr_type
);
717 PUSH_FIELD (class_type_node
, field
, "atable_syms",
718 symbols_array_ptr_type
);
719 PUSH_FIELD (class_type_node
, field
, "catch_classes", ptr_type_node
);
720 PUSH_FIELD (class_type_node
, field
, "interfaces",
721 build_pointer_type (class_ptr_type
));
722 PUSH_FIELD (class_type_node
, field
, "loader", ptr_type_node
);
723 PUSH_FIELD (class_type_node
, field
, "interface_count", short_type_node
);
724 PUSH_FIELD (class_type_node
, field
, "state", byte_type_node
);
725 PUSH_FIELD (class_type_node
, field
, "thread", ptr_type_node
);
726 PUSH_FIELD (class_type_node
, field
, "depth", short_type_node
);
727 PUSH_FIELD (class_type_node
, field
, "ancestors", ptr_type_node
);
728 PUSH_FIELD (class_type_node
, field
, "idt", ptr_type_node
);
729 PUSH_FIELD (class_type_node
, field
, "arrayclass", ptr_type_node
);
730 PUSH_FIELD (class_type_node
, field
, "protectionDomain", ptr_type_node
);
731 PUSH_FIELD (class_type_node
, field
, "hack_signers", ptr_type_node
);
732 PUSH_FIELD (class_type_node
, field
, "chain", ptr_type_node
);
733 PUSH_FIELD (class_type_node
, field
, "aux_info", ptr_type_node
);
734 for (t
= TYPE_FIELDS (class_type_node
); t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
735 FIELD_PRIVATE (t
) = 1;
736 push_super_field (class_type_node
, object_type_node
);
738 FINISH_RECORD (class_type_node
);
739 build_decl (TYPE_DECL
, get_identifier ("Class"), class_type_node
);
741 field_info_union_node
= make_node (UNION_TYPE
);
742 PUSH_FIELD (field_info_union_node
, field
, "boffset", int_type_node
);
743 PUSH_FIELD (field_info_union_node
, field
, "addr", ptr_type_node
);
745 PUSH_FIELD (field_info_union_node
, field
, "idx", unsigned_short_type_node
);
747 layout_type (field_info_union_node
);
749 PUSH_FIELD (field_type_node
, field
, "name", utf8const_ptr_type
);
750 PUSH_FIELD (field_type_node
, field
, "type", class_ptr_type
);
751 PUSH_FIELD (field_type_node
, field
, "accflags", access_flags_type_node
);
752 PUSH_FIELD (field_type_node
, field
, "bsize", unsigned_short_type_node
);
753 PUSH_FIELD (field_type_node
, field
, "info", field_info_union_node
);
754 FINISH_RECORD (field_type_node
);
755 build_decl (TYPE_DECL
, get_identifier ("Field"), field_type_node
);
757 nativecode_ptr_array_type_node
758 = build_array_type (nativecode_ptr_type_node
, one_elt_array_domain_type
);
760 PUSH_FIELD (dtable_type
, field
, "class", class_ptr_type
);
761 PUSH_FIELD (dtable_type
, field
, "methods", nativecode_ptr_array_type_node
);
762 FINISH_RECORD (dtable_type
);
763 build_decl (TYPE_DECL
, get_identifier ("dispatchTable"), dtable_type
);
765 #define jint_type int_type_node
766 #define jint_ptr_type ptr_type_node
768 jexception_type
= make_node (RECORD_TYPE
);
769 PUSH_FIELD (jexception_type
, field
, "start_pc", ptr_type_node
);
770 PUSH_FIELD (jexception_type
, field
, "end_pc", ptr_type_node
);
771 PUSH_FIELD (jexception_type
, field
, "handler_pc", ptr_type_node
);
772 PUSH_FIELD (jexception_type
, field
, "catch_type", class_ptr_type
);
773 FINISH_RECORD (jexception_type
);
774 build_decl (TYPE_DECL
, get_identifier ("jexception"), field_type_node
);
775 jexception_ptr_type
= build_pointer_type (jexception_type
);
777 lineNumberEntry_type
= make_node (RECORD_TYPE
);
778 PUSH_FIELD (lineNumberEntry_type
, field
, "line_nr", unsigned_short_type_node
);
779 PUSH_FIELD (lineNumberEntry_type
, field
, "start_pc", ptr_type_node
);
780 FINISH_RECORD (lineNumberEntry_type
);
782 lineNumbers_type
= make_node (RECORD_TYPE
);
783 PUSH_FIELD (lineNumbers_type
, field
, "length", unsigned_int_type_node
);
784 FINISH_RECORD (lineNumbers_type
);
786 #define instn_ptr_type_node ptr_type_node /* XXX JH */
788 #define lineNumbers_ptr_type_node build_pointer_type(lineNumbers_type)
790 PUSH_FIELD (method_type_node
, field
, "name", utf8const_ptr_type
);
791 PUSH_FIELD (method_type_node
, field
, "signature", utf8const_ptr_type
);
792 PUSH_FIELD (method_type_node
, field
, "accflags", access_flags_type_node
);
793 PUSH_FIELD (method_type_node
, field
, "index", unsigned_short_type_node
);
794 PUSH_FIELD (method_type_node
, field
, "ncode", nativecode_ptr_type_node
);
795 PUSH_FIELD (method_type_node
, field
, "throws", ptr_type_node
);
796 FINISH_RECORD (method_type_node
);
797 build_decl (TYPE_DECL
, get_identifier ("Method"), method_type_node
);
799 endlink
= end_params_node
= tree_cons (NULL_TREE
, void_type_node
, NULL_TREE
);
801 t
= tree_cons (NULL_TREE
, class_ptr_type
,
802 tree_cons (NULL_TREE
, int_type_node
, endlink
));
803 alloc_object_node
= builtin_function ("_Jv_AllocObject",
804 build_function_type (ptr_type_node
, t
),
805 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
806 DECL_IS_MALLOC (alloc_object_node
) = 1;
807 alloc_no_finalizer_node
=
808 builtin_function ("_Jv_AllocObjectNoFinalizer",
809 build_function_type (ptr_type_node
, t
),
810 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
811 DECL_IS_MALLOC (alloc_no_finalizer_node
) = 1;
813 t
= tree_cons (NULL_TREE
, ptr_type_node
, endlink
);
814 soft_initclass_node
= builtin_function ("_Jv_InitClass",
815 build_function_type (void_type_node
,
817 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
819 throw_node
= builtin_function ("_Jv_Throw",
820 build_function_type (ptr_type_node
, t
),
821 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
822 /* Mark throw_nodes as `noreturn' functions with side effects. */
823 TREE_THIS_VOLATILE (throw_node
) = 1;
824 TREE_SIDE_EFFECTS (throw_node
) = 1;
826 t
= build_function_type (int_type_node
, endlink
);
827 soft_monitorenter_node
828 = builtin_function ("_Jv_MonitorEnter", t
, 0, NOT_BUILT_IN
,
830 soft_monitorexit_node
831 = builtin_function ("_Jv_MonitorExit", t
, 0, NOT_BUILT_IN
,
834 t
= tree_cons (NULL_TREE
, int_type_node
,
835 tree_cons (NULL_TREE
, int_type_node
, endlink
));
837 = builtin_function ("_Jv_NewPrimArray",
838 build_function_type(ptr_type_node
, t
),
839 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
840 DECL_IS_MALLOC (soft_newarray_node
) = 1;
842 t
= tree_cons (NULL_TREE
, int_type_node
,
843 tree_cons (NULL_TREE
, class_ptr_type
,
844 tree_cons (NULL_TREE
, object_ptr_type_node
, endlink
)));
846 = builtin_function ("_Jv_NewObjectArray",
847 build_function_type (ptr_type_node
, t
),
848 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
849 DECL_IS_MALLOC (soft_anewarray_node
) = 1;
851 /* There is no endlink here because _Jv_NewMultiArray is a varargs
853 t
= tree_cons (NULL_TREE
, ptr_type_node
,
854 tree_cons (NULL_TREE
, int_type_node
, NULL_TREE
));
855 soft_multianewarray_node
856 = builtin_function ("_Jv_NewMultiArray",
857 build_function_type (ptr_type_node
, t
),
858 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
859 DECL_IS_MALLOC (soft_multianewarray_node
) = 1;
861 t
= build_function_type (void_type_node
,
862 tree_cons (NULL_TREE
, int_type_node
, endlink
));
863 soft_badarrayindex_node
864 = builtin_function ("_Jv_ThrowBadArrayIndex", t
,
865 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
866 /* Mark soft_badarrayindex_node as a `noreturn' function with side
868 TREE_THIS_VOLATILE (soft_badarrayindex_node
) = 1;
869 TREE_SIDE_EFFECTS (soft_badarrayindex_node
) = 1;
871 soft_nullpointer_node
872 = builtin_function ("_Jv_ThrowNullPointerException",
873 build_function_type (void_type_node
, endlink
),
874 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
875 /* Mark soft_nullpointer_node as a `noreturn' function with side
877 TREE_THIS_VOLATILE (soft_nullpointer_node
) = 1;
878 TREE_SIDE_EFFECTS (soft_nullpointer_node
) = 1;
880 t
= tree_cons (NULL_TREE
, class_ptr_type
,
881 tree_cons (NULL_TREE
, object_ptr_type_node
, endlink
));
883 = builtin_function ("_Jv_CheckCast",
884 build_function_type (ptr_type_node
, t
),
885 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
886 t
= tree_cons (NULL_TREE
, object_ptr_type_node
,
887 tree_cons (NULL_TREE
, class_ptr_type
, endlink
));
889 = builtin_function ("_Jv_IsInstanceOf",
890 build_function_type (boolean_type_node
, t
),
891 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
892 t
= tree_cons (NULL_TREE
, object_ptr_type_node
,
893 tree_cons (NULL_TREE
, object_ptr_type_node
, endlink
));
894 soft_checkarraystore_node
895 = builtin_function ("_Jv_CheckArrayStore",
896 build_function_type (void_type_node
, t
),
897 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
898 t
= tree_cons (NULL_TREE
, ptr_type_node
,
899 tree_cons (NULL_TREE
, ptr_type_node
,
900 tree_cons (NULL_TREE
, int_type_node
, endlink
)));
901 soft_lookupinterfacemethod_node
902 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
903 build_function_type (ptr_type_node
, t
),
904 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
906 t
= tree_cons (NULL_TREE
, object_ptr_type_node
,
907 tree_cons (NULL_TREE
, ptr_type_node
,
908 tree_cons (NULL_TREE
, ptr_type_node
,
909 tree_cons (NULL_TREE
, int_type_node
,
911 soft_lookupjnimethod_node
912 = builtin_function ("_Jv_LookupJNIMethod",
913 build_function_type (ptr_type_node
, t
),
914 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
915 t
= tree_cons (NULL_TREE
, ptr_type_node
, endlink
);
916 soft_getjnienvnewframe_node
917 = builtin_function ("_Jv_GetJNIEnvNewFrame",
918 build_function_type (ptr_type_node
, t
),
919 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
920 soft_jnipopsystemframe_node
921 = builtin_function ("_Jv_JNI_PopSystemFrame",
922 build_function_type (ptr_type_node
, t
),
923 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
926 = builtin_function ("_Jv_divI",
927 build_function_type (int_type_node
, t
),
928 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
931 = builtin_function ("_Jv_remI",
932 build_function_type (int_type_node
, t
),
933 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
936 = builtin_function ("_Jv_divJ",
937 build_function_type (long_type_node
, t
),
938 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
941 = builtin_function ("_Jv_remJ",
942 build_function_type (long_type_node
, t
),
943 0, NOT_BUILT_IN
, NULL
, NULL_TREE
);
945 /* Initialize variables for except.c. */
946 eh_personality_libfunc
= init_one_libfunc (USING_SJLJ_EXCEPTIONS
947 ? "__gcj_personality_sj0"
948 : "__gcj_personality_v0");
950 lang_eh_runtime_type
= do_nothing
;
954 initialize_builtins ();
955 soft_fmod_node
= built_in_decls
[BUILT_IN_FMOD
];
957 soft_fmodf_node
= built_in_decls
[BUILT_IN_FMODF
];
962 /* Look up NAME in the current binding level and its superiors
963 in the namespace of variables, functions and typedefs.
964 Return a ..._DECL node of some kind representing its definition,
965 or return 0 if it is undefined. */
968 lookup_name (tree name
)
971 if (current_binding_level
!= global_binding_level
972 && IDENTIFIER_LOCAL_VALUE (name
))
973 val
= IDENTIFIER_LOCAL_VALUE (name
);
975 val
= IDENTIFIER_GLOBAL_VALUE (name
);
979 /* Similar to `lookup_name' but look only at current binding level and
980 the previous one if its the parameter level. */
983 lookup_name_current_level (tree name
)
987 if (current_binding_level
== global_binding_level
)
988 return IDENTIFIER_GLOBAL_VALUE (name
);
990 if (IDENTIFIER_LOCAL_VALUE (name
) == 0)
993 for (t
= current_binding_level
->names
; t
; t
= TREE_CHAIN (t
))
994 if (DECL_NAME (t
) == name
)
1000 /* Use a binding level to record a labeled block declaration */
1003 push_labeled_block (tree lb
)
1005 tree name
= DECL_NAME (LABELED_BLOCK_LABEL (lb
));
1006 struct binding_level
*b
= current_binding_level
;
1007 tree oldlocal
= IDENTIFIER_LOCAL_VALUE (name
);
1009 b
->shadowed
= tree_cons (name
, oldlocal
, b
->shadowed
);
1010 TREE_CHAIN (lb
) = b
->names
;
1012 IDENTIFIER_LOCAL_VALUE (name
) = lb
;
1015 /* Pop the current binding level, reinstalling values for the previous
1019 pop_labeled_block (void)
1021 struct binding_level
*b
= current_binding_level
;
1022 tree label
= b
->names
;
1023 IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label
))) =
1026 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b
->shadowed
)) =
1027 TREE_VALUE (b
->shadowed
);
1029 /* Pop the current level, and free the structure for reuse. */
1030 current_binding_level
= current_binding_level
->level_chain
;
1031 b
->level_chain
= free_binding_level
;
1032 free_binding_level
= b
;
1035 /* Record a decl-node X as belonging to the current lexical scope.
1036 Check for errors (such as an incompatible declaration for the same
1037 name already seen in the same scope).
1039 Returns either X or an old decl for the same name.
1040 If an old decl is returned, it may have been smashed
1041 to agree with what X says. */
1047 tree name
= DECL_NAME (x
);
1048 struct binding_level
*b
= current_binding_level
;
1050 if (TREE_CODE (x
) != TYPE_DECL
)
1051 DECL_CONTEXT (x
) = current_function_decl
;
1054 t
= lookup_name_current_level (name
);
1055 if (t
!= 0 && t
== error_mark_node
)
1056 /* error_mark_node is 0 for a while during initialization! */
1059 error ("%J'%D' used prior to declaration", x
, x
);
1062 /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1063 to point to the TYPE_DECL.
1064 Since Java does not have typedefs, a type can only have
1065 one (true) name, given by a class, interface, or builtin. */
1066 if (TREE_CODE (x
) == TYPE_DECL
1067 && TYPE_NAME (TREE_TYPE (x
)) == 0
1068 && TREE_TYPE (x
) != error_mark_node
)
1070 TYPE_NAME (TREE_TYPE (x
)) = x
;
1071 TYPE_STUB_DECL (TREE_TYPE (x
)) = x
;
1074 /* This name is new in its binding level.
1075 Install the new declaration and return it. */
1076 if (b
== global_binding_level
)
1078 /* Install a global value. */
1080 IDENTIFIER_GLOBAL_VALUE (name
) = x
;
1084 /* Here to install a non-global value. */
1085 tree oldlocal
= IDENTIFIER_LOCAL_VALUE (name
);
1086 IDENTIFIER_LOCAL_VALUE (name
) = x
;
1089 /* Warn if shadowing an argument at the top level of the body. */
1090 if (oldlocal
!= 0 && !DECL_EXTERNAL (x
)
1091 /* This warning doesn't apply to the parms of a nested fcn. */
1092 && ! current_binding_level
->parm_flag
1093 /* Check that this is one level down from the parms. */
1094 && current_binding_level
->level_chain
->parm_flag
1095 /* Check that the decl being shadowed
1096 comes from the parm level, one level up. */
1097 && chain_member (oldlocal
, current_binding_level
->level_chain
->names
))
1099 if (TREE_CODE (oldlocal
) == PARM_DECL
)
1100 pedwarn ("declaration of `%s' shadows a parameter",
1101 IDENTIFIER_POINTER (name
));
1103 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
1104 IDENTIFIER_POINTER (name
));
1107 /* Maybe warn if shadowing something else. */
1108 else if (warn_shadow
&& !DECL_EXTERNAL (x
)
1109 /* No shadow warnings for internally generated vars. */
1110 && DECL_SOURCE_LINE (x
) != 0
1111 /* No shadow warnings for vars made for inlining. */
1112 && ! DECL_FROM_INLINE (x
))
1114 const char *warnstring
= 0;
1116 if (TREE_CODE (x
) == PARM_DECL
1117 && current_binding_level
->level_chain
->parm_flag
)
1118 /* Don't warn about the parm names in function declarator
1119 within a function declarator.
1120 It would be nice to avoid warning in any function
1121 declarator in a declaration, as opposed to a definition,
1122 but there is no way to tell it's not a definition. */
1124 else if (oldlocal
!= 0 && TREE_CODE (oldlocal
) == PARM_DECL
)
1125 warnstring
= "declaration of `%s' shadows a parameter";
1126 else if (oldlocal
!= 0)
1127 warnstring
= "declaration of `%s' shadows previous local";
1128 else if (IDENTIFIER_GLOBAL_VALUE (name
) != 0
1129 && IDENTIFIER_GLOBAL_VALUE (name
) != error_mark_node
)
1130 warnstring
= "declaration of `%s' shadows global declaration";
1133 warning (warnstring
, IDENTIFIER_POINTER (name
));
1137 /* If storing a local value, there may already be one (inherited).
1138 If so, record it for restoration when this binding level ends. */
1140 b
->shadowed
= tree_cons (name
, oldlocal
, b
->shadowed
);
1144 /* Put decls on list in reverse order.
1145 We will reverse them later if necessary. */
1146 TREE_CHAIN (x
) = b
->names
;
1153 pushdecl_force_head (tree x
)
1155 current_binding_level
->names
= x
;
1158 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1161 pushdecl_top_level (tree x
)
1164 struct binding_level
*b
= current_binding_level
;
1166 current_binding_level
= global_binding_level
;
1168 current_binding_level
= b
;
1172 /* Nonzero if we are currently in the global binding level. */
1175 global_bindings_p (void)
1177 return current_binding_level
== global_binding_level
;
1180 /* Return the list of declarations of the current level.
1181 Note that this list is in reverse order unless/until
1182 you nreverse it; and when you do nreverse it, you must
1183 store the result back using `storedecls' or you will lose. */
1188 return current_binding_level
->names
;
1191 /* Create a new `struct binding_level'. */
1193 static struct binding_level
*
1194 make_binding_level (void)
1197 return ggc_alloc_cleared (sizeof (struct binding_level
));
1201 pushlevel (int unused ATTRIBUTE_UNUSED
)
1203 struct binding_level
*newlevel
= NULL_BINDING_LEVEL
;
1206 /* If this is the top level of a function,
1207 just make sure that NAMED_LABELS is 0. */
1209 if (current_binding_level
== global_binding_level
)
1213 /* Reuse or create a struct for this binding level. */
1215 if (free_binding_level
)
1217 newlevel
= free_binding_level
;
1218 free_binding_level
= free_binding_level
->level_chain
;
1222 newlevel
= make_binding_level ();
1225 /* Add this level to the front of the chain (stack) of levels that
1228 *newlevel
= clear_binding_level
;
1229 newlevel
->level_chain
= current_binding_level
;
1230 current_binding_level
= newlevel
;
1231 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1232 newlevel
->binding_depth
= binding_depth
;
1234 fprintf (stderr
, "push %s level %p pc %d\n",
1235 (is_class_level
) ? "class" : "block", newlevel
, current_pc
);
1238 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1241 /* Exit a binding level.
1242 Pop the level off, and restore the state of the identifier-decl mappings
1243 that were in effect when this level was entered.
1245 If KEEP is nonzero, this level had explicit declarations, so
1246 and create a "block" (a BLOCK node) for the level
1247 to record its declarations and subblocks for symbol table output.
1249 If FUNCTIONBODY is nonzero, this level is the body of a function,
1250 so create a block as if KEEP were set and also clear out all
1253 If REVERSE is nonzero, reverse the order of decls before putting
1254 them into the BLOCK. */
1257 poplevel (int keep
, int reverse
, int functionbody
)
1260 /* The chain of decls was accumulated in reverse order.
1261 Put it into forward order, just for cleanliness. */
1263 tree subblocks
= current_binding_level
->blocks
;
1267 int block_previously_created
;
1269 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1272 if (current_binding_level
->end_pc
!= LARGEST_PC
)
1273 fprintf (stderr
, "pop %s level %p pc %d (end pc %d)\n",
1274 (is_class_level
) ? "class" : "block", current_binding_level
, current_pc
,
1275 current_binding_level
->end_pc
);
1277 fprintf (stderr
, "pop %s level %p pc %d\n",
1278 (is_class_level
) ? "class" : "block", current_binding_level
, current_pc
);
1280 if (is_class_level
!= (current_binding_level
== class_binding_level
))
1283 fprintf (stderr
, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
1287 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1289 /* Get the decls in the order they were written.
1290 Usually current_binding_level->names is in reverse order.
1291 But parameter decls were previously put in forward order. */
1294 current_binding_level
->names
1295 = decls
= nreverse (current_binding_level
->names
);
1297 decls
= current_binding_level
->names
;
1299 for (decl
= decls
; decl
; decl
= TREE_CHAIN (decl
))
1300 if (TREE_CODE (decl
) == VAR_DECL
1301 && DECL_LANG_SPECIFIC (decl
) != NULL
1302 && DECL_LOCAL_SLOT_NUMBER (decl
))
1303 LOCAL_VAR_OUT_OF_SCOPE_P (decl
) = 1;
1305 /* If there were any declarations in that level,
1306 or if this level is a function body,
1307 create a BLOCK to record them for the life of this function. */
1310 block_previously_created
= (current_binding_level
->this_block
!= 0);
1311 if (block_previously_created
)
1312 block
= current_binding_level
->this_block
;
1313 else if (keep
|| functionbody
)
1315 block
= make_node (BLOCK
);
1316 TREE_TYPE (block
) = void_type_node
;
1321 /* If any statements have been generated at this level, create a
1322 BIND_EXPR to hold them and copy the variables to it. This
1323 only applies to the bytecode compiler. */
1324 if (current_binding_level
->stmts
)
1327 tree
*var
= &BLOCK_VARS (block
);
1329 /* Copy decls from names list, ignoring labels. */
1332 tree next
= TREE_CHAIN (decl
);
1333 if (TREE_CODE (decl
) != LABEL_DECL
)
1336 var
= &TREE_CHAIN (decl
);
1342 bind
= build (BIND_EXPR
, TREE_TYPE (block
), BLOCK_VARS (block
),
1343 BLOCK_EXPR_BODY (block
), block
);
1345 BIND_EXPR_BODY (bind
) = current_binding_level
->stmts
;
1347 if (BIND_EXPR_BODY (bind
)
1348 && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind
)))
1349 TREE_SIDE_EFFECTS (bind
) = 1;
1351 /* FIXME: gimplifier brain damage. */
1352 if (BIND_EXPR_BODY (bind
) == NULL
)
1353 BIND_EXPR_BODY (bind
) = build_java_empty_stmt ();
1355 current_binding_level
->stmts
= NULL
;
1359 BLOCK_VARS (block
) = decls
;
1361 BLOCK_SUBBLOCKS (block
) = subblocks
;
1364 /* In each subblock, record that this is its superior. */
1366 for (link
= subblocks
; link
; link
= TREE_CHAIN (link
))
1367 BLOCK_SUPERCONTEXT (link
) = block
;
1369 /* Clear out the meanings of the local variables of this level. */
1371 for (link
= decls
; link
; link
= TREE_CHAIN (link
))
1373 tree name
= DECL_NAME (link
);
1374 if (name
!= 0 && IDENTIFIER_LOCAL_VALUE (name
) == link
)
1376 /* If the ident. was used or addressed via a local extern decl,
1377 don't forget that fact. */
1378 if (DECL_EXTERNAL (link
))
1380 if (TREE_USED (link
))
1381 TREE_USED (name
) = 1;
1382 if (TREE_ADDRESSABLE (link
))
1383 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link
)) = 1;
1385 IDENTIFIER_LOCAL_VALUE (name
) = 0;
1389 /* Restore all name-meanings of the outer levels
1390 that were shadowed by this level. */
1392 for (link
= current_binding_level
->shadowed
; link
; link
= TREE_CHAIN (link
))
1393 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link
)) = TREE_VALUE (link
);
1395 /* If the level being exited is the top level of a function,
1396 check over all the labels, and clear out the current
1397 (function local) meanings of their names. */
1401 /* If this is the top level block of a function,
1402 the vars are the function's parameters.
1403 Don't leave them in the BLOCK because they are
1404 found in the FUNCTION_DECL instead. */
1406 BLOCK_VARS (block
) = 0;
1408 /* Clear out the definitions of all label names,
1409 since their scopes end here,
1410 and add them to BLOCK_VARS. */
1413 for (link
= named_labels
; link
; link
= TREE_CHAIN (link
))
1415 tree label
= TREE_VALUE (link
);
1417 if (DECL_INITIAL (label
) == 0)
1419 error ("%Jlabel '%D' used but not defined", label
, label
);
1420 /* Avoid crashing later. */
1421 define_label (input_location
, DECL_NAME (label
));
1423 else if (warn_unused
[UNUSED_LABEL
] && !TREE_USED (label
))
1424 warning ("%Jlabel '%D' defined but not used", label
, label
);
1425 IDENTIFIER_LABEL_VALUE (DECL_NAME (label
)) = 0;
1427 /* Put the labels into the "variables" of the
1428 top-level block, so debugger can see them. */
1429 TREE_CHAIN (label
) = BLOCK_VARS (block
);
1430 BLOCK_VARS (block
) = label
;
1435 /* Pop the current level, and free the structure for reuse. */
1438 struct binding_level
*level
= current_binding_level
;
1439 current_binding_level
= current_binding_level
->level_chain
;
1441 level
->level_chain
= free_binding_level
;
1442 free_binding_level
= level
;
1445 /* Dispose of the block that we just made inside some higher level. */
1448 DECL_INITIAL (current_function_decl
) = block
;
1449 DECL_SAVED_TREE (current_function_decl
) = bind
;
1453 if (!block_previously_created
)
1454 current_binding_level
->blocks
1455 = chainon (current_binding_level
->blocks
, block
);
1457 /* If we did not make a block for the level just exited,
1458 any blocks made for inner levels
1459 (since they cannot be recorded as subblocks in that level)
1460 must be carried forward so they will later become subblocks
1461 of something else. */
1463 current_binding_level
->blocks
1464 = chainon (current_binding_level
->blocks
, subblocks
);
1467 java_add_stmt (bind
);
1470 TREE_USED (block
) = 1;
1475 maybe_pushlevels (int pc
)
1477 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1481 while (pending_local_decls
!= NULL_TREE
&&
1482 DECL_LOCAL_START_PC (pending_local_decls
) <= pc
)
1484 tree
*ptr
= &pending_local_decls
;
1485 tree decl
= *ptr
, next
;
1486 int end_pc
= DECL_LOCAL_END_PC (decl
);
1488 while (*ptr
!= NULL_TREE
1489 && DECL_LOCAL_START_PC (*ptr
) <= pc
1490 && DECL_LOCAL_END_PC (*ptr
) == end_pc
)
1491 ptr
= &TREE_CHAIN (*ptr
);
1492 pending_local_decls
= *ptr
;
1495 /* Force non-nested range to be nested in current range. */
1496 if (end_pc
> current_binding_level
->end_pc
)
1497 end_pc
= current_binding_level
->end_pc
;
1499 maybe_start_try (pc
, end_pc
);
1503 current_binding_level
->end_pc
= end_pc
;
1504 current_binding_level
->start_pc
= pc
;
1505 current_binding_level
->names
= NULL
;
1506 for ( ; decl
!= NULL_TREE
; decl
= next
)
1508 next
= TREE_CHAIN (decl
);
1509 push_jvm_slot (DECL_LOCAL_SLOT_NUMBER (decl
), decl
);
1513 maybe_start_try (pc
, 0);
1517 maybe_poplevels (int pc
)
1519 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1523 while (current_binding_level
->end_pc
<= pc
)
1525 maybe_end_try (current_binding_level
->start_pc
, pc
);
1528 maybe_end_try (0, pc
);
1531 /* Terminate any binding which began during the range beginning at
1532 start_pc. This tidies up improperly nested local variable ranges
1533 and exception handlers; a variable declared within an exception
1534 range is forcibly terminated when that exception ends. */
1537 force_poplevels (int start_pc
)
1539 while (current_binding_level
->start_pc
> start_pc
)
1541 if (pedantic
&& current_binding_level
->start_pc
> start_pc
)
1542 warning ("%JIn %D: overlapped variable and exception ranges at %d",
1543 current_function_decl
, current_function_decl
,
1544 current_binding_level
->start_pc
);
1549 /* Insert BLOCK at the end of the list of subblocks of the
1550 current binding level. This is used when a BIND_EXPR is expanded,
1551 to handle the BLOCK node inside the BIND_EXPR. */
1554 insert_block (tree block
)
1556 TREE_USED (block
) = 1;
1557 current_binding_level
->blocks
1558 = chainon (current_binding_level
->blocks
, block
);
1561 /* Set the BLOCK node for the innermost scope
1562 (the one we are currently in). */
1565 set_block (tree block
)
1567 current_binding_level
->this_block
= block
;
1568 current_binding_level
->names
= chainon (current_binding_level
->names
,
1569 BLOCK_VARS (block
));
1570 current_binding_level
->blocks
= chainon (current_binding_level
->blocks
,
1571 BLOCK_SUBBLOCKS (block
));
1574 /* integrate_decl_tree calls this function. */
1577 java_dup_lang_specific_decl (tree node
)
1580 struct lang_decl
*x
;
1582 if (!DECL_LANG_SPECIFIC (node
))
1585 lang_decl_size
= sizeof (struct lang_decl
);
1586 x
= ggc_alloc (lang_decl_size
);
1587 memcpy (x
, DECL_LANG_SPECIFIC (node
), lang_decl_size
);
1588 DECL_LANG_SPECIFIC (node
) = x
;
1592 give_name_to_locals (JCF
*jcf
)
1594 int i
, n
= DECL_LOCALVARIABLES_OFFSET (current_function_decl
);
1595 int code_offset
= DECL_CODE_OFFSET (current_function_decl
);
1597 pending_local_decls
= NULL_TREE
;
1601 n
= JCF_readu2 (jcf
);
1602 for (i
= 0; i
< n
; i
++)
1604 int start_pc
= JCF_readu2 (jcf
);
1605 int length
= JCF_readu2 (jcf
);
1606 int name_index
= JCF_readu2 (jcf
);
1607 int signature_index
= JCF_readu2 (jcf
);
1608 int slot
= JCF_readu2 (jcf
);
1609 tree name
= get_name_constant (jcf
, name_index
);
1610 tree type
= parse_signature (jcf
, signature_index
);
1611 if (slot
< DECL_ARG_SLOT_COUNT (current_function_decl
)
1613 && length
== DECL_CODE_LENGTH (current_function_decl
))
1615 tree decl
= TREE_VEC_ELT (decl_map
, slot
);
1616 DECL_NAME (decl
) = name
;
1617 SET_DECL_ASSEMBLER_NAME (decl
, name
);
1618 if (TREE_CODE (decl
) != PARM_DECL
|| TREE_TYPE (decl
) != type
)
1619 warning ("bad type in parameter debug info");
1624 int end_pc
= start_pc
+ length
;
1625 tree decl
= build_decl (VAR_DECL
, name
, type
);
1626 if (end_pc
> DECL_CODE_LENGTH (current_function_decl
))
1628 warning ("%Jbad PC range for debug info for local '%D'",
1630 end_pc
= DECL_CODE_LENGTH (current_function_decl
);
1633 /* Adjust start_pc if necessary so that the local's first
1634 store operation will use the relevant DECL as a
1635 destination. Fore more information, read the leading
1636 comments for expr.c:maybe_adjust_start_pc. */
1637 start_pc
= maybe_adjust_start_pc (jcf
, code_offset
, start_pc
, slot
);
1639 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl
);
1640 DECL_LOCAL_SLOT_NUMBER (decl
) = slot
;
1641 DECL_LOCAL_START_PC (decl
) = start_pc
;
1643 /* FIXME: The range used internally for exceptions and local
1644 variable ranges, is a half-open interval:
1645 start_pc <= pc < end_pc. However, the range used in the
1646 Java VM spec is inclusive at both ends:
1647 start_pc <= pc <= end_pc. */
1650 DECL_LOCAL_END_PC (decl
) = end_pc
;
1652 /* Now insert the new decl in the proper place in
1653 pending_local_decls. We are essentially doing an insertion sort,
1654 which works fine, since the list input will normally already
1656 ptr
= &pending_local_decls
;
1657 while (*ptr
!= NULL_TREE
1658 && (DECL_LOCAL_START_PC (*ptr
) > start_pc
1659 || (DECL_LOCAL_START_PC (*ptr
) == start_pc
1660 && DECL_LOCAL_END_PC (*ptr
) < end_pc
)))
1661 ptr
= &TREE_CHAIN (*ptr
);
1662 TREE_CHAIN (decl
) = *ptr
;
1667 pending_local_decls
= nreverse (pending_local_decls
);
1669 /* Fill in default names for the parameters. */
1670 for (parm
= DECL_ARGUMENTS (current_function_decl
), i
= 0;
1671 parm
!= NULL_TREE
; parm
= TREE_CHAIN (parm
), i
++)
1673 if (DECL_NAME (parm
) == NULL_TREE
)
1675 int arg_i
= METHOD_STATIC (current_function_decl
) ? i
+1 : i
;
1677 DECL_NAME (parm
) = get_identifier ("this");
1681 sprintf (buffer
, "ARG_%d", arg_i
);
1682 DECL_NAME (parm
) = get_identifier (buffer
);
1684 SET_DECL_ASSEMBLER_NAME (parm
, DECL_NAME (parm
));
1690 build_result_decl (tree fndecl
)
1692 tree restype
= TREE_TYPE (TREE_TYPE (fndecl
));
1693 tree result
= DECL_RESULT (fndecl
);
1696 /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1697 if (INTEGRAL_TYPE_P (restype
)
1698 && TYPE_PRECISION (restype
) < TYPE_PRECISION (integer_type_node
))
1699 restype
= integer_type_node
;
1700 result
= build_decl (RESULT_DECL
, NULL_TREE
, restype
);
1701 DECL_CONTEXT (result
) = fndecl
;
1702 DECL_RESULT (fndecl
) = result
;
1708 start_java_method (tree fndecl
)
1713 current_function_decl
= fndecl
;
1714 announce_function (fndecl
);
1716 i
= DECL_MAX_LOCALS(fndecl
) + DECL_MAX_STACK(fndecl
);
1717 decl_map
= make_tree_vec (i
);
1718 type_map
= xrealloc (type_map
, i
* sizeof (tree
));
1720 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1721 fprintf (stderr
, "%s:\n", lang_printable_name (fndecl
, 2));
1723 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1724 pushlevel (1); /* Push parameters. */
1726 ptr
= &DECL_ARGUMENTS (fndecl
);
1727 for (tem
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
)), i
= 0;
1728 tem
!= end_params_node
; tem
= TREE_CHAIN (tem
), i
++)
1730 tree parm_name
= NULL_TREE
, parm_decl
;
1731 tree parm_type
= TREE_VALUE (tem
);
1732 if (i
>= DECL_MAX_LOCALS (fndecl
))
1735 parm_decl
= build_decl (PARM_DECL
, parm_name
, parm_type
);
1736 DECL_CONTEXT (parm_decl
) = fndecl
;
1737 if (targetm
.calls
.promote_prototypes (parm_type
)
1738 && TYPE_PRECISION (parm_type
) < TYPE_PRECISION (integer_type_node
)
1739 && INTEGRAL_TYPE_P (parm_type
))
1740 parm_type
= integer_type_node
;
1741 DECL_ARG_TYPE (parm_decl
) = parm_type
;
1744 ptr
= &TREE_CHAIN (parm_decl
);
1746 /* Add parm_decl to the decl_map. */
1747 push_jvm_slot (i
, parm_decl
);
1749 type_map
[i
] = TREE_TYPE (parm_decl
);
1750 if (TYPE_IS_WIDE (TREE_TYPE (parm_decl
)))
1753 type_map
[i
] = void_type_node
;
1757 DECL_ARG_SLOT_COUNT (current_function_decl
) = i
;
1759 while (i
< DECL_MAX_LOCALS(fndecl
))
1760 type_map
[i
++] = NULL_TREE
;
1762 build_result_decl (fndecl
);
1764 /* Push local variables. */
1769 end_java_method (void)
1771 tree fndecl
= current_function_decl
;
1773 /* pop out of function */
1776 /* pop out of its parameters */
1779 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl
)) = fndecl
;
1781 flag_unit_at_a_time
= 0;
1782 finish_method (fndecl
);
1784 current_function_decl
= NULL_TREE
;
1787 /* Prepare a method for expansion. */
1790 finish_method (tree fndecl
)
1792 tree
*tp
= &DECL_SAVED_TREE (fndecl
);
1794 /* Wrap body of synchronized methods in a monitorenter,
1795 plus monitorexit cleanup. */
1796 if (METHOD_SYNCHRONIZED (fndecl
))
1798 tree enter
, exit
, lock
;
1799 if (METHOD_STATIC (fndecl
))
1800 lock
= build_class_ref (DECL_CONTEXT (fndecl
));
1802 lock
= DECL_ARGUMENTS (fndecl
);
1803 BUILD_MONITOR_ENTER (enter
, lock
);
1804 BUILD_MONITOR_EXIT (exit
, lock
);
1805 *tp
= build (COMPOUND_EXPR
, void_type_node
,
1807 build (TRY_FINALLY_EXPR
, void_type_node
, *tp
, exit
));
1810 /* Prepend class initialization for static methods reachable from
1812 if (METHOD_STATIC (fndecl
) && ! METHOD_PRIVATE (fndecl
)
1813 && ! DECL_CLINIT_P (fndecl
)
1814 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl
))))
1816 tree clas
= DECL_CONTEXT (fndecl
);
1817 tree init
= build (CALL_EXPR
, void_type_node
,
1818 build_address_of (soft_initclass_node
),
1819 build_tree_list (NULL_TREE
, build_class_ref (clas
)),
1821 *tp
= build (COMPOUND_EXPR
, TREE_TYPE (*tp
), init
, *tp
);
1824 /* Convert function tree to GENERIC prior to inlining. */
1825 java_genericize (fndecl
);
1827 /* Store the end of the function, so that we get good line number
1828 info for the epilogue. */
1829 if (DECL_STRUCT_FUNCTION (fndecl
))
1830 cfun
= DECL_STRUCT_FUNCTION (fndecl
);
1832 allocate_struct_function (fndecl
);
1833 cfun
->function_end_locus
.file
= DECL_SOURCE_FILE (fndecl
);
1834 cfun
->function_end_locus
.line
= DECL_FUNCTION_LAST_LINE (fndecl
);
1836 /* Defer inlining and expansion to the cgraph optimizers. */
1837 cgraph_finalize_function (fndecl
, false);
1840 /* Optimize and expand a function's entire body. */
1843 java_expand_body (tree fndecl
)
1845 tree_rest_of_compilation (fndecl
, 0);
1848 /* We pessimistically marked all methods and fields external until we
1849 knew what set of classes we were planning to compile. Now mark those
1850 associated with CLASS to be generated locally as not external. */
1853 java_mark_decl_local (tree decl
)
1855 DECL_EXTERNAL (decl
) = 0;
1857 /* If we've already constructed DECL_RTL, give encode_section_info
1858 a second chance, now that we've changed the flags. */
1859 if (DECL_RTL_SET_P (decl
))
1860 make_decl_rtl (decl
, NULL
);
1864 java_mark_class_local (tree
class)
1868 for (t
= TYPE_FIELDS (class); t
; t
= TREE_CHAIN (t
))
1869 if (FIELD_STATIC (t
))
1870 java_mark_decl_local (t
);
1872 for (t
= TYPE_METHODS (class); t
; t
= TREE_CHAIN (t
))
1873 if (!METHOD_ABSTRACT (t
) && (!METHOD_NATIVE (t
) || flag_jni
))
1874 java_mark_decl_local (t
);
1877 /* Add a statement to a compound_expr. */
1880 add_stmt_to_compound (existing
, type
, stmt
)
1881 tree existing
, type
, stmt
;
1887 tree expr
= build (COMPOUND_EXPR
, type
, existing
, stmt
);
1888 TREE_SIDE_EFFECTS (expr
)
1889 = TREE_SIDE_EFFECTS (existing
) | TREE_SIDE_EFFECTS (stmt
);
1896 /* Add a statement to the compound_expr currently being
1900 java_add_stmt (stmt
)
1904 annotate_with_locus (stmt
, input_location
);
1906 return current_binding_level
->stmts
1907 = add_stmt_to_compound (current_binding_level
->stmts
,
1908 TREE_TYPE (stmt
), stmt
);
1911 /* Add a variable to the current scope. */
1914 java_add_local_var (tree decl
)
1916 tree
*vars
= ¤t_binding_level
->names
;
1918 TREE_CHAIN (decl
) = next
;
1920 DECL_CONTEXT (decl
) = current_function_decl
;
1921 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl
);
1925 /* Return a pointer to the compound_expr currently being
1931 return ¤t_binding_level
->stmts
;
1935 #include "gt-java-decl.h"