No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / usr.bin / g++ / cc1plus / cplus-class.c
blob9113820700ff73c112f7496b7ea23bc0896f139b
1 /* Functions related to building and playing with classes.
2 Copyright (C) 1987 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@mcc.com)
5 This file is part of GNU CC.
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 1, or (at your option)
10 any later version.
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.
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "tree.h"
26 #include "cplus-tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "assert.h"
30 #include <stdio.h>
32 #include "obstack.h"
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
36 extern int xmalloc ();
37 extern void free ();
39 #define NULL 0
40 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
41 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
43 /* See cplus-decl.c for comment of this variable. */
44 extern int flag_int_enum_equivalence;
46 /* some statistics gathering help. */
47 static int n_vtables, n_vtable_entries, n_vtable_searches, n_vtable_elems;
48 static int n_convert_harshness, n_compute_conversion_costs, n_build_method_call;
49 static int n_inner_fields_searched;
51 /* Compute the ease with which a conversion can be performed
52 between an expected and the given type. */
53 static int convert_harshness ();
55 /* in decl.c. */
56 extern tree lookup_tag_current_binding_level ();
58 /* in method.c. */
59 extern void do_inline_function_hair ();
61 /* Way of stacking class types. */
62 static tree *current_class_base, *current_class_stack;
63 static int current_class_stacksize;
65 struct class_level
67 /* The previous class level. */
68 struct class_level *level_chain;
70 /* The class instance variable, as a PARM_DECL. */
71 tree decl;
72 /* The class instance variable, as an object. */
73 tree object;
74 /* The virtual function table pointer
75 for the class instance variable. */
76 tree vtable_decl;
78 /* Name of the current class. */
79 tree name;
80 /* Type of the current class. */
81 tree type;
83 /* Flags for this class level. */
84 int this_is_variable;
85 int memoized_lookups;
86 int save_memoized;
87 int unused;
90 tree current_class_decl, C_C_D; /* PARM_DECL: the class instance variable */
91 tree current_vtable_decl;
93 /* The following two can be derived from the previous one */
94 tree current_class_name; /* IDENTIFIER_NODE: name of current class */
95 tree current_class_type; /* _TYPE: the type of the current class */
96 tree prev_class_type; /* _TYPE: the previous type that was a class */
98 static tree get_vtable_name (), get_vfield_name ();
99 tree the_null_vtable_entry;
101 /* Way of stacking langauge names. */
102 static tree *current_lang_base, *current_lang_stack;
103 static int current_lang_stacksize;
105 /* Names of languages we recognize. */
106 tree lang_name_c, lang_name_cplusplus;
107 tree current_lang_name;
109 tree minus_one_node;
111 /* When layout out an aggregate type, the size of the
112 basetypes (virtual and non-virtual) is passed to layout_record
113 via this node. */
114 static tree base_layout_decl;
116 #if 0
117 /* Make sure that the tag NAME is defined *in the current binding level*
118 at least as a forward reference.
119 CODE says which kind of tag NAME ought to be.
121 Not used for C++. Not maintained. */
123 tree
124 start_struct (code, name)
125 enum tree_code code;
126 tree name;
128 /* If there is already a tag defined at this binding level
129 (as a forward reference), just return it. */
130 register tree ref = 0;
132 if (name != 0)
133 ref = lookup_tag (code, name, current_binding_level, 1);
134 if (ref && TREE_CODE (ref) == code)
136 if (TYPE_FIELDS (ref))
137 error ((code == UNION_TYPE ? "redefinition of `union %s'"
138 : "redefinition of `struct %s'"),
139 IDENTIFIER_POINTER (name));
141 return ref;
144 /* Otherwise create a forward-reference just so the tag is in scope. */
146 ref = make_lang_type (code);
147 /* Must re-synch this with xref_tag if you are going to use it. */
148 assert (0);
149 pushtag (name, ref);
150 return ref;
152 #endif
154 /* Virtual baseclass things. */
155 tree
156 build_vbase_pointer (exp, type)
157 tree exp, type;
159 char *name;
161 name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
162 sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
163 return build_component_ref (exp, get_identifier (name), 0, 0);
166 /* Build multi-level access to EXPR using hierarchy path PATH.
167 CODE is PLUS_EXPR if we are going with the grain,
168 and MINUS_EXPR if we are not (in which case, we cannot traverse
169 virtual baseclass links).
171 TYPE is the type we want this path to have on exit.
173 ALIAS_THIS is non-zero if EXPR in an expression involving `this'. */
174 tree
175 build_vbase_path (code, type, expr, path, alias_this)
176 enum tree_code code;
177 tree type;
178 tree expr;
179 tree path;
180 int alias_this;
182 register int changed = 0;
183 tree last = NULL_TREE, last_virtual = NULL_TREE;
184 int fixed_type_p = 0 && resolves_to_fixed_type_p (expr);
185 tree basetype;
186 tree offset = integer_zero_node;
188 if (TREE_CHAIN (path))
189 path = nreverse (copy_list (path));
191 basetype = TREE_VALUE (path);
192 while (path)
194 if (TREE_VIA_VIRTUAL (path))
196 last_virtual = TYPE_MAIN_VARIANT (TREE_VALUE (path));
197 if (code == PLUS_EXPR)
199 changed = ! fixed_type_p;
201 if (last)
202 expr = convert_pointer_to (TREE_VALUE (last), expr);
203 if (changed)
204 expr = build_vbase_pointer (build_indirect_ref (expr, 0),
205 last_virtual);
206 else
207 offset = ASSOC_OFFSET (value_member (last_virtual,
208 CLASSTYPE_VBASECLASSES (basetype)));
209 /* Happens in the case of parse errors. */
210 if (expr == error_mark_node)
211 return expr;
213 else
215 error_with_aggr_type (last_virtual, "cannot cast up from virtual baseclass `%s'");
216 return error_mark_node;
219 last = path;
220 path = TREE_CHAIN (path);
222 /* LAST is now the last basetype on the path. */
223 last = TREE_VALUE (last);
225 /* If we go through any virtual base pointers, make sure that
226 casts to BASETYPE from the last virtual base class use
227 the right value for BASETYPE. */
228 if (changed)
230 tree intype = TREE_TYPE (TREE_TYPE (expr));
231 if (TYPE_MAIN_VARIANT (intype) == TYPE_MAIN_VARIANT (last))
232 basetype = intype;
233 else
235 basetype = get_base_type (last, TYPE_MAIN_VARIANT (intype), 0);
236 offset = CLASSTYPE_OFFSET (basetype);
239 else
241 if (last_virtual && last != last_virtual)
242 basetype = get_base_type (last, last_virtual, 0);
243 else
244 basetype = last;
246 offset = genop (code, offset, CLASSTYPE_OFFSET (basetype));
247 /* 900324 JRV: If code was MINUS_EXPR, genop returned a negative
248 offset, so shouldn't the code always be PLUS_EXPR now? */
249 code = PLUS_EXPR;
252 if (TREE_INT_CST_LOW (offset))
254 /* For multiple inheritance: if `this' can be set by
255 any function, then it could be 0 on entry
256 to any function. Preserve such zeroness here.
257 Otherwise, only in the case of constructors need
258 we worry, and in those cases, it will be zero,
259 or initialized to some legal value to which we may
260 add. */
261 tree addr = TREE_CODE (expr) == ADDR_EXPR
262 ? expr : save_expr (expr);
263 expr = build (code, type, addr, offset);
265 if (alias_this == 0 || flag_this_is_variable)
266 return build (COND_EXPR, type,
267 build (EQ_EXPR, integer_type_node, addr, integer_zero_node),
268 build1 (NOP_EXPR, type, addr),
269 expr);
271 return build1 (NOP_EXPR, type, expr);
274 /* Virtual function things. */
276 /* Virtual functions to be dealt with after laying out our
277 virtual base classes (only if the type has any). */
278 static tree pending_hard_virtuals;
280 /* The names of the entries in the virtual table structure. */
281 static tree delta_name, pfn_name;
283 /* Temporary assoc list to memoize lookups of the left-most non-virtual
284 baseclass B in a lattice topped by T. B can appear multiple times
285 in the lattice.
286 TREE_PURPOSE is B's TYPE_MAIN_VARIANT.
287 TREE_VALUE is the path by which B is reached from T.
288 TREE_TYPE is B's real type.
290 If TREE_TYPE is NULL_TREE, it means that B was reached via
291 a virtual baseclass.
292 N.B.: This list consists of nodes on the temporary obstack. */
293 static tree leftmost_baseclasses;
295 /* Build an entry in the virtual function table.
296 DELTA is the offset for the `this' pointer.
297 PFN is an ADDR_EXPR containing a pointer to the virtual function.
298 Note that the index (DELTA2) in the virtual function table
299 is always 0. */
300 tree
301 build_vtable_entry (delta, pfn)
302 tree delta, pfn;
304 tree elems = tree_cons (NULL_TREE, delta,
305 tree_cons (NULL_TREE, integer_zero_node,
306 build_tree_list (NULL_TREE, pfn)));
307 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
308 TREE_LITERAL (entry) = 1;
309 TREE_STATIC (entry) = 1;
310 TREE_READONLY (entry) = 1;
312 #ifdef GATHER_STATISTICS
313 n_vtable_entries += 1;
314 #endif
316 return entry;
319 /* Given an object INSTANCE, return an expression which yields
320 the virtual function corresponding to INDEX. There are many special
321 cases for INSTANCE which we take care of here, mainly to avoid
322 creating extra tree nodes when we don't have to. */
323 tree
324 build_vfn_ref (ptr_to_instptr, instance, index)
325 tree *ptr_to_instptr, instance;
326 tree index;
328 extern int building_cleanup;
329 tree vtbl, aref;
330 tree basetype = TREE_TYPE (instance);
332 if (TREE_CODE (basetype) == REFERENCE_TYPE)
333 basetype = TREE_TYPE (basetype);
335 if (instance == C_C_D)
337 if (current_vtable_decl == NULL_TREE
338 || current_vtable_decl == error_mark_node
339 || get_base_type (DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type)), basetype, 0) == NULL_TREE)
340 vtbl = build_indirect_ref (build_vfield_ref (instance, basetype));
341 else
342 vtbl = current_vtable_decl;
344 else
346 if (optimize)
348 /* Try to figure out what a reference refers to, and
349 access its virtual function table directly. */
350 tree ref = 0;
352 if (TREE_CODE (instance) == INDIRECT_REF
353 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
354 ref = TREE_OPERAND (instance, 0);
355 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
356 ref = instance;
358 if (ref && TREE_CODE (ref) == VAR_DECL
359 && DECL_INITIAL (ref))
361 tree init = DECL_INITIAL (ref);
363 while (TREE_CODE (init) == NOP_EXPR
364 || TREE_CODE (init) == REFERENCE_EXPR)
365 init = TREE_OPERAND (init, 0);
366 if (TREE_CODE (init) == ADDR_EXPR)
368 init = TREE_OPERAND (init, 0);
369 if (IS_AGGR_TYPE (TREE_TYPE (init))
370 && (TREE_CODE (init) == PARM_DECL
371 || TREE_CODE (init) == VAR_DECL))
372 instance = init;
377 if (IS_AGGR_TYPE (instance)
378 && (TREE_CODE (instance) == RESULT_DECL
379 || TREE_CODE (instance) == PARM_DECL
380 || TREE_CODE (instance) == VAR_DECL))
381 vtbl = CLASS_ASSOC_VTABLE (basetype);
382 else
383 vtbl = build_indirect_ref (build_vfield_ref (instance, basetype), 0);
385 aref = build_array_ref (vtbl, index);
386 if (!building_cleanup && TREE_CODE (aref) == INDIRECT_REF)
387 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
389 *ptr_to_instptr = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
390 *ptr_to_instptr,
391 convert (integer_type_node, build_component_ref (aref, delta_name, 0, 0)));
392 return build_component_ref (aref, pfn_name, 0, 0);
395 /* Build a virtual function for type TYPE.
396 If ASSOC is non-NULL, build the vtable starting with the intial
397 approximation that it is the same as the one which is the head of
398 the assocation list. */
399 static tree
400 build_vtable (assoc, type)
401 tree assoc, type;
403 tree name = get_vtable_name (type);
404 tree virtuals, decl;
406 if (assoc)
408 virtuals = copy_list (ASSOC_VIRTUALS (assoc));
409 decl = build_decl (VAR_DECL, name, TREE_TYPE (ASSOC_VTABLE (assoc)));
411 else
413 virtuals = NULL_TREE;
414 decl = build_decl (VAR_DECL, name, void_type_node);
417 #ifdef GATHER_STATISTICS
418 n_vtables += 1;
419 n_vtable_elems += list_length (virtuals);
420 #endif
422 if (write_virtuals >= 2)
424 if (CLASSTYPE_INTERFACE_UNKNOWN (type) == 0)
426 TREE_PUBLIC (decl) = CLASSTYPE_VTABLE_NEEDS_WRITING (type);
427 TREE_EXTERNAL (decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (type);
430 else if (write_virtuals > 0)
431 TREE_PUBLIC (decl) = 1;
432 else if (write_virtuals < 0)
433 TREE_EXTERNAL (decl) = 1;
435 IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
436 /* Initialize the association list for this type, based
437 on our first approximation. */
438 CLASS_ASSOC_VTABLE (type) = decl;
439 CLASS_ASSOC_VIRTUALS (type) = virtuals;
441 TREE_STATIC (decl) = 1;
442 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
443 DECL_ALIGN (decl));
445 if (assoc && write_virtuals >= 0)
446 DECL_VIRTUAL_P (decl) = 1;
447 /* Remember which class this vtable is really for. */
448 DECL_VPARENT (decl) = type;
449 DECL_CONTEXT (decl) = type;
450 CLASSTYPE_MARKED3 (type) = 1;
451 CLASSTYPE_MARKED4 (type) = 1;
452 return decl;
455 /* Give TYPE a new virtual function table which is initialized
456 with a skeleton-copy of its original initialization. The only
457 entry that changes is the `delta' entry, so we can really
458 share a lot of structure.
460 FOR_TYPE is the derived type which caused this table to
461 be needed.
463 ASSOC is the type association which provided TYPE for FOR_TYPE.
465 The way we update BASE_ASSOC's vtable information is just to change the
466 association information in FOR_TYPE's association list. */
467 static void
468 prepare_fresh_vtable (assoc, base_assoc, for_type)
469 tree assoc, base_assoc, for_type;
471 tree basetype = ASSOC_TYPE (assoc);
472 tree orig_decl = ASSOC_VTABLE (assoc);
473 tree name = build_type_pathname (VTABLE_NAME_FORMAT, basetype, for_type);
474 tree new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
475 tree path;
476 int result;
478 assert (TREE_USED (assoc) == 0);
480 /* Remember which class this vtable is really for. */
481 DECL_VPARENT (new_decl) = ASSOC_TYPE (base_assoc);
482 DECL_CONTEXT (new_decl) = for_type;
484 TREE_STATIC (new_decl) = 1;
485 ASSOC_VTABLE (assoc) = pushdecl_top_level (new_decl);
486 DECL_VIRTUAL_P (new_decl) = 1;
487 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
489 /* Make fresh virtual list, so we can smash it later. */
490 assert (ASSOC_VIRTUALS (assoc));
491 ASSOC_VIRTUALS (assoc) = copy_list (ASSOC_VIRTUALS (assoc));
493 #ifdef GATHER_STATISTICS
494 n_vtables += 1;
495 n_vtable_elems += list_length (ASSOC_VIRTUALS (assoc));
496 #endif
498 /* Set `new_decl's PUBLIC and EXTERNAL bits. */
499 if (write_virtuals >= 2)
501 if (CLASSTYPE_INTERFACE_UNKNOWN (for_type) == 0)
503 TREE_PUBLIC (new_decl) = CLASSTYPE_VTABLE_NEEDS_WRITING (for_type);
504 TREE_EXTERNAL (new_decl) = ! CLASSTYPE_VTABLE_NEEDS_WRITING (for_type);
507 else if (write_virtuals > 0)
508 TREE_PUBLIC (new_decl) = 1;
509 else if (write_virtuals < 0)
510 TREE_EXTERNAL (new_decl) = 1;
512 CLASSTYPE_MARKED3 (basetype) = 1;
513 CLASSTYPE_MARKED4 (basetype) = 1;
515 /* Mark all types between FOR_TYPE and TYPE as having been
516 touched, so that if we change virtual function table entries,
517 new vtables will be initialized. We may reach the virtual
518 baseclass via ambiguous intervening baseclasses. This
519 loop makes sure we get through to the actual baseclass we marked. */
523 result = get_base_distance (basetype, for_type, 0, &path);
524 for_type = TREE_VALUE (path);
525 while (path)
527 CLASSTYPE_MARKED3 (TREE_VALUE (path)) = 1;
528 path = TREE_CHAIN (path);
531 while (result == -2);
534 /* Access the virtual function table entry that logically
535 contains BASE_FNDECL. VIRTUALS is the virtual function table's
536 initializer. */
537 static tree
538 get_vtable_entry (virtuals, base_fndecl)
539 tree virtuals, base_fndecl;
541 int i = (HOST_BITS_PER_INT >= BITS_PER_WORD
542 #ifdef VTABLE_USES_MASK
543 && 0
544 #endif
545 ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
546 & ((1<<(BITS_PER_WORD-1))-1))
547 : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
549 #ifdef GATHER_STATISTICS
550 n_vtable_searches += i;
551 #endif
553 while (i > 0)
555 virtuals = TREE_CHAIN (virtuals);
556 i -= 1;
558 return virtuals;
561 /* Put new entry ENTRY into virtual function table initializer
562 VIRTUALS. The virtual function table is for type CONTEXT.
564 Also update DECL_VINDEX (FNDECL). */
566 static void
567 modify_vtable_entry (old_entry_in_list, new_entry, fndecl, context)
568 tree old_entry_in_list, new_entry, fndecl, context;
570 tree base_pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list));
571 tree vindex;
573 /* We can't put in the really right offset information
574 here, since we have not yet laid out the class to
575 take into account virtual base classes. */
576 TREE_VALUE (old_entry_in_list) = new_entry;
577 vindex = DECL_VINDEX (TREE_OPERAND (base_pfn, 0));
578 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
579 SET_DECL_VINDEX (fndecl, vindex);
580 else
582 if (! tree_int_cst_equal (DECL_VINDEX (fndecl), vindex))
584 tree elts = CONSTRUCTOR_ELTS (new_entry);
585 tree vfield = CLASSTYPE_VFIELD (context);
586 /* Compute the relative offset of vtable we are really looking for. */
587 TREE_VALUE (elts) = genop (PLUS_EXPR,
588 build_int (DECL_OFFSET (vfield)
589 / DECL_SIZE_UNIT (vfield)),
590 TREE_VALUE (elts));
591 /* Say what index to use when we use that vtable. */
592 #ifndef VTABLE_USES_MASK
593 vindex = build_int_2 (TREE_INT_CST_LOW (vindex) & ~(1 << (BITS_PER_WORD -1)), 0);
594 #endif
595 TREE_VALUE (TREE_CHAIN (elts)) = vindex;
600 /* Modify virtual function tables in lattice topped by T to
601 place FNDECL in tables which previously held BASE_FNDECL.
602 PFN is just FNDECL wrapped in an ADDR_EXPR, so that it
603 is suitable for placement directly into an initializer.
605 All distinct virtual function tables that this type uses
606 must be updated. */
607 static void
608 modify_vtable_entries (t, fndecl, base_fndecl, pfn)
609 tree t;
610 tree fndecl, base_fndecl, pfn;
612 tree base_offset, offset;
613 tree context = DECL_CONTEXT (base_fndecl);
614 tree vfield = CLASSTYPE_VFIELD (t);
615 tree vfields, vbases;
617 DECL_VCONTEXT (fndecl) = DECL_VCONTEXT (base_fndecl);
619 if (DECL_CONTEXT (fndecl) == t
620 || !(TYPE_USES_VIRTUAL_BASECLASSES (t)
621 || TYPE_USES_MULTIPLE_INHERITANCE (t)))
622 offset = integer_zero_node;
623 else
625 tree assoc = virtual_member (DECL_CONTEXT (fndecl), CLASSTYPE_VBASECLASSES (t));
626 if (assoc == NULL_TREE)
627 assoc = assoc_value (DECL_CONTEXT (fndecl), t);
628 assert (assoc != NULL_TREE);
629 offset = ASSOC_OFFSET (assoc);
632 /* For each layer of base class (i.e., the first base class, and each
633 virtual base class from that one), modify the virtual function table
634 of the derived class to contain the new virtual function.
635 A class has as many vfields as it has virtual base classes (total). */
636 for (vfields = CLASSTYPE_VFIELDS (t); vfields; vfields = TREE_CHAIN (vfields))
638 int normal = 1;
639 tree assoc, this_offset;
640 tree base, path;
642 /* Find the right base class for this derived class, call it BASE. */
643 base = TREE_VALUE (vfields);
645 if (base != context)
647 /* If BASE_FNDECL is not contained in the vtable accessed by
648 the vslot, don't try to modify the vtable.
650 Virtual functions from virtual baseclasses are not in derived
651 virtual function tables. This is an implementation decision;
652 it keeps there from being a combinatorial exposion in the
653 number of different vtables which must be maintained. */
655 if (get_base_distance (base, context, 0, 0) == -1)
656 continue;
658 /* BASE_FNDECL is defined in a class derived from
659 the base class owning this VFIELD. */
661 /* Get the path starting from the deepest base class CONTEXT
662 of T (i.e., first defn of BASE_FNDECL). */
663 get_base_distance (context, t, 0, &path);
665 /* Get our best approximation of what to use for constructing
666 the virtual function table for T. */
669 /* Walk from base toward derived, stopping at the
670 most derived baseclass that matters. */
671 if (TREE_VIA_VIRTUAL (path))
673 base = TREE_VALUE (path);
674 assoc = value_member (TYPE_MAIN_VARIANT (base), CLASSTYPE_VBASECLASSES (t));
675 break;
677 if (TREE_CHAIN (path) == NULL_TREE
678 || (CLASSTYPE_BASECLASS (TREE_VALUE (TREE_CHAIN (path)), 1)
679 != TREE_VALUE (path))
680 || TREE_CHAIN (TREE_CHAIN (path)) == NULL_TREE)
682 base = TREE_VALUE (path);
683 assoc = assoc_value (TYPE_MAIN_VARIANT (base), t);
684 break;
686 path = TREE_CHAIN (path);
688 while (1);
690 /* Find the right offset for the this pointer based on the base
691 class we just found. */
692 base_offset = ASSOC_OFFSET (assoc);
693 if (base_offset == integer_zero_node)
694 this_offset = offset;
695 else
696 this_offset = genop (MINUS_EXPR, offset, base_offset);
698 /* Make sure we can modify the derived association with immunity. */
699 if (TREE_USED (CLASSTYPE_ASSOC (t)))
700 CLASSTYPE_ASSOC (t) = copy_assoc (CLASSTYPE_ASSOC (t));
702 /* We call this case NORMAL iff this virtual function table
703 pointer field has its storage reserved in this class.
704 This is normally the case without virtual baseclasses
705 or off-center multiple baseclasses. */
706 normal = (vfield != NULL_TREE
707 && TREE_VALUE (vfields) == DECL_FCONTEXT (vfield)
708 && (TREE_PURPOSE (vfields) == NULL_TREE
709 || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vfields))));
711 if (normal && TREE_PURPOSE (vfields))
712 /* Everything looks normal so far...check that we are really
713 working from VFIELD's basetype, and not some other appearance
714 of that basetype in the lattice. */
715 normal = (TREE_PURPOSE (vfields) == get_base_type (TREE_VALUE (vfields), t, 0));
717 if (normal)
719 /* In this case, it is *type*'s vtable we are modifying. */
720 context = t;
721 if (! CLASSTYPE_MARKED4 (t))
722 build_vtable (assoc, t);
723 assoc = CLASSTYPE_ASSOC (t);
725 else
727 /* This is our very own copy of `basetype' to play with. */
728 if (! CLASSTYPE_MARKED4 (ASSOC_TYPE (assoc)))
729 prepare_fresh_vtable (assoc, CLASSTYPE_ASSOC (base), t);
732 modify_vtable_entry (get_vtable_entry (ASSOC_VIRTUALS (assoc), base_fndecl),
733 build_vtable_entry (this_offset, pfn),
734 fndecl, context);
736 for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
738 tree this_offset;
739 tree base, path;
741 if (! ASSOC_VTABLE (vbases))
742 /* There are only two ways that a type can fail to have
743 virtual functions: neither it nor any of its base
744 types define virtual functions (in which case
745 no updating need be done), or virtual functions
746 accessible to it come from virtual base classes
747 (in which case we have or will get them modified
748 in other passes of this loop). */
749 continue;
751 base = TREE_VALUE (vbases);
752 path = NULL_TREE;
754 if (base != context
755 && get_base_distance (context, base, 0, &path) == -1)
756 continue;
758 /* Doesn't matter if not actually from this virtual base class,
759 but shouldn't come from deeper virtual baseclasses. The enclosing
760 loop should take care of such baseclasses. */
761 while (path)
763 if (TREE_VIA_VIRTUAL (path))
764 goto skip;
765 path = TREE_CHAIN (path);
768 base_offset = ASSOC_OFFSET (vbases);
769 if (base_offset == integer_zero_node)
770 this_offset = offset;
771 else
772 this_offset = genop (MINUS_EXPR, offset, base_offset);
774 /* Make sure we can modify the derived association with immunity. */
775 if (TREE_USED (CLASSTYPE_ASSOC (t)))
776 CLASSTYPE_ASSOC (t) = copy_assoc (CLASSTYPE_ASSOC (t));
778 /* This is our very own copy of `basetype' to play with. */
779 if (! CLASSTYPE_MARKED4 (ASSOC_TYPE (vbases)))
781 tree context_assoc = assoc_value (context, base);
782 prepare_fresh_vtable (vbases, context_assoc, t);
784 modify_vtable_entry (get_vtable_entry (ASSOC_VIRTUALS (vbases), base_fndecl),
785 build_vtable_entry (this_offset, pfn),
786 fndecl, context);
787 skip: {}
791 static tree
792 add_virtual_function (pending_virtuals, has_virtual, x, first)
793 tree pending_virtuals;
794 int *has_virtual;
795 tree x;
796 int first;
798 int debug_vbase = 1;
800 /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
801 convert to void *. Make such a conversion here. */
802 tree vfn = build1 (ADDR_EXPR, ptr_type_node, x);
803 TREE_LITERAL (vfn) = 1;
804 TREE_ADDRESSABLE (x) = CLASSTYPE_VTABLE_NEEDS_WRITING (current_class_type);
806 /* If the virtual function is a redefinition of a prior one,
807 figure out in which base class the new definition goes,
808 and if necessary, make a fresh virtual function table
809 to hold that entry. */
810 if (DECL_VINDEX (x) == NULL_TREE)
812 tree entry = build_vtable_entry (integer_zero_node, vfn);
814 /* Build a new INT_CST for this DECL_VINDEX. */
815 #ifdef VTABLE_USES_MASK
816 SET_DECL_VINDEX (x, build_int_2 (++(*has_virtual), 0));
817 #else
818 SET_DECL_VINDEX (x, build_int_2 (((1 << (BITS_PER_WORD - 1)) | ++(*has_virtual)), ~0));
819 #endif
820 pending_virtuals = tree_cons (DECL_VINDEX (x), entry, pending_virtuals);
822 /* Happens if declared twice in class. We will give error
823 later. */
824 else if (TREE_CODE (DECL_VINDEX (x)) == INTEGER_CST)
825 return pending_virtuals;
826 else if (debug_vbase && TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
828 /* Need an entry in some other virtual function table.
829 Deal with this after we have laid out our virtual base classes. */
830 pending_hard_virtuals = temp_tree_cons (x, vfn, pending_hard_virtuals);
832 else
834 /* Need an entry in some other virtual function table.
835 We can do this now. */
836 tree base_fndecl_list = DECL_VINDEX (x), base_fndecls, prev = 0;
837 tree vtable_context = DECL_FCONTEXT (CLASSTYPE_VFIELD (current_class_type));
838 tree true_base_fndecl = 0;
840 /* First assign DECL_VINDEX from the base vfn with which
841 we share our vtable. */
842 base_fndecls = base_fndecl_list;
843 while (base_fndecls)
845 if (TREE_CHAIN (base_fndecls) == NULL_TREE
846 || DECL_FCONTEXT (CLASSTYPE_VFIELD (DECL_CONTEXT (TREE_VALUE (base_fndecls)))) == vtable_context)
848 true_base_fndecl = TREE_VALUE (base_fndecls);
849 modify_vtable_entries (current_class_type, x,
850 true_base_fndecl, vfn);
851 if (prev)
852 TREE_CHAIN (prev) = TREE_CHAIN (base_fndecls);
853 else
854 base_fndecl_list = prev;
855 break;
857 prev = base_fndecls;
858 base_fndecls = TREE_CHAIN (base_fndecls);
861 /* Now fill in the rest of the vtables. */
862 base_fndecls = base_fndecl_list;
863 while (base_fndecls)
865 /* If we haven't found one we like, first one wins. */
866 if (true_base_fndecl == 0)
867 true_base_fndecl = TREE_VALUE (base_fndecls);
869 modify_vtable_entries (current_class_type, x,
870 TREE_VALUE (base_fndecls), vfn);
871 base_fndecls = TREE_CHAIN (base_fndecls);
874 DECL_VCONTEXT (x) = DECL_VCONTEXT (true_base_fndecl);
876 return pending_virtuals;
879 /* Obstack on which to build the vector of class methods. */
880 struct obstack class_obstack;
881 extern struct obstack *current_obstack;
883 /* Add method METHOD to class TYPE. This is used when a method
884 has been defined which did not initially appear in the class definition,
885 and helps cut down on spurious error messages.
887 FIELDS is the entry in the METHOD_VEC vector entry of the class type where
888 the method should be added. */
889 void
890 add_method (type, fields, method)
891 tree type, *fields, method;
893 /* We must make a copy of METHOD here, since we must be sure that
894 we have exclusive title to this method's TREE_CHAIN. */
895 int temp = allocation_temporary_p ();
896 tree decl;
898 if (temp)
899 end_temporary_allocation ();
901 decl = copy_node (method);
902 if (DECL_RTL (decl) == 0)
903 make_function_rtl (decl);
906 if (fields && *fields)
908 /* Take care not to hide destructor. */
909 TREE_CHAIN (decl) = TREE_CHAIN (*fields);
910 TREE_CHAIN (*fields) = decl;
912 else if (CLASSTYPE_METHOD_VEC (type) == 0)
914 tree method_vec = make_node (TREE_VEC);
915 if (DECL_NAME (TYPE_NAME (type)) == DECL_ORIGINAL_NAME (decl))
917 TREE_VEC_ELT (method_vec, 0) = decl;
918 TREE_VEC_LENGTH (method_vec) = 1;
920 else
922 obstack_free (current_obstack, method_vec);
923 obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
924 TREE_VEC_ELT (method_vec, 1) = decl;
925 TREE_VEC_LENGTH (method_vec) = 2;
926 obstack_finish (current_obstack);
928 CLASSTYPE_METHOD_VEC (type) = method_vec;
930 else
932 tree method_vec = CLASSTYPE_METHOD_VEC (type);
933 int len = TREE_VEC_LENGTH (method_vec);
935 /* Adding a new ctor or dtor. */
936 if (DECL_NAME (TYPE_NAME (type)) == DECL_ORIGINAL_NAME (decl))
937 TREE_VEC_ELT (method_vec, 0) = decl;
938 else
940 tree *end = (tree *)obstack_next_free (&class_obstack);
941 if (end != TREE_VEC_END (method_vec))
943 tree tmp_vec = copy_node (method_vec);
944 obstack_copy (current_obstack, &TREE_VEC_ELT (method_vec, 1), len);
945 obstack_blank (current_obstack, sizeof (tree *));
946 obstack_finish (current_obstack);
947 method_vec = tmp_vec;
949 else
951 /* We can easily extend the last such method_vec created. */
952 obstack_free (&class_obstack, method_vec);
953 obstack_blank (&class_obstack,
954 ((char *)end - (char *)method_vec) + sizeof (tree *));
955 method_vec = (tree)obstack_base (&class_obstack);
956 obstack_finish (&class_obstack);
958 TREE_VEC_ELT (method_vec, len) = decl;
959 TREE_VEC_LENGTH (method_vec) = len + 1;
960 CLASSTYPE_METHOD_VEC (type) = method_vec;
962 if (CLASSTYPE_BASELINK_VEC (type))
964 /* ??? May be better to know whether these can be extended? */
965 tree baselink_vec = copy_node (CLASSTYPE_BASELINK_VEC (type));
967 obstack_copy (current_obstack, &TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), 1), len);
968 TREE_VEC_ELT (baselink_vec, len) = 0;
969 TREE_VEC_LENGTH (baselink_vec) = len + 1;
970 CLASSTYPE_BASELINK_VEC (type) = baselink_vec;
974 DECL_CONTEXT (decl) = type;
975 DECL_VCONTEXT (decl) = type;
977 if (temp)
978 resume_temporary_allocation ();
981 /* Subroutines of finish_struct. */
983 /* Look through the list of fields for this struct, deleting
984 duplicates as we go. This must be recursive to handle
985 anonymous unions.
987 FIELD is the field which may not appear anywhere in FIELDS.
988 FIELD_PTR, if non-null, is the starting point at which
989 chained deletions may take place.
990 The value returned is the first acceptable entry found
991 in FIELDS.
993 Note that anonymous fields which are not of UNION_TYPE are
994 not duplicates, they are just anonymous fields. This happens
995 when we have unnamed bitfields, for example. */
996 static tree
997 delete_duplicate_fields_1 (field, field_ptr, fields)
998 tree field, *field_ptr, fields;
1000 tree x;
1001 tree prev = field_ptr ? *field_ptr : 0;
1002 if (DECL_NAME (field) == 0)
1004 if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
1005 return fields;
1007 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1008 fields = delete_duplicate_fields_1 (x, field_ptr, fields);
1009 if (prev)
1010 TREE_CHAIN (prev) = fields;
1011 return fields;
1013 else
1015 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1017 if (DECL_NAME (x) == 0)
1019 if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
1020 continue;
1021 TYPE_FIELDS (TREE_TYPE (x))
1022 = delete_duplicate_fields_1 (field, 0, TYPE_FIELDS (TREE_TYPE (x)));
1023 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1025 if (prev == 0)
1026 fields = TREE_CHAIN (fields);
1027 else
1028 TREE_CHAIN (prev) = TREE_CHAIN (x);
1031 else
1033 if (DECL_NAME (field) == DECL_NAME (x))
1035 if (TREE_CODE (field) == CONST_DECL
1036 && TREE_CODE (x) == CONST_DECL)
1037 error_with_decl (x, "duplicate enum value `%s'");
1038 else if (TREE_CODE (field) == CONST_DECL
1039 || TREE_CODE (x) == CONST_DECL)
1040 error_with_decl (x, "duplicate field `%s' (as enum and non-enum)");
1041 else
1042 error_with_decl (x, "duplicate member `%s'");
1043 if (prev == 0)
1044 fields = TREE_CHAIN (fields);
1045 else
1046 TREE_CHAIN (prev) = TREE_CHAIN (x);
1051 return fields;
1054 static void
1055 delete_duplicate_fields (fields)
1056 tree fields;
1058 tree x;
1059 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1060 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, &x, TREE_CHAIN (x));
1063 /* Add OFFSET to all child types of T.
1065 OFFSET, which is a type offset, is number of bytes.
1067 Note that we don't have to worry about having two paths to the
1068 same base type, since this type owns its association list. */
1069 static void
1070 propagate_basetype_offsets (for_type, t, offset)
1071 tree for_type, t;
1072 tree offset;
1074 int i, n_baselinks = CLASSTYPE_N_BASECLASSES (t);
1076 for (i = 1; i <= n_baselinks; i++)
1077 if (! CLASSTYPE_VIA_VIRTUAL (t, i))
1079 int j;
1080 tree basetype = CLASSTYPE_BASECLASS (t, i);
1081 tree assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), for_type);
1082 tree delta;
1084 for (j = i+1; j <= n_baselinks; j++)
1085 if (! CLASSTYPE_VIA_VIRTUAL (t, j))
1087 /* The next basetype offset must take into account the space
1088 between the classes, not just the size of each class. */
1089 delta = genop (MINUS_EXPR,
1090 CLASSTYPE_OFFSET (CLASSTYPE_BASECLASS (t, j)),
1091 CLASSTYPE_OFFSET (basetype));
1092 break;
1095 if (CLASSTYPE_OFFSET (basetype) == integer_zero_node)
1096 basetype = build_classtype_variant (basetype, offset, 0);
1097 else
1098 basetype = build_classtype_variant (basetype,
1099 genop (PLUS_EXPR, CLASSTYPE_OFFSET (basetype), offset), 0);
1100 /* Now make our own copy of this base type we can munge. */
1101 basetype = copy_node (basetype);
1102 copy_type_lang_specific (basetype);
1104 CLASSTYPE_BASECLASS (t, i) = basetype;
1105 ASSOC_TYPE (assoc) = basetype;
1106 ASSOC_OFFSET (assoc) = CLASSTYPE_OFFSET (basetype);
1107 TYPE_NAME (basetype) = copy_node (TYPE_NAME (basetype));
1108 TREE_TYPE (TYPE_NAME (basetype)) = basetype;
1109 DECL_OFFSET (TYPE_NAME (basetype))
1110 = TREE_INT_CST_LOW (CLASSTYPE_OFFSET (basetype)) * BITS_PER_UNIT;
1111 propagate_basetype_offsets (for_type, basetype, offset);
1113 /* Go to our next class that counts for offset propagation. */
1114 i = j;
1115 if (i <= n_baselinks)
1116 offset = genop (PLUS_EXPR, offset, delta);
1120 /* Change the visibility of T::FDECL to VISIBILITY.
1121 Return 1 if change was legit, otherwise return 0. */
1122 static int
1123 alter_visibility (t, fdecl, visibility)
1124 tree t;
1125 tree fdecl;
1126 enum visibility_type visibility;
1128 tree elem = purpose_member (t, DECL_VISIBILITY (fdecl));
1129 if (elem && TREE_VALUE (elem) != (tree)visibility)
1131 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1133 error_with_decl (TREE_TYPE (fdecl), "conflicting visibility specifications for method `%s', ignored");
1135 else error ("conflicting visibility specifications for field `%s', ignored", IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1137 else if (TREE_PRIVATE (fdecl) && visibility != visibility_private)
1138 error_with_decl (fdecl, "cannot make private %s non-private");
1139 else if (TREE_PROTECTED (fdecl) && visibility == visibility_public)
1141 error_with_decl (fdecl, "cannot make protected %s public");
1142 else if (elem == NULL_TREE)
1144 DECL_VISIBILITY (fdecl) = tree_cons (t, (tree)visibility,
1145 DECL_VISIBILITY (fdecl));
1146 return 1;
1148 return 0;
1151 /* If FOR_TYPE needs to reinitialize virtual function table pointers
1152 for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
1153 Returns BASE_INIT_LIST appropriately modified. */
1155 static tree
1156 maybe_fixup_vptrs (for_type, type, base_init_list)
1157 tree for_type, type, base_init_list;
1159 /* Now reinitialize any slots that don't fall under our virtual
1160 function table pointer. */
1161 tree vfields = CLASSTYPE_VFIELDS (type);
1162 while (vfields)
1164 tree basetype = get_base_type (TREE_VALUE (vfields), for_type, 0);
1165 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (basetype)
1166 && ((DECL_OFFSET (CLASSTYPE_VFIELD (basetype))
1167 + DECL_OFFSET (TYPE_NAME (basetype)))
1168 != DECL_OFFSET (CLASSTYPE_VFIELD (for_type)))
1169 && ((DECL_OFFSET (CLASSTYPE_VFIELD (basetype))
1170 + DECL_OFFSET (TYPE_NAME (basetype)))
1171 != (DECL_OFFSET (CLASSTYPE_VFIELD (type))
1172 + DECL_OFFSET (TYPE_NAME (type)))))
1173 base_init_list = tree_cons (error_mark_node, basetype,
1174 base_init_list);
1175 vfields = TREE_CHAIN (vfields);
1177 return base_init_list;
1180 /* If TYPE does not have a constructor, then the compiler must
1181 manually deal with all of the initialization this type requires.
1183 If a base initializer exists only to fill in the virtual function
1184 table pointer, then we mark that fact with the TREE_VIRTUAL bit.
1185 This way, we avoid multiple initializations of the same field by
1186 each virtual function table up the class hierarchy.
1188 Virtual base class pointers are not initialized here. They are
1189 initialized only at the "top level" of object creation. If we
1190 initialized them here, we would have to skip a lot of work. */
1192 static void
1193 build_class_init_list (type)
1194 tree type;
1196 tree base_init_list = NULL_TREE;
1197 tree member_init_list = NULL_TREE;
1199 /* Since we build member_init_list and base_init_list using
1200 tree_cons, backwards fields the all through work. */
1201 tree x;
1202 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (type);
1204 for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
1206 if (TREE_CODE (x) != FIELD_DECL)
1207 continue;
1209 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
1210 || DECL_INITIAL (x) != NULL_TREE)
1211 member_init_list = tree_cons (x, type, member_init_list);
1213 member_init_list = nreverse (member_init_list);
1215 /* We will end up doing this last. Need special marker
1216 to avoid infinite regress. */
1217 if (TYPE_VIRTUAL_P (type))
1219 base_init_list = build_tree_list (error_mark_node, type);
1220 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
1221 TREE_VALUE (base_init_list) = NULL_TREE;
1222 TREE_ADDRESSABLE (base_init_list) = 1;
1225 /* Each base class which needs to have initialization
1226 of some kind gets to make such requests known here. */
1227 for (i = n_baseclasses; i > 0; i--)
1229 tree basetype = CLASSTYPE_BASECLASS (type, i);
1230 tree blist;
1232 /* Don't initialize virtual baseclasses this way. */
1233 if (TREE_VIA_VIRTUAL (basetype))
1234 continue;
1236 if (TYPE_HAS_CONSTRUCTOR (basetype))
1238 /* ...and the last shall come first... */
1239 base_init_list = maybe_fixup_vptrs (type, basetype, base_init_list);
1240 base_init_list = tree_cons (NULL_TREE, basetype,
1241 base_init_list);
1242 continue;
1245 if ((blist = CLASSTYPE_BASE_INIT_LIST (basetype)) == NULL_TREE)
1246 /* Nothing to initialize. */
1247 continue;
1249 /* ...ditto... */
1250 base_init_list = maybe_fixup_vptrs (type, basetype, base_init_list);
1252 /* This is normally true for single inheritance.
1253 The win is we can shrink the chain of initializations
1254 to be done by only converting to the actual type
1255 we are interested in. */
1256 if (TREE_VALUE (blist)
1257 && TREE_CODE (TREE_VALUE (blist)) == RECORD_TYPE
1258 && (DECL_OFFSET (TYPE_NAME (basetype))
1259 == DECL_OFFSET (TYPE_NAME (TREE_VALUE (blist)))))
1261 if (base_init_list)
1263 /* Does it do more than just fill in a
1264 virtual function table pointer? */
1265 if (! TREE_ADDRESSABLE (blist))
1266 base_init_list = build_tree_list (blist, base_init_list);
1267 /* Can we get by just with the virtual function table
1268 pointer that it fills in? */
1269 else if (TREE_ADDRESSABLE (base_init_list)
1270 && TREE_VALUE (base_init_list) == 0)
1271 base_init_list = blist;
1272 /* Maybe, but it is not obvious as the previous case. */
1273 else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
1275 tree last = tree_last (base_init_list);
1276 while (TREE_VALUE (last)
1277 && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
1278 last = tree_last (TREE_VALUE (last));
1279 if (TREE_VALUE (last) == 0)
1280 base_init_list = build_tree_list (blist, base_init_list);
1283 else
1284 base_init_list = blist;
1286 else
1288 /* The function expand_aggr_init knows how to do the
1289 initialization of `basetype' without getting
1290 an explicit `blist'. */
1291 if (base_init_list)
1292 base_init_list = tree_cons (NULL_TREE, basetype, base_init_list);
1293 else
1294 base_init_list = CLASSTYPE_AS_LIST (basetype);
1298 if (base_init_list)
1299 if (member_init_list)
1300 CLASSTYPE_BASE_INIT_LIST (type) = build_tree_list (base_init_list, member_init_list);
1301 else
1302 CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
1303 else if (member_init_list)
1304 CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
1307 struct base_info
1309 int has_virtual;
1310 int max_has_virtual;
1311 int n_ancestors;
1312 tree vfield;
1313 tree vfields;
1314 char needs_default_ctor;
1315 char cant_have_default_ctor;
1316 char needs_const_ctor;
1317 char cant_have_const_ctor;
1320 /* Record information about type T derived from its base classes.
1321 Store most of that information in T itself, and place the
1322 remaining information in the struct BASE_INFO.
1324 Returns the index of the first base class to have virtual functions,
1325 or zero if no such base class. */
1327 static int
1328 finish_base_struct (t, b)
1329 tree t;
1330 struct base_info *b;
1332 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1333 int first_vfn_base_index = 0;
1335 bzero (b, sizeof (struct base_info));
1337 for (i = 1; i <= n_baseclasses; i++)
1339 tree basetype = CLASSTYPE_BASECLASS (t, i);
1341 /* If the type of basetype is incomplete, then
1342 we already complained about that fact
1343 (and we should have fixed it up as well). */
1344 if (TYPE_SIZE (basetype) == 0)
1346 int j;
1347 /* The base type is of incomplete type. It is
1348 probably best to pretend that it does not
1349 exist. */
1350 if (i == n_baseclasses)
1351 CLASSTYPE_BASECLASS (t, i) = NULL_TREE;
1352 CLASSTYPE_N_BASECLASSES (t) -= 1;
1353 n_baseclasses -= 1;
1354 for (j = i; j < n_baseclasses; j++)
1356 CLASSTYPE_BASECLASS (t, j) = CLASSTYPE_BASECLASS (t, j+1);
1357 SET_CLASSTYPE_VIAS (t, j,
1358 CLASSTYPE_VIA_PUBLIC (t, j+1),
1359 CLASSTYPE_VIA_VIRTUAL (t, j+1));
1363 if (TYPE_WRAP_TYPE (t) == NULL_TREE)
1364 TYPE_WRAP_TYPE (t) = TYPE_WRAP_TYPE (basetype);
1365 else if (TYPE_WRAP_TYPE (basetype)
1366 && TYPE_WRAP_TYPE (t) != TYPE_WRAP_TYPE (basetype))
1367 /* Must have its own. */
1368 TYPE_WRAP_TYPE (t) = error_mark_node;
1370 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1371 b->needs_default_ctor = 1;
1372 else if (TYPE_HAS_CONSTRUCTOR (basetype))
1373 b->cant_have_default_ctor = 1;
1374 if (TYPE_GETS_CONST_INIT_REF (basetype))
1375 b->needs_const_ctor = 1;
1376 else if (TYPE_GETS_INIT_REF (basetype))
1377 b->cant_have_const_ctor = 1;
1379 CLASSTYPE_ALTERS_VISIBILITIES_P (t)
1380 |= CLASSTYPE_ALTERS_VISIBILITIES_P (basetype);
1382 b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
1383 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1384 TYPE_NEEDS_CONSTRUCTOR (t) |= TYPE_NEEDS_CONSTRUCTOR (basetype);
1385 TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1386 TYPE_ANY_ASSIGNS_THIS (t) |= TYPE_ANY_ASSIGNS_THIS (basetype);
1387 TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (basetype);
1388 TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (basetype);
1390 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1391 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1393 if (CLASSTYPE_OFFSET (basetype) != integer_zero_node)
1395 /* Completely unshare potentially shared data, and
1396 update what is ours. */
1397 tree assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), t);
1398 basetype = copy_node (basetype);
1399 copy_type_lang_specific (basetype);
1400 CLASSTYPE_BASECLASS (t, i) = basetype;
1401 ASSOC_TYPE (assoc) = basetype;
1403 /* Propagate this offset through all the children. Do this
1404 before uniquizing baseclasses for virtual functions. */
1405 CLASSTYPE_ASSOC (basetype) = copy_assoc (CLASSTYPE_ASSOC (TYPE_MAIN_VARIANT (basetype)));
1407 propagate_basetype_offsets (basetype, basetype, CLASSTYPE_OFFSET (basetype));
1410 if (! CLASSTYPE_VIA_VIRTUAL (t, i))
1411 CLASSTYPE_N_SUPERCLASSES (t) += 1;
1413 if (TYPE_VIRTUAL_P (basetype))
1415 if (CLASSTYPE_VSIZE (basetype) > b->max_has_virtual)
1416 b->max_has_virtual = CLASSTYPE_VSIZE (basetype);
1418 /* Don't borrow virtuals from virtual baseclasses. */
1419 if (TREE_VIA_VIRTUAL (basetype))
1420 continue;
1422 if (first_vfn_base_index == 0)
1424 first_vfn_base_index = i;
1426 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1427 b->vfield = CLASSTYPE_VFIELD (basetype);
1428 b->vfields = CLASSTYPE_VFIELDS (basetype);
1429 CLASSTYPE_VFIELD (t) = b->vfield;
1431 else
1433 /* Only add unique vfields, and flatten them out as we go. */
1434 tree vfields = CLASSTYPE_VFIELDS (basetype);
1435 while (vfields)
1437 if (TREE_PURPOSE (vfields) == NULL_TREE
1438 || ! TREE_VIA_VIRTUAL (TREE_PURPOSE (vfields)))
1440 tree value = TREE_VALUE (vfields);
1441 if (TYPE_MAIN_VARIANT (basetype) == value)
1442 b->vfields = tree_cons (basetype, value, b->vfields);
1443 else
1444 b->vfields = tree_cons (get_base_type (value, basetype, 0),
1445 value, b->vfields);
1446 TREE_TYPE (b->vfields) = basetype;
1448 vfields = TREE_CHAIN (vfields);
1451 if (b->has_virtual == 0)
1453 first_vfn_base_index = i;
1454 b->has_virtual = CLASSTYPE_VSIZE (basetype);
1455 b->vfield = CLASSTYPE_VFIELD (basetype);
1456 CLASSTYPE_VFIELD (t) = b->vfield;
1461 if (b->vfield == 0)
1462 /* If all virtual functions come only from virtual baseclasses. */
1463 return 0;
1464 return first_vfn_base_index;
1467 static int
1468 typecode_p (type, code)
1469 tree type;
1470 enum tree_code code;
1472 return (TREE_CODE (type) == code
1473 || (TREE_CODE (type) == REFERENCE_TYPE
1474 && TREE_CODE (TREE_TYPE (type)) == code));
1477 /* Set memoizing fields and bits of T (and its variants) for later use.
1478 FIRST_VFN_BASE_INDEX is the first baseclass of T with virtual functions.
1479 MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables. */
1480 static void
1481 finish_struct_bits (t, first_vfn_base_index, max_has_virtual)
1482 tree t;
1483 int first_vfn_base_index, max_has_virtual;
1485 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1486 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1488 /* Fix up variants (if any). */
1489 tree variants = TYPE_NEXT_VARIANT (t);
1490 while (variants)
1492 TYPE_NEEDS_CONSTRUCTOR (variants) = TYPE_NEEDS_CONSTRUCTOR (t);
1493 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1494 TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1495 variants = TYPE_NEXT_VARIANT (variants);
1498 if (n_baseclasses && max_has_virtual)
1500 /* Done by `finish_struct' for classes without baseclasses. */
1501 int has_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
1502 if (has_abstract_virtuals == 0)
1503 for (i = CLASSTYPE_N_BASECLASSES (t); i >= 1; i--)
1504 has_abstract_virtuals
1505 |= (CLASSTYPE_ABSTRACT_VIRTUALS (CLASSTYPE_BASECLASS (t, i)) != 0);
1506 if (has_abstract_virtuals)
1507 CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1510 if (n_baseclasses)
1512 /* Notice whether this class has type conversion functions defined.
1513 Also report whether joining two types yields an ambiguity in the
1514 virtual function table, e.g.,
1516 struct A { virtual int f (); };
1517 struct B { virtual int f (); };
1518 struct C : A, B { / * no f (); * / }; / / error, ambiguous
1520 tree basetypes = CLASSTYPE_VBASECLASSES (t), basetype;
1521 int n_vbases = list_length (basetypes), j;
1523 build_mi_virtuals (n_baseclasses + (n_vbases*n_baseclasses), max_has_virtual);
1524 /* Fill in virutal function table with values which do not come
1525 "normal"ly, i.e., those which come from virtual and/or
1526 non-leftmost base classes. */
1527 for (i = n_baseclasses; basetypes; basetypes = TREE_CHAIN (basetypes))
1529 basetype = TREE_VALUE (basetypes);
1530 if (CLASSTYPE_VSIZE (basetype))
1531 for (j = n_baseclasses; j > 0; j--)
1533 tree this_base = CLASSTYPE_BASECLASS (t, j);
1534 if (get_base_distance (basetype, TYPE_MAIN_VARIANT (this_base), 0, 0) != -1)
1535 add_mi_virtuals (++i, TREE_CHAIN (ASSOC_VIRTUALS (basetypes)));
1538 for (i = n_baseclasses; i > 0; i--)
1540 basetype = CLASSTYPE_BASECLASS (t, i);
1542 if (TYPE_HAS_CONVERSION (basetype))
1544 TYPE_HAS_CONVERSION (t) = 1;
1545 TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
1546 TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
1548 if (TREE_VIA_VIRTUAL (basetype))
1549 /* Virtual functions from virtual baseclasses are done above. */;
1550 else if (i == first_vfn_base_index)
1551 add_mi_virtuals (i, TREE_CHAIN (CLASS_ASSOC_VIRTUALS (t)));
1552 else if (CLASSTYPE_VSIZE (basetype) != 0)
1553 add_mi_virtuals (i, TREE_CHAIN (CLASS_ASSOC_VIRTUALS (basetype)));
1555 report_ambiguous_mi_virtuals (n_baseclasses + (n_vbases*n_baseclasses), t);
1556 #if 0
1557 /* Now that we know what the virtual functiond table looks like,
1558 fix up offsets in the presence of virtual base classes. */
1559 if (n_vbases)
1560 fixup_vbase_offsets (t);
1561 #endif
1564 /* Need to test METHOD_VEC here in case all methods
1565 (conversions and otherwise) are inherited. */
1566 if (TYPE_HAS_CONVERSION (t) && method_vec != NULL_TREE)
1568 tree first_conversions[last_conversion_type];
1569 tree last_conversions[last_conversion_type];
1570 enum conversion_type conv_index;
1571 tree *tmp;
1572 int i;
1574 bzero (first_conversions, sizeof (first_conversions));
1575 for (tmp = &TREE_VEC_ELT (method_vec, 1);
1576 tmp != TREE_VEC_END (method_vec); tmp += 1)
1578 if (OPERATOR_TYPENAME_P (DECL_ORIGINAL_NAME (*tmp)))
1580 tree fntype = TREE_TYPE (*tmp);
1581 tree return_type = TREE_TYPE (fntype);
1582 assert (TREE_CODE (fntype) == METHOD_TYPE);
1584 if (typecode_p (return_type, POINTER_TYPE))
1586 if (TREE_READONLY (TREE_TYPE (return_type)))
1587 conv_index = constptr_conv;
1588 else
1589 conv_index = ptr_conv;
1591 else if (typecode_p (return_type, INTEGER_TYPE))
1593 TYPE_HAS_INT_CONVERSION (t) = 1;
1594 conv_index = int_conv;
1596 else if (typecode_p (return_type, REAL_TYPE))
1598 TYPE_HAS_REAL_CONVERSION (t) = 1;
1599 conv_index = real_conv;
1601 else
1602 continue;
1604 if (first_conversions[(int) conv_index] == NULL_TREE)
1605 first_conversions[(int) conv_index] = *tmp;
1606 last_conversions[(int) conv_index] = *tmp;
1610 for (i = 0; i < (int) last_conversion_type; i++)
1611 if (first_conversions[i] != last_conversions[i])
1612 CLASSTYPE_CONVERSION (t, i) = error_mark_node;
1613 else
1614 CLASSTYPE_CONVERSION (t, i) = first_conversions[i];
1617 /* If this type has constructors, force its mode to be BLKmode,
1618 and force its TREE_ADDRESSABLE bit to be nonzero. */
1619 if (TYPE_NEEDS_CONSTRUCTING (t) || TYPE_NEEDS_DESTRUCTOR (t))
1621 tree variants = t;
1623 if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
1624 DECL_MODE (TYPE_NAME (t)) = BLKmode;
1625 while (variants)
1627 TYPE_MODE (variants) = BLKmode;
1628 TREE_ADDRESSABLE (variants) = 1;
1629 variants = TYPE_NEXT_VARIANT (variants);
1634 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
1635 (or C++ class declaration).
1637 For C++, we must handle the building of derived classes.
1638 Also, C++ allows static class members. The way that this is
1639 handled is to keep the field name where it is (as the DECL_NAME
1640 of the field), and place the overloaded decl in the DECL_OFFSET
1641 of the field. layout_record and layout_union will know about this.
1643 More C++ hair: inline functions have text in their
1644 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
1645 meaningful tree structure. After the struct has been laid out, set
1646 things up so that this can happen.
1648 And still more: virtual functions. In the case of single inheritance,
1649 when a new virtual function is seen which redefines a virtual function
1650 from the base class, the new virtual function is placed into
1651 the virtual function table at exactly the same address that
1652 it had in the base class. When this is extended to multiple
1653 inheritance, the same thing happens, except that multiple virtual
1654 function tables must be maintained. The first virtual function
1655 table is treated in exactly the same way as in the case of single
1656 inheritance. Additional virtual function tables have different
1657 DELTAs, which tell how to adjust `this' to point to the right thing.
1659 LIST_OF_FIELDLISTS is just that. The elements of the list are
1660 TREE_LIST elements, whose TREE_PURPOSE field tells what visibility
1661 the list has, and the TREE_VALUE slot gives the actual fields.
1663 EMPTY is non-zero if this structure has no declarations following it.
1665 If flag_all_virtual == 1, then we lay all functions into
1666 the virtual function table, as though they were declared
1667 virtual. Constructors do not lay down in the virtual function table.
1669 If flag_all_virtual == 2, then we lay all functions into
1670 the virtual function table, such that virtual functions
1671 occupy a space by themselves, and then all functions
1672 of the class occupy a space by themselves. This is illustrated
1673 in the following diagram:
1675 class A; class B : A;
1677 Class A's vtbl: Class B's vtbl:
1678 --------------------------------------------------------------------
1679 | A's virtual functions| | B's virtual funcitions |
1680 | | | (may inherit some from A). |
1681 --------------------------------------------------------------------
1682 | All of A's functions | | All of A's functions |
1683 | (such as a->A::f). | | (such as b->A::f) |
1684 --------------------------------------------------------------------
1685 | B's new virtual functions |
1686 | (not defined in A.) |
1687 -------------------------------
1688 | All of B's functions |
1689 | (such as b->B::f) |
1690 -------------------------------
1692 this allows the program to make references to any function, virtual
1693 or otherwise in a type-consistant manner. */
1695 tree
1696 finish_struct (t, list_of_fieldlists, empty, warn_anon)
1697 tree t;
1698 tree list_of_fieldlists;
1699 int empty;
1700 int warn_anon;
1702 extern int interface_only, interface_unknown;
1703 int old;
1704 int round_up_size = 1;
1705 /* Set non-zero to debug using default functions.
1706 Not set by program. */
1707 static int debug_default_functions = 0;
1709 enum tree_code code = TREE_CODE (t);
1710 register tree x, y, method_vec;
1711 int needs_ctor = 0, needs_dtor = 0;
1712 int members_need_dtors = 0;
1713 tree name = TYPE_NAME (t), fields, fn_fields, tail;
1714 enum visibility_type visibility;
1715 int all_virtual;
1716 int has_virtual;
1717 int max_has_virtual;
1718 tree pending_virtuals = NULL_TREE;
1719 tree abstract_virtuals = NULL_TREE;
1720 tree vfield;
1721 tree vfields;
1722 int needs_default_ctor;
1723 int cant_have_default_ctor;
1724 int needs_const_ctor;
1725 int cant_have_const_ctor;
1727 /* The index of the first base class which has virtual
1728 functions. Only applied to non-virtual baseclasses. */
1729 int first_vfn_base_index;
1731 int i, n_baseclasses;
1732 int any_default_members = 0;
1733 char *err_name;
1734 int const_sans_init = 0;
1735 int ref_sans_init = 0;
1736 int nonprivate_method = 0;
1738 if (TREE_CODE (name) == TYPE_DECL)
1740 extern int lineno;
1742 DECL_SOURCE_FILE (name) = input_filename;
1743 DECL_SOURCE_LINE (name) = lineno;
1744 name = DECL_NAME (name);
1746 err_name = IDENTIFIER_POINTER (name);
1748 if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (name))
1750 warning ("un-usable class ignored (anonymous classes and unions are useless)");
1751 err_name = "(anon)";
1754 leftmost_baseclasses = NULL_TREE;
1755 if (TYPE_SIZE (t))
1757 if (TREE_CODE (t) == UNION_TYPE)
1758 error ("redefinition of `union %s'", err_name);
1759 else if (TREE_CODE (t) == RECORD_TYPE)
1760 error ("redefinition of `struct %s'", err_name);
1761 else
1762 assert (0);
1763 popclass (0);
1764 return t;
1767 #ifdef FIELD_XREF
1768 FIELD_xref_decl(current_function_decl,t);
1769 #endif
1771 /* If this type was previously laid out as a forward reference,
1772 make sure we lay it out again. */
1774 TYPE_SIZE (t) = 0;
1775 CLASSTYPE_GOT_SEMICOLON (t) = 0;
1776 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1777 CLASSTYPE_INTERFACE_UNKNOWN (t) = interface_unknown;
1779 old = suspend_momentary ();
1781 /* Install struct as DECL_FIELD_CONTEXT of each field decl.
1782 Also process specified field sizes.
1783 Set DECL_SIZE_UNIT to the specified size, or 0 if none specified.
1784 The specified size is found in the DECL_INITIAL.
1785 Store 0 there, except for ": 0" fields (so we can find them
1786 and delete them, below). */
1788 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1789 if (n_baseclasses >= 1)
1791 struct base_info base_info;
1793 /* If using multiple inheritance, this may cause variants of our
1794 basetypes to be used (instead of their canonical forms). */
1795 fields = layout_basetypes (t);
1796 y = tree_last (fields);
1798 first_vfn_base_index = finish_base_struct (t, &base_info);
1799 has_virtual = base_info.has_virtual;
1800 max_has_virtual = base_info.max_has_virtual;
1801 CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
1802 vfield = base_info.vfield;
1803 vfields = base_info.vfields;
1804 needs_default_ctor = base_info.needs_default_ctor;
1805 cant_have_default_ctor = base_info.cant_have_default_ctor;
1806 needs_const_ctor = base_info.needs_const_ctor;
1807 cant_have_const_ctor = base_info.cant_have_const_ctor;
1808 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1810 else
1812 first_vfn_base_index = 0;
1813 has_virtual = 0;
1814 max_has_virtual = 0;
1815 vfield = NULL_TREE;
1816 vfields = NULL_TREE;
1817 fields = NULL_TREE;
1818 y = NULL_TREE;
1819 needs_default_ctor = 0;
1820 cant_have_default_ctor = 0;
1821 needs_const_ctor = 0;
1822 cant_have_const_ctor = 0;
1825 if (write_virtuals == 3 && ! CLASSTYPE_INTERFACE_UNKNOWN (t))
1827 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1828 CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! interface_only;
1831 /* The three of these are approximations which may later be
1832 modified. Needed at this point to make add_virtual_function
1833 and modify_vtable_entries work. */
1834 CLASSTYPE_ASSOC (t) = make_assoc (integer_zero_node, t,
1835 0, 0, CLASSTYPE_ASSOC (t));
1836 CLASSTYPE_VFIELDS (t) = vfields;
1837 CLASSTYPE_VFIELD (t) = vfield;
1839 fn_fields = NULL_TREE;
1840 tail = NULL_TREE;
1841 if (y && list_of_fieldlists)
1842 TREE_CHAIN (y) = TREE_VALUE (list_of_fieldlists);
1844 #ifdef SOS
1845 if (flag_all_virtual == 2)
1846 all_virtual = 2;
1847 else
1848 #endif
1850 if (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (t))
1851 all_virtual = 1;
1852 else
1853 all_virtual = 0;
1856 if (CLASSTYPE_DECLARED_CLASS (t) == 0)
1858 nonprivate_method = 1;
1859 if (list_of_fieldlists
1860 && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
1861 TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_public;
1863 else if (list_of_fieldlists
1864 && TREE_PURPOSE (list_of_fieldlists) == (tree)visibility_default)
1865 TREE_PURPOSE (list_of_fieldlists) = (tree)visibility_private;
1867 while (list_of_fieldlists)
1869 visibility = (enum visibility_type)TREE_PURPOSE (list_of_fieldlists);
1871 for (x = TREE_VALUE (list_of_fieldlists); x; x = TREE_CHAIN (x))
1873 TREE_PRIVATE (x) = visibility == visibility_private;
1874 TREE_PROTECTED (x) = visibility == visibility_protected;
1875 #ifdef FIELD_XREF
1876 FIELD_xref_member(current_class_name,x);
1877 #endif
1879 if (TREE_CODE (x) == FUNCTION_DECL)
1881 /* Clear out this flag.
1883 @@ Doug may figure out how to break
1884 @@ this with nested classes and friends. */
1885 DECL_IN_AGGR_P (x) = 0;
1887 nonprivate_method |= ! TREE_PRIVATE (x);
1889 /* If this was an evil function, don't keep it in class. */
1890 if (IDENTIFIER_ERROR_LOCUS (DECL_NAME (x)))
1891 continue;
1893 if (y) TREE_CHAIN (y) = TREE_CHAIN (x);
1894 if (! fn_fields) fn_fields = x;
1895 else TREE_CHAIN (tail) = x;
1896 tail = x;
1897 if (DECL_CONTEXT (x))
1898 continue;
1900 DECL_CONTEXT (x) = t;
1901 DECL_VCONTEXT (x) = t;
1903 DECL_SIZE_UNIT (x) = 0;
1905 /* The name of the field is the original field name
1906 Save this in auxiliary field for later overloading. */
1907 if (DECL_VIRTUAL_P (x)
1908 || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
1910 pending_virtuals = add_virtual_function (pending_virtuals,
1911 &has_virtual, x,
1912 first_vfn_base_index);
1913 if (DECL_ABSTRACT_VIRTUAL_P (x))
1914 abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
1916 continue;
1919 /* Handle visibility declarations. */
1920 if (DECL_NAME (x) && TREE_CODE (DECL_NAME (x)) == SCOPE_REF)
1922 tree fdecl = TREE_OPERAND (DECL_NAME (x), 1);
1924 if (y) TREE_CHAIN (y) = TREE_CHAIN (x);
1925 /* Make type T see field decl FDECL with
1926 the visibility VISIBILITY. */
1927 if (TREE_CODE (fdecl) == TREE_LIST)
1929 fdecl = TREE_VALUE (fdecl);
1930 while (fdecl)
1932 if (alter_visibility (t, fdecl, visibility) == 0)
1933 break;
1934 fdecl = TREE_CHAIN (fdecl);
1937 else alter_visibility (t, fdecl, visibility);
1938 CLASSTYPE_ALTERS_VISIBILITIES_P (t) = 1;
1939 continue;
1942 /* Perform error checking that did not get done in grokdeclarator. */
1943 if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
1945 error_with_decl (x, "field `%s' invalidly declared function type");
1946 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
1948 else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
1950 error_with_decl (x, "field `%s' invalidly declared method type");
1951 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
1953 else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
1955 error_with_decl (x, "field `%s' invalidly declared offset type");
1956 TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
1958 /* If this is of reference type, check if it needs an init. */
1959 if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE
1960 && DECL_INITIAL (x) == 0)
1961 ref_sans_init = 1;
1963 /* When this goes into scope, it will be a non-local reference. */
1964 TREE_NONLOCAL (x) = 1;
1966 if (TREE_CODE (x) == FIELD_DECL)
1968 /* Never let anything with uninheritable virutals
1969 make it through without complaint. */
1970 if (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
1971 && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (x)))
1972 abstract_virtuals_error (x, TREE_TYPE (x));
1974 if (TYPE_LANG_SPECIFIC (TREE_TYPE (x)))
1976 if (TYPE_HAS_DEFAULT_CONSTRUCTOR (TREE_TYPE (x)))
1977 needs_default_ctor = 1;
1978 if (TYPE_GETS_CONST_INIT_REF (TREE_TYPE (x)))
1979 needs_const_ctor = 1;
1980 else if (TYPE_GETS_INIT_REF (TREE_TYPE (x)))
1981 cant_have_const_ctor = 1;
1983 else if (DECL_INITIAL (x) == NULL_TREE
1984 && (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (x))
1985 || TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE))
1986 cant_have_default_ctor = 1;
1988 /* If any field is const, the structure type is pseudo-const. */
1989 if (TREE_READONLY (x))
1991 C_TYPE_FIELDS_READONLY (t) = 1;
1992 if (DECL_INITIAL (x) == 0)
1993 const_sans_init = 1;
1995 else
1997 /* A field that is pseudo-const makes the structure likewise. */
1998 tree t1 = TREE_TYPE (x);
1999 while (TREE_CODE (t1) == ARRAY_TYPE)
2000 t1 = TREE_TYPE (t1);
2001 if (IS_AGGR_TYPE (t1))
2003 if (C_TYPE_FIELDS_READONLY (t1))
2004 C_TYPE_FIELDS_READONLY (t) = 1;
2005 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
2006 const_sans_init = 1;
2010 else if (TREE_STATIC (x) && TREE_CODE (t) == UNION_TYPE)
2011 /* Unions cannot have static members. */
2012 error_with_decl (x, "field `%s' declared static in union");
2014 if (! fields) fields = x;
2015 DECL_FIELD_CONTEXT (x) = t;
2016 DECL_SIZE_UNIT (x) = 0;
2018 if (TREE_PACKED (x))
2020 /* Invalid bit-field size done by grokfield. */
2021 /* Detect invalid bit-field type. */
2022 if (DECL_INITIAL (x)
2023 && TREE_CODE (TREE_TYPE (x)) != INTEGER_TYPE
2024 && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
2026 error_with_decl (x, "bit-field `%s' has invalid type");
2027 DECL_INITIAL (x) = NULL;
2029 if (DECL_INITIAL (x) && pedantic
2030 && TREE_TYPE (x) != integer_type_node
2031 && TREE_TYPE (x) != unsigned_type_node)
2032 warning_with_decl (x, "bit-field `%s' type invalid in ANSI C");
2034 /* Detect and ignore out of range field width. */
2035 if (DECL_INITIAL (x))
2037 register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
2039 if (width < 0)
2041 DECL_INITIAL (x) = NULL;
2042 warning_with_decl (x, "negative width in bit-field `%s'");
2044 else if (width == 0 && DECL_NAME (x) != 0)
2046 error_with_decl (x, "zero width for bit-field `%s'");
2047 DECL_INITIAL (x) = NULL;
2049 else if (width > TYPE_PRECISION (TREE_TYPE (x)))
2051 DECL_INITIAL (x) = NULL;
2052 warning_with_decl (x, "width of `%s' exceeds its type");
2056 /* Process valid field width. */
2057 if (DECL_INITIAL (x))
2059 register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
2061 if (width == 0)
2063 /* field size 0 => mark following field as "aligned" */
2064 if (TREE_CHAIN (x))
2065 DECL_ALIGN (TREE_CHAIN (x))
2066 = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY);
2067 /* field of size 0 at the end => round up the size. */
2068 else
2069 round_up_size = EMPTY_FIELD_BOUNDARY;
2071 else
2073 DECL_INITIAL (x) = NULL_TREE;
2074 DECL_SIZE_UNIT (x) = width;
2075 TREE_PACKED (x) = 1;
2076 /* Traditionally a bit field is unsigned
2077 even if declared signed. */
2078 if (flag_traditional
2079 && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
2080 TREE_TYPE (x) = unsigned_type_node;
2083 else
2084 /* Non-bit-fields are aligned for their type. */
2085 DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
2087 else if (TREE_CODE (x) == FIELD_DECL)
2089 tree type = TREE_TYPE (x);
2090 if (TREE_CODE (type) == ARRAY_TYPE)
2091 type = TREE_TYPE (type);
2092 if (code == UNION_TYPE)
2094 if (TYPE_NEEDS_CONSTRUCTING (type))
2095 error ("member %s::%s with constructor not allowed in union",
2096 IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (DECL_NAME (x)));
2097 if (TYPE_NEEDS_DESTRUCTOR (type))
2098 error ("member %s::%s with destructor (also) not allowed in union",
2099 IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (DECL_NAME (x)));
2101 else if (code == RECORD_TYPE)
2103 /* Array of record type doesn't matter for this bit. */
2104 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
2105 if (IS_AGGR_TYPE (type))
2107 needs_ctor |= TYPE_NEEDS_CONSTRUCTOR (type);
2108 needs_dtor |= TYPE_NEEDS_DESTRUCTOR (type);
2109 members_need_dtors |= TYPE_NEEDS_DESTRUCTOR (type);
2110 TYPE_GETS_ASSIGNMENT (t) |= TYPE_GETS_ASSIGNMENT (type);
2111 TYPE_GETS_INIT_REF (t) |= TYPE_GETS_INIT_REF (type);
2112 TYPE_GETS_CONST_INIT_REF (t) |= TYPE_GETS_CONST_INIT_REF (type);
2115 if (DECL_INITIAL (x) != NULL_TREE)
2117 /* `build_class_init_list' does not recognize non-FIELD_DECLs. */
2118 if (code == UNION_TYPE && any_default_members != 0)
2119 error ("multiple fields in union initialized");
2120 any_default_members = 1;
2123 y = x;
2125 list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
2126 /* link the tail while we have it! */
2127 if (y)
2129 TREE_CHAIN (y) = NULL_TREE;
2131 if (list_of_fieldlists
2132 && TREE_VALUE (list_of_fieldlists)
2133 && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
2134 TREE_CHAIN (y) = TREE_VALUE (list_of_fieldlists);
2138 if (tail) TREE_CHAIN (tail) = NULL_TREE;
2140 /* If this type has any constant members which did not come
2141 with their own initialization, mark that fact here. It is
2142 not an error here, since such types can be saved either by their
2143 constructors, or by fortuitous initialization. */
2144 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
2145 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
2146 CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
2148 if (vfield == 0
2149 && (has_virtual
2150 #ifdef SOS
2151 || TYPE_DYNAMIC (t)
2152 #endif
2155 /* We build this decl with ptr_type_node, and
2156 change the type when we know what it should be. */
2157 vfield = build_decl (FIELD_DECL, get_vfield_name (t), ptr_type_node);
2158 CLASSTYPE_VFIELD (t) = vfield;
2159 DECL_VIRTUAL_P (vfield) = 1;
2160 DECL_FIELD_CONTEXT (vfield) = t;
2161 SET_DECL_FCONTEXT (vfield, t);
2162 DECL_SIZE_UNIT (vfield) = 0;
2163 if (y)
2165 assert (TREE_CHAIN (y) == 0);
2166 TREE_CHAIN (y) = vfield;
2167 y = vfield;
2169 else fields = vfield;
2170 vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
2173 /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
2174 And they have already done their work.
2176 C++: maybe we will support default field initialization some day... */
2178 /* Delete all zero-width bit-fields from the front of the fieldlist */
2179 while (fields && TREE_PACKED (fields)
2180 && DECL_INITIAL (fields))
2181 fields = TREE_CHAIN (fields);
2182 /* Delete all such fields from the rest of the fields. */
2183 for (x = fields; x;)
2185 if (TREE_CHAIN (x) && TREE_PACKED (TREE_CHAIN (x))
2186 && DECL_INITIAL (TREE_CHAIN (x)))
2187 TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
2188 else x = TREE_CHAIN (x);
2190 /* Delete all duplicate fields from the fields */
2191 delete_duplicate_fields (fields);
2193 /* Now we have the final fieldlist for the data fields. Record it,
2194 then lay out the structure or union (including the fields). */
2196 TYPE_FIELDS (t) = fields;
2198 /* If there's a :0 field at the end, round the size to the
2199 EMPTY_FIELD_BOUNDARY. */
2200 TYPE_ALIGN (t) = round_up_size;
2202 if (debug_default_functions)
2204 if ((TYPE_NEEDS_CONSTRUCTOR (t) || TYPE_HAS_CONSTRUCTOR (t) || needs_ctor)
2205 && ! TYPE_HAS_INIT_REF (t))
2207 tree default_fn = cons_up_default_function (t, name, 1);
2208 TREE_CHAIN (default_fn) = fn_fields;
2209 DECL_CONTEXT (default_fn) = t;
2210 DECL_VCONTEXT (default_fn) = t;
2211 fn_fields = default_fn;
2212 TYPE_HAS_INIT_REF (t) = 1;
2213 default_fn = cons_up_default_function (t, name, 3);
2214 TREE_CHAIN (default_fn) = fn_fields;
2215 DECL_CONTEXT (default_fn) = t;
2216 DECL_VCONTEXT (default_fn) = t;
2217 fn_fields = default_fn;
2218 nonprivate_method = 1;
2221 if (! TYPE_HAS_DEFAULT_CONSTRUCTOR (t)
2222 && needs_default_ctor && ! cant_have_default_ctor)
2224 tree default_fn = cons_up_default_function (t, name, 2);
2225 TREE_CHAIN (default_fn) = fn_fields;
2226 DECL_CONTEXT (default_fn) = t;
2227 DECL_VCONTEXT (default_fn) = t;
2228 fn_fields = default_fn;
2229 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
2230 nonprivate_method = 1;
2233 /* Warn about duplicate methods in fn_fields. Also compact
2234 method lists so that lookup can be made faster.
2236 Algorithm: Outer loop builds lists by method name.
2237 Inner loop checks for redundant method names within a list.
2239 Data Structure: List of method lists. The outer list
2240 is a TREE_LIST, whose TREE_PURPOSE field is the field name
2241 and the TREE_VALUE is the TREE_CHAIN of the FUNCTION_DECLs.
2242 Friends are chained in the same way as member functions, but
2243 they live in the TREE_TYPE field of the outer list.
2244 That allows them to be quicky deleted, and requires
2245 no extra storage.
2247 If there are any constructors/destructors, they are moved to
2248 the front of the list. This makes pushclass more efficient.
2250 We also link each field which has shares a name with its
2251 baseclass to the head of the list of fields for that base class.
2252 This allows us to reduce search time in places like `build_method_call'
2253 to consider only reasonably likely functions. */
2255 if (fn_fields)
2257 /* Now prepare to gather fn_fields into vector. */
2258 struct obstack *ambient_obstack = current_obstack;
2259 current_obstack = &class_obstack;
2260 method_vec = make_node (TREE_VEC);
2261 /* Room has been saved for constructors and destructors. */
2262 current_obstack = ambient_obstack;
2263 /* Now make this a live vector. */
2264 obstack_free (&class_obstack, method_vec);
2265 obstack_blank (&class_obstack, sizeof (struct tree_vec));
2267 while (fn_fields)
2269 /* NEXT Pointer, TEST Pointer, and BASE Pointer. */
2270 tree nextp, *testp;
2272 nextp = TREE_CHAIN (fn_fields);
2273 TREE_CHAIN (fn_fields) = NULL_TREE;
2274 /* Constrcutors are handled easily in search routines.
2275 Besides, we know we wont find any, so do not bother looking. */
2276 if (DECL_ORIGINAL_NAME (fn_fields) == name
2277 && TREE_VEC_ELT (method_vec, 0) == 0)
2278 TREE_VEC_ELT (method_vec, 0) = fn_fields;
2279 else
2281 testp = &TREE_VEC_ELT (method_vec, 0);
2282 if (*testp == NULL_TREE)
2283 testp++;
2284 while ((int)testp < (int)obstack_next_free (&class_obstack)
2285 && DECL_ORIGINAL_NAME (*testp) != DECL_ORIGINAL_NAME (fn_fields))
2286 testp++;
2287 if ((int)testp < (int)obstack_next_free (&class_obstack))
2289 for (x = *testp; x; x = TREE_CHAIN (x))
2291 if (DECL_NAME (fn_fields) == DECL_NAME (x))
2293 /* We complain about multiple destructors on sight,
2294 so we do not repeat the warning here. Friend-friend
2295 ambiguities are warned about outside this loop. */
2296 if (! DESTRUCTOR_NAME_P (DECL_NAME (fn_fields)))
2297 error_with_file_and_line (DECL_SOURCE_FILE (fn_fields),
2298 DECL_SOURCE_LINE (fn_fields),
2299 "ambiguous method `%s' in structure",
2300 lang_printable_name (fn_fields));
2301 break;
2303 y = x;
2305 if (x == 0)
2306 if (*testp)
2307 TREE_CHAIN (y) = fn_fields;
2308 else
2309 *testp = fn_fields;
2311 else
2313 obstack_ptr_grow (&class_obstack, fn_fields);
2314 method_vec = (tree)obstack_base (&class_obstack);
2317 fn_fields = nextp;
2320 TREE_VEC_LENGTH (method_vec)
2321 = (tree *)obstack_next_free (&class_obstack) - (&TREE_VEC_ELT (method_vec, 0));
2322 obstack_finish (&class_obstack);
2323 CLASSTYPE_METHOD_VEC (t) = method_vec;
2325 if (nonprivate_method == 0
2326 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2327 && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
2329 for (i = 0; i <= n_baseclasses; i++)
2330 if (CLASSTYPE_VIA_PUBLIC (t, i))
2332 nonprivate_method = 1;
2333 break;
2335 if (nonprivate_method == 0)
2336 warning ("all class member functions are private");
2339 else
2341 method_vec = 0;
2343 /* Just in case these got accidently
2344 filled in by syntax errors. */
2345 TYPE_HAS_CONSTRUCTOR (t) = 0;
2346 TYPE_HAS_DESTRUCTOR (t) = 0;
2349 /* If there are constructors (and destructors), they are at the
2350 front. Place destructors at very front. Also warn if all
2351 constructors and/or destructors are private (in which case this
2352 class is effectively unusable. */
2353 if (TYPE_HAS_DESTRUCTOR (t))
2355 tree dtor, prev;
2357 for (dtor = TREE_VEC_ELT (method_vec, 0); dtor; prev = dtor, dtor = TREE_CHAIN (dtor))
2359 if (DESTRUCTOR_NAME_P (DECL_NAME (dtor)))
2361 if (TREE_PRIVATE (dtor)
2362 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2363 && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
2364 warning_with_decl (TYPE_NAME (t), "class `%s' only defines a private destructor and has no friends");
2365 break;
2368 /* Wild parse errors can cause this to happen. */
2369 if (dtor == NULL_TREE)
2370 TYPE_HAS_DESTRUCTOR (t) = 0;
2371 else if (dtor != TREE_VEC_ELT (method_vec, 0))
2373 TREE_CHAIN (prev) = TREE_CHAIN (dtor);
2374 TREE_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
2375 TREE_VEC_ELT (method_vec, 0) = dtor;
2378 else if (members_need_dtors
2379 || TYPE_USES_VIRTUAL_BASECLASSES (t)
2380 || TYPE_USES_MULTIPLE_INHERITANCE (t))
2382 /* Here we must cons up a destructor on the fly. */
2383 tree dtor = cons_up_default_function (t, name, 0);
2385 /* If we couldn't make it work, then pretend we didn't need it. */
2386 if (dtor == void_type_node)
2387 TYPE_NEEDS_DESTRUCTOR (t) = 0;
2388 else
2390 DECL_CONTEXT (dtor) = t;
2391 DECL_VCONTEXT (dtor) = t;
2392 if (DECL_VIRTUAL_P (dtor))
2393 pending_virtuals = add_virtual_function (pending_virtuals,
2394 &has_virtual, dtor);
2395 if (TYPE_HAS_CONSTRUCTOR (t))
2396 TREE_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
2397 else if (method_vec == 0)
2399 /* Now prepare to gather fn_fields into vector. */
2400 struct obstack *ambient_obstack = current_obstack;
2401 current_obstack = &class_obstack;
2402 method_vec = make_node (TREE_VEC);
2403 /* Room has been saved for constructors and destructors. */
2404 current_obstack = ambient_obstack;
2405 TREE_VEC_LENGTH (method_vec) = 1;
2406 CLASSTYPE_METHOD_VEC (t) = method_vec;
2408 TREE_VEC_ELT (method_vec, 0) = dtor;
2409 TYPE_HAS_DESTRUCTOR (t) = 1;
2412 if (TYPE_HAS_CONSTRUCTOR (t)
2413 && ! CLASSTYPE_DECLARED_EXCEPTION (t)
2414 && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
2415 && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
2417 int nonprivate_ctor = 0;
2418 tree ctor;
2420 for (ctor = TREE_VEC_ELT (method_vec, 0); ctor; ctor = TREE_CHAIN (ctor))
2421 if (! TREE_PRIVATE (ctor))
2423 nonprivate_ctor = 1;
2424 break;
2426 if (nonprivate_ctor == 0)
2427 warning ("class %s only defines private constructors and has no friends",
2428 TYPE_NAME_STRING (t));
2431 /* Now for each member function (except for constructors and
2432 destructors), compute where member functions of the same
2433 name reside in base classes. */
2434 if (n_baseclasses != 0
2435 && method_vec != NULL_TREE
2436 && TREE_VEC_LENGTH (method_vec) > 1)
2438 int len = TREE_VEC_LENGTH (method_vec);
2439 tree baselink_vec = make_tree_vec (len);
2440 int any_links = 0;
2442 for (i = 1; i < len; i++)
2444 TREE_VEC_ELT (baselink_vec, i)
2445 = get_baselinks (t, DECL_ORIGINAL_NAME (TREE_VEC_ELT (method_vec, i)));
2446 if (TREE_VEC_ELT (baselink_vec, i) != 0)
2447 any_links = 1;
2449 if (any_links != 0)
2450 CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
2451 else
2452 obstack_free (current_obstack, baselink_vec);
2455 /* We can't know this information until we have seen all of the
2456 constructors. */
2457 TYPE_NONE_ASSIGN_THIS (t) = 0;
2459 /* Pass layout information about base classes to layout_type, if any. */
2461 if (n_baseclasses)
2463 tree pseudo_basetype = TREE_TYPE (base_layout_decl);
2465 TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
2466 TYPE_FIELDS (t) = base_layout_decl;
2468 TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
2469 TYPE_SIZE_UNIT (pseudo_basetype) = TYPE_SIZE_UNIT (t);
2470 TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
2471 TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
2472 DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
2475 layout_type (t);
2477 if (n_baseclasses)
2478 TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
2480 /* C++: do not let empty structures exist. */
2481 if (integer_zerop (TYPE_SIZE (t)))
2482 TYPE_SIZE (t) = TYPE_SIZE (char_type_node);
2484 /* Set the TYPE_DECL for this type to contain the right
2485 value for DECL_OFFSET, so that we can use it as part
2486 of a COMPONENT_REF for multiple inheritance. */
2488 if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
2489 layout_decl (TYPE_NAME (t));
2491 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2493 tree vbases;
2495 max_has_virtual = layout_vbasetypes (t, max_has_virtual);
2496 vbases = CLASSTYPE_VBASECLASSES (t);
2497 CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
2499 /* Now fix up any virtual base class types that we
2500 left lying around. We must get these done
2501 before we try to lay out the virtual function table. */
2502 pending_hard_virtuals = nreverse (pending_hard_virtuals);
2503 #if 1
2504 /* This loop makes all the entries in the virtual function tables
2505 of interest contain the "latest" version of the functions
2506 we have defined. */
2508 while (vbases)
2510 tree virtuals = ASSOC_VIRTUALS (vbases);
2512 if (virtuals)
2513 virtuals = TREE_CHAIN (virtuals);
2515 while (virtuals != NULL_TREE)
2517 tree pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals));
2518 tree base_fndecl = TREE_OPERAND (pfn, 0);
2519 tree decl = get_first_matching_virtual (t, base_fndecl, 0);
2520 tree context = DECL_CONTEXT (decl);
2521 if (decl != base_fndecl && context != t)
2523 tree assoc = NULL_TREE, these_virtuals;
2524 int i = TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)) & ((1<<(BITS_PER_WORD-1))-1);
2526 if (TYPE_USES_VIRTUAL_BASECLASSES (context))
2527 assoc = virtual_member (DECL_CONTEXT (base_fndecl),
2528 CLASSTYPE_VBASECLASSES (context));
2529 if (assoc == NULL_TREE)
2530 assoc = assoc_value (DECL_CONTEXT (base_fndecl), context);
2531 if (assoc != NULL_TREE)
2533 these_virtuals = ASSOC_VIRTUALS (assoc);
2535 while (i-- > 0)
2536 these_virtuals = TREE_CHAIN (these_virtuals);
2537 pfn = FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (these_virtuals));
2538 modify_vtable_entries (t, decl, base_fndecl, pfn);
2541 virtuals = TREE_CHAIN (virtuals);
2543 vbases = TREE_CHAIN (vbases);
2545 #endif /* 1 */
2546 while (pending_hard_virtuals)
2548 /* Need an entry in some other virtual function table. */
2549 tree base_fndecls = DECL_VINDEX (TREE_PURPOSE (pending_hard_virtuals));
2550 while (base_fndecls)
2552 modify_vtable_entries (t, TREE_PURPOSE (pending_hard_virtuals),
2553 TREE_VALUE (base_fndecls),
2554 TREE_VALUE (pending_hard_virtuals));
2555 base_fndecls = TREE_CHAIN (base_fndecls);
2557 pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
2560 else
2561 CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
2563 if (pending_virtuals)
2565 pending_virtuals = nreverse (pending_virtuals);
2566 /* We must enter these virtuals into the table. */
2567 if (first_vfn_base_index == 0)
2569 pending_virtuals = tree_cons (NULL_TREE, the_null_vtable_entry,
2570 pending_virtuals);
2571 build_vtable (0, t);
2573 else
2575 /* Here we know enough to change the type of our virtual
2576 function table, but we will wait until later this function. */
2577 if (! CLASSTYPE_MARKED4 (t))
2578 build_vtable (assoc_value (TYPE_MAIN_VARIANT (CLASSTYPE_BASECLASS (t, first_vfn_base_index)), t), t);
2581 /* If this type has basetypes with constructors, then those
2582 constructors might clobber the virtual function table. But
2583 they don't if the derived class shares the exact vtable of the base
2584 class. */
2586 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
2588 else if (first_vfn_base_index)
2590 tree basetype = get_base_type (DECL_FIELD_CONTEXT (vfield), t, 0);
2591 tree assoc;
2593 if (TREE_VIA_VIRTUAL (basetype))
2594 assoc = virtual_member (DECL_FIELD_CONTEXT (vfield), CLASSTYPE_VBASECLASSES (t));
2595 else
2596 assoc = assoc_value (TYPE_MAIN_VARIANT (basetype), t);
2598 /* This class contributes nothing new to the virtual function
2599 table. However, it may have declared functions which
2600 went into the virtual function table "inherited" from the
2601 base class. If so, we grab a copy of those updated functions,
2602 and pretend they are ours. */
2604 #ifdef SOS
2605 /* Don't define this ahead of time if we have more
2606 fields to add later. */
2607 if (all_virtual == 2 && fn_fields != NULL_TREE)
2609 else
2610 #endif
2612 /* See if we should steal the virtual info from base class. */
2613 if (CLASS_ASSOC_VTABLE (t) == NULL_TREE)
2614 CLASS_ASSOC_VTABLE (t) = ASSOC_VTABLE (assoc);
2615 if (CLASS_ASSOC_VIRTUALS (t) == NULL_TREE)
2616 CLASS_ASSOC_VIRTUALS (t) = ASSOC_VIRTUALS (assoc);
2618 if (CLASS_ASSOC_VTABLE (t) != ASSOC_VTABLE (assoc))
2619 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
2622 if (has_virtual > max_has_virtual)
2623 max_has_virtual = has_virtual;
2624 if (max_has_virtual || first_vfn_base_index)
2626 #ifdef VTABLE_USES_MASK
2627 if (max_has_virtual >= VINDEX_MAX)
2629 error ("too many virtual functions for class `%s' (VINDEX_MAX < %d)", TYPE_NAME_STRING (t), has_virtual);
2631 #endif
2632 TYPE_VIRTUAL_P (t) = 1;
2633 CLASSTYPE_VSIZE (t) = has_virtual;
2634 if (first_vfn_base_index)
2636 if (pending_virtuals)
2637 CLASS_ASSOC_VIRTUALS (t) = chainon (CLASS_ASSOC_VIRTUALS (t),
2638 pending_virtuals);
2640 else if (has_virtual)
2642 CLASS_ASSOC_VIRTUALS (t) = pending_virtuals;
2643 if (write_virtuals >= 0)
2644 DECL_VIRTUAL_P (CLASS_ASSOC_VTABLE (t)) = 1;
2648 #ifdef SOS
2649 if (all_virtual == 2 && (max_has_virtual || method_vec))
2651 /* Now that we know the size of the virtual table, lay out
2652 the absolute table following it. */
2653 int i;
2654 tree tmp;
2655 tree pending_absolutes = NULL_TREE;
2656 int has_absolute = has_virtual;
2658 /* Local variables for building a table filled with strings
2659 containing the names of interesting things. */
2660 tree decl, init;
2661 tree start = NULL_TREE, next = NULL_TREE;
2662 tree *outer = &TREE_VEC_ELT (method_vec, 0);
2664 while (outer != TREE_VEC_END (method_vec))
2666 tree inner;
2667 for (inner = *outer; inner; inner = TREE_CHAIN (inner))
2669 tree entry;
2670 tree fn;
2672 /* Don't bother with functions which appear
2673 for visibility reasons. */
2674 if (DECL_FIELD_CONTEXT (inner) != t)
2675 continue;
2677 /* Must lay this function into its absolute table as well.
2678 This forces an inline function to be written out. */
2679 fn = build1 (ADDR_EXPR, ptr_type_node, inner);
2680 TREE_LITERAL (fn) = 1;
2681 DECL_DINDEX (inner) = build_int_2 (++has_absolute, 0);
2682 entry = build_vtable_entry (integer_zero_node, fn);
2683 pending_absolutes = tree_cons (DECL_DINDEX (inner), entry,
2684 pending_absolutes);
2686 outer++;
2689 CLASS_ASSOC_VIRTUALS (t) = chainon (CLASS_ASSOC_VIRTUALS (t),
2690 nreverse (pending_absolutes));
2691 if (TYPE_DYNAMIC (t))
2693 for (outer = &TREE_VEC_ELT (method_vec, 0);
2694 outer != TREE_VEC_END (method_vec);
2695 outer++)
2697 tree inner;
2698 for (inner = *outer; inner; inner = TREE_CHAIN (inner))
2700 tree str = make_node (STRING_CST);
2701 TREE_STRING_LENGTH (str) = IDENTIFIER_LENGTH (DECL_NAME (inner));
2702 TREE_STRING_POINTER (str) = IDENTIFIER_POINTER (DECL_NAME (inner));
2703 TREE_LITERAL (str) = 1;
2704 TREE_STATIC (str) = 1;
2705 TREE_TYPE (str)
2706 = build_cplus_array_type (char_type_node,
2707 build_index_type (build_int_2 (TREE_STRING_LENGTH (str) - 1, 0)));
2709 if (start)
2711 TREE_CHAIN (next) = build_tree_list (NULL_TREE, str);
2712 next = TREE_CHAIN (next);
2714 else
2716 start = build_tree_list (NULL_TREE, str);
2717 next = start;
2722 /* Lay out dynamic link table for SOS. */
2724 decl = finish_table (get_linktable_name (t),
2725 string_type_node, start, 0);
2727 has_virtual = has_absolute;
2728 CLASSTYPE_VSIZE (t) = has_virtual;
2729 if (has_virtual > max_has_virtual)
2730 max_has_virtual = has_virtual;
2731 if (vfield == 0)
2733 /* We build this decl with ptr_type_node, and
2734 change the type when we know what it should be. */
2735 vfield = build_decl (FIELD_DECL, get_vfield_name (t), ptr_type_node);
2736 CLASSTYPE_VFIELD (t) = vfield;
2737 DECL_VIRTUAL_P (vfield) = 1;
2738 DECL_FIELD_CONTEXT (vfield) = t;
2739 SET_DECL_FCONTEXT (vfield, t);
2740 DECL_SIZE_UNIT (vfield) = 0;
2741 y = tree_last (fields);
2742 if (y)
2743 TREE_CHAIN (y) = vfield;
2744 else
2745 fields = vfield;
2746 vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
2749 #endif
2751 /* Now lay out the virtual function table. */
2752 if (has_virtual)
2754 tree atype, itype;
2756 if (TREE_TYPE (vfield) == ptr_type_node)
2758 /* We must create a pointer to this table because
2759 the one inherited from base class does not exist.
2760 We will fill in the type when we know what it
2761 should really be. */
2762 itype = build_index_type (build_int_2 (has_virtual, 0));
2763 atype = build_array_type (vtable_entry_type, itype);
2764 layout_type (atype);
2765 TREE_TYPE (vfield) = build_pointer_type (atype);
2767 else
2769 atype = TREE_TYPE (TREE_TYPE (vfield));
2771 if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
2773 /* We must extend (or create) the boundaries on this array,
2774 because we picked up virtual functions from multiple
2775 base classes. */
2776 itype = build_index_type (build_int_2 (has_virtual, 0));
2777 atype = build_array_type (vtable_entry_type, itype);
2778 layout_type (atype);
2779 vfield = copy_node (vfield);
2780 TREE_TYPE (vfield) = build_pointer_type (atype);
2781 #if 0
2782 /* In the case of single inheritance, we can
2783 just move up the tree, since we share the
2784 same vptr slot. */
2785 if (TREE_CHAIN (vfields) == NULL_TREE)
2786 vfields = CLASSTYPE_AS_LIST (t);
2787 #endif
2791 CLASSTYPE_VFIELD (t) = vfield;
2792 if (TREE_TYPE (CLASS_ASSOC_VTABLE (t)) != atype)
2794 TREE_TYPE (CLASS_ASSOC_VTABLE (t)) = atype;
2795 layout_decl (CLASS_ASSOC_VTABLE (t));
2796 DECL_ALIGN (CLASS_ASSOC_VTABLE (t))
2797 = MAX (TYPE_ALIGN (double_type_node),
2798 DECL_ALIGN (CLASS_ASSOC_VTABLE (t)));
2801 else if (first_vfn_base_index)
2802 CLASSTYPE_VFIELD (t) = vfield;
2803 CLASSTYPE_VFIELDS (t) = vfields;
2805 /* Set all appropriate CLASSTYPE_... flags for this type
2806 and its variants. */
2807 TYPE_NEEDS_CONSTRUCTOR (t) |= needs_ctor || TYPE_HAS_CONSTRUCTOR (t);
2808 TYPE_NEEDS_CONSTRUCTING (t)
2809 |= ((TYPE_NEEDS_CONSTRUCTOR (t)|TYPE_USES_VIRTUAL_BASECLASSES (t))
2810 || (has_virtual | first_vfn_base_index)
2811 || any_default_members);
2812 TYPE_NEEDS_DESTRUCTOR (t) |= needs_dtor || TYPE_HAS_DESTRUCTOR (t);
2813 finish_struct_bits (t, first_vfn_base_index, max_has_virtual);
2815 /* Promote each bit-field's type to int if it is narrower than that.
2816 Also warn (or error) if static members are specified for a class
2817 which takes a constructor. */
2818 for (x = fields; x; x = TREE_CHAIN (x))
2820 if (TREE_PACKED (x)
2821 && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE
2822 && (TREE_INT_CST_LOW (DECL_SIZE (x)) * DECL_SIZE_UNIT (x)
2823 < TYPE_PRECISION (integer_type_node)))
2824 TREE_TYPE (x) = integer_type_node;
2827 if (TYPE_HAS_CONSTRUCTOR (t))
2829 tree vfields = CLASSTYPE_VFIELDS (t);
2831 while (vfields)
2833 /* Mark the fact that constructor for T
2834 could affect anybody inheriting from T
2835 who wants to initialize vtables for VFIELDS's type. */
2836 if (TREE_TYPE (vfields))
2837 TREE_ADDRESSABLE (vfields) = 1;
2838 vfields = TREE_CHAIN (vfields);
2840 if (any_default_members != 0)
2841 build_class_init_list (t);
2843 else if (TYPE_NEEDS_CONSTRUCTING (t))
2844 build_class_init_list (t);
2846 if (current_lang_name == lang_name_cplusplus)
2848 if (! CLASSTYPE_DECLARED_EXCEPTION (t))
2849 embrace_waiting_friends (t);
2851 /* Write out inline function definitions. */
2852 do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
2853 CLASSTYPE_INLINE_FRIENDS (t) = 0;
2856 if (CLASSTYPE_VSIZE (t) != 0)
2858 TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (CLASS_ASSOC_VTABLE (t)), vfield);
2860 if ((flag_this_is_variable & 1) == 0)
2862 tree vtbl_ptr = build_decl (VAR_DECL, get_identifier (VPTR_NAME),
2863 TREE_TYPE (vfield));
2864 TREE_REGDECL (vtbl_ptr) = 1;
2865 CLASSTYPE_VTBL_PTR (t) = vtbl_ptr;
2867 if (DECL_FIELD_CONTEXT (vfield) != t)
2869 tree assoc = assoc_value (DECL_FIELD_CONTEXT (vfield), t);
2870 tree offset = ASSOC_OFFSET (assoc);
2872 vfield = copy_node (vfield);
2874 if (! integer_zerop (offset))
2875 offset = convert_units (offset, BITS_PER_UNIT, 1);
2876 if (DECL_OFFSET (vfield))
2877 offset = genop (PLUS_EXPR, offset, build_int (DECL_OFFSET (vfield)));
2878 DECL_FIELD_CONTEXT (vfield) = t;
2879 DECL_OFFSET (vfield) = TREE_INT_CST_LOW (offset);
2880 CLASSTYPE_VFIELD (t) = vfield;
2884 /* Make the rtl for any new vtables we have created, and unmark
2885 the base types we marked. */
2886 unmark_finished_struct (t);
2888 /* Now out of this class's scope. However, if this class defined
2889 any new typedefs, then we must export those to the outer
2890 binding level. This is unpleasant. */
2891 x = gettags ();
2893 popclass (0);
2895 #if 0
2896 /* Remove aggregate types from the list of tags,
2897 since these appear at global scope. */
2898 while (x && IS_AGGR_TYPE (TREE_VALUE (x)))
2899 x = TREE_CHAIN (x);
2900 CLASSTYPE_TAGS (t) = x;
2901 y = x;
2902 while (x)
2904 if (IS_AGGR_TYPE (TREE_VALUE (x)))
2905 TREE_CHAIN (y) = TREE_CHAIN (x);
2906 x = TREE_CHAIN (x);
2908 #endif
2910 hack_incomplete_structures (t);
2912 resume_momentary (old);
2914 if (flag_cadillac)
2915 cadillac_finish_struct (t);
2917 return t;
2920 /* Return non-zero if the effective type of INSTANCE is static.
2921 Used to determine whether the virtual function table is needed
2922 or not. */
2924 resolves_to_fixed_type_p (instance)
2925 tree instance;
2927 switch (TREE_CODE (instance))
2929 case ADDR_EXPR:
2930 return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0));
2932 case COMPONENT_REF:
2933 /* Don't let pointers to members look like they hold a fixed type. */
2934 if (TREE_CODE (TREE_OPERAND (instance, 1)) != FIELD_DECL)
2935 return 0;
2937 case VAR_DECL:
2938 case PARM_DECL:
2939 case NEW_EXPR:
2940 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
2941 return 1;
2943 default:
2944 return 0;
2948 /* Ordering function for overload resolution. */
2950 rank_for_overload (x, y)
2951 struct candidate *x, *y;
2953 if (y->evil - x->evil)
2954 return y->evil - x->evil;
2955 if ((y->harshness[0] & 128) ^ (x->harshness[0] & 128))
2956 return y->harshness[0] - x->harshness[0];
2957 if (y->user - x->user)
2958 return y->user - x->user;
2959 if (y->b_or_d - x->b_or_d)
2960 return y->b_or_d - x->b_or_d;
2961 return y->easy - x->easy;
2964 /* TYPE is the type we wish to convert to. PARM is the parameter
2965 we have to work with. We use a somewhat arbitrary cost function
2966 to measure this conversion. */
2967 static int
2968 convert_harshness (type, parmtype, parm)
2969 register tree type, parmtype;
2970 tree parm;
2972 register enum tree_code codel = TREE_CODE (type);
2973 register enum tree_code coder = TREE_CODE (parmtype);
2975 #ifdef GATHER_STATISTICS
2976 n_convert_harshness++;
2977 #endif
2979 if (TYPE_MAIN_VARIANT (parmtype) == TYPE_MAIN_VARIANT (type))
2980 return 0;
2982 if (coder == ERROR_MARK)
2983 return 1;
2985 if (codel == POINTER_TYPE
2986 && (coder == METHOD_TYPE || coder == FUNCTION_TYPE))
2988 tree p1, p2;
2989 int harshness, new_harshness;
2991 /* Get to the METHOD_TYPE or FUNCTION_TYPE that this might be. */
2992 type = TREE_TYPE (type);
2994 if (coder != TREE_CODE (type))
2995 return 1;
2997 harshness = 0;
2999 /* We allow the default conversion between function type
3000 and pointer-to-function type for free. */
3001 if (type == parmtype)
3002 return 0;
3004 /* Compare return types. */
3005 harshness |= convert_harshness (TREE_TYPE (type), TREE_TYPE (parmtype), 0);
3006 if (harshness & 1)
3007 return 1;
3008 p1 = TYPE_ARG_TYPES (type);
3009 p2 = TYPE_ARG_TYPES (parmtype);
3010 while (p1 && p2)
3012 new_harshness = convert_harshness (TREE_VALUE (p1), TREE_VALUE (p2), 0);
3013 if (new_harshness & 1)
3014 return 1;
3015 if ((new_harshness & 7) == 0)
3016 harshness += new_harshness;
3017 else
3018 harshness |= new_harshness;
3019 p1 = TREE_CHAIN (p1);
3020 p2 = TREE_CHAIN (p2);
3022 if (p1 == p2)
3023 return harshness;
3024 if (p2)
3025 return 1;
3026 if (p1)
3027 return harshness | (TREE_PURPOSE (p1) == NULL_TREE);
3029 else if (codel == POINTER_TYPE && coder == OFFSET_TYPE)
3031 int harshness;
3033 /* Get to the OFFSET_TYPE that this might be. */
3034 type = TREE_TYPE (type);
3036 if (coder != TREE_CODE (type))
3037 return 1;
3039 harshness = 0;
3041 if (TYPE_OFFSET_BASETYPE (type) == TYPE_OFFSET_BASETYPE (parmtype))
3042 harshness = 0;
3043 else if (get_base_type (TYPE_OFFSET_BASETYPE (type),
3044 TYPE_OFFSET_BASETYPE (parmtype), 0))
3045 harshness = (1<<3);
3046 else
3047 return 1;
3048 /* Now test the OFFSET_TYPE's target compatability. */
3049 type = TREE_TYPE (type);
3050 parmtype = TREE_TYPE (parmtype);
3053 if (coder == UNKNOWN_TYPE)
3055 if (codel == FUNCTION_TYPE
3056 || codel == METHOD_TYPE
3057 || (codel == POINTER_TYPE
3058 && (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
3059 || TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)))
3060 return 0;
3061 return 1;
3064 if (coder == VOID_TYPE)
3065 return 1;
3067 if (codel == ENUMERAL_TYPE || codel == INTEGER_TYPE)
3069 /* Control equivalence of ints an enums. */
3071 if (codel == ENUMERAL_TYPE
3072 && flag_int_enum_equivalence == 0)
3074 /* Enums can be converted to ints, but not vice-versa. */
3075 if (coder != ENUMERAL_TYPE
3076 || TYPE_MAIN_VARIANT (type) != TYPE_MAIN_VARIANT (parmtype))
3077 return 1;
3080 /* else enums and ints (almost) freely interconvert. */
3082 if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
3084 int easy = TREE_UNSIGNED (type) ^ TREE_UNSIGNED (parmtype);
3085 if (codel != coder)
3086 easy += 1;
3087 if (TYPE_MODE (type) != TYPE_MODE (parmtype))
3088 easy += 2;
3089 return (easy << 4);
3091 else if (coder == REAL_TYPE)
3092 return (4<<4);
3095 if (codel == REAL_TYPE)
3096 if (coder == REAL_TYPE)
3097 /* Shun converting between float and double if a choice exists. */
3099 if (TYPE_MODE (type) != TYPE_MODE (parmtype))
3100 return (2<<4);
3101 return 0;
3103 else if (coder == INTEGER_TYPE || coder == ENUMERAL_TYPE)
3104 return (4<<4);
3106 /* convert arrays which have not previously been converted. */
3107 if (codel == ARRAY_TYPE)
3108 codel = POINTER_TYPE;
3109 if (coder == ARRAY_TYPE)
3110 coder = POINTER_TYPE;
3112 /* Conversions among pointers */
3113 if (codel == POINTER_TYPE && coder == POINTER_TYPE)
3115 register tree ttl = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3116 register tree ttr = TYPE_MAIN_VARIANT (TREE_TYPE (parmtype));
3117 int penalty = 4 * (ttl != ttr);
3118 /* Anything converts to void *. void * converts to anything.
3119 Since these may be `const void *' (etc.) use VOID_TYPE
3120 instead of void_type_node.
3121 Otherwise, the targets must be the same,
3122 except that we do allow (at some cost) conversion
3123 between signed and unsinged pointer types. */
3125 if ((TREE_CODE (ttl) == METHOD_TYPE
3126 || TREE_CODE (ttl) == FUNCTION_TYPE)
3127 && TREE_CODE (ttl) == TREE_CODE (ttr))
3129 if (comptypes (ttl, ttr, -1))
3130 return penalty<<4;
3131 return 1;
3134 if (!(TREE_CODE (ttl) == VOID_TYPE
3135 || TREE_CODE (ttr) == VOID_TYPE
3136 || (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (ttr)
3137 && (ttl = unsigned_type (ttl),
3138 ttr = unsigned_type (ttr),
3139 penalty = 10, 0))
3140 || (comp_target_types (ttl, ttr, 0))))
3141 return 1;
3143 if (ttr == ttl)
3144 return 4;
3146 if (IS_AGGR_TYPE (ttl) && IS_AGGR_TYPE (ttr))
3148 int b_or_d = get_base_distance (ttl, ttr, 0, 0);
3149 if (b_or_d < 0)
3150 return 1;
3151 return (b_or_d<<3) | 4;
3154 return (penalty<<4);
3157 if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
3159 /* This is not a bad match, but don't let it beat
3160 integer-enum combinations. */
3161 if (parm && integer_zerop (parm))
3162 return (4<<4);
3165 /* C++: one of the types must be a reference type. */
3167 tree ttl, ttr;
3168 register tree intype = TYPE_MAIN_VARIANT (parmtype);
3169 register enum tree_code form = TREE_CODE (intype);
3170 int penalty;
3172 if (codel == REFERENCE_TYPE || coder == REFERENCE_TYPE)
3174 ttl = TYPE_MAIN_VARIANT (type);
3176 if (codel == REFERENCE_TYPE)
3178 ttl = TYPE_MAIN_VARIANT (TREE_TYPE (ttl));
3180 if (form == OFFSET_TYPE)
3182 intype = TREE_TYPE (intype);
3183 form = TREE_CODE (intype);
3186 if (form == REFERENCE_TYPE)
3188 intype = TYPE_MAIN_VARIANT (TREE_TYPE (intype));
3190 if (ttl == intype)
3191 return 0;
3192 penalty = 2;
3194 else
3196 /* Can reference be built up? */
3197 if (ttl == intype)
3199 return 0;
3201 else
3202 penalty = 2;
3205 else if (form == REFERENCE_TYPE)
3207 if (parm)
3209 tree tmp = convert_from_reference (parm);
3210 intype = TYPE_MAIN_VARIANT (TREE_TYPE (tmp));
3212 else
3214 intype = parmtype;
3217 intype = TREE_TYPE (intype);
3219 while (TREE_CODE (intype) == REFERENCE_TYPE);
3220 intype = TYPE_MAIN_VARIANT (intype);
3223 if (ttl == intype)
3224 return 0;
3225 else
3226 penalty = 2;
3229 if (TREE_UNSIGNED (ttl) ^ TREE_UNSIGNED (intype))
3231 ttl = unsigned_type (ttl);
3232 intype = unsigned_type (intype);
3233 penalty += 2;
3236 ttr = intype;
3238 /* If the initializer is not an lvalue, then it does not
3239 matter if we make life easier for the programmer
3240 by creating a temporary variable with which to
3241 hold the result. */
3242 if (parm && (coder == INTEGER_TYPE
3243 || coder == ENUMERAL_TYPE
3244 || coder == REAL_TYPE)
3245 && ! lvalue_p (parm))
3246 return (convert_harshness (ttl, ttr, 0) | (penalty << 4));
3248 if (ttl == ttr)
3249 return 4;
3251 /* Pointers to voids always convert for pointers. But
3252 make them less natural than more specific matches. */
3253 if (TREE_CODE (ttl) == POINTER_TYPE && TREE_CODE (ttr) == POINTER_TYPE)
3254 if (TREE_TYPE (ttl) == void_type_node
3255 || TREE_TYPE (ttr) == void_type_node)
3256 return ((penalty+1)<<4);
3258 if (parm && codel != REFERENCE_TYPE)
3259 return (convert_harshness (ttl, ttr, 0) | (penalty << 4));
3261 /* Here it does matter. If this conversion is from
3262 derived to base, allow it. Otherwise, types must
3263 be compatible in the strong sense. */
3264 if (IS_AGGR_TYPE (ttl) && IS_AGGR_TYPE (ttr))
3266 int b_or_d = get_base_distance (ttl, ttr, 0, 0);
3267 if (b_or_d < 0)
3268 return 1;
3269 #if AMBIGUOUS_WORKING
3270 if (ttl == TYPE_MAIN_VARIANT (type)
3271 && TYPE_GETS_INIT_REF (type))
3272 return (b_or_d<<3) | 6;
3273 #endif
3274 return (b_or_d<<3) | 4;
3277 if (comp_target_types (ttl, intype, 1))
3278 return (penalty<<4);
3281 if (codel == RECORD_TYPE && coder == RECORD_TYPE)
3283 int b_or_d = get_base_distance (type, parmtype, 0, 0);
3284 if (b_or_d < 0)
3285 return 1;
3286 #if AMBIGUOUS_WORKING
3287 if (TYPE_GETS_INIT_REF (type))
3288 return (b_or_d<<3) | 6;
3289 #endif
3290 return (b_or_d<<3) | 4;
3292 return 1;
3295 /* Algorithm: Start out with no stikes against. For each argument
3296 which requires a (subjective) hard conversion (such as between
3297 floating point and integer), issue a strike. If there are the same
3298 number of formal and actual parameters in the list, there will be at
3299 least on strike, otherwise an exact match would have been found. If
3300 there are not the same number of arguments in the type lists, we are
3301 not dead yet: a `...' means that we can have more parms then were
3302 declared, and if we wind up in the default argument section of the
3303 list those can be used as well. If an exact match could be found for
3304 one of those cases, return it immediately. Otherwise, Rank the fields
3305 so that fields with fewer strikes are tried first.
3307 Conversions between builtin and user-defined types are allowed, but
3308 no function involving such a conversion is prefered to one which
3309 does not require such a conversion. Furthermore, such conversions
3310 must be unique. */
3312 void
3313 compute_conversion_costs (function, tta_in, cp, arglen)
3314 tree function;
3315 tree tta_in;
3316 struct candidate *cp;
3317 int arglen;
3319 tree ttf_in = TYPE_ARG_TYPES (TREE_TYPE (function));
3320 tree ttf = ttf_in;
3321 tree tta = tta_in;
3323 /* Start out with no strikes against. */
3324 int evil_strikes = 0;
3325 int user_strikes = 0;
3326 int b_or_d_strikes = 0;
3327 int easy_strikes = 0;
3329 int strike_index = 0, win, lose;
3331 #ifdef GATHER_STATISTICS
3332 n_compute_conversion_costs++;
3333 #endif
3335 cp->function = function;
3336 cp->arg = tta ? TREE_VALUE (tta) : NULL_TREE;
3337 cp->u.bad_arg = 0; /* optimistic! */
3339 bzero (cp->harshness, (arglen+1) * sizeof (short));
3341 while (ttf && tta)
3343 int harshness;
3345 if (ttf == void_list_node)
3346 break;
3348 if (type_unknown_p (TREE_VALUE (tta)))
3350 /* Must perform some instantiation here. */
3351 tree rhs = TREE_VALUE (tta);
3352 tree lhstype = TREE_VALUE (ttf);
3354 /* @@ This is to undo what `grokdeclarator' does to
3355 parameter types. It really should go through
3356 something more general. */
3358 TREE_TYPE (tta) = unknown_type_node;
3359 if (TREE_CODE (rhs) == OP_IDENTIFIER)
3360 rhs = build_instantiated_decl (lhstype, rhs);
3361 else
3363 /* Keep quiet about possible contravariance violations. */
3364 extern int inhibit_warnings;
3365 int old_inhibit_warnings = inhibit_warnings;
3366 inhibit_warnings = 1;
3368 rhs = instantiate_type (lhstype, rhs, 0);
3370 inhibit_warnings = old_inhibit_warnings;
3373 if (TREE_CODE (rhs) == ERROR_MARK)
3374 harshness = 1;
3375 else
3377 harshness = convert_harshness (lhstype, TREE_TYPE (rhs), rhs);
3378 /* harshness |= 2; */
3381 else
3382 harshness = convert_harshness (TREE_VALUE (ttf), TREE_TYPE (TREE_VALUE (tta)), TREE_VALUE (tta));
3384 cp->harshness[strike_index] = harshness;
3385 if (harshness & 1)
3387 cp->u.bad_arg = strike_index;
3388 evil_strikes = 1;
3390 else if (harshness & 2)
3392 user_strikes += 1;
3394 else if (harshness & 4)
3396 b_or_d_strikes += (harshness >> 3);
3398 else
3399 easy_strikes += harshness >> 4;
3400 ttf = TREE_CHAIN (ttf);
3401 tta = TREE_CHAIN (tta);
3402 strike_index += 1;
3405 if (tta)
3407 /* ran out of formals, and parmlist is fixed size. */
3408 if (ttf /* == void_type_node */)
3410 cp->evil = 1;
3411 cp->u.bad_arg = -1;
3412 return;
3415 else if (ttf && ttf != void_list_node)
3417 /* ran out of actuals, and no defaults. */
3418 if (TREE_PURPOSE (ttf) == NULL_TREE)
3420 cp->evil = 1;
3421 cp->u.bad_arg = -2;
3422 return;
3424 /* Store index of first default. */
3425 cp->harshness[arglen] = strike_index+1;
3427 else cp->harshness[arglen] = 0;
3429 /* Argument list lengths work out, so don't need to check them again. */
3430 if (evil_strikes)
3432 /* We do not check for derived->base conversions here, since in
3433 no case would they give evil strike counts, unless such conversions
3434 are somehow ambiguous. */
3436 /* See if any user-defined conversions apply.
3437 But make sure that we do not loop. */
3438 static int dont_convert_types = 0;
3440 if (dont_convert_types)
3442 cp->evil = 1;
3443 return;
3446 win = 0; /* Only get one chance to win. */
3447 ttf = TYPE_ARG_TYPES (TREE_TYPE (function));
3448 tta = tta_in;
3449 strike_index = 0;
3450 evil_strikes = 0;
3452 while (ttf && tta)
3454 if (ttf == void_list_node)
3455 break;
3457 lose = cp->harshness[strike_index];
3458 if (lose&1)
3460 tree actual_type = TREE_TYPE (TREE_VALUE (tta));
3461 tree formal_type = TREE_VALUE (ttf);
3463 dont_convert_types = 1;
3465 if (TREE_CODE (formal_type) == REFERENCE_TYPE)
3466 formal_type = TREE_TYPE (formal_type);
3467 if (TREE_CODE (actual_type) == REFERENCE_TYPE)
3468 actual_type = TREE_TYPE (actual_type);
3470 if (formal_type != error_mark_node
3471 && actual_type != error_mark_node)
3473 formal_type = TYPE_MAIN_VARIANT (formal_type);
3474 actual_type = TYPE_MAIN_VARIANT (actual_type);
3476 if (TYPE_HAS_CONSTRUCTOR (formal_type))
3478 /* If it has a constructor for this type, try to use it. */
3479 if (convert_to_aggr (formal_type, TREE_VALUE (tta), 0, 1)
3480 != error_mark_node)
3482 /* @@ There is no way to save this result yet.
3483 @@ So success is NULL_TREE for now. */
3484 win++;
3487 if (TYPE_LANG_SPECIFIC (actual_type) && TYPE_HAS_CONVERSION (actual_type))
3489 if (TREE_CODE (formal_type) == INTEGER_TYPE
3490 && TYPE_HAS_INT_CONVERSION (actual_type))
3491 win++;
3492 else if (TREE_CODE (formal_type) == REAL_TYPE
3493 && TYPE_HAS_REAL_CONVERSION (actual_type))
3494 win++;
3495 else
3497 tree conv = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_VALUE (tta), 0);
3498 if (conv)
3500 if (conv == error_mark_node)
3501 win += 2;
3502 else
3503 win++;
3505 else if (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE)
3507 conv = build_type_conversion (CALL_EXPR, formal_type, TREE_VALUE (tta), 0);
3508 if (conv)
3510 if (conv == error_mark_node)
3511 win += 2;
3512 else
3513 win++;
3519 dont_convert_types = 0;
3521 if (win == 1)
3523 user_strikes += 1;
3524 cp->harshness[strike_index] = 2;
3525 win = 0;
3527 else
3529 if (cp->u.bad_arg > strike_index)
3530 cp->u.bad_arg = strike_index;
3532 evil_strikes = win ? 2 : 1;
3533 break;
3537 ttf = TREE_CHAIN (ttf);
3538 tta = TREE_CHAIN (tta);
3539 strike_index += 1;
3543 /* Calling a non-const member function from a const member function
3544 is probably invalid, but for now we let it only draw a warning.
3545 We indicate that such a mismatch has occured by setting the
3546 harshness to a maximum value. */
3547 if (TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE
3548 && TREE_CODE (TREE_TYPE (TREE_VALUE (tta_in))) == POINTER_TYPE
3549 && (TREE_READONLY (TREE_TYPE (TREE_TYPE (TREE_VALUE (tta_in))))
3550 > TREE_READONLY (TREE_TYPE (TREE_VALUE (ttf_in)))))
3551 cp->harshness[0] |= 128;
3553 cp->evil = evil_strikes;
3554 cp->user = user_strikes;
3555 cp->b_or_d = b_or_d_strikes;
3556 cp->easy = easy_strikes;
3559 struct candidate *
3560 ideal_candidate (basetype, candidates, n_candidates, parms, len)
3561 tree basetype;
3562 struct candidate *candidates;
3563 int n_candidates;
3564 tree parms;
3565 int len;
3567 struct candidate *cp = candidates + n_candidates;
3568 int index, i;
3569 tree ttf;
3571 qsort (candidates, /* char *base */
3572 n_candidates, /* int nel */
3573 sizeof (struct candidate), /* int width */
3574 rank_for_overload); /* int (*compar)() */
3576 /* If the best candidate requires user-defined conversions,
3577 and its user-defined conversions are a strict subset
3578 of all other candidates requiring user-defined conversions,
3579 then it is, in fact, the best. */
3580 for (i = -1; cp + i != candidates; i--)
3581 if (cp[i].user == 0)
3582 break;
3584 if (i < -1)
3586 tree ttf0;
3588 /* Check that every other candidate requires those conversions
3589 as a strict subset of their conversions. */
3590 if (cp[i].user == cp[-1].user)
3591 goto non_subset;
3593 /* Look at subset relationship more closely. */
3594 while (i != -1)
3596 for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)),
3597 ttf0 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function)),
3598 index = 0;
3599 index < len;
3600 ttf = TREE_CHAIN (ttf), ttf0 = TREE_CHAIN (ttf0), index++)
3601 if (cp[i].harshness[index] & 2)
3603 /* If our "best" candidate also needs a conversion,
3604 it must be the same one. */
3605 if ((cp[-1].harshness[index] & 2)
3606 && TREE_VALUE (ttf) != TREE_VALUE (ttf0))
3607 goto non_subset;
3609 i++;
3611 /* The best was the best. */
3612 return cp - 1;
3613 non_subset:
3614 /* Use other rules for determining "bestness". */
3618 /* If the best two candidates we find require user-defined
3619 conversions, we may need to report and error message. */
3620 if (cp[-1].user && cp[-2].user
3621 && (cp[-1].b_or_d || cp[-2].b_or_d == 0))
3623 /* If the best two methods found involved user-defined
3624 type conversions, then we must see whether one
3625 of them is exactly what we wanted. If not, then
3626 we have an ambiguity. */
3627 int best = 0;
3628 tree tta = parms;
3629 tree f1, p1;
3631 #if AMBIGUOUS_WORKING
3632 if (cp[-1].b_or_d == 0
3633 && cp[-1].easy == 0
3634 && (cp[-2].b_or_d | cp[-2].easy) > 0)
3635 return cp - 1;
3636 #endif
3638 /* Stash all of our parameters in safe places
3639 so that we can perform type conversions in place. */
3640 while (tta)
3642 TREE_PURPOSE (tta) = TREE_VALUE (tta);
3643 tta = TREE_CHAIN (tta);
3646 i = 0;
3649 int exact_conversions = 0;
3651 i -= 1;
3652 tta = parms;
3653 if (DECL_STATIC_FUNCTION_P (cp[i].function))
3654 tta = TREE_CHAIN (tta);
3655 for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[i].function)), index = 0;
3656 index < len;
3657 tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
3659 if (cp[i].harshness[index] & 2)
3661 TREE_VALUE (tta)
3662 = build_type_conversion (CALL_EXPR, TREE_VALUE (ttf), TREE_PURPOSE (tta), 2);
3663 if (TREE_VALUE (tta))
3665 if (TREE_CODE (TREE_VALUE (tta)) != CONVERT_EXPR
3666 && (TREE_CODE (TREE_VALUE (tta)) != NOP_EXPR
3667 || comp_target_types (TREE_TYPE (TREE_VALUE (tta)),
3668 TREE_TYPE (TREE_OPERAND (TREE_VALUE (tta), 0)), 1)))
3669 exact_conversions += 1;
3671 else if (IS_AGGR_TYPE (TREE_VALUE (ttf))
3672 || (TREE_CODE (TREE_VALUE (ttf)) == REFERENCE_TYPE
3673 && IS_AGGR_TYPE (TREE_TYPE (TREE_VALUE (ttf)))))
3675 /* To get here we had to have succeeded via
3676 a constructor. */
3677 TREE_VALUE (tta) = TREE_PURPOSE (tta);
3678 exact_conversions += 1;
3682 if (exact_conversions == cp[i].user)
3684 if (best == 0)
3686 best = i;
3687 f1 = cp[best].function;
3688 p1 = TYPE_ARG_TYPES (TREE_TYPE (f1));
3690 else
3692 /* Don't complain if next best is from base class. */
3693 tree f2 = cp[i].function;
3694 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (f2));
3696 if (TREE_CODE (TREE_TYPE (f1)) == METHOD_TYPE
3697 && TREE_CODE (TREE_TYPE (f2)) == METHOD_TYPE
3698 && (cp[i].harshness[0] & 4) != 0
3699 && cp[best].harshness[0] < cp[i].harshness[0])
3701 #if 0
3702 /* For LUCID. */
3703 if (! compparms (TREE_CHAIN (p1), TREE_CHAIN (p2), 1))
3704 goto ret0;
3705 else
3706 #endif
3707 continue;
3709 else goto ret0;
3712 } while (cp + i != candidates);
3714 if (best)
3716 int exact_conversions = cp[best].user;
3717 tta = parms;
3718 if (DECL_STATIC_FUNCTION_P (cp[best].function))
3719 tta = TREE_CHAIN (parms);
3720 for (ttf = TYPE_ARG_TYPES (TREE_TYPE (cp[best].function)), index = 0;
3721 exact_conversions > 0;
3722 tta = TREE_CHAIN (tta), ttf = TREE_CHAIN (ttf), index++)
3724 if (cp[best].harshness[index] & 2)
3726 /* We must now fill in the slot we left behind.
3727 @@ This could be optimized to use the value previously
3728 @@ computed by build_type_conversion in some cases. */
3729 TREE_VALUE (tta) = convert (TREE_VALUE (ttf), TREE_PURPOSE (tta));
3730 exact_conversions -= 1;
3732 else TREE_VALUE (tta) = TREE_PURPOSE (tta);
3734 return cp + best;
3736 goto ret0;
3738 /* If the best two candidates we find both use default parameters,
3739 we may need to report and error. Don't need to worry if next-best
3740 candidate is forced to use user-defined conversion when best is not. */
3741 if (cp[-2].user == 0
3742 && cp[-1].harshness[len] != 0 && cp[-2].harshness[len] != 0)
3744 tree tt1 = TYPE_ARG_TYPES (TREE_TYPE (cp[-1].function));
3745 tree tt2 = TYPE_ARG_TYPES (TREE_TYPE (cp[-2].function));
3746 int i = cp[-1].harshness[len];
3747 if (cp[-2].harshness[len] < i)
3748 i = cp[-2].harshness[len];
3749 while (--i > 0)
3751 if (TYPE_MAIN_VARIANT (TREE_VALUE (tt1))
3752 != TYPE_MAIN_VARIANT (TREE_VALUE (tt2)))
3753 /* These lists are not identical, so we can choose our best candidate. */
3754 return cp - 1;
3755 tt1 = TREE_CHAIN (tt1);
3756 tt2 = TREE_CHAIN (tt2);
3758 /* To get here, both lists had the same parameters up to the defaults
3759 which were used. This is an ambiguous request. */
3760 goto ret0;
3763 /* Otherwise, return our best candidate. Note that if we get candidates
3764 from independent base classes, we have an ambiguity, even if one
3765 argument list look a little better than another one. */
3766 if (cp[-1].b_or_d && basetype && TYPE_USES_MULTIPLE_INHERITANCE (basetype))
3768 int i = n_candidates - 1, best;
3769 tree base1 = NULL_TREE;
3771 if (TREE_CODE (TREE_TYPE (candidates[i].function)) == FUNCTION_TYPE)
3772 return cp - 1;
3774 for (; i >= 0 && candidates[i].user == 0 && candidates[i].evil == 0; i--)
3776 if (TREE_CODE (TREE_TYPE (candidates[i].function)) == METHOD_TYPE)
3778 tree newbase = TYPE_METHOD_BASETYPE (TREE_TYPE (candidates[i].function));
3780 if (base1 != NULL_TREE)
3782 if (newbase != base1
3783 && ! get_base_type (newbase, base1, 0))
3785 char *buf = (char *)alloca (8192);
3786 error ("ambiguous request for function from distinct base classes of type `%s'", TYPE_NAME_STRING (basetype));
3787 error ("first candidate is `%s'", fndecl_as_string (buf, 0, candidates[best].function, 1));
3788 error ("second candidates is `%s'", fndecl_as_string (buf, 0, candidates[i].function, 1));
3789 return cp - 1;
3792 else
3794 best = i;
3795 base1 = newbase;
3798 else return cp - 1;
3802 #if AMBIGUOUS_WORKING
3803 if (cp[-1].user == cp[-2].user
3804 && cp[-1].b_or_d == cp[-2].b_or_d
3805 && cp[-1].easy == cp[-2].easy)
3806 goto ret0;
3807 #endif
3809 return cp - 1;
3811 ret0:
3812 /* In the case where there is no ideal candidate, restore
3813 TREE_VALUE slots of PARMS from TREE_PURPOSE slots. */
3814 while (parms)
3816 TREE_VALUE (parms) = TREE_PURPOSE (parms);
3817 parms = TREE_CHAIN (parms);
3819 return 0;
3822 /* Assume that if the class referred to is not in the
3823 current class hierarchy, that it may be remote.
3824 PARENT is assumed to be of aggregate type here. */
3825 static int
3826 may_be_remote (parent)
3827 tree parent;
3829 if (TYPE_OVERLOADS_METHOD_CALL_EXPR (parent) == 0)
3830 return 0;
3832 if (current_class_type == NULL_TREE)
3833 return 0;
3834 if (parent == current_class_type)
3835 return 0;
3837 if (get_base_type (parent, current_class_type, 0))
3838 return 0;
3839 return 1;
3842 /* Return the number of bytes that the arglist in PARMS would
3843 occupy on the stack. */
3845 get_arglist_len_in_bytes (parms)
3846 tree parms;
3848 register tree parm;
3849 register int bytecount = 0;
3851 for (parm = parms; parm; parm = TREE_CHAIN (parm))
3853 register tree pval = TREE_VALUE (parm);
3854 register int used, size;
3856 if (TREE_CODE (pval) == ERROR_MARK)
3857 continue;
3858 else if (TYPE_MODE (TREE_TYPE (pval)) != BLKmode)
3860 used = size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (pval)));
3861 #ifdef PUSH_ROUNDING
3862 size = PUSH_ROUNDING (size);
3863 #endif
3864 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
3865 / (PARM_BOUNDARY / BITS_PER_UNIT))
3866 * (PARM_BOUNDARY / BITS_PER_UNIT));
3868 else
3870 register tree size = size_in_bytes (TREE_TYPE (pval));
3871 register tree used_t = convert_units (convert_units (size, BITS_PER_UNIT, PARM_BOUNDARY),
3872 PARM_BOUNDARY, BITS_PER_UNIT);
3873 used = TREE_INT_CST_LOW (used_t);
3875 bytecount += used;
3877 return bytecount;
3880 tree
3881 build_vfield_ref (datum, type)
3882 tree datum, type;
3884 if (TREE_CODE (TREE_TYPE (datum)) == REFERENCE_TYPE)
3885 datum = convert_from_reference (datum);
3887 if (! TYPE_USES_VIRTUAL_BASECLASSES (type))
3888 return build (COMPONENT_REF, TREE_TYPE (CLASSTYPE_VFIELD (type)),
3889 datum, CLASSTYPE_VFIELD (type));
3890 return build_component_ref (datum, DECL_NAME (CLASSTYPE_VFIELD (type)), 0, 0);
3893 /* Build a call to a member of an object. I.e., one that overloads
3894 operator ()(), or is a pointer-to-function or pointer-to-method. */
3895 static tree
3896 build_field_call (basetype_path, instance_ptr, name, parms, err_name)
3897 tree basetype_path;
3898 tree instance_ptr, name, parms;
3899 char *err_name;
3901 tree field, instance;
3903 if (instance_ptr == current_class_decl)
3905 /* Check to see if we really have a reference to an instance variable
3906 with `operator()()' overloaded. */
3907 #if 1
3908 field = IDENTIFIER_CLASS_VALUE (name);
3909 #else
3910 field = identifier_class_value (name);
3911 #endif
3913 if (field == NULL_TREE)
3915 error ("`this' has no member named `%s'", err_name);
3916 return error_mark_node;
3919 if (TREE_CODE (field) == FIELD_DECL)
3921 /* If it's a field, try overloading operator (),
3922 or calling if the field is a pointer-to-function. */
3923 instance = build_component_ref_1 (C_C_D, field, 0, 1);
3924 if (instance == error_mark_node)
3925 return error_mark_node;
3927 if (TYPE_LANG_SPECIFIC (TREE_TYPE (instance))
3928 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (instance)))
3929 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, instance, parms);
3931 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
3932 if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == FUNCTION_TYPE)
3933 return build_function_call (instance, parms);
3934 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (instance))) == METHOD_TYPE)
3935 return build_function_call (instance, tree_cons (NULL_TREE, current_class_decl, parms));
3937 return NULL_TREE;
3940 /* Check to see if this is not really a reference to an instance variable
3941 with `operator()()' overloaded. */
3942 field = lookup_field (basetype_path, name, 1);
3944 /* This can happen if the reference was ambiguous
3945 or for visibility violations. */
3946 if (field == error_mark_node)
3947 return error_mark_node;
3948 if (field)
3950 tree basetype;
3951 tree ftype = TREE_TYPE (field);
3953 if (TYPE_LANG_SPECIFIC (ftype) && TYPE_OVERLOADS_CALL_EXPR (ftype))
3955 /* Make the next search for this field very short. */
3956 basetype = DECL_FIELD_CONTEXT (field);
3957 instance_ptr = convert_pointer_to (basetype, instance_ptr);
3959 instance = build_indirect_ref (instance_ptr, 0);
3960 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL,
3961 build_component_ref_1 (instance, field, 0, 0),
3962 parms);
3964 if (TREE_CODE (ftype) == POINTER_TYPE)
3966 if (TREE_CODE (TREE_TYPE (ftype)) == FUNCTION_TYPE
3967 || TREE_CODE (TREE_TYPE (ftype)) == METHOD_TYPE)
3969 /* This is a member which is a pointer to function. */
3970 tree ref = build_component_ref_1 (build_indirect_ref (instance_ptr, 0, 0),
3971 field, LOOKUP_COMPLAIN);
3972 if (ref == error_mark_node)
3973 return error_mark_node;
3974 return build_function_call (ref, parms);
3977 else if (TREE_CODE (ftype) == METHOD_TYPE)
3979 error ("invalid call via pointer-to-member function");
3980 return error_mark_node;
3982 else
3983 return NULL_TREE;
3985 return NULL_TREE;
3988 /* Build a method call of the form `EXP->SCOPES::NAME (PARMS)'.
3989 This is how virtual function calls are avoided. */
3990 tree
3991 build_scoped_method_call (exp, scopes, name, parms)
3992 tree exp;
3993 tree scopes;
3994 tree name;
3995 tree parms;
3997 /* Because this syntactic form does not allow
3998 a pointer to a base class to be `stolen',
3999 we need not protect the drived->base conversion
4000 that happens here.
4002 @@ But we do have to check visibility privileges later. */
4003 tree basename = (TREE_CODE (scopes) == SCOPE_REF) ? TREE_OPERAND (scopes, 1) : scopes;
4004 tree basetype, decl;
4005 tree type = TREE_TYPE (exp);
4007 if (type == error_mark_node
4008 || ! is_aggr_typedef (basename, 1))
4009 return error_mark_node;
4011 if (! IS_AGGR_TYPE (type))
4013 error ("base object of scoped method call is not of aggregate type");
4014 return error_mark_node;
4017 basetype = TREE_TYPE (TREE_TYPE (basename));
4019 if (basetype = basetype_or_else (basetype, type))
4021 if (basetype == error_mark_node)
4022 return error_mark_node;
4023 if (TREE_CODE (exp) == INDIRECT_REF)
4024 decl = build_indirect_ref (convert_pointer_to (basetype,
4025 build_unary_op (ADDR_EXPR, exp, 0)), 0);
4026 else
4027 decl = build_scoped_ref (exp, scopes);
4029 /* Call to a destructor. */
4030 if (TREE_CODE (name) == BIT_NOT_EXPR)
4032 /* Explicit call to destructor. */
4033 name = TREE_OPERAND (name, 0);
4034 if (! is_aggr_typedef (name, 1))
4035 return error_mark_node;
4036 if (TREE_TYPE (decl) != TREE_TYPE (TREE_TYPE (name)))
4038 error_with_aggr_type (TREE_TYPE (decl),
4039 "qualified type `%s' does not match destructor type `%s'",
4040 IDENTIFIER_POINTER (name));
4041 return error_mark_node;
4043 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (decl)))
4044 error_with_aggr_type (TREE_TYPE (decl), "type `%s' has no destructor");
4045 return build_delete (TREE_TYPE (decl), decl, integer_two_node,
4046 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
4049 /* Call to a method. */
4050 return build_method_call (decl, name, parms, NULL_TREE,
4051 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
4053 return error_mark_node;
4056 /* Build something of the form ptr->method (args)
4057 or object.method (args). This can also build
4058 calls to constructors, and find friends.
4060 Member functions always take their class variable
4061 as a pointer.
4063 INSTANCE is a class instance.
4065 NAME is the NAME field of the struct, union, or class
4066 whose type is that of INSTANCE.
4068 PARMS help to figure out what that NAME really refers to.
4070 BASETYPE_PATH, if non-NULL, tells which basetypes of INSTANCE
4071 we should be traversed before starting our search. We need
4072 this information to get protected accesses correct.
4074 FLAGS is the logical disjunction of zero or more LOOKUP_
4075 flags. See cplus-tree.h for more info.
4077 If this is all OK, calls build_function_call with the resolved
4078 member function.
4080 This function must also handle being called to perform
4081 initialization, promotion/coercion of arguments, and
4082 instantiation of default parameters.
4084 Note that NAME may refer to an instance variable name. If
4085 `operator()()' is defined for the type of that field, then we return
4086 that result. */
4087 tree
4088 build_method_call (instance, name, parms, basetype_path, flags)
4089 tree instance, name, parms, basetype_path;
4090 int flags;
4092 register tree function, fntype, value_type;
4093 register tree basetype, save_basetype;
4094 register tree baselink, result, method_name, parmtypes, parm;
4095 tree last;
4096 int pass;
4097 enum visibility_type visibility;
4098 int rank_for_overload ();
4100 /* Range of cases for vtable optimization. */
4101 enum vtable_needs
4103 not_needed, maybe_needed, unneeded, needed,
4105 enum vtable_needs need_vtbl = not_needed;
4107 char *err_name;
4108 char *name_kind;
4109 int ever_seen = 0;
4110 int wrap;
4111 tree wrap_type;
4112 tree instance_ptr = NULL_TREE;
4113 int all_virtual = flag_all_virtual;
4114 int static_call_context;
4115 tree saw_private = 0;
4116 tree saw_protected = 0;
4117 #ifdef SOS
4118 /* If call is a call to a constructor, then `dtbl'
4119 will first be initialized with the function table pointer
4120 of the appropriate type (calling "sosFindCode" as a last
4121 resort), the the call to the constructor will go through there. */
4122 tree dtbl = (flags & LOOKUP_DYNAMIC) ? TREE_VALUE (parms) : NULL_TREE;
4124 /* Flag saying whether or not `dtbl' has been inserted into the
4125 parameter list. This is needed because we cannot tell (until
4126 we have a match) whether this parameter should go in or not.
4128 If 1, then `dtbl' is living naturally.
4129 If 0, then `dtbl' is not among the parms that we know about.
4130 If -1, the `dtbl' was place into the parms unnaturally.
4132 Note that we may side-effect the parameter list, but in such a way
4133 that the caller of this function would never know. */
4134 int dtbl_inserted = (flags & LOOKUP_DYNAMIC);
4135 #endif
4137 /* Keep track of `const' and `volatile' objects. */
4138 int constp, volatilep;
4140 /* Know if this is explicit destructor call. */
4141 int dtor_specd = 0;
4143 #ifdef GATHER_STATISTICS
4144 n_build_method_call++;
4145 #endif
4147 if (instance == error_mark_node
4148 || name == error_mark_node
4149 || parms == error_mark_node
4150 || (instance != 0 && TREE_TYPE (instance) == error_mark_node))
4151 return error_mark_node;
4153 #if 0
4154 /* C++ 2.1 does not allow this, but ANSI probably will. */
4155 if (TREE_CODE (name) == BIT_NOT_EXPR)
4157 error ("invalid call to destructor, use qualified name `%s::~%s'",
4158 IDENTIFIER_POINTER (name), IDENTIFIER_POINTER (name));
4159 return error_mark_node;
4161 #else
4162 if (TREE_CODE (name) == BIT_NOT_EXPR)
4164 flags |= LOOKUP_DESTRUCTOR;
4165 name = TREE_OPERAND (name, 0);
4166 if (! is_aggr_typedef (name, 1))
4167 return error_mark_node;
4168 if (parms)
4169 error ("destructors take no parameters");
4170 basetype = TREE_TYPE (TREE_TYPE (name));
4171 if (! TYPE_HAS_DESTRUCTOR (basetype))
4172 error_with_aggr_type (basetype, "type `%s' has no destructor");
4173 instance = default_conversion (instance);
4174 if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
4175 instance_ptr = instance;
4176 else
4177 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
4178 return build_delete (basetype, instance_ptr, integer_two_node,
4179 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
4181 #endif
4183 if (TREE_CODE (name) == WRAPPER_EXPR)
4185 wrap_type = TREE_OPERAND (name, 0);
4186 name = TREE_OPERAND (name, 1);
4187 wrap = 1;
4189 else if (TREE_CODE (name) == ANTI_WRAPPER_EXPR)
4191 wrap_type = TREE_OPERAND (name, 0);
4192 name = TREE_OPERAND (name, 1);
4193 wrap = -1;
4195 else
4197 wrap_type = NULL_TREE;
4198 wrap = 0;
4201 /* Initialize name for error reporting. */
4202 if (TREE_CODE (name) == OP_IDENTIFIER)
4203 name = build_operator_fnname (&name, parms, 1);
4205 if (OPERATOR_NAME_P (name))
4207 char *p = operator_name_string (name);
4208 err_name = (char *)alloca (strlen (p) + 10);
4209 sprintf (err_name, "operator %s", p);
4211 else if (name == wrapper_name)
4212 err_name = "wrapper";
4213 else if (OPERATOR_TYPENAME_P (name))
4214 err_name = "type conversion operator";
4215 else if (TREE_CODE (name) == SCOPE_REF)
4216 err_name = IDENTIFIER_POINTER (TREE_OPERAND (name, 1));
4217 else
4218 err_name = IDENTIFIER_POINTER (name);
4220 #ifdef FIELD_XREF
4221 FIELD_xref_call(current_function_decl,err_name);
4222 #endif
4224 if (wrap)
4226 char *p = (char *)alloca (strlen (err_name) + 32);
4227 sprintf (p, "%s for `%s'", wrap < 0 ? "anti-wrapper" : "wrapper", err_name);
4228 err_name = p;
4231 if (instance == NULL_TREE)
4233 static_call_context = 0;
4235 basetype = NULL_TREE;
4236 /* Check cases where this is really a call to raise
4237 an exception. */
4238 if (current_class_type && TREE_CODE (name) == IDENTIFIER_NODE)
4240 basetype = purpose_member (name, CLASSTYPE_TAGS (current_class_type));
4241 if (basetype)
4242 basetype = TREE_VALUE (basetype);
4244 else if (TREE_CODE (name) == SCOPE_REF
4245 && TREE_CODE (TREE_OPERAND (name, 0)) == IDENTIFIER_NODE)
4247 if (! is_aggr_typedef (TREE_OPERAND (name, 0), 1))
4248 return error_mark_node;
4249 basetype = purpose_member (TREE_OPERAND (name, 1),
4250 CLASSTYPE_TAGS (TREE_TYPE (TREE_TYPE (TREE_OPERAND (name, 0)))));
4251 if (basetype)
4252 basetype = TREE_VALUE (basetype);
4255 if (basetype != NULL_TREE)
4257 /* call to a constructor... */
4258 else if (TREE_TYPE (name))
4259 basetype = TREE_TYPE (TREE_TYPE (name));
4260 else
4262 tree typedef_name = lookup_name (name);
4263 if (typedef_name && TREE_CODE (typedef_name) == TYPE_DECL)
4265 /* Cannonicalize the typedef name. */
4266 basetype = TREE_TYPE (typedef_name);
4267 name = DECL_NAME (TYPE_NAME (basetype));
4269 else
4271 error ("no constructor named `%s' in visible scope",
4272 IDENTIFIER_POINTER (name));
4273 return error_mark_node;
4276 if (wrap_type && wrap_type != basetype)
4278 error_with_aggr_type (wrap_type, "invalid constructor `%s::%s'",
4279 TYPE_NAME_STRING (basetype));
4280 return error_mark_node;
4282 if (TYPE_VIRTUAL_P (basetype))
4284 wrap_type = basetype;
4287 if (! IS_AGGR_TYPE (basetype))
4289 non_aggr_error:
4290 if ((flags & LOOKUP_COMPLAIN) && TREE_CODE (basetype) != ERROR_MARK)
4291 error ("request for member `%s' in something not a structure or union", err_name);
4293 return error_mark_node;
4296 else if (instance == C_C_D || instance == current_class_decl)
4298 extern tree ctor_label, dtor_label;
4300 /* When doing initialization, we side-effect the TREE_TYPE of
4301 C_C_D, hence we cannot set up BASETYPE from CURRENT_CLASS_TYPE. */
4302 basetype = TREE_TYPE (C_C_D);
4304 /* Anything manifestly `this' in constructors and destructors
4305 has a known type, so virtual function tables are not needed. */
4306 if (TYPE_VIRTUAL_P (basetype)
4307 && !(flags & LOOKUP_NONVIRTUAL)
4308 && wrap_type == NULL_TREE)
4309 need_vtbl = (dtor_label || ctor_label)
4310 ? unneeded : maybe_needed;
4312 static_call_context = 0;
4313 instance = C_C_D;
4314 instance_ptr = current_class_decl;
4315 result = build_field_call (CLASSTYPE_AS_LIST (current_class_type),
4316 instance_ptr, name, parms, err_name);
4318 if (result)
4319 return result;
4321 else if (TREE_CODE (instance) == RESULT_DECL)
4323 static_call_context = 0;
4324 basetype = TREE_TYPE (instance);
4325 if (wrap_type)
4327 if (basetype_or_else (basetype, wrap_type))
4328 basetype = wrap_type;
4329 else
4330 return error_mark_node;
4332 /* Should we ever have to make a virtual function reference
4333 from a RESULT_DECL, know that it must be of fixed type
4334 within the scope of this function. */
4335 else if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
4336 need_vtbl = maybe_needed;
4337 instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (basetype), instance);
4339 else if (instance == current_exception_object)
4341 instance_ptr = build1 (ADDR_EXPR, TYPE_POINTER_TO (current_exception_type),
4342 TREE_OPERAND (current_exception_object, 0));
4343 mark_addressable (TREE_OPERAND (current_exception_object, 0));
4344 result = build_field_call (CLASSTYPE_AS_LIST (current_exception_type),
4345 instance_ptr, name, parms, err_name);
4346 if (result)
4347 return result;
4348 error ("exception member `%s' cannot be invoked", err_name);
4349 return error_mark_node;
4351 else
4353 /* The MAIN_VARIANT of the type that `instance_ptr' winds up being. */
4354 tree inst_ptr_basetype;
4356 /* from the file "cplus-typeck.c". */
4357 extern tree unary_complex_lvalue ();
4359 static_call_context = (TREE_CODE (instance) == NOP_EXPR
4360 && TREE_OPERAND (instance, 0) == error_mark_node);
4362 /* the base type of an instance variable is pointer to class */
4363 basetype = TREE_TYPE (instance);
4365 if (TREE_CODE (basetype) == REFERENCE_TYPE)
4367 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (basetype));
4368 if (! IS_AGGR_TYPE (basetype))
4369 goto non_aggr_error;
4370 /* Call to convert not needed because we are remaining
4371 within the same type. */
4372 instance_ptr = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), instance);
4373 inst_ptr_basetype = basetype;
4375 else
4377 if (TREE_CODE (basetype) == POINTER_TYPE)
4379 basetype = TREE_TYPE (basetype);
4380 instance_ptr = instance;
4383 if (! IS_AGGR_TYPE (basetype))
4384 goto non_aggr_error;
4386 if (! instance_ptr)
4388 if ((lvalue_p (instance)
4389 && (instance_ptr = build_unary_op (ADDR_EXPR, instance, 0)))
4390 || (instance_ptr = unary_complex_lvalue (ADDR_EXPR, instance)))
4392 if (instance_ptr == error_mark_node)
4393 return error_mark_node;
4395 else if (TREE_CODE (instance) == NOP_EXPR
4396 || TREE_CODE (instance) == CONSTRUCTOR)
4398 /* A cast is not an lvalue. Initialize a fresh temp
4399 with the value we are casting from, and proceed with
4400 that temporary. We can't cast to a reference type,
4401 so that simplifies the initialization to something
4402 we can manage. */
4403 tree temp = get_temp_name (TREE_TYPE (instance), 0);
4404 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4405 expand_aggr_init (temp, instance, 0);
4406 else
4408 store_init_value (temp, instance);
4409 expand_decl_init (temp);
4411 instance = temp;
4412 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
4414 else
4416 assert (TREE_CODE (instance) == CALL_EXPR);
4417 if (TYPE_NEEDS_CONSTRUCTOR (basetype))
4418 instance = build_cplus_new (basetype, instance);
4419 else
4421 instance = get_temp_name (basetype, 0);
4422 TREE_ADDRESSABLE (instance) = 1;
4424 instance_ptr = build_unary_op (ADDR_EXPR, instance, 0);
4426 /* @@ Should we call comp_target_types here? */
4427 inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
4428 if (TYPE_MAIN_VARIANT (basetype) == TYPE_MAIN_VARIANT (inst_ptr_basetype))
4429 basetype = inst_ptr_basetype;
4430 else
4431 instance_ptr = convert (TYPE_POINTER_TO (basetype), instance_ptr);
4433 else
4434 inst_ptr_basetype = TREE_TYPE (TREE_TYPE (instance_ptr));
4437 if (basetype_path == NULL_TREE)
4438 basetype_path = CLASSTYPE_AS_LIST (inst_ptr_basetype);
4440 result = build_field_call (basetype_path, instance_ptr, name, parms, err_name);
4441 if (result)
4442 return result;
4444 if (wrap_type)
4446 if (basetype_or_else (basetype, wrap_type))
4447 basetype = wrap_type;
4448 else
4449 return error_mark_node;
4451 else if (!(flags & LOOKUP_NONVIRTUAL) && TYPE_VIRTUAL_P (basetype))
4453 if (TREE_VOLATILE (instance_ptr))
4455 /* This action is needed because the instance is needed
4456 for providing the base of the virtual function table.
4457 Without using a SAVE_EXPR, the function we are building
4458 may be called twice, or side effects on the instance
4459 variable (such as a post-increment), may happen twice. */
4460 instance_ptr = save_expr (instance_ptr);
4461 instance = build_indirect_ref (instance_ptr, 0);
4463 else if (TREE_CODE (TREE_TYPE (instance)) == POINTER_TYPE)
4465 /* This happens when called for operator new (). */
4466 instance = build_indirect_ref (instance, 0);
4469 need_vtbl = maybe_needed;
4473 if (TYPE_SIZE (basetype) == 0)
4475 /* This is worth complaining about, I think. */
4476 error_with_aggr_type (basetype, "cannot lookup method in incomplete type `%s'");
4477 return error_mark_node;
4480 /* Are we building a non-virtual wrapper? */
4481 if (flags & LOOKUP_NONVIRTUAL)
4483 if (all_virtual)
4484 sorry ("non-virtual call with -fall-virtual");
4485 if (wrap)
4486 wrap_type = basetype;
4489 save_basetype = basetype;
4491 if (all_virtual == 1
4492 && (! strncmp (IDENTIFIER_POINTER (name), OPERATOR_METHOD_FORMAT,
4493 OPERATOR_METHOD_LENGTH)
4494 || instance_ptr == NULL_TREE
4495 || (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype) == 0
4496 && TYPE_NEEDS_WRAPPER (basetype) == 0)))
4497 all_virtual = 0;
4499 last = NULL_TREE;
4500 for (parmtypes = 0, parm = parms; parm; parm = TREE_CHAIN (parm))
4502 tree t = TREE_TYPE (TREE_VALUE (parm));
4503 if (TREE_CODE (t) == OFFSET_TYPE)
4505 /* Convert OFFSET_TYPE entities to their normal selves. */
4506 TREE_VALUE (parm) = resolve_offset_ref (TREE_VALUE (parm));
4507 t = TREE_TYPE (TREE_VALUE (parm));
4509 if (TREE_CODE (t) == ARRAY_TYPE)
4511 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
4512 This eliminates needless calls to `compute_conversion_costs'. */
4513 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
4514 t = TREE_TYPE (TREE_VALUE (parm));
4516 if (t == error_mark_node)
4517 return error_mark_node;
4518 last = build_tree_list (NULL_TREE, t);
4519 parmtypes = chainon (parmtypes, last);
4522 if (instance)
4524 constp = TREE_READONLY (instance);
4525 volatilep = TREE_THIS_VOLATILE (instance);
4526 parms = tree_cons (NULL_TREE, instance_ptr, parms);
4528 else
4530 /* Raw constructors are always in charge. */
4531 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
4532 && ! (flags & LOOKUP_HAS_IN_CHARGE))
4534 flags |= LOOKUP_HAS_IN_CHARGE;
4535 parms = tree_cons (NULL_TREE, integer_one_node, parms);
4536 parmtypes = tree_cons (NULL_TREE, integer_type_node, parmtypes);
4539 if (flag_this_is_variable)
4541 constp = 0;
4542 volatilep = 0;
4543 parms = tree_cons (NULL_TREE, build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), integer_zero_node), parms);
4545 else
4547 constp = 0;
4548 volatilep = 0;
4549 instance_ptr = build_new (NULL_TREE, basetype, void_type_node, 0);
4550 if (instance_ptr == error_mark_node)
4551 return error_mark_node;
4552 instance_ptr = save_expr (instance_ptr);
4553 TREE_CALLS_NEW (instance_ptr) = 1;
4554 instance = build_indirect_ref (instance_ptr, 0);
4555 parms = tree_cons (NULL_TREE, instance_ptr, parms);
4558 parmtypes = tree_cons (NULL_TREE,
4559 build_pointer_type (build_type_variant (basetype, constp, volatilep)),
4560 parmtypes);
4561 if (last == NULL_TREE)
4562 last = parmtypes;
4564 /* Look up function name in the structure type definition. */
4566 if (wrap)
4568 if (wrap > 0)
4569 name_kind = "wrapper";
4570 else
4571 name_kind = "anti-wrapper";
4572 baselink = get_wrapper (basetype);
4574 else
4576 if (TREE_TYPE (name)
4577 && TREE_CODE (TREE_TYPE (name)) == TYPE_DECL
4578 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (name))))
4580 tree tmp = NULL_TREE;
4581 if (TREE_TYPE (name) == TYPE_NAME (basetype))
4582 tmp = basetype;
4583 else
4584 tmp = get_base_type (TREE_TYPE (TREE_TYPE (name)), basetype, 0);
4585 if (tmp != 0)
4587 name_kind = "constructor";
4589 if (TYPE_USES_VIRTUAL_BASECLASSES (basetype)
4590 && ! (flags & LOOKUP_HAS_IN_CHARGE))
4592 /* Constructors called for initialization
4593 only are never in charge. */
4594 tree tmplist;
4596 flags |= LOOKUP_HAS_IN_CHARGE;
4597 tmplist = tree_cons (NULL_TREE, integer_zero_node,
4598 TREE_CHAIN (parms));
4599 TREE_CHAIN (parms) = tmplist;
4600 tmplist = tree_cons (NULL_TREE, integer_type_node, TREE_CHAIN (parmtypes));
4601 TREE_CHAIN (parmtypes) = tmplist;
4604 #ifdef SOS
4605 if (TYPE_DYNAMIC (basetype) && dtbl_inserted == 0)
4607 tree parm, parmtype;
4608 dtbl = get_sos_dtable (basetype);
4609 parm = tree_cons (NULL_TREE, dtbl, TREE_CHAIN (parms));
4610 parmtype = tree_cons (NULL_TREE, build_pointer_type (ptr_type_node), TREE_CHAIN (parmtypes));
4611 TREE_CHAIN (parms) = parm;
4612 TREE_CHAIN (parmtypes) = parmtype;
4613 dtbl_inserted = -1;
4615 #endif
4616 /* constructors are in very specific places. */
4617 #ifdef SOS
4618 if (dtbl_inserted == -1)
4620 TREE_CHAIN (parmtypes) = TREE_CHAIN (TREE_CHAIN (parmtypes));
4621 TREE_CHAIN (parms) = TREE_CHAIN (TREE_CHAIN (parms));
4622 dtbl_inserted = 0;
4624 #endif
4625 basetype = tmp;
4627 else
4628 name_kind = "method";
4630 else name_kind = "method";
4632 if (basetype_path == NULL_TREE)
4633 basetype_path = CLASSTYPE_AS_LIST (basetype);
4634 result = lookup_fnfields (basetype_path, name,
4635 (flags & LOOKUP_COMPLAIN));
4636 if (result == error_mark_node)
4637 return error_mark_node;
4640 /* Now, go look for this method name. We do not find destructors here.
4642 Putting `void_list_node' on the end of the parmtypes
4643 fakes out `build_decl_overload' into doing the right thing. */
4644 TREE_CHAIN (last) = void_list_node;
4645 method_name = build_decl_overload (IDENTIFIER_POINTER (name),
4646 parmtypes,
4647 1 + (name == DECL_NAME (TYPE_NAME (save_basetype))));
4648 TREE_CHAIN (last) = NULL_TREE;
4650 for (pass = 0; pass < 2; pass++)
4652 struct candidate *candidates;
4653 struct candidate *cp;
4654 int len, best = 2;
4656 /* This increments every time we go up the type hierarchy.
4657 The idea is to prefer a function of the derived class if possible. */
4658 int b_or_d;
4660 baselink = result;
4662 if (pass > 0)
4664 candidates = (struct candidate *) alloca ((ever_seen+1) * sizeof (struct candidate));
4665 cp = candidates;
4666 len = list_length (parms);
4667 b_or_d = 0;
4669 /* First see if a global function has a shot at it. */
4670 if (flags & LOOKUP_GLOBAL)
4672 tree friend_parms;
4673 tree parm = TREE_VALUE (parms);
4675 if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE)
4676 friend_parms = parms;
4677 else if (TREE_CODE (TREE_TYPE (parm)) == POINTER_TYPE)
4679 parm = build_indirect_ref (parm, "friendifying parms (compiler error)");
4680 parm = convert (build_reference_type (TREE_TYPE (parm)), parm);
4681 friend_parms = tree_cons (NULL_TREE, parm, TREE_CHAIN (parms));
4683 else
4684 assert (0);
4686 cp->harshness
4687 = (unsigned short *)alloca ((len+1) * sizeof (short));
4688 result = build_overload_call (name, friend_parms, 0, cp);
4689 /* If it turns out to be the one we were actually looking for
4690 (it was probably a friend function), the return the
4691 good result. */
4692 if (TREE_CODE (result) == CALL_EXPR)
4693 return result;
4695 while (cp->evil == 0)
4697 /* non-standard uses: set the field to 0 to indicate
4698 we are using a non-member function. */
4699 cp->u.field = 0;
4700 if (cp->harshness[len] == 0
4701 && cp->harshness[len] == 0
4702 && cp->user == 0 && cp->b_or_d == 0
4703 && cp->easy < best)
4704 best = cp->easy;
4705 cp += 1;
4710 while (baselink)
4712 /* We have a hit (of sorts). If the parameter list is
4713 "error_mark_node", or some variant thereof, it won't
4714 match any methods. Since we have verified that the is
4715 some method vaguely matching this one (in name at least),
4716 silently return.
4718 Don't stop for friends, however. */
4719 tree basetypes = TREE_PURPOSE (baselink);
4721 function = TREE_VALUE (baselink);
4722 basetype = TREE_VALUE (basetypes);
4724 /* Cast the instance variable to the approriate type. */
4725 TREE_VALUE (parmtypes) = TYPE_POINTER_TO (basetype);
4727 if (DESTRUCTOR_NAME_P (DECL_NAME (function)))
4728 function = TREE_CHAIN (function);
4730 for (; function; function = TREE_CHAIN (function))
4732 #ifdef GATHER_STATISTICS
4733 n_inner_fields_searched++;
4734 #endif
4735 ever_seen++;
4737 /* Not looking for friends here. */
4738 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE
4739 && ! DECL_STATIC_FUNCTION_P (function))
4740 continue;
4742 if (pass == 0
4743 && DECL_NAME (function) == method_name)
4745 if (flags & LOOKUP_PROTECT)
4747 visibility = compute_visibility (basetypes, function);
4748 if (visibility == visibility_protected
4749 && flags & LOOKUP_PROTECTED_OK)
4750 visibility = visibility_public;
4753 if ((flags & LOOKUP_PROTECT) == 0
4754 || visibility == visibility_public)
4755 goto found_and_ok;
4756 else if (visibility == visibility_private)
4757 saw_private = function;
4758 else if (visibility == visibility_protected)
4759 saw_protected = function;
4760 /* If we fail on the exact match, we have
4761 an immediate failure. */
4762 goto found;
4764 if (pass > 0)
4766 tree these_parms = parms;
4768 #ifdef GATHER_STATISTICS
4769 n_inner_fields_searched++;
4770 #endif
4771 cp->harshness
4772 = (unsigned short *)alloca ((len+1) * sizeof (short));
4773 if (DECL_STATIC_FUNCTION_P (function))
4774 these_parms = TREE_CHAIN (these_parms);
4775 compute_conversion_costs (function, these_parms, cp, len);
4776 cp->b_or_d += b_or_d;
4777 if (cp->evil == 0)
4779 cp->u.field = function;
4780 cp->function = function;
4781 if (flags & LOOKUP_PROTECT)
4783 enum visibility_type this_v;
4784 this_v = compute_visibility (basetypes, function);
4785 if (this_v == visibility_protected
4786 && (flags & LOOKUP_PROTECTED_OK))
4787 this_v = visibility_public;
4788 if (this_v != visibility_public)
4790 if (this_v == visibility_private)
4791 saw_private = function;
4792 else
4793 saw_protected = function;
4794 continue;
4798 /* No "two-level" conversions. */
4799 if (flags & LOOKUP_NO_CONVERSION && cp->user != 0)
4800 continue;
4802 /* If we used default parameters, we must
4803 check to see whether anyone else might
4804 use them also, and report a possible
4805 ambiguity. */
4806 if (! TYPE_USES_MULTIPLE_INHERITANCE (save_basetype)
4807 && cp->harshness[len] == 0
4808 && (cp->harshness[0] & 128) == 0
4809 && cp->user == 0 && cp->b_or_d == 0
4810 && cp->easy < best)
4812 if (! DECL_STATIC_FUNCTION_P (function))
4813 TREE_VALUE (parms) = cp->arg;
4814 if (best == 2)
4815 goto found_and_maybe_warn;
4817 cp++;
4821 /* Now we have run through one link's member functions.
4822 arrange to head-insert this link's links. */
4823 baselink = next_baselink (baselink);
4824 b_or_d += 1;
4826 if (pass == 0)
4828 /* No exact match could be found. Now try to find match
4829 using default conversions. */
4830 if ((flags & LOOKUP_GLOBAL) && IDENTIFIER_GLOBAL_VALUE (name))
4831 if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == FUNCTION_DECL)
4832 ever_seen += 1;
4833 else if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (name)) == TREE_LIST)
4834 ever_seen += list_length (IDENTIFIER_GLOBAL_VALUE (name));
4836 if (ever_seen == 0)
4838 if (flags & LOOKUP_GLOBAL)
4839 error ("no global or member function `%s' defined", err_name);
4840 else
4841 error_with_aggr_type (save_basetype, "no member function `%s::%s'", err_name);
4842 return error_mark_node;
4844 continue;
4847 if (cp - candidates != 0)
4849 /* Rank from worst to best. Then cp will point to best one.
4850 Private fields have their bits flipped. For unsigned
4851 numbers, this should make them look very large.
4852 If the best alternate has a (signed) negative value,
4853 then all we ever saw were private members. */
4854 if (cp - candidates > 1)
4856 cp = ideal_candidate (save_basetype, candidates,
4857 cp - candidates, parms, len);
4858 if (cp == 0)
4860 error ("ambiguous type conversion requested for %s `%s'",
4861 name_kind, err_name);
4862 return error_mark_node;
4865 else if (cp[-1].evil == 2)
4867 error ("ambiguous type conversion requested for %s `%s'",
4868 name_kind, err_name);
4869 return error_mark_node;
4871 else cp--;
4873 /* The global function was the best, so use it. */
4874 if (cp->u.field == 0)
4876 /* We must convert the instance pointer into a reference type.
4877 Global overloaded functions can only either take
4878 aggregate objects (which come for free from references)
4879 or reference data types anyway. */
4880 TREE_VALUE (parms) = copy_node (instance_ptr);
4881 TREE_TYPE (TREE_VALUE (parms)) = build_reference_type (TREE_TYPE (TREE_TYPE (instance_ptr)));
4882 return build_function_call (cp->function, parms);
4885 function = cp->function;
4886 if (DECL_STATIC_FUNCTION_P (function))
4887 basetype = NULL_TREE;
4888 else
4890 basetype = TREE_TYPE (TREE_TYPE (cp->arg));
4891 TREE_VALUE (parms) = cp->arg;
4893 goto found_and_maybe_warn;
4896 if ((flags & ~LOOKUP_GLOBAL) & (LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY))
4898 char *tag_name, *buf;
4900 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
4901 == LOOKUP_SPECULATIVELY)
4902 return NULL_TREE;
4904 if (DECL_STATIC_FUNCTION_P (cp->function))
4905 parms = TREE_CHAIN (parms);
4906 if (ever_seen)
4908 if (((int)saw_protected|(int)saw_private) == 0)
4910 if (flags & LOOKUP_SPECULATIVELY)
4911 return NULL_TREE;
4912 if (static_call_context && TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
4913 error_with_aggr_type (TREE_TYPE (TREE_TYPE (instance_ptr)),
4914 "object missing in call to `%s::%s'",
4915 err_name);
4916 else
4917 report_type_mismatch (cp, parms, name_kind, err_name);
4919 else
4921 char buf[80];
4922 char *msg;
4923 tree seen = saw_private;
4925 if (saw_private)
4926 if (saw_protected)
4927 msg = "%s %%s (and the like) are private or protected";
4928 else
4929 msg = "the %s %%s is private";
4930 else
4932 msg = "the %s %%s is protected";
4933 seen = saw_protected;
4935 sprintf (buf, msg, name_kind);
4936 error_with_decl (seen, buf);
4937 error ("within this context");
4939 return error_mark_node;
4942 if ((flags & (LOOKUP_SPECULATIVELY|LOOKUP_COMPLAIN))
4943 == LOOKUP_COMPLAIN)
4945 if (TREE_CODE (save_basetype) == RECORD_TYPE)
4946 tag_name = "structure";
4947 else
4948 tag_name = "union";
4950 if (wrap)
4951 buf = "%s has no appropriate wrapper function defined";
4952 else
4954 buf = (char *)alloca (30 + strlen (err_name));
4955 strcpy (buf, "%s has no method named `%s'");
4958 error (buf, tag_name, err_name);
4959 return error_mark_node;
4961 return NULL_TREE;
4963 continue;
4965 found_and_maybe_warn:
4966 if (cp->harshness[0] & 128)
4968 if (flags & LOOKUP_COMPLAIN)
4970 error_with_decl (cp->function, "non-const member function `%s'");
4971 error ("called for const object at this point in file");
4973 /* Not good enough for a match. */
4974 else return error_mark_node;
4976 goto found_and_ok;
4978 /* Silently return error_mark_node. */
4979 return error_mark_node;
4981 found:
4982 if (visibility == visibility_private)
4984 if (flags & LOOKUP_COMPLAIN)
4985 error (TREE_PRIVATE (function)
4986 ? "%s `%s' is private"
4987 : "%s `%s' is from private base class",
4988 name_kind,
4989 lang_printable_name (function));
4990 return error_mark_node;
4992 else if (visibility == visibility_protected)
4994 if (flags & LOOKUP_COMPLAIN)
4995 error (TREE_PROTECTED (function)
4996 ? "%s `%s' is protected"
4997 : "%s `%s' has protected visibility from this point",
4998 name_kind,
4999 lang_printable_name (function));
5000 return error_mark_node;
5002 abort ();
5004 found_and_ok:
5006 /* From here on down, BASETYPE is the type that INSTANCE_PTR's
5007 type (if it exists) is a pointer to. */
5008 basetype = DECL_CONTEXT (function);
5009 fntype = TREE_TYPE (function);
5011 if (TREE_CODE (fntype) == POINTER_TYPE)
5012 fntype = TREE_TYPE (fntype);
5014 /* If we are referencing a virtual function from an object
5015 of effectively static type, then there is no need
5016 to go through the virtual function table. */
5017 if (need_vtbl == maybe_needed)
5019 int fixed_type = resolves_to_fixed_type_p (instance);
5021 if (all_virtual == 1
5022 && DECL_VINDEX (function)
5023 && may_be_remote (basetype))
5024 need_vtbl = needed;
5025 else if (DECL_VIRTUAL_P (function))
5026 need_vtbl = fixed_type ? unneeded : needed;
5027 else
5028 need_vtbl = not_needed;
5030 if (fixed_type && DECL_ABSTRACT_VIRTUAL_P (function))
5032 error_with_decl (function, "invalid call to abstract function `%s'");
5033 return error_mark_node;
5037 if (TREE_CODE (fntype) == METHOD_TYPE && static_call_context)
5039 /* Let's be nice to the user for now, and give reasonable
5040 default behavior. */
5041 instance_ptr = current_class_decl;
5042 if (instance_ptr)
5044 if (basetype != current_class_type)
5046 basetype = get_base_type (basetype, current_class_type, 1);
5047 if (basetype == 0)
5049 error_not_base_type (DECL_CONTEXT (function), current_class_type);
5050 return error_mark_node;
5052 else if (basetype == error_mark_node)
5053 return error_mark_node;
5056 else
5058 error_with_aggr_type (basetype, "cannot call member function `%s::%s' without object",
5059 err_name);
5060 return error_mark_node;
5064 value_type = TREE_TYPE (fntype) ? TREE_TYPE (fntype) : void_type_node;
5066 if (TYPE_SIZE (value_type) == 0)
5068 if (flags & LOOKUP_COMPLAIN)
5069 incomplete_type_error (0, value_type);
5070 return error_mark_node;
5073 /* We do not pass FUNCTION into `actualparameterlist', because by
5074 now everything should be ok. If not, then we have a serious error. */
5075 if (DECL_STATIC_FUNCTION_P (function))
5076 parms = actualparameterlist (NULL_TREE, TYPE_ARG_TYPES (fntype),
5077 TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL);
5078 else if (need_vtbl == unneeded)
5080 int sub_flags = DECL_CONSTRUCTOR_P (function) ? flags : LOOKUP_NORMAL;
5081 basetype = TREE_TYPE (instance);
5082 if (DECL_CONTEXT (function) != TYPE_MAIN_VARIANT (basetype)
5083 && (TYPE_USES_MULTIPLE_INHERITANCE (basetype)
5084 || TYPE_USES_VIRTUAL_BASECLASSES (basetype)))
5086 basetype = DECL_CONTEXT (function);
5087 instance_ptr = convert_pointer_to (basetype, instance_ptr);
5088 instance = build_indirect_ref (instance_ptr, 0);
5090 parms = tree_cons (NULL_TREE, instance_ptr,
5091 actualparameterlist (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, sub_flags));
5093 else
5095 if ((flags & LOOKUP_NONVIRTUAL) == 0)
5096 basetype = DECL_VCONTEXT (function);
5098 /* First parm could be integer_zerop with casts like
5099 ((Object*)0)->Object::IsA() */
5100 if (!integer_zerop (TREE_VALUE (parms)))
5102 instance_ptr = convert_pointer_to (build_type_variant (basetype, constp, volatilep),
5103 TREE_VALUE (parms));
5104 if (TREE_CODE (instance_ptr) == COND_EXPR)
5106 instance_ptr = save_expr (instance_ptr);
5107 instance = build_indirect_ref (instance_ptr);
5109 else if (TREE_CODE (instance_ptr) == NOP_EXPR
5110 && TREE_CODE (TREE_OPERAND (instance_ptr, 0)) == ADDR_EXPR
5111 && TREE_OPERAND (TREE_OPERAND (instance_ptr, 0), 0) == instance)
5113 else if (instance == NULL_TREE
5114 || TREE_CODE (instance) != INDIRECT_REF
5115 || TREE_OPERAND (instance, 0) != instance_ptr)
5116 instance = build_indirect_ref (instance_ptr);
5118 parms = tree_cons (NULL_TREE, instance_ptr,
5119 actualparameterlist (NULL_TREE, TREE_CHAIN (TYPE_ARG_TYPES (fntype)), TREE_CHAIN (parms), NULL_TREE, LOOKUP_NORMAL));
5122 /* See if there is a wrapper for this thing. */
5123 if (wrap < 0
5124 || static_call_context
5125 || name == wrapper_name
5126 || name == DECL_NAME (TYPE_NAME (basetype)))
5128 else if (wrap > 0 || TYPE_NEEDS_WRAPPER (basetype))
5130 flags &= ~LOOKUP_PROTECT;
5131 if (wrap == 0)
5133 wrap = TYPE_NEEDS_WRAPPER (basetype);
5134 /* If no wrapper specified, wrapper may be virtual. */
5135 flags &= ~LOOKUP_NONVIRTUAL;
5138 if (wrap)
5140 tree wrapped_result, unwrapped_result;
5141 register int bytecount = get_arglist_len_in_bytes (parms);
5143 if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
5144 parm = build_unary_op (ADDR_EXPR, function, 0);
5145 else
5147 fntype = build_cplus_method_type (basetype, TREE_TYPE (fntype), TYPE_ARG_TYPES (fntype));
5148 parm = build1 (NOP_EXPR, build_pointer_type (fntype), DECL_VINDEX (function));
5151 if (TYPE_HAS_WRAPPER_PRED (basetype))
5153 unwrapped_result = build_nt (CALL_EXPR, default_conversion (function), parms, NULL_TREE);
5155 assert (TREE_OPERAND (unwrapped_result, 1) != error_mark_node);
5157 TREE_TYPE (unwrapped_result) = value_type;
5158 TREE_VOLATILE (unwrapped_result) = 1;
5159 TREE_RAISES (unwrapped_result) = !! TYPE_RAISES_EXCEPTIONS (fntype);
5162 /* If this pointer walked as a result of multiple inheritance,
5163 keep its displaced value. */
5164 parms = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
5165 tree_cons (NULL_TREE, parm, TREE_CHAIN (parms)));
5167 wrapped_result = get_wrapper (basetype);
5168 assert (wrapped_result != NULL_TREE);
5169 assert (wrapped_result != error_mark_node);
5171 /* @@ Should BASETYPE_PATH get TREE_PURPOSE (wrapped_result) here? */
5172 wrapped_result
5173 = build_method_call (instance,
5174 DECL_ORIGINAL_NAME (TREE_VALUE (wrapped_result)),
5175 parms, basetype_path, flags);
5176 #if 0
5177 /* Do this if we want the result of operator->() to inherit
5178 the type of the function it is subbing for. */
5179 if (wrapped_result != error_mark_node)
5180 TREE_TYPE (wrapped_result) = value_type;
5181 #endif
5183 if (TYPE_HAS_WRAPPER_PRED (basetype))
5185 result = build_conditional_expr
5186 (build_method_call (instance, wrapper_pred_name, build_tree_list (NULL_TREE, parm), basetype_path, LOOKUP_NORMAL),
5187 wrapped_result,
5188 unwrapped_result);
5191 else
5193 result = wrapped_result;
5196 TREE_VOLATILE (result) = 1;
5197 return result;
5200 /* Constructors do not overload method calls. */
5201 else if (TYPE_OVERLOADS_METHOD_CALL_EXPR (basetype)
5202 && name != DECL_NAME (TYPE_NAME (basetype))
5203 && (TREE_CODE (function) != FUNCTION_DECL
5204 || strncmp (IDENTIFIER_POINTER (DECL_NAME (function)),
5205 OPERATOR_METHOD_FORMAT,
5206 OPERATOR_METHOD_LENGTH))
5207 #if 0
5208 && (may_be_remote (basetype)
5209 || (C_C_D ? TREE_TYPE (instance) != current_class_type : 1))
5210 #else
5211 /* This change by Larry Ketcham. */
5212 && (may_be_remote (basetype) || instance != C_C_D)
5213 #endif
5216 #ifdef ESKIT
5217 register int bytecount = 0;
5218 #else
5219 register int bytecount = get_arglist_len_in_bytes (parms);
5220 #endif
5221 tree fn_as_int;
5223 parms = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
5224 TREE_CHAIN (parms));
5226 if (!all_virtual && TREE_CODE (function) == FUNCTION_DECL)
5227 fn_as_int = build_unary_op (ADDR_EXPR, function, 0);
5228 else
5229 fn_as_int = convert (TREE_TYPE (default_conversion (function)), DECL_VINDEX (function));
5230 if (all_virtual == 1)
5231 fn_as_int = convert (integer_type_node, fn_as_int);
5233 result = build_opfncall (METHOD_CALL_EXPR, LOOKUP_NORMAL, instance, fn_as_int, parms);
5235 if (result == NULL_TREE)
5237 compiler_error ("could not overload `operator->()(...)'");
5238 return error_mark_node;
5240 else if (result == error_mark_node)
5241 return error_mark_node;
5243 #if 0
5244 /* Do this if we want the result of operator->() to inherit
5245 the type of the function it is subbing for. */
5246 TREE_TYPE (result) = value_type;
5247 #endif
5249 #ifdef ESKIT
5251 int used, size;
5253 /* Count the number of bytes of arguements to operator->(),
5254 not to the method itself. In the tally, don't count bytes
5255 for pointer to member function or for the bytecount. */
5256 parms = TREE_OPERAND (result, 1);
5257 bytecount = get_arglist_len_in_bytes (TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (parms))));
5258 used = size = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_VALUE (parms))));
5259 #ifdef PUSH_ROUNDING
5260 size = PUSH_ROUNDING (size);
5261 #endif
5262 used = (((size + PARM_BOUNDARY / BITS_PER_UNIT - 1)
5263 / (PARM_BOUNDARY / BITS_PER_UNIT))
5264 * (PARM_BOUNDARY / BITS_PER_UNIT));
5265 bytecount += used;
5266 TREE_CHAIN (TREE_CHAIN (parms))
5267 = tree_cons (NULL_TREE, build_int_2 (bytecount, 0),
5268 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (parms))));
5270 #endif
5272 return result;
5275 if (need_vtbl == needed)
5277 function = build_vfn_ref (&TREE_VALUE (parms), instance, DECL_VINDEX (function));
5278 TREE_TYPE (function) = build_pointer_type (fntype);
5280 #ifdef SOS
5281 else if (basetype && TYPE_DYNAMIC (basetype))
5283 function = build_array_ref (dtbl, DECL_DINDEX (function));
5284 TREE_TYPE (function) = build_pointer_type (fntype);
5286 #endif
5288 if (TREE_INLINE (function) && TREE_CODE (function) == FUNCTION_DECL)
5289 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
5290 else function = default_conversion (function);
5292 result =
5293 build_nt (CALL_EXPR, function, parms, NULL_TREE);
5295 TREE_TYPE (result) = value_type;
5296 TREE_VOLATILE (result) = 1;
5297 TREE_RAISES (result)
5298 = TYPE_RAISES_EXCEPTIONS (fntype) || (parms && TREE_RAISES (parms));
5299 return result;
5302 /* Similar to `build_method_call', but for overloaded non-member functions.
5303 The name of this function comes through NAME. The name depends
5304 on PARMS.
5306 Note that this function must handle simple `C' promotions,
5307 as well as variable numbers of arguments (...), and
5308 default arguments to boot.
5310 If the overloading is successful, we return a treenode which
5311 contains the call to the function.
5313 If overloading produces candidates which are probabe, but not definite,
5314 we hold these candidates. If FINAL_CP is non-zero, then we are free
5315 to assume that final_cp points to enough storage for all candidates that
5316 this function might generate. The `harshness' array is preallocated for
5317 the first candidate, but not for subsequent ones.
5319 Note that the DECL_RTL of FUNCTION must be made to agree with this
5320 function's new name. */
5322 tree
5323 build_overload_call (fnname, parms, complain, final_cp)
5324 tree fnname, parms;
5325 int complain;
5326 struct candidate *final_cp;
5328 /* must check for overloading here */
5329 tree overload_name, functions, function, parm;
5330 tree parmtypes = NULL_TREE, last = NULL_TREE;
5331 register tree outer;
5332 int length;
5333 int parmlength = list_length (parms);
5335 struct candidate *candidates, *cp;
5336 int rank_for_overload ();
5338 if (final_cp)
5340 final_cp[0].evil = 0;
5341 final_cp[0].user = 0;
5342 final_cp[0].b_or_d = 0;
5343 final_cp[0].easy = 0;
5344 final_cp[0].function = 0;
5345 /* end marker. */
5346 final_cp[1].evil = 1;
5349 for (parm = parms; parm; parm = TREE_CHAIN (parm))
5351 register tree t = TREE_TYPE (TREE_VALUE (parm));
5353 if (t == error_mark_node)
5354 return error_mark_node;
5355 if (TREE_CODE (t) == ARRAY_TYPE || TREE_CODE (t) == OFFSET_TYPE)
5357 /* Perform the conversion from ARRAY_TYPE to POINTER_TYPE in place.
5358 Also convert OFFSET_TYPE entities to their normal selves.
5359 This eliminates needless calls to `compute_conversion_costs'. */
5360 TREE_VALUE (parm) = default_conversion (TREE_VALUE (parm));
5361 t = TREE_TYPE (TREE_VALUE (parm));
5363 last = build_tree_list (NULL_TREE, t);
5364 parmtypes = chainon (parmtypes, last);
5366 if (last)
5367 TREE_CHAIN (last) = void_list_node;
5368 else
5369 parmtypes = void_list_node;
5370 overload_name = build_decl_overload (IDENTIFIER_POINTER (fnname), parmtypes, 0);
5372 /* Now check to see whether or not we can win.
5373 Note that if we are called from `build_method_call',
5374 then we cannot have a mis-match, because we would have
5375 already found such a winning case. */
5377 if (IDENTIFIER_GLOBAL_VALUE (overload_name))
5378 if (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (overload_name)) != TREE_LIST)
5379 return build_function_call (DECL_MAIN_VARIANT (IDENTIFIER_GLOBAL_VALUE (overload_name)), parms);
5381 functions = IDENTIFIER_GLOBAL_VALUE (fnname);
5383 if (functions == NULL_TREE)
5385 if (complain)
5386 error ("only member functions apply");
5387 if (final_cp)
5388 final_cp->evil = 1;
5389 return error_mark_node;
5392 if (TREE_CODE (functions) == FUNCTION_DECL)
5394 functions = DECL_MAIN_VARIANT (functions);
5395 if (final_cp)
5397 /* We are just curious whether this is a viable alternative or not. */
5398 compute_conversion_costs (functions, parms, final_cp, parmlength);
5399 return functions;
5401 else
5402 return build_function_call (functions, parms);
5405 if (TREE_VALUE (functions) == NULL_TREE)
5407 if (complain)
5408 error ("function `%s' declared overloaded, but no instances of that function declared",
5409 IDENTIFIER_POINTER (TREE_PURPOSE (functions)));
5410 return error_mark_node;
5413 if (TREE_CODE (TREE_VALUE (functions)) == TREE_LIST)
5415 register tree outer;
5416 length = 0;
5418 /* The list-of-lists should only occur for class things. */
5419 assert (functions == IDENTIFIER_CLASS_VALUE (fnname));
5421 for (outer = functions; outer; outer = TREE_CHAIN (outer))
5423 /* member functions. */
5424 length += list_length (TREE_VALUE (TREE_VALUE (outer)));
5425 /* friend functions. */
5426 length += list_length (TREE_TYPE (TREE_VALUE (outer)));
5429 else
5431 length = list_length (functions);
5434 if (final_cp)
5435 candidates = final_cp;
5436 else
5437 candidates = (struct candidate *)alloca ((length+1) * sizeof (struct candidate));
5439 cp = candidates;
5441 assert (TREE_CODE (TREE_VALUE (functions)) != TREE_LIST);
5442 /* OUTER is the list of FUNCTION_DECLS, in a TREE_LIST. */
5444 for (outer = functions; outer; outer = TREE_CHAIN (outer))
5446 function = TREE_VALUE (outer);
5447 if (TREE_CODE (function) != FUNCTION_DECL)
5449 if (TREE_CODE (function) == CONST_DECL)
5450 error_with_decl (function, "enumeral value `%s' conflicts with function of same name");
5451 else if (TREE_CODE (function) == VAR_DECL)
5452 if (TREE_STATIC (function))
5453 error_with_decl (function, "variable `%s' conflicts with function of same name");
5454 else
5455 error_with_decl (function, "constant field `%s' conflicts with function of same name");
5456 else if (TREE_CODE (function) == TYPE_DECL)
5457 continue;
5458 else abort ();
5459 error ("at this point in file");
5460 continue;
5462 function = DECL_MAIN_VARIANT (function);
5463 /* Can't use alloca here, since result might be
5464 passed to calling function. */
5465 cp->harshness
5466 = (unsigned short *)oballoc ((parmlength+1) * sizeof (short));
5467 compute_conversion_costs (function, parms, cp, parmlength);
5468 if (cp[0].evil == 0)
5470 cp[1].evil = 1;
5471 if (final_cp
5472 && cp[0].user == 0 && cp[0].b_or_d == 0
5473 && cp[0].easy <= 1)
5475 final_cp[0].easy = cp[0].easy;
5476 return function;
5478 cp++;
5482 if (cp - candidates)
5484 tree rval = error_mark_node;
5486 /* Leave marker. */
5487 cp[0].evil = 1;
5488 if (cp - candidates > 1)
5490 struct candidate *best_cp
5491 = ideal_candidate (NULL_TREE, candidates,
5492 cp - candidates, parms, parmlength);
5493 if (best_cp == 0)
5495 if (complain)
5496 error ("call of overloaded `%s' is ambiguous", IDENTIFIER_POINTER (fnname));
5497 return error_mark_node;
5499 else
5500 rval = best_cp->function;
5502 else
5504 cp -= 1;
5505 if (cp->evil > 1)
5507 if (complain)
5508 error ("type conversion ambiguous");
5510 else
5511 rval = cp->function;
5514 if (final_cp)
5515 return rval;
5517 return build_function_call (rval, parms);
5519 else if (complain)
5521 tree name;
5522 char *err_name;
5523 /* Initialize name for error reporting. */
5524 if (TREE_CODE (functions) == TREE_LIST)
5525 name = TREE_PURPOSE (functions);
5526 else
5527 name = DECL_ORIGINAL_NAME (functions);
5529 if (OPERATOR_NAME_P (name))
5531 char *opname = operator_name_string (name);
5532 err_name = (char *)alloca (strlen (opname) + 12);
5533 sprintf (err_name, "operator %s", opname);
5535 else
5536 err_name = IDENTIFIER_POINTER (name);
5538 report_type_mismatch (cp, parms, "function", err_name);
5540 return error_mark_node;
5543 void
5544 init_class_processing ()
5546 current_class_stacksize = 10;
5547 current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
5548 current_class_stack = current_class_base;
5550 current_lang_stacksize = 10;
5551 current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
5552 current_lang_stack = current_lang_base;
5554 delta_name = get_identifier (VTABLE_DELTA_NAME);
5555 pfn_name = get_identifier (VTABLE_PFN_NAME);
5557 /* Keep these values lying around. */
5558 minus_one_node = build_int_2 (-1, 0);
5559 the_null_vtable_entry = build_vtable_entry (integer_zero_node, integer_zero_node);
5560 base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
5561 TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
5563 obstack_init (&class_obstack);
5566 /* Set current scope to NAME. CODE tells us if this is a
5567 STRUCT, UNION, or ENUM environment.
5569 NAME may end up being NULL_TREE if this is an anonymous or
5570 late-bound struct (as in "struct { ... } foo;") */
5572 /* Here's a subroutine we need because C lacks lambdas. */
5573 void
5574 unuse_fields (type)
5575 tree type;
5577 tree fields;
5579 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
5581 if (TREE_CODE (fields) != FIELD_DECL)
5582 continue;
5584 TREE_USED (fields) = 0;
5585 if (DECL_ANON_UNION_ELEM (fields))
5586 unuse_fields (TREE_TYPE (fields));
5590 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5591 appropriate values, found by looking up the type definition of
5592 NAME (as a CODE).
5594 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5595 which can be seen locally to the class. They are shadowed by
5596 any subsequent local declaration (including parameter names).
5598 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5599 which have static meaning (i.e., static members, static
5600 member functions, enum declarations, etc).
5602 So that we may avoid calls to lookup_name, we cache the TYPE_DECL
5603 in the TREE_TYPE field of the name.
5605 For multiple inheritance, we perform a two-pass depth-first search
5606 of the type lattice. The first pass performs a pre-order search,
5607 marking types after the type has had its fields installed in
5608 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5609 unmarks the marked types. If a field or member function name
5610 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5611 that name becomes `error_mark_node'. */
5613 void
5614 pushclass (type, modify)
5615 tree type;
5616 int modify;
5618 push_memoized_context (type, modify);
5620 *current_class_stack++ = current_class_name;
5621 *current_class_stack++ = current_class_type;
5622 if (current_class_stack >= current_class_base + current_class_stacksize)
5624 current_class_base =
5625 (tree *)xrealloc (current_class_base,
5626 sizeof (tree) * (current_class_stacksize + 10));
5627 current_class_stack = current_class_base + current_class_stacksize;
5628 current_class_stacksize += 10;
5631 type = TYPE_MAIN_VARIANT (type);
5632 current_class_name = TYPE_NAME (type);
5633 if (TREE_CODE (current_class_name) == TYPE_DECL)
5634 current_class_name = DECL_NAME (current_class_name);
5635 current_class_type = type;
5637 if (type != prev_class_type && prev_class_type != NULL_TREE
5638 && current_class_stack == current_class_base + 2)
5640 popclass (-1);
5641 prev_class_type = 0;
5644 if (modify)
5646 tree tags;
5648 if (type != prev_class_type)
5650 build_mi_matrix (type);
5651 push_class_decls (type);
5652 free_mi_matrix ();
5653 prev_class_type = type;
5655 else
5656 unuse_fields (type);
5658 tags = CLASSTYPE_TAGS (type);
5659 while (tags)
5661 TREE_NONLOCAL (TREE_VALUE (tags)) = 1;
5662 pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags));
5663 if (IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) == NULL_TREE
5664 && TREE_CODE (TYPE_NAME (TREE_VALUE (tags))) == TYPE_DECL)
5665 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags))
5666 = TYPE_NAME (TREE_VALUE (tags));
5667 tags = TREE_CHAIN (tags);
5670 else
5671 pushlevel_class ();
5673 if (flag_cadillac)
5674 cadillac_push_class (type);
5677 /* Get out of the current class scope. If we were in a class scope
5678 previously, that is the one popped to. The flag MODIFY tells
5679 whether the current scope declarations needs to be modified
5680 as a result of popping to the new scope. */
5681 void
5682 popclass (modify)
5683 int modify;
5685 if (flag_cadillac)
5686 cadillac_pop_class ();
5688 if (modify < 0)
5690 /* Back this old class out completely. */
5691 tree tags = CLASSTYPE_TAGS (prev_class_type);
5693 pop_class_decls (prev_class_type);
5694 while (tags)
5696 TREE_NONLOCAL (TREE_VALUE (tags)) = 0;
5697 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
5698 tags = TREE_CHAIN (tags);
5700 return;
5702 if (modify)
5704 /* Just remove from this class what didn't make
5705 it into IDENTIFIER_CLASS_VALUE. */
5706 tree tags = CLASSTYPE_TAGS (current_class_type);
5708 while (tags)
5710 TREE_NONLOCAL (TREE_VALUE (tags)) = 0;
5711 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (tags)) = NULL_TREE;
5712 tags = TREE_CHAIN (tags);
5715 else
5716 poplevel_class ();
5718 current_class_type = *--current_class_stack;
5719 current_class_name = *--current_class_stack;
5721 if (current_class_type)
5723 if (CLASSTYPE_VTBL_PTR (current_class_type))
5725 current_vtable_decl = lookup_name (DECL_NAME (CLASSTYPE_VTBL_PTR (current_class_type)));
5726 if (current_vtable_decl)
5727 current_vtable_decl = build_indirect_ref (current_vtable_decl, 0);
5729 current_class_decl = lookup_name (get_identifier (THIS_NAME));
5730 if (current_class_decl)
5732 if (TREE_CODE (TREE_TYPE (current_class_decl)) == POINTER_TYPE)
5734 /* Can't call build_indirect_ref here, because it has special
5735 logic to return C_C_D given this argument. */
5736 C_C_D = build1 (INDIRECT_REF, current_class_type, current_class_decl);
5737 TREE_READONLY (C_C_D) = TREE_READONLY (TREE_TYPE (TREE_TYPE (current_class_decl)));
5738 TREE_VOLATILE (C_C_D) = TREE_VOLATILE (TREE_TYPE (TREE_TYPE (current_class_decl)));
5740 else
5741 C_C_D = current_class_decl;
5743 else C_C_D = NULL_TREE;
5745 else
5747 current_class_decl = NULL_TREE;
5748 current_vtable_decl = NULL_TREE;
5749 C_C_D = NULL_TREE;
5752 pop_memoized_context (modify);
5755 /* Set global variables CURRENT_LANG_NAME to appropriate value
5756 so that behavior of name-mangline machinery is correct. */
5758 void
5759 push_lang_context (name)
5760 tree name;
5762 *current_lang_stack++ = current_lang_name;
5763 if (current_lang_stack >= current_lang_base + current_lang_stacksize)
5765 current_lang_base =
5766 (tree *)xrealloc (current_lang_base,
5767 sizeof (tree) * (current_lang_stacksize + 10));
5768 current_lang_stack = current_lang_base + current_lang_stacksize;
5769 current_lang_stacksize += 10;
5772 if (name == lang_name_cplusplus)
5774 strict_prototype = strict_prototypes_lang_cplusplus;
5775 current_lang_name = name;
5777 else if (name == lang_name_c)
5779 strict_prototype = strict_prototypes_lang_c;
5780 current_lang_name = name;
5782 else
5783 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5785 if (flag_cadillac)
5786 cadillac_push_lang (name);
5789 /* Get out of the current language scope. */
5790 void
5791 pop_lang_context ()
5793 if (flag_cadillac)
5794 cadillac_pop_lang ();
5796 current_lang_name = *--current_lang_stack;
5797 if (current_lang_name == lang_name_cplusplus)
5798 strict_prototype = strict_prototypes_lang_cplusplus;
5799 else if (current_lang_name == lang_name_c)
5800 strict_prototype = strict_prototypes_lang_c;
5804 root_lang_context_p ()
5806 return current_lang_stack == current_lang_base;
5809 /* Type instantiation routines. */
5811 /* This function will instantiate the type of the expression given
5812 in RHS to match the type of LHSTYPE. If LHSTYPE is NULL_TREE,
5813 or other errors exist, the TREE_TYPE of RHS will be ERROR_MARK_NODE.
5815 This function is used in build_modify_expr, actualparameterlist,
5816 build_c_cast, and compute_conversion_costs. */
5817 tree
5818 instantiate_type (lhstype, rhs, complain)
5819 tree lhstype, rhs;
5820 int complain;
5822 if (TREE_CODE (rhs) == OP_IDENTIFIER)
5823 return build_instantiated_decl (lhstype, rhs);
5825 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5827 if (complain)
5828 error ("not enough type information");
5829 return error_mark_node;
5832 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5833 return rhs;
5835 /* This should really only be used when attempting to distinguish
5836 what sort of a pointer to function we have. For now, any
5837 arithmethic operation which is not supported on pointers
5838 is rejected as an error. */
5840 switch (TREE_CODE (rhs))
5842 case TYPE_EXPR:
5843 case CONVERT_EXPR:
5844 case SAVE_EXPR:
5845 case CONSTRUCTOR:
5846 case BUFFER_REF:
5847 assert (0);
5848 return error_mark_node;
5850 case INDIRECT_REF:
5851 case ARRAY_REF:
5852 TREE_TYPE (rhs) = lhstype;
5853 lhstype = build_pointer_type (lhstype);
5854 TREE_OPERAND (rhs, 0)
5855 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
5856 if (TREE_OPERAND (rhs, 0) == error_mark_node)
5857 return error_mark_node;
5859 return rhs;
5861 case NOP_EXPR:
5862 rhs = copy_node (TREE_OPERAND (rhs, 0));
5863 TREE_TYPE (rhs) = unknown_type_node;
5864 return instantiate_type (lhstype, rhs, complain);
5866 case COMPONENT_REF:
5868 tree field = TREE_OPERAND (rhs, 1);
5869 if (TREE_CODE (field) == TREE_LIST)
5871 tree function = instantiate_type (lhstype, field, complain);
5872 if (function == error_mark_node)
5873 return error_mark_node;
5874 assert (TREE_CODE (function) == FUNCTION_DECL);
5875 if (DECL_VIRTUAL_P (function))
5877 tree base = TREE_OPERAND (rhs, 0);
5878 tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
5879 if (base_ptr == error_mark_node)
5880 return error_mark_node;
5881 base_ptr = convert_pointer_to (DECL_VCONTEXT (function), base_ptr);
5882 if (base_ptr == error_mark_node)
5883 return error_mark_node;
5884 return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
5886 return function;
5889 assert (TREE_CODE (field) == FIELD_DECL);
5890 assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
5891 || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE));
5893 TREE_TYPE (rhs) = lhstype;
5894 /* First look for an exact match */
5896 while (field && TREE_TYPE (field) != lhstype)
5897 field = TREE_CHAIN (field);
5898 if (field)
5900 TREE_OPERAND (rhs, 1) = field;
5901 return rhs;
5904 /* No exact match found, look for a compatible function. */
5905 field = TREE_OPERAND (rhs, 1);
5906 while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
5907 field = TREE_CHAIN (field);
5908 if (field)
5910 TREE_OPERAND (rhs, 1) = field;
5911 field = TREE_CHAIN (field);
5912 while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
5913 field = TREE_CHAIN (field);
5914 if (field)
5916 if (complain)
5917 error ("ambiguous overload for COMPONENT_REF requested");
5918 return error_mark_node;
5921 else
5923 if (complain)
5924 error ("no appropriate overload exists for COMPONENT_REF");
5925 return error_mark_node;
5927 return rhs;
5930 case TREE_LIST:
5932 tree elem, baselink, name;
5933 int globals = overloaded_globals_p (rhs);
5935 /* If there's only one function we know about, return that. */
5936 if (globals > 0 && TREE_CHAIN (rhs) == NULL_TREE)
5937 return TREE_VALUE (rhs);
5939 /* First look for an exact match. Search either overloaded
5940 functions or member functions. May have to undo what
5941 `default_conversion' or `datatype' might do to lhstype. */
5943 if (TREE_CODE (lhstype) == POINTER_TYPE)
5944 if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
5945 || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
5946 lhstype = TREE_TYPE (lhstype);
5947 else
5949 if (complain)
5950 error ("invalid type combination for overload");
5951 return error_mark_node;
5954 if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
5956 if (complain)
5957 error ("cannot resolve overloaded function `%s' based on non-function type",
5958 IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
5959 return error_mark_node;
5962 if (globals > 0)
5964 assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL);
5965 elem = rhs;
5966 while (elem)
5967 if (TREE_TYPE (TREE_VALUE (elem)) != lhstype)
5968 elem = TREE_CHAIN (elem);
5969 else
5970 return TREE_VALUE (elem);
5971 /* No exact match found, look for a compatible function. */
5972 elem = rhs;
5973 while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 1))
5974 elem = TREE_CHAIN (elem);
5975 if (elem)
5977 tree save_elem = TREE_VALUE (elem);
5978 elem = TREE_CHAIN (elem);
5979 while (elem && ! comp_target_types (lhstype, TREE_TYPE (TREE_VALUE (elem)), 0))
5980 elem = TREE_CHAIN (elem);
5981 if (elem)
5983 if (complain)
5984 error ("ambiguous overload for overloaded function requested");
5985 return error_mark_node;
5987 return save_elem;
5989 if (complain)
5991 if (TREE_CHAIN (rhs))
5992 error ("no appropriate overload for overloaded function `%s' exists",
5993 IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
5994 else
5995 error ("function `%s' has inappropriate type signature",
5996 IDENTIFIER_POINTER (TREE_PURPOSE (rhs)));
5998 return error_mark_node;
6001 if (TREE_NONLOCAL (rhs))
6003 /* Got to get it as a baselink. */
6004 rhs = lookup_fnfields (CLASSTYPE_AS_LIST (current_class_type),
6005 TREE_PURPOSE (rhs), 0);
6007 else
6009 assert (TREE_CHAIN (rhs) == NULL_TREE);
6010 if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
6011 rhs = TREE_VALUE (rhs);
6012 assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL);
6015 for (baselink = rhs; baselink;
6016 baselink = next_baselink (baselink))
6018 elem = TREE_VALUE (baselink);
6019 while (elem)
6020 if (TREE_TYPE (elem) != lhstype)
6021 elem = TREE_CHAIN (elem);
6022 else
6023 return elem;
6026 /* No exact match found, look for a compatible method. */
6027 for (baselink = rhs; baselink;
6028 baselink = next_baselink (baselink))
6030 elem = TREE_VALUE (baselink);
6031 while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 1))
6032 elem = TREE_CHAIN (elem);
6033 if (elem)
6035 tree save_elem = elem;
6036 elem = TREE_CHAIN (elem);
6037 while (elem && ! comp_target_types (lhstype, TREE_TYPE (elem), 0))
6038 elem = TREE_CHAIN (elem);
6039 if (elem)
6041 if (complain)
6042 error ("ambiguous overload for overloaded method requested");
6043 return error_mark_node;
6045 return save_elem;
6047 name = DECL_ORIGINAL_NAME (TREE_VALUE (rhs));
6048 if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
6050 /* Try to instantiate from non-member functions. */
6051 rhs = IDENTIFIER_GLOBAL_VALUE (name);
6052 if (rhs && TREE_CODE (rhs) == TREE_LIST)
6054 /* This code seems to be missing a `return'. */
6055 abort ();
6056 instantiate_type (lhstype, rhs, complain);
6060 if (complain)
6061 error ("no static member functions named `%s'",
6062 IDENTIFIER_POINTER (name));
6063 return error_mark_node;
6067 case CALL_EXPR:
6068 /* This is too hard for now. */
6069 assert (0);
6070 return error_mark_node;
6072 case PLUS_EXPR:
6073 case MINUS_EXPR:
6074 case COMPOUND_EXPR:
6075 TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
6076 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6077 return error_mark_node;
6078 TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
6079 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6080 return error_mark_node;
6082 TREE_TYPE (rhs) = lhstype;
6083 return rhs;
6085 case MULT_EXPR:
6086 case TRUNC_DIV_EXPR:
6087 case FLOOR_DIV_EXPR:
6088 case CEIL_DIV_EXPR:
6089 case ROUND_DIV_EXPR:
6090 case RDIV_EXPR:
6091 case TRUNC_MOD_EXPR:
6092 case FLOOR_MOD_EXPR:
6093 case CEIL_MOD_EXPR:
6094 case ROUND_MOD_EXPR:
6095 case FIX_ROUND_EXPR:
6096 case FIX_FLOOR_EXPR:
6097 case FIX_CEIL_EXPR:
6098 case FIX_TRUNC_EXPR:
6099 case FLOAT_EXPR:
6100 case NEGATE_EXPR:
6101 case ABS_EXPR:
6102 case MAX_EXPR:
6103 case MIN_EXPR:
6104 case FFS_EXPR:
6106 case BIT_AND_EXPR:
6107 case BIT_IOR_EXPR:
6108 case BIT_XOR_EXPR:
6109 case LSHIFT_EXPR:
6110 case RSHIFT_EXPR:
6111 case LROTATE_EXPR:
6112 case RROTATE_EXPR:
6114 case PREINCREMENT_EXPR:
6115 case PREDECREMENT_EXPR:
6116 case POSTINCREMENT_EXPR:
6117 case POSTDECREMENT_EXPR:
6118 if (complain)
6119 error ("illegal operation on uninstantiated type");
6120 return error_mark_node;
6122 case TRUTH_AND_EXPR:
6123 case TRUTH_OR_EXPR:
6124 case LT_EXPR:
6125 case LE_EXPR:
6126 case GT_EXPR:
6127 case GE_EXPR:
6128 case EQ_EXPR:
6129 case NE_EXPR:
6130 case TRUTH_ANDIF_EXPR:
6131 case TRUTH_ORIF_EXPR:
6132 case TRUTH_NOT_EXPR:
6133 if (complain)
6134 error ("not enough type information");
6135 return error_mark_node;
6137 case COND_EXPR:
6138 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6140 if (complain)
6141 error ("not enough type information");
6142 return error_mark_node;
6144 TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
6145 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6146 return error_mark_node;
6147 TREE_OPERAND (rhs, 2) = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
6148 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6149 return error_mark_node;
6151 TREE_TYPE (rhs) = lhstype;
6152 return rhs;
6154 case MODIFY_EXPR:
6155 TREE_OPERAND (rhs, 1) = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
6156 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6157 return error_mark_node;
6159 TREE_TYPE (rhs) = lhstype;
6160 return rhs;
6162 case ADDR_EXPR:
6163 if (TREE_CODE (lhstype) != POINTER_TYPE)
6165 if (complain)
6166 error ("type for resolving address of overloaded function must be pointer type");
6167 return error_mark_node;
6169 TREE_TYPE (rhs) = lhstype;
6170 lhstype = TREE_TYPE (lhstype);
6171 TREE_OPERAND (rhs, 0) = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
6172 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6173 return error_mark_node;
6175 mark_addressable (TREE_OPERAND (rhs, 0));
6176 return rhs;
6178 case ENTRY_VALUE_EXPR:
6179 assert (0);
6180 return error_mark_node;
6182 case ERROR_MARK:
6183 return error_mark_node;
6185 default:
6186 assert (0);
6187 return error_mark_node;
6191 /* This routine is called when we finally know the type of expression
6192 we are looking for. If the operator encoded by EXP can take an
6193 argument of type TYPE, return the FUNCTION_DECL for that operator. */
6194 tree
6195 build_instantiated_decl (type, exp)
6196 tree type, exp;
6198 tree parmtypes, decl, name;
6200 assert (TREE_CODE (exp) == OP_IDENTIFIER);
6201 type = datatype (type);
6202 if (TREE_CODE (type) != POINTER_TYPE
6203 || (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
6204 && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE))
6206 error ("invalid type used to resolve overloaded function");
6207 return error_mark_node;
6211 /* Now we know the type of this function, so overload it. */
6212 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (type));
6213 name = build_operator_fnname (&exp, parmtypes, 0);
6214 if (name)
6216 name = build_decl_overload (IDENTIFIER_POINTER (name), parmtypes, 1);
6217 decl = lookup_name (name);
6218 if (decl)
6219 return decl;
6220 error ("no suitable declaration of `operator %s' for overloading",
6221 operator_name_string (name));
6222 return error_mark_node;
6224 return error_mark_node;
6227 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
6228 for the given TYPE. */
6229 static tree
6230 get_vtable_name (type)
6231 tree type;
6233 char *buf = (char *)alloca (sizeof (VTABLE_NAME_FORMAT)
6234 + TYPE_NAME_LENGTH (type) + 2);
6235 sprintf (buf, VTABLE_NAME_FORMAT, TYPE_NAME_STRING (type));
6236 return get_identifier (buf);
6239 /* Return the name of the virtual function pointer field
6240 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6241 this may have to look back through base types to find the
6242 ultimate field name. (For single inheritance, these could
6243 all be the same name. Who knows for multiple inheritance). */
6244 static tree
6245 get_vfield_name (type)
6246 tree type;
6248 char *buf;
6250 while (CLASSTYPE_N_BASECLASSES (type)
6251 && TYPE_VIRTUAL_P (CLASSTYPE_BASECLASS (type, 1))
6252 && ! CLASSTYPE_VIA_VIRTUAL (type, 1))
6253 type = CLASSTYPE_BASECLASS (type, 1);
6255 buf = (char *)alloca (sizeof (VFIELD_NAME_FORMAT)
6256 + TYPE_NAME_LENGTH (type) + 2);
6257 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6258 return get_identifier (buf);
6261 void
6262 print_class_statistics ()
6264 #ifdef GATHER_STATISTICS
6265 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6266 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6267 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6268 n_build_method_call, n_inner_fields_searched);
6269 if (n_vtables)
6271 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6272 n_vtables, n_vtable_searches);
6273 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6274 n_vtable_entries, n_vtable_elems);
6276 #endif