* predict.c (estimate_bb_frequencies): Correctly set
[official-gcc.git] / gcc / cp / tree.c
blob520aa164572126143fb5eb1cd24328592b52a002
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, 2003 Free Software Foundation, Inc.
4 Hacked by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "real.h"
31 #include "rtl.h"
32 #include "toplev.h"
33 #include "insn-config.h"
34 #include "integrate.h"
35 #include "tree-inline.h"
36 #include "target.h"
38 static tree bot_manip PARAMS ((tree *, int *, void *));
39 static tree bot_replace PARAMS ((tree *, int *, void *));
40 static tree build_cplus_array_type_1 PARAMS ((tree, tree));
41 static int list_hash_eq PARAMS ((const void *, const void *));
42 static hashval_t list_hash_pieces PARAMS ((tree, tree, tree));
43 static hashval_t list_hash PARAMS ((const void *));
44 static cp_lvalue_kind lvalue_p_1 PARAMS ((tree, int, int));
45 static tree no_linkage_helper PARAMS ((tree *, int *, void *));
46 static tree build_srcloc PARAMS ((const char *, int));
47 static tree mark_local_for_remap_r PARAMS ((tree *, int *, void *));
48 static tree cp_unsave_r PARAMS ((tree *, int *, void *));
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 extern int cp_statement_code_p PARAMS ((enum tree_code));
55 static tree handle_java_interface_attribute PARAMS ((tree *, tree, tree, int, bool *));
56 static tree handle_com_interface_attribute PARAMS ((tree *, tree, tree, int, bool *));
57 static tree handle_init_priority_attribute PARAMS ((tree *, tree, tree, int, bool *));
59 /* If REF is an lvalue, returns the kind of lvalue that REF is.
60 Otherwise, returns clk_none. If TREAT_CLASS_RVALUES_AS_LVALUES is
61 nonzero, rvalues of class type are considered lvalues. */
63 static cp_lvalue_kind
64 lvalue_p_1 (ref, treat_class_rvalues_as_lvalues, allow_cast_as_lvalue)
65 tree ref;
66 int treat_class_rvalues_as_lvalues;
67 int allow_cast_as_lvalue;
69 cp_lvalue_kind op1_lvalue_kind = clk_none;
70 cp_lvalue_kind op2_lvalue_kind = clk_none;
72 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
73 return clk_ordinary;
75 if (ref == current_class_ptr)
76 return clk_none;
78 switch (TREE_CODE (ref))
80 /* preincrements and predecrements are valid lvals, provided
81 what they refer to are valid lvals. */
82 case PREINCREMENT_EXPR:
83 case PREDECREMENT_EXPR:
84 case SAVE_EXPR:
85 case UNSAVE_EXPR:
86 case TRY_CATCH_EXPR:
87 case WITH_CLEANUP_EXPR:
88 case REALPART_EXPR:
89 case IMAGPART_EXPR:
90 return lvalue_p_1 (TREE_OPERAND (ref, 0),
91 treat_class_rvalues_as_lvalues,
92 allow_cast_as_lvalue);
94 case NOP_EXPR:
95 if (allow_cast_as_lvalue)
96 return lvalue_p_1 (TREE_OPERAND (ref, 0),
97 treat_class_rvalues_as_lvalues,
98 allow_cast_as_lvalue);
99 else
100 return clk_none;
102 case COMPONENT_REF:
103 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
104 treat_class_rvalues_as_lvalues,
105 allow_cast_as_lvalue);
106 if (op1_lvalue_kind
107 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
108 situations. */
109 && TREE_CODE (TREE_OPERAND (ref, 1)) == FIELD_DECL
110 && DECL_C_BIT_FIELD (TREE_OPERAND (ref, 1)))
112 /* Clear the ordinary bit. If this object was a class
113 rvalue we want to preserve that information. */
114 op1_lvalue_kind &= ~clk_ordinary;
115 /* The lvalue is for a btifield. */
116 op1_lvalue_kind |= clk_bitfield;
118 return op1_lvalue_kind;
120 case STRING_CST:
121 return clk_ordinary;
123 case VAR_DECL:
124 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
125 && DECL_LANG_SPECIFIC (ref)
126 && DECL_IN_AGGR_P (ref))
127 return clk_none;
128 case INDIRECT_REF:
129 case ARRAY_REF:
130 case PARM_DECL:
131 case RESULT_DECL:
132 if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
133 return clk_ordinary;
134 break;
136 /* A currently unresolved scope ref. */
137 case SCOPE_REF:
138 abort ();
139 case OFFSET_REF:
140 if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
141 return clk_ordinary;
142 /* Fall through. */
143 case MAX_EXPR:
144 case MIN_EXPR:
145 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
146 treat_class_rvalues_as_lvalues,
147 allow_cast_as_lvalue);
148 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
149 treat_class_rvalues_as_lvalues,
150 allow_cast_as_lvalue);
151 break;
153 case COND_EXPR:
154 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
155 treat_class_rvalues_as_lvalues,
156 allow_cast_as_lvalue);
157 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 2),
158 treat_class_rvalues_as_lvalues,
159 allow_cast_as_lvalue);
160 break;
162 case MODIFY_EXPR:
163 return clk_ordinary;
165 case COMPOUND_EXPR:
166 return lvalue_p_1 (TREE_OPERAND (ref, 1),
167 treat_class_rvalues_as_lvalues,
168 allow_cast_as_lvalue);
170 case TARGET_EXPR:
171 return treat_class_rvalues_as_lvalues ? clk_class : clk_none;
173 case CALL_EXPR:
174 case VA_ARG_EXPR:
175 /* Any class-valued call would be wrapped in a TARGET_EXPR. */
176 return clk_none;
178 case FUNCTION_DECL:
179 /* All functions (except non-static-member functions) are
180 lvalues. */
181 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
182 ? clk_none : clk_ordinary);
184 default:
185 break;
188 /* If one operand is not an lvalue at all, then this expression is
189 not an lvalue. */
190 if (!op1_lvalue_kind || !op2_lvalue_kind)
191 return clk_none;
193 /* Otherwise, it's an lvalue, and it has all the odd properties
194 contributed by either operand. */
195 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
196 /* It's not an ordinary lvalue if it involves either a bit-field or
197 a class rvalue. */
198 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
199 op1_lvalue_kind &= ~clk_ordinary;
200 return op1_lvalue_kind;
203 /* If REF is an lvalue, returns the kind of lvalue that REF is.
204 Otherwise, returns clk_none. Lvalues can be assigned, unless they
205 have TREE_READONLY, or unless they are FUNCTION_DECLs. Lvalues can
206 have their address taken, unless they have DECL_REGISTER. */
208 cp_lvalue_kind
209 real_lvalue_p (ref)
210 tree ref;
212 return lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/ 0, /*cast*/ 1);
215 /* This differs from real_lvalue_p in that class rvalues are
216 considered lvalues. */
219 lvalue_p (ref)
220 tree ref;
222 return
223 (lvalue_p_1 (ref, /*class rvalue ok*/ 1, /*cast*/ 1) != clk_none);
227 non_cast_lvalue_p (ref)
228 tree ref;
230 return
231 (lvalue_p_1 (ref, /*class rvalue ok*/ 1, /*cast*/ 0) != clk_none);
234 /* Return nonzero if REF is an lvalue valid for this language;
235 otherwise, print an error message and return zero. */
238 lvalue_or_else (ref, string)
239 tree ref;
240 const char *string;
242 int ret = lvalue_p_1 (ref, /* class rvalue ok */ 1, /* cast ok */ 1);
243 int win = (ret != clk_none);
244 if (! win)
245 error ("non-lvalue in %s", string);
246 return win;
250 non_cast_lvalue_or_else (ref, string)
251 tree ref;
252 const char *string;
254 int ret = lvalue_p_1 (ref, /* class rvalue ok */ 1, /* cast ok */ 0);
255 int win = (ret != clk_none);
256 if (! win)
257 error ("non-lvalue in %s", string);
258 return win;
261 /* Build a TARGET_EXPR, initializing the DECL with the VALUE. */
263 static tree
264 build_target_expr (decl, value)
265 tree decl;
266 tree value;
268 tree t;
270 t = build (TARGET_EXPR, TREE_TYPE (decl), decl, value,
271 cxx_maybe_build_cleanup (decl), NULL_TREE);
272 /* We always set TREE_SIDE_EFFECTS so that expand_expr does not
273 ignore the TARGET_EXPR. If there really turn out to be no
274 side-effects, then the optimizer should be able to get rid of
275 whatever code is generated anyhow. */
276 TREE_SIDE_EFFECTS (t) = 1;
278 return t;
281 /* INIT is a CALL_EXPR which needs info about its target.
282 TYPE is the type that this initialization should appear to have.
284 Build an encapsulation of the initialization to perform
285 and return it so that it can be processed by language-independent
286 and language-specific expression expanders. */
288 tree
289 build_cplus_new (type, init)
290 tree type;
291 tree init;
293 tree fn;
294 tree slot;
295 tree rval;
296 int is_ctor;
298 /* Make sure that we're not trying to create an instance of an
299 abstract class. */
300 abstract_virtuals_error (NULL_TREE, type);
302 if (TREE_CODE (init) != CALL_EXPR && TREE_CODE (init) != AGGR_INIT_EXPR)
303 return convert (type, init);
305 fn = TREE_OPERAND (init, 0);
306 is_ctor = (TREE_CODE (fn) == ADDR_EXPR
307 && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
308 && DECL_CONSTRUCTOR_P (TREE_OPERAND (fn, 0)));
310 slot = build (VAR_DECL, type);
311 DECL_ARTIFICIAL (slot) = 1;
312 DECL_CONTEXT (slot) = current_function_decl;
313 layout_decl (slot, 0);
315 /* We split the CALL_EXPR into its function and its arguments here.
316 Then, in expand_expr, we put them back together. The reason for
317 this is that this expression might be a default argument
318 expression. In that case, we need a new temporary every time the
319 expression is used. That's what break_out_target_exprs does; it
320 replaces every AGGR_INIT_EXPR with a copy that uses a fresh
321 temporary slot. Then, expand_expr builds up a call-expression
322 using the new slot. */
324 /* If we don't need to use a constructor to create an object of this
325 type, don't mess with AGGR_INIT_EXPR. */
326 if (is_ctor || TREE_ADDRESSABLE (type))
328 rval = build (AGGR_INIT_EXPR, type, fn, TREE_OPERAND (init, 1), slot);
329 TREE_SIDE_EFFECTS (rval) = 1;
330 AGGR_INIT_VIA_CTOR_P (rval) = is_ctor;
332 else
333 rval = init;
335 rval = build_target_expr (slot, rval);
337 return rval;
340 /* Build a TARGET_EXPR using INIT to initialize a new temporary of the
341 indicated TYPE. */
343 tree
344 build_target_expr_with_type (init, type)
345 tree init;
346 tree type;
348 tree slot;
349 tree rval;
351 if (TREE_CODE (init) == TARGET_EXPR)
352 return init;
354 slot = build (VAR_DECL, type);
355 DECL_ARTIFICIAL (slot) = 1;
356 DECL_CONTEXT (slot) = current_function_decl;
357 layout_decl (slot, 0);
358 rval = build_target_expr (slot, init);
360 return rval;
363 /* Like build_target_expr_with_type, but use the type of INIT. */
365 tree
366 get_target_expr (init)
367 tree init;
369 return build_target_expr_with_type (init, TREE_TYPE (init));
372 /* Recursively perform a preorder search EXP for CALL_EXPRs, making
373 copies where they are found. Returns a deep copy all nodes transitively
374 containing CALL_EXPRs. */
376 tree
377 break_out_calls (exp)
378 tree exp;
380 register tree t1, t2 = NULL_TREE;
381 register enum tree_code code;
382 register int changed = 0;
383 register int i;
385 if (exp == NULL_TREE)
386 return exp;
388 code = TREE_CODE (exp);
390 if (code == CALL_EXPR)
391 return copy_node (exp);
393 /* Don't try and defeat a save_expr, as it should only be done once. */
394 if (code == SAVE_EXPR)
395 return exp;
397 switch (TREE_CODE_CLASS (code))
399 default:
400 abort ();
402 case 'c': /* a constant */
403 case 't': /* a type node */
404 case 'x': /* something random, like an identifier or an ERROR_MARK. */
405 return exp;
407 case 'd': /* A decl node */
408 return exp;
410 case 'b': /* A block node */
412 /* Don't know how to handle these correctly yet. Must do a
413 break_out_calls on all DECL_INITIAL values for local variables,
414 and also break_out_calls on all sub-blocks and sub-statements. */
415 abort ();
417 return exp;
419 case 'e': /* an expression */
420 case 'r': /* a reference */
421 case 's': /* an expression with side effects */
422 for (i = TREE_CODE_LENGTH (code) - 1; i >= 0; i--)
424 t1 = break_out_calls (TREE_OPERAND (exp, i));
425 if (t1 != TREE_OPERAND (exp, i))
427 exp = copy_node (exp);
428 TREE_OPERAND (exp, i) = t1;
431 return exp;
433 case '<': /* a comparison expression */
434 case '2': /* a binary arithmetic expression */
435 t2 = break_out_calls (TREE_OPERAND (exp, 1));
436 if (t2 != TREE_OPERAND (exp, 1))
437 changed = 1;
438 case '1': /* a unary arithmetic expression */
439 t1 = break_out_calls (TREE_OPERAND (exp, 0));
440 if (t1 != TREE_OPERAND (exp, 0))
441 changed = 1;
442 if (changed)
444 if (TREE_CODE_LENGTH (code) == 1)
445 return build1 (code, TREE_TYPE (exp), t1);
446 else
447 return build (code, TREE_TYPE (exp), t1, t2);
449 return exp;
454 /* Construct, lay out and return the type of methods belonging to class
455 BASETYPE and whose arguments are described by ARGTYPES and whose values
456 are described by RETTYPE. If each type exists already, reuse it. */
458 tree
459 build_cplus_method_type (basetype, rettype, argtypes)
460 tree basetype, rettype, argtypes;
462 register tree t;
463 tree ptype;
464 int hashcode;
466 /* Make a node of the sort we want. */
467 t = make_node (METHOD_TYPE);
469 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
470 TREE_TYPE (t) = rettype;
471 ptype = build_pointer_type (basetype);
473 /* The actual arglist for this function includes a "hidden" argument
474 which is "this". Put it into the list of argument types. */
475 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
476 TYPE_ARG_TYPES (t) = argtypes;
477 TREE_SIDE_EFFECTS (argtypes) = 1; /* Mark first argtype as "artificial". */
479 /* If we already have such a type, use the old one and free this one.
480 Note that it also frees up the above cons cell if found. */
481 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
482 type_hash_list (argtypes);
484 t = type_hash_canon (hashcode, t);
486 if (!COMPLETE_TYPE_P (t))
487 layout_type (t);
489 return t;
492 static tree
493 build_cplus_array_type_1 (elt_type, index_type)
494 tree elt_type;
495 tree index_type;
497 tree t;
499 if (elt_type == error_mark_node || index_type == error_mark_node)
500 return error_mark_node;
502 /* Don't do the minimal thing just because processing_template_decl is
503 set; we want to give string constants the right type immediately, so
504 we don't have to fix them up at instantiation time. */
505 if ((processing_template_decl
506 && index_type && TYPE_MAX_VALUE (index_type)
507 && TREE_CODE (TYPE_MAX_VALUE (index_type)) != INTEGER_CST)
508 || uses_template_parms (elt_type)
509 || uses_template_parms (index_type))
511 t = make_node (ARRAY_TYPE);
512 TREE_TYPE (t) = elt_type;
513 TYPE_DOMAIN (t) = index_type;
515 else
516 t = build_array_type (elt_type, index_type);
518 /* Push these needs up so that initialization takes place
519 more easily. */
520 TYPE_NEEDS_CONSTRUCTING (t)
521 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
522 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
523 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
524 return t;
527 tree
528 build_cplus_array_type (elt_type, index_type)
529 tree elt_type;
530 tree index_type;
532 tree t;
533 int type_quals = cp_type_quals (elt_type);
534 int cv_quals = type_quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
535 int other_quals = type_quals & ~(TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE);
537 if (cv_quals)
538 elt_type = cp_build_qualified_type (elt_type, other_quals);
540 t = build_cplus_array_type_1 (elt_type, index_type);
542 if (cv_quals)
543 t = cp_build_qualified_type (t, cv_quals);
545 return t;
548 /* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
549 arrays correctly. In particular, if TYPE is an array of T's, and
550 TYPE_QUALS is non-empty, returns an array of qualified T's.
552 FLAGS determines how to deal with illformed qualifications. If
553 tf_ignore_bad_quals is set, then bad qualifications are dropped
554 (this is permitted if TYPE was introduced via a typedef or template
555 type parameter). If bad qualifications are dropped and tf_warning
556 is set, then a warning is issued for non-const qualifications. If
557 tf_ignore_bad_quals is not set and tf_error is not set, we
558 return error_mark_node. Otherwise, we issue an error, and ignore
559 the qualifications.
561 Qualification of a reference type is valid when the reference came
562 via a typedef or template type argument. [dcl.ref] No such
563 dispensation is provided for qualifying a function type. [dcl.fct]
564 DR 295 queries this and the proposed resolution brings it into line
565 with qualifiying a reference. We implement the DR. We also behave
566 in a similar manner for restricting non-pointer types. */
568 tree
569 cp_build_qualified_type_real (type, type_quals, complain)
570 tree type;
571 int type_quals;
572 tsubst_flags_t complain;
574 tree result;
575 int bad_quals = TYPE_UNQUALIFIED;
576 /* We keep bad function qualifiers separate, so that we can decide
577 whether to implement DR 295 or not. DR 295 break existing code,
578 unfortunately. Remove this variable to implement the defect
579 report. */
580 int bad_func_quals = TYPE_UNQUALIFIED;
582 if (type == error_mark_node)
583 return type;
585 if (type_quals == cp_type_quals (type))
586 return type;
588 /* A reference, fucntion or method type shall not be cv qualified.
589 [dcl.ref], [dct.fct] */
590 if (type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)
591 && (TREE_CODE (type) == REFERENCE_TYPE
592 || TREE_CODE (type) == FUNCTION_TYPE
593 || TREE_CODE (type) == METHOD_TYPE))
595 bad_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
596 if (TREE_CODE (type) != REFERENCE_TYPE)
597 bad_func_quals |= type_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
598 type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
601 /* A restrict-qualified type must be a pointer (or reference)
602 to object or incomplete type. */
603 if ((type_quals & TYPE_QUAL_RESTRICT)
604 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
605 && TREE_CODE (type) != TYPENAME_TYPE
606 && !POINTER_TYPE_P (type))
608 bad_quals |= TYPE_QUAL_RESTRICT;
609 type_quals &= ~TYPE_QUAL_RESTRICT;
612 if (bad_quals == TYPE_UNQUALIFIED)
613 /*OK*/;
614 else if (!(complain & (tf_error | tf_ignore_bad_quals)))
615 return error_mark_node;
616 else if (bad_func_quals && !(complain & tf_error))
617 return error_mark_node;
618 else
620 if (complain & tf_ignore_bad_quals)
621 /* We're not going to warn about constifying things that can't
622 be constified. */
623 bad_quals &= ~TYPE_QUAL_CONST;
624 bad_quals |= bad_func_quals;
625 if (bad_quals)
627 tree bad_type = build_qualified_type (ptr_type_node, bad_quals);
629 if (!(complain & tf_ignore_bad_quals)
630 || bad_func_quals)
631 error ("`%V' qualifiers cannot be applied to `%T'",
632 bad_type, type);
636 if (TREE_CODE (type) == ARRAY_TYPE)
638 /* In C++, the qualification really applies to the array element
639 type. Obtain the appropriately qualified element type. */
640 tree t;
641 tree element_type
642 = cp_build_qualified_type_real (TREE_TYPE (type),
643 type_quals,
644 complain);
646 if (element_type == error_mark_node)
647 return error_mark_node;
649 /* See if we already have an identically qualified type. */
650 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
651 if (cp_type_quals (t) == type_quals
652 && TYPE_NAME (t) == TYPE_NAME (type)
653 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type))
654 break;
656 if (!t)
658 /* Make a new array type, just like the old one, but with the
659 appropriately qualified element type. */
660 t = build_type_copy (type);
661 TREE_TYPE (t) = element_type;
664 /* Even if we already had this variant, we update
665 TYPE_NEEDS_CONSTRUCTING and TYPE_HAS_NONTRIVIAL_DESTRUCTOR in case
666 they changed since the variant was originally created.
668 This seems hokey; if there is some way to use a previous
669 variant *without* coming through here,
670 TYPE_NEEDS_CONSTRUCTING will never be updated. */
671 TYPE_NEEDS_CONSTRUCTING (t)
672 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
673 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
674 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
675 return t;
677 else if (TYPE_PTRMEMFUNC_P (type))
679 /* For a pointer-to-member type, we can't just return a
680 cv-qualified version of the RECORD_TYPE. If we do, we
681 haven't changed the field that contains the actual pointer to
682 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
683 tree t;
685 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
686 t = cp_build_qualified_type_real (t, type_quals, complain);
687 return build_ptrmemfunc_type (t);
690 /* Retrieve (or create) the appropriately qualified variant. */
691 result = build_qualified_type (type, type_quals);
693 /* If this was a pointer-to-method type, and we just made a copy,
694 then we need to unshare the record that holds the cached
695 pointer-to-member-function type, because these will be distinct
696 between the unqualified and qualified types. */
697 if (result != type
698 && TREE_CODE (type) == POINTER_TYPE
699 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
700 TYPE_LANG_SPECIFIC (result) = NULL;
702 return result;
705 /* Returns the canonical version of TYPE. In other words, if TYPE is
706 a typedef, returns the underlying type. The cv-qualification of
707 the type returned matches the type input; they will always be
708 compatible types. */
710 tree
711 canonical_type_variant (t)
712 tree t;
714 return cp_build_qualified_type (TYPE_MAIN_VARIANT (t), cp_type_quals (t));
717 /* Makes new binfos for the indirect bases under BINFO. T is the most
718 derived TYPE. PREV is the previous binfo, whose TREE_CHAIN we make
719 point to this binfo. We return the last BINFO created.
721 The CLASSTYPE_VBASECLASSES list of T is constructed in reverse
722 order (pre-order, depth-first, right-to-left). You must nreverse it.
724 The BINFO_INHERITANCE of a virtual base class points to the binfo
725 og the most derived type.
727 The binfo's TREE_CHAIN is set to inheritance graph order, but bases
728 for non-class types are not included (i.e. those which are
729 dependent bases in non-instantiated templates). */
731 tree
732 copy_base_binfos (binfo, t, prev)
733 tree binfo, t, prev;
735 tree binfos = BINFO_BASETYPES (binfo);
736 int n, ix;
738 if (prev)
739 TREE_CHAIN (prev) = binfo;
740 prev = binfo;
742 if (binfos == NULL_TREE)
743 return prev;
745 n = TREE_VEC_LENGTH (binfos);
747 /* Now copy the structure beneath BINFO. */
748 for (ix = 0; ix != n; ix++)
750 tree base_binfo = TREE_VEC_ELT (binfos, ix);
751 tree new_binfo = NULL_TREE;
753 if (!CLASS_TYPE_P (BINFO_TYPE (base_binfo)))
755 my_friendly_assert (binfo == TYPE_BINFO (t), 20030204);
757 new_binfo = base_binfo;
758 TREE_CHAIN (prev) = new_binfo;
759 prev = new_binfo;
760 BINFO_INHERITANCE_CHAIN (new_binfo) = binfo;
761 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
763 else if (TREE_VIA_VIRTUAL (base_binfo))
765 new_binfo = purpose_member (BINFO_TYPE (base_binfo),
766 CLASSTYPE_VBASECLASSES (t));
767 if (new_binfo)
768 new_binfo = TREE_VALUE (new_binfo);
771 if (!new_binfo)
773 new_binfo = make_binfo (BINFO_OFFSET (base_binfo),
774 base_binfo, NULL_TREE,
775 BINFO_VIRTUALS (base_binfo));
776 prev = copy_base_binfos (new_binfo, t, prev);
777 if (TREE_VIA_VIRTUAL (base_binfo))
779 CLASSTYPE_VBASECLASSES (t)
780 = tree_cons (BINFO_TYPE (new_binfo), new_binfo,
781 CLASSTYPE_VBASECLASSES (t));
782 TREE_VIA_VIRTUAL (new_binfo) = 1;
783 BINFO_INHERITANCE_CHAIN (new_binfo) = TYPE_BINFO (t);
785 else
786 BINFO_INHERITANCE_CHAIN (new_binfo) = binfo;
788 TREE_VEC_ELT (binfos, ix) = new_binfo;
791 return prev;
795 /* Hashing of lists so that we don't make duplicates.
796 The entry point is `list_hash_canon'. */
798 /* Now here is the hash table. When recording a list, it is added
799 to the slot whose index is the hash code mod the table size.
800 Note that the hash table is used for several kinds of lists.
801 While all these live in the same table, they are completely independent,
802 and the hash code is computed differently for each of these. */
804 static GTY ((param_is (union tree_node))) htab_t list_hash_table;
806 struct list_proxy
808 tree purpose;
809 tree value;
810 tree chain;
813 /* Compare ENTRY (an entry in the hash table) with DATA (a list_proxy
814 for a node we are thinking about adding). */
816 static int
817 list_hash_eq (entry, data)
818 const void *entry;
819 const void *data;
821 tree t = (tree) entry;
822 struct list_proxy *proxy = (struct list_proxy *) data;
824 return (TREE_VALUE (t) == proxy->value
825 && TREE_PURPOSE (t) == proxy->purpose
826 && TREE_CHAIN (t) == proxy->chain);
829 /* Compute a hash code for a list (chain of TREE_LIST nodes
830 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
831 TREE_COMMON slots), by adding the hash codes of the individual entries. */
833 static hashval_t
834 list_hash_pieces (purpose, value, chain)
835 tree purpose;
836 tree value;
837 tree chain;
839 hashval_t hashcode = 0;
841 if (chain)
842 hashcode += TYPE_HASH (chain);
844 if (value)
845 hashcode += TYPE_HASH (value);
846 else
847 hashcode += 1007;
848 if (purpose)
849 hashcode += TYPE_HASH (purpose);
850 else
851 hashcode += 1009;
852 return hashcode;
855 /* Hash an already existing TREE_LIST. */
857 static hashval_t
858 list_hash (p)
859 const void *p;
861 tree t = (tree) p;
862 return list_hash_pieces (TREE_PURPOSE (t),
863 TREE_VALUE (t),
864 TREE_CHAIN (t));
867 /* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
868 object for an identical list if one already exists. Otherwise, build a
869 new one, and record it as the canonical object. */
871 tree
872 hash_tree_cons (purpose, value, chain)
873 tree purpose, value, chain;
875 int hashcode = 0;
876 PTR* slot;
877 struct list_proxy proxy;
879 /* Hash the list node. */
880 hashcode = list_hash_pieces (purpose, value, chain);
881 /* Create a proxy for the TREE_LIST we would like to create. We
882 don't actually create it so as to avoid creating garbage. */
883 proxy.purpose = purpose;
884 proxy.value = value;
885 proxy.chain = chain;
886 /* See if it is already in the table. */
887 slot = htab_find_slot_with_hash (list_hash_table, &proxy, hashcode,
888 INSERT);
889 /* If not, create a new node. */
890 if (!*slot)
891 *slot = (PTR) tree_cons (purpose, value, chain);
892 return *slot;
895 /* Constructor for hashed lists. */
897 tree
898 hash_tree_chain (value, chain)
899 tree value, chain;
901 return hash_tree_cons (NULL_TREE, value, chain);
904 /* Similar, but used for concatenating two lists. */
906 tree
907 hash_chainon (list1, list2)
908 tree list1, list2;
910 if (list2 == 0)
911 return list1;
912 if (list1 == 0)
913 return list2;
914 if (TREE_CHAIN (list1) == NULL_TREE)
915 return hash_tree_chain (TREE_VALUE (list1), list2);
916 return hash_tree_chain (TREE_VALUE (list1),
917 hash_chainon (TREE_CHAIN (list1), list2));
920 /* Build an association between TYPE and some parameters:
922 OFFSET is the offset added to `this' to convert it to a pointer
923 of type `TYPE *'
925 BINFO is the base binfo to use, if we are deriving from one. This
926 is necessary, as we want specialized parent binfos from base
927 classes, so that the VTABLE_NAMEs of bases are for the most derived
928 type, instead of the simple type.
930 VTABLE is the virtual function table with which to initialize
931 sub-objects of type TYPE.
933 VIRTUALS are the virtual functions sitting in VTABLE. */
935 tree
936 make_binfo (offset, binfo, vtable, virtuals)
937 tree offset, binfo;
938 tree vtable, virtuals;
940 tree new_binfo = make_tree_vec (BINFO_LANG_ELTS);
941 tree type;
943 if (TREE_CODE (binfo) == TREE_VEC)
945 type = BINFO_TYPE (binfo);
946 BINFO_DEPENDENT_BASE_P (new_binfo) = BINFO_DEPENDENT_BASE_P (binfo);
948 else
950 type = binfo;
951 binfo = NULL_TREE;
952 BINFO_DEPENDENT_BASE_P (new_binfo) = 1;
955 TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
956 BINFO_OFFSET (new_binfo) = offset;
957 BINFO_VTABLE (new_binfo) = vtable;
958 BINFO_VIRTUALS (new_binfo) = virtuals;
960 if (binfo && !BINFO_DEPENDENT_BASE_P (binfo)
961 && BINFO_BASETYPES (binfo) != NULL_TREE)
963 BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));
964 /* We do not need to copy the accesses, as they are read only. */
965 BINFO_BASEACCESSES (new_binfo) = BINFO_BASEACCESSES (binfo);
967 return new_binfo;
970 void
971 debug_binfo (elem)
972 tree elem;
974 HOST_WIDE_INT n;
975 tree virtuals;
977 fprintf (stderr, "type \"%s\", offset = ",
978 TYPE_NAME_STRING (BINFO_TYPE (elem)));
979 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
980 TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
981 fprintf (stderr, "\nvtable type:\n");
982 debug_tree (BINFO_TYPE (elem));
983 if (BINFO_VTABLE (elem))
984 fprintf (stderr, "vtable decl \"%s\"\n",
985 IDENTIFIER_POINTER (DECL_NAME (get_vtbl_decl_for_binfo (elem))));
986 else
987 fprintf (stderr, "no vtable decl yet\n");
988 fprintf (stderr, "virtuals:\n");
989 virtuals = BINFO_VIRTUALS (elem);
990 n = 0;
992 while (virtuals)
994 tree fndecl = TREE_VALUE (virtuals);
995 fprintf (stderr, "%s [%ld =? %ld]\n",
996 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
997 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
998 ++n;
999 virtuals = TREE_CHAIN (virtuals);
1004 count_functions (t)
1005 tree t;
1007 int i;
1008 if (TREE_CODE (t) == FUNCTION_DECL)
1009 return 1;
1010 else if (TREE_CODE (t) == OVERLOAD)
1012 for (i=0; t; t = OVL_CHAIN (t))
1013 i++;
1014 return i;
1017 abort ();
1018 return 0;
1022 is_overloaded_fn (x)
1023 tree x;
1025 /* A baselink is also considered an overloaded function. */
1026 if (TREE_CODE (x) == OFFSET_REF)
1027 x = TREE_OPERAND (x, 1);
1028 if (BASELINK_P (x))
1029 x = BASELINK_FUNCTIONS (x);
1030 return (TREE_CODE (x) == FUNCTION_DECL
1031 || TREE_CODE (x) == TEMPLATE_ID_EXPR
1032 || DECL_FUNCTION_TEMPLATE_P (x)
1033 || TREE_CODE (x) == OVERLOAD);
1037 really_overloaded_fn (x)
1038 tree x;
1040 /* A baselink is also considered an overloaded function. */
1041 if (TREE_CODE (x) == OFFSET_REF)
1042 x = TREE_OPERAND (x, 1);
1043 if (BASELINK_P (x))
1044 x = BASELINK_FUNCTIONS (x);
1046 return ((TREE_CODE (x) == OVERLOAD && OVL_CHAIN (x))
1047 || DECL_FUNCTION_TEMPLATE_P (OVL_CURRENT (x))
1048 || TREE_CODE (x) == TEMPLATE_ID_EXPR);
1051 tree
1052 get_first_fn (from)
1053 tree from;
1055 my_friendly_assert (is_overloaded_fn (from), 9);
1056 /* A baselink is also considered an overloaded function. */
1057 if (BASELINK_P (from))
1058 from = BASELINK_FUNCTIONS (from);
1059 return OVL_CURRENT (from);
1062 /* Returns nonzero if T is a ->* or .* expression that refers to a
1063 member function. */
1066 bound_pmf_p (t)
1067 tree t;
1069 return (TREE_CODE (t) == OFFSET_REF
1070 && TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (t, 1))));
1073 /* Return a new OVL node, concatenating it with the old one. */
1075 tree
1076 ovl_cons (decl, chain)
1077 tree decl;
1078 tree chain;
1080 tree result = make_node (OVERLOAD);
1081 TREE_TYPE (result) = unknown_type_node;
1082 OVL_FUNCTION (result) = decl;
1083 TREE_CHAIN (result) = chain;
1085 return result;
1088 /* Build a new overloaded function. If this is the first one,
1089 just return it; otherwise, ovl_cons the _DECLs */
1091 tree
1092 build_overload (decl, chain)
1093 tree decl;
1094 tree chain;
1096 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
1097 return decl;
1098 if (chain && TREE_CODE (chain) != OVERLOAD)
1099 chain = ovl_cons (chain, NULL_TREE);
1100 return ovl_cons (decl, chain);
1104 is_aggr_type_2 (t1, t2)
1105 tree t1, t2;
1107 if (TREE_CODE (t1) != TREE_CODE (t2))
1108 return 0;
1109 return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1112 /* Returns nonzero if CODE is the code for a statement. */
1115 cp_statement_code_p (code)
1116 enum tree_code code;
1118 switch (code)
1120 case CTOR_INITIALIZER:
1121 case TRY_BLOCK:
1122 case HANDLER:
1123 case EH_SPEC_BLOCK:
1124 case USING_STMT:
1125 case TAG_DEFN:
1126 return 1;
1128 default:
1129 return 0;
1133 #define PRINT_RING_SIZE 4
1135 const char *
1136 cxx_printable_name (decl, v)
1137 tree decl;
1138 int v;
1140 static tree decl_ring[PRINT_RING_SIZE];
1141 static char *print_ring[PRINT_RING_SIZE];
1142 static int ring_counter;
1143 int i;
1145 /* Only cache functions. */
1146 if (v < 2
1147 || TREE_CODE (decl) != FUNCTION_DECL
1148 || DECL_LANG_SPECIFIC (decl) == 0)
1149 return lang_decl_name (decl, v);
1151 /* See if this print name is lying around. */
1152 for (i = 0; i < PRINT_RING_SIZE; i++)
1153 if (decl_ring[i] == decl)
1154 /* yes, so return it. */
1155 return print_ring[i];
1157 if (++ring_counter == PRINT_RING_SIZE)
1158 ring_counter = 0;
1160 if (current_function_decl != NULL_TREE)
1162 if (decl_ring[ring_counter] == current_function_decl)
1163 ring_counter += 1;
1164 if (ring_counter == PRINT_RING_SIZE)
1165 ring_counter = 0;
1166 if (decl_ring[ring_counter] == current_function_decl)
1167 abort ();
1170 if (print_ring[ring_counter])
1171 free (print_ring[ring_counter]);
1173 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v));
1174 decl_ring[ring_counter] = decl;
1175 return print_ring[ring_counter];
1178 /* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
1179 listed in RAISES. */
1181 tree
1182 build_exception_variant (type, raises)
1183 tree type;
1184 tree raises;
1186 tree v = TYPE_MAIN_VARIANT (type);
1187 int type_quals = TYPE_QUALS (type);
1189 for (; v; v = TYPE_NEXT_VARIANT (v))
1190 if (TYPE_QUALS (v) == type_quals
1191 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), 1))
1192 return v;
1194 /* Need to build a new variant. */
1195 v = build_type_copy (type);
1196 TYPE_RAISES_EXCEPTIONS (v) = raises;
1197 return v;
1200 /* Given a TEMPLATE_TEMPLATE_PARM node T, create a new
1201 BOUND_TEMPLATE_TEMPLATE_PARM bound with NEWARGS as its template
1202 arguments. */
1204 tree
1205 bind_template_template_parm (t, newargs)
1206 tree t;
1207 tree newargs;
1209 tree decl = TYPE_NAME (t);
1210 tree t2;
1212 t2 = make_aggr_type (BOUND_TEMPLATE_TEMPLATE_PARM);
1213 decl = build_decl (TYPE_DECL, DECL_NAME (decl), NULL_TREE);
1215 /* These nodes have to be created to reflect new TYPE_DECL and template
1216 arguments. */
1217 TEMPLATE_TYPE_PARM_INDEX (t2) = copy_node (TEMPLATE_TYPE_PARM_INDEX (t));
1218 TEMPLATE_PARM_DECL (TEMPLATE_TYPE_PARM_INDEX (t2)) = decl;
1219 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1220 = tree_cons (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t),
1221 newargs, NULL_TREE);
1223 TREE_TYPE (decl) = t2;
1224 TYPE_NAME (t2) = decl;
1225 TYPE_STUB_DECL (t2) = decl;
1226 TYPE_SIZE (t2) = 0;
1228 return t2;
1231 /* Called from count_trees via walk_tree. */
1233 static tree
1234 count_trees_r (tp, walk_subtrees, data)
1235 tree *tp ATTRIBUTE_UNUSED;
1236 int *walk_subtrees ATTRIBUTE_UNUSED;
1237 void *data;
1239 ++ *((int*) data);
1240 return NULL_TREE;
1243 /* Debugging function for measuring the rough complexity of a tree
1244 representation. */
1247 count_trees (t)
1248 tree t;
1250 int n_trees = 0;
1251 walk_tree_without_duplicates (&t, count_trees_r, &n_trees);
1252 return n_trees;
1255 /* Called from verify_stmt_tree via walk_tree. */
1257 static tree
1258 verify_stmt_tree_r (tp, walk_subtrees, data)
1259 tree *tp;
1260 int *walk_subtrees ATTRIBUTE_UNUSED;
1261 void *data;
1263 tree t = *tp;
1264 htab_t *statements = (htab_t *) data;
1265 void **slot;
1267 if (!statement_code_p (TREE_CODE (t)))
1268 return NULL_TREE;
1270 /* If this statement is already present in the hash table, then
1271 there is a circularity in the statement tree. */
1272 if (htab_find (*statements, t))
1273 abort ();
1275 slot = htab_find_slot (*statements, t, INSERT);
1276 *slot = t;
1278 return NULL_TREE;
1281 /* Debugging function to check that the statement T has not been
1282 corrupted. For now, this function simply checks that T contains no
1283 circularities. */
1285 void
1286 verify_stmt_tree (t)
1287 tree t;
1289 htab_t statements;
1290 statements = htab_create (37, htab_hash_pointer, htab_eq_pointer, NULL);
1291 walk_tree (&t, verify_stmt_tree_r, &statements, NULL);
1292 htab_delete (statements);
1295 /* Called from find_tree via walk_tree. */
1297 static tree
1298 find_tree_r (tp, walk_subtrees, data)
1299 tree *tp;
1300 int *walk_subtrees ATTRIBUTE_UNUSED;
1301 void *data;
1303 if (*tp == (tree) data)
1304 return (tree) data;
1306 return NULL_TREE;
1309 /* Returns X if X appears in the tree structure rooted at T. */
1311 tree
1312 find_tree (t, x)
1313 tree t;
1314 tree x;
1316 return walk_tree_without_duplicates (&t, find_tree_r, x);
1319 /* Passed to walk_tree. Checks for the use of types with no linkage. */
1321 static tree
1322 no_linkage_helper (tp, walk_subtrees, data)
1323 tree *tp;
1324 int *walk_subtrees ATTRIBUTE_UNUSED;
1325 void *data ATTRIBUTE_UNUSED;
1327 tree t = *tp;
1329 if (TYPE_P (t)
1330 && (CLASS_TYPE_P (t) || TREE_CODE (t) == ENUMERAL_TYPE)
1331 && (decl_function_context (TYPE_MAIN_DECL (t))
1332 || TYPE_ANONYMOUS_P (t)))
1333 return t;
1334 return NULL_TREE;
1337 /* Check if the type T depends on a type with no linkage and if so, return
1338 it. */
1340 tree
1341 no_linkage_check (t)
1342 tree t;
1344 /* There's no point in checking linkage on template functions; we
1345 can't know their complete types. */
1346 if (processing_template_decl)
1347 return NULL_TREE;
1349 t = walk_tree_without_duplicates (&t, no_linkage_helper, NULL);
1350 if (t != error_mark_node)
1351 return t;
1352 return NULL_TREE;
1355 #ifdef GATHER_STATISTICS
1356 extern int depth_reached;
1357 #endif
1359 void
1360 cxx_print_statistics ()
1362 print_search_statistics ();
1363 print_class_statistics ();
1364 #ifdef GATHER_STATISTICS
1365 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
1366 depth_reached);
1367 #endif
1370 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1371 (which is an ARRAY_TYPE). This counts only elements of the top
1372 array. */
1374 tree
1375 array_type_nelts_top (type)
1376 tree type;
1378 return fold (build (PLUS_EXPR, sizetype,
1379 array_type_nelts (type),
1380 integer_one_node));
1383 /* Return, as an INTEGER_CST node, the number of elements for TYPE
1384 (which is an ARRAY_TYPE). This one is a recursive count of all
1385 ARRAY_TYPEs that are clumped together. */
1387 tree
1388 array_type_nelts_total (type)
1389 tree type;
1391 tree sz = array_type_nelts_top (type);
1392 type = TREE_TYPE (type);
1393 while (TREE_CODE (type) == ARRAY_TYPE)
1395 tree n = array_type_nelts_top (type);
1396 sz = fold (build (MULT_EXPR, sizetype, sz, n));
1397 type = TREE_TYPE (type);
1399 return sz;
1402 /* Called from break_out_target_exprs via mapcar. */
1404 static tree
1405 bot_manip (tp, walk_subtrees, data)
1406 tree *tp;
1407 int *walk_subtrees;
1408 void *data;
1410 splay_tree target_remap = ((splay_tree) data);
1411 tree t = *tp;
1413 if (TREE_CONSTANT (t))
1415 /* There can't be any TARGET_EXPRs or their slot variables below
1416 this point. We used to check !TREE_SIDE_EFFECTS, but then we
1417 failed to copy an ADDR_EXPR of the slot VAR_DECL. */
1418 *walk_subtrees = 0;
1419 return NULL_TREE;
1421 if (TREE_CODE (t) == TARGET_EXPR)
1423 tree u;
1425 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
1427 mark_used (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 1), 0), 0));
1428 u = build_cplus_new
1429 (TREE_TYPE (t), break_out_target_exprs (TREE_OPERAND (t, 1)));
1431 else
1433 u = build_target_expr_with_type
1434 (break_out_target_exprs (TREE_OPERAND (t, 1)), TREE_TYPE (t));
1437 /* Map the old variable to the new one. */
1438 splay_tree_insert (target_remap,
1439 (splay_tree_key) TREE_OPERAND (t, 0),
1440 (splay_tree_value) TREE_OPERAND (u, 0));
1442 /* Replace the old expression with the new version. */
1443 *tp = u;
1444 /* We don't have to go below this point; the recursive call to
1445 break_out_target_exprs will have handled anything below this
1446 point. */
1447 *walk_subtrees = 0;
1448 return NULL_TREE;
1450 else if (TREE_CODE (t) == CALL_EXPR)
1451 mark_used (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
1453 /* Make a copy of this node. */
1454 return copy_tree_r (tp, walk_subtrees, NULL);
1457 /* Replace all remapped VAR_DECLs in T with their new equivalents.
1458 DATA is really a splay-tree mapping old variables to new
1459 variables. */
1461 static tree
1462 bot_replace (t, walk_subtrees, data)
1463 tree *t;
1464 int *walk_subtrees ATTRIBUTE_UNUSED;
1465 void *data;
1467 splay_tree target_remap = ((splay_tree) data);
1469 if (TREE_CODE (*t) == VAR_DECL)
1471 splay_tree_node n = splay_tree_lookup (target_remap,
1472 (splay_tree_key) *t);
1473 if (n)
1474 *t = (tree) n->value;
1477 return NULL_TREE;
1480 /* When we parse a default argument expression, we may create
1481 temporary variables via TARGET_EXPRs. When we actually use the
1482 default-argument expression, we make a copy of the expression, but
1483 we must replace the temporaries with appropriate local versions. */
1485 tree
1486 break_out_target_exprs (t)
1487 tree t;
1489 static int target_remap_count;
1490 static splay_tree target_remap;
1492 if (!target_remap_count++)
1493 target_remap = splay_tree_new (splay_tree_compare_pointers,
1494 /*splay_tree_delete_key_fn=*/NULL,
1495 /*splay_tree_delete_value_fn=*/NULL);
1496 walk_tree (&t, bot_manip, target_remap, NULL);
1497 walk_tree (&t, bot_replace, target_remap, NULL);
1499 if (!--target_remap_count)
1501 splay_tree_delete (target_remap);
1502 target_remap = NULL;
1505 return t;
1508 /* Obstack used for allocating nodes in template function and variable
1509 definitions. */
1511 /* Similar to `build_nt', except that we set TREE_COMPLEXITY to be the
1512 current line number. */
1514 tree
1515 build_min_nt VPARAMS ((enum tree_code code, ...))
1517 register tree t;
1518 register int length;
1519 register int i;
1521 VA_OPEN (p, code);
1522 VA_FIXEDARG (p, enum tree_code, code);
1524 t = make_node (code);
1525 length = TREE_CODE_LENGTH (code);
1526 TREE_COMPLEXITY (t) = lineno;
1528 for (i = 0; i < length; i++)
1530 tree x = va_arg (p, tree);
1531 TREE_OPERAND (t, i) = x;
1534 VA_CLOSE (p);
1535 return t;
1538 /* Similar to `build', except we set TREE_COMPLEXITY to the current
1539 line-number. */
1541 tree
1542 build_min VPARAMS ((enum tree_code code, tree tt, ...))
1544 register tree t;
1545 register int length;
1546 register int i;
1548 VA_OPEN (p, tt);
1549 VA_FIXEDARG (p, enum tree_code, code);
1550 VA_FIXEDARG (p, tree, tt);
1552 t = make_node (code);
1553 length = TREE_CODE_LENGTH (code);
1554 TREE_TYPE (t) = tt;
1555 TREE_COMPLEXITY (t) = lineno;
1557 for (i = 0; i < length; i++)
1559 tree x = va_arg (p, tree);
1560 TREE_OPERAND (t, i) = x;
1563 VA_CLOSE (p);
1564 return t;
1567 /* Returns an INTEGER_CST (of type `int') corresponding to I.
1568 Multiple calls with the same value of I may or may not yield the
1569 same node; therefore, callers should never modify the node
1570 returned. */
1572 static GTY(()) tree shared_int_cache[256];
1574 tree
1575 build_shared_int_cst (i)
1576 int i;
1578 if (i >= 256)
1579 return build_int_2 (i, 0);
1581 if (!shared_int_cache[i])
1582 shared_int_cache[i] = build_int_2 (i, 0);
1584 return shared_int_cache[i];
1587 tree
1588 get_type_decl (t)
1589 tree t;
1591 if (TREE_CODE (t) == TYPE_DECL)
1592 return t;
1593 if (TYPE_P (t))
1594 return TYPE_STUB_DECL (t);
1595 if (t == error_mark_node)
1596 return t;
1598 abort ();
1600 /* Stop compiler from complaining control reaches end of non-void function. */
1601 return 0;
1604 /* Return first vector element whose BINFO_TYPE is ELEM.
1605 Return 0 if ELEM is not in VEC. VEC may be NULL_TREE. */
1607 tree
1608 vec_binfo_member (elem, vec)
1609 tree elem, vec;
1611 int i;
1613 if (vec)
1614 for (i = 0; i < TREE_VEC_LENGTH (vec); ++i)
1615 if (same_type_p (elem, BINFO_TYPE (TREE_VEC_ELT (vec, i))))
1616 return TREE_VEC_ELT (vec, i);
1618 return NULL_TREE;
1621 /* Returns the namespace that contains DECL, whether directly or
1622 indirectly. */
1624 tree
1625 decl_namespace_context (decl)
1626 tree decl;
1628 while (1)
1630 if (TREE_CODE (decl) == NAMESPACE_DECL)
1631 return decl;
1632 else if (TYPE_P (decl))
1633 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
1634 else
1635 decl = CP_DECL_CONTEXT (decl);
1639 /* Return truthvalue of whether T1 is the same tree structure as T2.
1640 Return 1 if they are the same.
1641 Return 0 if they are understandably different.
1642 Return -1 if either contains tree structure not understood by
1643 this function. */
1646 cp_tree_equal (t1, t2)
1647 tree t1, t2;
1649 register enum tree_code code1, code2;
1650 int cmp;
1652 if (t1 == t2)
1653 return 1;
1654 if (t1 == 0 || t2 == 0)
1655 return 0;
1657 code1 = TREE_CODE (t1);
1658 code2 = TREE_CODE (t2);
1660 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
1662 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
1663 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1664 else
1665 return cp_tree_equal (TREE_OPERAND (t1, 0), t2);
1667 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
1668 || code2 == NON_LVALUE_EXPR)
1669 return cp_tree_equal (t1, TREE_OPERAND (t2, 0));
1671 if (code1 != code2)
1672 return 0;
1674 switch (code1)
1676 case INTEGER_CST:
1677 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
1678 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
1680 case REAL_CST:
1681 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
1683 case STRING_CST:
1684 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
1685 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
1686 TREE_STRING_LENGTH (t1));
1688 case CONSTRUCTOR:
1689 /* We need to do this when determining whether or not two
1690 non-type pointer to member function template arguments
1691 are the same. */
1692 if (!(same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
1693 /* The first operand is RTL. */
1694 && TREE_OPERAND (t1, 0) == TREE_OPERAND (t2, 0)))
1695 return 0;
1696 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1698 case TREE_LIST:
1699 cmp = cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
1700 if (cmp <= 0)
1701 return cmp;
1702 cmp = cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2));
1703 if (cmp <= 0)
1704 return cmp;
1705 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
1707 case SAVE_EXPR:
1708 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1710 case CALL_EXPR:
1711 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1712 if (cmp <= 0)
1713 return cmp;
1714 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1716 case TARGET_EXPR:
1717 /* Special case: if either target is an unallocated VAR_DECL,
1718 it means that it's going to be unified with whatever the
1719 TARGET_EXPR is really supposed to initialize, so treat it
1720 as being equivalent to anything. */
1721 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
1722 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
1723 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
1724 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
1725 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
1726 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
1727 cmp = 1;
1728 else
1729 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1730 if (cmp <= 0)
1731 return cmp;
1732 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
1734 case WITH_CLEANUP_EXPR:
1735 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1736 if (cmp <= 0)
1737 return cmp;
1738 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
1740 case COMPONENT_REF:
1741 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
1742 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1743 return 0;
1745 case VAR_DECL:
1746 case PARM_DECL:
1747 case CONST_DECL:
1748 case FUNCTION_DECL:
1749 return 0;
1751 case TEMPLATE_PARM_INDEX:
1752 return (TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
1753 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2)
1754 && same_type_p (TREE_TYPE (TEMPLATE_PARM_DECL (t1)),
1755 TREE_TYPE (TEMPLATE_PARM_DECL (t2))));
1757 case SIZEOF_EXPR:
1758 case ALIGNOF_EXPR:
1759 if (TREE_CODE (TREE_OPERAND (t1, 0)) != TREE_CODE (TREE_OPERAND (t2, 0)))
1760 return 0;
1761 if (TYPE_P (TREE_OPERAND (t1, 0)))
1762 return same_type_p (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
1763 break;
1765 case PTRMEM_CST:
1766 /* Two pointer-to-members are the same if they point to the same
1767 field or function in the same class. */
1768 return (PTRMEM_CST_MEMBER (t1) == PTRMEM_CST_MEMBER (t2)
1769 && same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2)));
1771 default:
1772 break;
1775 switch (TREE_CODE_CLASS (code1))
1777 case '1':
1778 case '2':
1779 case '<':
1780 case 'e':
1781 case 'r':
1782 case 's':
1784 int i;
1786 cmp = 1;
1787 for (i = 0; i < TREE_CODE_LENGTH (code1); ++i)
1789 cmp = cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
1790 if (cmp <= 0)
1791 return cmp;
1793 return cmp;
1796 case 't':
1797 return same_type_p (t1, t2) ? 1 : 0;
1800 return -1;
1803 /* Build a wrapper around a 'struct z_candidate' so we can use it as a
1804 tree. */
1806 tree
1807 build_zc_wrapper (ptr)
1808 struct z_candidate *ptr;
1810 tree t = make_node (WRAPPER);
1811 WRAPPER_ZC (t) = ptr;
1812 return t;
1815 static tree
1816 build_srcloc (file, line)
1817 const char *file;
1818 int line;
1820 tree t;
1822 t = make_node (SRCLOC);
1823 SRCLOC_FILE (t) = file;
1824 SRCLOC_LINE (t) = line;
1826 return t;
1829 tree
1830 build_srcloc_here ()
1832 return build_srcloc (input_filename, lineno);
1835 /* The type of ARG when used as an lvalue. */
1837 tree
1838 lvalue_type (arg)
1839 tree arg;
1841 tree type = TREE_TYPE (arg);
1842 if (TREE_CODE (arg) == OVERLOAD)
1843 type = unknown_type_node;
1844 return type;
1847 /* The type of ARG for printing error messages; denote lvalues with
1848 reference types. */
1850 tree
1851 error_type (arg)
1852 tree arg;
1854 tree type = TREE_TYPE (arg);
1855 if (TREE_CODE (type) == ARRAY_TYPE)
1857 else if (real_lvalue_p (arg))
1858 type = build_reference_type (lvalue_type (arg));
1859 else if (IS_AGGR_TYPE (type))
1860 type = lvalue_type (arg);
1862 return type;
1865 /* Does FUNCTION use a variable-length argument list? */
1868 varargs_function_p (function)
1869 tree function;
1871 tree parm = TYPE_ARG_TYPES (TREE_TYPE (function));
1872 for (; parm; parm = TREE_CHAIN (parm))
1873 if (TREE_VALUE (parm) == void_type_node)
1874 return 0;
1875 return 1;
1878 /* Returns 1 if decl is a member of a class. */
1881 member_p (decl)
1882 tree decl;
1884 const tree ctx = DECL_CONTEXT (decl);
1885 return (ctx && TYPE_P (ctx));
1888 /* Create a placeholder for member access where we don't actually have an
1889 object that the access is against. */
1891 tree
1892 build_dummy_object (type)
1893 tree type;
1895 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
1896 return build_indirect_ref (decl, NULL);
1899 /* We've gotten a reference to a member of TYPE. Return *this if appropriate,
1900 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
1901 binfo path from current_class_type to TYPE, or 0. */
1903 tree
1904 maybe_dummy_object (type, binfop)
1905 tree type;
1906 tree *binfop;
1908 tree decl, context;
1909 tree binfo;
1911 if (current_class_type
1912 && (binfo = lookup_base (current_class_type, type,
1913 ba_ignore | ba_quiet, NULL)))
1914 context = current_class_type;
1915 else
1917 /* Reference from a nested class member function. */
1918 context = type;
1919 binfo = TYPE_BINFO (type);
1922 if (binfop)
1923 *binfop = binfo;
1925 if (current_class_ref && context == current_class_type
1926 /* Kludge: Make sure that current_class_type is actually
1927 correct. It might not be if we're in the middle of
1928 tsubst_default_argument. */
1929 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (current_class_ref)),
1930 current_class_type))
1931 decl = current_class_ref;
1932 else
1933 decl = build_dummy_object (context);
1935 return decl;
1938 /* Returns 1 if OB is a placeholder object, or a pointer to one. */
1941 is_dummy_object (ob)
1942 tree ob;
1944 if (TREE_CODE (ob) == INDIRECT_REF)
1945 ob = TREE_OPERAND (ob, 0);
1946 return (TREE_CODE (ob) == NOP_EXPR
1947 && TREE_OPERAND (ob, 0) == void_zero_node);
1950 /* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
1953 pod_type_p (t)
1954 tree t;
1956 t = strip_array_types (t);
1958 if (t == error_mark_node)
1959 return 1;
1960 if (INTEGRAL_TYPE_P (t))
1961 return 1; /* integral, character or enumeral type */
1962 if (FLOAT_TYPE_P (t))
1963 return 1;
1964 if (TYPE_PTR_P (t))
1965 return 1; /* pointer to non-member */
1966 if (TYPE_PTRMEM_P (t))
1967 return 1; /* pointer to member object */
1968 if (TYPE_PTRMEMFUNC_P (t))
1969 return 1; /* pointer to member function */
1971 if (! CLASS_TYPE_P (t))
1972 return 0; /* other non-class type (reference or function) */
1973 if (CLASSTYPE_NON_POD_P (t))
1974 return 0;
1975 return 1;
1978 /* Returns 1 iff zero initialization of type T means actually storing
1979 zeros in it. */
1982 zero_init_p (t)
1983 tree t;
1985 t = strip_array_types (t);
1987 if (t == error_mark_node)
1988 return 1;
1990 /* NULL pointers to data members are initialized with -1. */
1991 if (TYPE_PTRMEM_P (t))
1992 return 0;
1994 /* Classes that contain types that can't be zero-initialized, cannot
1995 be zero-initialized themselves. */
1996 if (CLASS_TYPE_P (t) && CLASSTYPE_NON_ZERO_INIT_P (t))
1997 return 0;
1999 return 1;
2002 /* Table of valid C++ attributes. */
2003 const struct attribute_spec cxx_attribute_table[] =
2005 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2006 { "java_interface", 0, 0, false, false, false, handle_java_interface_attribute },
2007 { "com_interface", 0, 0, false, false, false, handle_com_interface_attribute },
2008 { "init_priority", 1, 1, true, false, false, handle_init_priority_attribute },
2009 { NULL, 0, 0, false, false, false, NULL }
2012 /* Handle a "java_interface" attribute; arguments as in
2013 struct attribute_spec.handler. */
2014 static tree
2015 handle_java_interface_attribute (node, name, args, flags, no_add_attrs)
2016 tree *node;
2017 tree name;
2018 tree args ATTRIBUTE_UNUSED;
2019 int flags;
2020 bool *no_add_attrs;
2022 if (DECL_P (*node)
2023 || !CLASS_TYPE_P (*node)
2024 || !TYPE_FOR_JAVA (*node))
2026 error ("`%s' attribute can only be applied to Java class definitions",
2027 IDENTIFIER_POINTER (name));
2028 *no_add_attrs = true;
2029 return NULL_TREE;
2031 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
2032 *node = build_type_copy (*node);
2033 TYPE_JAVA_INTERFACE (*node) = 1;
2035 return NULL_TREE;
2038 /* Handle a "com_interface" attribute; arguments as in
2039 struct attribute_spec.handler. */
2040 static tree
2041 handle_com_interface_attribute (node, name, args, flags, no_add_attrs)
2042 tree *node;
2043 tree name;
2044 tree args ATTRIBUTE_UNUSED;
2045 int flags ATTRIBUTE_UNUSED;
2046 bool *no_add_attrs;
2048 static int warned;
2050 *no_add_attrs = true;
2052 if (DECL_P (*node)
2053 || !CLASS_TYPE_P (*node)
2054 || *node != TYPE_MAIN_VARIANT (*node))
2056 warning ("`%s' attribute can only be applied to class definitions",
2057 IDENTIFIER_POINTER (name));
2058 return NULL_TREE;
2061 if (!warned++)
2062 warning ("`%s' is obsolete; g++ vtables are now COM-compatible by default",
2063 IDENTIFIER_POINTER (name));
2065 return NULL_TREE;
2068 /* Handle an "init_priority" attribute; arguments as in
2069 struct attribute_spec.handler. */
2070 static tree
2071 handle_init_priority_attribute (node, name, args, flags, no_add_attrs)
2072 tree *node;
2073 tree name;
2074 tree args;
2075 int flags ATTRIBUTE_UNUSED;
2076 bool *no_add_attrs;
2078 tree initp_expr = TREE_VALUE (args);
2079 tree decl = *node;
2080 tree type = TREE_TYPE (decl);
2081 int pri;
2083 STRIP_NOPS (initp_expr);
2085 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
2087 error ("requested init_priority is not an integer constant");
2088 *no_add_attrs = true;
2089 return NULL_TREE;
2092 pri = TREE_INT_CST_LOW (initp_expr);
2094 type = strip_array_types (type);
2096 if (decl == NULL_TREE
2097 || TREE_CODE (decl) != VAR_DECL
2098 || !TREE_STATIC (decl)
2099 || DECL_EXTERNAL (decl)
2100 || (TREE_CODE (type) != RECORD_TYPE
2101 && TREE_CODE (type) != UNION_TYPE)
2102 /* Static objects in functions are initialized the
2103 first time control passes through that
2104 function. This is not precise enough to pin down an
2105 init_priority value, so don't allow it. */
2106 || current_function_decl)
2108 error ("can only use `%s' attribute on file-scope definitions of objects of class type",
2109 IDENTIFIER_POINTER (name));
2110 *no_add_attrs = true;
2111 return NULL_TREE;
2114 if (pri > MAX_INIT_PRIORITY || pri <= 0)
2116 error ("requested init_priority is out of range");
2117 *no_add_attrs = true;
2118 return NULL_TREE;
2121 /* Check for init_priorities that are reserved for
2122 language and runtime support implementations.*/
2123 if (pri <= MAX_RESERVED_INIT_PRIORITY)
2125 warning
2126 ("requested init_priority is reserved for internal use");
2129 if (SUPPORTS_INIT_PRIORITY)
2131 DECL_INIT_PRIORITY (decl) = pri;
2132 return NULL_TREE;
2134 else
2136 error ("`%s' attribute is not supported on this platform",
2137 IDENTIFIER_POINTER (name));
2138 *no_add_attrs = true;
2139 return NULL_TREE;
2143 /* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
2144 thing pointed to by the constant. */
2146 tree
2147 make_ptrmem_cst (type, member)
2148 tree type;
2149 tree member;
2151 tree ptrmem_cst = make_node (PTRMEM_CST);
2152 /* If would seem a great convenience if make_node would set
2153 TREE_CONSTANT for things of class `c', but it does not. */
2154 TREE_CONSTANT (ptrmem_cst) = 1;
2155 TREE_TYPE (ptrmem_cst) = type;
2156 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
2157 return ptrmem_cst;
2160 /* Apply FUNC to all language-specific sub-trees of TP in a pre-order
2161 traversal. Called from walk_tree(). */
2163 tree
2164 cp_walk_subtrees (tp, walk_subtrees_p, func, data, htab)
2165 tree *tp;
2166 int *walk_subtrees_p;
2167 walk_tree_fn func;
2168 void *data;
2169 void *htab;
2171 enum tree_code code = TREE_CODE (*tp);
2172 tree result;
2174 #define WALK_SUBTREE(NODE) \
2175 do \
2177 result = walk_tree (&(NODE), func, data, htab); \
2178 if (result) \
2179 return result; \
2181 while (0)
2183 /* Not one of the easy cases. We must explicitly go through the
2184 children. */
2185 switch (code)
2187 case DEFAULT_ARG:
2188 case TEMPLATE_TEMPLATE_PARM:
2189 case BOUND_TEMPLATE_TEMPLATE_PARM:
2190 case UNBOUND_CLASS_TEMPLATE:
2191 case TEMPLATE_PARM_INDEX:
2192 case TEMPLATE_TYPE_PARM:
2193 case TYPENAME_TYPE:
2194 case TYPEOF_TYPE:
2195 case BASELINK:
2196 /* None of thse have subtrees other than those already walked
2197 above. */
2198 *walk_subtrees_p = 0;
2199 break;
2201 case PTRMEM_CST:
2202 WALK_SUBTREE (TREE_TYPE (*tp));
2203 *walk_subtrees_p = 0;
2204 break;
2206 case TREE_LIST:
2207 WALK_SUBTREE (TREE_PURPOSE (*tp));
2208 break;
2210 case OVERLOAD:
2211 WALK_SUBTREE (OVL_FUNCTION (*tp));
2212 WALK_SUBTREE (OVL_CHAIN (*tp));
2213 *walk_subtrees_p = 0;
2214 break;
2216 case RECORD_TYPE:
2217 if (TYPE_PTRMEMFUNC_P (*tp))
2218 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
2219 break;
2221 default:
2222 break;
2225 /* We didn't find what we were looking for. */
2226 return NULL_TREE;
2228 #undef WALK_SUBTREE
2231 /* Decide whether there are language-specific reasons to not inline a
2232 function as a tree. */
2235 cp_cannot_inline_tree_fn (fnp)
2236 tree *fnp;
2238 tree fn = *fnp;
2240 if (flag_really_no_inline
2241 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (fn)) == NULL)
2242 return 1;
2244 /* We can inline a template instantiation only if it's fully
2245 instantiated. */
2246 if (DECL_TEMPLATE_INFO (fn)
2247 && TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (fn)))
2249 fn = *fnp = instantiate_decl (fn, /*defer_ok=*/0);
2250 if (TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (fn)))
2251 return 1;
2254 /* Don't auto-inline anything that might not be bound within
2255 this unit of translation. */
2256 if (!DECL_DECLARED_INLINE_P (fn) && !(*targetm.binds_local_p) (fn))
2258 DECL_UNINLINABLE (fn) = 1;
2259 return 1;
2262 if (varargs_function_p (fn))
2264 DECL_UNINLINABLE (fn) = 1;
2265 return 1;
2268 if (! function_attribute_inlinable_p (fn))
2270 DECL_UNINLINABLE (fn) = 1;
2271 return 1;
2274 return 0;
2277 /* Add any pending functions other than the current function (already
2278 handled by the caller), that thus cannot be inlined, to FNS_P, then
2279 return the latest function added to the array, PREV_FN. */
2281 tree
2282 cp_add_pending_fn_decls (fns_p, prev_fn)
2283 void *fns_p;
2284 tree prev_fn;
2286 varray_type *fnsp = (varray_type *)fns_p;
2287 struct saved_scope *s;
2289 for (s = scope_chain; s; s = s->prev)
2290 if (s->function_decl && s->function_decl != prev_fn)
2292 VARRAY_PUSH_TREE (*fnsp, s->function_decl);
2293 prev_fn = s->function_decl;
2296 return prev_fn;
2299 /* Determine whether a tree node is an OVERLOAD node. Used to decide
2300 whether to copy a node or to preserve its chain when inlining a
2301 function. */
2304 cp_is_overload_p (t)
2305 tree t;
2307 return TREE_CODE (t) == OVERLOAD;
2310 /* Determine whether VAR is a declaration of an automatic variable in
2311 function FN. */
2314 cp_auto_var_in_fn_p (var, fn)
2315 tree var, fn;
2317 return (DECL_P (var) && DECL_CONTEXT (var) == fn
2318 && nonstatic_local_decl_p (var));
2321 /* Tell whether a declaration is needed for the RESULT of a function
2322 FN being inlined into CALLER or if the top node of target_exprs is
2323 to be used. */
2325 tree
2326 cp_copy_res_decl_for_inlining (result, fn, caller, decl_map_,
2327 need_decl, return_slot_addr)
2328 tree result, fn, caller;
2329 void *decl_map_;
2330 int *need_decl;
2331 tree return_slot_addr;
2333 splay_tree decl_map = (splay_tree)decl_map_;
2334 tree var;
2336 /* If FN returns an aggregate then the caller will always pass the
2337 address of the return slot explicitly. If we were just to
2338 create a new VAR_DECL here, then the result of this function
2339 would be copied (bitwise) into the variable initialized by the
2340 TARGET_EXPR. That's incorrect, so we must transform any
2341 references to the RESULT into references to the target. */
2343 /* We should have an explicit return slot iff the return type is
2344 TREE_ADDRESSABLE. See simplify_aggr_init_expr. */
2345 if (TREE_ADDRESSABLE (TREE_TYPE (result))
2346 != (return_slot_addr != NULL_TREE))
2347 abort ();
2349 *need_decl = !return_slot_addr;
2350 if (return_slot_addr)
2352 var = build_indirect_ref (return_slot_addr, "");
2353 if (! same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (var),
2354 TREE_TYPE (result)))
2355 abort ();
2357 /* Otherwise, make an appropriate copy. */
2358 else
2359 var = copy_decl_for_inlining (result, fn, caller);
2361 if (DECL_SAVED_FUNCTION_DATA (fn))
2363 tree nrv = DECL_SAVED_FUNCTION_DATA (fn)->x_return_value;
2364 if (nrv)
2366 /* We have a named return value; copy the name and source
2367 position so we can get reasonable debugging information, and
2368 register the return variable as its equivalent. */
2369 if (TREE_CODE (var) == VAR_DECL)
2371 DECL_NAME (var) = DECL_NAME (nrv);
2372 DECL_SOURCE_LOCATION (var) = DECL_SOURCE_LOCATION (nrv);
2373 DECL_ABSTRACT_ORIGIN (var) = DECL_ORIGIN (nrv);
2374 /* Don't lose initialization info. */
2375 DECL_INITIAL (var) = DECL_INITIAL (nrv);
2376 /* Don't forget that it needs to go in the stack. */
2377 TREE_ADDRESSABLE (var) = TREE_ADDRESSABLE (nrv);
2380 splay_tree_insert (decl_map,
2381 (splay_tree_key) nrv,
2382 (splay_tree_value) var);
2386 return var;
2389 /* Record that we're about to start inlining FN, and return nonzero if
2390 that's OK. Used for lang_hooks.tree_inlining.start_inlining. */
2393 cp_start_inlining (fn)
2394 tree fn;
2396 if (DECL_TEMPLATE_INSTANTIATION (fn))
2397 return push_tinst_level (fn);
2398 else
2399 return 1;
2402 /* Record that we're done inlining FN. Used for
2403 lang_hooks.tree_inlining.end_inlining. */
2405 void
2406 cp_end_inlining (fn)
2407 tree fn ATTRIBUTE_UNUSED;
2409 if (DECL_TEMPLATE_INSTANTIATION (fn))
2410 pop_tinst_level ();
2413 /* Initialize tree.c. */
2415 void
2416 init_tree ()
2418 lang_statement_code_p = cp_statement_code_p;
2419 list_hash_table = htab_create_ggc (31, list_hash, list_hash_eq, NULL);
2422 /* Called via walk_tree. If *TP points to a DECL_STMT for a local
2423 declaration, copies the declaration and enters it in the splay_tree
2424 pointed to by DATA (which is really a `splay_tree *'). */
2426 static tree
2427 mark_local_for_remap_r (tp, walk_subtrees, data)
2428 tree *tp;
2429 int *walk_subtrees ATTRIBUTE_UNUSED;
2430 void *data;
2432 tree t = *tp;
2433 splay_tree st = (splay_tree) data;
2434 tree decl;
2437 if (TREE_CODE (t) == DECL_STMT
2438 && nonstatic_local_decl_p (DECL_STMT_DECL (t)))
2439 decl = DECL_STMT_DECL (t);
2440 else if (TREE_CODE (t) == LABEL_STMT)
2441 decl = LABEL_STMT_LABEL (t);
2442 else if (TREE_CODE (t) == TARGET_EXPR
2443 && nonstatic_local_decl_p (TREE_OPERAND (t, 0)))
2444 decl = TREE_OPERAND (t, 0);
2445 else if (TREE_CODE (t) == CASE_LABEL)
2446 decl = CASE_LABEL_DECL (t);
2447 else
2448 decl = NULL_TREE;
2450 if (decl)
2452 tree copy;
2454 /* Make a copy. */
2455 copy = copy_decl_for_inlining (decl,
2456 DECL_CONTEXT (decl),
2457 DECL_CONTEXT (decl));
2459 /* Remember the copy. */
2460 splay_tree_insert (st,
2461 (splay_tree_key) decl,
2462 (splay_tree_value) copy);
2465 return NULL_TREE;
2468 /* Called via walk_tree when an expression is unsaved. Using the
2469 splay_tree pointed to by ST (which is really a `splay_tree'),
2470 remaps all local declarations to appropriate replacements. */
2472 static tree
2473 cp_unsave_r (tp, walk_subtrees, data)
2474 tree *tp;
2475 int *walk_subtrees;
2476 void *data;
2478 splay_tree st = (splay_tree) data;
2479 splay_tree_node n;
2481 /* Only a local declaration (variable or label). */
2482 if (nonstatic_local_decl_p (*tp))
2484 /* Lookup the declaration. */
2485 n = splay_tree_lookup (st, (splay_tree_key) *tp);
2487 /* If it's there, remap it. */
2488 if (n)
2489 *tp = (tree) n->value;
2491 else if (TREE_CODE (*tp) == SAVE_EXPR)
2492 remap_save_expr (tp, st, current_function_decl, walk_subtrees);
2493 else
2495 copy_tree_r (tp, walk_subtrees, NULL);
2497 /* Do whatever unsaving is required. */
2498 unsave_expr_1 (*tp);
2501 /* Keep iterating. */
2502 return NULL_TREE;
2505 /* Called whenever an expression needs to be unsaved. */
2507 tree
2508 cxx_unsave_expr_now (tp)
2509 tree tp;
2511 splay_tree st;
2513 /* Create a splay-tree to map old local variable declarations to new
2514 ones. */
2515 st = splay_tree_new (splay_tree_compare_pointers, NULL, NULL);
2517 /* Walk the tree once figuring out what needs to be remapped. */
2518 walk_tree (&tp, mark_local_for_remap_r, st, NULL);
2520 /* Walk the tree again, copying, remapping, and unsaving. */
2521 walk_tree (&tp, cp_unsave_r, st, NULL);
2523 /* Clean up. */
2524 splay_tree_delete (st);
2526 return tp;
2529 /* Returns the kind of special function that DECL (a FUNCTION_DECL)
2530 is. Note that sfk_none is zero, so this function can be used as a
2531 predicate to test whether or not DECL is a special function. */
2533 special_function_kind
2534 special_function_p (decl)
2535 tree decl;
2537 /* Rather than doing all this stuff with magic names, we should
2538 probably have a field of type `special_function_kind' in
2539 DECL_LANG_SPECIFIC. */
2540 if (DECL_COPY_CONSTRUCTOR_P (decl))
2541 return sfk_copy_constructor;
2542 if (DECL_CONSTRUCTOR_P (decl))
2543 return sfk_constructor;
2544 if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
2545 return sfk_assignment_operator;
2546 if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (decl))
2547 return sfk_destructor;
2548 if (DECL_COMPLETE_DESTRUCTOR_P (decl))
2549 return sfk_complete_destructor;
2550 if (DECL_BASE_DESTRUCTOR_P (decl))
2551 return sfk_base_destructor;
2552 if (DECL_DELETING_DESTRUCTOR_P (decl))
2553 return sfk_deleting_destructor;
2554 if (DECL_CONV_FN_P (decl))
2555 return sfk_conversion;
2557 return sfk_none;
2560 /* Returns true if and only if NODE is a name, i.e., a node created
2561 by the parser when processing an id-expression. */
2563 bool
2564 name_p (tree node)
2566 if (TREE_CODE (node) == TEMPLATE_ID_EXPR)
2567 node = TREE_OPERAND (node, 0);
2568 return (/* An ordinary unqualified name. */
2569 TREE_CODE (node) == IDENTIFIER_NODE
2570 /* A destructor name. */
2571 || TREE_CODE (node) == BIT_NOT_EXPR
2572 /* A qualified name. */
2573 || TREE_CODE (node) == SCOPE_REF);
2576 /* Returns nonzero if TYPE is a character type, including wchar_t. */
2579 char_type_p (type)
2580 tree type;
2582 return (same_type_p (type, char_type_node)
2583 || same_type_p (type, unsigned_char_type_node)
2584 || same_type_p (type, signed_char_type_node)
2585 || same_type_p (type, wchar_type_node));
2588 /* Returns the kind of linkage associated with the indicated DECL. Th
2589 value returned is as specified by the language standard; it is
2590 independent of implementation details regarding template
2591 instantiation, etc. For example, it is possible that a declaration
2592 to which this function assigns external linkage would not show up
2593 as a global symbol when you run `nm' on the resulting object file. */
2595 linkage_kind
2596 decl_linkage (decl)
2597 tree decl;
2599 /* This function doesn't attempt to calculate the linkage from first
2600 principles as given in [basic.link]. Instead, it makes use of
2601 the fact that we have already set TREE_PUBLIC appropriately, and
2602 then handles a few special cases. Ideally, we would calculate
2603 linkage first, and then transform that into a concrete
2604 implementation. */
2606 /* Things that don't have names have no linkage. */
2607 if (!DECL_NAME (decl))
2608 return lk_none;
2610 /* Things that are TREE_PUBLIC have external linkage. */
2611 if (TREE_PUBLIC (decl))
2612 return lk_external;
2614 /* Some things that are not TREE_PUBLIC have external linkage, too.
2615 For example, on targets that don't have weak symbols, we make all
2616 template instantiations have internal linkage (in the object
2617 file), but the symbols should still be treated as having external
2618 linkage from the point of view of the language. */
2619 if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
2620 return lk_external;
2622 /* Things in local scope do not have linkage, if they don't have
2623 TREE_PUBLIC set. */
2624 if (decl_function_context (decl))
2625 return lk_none;
2627 /* Everything else has internal linkage. */
2628 return lk_internal;
2631 /* EXP is an expression that we want to pre-evaluate. Returns via INITP an
2632 expression to perform the pre-evaluation, and returns directly an
2633 expression to use the precalculated result. */
2635 tree
2636 stabilize_expr (exp, initp)
2637 tree exp;
2638 tree *initp;
2640 tree init_expr;
2642 if (!TREE_SIDE_EFFECTS (exp))
2644 init_expr = void_zero_node;
2646 else if (!real_lvalue_p (exp)
2647 || !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (exp)))
2649 init_expr = get_target_expr (exp);
2650 exp = TARGET_EXPR_SLOT (init_expr);
2652 else
2654 exp = build_unary_op (ADDR_EXPR, exp, 1);
2655 init_expr = get_target_expr (exp);
2656 exp = TARGET_EXPR_SLOT (init_expr);
2657 exp = build_indirect_ref (exp, 0);
2660 *initp = init_expr;
2661 return exp;
2664 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
2665 /* Complain that some language-specific thing hanging off a tree
2666 node has been accessed improperly. */
2668 void
2669 lang_check_failed (file, line, function)
2670 const char *file;
2671 int line;
2672 const char *function;
2674 internal_error ("lang_* check: failed in %s, at %s:%d",
2675 function, trim_filename (file), line);
2677 #endif /* ENABLE_TREE_CHECKING */
2679 #include "gt-cp-tree.h"