Merge from the pain train
[official-gcc.git] / gcc / java / decl.c
blobb7aa61650d3548a5d06c847ce0f0aed302f9992d
1 /* Process declarations and variables for the GNU compiler for the
2 Java(TM) language.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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 "cgraph.h"
48 #include "tree-inline.h"
49 #include "target.h"
50 #include "version.h"
52 #if defined (DEBUG_JAVA_BINDING_LEVELS)
53 extern void indent (void);
54 #endif
56 static tree push_jvm_slot (int, tree);
57 static tree lookup_name_current_level (tree);
58 static tree push_promoted_type (const char *, tree);
59 static struct binding_level *make_binding_level (void);
60 static tree create_primitive_vtable (const char *);
61 static tree check_local_unnamed_variable (tree, tree, tree);
62 static void parse_version (void);
64 /* Used when computing the ABI version. */
65 #define GCJ_BINARYCOMPAT_ADDITION 5
67 /* The ABI version number. */
68 tree gcj_abi_version;
70 /* Name of the Cloneable class. */
71 tree java_lang_cloneable_identifier_node;
73 /* Name of the Serializable class. */
74 tree java_io_serializable_identifier_node;
76 /* The DECL_MAP is a mapping from (index, type) to a decl node.
77 If index < max_locals, it is the index of a local variable.
78 if index >= max_locals, then index-max_locals is a stack slot.
79 The DECL_MAP mapping is represented as a TREE_VEC whose elements
80 are a list of decls (VAR_DECL or PARM_DECL) chained by
81 DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
82 we search the chain for a decl with a matching TREE_TYPE. */
84 static GTY(()) tree decl_map;
86 /* The base_decl_map is contains one variable of ptr_type: this is
87 used to contain every variable of reference type that is ever
88 stored in a local variable slot. */
90 static GTY(()) tree base_decl_map;
92 /* An index used to make temporary identifiers unique. */
93 static int uniq;
95 /* A list of local variables VAR_DECLs for this method that we have seen
96 debug information, but we have not reached their starting (byte) PC yet. */
98 static GTY(()) tree pending_local_decls;
100 #if defined(DEBUG_JAVA_BINDING_LEVELS)
101 int binding_depth = 0;
102 int is_class_level = 0;
103 int current_pc;
105 void
106 indent (void)
108 int i;
110 for (i = 0; i < binding_depth*2; i++)
111 putc (' ', stderr);
113 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
115 /* True if decl is a named local variable, i.e. if it is an alias
116 that's used only for debugging purposes. */
118 static bool
119 debug_variable_p (tree decl)
121 if (TREE_CODE (decl) == PARM_DECL)
122 return false;
124 if (LOCAL_SLOT_P (decl))
125 return false;
127 return true;
130 /* Copy the value in decl into every live alias in the same local
131 variable slot. Some of these will be dead stores removed by the
132 optimizer. */
134 void
135 update_aliases (tree decl, int index, int pc)
137 tree decl_type = TREE_TYPE (decl);
138 tree tmp;
140 if (debug_variable_p (decl))
141 abort ();
143 for (tmp = TREE_VEC_ELT (decl_map, index);
144 tmp != NULL_TREE;
145 tmp = DECL_LOCAL_SLOT_CHAIN (tmp))
147 tree tmp_type = TREE_TYPE (tmp);
148 if (tmp != decl
149 && LOCAL_SLOT_P (tmp) == 0
150 && (pc == -1
151 || (pc >= DECL_LOCAL_START_PC (tmp)
152 && pc <= DECL_LOCAL_END_PC (tmp)))
153 && (tmp_type == decl_type
154 || (INTEGRAL_TYPE_P (tmp_type)
155 && INTEGRAL_TYPE_P (decl_type)
156 && TYPE_PRECISION (decl_type) <= 32
157 && TYPE_PRECISION (tmp_type) <= 32)
158 || (TREE_CODE (tmp_type) == POINTER_TYPE
159 && TREE_CODE (decl_type) == POINTER_TYPE)))
161 tree src = build1 (NOP_EXPR, tmp_type, decl);
162 if (LOCAL_VAR_OUT_OF_SCOPE_P (tmp))
163 abort ();
164 java_add_stmt (build2 (MODIFY_EXPR, tmp_type, tmp, src));
169 static tree
170 push_jvm_slot (int index, tree decl)
172 DECL_CONTEXT (decl) = current_function_decl;
173 layout_decl (decl, 0);
175 /* Now link the decl into the decl_map. */
176 if (DECL_LANG_SPECIFIC (decl) == NULL)
178 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
179 DECL_LOCAL_START_PC (decl) = 0;
180 DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
181 DECL_LOCAL_SLOT_NUMBER (decl) = index;
183 DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
184 TREE_VEC_ELT (decl_map, index) = decl;
186 return decl;
189 /* At the point of its creation a local variable decl inherits
190 whatever is already in the same slot. In the case of a local
191 variable that is declared but unused, we won't find anything. */
193 static void
194 initialize_local_variable (tree decl, int index)
196 tree decl_type = TREE_TYPE (decl);
197 if (TREE_CODE (decl_type) == POINTER_TYPE)
199 tree tmp = TREE_VEC_ELT (base_decl_map, index);
201 if (tmp)
203 /* At the point of its creation this decl inherits whatever
204 is in the slot. */
205 tree src = build1 (NOP_EXPR, decl_type, tmp);
206 java_add_stmt (build2 (MODIFY_EXPR, decl_type, decl, src));
209 else
211 tree tmp;
213 for (tmp = TREE_VEC_ELT (decl_map, index);
214 tmp != NULL_TREE;
215 tmp = DECL_LOCAL_SLOT_CHAIN (tmp))
217 tree tmp_type = TREE_TYPE (tmp);
218 if (tmp != decl
219 && ! debug_variable_p (tmp)
220 && (tmp_type == decl_type
221 || (INTEGRAL_TYPE_P (tmp_type)
222 && INTEGRAL_TYPE_P (decl_type)
223 && TYPE_PRECISION (decl_type) <= 32
224 && TYPE_PRECISION (tmp_type) <= 32
225 && TYPE_PRECISION (tmp_type)
226 >= TYPE_PRECISION (decl_type))))
228 java_add_stmt (build2 (MODIFY_EXPR, decl_type, decl, tmp));
229 return;
235 /* Find the best declaration based upon type. If 'decl' fits 'type' better
236 than 'best', return 'decl'. Otherwise return 'best'. */
238 static tree
239 check_local_unnamed_variable (tree best, tree decl, tree type)
241 tree decl_type = TREE_TYPE (decl);
243 if (LOCAL_VAR_OUT_OF_SCOPE_P (decl))
244 abort ();
246 /* Use the same decl for all integer types <= 32 bits. This is
247 necessary because sometimes a value is stored as (for example)
248 boolean but loaded as int. */
249 if (decl_type == type
250 || (INTEGRAL_TYPE_P (decl_type)
251 && INTEGRAL_TYPE_P (type)
252 && TYPE_PRECISION (decl_type) <= 32
253 && TYPE_PRECISION (type) <= 32
254 && TYPE_PRECISION (decl_type) >= TYPE_PRECISION (type))
255 /* ptr_type_node is used for null pointers, which are
256 assignment compatible with everything. */
257 || (TREE_CODE (decl_type) == POINTER_TYPE
258 && type == ptr_type_node)
259 /* Whenever anyone wants to use a slot that is initially
260 occupied by a PARM_DECL of pointer type they must get that
261 decl, even if they asked for a pointer to a different type.
262 However, if someone wants a scalar variable in a slot that
263 initially held a pointer arg -- or vice versa -- we create a
264 new VAR_DECL.
266 ???: As long as verification is correct, this will be a
267 compatible type. But maybe we should create a dummy variable
268 and replace all references to it with the DECL and a
269 NOP_EXPR.
271 || (TREE_CODE (decl_type) == POINTER_TYPE
272 && TREE_CODE (decl) == PARM_DECL
273 && TREE_CODE (type) == POINTER_TYPE))
275 if (best == NULL_TREE
276 || (decl_type == type && TREE_TYPE (best) != type))
277 return decl;
280 return best;
284 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
285 that is valid at PC (or -1 if any pc).
286 If there is no existing matching decl, allocate one. */
288 tree
289 find_local_variable (int index, tree type, int pc ATTRIBUTE_UNUSED)
291 tree tmp = TREE_VEC_ELT (decl_map, index);
292 tree decl = NULL_TREE;
294 /* Scan through every declaration that has been created in this
295 slot. We're only looking for variables that correspond to local
296 index declarations and PARM_DECLs, not named variables: such
297 local variables are used only for debugging information. */
298 while (tmp != NULL_TREE)
300 if (! debug_variable_p (tmp))
301 decl = check_local_unnamed_variable (decl, tmp, type);
302 tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
305 /* gcj has a function called promote_type(), which is used by both
306 the bytecode compiler and the source compiler. Unfortunately,
307 the type systems for the Java VM and the Java language are not
308 the same: a boolean in the VM promotes to an int, not to a wide
309 boolean. If our caller wants something to hold a boolean, that
310 had better be an int, because that slot might be re-used
311 later in integer context. */
312 if (TREE_CODE (type) == BOOLEAN_TYPE)
313 type = integer_type_node;
315 /* If we don't find a match, create one with the type passed in.
316 The name of the variable is #n#m, which n is the variable index
317 in the local variable area and m is a dummy identifier for
318 uniqueness -- multiple variables may share the same local
319 variable index. We don't call pushdecl() to push pointer types
320 into a binding expr because they'll all be replaced by a single
321 variable that is used for every reference in that local variable
322 slot. */
323 if (! decl)
325 char buf[64];
326 tree name;
327 sprintf (buf, "#slot#%d#%d", index, uniq++);
328 name = get_identifier (buf);
329 decl = build_decl (VAR_DECL, name, type);
330 DECL_IGNORED_P (decl) = 1;
331 DECL_ARTIFICIAL (decl) = 1;
332 decl = push_jvm_slot (index, decl);
333 LOCAL_SLOT_P (decl) = 1;
335 if (TREE_CODE (type) != POINTER_TYPE)
336 pushdecl_function_level (decl);
339 /* As well as creating a local variable that matches the type, we
340 also create a base variable (of ptr_type) that will hold all its
341 aliases. */
342 if (TREE_CODE (type) == POINTER_TYPE
343 && ! TREE_VEC_ELT (base_decl_map, index))
345 char buf[64];
346 tree name;
347 tree base_decl;
348 sprintf (buf, "#ref#%d#%d", index, uniq++);
349 name = get_identifier (buf);
350 base_decl
351 = TREE_VEC_ELT (base_decl_map, index)
352 = build_decl (VAR_DECL, name, ptr_type_node);
353 pushdecl_function_level (base_decl);
354 DECL_IGNORED_P (base_decl) = 1;
355 DECL_ARTIFICIAL (base_decl) = 1;
358 return decl;
361 /* Called during gimplification for every variable. If the variable
362 is a temporary of pointer type, replace it with a common variable
363 thath is used to hold all pointer types that are ever stored in
364 that slot. Set WANT_LVALUE if you want a variable that is to be
365 written to. */
367 tree
368 java_replace_reference (tree var_decl, bool want_lvalue)
370 tree decl_type;
372 if (! base_decl_map)
373 return var_decl;
375 decl_type = TREE_TYPE (var_decl);
377 if (TREE_CODE (decl_type) == POINTER_TYPE)
379 if (DECL_LANG_SPECIFIC (var_decl)
380 && LOCAL_SLOT_P (var_decl))
382 int index = DECL_LOCAL_SLOT_NUMBER (var_decl);
383 tree base_decl = TREE_VEC_ELT (base_decl_map, index);
385 if (! base_decl)
386 abort ();
388 if (! want_lvalue)
389 base_decl = build1 (NOP_EXPR, decl_type, base_decl);
391 return base_decl;
395 return var_decl;
399 /* Same as find_local_index, except that INDEX is a stack index. */
401 tree
402 find_stack_slot (int index, tree type)
404 return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
405 type, -1);
408 struct binding_level GTY(())
410 /* A chain of _DECL nodes for all variables, constants, functions,
411 * and typedef types. These are in the reverse of the order supplied.
413 tree names;
415 /* For each level, a list of shadowed outer-level local definitions
416 to be restored when this level is popped.
417 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
418 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
419 tree shadowed;
421 /* For each level (except not the global one),
422 a chain of BLOCK nodes for all the levels
423 that were entered and exited one level down. */
424 tree blocks;
426 /* The binding level which this one is contained in (inherits from). */
427 struct binding_level *level_chain;
429 /* The bytecode PC that marks the end of this level. */
430 int end_pc;
431 /* The bytecode PC that marks the start of this level. */
432 int start_pc;
434 /* The statements in this binding level. */
435 tree stmts;
437 /* An exception range associated with this binding level. */
438 struct eh_range * GTY((skip (""))) exception_range;
440 /* Binding depth at which this level began. Used only for debugging. */
441 unsigned binding_depth;
444 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
446 /* The binding level currently in effect. */
448 static GTY(()) struct binding_level *current_binding_level;
450 /* A chain of binding_level structures awaiting reuse. */
452 static GTY(()) struct binding_level *free_binding_level;
454 /* The outermost binding level, for names of file scope.
455 This is created when the compiler is started and exists
456 through the entire run. */
458 static GTY(()) struct binding_level *global_binding_level;
460 /* The binding level that holds variables declared at the outermost
461 level within a function body. */
463 static struct binding_level *function_binding_level;
465 /* A PC value bigger than any PC value we may ever may encounter. */
467 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
469 /* Binding level structures are initialized by copying this one. */
471 static const struct binding_level clear_binding_level
473 NULL_TREE, /* names */
474 NULL_TREE, /* shadowed */
475 NULL_TREE, /* blocks */
476 NULL_BINDING_LEVEL, /* level_chain */
477 LARGEST_PC, /* end_pc */
478 0, /* start_pc */
479 NULL, /* stmts */
480 NULL, /* exception_range */
481 0, /* binding_depth */
484 #if 0
485 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
486 that have names. Here so we can clear out their names' definitions
487 at the end of the function. */
489 static tree named_labels;
491 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
493 static tree shadowed_labels;
494 #endif
496 tree java_global_trees[JTI_MAX];
498 /* Build (and pushdecl) a "promoted type" for all standard
499 types shorter than int. */
501 static tree
502 push_promoted_type (const char *name, tree actual_type)
504 tree type = make_node (TREE_CODE (actual_type));
505 #if 1
506 tree in_min = TYPE_MIN_VALUE (int_type_node);
507 tree in_max = TYPE_MAX_VALUE (int_type_node);
508 #else
509 tree in_min = TYPE_MIN_VALUE (actual_type);
510 tree in_max = TYPE_MAX_VALUE (actual_type);
511 #endif
512 TYPE_MIN_VALUE (type) = copy_node (in_min);
513 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
514 TYPE_MAX_VALUE (type) = copy_node (in_max);
515 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
516 TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
517 layout_type (type);
518 pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
519 return type;
522 /* Return a definition for a builtin function named NAME and whose data type
523 is TYPE. TYPE should be a function type with argument types.
524 FUNCTION_CODE tells later passes how to compile calls to this function.
525 See tree.h for its possible values.
527 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
528 the name to be called if we can't opencode the function. If
529 ATTRS is nonzero, use that for the function's attribute list. */
531 tree
532 builtin_function (const char *name,
533 tree type,
534 int function_code,
535 enum built_in_class cl,
536 const char *library_name,
537 tree ARG_UNUSED (attrs))
539 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
540 DECL_EXTERNAL (decl) = 1;
541 TREE_PUBLIC (decl) = 1;
542 if (library_name)
543 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
544 make_decl_rtl (decl);
545 pushdecl (decl);
546 DECL_BUILT_IN_CLASS (decl) = cl;
547 DECL_FUNCTION_CODE (decl) = function_code;
548 return decl;
551 /* Return tree that represents a vtable for a primitive array. */
552 static tree
553 create_primitive_vtable (const char *name)
555 tree r;
556 char buf[50];
558 sprintf (buf, "_Jv_%sVTable", name);
559 r = build_decl (VAR_DECL, get_identifier (buf), ptr_type_node);
560 DECL_EXTERNAL (r) = 1;
561 return r;
564 static tree
565 do_nothing (tree t)
567 return t;
570 /* Parse the version string and compute the ABI version number. */
571 static void
572 parse_version (void)
574 const char *p = version_string;
575 unsigned int major = 0, minor = 0;
576 unsigned int abi_version;
578 /* Skip leading junk. */
579 while (*p && !ISDIGIT (*p))
580 ++p;
581 gcc_assert (*p);
583 /* Extract major version. */
584 while (ISDIGIT (*p))
586 major = major * 10 + *p - '0';
587 ++p;
590 gcc_assert (*p == '.' && ISDIGIT (p[1]));
591 ++p;
593 /* Extract minor version. */
594 while (ISDIGIT (*p))
596 minor = minor * 10 + *p - '0';
597 ++p;
600 /* Implicit in this computation is the idea that we won't break the
601 old-style binary ABI in a sub-minor release (e.g., from 4.0.0 to
602 4.0.1). */
603 abi_version = 10000 * major + 10 * minor;
604 /* It is helpful to distinguish BC ABI from ordinary ABI at this
605 level, since at some point we will recognize a variety of BC ABIs
606 (objects generated by different version of gcj), but will
607 probably always require strict matching for ordinary ABI. */
608 if (flag_indirect_dispatch)
609 abi_version += GCJ_BINARYCOMPAT_ADDITION;
611 gcj_abi_version = build_int_cstu (ptr_type_node, abi_version);
614 void
615 java_init_decl_processing (void)
617 tree endlink;
618 tree field = NULL_TREE;
619 tree t;
621 init_class_processing ();
623 current_function_decl = NULL;
624 current_binding_level = NULL_BINDING_LEVEL;
625 free_binding_level = NULL_BINDING_LEVEL;
626 pushlevel (0); /* make the binding_level structure for global names */
627 global_binding_level = current_binding_level;
629 /* The code here must be similar to build_common_tree_nodes{,_2} in
630 tree.c, especially as to the order of initializing common nodes. */
631 error_mark_node = make_node (ERROR_MARK);
632 TREE_TYPE (error_mark_node) = error_mark_node;
634 /* Create sizetype first - needed for other types. */
635 initialize_sizetypes (false);
637 byte_type_node = make_signed_type (8);
638 pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
639 short_type_node = make_signed_type (16);
640 pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
641 int_type_node = make_signed_type (32);
642 pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
643 long_type_node = make_signed_type (64);
644 pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));
646 unsigned_byte_type_node = make_unsigned_type (8);
647 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
648 unsigned_byte_type_node));
649 unsigned_short_type_node = make_unsigned_type (16);
650 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
651 unsigned_short_type_node));
652 unsigned_int_type_node = make_unsigned_type (32);
653 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
654 unsigned_int_type_node));
655 unsigned_long_type_node = make_unsigned_type (64);
656 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
657 unsigned_long_type_node));
659 /* This is not a java type, however tree-dfa requires a definition for
660 size_type_node. */
661 size_type_node = make_unsigned_type (POINTER_SIZE);
662 set_sizetype (size_type_node);
664 /* Define these next since types below may used them. */
665 integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
666 integer_zero_node = build_int_cst (NULL_TREE, 0);
667 integer_one_node = build_int_cst (NULL_TREE, 1);
668 integer_two_node = build_int_cst (NULL_TREE, 2);
669 integer_four_node = build_int_cst (NULL_TREE, 4);
670 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
672 /* A few values used for range checking in the lexer. */
673 decimal_int_max = build_int_cstu (unsigned_int_type_node, 0x80000000);
674 #if HOST_BITS_PER_WIDE_INT == 64
675 decimal_long_max = build_int_cstu (unsigned_long_type_node,
676 0x8000000000000000LL);
677 #elif HOST_BITS_PER_WIDE_INT == 32
678 decimal_long_max = build_int_cst_wide (unsigned_long_type_node,
679 0, 0x80000000);
680 #else
681 #error "unsupported size"
682 #endif
684 size_zero_node = size_int (0);
685 size_one_node = size_int (1);
686 bitsize_zero_node = bitsize_int (0);
687 bitsize_one_node = bitsize_int (1);
688 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
690 long_zero_node = build_int_cst (long_type_node, 0);
692 void_type_node = make_node (VOID_TYPE);
693 pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
694 layout_type (void_type_node); /* Uses size_zero_node */
696 ptr_type_node = build_pointer_type (void_type_node);
697 const_ptr_type_node
698 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
700 t = make_node (VOID_TYPE);
701 layout_type (t); /* Uses size_zero_node */
702 return_address_type_node = build_pointer_type (t);
704 null_pointer_node = build_int_cst (ptr_type_node, 0);
706 #if 0
707 /* Make a type to be the domain of a few array types
708 whose domains don't really matter.
709 200 is small enough that it always fits in size_t
710 and large enough that it can hold most function names for the
711 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
712 short_array_type_node = build_prim_array_type (short_type_node, 200);
713 #endif
714 char_type_node = make_node (CHAR_TYPE);
715 TYPE_PRECISION (char_type_node) = 16;
716 fixup_unsigned_type (char_type_node);
717 pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));
719 boolean_type_node = make_node (BOOLEAN_TYPE);
720 TYPE_PRECISION (boolean_type_node) = 1;
721 fixup_unsigned_type (boolean_type_node);
722 pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
723 boolean_type_node));
724 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
725 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
727 promoted_byte_type_node
728 = push_promoted_type ("promoted_byte", byte_type_node);
729 promoted_short_type_node
730 = push_promoted_type ("promoted_short", short_type_node);
731 promoted_char_type_node
732 = push_promoted_type ("promoted_char", char_type_node);
733 promoted_boolean_type_node
734 = push_promoted_type ("promoted_boolean", boolean_type_node);
736 float_type_node = make_node (REAL_TYPE);
737 TYPE_PRECISION (float_type_node) = 32;
738 pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
739 float_type_node));
740 layout_type (float_type_node);
742 double_type_node = make_node (REAL_TYPE);
743 TYPE_PRECISION (double_type_node) = 64;
744 pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
745 double_type_node));
746 layout_type (double_type_node);
748 float_zero_node = build_real (float_type_node, dconst0);
749 double_zero_node = build_real (double_type_node, dconst0);
751 /* These are the vtables for arrays of primitives. */
752 boolean_array_vtable = create_primitive_vtable ("boolean");
753 byte_array_vtable = create_primitive_vtable ("byte");
754 char_array_vtable = create_primitive_vtable ("char");
755 short_array_vtable = create_primitive_vtable ("short");
756 int_array_vtable = create_primitive_vtable ("int");
757 long_array_vtable = create_primitive_vtable ("long");
758 float_array_vtable = create_primitive_vtable ("float");
759 double_array_vtable = create_primitive_vtable ("double");
761 one_elt_array_domain_type = build_index_type (integer_one_node);
762 utf8const_type = make_node (RECORD_TYPE);
763 PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
764 PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
765 FINISH_RECORD (utf8const_type);
766 utf8const_ptr_type = build_pointer_type (utf8const_type);
768 atable_type = build_array_type (ptr_type_node,
769 one_elt_array_domain_type);
770 TYPE_NONALIASED_COMPONENT (atable_type) = 1;
771 atable_ptr_type = build_pointer_type (atable_type);
773 itable_type = build_array_type (ptr_type_node,
774 one_elt_array_domain_type);
775 TYPE_NONALIASED_COMPONENT (itable_type) = 1;
776 itable_ptr_type = build_pointer_type (itable_type);
778 symbol_type = make_node (RECORD_TYPE);
779 PUSH_FIELD (symbol_type, field, "clname", utf8const_ptr_type);
780 PUSH_FIELD (symbol_type, field, "name", utf8const_ptr_type);
781 PUSH_FIELD (symbol_type, field, "signature", utf8const_ptr_type);
782 FINISH_RECORD (symbol_type);
784 symbols_array_type = build_array_type (symbol_type,
785 one_elt_array_domain_type);
786 symbols_array_ptr_type = build_pointer_type (symbols_array_type);
788 assertion_entry_type = make_node (RECORD_TYPE);
789 PUSH_FIELD (assertion_entry_type, field, "assertion_code", integer_type_node);
790 PUSH_FIELD (assertion_entry_type, field, "op1", utf8const_ptr_type);
791 PUSH_FIELD (assertion_entry_type, field, "op2", utf8const_ptr_type);
792 FINISH_RECORD (assertion_entry_type);
794 assertion_table_type = build_array_type (assertion_entry_type,
795 one_elt_array_domain_type);
797 /* As you're adding items here, please update the code right after
798 this section, so that the filename containing the source code of
799 the pre-defined class gets registered correctly. */
800 unqualified_object_id_node = get_identifier ("Object");
801 object_type_node = lookup_class (get_identifier ("java.lang.Object"));
802 object_ptr_type_node = promote_type (object_type_node);
803 string_type_node = lookup_class (get_identifier ("java.lang.String"));
804 string_ptr_type_node = promote_type (string_type_node);
805 class_type_node = lookup_class (get_identifier ("java.lang.Class"));
806 throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
807 exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
808 runtime_exception_type_node =
809 lookup_class (get_identifier ("java.lang.RuntimeException"));
810 error_exception_type_node =
811 lookup_class (get_identifier ("java.lang.Error"));
813 rawdata_ptr_type_node
814 = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
816 add_predefined_file (get_identifier ("java/lang/Class.java"));
817 add_predefined_file (get_identifier ("java/lang/Error.java"));
818 add_predefined_file (get_identifier ("java/lang/Object.java"));
819 add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
820 add_predefined_file (get_identifier ("java/lang/String.java"));
821 add_predefined_file (get_identifier ("java/lang/Throwable.java"));
822 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
823 add_predefined_file (get_identifier ("java/lang/Exception.java"));
824 add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
825 add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
827 methodtable_type = make_node (RECORD_TYPE);
828 layout_type (methodtable_type);
829 build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
830 methodtable_ptr_type = build_pointer_type (methodtable_type);
832 TYPE_identifier_node = get_identifier ("TYPE");
833 init_identifier_node = get_identifier ("<init>");
834 clinit_identifier_node = get_identifier ("<clinit>");
835 finit_identifier_node = get_identifier ("finit$");
836 instinit_identifier_node = get_identifier ("instinit$");
837 void_signature_node = get_identifier ("()V");
838 length_identifier_node = get_identifier ("length");
839 finalize_identifier_node = get_identifier ("finalize");
840 this_identifier_node = get_identifier ("this");
841 super_identifier_node = get_identifier ("super");
842 continue_identifier_node = get_identifier ("continue");
843 access0_identifier_node = get_identifier ("access$0");
844 classdollar_identifier_node = get_identifier ("class$");
846 java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
847 java_io_serializable_identifier_node =
848 get_identifier ("java.io.Serializable");
850 /* for lack of a better place to put this stub call */
851 init_expr_processing();
853 constants_type_node = make_node (RECORD_TYPE);
854 PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
855 PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
856 PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
857 FINISH_RECORD (constants_type_node);
858 build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
860 access_flags_type_node = unsigned_short_type_node;
862 dtable_type = make_node (RECORD_TYPE);
863 dtable_ptr_type = build_pointer_type (dtable_type);
865 otable_type = build_array_type (integer_type_node,
866 one_elt_array_domain_type);
867 TYPE_NONALIASED_COMPONENT (otable_type) = 1;
868 otable_ptr_type = build_pointer_type (otable_type);
870 PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
871 DECL_FCONTEXT (field) = object_type_node;
872 TYPE_VFIELD (object_type_node) = field;
874 /* This isn't exactly true, but it is what we have in the source.
875 There is an unresolved issue here, which is whether the vtable
876 should be marked by the GC. */
877 if (! flag_hash_synchronization)
878 PUSH_FIELD (object_type_node, field, "sync_info",
879 build_pointer_type (object_type_node));
880 for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
881 FIELD_PRIVATE (t) = 1;
882 FINISH_RECORD (object_type_node);
884 field_type_node = make_node (RECORD_TYPE);
885 field_ptr_type_node = build_pointer_type (field_type_node);
886 method_type_node = make_node (RECORD_TYPE);
887 method_ptr_type_node = build_pointer_type (method_type_node);
889 set_super_info (0, class_type_node, object_type_node, 0);
890 set_super_info (0, string_type_node, object_type_node, 0);
891 class_ptr_type = build_pointer_type (class_type_node);
893 PUSH_FIELD (class_type_node, field, "next_or_version", class_ptr_type);
894 PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
895 PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
896 PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
897 PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
898 PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
899 PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
900 PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
901 PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
902 PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
903 PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
904 PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
905 PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
906 PUSH_FIELD (class_type_node, field, "otable", otable_ptr_type);
907 PUSH_FIELD (class_type_node, field, "otable_syms",
908 symbols_array_ptr_type);
909 PUSH_FIELD (class_type_node, field, "atable", atable_ptr_type);
910 PUSH_FIELD (class_type_node, field, "atable_syms",
911 symbols_array_ptr_type);
912 PUSH_FIELD (class_type_node, field, "itable", itable_ptr_type);
913 PUSH_FIELD (class_type_node, field, "itable_syms",
914 symbols_array_ptr_type);
915 PUSH_FIELD (class_type_node, field, "catch_classes", ptr_type_node);
916 PUSH_FIELD (class_type_node, field, "interfaces",
917 build_pointer_type (class_ptr_type));
918 PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
919 PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
920 PUSH_FIELD (class_type_node, field, "state", byte_type_node);
921 PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
922 PUSH_FIELD (class_type_node, field, "depth", short_type_node);
923 PUSH_FIELD (class_type_node, field, "ancestors", ptr_type_node);
924 PUSH_FIELD (class_type_node, field, "idt", ptr_type_node);
925 PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);
926 PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
927 PUSH_FIELD (class_type_node, field, "assertion_table", ptr_type_node);
928 PUSH_FIELD (class_type_node, field, "hack_signers", ptr_type_node);
929 PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
930 PUSH_FIELD (class_type_node, field, "aux_info", ptr_type_node);
931 PUSH_FIELD (class_type_node, field, "engine", ptr_type_node);
932 for (t = TYPE_FIELDS (class_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
933 FIELD_PRIVATE (t) = 1;
934 push_super_field (class_type_node, object_type_node);
936 FINISH_RECORD (class_type_node);
937 build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
939 field_info_union_node = make_node (UNION_TYPE);
940 PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
941 PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
942 #if 0
943 PUSH_FIELD (field_info_union_node, field, "idx", unsigned_short_type_node);
944 #endif
945 layout_type (field_info_union_node);
947 PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
948 PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
949 PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
950 PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
951 PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
952 FINISH_RECORD (field_type_node);
953 build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
955 nativecode_ptr_array_type_node
956 = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
958 PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
959 PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
960 FINISH_RECORD (dtable_type);
961 build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
963 jexception_type = make_node (RECORD_TYPE);
964 PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
965 PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
966 PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
967 PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
968 FINISH_RECORD (jexception_type);
969 build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
970 jexception_ptr_type = build_pointer_type (jexception_type);
972 lineNumberEntry_type = make_node (RECORD_TYPE);
973 PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
974 PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
975 FINISH_RECORD (lineNumberEntry_type);
977 lineNumbers_type = make_node (RECORD_TYPE);
978 PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
979 FINISH_RECORD (lineNumbers_type);
981 PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
982 PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
983 PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
984 PUSH_FIELD (method_type_node, field, "index", unsigned_short_type_node);
985 PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
986 PUSH_FIELD (method_type_node, field, "throws", ptr_type_node);
987 FINISH_RECORD (method_type_node);
988 build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
990 endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
992 t = tree_cons (NULL_TREE, class_ptr_type,
993 tree_cons (NULL_TREE, int_type_node, endlink));
994 alloc_object_node = builtin_function ("_Jv_AllocObject",
995 build_function_type (ptr_type_node, t),
996 0, NOT_BUILT_IN, NULL, NULL_TREE);
997 DECL_IS_MALLOC (alloc_object_node) = 1;
998 alloc_no_finalizer_node =
999 builtin_function ("_Jv_AllocObjectNoFinalizer",
1000 build_function_type (ptr_type_node, t),
1001 0, NOT_BUILT_IN, NULL, NULL_TREE);
1002 DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
1004 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1005 soft_initclass_node = builtin_function ("_Jv_InitClass",
1006 build_function_type (void_type_node,
1008 0, NOT_BUILT_IN, NULL, NULL_TREE);
1010 throw_node = builtin_function ("_Jv_Throw",
1011 build_function_type (ptr_type_node, t),
1012 0, NOT_BUILT_IN, NULL, NULL_TREE);
1013 /* Mark throw_nodes as `noreturn' functions with side effects. */
1014 TREE_THIS_VOLATILE (throw_node) = 1;
1015 TREE_SIDE_EFFECTS (throw_node) = 1;
1017 t = build_function_type (int_type_node, endlink);
1018 soft_monitorenter_node
1019 = builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
1020 NULL, NULL_TREE);
1021 soft_monitorexit_node
1022 = builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
1023 NULL, NULL_TREE);
1025 t = tree_cons (NULL_TREE, int_type_node,
1026 tree_cons (NULL_TREE, int_type_node, endlink));
1027 soft_newarray_node
1028 = builtin_function ("_Jv_NewPrimArray",
1029 build_function_type(ptr_type_node, t),
1030 0, NOT_BUILT_IN, NULL, NULL_TREE);
1031 DECL_IS_MALLOC (soft_newarray_node) = 1;
1033 t = tree_cons (NULL_TREE, int_type_node,
1034 tree_cons (NULL_TREE, class_ptr_type,
1035 tree_cons (NULL_TREE, object_ptr_type_node, endlink)));
1036 soft_anewarray_node
1037 = builtin_function ("_Jv_NewObjectArray",
1038 build_function_type (ptr_type_node, t),
1039 0, NOT_BUILT_IN, NULL, NULL_TREE);
1040 DECL_IS_MALLOC (soft_anewarray_node) = 1;
1042 /* There is no endlink here because _Jv_NewMultiArray is a varargs
1043 function. */
1044 t = tree_cons (NULL_TREE, ptr_type_node,
1045 tree_cons (NULL_TREE, int_type_node, NULL_TREE));
1046 soft_multianewarray_node
1047 = builtin_function ("_Jv_NewMultiArray",
1048 build_function_type (ptr_type_node, t),
1049 0, NOT_BUILT_IN, NULL, NULL_TREE);
1050 DECL_IS_MALLOC (soft_multianewarray_node) = 1;
1052 t = build_function_type (void_type_node,
1053 tree_cons (NULL_TREE, int_type_node, endlink));
1054 soft_badarrayindex_node
1055 = builtin_function ("_Jv_ThrowBadArrayIndex", t,
1056 0, NOT_BUILT_IN, NULL, NULL_TREE);
1057 /* Mark soft_badarrayindex_node as a `noreturn' function with side
1058 effects. */
1059 TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
1060 TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
1062 soft_nullpointer_node
1063 = builtin_function ("_Jv_ThrowNullPointerException",
1064 build_function_type (void_type_node, endlink),
1065 0, NOT_BUILT_IN, NULL, NULL_TREE);
1066 /* Mark soft_nullpointer_node as a `noreturn' function with side
1067 effects. */
1068 TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
1069 TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
1071 t = tree_cons (NULL_TREE, class_ptr_type,
1072 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1073 soft_checkcast_node
1074 = builtin_function ("_Jv_CheckCast",
1075 build_function_type (ptr_type_node, t),
1076 0, NOT_BUILT_IN, NULL, NULL_TREE);
1077 t = tree_cons (NULL_TREE, object_ptr_type_node,
1078 tree_cons (NULL_TREE, class_ptr_type, endlink));
1079 soft_instanceof_node
1080 = builtin_function ("_Jv_IsInstanceOf",
1081 build_function_type (boolean_type_node, t),
1082 0, NOT_BUILT_IN, NULL, NULL_TREE);
1083 DECL_IS_PURE (soft_instanceof_node) = 1;
1084 t = tree_cons (NULL_TREE, object_ptr_type_node,
1085 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1086 soft_checkarraystore_node
1087 = builtin_function ("_Jv_CheckArrayStore",
1088 build_function_type (void_type_node, t),
1089 0, NOT_BUILT_IN, NULL, NULL_TREE);
1090 t = tree_cons (NULL_TREE, ptr_type_node,
1091 tree_cons (NULL_TREE, ptr_type_node,
1092 tree_cons (NULL_TREE, int_type_node, endlink)));
1093 soft_lookupinterfacemethod_node
1094 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
1095 build_function_type (ptr_type_node, t),
1096 0, NOT_BUILT_IN, NULL, NULL_TREE);
1097 DECL_IS_PURE (soft_lookupinterfacemethod_node) = 1;
1098 t = tree_cons (NULL_TREE, ptr_type_node,
1099 tree_cons (NULL_TREE, ptr_type_node,
1100 tree_cons (NULL_TREE, ptr_type_node, endlink)));
1101 soft_lookupinterfacemethodbyname_node
1102 = builtin_function ("_Jv_LookupInterfaceMethod",
1103 build_function_type (ptr_type_node, t),
1104 0, NOT_BUILT_IN, NULL, NULL_TREE);
1105 t = tree_cons (NULL_TREE, object_ptr_type_node,
1106 tree_cons (NULL_TREE, ptr_type_node,
1107 tree_cons (NULL_TREE, ptr_type_node,
1108 tree_cons (NULL_TREE, int_type_node,
1109 endlink))));
1110 soft_lookupjnimethod_node
1111 = builtin_function ("_Jv_LookupJNIMethod",
1112 build_function_type (ptr_type_node, t),
1113 0, NOT_BUILT_IN, NULL, NULL_TREE);
1114 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1115 soft_getjnienvnewframe_node
1116 = builtin_function ("_Jv_GetJNIEnvNewFrame",
1117 build_function_type (ptr_type_node, t),
1118 0, NOT_BUILT_IN, NULL, NULL_TREE);
1119 soft_jnipopsystemframe_node
1120 = builtin_function ("_Jv_JNI_PopSystemFrame",
1121 build_function_type (ptr_type_node, t),
1122 0, NOT_BUILT_IN, NULL, NULL_TREE);
1124 soft_idiv_node
1125 = builtin_function ("_Jv_divI",
1126 build_function_type (int_type_node, t),
1127 0, NOT_BUILT_IN, NULL, NULL_TREE);
1129 soft_irem_node
1130 = builtin_function ("_Jv_remI",
1131 build_function_type (int_type_node, t),
1132 0, NOT_BUILT_IN, NULL, NULL_TREE);
1134 soft_ldiv_node
1135 = builtin_function ("_Jv_divJ",
1136 build_function_type (long_type_node, t),
1137 0, NOT_BUILT_IN, NULL, NULL_TREE);
1139 soft_lrem_node
1140 = builtin_function ("_Jv_remJ",
1141 build_function_type (long_type_node, t),
1142 0, NOT_BUILT_IN, NULL, NULL_TREE);
1144 /* Initialize variables for except.c. */
1145 eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
1146 ? "__gcj_personality_sj0"
1147 : "__gcj_personality_v0");
1149 lang_eh_runtime_type = do_nothing;
1151 init_jcf_parse ();
1153 initialize_builtins ();
1154 soft_fmod_node = built_in_decls[BUILT_IN_FMOD];
1155 #if 0
1156 soft_fmodf_node = built_in_decls[BUILT_IN_FMODF];
1157 #endif
1159 parse_version ();
1163 /* Look up NAME in the current binding level and its superiors
1164 in the namespace of variables, functions and typedefs.
1165 Return a ..._DECL node of some kind representing its definition,
1166 or return 0 if it is undefined. */
1168 tree
1169 lookup_name (tree name)
1171 tree val;
1172 if (current_binding_level != global_binding_level
1173 && IDENTIFIER_LOCAL_VALUE (name))
1174 val = IDENTIFIER_LOCAL_VALUE (name);
1175 else
1176 val = IDENTIFIER_GLOBAL_VALUE (name);
1177 return val;
1180 /* Similar to `lookup_name' but look only at current binding level and
1181 the previous one if its the parameter level. */
1183 static tree
1184 lookup_name_current_level (tree name)
1186 tree t;
1188 if (current_binding_level == global_binding_level)
1189 return IDENTIFIER_GLOBAL_VALUE (name);
1191 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
1192 return 0;
1194 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
1195 if (DECL_NAME (t) == name)
1196 break;
1198 return t;
1201 /* Use a binding level to record a labeled block declaration */
1203 void
1204 push_labeled_block (tree lb)
1206 tree name = DECL_NAME (LABELED_BLOCK_LABEL (lb));
1207 struct binding_level *b = current_binding_level;
1208 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1209 if (oldlocal != 0)
1210 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1211 TREE_CHAIN (lb) = b->names;
1212 b->names = lb;
1213 IDENTIFIER_LOCAL_VALUE (name) = lb;
1216 /* Pop the current binding level, reinstalling values for the previous
1217 labeled block */
1219 void
1220 pop_labeled_block (void)
1222 struct binding_level *b = current_binding_level;
1223 tree label = b->names;
1224 IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label))) =
1225 NULL_TREE;
1226 if (b->shadowed)
1227 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b->shadowed)) =
1228 TREE_VALUE (b->shadowed);
1230 /* Pop the current level, and free the structure for reuse. */
1231 current_binding_level = current_binding_level->level_chain;
1232 b->level_chain = free_binding_level;
1233 free_binding_level = b;
1236 /* Record a decl-node X as belonging to the current lexical scope.
1237 Check for errors (such as an incompatible declaration for the same
1238 name already seen in the same scope).
1240 Returns either X or an old decl for the same name.
1241 If an old decl is returned, it may have been smashed
1242 to agree with what X says. */
1244 tree
1245 pushdecl (tree x)
1247 tree t;
1248 tree name = DECL_NAME (x);
1249 struct binding_level *b = current_binding_level;
1251 if (TREE_CODE (x) != TYPE_DECL)
1252 DECL_CONTEXT (x) = current_function_decl;
1253 if (name)
1255 t = lookup_name_current_level (name);
1256 if (t != 0 && t == error_mark_node)
1257 /* error_mark_node is 0 for a while during initialization! */
1259 t = 0;
1260 error ("%J'%D' used prior to declaration", x, x);
1263 /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1264 to point to the TYPE_DECL.
1265 Since Java does not have typedefs, a type can only have
1266 one (true) name, given by a class, interface, or builtin. */
1267 if (TREE_CODE (x) == TYPE_DECL
1268 && TYPE_NAME (TREE_TYPE (x)) == 0
1269 && TREE_TYPE (x) != error_mark_node)
1271 TYPE_NAME (TREE_TYPE (x)) = x;
1272 TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1275 /* This name is new in its binding level.
1276 Install the new declaration and return it. */
1277 if (b == global_binding_level)
1279 /* Install a global value. */
1281 IDENTIFIER_GLOBAL_VALUE (name) = x;
1283 else
1285 /* Here to install a non-global value. */
1286 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1287 IDENTIFIER_LOCAL_VALUE (name) = x;
1289 #if 0
1290 /* Warn if shadowing an argument at the top level of the body. */
1291 if (oldlocal != 0 && !DECL_EXTERNAL (x)
1292 /* This warning doesn't apply to the parms of a nested fcn. */
1293 && ! current_binding_level->parm_flag
1294 /* Check that this is one level down from the parms. */
1295 && current_binding_level->level_chain->parm_flag
1296 /* Check that the decl being shadowed
1297 comes from the parm level, one level up. */
1298 && chain_member (oldlocal, current_binding_level->level_chain->names))
1300 if (TREE_CODE (oldlocal) == PARM_DECL)
1301 pedwarn ("declaration of %qs shadows a parameter",
1302 IDENTIFIER_POINTER (name));
1303 else
1304 pedwarn ("declaration of %qs shadows a symbol from the parameter list",
1305 IDENTIFIER_POINTER (name));
1308 /* Maybe warn if shadowing something else. */
1309 else if (warn_shadow && !DECL_EXTERNAL (x)
1310 /* No shadow warnings for internally generated vars. */
1311 && DECL_SOURCE_LINE (x) != 0
1312 /* No shadow warnings for vars made for inlining. */
1313 && ! DECL_FROM_INLINE (x))
1315 const char *warnstring = 0;
1317 if (TREE_CODE (x) == PARM_DECL
1318 && current_binding_level->level_chain->parm_flag)
1319 /* Don't warn about the parm names in function declarator
1320 within a function declarator.
1321 It would be nice to avoid warning in any function
1322 declarator in a declaration, as opposed to a definition,
1323 but there is no way to tell it's not a definition. */
1325 else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
1326 warnstring = "declaration of %qs shadows a parameter";
1327 else if (oldlocal != 0)
1328 warnstring = "declaration of %qs shadows previous local";
1329 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1330 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1331 warnstring = "declaration of %qs shadows global declaration";
1333 if (warnstring)
1334 warning (warnstring, IDENTIFIER_POINTER (name));
1336 #endif
1338 /* If storing a local value, there may already be one (inherited).
1339 If so, record it for restoration when this binding level ends. */
1340 if (oldlocal != 0)
1341 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1345 /* Put decls on list in reverse order.
1346 We will reverse them later if necessary. */
1347 TREE_CHAIN (x) = b->names;
1348 b->names = x;
1350 return x;
1353 void
1354 pushdecl_force_head (tree x)
1356 current_binding_level->names = x;
1359 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1361 tree
1362 pushdecl_top_level (tree x)
1364 tree t;
1365 struct binding_level *b = current_binding_level;
1367 current_binding_level = global_binding_level;
1368 t = pushdecl (x);
1369 current_binding_level = b;
1370 return t;
1373 /* Like pushdecl, only it places X in FUNCTION_BINDING_LEVEL, if appropriate. */
1375 tree
1376 pushdecl_function_level (tree x)
1378 tree t;
1379 struct binding_level *b = current_binding_level;
1381 current_binding_level = function_binding_level;
1382 t = pushdecl (x);
1383 current_binding_level = b;
1384 return t;
1387 /* Nonzero if we are currently in the global binding level. */
1390 global_bindings_p (void)
1392 return current_binding_level == global_binding_level;
1395 /* Return the list of declarations of the current level.
1396 Note that this list is in reverse order unless/until
1397 you nreverse it; and when you do nreverse it, you must
1398 store the result back using `storedecls' or you will lose. */
1400 tree
1401 getdecls (void)
1403 return current_binding_level->names;
1406 /* Create a new `struct binding_level'. */
1408 static struct binding_level *
1409 make_binding_level (void)
1411 /* NOSTRICT */
1412 return ggc_alloc_cleared (sizeof (struct binding_level));
1415 void
1416 pushlevel (int unused ATTRIBUTE_UNUSED)
1418 struct binding_level *newlevel = NULL_BINDING_LEVEL;
1420 #if 0
1421 /* If this is the top level of a function,
1422 just make sure that NAMED_LABELS is 0. */
1424 if (current_binding_level == global_binding_level)
1425 named_labels = 0;
1426 #endif
1428 /* Reuse or create a struct for this binding level. */
1430 if (free_binding_level)
1432 newlevel = free_binding_level;
1433 free_binding_level = free_binding_level->level_chain;
1435 else
1437 newlevel = make_binding_level ();
1440 /* Add this level to the front of the chain (stack) of levels that
1441 are active. */
1443 *newlevel = clear_binding_level;
1444 newlevel->level_chain = current_binding_level;
1445 current_binding_level = newlevel;
1446 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1447 newlevel->binding_depth = binding_depth;
1448 indent ();
1449 fprintf (stderr, "push %s level %p pc %d\n",
1450 (is_class_level) ? "class" : "block", newlevel, current_pc);
1451 is_class_level = 0;
1452 binding_depth++;
1453 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1456 /* Exit a binding level.
1457 Pop the level off, and restore the state of the identifier-decl mappings
1458 that were in effect when this level was entered.
1460 If KEEP is nonzero, this level had explicit declarations, so
1461 and create a "block" (a BLOCK node) for the level
1462 to record its declarations and subblocks for symbol table output.
1464 If FUNCTIONBODY is nonzero, this level is the body of a function,
1465 so create a block as if KEEP were set and also clear out all
1466 label names.
1468 If REVERSE is nonzero, reverse the order of decls before putting
1469 them into the BLOCK. */
1471 tree
1472 poplevel (int keep, int reverse, int functionbody)
1474 tree link;
1475 /* The chain of decls was accumulated in reverse order.
1476 Put it into forward order, just for cleanliness. */
1477 tree decls;
1478 tree subblocks = current_binding_level->blocks;
1479 tree block = 0;
1480 tree decl;
1481 tree bind = 0;
1483 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1484 binding_depth--;
1485 indent ();
1486 if (current_binding_level->end_pc != LARGEST_PC)
1487 fprintf (stderr, "pop %s level %p pc %d (end pc %d)\n",
1488 (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1489 current_binding_level->end_pc);
1490 else
1491 fprintf (stderr, "pop %s level %p pc %d\n",
1492 (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1493 #if 0
1494 if (is_class_level != (current_binding_level == class_binding_level))
1496 indent ();
1497 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
1499 is_class_level = 0;
1500 #endif
1501 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1503 /* Get the decls in the order they were written.
1504 Usually current_binding_level->names is in reverse order.
1505 But parameter decls were previously put in forward order. */
1507 if (reverse)
1508 current_binding_level->names
1509 = decls = nreverse (current_binding_level->names);
1510 else
1511 decls = current_binding_level->names;
1513 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1514 if (TREE_CODE (decl) == VAR_DECL
1515 && DECL_LANG_SPECIFIC (decl) != NULL
1516 && DECL_LOCAL_SLOT_NUMBER (decl))
1517 LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
1519 /* If there were any declarations in that level,
1520 or if this level is a function body,
1521 create a BLOCK to record them for the life of this function. */
1523 block = 0;
1524 if (keep || functionbody)
1526 block = make_node (BLOCK);
1527 TREE_TYPE (block) = void_type_node;
1530 if (current_binding_level->exception_range)
1531 expand_end_java_handler (current_binding_level->exception_range);
1533 if (block != 0)
1535 /* If any statements have been generated at this level, create a
1536 BIND_EXPR to hold them and copy the variables to it. This
1537 only applies to the bytecode compiler. */
1538 if (current_binding_level->stmts)
1540 tree decl = decls;
1541 tree *var = &BLOCK_VARS (block);
1543 /* Copy decls from names list, ignoring labels. */
1544 while (decl)
1546 tree next = TREE_CHAIN (decl);
1547 if (TREE_CODE (decl) != LABEL_DECL)
1549 *var = decl;
1550 var = &TREE_CHAIN (decl);
1552 decl = next;
1554 *var = NULL;
1556 bind = build3 (BIND_EXPR, TREE_TYPE (block), BLOCK_VARS (block),
1557 BLOCK_EXPR_BODY (block), block);
1558 BIND_EXPR_BODY (bind) = current_binding_level->stmts;
1560 if (BIND_EXPR_BODY (bind)
1561 && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
1562 TREE_SIDE_EFFECTS (bind) = 1;
1564 /* FIXME: gimplifier brain damage. */
1565 if (BIND_EXPR_BODY (bind) == NULL)
1566 BIND_EXPR_BODY (bind) = build_java_empty_stmt ();
1568 current_binding_level->stmts = NULL;
1570 else
1572 BLOCK_VARS (block) = decls;
1574 BLOCK_SUBBLOCKS (block) = subblocks;
1577 /* In each subblock, record that this is its superior. */
1579 for (link = subblocks; link; link = TREE_CHAIN (link))
1580 BLOCK_SUPERCONTEXT (link) = block;
1582 /* Clear out the meanings of the local variables of this level. */
1584 for (link = decls; link; link = TREE_CHAIN (link))
1586 tree name = DECL_NAME (link);
1587 if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1589 /* If the ident. was used or addressed via a local extern decl,
1590 don't forget that fact. */
1591 if (DECL_EXTERNAL (link))
1593 if (TREE_USED (link))
1594 TREE_USED (name) = 1;
1595 if (TREE_ADDRESSABLE (link))
1596 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1598 IDENTIFIER_LOCAL_VALUE (name) = 0;
1602 /* Restore all name-meanings of the outer levels
1603 that were shadowed by this level. */
1605 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1606 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1608 /* If the level being exited is the top level of a function,
1609 check over all the labels, and clear out the current
1610 (function local) meanings of their names. */
1612 if (functionbody)
1614 /* If this is the top level block of a function,
1615 the vars are the function's parameters.
1616 Don't leave them in the BLOCK because they are
1617 found in the FUNCTION_DECL instead. */
1619 BLOCK_VARS (block) = 0;
1621 /* Clear out the definitions of all label names,
1622 since their scopes end here,
1623 and add them to BLOCK_VARS. */
1625 #if 0
1626 for (link = named_labels; link; link = TREE_CHAIN (link))
1628 tree label = TREE_VALUE (link);
1630 if (DECL_INITIAL (label) == 0)
1632 error ("%Jlabel '%D' used but not defined", label, label);
1633 /* Avoid crashing later. */
1634 define_label (input_location, DECL_NAME (label));
1636 else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
1637 warning ("%Jlabel '%D' defined but not used", label, label);
1638 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1640 /* Put the labels into the "variables" of the
1641 top-level block, so debugger can see them. */
1642 TREE_CHAIN (label) = BLOCK_VARS (block);
1643 BLOCK_VARS (block) = label;
1645 #endif
1648 /* Pop the current level, and free the structure for reuse. */
1651 struct binding_level *level = current_binding_level;
1652 current_binding_level = current_binding_level->level_chain;
1654 level->level_chain = free_binding_level;
1655 free_binding_level = level;
1658 /* Dispose of the block that we just made inside some higher level. */
1659 if (functionbody)
1661 DECL_INITIAL (current_function_decl) = block;
1662 DECL_SAVED_TREE (current_function_decl) = bind;
1664 else
1666 if (block)
1668 current_binding_level->blocks
1669 = chainon (current_binding_level->blocks, block);
1671 /* If we did not make a block for the level just exited,
1672 any blocks made for inner levels
1673 (since they cannot be recorded as subblocks in that level)
1674 must be carried forward so they will later become subblocks
1675 of something else. */
1676 else if (subblocks)
1677 current_binding_level->blocks
1678 = chainon (current_binding_level->blocks, subblocks);
1680 if (bind)
1681 java_add_stmt (bind);
1684 if (block)
1685 TREE_USED (block) = 1;
1686 return block;
1689 void
1690 maybe_pushlevels (int pc)
1692 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1693 current_pc = pc;
1694 #endif
1696 while (pending_local_decls != NULL_TREE &&
1697 DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1699 tree *ptr = &pending_local_decls;
1700 tree decl = *ptr, next;
1701 int end_pc = DECL_LOCAL_END_PC (decl);
1703 while (*ptr != NULL_TREE
1704 && DECL_LOCAL_START_PC (*ptr) <= pc
1705 && DECL_LOCAL_END_PC (*ptr) == end_pc)
1706 ptr = &TREE_CHAIN (*ptr);
1707 pending_local_decls = *ptr;
1708 *ptr = NULL_TREE;
1710 /* Force non-nested range to be nested in current range by
1711 truncating variable lifetimes. */
1712 if (end_pc > current_binding_level->end_pc)
1714 end_pc = current_binding_level->end_pc;
1715 DECL_LOCAL_END_PC (decl) = end_pc;
1718 maybe_start_try (pc, end_pc);
1720 pushlevel (1);
1722 current_binding_level->end_pc = end_pc;
1723 current_binding_level->start_pc = pc;
1724 current_binding_level->names = NULL;
1725 for ( ; decl != NULL_TREE; decl = next)
1727 next = TREE_CHAIN (decl);
1728 push_jvm_slot (DECL_LOCAL_SLOT_NUMBER (decl), decl);
1729 pushdecl (decl);
1730 initialize_local_variable (decl, DECL_LOCAL_SLOT_NUMBER (decl));
1734 maybe_start_try (pc, 0);
1737 void
1738 maybe_poplevels (int pc)
1740 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1741 current_pc = pc;
1742 #endif
1744 while (current_binding_level->end_pc <= pc)
1745 poplevel (1, 0, 0);
1748 /* Terminate any binding which began during the range beginning at
1749 start_pc. This tidies up improperly nested local variable ranges
1750 and exception handlers; a variable declared within an exception
1751 range is forcibly terminated when that exception ends. */
1753 void
1754 force_poplevels (int start_pc)
1756 while (current_binding_level->start_pc > start_pc)
1758 if (pedantic && current_binding_level->start_pc > start_pc)
1759 warning ("%JIn %D: overlapped variable and exception ranges at %d",
1760 current_function_decl, current_function_decl,
1761 current_binding_level->start_pc);
1762 poplevel (1, 0, 0);
1766 /* Insert BLOCK at the end of the list of subblocks of the
1767 current binding level. This is used when a BIND_EXPR is expanded,
1768 to handle the BLOCK node inside the BIND_EXPR. */
1770 void
1771 insert_block (tree block)
1773 TREE_USED (block) = 1;
1774 current_binding_level->blocks
1775 = chainon (current_binding_level->blocks, block);
1778 /* integrate_decl_tree calls this function. */
1780 void
1781 java_dup_lang_specific_decl (tree node)
1783 int lang_decl_size;
1784 struct lang_decl *x;
1786 if (!DECL_LANG_SPECIFIC (node))
1787 return;
1789 lang_decl_size = sizeof (struct lang_decl);
1790 x = ggc_alloc (lang_decl_size);
1791 memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1792 DECL_LANG_SPECIFIC (node) = x;
1795 void
1796 give_name_to_locals (JCF *jcf)
1798 int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1799 int code_offset = DECL_CODE_OFFSET (current_function_decl);
1800 tree parm;
1801 pending_local_decls = NULL_TREE;
1802 if (n == 0)
1803 return;
1804 JCF_SEEK (jcf, n);
1805 n = JCF_readu2 (jcf);
1806 for (i = 0; i < n; i++)
1808 int start_pc = JCF_readu2 (jcf);
1809 int length = JCF_readu2 (jcf);
1810 int name_index = JCF_readu2 (jcf);
1811 int signature_index = JCF_readu2 (jcf);
1812 int slot = JCF_readu2 (jcf);
1813 tree name = get_name_constant (jcf, name_index);
1814 tree type = parse_signature (jcf, signature_index);
1815 if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1816 && start_pc == 0
1817 && length == DECL_CODE_LENGTH (current_function_decl))
1819 tree decl = TREE_VEC_ELT (decl_map, slot);
1820 DECL_NAME (decl) = name;
1821 SET_DECL_ASSEMBLER_NAME (decl, name);
1822 if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1823 warning ("bad type in parameter debug info");
1825 else
1827 tree *ptr;
1828 int end_pc = start_pc + length;
1829 tree decl = build_decl (VAR_DECL, name, type);
1830 if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1832 warning ("%Jbad PC range for debug info for local '%D'",
1833 decl, decl);
1834 end_pc = DECL_CODE_LENGTH (current_function_decl);
1837 /* Adjust start_pc if necessary so that the local's first
1838 store operation will use the relevant DECL as a
1839 destination. Fore more information, read the leading
1840 comments for expr.c:maybe_adjust_start_pc. */
1841 start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1843 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1844 DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1845 DECL_LOCAL_START_PC (decl) = start_pc;
1846 #if 0
1847 /* FIXME: The range used internally for exceptions and local
1848 variable ranges, is a half-open interval:
1849 start_pc <= pc < end_pc. However, the range used in the
1850 Java VM spec is inclusive at both ends:
1851 start_pc <= pc <= end_pc. */
1852 end_pc++;
1853 #endif
1854 DECL_LOCAL_END_PC (decl) = end_pc;
1856 /* Now insert the new decl in the proper place in
1857 pending_local_decls. We are essentially doing an insertion sort,
1858 which works fine, since the list input will normally already
1859 be sorted. */
1860 ptr = &pending_local_decls;
1861 while (*ptr != NULL_TREE
1862 && (DECL_LOCAL_START_PC (*ptr) > start_pc
1863 || (DECL_LOCAL_START_PC (*ptr) == start_pc
1864 && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1865 ptr = &TREE_CHAIN (*ptr);
1866 TREE_CHAIN (decl) = *ptr;
1867 *ptr = decl;
1871 pending_local_decls = nreverse (pending_local_decls);
1873 /* Fill in default names for the parameters. */
1874 for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1875 parm != NULL_TREE; parm = TREE_CHAIN (parm), i++)
1877 if (DECL_NAME (parm) == NULL_TREE)
1879 int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1880 if (arg_i == 0)
1881 DECL_NAME (parm) = get_identifier ("this");
1882 else
1884 char buffer[12];
1885 sprintf (buffer, "ARG_%d", arg_i);
1886 DECL_NAME (parm) = get_identifier (buffer);
1888 SET_DECL_ASSEMBLER_NAME (parm, DECL_NAME (parm));
1893 tree
1894 build_result_decl (tree fndecl)
1896 tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1897 tree result = DECL_RESULT (fndecl);
1898 if (! result)
1900 /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1901 if (INTEGRAL_TYPE_P (restype)
1902 && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
1903 restype = integer_type_node;
1904 result = build_decl (RESULT_DECL, NULL_TREE, restype);
1905 DECL_ARTIFICIAL (result) = 1;
1906 DECL_IGNORED_P (result) = 1;
1907 DECL_CONTEXT (result) = fndecl;
1908 DECL_RESULT (fndecl) = result;
1910 return result;
1913 void
1914 start_java_method (tree fndecl)
1916 tree tem, *ptr;
1917 int i;
1919 uniq = 0;
1921 current_function_decl = fndecl;
1922 announce_function (fndecl);
1924 i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1925 decl_map = make_tree_vec (i);
1926 base_decl_map = make_tree_vec (i);
1927 type_map = xrealloc (type_map, i * sizeof (tree));
1929 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1930 fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
1931 current_pc = 0;
1932 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1933 pushlevel (1); /* Push parameters. */
1935 ptr = &DECL_ARGUMENTS (fndecl);
1936 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1937 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
1939 tree parm_name = NULL_TREE, parm_decl;
1940 tree parm_type = TREE_VALUE (tem);
1941 if (i >= DECL_MAX_LOCALS (fndecl))
1942 abort ();
1944 parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
1945 DECL_CONTEXT (parm_decl) = fndecl;
1946 if (targetm.calls.promote_prototypes (parm_type)
1947 && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
1948 && INTEGRAL_TYPE_P (parm_type))
1949 parm_type = integer_type_node;
1950 DECL_ARG_TYPE (parm_decl) = parm_type;
1952 *ptr = parm_decl;
1953 ptr = &TREE_CHAIN (parm_decl);
1955 /* Add parm_decl to the decl_map. */
1956 push_jvm_slot (i, parm_decl);
1958 type_map[i] = TREE_TYPE (parm_decl);
1959 if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
1961 i++;
1962 type_map[i] = void_type_node;
1965 *ptr = NULL_TREE;
1966 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
1968 while (i < DECL_MAX_LOCALS(fndecl))
1969 type_map[i++] = NULL_TREE;
1971 build_result_decl (fndecl);
1973 /* Push local variables. */
1974 pushlevel (2);
1976 function_binding_level = current_binding_level;
1979 void
1980 end_java_method (void)
1982 tree fndecl = current_function_decl;
1984 /* pop out of function */
1985 poplevel (1, 1, 0);
1987 /* pop out of its parameters */
1988 poplevel (1, 0, 1);
1990 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
1992 if (DECL_SAVED_TREE (fndecl))
1994 tree fbody, block_body;
1995 /* Before we check initialization, attached all class initialization
1996 variable to the block_body */
1997 fbody = DECL_SAVED_TREE (fndecl);
1998 block_body = BIND_EXPR_BODY (fbody);
1999 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
2000 attach_init_test_initialization_flags, block_body);
2003 flag_unit_at_a_time = 0;
2004 finish_method (fndecl);
2006 if (! flag_unit_at_a_time)
2008 /* Nulling these fields when we no longer need them saves
2009 memory. */
2010 DECL_SAVED_TREE (fndecl) = NULL;
2011 DECL_STRUCT_FUNCTION (fndecl) = NULL;
2012 DECL_INITIAL (fndecl) = NULL_TREE;
2014 current_function_decl = NULL_TREE;
2017 /* Prepare a method for expansion. */
2019 void
2020 finish_method (tree fndecl)
2022 tree *tp = &DECL_SAVED_TREE (fndecl);
2024 /* Wrap body of synchronized methods in a monitorenter,
2025 plus monitorexit cleanup. */
2026 if (METHOD_SYNCHRONIZED (fndecl))
2028 tree enter, exit, lock;
2029 if (METHOD_STATIC (fndecl))
2030 lock = build_class_ref (DECL_CONTEXT (fndecl));
2031 else
2032 lock = DECL_ARGUMENTS (fndecl);
2033 BUILD_MONITOR_ENTER (enter, lock);
2034 BUILD_MONITOR_EXIT (exit, lock);
2035 *tp = build2 (COMPOUND_EXPR, void_type_node, enter,
2036 build2 (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
2039 /* Prepend class initialization for static methods reachable from
2040 other classes. */
2041 if (METHOD_STATIC (fndecl) && ! METHOD_PRIVATE (fndecl)
2042 && ! DECL_CLINIT_P (fndecl)
2043 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
2045 tree clas = DECL_CONTEXT (fndecl);
2046 tree init = build3 (CALL_EXPR, void_type_node,
2047 build_address_of (soft_initclass_node),
2048 build_tree_list (NULL_TREE, build_class_ref (clas)),
2049 NULL_TREE);
2050 *tp = build2 (COMPOUND_EXPR, TREE_TYPE (*tp), init, *tp);
2053 /* Convert function tree to GENERIC prior to inlining. */
2054 java_genericize (fndecl);
2056 /* Store the end of the function, so that we get good line number
2057 info for the epilogue. */
2058 if (DECL_STRUCT_FUNCTION (fndecl))
2059 cfun = DECL_STRUCT_FUNCTION (fndecl);
2060 else
2061 allocate_struct_function (fndecl);
2062 #ifdef USE_MAPPED_LOCATION
2063 cfun->function_end_locus = DECL_FUNCTION_LAST_LINE (fndecl);
2064 #else
2065 cfun->function_end_locus.file = DECL_SOURCE_FILE (fndecl);
2066 cfun->function_end_locus.line = DECL_FUNCTION_LAST_LINE (fndecl);
2067 #endif
2069 /* Defer inlining and expansion to the cgraph optimizers. */
2070 cgraph_finalize_function (fndecl, false);
2073 /* Optimize and expand a function's entire body. */
2075 void
2076 java_expand_body (tree fndecl)
2078 tree_rest_of_compilation (fndecl);
2081 /* We pessimistically marked all methods and fields external until we
2082 knew what set of classes we were planning to compile. Now mark those
2083 associated with CLASS to be generated locally as not external. */
2085 static void
2086 java_mark_decl_local (tree decl)
2088 DECL_EXTERNAL (decl) = 0;
2090 /* If we've already constructed DECL_RTL, give encode_section_info
2091 a second chance, now that we've changed the flags. */
2092 if (DECL_RTL_SET_P (decl))
2093 make_decl_rtl (decl);
2096 void
2097 java_mark_class_local (tree class)
2099 tree t;
2101 for (t = TYPE_FIELDS (class); t ; t = TREE_CHAIN (t))
2102 if (FIELD_STATIC (t))
2103 java_mark_decl_local (t);
2105 for (t = TYPE_METHODS (class); t ; t = TREE_CHAIN (t))
2106 if (!METHOD_ABSTRACT (t) && (!METHOD_NATIVE (t) || flag_jni))
2107 java_mark_decl_local (t);
2110 /* Add a statement to a compound_expr. */
2112 tree
2113 add_stmt_to_compound (tree existing, tree type, tree stmt)
2115 if (!stmt)
2116 return existing;
2117 else if (existing)
2119 tree expr = build2 (COMPOUND_EXPR, type, existing, stmt);
2120 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (existing)
2121 | TREE_SIDE_EFFECTS (stmt);
2122 return expr;
2124 else
2125 return stmt;
2128 /* Add a statement to the compound_expr currently being
2129 constructed. */
2131 tree
2132 java_add_stmt (tree stmt)
2134 if (input_filename)
2135 SET_EXPR_LOCATION (stmt, input_location);
2137 return current_binding_level->stmts
2138 = add_stmt_to_compound (current_binding_level->stmts,
2139 TREE_TYPE (stmt), stmt);
2142 /* Add a variable to the current scope. */
2144 tree
2145 java_add_local_var (tree decl)
2147 tree *vars = &current_binding_level->names;
2148 tree next = *vars;
2149 TREE_CHAIN (decl) = next;
2150 *vars = decl;
2151 DECL_CONTEXT (decl) = current_function_decl;
2152 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2153 return decl;
2156 /* Return a pointer to the compound_expr currently being
2157 constructed. */
2159 tree *
2160 get_stmts (void)
2162 return &current_binding_level->stmts;
2165 /* Register an exception range as belonging to the current binding
2166 level. There may only be one: if there are more, we'll create more
2167 binding levels. However, each range can have multiple handlers,
2168 and these are expanded when we call expand_end_java_handler(). */
2170 void
2171 register_exception_range (struct eh_range *range, int pc, int end_pc)
2173 if (current_binding_level->exception_range)
2174 abort ();
2175 current_binding_level->exception_range = range;
2176 current_binding_level->end_pc = end_pc;
2177 current_binding_level->start_pc = pc;
2180 #include "gt-java-decl.h"