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