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