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