Put RTTI entries at negative offsets in new ABI.
[gcc.git] / gcc / cp / tree.c
1 /* Language-dependent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GNU CC.
7
8 GNU CC 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 GNU CC 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 GNU CC; 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 #include "config.h"
24 #include "system.h"
25 #include "obstack.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "flags.h"
29 #include "rtl.h"
30 #include "toplev.h"
31 #include "ggc.h"
32 #include "insn-config.h"
33 #include "integrate.h"
34
35 static tree bot_manip PARAMS ((tree *, int *, void *));
36 static tree bot_replace PARAMS ((tree *, int *, void *));
37 static tree build_cplus_array_type_1 PARAMS ((tree, tree));
38 static void list_hash_add PARAMS ((int, tree));
39 static int list_hash PARAMS ((tree, tree, tree));
40 static tree list_hash_lookup PARAMS ((int, tree, tree, tree));
41 static cp_lvalue_kind lvalue_p_1 PARAMS ((tree, int));
42 static tree no_linkage_helper PARAMS ((tree *, int *, void *));
43 static tree build_srcloc PARAMS ((char *, int));
44 static void mark_list_hash PARAMS ((void *));
45 static int statement_code_p PARAMS ((enum tree_code));
46 static tree mark_local_for_remap_r PARAMS ((tree *, int *, void *));
47 static tree cp_unsave_r PARAMS ((tree *, int *, void *));
48 static void cp_unsave PARAMS ((tree *));
49 static tree build_target_expr PARAMS ((tree, tree));
50
51 /* If REF is an lvalue, returns the kind of lvalue that REF is.
52 Otherwise, returns clk_none. If TREAT_CLASS_RVALUES_AS_LVALUES is
53 non-zero, rvalues of class type are considered lvalues. */
54
55 static cp_lvalue_kind
56 lvalue_p_1 (ref, treat_class_rvalues_as_lvalues)
57 tree ref;
58 int treat_class_rvalues_as_lvalues;
59 {
60 cp_lvalue_kind op1_lvalue_kind = clk_none;
61 cp_lvalue_kind op2_lvalue_kind = clk_none;
62
63 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
64 return clk_ordinary;
65
66 if (ref == current_class_ptr && flag_this_is_variable <= 0)
67 return clk_none;
68
69 switch (TREE_CODE (ref))
70 {
71 /* preincrements and predecrements are valid lvals, provided
72 what they refer to are valid lvals. */
73 case PREINCREMENT_EXPR:
74 case PREDECREMENT_EXPR:
75 case SAVE_EXPR:
76 case UNSAVE_EXPR:
77 case TRY_CATCH_EXPR:
78 case WITH_CLEANUP_EXPR:
79 case REALPART_EXPR:
80 case IMAGPART_EXPR:
81 case NOP_EXPR:
82 return lvalue_p_1 (TREE_OPERAND (ref, 0),
83 treat_class_rvalues_as_lvalues);
84
85 case COMPONENT_REF:
86 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
87 treat_class_rvalues_as_lvalues);
88 if (op1_lvalue_kind
89 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
90 situations. */
91 && TREE_CODE (TREE_OPERAND (ref, 1)) == FIELD_DECL
92 && DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
93 {
94 /* Clear the ordinary bit. If this object was a class
95 rvalue we want to preserve that information. */
96 op1_lvalue_kind &= ~clk_ordinary;
97 /* The lvalue is for a btifield. */
98 op1_lvalue_kind |= clk_bitfield;
99 }
100 return op1_lvalue_kind;
101
102 case STRING_CST:
103 return clk_ordinary;
104
105 case VAR_DECL:
106 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
107 && DECL_LANG_SPECIFIC (ref)
108 && DECL_IN_AGGR_P (ref))
109 return clk_none;
110 case INDIRECT_REF:
111 case ARRAY_REF:
112 case PARM_DECL:
113 case RESULT_DECL:
114 if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
115 return clk_ordinary;
116 break;
117
118 /* A currently unresolved scope ref. */
119 case SCOPE_REF:
120 my_friendly_abort (103);
121 case OFFSET_REF:
122 if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
123 return clk_ordinary;
124 /* Fall through. */
125 case MAX_EXPR:
126 case MIN_EXPR:
127 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
128 treat_class_rvalues_as_lvalues);
129 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
130 treat_class_rvalues_as_lvalues);
131 break;
132
133 case COND_EXPR:
134 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
135 treat_class_rvalues_as_lvalues);
136 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 2),
137 treat_class_rvalues_as_lvalues);
138 break;
139
140 case MODIFY_EXPR:
141 return clk_ordinary;
142
143 case COMPOUND_EXPR:
144 return lvalue_p_1 (TREE_OPERAND (ref, 1),
145 treat_class_rvalues_as_lvalues);
146
147 case TARGET_EXPR:
148 return treat_class_rvalues_as_lvalues ? clk_class : clk_none;
149
150 case CALL_EXPR:
151 case VA_ARG_EXPR:
152 return ((treat_class_rvalues_as_lvalues
153 && IS_AGGR_TYPE (TREE_TYPE (ref)))
154 ? clk_class : clk_none);
155
156 case FUNCTION_DECL:
157 /* All functions (except non-static-member functions) are
158 lvalues. */
159 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
160 ? clk_none : clk_ordinary);
161
162 default:
163 break;
164 }
165
166 /* If one operand is not an lvalue at all, then this expression is
167 not an lvalue. */
168 if (!op1_lvalue_kind || !op2_lvalue_kind)
169 return clk_none;
170
171 /* Otherwise, it's an lvalue, and it has all the odd properties
172 contributed by either operand. */
173 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
174 /* It's not an ordinary lvalue if it involves either a bit-field or
175 a class rvalue. */
176 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
177 op1_lvalue_kind &= ~clk_ordinary;
178 return op1_lvalue_kind;
179 }
180
181 /* If REF is an lvalue, returns the kind of lvalue that REF is.
182 Otherwise, returns clk_none. Lvalues can be assigned, unless they
183 have TREE_READONLY, or unless they are FUNCTION_DECLs. Lvalues can
184 have their address taken, unless they have DECL_REGISTER. */
185
186 cp_lvalue_kind
187 real_lvalue_p (ref)
188 tree ref;
189 {
190 return lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/0);
191 }
192
193 /* This differs from real_lvalue_p in that class rvalues are
194 considered lvalues. */
195
196 int
197 lvalue_p (ref)
198 tree ref;
199 {
200 return
201 (lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/1) != clk_none);
202 }
203
204 /* Return nonzero if REF is an lvalue valid for this language;
205 otherwise, print an error message and return zero. */
206
207 int
208 lvalue_or_else (ref, string)
209 tree ref;
210 const char *string;
211 {
212 int win = lvalue_p (ref);
213 if (! win)
214 error ("non-lvalue in %s", string);
215 return win;
216 }
217
218 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
219
220 static tree
221 build_target_expr (decl, value)
222 tree decl;
223 tree value;
224 {
225 tree t;
226
227 t = build (TARGET_EXPR, TREE_TYPE (decl), decl, value,
228 maybe_build_cleanup (decl), NULL_TREE);
229 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
230 ignore the TARGET_EXPR. If there really turn out to be no
231 side-effects, then the optimizer should be able to get rid of
232 whatever code is generated anyhow. */
233 TREE_SIDE_EFFECTS (t) = 1;
234
235 return t;
236 }
237
238 /* INIT is a CALL_EXPR which needs info about its target.
239 TYPE is the type that this initialization should appear to have.
240
241 Build an encapsulation of the initialization to perform
242 and return it so that it can be processed by language-independent
243 and language-specific expression expanders. */
244
245 tree
246 build_cplus_new (type, init)
247 tree type;
248 tree init;
249 {
250 tree fn;
251 tree slot;
252 tree rval;
253
254 /* Make sure that we're not trying to create an instance of an
255 abstract class. */
256 abstract_virtuals_error (NULL_TREE, type);
257
258 if (TREE_CODE (init) != CALL_EXPR && TREE_CODE (init) != AGGR_INIT_EXPR)
259 return convert (type, init);
260
261 slot = build (VAR_DECL, type);
262 DECL_ARTIFICIAL (slot) = 1;
263 DECL_CONTEXT (slot) = current_function_decl;
264 layout_decl (slot, 0);
265
266 /* We split the CALL_EXPR into its function and its arguments here.
267 Then, in expand_expr, we put them back together. The reason for
268 this is that this expression might be a default argument
269 expression. In that case, we need a new temporary every time the
270 expression is used. That's what break_out_target_exprs does; it
271 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
272 temporary slot. Then, expand_expr builds up a call-expression
273 using the new slot. */
274 fn = TREE_OPERAND (init, 0);
275 rval = build (AGGR_INIT_EXPR, type, fn, TREE_OPERAND (init, 1), slot);
276 TREE_SIDE_EFFECTS (rval) = 1;
277 AGGR_INIT_VIA_CTOR_P (rval)
278 = (TREE_CODE (fn) == ADDR_EXPR
279 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
280 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
281 rval = build_target_expr (slot, rval);
282
283 return rval;
284 }
285
286 /* Buidl a TARGET_EXPR using INIT to initialize a new temporary of the
287 indicated TYPE. */
288
289 tree
290 build_target_expr_with_type (init, type)
291 tree init;
292 tree type;
293 {
294 tree slot;
295 tree rval;
296
297 slot = build (VAR_DECL, type);
298 DECL_ARTIFICIAL (slot) = 1;
299 DECL_CONTEXT (slot) = current_function_decl;
300 layout_decl (slot, 0);
301 rval = build_target_expr (slot, init);
302
303 return rval;
304 }
305
306 /* Like build_target_expr_with_type, but use the type of INIT. */
307
308 tree
309 get_target_expr (init)
310 tree init;
311 {
312 return build_target_expr_with_type (init, TREE_TYPE (init));
313 }
314
315 /* Recursively search EXP for CALL_EXPRs that need cleanups and replace
316 these CALL_EXPRs with tree nodes that will perform the cleanups. */
317
318 tree
319 break_out_cleanups (exp)
320 tree exp;
321 {
322 tree tmp = exp;
323
324 if (TREE_CODE (tmp) == CALL_EXPR
325 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (tmp)))
326 return build_cplus_new (TREE_TYPE (tmp), tmp);
327
328 while (TREE_CODE (tmp) == NOP_EXPR
329 || TREE_CODE (tmp) == CONVERT_EXPR
330 || TREE_CODE (tmp) == NON_LVALUE_EXPR)
331 {
332 if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
333 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
334 {
335 TREE_OPERAND (tmp, 0)
336 = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
337 TREE_OPERAND (tmp, 0));
338 break;
339 }
340 else
341 tmp = TREE_OPERAND (tmp, 0);
342 }
343 return exp;
344 }
345
346 /* Recursively perform a preorder search EXP for CALL_EXPRs, making
347 copies where they are found. Returns a deep copy all nodes transitively
348 containing CALL_EXPRs. */
349
350 tree
351 break_out_calls (exp)
352 tree exp;
353 {
354 register tree t1, t2 = NULL_TREE;
355 register enum tree_code code;
356 register int changed = 0;
357 register int i;
358
359 if (exp == NULL_TREE)
360 return exp;
361
362 code = TREE_CODE (exp);
363
364 if (code == CALL_EXPR)
365 return copy_node (exp);
366
367 /* Don't try and defeat a save_expr, as it should only be done once. */
368 if (code == SAVE_EXPR)
369 return exp;
370
371 switch (TREE_CODE_CLASS (code))
372 {
373 default:
374 abort ();
375
376 case 'c': /* a constant */
377 case 't': /* a type node */
378 case 'x': /* something random, like an identifier or an ERROR_MARK. */
379 return exp;
380
381 case 'd': /* A decl node */
382 #if 0 /* This is bogus. jason 9/21/94 */
383
384 t1 = break_out_calls (DECL_INITIAL (exp));
385 if (t1 != DECL_INITIAL (exp))
386 {
387 exp = copy_node (exp);
388 DECL_INITIAL (exp) = t1;
389 }
390 #endif
391 return exp;
392
393 case 'b': /* A block node */
394 {
395 /* Don't know how to handle these correctly yet. Must do a
396 break_out_calls on all DECL_INITIAL values for local variables,
397 and also break_out_calls on all sub-blocks and sub-statements. */
398 abort ();
399 }
400 return exp;
401
402 case 'e': /* an expression */
403 case 'r': /* a reference */
404 case 's': /* an expression with side effects */
405 for (i = tree_code_length[(int) code] - 1; i >= 0; i--)
406 {
407 t1 = break_out_calls (TREE_OPERAND (exp, i));
408 if (t1 != TREE_OPERAND (exp, i))
409 {
410 exp = copy_node (exp);
411 TREE_OPERAND (exp, i) = t1;
412 }
413 }
414 return exp;
415
416 case '<': /* a comparison expression */
417 case '2': /* a binary arithmetic expression */
418 t2 = break_out_calls (TREE_OPERAND (exp, 1));
419 if (t2 != TREE_OPERAND (exp, 1))
420 changed = 1;
421 case '1': /* a unary arithmetic expression */
422 t1 = break_out_calls (TREE_OPERAND (exp, 0));
423 if (t1 != TREE_OPERAND (exp, 0))
424 changed = 1;
425 if (changed)
426 {
427 if (tree_code_length[(int) code] == 1)
428 return build1 (code, TREE_TYPE (exp), t1);
429 else
430 return build (code, TREE_TYPE (exp), t1, t2);
431 }
432 return exp;
433 }
434
435 }
436 \f
437 extern struct obstack permanent_obstack;
438
439 /* Here is how primitive or already-canonicalized types' hash
440 codes are made. MUST BE CONSISTENT WITH tree.c !!! */
441 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
442
443 /* Construct, lay out and return the type of methods belonging to class
444 BASETYPE and whose arguments are described by ARGTYPES and whose values
445 are described by RETTYPE. If each type exists already, reuse it. */
446
447 tree
448 build_cplus_method_type (basetype, rettype, argtypes)
449 tree basetype, rettype, argtypes;
450 {
451 register tree t;
452 tree ptype;
453 int hashcode;
454
455 /* Make a node of the sort we want. */
456 t = make_node (METHOD_TYPE);
457
458 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
459 TREE_TYPE (t) = rettype;
460 ptype = build_pointer_type (basetype);
461
462 /* The actual arglist for this function includes a "hidden" argument
463 which is "this". Put it into the list of argument types. Make
464 sure that the new argument list is allocated on the same obstack
465 as the type. */
466 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
467 TYPE_ARG_TYPES (t) = argtypes;
468 TREE_SIDE_EFFECTS (argtypes) = 1; /* Mark first argtype as "artificial". */
469
470 /* If we already have such a type, use the old one and free this one.
471 Note that it also frees up the above cons cell if found. */
472 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
473 type_hash_list (argtypes);
474
475 t = type_hash_canon (hashcode, t);
476
477 if (!COMPLETE_TYPE_P (t))
478 layout_type (t);
479
480 return t;
481 }
482
483 static tree
484 build_cplus_array_type_1 (elt_type, index_type)
485 tree elt_type;
486 tree index_type;
487 {
488 tree t;
489
490 if (elt_type == error_mark_node || index_type == error_mark_node)
491 return error_mark_node;
492
493 if (processing_template_decl
494 || uses_template_parms (elt_type)
495 || uses_template_parms (index_type))
496 {
497 t = make_node (ARRAY_TYPE);
498 TREE_TYPE (t) = elt_type;
499 TYPE_DOMAIN (t) = index_type;
500 }
501 else
502 t = build_array_type (elt_type, index_type);
503
504 /* Push these needs up so that initialization takes place
505 more easily. */
506 TYPE_NEEDS_CONSTRUCTING (t)
507 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
508 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
509 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
510 return t;
511 }
512
513 tree
514 build_cplus_array_type (elt_type, index_type)
515 tree elt_type;
516 tree index_type;
517 {
518 tree t;
519 int type_quals = CP_TYPE_QUALS (elt_type);
520
521 elt_type = TYPE_MAIN_VARIANT (elt_type);
522
523 t = build_cplus_array_type_1 (elt_type, index_type);
524
525 if (type_quals != TYPE_UNQUALIFIED)
526 t = cp_build_qualified_type (t, type_quals);
527
528 return t;
529 }
530 \f
531 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
532 arrays correctly. In particular, if TYPE is an array of T's, and
533 TYPE_QUALS is non-empty, returns an array of qualified T's. If
534 at attempt is made to qualify a type illegally, and COMPLAIN is
535 non-zero, an error is issued. If COMPLAIN is zero, error_mark_node
536 is returned. */
537
538 tree
539 cp_build_qualified_type_real (type, type_quals, complain)
540 tree type;
541 int type_quals;
542 int complain;
543 {
544 tree result;
545
546 if (type == error_mark_node)
547 return type;
548
549 if (type_quals == TYPE_QUALS (type))
550 return type;
551
552 /* A restrict-qualified pointer type must be a pointer (or reference)
553 to object or incomplete type. */
554 if ((type_quals & TYPE_QUAL_RESTRICT)
555 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
556 && (!POINTER_TYPE_P (type)
557 || TYPE_PTRMEM_P (type)
558 || TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE))
559 {
560 if (complain)
561 cp_error ("`%T' cannot be `restrict'-qualified", type);
562 else
563 return error_mark_node;
564
565 type_quals &= ~TYPE_QUAL_RESTRICT;
566 }
567
568 if (type_quals != TYPE_UNQUALIFIED
569 && TREE_CODE (type) == FUNCTION_TYPE)
570 {
571 if (complain)
572 cp_error ("`%T' cannot be `const'-, `volatile'-, or `restrict'-qualified", type);
573 else
574 return error_mark_node;
575 type_quals = TYPE_UNQUALIFIED;
576 }
577 else if (TREE_CODE (type) == ARRAY_TYPE)
578 {
579 /* In C++, the qualification really applies to the array element
580 type. Obtain the appropriately qualified element type. */
581 tree t;
582 tree element_type
583 = cp_build_qualified_type_real (TREE_TYPE (type),
584 type_quals,
585 complain);
586
587 if (element_type == error_mark_node)
588 return error_mark_node;
589
590 /* See if we already have an identically qualified type. */
591 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
592 if (CP_TYPE_QUALS (t) == type_quals)
593 break;
594
595 /* If we didn't already have it, create it now. */
596 if (!t)
597 {
598 /* Make a new array type, just like the old one, but with the
599 appropriately qualified element type. */
600 t = build_type_copy (type);
601 TREE_TYPE (t) = element_type;
602 }
603
604 /* Even if we already had this variant, we update
605 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
606 they changed since the variant was originally created.
607
608 This seems hokey; if there is some way to use a previous
609 variant *without* coming through here,
610 TYPE_NEEDS_CONSTRUCTING will never be updated. */
611 TYPE_NEEDS_CONSTRUCTING (t)
612 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
613 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
614 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
615 return t;
616 }
617 else if (TYPE_PTRMEMFUNC_P (type))
618 {
619 /* For a pointer-to-member type, we can't just return a
620 cv-qualified version of the RECORD_TYPE. If we do, we
621 haven't change the field that contains the actual pointer to
622 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
623 tree t;
624
625 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
626 t = cp_build_qualified_type_real (t, type_quals, complain);
627 return build_ptrmemfunc_type (t);
628 }
629
630 /* Retrieve (or create) the appropriately qualified variant. */
631 result = build_qualified_type (type, type_quals);
632
633 /* If this was a pointer-to-method type, and we just made a copy,
634 then we need to clear the cached associated
635 pointer-to-member-function type; it is not valid for the new
636 type. */
637 if (result != type
638 && TREE_CODE (type) == POINTER_TYPE
639 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
640 TYPE_SET_PTRMEMFUNC_TYPE (result, NULL_TREE);
641
642 return result;
643 }
644
645 /* Returns the canonical version of TYPE. In other words, if TYPE is
646 a typedef, returns the underlying type. The cv-qualification of
647 the type returned matches the type input; they will always be
648 compatible types. */
649
650 tree
651 canonical_type_variant (t)
652 tree t;
653 {
654 return cp_build_qualified_type (TYPE_MAIN_VARIANT (t), CP_TYPE_QUALS (t));
655 }
656 \f
657 /* Makes new binfos for the indirect bases under BINFO, and updates
658 BINFO_OFFSET for them and their bases. */
659
660 void
661 unshare_base_binfos (binfo)
662 tree binfo;
663 {
664 tree binfos = BINFO_BASETYPES (binfo);
665 tree new_binfo;
666 int j;
667
668 if (binfos == NULL_TREE)
669 return;
670
671 /* Now unshare the structure beneath BINFO. */
672 for (j = TREE_VEC_LENGTH (binfos)-1;
673 j >= 0; j--)
674 {
675 tree base_binfo = TREE_VEC_ELT (binfos, j);
676 new_binfo = TREE_VEC_ELT (binfos, j)
677 = make_binfo (BINFO_OFFSET (base_binfo),
678 base_binfo,
679 BINFO_VTABLE (base_binfo),
680 BINFO_VIRTUALS (base_binfo));
681 TREE_VIA_PUBLIC (new_binfo) = TREE_VIA_PUBLIC (base_binfo);
682 TREE_VIA_PROTECTED (new_binfo) = TREE_VIA_PROTECTED (base_binfo);
683 TREE_VIA_VIRTUAL (new_binfo) = TREE_VIA_VIRTUAL (base_binfo);
684 BINFO_INHERITANCE_CHAIN (new_binfo) = binfo;
685 unshare_base_binfos (new_binfo);
686 }
687 }
688
689 \f
690 /* Hashing of lists so that we don't make duplicates.
691 The entry point is `list_hash_canon'. */
692
693 /* Each hash table slot is a bucket containing a chain
694 of these structures. */
695
696 struct list_hash
697 {
698 struct list_hash *next; /* Next structure in the bucket. */
699 int hashcode; /* Hash code of this list. */
700 tree list; /* The list recorded here. */
701 };
702
703 /* Now here is the hash table. When recording a list, it is added
704 to the slot whose index is the hash code mod the table size.
705 Note that the hash table is used for several kinds of lists.
706 While all these live in the same table, they are completely independent,
707 and the hash code is computed differently for each of these. */
708
709 #define TYPE_HASH_SIZE 59
710 static struct list_hash *list_hash_table[TYPE_HASH_SIZE];
711
712 /* Compute a hash code for a list (chain of TREE_LIST nodes
713 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
714 TREE_COMMON slots), by adding the hash codes of the individual entries. */
715
716 static int
717 list_hash (purpose, value, chain)
718 tree purpose, value, chain;
719 {
720 register int hashcode = 0;
721
722 if (chain)
723 hashcode += TYPE_HASH (chain);
724
725 if (value)
726 hashcode += TYPE_HASH (value);
727 else
728 hashcode += 1007;
729 if (purpose)
730 hashcode += TYPE_HASH (purpose);
731 else
732 hashcode += 1009;
733 return hashcode;
734 }
735
736 /* Look in the type hash table for a type isomorphic to TYPE.
737 If one is found, return it. Otherwise return 0. */
738
739 static tree
740 list_hash_lookup (hashcode, purpose, value, chain)
741 int hashcode;
742 tree purpose, value, chain;
743 {
744 register struct list_hash *h;
745
746 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
747 if (h->hashcode == hashcode
748 && TREE_PURPOSE (h->list) == purpose
749 && TREE_VALUE (h->list) == value
750 && TREE_CHAIN (h->list) == chain)
751 return h->list;
752 return 0;
753 }
754
755 /* Add an entry to the list-hash-table
756 for a list TYPE whose hash code is HASHCODE. */
757
758 static void
759 list_hash_add (hashcode, list)
760 int hashcode;
761 tree list;
762 {
763 register struct list_hash *h;
764
765 h = (struct list_hash *) obstack_alloc (&permanent_obstack, sizeof (struct list_hash));
766 h->hashcode = hashcode;
767 h->list = list;
768 h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
769 list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
770 }
771
772 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
773 object for an identical list if one already exists. Otherwise, build a
774 new one, and record it as the canonical object. */
775
776 /* Set to 1 to debug without canonicalization. Never set by program. */
777
778 static int debug_no_list_hash = 0;
779
780 tree
781 hash_tree_cons (purpose, value, chain)
782 tree purpose, value, chain;
783 {
784 tree t;
785 int hashcode = 0;
786
787 if (! debug_no_list_hash)
788 {
789 hashcode = list_hash (purpose, value, chain);
790 t = list_hash_lookup (hashcode, purpose, value, chain);
791 if (t)
792 return t;
793 }
794
795 t = tree_cons (purpose, value, chain);
796
797 /* If this is a new list, record it for later reuse. */
798 if (! debug_no_list_hash)
799 list_hash_add (hashcode, t);
800
801 return t;
802 }
803
804 /* Constructor for hashed lists. */
805
806 tree
807 hash_tree_chain (value, chain)
808 tree value, chain;
809 {
810 return hash_tree_cons (NULL_TREE, value, chain);
811 }
812
813 /* Similar, but used for concatenating two lists. */
814
815 tree
816 hash_chainon (list1, list2)
817 tree list1, list2;
818 {
819 if (list2 == 0)
820 return list1;
821 if (list1 == 0)
822 return list2;
823 if (TREE_CHAIN (list1) == NULL_TREE)
824 return hash_tree_chain (TREE_VALUE (list1), list2);
825 return hash_tree_chain (TREE_VALUE (list1),
826 hash_chainon (TREE_CHAIN (list1), list2));
827 }
828 \f
829 /* Build an association between TYPE and some parameters:
830
831 OFFSET is the offset added to `this' to convert it to a pointer
832 of type `TYPE *'
833
834 BINFO is the base binfo to use, if we are deriving from one. This
835 is necessary, as we want specialized parent binfos from base
836 classes, so that the VTABLE_NAMEs of bases are for the most derived
837 type, instead of the simple type.
838
839 VTABLE is the virtual function table with which to initialize
840 sub-objects of type TYPE.
841
842 VIRTUALS are the virtual functions sitting in VTABLE. */
843
844 tree
845 make_binfo (offset, binfo, vtable, virtuals)
846 tree offset, binfo;
847 tree vtable, virtuals;
848 {
849 tree new_binfo = make_tree_vec (8);
850 tree type;
851
852 if (TREE_CODE (binfo) == TREE_VEC)
853 type = BINFO_TYPE (binfo);
854 else
855 {
856 type = binfo;
857 binfo = CLASS_TYPE_P (type) ? TYPE_BINFO (binfo) : NULL_TREE;
858 }
859
860 TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
861 BINFO_OFFSET (new_binfo) = offset;
862 BINFO_VTABLE (new_binfo) = vtable;
863 BINFO_VIRTUALS (new_binfo) = virtuals;
864
865 if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
866 BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));
867 return new_binfo;
868 }
869
870 /* Return the binfo value for ELEM in TYPE. */
871
872 tree
873 binfo_value (elem, type)
874 tree elem;
875 tree type;
876 {
877 if (get_base_distance (elem, type, 0, (tree *)0) == -2)
878 compiler_error ("base class `%s' ambiguous in binfo_value",
879 TYPE_NAME_STRING (elem));
880 if (elem == type)
881 return TYPE_BINFO (type);
882 if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
883 return type;
884 return get_binfo (elem, type, 0);
885 }
886
887 /* Return a TREE_LIST whose TREE_VALUE nodes along the
888 BINFO_INHERITANCE_CHAIN for BINFO, but in the opposite order. In
889 other words, while the BINFO_INHERITANCE_CHAIN goes from base
890 classes to derived classes, the reversed path goes from derived
891 classes to base classes. */
892
893 tree
894 reverse_path (binfo)
895 tree binfo;
896 {
897 tree reversed_path;
898
899 reversed_path = NULL_TREE;
900 while (binfo)
901 {
902 reversed_path = tree_cons (NULL_TREE, binfo, reversed_path);
903 binfo = BINFO_INHERITANCE_CHAIN (binfo);
904 }
905
906 return reversed_path;
907 }
908
909 void
910 debug_binfo (elem)
911 tree elem;
912 {
913 HOST_WIDE_INT n;
914 tree virtuals;
915
916 fprintf (stderr, "type \"%s\", offset = ",
917 TYPE_NAME_STRING (BINFO_TYPE (elem)));
918 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
919 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
920 fprintf (stderr, "\nvtable type:\n");
921 debug_tree (BINFO_TYPE (elem));
922 if (BINFO_VTABLE (elem))
923 fprintf (stderr, "vtable decl \"%s\"\n",
924 IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
925 else
926 fprintf (stderr, "no vtable decl yet\n");
927 fprintf (stderr, "virtuals:\n");
928 virtuals = BINFO_VIRTUALS (elem);
929 n = first_vfun_index (BINFO_TYPE (elem));
930
931 while (virtuals)
932 {
933 tree fndecl = TREE_VALUE (virtuals);
934 fprintf (stderr, "%s [%ld =? %ld]\n",
935 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
936 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
937 ++n;
938 virtuals = TREE_CHAIN (virtuals);
939 }
940 }
941
942 int
943 count_functions (t)
944 tree t;
945 {
946 int i;
947 if (TREE_CODE (t) == FUNCTION_DECL)
948 return 1;
949 else if (TREE_CODE (t) == OVERLOAD)
950 {
951 for (i=0; t; t = OVL_CHAIN (t))
952 i++;
953 return i;
954 }
955
956 my_friendly_abort (359);
957 return 0;
958 }
959
960 int
961 is_overloaded_fn (x)
962 tree x;
963 {
964 /* A baselink is also considered an overloaded function. */
965 if (TREE_CODE (x) == OFFSET_REF)
966 x = TREE_OPERAND (x, 1);
967 if (BASELINK_P (x))
968 x = TREE_VALUE (x);
969 return (TREE_CODE (x) == FUNCTION_DECL
970 || TREE_CODE (x) == TEMPLATE_ID_EXPR
971 || DECL_FUNCTION_TEMPLATE_P (x)
972 || TREE_CODE (x) == OVERLOAD);
973 }
974
975 int
976 really_overloaded_fn (x)
977 tree x;
978 {
979 /* A baselink is also considered an overloaded function. */
980 if (TREE_CODE (x) == OFFSET_REF)
981 x = TREE_OPERAND (x, 1);
982 if (BASELINK_P (x))
983 x = TREE_VALUE (x);
984 return (TREE_CODE (x) == OVERLOAD
985 && (TREE_CHAIN (x) != NULL_TREE
986 || DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (x))));
987 }
988
989 tree
990 get_first_fn (from)
991 tree from;
992 {
993 my_friendly_assert (is_overloaded_fn (from), 9);
994 /* A baselink is also considered an overloaded function. */
995 if (BASELINK_P (from))
996 from = TREE_VALUE (from);
997 return OVL_CURRENT (from);
998 }
999
1000 /* Returns nonzero if T is a ->* or .* expression that refers to a
1001 member function. */
1002
1003 int
1004 bound_pmf_p (t)
1005 tree t;
1006 {
1007 return (TREE_CODE (t) == OFFSET_REF
1008 && TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (t, 1))));
1009 }
1010
1011 /* Return a new OVL node, concatenating it with the old one. */
1012
1013 tree
1014 ovl_cons (decl, chain)
1015 tree decl;
1016 tree chain;
1017 {
1018 tree result = make_node (OVERLOAD);
1019 TREE_TYPE (result) = unknown_type_node;
1020 OVL_FUNCTION (result) = decl;
1021 TREE_CHAIN (result) = chain;
1022
1023 return result;
1024 }
1025
1026 /* Build a new overloaded function. If this is the first one,
1027 just return it; otherwise, ovl_cons the _DECLs */
1028
1029 tree
1030 build_overload (decl, chain)
1031 tree decl;
1032 tree chain;
1033 {
1034 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1035 return decl;
1036 if (chain && TREE_CODE (chain) != OVERLOAD)
1037 chain = ovl_cons (chain, NULL_TREE);
1038 return ovl_cons (decl, chain);
1039 }
1040
1041 /* True if fn is in ovl. */
1042
1043 int
1044 ovl_member (fn, ovl)
1045 tree fn;
1046 tree ovl;
1047 {
1048 if (ovl == NULL_TREE)
1049 return 0;
1050 if (TREE_CODE (ovl) != OVERLOAD)
1051 return ovl == fn;
1052 for (; ovl; ovl = OVL_CHAIN (ovl))
1053 if (OVL_FUNCTION (ovl) == fn)
1054 return 1;
1055 return 0;
1056 }
1057
1058 int
1059 is_aggr_type_2 (t1, t2)
1060 tree t1, t2;
1061 {
1062 if (TREE_CODE (t1) != TREE_CODE (t2))
1063 return 0;
1064 return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1065 }
1066
1067 /* Returns non-zero if CODE is the code for a statement. */
1068
1069 static int
1070 statement_code_p (code)
1071 enum tree_code code;
1072 {
1073 switch (code)
1074 {
1075 case EXPR_STMT:
1076 case COMPOUND_STMT:
1077 case DECL_STMT:
1078 case IF_STMT:
1079 case FOR_STMT:
1080 case WHILE_STMT:
1081 case DO_STMT:
1082 case RETURN_STMT:
1083 case BREAK_STMT:
1084 case CONTINUE_STMT:
1085 case SWITCH_STMT:
1086 case GOTO_STMT:
1087 case LABEL_STMT:
1088 case ASM_STMT:
1089 case SUBOBJECT:
1090 case CLEANUP_STMT:
1091 case START_CATCH_STMT:
1092 case CTOR_STMT:
1093 case SCOPE_STMT:
1094 case CTOR_INITIALIZER:
1095 case CASE_LABEL:
1096 case RETURN_INIT:
1097 case TRY_BLOCK:
1098 case HANDLER:
1099 return 1;
1100
1101 default:
1102 return 0;
1103 }
1104 }
1105 \f
1106 #define PRINT_RING_SIZE 4
1107
1108 const char *
1109 lang_printable_name (decl, v)
1110 tree decl;
1111 int v;
1112 {
1113 static tree decl_ring[PRINT_RING_SIZE];
1114 static char *print_ring[PRINT_RING_SIZE];
1115 static int ring_counter;
1116 int i;
1117
1118 /* Only cache functions. */
1119 if (v < 2
1120 || TREE_CODE (decl) != FUNCTION_DECL
1121 || DECL_LANG_SPECIFIC (decl) == 0)
1122 return lang_decl_name (decl, v);
1123
1124 /* See if this print name is lying around. */
1125 for (i = 0; i < PRINT_RING_SIZE; i++)
1126 if (decl_ring[i] == decl)
1127 /* yes, so return it. */
1128 return print_ring[i];
1129
1130 if (++ring_counter == PRINT_RING_SIZE)
1131 ring_counter = 0;
1132
1133 if (current_function_decl != NULL_TREE)
1134 {
1135 if (decl_ring[ring_counter] == current_function_decl)
1136 ring_counter += 1;
1137 if (ring_counter == PRINT_RING_SIZE)
1138 ring_counter = 0;
1139 if (decl_ring[ring_counter] == current_function_decl)
1140 my_friendly_abort (106);
1141 }
1142
1143 if (print_ring[ring_counter])
1144 free (print_ring[ring_counter]);
1145
1146 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v));
1147 decl_ring[ring_counter] = decl;
1148 return print_ring[ring_counter];
1149 }
1150 \f
1151 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1152 listed in RAISES. */
1153
1154 tree
1155 build_exception_variant (type, raises)
1156 tree type;
1157 tree raises;
1158 {
1159 tree v = TYPE_MAIN_VARIANT (type);
1160 int type_quals = TYPE_QUALS (type);
1161
1162 for (; v; v = TYPE_NEXT_VARIANT (v))
1163 if (TYPE_QUALS (v) == type_quals
1164 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), 1))
1165 return v;
1166
1167 /* Need to build a new variant. */
1168 v = build_type_copy (type);
1169 TYPE_RAISES_EXCEPTIONS (v) = raises;
1170 return v;
1171 }
1172
1173 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new one together with its
1174 lang_specific field and its corresponding TEMPLATE_DECL node */
1175
1176 tree
1177 copy_template_template_parm (t)
1178 tree t;
1179 {
1180 tree template = TYPE_NAME (t);
1181 tree t2;
1182
1183 t2 = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
1184 template = copy_node (template);
1185 copy_lang_decl (template);
1186
1187 TREE_TYPE (template) = t2;
1188 TYPE_NAME (t2) = template;
1189 TYPE_STUB_DECL (t2) = template;
1190
1191 /* No need to copy these */
1192 TYPE_FIELDS (t2) = TYPE_FIELDS (t);
1193 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1194 = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
1195 return t2;
1196 }
1197
1198 /* Apply FUNC to all the sub-trees of TP in a pre-order traversal.
1199 FUNC is called with the DATA and the address of each sub-tree. If
1200 FUNC returns a non-NULL value, the traversal is aborted, and the
1201 value returned by FUNC is returned. */
1202
1203 tree
1204 walk_tree (tp, func, data)
1205 tree *tp;
1206 walk_tree_fn func;
1207 void *data;
1208 {
1209 enum tree_code code;
1210 int walk_subtrees;
1211 tree result;
1212
1213 #define WALK_SUBTREE(NODE) \
1214 do \
1215 { \
1216 result = walk_tree (&(NODE), func, data); \
1217 if (result) \
1218 return result; \
1219 } \
1220 while (0)
1221
1222 /* Skip empty subtrees. */
1223 if (!*tp)
1224 return NULL_TREE;
1225
1226 /* Call the function. */
1227 walk_subtrees = 1;
1228 result = (*func) (tp, &walk_subtrees, data);
1229
1230 /* If we found something, return it. */
1231 if (result)
1232 return result;
1233
1234 /* Even if we didn't, FUNC may have decided that there was nothing
1235 interesting below this point in the tree. */
1236 if (!walk_subtrees)
1237 return NULL_TREE;
1238
1239 code = TREE_CODE (*tp);
1240
1241 /* Handle common cases up front. */
1242 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1243 || TREE_CODE_CLASS (code) == 'r'
1244 || TREE_CODE_CLASS (code) == 's')
1245 {
1246 int i, len;
1247
1248 /* Walk over all the sub-trees of this operand. */
1249 len = first_rtl_op (code);
1250 /* TARGET_EXPRs are peculiar: operands 1 and 3 can be the same.
1251 But, we only want to walk once. */
1252 if (code == TARGET_EXPR
1253 && TREE_OPERAND (*tp, 3) == TREE_OPERAND (*tp, 1))
1254 --len;
1255 /* Go through the subtrees. We need to do this in forward order so
1256 that the scope of a FOR_EXPR is handled properly. */
1257 for (i = 0; i < len; ++i)
1258 WALK_SUBTREE (TREE_OPERAND (*tp, i));
1259
1260 /* For statements, we also walk the chain so that we cover the
1261 entire statement tree. */
1262 if (statement_code_p (code))
1263 {
1264 if (code == DECL_STMT
1265 && DECL_STMT_DECL (*tp)
1266 && DECL_P (DECL_STMT_DECL (*tp)))
1267 {
1268 /* Walk the DECL_INITIAL and DECL_SIZE. We don't want to walk
1269 into declarations that are just mentioned, rather than
1270 declared; they don't really belong to this part of the tree.
1271 And, we can see cycles: the initializer for a declaration can
1272 refer to the declaration itself. */
1273 WALK_SUBTREE (DECL_INITIAL (DECL_STMT_DECL (*tp)));
1274 WALK_SUBTREE (DECL_SIZE (DECL_STMT_DECL (*tp)));
1275 WALK_SUBTREE (DECL_SIZE_UNIT (DECL_STMT_DECL (*tp)));
1276 }
1277
1278 WALK_SUBTREE (TREE_CHAIN (*tp));
1279 }
1280
1281 /* We didn't find what we were looking for. */
1282 return NULL_TREE;
1283 }
1284 else if (TREE_CODE_CLASS (code) == 'd')
1285 {
1286 WALK_SUBTREE (TREE_TYPE (*tp));
1287
1288 /* We didn't find what we were looking for. */
1289 return NULL_TREE;
1290 }
1291
1292 /* Not one of the easy cases. We must explicitly go through the
1293 children. */
1294 switch (code)
1295 {
1296 case ERROR_MARK:
1297 case IDENTIFIER_NODE:
1298 case INTEGER_CST:
1299 case REAL_CST:
1300 case STRING_CST:
1301 case DEFAULT_ARG:
1302 case TEMPLATE_TEMPLATE_PARM:
1303 case TEMPLATE_PARM_INDEX:
1304 case TEMPLATE_TYPE_PARM:
1305 case REAL_TYPE:
1306 case COMPLEX_TYPE:
1307 case VOID_TYPE:
1308 case BOOLEAN_TYPE:
1309 case TYPENAME_TYPE:
1310 case UNION_TYPE:
1311 case ENUMERAL_TYPE:
1312 case TYPEOF_TYPE:
1313 case BLOCK:
1314 /* None of thse have subtrees other than those already walked
1315 above. */
1316 break;
1317
1318 case PTRMEM_CST:
1319 WALK_SUBTREE (TREE_TYPE (*tp));
1320 break;
1321
1322 case POINTER_TYPE:
1323 case REFERENCE_TYPE:
1324 WALK_SUBTREE (TREE_TYPE (*tp));
1325 break;
1326
1327 case TREE_LIST:
1328 WALK_SUBTREE (TREE_PURPOSE (*tp));
1329 WALK_SUBTREE (TREE_VALUE (*tp));
1330 WALK_SUBTREE (TREE_CHAIN (*tp));
1331 break;
1332
1333 case OVERLOAD:
1334 WALK_SUBTREE (OVL_FUNCTION (*tp));
1335 WALK_SUBTREE (OVL_CHAIN (*tp));
1336 break;
1337
1338 case TREE_VEC:
1339 {
1340 int len = TREE_VEC_LENGTH (*tp);
1341 while (len--)
1342 WALK_SUBTREE (TREE_VEC_ELT (*tp, len));
1343 }
1344 break;
1345
1346 case COMPLEX_CST:
1347 WALK_SUBTREE (TREE_REALPART (*tp));
1348 WALK_SUBTREE (TREE_IMAGPART (*tp));
1349 break;
1350
1351 case CONSTRUCTOR:
1352 WALK_SUBTREE (CONSTRUCTOR_ELTS (*tp));
1353 break;
1354
1355 case METHOD_TYPE:
1356 WALK_SUBTREE (TYPE_METHOD_BASETYPE (*tp));
1357 /* Fall through. */
1358
1359 case FUNCTION_TYPE:
1360 WALK_SUBTREE (TREE_TYPE (*tp));
1361 WALK_SUBTREE (TYPE_ARG_TYPES (*tp));
1362 break;
1363
1364 case ARRAY_TYPE:
1365 WALK_SUBTREE (TREE_TYPE (*tp));
1366 WALK_SUBTREE (TYPE_DOMAIN (*tp));
1367 break;
1368
1369 case INTEGER_TYPE:
1370 WALK_SUBTREE (TYPE_MIN_VALUE (*tp));
1371 WALK_SUBTREE (TYPE_MAX_VALUE (*tp));
1372 break;
1373
1374 case OFFSET_TYPE:
1375 WALK_SUBTREE (TREE_TYPE (*tp));
1376 WALK_SUBTREE (TYPE_OFFSET_BASETYPE (*tp));
1377 break;
1378
1379 case RECORD_TYPE:
1380 if (TYPE_PTRMEMFUNC_P (*tp))
1381 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
1382 break;
1383
1384 default:
1385 my_friendly_abort (19990803);
1386 }
1387
1388 /* We didn't find what we were looking for. */
1389 return NULL_TREE;
1390
1391 #undef WALK_SUBTREE
1392 }
1393
1394 /* Passed to walk_tree. Checks for the use of types with no linkage. */
1395
1396 static tree
1397 no_linkage_helper (tp, walk_subtrees, data)
1398 tree *tp;
1399 int *walk_subtrees ATTRIBUTE_UNUSED;
1400 void *data ATTRIBUTE_UNUSED;
1401 {
1402 tree t = *tp;
1403
1404 if (TYPE_P (t)
1405 && (IS_AGGR_TYPE (t) || TREE_CODE (t) == ENUMERAL_TYPE)
1406 && (decl_function_context (TYPE_MAIN_DECL (t))
1407 || ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))))
1408 return t;
1409 return NULL_TREE;
1410 }
1411
1412 /* Check if the type T depends on a type with no linkage and if so, return
1413 it. */
1414
1415 tree
1416 no_linkage_check (t)
1417 tree t;
1418 {
1419 /* There's no point in checking linkage on template functions; we
1420 can't know their complete types. */
1421 if (processing_template_decl)
1422 return NULL_TREE;
1423
1424 t = walk_tree (&t, no_linkage_helper, NULL);
1425 if (t != error_mark_node)
1426 return t;
1427 return NULL_TREE;
1428 }
1429
1430 /* Passed to walk_tree. Copies the node pointed to, if appropriate. */
1431
1432 tree
1433 copy_tree_r (tp, walk_subtrees, data)
1434 tree *tp;
1435 int *walk_subtrees;
1436 void *data ATTRIBUTE_UNUSED;
1437 {
1438 enum tree_code code = TREE_CODE (*tp);
1439
1440 /* We make copies of most nodes. */
1441 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))
1442 || TREE_CODE_CLASS (code) == 'r'
1443 || TREE_CODE_CLASS (code) == 'c'
1444 || TREE_CODE_CLASS (code) == 's'
1445 || code == PARM_DECL
1446 || code == TREE_LIST
1447 || code == TREE_VEC
1448 || code == OVERLOAD)
1449 {
1450 /* Because the chain gets clobbered when we make a copy, we save it
1451 here. */
1452 tree chain = TREE_CHAIN (*tp);
1453
1454 /* Copy the node. */
1455 *tp = copy_node (*tp);
1456
1457 /* Now, restore the chain, if appropriate. That will cause
1458 walk_tree to walk into the chain as well. */
1459 if (code == PARM_DECL || code == TREE_LIST || code == OVERLOAD
1460 || statement_code_p (code))
1461 TREE_CHAIN (*tp) = chain;
1462
1463 /* For now, we don't update BLOCKs when we make copies. So, we
1464 have to nullify all scope-statements. */
1465 if (TREE_CODE (*tp) == SCOPE_STMT)
1466 SCOPE_STMT_BLOCK (*tp) = NULL_TREE;
1467 }
1468 else if (code == TEMPLATE_TEMPLATE_PARM)
1469 /* These must be copied specially. */
1470 *tp = copy_template_template_parm (*tp);
1471 else if (TREE_CODE_CLASS (code) == 't')
1472 /* There's no need to copy types, or anything beneath them. */
1473 *walk_subtrees = 0;
1474
1475 return NULL_TREE;
1476 }
1477
1478 #ifdef GATHER_STATISTICS
1479 extern int depth_reached;
1480 #endif
1481
1482 void
1483 print_lang_statistics ()
1484 {
1485 print_search_statistics ();
1486 print_class_statistics ();
1487 #ifdef GATHER_STATISTICS
1488 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1489 depth_reached);
1490 #endif
1491 }
1492
1493 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1494 (which is an ARRAY_TYPE). This counts only elements of the top
1495 array. */
1496
1497 tree
1498 array_type_nelts_top (type)
1499 tree type;
1500 {
1501 return fold (build (PLUS_EXPR, sizetype,
1502 array_type_nelts (type),
1503 integer_one_node));
1504 }
1505
1506 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1507 (which is an ARRAY_TYPE). This one is a recursive count of all
1508 ARRAY_TYPEs that are clumped together. */
1509
1510 tree
1511 array_type_nelts_total (type)
1512 tree type;
1513 {
1514 tree sz = array_type_nelts_top (type);
1515 type = TREE_TYPE (type);
1516 while (TREE_CODE (type) == ARRAY_TYPE)
1517 {
1518 tree n = array_type_nelts_top (type);
1519 sz = fold (build (MULT_EXPR, sizetype, sz, n));
1520 type = TREE_TYPE (type);
1521 }
1522 return sz;
1523 }
1524
1525 /* Called from break_out_target_exprs via mapcar. */
1526
1527 static tree
1528 bot_manip (tp, walk_subtrees, data)
1529 tree *tp;
1530 int *walk_subtrees;
1531 void *data;
1532 {
1533 splay_tree target_remap = ((splay_tree) data);
1534 tree t = *tp;
1535
1536 if (TREE_CODE (t) != TREE_LIST && ! TREE_SIDE_EFFECTS (t))
1537 {
1538 /* There can't be any TARGET_EXPRs below this point. */
1539 *walk_subtrees = 0;
1540 return NULL_TREE;
1541 }
1542 else if (TREE_CODE (t) == TARGET_EXPR)
1543 {
1544 tree u;
1545
1546 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
1547 {
1548 mark_used (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 1), 0), 0));
1549 u = build_cplus_new
1550 (TREE_TYPE (t), break_out_target_exprs (TREE_OPERAND (t, 1)));
1551 }
1552 else
1553 {
1554 tree var;
1555
1556 u = copy_node (t);
1557 var = build (VAR_DECL, TREE_TYPE (t));
1558 DECL_CONTEXT (var) = current_function_decl;
1559 layout_decl (var, 0);
1560 TREE_OPERAND (u, 0) = var;
1561 }
1562
1563 /* Map the old variable to the new one. */
1564 splay_tree_insert (target_remap,
1565 (splay_tree_key) TREE_OPERAND (t, 0),
1566 (splay_tree_value) TREE_OPERAND (u, 0));
1567
1568 /* Replace the old expression with the new version. */
1569 *tp = u;
1570 /* We don't have to go below this point; the recursive call to
1571 break_out_target_exprs will have handled anything below this
1572 point. */
1573 *walk_subtrees = 0;
1574 return NULL_TREE;
1575 }
1576 else if (TREE_CODE (t) == CALL_EXPR)
1577 mark_used (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
1578
1579 /* Make a copy of this node. */
1580 return copy_tree_r (tp, walk_subtrees, NULL);
1581 }
1582
1583 /* Replace all remapped VAR_DECLs in T with their new equivalents.
1584 DATA is really a splay-tree mapping old variables to new
1585 variables. */
1586
1587 static tree
1588 bot_replace (t, walk_subtrees, data)
1589 tree *t;
1590 int *walk_subtrees ATTRIBUTE_UNUSED;
1591 void *data;
1592 {
1593 splay_tree target_remap = ((splay_tree) data);
1594
1595 if (TREE_CODE (*t) == VAR_DECL)
1596 {
1597 splay_tree_node n = splay_tree_lookup (target_remap,
1598 (splay_tree_key) *t);
1599 if (n)
1600 *t = (tree) n->value;
1601 }
1602
1603 return NULL_TREE;
1604 }
1605
1606 /* When we parse a default argument expression, we may create
1607 temporary variables via TARGET_EXPRs. When we actually use the
1608 default-argument expression, we make a copy of the expression, but
1609 we must replace the temporaries with appropriate local versions. */
1610
1611 tree
1612 break_out_target_exprs (t)
1613 tree t;
1614 {
1615 static int target_remap_count;
1616 static splay_tree target_remap;
1617
1618 if (!target_remap_count++)
1619 target_remap = splay_tree_new (splay_tree_compare_pointers,
1620 /*splay_tree_delete_key_fn=*/NULL,
1621 /*splay_tree_delete_value_fn=*/NULL);
1622 walk_tree (&t, bot_manip, target_remap);
1623 walk_tree (&t, bot_replace, target_remap);
1624
1625 if (!--target_remap_count)
1626 {
1627 splay_tree_delete (target_remap);
1628 target_remap = NULL;
1629 }
1630
1631 return t;
1632 }
1633
1634 /* Obstack used for allocating nodes in template function and variable
1635 definitions. */
1636
1637 /* Similar to `build_nt', except that we set TREE_COMPLEXITY to be the
1638 current line number. */
1639
1640 tree
1641 build_min_nt VPARAMS ((enum tree_code code, ...))
1642 {
1643 #ifndef ANSI_PROTOTYPES
1644 enum tree_code code;
1645 #endif
1646 va_list p;
1647 register tree t;
1648 register int length;
1649 register int i;
1650
1651 VA_START (p, code);
1652
1653 #ifndef ANSI_PROTOTYPES
1654 code = va_arg (p, enum tree_code);
1655 #endif
1656
1657 t = make_node (code);
1658 length = tree_code_length[(int) code];
1659 TREE_COMPLEXITY (t) = lineno;
1660
1661 for (i = 0; i < length; i++)
1662 {
1663 tree x = va_arg (p, tree);
1664 TREE_OPERAND (t, i) = x;
1665 }
1666
1667 va_end (p);
1668 return t;
1669 }
1670
1671 /* Similar to `build', except we set TREE_COMPLEXITY to the current
1672 line-number. */
1673
1674 tree
1675 build_min VPARAMS ((enum tree_code code, tree tt, ...))
1676 {
1677 #ifndef ANSI_PROTOTYPES
1678 enum tree_code code;
1679 tree tt;
1680 #endif
1681 va_list p;
1682 register tree t;
1683 register int length;
1684 register int i;
1685
1686 VA_START (p, tt);
1687
1688 #ifndef ANSI_PROTOTYPES
1689 code = va_arg (p, enum tree_code);
1690 tt = va_arg (p, tree);
1691 #endif
1692
1693 t = make_node (code);
1694 length = tree_code_length[(int) code];
1695 TREE_TYPE (t) = tt;
1696 TREE_COMPLEXITY (t) = lineno;
1697
1698 for (i = 0; i < length; i++)
1699 {
1700 tree x = va_arg (p, tree);
1701 TREE_OPERAND (t, i) = x;
1702 }
1703
1704 va_end (p);
1705 return t;
1706 }
1707
1708 /* Returns an INTEGER_CST (of type `int') corresponding to I.
1709 Multiple calls with the same value of I may or may not yield the
1710 same node; therefore, callers should never modify the node
1711 returned. */
1712
1713 tree
1714 build_shared_int_cst (i)
1715 int i;
1716 {
1717 static tree cache[256];
1718
1719 if (i >= 256)
1720 return build_int_2 (i, 0);
1721
1722 if (!cache[i])
1723 cache[i] = build_int_2 (i, 0);
1724
1725 return cache[i];
1726 }
1727
1728 tree
1729 get_type_decl (t)
1730 tree t;
1731 {
1732 if (TREE_CODE (t) == TYPE_DECL)
1733 return t;
1734 if (TYPE_P (t))
1735 return TYPE_STUB_DECL (t);
1736
1737 my_friendly_abort (42);
1738
1739 /* Stop compiler from complaining control reaches end of non-void function. */
1740 return 0;
1741 }
1742
1743 int
1744 can_free (obstack, t)
1745 struct obstack *obstack;
1746 tree t;
1747 {
1748 int size = 0;
1749
1750 if (TREE_CODE (t) == TREE_VEC)
1751 size = (TREE_VEC_LENGTH (t)-1) * sizeof (tree) + sizeof (struct tree_vec);
1752 else
1753 my_friendly_abort (42);
1754
1755 #define ROUND(x) ((x + obstack_alignment_mask (obstack)) \
1756 & ~ obstack_alignment_mask (obstack))
1757 if ((char *)t + ROUND (size) == obstack_next_free (obstack))
1758 return 1;
1759 #undef ROUND
1760
1761 return 0;
1762 }
1763
1764 /* Return first vector element whose BINFO_TYPE is ELEM.
1765 Return 0 if ELEM is not in VEC. VEC may be NULL_TREE. */
1766
1767 tree
1768 vec_binfo_member (elem, vec)
1769 tree elem, vec;
1770 {
1771 int i;
1772
1773 if (vec)
1774 for (i = 0; i < TREE_VEC_LENGTH (vec); ++i)
1775 if (same_type_p (elem, BINFO_TYPE (TREE_VEC_ELT (vec, i))))
1776 return TREE_VEC_ELT (vec, i);
1777
1778 return NULL_TREE;
1779 }
1780
1781 /* Returns the namespace that contains DECL, whether directly or
1782 indirectly. */
1783
1784 tree
1785 decl_namespace_context (decl)
1786 tree decl;
1787 {
1788 while (1)
1789 {
1790 if (TREE_CODE (decl) == NAMESPACE_DECL)
1791 return decl;
1792 else if (TYPE_P (decl))
1793 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
1794 else
1795 decl = CP_DECL_CONTEXT (decl);
1796 }
1797 }
1798
1799 /* Return truthvalue of whether T1 is the same tree structure as T2.
1800 Return 1 if they are the same.
1801 Return 0 if they are understandably different.
1802 Return -1 if either contains tree structure not understood by
1803 this function. */
1804
1805 int
1806 cp_tree_equal (t1, t2)
1807 tree t1, t2;
1808 {
1809 register enum tree_code code1, code2;
1810 int cmp;
1811
1812 if (t1 == t2)
1813 return 1;
1814 if (t1 == 0 || t2 == 0)
1815 return 0;
1816
1817 code1 = TREE_CODE (t1);
1818 code2 = TREE_CODE (t2);
1819
1820 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
1821 {
1822 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
1823 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1824 else
1825 return cp_tree_equal (TREE_OPERAND (t1, 0), t2);
1826 }
1827 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
1828 || code2 == NON_LVALUE_EXPR)
1829 return cp_tree_equal (t1, TREE_OPERAND (t2, 0));
1830
1831 if (code1 != code2)
1832 return 0;
1833
1834 switch (code1)
1835 {
1836 case INTEGER_CST:
1837 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1838 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1839
1840 case REAL_CST:
1841 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
1842
1843 case STRING_CST:
1844 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1845 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1846 TREE_STRING_LENGTH (t1));
1847
1848 case CONSTRUCTOR:
1849 /* We need to do this when determining whether or not two
1850 non-type pointer to member function template arguments
1851 are the same. */
1852 if (!(same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1853 /* The first operand is RTL. */
1854 && TREE_OPERAND (t1, 0) == TREE_OPERAND (t2, 0)))
1855 return 0;
1856 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1857
1858 case TREE_LIST:
1859 cmp = cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1860 if (cmp <= 0)
1861 return cmp;
1862 cmp = cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2));
1863 if (cmp <= 0)
1864 return cmp;
1865 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
1866
1867 case SAVE_EXPR:
1868 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1869
1870 case CALL_EXPR:
1871 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1872 if (cmp <= 0)
1873 return cmp;
1874 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1875
1876 case TARGET_EXPR:
1877 /* Special case: if either target is an unallocated VAR_DECL,
1878 it means that it's going to be unified with whatever the
1879 TARGET_EXPR is really supposed to initialize, so treat it
1880 as being equivalent to anything. */
1881 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
1882 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
1883 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
1884 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
1885 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
1886 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
1887 cmp = 1;
1888 else
1889 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1890 if (cmp <= 0)
1891 return cmp;
1892 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1893
1894 case WITH_CLEANUP_EXPR:
1895 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1896 if (cmp <= 0)
1897 return cmp;
1898 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
1899
1900 case COMPONENT_REF:
1901 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
1902 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1903 return 0;
1904
1905 case VAR_DECL:
1906 case PARM_DECL:
1907 case CONST_DECL:
1908 case FUNCTION_DECL:
1909 return 0;
1910
1911 case TEMPLATE_PARM_INDEX:
1912 return TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
1913 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2);
1914
1915 case SIZEOF_EXPR:
1916 case ALIGNOF_EXPR:
1917 if (TREE_CODE (TREE_OPERAND (t1, 0)) != TREE_CODE (TREE_OPERAND (t2, 0)))
1918 return 0;
1919 if (TYPE_P (TREE_OPERAND (t1, 0)))
1920 return same_type_p (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1921 break;
1922
1923 case PTRMEM_CST:
1924 /* Two pointer-to-members are the same if they point to the same
1925 field or function in the same class. */
1926 return (PTRMEM_CST_MEMBER (t1) == PTRMEM_CST_MEMBER (t2)
1927 && same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2)));
1928
1929 default:
1930 break;
1931 }
1932
1933 switch (TREE_CODE_CLASS (code1))
1934 {
1935 int i;
1936 case '1':
1937 case '2':
1938 case '<':
1939 case 'e':
1940 case 'r':
1941 case 's':
1942 cmp = 1;
1943 for (i=0; i<tree_code_length[(int) code1]; ++i)
1944 {
1945 cmp = cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
1946 if (cmp <= 0)
1947 return cmp;
1948 }
1949 return cmp;
1950 }
1951
1952 return -1;
1953 }
1954
1955 /* Build a wrapper around some pointer PTR so we can use it as a tree. */
1956
1957 tree
1958 build_ptr_wrapper (ptr)
1959 void *ptr;
1960 {
1961 tree t = make_node (WRAPPER);
1962 WRAPPER_PTR (t) = ptr;
1963 return t;
1964 }
1965
1966 /* Same, but on the expression_obstack. */
1967
1968 tree
1969 build_expr_ptr_wrapper (ptr)
1970 void *ptr;
1971 {
1972 return build_ptr_wrapper (ptr);
1973 }
1974
1975 /* Build a wrapper around some integer I so we can use it as a tree. */
1976
1977 tree
1978 build_int_wrapper (i)
1979 int i;
1980 {
1981 tree t = make_node (WRAPPER);
1982 WRAPPER_INT (t) = i;
1983 return t;
1984 }
1985
1986 static tree
1987 build_srcloc (file, line)
1988 char *file;
1989 int line;
1990 {
1991 tree t;
1992
1993 t = make_node (SRCLOC);
1994 SRCLOC_FILE (t) = file;
1995 SRCLOC_LINE (t) = line;
1996
1997 return t;
1998 }
1999
2000 tree
2001 build_srcloc_here ()
2002 {
2003 return build_srcloc (input_filename, lineno);
2004 }
2005
2006 /* The type of ARG when used as an lvalue. */
2007
2008 tree
2009 lvalue_type (arg)
2010 tree arg;
2011 {
2012 tree type = TREE_TYPE (arg);
2013 if (TREE_CODE (arg) == OVERLOAD)
2014 type = unknown_type_node;
2015 return type;
2016 }
2017
2018 /* The type of ARG for printing error messages; denote lvalues with
2019 reference types. */
2020
2021 tree
2022 error_type (arg)
2023 tree arg;
2024 {
2025 tree type = TREE_TYPE (arg);
2026 if (TREE_CODE (type) == ARRAY_TYPE)
2027 ;
2028 else if (real_lvalue_p (arg))
2029 type = build_reference_type (lvalue_type (arg));
2030 else if (IS_AGGR_TYPE (type))
2031 type = lvalue_type (arg);
2032
2033 return type;
2034 }
2035
2036 /* Does FUNCTION use a variable-length argument list? */
2037
2038 int
2039 varargs_function_p (function)
2040 tree function;
2041 {
2042 tree parm = TYPE_ARG_TYPES (TREE_TYPE (function));
2043 for (; parm; parm = TREE_CHAIN (parm))
2044 if (TREE_VALUE (parm) == void_type_node)
2045 return 0;
2046 return 1;
2047 }
2048
2049 /* Returns 1 if decl is a member of a class. */
2050
2051 int
2052 member_p (decl)
2053 tree decl;
2054 {
2055 const tree ctx = DECL_CONTEXT (decl);
2056 return (ctx && TYPE_P (ctx));
2057 }
2058
2059 /* Create a placeholder for member access where we don't actually have an
2060 object that the access is against. */
2061
2062 tree
2063 build_dummy_object (type)
2064 tree type;
2065 {
2066 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
2067 return build_indirect_ref (decl, NULL_PTR);
2068 }
2069
2070 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
2071 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
2072 binfo path from current_class_type to TYPE, or 0. */
2073
2074 tree
2075 maybe_dummy_object (type, binfop)
2076 tree type;
2077 tree *binfop;
2078 {
2079 tree decl, context;
2080
2081 if (current_class_type
2082 && get_base_distance (type, current_class_type, 0, binfop) != -1)
2083 context = current_class_type;
2084 else
2085 {
2086 /* Reference from a nested class member function. */
2087 context = type;
2088 if (binfop)
2089 *binfop = TYPE_BINFO (type);
2090 }
2091
2092 if (current_class_ref && context == current_class_type)
2093 decl = current_class_ref;
2094 else
2095 decl = build_dummy_object (context);
2096
2097 return decl;
2098 }
2099
2100 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
2101
2102 int
2103 is_dummy_object (ob)
2104 tree ob;
2105 {
2106 if (TREE_CODE (ob) == INDIRECT_REF)
2107 ob = TREE_OPERAND (ob, 0);
2108 return (TREE_CODE (ob) == NOP_EXPR
2109 && TREE_OPERAND (ob, 0) == void_zero_node);
2110 }
2111
2112 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
2113
2114 int
2115 pod_type_p (t)
2116 tree t;
2117 {
2118 while (TREE_CODE (t) == ARRAY_TYPE)
2119 t = TREE_TYPE (t);
2120
2121 if (INTEGRAL_TYPE_P (t))
2122 return 1; /* integral, character or enumeral type */
2123 if (FLOAT_TYPE_P (t))
2124 return 1;
2125 if (TYPE_PTR_P (t))
2126 return 1; /* pointer to non-member */
2127 if (TYPE_PTRMEM_P (t))
2128 return 1; /* pointer to member object */
2129 if (TYPE_PTRMEMFUNC_P (t))
2130 return 1; /* pointer to member function */
2131
2132 if (! CLASS_TYPE_P (t))
2133 return 0; /* other non-class type (reference or function) */
2134 if (CLASSTYPE_NON_POD_P (t))
2135 return 0;
2136 return 1;
2137 }
2138
2139 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid C++-specific
2140 attribute for either declaration DECL or type TYPE and 0 otherwise.
2141 Plugged into valid_lang_attribute. */
2142
2143 int
2144 cp_valid_lang_attribute (attr_name, attr_args, decl, type)
2145 tree attr_name;
2146 tree attr_args ATTRIBUTE_UNUSED;
2147 tree decl ATTRIBUTE_UNUSED;
2148 tree type ATTRIBUTE_UNUSED;
2149 {
2150 if (is_attribute_p ("com_interface", attr_name))
2151 {
2152 if (! flag_vtable_thunks)
2153 {
2154 error ("`com_interface' only supported with -fvtable-thunks");
2155 return 0;
2156 }
2157
2158 if (attr_args != NULL_TREE
2159 || decl != NULL_TREE
2160 || ! CLASS_TYPE_P (type)
2161 || type != TYPE_MAIN_VARIANT (type))
2162 {
2163 warning ("`com_interface' attribute can only be applied to class definitions");
2164 return 0;
2165 }
2166
2167 CLASSTYPE_COM_INTERFACE (type) = 1;
2168 return 1;
2169 }
2170 else if (is_attribute_p ("init_priority", attr_name))
2171 {
2172 tree initp_expr = (attr_args ? TREE_VALUE (attr_args): NULL_TREE);
2173 int pri;
2174
2175 if (initp_expr)
2176 STRIP_NOPS (initp_expr);
2177
2178 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
2179 {
2180 error ("requested init_priority is not an integer constant");
2181 return 0;
2182 }
2183
2184 pri = TREE_INT_CST_LOW (initp_expr);
2185
2186 while (TREE_CODE (type) == ARRAY_TYPE)
2187 type = TREE_TYPE (type);
2188
2189 if (decl == NULL_TREE
2190 || TREE_CODE (decl) != VAR_DECL
2191 || ! TREE_STATIC (decl)
2192 || DECL_EXTERNAL (decl)
2193 || (TREE_CODE (type) != RECORD_TYPE
2194 && TREE_CODE (type) != UNION_TYPE)
2195 /* Static objects in functions are initialized the
2196 first time control passes through that
2197 function. This is not precise enough to pin down an
2198 init_priority value, so don't allow it. */
2199 || current_function_decl)
2200 {
2201 error ("can only use init_priority attribute on file-scope definitions of objects of class type");
2202 return 0;
2203 }
2204
2205 if (pri > MAX_INIT_PRIORITY || pri <= 0)
2206 {
2207 error ("requested init_priority is out of range");
2208 return 0;
2209 }
2210
2211 /* Check for init_priorities that are reserved for
2212 language and runtime support implementations.*/
2213 if (pri <= MAX_RESERVED_INIT_PRIORITY)
2214 {
2215 warning
2216 ("requested init_priority is reserved for internal use");
2217 }
2218
2219 DECL_INIT_PRIORITY (decl) = pri;
2220 return 1;
2221 }
2222
2223 return 0;
2224 }
2225
2226 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
2227 thing pointed to by the constant. */
2228
2229 tree
2230 make_ptrmem_cst (type, member)
2231 tree type;
2232 tree member;
2233 {
2234 tree ptrmem_cst = make_node (PTRMEM_CST);
2235 /* If would seem a great convenience if make_node would set
2236 TREE_CONSTANT for things of class `c', but it does not. */
2237 TREE_CONSTANT (ptrmem_cst) = 1;
2238 TREE_TYPE (ptrmem_cst) = type;
2239 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
2240 return ptrmem_cst;
2241 }
2242
2243 /* Mark ARG (which is really a list_hash_table **) for GC. */
2244
2245 static void
2246 mark_list_hash (arg)
2247 void *arg;
2248 {
2249 struct list_hash *lh;
2250
2251 for (lh = * ((struct list_hash **) arg); lh; lh = lh->next)
2252 ggc_mark_tree (lh->list);
2253 }
2254
2255 /* Initialize tree.c. */
2256
2257 void
2258 init_tree ()
2259 {
2260 make_lang_type_fn = cp_make_lang_type;
2261 lang_unsave = cp_unsave;
2262 ggc_add_root (list_hash_table,
2263 sizeof (list_hash_table) / sizeof (struct list_hash *),
2264 sizeof (struct list_hash *),
2265 mark_list_hash);
2266 }
2267
2268 /* The SAVE_EXPR pointed to by TP is being copied. If ST contains
2269 information indicating to what new SAVE_EXPR this one should be
2270 mapped, use that one. Otherwise, create a new node and enter it in
2271 ST. FN is the function into which the copy will be placed. */
2272
2273 void
2274 remap_save_expr (tp, st, fn, walk_subtrees)
2275 tree *tp;
2276 splay_tree st;
2277 tree fn;
2278 int *walk_subtrees;
2279 {
2280 splay_tree_node n;
2281
2282 /* See if we already encountered this SAVE_EXPR. */
2283 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2284
2285 /* If we didn't already remap this SAVE_EXPR, do so now. */
2286 if (!n)
2287 {
2288 tree t = copy_node (*tp);
2289
2290 /* The SAVE_EXPR is now part of the function into which we
2291 are inlining this body. */
2292 SAVE_EXPR_CONTEXT (t) = fn;
2293 /* And we haven't evaluated it yet. */
2294 SAVE_EXPR_RTL (t) = NULL_RTX;
2295 /* Remember this SAVE_EXPR. */
2296 n = splay_tree_insert (st,
2297 (splay_tree_key) *tp,
2298 (splay_tree_value) t);
2299 }
2300 else
2301 /* We've already walked into this SAVE_EXPR, so we needn't do it
2302 again. */
2303 *walk_subtrees = 0;
2304
2305 /* Replace this SAVE_EXPR with the copy. */
2306 *tp = (tree) n->value;
2307 }
2308
2309 /* Called via walk_tree. If *TP points to a DECL_STMT for a local
2310 declaration, copies the declaration and enters it in the splay_tree
2311 pointed to by DATA (which is really a `splay_tree *'). */
2312
2313 static tree
2314 mark_local_for_remap_r (tp, walk_subtrees, data)
2315 tree *tp;
2316 int *walk_subtrees ATTRIBUTE_UNUSED;
2317 void *data;
2318 {
2319 tree t = *tp;
2320 splay_tree st = (splay_tree) data;
2321 tree decl;
2322
2323
2324 if (TREE_CODE (t) == DECL_STMT
2325 && nonstatic_local_decl_p (DECL_STMT_DECL (t)))
2326 decl = DECL_STMT_DECL (t);
2327 else if (TREE_CODE (t) == LABEL_STMT)
2328 decl = LABEL_STMT_LABEL (t);
2329 else if (TREE_CODE (t) == TARGET_EXPR
2330 && nonstatic_local_decl_p (TREE_OPERAND (t, 0)))
2331 decl = TREE_OPERAND (t, 0);
2332 else
2333 decl = NULL_TREE;
2334
2335 if (decl)
2336 {
2337 tree copy;
2338
2339 /* Make a copy. */
2340 copy = copy_decl_for_inlining (decl,
2341 DECL_CONTEXT (decl),
2342 DECL_CONTEXT (decl));
2343
2344 /* Remember the copy. */
2345 splay_tree_insert (st,
2346 (splay_tree_key) decl,
2347 (splay_tree_value) copy);
2348 }
2349
2350 return NULL_TREE;
2351 }
2352
2353 /* Called via walk_tree when an expression is unsaved. Using the
2354 splay_tree pointed to by ST (which is really a `splay_tree'),
2355 remaps all local declarations to appropriate replacements. */
2356
2357 static tree
2358 cp_unsave_r (tp, walk_subtrees, data)
2359 tree *tp;
2360 int *walk_subtrees;
2361 void *data;
2362 {
2363 splay_tree st = (splay_tree) data;
2364 splay_tree_node n;
2365
2366 /* Only a local declaration (variable or label). */
2367 if (nonstatic_local_decl_p (*tp))
2368 {
2369 /* Lookup the declaration. */
2370 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2371
2372 /* If it's there, remap it. */
2373 if (n)
2374 *tp = (tree) n->value;
2375 }
2376 else if (TREE_CODE (*tp) == SAVE_EXPR)
2377 remap_save_expr (tp, st, current_function_decl, walk_subtrees);
2378 else
2379 {
2380 copy_tree_r (tp, walk_subtrees, NULL);
2381
2382 /* Do whatever unsaving is required. */
2383 unsave_expr_1 (*tp);
2384 }
2385
2386 /* Keep iterating. */
2387 return NULL_TREE;
2388 }
2389
2390 /* Called by unsave_expr_now whenever an expression (*TP) needs to be
2391 unsaved. */
2392
2393 static void
2394 cp_unsave (tp)
2395 tree *tp;
2396 {
2397 splay_tree st;
2398
2399 /* Create a splay-tree to map old local variable declarations to new
2400 ones. */
2401 st = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2402
2403 /* Walk the tree once figuring out what needs to be remapped. */
2404 walk_tree (tp, mark_local_for_remap_r, st);
2405
2406 /* Walk the tree again, copying, remapping, and unsaving. */
2407 walk_tree (tp, cp_unsave_r, st);
2408
2409 /* Clean up. */
2410 splay_tree_delete (st);
2411 }