Daily bump.
[gcc.git] / gcc / java / decl.c
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.
5
6 This file is part of GCC.
7
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.
12
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.
17
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, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22
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. */
26
27 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
28
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"
51
52 #if defined (DEBUG_JAVA_BINDING_LEVELS)
53 extern void indent (void);
54 #endif
55
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);
63
64
65 /* The following ABI flags are used in the high-order bits of the version
66 ID field. The version ID number itself should never be larger than
67 0xfffff, so it should be safe to use top 12 bits for these flags. */
68
69 #define FLAG_BINARYCOMPAT_ABI (1<<31) /* Class is built with the BC-ABI. */
70
71 #define FLAG_BOOTSTRAP_LOADER (1<<30) /* Used when defining a class that
72 should be loaded by the bootstrap
73 loader. */
74
75 /* If an ABI change is made within a GCC release series, rendering current
76 binaries incompatible with the old runtimes, this number can be set to
77 enforce the compatibility rules. */
78 #define MINOR_BINARYCOMPAT_ABI_VERSION 0
79
80 /* The runtime may recognize a variety of BC ABIs (objects generated by
81 different version of gcj), but will probably always require strict
82 matching for the ordinary (C++) ABI. */
83
84 /* The version ID of the BC ABI that we generate. This must be kept in
85 sync with parse_version(), libgcj, and reality (if the BC format changes,
86 this must change). */
87 #define GCJ_CURRENT_BC_ABI_VERSION \
88 (4 * 100000 + 0 * 1000 + MINOR_BINARYCOMPAT_ABI_VERSION)
89
90 /* The ABI version number. */
91 tree gcj_abi_version;
92
93 /* Name of the Cloneable class. */
94 tree java_lang_cloneable_identifier_node;
95
96 /* Name of the Serializable class. */
97 tree java_io_serializable_identifier_node;
98
99 /* The DECL_MAP is a mapping from (index, type) to a decl node.
100 If index < max_locals, it is the index of a local variable.
101 if index >= max_locals, then index-max_locals is a stack slot.
102 The DECL_MAP mapping is represented as a TREE_VEC whose elements
103 are a list of decls (VAR_DECL or PARM_DECL) chained by
104 DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
105 we search the chain for a decl with a matching TREE_TYPE. */
106
107 static GTY(()) tree decl_map;
108
109 /* The base_decl_map is contains one variable of ptr_type: this is
110 used to contain every variable of reference type that is ever
111 stored in a local variable slot. */
112
113 static GTY(()) tree base_decl_map;
114
115 /* An index used to make temporary identifiers unique. */
116 static int uniq;
117
118 /* A list of local variables VAR_DECLs for this method that we have seen
119 debug information, but we have not reached their starting (byte) PC yet. */
120
121 static GTY(()) tree pending_local_decls;
122
123 /* The decl for "_Jv_ResolvePoolEntry". */
124 tree soft_resolvepoolentry_node;
125
126 #if defined(DEBUG_JAVA_BINDING_LEVELS)
127 int binding_depth = 0;
128 int is_class_level = 0;
129 int current_pc;
130
131 void
132 indent (void)
133 {
134 int i;
135
136 for (i = 0; i < binding_depth*2; i++)
137 putc (' ', stderr);
138 }
139 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
140
141 /* True if decl is a named local variable, i.e. if it is an alias
142 that's used only for debugging purposes. */
143
144 static bool
145 debug_variable_p (tree decl)
146 {
147 if (TREE_CODE (decl) == PARM_DECL)
148 return false;
149
150 if (LOCAL_SLOT_P (decl))
151 return false;
152
153 return true;
154 }
155
156 /* Copy the value in decl into every live alias in the same local
157 variable slot. Some of these will be dead stores removed by the
158 optimizer. */
159
160 void
161 update_aliases (tree decl, int index, int pc)
162 {
163 tree decl_type = TREE_TYPE (decl);
164 tree tmp;
165
166 gcc_assert (! debug_variable_p (decl));
167
168 for (tmp = TREE_VEC_ELT (decl_map, index);
169 tmp != NULL_TREE;
170 tmp = DECL_LOCAL_SLOT_CHAIN (tmp))
171 {
172 tree tmp_type = TREE_TYPE (tmp);
173 if (tmp != decl
174 && LOCAL_SLOT_P (tmp) == 0
175 && (pc == -1
176 || (pc >= DECL_LOCAL_START_PC (tmp)
177 && pc < DECL_LOCAL_END_PC (tmp)))
178 /* This test is < (rather than <=) because there's no point
179 updating an alias that's about to die at the end of this
180 instruction. */
181 && (tmp_type == decl_type
182 || (INTEGRAL_TYPE_P (tmp_type)
183 && INTEGRAL_TYPE_P (decl_type)
184 && TYPE_PRECISION (decl_type) <= 32
185 && TYPE_PRECISION (tmp_type) <= 32)
186 || (TREE_CODE (tmp_type) == POINTER_TYPE
187 && TREE_CODE (decl_type) == POINTER_TYPE)))
188 {
189 tree src = build1 (NOP_EXPR, tmp_type, decl);
190 gcc_assert (! LOCAL_VAR_OUT_OF_SCOPE_P (tmp));
191 java_add_stmt (build2 (MODIFY_EXPR, tmp_type, tmp, src));
192 }
193 }
194 }
195
196 static tree
197 push_jvm_slot (int index, tree decl)
198 {
199 DECL_CONTEXT (decl) = current_function_decl;
200 layout_decl (decl, 0);
201
202 /* Now link the decl into the decl_map. */
203 if (DECL_LANG_SPECIFIC (decl) == NULL)
204 {
205 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
206 DECL_LOCAL_START_PC (decl) = 0;
207 DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
208 DECL_LOCAL_SLOT_NUMBER (decl) = index;
209 }
210 DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
211 TREE_VEC_ELT (decl_map, index) = decl;
212
213 return decl;
214 }
215
216 /* At the point of its creation a local variable decl inherits
217 whatever is already in the same slot. In the case of a local
218 variable that is declared but unused, we won't find anything. */
219
220 static void
221 initialize_local_variable (tree decl, int index)
222 {
223 tree decl_type = TREE_TYPE (decl);
224 if (TREE_CODE (decl_type) == POINTER_TYPE)
225 {
226 tree tmp = TREE_VEC_ELT (base_decl_map, index);
227
228 if (tmp)
229 {
230 /* At the point of its creation this decl inherits whatever
231 is in the slot. */
232 tree src = build1 (NOP_EXPR, decl_type, tmp);
233 java_add_stmt (build2 (MODIFY_EXPR, decl_type, decl, src));
234 }
235 }
236 else
237 {
238 tree tmp;
239
240 for (tmp = TREE_VEC_ELT (decl_map, index);
241 tmp != NULL_TREE;
242 tmp = DECL_LOCAL_SLOT_CHAIN (tmp))
243 {
244 tree tmp_type = TREE_TYPE (tmp);
245 if (tmp != decl
246 && ! debug_variable_p (tmp)
247 && (tmp_type == decl_type
248 || (INTEGRAL_TYPE_P (tmp_type)
249 && INTEGRAL_TYPE_P (decl_type)
250 && TYPE_PRECISION (decl_type) <= 32
251 && TYPE_PRECISION (tmp_type) <= 32
252 && TYPE_PRECISION (tmp_type)
253 >= TYPE_PRECISION (decl_type))))
254 {
255 java_add_stmt (build2 (MODIFY_EXPR, decl_type, decl, tmp));
256 return;
257 }
258 }
259 }
260 }
261
262 /* Find the best declaration based upon type. If 'decl' fits 'type' better
263 than 'best', return 'decl'. Otherwise return 'best'. */
264
265 static tree
266 check_local_unnamed_variable (tree best, tree decl, tree type)
267 {
268 tree decl_type = TREE_TYPE (decl);
269
270 gcc_assert (! LOCAL_VAR_OUT_OF_SCOPE_P (decl));
271
272 /* Use the same decl for all integer types <= 32 bits. This is
273 necessary because sometimes a value is stored as (for example)
274 boolean but loaded as int. */
275 if (decl_type == type
276 || (INTEGRAL_TYPE_P (decl_type)
277 && INTEGRAL_TYPE_P (type)
278 && TYPE_PRECISION (decl_type) <= 32
279 && TYPE_PRECISION (type) <= 32
280 && TYPE_PRECISION (decl_type) >= TYPE_PRECISION (type))
281 /* ptr_type_node is used for null pointers, which are
282 assignment compatible with everything. */
283 || (TREE_CODE (decl_type) == POINTER_TYPE
284 && type == ptr_type_node)
285 /* Whenever anyone wants to use a slot that is initially
286 occupied by a PARM_DECL of pointer type they must get that
287 decl, even if they asked for a pointer to a different type.
288 However, if someone wants a scalar variable in a slot that
289 initially held a pointer arg -- or vice versa -- we create a
290 new VAR_DECL.
291
292 ???: As long as verification is correct, this will be a
293 compatible type. But maybe we should create a dummy variable
294 and replace all references to it with the DECL and a
295 NOP_EXPR.
296 */
297 || (TREE_CODE (decl_type) == POINTER_TYPE
298 && TREE_CODE (decl) == PARM_DECL
299 && TREE_CODE (type) == POINTER_TYPE))
300 {
301 if (best == NULL_TREE
302 || (decl_type == type && TREE_TYPE (best) != type))
303 return decl;
304 }
305
306 return best;
307 }
308
309
310 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
311 that is valid at PC (or -1 if any pc).
312 If there is no existing matching decl, allocate one. */
313
314 tree
315 find_local_variable (int index, tree type, int pc ATTRIBUTE_UNUSED)
316 {
317 tree tmp = TREE_VEC_ELT (decl_map, index);
318 tree decl = NULL_TREE;
319
320 /* Scan through every declaration that has been created in this
321 slot. We're only looking for variables that correspond to local
322 index declarations and PARM_DECLs, not named variables: such
323 local variables are used only for debugging information. */
324 while (tmp != NULL_TREE)
325 {
326 if (! debug_variable_p (tmp))
327 decl = check_local_unnamed_variable (decl, tmp, type);
328 tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
329 }
330
331 /* gcj has a function called promote_type(), which is used by both
332 the bytecode compiler and the source compiler. Unfortunately,
333 the type systems for the Java VM and the Java language are not
334 the same: a boolean in the VM promotes to an int, not to a wide
335 boolean. If our caller wants something to hold a boolean, that
336 had better be an int, because that slot might be re-used
337 later in integer context. */
338 if (TREE_CODE (type) == BOOLEAN_TYPE)
339 type = integer_type_node;
340
341 /* If we don't find a match, create one with the type passed in.
342 The name of the variable is #n#m, which n is the variable index
343 in the local variable area and m is a dummy identifier for
344 uniqueness -- multiple variables may share the same local
345 variable index. We don't call pushdecl() to push pointer types
346 into a binding expr because they'll all be replaced by a single
347 variable that is used for every reference in that local variable
348 slot. */
349 if (! decl)
350 {
351 char buf[64];
352 tree name;
353 sprintf (buf, "#slot#%d#%d", index, uniq++);
354 name = get_identifier (buf);
355 decl = build_decl (VAR_DECL, name, type);
356 DECL_IGNORED_P (decl) = 1;
357 DECL_ARTIFICIAL (decl) = 1;
358 decl = push_jvm_slot (index, decl);
359 LOCAL_SLOT_P (decl) = 1;
360
361 if (TREE_CODE (type) != POINTER_TYPE)
362 pushdecl_function_level (decl);
363 }
364
365 /* As well as creating a local variable that matches the type, we
366 also create a base variable (of ptr_type) that will hold all its
367 aliases. */
368 if (TREE_CODE (type) == POINTER_TYPE
369 && ! TREE_VEC_ELT (base_decl_map, index))
370 {
371 char buf[64];
372 tree name;
373 tree base_decl;
374 sprintf (buf, "#ref#%d#%d", index, uniq++);
375 name = get_identifier (buf);
376 base_decl
377 = TREE_VEC_ELT (base_decl_map, index)
378 = build_decl (VAR_DECL, name, ptr_type_node);
379 pushdecl_function_level (base_decl);
380 DECL_IGNORED_P (base_decl) = 1;
381 DECL_ARTIFICIAL (base_decl) = 1;
382 }
383
384 return decl;
385 }
386
387 /* Called during gimplification for every variable. If the variable
388 is a temporary of pointer type, replace it with a common variable
389 thath is used to hold all pointer types that are ever stored in
390 that slot. Set WANT_LVALUE if you want a variable that is to be
391 written to. */
392
393 tree
394 java_replace_reference (tree var_decl, bool want_lvalue)
395 {
396 tree decl_type;
397
398 if (! base_decl_map)
399 return var_decl;
400
401 decl_type = TREE_TYPE (var_decl);
402
403 if (TREE_CODE (decl_type) == POINTER_TYPE)
404 {
405 if (DECL_LANG_SPECIFIC (var_decl)
406 && LOCAL_SLOT_P (var_decl))
407 {
408 int index = DECL_LOCAL_SLOT_NUMBER (var_decl);
409 tree base_decl = TREE_VEC_ELT (base_decl_map, index);
410
411 gcc_assert (base_decl);
412 if (! want_lvalue)
413 base_decl = build1 (NOP_EXPR, decl_type, base_decl);
414
415 return base_decl;
416 }
417 }
418
419 return var_decl;
420 }
421
422
423 /* Same as find_local_index, except that INDEX is a stack index. */
424
425 tree
426 find_stack_slot (int index, tree type)
427 {
428 return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
429 type, -1);
430 }
431
432 struct binding_level GTY(())
433 {
434 /* A chain of _DECL nodes for all variables, constants, functions,
435 * and typedef types. These are in the reverse of the order supplied.
436 */
437 tree names;
438
439 /* For each level, a list of shadowed outer-level local definitions
440 to be restored when this level is popped.
441 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
442 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
443 tree shadowed;
444
445 /* For each level (except not the global one),
446 a chain of BLOCK nodes for all the levels
447 that were entered and exited one level down. */
448 tree blocks;
449
450 /* The binding level which this one is contained in (inherits from). */
451 struct binding_level *level_chain;
452
453 /* The bytecode PC that marks the end of this level. */
454 int end_pc;
455 /* The bytecode PC that marks the start of this level. */
456 int start_pc;
457
458 /* The statements in this binding level. */
459 tree stmts;
460
461 /* An exception range associated with this binding level. */
462 struct eh_range * GTY((skip (""))) exception_range;
463
464 /* Binding depth at which this level began. Used only for debugging. */
465 unsigned binding_depth;
466 };
467
468 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
469
470 /* The binding level currently in effect. */
471
472 static GTY(()) struct binding_level *current_binding_level;
473
474 /* A chain of binding_level structures awaiting reuse. */
475
476 static GTY(()) struct binding_level *free_binding_level;
477
478 /* The outermost binding level, for names of file scope.
479 This is created when the compiler is started and exists
480 through the entire run. */
481
482 static GTY(()) struct binding_level *global_binding_level;
483
484 /* The binding level that holds variables declared at the outermost
485 level within a function body. */
486
487 static struct binding_level *function_binding_level;
488
489 /* A PC value bigger than any PC value we may ever may encounter. */
490
491 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
492
493 /* Binding level structures are initialized by copying this one. */
494
495 static const struct binding_level clear_binding_level
496 = {
497 NULL_TREE, /* names */
498 NULL_TREE, /* shadowed */
499 NULL_TREE, /* blocks */
500 NULL_BINDING_LEVEL, /* level_chain */
501 LARGEST_PC, /* end_pc */
502 0, /* start_pc */
503 NULL, /* stmts */
504 NULL, /* exception_range */
505 0, /* binding_depth */
506 };
507
508 #if 0
509 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
510 that have names. Here so we can clear out their names' definitions
511 at the end of the function. */
512
513 static tree named_labels;
514
515 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
516
517 static tree shadowed_labels;
518 #endif
519
520 tree java_global_trees[JTI_MAX];
521
522 /* Build (and pushdecl) a "promoted type" for all standard
523 types shorter than int. */
524
525 static tree
526 push_promoted_type (const char *name, tree actual_type)
527 {
528 tree type = make_node (TREE_CODE (actual_type));
529 #if 1
530 tree in_min = TYPE_MIN_VALUE (int_type_node);
531 tree in_max = TYPE_MAX_VALUE (int_type_node);
532 #else
533 tree in_min = TYPE_MIN_VALUE (actual_type);
534 tree in_max = TYPE_MAX_VALUE (actual_type);
535 #endif
536 TYPE_MIN_VALUE (type) = copy_node (in_min);
537 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
538 TYPE_MAX_VALUE (type) = copy_node (in_max);
539 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
540 TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
541 TYPE_STRING_FLAG (type) = TYPE_STRING_FLAG (actual_type);
542 layout_type (type);
543 pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
544 return type;
545 }
546
547 /* Return a definition for a builtin function named NAME and whose data type
548 is TYPE. TYPE should be a function type with argument types.
549 FUNCTION_CODE tells later passes how to compile calls to this function.
550 See tree.h for its possible values.
551
552 If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
553 the name to be called if we can't opencode the function. If
554 ATTRS is nonzero, use that for the function's attribute list. */
555
556 tree
557 builtin_function (const char *name,
558 tree type,
559 int function_code,
560 enum built_in_class cl,
561 const char *library_name,
562 tree ARG_UNUSED (attrs))
563 {
564 tree decl = build_decl (FUNCTION_DECL, get_identifier (name), type);
565 DECL_EXTERNAL (decl) = 1;
566 TREE_PUBLIC (decl) = 1;
567 if (library_name)
568 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (library_name));
569 pushdecl (decl);
570 DECL_BUILT_IN_CLASS (decl) = cl;
571 DECL_FUNCTION_CODE (decl) = function_code;
572 return decl;
573 }
574
575 /* Return tree that represents a vtable for a primitive array. */
576 static tree
577 create_primitive_vtable (const char *name)
578 {
579 tree r;
580 char buf[50];
581
582 sprintf (buf, "_Jv_%sVTable", name);
583 r = build_decl (VAR_DECL, get_identifier (buf), ptr_type_node);
584 DECL_EXTERNAL (r) = 1;
585 return r;
586 }
587
588 static tree
589 do_nothing (tree t)
590 {
591 return t;
592 }
593
594 /* Parse the version string and compute the ABI version number. */
595 static void
596 parse_version (void)
597 {
598 const char *p = version_string;
599 unsigned int major = 0, minor = 0;
600 unsigned int abi_version;
601
602 /* Skip leading junk. */
603 while (*p && !ISDIGIT (*p))
604 ++p;
605 gcc_assert (*p);
606
607 /* Extract major version. */
608 while (ISDIGIT (*p))
609 {
610 major = major * 10 + *p - '0';
611 ++p;
612 }
613
614 gcc_assert (*p == '.' && ISDIGIT (p[1]));
615 ++p;
616
617 /* Extract minor version. */
618 while (ISDIGIT (*p))
619 {
620 minor = minor * 10 + *p - '0';
621 ++p;
622 }
623
624 if (flag_indirect_dispatch)
625 {
626 abi_version = GCJ_CURRENT_BC_ABI_VERSION;
627 abi_version |= FLAG_BINARYCOMPAT_ABI;
628 }
629 else /* C++ ABI */
630 {
631 /* Implicit in this computation is the idea that we won't break the
632 old-style binary ABI in a sub-minor release (e.g., from 4.0.0 to
633 4.0.1). */
634 abi_version = 100000 * major + 1000 * minor;
635 }
636 if (flag_bootstrap_classes)
637 abi_version |= FLAG_BOOTSTRAP_LOADER;
638
639 gcj_abi_version = build_int_cstu (ptr_type_node, abi_version);
640 }
641
642 void
643 java_init_decl_processing (void)
644 {
645 tree endlink;
646 tree field = NULL_TREE;
647 tree t;
648
649 init_class_processing ();
650
651 current_function_decl = NULL;
652 current_binding_level = NULL_BINDING_LEVEL;
653 free_binding_level = NULL_BINDING_LEVEL;
654 pushlevel (0); /* make the binding_level structure for global names */
655 global_binding_level = current_binding_level;
656
657 /* The code here must be similar to build_common_tree_nodes{,_2} in
658 tree.c, especially as to the order of initializing common nodes. */
659 error_mark_node = make_node (ERROR_MARK);
660 TREE_TYPE (error_mark_node) = error_mark_node;
661
662 /* Create sizetype first - needed for other types. */
663 initialize_sizetypes (false);
664
665 byte_type_node = make_signed_type (8);
666 pushdecl (build_decl (TYPE_DECL, get_identifier ("byte"), byte_type_node));
667 short_type_node = make_signed_type (16);
668 pushdecl (build_decl (TYPE_DECL, get_identifier ("short"), short_type_node));
669 int_type_node = make_signed_type (32);
670 pushdecl (build_decl (TYPE_DECL, get_identifier ("int"), int_type_node));
671 long_type_node = make_signed_type (64);
672 pushdecl (build_decl (TYPE_DECL, get_identifier ("long"), long_type_node));
673
674 unsigned_byte_type_node = make_unsigned_type (8);
675 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned byte"),
676 unsigned_byte_type_node));
677 unsigned_short_type_node = make_unsigned_type (16);
678 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned short"),
679 unsigned_short_type_node));
680 unsigned_int_type_node = make_unsigned_type (32);
681 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned int"),
682 unsigned_int_type_node));
683 unsigned_long_type_node = make_unsigned_type (64);
684 pushdecl (build_decl (TYPE_DECL, get_identifier ("unsigned long"),
685 unsigned_long_type_node));
686
687 /* This is not a java type, however tree-dfa requires a definition for
688 size_type_node. */
689 size_type_node = make_unsigned_type (POINTER_SIZE);
690 set_sizetype (size_type_node);
691
692 /* Define these next since types below may used them. */
693 integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
694 integer_zero_node = build_int_cst (NULL_TREE, 0);
695 integer_one_node = build_int_cst (NULL_TREE, 1);
696 integer_two_node = build_int_cst (NULL_TREE, 2);
697 integer_four_node = build_int_cst (NULL_TREE, 4);
698 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
699
700 /* A few values used for range checking in the lexer. */
701 decimal_int_max = build_int_cstu (unsigned_int_type_node, 0x80000000);
702 #if HOST_BITS_PER_WIDE_INT == 64
703 decimal_long_max = build_int_cstu (unsigned_long_type_node,
704 0x8000000000000000LL);
705 #elif HOST_BITS_PER_WIDE_INT == 32
706 decimal_long_max = build_int_cst_wide (unsigned_long_type_node,
707 0, 0x80000000);
708 #else
709 #error "unsupported size"
710 #endif
711
712 size_zero_node = size_int (0);
713 size_one_node = size_int (1);
714 bitsize_zero_node = bitsize_int (0);
715 bitsize_one_node = bitsize_int (1);
716 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
717
718 long_zero_node = build_int_cst (long_type_node, 0);
719
720 void_type_node = make_node (VOID_TYPE);
721 pushdecl (build_decl (TYPE_DECL, get_identifier ("void"), void_type_node));
722 layout_type (void_type_node); /* Uses size_zero_node */
723
724 ptr_type_node = build_pointer_type (void_type_node);
725 const_ptr_type_node
726 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
727
728 t = make_node (VOID_TYPE);
729 layout_type (t); /* Uses size_zero_node */
730 return_address_type_node = build_pointer_type (t);
731
732 null_pointer_node = build_int_cst (ptr_type_node, 0);
733
734 #if 0
735 /* Make a type to be the domain of a few array types
736 whose domains don't really matter.
737 200 is small enough that it always fits in size_t
738 and large enough that it can hold most function names for the
739 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
740 short_array_type_node = build_prim_array_type (short_type_node, 200);
741 #endif
742 char_type_node = make_node (INTEGER_TYPE);
743 TYPE_STRING_FLAG (char_type_node) = 1;
744 TYPE_PRECISION (char_type_node) = 16;
745 fixup_unsigned_type (char_type_node);
746 pushdecl (build_decl (TYPE_DECL, get_identifier ("char"), char_type_node));
747
748 boolean_type_node = make_node (BOOLEAN_TYPE);
749 TYPE_PRECISION (boolean_type_node) = 1;
750 fixup_unsigned_type (boolean_type_node);
751 pushdecl (build_decl (TYPE_DECL, get_identifier ("boolean"),
752 boolean_type_node));
753 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
754 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
755
756 promoted_byte_type_node
757 = push_promoted_type ("promoted_byte", byte_type_node);
758 promoted_short_type_node
759 = push_promoted_type ("promoted_short", short_type_node);
760 promoted_char_type_node
761 = push_promoted_type ("promoted_char", char_type_node);
762 promoted_boolean_type_node
763 = push_promoted_type ("promoted_boolean", boolean_type_node);
764
765 float_type_node = make_node (REAL_TYPE);
766 TYPE_PRECISION (float_type_node) = 32;
767 pushdecl (build_decl (TYPE_DECL, get_identifier ("float"),
768 float_type_node));
769 layout_type (float_type_node);
770
771 double_type_node = make_node (REAL_TYPE);
772 TYPE_PRECISION (double_type_node) = 64;
773 pushdecl (build_decl (TYPE_DECL, get_identifier ("double"),
774 double_type_node));
775 layout_type (double_type_node);
776
777 float_zero_node = build_real (float_type_node, dconst0);
778 double_zero_node = build_real (double_type_node, dconst0);
779
780 /* These are the vtables for arrays of primitives. */
781 boolean_array_vtable = create_primitive_vtable ("boolean");
782 byte_array_vtable = create_primitive_vtable ("byte");
783 char_array_vtable = create_primitive_vtable ("char");
784 short_array_vtable = create_primitive_vtable ("short");
785 int_array_vtable = create_primitive_vtable ("int");
786 long_array_vtable = create_primitive_vtable ("long");
787 float_array_vtable = create_primitive_vtable ("float");
788 double_array_vtable = create_primitive_vtable ("double");
789
790 one_elt_array_domain_type = build_index_type (integer_one_node);
791 utf8const_type = make_node (RECORD_TYPE);
792 PUSH_FIELD (utf8const_type, field, "hash", unsigned_short_type_node);
793 PUSH_FIELD (utf8const_type, field, "length", unsigned_short_type_node);
794 FINISH_RECORD (utf8const_type);
795 utf8const_ptr_type = build_pointer_type (utf8const_type);
796
797 atable_type = build_array_type (ptr_type_node,
798 one_elt_array_domain_type);
799 TYPE_NONALIASED_COMPONENT (atable_type) = 1;
800 atable_ptr_type = build_pointer_type (atable_type);
801
802 itable_type = build_array_type (ptr_type_node,
803 one_elt_array_domain_type);
804 TYPE_NONALIASED_COMPONENT (itable_type) = 1;
805 itable_ptr_type = build_pointer_type (itable_type);
806
807 symbol_type = make_node (RECORD_TYPE);
808 PUSH_FIELD (symbol_type, field, "clname", utf8const_ptr_type);
809 PUSH_FIELD (symbol_type, field, "name", utf8const_ptr_type);
810 PUSH_FIELD (symbol_type, field, "signature", utf8const_ptr_type);
811 FINISH_RECORD (symbol_type);
812
813 symbols_array_type = build_array_type (symbol_type,
814 one_elt_array_domain_type);
815 symbols_array_ptr_type = build_pointer_type (symbols_array_type);
816
817 assertion_entry_type = make_node (RECORD_TYPE);
818 PUSH_FIELD (assertion_entry_type, field, "assertion_code", integer_type_node);
819 PUSH_FIELD (assertion_entry_type, field, "op1", utf8const_ptr_type);
820 PUSH_FIELD (assertion_entry_type, field, "op2", utf8const_ptr_type);
821 FINISH_RECORD (assertion_entry_type);
822
823 assertion_table_type = build_array_type (assertion_entry_type,
824 one_elt_array_domain_type);
825
826 /* As you're adding items here, please update the code right after
827 this section, so that the filename containing the source code of
828 the pre-defined class gets registered correctly. */
829 unqualified_object_id_node = get_identifier ("Object");
830 object_type_node = lookup_class (get_identifier ("java.lang.Object"));
831 object_ptr_type_node = promote_type (object_type_node);
832 string_type_node = lookup_class (get_identifier ("java.lang.String"));
833 string_ptr_type_node = promote_type (string_type_node);
834 class_type_node = lookup_class (get_identifier ("java.lang.Class"));
835 throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
836 exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
837 runtime_exception_type_node =
838 lookup_class (get_identifier ("java.lang.RuntimeException"));
839 error_exception_type_node =
840 lookup_class (get_identifier ("java.lang.Error"));
841
842 rawdata_ptr_type_node
843 = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
844
845 add_predefined_file (get_identifier ("java/lang/Class.java"));
846 add_predefined_file (get_identifier ("java/lang/Error.java"));
847 add_predefined_file (get_identifier ("java/lang/Object.java"));
848 add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
849 add_predefined_file (get_identifier ("java/lang/String.java"));
850 add_predefined_file (get_identifier ("java/lang/Throwable.java"));
851 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
852 add_predefined_file (get_identifier ("java/lang/Exception.java"));
853 add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
854 add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
855
856 methodtable_type = make_node (RECORD_TYPE);
857 layout_type (methodtable_type);
858 build_decl (TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
859 methodtable_ptr_type = build_pointer_type (methodtable_type);
860
861 TYPE_identifier_node = get_identifier ("TYPE");
862 init_identifier_node = get_identifier ("<init>");
863 clinit_identifier_node = get_identifier ("<clinit>");
864 finit_identifier_node = get_identifier ("finit$");
865 instinit_identifier_node = get_identifier ("instinit$");
866 void_signature_node = get_identifier ("()V");
867 length_identifier_node = get_identifier ("length");
868 finalize_identifier_node = get_identifier ("finalize");
869 this_identifier_node = get_identifier ("this");
870 super_identifier_node = get_identifier ("super");
871 continue_identifier_node = get_identifier ("continue");
872 access0_identifier_node = get_identifier ("access$0");
873 classdollar_identifier_node = get_identifier ("class$");
874
875 java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
876 java_io_serializable_identifier_node =
877 get_identifier ("java.io.Serializable");
878
879 /* for lack of a better place to put this stub call */
880 init_expr_processing();
881
882 constants_type_node = make_node (RECORD_TYPE);
883 PUSH_FIELD (constants_type_node, field, "size", unsigned_int_type_node);
884 PUSH_FIELD (constants_type_node, field, "tags", ptr_type_node);
885 PUSH_FIELD (constants_type_node, field, "data", ptr_type_node);
886 FINISH_RECORD (constants_type_node);
887 build_decl (TYPE_DECL, get_identifier ("constants"), constants_type_node);
888
889 access_flags_type_node = unsigned_short_type_node;
890
891 dtable_type = make_node (RECORD_TYPE);
892 dtable_ptr_type = build_pointer_type (dtable_type);
893
894 otable_type = build_array_type (integer_type_node,
895 one_elt_array_domain_type);
896 TYPE_NONALIASED_COMPONENT (otable_type) = 1;
897 otable_ptr_type = build_pointer_type (otable_type);
898
899 PUSH_FIELD (object_type_node, field, "vtable", dtable_ptr_type);
900 DECL_FCONTEXT (field) = object_type_node;
901 TYPE_VFIELD (object_type_node) = field;
902
903 /* This isn't exactly true, but it is what we have in the source.
904 There is an unresolved issue here, which is whether the vtable
905 should be marked by the GC. */
906 if (! flag_hash_synchronization)
907 PUSH_FIELD (object_type_node, field, "sync_info",
908 build_pointer_type (object_type_node));
909 for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
910 FIELD_PRIVATE (t) = 1;
911 FINISH_RECORD (object_type_node);
912
913 field_type_node = make_node (RECORD_TYPE);
914 field_ptr_type_node = build_pointer_type (field_type_node);
915 method_type_node = make_node (RECORD_TYPE);
916 method_ptr_type_node = build_pointer_type (method_type_node);
917
918 set_super_info (0, class_type_node, object_type_node, 0);
919 set_super_info (0, string_type_node, object_type_node, 0);
920 class_ptr_type = build_pointer_type (class_type_node);
921
922 PUSH_FIELD (class_type_node, field, "next_or_version", class_ptr_type);
923 PUSH_FIELD (class_type_node, field, "name", utf8const_ptr_type);
924 PUSH_FIELD (class_type_node, field, "accflags", access_flags_type_node);
925 PUSH_FIELD (class_type_node, field, "superclass", class_ptr_type);
926 PUSH_FIELD (class_type_node, field, "constants", constants_type_node);
927 PUSH_FIELD (class_type_node, field, "methods", method_ptr_type_node);
928 PUSH_FIELD (class_type_node, field, "method_count", short_type_node);
929 PUSH_FIELD (class_type_node, field, "vtable_method_count", short_type_node);
930 PUSH_FIELD (class_type_node, field, "fields", field_ptr_type_node);
931 PUSH_FIELD (class_type_node, field, "size_in_bytes", int_type_node);
932 PUSH_FIELD (class_type_node, field, "field_count", short_type_node);
933 PUSH_FIELD (class_type_node, field, "static_field_count", short_type_node);
934 PUSH_FIELD (class_type_node, field, "vtable", dtable_ptr_type);
935 PUSH_FIELD (class_type_node, field, "otable", otable_ptr_type);
936 PUSH_FIELD (class_type_node, field, "otable_syms",
937 symbols_array_ptr_type);
938 PUSH_FIELD (class_type_node, field, "atable", atable_ptr_type);
939 PUSH_FIELD (class_type_node, field, "atable_syms",
940 symbols_array_ptr_type);
941 PUSH_FIELD (class_type_node, field, "itable", itable_ptr_type);
942 PUSH_FIELD (class_type_node, field, "itable_syms",
943 symbols_array_ptr_type);
944 PUSH_FIELD (class_type_node, field, "catch_classes", ptr_type_node);
945 PUSH_FIELD (class_type_node, field, "interfaces",
946 build_pointer_type (class_ptr_type));
947 PUSH_FIELD (class_type_node, field, "loader", ptr_type_node);
948 PUSH_FIELD (class_type_node, field, "interface_count", short_type_node);
949 PUSH_FIELD (class_type_node, field, "state", byte_type_node);
950 PUSH_FIELD (class_type_node, field, "thread", ptr_type_node);
951 PUSH_FIELD (class_type_node, field, "depth", short_type_node);
952 PUSH_FIELD (class_type_node, field, "ancestors", ptr_type_node);
953 PUSH_FIELD (class_type_node, field, "idt", ptr_type_node);
954 PUSH_FIELD (class_type_node, field, "arrayclass", ptr_type_node);
955 PUSH_FIELD (class_type_node, field, "protectionDomain", ptr_type_node);
956 PUSH_FIELD (class_type_node, field, "assertion_table", ptr_type_node);
957 PUSH_FIELD (class_type_node, field, "hack_signers", ptr_type_node);
958 PUSH_FIELD (class_type_node, field, "chain", ptr_type_node);
959 PUSH_FIELD (class_type_node, field, "aux_info", ptr_type_node);
960 PUSH_FIELD (class_type_node, field, "engine", ptr_type_node);
961 for (t = TYPE_FIELDS (class_type_node); t != NULL_TREE; t = TREE_CHAIN (t))
962 FIELD_PRIVATE (t) = 1;
963 push_super_field (class_type_node, object_type_node);
964
965 FINISH_RECORD (class_type_node);
966 build_decl (TYPE_DECL, get_identifier ("Class"), class_type_node);
967
968 field_info_union_node = make_node (UNION_TYPE);
969 PUSH_FIELD (field_info_union_node, field, "boffset", int_type_node);
970 PUSH_FIELD (field_info_union_node, field, "addr", ptr_type_node);
971 #if 0
972 PUSH_FIELD (field_info_union_node, field, "idx", unsigned_short_type_node);
973 #endif
974 layout_type (field_info_union_node);
975
976 PUSH_FIELD (field_type_node, field, "name", utf8const_ptr_type);
977 PUSH_FIELD (field_type_node, field, "type", class_ptr_type);
978 PUSH_FIELD (field_type_node, field, "accflags", access_flags_type_node);
979 PUSH_FIELD (field_type_node, field, "bsize", unsigned_short_type_node);
980 PUSH_FIELD (field_type_node, field, "info", field_info_union_node);
981 FINISH_RECORD (field_type_node);
982 build_decl (TYPE_DECL, get_identifier ("Field"), field_type_node);
983
984 nativecode_ptr_array_type_node
985 = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
986
987 PUSH_FIELD (dtable_type, field, "class", class_ptr_type);
988 PUSH_FIELD (dtable_type, field, "methods", nativecode_ptr_array_type_node);
989 FINISH_RECORD (dtable_type);
990 build_decl (TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
991
992 jexception_type = make_node (RECORD_TYPE);
993 PUSH_FIELD (jexception_type, field, "start_pc", ptr_type_node);
994 PUSH_FIELD (jexception_type, field, "end_pc", ptr_type_node);
995 PUSH_FIELD (jexception_type, field, "handler_pc", ptr_type_node);
996 PUSH_FIELD (jexception_type, field, "catch_type", class_ptr_type);
997 FINISH_RECORD (jexception_type);
998 build_decl (TYPE_DECL, get_identifier ("jexception"), field_type_node);
999 jexception_ptr_type = build_pointer_type (jexception_type);
1000
1001 lineNumberEntry_type = make_node (RECORD_TYPE);
1002 PUSH_FIELD (lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
1003 PUSH_FIELD (lineNumberEntry_type, field, "start_pc", ptr_type_node);
1004 FINISH_RECORD (lineNumberEntry_type);
1005
1006 lineNumbers_type = make_node (RECORD_TYPE);
1007 PUSH_FIELD (lineNumbers_type, field, "length", unsigned_int_type_node);
1008 FINISH_RECORD (lineNumbers_type);
1009
1010 PUSH_FIELD (method_type_node, field, "name", utf8const_ptr_type);
1011 PUSH_FIELD (method_type_node, field, "signature", utf8const_ptr_type);
1012 PUSH_FIELD (method_type_node, field, "accflags", access_flags_type_node);
1013 PUSH_FIELD (method_type_node, field, "index", unsigned_short_type_node);
1014 PUSH_FIELD (method_type_node, field, "ncode", nativecode_ptr_type_node);
1015 PUSH_FIELD (method_type_node, field, "throws", ptr_type_node);
1016 FINISH_RECORD (method_type_node);
1017 build_decl (TYPE_DECL, get_identifier ("Method"), method_type_node);
1018
1019 endlink = end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
1020
1021 t = tree_cons (NULL_TREE, class_ptr_type, endlink);
1022 alloc_object_node = builtin_function ("_Jv_AllocObject",
1023 build_function_type (ptr_type_node, t),
1024 0, NOT_BUILT_IN, NULL, NULL_TREE);
1025 DECL_IS_MALLOC (alloc_object_node) = 1;
1026 alloc_no_finalizer_node =
1027 builtin_function ("_Jv_AllocObjectNoFinalizer",
1028 build_function_type (ptr_type_node, t),
1029 0, NOT_BUILT_IN, NULL, NULL_TREE);
1030 DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
1031
1032 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1033 soft_initclass_node = builtin_function ("_Jv_InitClass",
1034 build_function_type (void_type_node,
1035 t),
1036 0, NOT_BUILT_IN, NULL, NULL_TREE);
1037 t = tree_cons (NULL_TREE, class_ptr_type,
1038 tree_cons (NULL_TREE, int_type_node, endlink));
1039 soft_resolvepoolentry_node
1040 = builtin_function ("_Jv_ResolvePoolEntry",
1041 build_function_type (ptr_type_node, t),
1042 0,NOT_BUILT_IN, NULL, NULL_TREE);
1043 DECL_IS_PURE (soft_resolvepoolentry_node) = 1;
1044 throw_node = builtin_function ("_Jv_Throw",
1045 build_function_type (void_type_node, t),
1046 0, NOT_BUILT_IN, NULL, NULL_TREE);
1047 /* Mark throw_nodes as `noreturn' functions with side effects. */
1048 TREE_THIS_VOLATILE (throw_node) = 1;
1049 TREE_SIDE_EFFECTS (throw_node) = 1;
1050
1051 t = build_function_type (void_type_node, tree_cons (NULL_TREE, ptr_type_node,
1052 endlink));
1053 soft_monitorenter_node
1054 = builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
1055 NULL, NULL_TREE);
1056 soft_monitorexit_node
1057 = builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
1058 NULL, NULL_TREE);
1059
1060 t = tree_cons (NULL_TREE, ptr_type_node,
1061 tree_cons (NULL_TREE, int_type_node, endlink));
1062 soft_newarray_node
1063 = builtin_function ("_Jv_NewPrimArray",
1064 build_function_type (ptr_type_node, t),
1065 0, NOT_BUILT_IN, NULL, NULL_TREE);
1066 DECL_IS_MALLOC (soft_newarray_node) = 1;
1067
1068 t = tree_cons (NULL_TREE, int_type_node,
1069 tree_cons (NULL_TREE, class_ptr_type,
1070 tree_cons (NULL_TREE, object_ptr_type_node,
1071 endlink)));
1072 soft_anewarray_node
1073 = builtin_function ("_Jv_NewObjectArray",
1074 build_function_type (ptr_type_node, t),
1075 0, NOT_BUILT_IN, NULL, NULL_TREE);
1076 DECL_IS_MALLOC (soft_anewarray_node) = 1;
1077
1078 /* There is no endlink here because _Jv_NewMultiArray is a varargs
1079 function. */
1080 t = tree_cons (NULL_TREE, ptr_type_node,
1081 tree_cons (NULL_TREE, int_type_node, NULL_TREE));
1082 soft_multianewarray_node
1083 = builtin_function ("_Jv_NewMultiArray",
1084 build_function_type (ptr_type_node, t),
1085 0, NOT_BUILT_IN, NULL, NULL_TREE);
1086 DECL_IS_MALLOC (soft_multianewarray_node) = 1;
1087
1088 t = build_function_type (void_type_node,
1089 tree_cons (NULL_TREE, int_type_node, endlink));
1090 soft_badarrayindex_node
1091 = builtin_function ("_Jv_ThrowBadArrayIndex", t,
1092 0, NOT_BUILT_IN, NULL, NULL_TREE);
1093 /* Mark soft_badarrayindex_node as a `noreturn' function with side
1094 effects. */
1095 TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
1096 TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
1097
1098 soft_nullpointer_node
1099 = builtin_function ("_Jv_ThrowNullPointerException",
1100 build_function_type (void_type_node, endlink),
1101 0, NOT_BUILT_IN, NULL, NULL_TREE);
1102 /* Mark soft_nullpointer_node as a `noreturn' function with side
1103 effects. */
1104 TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
1105 TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
1106
1107 soft_abstractmethod_node
1108 = builtin_function ("_Jv_ThrowAbstractMethodError",
1109 build_function_type (void_type_node, endlink),
1110 0, NOT_BUILT_IN, NULL, NULL_TREE);
1111 /* Mark soft_abstractmethod_node as a `noreturn' function with side
1112 effects. */
1113 TREE_THIS_VOLATILE (soft_abstractmethod_node) = 1;
1114 TREE_SIDE_EFFECTS (soft_abstractmethod_node) = 1;
1115
1116 soft_nosuchfield_node
1117 = builtin_function ("_Jv_ThrowNoSuchFieldError",
1118 build_function_type (void_type_node, endlink),
1119 0, NOT_BUILT_IN, NULL, NULL_TREE);
1120 /* Mark soft_nosuchfield_node as a `noreturn' function with side
1121 effects. */
1122 TREE_THIS_VOLATILE (soft_nosuchfield_node) = 1;
1123 TREE_SIDE_EFFECTS (soft_nosuchfield_node) = 1;
1124
1125 t = tree_cons (NULL_TREE, class_ptr_type,
1126 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1127 soft_checkcast_node
1128 = builtin_function ("_Jv_CheckCast",
1129 build_function_type (ptr_type_node, t),
1130 0, NOT_BUILT_IN, NULL, NULL_TREE);
1131 t = tree_cons (NULL_TREE, object_ptr_type_node,
1132 tree_cons (NULL_TREE, class_ptr_type, endlink));
1133 soft_instanceof_node
1134 = builtin_function ("_Jv_IsInstanceOf",
1135 build_function_type (boolean_type_node, t),
1136 0, NOT_BUILT_IN, NULL, NULL_TREE);
1137 DECL_IS_PURE (soft_instanceof_node) = 1;
1138 t = tree_cons (NULL_TREE, object_ptr_type_node,
1139 tree_cons (NULL_TREE, object_ptr_type_node, endlink));
1140 soft_checkarraystore_node
1141 = builtin_function ("_Jv_CheckArrayStore",
1142 build_function_type (void_type_node, t),
1143 0, NOT_BUILT_IN, NULL, NULL_TREE);
1144 t = tree_cons (NULL_TREE, ptr_type_node,
1145 tree_cons (NULL_TREE, ptr_type_node,
1146 tree_cons (NULL_TREE, int_type_node, endlink)));
1147 soft_lookupinterfacemethod_node
1148 = builtin_function ("_Jv_LookupInterfaceMethodIdx",
1149 build_function_type (ptr_type_node, t),
1150 0, NOT_BUILT_IN, NULL, NULL_TREE);
1151 DECL_IS_PURE (soft_lookupinterfacemethod_node) = 1;
1152 t = tree_cons (NULL_TREE, ptr_type_node,
1153 tree_cons (NULL_TREE, ptr_type_node,
1154 tree_cons (NULL_TREE, ptr_type_node, endlink)));
1155 soft_lookupinterfacemethodbyname_node
1156 = builtin_function ("_Jv_LookupInterfaceMethod",
1157 build_function_type (ptr_type_node, t),
1158 0, NOT_BUILT_IN, NULL, NULL_TREE);
1159 t = tree_cons (NULL_TREE, object_ptr_type_node,
1160 tree_cons (NULL_TREE, ptr_type_node,
1161 tree_cons (NULL_TREE, ptr_type_node,
1162 tree_cons (NULL_TREE, int_type_node,
1163 endlink))));
1164 soft_lookupjnimethod_node
1165 = builtin_function ("_Jv_LookupJNIMethod",
1166 build_function_type (ptr_type_node, t),
1167 0, NOT_BUILT_IN, NULL, NULL_TREE);
1168 t = tree_cons (NULL_TREE, ptr_type_node, endlink);
1169 soft_getjnienvnewframe_node
1170 = builtin_function ("_Jv_GetJNIEnvNewFrame",
1171 build_function_type (ptr_type_node, t),
1172 0, NOT_BUILT_IN, NULL, NULL_TREE);
1173 soft_jnipopsystemframe_node
1174 = builtin_function ("_Jv_JNI_PopSystemFrame",
1175 build_function_type (void_type_node, t),
1176 0, NOT_BUILT_IN, NULL, NULL_TREE);
1177
1178 t = tree_cons (NULL_TREE, object_ptr_type_node, endlink);
1179 soft_unwrapjni_node
1180 = builtin_function ("_Jv_UnwrapJNIweakReference",
1181 build_function_type (object_ptr_type_node, t),
1182 0, NOT_BUILT_IN, NULL, NULL_TREE);
1183
1184 t = tree_cons (NULL_TREE, int_type_node,
1185 tree_cons (NULL_TREE, int_type_node, endlink));
1186 soft_idiv_node
1187 = builtin_function ("_Jv_divI",
1188 build_function_type (int_type_node, t),
1189 0, NOT_BUILT_IN, NULL, NULL_TREE);
1190
1191 soft_irem_node
1192 = builtin_function ("_Jv_remI",
1193 build_function_type (int_type_node, t),
1194 0, NOT_BUILT_IN, NULL, NULL_TREE);
1195
1196 t = tree_cons (NULL_TREE, long_type_node,
1197 tree_cons (NULL_TREE, long_type_node, endlink));
1198 soft_ldiv_node
1199 = builtin_function ("_Jv_divJ",
1200 build_function_type (long_type_node, t),
1201 0, NOT_BUILT_IN, NULL, NULL_TREE);
1202
1203 soft_lrem_node
1204 = builtin_function ("_Jv_remJ",
1205 build_function_type (long_type_node, t),
1206 0, NOT_BUILT_IN, NULL, NULL_TREE);
1207
1208 /* Initialize variables for except.c. */
1209 eh_personality_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
1210 ? "__gcj_personality_sj0"
1211 : "__gcj_personality_v0");
1212 default_init_unwind_resume_libfunc ();
1213
1214 lang_eh_runtime_type = do_nothing;
1215
1216 init_jcf_parse ();
1217
1218 initialize_builtins ();
1219 soft_fmod_node = built_in_decls[BUILT_IN_FMOD];
1220 #if 0
1221 soft_fmodf_node = built_in_decls[BUILT_IN_FMODF];
1222 #endif
1223
1224 parse_version ();
1225 }
1226
1227
1228 /* Look up NAME in the current binding level and its superiors
1229 in the namespace of variables, functions and typedefs.
1230 Return a ..._DECL node of some kind representing its definition,
1231 or return 0 if it is undefined. */
1232
1233 tree
1234 lookup_name (tree name)
1235 {
1236 tree val;
1237 if (current_binding_level != global_binding_level
1238 && IDENTIFIER_LOCAL_VALUE (name))
1239 val = IDENTIFIER_LOCAL_VALUE (name);
1240 else
1241 val = IDENTIFIER_GLOBAL_VALUE (name);
1242 return val;
1243 }
1244
1245 /* Similar to `lookup_name' but look only at current binding level and
1246 the previous one if it's the parameter level. */
1247
1248 static tree
1249 lookup_name_current_level (tree name)
1250 {
1251 tree t;
1252
1253 if (current_binding_level == global_binding_level)
1254 return IDENTIFIER_GLOBAL_VALUE (name);
1255
1256 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
1257 return 0;
1258
1259 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
1260 if (DECL_NAME (t) == name)
1261 break;
1262
1263 return t;
1264 }
1265
1266 /* Use a binding level to record a labeled block declaration */
1267
1268 void
1269 push_labeled_block (tree lb)
1270 {
1271 tree name = DECL_NAME (LABELED_BLOCK_LABEL (lb));
1272 struct binding_level *b = current_binding_level;
1273 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1274 if (oldlocal != 0)
1275 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1276 TREE_CHAIN (lb) = b->names;
1277 b->names = lb;
1278 IDENTIFIER_LOCAL_VALUE (name) = lb;
1279 }
1280
1281 /* Pop the current binding level, reinstalling values for the previous
1282 labeled block */
1283
1284 void
1285 pop_labeled_block (void)
1286 {
1287 struct binding_level *b = current_binding_level;
1288 tree label = b->names;
1289 IDENTIFIER_LOCAL_VALUE (DECL_NAME (LABELED_BLOCK_LABEL (label))) =
1290 NULL_TREE;
1291 if (b->shadowed)
1292 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (b->shadowed)) =
1293 TREE_VALUE (b->shadowed);
1294
1295 /* Pop the current level, and free the structure for reuse. */
1296 current_binding_level = current_binding_level->level_chain;
1297 b->level_chain = free_binding_level;
1298 free_binding_level = b;
1299 }
1300
1301 /* Record a decl-node X as belonging to the current lexical scope.
1302 Check for errors (such as an incompatible declaration for the same
1303 name already seen in the same scope).
1304
1305 Returns either X or an old decl for the same name.
1306 If an old decl is returned, it may have been smashed
1307 to agree with what X says. */
1308
1309 tree
1310 pushdecl (tree x)
1311 {
1312 tree t;
1313 tree name = DECL_NAME (x);
1314 struct binding_level *b = current_binding_level;
1315
1316 if (TREE_CODE (x) != TYPE_DECL)
1317 DECL_CONTEXT (x) = current_function_decl;
1318 if (name)
1319 {
1320 t = lookup_name_current_level (name);
1321 if (t != 0 && t == error_mark_node)
1322 /* error_mark_node is 0 for a while during initialization! */
1323 {
1324 t = 0;
1325 error ("%q+D used prior to declaration", x);
1326 }
1327
1328 /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1329 to point to the TYPE_DECL.
1330 Since Java does not have typedefs, a type can only have
1331 one (true) name, given by a class, interface, or builtin. */
1332 if (TREE_CODE (x) == TYPE_DECL
1333 && TYPE_NAME (TREE_TYPE (x)) == 0
1334 && TREE_TYPE (x) != error_mark_node)
1335 {
1336 TYPE_NAME (TREE_TYPE (x)) = x;
1337 TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1338 }
1339
1340 /* This name is new in its binding level.
1341 Install the new declaration and return it. */
1342 if (b == global_binding_level)
1343 {
1344 /* Install a global value. */
1345
1346 IDENTIFIER_GLOBAL_VALUE (name) = x;
1347 }
1348 else
1349 {
1350 /* Here to install a non-global value. */
1351 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1352 IDENTIFIER_LOCAL_VALUE (name) = x;
1353
1354 #if 0
1355 /* Warn if shadowing an argument at the top level of the body. */
1356 if (oldlocal != 0 && !DECL_EXTERNAL (x)
1357 /* This warning doesn't apply to the parms of a nested fcn. */
1358 && ! current_binding_level->parm_flag
1359 /* Check that this is one level down from the parms. */
1360 && current_binding_level->level_chain->parm_flag
1361 /* Check that the decl being shadowed
1362 comes from the parm level, one level up. */
1363 && chain_member (oldlocal, current_binding_level->level_chain->names))
1364 {
1365 if (TREE_CODE (oldlocal) == PARM_DECL)
1366 pedwarn ("declaration of %qs shadows a parameter",
1367 IDENTIFIER_POINTER (name));
1368 else
1369 pedwarn ("declaration of %qs shadows a symbol from the parameter list",
1370 IDENTIFIER_POINTER (name));
1371 }
1372
1373 /* Maybe warn if shadowing something else. */
1374 else if (warn_shadow && !DECL_EXTERNAL (x)
1375 /* No shadow warnings for internally generated vars. */
1376 && DECL_SOURCE_LINE (x) != 0
1377 /* No shadow warnings for vars made for inlining. */
1378 && ! DECL_FROM_INLINE (x))
1379 {
1380 const char *warnstring = 0;
1381
1382 if (TREE_CODE (x) == PARM_DECL
1383 && current_binding_level->level_chain->parm_flag)
1384 /* Don't warn about the parm names in function declarator
1385 within a function declarator.
1386 It would be nice to avoid warning in any function
1387 declarator in a declaration, as opposed to a definition,
1388 but there is no way to tell it's not a definition. */
1389 ;
1390 else if (oldlocal != 0 && TREE_CODE (oldlocal) == PARM_DECL)
1391 warnstring = "declaration of %qs shadows a parameter";
1392 else if (oldlocal != 0)
1393 warnstring = "declaration of %qs shadows previous local";
1394 else if (IDENTIFIER_GLOBAL_VALUE (name) != 0
1395 && IDENTIFIER_GLOBAL_VALUE (name) != error_mark_node)
1396 warnstring = "declaration of %qs shadows global declaration";
1397
1398 if (warnstring)
1399 warning (0, warnstring, IDENTIFIER_POINTER (name));
1400 }
1401 #endif
1402
1403 /* If storing a local value, there may already be one (inherited).
1404 If so, record it for restoration when this binding level ends. */
1405 if (oldlocal != 0)
1406 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1407 }
1408 }
1409
1410 /* Put decls on list in reverse order.
1411 We will reverse them later if necessary. */
1412 TREE_CHAIN (x) = b->names;
1413 b->names = x;
1414
1415 return x;
1416 }
1417
1418 void
1419 pushdecl_force_head (tree x)
1420 {
1421 current_binding_level->names = x;
1422 }
1423
1424 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1425
1426 tree
1427 pushdecl_top_level (tree x)
1428 {
1429 tree t;
1430 struct binding_level *b = current_binding_level;
1431
1432 current_binding_level = global_binding_level;
1433 t = pushdecl (x);
1434 current_binding_level = b;
1435 return t;
1436 }
1437
1438 /* Like pushdecl, only it places X in FUNCTION_BINDING_LEVEL, if appropriate. */
1439
1440 tree
1441 pushdecl_function_level (tree x)
1442 {
1443 tree t;
1444 struct binding_level *b = current_binding_level;
1445
1446 current_binding_level = function_binding_level;
1447 t = pushdecl (x);
1448 current_binding_level = b;
1449 return t;
1450 }
1451
1452 /* Nonzero if we are currently in the global binding level. */
1453
1454 int
1455 global_bindings_p (void)
1456 {
1457 return current_binding_level == global_binding_level;
1458 }
1459
1460 /* Return the list of declarations of the current level.
1461 Note that this list is in reverse order unless/until
1462 you nreverse it; and when you do nreverse it, you must
1463 store the result back using `storedecls' or you will lose. */
1464
1465 tree
1466 getdecls (void)
1467 {
1468 return current_binding_level->names;
1469 }
1470
1471 /* Create a new `struct binding_level'. */
1472
1473 static struct binding_level *
1474 make_binding_level (void)
1475 {
1476 /* NOSTRICT */
1477 return ggc_alloc_cleared (sizeof (struct binding_level));
1478 }
1479
1480 void
1481 pushlevel (int unused ATTRIBUTE_UNUSED)
1482 {
1483 struct binding_level *newlevel = NULL_BINDING_LEVEL;
1484
1485 #if 0
1486 /* If this is the top level of a function,
1487 just make sure that NAMED_LABELS is 0. */
1488
1489 if (current_binding_level == global_binding_level)
1490 named_labels = 0;
1491 #endif
1492
1493 /* Reuse or create a struct for this binding level. */
1494
1495 if (free_binding_level)
1496 {
1497 newlevel = free_binding_level;
1498 free_binding_level = free_binding_level->level_chain;
1499 }
1500 else
1501 {
1502 newlevel = make_binding_level ();
1503 }
1504
1505 /* Add this level to the front of the chain (stack) of levels that
1506 are active. */
1507
1508 *newlevel = clear_binding_level;
1509 newlevel->level_chain = current_binding_level;
1510 current_binding_level = newlevel;
1511 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1512 newlevel->binding_depth = binding_depth;
1513 indent ();
1514 fprintf (stderr, "push %s level %p pc %d\n",
1515 (is_class_level) ? "class" : "block", newlevel, current_pc);
1516 is_class_level = 0;
1517 binding_depth++;
1518 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1519 }
1520
1521 /* Exit a binding level.
1522 Pop the level off, and restore the state of the identifier-decl mappings
1523 that were in effect when this level was entered.
1524
1525 If KEEP is nonzero, this level had explicit declarations, so
1526 and create a "block" (a BLOCK node) for the level
1527 to record its declarations and subblocks for symbol table output.
1528
1529 If FUNCTIONBODY is nonzero, this level is the body of a function,
1530 so create a block as if KEEP were set and also clear out all
1531 label names.
1532
1533 If REVERSE is nonzero, reverse the order of decls before putting
1534 them into the BLOCK. */
1535
1536 tree
1537 poplevel (int keep, int reverse, int functionbody)
1538 {
1539 tree link;
1540 /* The chain of decls was accumulated in reverse order.
1541 Put it into forward order, just for cleanliness. */
1542 tree decls;
1543 tree subblocks = current_binding_level->blocks;
1544 tree block = 0;
1545 tree decl;
1546 tree bind = 0;
1547
1548 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1549 binding_depth--;
1550 indent ();
1551 if (current_binding_level->end_pc != LARGEST_PC)
1552 fprintf (stderr, "pop %s level %p pc %d (end pc %d)\n",
1553 (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1554 current_binding_level->end_pc);
1555 else
1556 fprintf (stderr, "pop %s level %p pc %d\n",
1557 (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1558 #if 0
1559 if (is_class_level != (current_binding_level == class_binding_level))
1560 {
1561 indent ();
1562 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
1563 }
1564 is_class_level = 0;
1565 #endif
1566 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1567
1568 /* Get the decls in the order they were written.
1569 Usually current_binding_level->names is in reverse order.
1570 But parameter decls were previously put in forward order. */
1571
1572 if (reverse)
1573 current_binding_level->names
1574 = decls = nreverse (current_binding_level->names);
1575 else
1576 decls = current_binding_level->names;
1577
1578 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1579 if (TREE_CODE (decl) == VAR_DECL
1580 && DECL_LANG_SPECIFIC (decl) != NULL
1581 && DECL_LOCAL_SLOT_NUMBER (decl))
1582 LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
1583
1584 /* If there were any declarations in that level,
1585 or if this level is a function body,
1586 create a BLOCK to record them for the life of this function. */
1587
1588 block = 0;
1589 if (keep || functionbody)
1590 {
1591 block = make_node (BLOCK);
1592 TREE_TYPE (block) = void_type_node;
1593 }
1594
1595 if (current_binding_level->exception_range)
1596 expand_end_java_handler (current_binding_level->exception_range);
1597
1598 if (block != 0)
1599 {
1600 /* If any statements have been generated at this level, create a
1601 BIND_EXPR to hold them and copy the variables to it. This
1602 only applies to the bytecode compiler. */
1603 if (current_binding_level->stmts)
1604 {
1605 tree decl = decls;
1606 tree *var = &BLOCK_VARS (block);
1607
1608 /* Copy decls from names list, ignoring labels. */
1609 while (decl)
1610 {
1611 tree next = TREE_CHAIN (decl);
1612 if (TREE_CODE (decl) != LABEL_DECL)
1613 {
1614 *var = decl;
1615 var = &TREE_CHAIN (decl);
1616 }
1617 decl = next;
1618 }
1619 *var = NULL;
1620
1621 bind = build3 (BIND_EXPR, TREE_TYPE (block), BLOCK_VARS (block),
1622 BLOCK_EXPR_BODY (block), block);
1623 BIND_EXPR_BODY (bind) = current_binding_level->stmts;
1624
1625 if (BIND_EXPR_BODY (bind)
1626 && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
1627 TREE_SIDE_EFFECTS (bind) = 1;
1628
1629 /* FIXME: gimplifier brain damage. */
1630 if (BIND_EXPR_BODY (bind) == NULL)
1631 BIND_EXPR_BODY (bind) = build_java_empty_stmt ();
1632
1633 current_binding_level->stmts = NULL;
1634 }
1635 else
1636 {
1637 BLOCK_VARS (block) = decls;
1638 }
1639 BLOCK_SUBBLOCKS (block) = subblocks;
1640 }
1641
1642 /* In each subblock, record that this is its superior. */
1643
1644 for (link = subblocks; link; link = TREE_CHAIN (link))
1645 BLOCK_SUPERCONTEXT (link) = block;
1646
1647 /* Clear out the meanings of the local variables of this level. */
1648
1649 for (link = decls; link; link = TREE_CHAIN (link))
1650 {
1651 tree name = DECL_NAME (link);
1652 if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1653 {
1654 /* If the ident. was used or addressed via a local extern decl,
1655 don't forget that fact. */
1656 if (DECL_EXTERNAL (link))
1657 {
1658 if (TREE_USED (link))
1659 TREE_USED (name) = 1;
1660 if (TREE_ADDRESSABLE (link))
1661 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1662 }
1663 IDENTIFIER_LOCAL_VALUE (name) = 0;
1664 }
1665 }
1666
1667 /* Restore all name-meanings of the outer levels
1668 that were shadowed by this level. */
1669
1670 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1671 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1672
1673 /* If the level being exited is the top level of a function,
1674 check over all the labels, and clear out the current
1675 (function local) meanings of their names. */
1676
1677 if (functionbody)
1678 {
1679 /* If this is the top level block of a function,
1680 the vars are the function's parameters.
1681 Don't leave them in the BLOCK because they are
1682 found in the FUNCTION_DECL instead. */
1683
1684 BLOCK_VARS (block) = 0;
1685
1686 /* Clear out the definitions of all label names,
1687 since their scopes end here,
1688 and add them to BLOCK_VARS. */
1689
1690 #if 0
1691 for (link = named_labels; link; link = TREE_CHAIN (link))
1692 {
1693 tree label = TREE_VALUE (link);
1694
1695 if (DECL_INITIAL (label) == 0)
1696 {
1697 error ("label %q+D used but not defined", label);
1698 /* Avoid crashing later. */
1699 define_label (input_location, DECL_NAME (label));
1700 }
1701 else if (warn_unused[UNUSED_LABEL] && !TREE_USED (label))
1702 warning (0, "label %q+D defined but not used", label);
1703 IDENTIFIER_LABEL_VALUE (DECL_NAME (label)) = 0;
1704
1705 /* Put the labels into the "variables" of the
1706 top-level block, so debugger can see them. */
1707 TREE_CHAIN (label) = BLOCK_VARS (block);
1708 BLOCK_VARS (block) = label;
1709 }
1710 #endif
1711 }
1712
1713 /* Pop the current level, and free the structure for reuse. */
1714
1715 {
1716 struct binding_level *level = current_binding_level;
1717 current_binding_level = current_binding_level->level_chain;
1718
1719 level->level_chain = free_binding_level;
1720 free_binding_level = level;
1721 }
1722
1723 /* Dispose of the block that we just made inside some higher level. */
1724 if (functionbody)
1725 {
1726 DECL_INITIAL (current_function_decl) = block;
1727 DECL_SAVED_TREE (current_function_decl) = bind;
1728 }
1729 else
1730 {
1731 if (block)
1732 {
1733 current_binding_level->blocks
1734 = chainon (current_binding_level->blocks, block);
1735 }
1736 /* If we did not make a block for the level just exited,
1737 any blocks made for inner levels
1738 (since they cannot be recorded as subblocks in that level)
1739 must be carried forward so they will later become subblocks
1740 of something else. */
1741 else if (subblocks)
1742 current_binding_level->blocks
1743 = chainon (current_binding_level->blocks, subblocks);
1744
1745 if (bind)
1746 java_add_stmt (bind);
1747 }
1748
1749 if (block)
1750 TREE_USED (block) = 1;
1751 return block;
1752 }
1753
1754 void
1755 maybe_pushlevels (int pc)
1756 {
1757 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1758 current_pc = pc;
1759 #endif
1760
1761 while (pending_local_decls != NULL_TREE &&
1762 DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1763 {
1764 tree *ptr = &pending_local_decls;
1765 tree decl = *ptr, next;
1766 int end_pc = DECL_LOCAL_END_PC (decl);
1767
1768 while (*ptr != NULL_TREE
1769 && DECL_LOCAL_START_PC (*ptr) <= pc
1770 && DECL_LOCAL_END_PC (*ptr) == end_pc)
1771 ptr = &TREE_CHAIN (*ptr);
1772 pending_local_decls = *ptr;
1773 *ptr = NULL_TREE;
1774
1775 /* Force non-nested range to be nested in current range by
1776 truncating variable lifetimes. */
1777 if (end_pc > current_binding_level->end_pc)
1778 {
1779 end_pc = current_binding_level->end_pc;
1780 DECL_LOCAL_END_PC (decl) = end_pc;
1781 }
1782
1783 maybe_start_try (pc, end_pc);
1784
1785 pushlevel (1);
1786
1787 current_binding_level->end_pc = end_pc;
1788 current_binding_level->start_pc = pc;
1789 current_binding_level->names = NULL;
1790 for ( ; decl != NULL_TREE; decl = next)
1791 {
1792 next = TREE_CHAIN (decl);
1793 push_jvm_slot (DECL_LOCAL_SLOT_NUMBER (decl), decl);
1794 pushdecl (decl);
1795 initialize_local_variable (decl, DECL_LOCAL_SLOT_NUMBER (decl));
1796 }
1797 }
1798
1799 maybe_start_try (pc, 0);
1800 }
1801
1802 void
1803 maybe_poplevels (int pc)
1804 {
1805 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1806 current_pc = pc;
1807 #endif
1808
1809 /* FIXME: I'm pretty sure that this is wrong. Variable scopes are
1810 inclusive, so a variable is live if pc == end_pc. Here, we
1811 terminate a range if the current pc is equal to the end of the
1812 range, and this is *before* we have generated code for the
1813 instruction at end_pc. We're closing a binding level one
1814 instruction too early.*/
1815 while (current_binding_level->end_pc <= pc)
1816 poplevel (1, 0, 0);
1817 }
1818
1819 /* Terminate any binding which began during the range beginning at
1820 start_pc. This tidies up improperly nested local variable ranges
1821 and exception handlers; a variable declared within an exception
1822 range is forcibly terminated when that exception ends. */
1823
1824 void
1825 force_poplevels (int start_pc)
1826 {
1827 while (current_binding_level->start_pc > start_pc)
1828 {
1829 if (pedantic && current_binding_level->start_pc > start_pc)
1830 warning (0, "In %+D: overlapped variable and exception ranges at %d",
1831 current_function_decl,
1832 current_binding_level->start_pc);
1833 poplevel (1, 0, 0);
1834 }
1835 }
1836
1837 /* Insert BLOCK at the end of the list of subblocks of the
1838 current binding level. This is used when a BIND_EXPR is expanded,
1839 to handle the BLOCK node inside the BIND_EXPR. */
1840
1841 void
1842 insert_block (tree block)
1843 {
1844 TREE_USED (block) = 1;
1845 current_binding_level->blocks
1846 = chainon (current_binding_level->blocks, block);
1847 }
1848
1849 /* integrate_decl_tree calls this function. */
1850
1851 void
1852 java_dup_lang_specific_decl (tree node)
1853 {
1854 int lang_decl_size;
1855 struct lang_decl *x;
1856
1857 if (!DECL_LANG_SPECIFIC (node))
1858 return;
1859
1860 lang_decl_size = sizeof (struct lang_decl);
1861 x = ggc_alloc (lang_decl_size);
1862 memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1863 DECL_LANG_SPECIFIC (node) = x;
1864 }
1865
1866 void
1867 give_name_to_locals (JCF *jcf)
1868 {
1869 int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1870 int code_offset = DECL_CODE_OFFSET (current_function_decl);
1871 tree parm;
1872 pending_local_decls = NULL_TREE;
1873 if (n == 0)
1874 return;
1875 JCF_SEEK (jcf, n);
1876 n = JCF_readu2 (jcf);
1877 for (i = 0; i < n; i++)
1878 {
1879 int start_pc = JCF_readu2 (jcf);
1880 int length = JCF_readu2 (jcf);
1881 int name_index = JCF_readu2 (jcf);
1882 int signature_index = JCF_readu2 (jcf);
1883 int slot = JCF_readu2 (jcf);
1884 tree name = get_name_constant (jcf, name_index);
1885 tree type = parse_signature (jcf, signature_index);
1886 if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1887 && start_pc == 0
1888 && length == DECL_CODE_LENGTH (current_function_decl))
1889 {
1890 tree decl = TREE_VEC_ELT (decl_map, slot);
1891 DECL_NAME (decl) = name;
1892 if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1893 warning (0, "bad type in parameter debug info");
1894 }
1895 else
1896 {
1897 tree *ptr;
1898 int end_pc = start_pc + length;
1899 tree decl = build_decl (VAR_DECL, name, type);
1900 if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1901 {
1902 warning (0, "bad PC range for debug info for local %q+D",
1903 decl);
1904 end_pc = DECL_CODE_LENGTH (current_function_decl);
1905 }
1906
1907 /* Adjust start_pc if necessary so that the local's first
1908 store operation will use the relevant DECL as a
1909 destination. Fore more information, read the leading
1910 comments for expr.c:maybe_adjust_start_pc. */
1911 start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1912
1913 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1914 DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1915 DECL_LOCAL_START_PC (decl) = start_pc;
1916 #if 0
1917 /* FIXME: The range used internally for exceptions and local
1918 variable ranges, is a half-open interval:
1919 start_pc <= pc < end_pc. However, the range used in the
1920 Java VM spec is inclusive at both ends:
1921 start_pc <= pc <= end_pc. */
1922 end_pc++;
1923 #endif
1924 DECL_LOCAL_END_PC (decl) = end_pc;
1925
1926 /* Now insert the new decl in the proper place in
1927 pending_local_decls. We are essentially doing an insertion sort,
1928 which works fine, since the list input will normally already
1929 be sorted. */
1930 ptr = &pending_local_decls;
1931 while (*ptr != NULL_TREE
1932 && (DECL_LOCAL_START_PC (*ptr) > start_pc
1933 || (DECL_LOCAL_START_PC (*ptr) == start_pc
1934 && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1935 ptr = &TREE_CHAIN (*ptr);
1936 TREE_CHAIN (decl) = *ptr;
1937 *ptr = decl;
1938 }
1939 }
1940
1941 pending_local_decls = nreverse (pending_local_decls);
1942
1943 /* Fill in default names for the parameters. */
1944 for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1945 parm != NULL_TREE; parm = TREE_CHAIN (parm), i++)
1946 {
1947 if (DECL_NAME (parm) == NULL_TREE)
1948 {
1949 int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1950 if (arg_i == 0)
1951 DECL_NAME (parm) = get_identifier ("this");
1952 else
1953 {
1954 char buffer[12];
1955 sprintf (buffer, "ARG_%d", arg_i);
1956 DECL_NAME (parm) = get_identifier (buffer);
1957 }
1958 }
1959 }
1960 }
1961
1962 tree
1963 build_result_decl (tree fndecl)
1964 {
1965 tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1966 tree result = DECL_RESULT (fndecl);
1967 if (! result)
1968 {
1969 /* To be compatible with C_PROMOTING_INTEGER_TYPE_P in cc1/cc1plus. */
1970 if (INTEGRAL_TYPE_P (restype)
1971 && TYPE_PRECISION (restype) < TYPE_PRECISION (integer_type_node))
1972 restype = integer_type_node;
1973 result = build_decl (RESULT_DECL, NULL_TREE, restype);
1974 DECL_ARTIFICIAL (result) = 1;
1975 DECL_IGNORED_P (result) = 1;
1976 DECL_CONTEXT (result) = fndecl;
1977 DECL_RESULT (fndecl) = result;
1978 }
1979 return result;
1980 }
1981
1982 void
1983 start_java_method (tree fndecl)
1984 {
1985 tree tem, *ptr;
1986 int i;
1987
1988 uniq = 0;
1989
1990 current_function_decl = fndecl;
1991 announce_function (fndecl);
1992
1993 i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1994 decl_map = make_tree_vec (i);
1995 base_decl_map = make_tree_vec (i);
1996 type_map = xrealloc (type_map, i * sizeof (tree));
1997
1998 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1999 fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
2000 current_pc = 0;
2001 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
2002 pushlevel (1); /* Push parameters. */
2003
2004 ptr = &DECL_ARGUMENTS (fndecl);
2005 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
2006 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
2007 {
2008 tree parm_name = NULL_TREE, parm_decl;
2009 tree parm_type = TREE_VALUE (tem);
2010 gcc_assert (i < DECL_MAX_LOCALS (fndecl));
2011
2012 parm_decl = build_decl (PARM_DECL, parm_name, parm_type);
2013 DECL_CONTEXT (parm_decl) = fndecl;
2014 if (targetm.calls.promote_prototypes (parm_type)
2015 && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
2016 && INTEGRAL_TYPE_P (parm_type))
2017 parm_type = integer_type_node;
2018 DECL_ARG_TYPE (parm_decl) = parm_type;
2019
2020 *ptr = parm_decl;
2021 ptr = &TREE_CHAIN (parm_decl);
2022
2023 /* Add parm_decl to the decl_map. */
2024 push_jvm_slot (i, parm_decl);
2025
2026 type_map[i] = TREE_TYPE (parm_decl);
2027 if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
2028 {
2029 i++;
2030 type_map[i] = void_type_node;
2031 }
2032 }
2033 *ptr = NULL_TREE;
2034 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
2035
2036 while (i < DECL_MAX_LOCALS(fndecl))
2037 type_map[i++] = NULL_TREE;
2038
2039 build_result_decl (fndecl);
2040
2041 /* Push local variables. */
2042 pushlevel (2);
2043
2044 function_binding_level = current_binding_level;
2045 }
2046
2047 void
2048 end_java_method (void)
2049 {
2050 tree fndecl = current_function_decl;
2051
2052 /* pop out of function */
2053 poplevel (1, 1, 0);
2054
2055 /* pop out of its parameters */
2056 poplevel (1, 0, 1);
2057
2058 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
2059
2060 if (DECL_SAVED_TREE (fndecl))
2061 {
2062 tree fbody, block_body;
2063 /* Before we check initialization, attached all class initialization
2064 variable to the block_body */
2065 fbody = DECL_SAVED_TREE (fndecl);
2066 block_body = BIND_EXPR_BODY (fbody);
2067 htab_traverse (DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
2068 attach_init_test_initialization_flags, block_body);
2069 }
2070
2071 finish_method (fndecl);
2072
2073 if (! flag_unit_at_a_time)
2074 {
2075 /* Nulling these fields when we no longer need them saves
2076 memory. */
2077 DECL_SAVED_TREE (fndecl) = NULL;
2078 DECL_STRUCT_FUNCTION (fndecl) = NULL;
2079 DECL_INITIAL (fndecl) = NULL_TREE;
2080 }
2081 current_function_decl = NULL_TREE;
2082 }
2083
2084 /* Prepare a method for expansion. */
2085
2086 void
2087 finish_method (tree fndecl)
2088 {
2089 tree *tp = &DECL_SAVED_TREE (fndecl);
2090
2091 /* Wrap body of synchronized methods in a monitorenter,
2092 plus monitorexit cleanup. */
2093 if (METHOD_SYNCHRONIZED (fndecl))
2094 {
2095 tree enter, exit, lock;
2096 if (METHOD_STATIC (fndecl))
2097 lock = build_class_ref (DECL_CONTEXT (fndecl));
2098 else
2099 lock = DECL_ARGUMENTS (fndecl);
2100 BUILD_MONITOR_ENTER (enter, lock);
2101 BUILD_MONITOR_EXIT (exit, lock);
2102 *tp = build2 (COMPOUND_EXPR, void_type_node, enter,
2103 build2 (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
2104 }
2105
2106 /* Prepend class initialization for static methods reachable from
2107 other classes. */
2108 if (METHOD_STATIC (fndecl)
2109 && (! METHOD_PRIVATE (fndecl)
2110 || INNER_CLASS_P (DECL_CONTEXT (fndecl)))
2111 && ! DECL_CLINIT_P (fndecl)
2112 && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (fndecl))))
2113 {
2114 tree clas = DECL_CONTEXT (fndecl);
2115 tree init = build3 (CALL_EXPR, void_type_node,
2116 build_address_of (soft_initclass_node),
2117 build_tree_list (NULL_TREE, build_class_ref (clas)),
2118 NULL_TREE);
2119 *tp = build2 (COMPOUND_EXPR, TREE_TYPE (*tp), init, *tp);
2120 }
2121
2122 /* Convert function tree to GENERIC prior to inlining. */
2123 java_genericize (fndecl);
2124
2125 /* Store the end of the function, so that we get good line number
2126 info for the epilogue. */
2127 if (DECL_STRUCT_FUNCTION (fndecl))
2128 cfun = DECL_STRUCT_FUNCTION (fndecl);
2129 else
2130 allocate_struct_function (fndecl);
2131 #ifdef USE_MAPPED_LOCATION
2132 cfun->function_end_locus = DECL_FUNCTION_LAST_LINE (fndecl);
2133 #else
2134 cfun->function_end_locus.file = DECL_SOURCE_FILE (fndecl);
2135 cfun->function_end_locus.line = DECL_FUNCTION_LAST_LINE (fndecl);
2136 #endif
2137
2138 /* Defer inlining and expansion to the cgraph optimizers. */
2139 cgraph_finalize_function (fndecl, false);
2140 }
2141
2142 /* Optimize and expand a function's entire body. */
2143
2144 void
2145 java_expand_body (tree fndecl)
2146 {
2147 tree_rest_of_compilation (fndecl);
2148 }
2149
2150 /* We pessimistically marked all methods and fields external until we
2151 knew what set of classes we were planning to compile. Now mark those
2152 associated with CLASS to be generated locally as not external. */
2153
2154 static void
2155 java_mark_decl_local (tree decl)
2156 {
2157 DECL_EXTERNAL (decl) = 0;
2158
2159 /* If we've already constructed DECL_RTL, give encode_section_info
2160 a second chance, now that we've changed the flags. */
2161 /* ??? Ideally, we'd have flag_unit_at_a_time set, and not have done
2162 anything that would have referenced DECL_RTL so far. But at the
2163 moment we force flag_unit_at_a_time off due to excessive memory
2164 consumption when compiling large jar files. Which probably means
2165 that we need to re-order how we process jar files... */
2166 if (DECL_RTL_SET_P (decl))
2167 make_decl_rtl (decl);
2168 }
2169
2170 /* Given appropriate target support, G++ will emit hidden aliases for native
2171 methods. Using this hidden name is required for proper operation of
2172 _Jv_Method::ncode, but it doesn't hurt to use it everywhere. Look for
2173 proper target support, then mark the method for aliasing. */
2174
2175 static void
2176 java_mark_cni_decl_local (tree decl)
2177 {
2178 /* Setting DECL_LOCAL_CNI_METHOD_P changes the behavior of the mangler.
2179 We expect that we should not yet have referenced this decl in a
2180 context that requires it. Check this invariant even if we don't have
2181 support for hidden aliases. */
2182 gcc_assert (!DECL_ASSEMBLER_NAME_SET_P (decl));
2183
2184 #if !defined(HAVE_GAS_HIDDEN) || !defined(ASM_OUTPUT_DEF)
2185 return;
2186 #endif
2187
2188 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
2189 DECL_LOCAL_CNI_METHOD_P (decl) = 1;
2190 }
2191
2192 /* Use the preceding two functions and mark all members of the class. */
2193
2194 void
2195 java_mark_class_local (tree class)
2196 {
2197 tree t;
2198
2199 for (t = TYPE_FIELDS (class); t ; t = TREE_CHAIN (t))
2200 if (FIELD_STATIC (t))
2201 java_mark_decl_local (t);
2202
2203 for (t = TYPE_METHODS (class); t ; t = TREE_CHAIN (t))
2204 if (!METHOD_ABSTRACT (t))
2205 {
2206 if (METHOD_NATIVE (t) && !flag_jni)
2207 java_mark_cni_decl_local (t);
2208 else
2209 java_mark_decl_local (t);
2210 }
2211 }
2212
2213 /* Add a statement to a compound_expr. */
2214
2215 tree
2216 add_stmt_to_compound (tree existing, tree type, tree stmt)
2217 {
2218 if (!stmt)
2219 return existing;
2220 else if (existing)
2221 {
2222 tree expr = build2 (COMPOUND_EXPR, type, existing, stmt);
2223 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (existing)
2224 | TREE_SIDE_EFFECTS (stmt);
2225 return expr;
2226 }
2227 else
2228 return stmt;
2229 }
2230
2231 /* Add a statement to the compound_expr currently being
2232 constructed. */
2233
2234 tree
2235 java_add_stmt (tree stmt)
2236 {
2237 if (input_filename)
2238 SET_EXPR_LOCATION (stmt, input_location);
2239
2240 return current_binding_level->stmts
2241 = add_stmt_to_compound (current_binding_level->stmts,
2242 TREE_TYPE (stmt), stmt);
2243 }
2244
2245 /* Add a variable to the current scope. */
2246
2247 tree
2248 java_add_local_var (tree decl)
2249 {
2250 tree *vars = &current_binding_level->names;
2251 tree next = *vars;
2252 TREE_CHAIN (decl) = next;
2253 *vars = decl;
2254 DECL_CONTEXT (decl) = current_function_decl;
2255 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2256 return decl;
2257 }
2258
2259 /* Return a pointer to the compound_expr currently being
2260 constructed. */
2261
2262 tree *
2263 get_stmts (void)
2264 {
2265 return &current_binding_level->stmts;
2266 }
2267
2268 /* Register an exception range as belonging to the current binding
2269 level. There may only be one: if there are more, we'll create more
2270 binding levels. However, each range can have multiple handlers,
2271 and these are expanded when we call expand_end_java_handler(). */
2272
2273 void
2274 register_exception_range (struct eh_range *range, int pc, int end_pc)
2275 {
2276 gcc_assert (! current_binding_level->exception_range);
2277 current_binding_level->exception_range = range;
2278 current_binding_level->end_pc = end_pc;
2279 current_binding_level->start_pc = pc;
2280 }
2281
2282 #include "gt-java-decl.h"