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