FSF GCC merge 02/23/03
[official-gcc.git] / gcc / java / decl.c
blob05f80ea03c58fa5b1f2e1ef127380e8b238f0513
1 /* Process declarations and variables for the GNU compiler for the
2 Java(TM) language.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 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. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "tree.h"
34 #include "rtl.h"
35 #include "real.h"
36 #include "toplev.h"
37 #include "flags.h"
38 #include "java-tree.h"
39 #include "jcf.h"
40 #include "function.h"
41 #include "expr.h"
42 #include "libfuncs.h"
43 #include "except.h"
44 #include "java-except.h"
45 #include "ggc.h"
46 #include "timevar.h"
47 #include "tree-inline.h"
49 #if defined (DEBUG_JAVA_BINDING_LEVELS)
50 extern void indent (void);
51 #endif
53 static tree push_jvm_slot (int, tree);
54 static tree lookup_name_current_level (tree);
55 static tree push_promoted_type (const char *, tree);
56 static struct binding_level *make_binding_level (void);
57 static tree create_primitive_vtable (const char *);
58 static tree check_local_named_variable (tree, tree, int, int *);
59 static tree check_local_unnamed_variable (tree, tree, tree);
60 static void dump_function (enum tree_dump_index, tree);
62 /* Name of the Cloneable class. */
63 tree java_lang_cloneable_identifier_node;
65 /* Name of the Serializable class. */
66 tree java_io_serializable_identifier_node;
68 /* Set to nonzero value in order to emit class initialization code
69 before static field references. */
70 extern int always_initialize_class_p;
72 /* The DECL_MAP is a mapping from (index, type) to a decl node.
73 If index < max_locals, it is the index of a local variable.
74 if index >= max_locals, then index-max_locals is a stack slot.
75 The DECL_MAP mapping is represented as a TREE_VEC whose elements
76 are a list of decls (VAR_DECL or PARM_DECL) chained by
77 DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
78 we search the chain for a decl with a matching TREE_TYPE. */
80 static GTY(()) tree decl_map;
82 /* A list of local variables VAR_DECLs for this method that we have seen
83 debug information, but we have not reached their starting (byte) PC yet. */
85 static GTY(()) tree pending_local_decls;
87 /* Push a local variable or stack slot into the decl_map,
88 and assign it an rtl. */
90 #if defined(DEBUG_JAVA_BINDING_LEVELS)
91 int binding_depth = 0;
92 int is_class_level = 0;
93 int current_pc;
95 void
96 indent (void)
98 register unsigned i;
100 for (i = 0; i < binding_depth*2; i++)
101 putc (' ', stderr);
103 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
105 static tree
106 push_jvm_slot (int index, tree decl)
108 struct rtx_def *rtl = NULL;
109 tree type = TREE_TYPE (decl);
110 tree tmp;
112 DECL_CONTEXT (decl) = current_function_decl;
113 layout_decl (decl, 0);
115 /* See if we have an appropriate rtl (i.e. same mode) at this index.
116 If so, we must use it. */
117 tmp = TREE_VEC_ELT (decl_map, index);
118 while (tmp != NULL_TREE)
120 if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (tmp)))
121 rtl = DECL_RTL_IF_SET (tmp);
122 if (rtl != NULL)
123 break;
124 tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
126 if (rtl != NULL)
127 SET_DECL_RTL (decl, rtl);
128 else
130 if (index >= DECL_MAX_LOCALS (current_function_decl))
131 DECL_REGISTER (decl) = 1;
132 expand_decl (decl);
135 /* Now link the decl into the decl_map. */
136 if (DECL_LANG_SPECIFIC (decl) == NULL)
138 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
139 DECL_LOCAL_START_PC (decl) = 0;
140 DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
141 DECL_LOCAL_SLOT_NUMBER (decl) = index;
143 DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
144 TREE_VEC_ELT (decl_map, index) = decl;
145 return decl;
148 /* Find out if 'decl' passed in fits the defined PC location better than
149 'best'. Return decl if it does, return best if it doesn't. If decl
150 is returned, then updated is set to true. */
152 static tree
153 check_local_named_variable (tree best, tree decl, int pc, int *updated)
155 if (pc >= DECL_LOCAL_START_PC (decl)
156 && pc < DECL_LOCAL_END_PC (decl))
158 if (best == NULL_TREE
159 || (DECL_LOCAL_START_PC (decl) > DECL_LOCAL_START_PC (best)
160 && DECL_LOCAL_END_PC (decl) < DECL_LOCAL_END_PC (best)))
162 *updated = 1;
163 return decl;
167 return best;
170 /* Find the best declaration based upon type. If 'decl' fits 'type' better
171 than 'best', return 'decl'. Otherwise return 'best'. */
173 static tree
174 check_local_unnamed_variable (tree best, tree decl, tree type)
176 if (TREE_TYPE (decl) == type
177 || (TREE_CODE (TREE_TYPE (decl)) == TREE_CODE (type)
178 && TYPE_PRECISION (TREE_TYPE (decl)) <= 32
179 && TYPE_PRECISION (type) <= 32
180 && TREE_CODE (type) != POINTER_TYPE)
181 || (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
182 && type == ptr_type_node))
184 if (best == NULL_TREE
185 || (TREE_TYPE (decl) == type && TREE_TYPE (best) != type))
186 return decl;
189 return best;
193 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
194 that is valid at PC (or -1 if any pc).
195 If there is no existing matching decl, allocate one. */
197 tree
198 find_local_variable (int index, tree type, int pc)
200 tree decl = TREE_VEC_ELT (decl_map, index);
201 tree best = NULL_TREE;
202 int found_scoped_var = 0;
204 /* Scan through every declaration that has been created in this slot. */
205 while (decl != NULL_TREE)
207 /* Variables created in give_name_to_locals() have a name and have
208 a specified scope, so we can handle them specifically. We want
209 to use the specific decls created for those so they are assigned
210 the right variables in the debugging information. */
211 if (DECL_NAME (decl) != NULL_TREE)
213 /* This is a variable we have a name for, so it has a scope
214 supplied in the class file. But it only matters when we
215 actually have a PC to use. If pc<0, then we are asking
216 for a stack slot and this decl won't be one of those. */
217 if (pc >= 0)
218 best = check_local_named_variable (best, decl, pc,
219 &found_scoped_var);
221 /* We scan for type information unless we found a variable in the
222 proper scope already. */
223 else if (!found_scoped_var)
225 /* If we don't have scoping information for a variable, we use
226 a different method to look it up. */
227 best = check_local_unnamed_variable (best, decl, type);
230 decl = DECL_LOCAL_SLOT_CHAIN (decl);
233 if (best != NULL_TREE)
234 return best;
236 /* If we don't find a match, create one with the type passed in. */
237 return push_jvm_slot (index, build_decl (VAR_DECL, NULL_TREE, type));
241 /* Same as find_local_index, except that INDEX is a stack index. */
243 tree
244 find_stack_slot (int index, tree type)
246 return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
247 type, -1);
250 struct binding_level
252 /* A chain of _DECL nodes for all variables, constants, functions,
253 * and typedef types. These are in the reverse of the order supplied.
255 tree names;
257 /* For each level, a list of shadowed outer-level local definitions
258 to be restored when this level is popped.
259 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
260 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
261 tree shadowed;
263 /* For each level (except not the global one),
264 a chain of BLOCK nodes for all the levels
265 that were entered and exited one level down. */
266 tree blocks;
268 /* The BLOCK node for this level, if one has been preallocated.
269 If 0, the BLOCK is allocated (if needed) when the level is popped. */
270 tree this_block;
272 /* The binding level which this one is contained in (inherits from). */
273 struct binding_level *level_chain;
275 /* The bytecode PC that marks the end of this level. */
276 int end_pc;
277 /* The bytecode PC that marks the start of this level. */
278 int start_pc;
280 #if defined(DEBUG_JAVA_BINDING_LEVELS)
281 /* Binding depth at which this level began. */
282 unsigned binding_depth;
283 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
286 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
288 /* The binding level currently in effect. */
290 static struct binding_level *current_binding_level;
292 /* A chain of binding_level structures awaiting reuse. */
294 static struct binding_level *free_binding_level;
296 /* The outermost binding level, for names of file scope.
297 This is created when the compiler is started and exists
298 through the entire run. */
300 static struct binding_level *global_binding_level;
302 /* A PC value bigger than any PC value we may ever may encounter. */
304 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
306 /* Binding level structures are initialized by copying this one. */
308 static const struct binding_level clear_binding_level
309 = {NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
310 NULL_BINDING_LEVEL, LARGEST_PC, 0};
312 #if 0
313 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
314 that have names. Here so we can clear out their names' definitions
315 at the end of the function. */
317 static tree named_labels;
319 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
321 static tree shadowed_labels;
322 #endif
324 tree java_global_trees[JTI_MAX];
326 /* Build (and pushdecl) a "promoted type" for all standard
327 types shorter than int. */
329 static tree
330 push_promoted_type (const char *name, tree actual_type)
332 tree type = make_node (TREE_CODE (actual_type));
333 #if 1
334 tree in_min = TYPE_MIN_VALUE (int_type_node);
335 tree in_max = TYPE_MAX_VALUE (int_type_node);
336 #else
337 tree in_min = TYPE_MIN_VALUE (actual_type);
338 tree in_max = TYPE_MAX_VALUE (actual_type);
339 #endif
340 TYPE_MIN_VALUE (type) = copy_node (in_min);
341 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
342 TYPE_MAX_VALUE (type) = copy_node (in_max);
343 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
344 TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
345 layout_type (type);
346 pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
347 return type;
350 /* Return a definition for a builtin function named NAME and whose data type
351 is TYPE. TYPE should be a function type with argument types.
352 FUNCTION_CODE tells later passes how to compile calls to this function.
353 See tree.h for its possible values.
355 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
356 the name to be called if we can't opencode the function. If
357 ATTRS is nonzero, use that for the function's attribute list. */
359 tree
360 builtin_function (const char *name,
361 tree type,
362 int function_code,
363 enum built_in_class class,
364 const char *library_name,
365 tree attrs ATTRIBUTE_UNUSED)
367 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
368 DECL_EXTERNAL (decl) = 1;
369 TREE_PUBLIC (decl) = 1;
370 if (library_name)
371 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
372 make_decl_rtl (decl, NULL);
373 pushdecl (decl);
374 DECL_BUILT_IN_CLASS (decl) = class;
375 DECL_FUNCTION_CODE (decl) = function_code;
376 return decl;
379 /* Return tree that represents a vtable for a primitive array. */
380 static tree
381 create_primitive_vtable (const char *name)
383 tree r;
384 char buf[50];
386 sprintf (buf, "_Jv_%sVTable", name);
387 r = build_decl (VAR_DECL, get_identifier (buf), ptr_type_node);
388 DECL_EXTERNAL (r) = 1;
389 return r;
392 void
393 java_init_decl_processing (void)
395 register tree endlink;
396 tree field = NULL_TREE;
397 tree t;
399 init_class_processing ();
400 init_resource_processing ();
402 current_function_decl = NULL;
403 current_binding_level = NULL_BINDING_LEVEL;
404 free_binding_level = NULL_BINDING_LEVEL;
405 pushlevel (0); /* make the binding_level structure for global names */
406 global_binding_level = current_binding_level;
408 /* The code here must be similar to build_common_tree_nodes{,_2} in
409 tree.c, especially as to the order of initializing common nodes. */
410 error_mark_node = make_node (ERROR_MARK);
411 TREE_TYPE (error_mark_node) = error_mark_node;
413 /* Create sizetype first - needed for other types. */
414 initialize_sizetypes ();
416 byte_type_node = make_signed_type (8);
417 pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
418 short_type_node = make_signed_type (16);
419 pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
420 int_type_node = make_signed_type (32);
421 pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
422 long_type_node = make_signed_type (64);
423 pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));
425 unsigned_byte_type_node = make_unsigned_type (8);
426 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
427 unsigned_byte_type_node));
428 unsigned_short_type_node = make_unsigned_type (16);
429 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
430 unsigned_short_type_node));
431 unsigned_int_type_node = make_unsigned_type (32);
432 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
433 unsigned_int_type_node));
434 unsigned_long_type_node = make_unsigned_type (64);
435 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
436 unsigned_long_type_node));
438 set_sizetype (make_unsigned_type (POINTER_SIZE));
440 /* Define these next since types below may used them. */
441 integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
442 integer_zero_node = build_int_2 (0, 0);
443 integer_one_node = build_int_2 (1, 0);
444 integer_two_node = build_int_2 (2, 0);
445 integer_four_node = build_int_2 (4, 0);
446 integer_minus_one_node = build_int_2 (-1, -1);
448 /* A few values used for range checking in the lexer. */
449 decimal_int_max = build_int_2 (0x80000000, 0);
450 TREE_TYPE (decimal_int_max) = unsigned_int_type_node;
451 #if HOST_BITS_PER_WIDE_INT == 64
452 decimal_long_max = build_int_2 (0x8000000000000000LL, 0);
453 #else
454 #if HOST_BITS_PER_WIDE_INT == 32
455 decimal_long_max = build_int_2 (0, 0x80000000);
456 #else
457 #error "unsupported size"
458 #endif
459 #endif
460 TREE_TYPE (decimal_long_max) = unsigned_long_type_node;
462 size_zero_node = size_int (0);
463 size_one_node = size_int (1);
464 bitsize_zero_node = bitsize_int (0);
465 bitsize_one_node = bitsize_int (1);
466 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
468 long_zero_node = build_int_2 (0, 0);
469 TREE_TYPE (long_zero_node) = long_type_node;
471 void_type_node = make_node (VOID_TYPE);
472 pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
473 layout_type (void_type_node); /* Uses size_zero_node */
474 ptr_type_node = build_pointer_type (void_type_node);
475 t = make_node (VOID_TYPE);
476 layout_type (t); /* Uses size_zero_node */
477 return_address_type_node = build_pointer_type (t);
479 null_pointer_node = build_int_2 (0, 0);
480 TREE_TYPE (null_pointer_node) = ptr_type_node;
482 /* Used by the parser to represent empty statements and blocks. */
483 empty_stmt_node = build1 (NOP_EXPR, void_type_node, size_zero_node);
484 CAN_COMPLETE_NORMALLY (empty_stmt_node) = 1;
486 #if 0
487 /* Make a type to be the domain of a few array types
488 whose domains don't really matter.
489 200 is small enough that it always fits in size_t
490 and large enough that it can hold most function names for the
491 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
492 short_array_type_node = build_prim_array_type (short_type_node, 200);
493 #endif
494 char_type_node = make_node (CHAR_TYPE);
495 TYPE_PRECISION (char_type_node) = 16;
496 fixup_unsigned_type (char_type_node);
497 pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));
499 boolean_type_node = make_node (BOOLEAN_TYPE);
500 TYPE_PRECISION (boolean_type_node) = 1;
501 fixup_unsigned_type (boolean_type_node);
502 pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
503 boolean_type_node));
504 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
505 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
507 promoted_byte_type_node
508 = push_promoted_type ("promoted_byte", byte_type_node);
509 promoted_short_type_node
510 = push_promoted_type ("promoted_short", short_type_node);
511 promoted_char_type_node
512 = push_promoted_type ("promoted_char", char_type_node);
513 promoted_boolean_type_node
514 = push_promoted_type ("promoted_boolean", boolean_type_node);
516 float_type_node = make_node (REAL_TYPE);
517 TYPE_PRECISION (float_type_node) = 32;
518 pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
519 float_type_node));
520 layout_type (float_type_node);
522 double_type_node = make_node (REAL_TYPE);
523 TYPE_PRECISION (double_type_node) = 64;
524 pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
525 double_type_node));
526 layout_type (double_type_node);
528 float_zero_node = build_real (float_type_node, dconst0);
529 double_zero_node = build_real (double_type_node, dconst0);
531 /* These are the vtables for arrays of primitives. */
532 boolean_array_vtable = create_primitive_vtable ("boolean");
533 byte_array_vtable = create_primitive_vtable ("byte");
534 char_array_vtable = create_primitive_vtable ("char");
535 short_array_vtable = create_primitive_vtable ("short");
536 int_array_vtable = create_primitive_vtable ("int");
537 long_array_vtable = create_primitive_vtable ("long");
538 float_array_vtable = create_primitive_vtable ("float");
539 double_array_vtable = create_primitive_vtable ("double");
541 /* As you're adding items here, please update the code right after
542 this section, so that the filename containing the source code of
543 the pre-defined class gets registered correctly. */
544 unqualified_object_id_node = get_identifier ("Object");
545 object_type_node = lookup_class (get_identifier ("java.lang.Object"));
546 object_ptr_type_node = promote_type (object_type_node);
547 string_type_node = lookup_class (get_identifier ("java.lang.String"));
548 string_ptr_type_node = promote_type (string_type_node);
549 class_type_node = lookup_class (get_identifier ("java.lang.Class"));
550 throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
551 exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
552 runtime_exception_type_node =
553 lookup_class (get_identifier ("java.lang.RuntimeException"));
554 error_exception_type_node =
555 lookup_class (get_identifier ("java.lang.Error"));
556 class_not_found_type_node =
557 lookup_class (get_identifier ("java.lang.ClassNotFoundException"));
558 no_class_def_found_type_node =
559 lookup_class (get_identifier ("java.lang.NoClassDefFoundError"));
561 rawdata_ptr_type_node
562 = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
564 add_predefined_file (get_identifier ("java/lang/Class.java"));
565 add_predefined_file (get_identifier ("java/lang/Error.java"));
566 add_predefined_file (get_identifier ("java/lang/Object.java"));
567 add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
568 add_predefined_file (get_identifier ("java/lang/String.java"));
569 add_predefined_file (get_identifier ("java/lang/Throwable.java"));
570 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
571 add_predefined_file (get_identifier ("java/lang/Exception.java"));
572 add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
573 add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
574 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
576 methodtable_type = make_node (RECORD_TYPE);
577 layout_type (methodtable_type);
578 build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
579 methodtable_ptr_type = build_pointer_type (methodtable_type);
581 TYPE_identifier_node = get_identifier ("TYPE");
582 init_identifier_node = get_identifier ("<init>");
583 clinit_identifier_node = get_identifier ("<clinit>");
584 finit_identifier_node = get_identifier ("finit$");
585 instinit_identifier_node = get_identifier ("instinit$");
586 void_signature_node = get_identifier ("()V");
587 length_identifier_node = get_identifier ("length");
588 finalize_identifier_node = get_identifier ("finalize");
589 this_identifier_node = get_identifier ("this");
590 super_identifier_node = get_identifier ("super");
591 continue_identifier_node = get_identifier ("continue");
592 access0_identifier_node = get_identifier ("access$0");
593 classdollar_identifier_node = get_identifier ("class$");
595 java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
596 java_io_serializable_identifier_node =
597 get_identifier ("java.io.Serializable");
599 /* for lack of a better place to put this stub call */
600 init_expr_processing();
602 utf8const_type = make_node (RECORD_TYPE);
603 PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
604 PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
605 FINISH_RECORD (utf8const_type);
606 utf8const_ptr_type = build_pointer_type (utf8const_type);
608 constants_type_node = make_node (RECORD_TYPE);
609 PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
610 PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
611 PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
612 FINISH_RECORD (constants_type_node);
613 build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
615 access_flags_type_node = unsigned_short_type_node;
617 dtable_type = make_node (RECORD_TYPE);
618 dtable_ptr_type = build_pointer_type (dtable_type);
620 one_elt_array_domain_type = build_index_type (integer_one_node);
621 otable_type = build_array_type (integer_type_node,
622 one_elt_array_domain_type);
623 TYPE_NONALIASED_COMPONENT (otable_type) = 1;
624 otable_ptr_type = build_pointer_type (otable_type);
626 method_symbol_type = make_node (RECORD_TYPE);
627 PUSH_FIELD (method_symbol_type, field, "clname", utf8const_ptr_type);
628 PUSH_FIELD (method_symbol_type, field, "name", utf8const_ptr_type);
629 PUSH_FIELD (method_symbol_type, field, "signature", utf8const_ptr_type);
630 FINISH_RECORD (method_symbol_type);
632 method_symbols_array_type = build_array_type (method_symbol_type,
633 one_elt_array_domain_type);
634 method_symbols_array_ptr_type = build_pointer_type
635 (method_symbols_array_type);
637 otable_decl = build_decl (VAR_DECL, get_identifier ("otable"), otable_type);
638 DECL_EXTERNAL (otable_decl) = 1;
639 TREE_STATIC (otable_decl) = 1;
640 TREE_READONLY (otable_decl) = 1;
641 pushdecl (otable_decl);
643 otable_syms_decl = build_decl (VAR_DECL, get_identifier ("otable_syms"),
644 method_symbols_array_type);
645 TREE_STATIC (otable_syms_decl) = 1;
646 TREE_CONSTANT (otable_syms_decl) = 1;
647 pushdecl (otable_syms_decl);
649 PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
650 /* This isn't exactly true, but it is what we have in the source.
651 There is an unresolved issue here, which is whether the vtable
652 should be marked by the GC. */
653 if (! flag_hash_synchronization)
654 PUSH_FIELD (object_type_node, field, "sync_info",
655 build_pointer_type (object_type_node));
656 for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
657 FIELD_PRIVATE (t) = 1;
658 FINISH_RECORD (object_type_node);
660 field_type_node = make_node (RECORD_TYPE);
661 field_ptr_type_node = build_pointer_type (field_type_node);
662 method_type_node = make_node (RECORD_TYPE);
663 method_ptr_type_node = build_pointer_type (method_type_node);
665 set_super_info (0, class_type_node, object_type_node, 0);
666 set_super_info (0, string_type_node, object_type_node, 0);
667 class_ptr_type = build_pointer_type (class_type_node);
669 PUSH_FIELD (class_type_node, field, "next", class_ptr_type);
670 PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
671 PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
672 PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
673 PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
674 PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
675 PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
676 PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
677 PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
678 PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
679 PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
680 PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
681 PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
682 PUSH_FIELD (class_type_node, field, "otable", otable_ptr_type);
683 PUSH_FIELD (class_type_node, field, "otable_syms",
684 method_symbols_array_ptr_type);
685 PUSH_FIELD (class_type_node, field, "interfaces",
686 build_pointer_type (class_ptr_type));
687 PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
688 PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
689 PUSH_FIELD (class_type_node, field, "state", byte_type_node);
690 PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
691 PUSH_FIELD (class_type_node, field, "depth", short_type_node);
692 PUSH_FIELD (class_type_node, field, "ancestors", ptr_type_node);
693 PUSH_FIELD (class_type_node, field, "idt", ptr_type_node);
694 PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);
695 PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
696 PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
697 for (t = TYPE_FIELDS (class_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
698 FIELD_PRIVATE (t) = 1;
699 push_super_field (class_type_node, object_type_node);
701 FINISH_RECORD (class_type_node);
702 build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
704 field_info_union_node = make_node (UNION_TYPE);
705 PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
706 PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
707 #if 0
708 PUSH_FIELD (field_info_union_node, field, "idx", unsigned_short_type_node);
709 #endif
710 layout_type (field_info_union_node);
712 PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
713 PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
714 PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
715 PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
716 PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
717 FINISH_RECORD (field_type_node);
718 build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
720 nativecode_ptr_array_type_node
721 = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
723 PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
724 PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
725 FINISH_RECORD (dtable_type);
726 build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
728 #define jint_type int_type_node
729 #define jint_ptr_type ptr_type_node
731 jexception_type = make_node (RECORD_TYPE);
732 PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
733 PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
734 PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
735 PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
736 FINISH_RECORD (jexception_type);
737 build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
738 jexception_ptr_type = build_pointer_type (jexception_type);
740 lineNumberEntry_type = make_node (RECORD_TYPE);
741 PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
742 PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
743 FINISH_RECORD (lineNumberEntry_type);
745 lineNumbers_type = make_node (RECORD_TYPE);
746 PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
747 FINISH_RECORD (lineNumbers_type);
749 #define instn_ptr_type_node ptr_type_node /* XXX JH */
751 #define lineNumbers_ptr_type_node build_pointer_type(lineNumbers_type)
753 PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
754 PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
755 PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
756 PUSH_FIELD (method_type_node, field, "index", unsigned_short_type_node);
757 PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
758 PUSH_FIELD (method_type_node, field, "throws", ptr_type_node);
759 FINISH_RECORD (method_type_node);
760 build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
762 endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
764 t = tree_cons (NULL_TREE, class_ptr_type,
765 tree_cons (NULL_TREE, int_type_node, endlink));
766 alloc_object_node = builtin_function ("_Jv_AllocObject",
767 build_function_type (ptr_type_node, t),
768 0, NOT_BUILT_IN, NULL, NULL_TREE);
769 DECL_IS_MALLOC (alloc_object_node) = 1;
770 alloc_no_finalizer_node =
771 builtin_function ("_Jv_AllocObjectNoFinalizer",
772 build_function_type (ptr_type_node, t),
773 0, NOT_BUILT_IN, NULL, NULL_TREE);
774 DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
776 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
777 soft_initclass_node = builtin_function ("_Jv_InitClass",
778 build_function_type (void_type_node,
780 0, NOT_BUILT_IN, NULL, NULL_TREE);
782 throw_node = builtin_function ("_Jv_Throw",
783 build_function_type (ptr_type_node, t),
784 0, NOT_BUILT_IN, NULL, NULL_TREE);
785 /* Mark throw_nodes as `noreturn' functions with side effects. */
786 TREE_THIS_VOLATILE (throw_node) = 1;
787 TREE_SIDE_EFFECTS (throw_node) = 1;
789 t = build_function_type (int_type_node, endlink);
790 soft_monitorenter_node
791 = builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
792 NULL, NULL_TREE);
793 soft_monitorexit_node
794 = builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
795 NULL, NULL_TREE);
797 t = tree_cons (NULL_TREE, int_type_node,
798 tree_cons (NULL_TREE, int_type_node, endlink));
799 soft_newarray_node
800 = builtin_function ("_Jv_NewPrimArray",
801 build_function_type(ptr_type_node, t),
802 0, NOT_BUILT_IN, NULL, NULL_TREE);
803 DECL_IS_MALLOC (soft_newarray_node) = 1;
805 t = tree_cons (NULL_TREE, int_type_node,
806 tree_cons (NULL_TREE, class_ptr_type,
807 tree_cons (NULL_TREE, object_ptr_type_node, endlink)));
808 soft_anewarray_node
809 = builtin_function ("_Jv_NewObjectArray",
810 build_function_type (ptr_type_node, t),
811 0, NOT_BUILT_IN, NULL, NULL_TREE);
812 DECL_IS_MALLOC (soft_anewarray_node) = 1;
814 /* There is no endlink here because _Jv_NewMultiArray is a varargs
815 function. */
816 t = tree_cons (NULL_TREE, ptr_type_node,
817 tree_cons (NULL_TREE, int_type_node, NULL_TREE));
818 soft_multianewarray_node
819 = builtin_function ("_Jv_NewMultiArray",
820 build_function_type (ptr_type_node, t),
821 0, NOT_BUILT_IN, NULL, NULL_TREE);
822 DECL_IS_MALLOC (soft_multianewarray_node) = 1;
824 t = build_function_type (void_type_node,
825 tree_cons (NULL_TREE, int_type_node, endlink));
826 soft_badarrayindex_node
827 = builtin_function ("_Jv_ThrowBadArrayIndex", t,
828 0, NOT_BUILT_IN, NULL, NULL_TREE);
829 /* Mark soft_badarrayindex_node as a `noreturn' function with side
830 effects. */
831 TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
832 TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
834 soft_nullpointer_node
835 = builtin_function ("_Jv_ThrowNullPointerException",
836 build_function_type (void_type_node, endlink),
837 0, NOT_BUILT_IN, NULL, NULL_TREE);
838 /* Mark soft_nullpointer_node as a `noreturn' function with side
839 effects. */
840 TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
841 TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
843 t = tree_cons (NULL_TREE, class_ptr_type,
844 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
845 soft_checkcast_node
846 = builtin_function ("_Jv_CheckCast",
847 build_function_type (ptr_type_node, t),
848 0, NOT_BUILT_IN, NULL, NULL_TREE);
849 t = tree_cons (NULL_TREE, object_ptr_type_node,
850 tree_cons (NULL_TREE, class_ptr_type, endlink));
851 soft_instanceof_node
852 = builtin_function ("_Jv_IsInstanceOf",
853 build_function_type (boolean_type_node, t),
854 0, NOT_BUILT_IN, NULL, NULL_TREE);
855 t = tree_cons (NULL_TREE, object_ptr_type_node,
856 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
857 soft_checkarraystore_node
858 = builtin_function ("_Jv_CheckArrayStore",
859 build_function_type (void_type_node, t),
860 0, NOT_BUILT_IN, NULL, NULL_TREE);
861 t = tree_cons (NULL_TREE, ptr_type_node,
862 tree_cons (NULL_TREE, ptr_type_node,
863 tree_cons (NULL_TREE, int_type_node, endlink)));
864 soft_lookupinterfacemethod_node
865 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
866 build_function_type (ptr_type_node, t),
867 0, NOT_BUILT_IN, NULL, NULL_TREE);
869 t = tree_cons (NULL_TREE, object_ptr_type_node,
870 tree_cons (NULL_TREE, ptr_type_node,
871 tree_cons (NULL_TREE, ptr_type_node,
872 tree_cons (NULL_TREE, int_type_node,
873 endlink))));
874 soft_lookupjnimethod_node
875 = builtin_function ("_Jv_LookupJNIMethod",
876 build_function_type (ptr_type_node, t),
877 0, NOT_BUILT_IN, NULL, NULL_TREE);
878 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
879 soft_getjnienvnewframe_node
880 = builtin_function ("_Jv_GetJNIEnvNewFrame",
881 build_function_type (ptr_type_node, t),
882 0, NOT_BUILT_IN, NULL, NULL_TREE);
883 soft_jnipopsystemframe_node
884 = builtin_function ("_Jv_JNI_PopSystemFrame",
885 build_function_type (ptr_type_node, t),
886 0, NOT_BUILT_IN, NULL, NULL_TREE);
888 t = tree_cons (NULL_TREE, double_type_node,
889 tree_cons (NULL_TREE, double_type_node, endlink));
890 soft_fmod_node
891 = builtin_function ("__builtin_fmod",
892 build_function_type (double_type_node, t),
893 BUILT_IN_FMOD, BUILT_IN_NORMAL, "fmod", NULL_TREE);
895 #if 0
896 t = tree_cons (NULL_TREE, float_type_node,
897 tree_cons (NULL_TREE, float_type_node, endlink));
898 soft_fmodf_node
899 = builtin_function ("__builtin_fmodf",
900 build_function_type (float_type_node, t),
901 BUILT_IN_FMOD, BUILT_IN_NORMAL, "fmodf", NULL_TREE);
902 #endif
904 soft_idiv_node
905 = builtin_function ("_Jv_divI",
906 build_function_type (int_type_node, t),
907 0, NOT_BUILT_IN, NULL, NULL_TREE);
909 soft_irem_node
910 = builtin_function ("_Jv_remI",
911 build_function_type (int_type_node, t),
912 0, NOT_BUILT_IN, NULL, NULL_TREE);
914 soft_ldiv_node
915 = builtin_function ("_Jv_divJ",
916 build_function_type (long_type_node, t),
917 0, NOT_BUILT_IN, NULL, NULL_TREE);
919 soft_lrem_node
920 = builtin_function ("_Jv_remJ",
921 build_function_type (long_type_node, t),
922 0, NOT_BUILT_IN, NULL, NULL_TREE);
924 /* Initialize variables for except.c. */
925 eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
926 ? "__gcj_personality_sj0"
927 : "__gcj_personality_v0");
928 lang_eh_runtime_type = prepare_eh_table_type;
930 init_jcf_parse ();
932 initialize_builtins ();
936 /* Look up NAME in the current binding level and its superiors
937 in the namespace of variables, functions and typedefs.
938 Return a ..._DECL node of some kind representing its definition,
939 or return 0 if it is undefined. */
941 tree
942 lookup_name (tree name)
944 register tree val;
945 if (current_binding_level != global_binding_level
946 && IDENTIFIER_LOCAL_VALUE (name))
947 val = IDENTIFIER_LOCAL_VALUE (name);
948 else
949 val = IDENTIFIER_GLOBAL_VALUE (name);
950 return val;
953 /* Similar to `lookup_name' but look only at current binding level and
954 the previous one if its the parameter level. */
956 static tree
957 lookup_name_current_level (tree name)
959 register tree t;
961 if (current_binding_level == global_binding_level)
962 return IDENTIFIER_GLOBAL_VALUE (name);
964 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
965 return 0;
967 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
968 if (DECL_NAME (t) == name)
969 break;
971 return t;
974 /* Use a binding level to record a labeled block declaration */
976 void
977 push_labeled_block (tree lb)
979 register tree name = DECL_NAME (LABELED_BLOCK_LABEL (lb));
980 register struct binding_level *b = current_binding_level;
981 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
982 if (oldlocal != 0)
983 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
984 TREE_CHAIN (lb) = b->names;
985 b->names = lb;
986 IDENTIFIER_LOCAL_VALUE (name) = lb;
989 /* Pop the current binding level, reinstalling values for the previous
990 labeled block */
992 void
993 pop_labeled_block (void)
995 struct binding_level *b = current_binding_level;
996 tree label = b->names;
997 IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label))) =
998 NULL_TREE;
999 if (b->shadowed)
1000 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b->shadowed)) =
1001 TREE_VALUE (b->shadowed);
1003 /* Pop the current level, and free the structure for reuse. */
1004 current_binding_level = current_binding_level->level_chain;
1005 b->level_chain = free_binding_level;
1006 free_binding_level = b;
1009 /* Record a decl-node X as belonging to the current lexical scope.
1010 Check for errors (such as an incompatible declaration for the same
1011 name already seen in the same scope).
1013 Returns either X or an old decl for the same name.
1014 If an old decl is returned, it may have been smashed
1015 to agree with what X says. */
1017 tree
1018 pushdecl (tree x)
1020 register tree t;
1021 register tree name = DECL_NAME (x);
1022 register struct binding_level *b = current_binding_level;
1024 if (TREE_CODE (x) != TYPE_DECL)
1025 DECL_CONTEXT (x) = current_function_decl;
1026 if (name)
1028 const char *file;
1029 int line;
1031 t = lookup_name_current_level (name);
1032 if (t != 0 && t == error_mark_node)
1033 /* error_mark_node is 0 for a while during initialization! */
1035 t = 0;
1036 error_with_decl (x, "`%s' used prior to declaration");
1039 if (t != 0)
1041 file = DECL_SOURCE_FILE (t);
1042 line = DECL_SOURCE_LINE (t);
1045 /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1046 to point to the TYPE_DECL.
1047 Since Java does not have typedefs, a type can only have
1048 one (true) name, given by a class, interface, or builtin. */
1049 if (TREE_CODE (x) == TYPE_DECL
1050 && TYPE_NAME (TREE_TYPE (x)) == 0
1051 && TREE_TYPE (x) != error_mark_node)
1053 TYPE_NAME (TREE_TYPE (x)) = x;
1054 TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1057 /* This name is new in its binding level.
1058 Install the new declaration and return it. */
1059 if (b == global_binding_level)
1061 /* Install a global value. */
1063 IDENTIFIER_GLOBAL_VALUE (name) = x;
1065 else
1067 /* Here to install a non-global value. */
1068 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1069 IDENTIFIER_LOCAL_VALUE (name) = x;
1071 #if 0
1072 /* Warn if shadowing an argument at the top level of the body. */
1073 if (oldlocal != 0 && !DECL_EXTERNAL (x)
1074 /* This warning doesn't apply to the parms of a nested fcn. */
1075 && ! current_binding_level->parm_flag
1076 /* Check that this is one level down from the parms. */
1077 && current_binding_level->level_chain->parm_flag
1078 /* Check that the decl being shadowed
1079 comes from the parm level, one level up. */
1080 && chain_member (oldlocal, current_binding_level->level_chain->names))
1082 if (TREE_CODE (oldlocal) == PARM_DECL)
1083 pedwarn ("declaration of `%s' shadows a parameter",
1084 IDENTIFIER_POINTER (name));
1085 else
1086 pedwarn ("declaration of `%s' shadows a symbol from the parameter list",
1087 IDENTIFIER_POINTER (name));
1090 /* Maybe warn if shadowing something else. */
1091 else if (warn_shadow && !DECL_EXTERNAL (x)
1092 /* No shadow warnings for internally generated vars. */
1093 && DECL_SOURCE_LINE (x) != 0
1094 /* No shadow warnings for vars made for inlining. */
1095 && ! DECL_FROM_INLINE (x))
1097 const char *warnstring = 0;
1099 if (TREE_CODE (x) == PARM_DECL
1100 && current_binding_level->level_chain->parm_flag)
1101 /* Don't warn about the parm names in function declarator
1102 within a function declarator.
1103 It would be nice to avoid warning in any function
1104 declarator in a declaration, as opposed to a definition,
1105 but there is no way to tell it's not a definition. */
1107 else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
1108 warnstring = "declaration of `%s' shadows a parameter";
1109 else if (oldlocal != 0)
1110 warnstring = "declaration of `%s' shadows previous local";
1111 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1112 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1113 warnstring = "declaration of `%s' shadows global declaration";
1115 if (warnstring)
1116 warning (warnstring, IDENTIFIER_POINTER (name));
1118 #endif
1120 /* If storing a local value, there may already be one (inherited).
1121 If so, record it for restoration when this binding level ends. */
1122 if (oldlocal != 0)
1123 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1127 /* Put decls on list in reverse order.
1128 We will reverse them later if necessary. */
1129 TREE_CHAIN (x) = b->names;
1130 b->names = x;
1132 return x;
1135 void
1136 pushdecl_force_head (tree x)
1138 current_binding_level->names = x;
1141 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1143 tree
1144 pushdecl_top_level (tree x)
1146 register tree t;
1147 register struct binding_level *b = current_binding_level;
1149 current_binding_level = global_binding_level;
1150 t = pushdecl (x);
1151 current_binding_level = b;
1152 return t;
1155 /* Nonzero if we are currently in the global binding level. */
1158 global_bindings_p (void)
1160 return current_binding_level == global_binding_level;
1163 /* Return the list of declarations of the current level.
1164 Note that this list is in reverse order unless/until
1165 you nreverse it; and when you do nreverse it, you must
1166 store the result back using `storedecls' or you will lose. */
1168 tree
1169 getdecls (void)
1171 return current_binding_level->names;
1174 /* Create a new `struct binding_level'. */
1176 static struct binding_level *
1177 make_binding_level (void)
1179 /* NOSTRICT */
1180 return xmalloc (sizeof (struct binding_level));
1183 void
1184 pushlevel (int unused ATTRIBUTE_UNUSED)
1186 register struct binding_level *newlevel = NULL_BINDING_LEVEL;
1188 #if 0
1189 /* If this is the top level of a function,
1190 just make sure that NAMED_LABELS is 0. */
1192 if (current_binding_level == global_binding_level)
1193 named_labels = 0;
1194 #endif
1196 /* Reuse or create a struct for this binding level. */
1198 if (free_binding_level)
1200 newlevel = free_binding_level;
1201 free_binding_level = free_binding_level->level_chain;
1203 else
1205 newlevel = make_binding_level ();
1208 /* Add this level to the front of the chain (stack) of levels that
1209 are active. */
1211 *newlevel = clear_binding_level;
1212 newlevel->level_chain = current_binding_level;
1213 current_binding_level = newlevel;
1214 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1215 newlevel->binding_depth = binding_depth;
1216 indent ();
1217 fprintf (stderr, "push %s level 0x%08x pc %d\n",
1218 (is_class_level) ? "class" : "block", newlevel, current_pc);
1219 is_class_level = 0;
1220 binding_depth++;
1221 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1224 /* Exit a binding level.
1225 Pop the level off, and restore the state of the identifier-decl mappings
1226 that were in effect when this level was entered.
1228 If KEEP is nonzero, this level had explicit declarations, so
1229 and create a "block" (a BLOCK node) for the level
1230 to record its declarations and subblocks for symbol table output.
1232 If FUNCTIONBODY is nonzero, this level is the body of a function,
1233 so create a block as if KEEP were set and also clear out all
1234 label names.
1236 If REVERSE is nonzero, reverse the order of decls before putting
1237 them into the BLOCK. */
1239 tree
1240 poplevel (int keep, int reverse, int functionbody)
1242 register tree link;
1243 /* The chain of decls was accumulated in reverse order.
1244 Put it into forward order, just for cleanliness. */
1245 tree decls;
1246 tree subblocks = current_binding_level->blocks;
1247 tree block = 0;
1248 tree decl;
1249 int block_previously_created;
1251 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1252 binding_depth--;
1253 indent ();
1254 if (current_binding_level->end_pc != LARGEST_PC)
1255 fprintf (stderr, "pop %s level 0x%08x pc %d (end pc %d)\n",
1256 (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1257 current_binding_level->end_pc);
1258 else
1259 fprintf (stderr, "pop %s level 0x%08x pc %d\n",
1260 (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1261 #if 0
1262 if (is_class_level != (current_binding_level == class_binding_level))
1264 indent ();
1265 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
1267 is_class_level = 0;
1268 #endif
1269 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1271 /* Get the decls in the order they were written.
1272 Usually current_binding_level->names is in reverse order.
1273 But parameter decls were previously put in forward order. */
1275 if (reverse)
1276 current_binding_level->names
1277 = decls = nreverse (current_binding_level->names);
1278 else
1279 decls = current_binding_level->names;
1281 /* Output any nested inline functions within this block
1282 if they weren't already output. */
1284 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1285 if (TREE_CODE (decl) == FUNCTION_DECL
1286 && ! TREE_ASM_WRITTEN (decl)
1287 && DECL_INITIAL (decl) != 0
1288 && TREE_ADDRESSABLE (decl))
1290 /* If this decl was copied from a file-scope decl
1291 on account of a block-scope extern decl,
1292 propagate TREE_ADDRESSABLE to the file-scope decl.
1294 DECL_ABSTRACT_ORIGIN can be set to itself if warn_return_type is
1295 true, since then the decl goes through save_for_inline_copying. */
1296 if (DECL_ABSTRACT_ORIGIN (decl) != 0
1297 && DECL_ABSTRACT_ORIGIN (decl) != decl)
1298 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1299 else
1301 push_function_context ();
1302 output_inline_function (decl);
1303 pop_function_context ();
1307 /* If there were any declarations in that level,
1308 or if this level is a function body,
1309 create a BLOCK to record them for the life of this function. */
1311 block = 0;
1312 block_previously_created = (current_binding_level->this_block != 0);
1313 if (block_previously_created)
1314 block = current_binding_level->this_block;
1315 else if (keep || functionbody)
1316 block = make_node (BLOCK);
1317 if (block != 0)
1319 BLOCK_VARS (block) = decls;
1320 BLOCK_SUBBLOCKS (block) = subblocks;
1323 /* In each subblock, record that this is its superior. */
1325 for (link = subblocks; link; link = TREE_CHAIN (link))
1326 BLOCK_SUPERCONTEXT (link) = block;
1328 /* Clear out the meanings of the local variables of this level. */
1330 for (link = decls; link; link = TREE_CHAIN (link))
1332 tree name = DECL_NAME (link);
1333 if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1335 /* If the ident. was used or addressed via a local extern decl,
1336 don't forget that fact. */
1337 if (DECL_EXTERNAL (link))
1339 if (TREE_USED (link))
1340 TREE_USED (name) = 1;
1341 if (TREE_ADDRESSABLE (link))
1342 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1344 IDENTIFIER_LOCAL_VALUE (name) = 0;
1348 /* Restore all name-meanings of the outer levels
1349 that were shadowed by this level. */
1351 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1352 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1354 /* If the level being exited is the top level of a function,
1355 check over all the labels, and clear out the current
1356 (function local) meanings of their names. */
1358 if (functionbody)
1360 /* If this is the top level block of a function,
1361 the vars are the function's parameters.
1362 Don't leave them in the BLOCK because they are
1363 found in the FUNCTION_DECL instead. */
1365 BLOCK_VARS (block) = 0;
1367 /* Clear out the definitions of all label names,
1368 since their scopes end here,
1369 and add them to BLOCK_VARS. */
1371 #if 0
1372 for (link = named_labels; link; link = TREE_CHAIN (link))
1374 register tree label = TREE_VALUE (link);
1376 if (DECL_INITIAL (label) == 0)
1378 error_with_decl (label, "label `%s' used but not defined");
1379 /* Avoid crashing later. */
1380 define_label (input_filename, lineno,
1381 DECL_NAME (label));
1383 else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
1384 warning_with_decl (label, "label `%s' defined but not used");
1385 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1387 /* Put the labels into the "variables" of the
1388 top-level block, so debugger can see them. */
1389 TREE_CHAIN (label) = BLOCK_VARS (block);
1390 BLOCK_VARS (block) = label;
1392 #endif
1395 /* Pop the current level, and free the structure for reuse. */
1398 register struct binding_level *level = current_binding_level;
1399 current_binding_level = current_binding_level->level_chain;
1401 level->level_chain = free_binding_level;
1402 free_binding_level = level;
1405 /* Dispose of the block that we just made inside some higher level. */
1406 if (functionbody)
1407 DECL_INITIAL (current_function_decl) = block;
1408 else if (block)
1410 if (!block_previously_created)
1411 current_binding_level->blocks
1412 = chainon (current_binding_level->blocks, block);
1414 /* If we did not make a block for the level just exited,
1415 any blocks made for inner levels
1416 (since they cannot be recorded as subblocks in that level)
1417 must be carried forward so they will later become subblocks
1418 of something else. */
1419 else if (subblocks)
1420 current_binding_level->blocks
1421 = chainon (current_binding_level->blocks, subblocks);
1423 /* Set the TYPE_CONTEXTs for all of the tagged types belonging to this
1424 binding contour so that they point to the appropriate construct, i.e.
1425 either to the current FUNCTION_DECL node, or else to the BLOCK node
1426 we just constructed.
1428 Note that for tagged types whose scope is just the formal parameter
1429 list for some function type specification, we can't properly set
1430 their TYPE_CONTEXTs here, because we don't have a pointer to the
1431 appropriate FUNCTION_TYPE node readily available to us. For those
1432 cases, the TYPE_CONTEXTs of the relevant tagged type nodes get set
1433 in `grokdeclarator' as soon as we have created the FUNCTION_TYPE
1434 node which will represent the "scope" for these "parameter list local"
1435 tagged types.
1438 if (block)
1439 TREE_USED (block) = 1;
1440 return block;
1443 void
1444 maybe_pushlevels (int pc)
1446 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1447 current_pc = pc;
1448 #endif
1450 while (pending_local_decls != NULL_TREE &&
1451 DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1453 tree *ptr = &pending_local_decls;
1454 tree decl = *ptr;
1455 int end_pc = DECL_LOCAL_END_PC (decl);
1457 while (*ptr != NULL_TREE
1458 && DECL_LOCAL_START_PC (*ptr) <= pc
1459 && DECL_LOCAL_END_PC (*ptr) == end_pc)
1460 ptr = &TREE_CHAIN (*ptr);
1461 pending_local_decls = *ptr;
1462 *ptr = NULL_TREE;
1464 /* Force non-nested range to be nested in current range. */
1465 if (end_pc > current_binding_level->end_pc)
1466 end_pc = current_binding_level->end_pc;
1468 maybe_start_try (pc, end_pc);
1470 pushlevel (1);
1471 expand_start_bindings (0);
1473 current_binding_level->end_pc = end_pc;
1474 current_binding_level->start_pc = pc;
1475 current_binding_level->names = decl;
1476 for ( ; decl != NULL_TREE; decl = TREE_CHAIN (decl))
1478 push_jvm_slot (DECL_LOCAL_SLOT_NUMBER (decl), decl);
1482 maybe_start_try (pc, 0);
1485 void
1486 maybe_poplevels (int pc)
1488 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1489 current_pc = pc;
1490 #endif
1492 while (current_binding_level->end_pc <= pc)
1494 expand_end_bindings (getdecls (), 1, 0);
1495 maybe_end_try (current_binding_level->start_pc, pc);
1496 poplevel (1, 0, 0);
1498 maybe_end_try (0, pc);
1501 /* Terminate any binding which began during the range beginning at
1502 start_pc. This tidies up improperly nested local variable ranges
1503 and exception handlers; a variable declared within an exception
1504 range is forcibly terminated when that exception ends. */
1506 void
1507 force_poplevels (int start_pc)
1509 while (current_binding_level->start_pc > start_pc)
1511 if (pedantic && current_binding_level->start_pc > start_pc)
1512 warning_with_decl (current_function_decl,
1513 "In %s: overlapped variable and exception ranges at %d",
1514 current_binding_level->start_pc);
1515 expand_end_bindings (getdecls (), 1, 0);
1516 poplevel (1, 0, 0);
1520 /* Insert BLOCK at the end of the list of subblocks of the
1521 current binding level. This is used when a BIND_EXPR is expanded,
1522 to handle the BLOCK node inside the BIND_EXPR. */
1524 void
1525 insert_block (tree block)
1527 TREE_USED (block) = 1;
1528 current_binding_level->blocks
1529 = chainon (current_binding_level->blocks, block);
1532 /* Set the BLOCK node for the innermost scope
1533 (the one we are currently in). */
1535 void
1536 set_block (tree block)
1538 current_binding_level->this_block = block;
1539 current_binding_level->names = chainon (current_binding_level->names,
1540 BLOCK_VARS (block));
1541 current_binding_level->blocks = chainon (current_binding_level->blocks,
1542 BLOCK_SUBBLOCKS (block));
1545 /* integrate_decl_tree calls this function. */
1547 void
1548 java_dup_lang_specific_decl (tree node)
1550 int lang_decl_size;
1551 struct lang_decl *x;
1553 if (!DECL_LANG_SPECIFIC (node))
1554 return;
1556 lang_decl_size = sizeof (struct lang_decl);
1557 x = ggc_alloc (lang_decl_size);
1558 memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1559 DECL_LANG_SPECIFIC (node) = x;
1562 void
1563 give_name_to_locals (JCF *jcf)
1565 int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1566 int code_offset = DECL_CODE_OFFSET (current_function_decl);
1567 tree parm;
1568 pending_local_decls = NULL_TREE;
1569 if (n == 0)
1570 return;
1571 JCF_SEEK (jcf, n);
1572 n = JCF_readu2 (jcf);
1573 for (i = 0; i < n; i++)
1575 int start_pc = JCF_readu2 (jcf);
1576 int length = JCF_readu2 (jcf);
1577 int name_index = JCF_readu2 (jcf);
1578 int signature_index = JCF_readu2 (jcf);
1579 int slot = JCF_readu2 (jcf);
1580 tree name = get_name_constant (jcf, name_index);
1581 tree type = parse_signature (jcf, signature_index);
1582 if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1583 && start_pc == 0
1584 && length == DECL_CODE_LENGTH (current_function_decl))
1586 tree decl = TREE_VEC_ELT (decl_map, slot);
1587 DECL_NAME (decl) = name;
1588 SET_DECL_ASSEMBLER_NAME (decl, name);
1589 if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1590 warning ("bad type in parameter debug info");
1592 else
1594 tree *ptr;
1595 int end_pc = start_pc + length;
1596 tree decl = build_decl (VAR_DECL, name, type);
1597 if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1599 warning_with_decl (decl,
1600 "bad PC range for debug info for local `%s'");
1601 end_pc = DECL_CODE_LENGTH (current_function_decl);
1604 /* Adjust start_pc if necessary so that the local's first
1605 store operation will use the relevant DECL as a
1606 destination. Fore more information, read the leading
1607 comments for expr.c:maybe_adjust_start_pc. */
1608 start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1610 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1611 DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1612 DECL_LOCAL_START_PC (decl) = start_pc;
1613 #if 0
1614 /* FIXME: The range used internally for exceptions and local
1615 variable ranges, is a half-open interval:
1616 start_pc <= pc < end_pc. However, the range used in the
1617 Java VM spec is inclusive at both ends:
1618 start_pc <= pc <= end_pc. */
1619 end_pc++;
1620 #endif
1621 DECL_LOCAL_END_PC (decl) = end_pc;
1623 /* Now insert the new decl in the proper place in
1624 pending_local_decls. We are essentially doing an insertion sort,
1625 which works fine, since the list input will normally already
1626 be sorted. */
1627 ptr = &pending_local_decls;
1628 while (*ptr != NULL_TREE
1629 && (DECL_LOCAL_START_PC (*ptr) > start_pc
1630 || (DECL_LOCAL_START_PC (*ptr) == start_pc
1631 && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1632 ptr = &TREE_CHAIN (*ptr);
1633 TREE_CHAIN (decl) = *ptr;
1634 *ptr = decl;
1638 pending_local_decls = nreverse (pending_local_decls);
1640 /* Fill in default names for the parameters. */
1641 for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1642 parm != NULL_TREE; parm = TREE_CHAIN (parm), i++)
1644 if (DECL_NAME (parm) == NULL_TREE)
1646 int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1647 if (arg_i == 0)
1648 DECL_NAME (parm) = get_identifier ("this");
1649 else
1651 char buffer[12];
1652 sprintf (buffer, "ARG_%d", arg_i);
1653 DECL_NAME (parm) = get_identifier (buffer);
1655 SET_DECL_ASSEMBLER_NAME (parm, DECL_NAME (parm));
1660 tree
1661 build_result_decl (tree fndecl)
1663 tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1664 tree result = DECL_RESULT (fndecl);
1665 if (! result)
1667 /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1668 if (INTEGRAL_TYPE_P (restype)
1669 && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
1670 restype = integer_type_node;
1671 result = build_decl (RESULT_DECL, NULL_TREE, restype);
1672 DECL_CONTEXT (result) = fndecl;
1673 DECL_RESULT (fndecl) = result;
1675 return result;
1678 void
1679 complete_start_java_method (tree fndecl)
1681 if (! flag_emit_class_files)
1683 /* Initialize the RTL code for the function. */
1684 init_function_start (fndecl, input_filename, lineno);
1686 /* Set up parameters and prepare for return, for the function. */
1687 expand_function_start (fndecl, 0);
1690 #if 0
1691 /* If this fcn was already referenced via a block-scope `extern' decl (or
1692 an implicit decl), propagate certain information about the usage. */
1693 if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (current_function_decl)))
1694 TREE_ADDRESSABLE (current_function_decl) = 1;
1696 #endif
1698 if (METHOD_STATIC (fndecl) && ! METHOD_PRIVATE (fndecl)
1699 && ! flag_emit_class_files
1700 && ! DECL_CLINIT_P (fndecl)
1701 && ! CLASS_INTERFACE (TYPE_NAME (current_class)))
1703 tree clas = DECL_CONTEXT (fndecl);
1704 tree init = build (CALL_EXPR, void_type_node,
1705 build_address_of (soft_initclass_node),
1706 build_tree_list (NULL_TREE, build_class_ref (clas)),
1707 NULL_TREE);
1708 TREE_SIDE_EFFECTS (init) = 1;
1709 expand_expr_stmt (init);
1712 /* Push local variables. Function compiled from source code are
1713 using a different local variables management, and for them,
1714 pushlevel shouldn't be called from here. */
1715 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (fndecl)))
1717 pushlevel (2);
1718 if (! flag_emit_class_files)
1719 expand_start_bindings (1);
1722 if (METHOD_SYNCHRONIZED (fndecl) && ! flag_emit_class_files)
1724 /* Wrap function body with a monitorenter plus monitorexit cleanup. */
1725 tree enter, exit, lock;
1726 if (METHOD_STATIC (fndecl))
1727 lock = build_class_ref (DECL_CONTEXT (fndecl));
1728 else
1729 lock = DECL_ARGUMENTS (fndecl);
1730 BUILD_MONITOR_ENTER (enter, lock);
1731 BUILD_MONITOR_EXIT (exit, lock);
1732 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (fndecl)))
1734 expand_expr_stmt (enter);
1735 expand_decl_cleanup (NULL_TREE, exit);
1737 else
1739 tree function_body = DECL_FUNCTION_BODY (fndecl);
1740 tree body = BLOCK_EXPR_BODY (function_body);
1741 lock = build (COMPOUND_EXPR, void_type_node,
1742 enter,
1743 build (TRY_FINALLY_EXPR, void_type_node, body, exit));
1744 TREE_SIDE_EFFECTS (lock) = 1;
1745 BLOCK_EXPR_BODY (function_body) = lock;
1750 void
1751 start_java_method (tree fndecl)
1753 tree tem, *ptr;
1754 int i;
1756 current_function_decl = fndecl;
1757 announce_function (fndecl);
1759 i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1760 decl_map = make_tree_vec (i);
1761 type_map = xrealloc (type_map, i * sizeof (tree));
1763 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1764 fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
1765 current_pc = 0;
1766 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1767 pushlevel (1); /* Push parameters. */
1769 ptr = &DECL_ARGUMENTS (fndecl);
1770 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1771 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
1773 tree parm_name = NULL_TREE, parm_decl;
1774 tree parm_type = TREE_VALUE (tem);
1775 if (i >= DECL_MAX_LOCALS (fndecl))
1776 abort ();
1778 parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
1779 DECL_CONTEXT (parm_decl) = fndecl;
1780 if (PROMOTE_PROTOTYPES
1781 && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
1782 && INTEGRAL_TYPE_P (parm_type))
1783 parm_type = integer_type_node;
1784 DECL_ARG_TYPE (parm_decl) = parm_type;
1786 *ptr = parm_decl;
1787 ptr = &TREE_CHAIN (parm_decl);
1789 /* Add parm_decl to the decl_map. */
1790 push_jvm_slot (i, parm_decl);
1792 type_map[i] = TREE_TYPE (parm_decl);
1793 if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
1795 i++;
1796 type_map[i] = void_type_node;
1799 *ptr = NULL_TREE;
1800 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
1802 while (i < DECL_MAX_LOCALS(fndecl))
1803 type_map[i++] = NULL_TREE;
1805 build_result_decl (fndecl);
1806 complete_start_java_method (fndecl);
1809 void
1810 end_java_method (void)
1812 tree fndecl = current_function_decl;
1814 expand_end_bindings (getdecls (), 1, 0);
1815 /* pop out of function */
1816 poplevel (1, 1, 0);
1818 /* pop out of its parameters */
1819 poplevel (1, 0, 1);
1821 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
1823 /* Generate rtl for function exit. */
1824 expand_function_end (input_filename, lineno, 0);
1826 /* Run the optimizers and output assembler code for this function. */
1827 rest_of_compilation (fndecl);
1829 current_function_decl = NULL_TREE;
1832 /* Dump FUNCTION_DECL FN as tree dump PHASE. */
1834 static void
1835 dump_function (enum tree_dump_index phase, tree fn)
1837 FILE *stream;
1838 int flags;
1840 stream = dump_begin (phase, &flags);
1841 if (stream)
1843 dump_node (fn, TDF_SLIM | flags, stream);
1844 dump_end (phase, stream);
1848 void java_optimize_inline (tree fndecl)
1850 if (flag_inline_trees)
1852 timevar_push (TV_INTEGRATION);
1853 optimize_inline_calls (fndecl);
1854 timevar_pop (TV_INTEGRATION);
1855 dump_function (TDI_inlined, fndecl);
1859 #include "gt-java-decl.h"