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