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)
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. */
31 extern struct obstack
*current_obstack
;
32 extern struct obstack permanent_obstack
;
33 extern struct obstack
*saveable_obstack
;
37 extern void compiler_error ();
38 extern void make_decl_rtl
PROTO((tree
, char *, int));
40 /* Used to help generate globally unique names for signature tables. */
42 static int global_sigtable_name_counter
;
44 /* Build an identifier for a signature pointer or reference, so we
45 can use it's name in function name mangling. */
48 build_signature_pointer_or_reference_name (to_type
, constp
, volatilep
, refp
)
50 int constp
, volatilep
, refp
;
52 char * sig_name
= TYPE_NAME_STRING (to_type
);
53 int name_len
= TYPE_NAME_LENGTH (to_type
) + constp
+ volatilep
;
58 name
= (char *) alloca (name_len
+ sizeof (SIGNATURE_REFERENCE_NAME
) +2);
59 sprintf (name
, SIGNATURE_REFERENCE_NAME_FORMAT
,
60 constp
? "C" : "", volatilep
? "V": "", sig_name
);
64 name
= (char *) alloca (name_len
+ sizeof (SIGNATURE_POINTER_NAME
) + 2);
65 sprintf (name
, SIGNATURE_POINTER_NAME_FORMAT
,
66 constp
? "C" : "", volatilep
? "V": "", sig_name
);
68 return get_identifier (name
);
71 /* Build a DECL node for a signature pointer or reference, so we can
72 tell the debugger the structure of signature pointers/references.
73 This function is called at most eight times for a given signature,
74 once for each [const] [volatile] signature pointer/reference. */
77 build_signature_pointer_or_reference_decl (type
, name
)
82 /* We don't enter this declaration in any sort of symbol table. */
83 decl
= build_decl (TYPE_DECL
, name
, type
);
84 TYPE_NAME (type
) = decl
;
85 TREE_CHAIN (type
) = decl
;
88 /* Construct, lay out and return the type of pointers or references
89 to signature TO_TYPE. If such a type has already been constructed,
90 reuse it. If CONSTP or VOLATILEP is specified, make the `optr' const
91 or volatile, respectively. If we are constructing a const/volatile
92 type variant and the main type variant doesn't exist yet, it is built
93 as well. If REFP is 1, we construct a signature reference, otherwise
94 a signature pointer is constructed.
96 This function is a subroutine of `build_signature_pointer_type' and
97 `build_signature_reference_type'. */
100 build_signature_pointer_or_reference_type (to_type
, constp
, volatilep
, refp
)
102 int constp
, volatilep
, refp
;
105 register struct obstack
*ambient_obstack
= current_obstack
;
106 register struct obstack
*ambient_saveable_obstack
= saveable_obstack
;
108 m
= refp
? SIGNATURE_REFERENCE_TO (to_type
) : SIGNATURE_POINTER_TO (to_type
);
110 /* If we don't have the main variant yet, construct it. */
112 && (constp
|| volatilep
))
113 m
= build_signature_pointer_or_reference_type (to_type
, 0, 0, refp
);
115 /* Treat any nonzero argument as 1. */
117 volatilep
= !!volatilep
;
120 /* If not generating auxiliary info, search the chain of variants to see
121 if there is already one there just like the one we need to have. If so,
122 use that existing one.
124 We don't do this in the case where we are generating aux info because
125 in that case we want each typedef names to get it's own distinct type
126 node, even if the type of this new typedef is the same as some other
129 if (m
&& !flag_gen_aux_info
)
130 for (t
= m
; t
; t
= TYPE_NEXT_VARIANT (t
))
131 if (constp
== TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (t
))))
132 && volatilep
== TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (t
)))))
135 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
136 if (TREE_PERMANENT (to_type
))
138 current_obstack
= &permanent_obstack
;
139 saveable_obstack
= &permanent_obstack
;
142 /* A signature pointer or reference to a signature `s' looks like this:
149 A `const' signature pointer/reference is a
156 Similarly, for `volatile' and `const volatile'. */
158 t
= make_lang_type (RECORD_TYPE
);
160 tree obj_type
= build_type_variant (void_type_node
, constp
, volatilep
);
161 tree optr_type
= build_pointer_type (obj_type
);
164 optr
= build_lang_field_decl (FIELD_DECL
,
165 get_identifier (SIGNATURE_OPTR_NAME
),
167 DECL_FIELD_CONTEXT (optr
) = t
;
168 DECL_CLASS_CONTEXT (optr
) = t
;
171 /* We can share the `sptr' field among type variants. */
172 sptr
= TREE_CHAIN (TYPE_FIELDS (m
));
175 tree sig_tbl_type
= cp_build_type_variant (to_type
, 1, 0);
177 sptr
= build_lang_field_decl (FIELD_DECL
,
178 get_identifier (SIGNATURE_SPTR_NAME
),
179 build_pointer_type (sig_tbl_type
));
180 DECL_FIELD_CONTEXT (sptr
) = t
;
181 DECL_CLASS_CONTEXT (sptr
) = t
;
182 TREE_CHAIN (sptr
) = NULL_TREE
;
185 TREE_CHAIN (optr
) = sptr
;
186 TYPE_FIELDS (t
) = optr
;
187 /* Allow signature pointers/references to be grabbed 2 words at a time.
188 For this to work on a Sparc, we need 8-byte alignment. */
189 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (double_type_node
),
190 TYPE_ALIGN (optr_type
));
192 /* A signature pointer/reference type isn't a `real' class type. */
193 IS_AGGR_TYPE (t
) = 0;
197 tree name
= build_signature_pointer_or_reference_name (to_type
, constp
,
200 /* Build a DECL node for this type, so the debugger has access to it. */
201 build_signature_pointer_or_reference_decl (t
, name
);
204 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
205 IS_SIGNATURE_POINTER (t
) = ! refp
;
206 IS_SIGNATURE_REFERENCE (t
) = refp
;
207 SIGNATURE_TYPE (t
) = to_type
;
211 /* Add this type to the chain of variants of TYPE.
212 Every type has to be its own TYPE_MAIN_VARIANT. */
213 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (m
);
214 TYPE_NEXT_VARIANT (m
) = t
;
217 /* Record this type as the reference to TO_TYPE. */
218 SIGNATURE_REFERENCE_TO (to_type
) = t
;
220 /* Record this type as the pointer to TO_TYPE. */
221 SIGNATURE_POINTER_TO (to_type
) = t
;
223 /* Lay out the type. This function has many callers that are concerned
224 with expression-construction, and this simplifies them all.
225 Also, it guarantees the TYPE_SIZE is permanent if the type is. */
228 current_obstack
= ambient_obstack
;
229 saveable_obstack
= ambient_saveable_obstack
;
231 /* Output debug information for this type. */
232 rest_of_type_compilation (t
, 1);
237 /* Construct, lay out and return the type of pointers to signature TO_TYPE. */
240 build_signature_pointer_type (to_type
, constp
, volatilep
)
242 int constp
, volatilep
;
245 build_signature_pointer_or_reference_type (to_type
, constp
, volatilep
, 0);
248 /* Construct, lay out and return the type of pointers to signature TO_TYPE. */
251 build_signature_reference_type (to_type
, constp
, volatilep
)
253 int constp
, volatilep
;
256 build_signature_pointer_or_reference_type (to_type
, constp
, volatilep
, 1);
259 /* Return the name of the signature table (as an IDENTIFIER_NODE)
260 for the given signature type SIG_TYPE and rhs type RHS_TYPE. */
263 get_sigtable_name (sig_type
, rhs_type
)
264 tree sig_type
, rhs_type
;
266 tree sig_type_id
= build_typename_overload (sig_type
);
267 tree rhs_type_id
= build_typename_overload (rhs_type
);
268 char *buf
= (char *) alloca (sizeof (SIGTABLE_NAME_FORMAT_LONG
)
269 + IDENTIFIER_LENGTH (sig_type_id
)
270 + IDENTIFIER_LENGTH (rhs_type_id
) + 20);
271 char *sig_ptr
= IDENTIFIER_POINTER (sig_type_id
);
272 char *rhs_ptr
= IDENTIFIER_POINTER (rhs_type_id
);
275 for (i
= 0; sig_ptr
[i
] == OPERATOR_TYPENAME_FORMAT
[i
]; i
++)
277 while (sig_ptr
[i
] >= '0' && sig_ptr
[i
] <= '9')
280 for (j
= 0; rhs_ptr
[j
] == OPERATOR_TYPENAME_FORMAT
[j
]; j
++)
282 while (rhs_ptr
[j
] >= '0' && rhs_ptr
[j
] <= '9')
285 if (IS_SIGNATURE (rhs_type
))
286 sprintf (buf
, SIGTABLE_NAME_FORMAT_LONG
, sig_ptr
+i
, rhs_ptr
+j
,
287 global_sigtable_name_counter
++);
289 sprintf (buf
, SIGTABLE_NAME_FORMAT
, sig_ptr
+i
, rhs_ptr
+j
);
290 return get_identifier (buf
);
293 /* Build a field decl that points to a signature member function. */
296 build_member_function_pointer (member
)
299 char *namstr
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (member
));
300 int namlen
= IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (member
));
304 name
= (char *) alloca (namlen
+ sizeof (SIGNATURE_FIELD_NAME
) + 2);
305 sprintf (name
, SIGNATURE_FIELD_NAME_FORMAT
, namstr
);
307 /* @@ Do we really want to xref signature table fields? */
308 GNU_xref_ref (current_function_decl
, name
);
310 entry
= build_lang_field_decl (FIELD_DECL
, get_identifier (name
),
311 TYPE_MAIN_VARIANT (sigtable_entry_type
));
312 TREE_CONSTANT (entry
) = 1;
313 TREE_READONLY (entry
) = 1;
315 /* @@ Do we really want to xref signature table fields? */
316 GNU_xref_decl (current_function_decl
, entry
);
321 /* For each FUNCTION_DECL in a signature we construct a member function
322 pointer of the appropriate type. We also need two flags to test
323 whether the member function pointer points to a virtual function or
324 to a default implementation. Those flags will be the two lower order
325 bits of the member function pointer (or the two higher order bits,
326 based on the configuration).
328 The new FIELD_DECLs are appended at the end of the last (and only)
329 sublist of `list_of_fieldlists.'
331 As a side effect, each member function in the signature gets the
332 `decl.ignored' bit turned on, so we don't output debug info for it. */
335 append_signature_fields (list_of_fieldlists
)
336 tree list_of_fieldlists
;
339 tree last_x
= NULL_TREE
;
342 tree mfptr_list
= NULL_TREE
;
344 /* For signatures it should actually be only a list with one element. */
345 for (l
= list_of_fieldlists
; l
; l
= TREE_CHAIN (l
))
347 for (x
= TREE_VALUE (l
); x
; x
= TREE_CHAIN (x
))
349 if (TREE_CODE (x
) == FUNCTION_DECL
)
351 mfptr
= build_member_function_pointer (x
);
352 DECL_MEMFUNC_POINTER_TO (x
) = mfptr
;
353 DECL_MEMFUNC_POINTING_TO (mfptr
) = x
;
354 DECL_IGNORED_P (x
) = 1;
355 DECL_IN_AGGR_P (mfptr
) = 1;
357 mfptr_list
= last_mfptr
= mfptr
;
360 TREE_CHAIN (last_mfptr
) = mfptr
;
368 /* Append the lists. */
369 if (last_x
&& mfptr_list
)
371 TREE_CHAIN (last_x
) = mfptr_list
;
372 TREE_CHAIN (last_mfptr
) = NULL_TREE
;
376 /* Compare the types of a signature member function and a class member
377 function. Returns 1 if the types are in the C++ `<=' relationship.
379 If we have a signature pointer/reference as argument or return type
380 we don't want to do a recursive conformance check. The conformance
381 check only succeeds if both LHS and RHS refer to the same signature
382 pointer. Otherwise we need to keep information about parameter types
383 around at run time to initialize the signature table correctly. */
386 match_method_types (sig_mtype
, class_mtype
)
387 tree sig_mtype
, class_mtype
;
389 tree sig_return_type
= TREE_TYPE (sig_mtype
);
390 tree sig_arg_types
= TYPE_ARG_TYPES (sig_mtype
);
391 tree class_return_type
= TREE_TYPE (class_mtype
);
392 tree class_arg_types
= TYPE_ARG_TYPES (class_mtype
);
394 /* The return types have to be the same. */
395 if (! comptypes (sig_return_type
, class_return_type
, 1))
398 /* Compare the first argument `this.' */
400 /* Get the type of what the `optr' is pointing to. */
402 TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_VALUE (sig_arg_types
))));
403 tree class_this
= TREE_VALUE (class_arg_types
);
405 if (TREE_CODE (class_this
) == RECORD_TYPE
) /* Is `this' a sig ptr? */
406 class_this
= TREE_TYPE (TREE_TYPE (TYPE_FIELDS (class_this
)));
408 class_this
= TREE_TYPE (class_this
);
410 /* If a signature method's `this' is const or volatile, so has to be
411 the corresponding class method's `this.' */
412 if ((TYPE_READONLY (sig_this
) && ! TYPE_READONLY (class_this
))
413 || (TYPE_VOLATILE (sig_this
) && ! TYPE_VOLATILE (class_this
)))
417 sig_arg_types
= TREE_CHAIN (sig_arg_types
);
418 class_arg_types
= TREE_CHAIN (class_arg_types
);
420 /* The number of arguments and the argument types have to be the same. */
421 return compparms (sig_arg_types
, class_arg_types
, 3);
424 /* Undo casts of opaque type variables to the RHS types. */
430 tree field
= TYPE_FIELDS (sig_ty
);
432 /* Since all the FIELD_DECLs for the signature table entries are at the end
433 of the chain (see `append_signature_fields'), we can do it this way. */
434 for (; field
&& TREE_CODE (field
) != FIELD_DECL
; field
= TREE_CHAIN (field
))
435 if (TYPE_MAIN_VARIANT (TREE_TYPE (field
)) == opaque_type_node
)
436 TREE_TYPE (TREE_TYPE (field
)) = TREE_TYPE (ptr_type_node
);
439 /* Do the type checking necessary to see whether the `rhs' conforms to
440 the lhs's `sig_ty'. Depending on the type of `rhs' return a NULL_TREE,
441 an integer_zero_node, a constructor, or an expression offsetting the
442 `rhs' signature table. */
445 build_signature_table_constructor (sig_ty
, rhs
)
448 tree rhstype
= TREE_TYPE (rhs
);
449 tree sig_field
= TYPE_FIELDS (sig_ty
);
450 tree result
= NULL_TREE
;
451 tree first_rhs_field
= NULL_TREE
;
453 int sig_ptr_p
= IS_SIGNATURE (rhstype
);
454 int offset_p
= sig_ptr_p
;
456 rhstype
= sig_ptr_p
? rhstype
: TREE_TYPE (rhstype
);
458 if (CLASSTYPE_TAGS (sig_ty
))
460 sorry ("conformance check with signature containing class declarations");
461 return error_mark_node
;
464 for (; sig_field
; sig_field
= TREE_CHAIN (sig_field
))
466 tree basetype_path
, baselink
, basetypes
;
467 tree sig_method
, sig_mname
, sig_mtype
;
468 tree rhs_method
, tbl_entry
;
470 if (TREE_CODE (sig_field
) == TYPE_DECL
)
472 tree sig_field_type
= TREE_TYPE (sig_field
);
474 if (TYPE_MAIN_VARIANT (sig_field_type
) == opaque_type_node
)
476 /* We've got an opaque type here. */
477 tree oty_name
= DECL_NAME (sig_field
);
478 tree oty_type
= lookup_field (rhstype
, oty_name
, 1, 1);
480 if (oty_type
== NULL_TREE
|| oty_type
== error_mark_node
)
482 cp_error ("class `%T' does not contain type `%T'",
485 return error_mark_node
;
487 oty_type
= TREE_TYPE (oty_type
);
489 /* Cast `sig_field' to be of type `oty_type'. This will be
490 undone in `undo_casts' by walking over all the TYPE_DECLs. */
491 TREE_TYPE (sig_field_type
) = TREE_TYPE (oty_type
);
493 /* If we don't have an opaque type, we can ignore the `typedef'. */
497 /* Find the signature method corresponding to `sig_field'. */
498 sig_method
= DECL_MEMFUNC_POINTING_TO (sig_field
);
499 sig_mname
= DECL_NAME (sig_method
);
500 sig_mtype
= TREE_TYPE (sig_method
);
502 basetype_path
= TYPE_BINFO (rhstype
);
503 baselink
= lookup_fnfields (basetype_path
, sig_mname
, 0);
504 if (baselink
== NULL_TREE
|| baselink
== error_mark_node
)
506 if (! IS_DEFAULT_IMPLEMENTATION (sig_method
))
508 cp_error ("class `%T' does not contain method `%D'",
511 return error_mark_node
;
515 /* We use the signature's default implementation. */
516 rhs_method
= sig_method
;
521 /* Find the class method of the correct type. */
523 basetypes
= TREE_PURPOSE (baselink
);
524 if (TREE_CODE (basetypes
) == TREE_LIST
)
525 basetypes
= TREE_VALUE (basetypes
);
527 rhs_method
= TREE_VALUE (baselink
);
528 for (; rhs_method
; rhs_method
= TREE_CHAIN (rhs_method
))
529 if (sig_mname
== DECL_NAME (rhs_method
)
530 && ! DECL_STATIC_FUNCTION_P (rhs_method
)
531 && match_method_types (sig_mtype
, TREE_TYPE (rhs_method
)))
534 if (rhs_method
== NULL_TREE
535 || (compute_access (basetypes
, rhs_method
)
536 != access_public_node
))
538 error ("class `%s' does not contain a method conforming to `%s'",
539 TYPE_NAME_STRING (rhstype
),
540 fndecl_as_string (sig_method
, 1));
542 return error_mark_node
;
546 if (sig_ptr_p
&& rhs_method
!= sig_method
)
548 tree rhs_field
= DECL_MEMFUNC_POINTER_TO (rhs_method
);
550 if (first_rhs_field
== NULL_TREE
)
552 first_rhs_field
= rhs_field
;
553 last_rhs_field
= rhs_field
;
555 else if (TREE_CHAIN (last_rhs_field
) == rhs_field
)
556 last_rhs_field
= rhs_field
;
560 tbl_entry
= build_component_ref (rhs
, DECL_NAME (rhs_field
),
565 tree tag
, vb_off
, delta
, idx
, pfn
, vt_off
;
566 tree tag_decl
, vb_off_decl
, delta_decl
, index_decl
;
567 tree pfn_decl
, vt_off_decl
;
569 if (rhs_method
== sig_method
)
571 /* default implementation */
572 tag
= build_unary_op (NEGATE_EXPR
, integer_one_node
, 0);
573 vb_off
= build_unary_op (NEGATE_EXPR
, integer_one_node
, 0);
574 delta
= integer_zero_node
;
575 idx
= integer_zero_node
;
576 pfn
= build_addr_func (rhs_method
);
577 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (rhs_method
)) = 1;
578 TREE_TYPE (pfn
) = ptr_type_node
;
579 TREE_ADDRESSABLE (rhs_method
) = 1;
580 offset_p
= 0; /* we can't offset the rhs sig table */
582 else if (DECL_VINDEX (rhs_method
))
584 /* virtual member function */
585 tag
= integer_one_node
;
586 vb_off
= build_unary_op (NEGATE_EXPR
, integer_one_node
, 0);
587 if (flag_vtable_thunks
)
589 (get_binfo (DECL_CONTEXT (rhs_method
), rhstype
, 1));
592 (get_binfo (DECL_CLASS_CONTEXT (rhs_method
), rhstype
, 1));
593 idx
= DECL_VINDEX (rhs_method
);
594 vt_off
= get_vfield_offset (get_binfo (DECL_CONTEXT (rhs_method
),
599 /* non-virtual member function */
600 tag
= integer_zero_node
;
601 vb_off
= build_unary_op (NEGATE_EXPR
, integer_one_node
, 0);
602 delta
= BINFO_OFFSET (get_binfo (DECL_CLASS_CONTEXT (rhs_method
),
604 idx
= integer_zero_node
;
605 pfn
= build_addr_func (rhs_method
);
606 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (rhs_method
)) = 1;
607 TREE_TYPE (pfn
) = ptr_type_node
;
608 TREE_ADDRESSABLE (rhs_method
) = 1;
611 /* Since digest_init doesn't handle initializing selected fields
612 of a struct (i.e., anonymous union), we build the constructor
613 by hand, without calling digest_init. */
614 tag_decl
= TYPE_FIELDS (sigtable_entry_type
);
615 vb_off_decl
= TREE_CHAIN (tag_decl
);
616 delta_decl
= TREE_CHAIN (vb_off_decl
);
617 index_decl
= TREE_CHAIN (delta_decl
);
618 pfn_decl
= TREE_CHAIN (index_decl
);
619 vt_off_decl
= TREE_CHAIN (pfn_decl
);
621 tag
= convert (TREE_TYPE (tag_decl
), tag
);
622 vb_off
= convert (TREE_TYPE (vb_off_decl
), vb_off
);
623 delta
= convert (TREE_TYPE (delta_decl
), delta
);
624 idx
= convert (TREE_TYPE (index_decl
), idx
);
626 if (DECL_VINDEX (rhs_method
))
628 vt_off
= convert (TREE_TYPE (vt_off_decl
), vt_off
);
630 tbl_entry
= build_tree_list (vt_off_decl
, vt_off
);
634 pfn
= convert (TREE_TYPE (pfn_decl
), pfn
);
636 tbl_entry
= build_tree_list (pfn_decl
, pfn
);
638 tbl_entry
= tree_cons (delta_decl
, delta
,
639 tree_cons (index_decl
, idx
, tbl_entry
));
640 tbl_entry
= tree_cons (tag_decl
, tag
,
641 tree_cons (vb_off_decl
, vb_off
, tbl_entry
));
642 tbl_entry
= build (CONSTRUCTOR
, sigtable_entry_type
,
643 NULL_TREE
, tbl_entry
);
645 TREE_CONSTANT (tbl_entry
) = 1;
648 /* Chain those function address expressions together. */
650 result
= tree_cons (NULL_TREE
, tbl_entry
, result
);
652 result
= build_tree_list (NULL_TREE
, tbl_entry
);
655 if (result
== NULL_TREE
)
657 /* The signature was empty, we don't need a signature table. */
664 if (first_rhs_field
== TYPE_FIELDS (rhstype
))
666 /* The sptr field on the lhs can be copied from the rhs. */
668 return integer_zero_node
;
672 /* The sptr field on the lhs will point into the rhs sigtable. */
674 return build_component_ref (rhs
, DECL_NAME (first_rhs_field
),
679 /* We need to construct a new signature table. */
680 result
= build_nt (CONSTRUCTOR
, NULL_TREE
, nreverse (result
));
681 TREE_HAS_CONSTRUCTOR (result
) = 1;
682 TREE_CONSTANT (result
) = !sig_ptr_p
;
688 /* Build a signature table declaration and initialize it or return an
689 existing one if we built one already. If we don't get a constructor
690 as initialization expression, we don't need a new signature table
691 variable and just hand back the init expression.
693 The declaration processing is done by hand instead of using `cp_finish_decl'
694 so that we can make signature pointers global variables instead of
698 build_sigtable (sig_type
, rhs_type
, init_from
)
699 tree sig_type
, rhs_type
, init_from
;
701 tree name
= NULL_TREE
;
702 tree decl
= NULL_TREE
;
705 push_obstacks_nochange ();
706 end_temporary_allocation ();
708 if (! IS_SIGNATURE (rhs_type
))
710 name
= get_sigtable_name (sig_type
, rhs_type
);
711 decl
= IDENTIFIER_GLOBAL_VALUE (name
);
713 if (decl
== NULL_TREE
)
717 /* We allow only one signature table to be generated for signatures
718 with opaque types. Otherwise we create a loophole in the type
719 system since we could cast data from one classes implementation
720 of the opaque type to that of another class. */
721 if (SIGNATURE_HAS_OPAQUE_TYPEDECLS (sig_type
)
722 && SIGTABLE_HAS_BEEN_GENERATED (sig_type
))
724 error ("signature with opaque type implemented by multiple classes");
725 return error_mark_node
;
727 SIGTABLE_HAS_BEEN_GENERATED (sig_type
) = 1;
729 init_expr
= build_signature_table_constructor (sig_type
, init_from
);
730 if (init_expr
== NULL_TREE
|| TREE_CODE (init_expr
) != CONSTRUCTOR
)
733 if (name
== NULL_TREE
)
734 name
= get_sigtable_name (sig_type
, rhs_type
);
736 tree context
= current_function_decl
;
738 /* Make the signature table global, not just static in whichever
739 function a signature pointer/ref is used for the first time. */
740 current_function_decl
= NULL_TREE
;
741 decl
= pushdecl_top_level (build_decl (VAR_DECL
, name
, sig_type
));
742 current_function_decl
= context
;
744 IDENTIFIER_GLOBAL_VALUE (name
) = decl
;
745 store_init_value (decl
, init_expr
);
746 if (IS_SIGNATURE (rhs_type
))
748 init
= DECL_INITIAL (decl
);
749 DECL_INITIAL (decl
) = error_mark_node
;
752 DECL_ALIGN (decl
) = MAX (TYPE_ALIGN (double_type_node
),
755 /* GDB-4.7 doesn't find the initialization value of a signature table
756 when it is constant. */
757 TREE_READONLY (decl
) = 1;
759 TREE_STATIC (decl
) = 1;
760 TREE_USED (decl
) = 1;
762 make_decl_rtl (decl
, NULL
, 1);
763 if (IS_SIGNATURE (rhs_type
))
764 expand_static_init (decl
, init
);
772 /* Create a constructor or modify expression if the LHS of an assignment
773 is a signature pointer or a signature reference. If LHS is a record
774 type node, we build a constructor, otherwise a compound expression. */
777 build_signature_pointer_constructor (lhs
, rhs
)
780 register struct obstack
*ambient_obstack
= current_obstack
;
781 register struct obstack
*ambient_saveable_obstack
= saveable_obstack
;
782 int initp
= (TREE_CODE (lhs
) == RECORD_TYPE
);
783 tree lhstype
= initp
? lhs
: TREE_TYPE (lhs
);
784 tree rhstype
= TREE_TYPE (rhs
);
785 tree sig_ty
= SIGNATURE_TYPE (lhstype
);
786 tree sig_tbl
, sptr_expr
, optr_expr
;
789 if (! ((TREE_CODE (rhstype
) == POINTER_TYPE
790 && TREE_CODE (TREE_TYPE (rhstype
)) == RECORD_TYPE
)
791 || (TYPE_LANG_SPECIFIC (rhstype
) &&
792 (IS_SIGNATURE_POINTER (rhstype
)
793 || IS_SIGNATURE_REFERENCE (rhstype
)))))
795 error ("invalid assignment to signature pointer or reference");
796 return error_mark_node
;
799 if (TYPE_SIZE (sig_ty
) == NULL_TREE
)
801 cp_error ("undefined signature `%T' used in signature %s declaration",
803 IS_SIGNATURE_POINTER (lhstype
) ? "pointer" : "reference");
804 return error_mark_node
;
807 /* If SIG_TY is permanent, make the signature table constructor and
808 the signature pointer/reference constructor permanent too. */
809 if (TREE_PERMANENT (sig_ty
))
811 current_obstack
= &permanent_obstack
;
812 saveable_obstack
= &permanent_obstack
;
815 if (TYPE_LANG_SPECIFIC (rhstype
) &&
816 (IS_SIGNATURE_POINTER (rhstype
) || IS_SIGNATURE_REFERENCE (rhstype
)))
818 if (SIGNATURE_TYPE (rhstype
) == sig_ty
)
820 /* LHS and RHS are signature pointers/refs of the same signature. */
821 optr_expr
= build_optr_ref (rhs
);
822 sptr_expr
= build_sptr_ref (rhs
);
826 /* We need to create a new signature table and copy
827 elements from the rhs signature table. */
828 tree rhs_sptr_ref
= build_sptr_ref (rhs
);
829 tree rhs_tbl
= build1 (INDIRECT_REF
, SIGNATURE_TYPE (rhstype
),
832 sig_tbl
= build_sigtable (sig_ty
, SIGNATURE_TYPE (rhstype
), rhs_tbl
);
833 if (sig_tbl
== error_mark_node
)
834 return error_mark_node
;
836 optr_expr
= build_optr_ref (rhs
);
837 if (sig_tbl
== NULL_TREE
)
838 /* The signature was empty. The signature pointer is
839 pretty useless, but the user has been warned. */
840 sptr_expr
= copy_node (null_pointer_node
);
841 else if (sig_tbl
== integer_zero_node
)
842 sptr_expr
= rhs_sptr_ref
;
844 sptr_expr
= build_unary_op (ADDR_EXPR
, sig_tbl
, 0);
845 TREE_TYPE (sptr_expr
) = build_pointer_type (sig_ty
);
850 sig_tbl
= build_sigtable (sig_ty
, TREE_TYPE (rhstype
), rhs
);
851 if (sig_tbl
== error_mark_node
)
852 return error_mark_node
;
855 if (sig_tbl
== NULL_TREE
)
856 /* The signature was empty. The signature pointer is
857 pretty useless, but the user has been warned. */
859 sptr_expr
= copy_node (null_pointer_node
);
860 TREE_TYPE (sptr_expr
) = build_pointer_type (sig_ty
);
863 sptr_expr
= build_unary_op (ADDR_EXPR
, sig_tbl
, 0);
868 result
= tree_cons (NULL_TREE
, optr_expr
,
869 build_tree_list (NULL_TREE
, sptr_expr
));
870 result
= build_nt (CONSTRUCTOR
, NULL_TREE
, result
);
871 TREE_HAS_CONSTRUCTOR (result
) = 1;
872 result
= digest_init (lhstype
, result
, 0);
876 if (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
))
877 readonly_error (lhs
, "assignment", 0);
879 optr_expr
= build_modify_expr (build_optr_ref (lhs
), NOP_EXPR
,
881 sptr_expr
= build_modify_expr (build_sptr_ref (lhs
), NOP_EXPR
,
884 result
= tree_cons (NULL_TREE
, optr_expr
,
885 tree_cons (NULL_TREE
, sptr_expr
,
886 build_tree_list (NULL_TREE
, lhs
)));
887 result
= build_compound_expr (result
);
890 current_obstack
= ambient_obstack
;
891 saveable_obstack
= ambient_saveable_obstack
;
895 /* Build a temporary variable declaration for the instance of a signature
896 member function call if it isn't a declaration node already. Simply
897 using a SAVE_EXPR doesn't work since we need `this' in both branches
898 of a conditional expression. */
906 if (TREE_CODE_CLASS (TREE_CODE (instance
)) == 'd')
910 decl
= build_decl (VAR_DECL
, NULL_TREE
, TREE_TYPE (instance
));
911 DECL_REGISTER (decl
) = 1;
912 layout_decl (decl
, 0);
919 /* Build a signature member function call. Looks up the signature table
920 entry corresponding to FUNCTION. Depending on the value of the CODE
921 field, either call the function in PFN directly, or use OFFSET to
922 index the object's virtual function table. */
925 build_signature_method_call (function
, parms
)
926 tree function
, parms
;
928 tree instance
= TREE_VALUE (parms
);
929 tree saved_instance
= save_this (instance
); /* Create temp for `this'. */
930 tree object_ptr
= build_optr_ref (saved_instance
);
931 tree new_object_ptr
, new_parms
;
932 tree signature_tbl_ptr
= build_sptr_ref (saved_instance
);
933 tree sig_field_name
= DECL_NAME (DECL_MEMFUNC_POINTER_TO (function
));
934 tree basetype
= DECL_CONTEXT (function
);
935 tree basetype_path
= TYPE_BINFO (basetype
);
936 tree tbl_entry
= build_component_ref (build1 (INDIRECT_REF
, basetype
,
938 sig_field_name
, basetype_path
, 1);
939 tree tag
, delta
, pfn
, vt_off
, idx
, vfn
;
940 tree deflt_call
= NULL_TREE
, direct_call
, virtual_call
, result
;
942 tbl_entry
= save_expr (tbl_entry
);
943 tag
= build_component_ref (tbl_entry
, tag_identifier
, NULL_TREE
, 1);
944 delta
= build_component_ref (tbl_entry
, delta_identifier
, NULL_TREE
, 1);
945 pfn
= build_component_ref (tbl_entry
, pfn_identifier
, NULL_TREE
, 1);
946 vt_off
= build_component_ref (tbl_entry
, vt_off_identifier
, NULL_TREE
, 1);
947 idx
= build_component_ref (tbl_entry
, index_identifier
, NULL_TREE
, 1);
948 TREE_TYPE (pfn
) = build_pointer_type (TREE_TYPE (function
));
950 if (IS_DEFAULT_IMPLEMENTATION (function
))
952 pfn
= save_expr (pfn
);
953 deflt_call
= build_function_call (pfn
, parms
);
956 new_object_ptr
= build (PLUS_EXPR
, build_pointer_type (basetype
),
957 convert (ptrdiff_type_node
, object_ptr
),
958 convert (ptrdiff_type_node
, delta
));
960 parms
= tree_cons (NULL_TREE
,
961 convert (build_pointer_type (basetype
), object_ptr
),
963 new_parms
= tree_cons (NULL_TREE
, new_object_ptr
, TREE_CHAIN (parms
));
966 /* Cast the signature method to have `this' of a normal pointer type. */
967 tree old_this
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn
))));
969 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn
)))) =
970 build_type_variant (build_pointer_type (basetype
),
971 TYPE_READONLY (old_this
),
972 TYPE_VOLATILE (old_this
));
974 direct_call
= build_function_call (pfn
, new_parms
);
977 tree vfld
, vtbl
, aref
;
979 vfld
= build (PLUS_EXPR
,
980 build_pointer_type (build_pointer_type (vtbl_type_node
)),
981 convert (ptrdiff_type_node
, object_ptr
),
982 convert (ptrdiff_type_node
, vt_off
));
983 vtbl
= build_indirect_ref (build_indirect_ref (vfld
, NULL_PTR
),
985 aref
= build_array_ref (vtbl
, idx
);
987 if (flag_vtable_thunks
)
990 vfn
= build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
992 TREE_TYPE (vfn
) = build_pointer_type (TREE_TYPE (function
));
994 virtual_call
= build_function_call (vfn
, new_parms
);
997 /* Undo the cast, make `this' a signature pointer again. */
998 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn
)))) = old_this
;
1001 /* Once the function was found, there should be no reason why we
1002 couldn't build the member function pointer call. */
1003 if (!direct_call
|| direct_call
== error_mark_node
1004 || !virtual_call
|| virtual_call
== error_mark_node
1005 || (IS_DEFAULT_IMPLEMENTATION (function
)
1006 && (!deflt_call
|| deflt_call
== error_mark_node
)))
1008 compiler_error ("cannot build call of signature member function `%s'",
1009 fndecl_as_string (function
, 1));
1010 return error_mark_node
;
1013 if (IS_DEFAULT_IMPLEMENTATION (function
))
1015 tree test
= build_binary_op_nodefault (LT_EXPR
, tag
, integer_zero_node
,
1017 result
= build_conditional_expr (tag
,
1018 build_conditional_expr (test
,
1024 result
= build_conditional_expr (tag
, virtual_call
, direct_call
);
1026 /* If we created a temporary variable for `this', initialize it first. */
1027 if (instance
!= saved_instance
)
1028 result
= build (COMPOUND_EXPR
, TREE_TYPE (result
),
1029 build_modify_expr (saved_instance
, NOP_EXPR
, instance
),
1035 /* Create a COMPONENT_REF expression for referencing the OPTR field
1036 of a signature pointer or reference. */
1039 build_optr_ref (instance
)
1042 tree field
= get_identifier (SIGNATURE_OPTR_NAME
);
1044 return build_component_ref (instance
, field
, NULL_TREE
, 1);
1047 /* Create a COMPONENT_REF expression for referencing the SPTR field
1048 of a signature pointer or reference. */
1051 build_sptr_ref (instance
)
1054 tree field
= get_identifier (SIGNATURE_SPTR_NAME
);
1056 return build_component_ref (instance
, field
, NULL_TREE
, 1);