Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / gcc / cp / sig.c
1 /* Functions dealing with signatures and signature pointers/references.
2    Copyright (C) 1992, 93-97, 1998 Free Software Foundation, Inc.
3    Contributed by Gerald Baumgartner (gb@cs.purdue.edu)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "obstack.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "flags.h"
29 #include "assert.h"
30 #include "toplev.h"
31
32 extern struct obstack *current_obstack;
33 extern struct obstack permanent_obstack;
34 extern struct obstack *saveable_obstack;
35
36 static tree save_this PROTO((tree));
37 static tree build_sptr_ref PROTO((tree));
38 static tree build_member_function_pointer PROTO((tree));
39 static void undo_casts PROTO((tree));
40 static tree build_signature_pointer_or_reference_name
41         PROTO((tree, int, int));
42 static void build_signature_pointer_or_reference_decl
43         PROTO((tree, tree));
44 static tree build_signature_pointer_or_reference_type 
45         PROTO((tree, int, int));
46 static tree get_sigtable_name PROTO((tree, tree));
47 static tree build_signature_table_constructor PROTO((tree, tree));
48 static int match_method_types PROTO((tree, tree));
49 static tree build_sigtable PROTO((tree, tree, tree));
50
51 /* Used to help generate globally unique names for signature tables.  */
52
53 static int global_sigtable_name_counter;
54
55 /* Build an identifier for a signature pointer or reference, so we
56    can use it's name in function name mangling.  */
57
58 static tree
59 build_signature_pointer_or_reference_name (to_type, type_quals, refp)
60      tree to_type;
61      int type_quals;
62      int refp;
63 {
64   const char * sig_name = TYPE_NAME_STRING (to_type);
65   int name_len = TYPE_NAME_LENGTH (to_type) + 3 /* Enough room for
66                                                    C,V,R.  */;
67   char * name;
68
69   const char *const_rep = (type_quals & TYPE_QUAL_CONST) ? "C" : "";
70   const char *restrict_rep = (type_quals & TYPE_QUAL_RESTRICT) ? "R" : ""; 
71   const char *volatile_rep = (type_quals & TYPE_QUAL_VOLATILE) ? "C" : "";
72
73   if (refp)
74     {
75       name = (char *) alloca (name_len + sizeof (SIGNATURE_REFERENCE_NAME) +2);
76       sprintf (name, SIGNATURE_REFERENCE_NAME_FORMAT,
77                const_rep, volatile_rep, restrict_rep, sig_name);
78     }
79   else
80     {
81       name = (char *) alloca (name_len + sizeof (SIGNATURE_POINTER_NAME) + 2);
82       sprintf (name, SIGNATURE_POINTER_NAME_FORMAT,
83                const_rep, volatile_rep, restrict_rep, sig_name);
84     }
85   return get_identifier (name);
86 }
87
88 /* Build a DECL node for a signature pointer or reference, so we can
89    tell the debugger the structure of signature pointers/references.
90    This function is called at most eight times for a given signature,
91    once for each [const] [volatile] signature pointer/reference.  */
92
93 static void
94 build_signature_pointer_or_reference_decl (type, name)
95      tree type, name;
96 {
97   tree decl;
98
99   /* We don't enter this declaration in any sort of symbol table.  */
100   decl = build_decl (TYPE_DECL, name, type);
101   TYPE_NAME (type) = decl;
102   TREE_CHAIN (type) = decl;
103 }
104
105 /* Construct, lay out and return the type of pointers or references to
106    signature TO_TYPE.  If such a type has already been constructed,
107    reuse it. If TYPE_QUALS are specified, qualify the `optr'.  If we
108    are constructing a const/volatile type variant and the main type
109    variant doesn't exist yet, it is built as well.  If REFP is 1, we
110    construct a signature reference, otherwise a signature pointer is
111    constructed.
112
113    This function is a subroutine of `build_signature_pointer_type' and
114    `build_signature_reference_type'.  */
115
116 static tree
117 build_signature_pointer_or_reference_type (to_type, type_quals, refp)
118      tree to_type;
119      int type_quals;
120      int refp;
121 {
122   register tree t, m;
123   register struct obstack *ambient_obstack = current_obstack;
124   register struct obstack *ambient_saveable_obstack = saveable_obstack;
125
126   m = refp ? SIGNATURE_REFERENCE_TO (to_type) : SIGNATURE_POINTER_TO (to_type);
127
128   /* If we don't have the main variant yet, construct it.  */
129   if (m == NULL_TREE && type_quals != TYPE_UNQUALIFIED)
130     m = build_signature_pointer_or_reference_type (to_type, 
131                                                    TYPE_UNQUALIFIED, refp);
132
133   /* Treat any nonzero argument as 1.  */
134   refp = !!refp;
135
136   /* If not generating auxiliary info, search the chain of variants to see
137      if there is already one there just like the one we need to have.  If so,
138      use that existing one.
139
140      We don't do this in the case where we are generating aux info because
141      in that case we want each typedef names to get it's own distinct type
142      node, even if the type of this new typedef is the same as some other
143      (existing) type.  */
144
145   if (m && !flag_gen_aux_info)
146     for (t = m; t; t = TYPE_NEXT_VARIANT (t))
147       if (type_quals == CP_TYPE_QUALS (TREE_TYPE (TREE_TYPE
148                                                   (TYPE_FIELDS (t)))))
149         return t;
150
151   /* We need a new one.  If TO_TYPE is permanent, make this permanent too.  */
152   if (TREE_PERMANENT (to_type))
153     {
154       current_obstack = &permanent_obstack;
155       saveable_obstack = &permanent_obstack;
156     }
157
158   /* A signature pointer or reference to a signature `s' looks like this:
159
160        struct {
161          void * optr;
162          const s * sptr;
163        };
164
165      A `const' signature pointer/reference is a
166
167        struct {
168          const void * optr;
169          const s * sptr;
170        };
171
172      Similarly, for `volatile' and `const volatile'.  */
173
174   t = make_lang_type (RECORD_TYPE);
175   {
176     tree obj_type = build_qualified_type (void_type_node, type_quals);
177     tree optr_type = build_pointer_type (obj_type);
178     tree optr, sptr;
179
180     optr = build_lang_field_decl (FIELD_DECL,
181                                   get_identifier (SIGNATURE_OPTR_NAME),
182                                   optr_type);
183     DECL_FIELD_CONTEXT (optr) = t;
184     DECL_CLASS_CONTEXT (optr) = t;
185
186     if (m)
187       /* We can share the `sptr' field among type variants.  */
188       sptr = TREE_CHAIN (TYPE_FIELDS (m));
189     else
190       {
191         tree sig_tbl_type = 
192           cp_build_qualified_type (to_type, TYPE_QUAL_CONST);
193         
194         sptr = build_lang_field_decl (FIELD_DECL,
195                                       get_identifier (SIGNATURE_SPTR_NAME),
196                                       build_pointer_type (sig_tbl_type));
197         DECL_FIELD_CONTEXT (sptr) = t;
198         DECL_CLASS_CONTEXT (sptr) = t;
199         TREE_CHAIN (sptr) = NULL_TREE;
200       }
201
202     TREE_CHAIN (optr) = sptr;
203     TYPE_FIELDS (t) = optr;
204     /* Allow signature pointers/references to be grabbed 2 words at a time.
205        For this to work on a Sparc, we need 8-byte alignment.  */
206     TYPE_ALIGN (t) = MAX (TYPE_ALIGN (double_type_node),
207                           TYPE_ALIGN (optr_type));
208
209     /* A signature pointer/reference type isn't a `real' class type.  */
210     SET_IS_AGGR_TYPE (t, 0);
211   }
212
213   {
214     tree name = build_signature_pointer_or_reference_name (to_type, 
215                                                            type_quals,
216                                                            refp);
217
218     /* Build a DECL node for this type, so the debugger has access to it.  */
219     build_signature_pointer_or_reference_decl (t, name);
220   }
221
222   CLASSTYPE_GOT_SEMICOLON (t) = 1;
223   IS_SIGNATURE_POINTER (t) = ! refp;
224   IS_SIGNATURE_REFERENCE (t) = refp;
225   SIGNATURE_TYPE (t) = to_type;
226
227   if (m)
228     {
229       /* Add this type to the chain of variants of TYPE.
230          Every type has to be its own TYPE_MAIN_VARIANT.  */
231       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
232       TYPE_NEXT_VARIANT (m) = t;
233     }
234   else if (refp)
235     /* Record this type as the reference to TO_TYPE.  */
236     SIGNATURE_REFERENCE_TO (to_type) = t;
237   else
238     /* Record this type as the pointer to TO_TYPE.  */
239     SIGNATURE_POINTER_TO (to_type) = t;
240
241   /* Lay out the type.  This function has many callers that are concerned
242      with expression-construction, and this simplifies them all.
243      Also, it guarantees the TYPE_SIZE is permanent if the type is.  */
244   layout_type (t);
245
246   current_obstack = ambient_obstack;
247   saveable_obstack = ambient_saveable_obstack;
248
249   /* Output debug information for this type.  */
250   rest_of_type_compilation (t, 1);
251
252   return t;
253 }
254
255 /* Construct, lay out and return the type of pointers to signature TO_TYPE.  */
256
257 tree
258 build_signature_pointer_type (to_type)
259      tree to_type;
260 {
261   return
262     build_signature_pointer_or_reference_type (TYPE_MAIN_VARIANT (to_type),
263                                                CP_TYPE_QUALS (to_type), 0);
264 }
265
266 /* Construct, lay out and return the type of pointers to signature TO_TYPE.  */
267
268 tree
269 build_signature_reference_type (to_type)
270      tree to_type;
271 {
272   return
273     build_signature_pointer_or_reference_type (TYPE_MAIN_VARIANT (to_type),
274                                                CP_TYPE_QUALS (to_type), 1);
275 }
276
277 /* Return the name of the signature table (as an IDENTIFIER_NODE)
278    for the given signature type SIG_TYPE and rhs type RHS_TYPE.  */
279
280 static tree
281 get_sigtable_name (sig_type, rhs_type)
282      tree sig_type, rhs_type;
283 {
284   tree sig_type_id = build_typename_overload (sig_type);
285   tree rhs_type_id = build_typename_overload (rhs_type);
286   char *buf = (char *) alloca (sizeof (SIGTABLE_NAME_FORMAT_LONG)
287                                + IDENTIFIER_LENGTH (sig_type_id)
288                                + IDENTIFIER_LENGTH (rhs_type_id) + 20);
289   const char *sig_ptr = IDENTIFIER_POINTER (sig_type_id);
290   const char *rhs_ptr = IDENTIFIER_POINTER (rhs_type_id);
291   int i, j;
292
293   for (i = 0; sig_ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++)
294     /* do nothing */;
295   while (sig_ptr[i] >= '0' && sig_ptr[i] <= '9')
296     i += 1;
297
298   for (j = 0; rhs_ptr[j] == OPERATOR_TYPENAME_FORMAT[j]; j++)
299     /* do nothing */;
300   while (rhs_ptr[j] >= '0' && rhs_ptr[j] <= '9')
301     j += 1;
302
303   if (IS_SIGNATURE (rhs_type))
304     sprintf (buf, SIGTABLE_NAME_FORMAT_LONG, sig_ptr+i, rhs_ptr+j,
305              global_sigtable_name_counter++);
306   else
307     sprintf (buf, SIGTABLE_NAME_FORMAT, sig_ptr+i, rhs_ptr+j);
308   return get_identifier (buf);
309 }
310
311 /* Build a field decl that points to a signature member function.  */
312
313 static tree
314 build_member_function_pointer (member)
315      tree member;
316 {
317   const char *namstr = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (member));
318   int namlen = IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (member));
319   char *name;
320   tree entry;
321   
322   name = (char *) alloca (namlen + sizeof (SIGNATURE_FIELD_NAME) + 2);
323   sprintf (name, SIGNATURE_FIELD_NAME_FORMAT, namstr);
324
325   /* @@ Do we really want to xref signature table fields?  */
326   GNU_xref_ref (current_function_decl, name);
327
328   entry = build_lang_field_decl (FIELD_DECL, get_identifier (name),
329                                  sigtable_entry_type);
330   TREE_CONSTANT (entry) = 1;
331   TREE_READONLY (entry) = 1;
332
333   /* @@ Do we really want to xref signature table fields?  */
334   GNU_xref_decl (current_function_decl, entry);
335
336   return entry;
337 }
338
339 /* For each FUNCTION_DECL in a signature we construct a member function
340    pointer of the appropriate type.  We also need two flags to test
341    whether the member function pointer points to a virtual function or
342    to a default implementation.  Those flags will be the two lower order
343    bits of the member function pointer (or the two higher order bits,
344    based on the configuration).
345
346    The new FIELD_DECLs are appended at the end of the last (and only)
347    sublist of `list_of_fieldlists.'
348
349    T is the signature type.
350   
351    As a side effect, each member function in the signature gets the
352    `decl.ignored' bit turned on, so we don't output debug info for it.  */
353
354 void
355 append_signature_fields (t)
356      tree t;
357 {
358   tree x;
359   tree mfptr;
360   tree last_mfptr = NULL_TREE;
361   tree mfptr_list = NULL_TREE;
362               
363   for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
364     {
365       if (TREE_CODE (x) == FUNCTION_DECL)
366         {
367           mfptr = build_member_function_pointer (x);
368           DECL_MEMFUNC_POINTER_TO (x) = mfptr;
369           DECL_MEMFUNC_POINTING_TO (mfptr) = x;
370           DECL_IGNORED_P (x) = 1;
371           DECL_IN_AGGR_P (mfptr) = 1;
372           if (! mfptr_list)
373             mfptr_list = last_mfptr = mfptr;
374           else
375             {
376               TREE_CHAIN (last_mfptr) = mfptr;
377               last_mfptr = mfptr;
378             }
379         }
380     }
381
382   /* The member function pointers must come after the TYPE_DECLs, in
383      this case, because build_signature_table_constructor depends on
384      finding opaque TYPE_DECLS before the functions that make use of
385      them.  */
386   if (last_mfptr)
387     TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), mfptr_list);
388 }
389
390 /* Compare the types of a signature member function and a class member
391    function.  Returns 1 if the types are in the C++ `<=' relationship.
392
393    If we have a signature pointer/reference as argument or return type
394    we don't want to do a recursive conformance check.  The conformance
395    check only succeeds if both LHS and RHS refer to the same signature
396    pointer.  Otherwise we need to keep information about parameter types
397    around at run time to initialize the signature table correctly.  */
398
399 static int
400 match_method_types (sig_mtype, class_mtype)
401      tree sig_mtype, class_mtype;
402 {
403   tree sig_return_type = TREE_TYPE (sig_mtype);
404   tree sig_arg_types = TYPE_ARG_TYPES (sig_mtype);
405   tree class_return_type = TREE_TYPE (class_mtype);
406   tree class_arg_types = TYPE_ARG_TYPES (class_mtype);
407
408   /* The return types have to be the same.  */
409   if (!same_type_p (sig_return_type, class_return_type))
410     return 0;
411
412   /* Compare the first argument `this.'  */
413   {
414     /* Get the type of what the `optr' is pointing to.  */
415     tree sig_this
416       = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_VALUE (sig_arg_types))));
417     tree class_this = TREE_VALUE (class_arg_types);
418
419     if (TREE_CODE (class_this) == RECORD_TYPE)  /* Is `this' a sig ptr?  */
420       class_this = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (class_this)));
421     else
422       class_this = TREE_TYPE (class_this);
423
424     /* If a signature method's `this' is const or volatile, so has to be
425        the corresponding class method's `this.'  */
426     if (!at_least_as_qualified_p (class_this, sig_this))
427       return 0;
428   }
429
430   sig_arg_types = TREE_CHAIN (sig_arg_types);
431   class_arg_types = TREE_CHAIN (class_arg_types);
432
433   /* The number of arguments and the argument types have to be the same.  */
434   return compparms (sig_arg_types, class_arg_types);
435 }
436
437 /* Undo casts of opaque type variables to the RHS types.  */
438
439 static void
440 undo_casts (sig_ty)
441      tree sig_ty;
442 {
443   tree field = TYPE_FIELDS (sig_ty);
444
445   /* Since all the FIELD_DECLs for the signature table entries are at the end
446      of the chain (see `append_signature_fields'), we can do it this way.  */
447   for (; field && TREE_CODE (field) != FIELD_DECL; field = TREE_CHAIN (field))
448     if (TYPE_MAIN_VARIANT (TREE_TYPE (field)) == opaque_type_node)
449       TREE_TYPE (TREE_TYPE (field)) = TREE_TYPE (ptr_type_node);
450 }
451
452 /* Do the type checking necessary to see whether the `rhs' conforms to
453    the lhs's `sig_ty'.  Depending on the type of `rhs' return a NULL_TREE,
454    an integer_zero_node, a constructor, or an expression offsetting the
455    `rhs' signature table.  */
456
457 static tree
458 build_signature_table_constructor (sig_ty, rhs)
459      tree sig_ty, rhs;
460 {
461   tree rhstype = TREE_TYPE (rhs);
462   tree sig_field = TYPE_FIELDS (sig_ty);
463   tree result = NULL_TREE;
464   tree first_rhs_field = NULL_TREE;
465   tree last_rhs_field = NULL_TREE;
466   int sig_ptr_p = IS_SIGNATURE (rhstype);
467   int offset_p = sig_ptr_p;
468
469   rhstype = sig_ptr_p ? rhstype : TREE_TYPE (rhstype);
470
471   if (CLASSTYPE_TAGS (sig_ty))
472     {
473       sorry ("conformance check with signature containing class declarations");
474       return error_mark_node;
475     }
476
477   for (; sig_field; sig_field = TREE_CHAIN (sig_field))
478     {
479       tree basetype_path, baselink, basetypes;
480       tree sig_method, sig_mname, sig_mtype;
481       tree rhs_method, tbl_entry;
482
483       if (TREE_CODE (sig_field) == TYPE_DECL)
484         {
485           tree sig_field_type = TREE_TYPE (sig_field);
486
487           if (TYPE_MAIN_VARIANT (sig_field_type) == opaque_type_node)
488             {
489               /* We've got an opaque type here.  */
490               tree oty_name = DECL_NAME (sig_field);
491               tree oty_type = lookup_field (rhstype, oty_name, 1, 1);
492
493               if (oty_type == NULL_TREE || oty_type == error_mark_node)
494                 {
495                   cp_error ("class `%T' does not contain type `%T'",
496                             rhstype, oty_type);
497                   undo_casts (sig_ty);
498                   return error_mark_node;
499                 }
500               oty_type = TREE_TYPE (oty_type);
501
502               /* Cast `sig_field' to be of type `oty_type'.  This will be
503                  undone in `undo_casts' by walking over all the TYPE_DECLs.  */
504               TREE_TYPE (sig_field_type) = TREE_TYPE (oty_type);
505             }
506           /* If we don't have an opaque type, we can ignore the `typedef'.  */
507           continue;
508         }
509
510       /* Find the signature method corresponding to `sig_field'.  */
511       sig_method = DECL_MEMFUNC_POINTING_TO (sig_field);
512       sig_mname = DECL_NAME (sig_method);
513       sig_mtype = TREE_TYPE (sig_method);
514
515       basetype_path = TYPE_BINFO (rhstype);
516       baselink = lookup_fnfields (basetype_path, sig_mname, 0);
517       if (baselink == NULL_TREE || baselink == error_mark_node)
518         {
519           if (! IS_DEFAULT_IMPLEMENTATION (sig_method))
520             {
521               cp_error ("class `%T' does not contain method `%D'",
522                         rhstype, sig_mname);
523               undo_casts (sig_ty);
524               return error_mark_node;
525             }
526           else
527             {
528               /* We use the signature's default implementation.  */
529               rhs_method = sig_method;
530             }
531         }
532       else
533         {
534           /* Find the class method of the correct type.  */
535           tree rhs_methods;
536           basetypes = TREE_PURPOSE (baselink);
537           if (TREE_CODE (basetypes) == TREE_LIST)
538             basetypes = TREE_VALUE (basetypes);
539
540           rhs_methods = TREE_VALUE (baselink);
541           for (; rhs_methods; rhs_methods = OVL_NEXT (rhs_methods))
542             if ((rhs_method = OVL_CURRENT (rhs_methods))
543                 && sig_mname == DECL_NAME (rhs_method)
544                 && ! DECL_STATIC_FUNCTION_P (rhs_method)
545                 && match_method_types (sig_mtype, TREE_TYPE (rhs_method)))
546               break;
547
548           if (rhs_methods == NULL_TREE
549               || !accessible_p (basetypes, rhs_method))
550             {
551               cp_error ("`%T' does not contain a method conforming to `%#D'",
552                      rhstype, sig_method);
553               undo_casts (sig_ty);
554               return error_mark_node;
555             }
556         }
557
558       if (sig_ptr_p && rhs_method != sig_method)
559         {
560           tree rhs_field = DECL_MEMFUNC_POINTER_TO (rhs_method);
561
562           if (first_rhs_field == NULL_TREE)
563             {
564               first_rhs_field = rhs_field;
565               last_rhs_field = rhs_field;
566             }
567           else if (TREE_CHAIN (last_rhs_field) == rhs_field)
568             last_rhs_field = rhs_field;
569           else
570             offset_p = 0;
571           
572           tbl_entry = build_component_ref (rhs, DECL_NAME (rhs_field),
573                                            NULL_TREE, 1);
574         }
575       else
576         {
577           tree tag, vb_off, delta, idx, pfn = NULL_TREE, vt_off = NULL_TREE;
578           tree tag_decl, vb_off_decl, delta_decl, index_decl;
579           tree pfn_decl, vt_off_decl;
580
581           if (rhs_method == sig_method)
582             {
583               /* default implementation */
584               tag = build_unary_op (NEGATE_EXPR, integer_one_node, 0);
585               vb_off = build_unary_op (NEGATE_EXPR, integer_one_node, 0);
586               delta = integer_zero_node;
587               idx = integer_zero_node;
588               pfn = build_addr_func (rhs_method);
589               TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (rhs_method)) = 1;
590               TREE_TYPE (pfn) = ptr_type_node;
591               TREE_ADDRESSABLE (rhs_method) = 1;
592               offset_p = 0;     /* we can't offset the rhs sig table */
593             }
594           else if (DECL_VINDEX (rhs_method))
595             {
596               /* virtual member function */
597               tag = integer_one_node;
598               vb_off = build_unary_op (NEGATE_EXPR, integer_one_node, 0);
599               if (flag_vtable_thunks)
600                 delta = BINFO_OFFSET
601                   (get_binfo (DECL_CONTEXT (rhs_method), rhstype, 1));
602               else
603                 delta = BINFO_OFFSET
604                   (get_binfo (DECL_CLASS_CONTEXT (rhs_method), rhstype, 1));
605               idx = DECL_VINDEX (rhs_method);
606               vt_off = get_vfield_offset (get_binfo (DECL_CONTEXT (rhs_method),
607                                                      rhstype, 0));
608             }
609           else
610             {
611               /* non-virtual member function */
612               tag = integer_zero_node;
613               vb_off = build_unary_op (NEGATE_EXPR, integer_one_node, 0);
614               delta = BINFO_OFFSET (get_binfo (DECL_CLASS_CONTEXT (rhs_method),
615                                                rhstype, 1));
616               idx = integer_zero_node;
617               pfn = build_addr_func (rhs_method);
618               TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (rhs_method)) = 1;
619               TREE_TYPE (pfn) = ptr_type_node;
620               TREE_ADDRESSABLE (rhs_method) = 1;
621             }
622
623           /* Since digest_init doesn't handle initializing selected fields
624              of a struct (i.e., anonymous union), we build the constructor
625              by hand, without calling digest_init.  */
626           tag_decl = TYPE_FIELDS (sigtable_entry_type);
627           vb_off_decl = TREE_CHAIN (tag_decl);
628           delta_decl = TREE_CHAIN (vb_off_decl);
629           index_decl = TREE_CHAIN (delta_decl);
630           pfn_decl = TREE_CHAIN (index_decl);
631           vt_off_decl = TREE_CHAIN (pfn_decl);
632           
633           tag = cp_convert (TREE_TYPE (tag_decl), tag);
634           vb_off = cp_convert (TREE_TYPE (vb_off_decl), vb_off);
635           delta = cp_convert (TREE_TYPE (delta_decl), delta);
636           idx = cp_convert (TREE_TYPE (index_decl), idx);
637
638           if (DECL_VINDEX (rhs_method))
639             {
640               vt_off = cp_convert (TREE_TYPE (vt_off_decl), vt_off);
641
642               tbl_entry = build_tree_list (vt_off_decl, vt_off);
643             }
644           else
645             {
646               pfn = cp_convert (TREE_TYPE (pfn_decl), pfn);
647
648               tbl_entry = build_tree_list (pfn_decl, pfn);
649             }
650           tbl_entry = tree_cons (delta_decl, delta,
651                                  tree_cons (index_decl, idx, tbl_entry));
652           tbl_entry = tree_cons (tag_decl, tag,
653                                  tree_cons (vb_off_decl, vb_off, tbl_entry));
654           tbl_entry = build (CONSTRUCTOR, sigtable_entry_type,
655                              NULL_TREE, tbl_entry);
656
657           TREE_CONSTANT (tbl_entry) = 1;
658         }
659
660       /* Chain those function address expressions together.  */
661       if (result)
662         result = tree_cons (NULL_TREE, tbl_entry, result);
663       else
664         result = build_tree_list (NULL_TREE, tbl_entry);
665     }
666
667   if (result == NULL_TREE)
668     {
669       /* The signature was empty, we don't need a signature table.  */
670       undo_casts (sig_ty);
671       return NULL_TREE;
672     }
673
674   if (offset_p)
675     {
676       if (first_rhs_field == TYPE_FIELDS (rhstype))
677         {
678           /* The sptr field on the lhs can be copied from the rhs.  */
679           undo_casts (sig_ty);
680           return integer_zero_node;
681         }
682       else
683         {
684           /* The sptr field on the lhs will point into the rhs sigtable.  */
685           undo_casts (sig_ty);
686           return build_component_ref (rhs, DECL_NAME (first_rhs_field),
687                                       NULL_TREE, 0);
688         }
689     }
690
691   /* We need to construct a new signature table.  */
692   result = build_nt (CONSTRUCTOR, NULL_TREE, nreverse (result));
693   TREE_HAS_CONSTRUCTOR (result) = 1;
694   TREE_CONSTANT (result) = !sig_ptr_p;
695
696   undo_casts (sig_ty);
697   return result;
698 }
699
700 /* Build a signature table declaration and initialize it or return an
701    existing one if we built one already.  If we don't get a constructor
702    as initialization expression, we don't need a new signature table
703    variable and just hand back the init expression.
704
705    The declaration processing is done by hand instead of using `cp_finish_decl'
706    so that we can make signature pointers global variables instead of
707    static ones.  */
708
709 static tree
710 build_sigtable (sig_type, rhs_type, init_from)
711      tree sig_type, rhs_type, init_from;
712 {
713   tree name = NULL_TREE;
714   tree decl = NULL_TREE;
715   tree init_expr;
716
717   push_obstacks_nochange ();
718   end_temporary_allocation ();
719
720   if (! IS_SIGNATURE (rhs_type))
721     {
722       name = get_sigtable_name (sig_type, rhs_type);
723       decl = IDENTIFIER_GLOBAL_VALUE (name);
724     }
725   if (decl == NULL_TREE)
726     {
727       tree init = NULL_TREE;
728
729       /* We allow only one signature table to be generated for signatures
730          with opaque types.  Otherwise we create a loophole in the type
731          system since we could cast data from one classes implementation
732          of the opaque type to that of another class.  */
733       if (SIGNATURE_HAS_OPAQUE_TYPEDECLS (sig_type)
734           && SIGTABLE_HAS_BEEN_GENERATED (sig_type))
735         {
736           error ("signature with opaque type implemented by multiple classes");
737           return error_mark_node;
738         }
739       SIGTABLE_HAS_BEEN_GENERATED (sig_type) = 1;
740
741       init_expr = build_signature_table_constructor (sig_type, init_from);
742       if (init_expr == NULL_TREE || TREE_CODE (init_expr) != CONSTRUCTOR)
743         return init_expr;
744
745       if (name == NULL_TREE)
746         name = get_sigtable_name (sig_type, rhs_type);
747       {
748         tree context = current_function_decl;
749
750         /* Make the signature table global, not just static in whichever
751            function a signature pointer/ref is used for the first time.  */
752         current_function_decl = NULL_TREE;
753         decl = pushdecl_top_level (build_decl (VAR_DECL, name, sig_type));
754         current_function_decl = context;
755       }
756       SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
757       store_init_value (decl, init_expr);
758       if (IS_SIGNATURE (rhs_type))
759         {
760           init = DECL_INITIAL (decl);
761           DECL_INITIAL (decl) = error_mark_node;
762         }
763
764       DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
765                                DECL_ALIGN (decl));
766 #if 0
767       /* GDB-4.7 doesn't find the initialization value of a signature table
768          when it is constant.  */
769       TREE_READONLY (decl) = 1;
770 #endif
771       TREE_STATIC (decl) = 1;
772       TREE_USED (decl) = 1;
773
774       make_decl_rtl (decl, NULL, 1);
775       if (IS_SIGNATURE (rhs_type))
776         expand_static_init (decl, init);
777     }
778
779   pop_obstacks ();
780
781   return decl;
782 }
783
784 /* Create a constructor or modify expression if the LHS of an assignment
785    is a signature pointer or a signature reference.  If LHS is a record
786    type node, we build a constructor, otherwise a compound expression.  */
787
788 tree
789 build_signature_pointer_constructor (lhs, rhs)
790      tree lhs, rhs;
791 {
792   register struct obstack *ambient_obstack = current_obstack;
793   register struct obstack *ambient_saveable_obstack = saveable_obstack;
794   int initp = (TREE_CODE (lhs) == RECORD_TYPE);
795   tree lhstype = initp ? lhs : TREE_TYPE (lhs);
796   tree rhstype = TREE_TYPE (rhs);
797   tree sig_ty  = SIGNATURE_TYPE (lhstype);
798   tree sig_tbl, sptr_expr, optr_expr;
799   tree result;
800
801   if (! ((TREE_CODE (rhstype) == POINTER_TYPE
802           && TREE_CODE (TREE_TYPE (rhstype)) == RECORD_TYPE)
803          || (TYPE_LANG_SPECIFIC (rhstype)
804              && (IS_SIGNATURE_POINTER (rhstype)
805                  || IS_SIGNATURE_REFERENCE (rhstype)))))
806     {
807       error ("invalid assignment to signature pointer or reference");
808       return error_mark_node;
809     }
810
811   if (TYPE_SIZE (sig_ty) == NULL_TREE)
812     {
813       cp_error ("undefined signature `%T' used in signature %s declaration",
814                 sig_ty,
815                 IS_SIGNATURE_POINTER (lhstype) ? "pointer" : "reference");
816       return error_mark_node;
817     }
818
819   /* If SIG_TY is permanent, make the signature table constructor and
820      the signature pointer/reference constructor permanent too.  */
821   if (TREE_PERMANENT (sig_ty))
822     {
823       current_obstack = &permanent_obstack;
824       saveable_obstack = &permanent_obstack;
825     }
826
827   if (TYPE_LANG_SPECIFIC (rhstype)
828       && (IS_SIGNATURE_POINTER (rhstype) || IS_SIGNATURE_REFERENCE (rhstype)))
829     {
830       if (SIGNATURE_TYPE (rhstype) == sig_ty)
831         {
832           /* LHS and RHS are signature pointers/refs of the same signature.  */
833           optr_expr = build_optr_ref (rhs);
834           sptr_expr = build_sptr_ref (rhs);
835         }
836       else
837         {
838           /* We need to create a new signature table and copy
839              elements from the rhs signature table.  */
840           tree rhs_sptr_ref = build_sptr_ref (rhs);
841           tree rhs_tbl = build1 (INDIRECT_REF, SIGNATURE_TYPE (rhstype),
842                                  rhs_sptr_ref);
843
844           sig_tbl = build_sigtable (sig_ty, SIGNATURE_TYPE (rhstype), rhs_tbl);
845           if (sig_tbl == error_mark_node)
846             return error_mark_node;
847
848           optr_expr = build_optr_ref (rhs);
849           if (sig_tbl == NULL_TREE)
850             /* The signature was empty.  The signature pointer is
851                pretty useless, but the user has been warned.  */
852             sptr_expr = copy_node (null_pointer_node);
853           else if (sig_tbl == integer_zero_node)
854             sptr_expr = rhs_sptr_ref;
855           else
856             sptr_expr = build_unary_op (ADDR_EXPR, sig_tbl, 0);
857           TREE_TYPE (sptr_expr) = build_pointer_type (sig_ty);
858         }
859     }
860   else
861     {
862       sig_tbl = build_sigtable (sig_ty, TREE_TYPE (rhstype), rhs);
863       if (sig_tbl == error_mark_node)
864         return error_mark_node;
865
866       optr_expr = rhs;
867       if (sig_tbl == NULL_TREE)
868         /* The signature was empty.  The signature pointer is
869            pretty useless, but the user has been warned.  */
870         {
871           sptr_expr = copy_node (null_pointer_node);
872           TREE_TYPE (sptr_expr) = build_pointer_type (sig_ty);
873         }
874       else
875         sptr_expr = build_unary_op (ADDR_EXPR, sig_tbl, 0);
876     }
877
878   if (initp)
879     {
880       result = tree_cons (NULL_TREE, optr_expr,
881                           build_tree_list (NULL_TREE, sptr_expr));
882       result = build_nt (CONSTRUCTOR, NULL_TREE, result);
883       result = digest_init (lhstype, result, 0);
884     }
885   else
886     {
887       if (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype))
888           readonly_error (lhs, "assignment", 0);
889
890       optr_expr = build_modify_expr (build_optr_ref (lhs), NOP_EXPR,
891                                      optr_expr);
892       sptr_expr = build_modify_expr (build_sptr_ref (lhs), NOP_EXPR,
893                                      sptr_expr);
894
895       result = tree_cons (NULL_TREE, optr_expr,
896                           tree_cons (NULL_TREE, sptr_expr,
897                                      build_tree_list (NULL_TREE, lhs)));
898       result = build_compound_expr (result);
899     }
900
901   current_obstack = ambient_obstack;
902   saveable_obstack = ambient_saveable_obstack;
903   return result;
904 }
905
906 /* Build a temporary variable declaration for the instance of a signature
907    member function call if it isn't a declaration node already.  Simply
908    using a SAVE_EXPR doesn't work since we need `this' in both branches
909    of a conditional expression.  */
910
911 static tree
912 save_this (instance)
913      tree instance;
914 {
915   tree decl;
916
917   if (TREE_CODE_CLASS (TREE_CODE (instance)) == 'd')
918     decl = instance;
919   else
920     {
921       decl = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (instance));
922       DECL_REGISTER (decl) = 1;
923       layout_decl (decl, 0);
924       expand_decl (decl);
925     }
926
927   return decl;
928 }
929
930 /* Build a signature member function call.  Looks up the signature table
931    entry corresponding to FUNCTION.  Depending on the value of the CODE
932    field, either call the function in PFN directly, or use OFFSET to
933    index the object's virtual function table.  */
934
935 tree
936 build_signature_method_call (function, parms)
937      tree function, parms;
938 {
939   tree instance = TREE_VALUE (parms);
940   tree saved_instance = save_this (instance);   /* Create temp for `this'.  */
941   tree object_ptr = build_optr_ref (saved_instance);
942   tree new_object_ptr, new_parms;
943   tree signature_tbl_ptr = build_sptr_ref (saved_instance);
944   tree sig_field_name = DECL_NAME (DECL_MEMFUNC_POINTER_TO (function));
945   tree basetype = DECL_CONTEXT (function);
946   tree basetype_path = TYPE_BINFO (basetype);
947   tree tbl_entry = build_component_ref (build1 (INDIRECT_REF, basetype,
948                                                 signature_tbl_ptr),
949                                         sig_field_name, basetype_path, 1);
950   tree tag, delta, pfn, vt_off, idx, vfn;
951   tree deflt_call = NULL_TREE, direct_call, virtual_call, result;
952
953   tbl_entry = save_expr (tbl_entry);
954   tag = build_component_ref (tbl_entry, tag_identifier, NULL_TREE, 1);
955   delta = build_component_ref (tbl_entry, delta_identifier, NULL_TREE, 1);
956   pfn = build_component_ref (tbl_entry, pfn_identifier, NULL_TREE, 1);
957   vt_off = build_component_ref (tbl_entry, vt_off_identifier, NULL_TREE, 1);
958   idx = build_component_ref (tbl_entry, index_identifier, NULL_TREE, 1);
959   TREE_TYPE (pfn) = build_pointer_type (TREE_TYPE (function)); 
960
961   if (IS_DEFAULT_IMPLEMENTATION (function))
962     {
963       pfn = save_expr (pfn);
964       deflt_call = build_function_call (pfn, parms);
965     }
966
967   new_object_ptr = build (PLUS_EXPR, build_pointer_type (basetype),
968                           cp_convert (ptrdiff_type_node, object_ptr),
969                           cp_convert (ptrdiff_type_node, delta));
970
971   parms = tree_cons (NULL_TREE,
972                      cp_convert (build_pointer_type (basetype), object_ptr),
973                      TREE_CHAIN (parms));
974   new_parms = tree_cons (NULL_TREE, new_object_ptr, TREE_CHAIN (parms));
975
976   {
977     /* Cast the signature method to have `this' of a normal pointer type.  */
978     tree old_this = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn))));
979
980     TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn))))
981       = build_qualified_type (build_pointer_type (basetype),
982                               TYPE_QUALS (old_this));
983
984     direct_call = build_function_call (pfn, new_parms);
985
986     {
987       tree vfld, vtbl, aref;
988
989       vfld = build (PLUS_EXPR,
990                     build_pointer_type (build_pointer_type (vtbl_type_node)),
991                     cp_convert (ptrdiff_type_node, object_ptr),
992                     cp_convert (ptrdiff_type_node, vt_off));
993       vtbl = build_indirect_ref (build_indirect_ref (vfld, NULL_PTR),
994                                  NULL_PTR);
995       aref = build_array_ref (vtbl, idx);
996
997       if (flag_vtable_thunks)
998         vfn = aref;
999       else
1000         vfn = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
1001
1002       TREE_TYPE (vfn) = build_pointer_type (TREE_TYPE (function));
1003
1004       virtual_call = build_function_call (vfn, new_parms);
1005     }
1006
1007     /* Undo the cast, make `this' a signature pointer again.  */
1008     TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn)))) = old_this;
1009   }
1010
1011   /* Once the function was found, there should be no reason why we
1012      couldn't build the member function pointer call.  */
1013   if (!direct_call || direct_call == error_mark_node
1014       || !virtual_call || virtual_call == error_mark_node
1015       || (IS_DEFAULT_IMPLEMENTATION (function)
1016           && (!deflt_call || deflt_call == error_mark_node)))
1017     {
1018       compiler_error ("cannot build call of signature member function `%s'",
1019                       fndecl_as_string (function, 1));
1020       return error_mark_node;
1021     }
1022
1023   if (IS_DEFAULT_IMPLEMENTATION (function))
1024     {
1025       tree test = build_binary_op_nodefault (LT_EXPR, tag, integer_zero_node,
1026                                              LT_EXPR);
1027       result = build_conditional_expr (tag,
1028                                        build_conditional_expr (test,
1029                                                                deflt_call,
1030                                                                virtual_call),
1031                                        direct_call);
1032     }
1033   else
1034     result = build_conditional_expr (tag, virtual_call, direct_call);
1035
1036   /* If we created a temporary variable for `this', initialize it first.  */
1037   if (instance != saved_instance)
1038     result = build (COMPOUND_EXPR, TREE_TYPE (result),
1039                     build_modify_expr (saved_instance, NOP_EXPR, instance),
1040                     result);
1041
1042   return result;
1043 }
1044
1045 /* Create a COMPONENT_REF expression for referencing the OPTR field
1046    of a signature pointer or reference.  */
1047
1048 tree
1049 build_optr_ref (instance)
1050      tree instance;
1051 {
1052   tree field = get_identifier (SIGNATURE_OPTR_NAME);
1053
1054   return build_component_ref (instance, field, NULL_TREE, 1);
1055 }
1056
1057 /* Create a COMPONENT_REF expression for referencing the SPTR field
1058    of a signature pointer or reference.  */
1059
1060 static tree
1061 build_sptr_ref (instance)
1062      tree instance;
1063 {
1064   tree field = get_identifier (SIGNATURE_SPTR_NAME);
1065
1066   return build_component_ref (instance, field, NULL_TREE, 1);
1067 }