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)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
32 extern struct obstack *current_obstack;
33 extern struct obstack permanent_obstack;
34 extern struct obstack *saveable_obstack;
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
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));
51 /* Used to help generate globally unique names for signature tables. */
53 static int global_sigtable_name_counter;
55 /* Build an identifier for a signature pointer or reference, so we
56 can use it's name in function name mangling. */
59 build_signature_pointer_or_reference_name (to_type, type_quals, refp)
64 const char * sig_name = TYPE_NAME_STRING (to_type);
65 int name_len = TYPE_NAME_LENGTH (to_type) + 3 /* Enough room for
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" : "";
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);
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);
85 return get_identifier (name);
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. */
94 build_signature_pointer_or_reference_decl (type, name)
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;
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
113 This function is a subroutine of `build_signature_pointer_type' and
114 `build_signature_reference_type'. */
117 build_signature_pointer_or_reference_type (to_type, type_quals, refp)
123 register struct obstack *ambient_obstack = current_obstack;
124 register struct obstack *ambient_saveable_obstack = saveable_obstack;
126 m = refp ? SIGNATURE_REFERENCE_TO (to_type) : SIGNATURE_POINTER_TO (to_type);
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);
133 /* Treat any nonzero argument as 1. */
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.
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
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
151 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
152 if (TREE_PERMANENT (to_type))
154 current_obstack = &permanent_obstack;
155 saveable_obstack = &permanent_obstack;
158 /* A signature pointer or reference to a signature `s' looks like this:
165 A `const' signature pointer/reference is a
172 Similarly, for `volatile' and `const volatile'. */
174 t = make_lang_type (RECORD_TYPE);
176 tree obj_type = build_qualified_type (void_type_node, type_quals);
177 tree optr_type = build_pointer_type (obj_type);
180 optr = build_lang_field_decl (FIELD_DECL,
181 get_identifier (SIGNATURE_OPTR_NAME),
183 DECL_FIELD_CONTEXT (optr) = t;
184 DECL_CLASS_CONTEXT (optr) = t;
187 /* We can share the `sptr' field among type variants. */
188 sptr = TREE_CHAIN (TYPE_FIELDS (m));
192 cp_build_qualified_type (to_type, TYPE_QUAL_CONST);
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;
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));
209 /* A signature pointer/reference type isn't a `real' class type. */
210 SET_IS_AGGR_TYPE (t, 0);
214 tree name = build_signature_pointer_or_reference_name (to_type,
218 /* Build a DECL node for this type, so the debugger has access to it. */
219 build_signature_pointer_or_reference_decl (t, name);
222 CLASSTYPE_GOT_SEMICOLON (t) = 1;
223 IS_SIGNATURE_POINTER (t) = ! refp;
224 IS_SIGNATURE_REFERENCE (t) = refp;
225 SIGNATURE_TYPE (t) = to_type;
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;
235 /* Record this type as the reference to TO_TYPE. */
236 SIGNATURE_REFERENCE_TO (to_type) = t;
238 /* Record this type as the pointer to TO_TYPE. */
239 SIGNATURE_POINTER_TO (to_type) = t;
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. */
246 current_obstack = ambient_obstack;
247 saveable_obstack = ambient_saveable_obstack;
249 /* Output debug information for this type. */
250 rest_of_type_compilation (t, 1);
255 /* Construct, lay out and return the type of pointers to signature TO_TYPE. */
258 build_signature_pointer_type (to_type)
262 build_signature_pointer_or_reference_type (TYPE_MAIN_VARIANT (to_type),
263 CP_TYPE_QUALS (to_type), 0);
266 /* Construct, lay out and return the type of pointers to signature TO_TYPE. */
269 build_signature_reference_type (to_type)
273 build_signature_pointer_or_reference_type (TYPE_MAIN_VARIANT (to_type),
274 CP_TYPE_QUALS (to_type), 1);
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. */
281 get_sigtable_name (sig_type, rhs_type)
282 tree sig_type, rhs_type;
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);
293 for (i = 0; sig_ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++)
295 while (sig_ptr[i] >= '0' && sig_ptr[i] <= '9')
298 for (j = 0; rhs_ptr[j] == OPERATOR_TYPENAME_FORMAT[j]; j++)
300 while (rhs_ptr[j] >= '0' && rhs_ptr[j] <= '9')
303 if (IS_SIGNATURE (rhs_type))
304 sprintf (buf, SIGTABLE_NAME_FORMAT_LONG, sig_ptr+i, rhs_ptr+j,
305 global_sigtable_name_counter++);
307 sprintf (buf, SIGTABLE_NAME_FORMAT, sig_ptr+i, rhs_ptr+j);
308 return get_identifier (buf);
311 /* Build a field decl that points to a signature member function. */
314 build_member_function_pointer (member)
317 const char *namstr = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (member));
318 int namlen = IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (member));
322 name = (char *) alloca (namlen + sizeof (SIGNATURE_FIELD_NAME) + 2);
323 sprintf (name, SIGNATURE_FIELD_NAME_FORMAT, namstr);
325 /* @@ Do we really want to xref signature table fields? */
326 GNU_xref_ref (current_function_decl, name);
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;
333 /* @@ Do we really want to xref signature table fields? */
334 GNU_xref_decl (current_function_decl, entry);
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).
346 The new FIELD_DECLs are appended at the end of the last (and only)
347 sublist of `list_of_fieldlists.'
349 T is the signature type.
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. */
355 append_signature_fields (t)
360 tree last_mfptr = NULL_TREE;
361 tree mfptr_list = NULL_TREE;
363 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
365 if (TREE_CODE (x) == FUNCTION_DECL)
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;
373 mfptr_list = last_mfptr = mfptr;
376 TREE_CHAIN (last_mfptr) = mfptr;
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
387 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), mfptr_list);
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.
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. */
400 match_method_types (sig_mtype, class_mtype)
401 tree sig_mtype, class_mtype;
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);
408 /* The return types have to be the same. */
409 if (!same_type_p (sig_return_type, class_return_type))
412 /* Compare the first argument `this.' */
414 /* Get the type of what the `optr' is pointing to. */
416 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_VALUE (sig_arg_types))));
417 tree class_this = TREE_VALUE (class_arg_types);
419 if (TREE_CODE (class_this) == RECORD_TYPE) /* Is `this' a sig ptr? */
420 class_this = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (class_this)));
422 class_this = TREE_TYPE (class_this);
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))
430 sig_arg_types = TREE_CHAIN (sig_arg_types);
431 class_arg_types = TREE_CHAIN (class_arg_types);
433 /* The number of arguments and the argument types have to be the same. */
434 return compparms (sig_arg_types, class_arg_types);
437 /* Undo casts of opaque type variables to the RHS types. */
443 tree field = TYPE_FIELDS (sig_ty);
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);
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. */
458 build_signature_table_constructor (sig_ty, rhs)
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;
469 rhstype = sig_ptr_p ? rhstype : TREE_TYPE (rhstype);
471 if (CLASSTYPE_TAGS (sig_ty))
473 sorry ("conformance check with signature containing class declarations");
474 return error_mark_node;
477 for (; sig_field; sig_field = TREE_CHAIN (sig_field))
479 tree basetype_path, baselink, basetypes;
480 tree sig_method, sig_mname, sig_mtype;
481 tree rhs_method, tbl_entry;
483 if (TREE_CODE (sig_field) == TYPE_DECL)
485 tree sig_field_type = TREE_TYPE (sig_field);
487 if (TYPE_MAIN_VARIANT (sig_field_type) == opaque_type_node)
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);
493 if (oty_type == NULL_TREE || oty_type == error_mark_node)
495 cp_error ("class `%T' does not contain type `%T'",
498 return error_mark_node;
500 oty_type = TREE_TYPE (oty_type);
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);
506 /* If we don't have an opaque type, we can ignore the `typedef'. */
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);
515 basetype_path = TYPE_BINFO (rhstype);
516 baselink = lookup_fnfields (basetype_path, sig_mname, 0);
517 if (baselink == NULL_TREE || baselink == error_mark_node)
519 if (! IS_DEFAULT_IMPLEMENTATION (sig_method))
521 cp_error ("class `%T' does not contain method `%D'",
524 return error_mark_node;
528 /* We use the signature's default implementation. */
529 rhs_method = sig_method;
534 /* Find the class method of the correct type. */
536 basetypes = TREE_PURPOSE (baselink);
537 if (TREE_CODE (basetypes) == TREE_LIST)
538 basetypes = TREE_VALUE (basetypes);
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)))
548 if (rhs_methods == NULL_TREE
549 || !accessible_p (basetypes, rhs_method))
551 cp_error ("`%T' does not contain a method conforming to `%#D'",
552 rhstype, sig_method);
554 return error_mark_node;
558 if (sig_ptr_p && rhs_method != sig_method)
560 tree rhs_field = DECL_MEMFUNC_POINTER_TO (rhs_method);
562 if (first_rhs_field == NULL_TREE)
564 first_rhs_field = rhs_field;
565 last_rhs_field = rhs_field;
567 else if (TREE_CHAIN (last_rhs_field) == rhs_field)
568 last_rhs_field = rhs_field;
572 tbl_entry = build_component_ref (rhs, DECL_NAME (rhs_field),
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;
581 if (rhs_method == sig_method)
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 */
594 else if (DECL_VINDEX (rhs_method))
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)
601 (get_binfo (DECL_CONTEXT (rhs_method), rhstype, 1));
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),
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),
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;
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);
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);
638 if (DECL_VINDEX (rhs_method))
640 vt_off = cp_convert (TREE_TYPE (vt_off_decl), vt_off);
642 tbl_entry = build_tree_list (vt_off_decl, vt_off);
646 pfn = cp_convert (TREE_TYPE (pfn_decl), pfn);
648 tbl_entry = build_tree_list (pfn_decl, pfn);
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);
657 TREE_CONSTANT (tbl_entry) = 1;
660 /* Chain those function address expressions together. */
662 result = tree_cons (NULL_TREE, tbl_entry, result);
664 result = build_tree_list (NULL_TREE, tbl_entry);
667 if (result == NULL_TREE)
669 /* The signature was empty, we don't need a signature table. */
676 if (first_rhs_field == TYPE_FIELDS (rhstype))
678 /* The sptr field on the lhs can be copied from the rhs. */
680 return integer_zero_node;
684 /* The sptr field on the lhs will point into the rhs sigtable. */
686 return build_component_ref (rhs, DECL_NAME (first_rhs_field),
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;
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.
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
710 build_sigtable (sig_type, rhs_type, init_from)
711 tree sig_type, rhs_type, init_from;
713 tree name = NULL_TREE;
714 tree decl = NULL_TREE;
717 push_obstacks_nochange ();
718 end_temporary_allocation ();
720 if (! IS_SIGNATURE (rhs_type))
722 name = get_sigtable_name (sig_type, rhs_type);
723 decl = IDENTIFIER_GLOBAL_VALUE (name);
725 if (decl == NULL_TREE)
727 tree init = NULL_TREE;
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))
736 error ("signature with opaque type implemented by multiple classes");
737 return error_mark_node;
739 SIGTABLE_HAS_BEEN_GENERATED (sig_type) = 1;
741 init_expr = build_signature_table_constructor (sig_type, init_from);
742 if (init_expr == NULL_TREE || TREE_CODE (init_expr) != CONSTRUCTOR)
745 if (name == NULL_TREE)
746 name = get_sigtable_name (sig_type, rhs_type);
748 tree context = current_function_decl;
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;
756 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
757 store_init_value (decl, init_expr);
758 if (IS_SIGNATURE (rhs_type))
760 init = DECL_INITIAL (decl);
761 DECL_INITIAL (decl) = error_mark_node;
764 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
767 /* GDB-4.7 doesn't find the initialization value of a signature table
768 when it is constant. */
769 TREE_READONLY (decl) = 1;
771 TREE_STATIC (decl) = 1;
772 TREE_USED (decl) = 1;
774 make_decl_rtl (decl, NULL, 1);
775 if (IS_SIGNATURE (rhs_type))
776 expand_static_init (decl, init);
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. */
789 build_signature_pointer_constructor (lhs, rhs)
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;
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)))))
807 error ("invalid assignment to signature pointer or reference");
808 return error_mark_node;
811 if (TYPE_SIZE (sig_ty) == NULL_TREE)
813 cp_error ("undefined signature `%T' used in signature %s declaration",
815 IS_SIGNATURE_POINTER (lhstype) ? "pointer" : "reference");
816 return error_mark_node;
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))
823 current_obstack = &permanent_obstack;
824 saveable_obstack = &permanent_obstack;
827 if (TYPE_LANG_SPECIFIC (rhstype)
828 && (IS_SIGNATURE_POINTER (rhstype) || IS_SIGNATURE_REFERENCE (rhstype)))
830 if (SIGNATURE_TYPE (rhstype) == sig_ty)
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);
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),
844 sig_tbl = build_sigtable (sig_ty, SIGNATURE_TYPE (rhstype), rhs_tbl);
845 if (sig_tbl == error_mark_node)
846 return error_mark_node;
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;
856 sptr_expr = build_unary_op (ADDR_EXPR, sig_tbl, 0);
857 TREE_TYPE (sptr_expr) = build_pointer_type (sig_ty);
862 sig_tbl = build_sigtable (sig_ty, TREE_TYPE (rhstype), rhs);
863 if (sig_tbl == error_mark_node)
864 return error_mark_node;
867 if (sig_tbl == NULL_TREE)
868 /* The signature was empty. The signature pointer is
869 pretty useless, but the user has been warned. */
871 sptr_expr = copy_node (null_pointer_node);
872 TREE_TYPE (sptr_expr) = build_pointer_type (sig_ty);
875 sptr_expr = build_unary_op (ADDR_EXPR, sig_tbl, 0);
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);
887 if (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype))
888 readonly_error (lhs, "assignment", 0);
890 optr_expr = build_modify_expr (build_optr_ref (lhs), NOP_EXPR,
892 sptr_expr = build_modify_expr (build_sptr_ref (lhs), NOP_EXPR,
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);
901 current_obstack = ambient_obstack;
902 saveable_obstack = ambient_saveable_obstack;
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. */
917 if (TREE_CODE_CLASS (TREE_CODE (instance)) == 'd')
921 decl = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (instance));
922 DECL_REGISTER (decl) = 1;
923 layout_decl (decl, 0);
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. */
936 build_signature_method_call (function, parms)
937 tree function, parms;
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,
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;
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));
961 if (IS_DEFAULT_IMPLEMENTATION (function))
963 pfn = save_expr (pfn);
964 deflt_call = build_function_call (pfn, parms);
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));
971 parms = tree_cons (NULL_TREE,
972 cp_convert (build_pointer_type (basetype), object_ptr),
974 new_parms = tree_cons (NULL_TREE, new_object_ptr, TREE_CHAIN (parms));
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))));
980 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn))))
981 = build_qualified_type (build_pointer_type (basetype),
982 TYPE_QUALS (old_this));
984 direct_call = build_function_call (pfn, new_parms);
987 tree vfld, vtbl, aref;
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),
995 aref = build_array_ref (vtbl, idx);
997 if (flag_vtable_thunks)
1000 vfn = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
1002 TREE_TYPE (vfn) = build_pointer_type (TREE_TYPE (function));
1004 virtual_call = build_function_call (vfn, new_parms);
1007 /* Undo the cast, make `this' a signature pointer again. */
1008 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn)))) = old_this;
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)))
1018 compiler_error ("cannot build call of signature member function `%s'",
1019 fndecl_as_string (function, 1));
1020 return error_mark_node;
1023 if (IS_DEFAULT_IMPLEMENTATION (function))
1025 tree test = build_binary_op_nodefault (LT_EXPR, tag, integer_zero_node,
1027 result = build_conditional_expr (tag,
1028 build_conditional_expr (test,
1034 result = build_conditional_expr (tag, virtual_call, direct_call);
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),
1045 /* Create a COMPONENT_REF expression for referencing the OPTR field
1046 of a signature pointer or reference. */
1049 build_optr_ref (instance)
1052 tree field = get_identifier (SIGNATURE_OPTR_NAME);
1054 return build_component_ref (instance, field, NULL_TREE, 1);
1057 /* Create a COMPONENT_REF expression for referencing the SPTR field
1058 of a signature pointer or reference. */
1061 build_sptr_ref (instance)
1064 tree field = get_identifier (SIGNATURE_SPTR_NAME);
1066 return build_component_ref (instance, field, NULL_TREE, 1);