2015-05-22 Pascal Obry <obry@adacore.com>
[official-gcc.git] / gcc / java / decl.c
blob72133b4fcd2f84d16deecea1b8fc3a5961d087e8
1 /* Process declarations and variables for the GNU compiler for the
2 Java(TM) language.
3 Copyright (C) 1996-2015 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "hash-set.h"
31 #include "machmode.h"
32 #include "vec.h"
33 #include "double-int.h"
34 #include "input.h"
35 #include "alias.h"
36 #include "symtab.h"
37 #include "options.h"
38 #include "real.h"
39 #include "wide-int.h"
40 #include "inchash.h"
41 #include "tree.h"
42 #include "stor-layout.h"
43 #include "stringpool.h"
44 #include "varasm.h"
45 #include "diagnostic-core.h"
46 #include "toplev.h"
47 #include "flags.h"
48 #include "java-tree.h"
49 #include "jcf.h"
50 #include "java-except.h"
51 #include "ggc.h"
52 #include "hash-map.h"
53 #include "is-a.h"
54 #include "plugin-api.h"
55 #include "tm.h"
56 #include "hard-reg-set.h"
57 #include "input.h"
58 #include "function.h"
59 #include "ipa-ref.h"
60 #include "cgraph.h"
61 #include "tree-inline.h"
62 #include "target.h"
63 #include "version.h"
64 #include "tree-iterator.h"
65 #include "langhooks.h"
67 #if defined (DEBUG_JAVA_BINDING_LEVELS)
68 extern void indent (void);
69 #endif
71 static tree push_jvm_slot (int, tree);
72 static tree lookup_name_current_level (tree);
73 static tree push_promoted_type (const char *, tree);
74 static struct binding_level *make_binding_level (void);
75 static tree create_primitive_vtable (const char *);
76 static tree check_local_unnamed_variable (tree, tree, tree);
77 static void parse_version (void);
80 /* The following ABI flags are used in the high-order bits of the version
81 ID field. The version ID number itself should never be larger than
82 0xfffff, so it should be safe to use top 12 bits for these flags. */
84 #define FLAG_BINARYCOMPAT_ABI (1<<31) /* Class is built with the BC-ABI. */
86 #define FLAG_BOOTSTRAP_LOADER (1<<30) /* Used when defining a class that
87 should be loaded by the bootstrap
88 loader. */
90 /* If an ABI change is made within a GCC release series, rendering current
91 binaries incompatible with the old runtimes, this number must be set to
92 enforce the compatibility rules. */
93 #define MINOR_BINARYCOMPAT_ABI_VERSION 1
95 /* The runtime may recognize a variety of BC ABIs (objects generated by
96 different version of gcj), but will probably always require strict
97 matching for the ordinary (C++) ABI. */
99 /* The version ID of the BC ABI that we generate. This must be kept in
100 sync with parse_version(), libgcj, and reality (if the BC format changes,
101 this must change). */
102 #define GCJ_CURRENT_BC_ABI_VERSION \
103 (4 * 100000 + 0 * 1000 + MINOR_BINARYCOMPAT_ABI_VERSION)
105 /* The ABI version number. */
106 tree gcj_abi_version;
108 /* Name of the Cloneable class. */
109 tree java_lang_cloneable_identifier_node;
111 /* Name of the Serializable class. */
112 tree java_io_serializable_identifier_node;
114 /* The DECL_MAP is a mapping from (index, type) to a decl node.
115 If index < max_locals, it is the index of a local variable.
116 if index >= max_locals, then index-max_locals is a stack slot.
117 The DECL_MAP mapping is represented as a TREE_VEC whose elements
118 are a list of decls (VAR_DECL or PARM_DECL) chained by
119 DECL_LOCAL_SLOT_CHAIN; the index finds the TREE_VEC element, and then
120 we search the chain for a decl with a matching TREE_TYPE. */
122 static GTY(()) tree decl_map;
124 /* The base_decl_map is contains one variable of ptr_type: this is
125 used to contain every variable of reference type that is ever
126 stored in a local variable slot. */
128 static GTY(()) tree base_decl_map;
130 /* An index used to make temporary identifiers unique. */
131 static int uniq;
133 /* A list of local variables VAR_DECLs for this method that we have seen
134 debug information, but we have not reached their starting (byte) PC yet. */
136 static GTY(()) tree pending_local_decls;
138 /* The decl for "_Jv_ResolvePoolEntry". */
139 tree soft_resolvepoolentry_node;
141 /* The decl for the .constants field of an instance of Class. */
142 tree constants_field_decl_node;
144 /* The decl for the .data field of an instance of Class. */
145 tree constants_data_field_decl_node;
147 #if defined(DEBUG_JAVA_BINDING_LEVELS)
148 int binding_depth = 0;
149 int is_class_level = 0;
150 int current_pc;
152 void
153 indent (void)
155 int i;
157 for (i = 0; i < binding_depth*2; i++)
158 putc (' ', stderr);
160 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
162 /* True if decl is a named local variable, i.e. if it is an alias
163 that's used only for debugging purposes. */
165 static bool
166 debug_variable_p (tree decl)
168 if (TREE_CODE (decl) == PARM_DECL)
169 return false;
171 if (LOCAL_SLOT_P (decl))
172 return false;
174 return true;
177 static tree
178 push_jvm_slot (int index, tree decl)
180 DECL_CONTEXT (decl) = current_function_decl;
181 layout_decl (decl, 0);
183 /* Now link the decl into the decl_map. */
184 if (DECL_LANG_SPECIFIC (decl) == NULL)
186 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
187 DECL_LOCAL_START_PC (decl) = 0;
188 DECL_LOCAL_END_PC (decl) = DECL_CODE_LENGTH (current_function_decl);
189 DECL_LOCAL_SLOT_NUMBER (decl) = index;
191 DECL_LOCAL_SLOT_CHAIN (decl) = TREE_VEC_ELT (decl_map, index);
192 TREE_VEC_ELT (decl_map, index) = decl;
194 return decl;
197 /* Find the best declaration based upon type. If 'decl' fits 'type' better
198 than 'best', return 'decl'. Otherwise return 'best'. */
200 static tree
201 check_local_unnamed_variable (tree best, tree decl, tree type)
203 tree decl_type = TREE_TYPE (decl);
205 gcc_assert (! LOCAL_VAR_OUT_OF_SCOPE_P (decl));
207 /* Use the same decl for all integer types <= 32 bits. This is
208 necessary because sometimes a value is stored as (for example)
209 boolean but loaded as int. */
210 if (decl_type == type
211 || (INTEGRAL_TYPE_P (decl_type)
212 && INTEGRAL_TYPE_P (type)
213 && TYPE_PRECISION (decl_type) <= 32
214 && TYPE_PRECISION (type) <= 32
215 && TYPE_PRECISION (decl_type) >= TYPE_PRECISION (type))
216 /* ptr_type_node is used for null pointers, which are
217 assignment compatible with everything. */
218 || (TREE_CODE (decl_type) == POINTER_TYPE
219 && type == ptr_type_node)
220 /* Whenever anyone wants to use a slot that is initially
221 occupied by a PARM_DECL of pointer type they must get that
222 decl, even if they asked for a pointer to a different type.
223 However, if someone wants a scalar variable in a slot that
224 initially held a pointer arg -- or vice versa -- we create a
225 new VAR_DECL.
227 ???: As long as verification is correct, this will be a
228 compatible type. But maybe we should create a dummy variable
229 and replace all references to it with the DECL and a
230 NOP_EXPR.
232 || (TREE_CODE (decl_type) == POINTER_TYPE
233 && TREE_CODE (decl) == PARM_DECL
234 && TREE_CODE (type) == POINTER_TYPE))
236 if (best == NULL_TREE
237 || (decl_type == type && TREE_TYPE (best) != type))
238 return decl;
241 return best;
245 /* Find a VAR_DECL (or PARM_DECL) at local index INDEX that has type TYPE,
246 that is valid at PC (or -1 if any pc).
247 If there is no existing matching decl, allocate one. */
249 tree
250 find_local_variable (int index, tree type, int pc ATTRIBUTE_UNUSED)
252 tree tmp = TREE_VEC_ELT (decl_map, index);
253 tree decl = NULL_TREE;
255 /* Scan through every declaration that has been created in this
256 slot. We're only looking for variables that correspond to local
257 index declarations and PARM_DECLs, not named variables: such
258 local variables are used only for debugging information. */
259 while (tmp != NULL_TREE)
261 if (! debug_variable_p (tmp))
262 decl = check_local_unnamed_variable (decl, tmp, type);
263 tmp = DECL_LOCAL_SLOT_CHAIN (tmp);
266 /* gcj has a function called promote_type(), which is used by both
267 the bytecode compiler and the source compiler. Unfortunately,
268 the type systems for the Java VM and the Java language are not
269 the same: a boolean in the VM promotes to an int, not to a wide
270 boolean. If our caller wants something to hold a boolean, that
271 had better be an int, because that slot might be re-used
272 later in integer context. */
273 if (TREE_CODE (type) == BOOLEAN_TYPE)
274 type = integer_type_node;
276 /* If we don't find a match, create one with the type passed in.
277 The name of the variable is #n#m, which n is the variable index
278 in the local variable area and m is a dummy identifier for
279 uniqueness -- multiple variables may share the same local
280 variable index. We don't call pushdecl() to push pointer types
281 into a binding expr because they'll all be replaced by a single
282 variable that is used for every reference in that local variable
283 slot. */
284 if (! decl)
286 char buf[64];
287 tree name;
288 sprintf (buf, "#slot#%d#%d", index, uniq++);
289 name = get_identifier (buf);
290 decl = build_decl (input_location, VAR_DECL, name, type);
291 DECL_IGNORED_P (decl) = 1;
292 DECL_ARTIFICIAL (decl) = 1;
293 decl = push_jvm_slot (index, decl);
294 LOCAL_SLOT_P (decl) = 1;
296 if (TREE_CODE (type) != POINTER_TYPE)
297 pushdecl_function_level (decl);
300 /* As well as creating a local variable that matches the type, we
301 also create a base variable (of ptr_type) that will hold all its
302 aliases. */
303 if (TREE_CODE (type) == POINTER_TYPE
304 && ! TREE_VEC_ELT (base_decl_map, index))
306 char buf[64];
307 tree name;
308 tree base_decl;
309 sprintf (buf, "#ref#%d#%d", index, uniq++);
310 name = get_identifier (buf);
311 base_decl
312 = TREE_VEC_ELT (base_decl_map, index)
313 = build_decl (input_location, VAR_DECL, name, ptr_type_node);
314 pushdecl_function_level (base_decl);
315 DECL_IGNORED_P (base_decl) = 1;
316 DECL_ARTIFICIAL (base_decl) = 1;
319 return decl;
322 /* Called during genericization for every variable. If the variable
323 is a temporary of pointer type, replace it with a common variable
324 thath is used to hold all pointer types that are ever stored in
325 that slot. Set WANT_LVALUE if you want a variable that is to be
326 written to. */
328 static tree
329 java_replace_reference (tree var_decl, bool want_lvalue)
331 tree decl_type;
333 if (! base_decl_map)
334 return var_decl;
336 decl_type = TREE_TYPE (var_decl);
338 if (TREE_CODE (decl_type) == POINTER_TYPE)
340 if (DECL_LANG_SPECIFIC (var_decl)
341 && LOCAL_SLOT_P (var_decl))
343 int index = DECL_LOCAL_SLOT_NUMBER (var_decl);
344 tree base_decl = TREE_VEC_ELT (base_decl_map, index);
346 gcc_assert (base_decl);
347 if (! want_lvalue)
348 base_decl = build1 (NOP_EXPR, decl_type, base_decl);
350 return base_decl;
354 return var_decl;
357 /* Helper for java_genericize. */
359 tree
360 java_replace_references (tree *tp, int *walk_subtrees,
361 void *data ATTRIBUTE_UNUSED)
363 if (TREE_CODE (*tp) == MODIFY_EXPR)
365 source_location loc = EXPR_LOCATION (*tp);
366 tree lhs = TREE_OPERAND (*tp, 0);
367 /* This is specific to the bytecode compiler. If a variable has
368 LOCAL_SLOT_P set, replace an assignment to it with an assignment
369 to the corresponding variable that holds all its aliases. */
370 if (TREE_CODE (lhs) == VAR_DECL
371 && DECL_LANG_SPECIFIC (lhs)
372 && LOCAL_SLOT_P (lhs)
373 && TREE_CODE (TREE_TYPE (lhs)) == POINTER_TYPE)
375 tree new_lhs = java_replace_reference (lhs, /* want_lvalue */ true);
376 tree new_rhs = build1 (NOP_EXPR, TREE_TYPE (new_lhs),
377 TREE_OPERAND (*tp, 1));
378 tree tem = build2 (MODIFY_EXPR, TREE_TYPE (new_lhs),
379 new_lhs, new_rhs);
380 *tp = build1 (NOP_EXPR, TREE_TYPE (lhs), tem);
381 SET_EXPR_LOCATION (tem, loc);
382 SET_EXPR_LOCATION (new_rhs, loc);
383 SET_EXPR_LOCATION (*tp, loc);
386 if (TREE_CODE (*tp) == VAR_DECL)
388 *tp = java_replace_reference (*tp, /* want_lvalue */ false);
389 *walk_subtrees = 0;
392 return NULL_TREE;
395 /* Same as find_local_index, except that INDEX is a stack index. */
397 tree
398 find_stack_slot (int index, tree type)
400 return find_local_variable (index + DECL_MAX_LOCALS (current_function_decl),
401 type, -1);
404 struct GTY(())
405 binding_level {
406 /* A chain of _DECL nodes for all variables, constants, functions,
407 * and typedef types. These are in the reverse of the order supplied.
409 tree names;
411 /* For each level, a list of shadowed outer-level local definitions
412 to be restored when this level is popped.
413 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
414 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
415 tree shadowed;
417 /* For each level (except not the global one),
418 a chain of BLOCK nodes for all the levels
419 that were entered and exited one level down. */
420 tree blocks;
422 /* The binding level which this one is contained in (inherits from). */
423 struct binding_level *level_chain;
425 /* The bytecode PC that marks the end of this level. */
426 int end_pc;
427 /* The bytecode PC that marks the start of this level. */
428 int start_pc;
430 /* The statements in this binding level. */
431 tree stmts;
433 /* An exception range associated with this binding level. */
434 struct eh_range * GTY((skip (""))) exception_range;
436 /* Binding depth at which this level began. Used only for debugging. */
437 unsigned binding_depth;
439 /* The location at which this level began. */
440 source_location loc;
443 #define NULL_BINDING_LEVEL (struct binding_level *) NULL
445 /* The binding level currently in effect. */
447 static GTY(()) struct binding_level *current_binding_level;
449 /* A chain of binding_level structures awaiting reuse. */
451 static GTY(()) struct binding_level *free_binding_level;
453 /* The outermost binding level, for names of file scope.
454 This is created when the compiler is started and exists
455 through the entire run. */
457 static GTY(()) struct binding_level *global_binding_level;
459 /* The binding level that holds variables declared at the outermost
460 level within a function body. */
462 static struct binding_level *function_binding_level;
464 /* A PC value bigger than any PC value we may ever may encounter. */
466 #define LARGEST_PC (( (unsigned int)1 << (HOST_BITS_PER_INT - 1)) - 1)
468 /* Binding level structures are initialized by copying this one. */
470 static const struct binding_level clear_binding_level
472 NULL_TREE, /* names */
473 NULL_TREE, /* shadowed */
474 NULL_TREE, /* blocks */
475 NULL_BINDING_LEVEL, /* level_chain */
476 LARGEST_PC, /* end_pc */
477 0, /* start_pc */
478 NULL, /* stmts */
479 NULL, /* exception_range */
480 0, /* binding_depth */
481 0, /* loc */
484 tree java_global_trees[JTI_MAX];
486 /* Build (and pushdecl) a "promoted type" for all standard
487 types shorter than int. */
489 static tree
490 push_promoted_type (const char *name, tree actual_type)
492 tree type = make_node (TREE_CODE (actual_type));
493 #if 1
494 tree in_min = TYPE_MIN_VALUE (int_type_node);
495 tree in_max = TYPE_MAX_VALUE (int_type_node);
496 #else
497 tree in_min = TYPE_MIN_VALUE (actual_type);
498 tree in_max = TYPE_MAX_VALUE (actual_type);
499 #endif
500 TYPE_MIN_VALUE (type) = copy_node (in_min);
501 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
502 TYPE_MAX_VALUE (type) = copy_node (in_max);
503 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
504 TYPE_PRECISION (type) = TYPE_PRECISION (int_type_node);
505 TYPE_STRING_FLAG (type) = TYPE_STRING_FLAG (actual_type);
506 layout_type (type);
507 pushdecl (build_decl (input_location,
508 TYPE_DECL, get_identifier (name), type));
509 return type;
512 /* Return tree that represents a vtable for a primitive array. */
513 static tree
514 create_primitive_vtable (const char *name)
516 tree r;
517 char buf[50];
519 sprintf (buf, "_Jv_%sVTable", name);
520 r = build_decl (input_location,
521 VAR_DECL, get_identifier (buf), ptr_type_node);
522 DECL_EXTERNAL (r) = 1;
523 return r;
526 /* Parse the version string and compute the ABI version number. */
527 static void
528 parse_version (void)
530 const char *p = version_string;
531 unsigned int major = 0, minor = 0;
532 unsigned int abi_version;
534 /* Skip leading junk. */
535 while (*p && !ISDIGIT (*p))
536 ++p;
537 gcc_assert (*p);
539 /* Extract major version. */
540 while (ISDIGIT (*p))
542 major = major * 10 + *p - '0';
543 ++p;
546 gcc_assert (*p == '.' && ISDIGIT (p[1]));
547 ++p;
549 /* Extract minor version. */
550 while (ISDIGIT (*p))
552 minor = minor * 10 + *p - '0';
553 ++p;
556 if (flag_indirect_dispatch)
558 abi_version = GCJ_CURRENT_BC_ABI_VERSION;
559 abi_version |= FLAG_BINARYCOMPAT_ABI;
561 else /* C++ ABI */
563 /* Implicit in this computation is the idea that we won't break the
564 old-style binary ABI in a sub-minor release (e.g., from 4.0.0 to
565 4.0.1). */
566 abi_version = 100000 * major + 1000 * minor;
568 if (flag_bootstrap_classes)
569 abi_version |= FLAG_BOOTSTRAP_LOADER;
571 gcj_abi_version = build_int_cstu (ptr_type_node, abi_version);
574 void
575 java_init_decl_processing (void)
577 tree field = NULL_TREE;
578 tree t;
580 init_class_processing ();
582 current_function_decl = NULL;
583 current_binding_level = NULL_BINDING_LEVEL;
584 free_binding_level = NULL_BINDING_LEVEL;
585 pushlevel (0); /* make the binding_level structure for global names */
586 global_binding_level = current_binding_level;
588 /* Build common tree nodes, Java has an unsigned char. */
589 build_common_tree_nodes (false, false);
591 /* ??? Now we continue and override some of the built types again
592 with Java specific types. As the above generated types are
593 supposed to match the targets C ABI this isn't really the way
594 to go and any Java specifics should _not_ use those global types
595 if the Java ABI does not match the C one. */
597 byte_type_node = make_signed_type (8);
598 pushdecl (build_decl (BUILTINS_LOCATION,
599 TYPE_DECL, get_identifier ("byte"), byte_type_node));
600 short_type_node = make_signed_type (16);
601 pushdecl (build_decl (BUILTINS_LOCATION,
602 TYPE_DECL, get_identifier ("short"), short_type_node));
603 int_type_node = make_signed_type (32);
604 pushdecl (build_decl (BUILTINS_LOCATION,
605 TYPE_DECL, get_identifier ("int"), int_type_node));
606 long_type_node = make_signed_type (64);
607 pushdecl (build_decl (BUILTINS_LOCATION,
608 TYPE_DECL, get_identifier ("long"), long_type_node));
610 unsigned_byte_type_node = make_unsigned_type (8);
611 pushdecl (build_decl (BUILTINS_LOCATION,
612 TYPE_DECL, get_identifier ("unsigned byte"),
613 unsigned_byte_type_node));
614 unsigned_short_type_node = make_unsigned_type (16);
615 pushdecl (build_decl (BUILTINS_LOCATION,
616 TYPE_DECL, get_identifier ("unsigned short"),
617 unsigned_short_type_node));
618 unsigned_int_type_node = make_unsigned_type (32);
619 pushdecl (build_decl (BUILTINS_LOCATION,
620 TYPE_DECL, get_identifier ("unsigned int"),
621 unsigned_int_type_node));
622 unsigned_long_type_node = make_unsigned_type (64);
623 pushdecl (build_decl (BUILTINS_LOCATION,
624 TYPE_DECL, get_identifier ("unsigned long"),
625 unsigned_long_type_node));
627 /* Define these next since types below may used them. */
628 integer_type_node = java_type_for_size (INT_TYPE_SIZE, 0);
629 integer_zero_node = build_int_cst (NULL_TREE, 0);
630 integer_one_node = build_int_cst (NULL_TREE, 1);
631 integer_two_node = build_int_cst (NULL_TREE, 2);
632 integer_three_node = build_int_cst (NULL_TREE, 3);
633 integer_four_node = build_int_cst (NULL_TREE, 4);
634 integer_minus_one_node = build_int_cst (NULL_TREE, -1);
636 /* A few values used for range checking in the lexer. */
637 decimal_int_max = build_int_cstu (unsigned_int_type_node, 0x80000000);
638 decimal_long_max
639 = double_int_to_tree (unsigned_long_type_node,
640 double_int_zero.set_bit (64));
642 long_zero_node = build_int_cst (long_type_node, 0);
644 pushdecl (build_decl (BUILTINS_LOCATION,
645 TYPE_DECL, get_identifier ("void"), void_type_node));
647 t = make_node (VOID_TYPE);
648 layout_type (t); /* Uses size_zero_node */
649 return_address_type_node = build_pointer_type (t);
651 char_type_node = make_unsigned_type (16);
652 TYPE_STRING_FLAG (char_type_node) = 1;
653 pushdecl (build_decl (BUILTINS_LOCATION,
654 TYPE_DECL, get_identifier ("char"), char_type_node));
656 boolean_type_node = make_unsigned_type (1);
657 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
658 pushdecl (build_decl (BUILTINS_LOCATION,
659 TYPE_DECL, get_identifier ("boolean"),
660 boolean_type_node));
661 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
662 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
664 promoted_byte_type_node
665 = push_promoted_type ("promoted_byte", byte_type_node);
666 promoted_short_type_node
667 = push_promoted_type ("promoted_short", short_type_node);
668 promoted_char_type_node
669 = push_promoted_type ("promoted_char", char_type_node);
670 promoted_boolean_type_node
671 = push_promoted_type ("promoted_boolean", boolean_type_node);
673 float_type_node = make_node (REAL_TYPE);
674 TYPE_PRECISION (float_type_node) = 32;
675 pushdecl (build_decl (BUILTINS_LOCATION,
676 TYPE_DECL, get_identifier ("float"),
677 float_type_node));
678 layout_type (float_type_node);
680 double_type_node = make_node (REAL_TYPE);
681 TYPE_PRECISION (double_type_node) = 64;
682 pushdecl (build_decl (BUILTINS_LOCATION,
683 TYPE_DECL, get_identifier ("double"),
684 double_type_node));
685 layout_type (double_type_node);
687 float_zero_node = build_real (float_type_node, dconst0);
688 double_zero_node = build_real (double_type_node, dconst0);
690 /* These are the vtables for arrays of primitives. */
691 boolean_array_vtable = create_primitive_vtable ("boolean");
692 byte_array_vtable = create_primitive_vtable ("byte");
693 char_array_vtable = create_primitive_vtable ("char");
694 short_array_vtable = create_primitive_vtable ("short");
695 int_array_vtable = create_primitive_vtable ("int");
696 long_array_vtable = create_primitive_vtable ("long");
697 float_array_vtable = create_primitive_vtable ("float");
698 double_array_vtable = create_primitive_vtable ("double");
700 one_elt_array_domain_type = build_index_type (integer_one_node);
701 utf8const_type = make_node (RECORD_TYPE);
702 PUSH_FIELD (input_location,
703 utf8const_type, field, "hash", unsigned_short_type_node);
704 PUSH_FIELD (input_location,
705 utf8const_type, field, "length", unsigned_short_type_node);
706 FINISH_RECORD (utf8const_type);
707 utf8const_ptr_type = build_pointer_type (utf8const_type);
709 atable_type = build_array_type (ptr_type_node,
710 one_elt_array_domain_type);
711 TYPE_NONALIASED_COMPONENT (atable_type) = 1;
712 atable_ptr_type = build_pointer_type (atable_type);
714 itable_type = build_array_type (ptr_type_node,
715 one_elt_array_domain_type);
716 TYPE_NONALIASED_COMPONENT (itable_type) = 1;
717 itable_ptr_type = build_pointer_type (itable_type);
719 symbol_type = make_node (RECORD_TYPE);
720 PUSH_FIELD (input_location,
721 symbol_type, field, "clname", utf8const_ptr_type);
722 PUSH_FIELD (input_location, symbol_type, field, "name", utf8const_ptr_type);
723 PUSH_FIELD (input_location,
724 symbol_type, field, "signature", utf8const_ptr_type);
725 FINISH_RECORD (symbol_type);
727 symbols_array_type = build_array_type (symbol_type,
728 one_elt_array_domain_type);
729 symbols_array_ptr_type = build_pointer_type (symbols_array_type);
731 assertion_entry_type = make_node (RECORD_TYPE);
732 PUSH_FIELD (input_location,
733 assertion_entry_type, field, "assertion_code", integer_type_node);
734 PUSH_FIELD (input_location,
735 assertion_entry_type, field, "op1", utf8const_ptr_type);
736 PUSH_FIELD (input_location,
737 assertion_entry_type, field, "op2", utf8const_ptr_type);
738 FINISH_RECORD (assertion_entry_type);
740 assertion_table_type = build_array_type (assertion_entry_type,
741 one_elt_array_domain_type);
743 /* As you're adding items here, please update the code right after
744 this section, so that the filename containing the source code of
745 the pre-defined class gets registered correctly. */
746 unqualified_object_id_node = get_identifier ("Object");
747 object_type_node = lookup_class (get_identifier ("java.lang.Object"));
748 object_ptr_type_node = promote_type (object_type_node);
749 string_type_node = lookup_class (get_identifier ("java.lang.String"));
750 string_ptr_type_node = promote_type (string_type_node);
751 class_type_node = lookup_class (get_identifier ("java.lang.Class"));
752 throwable_type_node = lookup_class (get_identifier ("java.lang.Throwable"));
753 exception_type_node = lookup_class (get_identifier ("java.lang.Exception"));
754 runtime_exception_type_node =
755 lookup_class (get_identifier ("java.lang.RuntimeException"));
756 error_exception_type_node =
757 lookup_class (get_identifier ("java.lang.Error"));
759 rawdata_ptr_type_node
760 = promote_type (lookup_class (get_identifier ("gnu.gcj.RawData")));
762 add_predefined_file (get_identifier ("java/lang/Class.java"));
763 add_predefined_file (get_identifier ("java/lang/Error.java"));
764 add_predefined_file (get_identifier ("java/lang/Object.java"));
765 add_predefined_file (get_identifier ("java/lang/RuntimeException.java"));
766 add_predefined_file (get_identifier ("java/lang/String.java"));
767 add_predefined_file (get_identifier ("java/lang/Throwable.java"));
768 add_predefined_file (get_identifier ("gnu/gcj/RawData.java"));
769 add_predefined_file (get_identifier ("java/lang/Exception.java"));
770 add_predefined_file (get_identifier ("java/lang/ClassNotFoundException.java"));
771 add_predefined_file (get_identifier ("java/lang/NoClassDefFoundError.java"));
773 methodtable_type = make_node (RECORD_TYPE);
774 layout_type (methodtable_type);
775 build_decl (BUILTINS_LOCATION,
776 TYPE_DECL, get_identifier ("methodtable"), methodtable_type);
777 methodtable_ptr_type = build_pointer_type (methodtable_type);
779 TYPE_identifier_node = get_identifier ("TYPE");
780 init_identifier_node = get_identifier ("<init>");
781 clinit_identifier_node = get_identifier ("<clinit>");
782 void_signature_node = get_identifier ("()V");
783 finalize_identifier_node = get_identifier ("finalize");
784 this_identifier_node = get_identifier ("this");
786 java_lang_cloneable_identifier_node = get_identifier ("java.lang.Cloneable");
787 java_io_serializable_identifier_node =
788 get_identifier ("java.io.Serializable");
790 /* for lack of a better place to put this stub call */
791 init_expr_processing();
793 constants_type_node = make_node (RECORD_TYPE);
794 PUSH_FIELD (input_location,
795 constants_type_node, field, "size", unsigned_int_type_node);
796 PUSH_FIELD (input_location,
797 constants_type_node, field, "tags", ptr_type_node);
798 PUSH_FIELD (input_location,
799 constants_type_node, field, "data", ptr_type_node);
800 constants_data_field_decl_node = field;
801 FINISH_RECORD (constants_type_node);
802 build_decl (BUILTINS_LOCATION,
803 TYPE_DECL, get_identifier ("constants"), constants_type_node);
805 access_flags_type_node = unsigned_short_type_node;
807 dtable_type = make_node (RECORD_TYPE);
808 dtable_ptr_type = build_pointer_type (dtable_type);
810 otable_type = build_array_type (integer_type_node,
811 one_elt_array_domain_type);
812 TYPE_NONALIASED_COMPONENT (otable_type) = 1;
813 otable_ptr_type = build_pointer_type (otable_type);
815 PUSH_FIELD (input_location,
816 object_type_node, field, "vtable", dtable_ptr_type);
817 DECL_FCONTEXT (field) = object_type_node;
818 TYPE_VFIELD (object_type_node) = field;
820 /* This isn't exactly true, but it is what we have in the source.
821 There is an unresolved issue here, which is whether the vtable
822 should be marked by the GC. */
823 if (! flag_hash_synchronization)
824 PUSH_FIELD (input_location, object_type_node, field, "sync_info",
825 build_pointer_type (object_type_node));
826 for (t = TYPE_FIELDS (object_type_node); t != NULL_TREE; t = DECL_CHAIN (t))
827 FIELD_PRIVATE (t) = 1;
828 FINISH_RECORD (object_type_node);
830 field_type_node = make_node (RECORD_TYPE);
831 field_ptr_type_node = build_pointer_type (field_type_node);
832 method_type_node = make_node (RECORD_TYPE);
833 method_ptr_type_node = build_pointer_type (method_type_node);
835 set_super_info (0, class_type_node, object_type_node, 0);
836 set_super_info (0, string_type_node, object_type_node, 0);
837 class_ptr_type = build_pointer_type (class_type_node);
839 PUSH_FIELD (input_location,
840 class_type_node, field, "next_or_version", class_ptr_type);
841 PUSH_FIELD (input_location,
842 class_type_node, field, "name", utf8const_ptr_type);
843 PUSH_FIELD (input_location,
844 class_type_node, field, "accflags", access_flags_type_node);
845 PUSH_FIELD (input_location,
846 class_type_node, field, "superclass", class_ptr_type);
847 PUSH_FIELD (input_location,
848 class_type_node, field, "constants", constants_type_node);
849 constants_field_decl_node = field;
850 PUSH_FIELD (input_location,
851 class_type_node, field, "methods", method_ptr_type_node);
852 PUSH_FIELD (input_location,
853 class_type_node, field, "method_count", short_type_node);
854 PUSH_FIELD (input_location,
855 class_type_node, field, "vtable_method_count", short_type_node);
856 PUSH_FIELD (input_location,
857 class_type_node, field, "fields", field_ptr_type_node);
858 PUSH_FIELD (input_location,
859 class_type_node, field, "size_in_bytes", int_type_node);
860 PUSH_FIELD (input_location,
861 class_type_node, field, "field_count", short_type_node);
862 PUSH_FIELD (input_location,
863 class_type_node, field, "static_field_count", short_type_node);
864 PUSH_FIELD (input_location,
865 class_type_node, field, "vtable", dtable_ptr_type);
866 PUSH_FIELD (input_location,
867 class_type_node, field, "otable", otable_ptr_type);
868 PUSH_FIELD (input_location,
869 class_type_node, field, "otable_syms",
870 symbols_array_ptr_type);
871 PUSH_FIELD (input_location,
872 class_type_node, field, "atable", atable_ptr_type);
873 PUSH_FIELD (input_location,
874 class_type_node, field, "atable_syms",
875 symbols_array_ptr_type);
876 PUSH_FIELD (input_location,
877 class_type_node, field, "itable", itable_ptr_type);
878 PUSH_FIELD (input_location, class_type_node, field, "itable_syms",
879 symbols_array_ptr_type);
880 PUSH_FIELD (input_location,
881 class_type_node, field, "catch_classes", ptr_type_node);
882 PUSH_FIELD (input_location, class_type_node, field, "interfaces",
883 build_pointer_type (class_ptr_type));
884 PUSH_FIELD (input_location, class_type_node, field, "loader", ptr_type_node);
885 PUSH_FIELD (input_location,
886 class_type_node, field, "interface_count", short_type_node);
887 PUSH_FIELD (input_location, class_type_node, field, "state", byte_type_node);
888 PUSH_FIELD (input_location, class_type_node, field, "thread", ptr_type_node);
889 PUSH_FIELD (input_location,
890 class_type_node, field, "depth", short_type_node);
891 PUSH_FIELD (input_location,
892 class_type_node, field, "ancestors", ptr_type_node);
893 PUSH_FIELD (input_location, class_type_node, field, "idt", ptr_type_node);
894 PUSH_FIELD (input_location,
895 class_type_node, field, "arrayclass", ptr_type_node);
896 PUSH_FIELD (input_location,
897 class_type_node, field, "protectionDomain", ptr_type_node);
898 PUSH_FIELD (input_location,
899 class_type_node, field, "assertion_table", ptr_type_node);
900 PUSH_FIELD (input_location,
901 class_type_node, field, "hack_signers", ptr_type_node);
902 PUSH_FIELD (input_location, class_type_node, field, "chain", ptr_type_node);
903 PUSH_FIELD (input_location,
904 class_type_node, field, "aux_info", ptr_type_node);
905 PUSH_FIELD (input_location, class_type_node, field, "engine", ptr_type_node);
906 PUSH_FIELD (input_location,
907 class_type_node, field, "reflection_data", ptr_type_node);
908 for (t = TYPE_FIELDS (class_type_node); t != NULL_TREE; t = DECL_CHAIN (t))
909 FIELD_PRIVATE (t) = 1;
910 push_super_field (class_type_node, object_type_node);
912 FINISH_RECORD (class_type_node);
913 build_decl (BUILTINS_LOCATION,
914 TYPE_DECL, get_identifier ("Class"), class_type_node);
916 field_info_union_node = make_node (UNION_TYPE);
917 PUSH_FIELD (input_location,
918 field_info_union_node, field, "boffset", int_type_node);
919 PUSH_FIELD (input_location,
920 field_info_union_node, field, "addr", ptr_type_node);
921 layout_type (field_info_union_node);
923 PUSH_FIELD (input_location,
924 field_type_node, field, "name", utf8const_ptr_type);
925 PUSH_FIELD (input_location, field_type_node, field, "type", class_ptr_type);
926 PUSH_FIELD (input_location,
927 field_type_node, field, "accflags", access_flags_type_node);
928 PUSH_FIELD (input_location,
929 field_type_node, field, "bsize", unsigned_short_type_node);
930 PUSH_FIELD (input_location,
931 field_type_node, field, "info", field_info_union_node);
932 FINISH_RECORD (field_type_node);
933 build_decl (BUILTINS_LOCATION,
934 TYPE_DECL, get_identifier ("Field"), field_type_node);
936 nativecode_ptr_array_type_node
937 = build_array_type (nativecode_ptr_type_node, one_elt_array_domain_type);
939 PUSH_FIELD (input_location,
940 dtable_type, field, "class", class_ptr_type);
941 PUSH_FIELD (input_location,
942 dtable_type, field, "methods", nativecode_ptr_array_type_node);
943 FINISH_RECORD (dtable_type);
944 build_decl (BUILTINS_LOCATION,
945 TYPE_DECL, get_identifier ("dispatchTable"), dtable_type);
947 jexception_type = make_node (RECORD_TYPE);
948 PUSH_FIELD (input_location,
949 jexception_type, field, "start_pc", ptr_type_node);
950 PUSH_FIELD (input_location, jexception_type, field, "end_pc", ptr_type_node);
951 PUSH_FIELD (input_location,
952 jexception_type, field, "handler_pc", ptr_type_node);
953 PUSH_FIELD (input_location,
954 jexception_type, field, "catch_type", class_ptr_type);
955 FINISH_RECORD (jexception_type);
956 build_decl (BUILTINS_LOCATION,
957 TYPE_DECL, get_identifier ("jexception"), field_type_node);
958 jexception_ptr_type = build_pointer_type (jexception_type);
960 lineNumberEntry_type = make_node (RECORD_TYPE);
961 PUSH_FIELD (input_location,
962 lineNumberEntry_type, field, "line_nr", unsigned_short_type_node);
963 PUSH_FIELD (input_location,
964 lineNumberEntry_type, field, "start_pc", ptr_type_node);
965 FINISH_RECORD (lineNumberEntry_type);
967 lineNumbers_type = make_node (RECORD_TYPE);
968 PUSH_FIELD (input_location,
969 lineNumbers_type, field, "length", unsigned_int_type_node);
970 FINISH_RECORD (lineNumbers_type);
972 PUSH_FIELD (input_location,
973 method_type_node, field, "name", utf8const_ptr_type);
974 PUSH_FIELD (input_location,
975 method_type_node, field, "signature", utf8const_ptr_type);
976 PUSH_FIELD (input_location,
977 method_type_node, field, "accflags", access_flags_type_node);
978 PUSH_FIELD (input_location,
979 method_type_node, field, "index", unsigned_short_type_node);
980 PUSH_FIELD (input_location,
981 method_type_node, field, "ncode", nativecode_ptr_type_node);
982 PUSH_FIELD (input_location,
983 method_type_node, field, "throws", ptr_type_node);
984 FINISH_RECORD (method_type_node);
985 build_decl (BUILTINS_LOCATION,
986 TYPE_DECL, get_identifier ("Method"), method_type_node);
988 end_params_node = tree_cons (NULL_TREE, void_type_node, NULL_TREE);
990 t = build_function_type_list (ptr_type_node, class_ptr_type, NULL_TREE);
991 alloc_object_node = add_builtin_function ("_Jv_AllocObject", t,
992 0, NOT_BUILT_IN, NULL, NULL_TREE);
993 DECL_IS_MALLOC (alloc_object_node) = 1;
994 alloc_no_finalizer_node =
995 add_builtin_function ("_Jv_AllocObjectNoFinalizer", t,
996 0, NOT_BUILT_IN, NULL, NULL_TREE);
997 DECL_IS_MALLOC (alloc_no_finalizer_node) = 1;
999 t = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
1000 soft_initclass_node = add_builtin_function ("_Jv_InitClass", t,
1001 0, NOT_BUILT_IN, NULL, NULL_TREE);
1002 t = build_function_type_list (ptr_type_node,
1003 class_ptr_type, int_type_node, NULL_TREE);
1004 soft_resolvepoolentry_node
1005 = add_builtin_function ("_Jv_ResolvePoolEntry", t,
1006 0,NOT_BUILT_IN, NULL, NULL_TREE);
1007 DECL_PURE_P (soft_resolvepoolentry_node) = 1;
1008 t = build_function_type_list (void_type_node,
1009 class_ptr_type, int_type_node, NULL_TREE);
1010 throw_node = add_builtin_function ("_Jv_Throw", t,
1011 0, NOT_BUILT_IN, NULL, NULL_TREE);
1012 /* Mark throw_nodes as `noreturn' functions with side effects. */
1013 TREE_THIS_VOLATILE (throw_node) = 1;
1014 TREE_SIDE_EFFECTS (throw_node) = 1;
1016 t = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
1017 soft_monitorenter_node
1018 = add_builtin_function ("_Jv_MonitorEnter", t, 0, NOT_BUILT_IN,
1019 NULL, NULL_TREE);
1020 soft_monitorexit_node
1021 = add_builtin_function ("_Jv_MonitorExit", t, 0, NOT_BUILT_IN,
1022 NULL, NULL_TREE);
1024 t = build_function_type_list (ptr_type_node,
1025 ptr_type_node, int_type_node, NULL_TREE);
1026 soft_newarray_node
1027 = add_builtin_function ("_Jv_NewPrimArray", t,
1028 0, NOT_BUILT_IN, NULL, NULL_TREE);
1029 DECL_IS_MALLOC (soft_newarray_node) = 1;
1031 t = build_function_type_list (ptr_type_node,
1032 int_type_node, class_ptr_type,
1033 object_ptr_type_node, NULL_TREE);
1034 soft_anewarray_node
1035 = add_builtin_function ("_Jv_NewObjectArray", t,
1036 0, NOT_BUILT_IN, NULL, NULL_TREE);
1037 DECL_IS_MALLOC (soft_anewarray_node) = 1;
1039 t = build_varargs_function_type_list (ptr_type_node,
1040 ptr_type_node, int_type_node,
1041 NULL_TREE);
1042 soft_multianewarray_node
1043 = add_builtin_function ("_Jv_NewMultiArray", t,
1044 0, NOT_BUILT_IN, NULL, NULL_TREE);
1045 DECL_IS_MALLOC (soft_multianewarray_node) = 1;
1047 t = build_function_type_list (void_type_node, int_type_node, NULL_TREE);
1048 soft_badarrayindex_node
1049 = add_builtin_function ("_Jv_ThrowBadArrayIndex", t,
1050 0, NOT_BUILT_IN, NULL, NULL_TREE);
1051 /* Mark soft_badarrayindex_node as a `noreturn' function with side
1052 effects. */
1053 TREE_THIS_VOLATILE (soft_badarrayindex_node) = 1;
1054 TREE_SIDE_EFFECTS (soft_badarrayindex_node) = 1;
1056 t = build_function_type_list (void_type_node, NULL_TREE);
1057 soft_nullpointer_node
1058 = add_builtin_function ("_Jv_ThrowNullPointerException", t,
1059 0, NOT_BUILT_IN, NULL, NULL_TREE);
1060 /* Mark soft_nullpointer_node as a `noreturn' function with side
1061 effects. */
1062 TREE_THIS_VOLATILE (soft_nullpointer_node) = 1;
1063 TREE_SIDE_EFFECTS (soft_nullpointer_node) = 1;
1065 soft_abstractmethod_node
1066 = add_builtin_function ("_Jv_ThrowAbstractMethodError", t,
1067 0, NOT_BUILT_IN, NULL, NULL_TREE);
1068 /* Mark soft_abstractmethod_node as a `noreturn' function with side
1069 effects. */
1070 TREE_THIS_VOLATILE (soft_abstractmethod_node) = 1;
1071 TREE_SIDE_EFFECTS (soft_abstractmethod_node) = 1;
1073 soft_nosuchfield_node
1074 = add_builtin_function ("_Jv_ThrowNoSuchFieldError", t,
1075 0, NOT_BUILT_IN, NULL, NULL_TREE);
1076 /* Mark soft_nosuchfield_node as a `noreturn' function with side
1077 effects. */
1078 TREE_THIS_VOLATILE (soft_nosuchfield_node) = 1;
1079 TREE_SIDE_EFFECTS (soft_nosuchfield_node) = 1;
1081 t = build_function_type_list (ptr_type_node,
1082 class_ptr_type, object_ptr_type_node,
1083 NULL_TREE);
1084 soft_checkcast_node
1085 = add_builtin_function ("_Jv_CheckCast", t,
1086 0, NOT_BUILT_IN, NULL, NULL_TREE);
1087 t = build_function_type_list (boolean_type_node,
1088 object_ptr_type_node, class_ptr_type,
1089 NULL_TREE);
1090 soft_instanceof_node
1091 = add_builtin_function ("_Jv_IsInstanceOf", t,
1092 0, NOT_BUILT_IN, NULL, NULL_TREE);
1093 DECL_PURE_P (soft_instanceof_node) = 1;
1094 t = build_function_type_list (void_type_node,
1095 object_ptr_type_node, object_ptr_type_node,
1096 NULL_TREE);
1097 soft_checkarraystore_node
1098 = add_builtin_function ("_Jv_CheckArrayStore", t,
1099 0, NOT_BUILT_IN, NULL, NULL_TREE);
1100 t = build_function_type_list (ptr_type_node,
1101 ptr_type_node, ptr_type_node, int_type_node,
1102 NULL_TREE);
1103 soft_lookupinterfacemethod_node
1104 = add_builtin_function ("_Jv_LookupInterfaceMethodIdx", t,
1105 0, NOT_BUILT_IN, NULL, NULL_TREE);
1106 DECL_PURE_P (soft_lookupinterfacemethod_node) = 1;
1108 t = build_function_type_list (ptr_type_node,
1109 ptr_type_node, ptr_type_node, ptr_type_node,
1110 NULL_TREE);
1111 soft_lookupinterfacemethodbyname_node
1112 = add_builtin_function ("_Jv_LookupInterfaceMethod", t,
1113 0, NOT_BUILT_IN, NULL, NULL_TREE);
1114 t = build_function_type_list (ptr_type_node,
1115 object_ptr_type_node, ptr_type_node,
1116 ptr_type_node, int_type_node, NULL_TREE);
1117 soft_lookupjnimethod_node
1118 = add_builtin_function ("_Jv_LookupJNIMethod", t,
1119 0, NOT_BUILT_IN, NULL, NULL_TREE);
1120 t = build_function_type_list (ptr_type_node, ptr_type_node, NULL_TREE);
1121 soft_getjnienvnewframe_node
1122 = add_builtin_function ("_Jv_GetJNIEnvNewFrame", t,
1123 0, NOT_BUILT_IN, NULL, NULL_TREE);
1124 t = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE);
1125 soft_jnipopsystemframe_node
1126 = add_builtin_function ("_Jv_JNI_PopSystemFrame", t,
1127 0, NOT_BUILT_IN, NULL, NULL_TREE);
1129 t = build_function_type_list (object_ptr_type_node,
1130 object_ptr_type_node, NULL_TREE);
1131 soft_unwrapjni_node
1132 = add_builtin_function ("_Jv_UnwrapJNIweakReference", t,
1133 0, NOT_BUILT_IN, NULL, NULL_TREE);
1135 t = build_function_type_list (int_type_node,
1136 int_type_node, int_type_node, NULL_TREE);
1137 soft_idiv_node
1138 = add_builtin_function ("_Jv_divI", t,
1139 0, NOT_BUILT_IN, NULL, NULL_TREE);
1141 soft_irem_node
1142 = add_builtin_function ("_Jv_remI", t,
1143 0, NOT_BUILT_IN, NULL, NULL_TREE);
1145 t = build_function_type_list (long_type_node,
1146 long_type_node, long_type_node, NULL_TREE);
1147 soft_ldiv_node
1148 = add_builtin_function ("_Jv_divJ", t,
1149 0, NOT_BUILT_IN, NULL, NULL_TREE);
1151 soft_lrem_node
1152 = add_builtin_function ("_Jv_remJ", t,
1153 0, NOT_BUILT_IN, NULL, NULL_TREE);
1155 initialize_builtins ();
1157 soft_fmod_node = builtin_decl_explicit (BUILT_IN_FMOD);
1159 parse_version ();
1163 /* Look up NAME in the current binding level and its superiors
1164 in the namespace of variables, functions and typedefs.
1165 Return a ..._DECL node of some kind representing its definition,
1166 or return 0 if it is undefined. */
1168 tree
1169 lookup_name (tree name)
1171 tree val;
1172 if (current_binding_level != global_binding_level
1173 && IDENTIFIER_LOCAL_VALUE (name))
1174 val = IDENTIFIER_LOCAL_VALUE (name);
1175 else
1176 val = IDENTIFIER_GLOBAL_VALUE (name);
1177 return val;
1180 /* Similar to `lookup_name' but look only at current binding level and
1181 the previous one if it's the parameter level. */
1183 static tree
1184 lookup_name_current_level (tree name)
1186 tree t;
1188 if (current_binding_level == global_binding_level)
1189 return IDENTIFIER_GLOBAL_VALUE (name);
1191 if (IDENTIFIER_LOCAL_VALUE (name) == 0)
1192 return 0;
1194 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
1195 if (DECL_NAME (t) == name)
1196 break;
1198 return t;
1201 /* Record a decl-node X as belonging to the current lexical scope.
1202 Check for errors (such as an incompatible declaration for the same
1203 name already seen in the same scope).
1205 Returns either X or an old decl for the same name.
1206 If an old decl is returned, it may have been smashed
1207 to agree with what X says. */
1209 tree
1210 pushdecl (tree x)
1212 tree t;
1213 tree name = DECL_NAME (x);
1214 struct binding_level *b = current_binding_level;
1216 if (TREE_CODE (x) != TYPE_DECL)
1217 DECL_CONTEXT (x) = current_function_decl;
1218 if (name)
1220 t = lookup_name_current_level (name);
1221 if (t != 0 && t == error_mark_node)
1222 /* error_mark_node is 0 for a while during initialization! */
1224 t = 0;
1225 error ("%q+D used prior to declaration", x);
1228 /* If we're naming a hitherto-unnamed type, set its TYPE_NAME
1229 to point to the TYPE_DECL.
1230 Since Java does not have typedefs, a type can only have
1231 one (true) name, given by a class, interface, or builtin. */
1232 if (TREE_CODE (x) == TYPE_DECL
1233 && TYPE_NAME (TREE_TYPE (x)) == 0
1234 && TREE_TYPE (x) != error_mark_node)
1236 TYPE_NAME (TREE_TYPE (x)) = x;
1237 TYPE_STUB_DECL (TREE_TYPE (x)) = x;
1240 /* This name is new in its binding level.
1241 Install the new declaration and return it. */
1242 if (b == global_binding_level)
1244 /* Install a global value. */
1246 IDENTIFIER_GLOBAL_VALUE (name) = x;
1248 else
1250 /* Here to install a non-global value. */
1251 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
1252 IDENTIFIER_LOCAL_VALUE (name) = x;
1254 /* If storing a local value, there may already be one (inherited).
1255 If so, record it for restoration when this binding level ends. */
1256 if (oldlocal != 0)
1257 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
1261 /* Put decls on list in reverse order.
1262 We will reverse them later if necessary. */
1263 DECL_CHAIN (x) = b->names;
1264 b->names = x;
1266 return x;
1269 void
1270 pushdecl_force_head (tree x)
1272 current_binding_level->names = x;
1275 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL, if appropriate. */
1277 tree
1278 pushdecl_top_level (tree x)
1280 tree t;
1281 struct binding_level *b = current_binding_level;
1283 current_binding_level = global_binding_level;
1284 t = pushdecl (x);
1285 current_binding_level = b;
1286 return t;
1289 /* Like pushdecl, only it places X in FUNCTION_BINDING_LEVEL, if appropriate. */
1291 tree
1292 pushdecl_function_level (tree x)
1294 tree t;
1295 struct binding_level *b = current_binding_level;
1297 current_binding_level = function_binding_level;
1298 t = pushdecl (x);
1299 current_binding_level = b;
1300 return t;
1303 /* Return true if we are in the global binding level. */
1305 bool
1306 global_bindings_p (void)
1308 return current_binding_level == global_binding_level;
1311 /* Return the list of declarations of the current level.
1312 Note that this list is in reverse order unless/until
1313 you nreverse it; and when you do nreverse it, you must
1314 store the result back using `storedecls' or you will lose. */
1316 tree
1317 getdecls (void)
1319 return current_binding_level->names;
1322 /* Create a new `struct binding_level'. */
1324 static struct binding_level *
1325 make_binding_level (void)
1327 /* NOSTRICT */
1328 return ggc_cleared_alloc<binding_level> ();
1331 void
1332 pushlevel (int unused ATTRIBUTE_UNUSED)
1334 struct binding_level *newlevel = NULL_BINDING_LEVEL;
1336 /* Reuse or create a struct for this binding level. */
1338 if (free_binding_level)
1340 newlevel = free_binding_level;
1341 free_binding_level = free_binding_level->level_chain;
1343 else
1345 newlevel = make_binding_level ();
1348 /* Add this level to the front of the chain (stack) of levels that
1349 are active. */
1351 *newlevel = clear_binding_level;
1352 newlevel->level_chain = current_binding_level;
1353 newlevel->loc = input_location;
1354 current_binding_level = newlevel;
1355 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1356 newlevel->binding_depth = binding_depth;
1357 indent ();
1358 fprintf (stderr, "push %s level %p pc %d\n",
1359 (is_class_level) ? "class" : "block", newlevel, current_pc);
1360 is_class_level = 0;
1361 binding_depth++;
1362 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1365 /* Exit a binding level.
1366 Pop the level off, and restore the state of the identifier-decl mappings
1367 that were in effect when this level was entered.
1369 If KEEP is nonzero, this level had explicit declarations, so
1370 and create a "block" (a BLOCK node) for the level
1371 to record its declarations and subblocks for symbol table output.
1373 If FUNCTIONBODY is nonzero, this level is the body of a function,
1374 so create a block as if KEEP were set and also clear out all
1375 label names.
1377 If REVERSE is nonzero, reverse the order of decls before putting
1378 them into the BLOCK. */
1380 tree
1381 poplevel (int keep, int reverse, int functionbody)
1383 tree link;
1384 /* The chain of decls was accumulated in reverse order.
1385 Put it into forward order, just for cleanliness. */
1386 tree decls;
1387 tree subblocks = current_binding_level->blocks;
1388 tree block = 0;
1389 tree decl;
1390 tree bind = 0;
1392 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1393 binding_depth--;
1394 indent ();
1395 if (current_binding_level->end_pc != LARGEST_PC)
1396 fprintf (stderr, "pop %s level %p pc %d (end pc %d)\n",
1397 (is_class_level) ? "class" : "block", current_binding_level, current_pc,
1398 current_binding_level->end_pc);
1399 else
1400 fprintf (stderr, "pop %s level %p pc %d\n",
1401 (is_class_level) ? "class" : "block", current_binding_level, current_pc);
1402 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1404 /* Get the decls in the order they were written.
1405 Usually current_binding_level->names is in reverse order.
1406 But parameter decls were previously put in forward order. */
1408 if (reverse)
1409 current_binding_level->names
1410 = decls = nreverse (current_binding_level->names);
1411 else
1412 decls = current_binding_level->names;
1414 for (decl = decls; decl; decl = DECL_CHAIN (decl))
1415 if (TREE_CODE (decl) == VAR_DECL
1416 && DECL_LANG_SPECIFIC (decl) != NULL
1417 && DECL_LOCAL_SLOT_NUMBER (decl))
1418 LOCAL_VAR_OUT_OF_SCOPE_P (decl) = 1;
1420 /* If there were any declarations in that level,
1421 or if this level is a function body,
1422 create a BLOCK to record them for the life of this function. */
1424 block = 0;
1425 if (keep || functionbody)
1426 block = make_node (BLOCK);
1428 if (current_binding_level->exception_range)
1429 expand_end_java_handler (current_binding_level->exception_range);
1431 if (block != 0)
1433 /* If any statements have been generated at this level, create a
1434 BIND_EXPR to hold them and copy the variables to it. This
1435 only applies to the bytecode compiler. */
1436 if (current_binding_level->stmts)
1438 tree decl = decls;
1439 tree *var = &BLOCK_VARS (block);
1441 /* Copy decls from names list, ignoring labels. */
1442 while (decl)
1444 tree next = DECL_CHAIN (decl);
1445 if (TREE_CODE (decl) != LABEL_DECL)
1447 *var = decl;
1448 var = &DECL_CHAIN (decl);
1450 decl = next;
1452 *var = NULL;
1454 bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
1455 BLOCK_EXPR_BODY (block), block);
1456 BIND_EXPR_BODY (bind) = current_binding_level->stmts;
1458 if (BIND_EXPR_BODY (bind)
1459 && TREE_SIDE_EFFECTS (BIND_EXPR_BODY (bind)))
1460 TREE_SIDE_EFFECTS (bind) = 1;
1462 /* FIXME: gimplifier brain damage. */
1463 if (BIND_EXPR_BODY (bind) == NULL)
1464 BIND_EXPR_BODY (bind) = build_java_empty_stmt ();
1466 SET_EXPR_LOCATION (bind, current_binding_level->loc);
1468 current_binding_level->stmts = NULL;
1470 else
1472 BLOCK_VARS (block) = decls;
1474 BLOCK_SUBBLOCKS (block) = subblocks;
1477 /* In each subblock, record that this is its superior. */
1479 for (link = subblocks; link; link = BLOCK_CHAIN (link))
1480 BLOCK_SUPERCONTEXT (link) = block;
1482 /* Clear out the meanings of the local variables of this level. */
1484 for (link = decls; link; link = DECL_CHAIN (link))
1486 tree name = DECL_NAME (link);
1487 if (name != 0 && IDENTIFIER_LOCAL_VALUE (name) == link)
1489 /* If the ident. was used or addressed via a local extern decl,
1490 don't forget that fact. */
1491 if (DECL_EXTERNAL (link))
1493 if (TREE_USED (link))
1494 TREE_USED (name) = 1;
1495 if (TREE_ADDRESSABLE (link))
1496 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1498 IDENTIFIER_LOCAL_VALUE (name) = 0;
1502 /* Restore all name-meanings of the outer levels
1503 that were shadowed by this level. */
1505 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1506 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
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. */
1512 if (functionbody)
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. */
1519 BLOCK_VARS (block) = 0;
1522 /* Pop the current level, and free the structure for reuse. */
1525 struct binding_level *level = current_binding_level;
1526 current_binding_level = current_binding_level->level_chain;
1528 level->level_chain = free_binding_level;
1529 free_binding_level = level;
1532 /* Dispose of the block that we just made inside some higher level. */
1533 if (functionbody)
1535 DECL_INITIAL (current_function_decl) = block;
1536 DECL_SAVED_TREE (current_function_decl) = bind;
1538 else
1540 if (block)
1542 current_binding_level->blocks
1543 = block_chainon (current_binding_level->blocks, block);
1545 /* If we did not make a block for the level just exited,
1546 any blocks made for inner levels
1547 (since they cannot be recorded as subblocks in that level)
1548 must be carried forward so they will later become subblocks
1549 of something else. */
1550 else if (subblocks)
1551 current_binding_level->blocks
1552 = block_chainon (current_binding_level->blocks, subblocks);
1554 if (bind)
1555 java_add_stmt (bind);
1558 if (block)
1559 TREE_USED (block) = 1;
1560 return block;
1563 void
1564 maybe_pushlevels (int pc)
1566 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1567 current_pc = pc;
1568 #endif
1570 while (pending_local_decls != NULL_TREE &&
1571 DECL_LOCAL_START_PC (pending_local_decls) <= pc)
1573 tree *ptr = &pending_local_decls;
1574 tree decl = *ptr, next;
1575 int end_pc = DECL_LOCAL_END_PC (decl);
1577 while (*ptr != NULL_TREE
1578 && DECL_LOCAL_START_PC (*ptr) <= pc
1579 && DECL_LOCAL_END_PC (*ptr) == end_pc)
1580 ptr = &DECL_CHAIN (*ptr);
1581 pending_local_decls = *ptr;
1582 *ptr = NULL_TREE;
1584 /* Force non-nested range to be nested in current range by
1585 truncating variable lifetimes. */
1586 if (end_pc > current_binding_level->end_pc)
1588 tree t;
1589 end_pc = current_binding_level->end_pc;
1590 for (t = decl; t != NULL_TREE; t = DECL_CHAIN (t))
1591 DECL_LOCAL_END_PC (t) = end_pc;
1594 maybe_start_try (pc, end_pc);
1596 pushlevel (1);
1598 current_binding_level->end_pc = end_pc;
1599 current_binding_level->start_pc = pc;
1600 current_binding_level->names = NULL;
1601 for ( ; decl != NULL_TREE; decl = next)
1603 int index = DECL_LOCAL_SLOT_NUMBER (decl);
1604 tree base_decl;
1605 next = DECL_CHAIN (decl);
1606 push_jvm_slot (index, decl);
1607 pushdecl (decl);
1608 base_decl
1609 = find_local_variable (index, TREE_TYPE (decl), pc);
1610 if (TREE_CODE (TREE_TYPE (base_decl)) == POINTER_TYPE)
1611 base_decl = TREE_VEC_ELT (base_decl_map, index);
1612 SET_DECL_VALUE_EXPR (decl, base_decl);
1613 DECL_HAS_VALUE_EXPR_P (decl) = 1;
1617 maybe_start_try (pc, 0);
1620 void
1621 maybe_poplevels (int pc)
1623 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1624 current_pc = pc;
1625 #endif
1627 /* FIXME: I'm pretty sure that this is wrong. Variable scopes are
1628 inclusive, so a variable is live if pc == end_pc. Here, we
1629 terminate a range if the current pc is equal to the end of the
1630 range, and this is *before* we have generated code for the
1631 instruction at end_pc. We're closing a binding level one
1632 instruction too early.*/
1633 while (current_binding_level->end_pc <= pc)
1634 poplevel (1, 0, 0);
1637 /* Terminate any binding which began during the range beginning at
1638 start_pc. This tidies up improperly nested local variable ranges
1639 and exception handlers; a variable declared within an exception
1640 range is forcibly terminated when that exception ends. */
1642 void
1643 force_poplevels (int start_pc)
1645 while (current_binding_level->start_pc > start_pc)
1647 if (pedantic && current_binding_level->start_pc > start_pc)
1648 warning (0, "In %+D: overlapped variable and exception ranges at %d",
1649 current_function_decl,
1650 current_binding_level->start_pc);
1651 poplevel (1, 0, 0);
1655 /* integrate_decl_tree calls this function. */
1657 void
1658 java_dup_lang_specific_decl (tree node)
1660 int lang_decl_size;
1661 struct lang_decl *x;
1663 if (!DECL_LANG_SPECIFIC (node))
1664 return;
1666 lang_decl_size = sizeof (struct lang_decl);
1667 x = ggc_alloc<struct lang_decl> ();
1668 memcpy (x, DECL_LANG_SPECIFIC (node), lang_decl_size);
1669 DECL_LANG_SPECIFIC (node) = x;
1672 void
1673 give_name_to_locals (JCF *jcf)
1675 int i, n = DECL_LOCALVARIABLES_OFFSET (current_function_decl);
1676 int code_offset = DECL_CODE_OFFSET (current_function_decl);
1677 tree parm;
1678 pending_local_decls = NULL_TREE;
1679 if (n == 0)
1680 return;
1681 JCF_SEEK (jcf, n);
1682 n = JCF_readu2 (jcf);
1683 for (i = 0; i < n; i++)
1685 int start_pc = JCF_readu2 (jcf);
1686 int length = JCF_readu2 (jcf);
1687 int name_index = JCF_readu2 (jcf);
1688 int signature_index = JCF_readu2 (jcf);
1689 int slot = JCF_readu2 (jcf);
1690 tree name = get_name_constant (jcf, name_index);
1691 tree type = parse_signature (jcf, signature_index);
1692 if (slot < DECL_ARG_SLOT_COUNT (current_function_decl)
1693 && start_pc == 0
1694 && length == DECL_CODE_LENGTH (current_function_decl))
1696 tree decl = TREE_VEC_ELT (decl_map, slot);
1697 DECL_NAME (decl) = name;
1698 if (TREE_CODE (decl) != PARM_DECL || TREE_TYPE (decl) != type)
1699 warning (0, "bad type in parameter debug info");
1701 else
1703 tree *ptr;
1704 int end_pc = start_pc + length;
1705 tree decl = build_decl (input_location, VAR_DECL, name, type);
1706 if (end_pc > DECL_CODE_LENGTH (current_function_decl))
1708 warning (0, "bad PC range for debug info for local %q+D",
1709 decl);
1710 end_pc = DECL_CODE_LENGTH (current_function_decl);
1713 /* Adjust start_pc if necessary so that the local's first
1714 store operation will use the relevant DECL as a
1715 destination. Fore more information, read the leading
1716 comments for expr.c:maybe_adjust_start_pc. */
1717 start_pc = maybe_adjust_start_pc (jcf, code_offset, start_pc, slot);
1719 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
1720 DECL_LOCAL_SLOT_NUMBER (decl) = slot;
1721 DECL_LOCAL_START_PC (decl) = start_pc;
1722 DECL_LOCAL_END_PC (decl) = end_pc;
1724 /* Now insert the new decl in the proper place in
1725 pending_local_decls. We are essentially doing an insertion sort,
1726 which works fine, since the list input will normally already
1727 be sorted. */
1728 ptr = &pending_local_decls;
1729 while (*ptr != NULL_TREE
1730 && (DECL_LOCAL_START_PC (*ptr) > start_pc
1731 || (DECL_LOCAL_START_PC (*ptr) == start_pc
1732 && DECL_LOCAL_END_PC (*ptr) < end_pc)))
1733 ptr = &DECL_CHAIN (*ptr);
1734 DECL_CHAIN (decl) = *ptr;
1735 *ptr = decl;
1739 pending_local_decls = nreverse (pending_local_decls);
1741 /* Fill in default names for the parameters. */
1742 for (parm = DECL_ARGUMENTS (current_function_decl), i = 0;
1743 parm != NULL_TREE; parm = DECL_CHAIN (parm), i++)
1745 if (DECL_NAME (parm) == NULL_TREE)
1747 int arg_i = METHOD_STATIC (current_function_decl) ? i+1 : i;
1748 if (arg_i == 0)
1749 DECL_NAME (parm) = get_identifier ("this");
1750 else
1752 char buffer[12];
1753 sprintf (buffer, "ARG_%d", arg_i);
1754 DECL_NAME (parm) = get_identifier (buffer);
1760 tree
1761 build_result_decl (tree fndecl)
1763 tree restype = TREE_TYPE (TREE_TYPE (fndecl));
1764 tree result = DECL_RESULT (fndecl);
1765 if (! result)
1767 result = build_decl (DECL_SOURCE_LOCATION (fndecl),
1768 RESULT_DECL, NULL_TREE, restype);
1769 DECL_ARTIFICIAL (result) = 1;
1770 DECL_IGNORED_P (result) = 1;
1771 DECL_CONTEXT (result) = fndecl;
1772 DECL_RESULT (fndecl) = result;
1774 return result;
1777 void
1778 start_java_method (tree fndecl)
1780 tree tem, *ptr;
1781 int i;
1783 uniq = 0;
1785 current_function_decl = fndecl;
1786 announce_function (fndecl);
1788 i = DECL_MAX_LOCALS(fndecl) + DECL_MAX_STACK(fndecl);
1789 decl_map = make_tree_vec (i);
1790 base_decl_map = make_tree_vec (i);
1791 type_map = XRESIZEVEC (tree, type_map, i);
1793 #if defined(DEBUG_JAVA_BINDING_LEVELS)
1794 fprintf (stderr, "%s:\n", lang_printable_name (fndecl, 2));
1795 current_pc = 0;
1796 #endif /* defined(DEBUG_JAVA_BINDING_LEVELS) */
1797 pushlevel (1); /* Push parameters. */
1799 ptr = &DECL_ARGUMENTS (fndecl);
1800 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
1801 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
1803 tree parm_name = NULL_TREE, parm_decl;
1804 tree parm_type = TREE_VALUE (tem);
1805 gcc_assert (i < DECL_MAX_LOCALS (fndecl));
1807 parm_decl = build_decl (input_location, PARM_DECL, parm_name, parm_type);
1808 DECL_CONTEXT (parm_decl) = fndecl;
1809 if (targetm.calls.promote_prototypes (parm_type)
1810 && TYPE_PRECISION (parm_type) < TYPE_PRECISION (integer_type_node)
1811 && INTEGRAL_TYPE_P (parm_type))
1812 parm_type = integer_type_node;
1813 DECL_ARG_TYPE (parm_decl) = parm_type;
1815 *ptr = parm_decl;
1816 ptr = &DECL_CHAIN (parm_decl);
1818 /* Add parm_decl to the decl_map. */
1819 push_jvm_slot (i, parm_decl);
1821 /* The this parameter of methods is artificial. */
1822 if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE && i == 0)
1823 DECL_ARTIFICIAL (parm_decl) = 1;
1825 type_map[i] = TREE_TYPE (parm_decl);
1826 if (TYPE_IS_WIDE (TREE_TYPE (parm_decl)))
1828 i++;
1829 type_map[i] = void_type_node;
1832 *ptr = NULL_TREE;
1833 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
1835 while (i < DECL_MAX_LOCALS(fndecl))
1836 type_map[i++] = NULL_TREE;
1838 build_result_decl (fndecl);
1839 DECL_SOURCE_LOCATION (fndecl) = input_location;
1841 /* Push local variables. */
1842 pushlevel (2);
1844 function_binding_level = current_binding_level;
1847 void
1848 end_java_method (void)
1850 tree fndecl = current_function_decl;
1852 /* pop out of function */
1853 poplevel (1, 1, 0);
1855 /* pop out of its parameters */
1856 poplevel (1, 0, 1);
1858 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
1860 if (DECL_SAVED_TREE (fndecl))
1862 tree fbody, block_body;
1863 /* Before we check initialization, attached all class initialization
1864 variable to the block_body */
1865 fbody = DECL_SAVED_TREE (fndecl);
1866 block_body = BIND_EXPR_BODY (fbody);
1867 hash_table<treetreehasher> *ht = DECL_FUNCTION_INIT_TEST_TABLE (fndecl);
1868 ht->traverse<tree, attach_init_test_initialization_flags> (block_body);
1871 finish_method (fndecl);
1873 current_function_decl = NULL_TREE;
1874 base_decl_map = NULL_TREE;
1877 /* Prepare a method for expansion. */
1879 void
1880 finish_method (tree fndecl)
1882 tree *tp = &DECL_SAVED_TREE (fndecl);
1884 /* Wrap body of synchronized methods in a monitorenter,
1885 plus monitorexit cleanup. */
1886 if (METHOD_SYNCHRONIZED (fndecl))
1888 tree enter, exit, lock;
1889 if (METHOD_STATIC (fndecl))
1890 lock = build_class_ref (DECL_CONTEXT (fndecl));
1891 else
1892 lock = DECL_ARGUMENTS (fndecl);
1893 BUILD_MONITOR_ENTER (enter, lock);
1894 BUILD_MONITOR_EXIT (exit, lock);
1895 *tp = build2 (COMPOUND_EXPR, void_type_node, enter,
1896 build2 (TRY_FINALLY_EXPR, void_type_node, *tp, exit));
1899 /* Convert function tree to GENERIC prior to inlining. */
1900 java_genericize (fndecl);
1902 /* Store the end of the function, so that we get good line number
1903 info for the epilogue. */
1904 if (DECL_STRUCT_FUNCTION (fndecl))
1905 set_cfun (DECL_STRUCT_FUNCTION (fndecl));
1906 else
1907 allocate_struct_function (fndecl, false);
1908 cfun->function_end_locus = DECL_FUNCTION_LAST_LINE (fndecl);
1910 /* Defer inlining and expansion to the cgraph optimizers. */
1911 cgraph_node::finalize_function (fndecl, false);
1914 /* We pessimistically marked all methods and fields external until we
1915 knew what set of classes we were planning to compile. Now mark those
1916 associated with CLASS to be generated locally as not external. */
1918 static void
1919 java_mark_decl_local (tree decl)
1921 DECL_EXTERNAL (decl) = 0;
1923 #ifdef ENABLE_CHECKING
1924 /* Double check that we didn't pass the function to the callgraph early. */
1925 if (TREE_CODE (decl) == FUNCTION_DECL)
1927 struct cgraph_node *node = cgraph_node::get (decl);
1928 gcc_assert (!node || !node->definition);
1930 #endif
1931 gcc_assert (!DECL_RTL_SET_P (decl));
1934 /* Given appropriate target support, G++ will emit hidden aliases for native
1935 methods. Using this hidden name is required for proper operation of
1936 _Jv_Method::ncode, but it doesn't hurt to use it everywhere. Look for
1937 proper target support, then mark the method for aliasing. */
1939 static void
1940 java_mark_cni_decl_local (tree decl)
1942 #if !defined(HAVE_GAS_HIDDEN) || !defined(ASM_OUTPUT_DEF)
1943 return;
1944 #endif
1946 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1947 DECL_LOCAL_CNI_METHOD_P (decl) = 1;
1949 /* Setting DECL_LOCAL_CNI_METHOD_P changes the behavior of the
1950 mangler. We might have already referenced this native method and
1951 therefore created its name, but even if we have it won't hurt.
1952 We'll just go via its externally visible name, rather than its
1953 hidden alias. However, we must force things so that the correct
1954 mangling is done. */
1956 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1957 java_mangle_decl (decl);
1958 if (DECL_RTL_SET_P (decl))
1960 SET_DECL_RTL (decl, 0);
1961 make_decl_rtl (decl);
1965 /* Use the preceding two functions and mark all members of the class. */
1967 void
1968 java_mark_class_local (tree klass)
1970 tree t;
1972 for (t = TYPE_FIELDS (klass); t ; t = DECL_CHAIN (t))
1973 if (FIELD_STATIC (t))
1975 if (DECL_EXTERNAL (t))
1976 vec_safe_push (pending_static_fields, t);
1977 java_mark_decl_local (t);
1980 for (t = TYPE_METHODS (klass); t ; t = DECL_CHAIN (t))
1981 if (!METHOD_ABSTRACT (t))
1983 if (METHOD_NATIVE (t) && !flag_jni)
1984 java_mark_cni_decl_local (t);
1985 else
1986 java_mark_decl_local (t);
1990 /* Add a statement to a compound_expr. */
1992 tree
1993 add_stmt_to_compound (tree existing, tree type, tree stmt)
1995 if (!stmt)
1996 return existing;
1997 else if (existing)
1999 tree expr = build2 (COMPOUND_EXPR, type, existing, stmt);
2000 TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (existing)
2001 | TREE_SIDE_EFFECTS (stmt);
2002 return expr;
2004 else
2005 return stmt;
2008 /* If this node is an expr, mark its input location. Called from
2009 walk_tree(). */
2011 static tree
2012 set_input_location (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
2013 void *data ATTRIBUTE_UNUSED)
2015 tree t = *tp;
2017 if (CAN_HAVE_LOCATION_P (t))
2019 if (EXPR_HAS_LOCATION(t))
2020 return t; /* Don't walk any further into this expr. */
2021 else
2022 SET_EXPR_LOCATION (t, input_location);
2025 return NULL_TREE; /* Continue walking this expr. */
2028 /* Add a statement to the statement_list currently being constructed.
2029 If the statement_list is null, we don't create a singleton list.
2030 This is necessary because poplevel() assumes that adding a
2031 statement to a null statement_list returns the statement. */
2033 tree
2034 java_add_stmt (tree new_stmt)
2036 tree stmts = current_binding_level->stmts;
2037 tree_stmt_iterator i;
2039 if (LOCATION_FILE (input_location))
2040 walk_tree (&new_stmt, set_input_location, NULL, NULL);
2042 if (stmts == NULL)
2043 return current_binding_level->stmts = new_stmt;
2045 /* Force STMTS to be a statement_list. */
2046 if (TREE_CODE (stmts) != STATEMENT_LIST)
2048 tree t = make_node (STATEMENT_LIST);
2049 i = tsi_last (t);
2050 tsi_link_after (&i, stmts, TSI_CONTINUE_LINKING);
2051 stmts = t;
2054 i = tsi_last (stmts);
2055 tsi_link_after (&i, new_stmt, TSI_CONTINUE_LINKING);
2056 TREE_TYPE (stmts) = void_type_node;
2058 return current_binding_level->stmts = stmts;
2061 /* Add a variable to the current scope. */
2063 tree
2064 java_add_local_var (tree decl)
2066 tree *vars = &current_binding_level->names;
2067 tree next = *vars;
2068 DECL_CHAIN (decl) = next;
2069 *vars = decl;
2070 DECL_CONTEXT (decl) = current_function_decl;
2071 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2072 return decl;
2075 /* Return a pointer to the compound_expr currently being
2076 constructed. */
2078 tree *
2079 get_stmts (void)
2081 return &current_binding_level->stmts;
2084 /* Register an exception range as belonging to the current binding
2085 level. There may only be one: if there are more, we'll create more
2086 binding levels. However, each range can have multiple handlers,
2087 and these are expanded when we call expand_end_java_handler(). */
2089 void
2090 register_exception_range (struct eh_range *range, int pc, int end_pc)
2092 gcc_assert (! current_binding_level->exception_range);
2093 current_binding_level->exception_range = range;
2094 current_binding_level->end_pc = end_pc;
2095 current_binding_level->start_pc = pc;
2098 #include "gt-java-decl.h"