]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/tree.c
optabs.c (emit_cmp_and_jump_insns): Be more thorough in canonization.
[thirdparty/gcc.git] / gcc / cp / tree.c
CommitLineData
8d08fdba 1/* Language-dependent node constructors for parse phase of GNU compiler.
c12f5242 2 Copyright (C) 1987, 88, 92-98, 1999 Free Software Foundation, Inc.
8d08fdba
MS
3 Hacked by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
8d08fdba
MS
21
22#include "config.h"
8d052bc7 23#include "system.h"
8d08fdba
MS
24#include "obstack.h"
25#include "tree.h"
26#include "cp-tree.h"
27#include "flags.h"
28cbf42c 28#include "rtl.h"
12027a89
RL
29#include "toplev.h"
30
49c249e1
JM
31static tree bot_manip PROTO((tree));
32static tree perm_manip PROTO((tree));
33static tree build_cplus_array_type_1 PROTO((tree, tree));
34static void list_hash_add PROTO((int, tree));
35static int list_hash PROTO((tree, tree, tree));
51632249 36static tree list_hash_lookup PROTO((int, tree, tree, tree));
9a71c18b 37static void propagate_binfo_offsets PROTO((tree, tree));
69ac77ce 38static int avoid_overlap PROTO((tree, tree));
27b8d0cd 39static cp_lvalue_kind lvalue_p_1 PROTO((tree, int));
d8e178a0
KG
40static tree no_linkage_helper PROTO((tree));
41static tree build_srcloc PROTO((char *, int));
49c249e1 42
8d08fdba
MS
43#define CEIL(x,y) (((x) + (y) - 1) / (y))
44
27b8d0cd
MM
45/* If REF is an lvalue, returns the kind of lvalue that REF is.
46 Otherwise, returns clk_none. If TREAT_CLASS_RVALUES_AS_LVALUES is
47 non-zero, rvalues of class type are considered lvalues. */
8d08fdba 48
27b8d0cd 49static cp_lvalue_kind
69851283 50lvalue_p_1 (ref, treat_class_rvalues_as_lvalues)
8ccc31eb 51 tree ref;
69851283 52 int treat_class_rvalues_as_lvalues;
8ccc31eb 53{
27b8d0cd
MM
54 cp_lvalue_kind op1_lvalue_kind = clk_none;
55 cp_lvalue_kind op2_lvalue_kind = clk_none;
56
8ccc31eb 57 if (TREE_CODE (TREE_TYPE (ref)) == REFERENCE_TYPE)
27b8d0cd 58 return clk_ordinary;
8ccc31eb 59
4ac14744 60 if (ref == current_class_ptr && flag_this_is_variable <= 0)
27b8d0cd 61 return clk_none;
8ccc31eb
MS
62
63 switch (TREE_CODE (ref))
64 {
65 /* preincrements and predecrements are valid lvals, provided
e92cc029 66 what they refer to are valid lvals. */
8ccc31eb
MS
67 case PREINCREMENT_EXPR:
68 case PREDECREMENT_EXPR:
8ccc31eb 69 case SAVE_EXPR:
c7ae64f2
JM
70 case UNSAVE_EXPR:
71 case TRY_CATCH_EXPR:
72 case WITH_CLEANUP_EXPR:
69851283
MM
73 case REALPART_EXPR:
74 case IMAGPART_EXPR:
06126ca2 75 case NOP_EXPR:
69851283
MM
76 return lvalue_p_1 (TREE_OPERAND (ref, 0),
77 treat_class_rvalues_as_lvalues);
8ccc31eb 78
27b8d0cd
MM
79 case COMPONENT_REF:
80 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
81 treat_class_rvalues_as_lvalues);
82 if (op1_lvalue_kind
83 /* The "field" can be a FUNCTION_DECL or an OVERLOAD in some
84 situations. */
85 && TREE_CODE (TREE_OPERAND (ref, 1)) == FIELD_DECL
86 && DECL_BIT_FIELD (TREE_OPERAND (ref, 1)))
87 {
88 /* Clear the ordinary bit. If this object was a class
89 rvalue we want to preserve that information. */
90 op1_lvalue_kind &= ~clk_ordinary;
91 /* The lvalue is for a btifield. */
92 op1_lvalue_kind |= clk_bitfield;
93 }
94 return op1_lvalue_kind;
95
8ccc31eb 96 case STRING_CST:
27b8d0cd 97 return clk_ordinary;
8ccc31eb
MS
98
99 case VAR_DECL:
100 if (TREE_READONLY (ref) && ! TREE_STATIC (ref)
101 && DECL_LANG_SPECIFIC (ref)
102 && DECL_IN_AGGR_P (ref))
27b8d0cd 103 return clk_none;
8ccc31eb
MS
104 case INDIRECT_REF:
105 case ARRAY_REF:
106 case PARM_DECL:
107 case RESULT_DECL:
59e76fc6 108 if (TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE)
27b8d0cd 109 return clk_ordinary;
8ccc31eb
MS
110 break;
111
8ccc31eb
MS
112 /* A currently unresolved scope ref. */
113 case SCOPE_REF:
114 my_friendly_abort (103);
115 case OFFSET_REF:
116 if (TREE_CODE (TREE_OPERAND (ref, 1)) == FUNCTION_DECL)
27b8d0cd
MM
117 return clk_ordinary;
118 /* Fall through. */
119 case MAX_EXPR:
120 case MIN_EXPR:
121 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 0),
122 treat_class_rvalues_as_lvalues);
123 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
124 treat_class_rvalues_as_lvalues);
8ccc31eb
MS
125 break;
126
127 case COND_EXPR:
27b8d0cd
MM
128 op1_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 1),
129 treat_class_rvalues_as_lvalues);
130 op2_lvalue_kind = lvalue_p_1 (TREE_OPERAND (ref, 2),
131 treat_class_rvalues_as_lvalues);
132 break;
8ccc31eb
MS
133
134 case MODIFY_EXPR:
27b8d0cd 135 return clk_ordinary;
8ccc31eb
MS
136
137 case COMPOUND_EXPR:
69851283 138 return lvalue_p_1 (TREE_OPERAND (ref, 1),
27b8d0cd 139 treat_class_rvalues_as_lvalues);
69851283
MM
140
141 case TARGET_EXPR:
27b8d0cd 142 return treat_class_rvalues_as_lvalues ? clk_class : clk_none;
69851283
MM
143
144 case CALL_EXPR:
27b8d0cd
MM
145 return ((treat_class_rvalues_as_lvalues
146 && IS_AGGR_TYPE (TREE_TYPE (ref)))
147 ? clk_class : clk_none);
69851283
MM
148
149 case FUNCTION_DECL:
150 /* All functions (except non-static-member functions) are
151 lvalues. */
27b8d0cd
MM
152 return (DECL_NONSTATIC_MEMBER_FUNCTION_P (ref)
153 ? clk_none : clk_ordinary);
7f85441b
KG
154
155 default:
156 break;
8ccc31eb
MS
157 }
158
27b8d0cd
MM
159 /* If one operand is not an lvalue at all, then this expression is
160 not an lvalue. */
161 if (!op1_lvalue_kind || !op2_lvalue_kind)
162 return clk_none;
163
164 /* Otherwise, it's an lvalue, and it has all the odd properties
165 contributed by either operand. */
166 op1_lvalue_kind = op1_lvalue_kind | op2_lvalue_kind;
167 /* It's not an ordinary lvalue if it involves either a bit-field or
168 a class rvalue. */
169 if ((op1_lvalue_kind & ~clk_ordinary) != clk_none)
170 op1_lvalue_kind &= ~clk_ordinary;
171 return op1_lvalue_kind;
8ccc31eb
MS
172}
173
27b8d0cd
MM
174/* If REF is an lvalue, returns the kind of lvalue that REF is.
175 Otherwise, returns clk_none. Lvalues can be assigned, unless they
176 have TREE_READONLY, or unless they are FUNCTION_DECLs. Lvalues can
177 have their address taken, unless they have DECL_REGISTER. */
69851283 178
27b8d0cd 179cp_lvalue_kind
69851283
MM
180real_lvalue_p (ref)
181 tree ref;
182{
183 return lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/0);
184}
185
27b8d0cd
MM
186/* This differs from real_lvalue_p in that class rvalues are
187 considered lvalues. */
69851283 188
8d08fdba
MS
189int
190lvalue_p (ref)
191 tree ref;
192{
27b8d0cd
MM
193 return
194 (lvalue_p_1 (ref, /*treat_class_rvalues_as_lvalues=*/1) != clk_none);
8d08fdba
MS
195}
196
197/* Return nonzero if REF is an lvalue valid for this language;
198 otherwise, print an error message and return zero. */
199
200int
201lvalue_or_else (ref, string)
202 tree ref;
834003f4 203 const char *string;
8d08fdba
MS
204{
205 int win = lvalue_p (ref);
206 if (! win)
8251199e 207 error ("non-lvalue in %s", string);
8d08fdba
MS
208 return win;
209}
210
211/* INIT is a CALL_EXPR which needs info about its target.
212 TYPE is the type that this initialization should appear to have.
213
214 Build an encapsulation of the initialization to perform
215 and return it so that it can be processed by language-independent
2ee887f2 216 and language-specific expression expanders. */
e92cc029 217
8d08fdba 218tree
5566b478 219build_cplus_new (type, init)
8d08fdba
MS
220 tree type;
221 tree init;
8d08fdba 222{
e8abc66f
MS
223 tree slot;
224 tree rval;
225
27b8d0cd
MM
226 /* Make sure that we're not trying to create an instance of an
227 abstract class. */
5bb2f1e7 228 abstract_virtuals_error (NULL_TREE, type);
27b8d0cd 229
02531345 230 if (TREE_CODE (init) != CALL_EXPR && TREE_CODE (init) != AGGR_INIT_EXPR)
06126ca2 231 return convert (type, init);
c11b6f21 232
e8abc66f 233 slot = build (VAR_DECL, type);
aa36c081 234 DECL_ARTIFICIAL (slot) = 1;
e8abc66f 235 layout_decl (slot, 0);
02531345 236 rval = build (AGGR_INIT_EXPR, type,
e8abc66f 237 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), slot);
8d08fdba 238 TREE_SIDE_EFFECTS (rval) = 1;
e349ee73 239 rval = build (TARGET_EXPR, type, slot, rval, NULL_TREE, NULL_TREE);
8d08fdba 240 TREE_SIDE_EFFECTS (rval) = 1;
8d08fdba 241
8d08fdba
MS
242 return rval;
243}
244
aa36c081
JM
245/* Encapsulate the expression INIT in a TARGET_EXPR. */
246
247tree
248get_target_expr (init)
249 tree init;
250{
251 tree slot;
252 tree rval;
253
254 slot = build (VAR_DECL, TREE_TYPE (init));
255 DECL_ARTIFICIAL (slot) = 1;
256 layout_decl (slot, 0);
257 rval = build (TARGET_EXPR, TREE_TYPE (init), slot, init,
258 NULL_TREE, NULL_TREE);
259 TREE_SIDE_EFFECTS (rval) = 1;
260
261 return rval;
262}
263
8d08fdba
MS
264/* Recursively search EXP for CALL_EXPRs that need cleanups and replace
265 these CALL_EXPRs with tree nodes that will perform the cleanups. */
266
267tree
268break_out_cleanups (exp)
269 tree exp;
270{
271 tree tmp = exp;
272
273 if (TREE_CODE (tmp) == CALL_EXPR
274 && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (tmp)))
5566b478 275 return build_cplus_new (TREE_TYPE (tmp), tmp);
8d08fdba
MS
276
277 while (TREE_CODE (tmp) == NOP_EXPR
278 || TREE_CODE (tmp) == CONVERT_EXPR
279 || TREE_CODE (tmp) == NON_LVALUE_EXPR)
280 {
281 if (TREE_CODE (TREE_OPERAND (tmp, 0)) == CALL_EXPR
282 && TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (TREE_OPERAND (tmp, 0))))
283 {
284 TREE_OPERAND (tmp, 0)
285 = build_cplus_new (TREE_TYPE (TREE_OPERAND (tmp, 0)),
5566b478 286 TREE_OPERAND (tmp, 0));
8d08fdba
MS
287 break;
288 }
289 else
290 tmp = TREE_OPERAND (tmp, 0);
291 }
292 return exp;
293}
294
295/* Recursively perform a preorder search EXP for CALL_EXPRs, making
296 copies where they are found. Returns a deep copy all nodes transitively
297 containing CALL_EXPRs. */
298
299tree
300break_out_calls (exp)
301 tree exp;
302{
a703fb38 303 register tree t1, t2 = NULL_TREE;
8d08fdba
MS
304 register enum tree_code code;
305 register int changed = 0;
306 register int i;
307
308 if (exp == NULL_TREE)
309 return exp;
310
311 code = TREE_CODE (exp);
312
313 if (code == CALL_EXPR)
314 return copy_node (exp);
315
e92cc029 316 /* Don't try and defeat a save_expr, as it should only be done once. */
8d08fdba
MS
317 if (code == SAVE_EXPR)
318 return exp;
319
320 switch (TREE_CODE_CLASS (code))
321 {
322 default:
323 abort ();
324
325 case 'c': /* a constant */
326 case 't': /* a type node */
327 case 'x': /* something random, like an identifier or an ERROR_MARK. */
328 return exp;
329
330 case 'd': /* A decl node */
f376e137
MS
331#if 0 /* This is bogus. jason 9/21/94 */
332
8d08fdba
MS
333 t1 = break_out_calls (DECL_INITIAL (exp));
334 if (t1 != DECL_INITIAL (exp))
335 {
336 exp = copy_node (exp);
337 DECL_INITIAL (exp) = t1;
338 }
f376e137 339#endif
8d08fdba
MS
340 return exp;
341
342 case 'b': /* A block node */
343 {
344 /* Don't know how to handle these correctly yet. Must do a
345 break_out_calls on all DECL_INITIAL values for local variables,
346 and also break_out_calls on all sub-blocks and sub-statements. */
347 abort ();
348 }
349 return exp;
350
351 case 'e': /* an expression */
352 case 'r': /* a reference */
353 case 's': /* an expression with side effects */
354 for (i = tree_code_length[(int) code] - 1; i >= 0; i--)
355 {
356 t1 = break_out_calls (TREE_OPERAND (exp, i));
357 if (t1 != TREE_OPERAND (exp, i))
358 {
359 exp = copy_node (exp);
360 TREE_OPERAND (exp, i) = t1;
361 }
362 }
363 return exp;
364
365 case '<': /* a comparison expression */
366 case '2': /* a binary arithmetic expression */
367 t2 = break_out_calls (TREE_OPERAND (exp, 1));
368 if (t2 != TREE_OPERAND (exp, 1))
369 changed = 1;
370 case '1': /* a unary arithmetic expression */
371 t1 = break_out_calls (TREE_OPERAND (exp, 0));
372 if (t1 != TREE_OPERAND (exp, 0))
373 changed = 1;
374 if (changed)
375 {
376 if (tree_code_length[(int) code] == 1)
377 return build1 (code, TREE_TYPE (exp), t1);
378 else
379 return build (code, TREE_TYPE (exp), t1, t2);
380 }
381 return exp;
382 }
383
384}
385\f
386extern struct obstack *current_obstack;
387extern struct obstack permanent_obstack, class_obstack;
388extern struct obstack *saveable_obstack;
5156628f 389extern struct obstack *expression_obstack;
8d08fdba
MS
390
391/* Here is how primitive or already-canonicalized types' hash
392 codes are made. MUST BE CONSISTENT WITH tree.c !!! */
393#define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
394
395/* Construct, lay out and return the type of methods belonging to class
396 BASETYPE and whose arguments are described by ARGTYPES and whose values
397 are described by RETTYPE. If each type exists already, reuse it. */
e92cc029 398
8d08fdba
MS
399tree
400build_cplus_method_type (basetype, rettype, argtypes)
401 tree basetype, rettype, argtypes;
402{
403 register tree t;
404 tree ptype;
405 int hashcode;
406
407 /* Make a node of the sort we want. */
408 t = make_node (METHOD_TYPE);
409
410 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
411 TREE_TYPE (t) = rettype;
6eabb241 412 ptype = build_pointer_type (basetype);
863adfc0 413
8d08fdba 414 /* The actual arglist for this function includes a "hidden" argument
558475f0
MM
415 which is "this". Put it into the list of argument types. Make
416 sure that the new argument list is allocated on the same obstack
417 as the type. */
418 push_obstacks (TYPE_OBSTACK (t), TYPE_OBSTACK (t));
8d08fdba
MS
419 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
420 TYPE_ARG_TYPES (t) = argtypes;
421 TREE_SIDE_EFFECTS (argtypes) = 1; /* Mark first argtype as "artificial". */
558475f0 422 pop_obstacks ();
8d08fdba
MS
423
424 /* If we already have such a type, use the old one and free this one.
425 Note that it also frees up the above cons cell if found. */
558475f0
MM
426 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
427 type_hash_list (argtypes);
428
8d08fdba
MS
429 t = type_hash_canon (hashcode, t);
430
431 if (TYPE_SIZE (t) == 0)
432 layout_type (t);
433
434 return t;
435}
436
bd6dd845 437static tree
e349ee73 438build_cplus_array_type_1 (elt_type, index_type)
8d08fdba
MS
439 tree elt_type;
440 tree index_type;
441{
8d08fdba
MS
442 tree t;
443
adecb3f4
MM
444 if (elt_type == error_mark_node || index_type == error_mark_node)
445 return error_mark_node;
446
db3626d1
MM
447 push_obstacks_nochange ();
448
9188c363 449 /* If both ELT_TYPE and INDEX_TYPE are permanent,
8d08fdba
MS
450 make this permanent too. */
451 if (TREE_PERMANENT (elt_type)
452 && (index_type == 0 || TREE_PERMANENT (index_type)))
db3626d1 453 end_temporary_allocation ();
8d08fdba 454
7bdbfa05
MM
455 if (processing_template_decl
456 || uses_template_parms (elt_type)
457 || uses_template_parms (index_type))
5566b478
MS
458 {
459 t = make_node (ARRAY_TYPE);
460 TREE_TYPE (t) = elt_type;
461 TYPE_DOMAIN (t) = index_type;
462 }
463 else
464 t = build_array_type (elt_type, index_type);
8d08fdba
MS
465
466 /* Push these needs up so that initialization takes place
467 more easily. */
db3626d1
MM
468 TYPE_NEEDS_CONSTRUCTING (t)
469 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (elt_type));
470 TYPE_NEEDS_DESTRUCTOR (t)
471 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (elt_type));
472 pop_obstacks ();
8d08fdba
MS
473 return t;
474}
e349ee73
MS
475
476tree
477build_cplus_array_type (elt_type, index_type)
478 tree elt_type;
479 tree index_type;
480{
481 tree t;
91063b51
MM
482 int type_quals = CP_TYPE_QUALS (elt_type);
483
e349ee73
MS
484 elt_type = TYPE_MAIN_VARIANT (elt_type);
485
486 t = build_cplus_array_type_1 (elt_type, index_type);
487
91063b51
MM
488 if (type_quals != TYPE_UNQUALIFIED)
489 t = cp_build_qualified_type (t, type_quals);
e349ee73
MS
490
491 return t;
492}
8d08fdba 493\f
adecb3f4
MM
494/* Make a variant of TYPE, qualified with the TYPE_QUALS. Handles
495 arrays correctly. In particular, if TYPE is an array of T's, and
496 TYPE_QUALS is non-empty, returns an array of qualified T's. If
497 at attempt is made to qualify a type illegally, and COMPLAIN is
498 non-zero, an error is issued. If COMPLAIN is zero, error_mark_node
499 is returned. */
f376e137
MS
500
501tree
adecb3f4 502cp_build_qualified_type_real (type, type_quals, complain)
f376e137 503 tree type;
91063b51 504 int type_quals;
adecb3f4 505 int complain;
f376e137 506{
2adeacc9
MM
507 tree result;
508
e76a2646
MS
509 if (type == error_mark_node)
510 return type;
511
91063b51
MM
512 /* A restrict-qualified pointer type must be a pointer (or reference)
513 to object or incomplete type. */
514 if ((type_quals & TYPE_QUAL_RESTRICT)
adecb3f4 515 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
91063b51
MM
516 && (!POINTER_TYPE_P (type)
517 || TYPE_PTRMEM_P (type)
518 || TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE))
519 {
adecb3f4
MM
520 if (complain)
521 cp_error ("`%T' cannot be `restrict'-qualified", type);
522 else
523 return error_mark_node;
524
91063b51
MM
525 type_quals &= ~TYPE_QUAL_RESTRICT;
526 }
527
77700469
MM
528 if (type_quals != TYPE_UNQUALIFIED
529 && TREE_CODE (type) == FUNCTION_TYPE)
530 {
adecb3f4
MM
531 if (complain)
532 cp_error ("`%T' cannot be `const'-, `volatile'-, or `restrict'-qualified", type);
533 else
534 return error_mark_node;
77700469
MM
535 type_quals = TYPE_UNQUALIFIED;
536 }
537 else if (TREE_CODE (type) == ARRAY_TYPE)
f376e137 538 {
db3626d1
MM
539 /* In C++, the qualification really applies to the array element
540 type. Obtain the appropriately qualified element type. */
541 tree t;
542 tree element_type
543 = cp_build_qualified_type_real (TREE_TYPE (type),
544 type_quals,
545 complain);
546
547 if (element_type == error_mark_node)
adecb3f4 548 return error_mark_node;
f376e137 549
db3626d1
MM
550 /* See if we already have an identically qualified type. */
551 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
552 if (CP_TYPE_QUALS (t) == type_quals)
553 break;
f376e137 554
db3626d1
MM
555 /* If we didn't already have it, create it now. */
556 if (!t)
f376e137 557 {
db3626d1
MM
558 /* Make a new array type, just like the old one, but with the
559 appropriately qualified element type. */
560 t = build_type_copy (type);
561 TREE_TYPE (t) = element_type;
f376e137
MS
562 }
563
db3626d1
MM
564 /* Even if we already had this variant, we update
565 TYPE_NEEDS_CONSTRUCTING and TYPE_NEEDS_DESTRUCTOR in case
566 they changed since the variant was originally created.
567
568 This seems hokey; if there is some way to use a previous
569 variant *without* coming through here,
570 TYPE_NEEDS_CONSTRUCTING will never be updated. */
571 TYPE_NEEDS_CONSTRUCTING (t)
572 = TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (element_type));
573 TYPE_NEEDS_DESTRUCTOR (t)
574 = TYPE_NEEDS_DESTRUCTOR (TYPE_MAIN_VARIANT (element_type));
575 return t;
f376e137 576 }
2adeacc9
MM
577 else if (TYPE_PTRMEMFUNC_P (type))
578 {
579 /* For a pointer-to-member type, we can't just return a
580 cv-qualified version of the RECORD_TYPE. If we do, we
581 haven't change the field that contains the actual pointer to
582 a method, and so TYPE_PTRMEMFUNC_FN_TYPE will be wrong. */
583 tree t;
584
585 t = TYPE_PTRMEMFUNC_FN_TYPE (type);
586 t = cp_build_qualified_type_real (t, type_quals, complain);
46cbda4a 587 return build_ptrmemfunc_type (t);
2adeacc9
MM
588 }
589
590 /* Retrieve (or create) the appropriately qualified variant. */
591 result = build_qualified_type (type, type_quals);
592
593 /* If this was a pointer-to-method type, and we just made a copy,
594 then we need to clear the cached associated
595 pointer-to-member-function type; it is not valid for the new
596 type. */
597 if (result != type
598 && TREE_CODE (type) == POINTER_TYPE
599 && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
600 TYPE_SET_PTRMEMFUNC_TYPE (result, NULL_TREE);
601
602 return result;
f376e137 603}
53929c47
JM
604
605/* Returns the canonical version of TYPE. In other words, if TYPE is
606 a typedef, returns the underlying type. The cv-qualification of
607 the type returned matches the type input; they will always be
608 compatible types. */
609
610tree
611canonical_type_variant (t)
612 tree t;
613{
91063b51 614 return cp_build_qualified_type (TYPE_MAIN_VARIANT (t), CP_TYPE_QUALS (t));
53929c47 615}
f376e137 616\f
8d08fdba
MS
617/* Add OFFSET to all base types of T.
618
619 OFFSET, which is a type offset, is number of bytes.
620
621 Note that we don't have to worry about having two paths to the
622 same base type, since this type owns its association list. */
e92cc029 623
9a71c18b 624static void
8d08fdba
MS
625propagate_binfo_offsets (binfo, offset)
626 tree binfo;
627 tree offset;
628{
629 tree binfos = BINFO_BASETYPES (binfo);
630 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
631
632 for (i = 0; i < n_baselinks; /* note increment is done in the loop. */)
633 {
634 tree base_binfo = TREE_VEC_ELT (binfos, i);
635
636 if (TREE_VIA_VIRTUAL (base_binfo))
dfbcd65a 637 i += 1;
8d08fdba
MS
638 else
639 {
640 int j;
a703fb38 641 tree delta = NULL_TREE;
8d08fdba
MS
642
643 for (j = i+1; j < n_baselinks; j++)
644 if (! TREE_VIA_VIRTUAL (TREE_VEC_ELT (binfos, j)))
645 {
646 /* The next basetype offset must take into account the space
647 between the classes, not just the size of each class. */
648 delta = size_binop (MINUS_EXPR,
649 BINFO_OFFSET (TREE_VEC_ELT (binfos, j)),
650 BINFO_OFFSET (base_binfo));
651 break;
652 }
653
654#if 0
655 if (BINFO_OFFSET_ZEROP (base_binfo))
656 BINFO_OFFSET (base_binfo) = offset;
657 else
658 BINFO_OFFSET (base_binfo)
659 = size_binop (PLUS_EXPR, BINFO_OFFSET (base_binfo), offset);
660#else
661 BINFO_OFFSET (base_binfo) = offset;
662#endif
9a71c18b 663
dfbcd65a 664 propagate_binfo_offsets (base_binfo, offset);
8d08fdba
MS
665
666 /* Go to our next class that counts for offset propagation. */
667 i = j;
668 if (i < n_baselinks)
669 offset = size_binop (PLUS_EXPR, offset, delta);
670 }
671 }
672}
673
dfbcd65a 674/* Makes new binfos for the indirect bases under BINFO, and updates
9a71c18b
JM
675 BINFO_OFFSET for them and their bases. */
676
dfbcd65a
JM
677void
678unshare_base_binfos (binfo)
679 tree binfo;
9a71c18b 680{
dfbcd65a
JM
681 tree binfos = BINFO_BASETYPES (binfo);
682 tree new_binfo;
683 int j;
9a71c18b 684
dfbcd65a
JM
685 if (binfos == NULL_TREE)
686 return;
9a71c18b 687
dfbcd65a
JM
688 /* Now unshare the structure beneath BINFO. */
689 for (j = TREE_VEC_LENGTH (binfos)-1;
690 j >= 0; j--)
691 {
692 tree base_binfo = TREE_VEC_ELT (binfos, j);
693 new_binfo = TREE_VEC_ELT (binfos, j)
694 = make_binfo (BINFO_OFFSET (base_binfo),
695 base_binfo,
696 BINFO_VTABLE (base_binfo),
697 BINFO_VIRTUALS (base_binfo));
698 TREE_VIA_PUBLIC (new_binfo) = TREE_VIA_PUBLIC (base_binfo);
699 TREE_VIA_PROTECTED (new_binfo) = TREE_VIA_PROTECTED (base_binfo);
700 TREE_VIA_VIRTUAL (new_binfo) = TREE_VIA_VIRTUAL (base_binfo);
701 BINFO_INHERITANCE_CHAIN (new_binfo) = binfo;
702 unshare_base_binfos (new_binfo);
9a71c18b
JM
703 }
704}
705
706/* Finish the work of layout_record, now taking virtual bases into account.
707 Also compute the actual offsets that our base classes will have.
708 This must be performed after the fields are laid out, since virtual
709 baseclasses must lay down at the end of the record.
8d08fdba 710
9a71c18b 711 Returns the maximum number of virtual functions any of the
8d08fdba 712 baseclasses provide. */
e92cc029 713
8d08fdba 714int
9a71c18b 715layout_basetypes (rec, max)
8d08fdba
MS
716 tree rec;
717 int max;
718{
9a71c18b
JM
719 tree binfos = TYPE_BINFO_BASETYPES (rec);
720 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
721
ca107ded 722 tree vbase_types;
8d08fdba 723
f8344bea 724 unsigned int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
f8344bea 725 unsigned int desired_align;
8d08fdba 726
0b41abe6 727 /* Record size so far is CONST_SIZE bits, where CONST_SIZE is an integer. */
f8344bea 728 register unsigned int const_size = 0;
f8344bea 729 unsigned int nonvirtual_const_size;
8d08fdba 730
0b41abe6
JM
731#ifdef STRUCTURE_SIZE_BOUNDARY
732 /* Packed structures don't need to have minimum size. */
733 if (! TYPE_PACKED (rec))
734 record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY);
735#endif
736
ca107ded
MM
737 /* Get all the virtual base types that this type uses. The
738 TREE_VALUE slot holds the virtual baseclass type. Note that
739 get_vbase_types makes copies of the virtual base BINFOs, so that
740 the vbase_types are unshared. */
70c532b5 741 vbase_types = CLASSTYPE_VBASECLASSES (rec);
8d08fdba 742
0b41abe6
JM
743 my_friendly_assert (TREE_CODE (TYPE_SIZE (rec)) == INTEGER_CST, 19970302);
744 const_size = TREE_INT_CST_LOW (TYPE_SIZE (rec));
8d08fdba
MS
745
746 nonvirtual_const_size = const_size;
8d08fdba
MS
747
748 while (vbase_types)
749 {
750 tree basetype = BINFO_TYPE (vbase_types);
751 tree offset;
752
f0e01782
MS
753 desired_align = TYPE_ALIGN (basetype);
754 record_align = MAX (record_align, desired_align);
755
8d08fdba
MS
756 if (const_size == 0)
757 offset = integer_zero_node;
758 else
f0e01782
MS
759 {
760 /* Give each virtual base type the alignment it wants. */
9a71c18b 761 const_size = CEIL (const_size, desired_align) * desired_align;
f0e01782
MS
762 offset = size_int (CEIL (const_size, BITS_PER_UNIT));
763 }
8d08fdba
MS
764
765 if (CLASSTYPE_VSIZE (basetype) > max)
766 max = CLASSTYPE_VSIZE (basetype);
767 BINFO_OFFSET (vbase_types) = offset;
768
0b41abe6
JM
769 /* Every virtual baseclass takes a least a UNIT, so that we can
770 take it's address and get something different for each base. */
771 const_size += MAX (BITS_PER_UNIT,
772 TREE_INT_CST_LOW (CLASSTYPE_SIZE (basetype)));
8d08fdba
MS
773
774 vbase_types = TREE_CHAIN (vbase_types);
775 }
776
e1cd6e56
MS
777 if (const_size)
778 {
779 /* Because a virtual base might take a single byte above,
38e01259 780 we have to re-adjust the total size to make sure it is
e1cd6e56
MS
781 a multiple of the alignment. */
782 /* Give the whole object the alignment it wants. */
783 const_size = CEIL (const_size, record_align) * record_align;
784 }
785
f0e01782
MS
786 /* Set the alignment in the complete type. We don't set CLASSTYPE_ALIGN
787 here, as that is for this class, without any virtual base classes. */
788 TYPE_ALIGN (rec) = record_align;
8d08fdba 789 if (const_size != nonvirtual_const_size)
25868f6c
SS
790 {
791 TYPE_SIZE (rec) = size_int (const_size);
792 TYPE_SIZE_UNIT (rec) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (rec),
793 size_int (BITS_PER_UNIT));
794 }
8d08fdba 795
9a71c18b
JM
796 /* Now propagate offset information throughout the lattice. */
797 for (i = 0; i < n_baseclasses; i++)
8d08fdba 798 {
9a71c18b
JM
799 register tree base_binfo = TREE_VEC_ELT (binfos, i);
800 register tree basetype = BINFO_TYPE (base_binfo);
801 tree field = TYPE_FIELDS (rec);
8ccc31eb 802
9a71c18b 803 if (TREE_VIA_VIRTUAL (base_binfo))
dfbcd65a 804 continue;
ca107ded 805
dfbcd65a 806 my_friendly_assert (TREE_TYPE (field) == basetype, 23897);
ca107ded 807
dfbcd65a 808 if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
8251199e 809 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
dfbcd65a
JM
810 basetype, rec);
811
812 BINFO_OFFSET (base_binfo)
813 = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field)),
814 BITS_PER_UNIT));
815 propagate_binfo_offsets (base_binfo, BINFO_OFFSET (base_binfo));
816 TYPE_FIELDS (rec) = TREE_CHAIN (field);
9a71c18b 817 }
8d08fdba 818
9a71c18b
JM
819 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
820 vbase_types = TREE_CHAIN (vbase_types))
821 {
822 BINFO_INHERITANCE_CHAIN (vbase_types) = TYPE_BINFO (rec);
823 unshare_base_binfos (vbase_types);
dfbcd65a 824 propagate_binfo_offsets (vbase_types, BINFO_OFFSET (vbase_types));
ca107ded
MM
825
826 if (extra_warnings)
827 {
828 tree basetype = BINFO_TYPE (vbase_types);
829 if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
8251199e 830 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
ca107ded
MM
831 basetype, rec);
832 }
8d08fdba
MS
833 }
834
835 return max;
836}
837
732dcb6f
JM
838/* If the empty base field in DECL overlaps with a base of the same type in
839 NEWDECL, which is either another base field or the first data field of
840 the class, pad the base just before NEWDECL and return 1. Otherwise,
841 return 0. */
842
843static int
844avoid_overlap (decl, newdecl)
845 tree decl, newdecl;
846{
847 tree field;
848
849 if (newdecl == NULL_TREE
850 || ! types_overlap_p (TREE_TYPE (decl), TREE_TYPE (newdecl)))
851 return 0;
852
853 for (field = decl; TREE_CHAIN (field) && TREE_CHAIN (field) != newdecl;
854 field = TREE_CHAIN (field))
855 ;
856
857 DECL_SIZE (field) = integer_one_node;
80cd3eca
JM
858
859 return 1;
732dcb6f
JM
860}
861
9a71c18b
JM
862/* Returns a list of fields to stand in for the base class subobjects
863 of REC. These fields are later removed by layout_basetypes. */
864
865tree
866build_base_fields (rec)
867 tree rec;
868{
869 /* Chain to hold all the new FIELD_DECLs which stand in for base class
870 subobjects. */
871 tree base_decls = NULL_TREE;
872 tree binfos = TYPE_BINFO_BASETYPES (rec);
873 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
732dcb6f
JM
874 tree decl, nextdecl;
875 int i, saw_empty = 0;
9a71c18b
JM
876 unsigned int base_align = 0;
877
878 for (i = 0; i < n_baseclasses; ++i)
879 {
880 register tree base_binfo = TREE_VEC_ELT (binfos, i);
881 register tree basetype = BINFO_TYPE (base_binfo);
882
883 if (TYPE_SIZE (basetype) == 0)
884 /* This error is now reported in xref_tag, thus giving better
885 location information. */
886 continue;
887
888 if (TREE_VIA_VIRTUAL (base_binfo))
889 continue;
8d08fdba 890
4ce3d537 891 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
9a71c18b
JM
892 DECL_ARTIFICIAL (decl) = 1;
893 DECL_FIELD_CONTEXT (decl) = DECL_CLASS_CONTEXT (decl) = rec;
894 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
895 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
896 TREE_CHAIN (decl) = base_decls;
897 base_decls = decl;
898
732dcb6f
JM
899 if (! flag_new_abi)
900 {
901 /* Brain damage for backwards compatibility. For no good reason,
902 the old layout_basetypes made every base at least as large as
903 the alignment for the bases up to that point, gratuitously
904 wasting space. So we do the same thing here. */
905 base_align = MAX (base_align, DECL_ALIGN (decl));
906 DECL_SIZE (decl)
907 = size_int (MAX (TREE_INT_CST_LOW (DECL_SIZE (decl)),
1ddb2906 908 (int) base_align));
732dcb6f
JM
909 }
910 else if (DECL_SIZE (decl) == integer_zero_node)
911 saw_empty = 1;
9a71c18b 912 }
8d08fdba 913
9a71c18b
JM
914 /* Reverse the list of fields so we allocate the bases in the proper
915 order. */
732dcb6f
JM
916 base_decls = nreverse (base_decls);
917
918 /* In the presence of empty base classes, we run the risk of allocating
919 two objects of the same class on top of one another. Avoid that. */
920 if (flag_new_abi && saw_empty)
921 for (decl = base_decls; decl; decl = TREE_CHAIN (decl))
922 {
923 if (DECL_SIZE (decl) == integer_zero_node)
924 {
925 /* First step through the following bases until we find
926 an overlap or a non-empty base. */
927 for (nextdecl = TREE_CHAIN (decl); nextdecl;
928 nextdecl = TREE_CHAIN (nextdecl))
929 {
930 if (avoid_overlap (decl, nextdecl)
931 || DECL_SIZE (nextdecl) != integer_zero_node)
932 goto nextbase;
933 }
934
935 /* If we're still looking, also check against the first
936 field. */
937 for (nextdecl = TYPE_FIELDS (rec);
938 nextdecl && TREE_CODE (nextdecl) != FIELD_DECL;
939 nextdecl = TREE_CHAIN (nextdecl))
940 /* keep looking */;
941 avoid_overlap (decl, nextdecl);
942 }
943 nextbase:;
944 }
945
946 return base_decls;
9a71c18b 947}
7177d104 948
9a71c18b 949/* Returns list of virtual base class pointers in a FIELD_DECL chain. */
e92cc029 950
8d08fdba 951tree
9a71c18b
JM
952build_vbase_pointer_fields (rec)
953 tree rec;
8d08fdba
MS
954{
955 /* Chain to hold all the new FIELD_DECLs which point at virtual
956 base classes. */
957 tree vbase_decls = NULL_TREE;
9a71c18b
JM
958 tree binfos = TYPE_BINFO_BASETYPES (rec);
959 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
960 tree decl;
961 int i;
0b41abe6 962
8d08fdba
MS
963 /* Handle basetypes almost like fields, but record their
964 offsets differently. */
965
966 for (i = 0; i < n_baseclasses; i++)
967 {
8d08fdba
MS
968 register tree base_binfo = TREE_VEC_ELT (binfos, i);
969 register tree basetype = BINFO_TYPE (base_binfo);
8d08fdba
MS
970
971 if (TYPE_SIZE (basetype) == 0)
0b41abe6
JM
972 /* This error is now reported in xref_tag, thus giving better
973 location information. */
974 continue;
8d08fdba
MS
975
976 /* All basetypes are recorded in the association list of the
977 derived type. */
978
979 if (TREE_VIA_VIRTUAL (base_binfo))
980 {
981 int j;
d8e178a0 982 const char *name;
8d08fdba
MS
983
984 /* The offset for a virtual base class is only used in computing
985 virtual function tables and for initializing virtual base
986 pointers. It is built once `get_vbase_types' is called. */
987
988 /* If this basetype can come from another vbase pointer
989 without an additional indirection, we will share
990 that pointer. If an indirection is involved, we
991 make our own pointer. */
992 for (j = 0; j < n_baseclasses; j++)
993 {
994 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
995 if (! TREE_VIA_VIRTUAL (other_base_binfo)
996 && binfo_member (basetype,
9a71c18b
JM
997 CLASSTYPE_VBASECLASSES (BINFO_TYPE
998 (other_base_binfo))
999 ))
8d08fdba
MS
1000 goto got_it;
1001 }
38f01e5a 1002 FORMAT_VBASE_NAME (name, basetype);
4ce3d537
MM
1003 decl = build_lang_decl (FIELD_DECL, get_identifier (name),
1004 build_pointer_type (basetype));
8d08fdba
MS
1005 /* If you change any of the below, take a look at all the
1006 other VFIELD_BASEs and VTABLE_BASEs in the code, and change
e92cc029 1007 them too. */
8d08fdba
MS
1008 DECL_ASSEMBLER_NAME (decl) = get_identifier (VTABLE_BASE);
1009 DECL_VIRTUAL_P (decl) = 1;
d2e5ee5c 1010 DECL_ARTIFICIAL (decl) = 1;
8d08fdba
MS
1011 DECL_FIELD_CONTEXT (decl) = rec;
1012 DECL_CLASS_CONTEXT (decl) = rec;
1013 DECL_FCONTEXT (decl) = basetype;
49ad7cfa 1014 DECL_SAVED_INSNS (decl) = 0;
8d08fdba
MS
1015 DECL_FIELD_SIZE (decl) = 0;
1016 DECL_ALIGN (decl) = TYPE_ALIGN (ptr_type_node);
1017 TREE_CHAIN (decl) = vbase_decls;
1018 BINFO_VPTR_FIELD (base_binfo) = decl;
1019 vbase_decls = decl;
1020
8d08fdba
MS
1021 got_it:
1022 /* The space this decl occupies has already been accounted for. */
9a71c18b 1023 ;
8d08fdba 1024 }
8d08fdba
MS
1025 }
1026
8d08fdba
MS
1027 return vbase_decls;
1028}
1029\f
1030/* Hashing of lists so that we don't make duplicates.
1031 The entry point is `list_hash_canon'. */
1032
1033/* Each hash table slot is a bucket containing a chain
1034 of these structures. */
1035
1036struct list_hash
1037{
1038 struct list_hash *next; /* Next structure in the bucket. */
1039 int hashcode; /* Hash code of this list. */
1040 tree list; /* The list recorded here. */
1041};
1042
1043/* Now here is the hash table. When recording a list, it is added
1044 to the slot whose index is the hash code mod the table size.
1045 Note that the hash table is used for several kinds of lists.
1046 While all these live in the same table, they are completely independent,
1047 and the hash code is computed differently for each of these. */
1048
1049#define TYPE_HASH_SIZE 59
37c46b43 1050static struct list_hash *list_hash_table[TYPE_HASH_SIZE];
8d08fdba
MS
1051
1052/* Compute a hash code for a list (chain of TREE_LIST nodes
1053 with goodies in the TREE_PURPOSE, TREE_VALUE, and bits of the
1054 TREE_COMMON slots), by adding the hash codes of the individual entries. */
1055
37c46b43
MS
1056static int
1057list_hash (purpose, value, chain)
1058 tree purpose, value, chain;
8d08fdba
MS
1059{
1060 register int hashcode = 0;
1061
37c46b43
MS
1062 if (chain)
1063 hashcode += TYPE_HASH (chain);
8d08fdba 1064
37c46b43
MS
1065 if (value)
1066 hashcode += TYPE_HASH (value);
8d08fdba
MS
1067 else
1068 hashcode += 1007;
37c46b43
MS
1069 if (purpose)
1070 hashcode += TYPE_HASH (purpose);
8d08fdba
MS
1071 else
1072 hashcode += 1009;
1073 return hashcode;
1074}
1075
1076/* Look in the type hash table for a type isomorphic to TYPE.
1077 If one is found, return it. Otherwise return 0. */
1078
37c46b43 1079static tree
51632249
JM
1080list_hash_lookup (hashcode, purpose, value, chain)
1081 int hashcode;
37c46b43 1082 tree purpose, value, chain;
8d08fdba
MS
1083{
1084 register struct list_hash *h;
37c46b43 1085
8d08fdba
MS
1086 for (h = list_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
1087 if (h->hashcode == hashcode
37c46b43
MS
1088 && TREE_PURPOSE (h->list) == purpose
1089 && TREE_VALUE (h->list) == value
1090 && TREE_CHAIN (h->list) == chain)
1091 return h->list;
8d08fdba
MS
1092 return 0;
1093}
1094
1095/* Add an entry to the list-hash-table
1096 for a list TYPE whose hash code is HASHCODE. */
1097
37c46b43 1098static void
8d08fdba
MS
1099list_hash_add (hashcode, list)
1100 int hashcode;
1101 tree list;
1102{
1103 register struct list_hash *h;
1104
1105 h = (struct list_hash *) obstack_alloc (&class_obstack, sizeof (struct list_hash));
1106 h->hashcode = hashcode;
1107 h->list = list;
1108 h->next = list_hash_table[hashcode % TYPE_HASH_SIZE];
1109 list_hash_table[hashcode % TYPE_HASH_SIZE] = h;
1110}
1111
51632249
JM
1112/* Given list components PURPOSE, VALUE, AND CHAIN, return the canonical
1113 object for an identical list if one already exists. Otherwise, build a
1114 new one, and record it as the canonical object. */
8d08fdba
MS
1115
1116/* Set to 1 to debug without canonicalization. Never set by program. */
e92cc029 1117
a0a33927 1118static int debug_no_list_hash = 0;
8d08fdba 1119
8d08fdba 1120tree
51632249 1121hash_tree_cons (purpose, value, chain)
8d08fdba
MS
1122 tree purpose, value, chain;
1123{
1124 struct obstack *ambient_obstack = current_obstack;
1125 tree t;
a703fb38 1126 int hashcode = 0;
8d08fdba 1127
37c46b43
MS
1128 if (! debug_no_list_hash)
1129 {
1130 hashcode = list_hash (purpose, value, chain);
51632249 1131 t = list_hash_lookup (hashcode, purpose, value, chain);
37c46b43
MS
1132 if (t)
1133 return t;
1134 }
1135
8d08fdba 1136 current_obstack = &class_obstack;
37c46b43 1137
8d08fdba 1138 t = tree_cons (purpose, value, chain);
37c46b43
MS
1139
1140 /* If this is a new list, record it for later reuse. */
1141 if (! debug_no_list_hash)
1142 list_hash_add (hashcode, t);
1143
8d08fdba
MS
1144 current_obstack = ambient_obstack;
1145 return t;
1146}
1147
1148/* Constructor for hashed lists. */
e92cc029 1149
8d08fdba
MS
1150tree
1151hash_tree_chain (value, chain)
1152 tree value, chain;
1153{
51632249 1154 return hash_tree_cons (NULL_TREE, value, chain);
8d08fdba
MS
1155}
1156
1157/* Similar, but used for concatenating two lists. */
e92cc029 1158
8d08fdba
MS
1159tree
1160hash_chainon (list1, list2)
1161 tree list1, list2;
1162{
1163 if (list2 == 0)
1164 return list1;
1165 if (list1 == 0)
1166 return list2;
1167 if (TREE_CHAIN (list1) == NULL_TREE)
1168 return hash_tree_chain (TREE_VALUE (list1), list2);
1169 return hash_tree_chain (TREE_VALUE (list1),
1170 hash_chainon (TREE_CHAIN (list1), list2));
1171}
8d08fdba
MS
1172\f
1173/* Build an association between TYPE and some parameters:
1174
1175 OFFSET is the offset added to `this' to convert it to a pointer
1176 of type `TYPE *'
1177
8926095f
MS
1178 BINFO is the base binfo to use, if we are deriving from one. This
1179 is necessary, as we want specialized parent binfos from base
1180 classes, so that the VTABLE_NAMEs of bases are for the most derived
38e01259 1181 type, instead of the simple type.
8926095f 1182
8d08fdba
MS
1183 VTABLE is the virtual function table with which to initialize
1184 sub-objects of type TYPE.
1185
ca107ded 1186 VIRTUALS are the virtual functions sitting in VTABLE. */
8d08fdba
MS
1187
1188tree
ca107ded 1189make_binfo (offset, binfo, vtable, virtuals)
8926095f 1190 tree offset, binfo;
8d08fdba 1191 tree vtable, virtuals;
8d08fdba 1192{
6c011b01 1193 tree new_binfo = make_tree_vec (7);
8926095f 1194 tree type;
8d08fdba 1195
8926095f
MS
1196 if (TREE_CODE (binfo) == TREE_VEC)
1197 type = BINFO_TYPE (binfo);
1198 else
1199 {
1200 type = binfo;
7ddedda4 1201 binfo = CLASS_TYPE_P (type) ? TYPE_BINFO (binfo) : NULL_TREE;
8926095f 1202 }
8d08fdba 1203
8926095f
MS
1204 TREE_TYPE (new_binfo) = TYPE_MAIN_VARIANT (type);
1205 BINFO_OFFSET (new_binfo) = offset;
1206 BINFO_VTABLE (new_binfo) = vtable;
1207 BINFO_VIRTUALS (new_binfo) = virtuals;
1208 BINFO_VPTR_FIELD (new_binfo) = NULL_TREE;
8d08fdba 1209
8926095f
MS
1210 if (binfo && BINFO_BASETYPES (binfo) != NULL_TREE)
1211 BINFO_BASETYPES (new_binfo) = copy_node (BINFO_BASETYPES (binfo));
1212 return new_binfo;
8d08fdba
MS
1213}
1214
8d08fdba
MS
1215/* Return the binfo value for ELEM in TYPE. */
1216
1217tree
1218binfo_value (elem, type)
1219 tree elem;
1220 tree type;
1221{
1222 if (get_base_distance (elem, type, 0, (tree *)0) == -2)
8251199e 1223 compiler_error ("base class `%s' ambiguous in binfo_value",
8d08fdba
MS
1224 TYPE_NAME_STRING (elem));
1225 if (elem == type)
1226 return TYPE_BINFO (type);
1227 if (TREE_CODE (elem) == RECORD_TYPE && TYPE_BINFO (elem) == type)
1228 return type;
1229 return get_binfo (elem, type, 0);
1230}
1231
dfbcd65a 1232/* Return a reversed copy of the BINFO-chain given by PATH. (If the
ca107ded
MM
1233 BINFO_INHERITANCE_CHAIN points from base classes to derived
1234 classes, it will instead point from derived classes to base
dfbcd65a 1235 classes.) Returns the first node in the reversed chain. */
ca107ded 1236
8d08fdba 1237tree
dfbcd65a 1238reverse_path (path)
8d08fdba
MS
1239 tree path;
1240{
dfbcd65a
JM
1241 register tree prev = NULL_TREE, cur;
1242 push_expression_obstack ();
1243 for (cur = path; cur; cur = BINFO_INHERITANCE_CHAIN (cur))
8d08fdba 1244 {
dfbcd65a
JM
1245 tree r = copy_node (cur);
1246 BINFO_INHERITANCE_CHAIN (r) = prev;
1247 prev = r;
8d08fdba 1248 }
dfbcd65a 1249 pop_obstacks ();
8d08fdba
MS
1250 return prev;
1251}
1252
8d08fdba
MS
1253void
1254debug_binfo (elem)
1255 tree elem;
1256{
f30432d7 1257 unsigned HOST_WIDE_INT n;
8d08fdba
MS
1258 tree virtuals;
1259
71e89f27 1260 fprintf (stderr, "type \"%s\"; offset = %ld\n",
8d08fdba 1261 TYPE_NAME_STRING (BINFO_TYPE (elem)),
71e89f27 1262 (long) TREE_INT_CST_LOW (BINFO_OFFSET (elem)));
8d08fdba
MS
1263 fprintf (stderr, "vtable type:\n");
1264 debug_tree (BINFO_TYPE (elem));
1265 if (BINFO_VTABLE (elem))
1266 fprintf (stderr, "vtable decl \"%s\"\n", IDENTIFIER_POINTER (DECL_NAME (BINFO_VTABLE (elem))));
1267 else
1268 fprintf (stderr, "no vtable decl yet\n");
1269 fprintf (stderr, "virtuals:\n");
1270 virtuals = BINFO_VIRTUALS (elem);
f30432d7 1271
aff08c18 1272 n = skip_rtti_stuff (&virtuals, BINFO_TYPE (elem));
f30432d7 1273
8d08fdba
MS
1274 while (virtuals)
1275 {
83f2ccf4 1276 tree fndecl = TREE_VALUE (virtuals);
71e89f27 1277 fprintf (stderr, "%s [%ld =? %ld]\n",
8d08fdba 1278 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl)),
71e89f27 1279 (long) n, (long) TREE_INT_CST_LOW (DECL_VINDEX (fndecl)));
f30432d7 1280 ++n;
8d08fdba 1281 virtuals = TREE_CHAIN (virtuals);
8d08fdba
MS
1282 }
1283}
1284
2c73f9f5 1285/* Initialize an CPLUS_BINDING node that does not live on an obstack. */
8d08fdba 1286
2c73f9f5
ML
1287tree
1288binding_init (node)
1289 struct tree_binding* node;
8d08fdba 1290{
2c73f9f5
ML
1291 static struct tree_binding* source;
1292 if (!source)
1293 {
1294 extern struct obstack permanent_obstack;
1295 push_obstacks (&permanent_obstack, &permanent_obstack);
1296 source = (struct tree_binding*)make_node (CPLUS_BINDING);
1297 pop_obstacks ();
1298 }
1299 *node = *source;
1300 TREE_PERMANENT ((tree)node) = 0;
1301 return (tree)node;
8d08fdba
MS
1302}
1303
1304int
1305count_functions (t)
1306 tree t;
1307{
2c73f9f5 1308 int i;
8d08fdba
MS
1309 if (TREE_CODE (t) == FUNCTION_DECL)
1310 return 1;
2c73f9f5
ML
1311 else if (TREE_CODE (t) == OVERLOAD)
1312 {
1313 for (i=0; t; t = OVL_CHAIN (t))
1314 i++;
1315 return i;
1316 }
8d08fdba 1317
5b605f68 1318 my_friendly_abort (359);
0d16d68e 1319 return 0;
8d08fdba
MS
1320}
1321
8d08fdba
MS
1322int
1323is_overloaded_fn (x)
1324 tree x;
1325{
4bb0968f 1326 /* A baselink is also considered an overloaded function. */
05e0b2f4
JM
1327 if (TREE_CODE (x) == OFFSET_REF)
1328 x = TREE_OPERAND (x, 1);
4bb0968f
MM
1329 if (BASELINK_P (x))
1330 x = TREE_VALUE (x);
06ab59df
MM
1331 return (TREE_CODE (x) == FUNCTION_DECL
1332 || TREE_CODE (x) == TEMPLATE_ID_EXPR
1333 || DECL_FUNCTION_TEMPLATE_P (x)
2c73f9f5 1334 || TREE_CODE (x) == OVERLOAD);
8d08fdba
MS
1335}
1336
8926095f
MS
1337int
1338really_overloaded_fn (x)
1339 tree x;
1340{
4bb0968f 1341 /* A baselink is also considered an overloaded function. */
05e0b2f4
JM
1342 if (TREE_CODE (x) == OFFSET_REF)
1343 x = TREE_OPERAND (x, 1);
4bb0968f 1344 if (BASELINK_P (x))
2c73f9f5
ML
1345 x = TREE_VALUE (x);
1346 return (TREE_CODE (x) == OVERLOAD
1347 && (TREE_CHAIN (x) != NULL_TREE
1348 || DECL_FUNCTION_TEMPLATE_P (OVL_FUNCTION (x))));
8926095f
MS
1349}
1350
8d08fdba
MS
1351tree
1352get_first_fn (from)
1353 tree from;
1354{
06ab59df 1355 my_friendly_assert (is_overloaded_fn (from), 9);
2c73f9f5 1356 /* A baselink is also considered an overloaded function. */
4bb0968f 1357 if (BASELINK_P (from))
2c73f9f5
ML
1358 from = TREE_VALUE (from);
1359 return OVL_CURRENT (from);
1360}
8d08fdba 1361
8d7f862c
JM
1362/* Returns nonzero if T is a ->* or .* expression that refers to a
1363 member function. */
1364
1365int
1366bound_pmf_p (t)
1367 tree t;
1368{
1369 return (TREE_CODE (t) == OFFSET_REF
1370 && TYPE_PTRMEMFUNC_P (TREE_TYPE (TREE_OPERAND (t, 1))));
1371}
1372
2c73f9f5
ML
1373/* Return a new OVL node, concatenating it with the old one. */
1374
1375tree
1376ovl_cons (decl, chain)
1377 tree decl;
1378 tree chain;
1379{
1380 tree result = make_node (OVERLOAD);
1381 TREE_TYPE (result) = unknown_type_node;
1382 OVL_FUNCTION (result) = decl;
1383 TREE_CHAIN (result) = chain;
1384
1385 return result;
1386}
1387
1388/* Same as ovl_cons, but on the scratch_obstack. */
1389
1390tree
1391scratch_ovl_cons (value, chain)
1392 tree value, chain;
1393{
1394 register tree node;
1395 register struct obstack *ambient_obstack = current_obstack;
1396 extern struct obstack *expression_obstack;
1397 current_obstack = expression_obstack;
1398 node = ovl_cons (value, chain);
1399 current_obstack = ambient_obstack;
1400 return node;
1401}
1402
1403/* Build a new overloaded function. If this is the first one,
1404 just return it; otherwise, ovl_cons the _DECLs */
1405
1406tree
1407build_overload (decl, chain)
1408 tree decl;
1409 tree chain;
1410{
161c12b0 1411 if (! chain && TREE_CODE (decl) != TEMPLATE_DECL)
2c73f9f5 1412 return decl;
161c12b0 1413 if (chain && TREE_CODE (chain) != OVERLOAD)
2c73f9f5
ML
1414 chain = ovl_cons (chain, NULL_TREE);
1415 return ovl_cons (decl, chain);
1416}
1417
1418/* True if fn is in ovl. */
1419
1420int
1421ovl_member (fn, ovl)
1422 tree fn;
1423 tree ovl;
1424{
92ac31f1 1425 if (ovl == NULL_TREE)
2c73f9f5 1426 return 0;
92ac31f1 1427 if (TREE_CODE (ovl) != OVERLOAD)
2c169bab 1428 return ovl == fn;
2c73f9f5 1429 for (; ovl; ovl = OVL_CHAIN (ovl))
2c169bab 1430 if (OVL_FUNCTION (ovl) == fn)
2c73f9f5
ML
1431 return 1;
1432 return 0;
8d08fdba
MS
1433}
1434
8d08fdba
MS
1435int
1436is_aggr_type_2 (t1, t2)
1437 tree t1, t2;
1438{
1439 if (TREE_CODE (t1) != TREE_CODE (t2))
1440 return 0;
1441 return IS_AGGR_TYPE (t1) && IS_AGGR_TYPE (t2);
1442}
8d08fdba
MS
1443\f
1444#define PRINT_RING_SIZE 4
1445
1446char *
2ba25f50 1447lang_printable_name (decl, v)
8d08fdba 1448 tree decl;
2ba25f50 1449 int v;
8d08fdba
MS
1450{
1451 static tree decl_ring[PRINT_RING_SIZE];
1452 static char *print_ring[PRINT_RING_SIZE];
1453 static int ring_counter;
1454 int i;
1455
1456 /* Only cache functions. */
2ba25f50
MS
1457 if (v < 2
1458 || TREE_CODE (decl) != FUNCTION_DECL
8d08fdba 1459 || DECL_LANG_SPECIFIC (decl) == 0)
2ba25f50 1460 return lang_decl_name (decl, v);
8d08fdba
MS
1461
1462 /* See if this print name is lying around. */
1463 for (i = 0; i < PRINT_RING_SIZE; i++)
1464 if (decl_ring[i] == decl)
1465 /* yes, so return it. */
1466 return print_ring[i];
1467
1468 if (++ring_counter == PRINT_RING_SIZE)
1469 ring_counter = 0;
1470
1471 if (current_function_decl != NULL_TREE)
1472 {
1473 if (decl_ring[ring_counter] == current_function_decl)
1474 ring_counter += 1;
1475 if (ring_counter == PRINT_RING_SIZE)
1476 ring_counter = 0;
1477 if (decl_ring[ring_counter] == current_function_decl)
1478 my_friendly_abort (106);
1479 }
1480
1481 if (print_ring[ring_counter])
1482 free (print_ring[ring_counter]);
1483
2ba25f50
MS
1484 print_ring[ring_counter] = xstrdup (lang_decl_name (decl, v));
1485 decl_ring[ring_counter] = decl;
8d08fdba
MS
1486 return print_ring[ring_counter];
1487}
1488\f
f30432d7 1489/* Build the FUNCTION_TYPE or METHOD_TYPE which may throw exceptions
8d08fdba 1490 listed in RAISES. */
e92cc029 1491
8d08fdba 1492tree
f30432d7
MS
1493build_exception_variant (type, raises)
1494 tree type;
8d08fdba
MS
1495 tree raises;
1496{
8d08fdba 1497 tree v = TYPE_MAIN_VARIANT (type);
91063b51 1498 int type_quals = TYPE_QUALS (type);
8d08fdba 1499
45537677 1500 for (; v; v = TYPE_NEXT_VARIANT (v))
4cc1d462
NS
1501 if (TYPE_QUALS (v) == type_quals
1502 && comp_except_specs (raises, TYPE_RAISES_EXCEPTIONS (v), 1))
1503 return v;
8d08fdba
MS
1504
1505 /* Need to build a new variant. */
45537677
MS
1506 v = build_type_copy (type);
1507
8d08fdba 1508 if (raises && ! TREE_PERMANENT (raises))
0dd3962d 1509 raises = copy_to_permanent (raises);
5566b478 1510
8d08fdba
MS
1511 TYPE_RAISES_EXCEPTIONS (v) = raises;
1512 return v;
1513}
1514
73b0fce8
KL
1515/* Given a TEMPLATE_TEMPLATE_PARM node T, create a new one together with its
1516 lang_specific field and its corresponding TEMPLATE_DECL node */
1517
1518tree
1519copy_template_template_parm (t)
1520 tree t;
1521{
1522 tree template = TYPE_NAME (t);
6b9b6b15
JM
1523 tree t2;
1524
1525 /* Make sure these end up on the permanent_obstack. */
9188c363 1526 push_permanent_obstack ();
6b9b6b15
JM
1527
1528 t2 = make_lang_type (TEMPLATE_TEMPLATE_PARM);
73b0fce8
KL
1529 template = copy_node (template);
1530 copy_lang_decl (template);
6b9b6b15
JM
1531
1532 pop_obstacks ();
1533
73b0fce8
KL
1534 TREE_TYPE (template) = t2;
1535 TYPE_NAME (t2) = template;
1536 TYPE_STUB_DECL (t2) = template;
1537
1538 /* No need to copy these */
1539 TYPE_FIELDS (t2) = TYPE_FIELDS (t);
7ddedda4
MM
1540 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t2)
1541 = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
73b0fce8
KL
1542 return t2;
1543}
1544
50a6dbd7
JM
1545/* Walk through the tree structure T, applying func. If func ever returns
1546 non-null, return that value. */
1547
297e73d8 1548tree
50a6dbd7
JM
1549search_tree (t, func)
1550 tree t;
1551 tree (*func) PROTO((tree));
1552{
acb619d9 1553#define TRY(ARG) if (tmp=search_tree (ARG, func), tmp != NULL_TREE) return tmp
50a6dbd7
JM
1554
1555 tree tmp;
2adeacc9 1556 enum tree_code code;
50a6dbd7
JM
1557
1558 if (t == NULL_TREE)
1559 return t;
2adeacc9
MM
1560
1561 tmp = func (t);
1562 if (tmp)
50a6dbd7
JM
1563 return tmp;
1564
2adeacc9
MM
1565 /* Handle some common cases up front. */
1566 code = TREE_CODE (t);
1567 if (TREE_CODE_CLASS (code) == '1')
1568 {
1569 TRY (TREE_OPERAND (t, 0));
1570 return NULL_TREE;
1571 }
1572 else if (TREE_CODE_CLASS (code) == '2' || TREE_CODE_CLASS (code) == '<')
1573 {
1574 TRY (TREE_OPERAND (t, 0));
1575 TRY (TREE_OPERAND (t, 1));
1576 return NULL_TREE;
1577 }
1578
1579 switch (code)
50a6dbd7
JM
1580 {
1581 case ERROR_MARK:
1582 break;
1583
1584 case IDENTIFIER_NODE:
1585 break;
1586
1587 case VAR_DECL:
1588 case FUNCTION_DECL:
1589 case CONST_DECL:
1590 case TEMPLATE_DECL:
1591 case NAMESPACE_DECL:
1592 break;
1593
1594 case TYPE_DECL:
1595 TRY (TREE_TYPE (t));
1596 break;
1597
1598 case PARM_DECL:
1599 TRY (TREE_TYPE (t));
1600 TRY (TREE_CHAIN (t));
1601 break;
1602
1603 case TREE_LIST:
1604 TRY (TREE_PURPOSE (t));
1605 TRY (TREE_VALUE (t));
1606 TRY (TREE_CHAIN (t));
1607 break;
1608
1609 case OVERLOAD:
1610 TRY (OVL_FUNCTION (t));
1611 TRY (OVL_CHAIN (t));
1612 break;
1613
1614 case TREE_VEC:
1615 {
1616 int len = TREE_VEC_LENGTH (t);
1617
1618 t = copy_node (t);
1619 while (len--)
1620 TRY (TREE_VEC_ELT (t, len));
1621 }
1622 break;
1623
1624 case INTEGER_CST:
1625 case REAL_CST:
1626 case STRING_CST:
1627 case DEFAULT_ARG:
1628 break;
1629
61a127b3
MM
1630 case PTRMEM_CST:
1631 TRY (TREE_TYPE (t));
1632 break;
1633
50a6dbd7
JM
1634 case COND_EXPR:
1635 case TARGET_EXPR:
1636 case AGGR_INIT_EXPR:
1637 case NEW_EXPR:
1638 TRY (TREE_OPERAND (t, 0));
1639 TRY (TREE_OPERAND (t, 1));
1640 TRY (TREE_OPERAND (t, 2));
1641 break;
1642
88049523
RH
1643 case TRUTH_AND_EXPR:
1644 case TRUTH_OR_EXPR:
1645 case TRUTH_XOR_EXPR:
50a6dbd7
JM
1646 case TRUTH_ANDIF_EXPR:
1647 case TRUTH_ORIF_EXPR:
50a6dbd7
JM
1648 case PREDECREMENT_EXPR:
1649 case PREINCREMENT_EXPR:
1650 case POSTDECREMENT_EXPR:
1651 case POSTINCREMENT_EXPR:
1652 case ARRAY_REF:
1653 case SCOPE_REF:
1654 case TRY_CATCH_EXPR:
1655 case WITH_CLEANUP_EXPR:
1656 case CALL_EXPR:
558475f0
MM
1657 case COMPOUND_EXPR:
1658 case MODIFY_EXPR:
f1dedc31 1659 case INIT_EXPR:
50a6dbd7
JM
1660 TRY (TREE_OPERAND (t, 0));
1661 TRY (TREE_OPERAND (t, 1));
1662 break;
1663
1664 case SAVE_EXPR:
50a6dbd7
JM
1665 case ADDR_EXPR:
1666 case INDIRECT_REF:
50a6dbd7 1667 case TRUTH_NOT_EXPR:
50a6dbd7
JM
1668 case COMPONENT_REF:
1669 case CLEANUP_POINT_EXPR:
1670 case LOOKUP_EXPR:
558475f0 1671 case THROW_EXPR:
22e9174f
MM
1672 case EXIT_EXPR:
1673 case LOOP_EXPR:
50a6dbd7
JM
1674 TRY (TREE_OPERAND (t, 0));
1675 break;
1676
1677 case MODOP_EXPR:
50a6dbd7
JM
1678 case ARROW_EXPR:
1679 case DOTSTAR_EXPR:
1680 case TYPEID_EXPR:
40242ccf 1681 case PSEUDO_DTOR_EXPR:
50a6dbd7
JM
1682 break;
1683
1684 case COMPLEX_CST:
1685 TRY (TREE_REALPART (t));
1686 TRY (TREE_IMAGPART (t));
1687 break;
1688
1689 case CONSTRUCTOR:
1690 TRY (CONSTRUCTOR_ELTS (t));
1691 break;
1692
1693 case TEMPLATE_TEMPLATE_PARM:
1694 case TEMPLATE_PARM_INDEX:
1695 case TEMPLATE_TYPE_PARM:
1696 break;
1697
1698 case BIND_EXPR:
558475f0 1699 case STMT_EXPR:
50a6dbd7
JM
1700 break;
1701
1702 case REAL_TYPE:
1703 case COMPLEX_TYPE:
1704 case VOID_TYPE:
1705 case BOOLEAN_TYPE:
1706 case TYPENAME_TYPE:
1707 case UNION_TYPE:
1708 case ENUMERAL_TYPE:
b894fc05 1709 case TYPEOF_TYPE:
50a6dbd7
JM
1710 break;
1711
1712 case POINTER_TYPE:
1713 case REFERENCE_TYPE:
1714 TRY (TREE_TYPE (t));
1715 break;
1716
1717 case FUNCTION_TYPE:
1718 case METHOD_TYPE:
1719 TRY (TREE_TYPE (t));
1720 TRY (TYPE_ARG_TYPES (t));
1721 break;
1722
1723 case ARRAY_TYPE:
1724 TRY (TREE_TYPE (t));
1725 TRY (TYPE_DOMAIN (t));
1726 break;
1727
1728 case INTEGER_TYPE:
1729 TRY (TYPE_MAX_VALUE (t));
1730 break;
1731
1732 case OFFSET_TYPE:
1733 TRY (TREE_TYPE (t));
1734 TRY (TYPE_OFFSET_BASETYPE (t));
1735 break;
1736
1737 case RECORD_TYPE:
1738 if (TYPE_PTRMEMFUNC_P (t))
1739 TRY (TYPE_PTRMEMFUNC_FN_TYPE (t));
1740 break;
1741
50a6dbd7 1742 default:
2adeacc9 1743 my_friendly_abort (19990803);
50a6dbd7
JM
1744 }
1745
1746 return NULL_TREE;
1747
1748#undef TRY
1749}
1750
1751/* Passed to search_tree. Checks for the use of types with no linkage. */
1752
1753static tree
1754no_linkage_helper (t)
1755 tree t;
1756{
1757 if (TYPE_P (t)
1758 && (IS_AGGR_TYPE (t) || TREE_CODE (t) == ENUMERAL_TYPE)
1759 && (decl_function_context (TYPE_MAIN_DECL (t))
1760 || ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))))
1761 return t;
1762 return NULL_TREE;
1763}
1764
1765/* Check if the type T depends on a type with no linkage and if so, return
1766 it. */
1767
1768tree
1769no_linkage_check (t)
1770 tree t;
1771{
2adeacc9
MM
1772 /* There's no point in checking linkage on template functions; we
1773 can't know their complete types. */
1774 if (processing_template_decl)
1775 return NULL_TREE;
1776
50a6dbd7
JM
1777 t = search_tree (t, no_linkage_helper);
1778 if (t != error_mark_node)
1779 return t;
1780 return NULL_TREE;
1781}
1782
1783
88b117b7
MM
1784/* Make copies of all the nodes below T. If FUNC is non-NULL, call it
1785 for each node. */
878cd289
MS
1786
1787tree
1788mapcar (t, func)
8d08fdba 1789 tree t;
49c249e1 1790 tree (*func) PROTO((tree));
8d08fdba 1791{
878cd289 1792 tree tmp;
558475f0 1793 enum tree_code code;
8d08fdba 1794
878cd289 1795 if (t == NULL_TREE)
8d08fdba
MS
1796 return t;
1797
88b117b7
MM
1798 if (func)
1799 {
1800 tmp = func (t);
1801 if (tmp)
1802 return tmp;
1803 }
878cd289 1804
558475f0
MM
1805 /* Handle some common cases up front. */
1806 code = TREE_CODE (t);
1807 if (TREE_CODE_CLASS (code) == '1')
1808 {
1809 t = copy_node (t);
1810 TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
1811 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1812 return t;
1813 }
1814 else if (TREE_CODE_CLASS (code) == '2' || TREE_CODE_CLASS (code) == '<')
1815 {
1816 t = copy_node (t);
1817 TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
1818 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1819 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
1820 return t;
1821 }
1822
5566b478 1823 switch (TREE_CODE (t))
8d08fdba
MS
1824 {
1825 case ERROR_MARK:
1826 return error_mark_node;
1827
1828 case VAR_DECL:
1829 case FUNCTION_DECL:
1830 case CONST_DECL:
75650646
MM
1831 /* Rather than aborting, return error_mark_node. This allows us
1832 to report a sensible error message on code like this:
1833
ae16ec5f
MM
1834 void g() { int i; f<i>(7); }
1835
1836 In a case like:
1837
1838 void g() { const int i = 7; f<i>(7); }
1839
1840 however, we must actually return the constant initializer. */
2b0a63a3
MM
1841 if (TREE_READONLY_DECL_P (t))
1842 {
1843 tmp = decl_constant_value (t);
1844 if (tmp != t)
1845 return mapcar (tmp, func);
1846 }
1847 return error_mark_node;
8d08fdba
MS
1848
1849 case PARM_DECL:
1850 {
1851 tree chain = TREE_CHAIN (t);
1852 t = copy_node (t);
878cd289
MS
1853 TREE_CHAIN (t) = mapcar (chain, func);
1854 TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
1855 DECL_INITIAL (t) = mapcar (DECL_INITIAL (t), func);
1856 DECL_SIZE (t) = mapcar (DECL_SIZE (t), func);
8d08fdba
MS
1857 return t;
1858 }
1859
1860 case TREE_LIST:
1861 {
1862 tree chain = TREE_CHAIN (t);
1863 t = copy_node (t);
878cd289
MS
1864 TREE_PURPOSE (t) = mapcar (TREE_PURPOSE (t), func);
1865 TREE_VALUE (t) = mapcar (TREE_VALUE (t), func);
1866 TREE_CHAIN (t) = mapcar (chain, func);
8d08fdba
MS
1867 return t;
1868 }
1869
42c7b807
JM
1870 case OVERLOAD:
1871 {
1872 tree chain = OVL_CHAIN (t);
1873 t = copy_node (t);
1874 OVL_FUNCTION (t) = mapcar (OVL_FUNCTION (t), func);
1875 OVL_CHAIN (t) = mapcar (chain, func);
1876 return t;
1877 }
1878
8d08fdba
MS
1879 case TREE_VEC:
1880 {
1881 int len = TREE_VEC_LENGTH (t);
1882
1883 t = copy_node (t);
1884 while (len--)
878cd289 1885 TREE_VEC_ELT (t, len) = mapcar (TREE_VEC_ELT (t, len), func);
8d08fdba
MS
1886 return t;
1887 }
1888
1889 case INTEGER_CST:
1890 case REAL_CST:
1891 case STRING_CST:
1892 return copy_node (t);
1893
61a127b3
MM
1894 case PTRMEM_CST:
1895 t = copy_node (t);
1896 TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
1897 PTRMEM_CST_MEMBER (t) = mapcar (PTRMEM_CST_MEMBER (t), func);
1898 return t;
1899
8d08fdba
MS
1900 case COND_EXPR:
1901 case TARGET_EXPR:
02531345 1902 case AGGR_INIT_EXPR:
8d08fdba 1903 t = copy_node (t);
878cd289
MS
1904 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1905 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
1906 TREE_OPERAND (t, 2) = mapcar (TREE_OPERAND (t, 2), func);
8d08fdba
MS
1907 return t;
1908
88049523
RH
1909 case TRUTH_AND_EXPR:
1910 case TRUTH_OR_EXPR:
1911 case TRUTH_XOR_EXPR:
8d08fdba
MS
1912 case TRUTH_ANDIF_EXPR:
1913 case TRUTH_ORIF_EXPR:
8d08fdba
MS
1914 case PREDECREMENT_EXPR:
1915 case PREINCREMENT_EXPR:
1916 case POSTDECREMENT_EXPR:
1917 case POSTINCREMENT_EXPR:
5566b478
MS
1918 case ARRAY_REF:
1919 case SCOPE_REF:
6748b643
JM
1920 case TRY_CATCH_EXPR:
1921 case WITH_CLEANUP_EXPR:
558475f0
MM
1922 case COMPOUND_EXPR:
1923 case MODIFY_EXPR:
f1dedc31 1924 case INIT_EXPR:
8d08fdba 1925 t = copy_node (t);
878cd289
MS
1926 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1927 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
8d08fdba
MS
1928 return t;
1929
7834ab39
MS
1930 case CALL_EXPR:
1931 t = copy_node (t);
1932 TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
1933 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1934 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
43f887f9 1935 TREE_OPERAND (t, 2) = NULL_TREE;
7834ab39
MS
1936 return t;
1937
558475f0 1938 case SAVE_EXPR:
8d08fdba
MS
1939 case ADDR_EXPR:
1940 case INDIRECT_REF:
8d08fdba 1941 case TRUTH_NOT_EXPR:
8d08fdba 1942 case COMPONENT_REF:
6748b643 1943 case CLEANUP_POINT_EXPR:
558475f0
MM
1944 case THROW_EXPR:
1945 case STMT_EXPR:
8d08fdba 1946 t = copy_node (t);
94e098d1 1947 TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
878cd289 1948 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
8d08fdba
MS
1949 return t;
1950
00595019 1951 case POINTER_TYPE:
e76a2646 1952 tmp = build_pointer_type (mapcar (TREE_TYPE (t), func));
91063b51 1953 return cp_build_qualified_type (tmp, TYPE_QUALS (t));
00595019 1954 case REFERENCE_TYPE:
e76a2646 1955 tmp = build_reference_type (mapcar (TREE_TYPE (t), func));
91063b51 1956 return cp_build_qualified_type (tmp, TYPE_QUALS (t));
00595019 1957 case FUNCTION_TYPE:
e76a2646
MS
1958 tmp = build_function_type (mapcar (TREE_TYPE (t), func),
1959 mapcar (TYPE_ARG_TYPES (t), func));
91063b51 1960 return cp_build_qualified_type (tmp, TYPE_QUALS (t));
00595019 1961 case ARRAY_TYPE:
5156628f
MS
1962 tmp = build_cplus_array_type (mapcar (TREE_TYPE (t), func),
1963 mapcar (TYPE_DOMAIN (t), func));
91063b51 1964 return cp_build_qualified_type (tmp, CP_TYPE_QUALS (t));
b7484fbe 1965 case INTEGER_TYPE:
e76a2646 1966 tmp = build_index_type (mapcar (TYPE_MAX_VALUE (t), func));
91063b51 1967 return cp_build_qualified_type (tmp, TYPE_QUALS (t));
00595019 1968 case OFFSET_TYPE:
e76a2646
MS
1969 tmp = build_offset_type (mapcar (TYPE_OFFSET_BASETYPE (t), func),
1970 mapcar (TREE_TYPE (t), func));
91063b51 1971 return cp_build_qualified_type (tmp, TYPE_QUALS (t));
00595019 1972 case METHOD_TYPE:
e76a2646
MS
1973 tmp = build_cplus_method_type
1974 (mapcar (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))), func),
1975 mapcar (TREE_TYPE (t), func),
1976 mapcar (TREE_CHAIN (TYPE_ARG_TYPES (t)), func));
91063b51 1977 return cp_build_qualified_type (tmp, TYPE_QUALS (t));
b7484fbe 1978
37c46b43
MS
1979 case COMPLEX_CST:
1980 t = copy_node (t);
1981 TREE_REALPART (t) = mapcar (TREE_REALPART (t), func);
1982 TREE_IMAGPART (t) = mapcar (TREE_REALPART (t), func);
1983 return t;
1984
5156628f
MS
1985 case CONSTRUCTOR:
1986 t = copy_node (t);
1987 CONSTRUCTOR_ELTS (t) = mapcar (CONSTRUCTOR_ELTS (t), func);
1988 return t;
1989
73b0fce8
KL
1990 case TEMPLATE_TEMPLATE_PARM:
1991 return copy_template_template_parm (t);
1992
67da3287
MM
1993 case BIND_EXPR:
1994 t = copy_node (t);
1995 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
1996 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
1997 TREE_OPERAND (t, 2) = NULL_TREE;
1998 return t;
1999
285baa06
JM
2000 case NEW_EXPR:
2001 t = copy_node (t);
2002 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
2003 TREE_OPERAND (t, 1) = mapcar (TREE_OPERAND (t, 1), func);
2004 TREE_OPERAND (t, 2) = mapcar (TREE_OPERAND (t, 2), func);
2005 return t;
2006
672476cb 2007 case LOOKUP_EXPR:
22e9174f
MM
2008 case EXIT_EXPR:
2009 case LOOP_EXPR:
672476cb
MM
2010 t = copy_node (t);
2011 TREE_OPERAND (t, 0) = mapcar (TREE_OPERAND (t, 0), func);
2012 return t;
2013
558475f0
MM
2014 case RTL_EXPR:
2015 t = copy_node (t);
2016 TREE_TYPE (t) = mapcar (TREE_TYPE (t), func);
2017 return t;
2018
00595019
MS
2019 case RECORD_TYPE:
2020 if (TYPE_PTRMEMFUNC_P (t))
2021 return build_ptrmemfunc_type
878cd289 2022 (mapcar (TYPE_PTRMEMFUNC_FN_TYPE (t), func));
00595019 2023 /* else fall through */
8d08fdba 2024
558475f0
MM
2025 default:
2026 my_friendly_abort (19990815);
8d08fdba
MS
2027 }
2028 my_friendly_abort (107);
2029 /* NOTREACHED */
2030 return NULL_TREE;
2031}
2032
88b117b7
MM
2033/* Returns T if T is allocated on the permanent obstack, NULL_TREE
2034 otherwise. */
2035
2036tree
2037permanent_p (t)
2038 tree t;
2039{
2040 return TREE_PERMANENT (t) ? t : NULL_TREE;
2041}
2042
878cd289
MS
2043static tree
2044perm_manip (t)
2045 tree t;
2046{
2047 if (TREE_PERMANENT (t))
2048 return t;
73c9f270 2049
ec255269
MS
2050 /* Support `void f () { extern int i; A<&i> a; }' */
2051 if ((TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == FUNCTION_DECL)
2052 && TREE_PUBLIC (t))
73c9f270
JM
2053 {
2054 t = copy_node (t);
2055
2056 /* copy_rtx won't make a new SYMBOL_REF, so call make_decl_rtl again. */
2057 DECL_RTL (t) = 0;
2058 make_decl_rtl (t, NULL_PTR, 1);
2059
2060 return t;
2061 }
878cd289
MS
2062 return NULL_TREE;
2063}
2064
8d08fdba
MS
2065/* Assuming T is a node built bottom-up, make it all exist on
2066 permanent obstack, if it is not permanent already. */
e92cc029 2067
8d08fdba
MS
2068tree
2069copy_to_permanent (t)
2070 tree t;
2071{
8d08fdba
MS
2072 if (t == NULL_TREE || TREE_PERMANENT (t))
2073 return t;
2074
9188c363 2075 push_permanent_obstack ();
878cd289 2076 t = mapcar (t, perm_manip);
73c9f270 2077 pop_obstacks ();
8d08fdba
MS
2078
2079 return t;
2080}
2081
5566b478
MS
2082#ifdef GATHER_STATISTICS
2083extern int depth_reached;
2084#endif
2085
8d08fdba
MS
2086void
2087print_lang_statistics ()
2088{
e66d884e 2089 extern struct obstack decl_obstack;
8d08fdba 2090 print_obstack_statistics ("class_obstack", &class_obstack);
5566b478 2091 print_obstack_statistics ("decl_obstack", &decl_obstack);
8d08fdba
MS
2092 print_search_statistics ();
2093 print_class_statistics ();
5566b478
MS
2094#ifdef GATHER_STATISTICS
2095 fprintf (stderr, "maximum template instantiation depth reached: %d\n",
2096 depth_reached);
2097#endif
8d08fdba
MS
2098}
2099
2100/* This is used by the `assert' macro. It is provided in libgcc.a,
2101 which `cc' doesn't know how to link. Note that the C++ front-end
2102 no longer actually uses the `assert' macro (instead, it calls
2103 my_friendly_assert). But all of the back-end files still need this. */
e92cc029 2104
8d08fdba
MS
2105void
2106__eprintf (string, expression, line, filename)
8d08fdba
MS
2107 const char *string;
2108 const char *expression;
2109 unsigned line;
2110 const char *filename;
8d08fdba
MS
2111{
2112 fprintf (stderr, string, expression, line, filename);
2113 fflush (stderr);
2114 abort ();
2115}
2116
e92cc029
MS
2117/* Return, as an INTEGER_CST node, the number of elements for TYPE
2118 (which is an ARRAY_TYPE). This counts only elements of the top
2119 array. */
8d08fdba
MS
2120
2121tree
2122array_type_nelts_top (type)
2123 tree type;
2124{
eae89e04 2125 return fold (build (PLUS_EXPR, sizetype,
8d08fdba
MS
2126 array_type_nelts (type),
2127 integer_one_node));
2128}
2129
e92cc029
MS
2130/* Return, as an INTEGER_CST node, the number of elements for TYPE
2131 (which is an ARRAY_TYPE). This one is a recursive count of all
2132 ARRAY_TYPEs that are clumped together. */
8d08fdba
MS
2133
2134tree
2135array_type_nelts_total (type)
2136 tree type;
2137{
2138 tree sz = array_type_nelts_top (type);
2139 type = TREE_TYPE (type);
2140 while (TREE_CODE (type) == ARRAY_TYPE)
2141 {
2142 tree n = array_type_nelts_top (type);
eae89e04 2143 sz = fold (build (MULT_EXPR, sizetype, sz, n));
8d08fdba
MS
2144 type = TREE_TYPE (type);
2145 }
2146 return sz;
2147}
878cd289
MS
2148
2149static
2150tree
2151bot_manip (t)
2152 tree t;
2153{
2154 if (TREE_CODE (t) != TREE_LIST && ! TREE_SIDE_EFFECTS (t))
2155 return t;
2156 else if (TREE_CODE (t) == TARGET_EXPR)
73aad9b9 2157 {
02531345 2158 if (TREE_CODE (TREE_OPERAND (t, 1)) == AGGR_INIT_EXPR)
73aad9b9
JM
2159 {
2160 mark_used (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 1), 0), 0));
2161 return build_cplus_new
2162 (TREE_TYPE (t), break_out_target_exprs (TREE_OPERAND (t, 1)));
2163 }
2164 t = copy_node (t);
2165 TREE_OPERAND (t, 0) = build (VAR_DECL, TREE_TYPE (t));
2166 layout_decl (TREE_OPERAND (t, 0), 0);
2167 return t;
2168 }
2169 else if (TREE_CODE (t) == CALL_EXPR)
2170 mark_used (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
2171
878cd289
MS
2172 return NULL_TREE;
2173}
2174
2175/* Actually, we'll just clean out the target exprs for the moment. */
e92cc029 2176
878cd289
MS
2177tree
2178break_out_target_exprs (t)
2179 tree t;
2180{
2181 return mapcar (t, bot_manip);
2182}
f30432d7 2183
5566b478
MS
2184/* Obstack used for allocating nodes in template function and variable
2185 definitions. */
2186
5566b478
MS
2187/* Similar to `build_nt', except we build
2188 on the permanent_obstack, regardless. */
2189
2190tree
2191build_min_nt VPROTO((enum tree_code code, ...))
2192{
2193#ifndef __STDC__
2194 enum tree_code code;
2195#endif
2196 register struct obstack *ambient_obstack = expression_obstack;
2197 va_list p;
2198 register tree t;
2199 register int length;
2200 register int i;
2201
2202 VA_START (p, code);
2203
2204#ifndef __STDC__
2205 code = va_arg (p, enum tree_code);
2206#endif
2207
2208 expression_obstack = &permanent_obstack;
2209
2210 t = make_node (code);
2211 length = tree_code_length[(int) code];
2212 TREE_COMPLEXITY (t) = lineno;
2213
2214 for (i = 0; i < length; i++)
2215 {
2216 tree x = va_arg (p, tree);
2217 TREE_OPERAND (t, i) = copy_to_permanent (x);
2218 }
2219
2220 va_end (p);
2221 expression_obstack = ambient_obstack;
2222 return t;
2223}
2224
2225/* Similar to `build', except we build
2226 on the permanent_obstack, regardless. */
2227
2228tree
2229build_min VPROTO((enum tree_code code, tree tt, ...))
2230{
2231#ifndef __STDC__
2232 enum tree_code code;
2233 tree tt;
2234#endif
2235 register struct obstack *ambient_obstack = expression_obstack;
2236 va_list p;
2237 register tree t;
2238 register int length;
2239 register int i;
2240
2241 VA_START (p, tt);
2242
2243#ifndef __STDC__
2244 code = va_arg (p, enum tree_code);
2245 tt = va_arg (p, tree);
2246#endif
2247
2248 expression_obstack = &permanent_obstack;
2249
2250 t = make_node (code);
2251 length = tree_code_length[(int) code];
672476cb 2252 TREE_TYPE (t) = copy_to_permanent (tt);
5566b478
MS
2253 TREE_COMPLEXITY (t) = lineno;
2254
2255 for (i = 0; i < length; i++)
2256 {
2257 tree x = va_arg (p, tree);
2258 TREE_OPERAND (t, i) = copy_to_permanent (x);
2259 }
2260
2261 va_end (p);
2262 expression_obstack = ambient_obstack;
2263 return t;
2264}
2265
2266/* Same as `tree_cons' but make a permanent object. */
2267
2268tree
2269min_tree_cons (purpose, value, chain)
2270 tree purpose, value, chain;
2271{
2272 register tree node;
2273 register struct obstack *ambient_obstack = current_obstack;
2274 current_obstack = &permanent_obstack;
2275
fc378698
MS
2276 node = tree_cons (copy_to_permanent (purpose),
2277 copy_to_permanent (value), chain);
5566b478
MS
2278 current_obstack = ambient_obstack;
2279 return node;
2280}
2281
2282tree
2283get_type_decl (t)
2284 tree t;
2285{
5566b478
MS
2286 if (TREE_CODE (t) == TYPE_DECL)
2287 return t;
2288 if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
2289 return TYPE_STUB_DECL (t);
2290
2291 my_friendly_abort (42);
4e1e2064
MH
2292
2293 /* Stop compiler from complaining control reaches end of non-void function. */
2294 return 0;
5566b478
MS
2295}
2296
2297int
2298can_free (obstack, t)
2299 struct obstack *obstack;
2300 tree t;
2301{
a703fb38 2302 int size = 0;
5566b478
MS
2303
2304 if (TREE_CODE (t) == TREE_VEC)
2305 size = (TREE_VEC_LENGTH (t)-1) * sizeof (tree) + sizeof (struct tree_vec);
2306 else
2307 my_friendly_abort (42);
2308
2309#define ROUND(x) ((x + obstack_alignment_mask (obstack)) \
2310 & ~ obstack_alignment_mask (obstack))
2311 if ((char *)t + ROUND (size) == obstack_next_free (obstack))
2312 return 1;
2313#undef ROUND
2314
2315 return 0;
2316}
2317
2318/* Return first vector element whose BINFO_TYPE is ELEM.
934c6b13 2319 Return 0 if ELEM is not in VEC. VEC may be NULL_TREE. */
5566b478
MS
2320
2321tree
2322vec_binfo_member (elem, vec)
2323 tree elem, vec;
2324{
2325 int i;
934c6b13
MS
2326
2327 if (vec)
2328 for (i = 0; i < TREE_VEC_LENGTH (vec); ++i)
3bfdc719 2329 if (same_type_p (elem, BINFO_TYPE (TREE_VEC_ELT (vec, i))))
934c6b13
MS
2330 return TREE_VEC_ELT (vec, i);
2331
5566b478
MS
2332 return NULL_TREE;
2333}
e76a2646
MS
2334
2335/* Kludge around the fact that DECL_CONTEXT for virtual functions returns
2336 the wrong thing for decl_function_context. Hopefully the uses in the
2337 backend won't matter, since we don't need a static chain for local class
2338 methods. FIXME! */
2339
2340tree
2341hack_decl_function_context (decl)
2342 tree decl;
2343{
2344 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl))
2345 return decl_function_context (TYPE_MAIN_DECL (DECL_CLASS_CONTEXT (decl)));
2346 return decl_function_context (decl);
2347}
67d743fe 2348
700466c2
JM
2349/* Returns the namespace that contains DECL, whether directly or
2350 indirectly. */
2351
2352tree
2353decl_namespace_context (decl)
2354 tree decl;
2355{
2356 while (1)
2357 {
2358 if (TREE_CODE (decl) == NAMESPACE_DECL)
2359 return decl;
2360 else if (TYPE_P (decl))
2361 decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
2362 else
2363 decl = CP_DECL_CONTEXT (decl);
2364 }
2365}
2366
67d743fe
MS
2367/* Return truthvalue of whether T1 is the same tree structure as T2.
2368 Return 1 if they are the same.
2369 Return 0 if they are understandably different.
2370 Return -1 if either contains tree structure not understood by
2371 this function. */
2372
2373int
2374cp_tree_equal (t1, t2)
2375 tree t1, t2;
2376{
2377 register enum tree_code code1, code2;
2378 int cmp;
2379
2380 if (t1 == t2)
2381 return 1;
2382 if (t1 == 0 || t2 == 0)
2383 return 0;
2384
2385 code1 = TREE_CODE (t1);
2386 code2 = TREE_CODE (t2);
2387
2388 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
a703fb38
KG
2389 {
2390 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
2391 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2392 else
2393 return cp_tree_equal (TREE_OPERAND (t1, 0), t2);
2394 }
67d743fe
MS
2395 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
2396 || code2 == NON_LVALUE_EXPR)
2397 return cp_tree_equal (t1, TREE_OPERAND (t2, 0));
2398
2399 if (code1 != code2)
2400 return 0;
2401
2402 switch (code1)
2403 {
2404 case INTEGER_CST:
2405 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2406 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2407
2408 case REAL_CST:
2409 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2410
2411 case STRING_CST:
2412 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2413 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2414 TREE_STRING_LENGTH (t1));
2415
2416 case CONSTRUCTOR:
7dd4bdf5
MM
2417 /* We need to do this when determining whether or not two
2418 non-type pointer to member function template arguments
2419 are the same. */
3bfdc719 2420 if (!(same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
7dd4bdf5
MM
2421 /* The first operand is RTL. */
2422 && TREE_OPERAND (t1, 0) == TREE_OPERAND (t2, 0)))
2423 return 0;
2424 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2425
2426 case TREE_LIST:
2427 cmp = cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
2428 if (cmp <= 0)
2429 return cmp;
2430 cmp = cp_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2));
2431 if (cmp <= 0)
2432 return cmp;
2433 return cp_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
67d743fe
MS
2434
2435 case SAVE_EXPR:
2436 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2437
2438 case CALL_EXPR:
2439 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2440 if (cmp <= 0)
2441 return cmp;
2442 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2443
2444 case TARGET_EXPR:
2445 /* Special case: if either target is an unallocated VAR_DECL,
2446 it means that it's going to be unified with whatever the
2447 TARGET_EXPR is really supposed to initialize, so treat it
2448 as being equivalent to anything. */
2449 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
2450 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
2451 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
2452 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
2453 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
2454 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
2455 cmp = 1;
2456 else
2457 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2458 if (cmp <= 0)
2459 return cmp;
2460 return cp_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2461
2462 case WITH_CLEANUP_EXPR:
2463 cmp = cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2464 if (cmp <= 0)
2465 return cmp;
2466 return cp_tree_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
2467
2468 case COMPONENT_REF:
2469 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
2470 return cp_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2471 return 0;
2472
2473 case VAR_DECL:
2474 case PARM_DECL:
2475 case CONST_DECL:
2476 case FUNCTION_DECL:
2477 return 0;
2478
f84b4be9
JM
2479 case TEMPLATE_PARM_INDEX:
2480 return TEMPLATE_PARM_IDX (t1) == TEMPLATE_PARM_IDX (t2)
2481 && TEMPLATE_PARM_LEVEL (t1) == TEMPLATE_PARM_LEVEL (t2);
67d743fe
MS
2482
2483 case SIZEOF_EXPR:
abff8e06 2484 case ALIGNOF_EXPR:
67d743fe
MS
2485 if (TREE_CODE (TREE_OPERAND (t1, 0)) != TREE_CODE (TREE_OPERAND (t2, 0)))
2486 return 0;
2487 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (t1, 0))) == 't')
3bfdc719 2488 return same_type_p (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
67d743fe 2489 break;
7f85441b 2490
61a127b3
MM
2491 case PTRMEM_CST:
2492 /* Two pointer-to-members are the same if they point to the same
2493 field or function in the same class. */
2494 return (PTRMEM_CST_MEMBER (t1) == PTRMEM_CST_MEMBER (t2)
3bfdc719 2495 && same_type_p (PTRMEM_CST_CLASS (t1), PTRMEM_CST_CLASS (t2)));
61a127b3 2496
7f85441b
KG
2497 default:
2498 break;
67d743fe
MS
2499 }
2500
2501 switch (TREE_CODE_CLASS (code1))
2502 {
2503 int i;
2504 case '1':
2505 case '2':
2506 case '<':
2507 case 'e':
2508 case 'r':
2509 case 's':
2510 cmp = 1;
2511 for (i=0; i<tree_code_length[(int) code1]; ++i)
2512 {
2513 cmp = cp_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2514 if (cmp <= 0)
2515 return cmp;
2516 }
2517 return cmp;
2518 }
2519
2520 return -1;
2521}
73aad9b9 2522
7ddedda4
MM
2523/* Similar to make_tree_vec, but build on the momentary_obstack.
2524 Thus, these vectors are really and truly temporary. */
73aad9b9
JM
2525
2526tree
2527make_temp_vec (len)
2528 int len;
2529{
2530 register tree node;
7ddedda4 2531 push_expression_obstack ();
73aad9b9 2532 node = make_tree_vec (len);
7ddedda4 2533 pop_obstacks ();
73aad9b9
JM
2534 return node;
2535}
d11ad92e 2536
5ffe581d
JM
2537/* Build a wrapper around some pointer PTR so we can use it as a tree. */
2538
2539tree
2540build_ptr_wrapper (ptr)
2541 void *ptr;
2542{
2543 tree t = make_node (WRAPPER);
2544 WRAPPER_PTR (t) = ptr;
2545 return t;
2546}
2547
2548/* Same, but on the expression_obstack. */
2549
2550tree
2551build_expr_ptr_wrapper (ptr)
2552 void *ptr;
2553{
2554 tree t;
2555 push_expression_obstack ();
2556 t = build_ptr_wrapper (ptr);
2557 pop_obstacks ();
2558 return t;
2559}
2560
2561/* Build a wrapper around some integer I so we can use it as a tree. */
2562
2563tree
2564build_int_wrapper (i)
2565 int i;
2566{
2567 tree t = make_node (WRAPPER);
2568 WRAPPER_INT (t) = i;
2569 return t;
2570}
2571
d8e178a0 2572static tree
1139b3d8
JM
2573build_srcloc (file, line)
2574 char *file;
2575 int line;
2576{
a48ebb56
BK
2577 tree t;
2578
2579 /* Make sure that we put these on the permanent obstack; up in
2580 add_pending_template, we pass this return value into perm_tree_cons,
2581 which also puts it on the permanent_obstack. However, this wasn't
2582 explicitly doing the same. */
2583 register struct obstack *ambient_obstack = current_obstack;
2584 current_obstack = &permanent_obstack;
2585
2586 t = make_node (SRCLOC);
1139b3d8
JM
2587 SRCLOC_FILE (t) = file;
2588 SRCLOC_LINE (t) = line;
a48ebb56
BK
2589
2590 current_obstack = ambient_obstack;
2591
1139b3d8
JM
2592 return t;
2593}
2594
2595tree
2596build_srcloc_here ()
2597{
2598 return build_srcloc (input_filename, lineno);
2599}
2600
e66d884e
JM
2601void
2602push_expression_obstack ()
2603{
2604 push_obstacks_nochange ();
2605 current_obstack = expression_obstack;
2606}
2607
9188c363
MM
2608/* Begin allocating on the permanent obstack. When you're done
2609 allocating there, call pop_obstacks to return to the previous set
2610 of obstacks. */
2611
2612void
2613push_permanent_obstack ()
2614{
2615 push_obstacks_nochange ();
2616 end_temporary_allocation ();
2617}
2618
d11ad92e
MS
2619/* The type of ARG when used as an lvalue. */
2620
2621tree
2622lvalue_type (arg)
2623 tree arg;
2624{
2c73f9f5
ML
2625 tree type = TREE_TYPE (arg);
2626 if (TREE_CODE (arg) == OVERLOAD)
2627 type = unknown_type_node;
8cd4c175 2628 return type;
d11ad92e
MS
2629}
2630
2631/* The type of ARG for printing error messages; denote lvalues with
2632 reference types. */
2633
2634tree
2635error_type (arg)
2636 tree arg;
2637{
2638 tree type = TREE_TYPE (arg);
2639 if (TREE_CODE (type) == ARRAY_TYPE)
2640 ;
2641 else if (real_lvalue_p (arg))
2642 type = build_reference_type (lvalue_type (arg));
2643 else if (IS_AGGR_TYPE (type))
2644 type = lvalue_type (arg);
2645
2646 return type;
2647}
eb66be0e
MS
2648
2649/* Does FUNCTION use a variable-length argument list? */
2650
2651int
2652varargs_function_p (function)
2653 tree function;
2654{
2655 tree parm = TYPE_ARG_TYPES (TREE_TYPE (function));
2656 for (; parm; parm = TREE_CHAIN (parm))
2657 if (TREE_VALUE (parm) == void_type_node)
2658 return 0;
2659 return 1;
2660}
f94ae2f5
JM
2661
2662/* Returns 1 if decl is a member of a class. */
2663
2664int
2665member_p (decl)
2666 tree decl;
2667{
2668 tree ctx = DECL_CONTEXT (decl);
2669 return (ctx && TREE_CODE_CLASS (TREE_CODE (ctx)) == 't');
2670}
51924768
JM
2671
2672/* Create a placeholder for member access where we don't actually have an
2673 object that the access is against. */
2674
2675tree
2676build_dummy_object (type)
2677 tree type;
2678{
44689c12 2679 tree decl = build1 (NOP_EXPR, build_pointer_type (type), void_zero_node);
51924768
JM
2680 return build_indirect_ref (decl, NULL_PTR);
2681}
2682
2683/* We've gotten a reference to a member of TYPE. Return *this if appropriate,
2684 or a dummy object otherwise. If BINFOP is non-0, it is filled with the
2685 binfo path from current_class_type to TYPE, or 0. */
2686
2687tree
2688maybe_dummy_object (type, binfop)
2689 tree type;
2690 tree *binfop;
2691{
2692 tree decl, context;
2693
2694 if (current_class_type
2695 && get_base_distance (type, current_class_type, 0, binfop) != -1)
2696 context = current_class_type;
2697 else
2698 {
2699 /* Reference from a nested class member function. */
2700 context = type;
2701 if (binfop)
2702 *binfop = TYPE_BINFO (type);
2703 }
2704
2705 if (current_class_ref && context == current_class_type)
2706 decl = current_class_ref;
2707 else
2708 decl = build_dummy_object (context);
2709
2710 return decl;
2711}
2712
2713/* Returns 1 if OB is a placeholder object, or a pointer to one. */
2714
2715int
2716is_dummy_object (ob)
2717 tree ob;
2718{
2719 if (TREE_CODE (ob) == INDIRECT_REF)
2720 ob = TREE_OPERAND (ob, 0);
2721 return (TREE_CODE (ob) == NOP_EXPR
44689c12 2722 && TREE_OPERAND (ob, 0) == void_zero_node);
51924768 2723}
5524676d
JM
2724
2725/* Returns 1 iff type T is a POD type, as defined in [basic.types]. */
2726
2727int
2728pod_type_p (t)
2729 tree t;
2730{
5524676d
JM
2731 while (TREE_CODE (t) == ARRAY_TYPE)
2732 t = TREE_TYPE (t);
2733
52fb2769
NS
2734 if (INTEGRAL_TYPE_P (t))
2735 return 1; /* integral, character or enumeral type */
2736 if (FLOAT_TYPE_P (t))
5524676d 2737 return 1;
52fb2769
NS
2738 if (TYPE_PTR_P (t))
2739 return 1; /* pointer to non-member */
2740 if (TYPE_PTRMEM_P (t))
2741 return 1; /* pointer to member object */
2742 if (TYPE_PTRMEMFUNC_P (t))
2743 return 1; /* pointer to member function */
2744
2745 if (! CLASS_TYPE_P (t))
2746 return 0; /* other non-class type (reference or function) */
2747 if (CLASSTYPE_NON_POD_P (t))
5524676d 2748 return 0;
5524676d
JM
2749 return 1;
2750}
e5dc5fb2 2751
e5dc5fb2
JM
2752/* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid C++-specific
2753 attribute for either declaration DECL or type TYPE and 0 otherwise.
2754 Plugged into valid_lang_attribute. */
2755
2756int
2757cp_valid_lang_attribute (attr_name, attr_args, decl, type)
2758 tree attr_name;
2759 tree attr_args ATTRIBUTE_UNUSED;
2760 tree decl ATTRIBUTE_UNUSED;
2761 tree type ATTRIBUTE_UNUSED;
2762{
9db83085 2763 if (is_attribute_p ("com_interface", attr_name))
e5dc5fb2
JM
2764 {
2765 if (! flag_vtable_thunks)
2766 {
2767 error ("`com_interface' only supported with -fvtable-thunks");
2768 return 0;
2769 }
2770
2771 if (attr_args != NULL_TREE
2772 || decl != NULL_TREE
2773 || ! CLASS_TYPE_P (type)
2774 || type != TYPE_MAIN_VARIANT (type))
2775 {
2776 warning ("`com_interface' attribute can only be applied to class definitions");
2777 return 0;
2778 }
2779
2780 CLASSTYPE_COM_INTERFACE (type) = 1;
2781 return 1;
2782 }
9db83085 2783 else if (is_attribute_p ("init_priority", attr_name))
e5dc5fb2
JM
2784 {
2785 tree initp_expr = (attr_args ? TREE_VALUE (attr_args): NULL_TREE);
2786 int pri;
2787
2788 if (initp_expr)
2789 STRIP_NOPS (initp_expr);
2790
2791 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
2792 {
2793 error ("requested init_priority is not an integer constant");
2794 return 0;
2795 }
2796
2797 pri = TREE_INT_CST_LOW (initp_expr);
2798
2799 while (TREE_CODE (type) == ARRAY_TYPE)
2800 type = TREE_TYPE (type);
2801
2802 if (decl == NULL_TREE
2803 || TREE_CODE (decl) != VAR_DECL
2804 || ! TREE_STATIC (decl)
2805 || DECL_EXTERNAL (decl)
2806 || (TREE_CODE (type) != RECORD_TYPE
2807 && TREE_CODE (type) != UNION_TYPE)
2808 /* Static objects in functions are initialized the
2809 first time control passes through that
2810 function. This is not precise enough to pin down an
2811 init_priority value, so don't allow it. */
2812 || current_function_decl)
2813 {
2814 error ("can only use init_priority attribute on file-scope definitions of objects of class type");
2815 return 0;
2816 }
2817
2818 if (pri > MAX_INIT_PRIORITY || pri <= 0)
2819 {
2820 error ("requested init_priority is out of range");
2821 return 0;
2822 }
2823
2824 /* Check for init_priorities that are reserved for
2825 language and runtime support implementations.*/
2826 if (pri <= MAX_RESERVED_INIT_PRIORITY)
2827 {
2828 warning
2829 ("requested init_priority is reserved for internal use");
2830 }
2831
0aafb128 2832 DECL_INIT_PRIORITY (decl) = pri;
e5dc5fb2
JM
2833 return 1;
2834 }
2835
2836 return 0;
2837}
87533b37
MM
2838
2839/* Return a new PTRMEM_CST of the indicated TYPE. The MEMBER is the
2840 thing pointed to by the constant. */
2841
2842tree
2843make_ptrmem_cst (type, member)
2844 tree type;
2845 tree member;
2846{
2847 tree ptrmem_cst = make_node (PTRMEM_CST);
2848 /* If would seem a great convenience if make_node would set
2849 TREE_CONSTANT for things of class `c', but it does not. */
2850 TREE_CONSTANT (ptrmem_cst) = 1;
2851 TREE_TYPE (ptrmem_cst) = type;
2852 PTRMEM_CST_MEMBER (ptrmem_cst) = member;
2853 return ptrmem_cst;
2854}
2855
0a818f84
GRK
2856/* Initialize unsave for C++. */
2857void
2858init_cplus_unsave ()
2859{
2860 lang_unsave_expr_now = cplus_unsave_expr_now;
2861}
2862
2863/* The C++ version of unsave_expr_now.
2864 See gcc/tree.c:unsave_expr_now for comments. */
2865
2866tree
2867cplus_unsave_expr_now (expr)
2868 tree expr;
2869{
2870 if (expr == NULL)
2871 return expr;
2872
2873 else if (TREE_CODE (expr) == AGGR_INIT_EXPR)
2874 {
2875 unsave_expr_now (TREE_OPERAND (expr,0));
2876 if (TREE_OPERAND (expr, 1)
2877 && TREE_CODE (TREE_OPERAND (expr, 1)) == TREE_LIST)
2878 {
2879 tree exp = TREE_OPERAND (expr, 1);
2880 while (exp)
2881 {
2882 unsave_expr_now (TREE_VALUE (exp));
2883 exp = TREE_CHAIN (exp);
2884 }
2885 }
2886 unsave_expr_now (TREE_OPERAND (expr,2));
2887 return expr;
2888 }
2889
2890 else
2891 return expr;
2892}
2893