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