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