Various fixes for -Wall problems from Kaveh. See ChangeLog for details.
[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 compiler_error ();
36
37 static tree save_this PROTO((tree));
38 static tree build_sptr_ref PROTO((tree));
39 static tree build_member_function_pointer PROTO((tree));
40 static void undo_casts PROTO((tree));
41 static tree build_signature_pointer_or_reference_name
42 PROTO((tree, int, int, int));
43 static void build_signature_pointer_or_reference_decl
44 PROTO((tree, tree));
45 static tree build_signature_pointer_or_reference_type
46 PROTO((tree, int, int, int));
47 static tree get_sigtable_name PROTO((tree, tree));
48 static tree build_signature_table_constructor PROTO((tree, tree));
49 static int match_method_types PROTO((tree, tree));
50 static tree build_sigtable PROTO((tree, tree, tree));
51
52 /* Used to help generate globally unique names for signature tables. */
53
54 static int global_sigtable_name_counter;
55
56 /* Build an identifier for a signature pointer or reference, so we
57 can use it's name in function name mangling. */
58
59 static tree
60 build_signature_pointer_or_reference_name (to_type, constp, volatilep, refp)
61 tree to_type;
62 int constp, volatilep, refp;
63 {
64 char * sig_name = TYPE_NAME_STRING (to_type);
65 int name_len = TYPE_NAME_LENGTH (to_type) + constp + volatilep;
66 char * name;
67
68 if (refp)
69 {
70 name = (char *) alloca (name_len + sizeof (SIGNATURE_REFERENCE_NAME) +2);
71 sprintf (name, SIGNATURE_REFERENCE_NAME_FORMAT,
72 constp ? "C" : "", volatilep ? "V": "", sig_name);
73 }
74 else
75 {
76 name = (char *) alloca (name_len + sizeof (SIGNATURE_POINTER_NAME) + 2);
77 sprintf (name, SIGNATURE_POINTER_NAME_FORMAT,
78 constp ? "C" : "", volatilep ? "V": "", sig_name);
79 }
80 return get_identifier (name);
81 }
82
83 /* Build a DECL node for a signature pointer or reference, so we can
84 tell the debugger the structure of signature pointers/references.
85 This function is called at most eight times for a given signature,
86 once for each [const] [volatile] signature pointer/reference. */
87
88 static void
89 build_signature_pointer_or_reference_decl (type, name)
90 tree type, name;
91 {
92 tree decl;
93
94 /* We don't enter this declaration in any sort of symbol table. */
95 decl = build_decl (TYPE_DECL, name, type);
96 TYPE_NAME (type) = decl;
97 TREE_CHAIN (type) = decl;
98 }
99
100 /* Construct, lay out and return the type of pointers or references
101 to signature TO_TYPE. If such a type has already been constructed,
102 reuse it. If CONSTP or VOLATILEP is specified, make the `optr' const
103 or volatile, respectively. If we are constructing a const/volatile
104 type variant and the main type variant doesn't exist yet, it is built
105 as well. If REFP is 1, we construct a signature reference, otherwise
106 a signature pointer is constructed.
107
108 This function is a subroutine of `build_signature_pointer_type' and
109 `build_signature_reference_type'. */
110
111 static tree
112 build_signature_pointer_or_reference_type (to_type, constp, volatilep, refp)
113 tree to_type;
114 int constp, volatilep, refp;
115 {
116 register tree t, m;
117 register struct obstack *ambient_obstack = current_obstack;
118 register struct obstack *ambient_saveable_obstack = saveable_obstack;
119
120 m = refp ? SIGNATURE_REFERENCE_TO (to_type) : SIGNATURE_POINTER_TO (to_type);
121
122 /* If we don't have the main variant yet, construct it. */
123 if (m == NULL_TREE
124 && (constp || volatilep))
125 m = build_signature_pointer_or_reference_type (to_type, 0, 0, refp);
126
127 /* Treat any nonzero argument as 1. */
128 constp = !!constp;
129 volatilep = !!volatilep;
130 refp = !!refp;
131
132 /* If not generating auxiliary info, search the chain of variants to see
133 if there is already one there just like the one we need to have. If so,
134 use that existing one.
135
136 We don't do this in the case where we are generating aux info because
137 in that case we want each typedef names to get it's own distinct type
138 node, even if the type of this new typedef is the same as some other
139 (existing) type. */
140
141 if (m && !flag_gen_aux_info)
142 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
143 if (constp == TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (t))))
144 && volatilep == TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (t)))))
145 return t;
146
147 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
148 if (TREE_PERMANENT (to_type))
149 {
150 current_obstack = &permanent_obstack;
151 saveable_obstack = &permanent_obstack;
152 }
153
154 /* A signature pointer or reference to a signature `s' looks like this:
155
156 struct {
157 void * optr;
158 const s * sptr;
159 };
160
161 A `const' signature pointer/reference is a
162
163 struct {
164 const void * optr;
165 const s * sptr;
166 };
167
168 Similarly, for `volatile' and `const volatile'. */
169
170 t = make_lang_type (RECORD_TYPE);
171 {
172 tree obj_type = build_type_variant (void_type_node, constp, volatilep);
173 tree optr_type = build_pointer_type (obj_type);
174 tree optr, sptr;
175
176 optr = build_lang_field_decl (FIELD_DECL,
177 get_identifier (SIGNATURE_OPTR_NAME),
178 optr_type);
179 DECL_FIELD_CONTEXT (optr) = t;
180 DECL_CLASS_CONTEXT (optr) = t;
181
182 if (m)
183 /* We can share the `sptr' field among type variants. */
184 sptr = TREE_CHAIN (TYPE_FIELDS (m));
185 else
186 {
187 tree sig_tbl_type = cp_build_type_variant (to_type, 1, 0);
188
189 sptr = build_lang_field_decl (FIELD_DECL,
190 get_identifier (SIGNATURE_SPTR_NAME),
191 build_pointer_type (sig_tbl_type));
192 DECL_FIELD_CONTEXT (sptr) = t;
193 DECL_CLASS_CONTEXT (sptr) = t;
194 TREE_CHAIN (sptr) = NULL_TREE;
195 }
196
197 TREE_CHAIN (optr) = sptr;
198 TYPE_FIELDS (t) = optr;
199 /* Allow signature pointers/references to be grabbed 2 words at a time.
200 For this to work on a Sparc, we need 8-byte alignment. */
201 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (double_type_node),
202 TYPE_ALIGN (optr_type));
203
204 /* A signature pointer/reference type isn't a `real' class type. */
205 IS_AGGR_TYPE (t) = 0;
206 }
207
208 {
209 tree name = build_signature_pointer_or_reference_name (to_type, constp,
210 volatilep, refp);
211
212 /* Build a DECL node for this type, so the debugger has access to it. */
213 build_signature_pointer_or_reference_decl (t, name);
214 }
215
216 CLASSTYPE_GOT_SEMICOLON (t) = 1;
217 IS_SIGNATURE_POINTER (t) = ! refp;
218 IS_SIGNATURE_REFERENCE (t) = refp;
219 SIGNATURE_TYPE (t) = to_type;
220
221 if (m)
222 {
223 /* Add this type to the chain of variants of TYPE.
224 Every type has to be its own TYPE_MAIN_VARIANT. */
225 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
226 TYPE_NEXT_VARIANT (m) = t;
227 }
228 else if (refp)
229 /* Record this type as the reference to TO_TYPE. */
230 SIGNATURE_REFERENCE_TO (to_type) = t;
231 else
232 /* Record this type as the pointer to TO_TYPE. */
233 SIGNATURE_POINTER_TO (to_type) = t;
234
235 /* Lay out the type. This function has many callers that are concerned
236 with expression-construction, and this simplifies them all.
237 Also, it guarantees the TYPE_SIZE is permanent if the type is. */
238 layout_type (t);
239
240 current_obstack = ambient_obstack;
241 saveable_obstack = ambient_saveable_obstack;
242
243 /* Output debug information for this type. */
244 rest_of_type_compilation (t, 1);
245
246 return t;
247 }
248
249 /* Construct, lay out and return the type of pointers to signature TO_TYPE. */
250
251 tree
252 build_signature_pointer_type (to_type, constp, volatilep)
253 tree to_type;
254 int constp, volatilep;
255 {
256 return
257 build_signature_pointer_or_reference_type (to_type, constp, volatilep, 0);
258 }
259
260 /* Construct, lay out and return the type of pointers to signature TO_TYPE. */
261
262 tree
263 build_signature_reference_type (to_type, constp, volatilep)
264 tree to_type;
265 int constp, volatilep;
266 {
267 return
268 build_signature_pointer_or_reference_type (to_type, constp, volatilep, 1);
269 }
270
271 /* Return the name of the signature table (as an IDENTIFIER_NODE)
272 for the given signature type SIG_TYPE and rhs type RHS_TYPE. */
273
274 static tree
275 get_sigtable_name (sig_type, rhs_type)
276 tree sig_type, rhs_type;
277 {
278 tree sig_type_id = build_typename_overload (sig_type);
279 tree rhs_type_id = build_typename_overload (rhs_type);
280 char *buf = (char *) alloca (sizeof (SIGTABLE_NAME_FORMAT_LONG)
281 + IDENTIFIER_LENGTH (sig_type_id)
282 + IDENTIFIER_LENGTH (rhs_type_id) + 20);
283 char *sig_ptr = IDENTIFIER_POINTER (sig_type_id);
284 char *rhs_ptr = IDENTIFIER_POINTER (rhs_type_id);
285 int i, j;
286
287 for (i = 0; sig_ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++)
288 /* do nothing */;
289 while (sig_ptr[i] >= '0' && sig_ptr[i] <= '9')
290 i += 1;
291
292 for (j = 0; rhs_ptr[j] == OPERATOR_TYPENAME_FORMAT[j]; j++)
293 /* do nothing */;
294 while (rhs_ptr[j] >= '0' && rhs_ptr[j] <= '9')
295 j += 1;
296
297 if (IS_SIGNATURE (rhs_type))
298 sprintf (buf, SIGTABLE_NAME_FORMAT_LONG, sig_ptr+i, rhs_ptr+j,
299 global_sigtable_name_counter++);
300 else
301 sprintf (buf, SIGTABLE_NAME_FORMAT, sig_ptr+i, rhs_ptr+j);
302 return get_identifier (buf);
303 }
304
305 /* Build a field decl that points to a signature member function. */
306
307 static tree
308 build_member_function_pointer (member)
309 tree member;
310 {
311 char *namstr = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (member));
312 int namlen = IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (member));
313 char *name;
314 tree entry;
315
316 name = (char *) alloca (namlen + sizeof (SIGNATURE_FIELD_NAME) + 2);
317 sprintf (name, SIGNATURE_FIELD_NAME_FORMAT, namstr);
318
319 /* @@ Do we really want to xref signature table fields? */
320 GNU_xref_ref (current_function_decl, name);
321
322 entry = build_lang_field_decl (FIELD_DECL, get_identifier (name),
323 TYPE_MAIN_VARIANT (sigtable_entry_type));
324 TREE_CONSTANT (entry) = 1;
325 TREE_READONLY (entry) = 1;
326
327 /* @@ Do we really want to xref signature table fields? */
328 GNU_xref_decl (current_function_decl, entry);
329
330 return entry;
331 }
332
333 /* For each FUNCTION_DECL in a signature we construct a member function
334 pointer of the appropriate type. We also need two flags to test
335 whether the member function pointer points to a virtual function or
336 to a default implementation. Those flags will be the two lower order
337 bits of the member function pointer (or the two higher order bits,
338 based on the configuration).
339
340 The new FIELD_DECLs are appended at the end of the last (and only)
341 sublist of `list_of_fieldlists.'
342
343 As a side effect, each member function in the signature gets the
344 `decl.ignored' bit turned on, so we don't output debug info for it. */
345
346 void
347 append_signature_fields (list_of_fieldlists)
348 tree list_of_fieldlists;
349 {
350 tree l, x;
351 tree last_x = NULL_TREE;
352 tree mfptr;
353 tree last_mfptr = NULL_TREE;
354 tree mfptr_list = NULL_TREE;
355
356 /* For signatures it should actually be only a list with one element. */
357 for (l = list_of_fieldlists; l; l = TREE_CHAIN (l))
358 {
359 for (x = TREE_VALUE (l); x; x = TREE_CHAIN (x))
360 {
361 if (TREE_CODE (x) == FUNCTION_DECL)
362 {
363 mfptr = build_member_function_pointer (x);
364 DECL_MEMFUNC_POINTER_TO (x) = mfptr;
365 DECL_MEMFUNC_POINTING_TO (mfptr) = x;
366 DECL_IGNORED_P (x) = 1;
367 DECL_IN_AGGR_P (mfptr) = 1;
368 if (! mfptr_list)
369 mfptr_list = last_mfptr = mfptr;
370 else
371 {
372 TREE_CHAIN (last_mfptr) = mfptr;
373 last_mfptr = mfptr;
374 }
375 }
376 last_x = x;
377 }
378 }
379
380 /* Append the lists. */
381 if (last_x && mfptr_list)
382 {
383 TREE_CHAIN (last_x) = mfptr_list;
384 TREE_CHAIN (last_mfptr) = NULL_TREE;
385 }
386 }
387
388 /* Compare the types of a signature member function and a class member
389 function. Returns 1 if the types are in the C++ `<=' relationship.
390
391 If we have a signature pointer/reference as argument or return type
392 we don't want to do a recursive conformance check. The conformance
393 check only succeeds if both LHS and RHS refer to the same signature
394 pointer. Otherwise we need to keep information about parameter types
395 around at run time to initialize the signature table correctly. */
396
397 static int
398 match_method_types (sig_mtype, class_mtype)
399 tree sig_mtype, class_mtype;
400 {
401 tree sig_return_type = TREE_TYPE (sig_mtype);
402 tree sig_arg_types = TYPE_ARG_TYPES (sig_mtype);
403 tree class_return_type = TREE_TYPE (class_mtype);
404 tree class_arg_types = TYPE_ARG_TYPES (class_mtype);
405
406 /* The return types have to be the same. */
407 if (! comptypes (sig_return_type, class_return_type, 1))
408 return 0;
409
410 /* Compare the first argument `this.' */
411 {
412 /* Get the type of what the `optr' is pointing to. */
413 tree sig_this
414 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_VALUE (sig_arg_types))));
415 tree class_this = TREE_VALUE (class_arg_types);
416
417 if (TREE_CODE (class_this) == RECORD_TYPE) /* Is `this' a sig ptr? */
418 class_this = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (class_this)));
419 else
420 class_this = TREE_TYPE (class_this);
421
422 /* If a signature method's `this' is const or volatile, so has to be
423 the corresponding class method's `this.' */
424 if ((TYPE_READONLY (sig_this) && ! TYPE_READONLY (class_this))
425 || (TYPE_VOLATILE (sig_this) && ! TYPE_VOLATILE (class_this)))
426 return 0;
427 }
428
429 sig_arg_types = TREE_CHAIN (sig_arg_types);
430 class_arg_types = TREE_CHAIN (class_arg_types);
431
432 /* The number of arguments and the argument types have to be the same. */
433 return compparms (sig_arg_types, class_arg_types, 3);
434 }
435
436 /* Undo casts of opaque type variables to the RHS types. */
437
438 static void
439 undo_casts (sig_ty)
440 tree sig_ty;
441 {
442 tree field = TYPE_FIELDS (sig_ty);
443
444 /* Since all the FIELD_DECLs for the signature table entries are at the end
445 of the chain (see `append_signature_fields'), we can do it this way. */
446 for (; field && TREE_CODE (field) != FIELD_DECL; field = TREE_CHAIN (field))
447 if (TYPE_MAIN_VARIANT (TREE_TYPE (field)) == opaque_type_node)
448 TREE_TYPE (TREE_TYPE (field)) = TREE_TYPE (ptr_type_node);
449 }
450
451 /* Do the type checking necessary to see whether the `rhs' conforms to
452 the lhs's `sig_ty'. Depending on the type of `rhs' return a NULL_TREE,
453 an integer_zero_node, a constructor, or an expression offsetting the
454 `rhs' signature table. */
455
456 static tree
457 build_signature_table_constructor (sig_ty, rhs)
458 tree sig_ty, rhs;
459 {
460 tree rhstype = TREE_TYPE (rhs);
461 tree sig_field = TYPE_FIELDS (sig_ty);
462 tree result = NULL_TREE;
463 tree first_rhs_field = NULL_TREE;
464 tree last_rhs_field = NULL_TREE;
465 int sig_ptr_p = IS_SIGNATURE (rhstype);
466 int offset_p = sig_ptr_p;
467
468 rhstype = sig_ptr_p ? rhstype : TREE_TYPE (rhstype);
469
470 if (CLASSTYPE_TAGS (sig_ty))
471 {
472 sorry ("conformance check with signature containing class declarations");
473 return error_mark_node;
474 }
475
476 for (; sig_field; sig_field = TREE_CHAIN (sig_field))
477 {
478 tree basetype_path, baselink, basetypes;
479 tree sig_method, sig_mname, sig_mtype;
480 tree rhs_method, tbl_entry;
481
482 if (TREE_CODE (sig_field) == TYPE_DECL)
483 {
484 tree sig_field_type = TREE_TYPE (sig_field);
485
486 if (TYPE_MAIN_VARIANT (sig_field_type) == opaque_type_node)
487 {
488 /* We've got an opaque type here. */
489 tree oty_name = DECL_NAME (sig_field);
490 tree oty_type = lookup_field (rhstype, oty_name, 1, 1);
491
492 if (oty_type == NULL_TREE || oty_type == error_mark_node)
493 {
494 cp_error ("class `%T' does not contain type `%T'",
495 rhstype, oty_type);
496 undo_casts (sig_ty);
497 return error_mark_node;
498 }
499 oty_type = TREE_TYPE (oty_type);
500
501 /* Cast `sig_field' to be of type `oty_type'. This will be
502 undone in `undo_casts' by walking over all the TYPE_DECLs. */
503 TREE_TYPE (sig_field_type) = TREE_TYPE (oty_type);
504 }
505 /* If we don't have an opaque type, we can ignore the `typedef'. */
506 continue;
507 }
508
509 /* Find the signature method corresponding to `sig_field'. */
510 sig_method = DECL_MEMFUNC_POINTING_TO (sig_field);
511 sig_mname = DECL_NAME (sig_method);
512 sig_mtype = TREE_TYPE (sig_method);
513
514 basetype_path = TYPE_BINFO (rhstype);
515 baselink = lookup_fnfields (basetype_path, sig_mname, 0);
516 if (baselink == NULL_TREE || baselink == error_mark_node)
517 {
518 if (! IS_DEFAULT_IMPLEMENTATION (sig_method))
519 {
520 cp_error ("class `%T' does not contain method `%D'",
521 rhstype, sig_mname);
522 undo_casts (sig_ty);
523 return error_mark_node;
524 }
525 else
526 {
527 /* We use the signature's default implementation. */
528 rhs_method = sig_method;
529 }
530 }
531 else
532 {
533 /* Find the class method of the correct type. */
534
535 basetypes = TREE_PURPOSE (baselink);
536 if (TREE_CODE (basetypes) == TREE_LIST)
537 basetypes = TREE_VALUE (basetypes);
538
539 rhs_method = TREE_VALUE (baselink);
540 for (; rhs_method; rhs_method = TREE_CHAIN (rhs_method))
541 if (sig_mname == DECL_NAME (rhs_method)
542 && ! DECL_STATIC_FUNCTION_P (rhs_method)
543 && match_method_types (sig_mtype, TREE_TYPE (rhs_method)))
544 break;
545
546 if (rhs_method == NULL_TREE
547 || (compute_access (basetypes, rhs_method)
548 != access_public_node))
549 {
550 error ("class `%s' does not contain a method conforming to `%s'",
551 TYPE_NAME_STRING (rhstype),
552 fndecl_as_string (sig_method, 1));
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 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 TREE_HAS_CONSTRUCTOR (result) = 1;
884 result = digest_init (lhstype, result, 0);
885 }
886 else
887 {
888 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype))
889 readonly_error (lhs, "assignment", 0);
890
891 optr_expr = build_modify_expr (build_optr_ref (lhs), NOP_EXPR,
892 optr_expr);
893 sptr_expr = build_modify_expr (build_sptr_ref (lhs), NOP_EXPR,
894 sptr_expr);
895
896 result = tree_cons (NULL_TREE, optr_expr,
897 tree_cons (NULL_TREE, sptr_expr,
898 build_tree_list (NULL_TREE, lhs)));
899 result = build_compound_expr (result);
900 }
901
902 current_obstack = ambient_obstack;
903 saveable_obstack = ambient_saveable_obstack;
904 return result;
905 }
906
907 /* Build a temporary variable declaration for the instance of a signature
908 member function call if it isn't a declaration node already. Simply
909 using a SAVE_EXPR doesn't work since we need `this' in both branches
910 of a conditional expression. */
911
912 static tree
913 save_this (instance)
914 tree instance;
915 {
916 tree decl;
917
918 if (TREE_CODE_CLASS (TREE_CODE (instance)) == 'd')
919 decl = instance;
920 else
921 {
922 decl = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (instance));
923 DECL_REGISTER (decl) = 1;
924 layout_decl (decl, 0);
925 expand_decl (decl);
926 }
927
928 return decl;
929 }
930
931 /* Build a signature member function call. Looks up the signature table
932 entry corresponding to FUNCTION. Depending on the value of the CODE
933 field, either call the function in PFN directly, or use OFFSET to
934 index the object's virtual function table. */
935
936 tree
937 build_signature_method_call (function, parms)
938 tree function, parms;
939 {
940 tree instance = TREE_VALUE (parms);
941 tree saved_instance = save_this (instance); /* Create temp for `this'. */
942 tree object_ptr = build_optr_ref (saved_instance);
943 tree new_object_ptr, new_parms;
944 tree signature_tbl_ptr = build_sptr_ref (saved_instance);
945 tree sig_field_name = DECL_NAME (DECL_MEMFUNC_POINTER_TO (function));
946 tree basetype = DECL_CONTEXT (function);
947 tree basetype_path = TYPE_BINFO (basetype);
948 tree tbl_entry = build_component_ref (build1 (INDIRECT_REF, basetype,
949 signature_tbl_ptr),
950 sig_field_name, basetype_path, 1);
951 tree tag, delta, pfn, vt_off, idx, vfn;
952 tree deflt_call = NULL_TREE, direct_call, virtual_call, result;
953
954 tbl_entry = save_expr (tbl_entry);
955 tag = build_component_ref (tbl_entry, tag_identifier, NULL_TREE, 1);
956 delta = build_component_ref (tbl_entry, delta_identifier, NULL_TREE, 1);
957 pfn = build_component_ref (tbl_entry, pfn_identifier, NULL_TREE, 1);
958 vt_off = build_component_ref (tbl_entry, vt_off_identifier, NULL_TREE, 1);
959 idx = build_component_ref (tbl_entry, index_identifier, NULL_TREE, 1);
960 TREE_TYPE (pfn) = build_pointer_type (TREE_TYPE (function));
961
962 if (IS_DEFAULT_IMPLEMENTATION (function))
963 {
964 pfn = save_expr (pfn);
965 deflt_call = build_function_call (pfn, parms);
966 }
967
968 new_object_ptr = build (PLUS_EXPR, build_pointer_type (basetype),
969 cp_convert (ptrdiff_type_node, object_ptr),
970 cp_convert (ptrdiff_type_node, delta));
971
972 parms = tree_cons (NULL_TREE,
973 cp_convert (build_pointer_type (basetype), object_ptr),
974 TREE_CHAIN (parms));
975 new_parms = tree_cons (NULL_TREE, new_object_ptr, TREE_CHAIN (parms));
976
977 {
978 /* Cast the signature method to have `this' of a normal pointer type. */
979 tree old_this = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn))));
980
981 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn))))
982 = build_type_variant (build_pointer_type (basetype),
983 TYPE_READONLY (old_this),
984 TYPE_VOLATILE (old_this));
985
986 direct_call = build_function_call (pfn, new_parms);
987
988 {
989 tree vfld, vtbl, aref;
990
991 vfld = build (PLUS_EXPR,
992 build_pointer_type (build_pointer_type (vtbl_type_node)),
993 cp_convert (ptrdiff_type_node, object_ptr),
994 cp_convert (ptrdiff_type_node, vt_off));
995 vtbl = build_indirect_ref (build_indirect_ref (vfld, NULL_PTR),
996 NULL_PTR);
997 aref = build_array_ref (vtbl, idx);
998
999 if (flag_vtable_thunks)
1000 vfn = aref;
1001 else
1002 vfn = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
1003
1004 TREE_TYPE (vfn) = build_pointer_type (TREE_TYPE (function));
1005
1006 virtual_call = build_function_call (vfn, new_parms);
1007 }
1008
1009 /* Undo the cast, make `this' a signature pointer again. */
1010 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn)))) = old_this;
1011 }
1012
1013 /* Once the function was found, there should be no reason why we
1014 couldn't build the member function pointer call. */
1015 if (!direct_call || direct_call == error_mark_node
1016 || !virtual_call || virtual_call == error_mark_node
1017 || (IS_DEFAULT_IMPLEMENTATION (function)
1018 && (!deflt_call || deflt_call == error_mark_node)))
1019 {
1020 compiler_error ("cannot build call of signature member function `%s'",
1021 fndecl_as_string (function, 1));
1022 return error_mark_node;
1023 }
1024
1025 if (IS_DEFAULT_IMPLEMENTATION (function))
1026 {
1027 tree test = build_binary_op_nodefault (LT_EXPR, tag, integer_zero_node,
1028 LT_EXPR);
1029 result = build_conditional_expr (tag,
1030 build_conditional_expr (test,
1031 deflt_call,
1032 virtual_call),
1033 direct_call);
1034 }
1035 else
1036 result = build_conditional_expr (tag, virtual_call, direct_call);
1037
1038 /* If we created a temporary variable for `this', initialize it first. */
1039 if (instance != saved_instance)
1040 result = build (COMPOUND_EXPR, TREE_TYPE (result),
1041 build_modify_expr (saved_instance, NOP_EXPR, instance),
1042 result);
1043
1044 return result;
1045 }
1046
1047 /* Create a COMPONENT_REF expression for referencing the OPTR field
1048 of a signature pointer or reference. */
1049
1050 tree
1051 build_optr_ref (instance)
1052 tree instance;
1053 {
1054 tree field = get_identifier (SIGNATURE_OPTR_NAME);
1055
1056 return build_component_ref (instance, field, NULL_TREE, 1);
1057 }
1058
1059 /* Create a COMPONENT_REF expression for referencing the SPTR field
1060 of a signature pointer or reference. */
1061
1062 static tree
1063 build_sptr_ref (instance)
1064 tree instance;
1065 {
1066 tree field = get_identifier (SIGNATURE_SPTR_NAME);
1067
1068 return build_component_ref (instance, field, NULL_TREE, 1);
1069 }