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 static tree build_sptr_ref
PROTO((tree
));
42 /* Used to help generate globally unique names for signature tables. */
44 static int global_sigtable_name_counter
;
46 /* Build an identifier for a signature pointer or reference, so we
47 can use it's name in function name mangling. */
50 build_signature_pointer_or_reference_name (to_type
, constp
, volatilep
, refp
)
52 int constp
, volatilep
, refp
;
54 char * sig_name
= TYPE_NAME_STRING (to_type
);
55 int name_len
= TYPE_NAME_LENGTH (to_type
) + constp
+ volatilep
;
60 name
= (char *) alloca (name_len
+ sizeof (SIGNATURE_REFERENCE_NAME
) +2);
61 sprintf (name
, SIGNATURE_REFERENCE_NAME_FORMAT
,
62 constp
? "C" : "", volatilep
? "V": "", sig_name
);
66 name
= (char *) alloca (name_len
+ sizeof (SIGNATURE_POINTER_NAME
) + 2);
67 sprintf (name
, SIGNATURE_POINTER_NAME_FORMAT
,
68 constp
? "C" : "", volatilep
? "V": "", sig_name
);
70 return get_identifier (name
);
73 /* Build a DECL node for a signature pointer or reference, so we can
74 tell the debugger the structure of signature pointers/references.
75 This function is called at most eight times for a given signature,
76 once for each [const] [volatile] signature pointer/reference. */
79 build_signature_pointer_or_reference_decl (type
, name
)
84 /* We don't enter this declaration in any sort of symbol table. */
85 decl
= build_decl (TYPE_DECL
, name
, type
);
86 TYPE_NAME (type
) = decl
;
87 TREE_CHAIN (type
) = decl
;
90 /* Construct, lay out and return the type of pointers or references
91 to signature TO_TYPE. If such a type has already been constructed,
92 reuse it. If CONSTP or VOLATILEP is specified, make the `optr' const
93 or volatile, respectively. If we are constructing a const/volatile
94 type variant and the main type variant doesn't exist yet, it is built
95 as well. If REFP is 1, we construct a signature reference, otherwise
96 a signature pointer is constructed.
98 This function is a subroutine of `build_signature_pointer_type' and
99 `build_signature_reference_type'. */
102 build_signature_pointer_or_reference_type (to_type
, constp
, volatilep
, refp
)
104 int constp
, volatilep
, refp
;
107 register struct obstack
*ambient_obstack
= current_obstack
;
108 register struct obstack
*ambient_saveable_obstack
= saveable_obstack
;
110 m
= refp
? SIGNATURE_REFERENCE_TO (to_type
) : SIGNATURE_POINTER_TO (to_type
);
112 /* If we don't have the main variant yet, construct it. */
114 && (constp
|| volatilep
))
115 m
= build_signature_pointer_or_reference_type (to_type
, 0, 0, refp
);
117 /* Treat any nonzero argument as 1. */
119 volatilep
= !!volatilep
;
122 /* If not generating auxiliary info, search the chain of variants to see
123 if there is already one there just like the one we need to have. If so,
124 use that existing one.
126 We don't do this in the case where we are generating aux info because
127 in that case we want each typedef names to get it's own distinct type
128 node, even if the type of this new typedef is the same as some other
131 if (m
&& !flag_gen_aux_info
)
132 for (t
= m
; t
; t
= TYPE_NEXT_VARIANT (t
))
133 if (constp
== TYPE_READONLY (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (t
))))
134 && volatilep
== TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (TYPE_FIELDS (t
)))))
137 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
138 if (TREE_PERMANENT (to_type
))
140 current_obstack
= &permanent_obstack
;
141 saveable_obstack
= &permanent_obstack
;
144 /* A signature pointer or reference to a signature `s' looks like this:
151 A `const' signature pointer/reference is a
158 Similarly, for `volatile' and `const volatile'. */
160 t
= make_lang_type (RECORD_TYPE
);
162 tree obj_type
= build_type_variant (void_type_node
, constp
, volatilep
);
163 tree optr_type
= build_pointer_type (obj_type
);
166 optr
= build_lang_field_decl (FIELD_DECL
,
167 get_identifier (SIGNATURE_OPTR_NAME
),
169 DECL_FIELD_CONTEXT (optr
) = t
;
170 DECL_CLASS_CONTEXT (optr
) = t
;
173 /* We can share the `sptr' field among type variants. */
174 sptr
= TREE_CHAIN (TYPE_FIELDS (m
));
177 tree sig_tbl_type
= cp_build_type_variant (to_type
, 1, 0);
179 sptr
= build_lang_field_decl (FIELD_DECL
,
180 get_identifier (SIGNATURE_SPTR_NAME
),
181 build_pointer_type (sig_tbl_type
));
182 DECL_FIELD_CONTEXT (sptr
) = t
;
183 DECL_CLASS_CONTEXT (sptr
) = t
;
184 TREE_CHAIN (sptr
) = NULL_TREE
;
187 TREE_CHAIN (optr
) = sptr
;
188 TYPE_FIELDS (t
) = optr
;
189 /* Allow signature pointers/references to be grabbed 2 words at a time.
190 For this to work on a Sparc, we need 8-byte alignment. */
191 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (double_type_node
),
192 TYPE_ALIGN (optr_type
));
194 /* A signature pointer/reference type isn't a `real' class type. */
195 IS_AGGR_TYPE (t
) = 0;
199 tree name
= build_signature_pointer_or_reference_name (to_type
, constp
,
202 /* Build a DECL node for this type, so the debugger has access to it. */
203 build_signature_pointer_or_reference_decl (t
, name
);
206 CLASSTYPE_GOT_SEMICOLON (t
) = 1;
207 IS_SIGNATURE_POINTER (t
) = ! refp
;
208 IS_SIGNATURE_REFERENCE (t
) = refp
;
209 SIGNATURE_TYPE (t
) = to_type
;
213 /* Add this type to the chain of variants of TYPE.
214 Every type has to be its own TYPE_MAIN_VARIANT. */
215 TYPE_NEXT_VARIANT (t
) = TYPE_NEXT_VARIANT (m
);
216 TYPE_NEXT_VARIANT (m
) = t
;
219 /* Record this type as the reference to TO_TYPE. */
220 SIGNATURE_REFERENCE_TO (to_type
) = t
;
222 /* Record this type as the pointer to TO_TYPE. */
223 SIGNATURE_POINTER_TO (to_type
) = t
;
225 /* Lay out the type. This function has many callers that are concerned
226 with expression-construction, and this simplifies them all.
227 Also, it guarantees the TYPE_SIZE is permanent if the type is. */
230 current_obstack
= ambient_obstack
;
231 saveable_obstack
= ambient_saveable_obstack
;
233 /* Output debug information for this type. */
234 rest_of_type_compilation (t
, 1);
239 /* Construct, lay out and return the type of pointers to signature TO_TYPE. */
242 build_signature_pointer_type (to_type
, constp
, volatilep
)
244 int constp
, volatilep
;
247 build_signature_pointer_or_reference_type (to_type
, constp
, volatilep
, 0);
250 /* Construct, lay out and return the type of pointers to signature TO_TYPE. */
253 build_signature_reference_type (to_type
, constp
, volatilep
)
255 int constp
, volatilep
;
258 build_signature_pointer_or_reference_type (to_type
, constp
, volatilep
, 1);
261 /* Return the name of the signature table (as an IDENTIFIER_NODE)
262 for the given signature type SIG_TYPE and rhs type RHS_TYPE. */
265 get_sigtable_name (sig_type
, rhs_type
)
266 tree sig_type
, rhs_type
;
268 tree sig_type_id
= build_typename_overload (sig_type
);
269 tree rhs_type_id
= build_typename_overload (rhs_type
);
270 char *buf
= (char *) alloca (sizeof (SIGTABLE_NAME_FORMAT_LONG
)
271 + IDENTIFIER_LENGTH (sig_type_id
)
272 + IDENTIFIER_LENGTH (rhs_type_id
) + 20);
273 char *sig_ptr
= IDENTIFIER_POINTER (sig_type_id
);
274 char *rhs_ptr
= IDENTIFIER_POINTER (rhs_type_id
);
277 for (i
= 0; sig_ptr
[i
] == OPERATOR_TYPENAME_FORMAT
[i
]; i
++)
279 while (sig_ptr
[i
] >= '0' && sig_ptr
[i
] <= '9')
282 for (j
= 0; rhs_ptr
[j
] == OPERATOR_TYPENAME_FORMAT
[j
]; j
++)
284 while (rhs_ptr
[j
] >= '0' && rhs_ptr
[j
] <= '9')
287 if (IS_SIGNATURE (rhs_type
))
288 sprintf (buf
, SIGTABLE_NAME_FORMAT_LONG
, sig_ptr
+i
, rhs_ptr
+j
,
289 global_sigtable_name_counter
++);
291 sprintf (buf
, SIGTABLE_NAME_FORMAT
, sig_ptr
+i
, rhs_ptr
+j
);
292 return get_identifier (buf
);
295 /* Build a field decl that points to a signature member function. */
298 build_member_function_pointer (member
)
301 char *namstr
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (member
));
302 int namlen
= IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (member
));
306 name
= (char *) alloca (namlen
+ sizeof (SIGNATURE_FIELD_NAME
) + 2);
307 sprintf (name
, SIGNATURE_FIELD_NAME_FORMAT
, namstr
);
309 /* @@ Do we really want to xref signature table fields? */
310 GNU_xref_ref (current_function_decl
, name
);
312 entry
= build_lang_field_decl (FIELD_DECL
, get_identifier (name
),
313 TYPE_MAIN_VARIANT (sigtable_entry_type
));
314 TREE_CONSTANT (entry
) = 1;
315 TREE_READONLY (entry
) = 1;
317 /* @@ Do we really want to xref signature table fields? */
318 GNU_xref_decl (current_function_decl
, entry
);
323 /* For each FUNCTION_DECL in a signature we construct a member function
324 pointer of the appropriate type. We also need two flags to test
325 whether the member function pointer points to a virtual function or
326 to a default implementation. Those flags will be the two lower order
327 bits of the member function pointer (or the two higher order bits,
328 based on the configuration).
330 The new FIELD_DECLs are appended at the end of the last (and only)
331 sublist of `list_of_fieldlists.'
333 As a side effect, each member function in the signature gets the
334 `decl.ignored' bit turned on, so we don't output debug info for it. */
337 append_signature_fields (list_of_fieldlists
)
338 tree list_of_fieldlists
;
341 tree last_x
= NULL_TREE
;
344 tree mfptr_list
= NULL_TREE
;
346 /* For signatures it should actually be only a list with one element. */
347 for (l
= list_of_fieldlists
; l
; l
= TREE_CHAIN (l
))
349 for (x
= TREE_VALUE (l
); x
; x
= TREE_CHAIN (x
))
351 if (TREE_CODE (x
) == FUNCTION_DECL
)
353 mfptr
= build_member_function_pointer (x
);
354 DECL_MEMFUNC_POINTER_TO (x
) = mfptr
;
355 DECL_MEMFUNC_POINTING_TO (mfptr
) = x
;
356 DECL_IGNORED_P (x
) = 1;
357 DECL_IN_AGGR_P (mfptr
) = 1;
359 mfptr_list
= last_mfptr
= mfptr
;
362 TREE_CHAIN (last_mfptr
) = mfptr
;
370 /* Append the lists. */
371 if (last_x
&& mfptr_list
)
373 TREE_CHAIN (last_x
) = mfptr_list
;
374 TREE_CHAIN (last_mfptr
) = NULL_TREE
;
378 /* Compare the types of a signature member function and a class member
379 function. Returns 1 if the types are in the C++ `<=' relationship.
381 If we have a signature pointer/reference as argument or return type
382 we don't want to do a recursive conformance check. The conformance
383 check only succeeds if both LHS and RHS refer to the same signature
384 pointer. Otherwise we need to keep information about parameter types
385 around at run time to initialize the signature table correctly. */
388 match_method_types (sig_mtype
, class_mtype
)
389 tree sig_mtype
, class_mtype
;
391 tree sig_return_type
= TREE_TYPE (sig_mtype
);
392 tree sig_arg_types
= TYPE_ARG_TYPES (sig_mtype
);
393 tree class_return_type
= TREE_TYPE (class_mtype
);
394 tree class_arg_types
= TYPE_ARG_TYPES (class_mtype
);
396 /* The return types have to be the same. */
397 if (! comptypes (sig_return_type
, class_return_type
, 1))
400 /* Compare the first argument `this.' */
402 /* Get the type of what the `optr' is pointing to. */
404 = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_VALUE (sig_arg_types
))));
405 tree class_this
= TREE_VALUE (class_arg_types
);
407 if (TREE_CODE (class_this
) == RECORD_TYPE
) /* Is `this' a sig ptr? */
408 class_this
= TREE_TYPE (TREE_TYPE (TYPE_FIELDS (class_this
)));
410 class_this
= TREE_TYPE (class_this
);
412 /* If a signature method's `this' is const or volatile, so has to be
413 the corresponding class method's `this.' */
414 if ((TYPE_READONLY (sig_this
) && ! TYPE_READONLY (class_this
))
415 || (TYPE_VOLATILE (sig_this
) && ! TYPE_VOLATILE (class_this
)))
419 sig_arg_types
= TREE_CHAIN (sig_arg_types
);
420 class_arg_types
= TREE_CHAIN (class_arg_types
);
422 /* The number of arguments and the argument types have to be the same. */
423 return compparms (sig_arg_types
, class_arg_types
, 3);
426 /* Undo casts of opaque type variables to the RHS types. */
432 tree field
= TYPE_FIELDS (sig_ty
);
434 /* Since all the FIELD_DECLs for the signature table entries are at the end
435 of the chain (see `append_signature_fields'), we can do it this way. */
436 for (; field
&& TREE_CODE (field
) != FIELD_DECL
; field
= TREE_CHAIN (field
))
437 if (TYPE_MAIN_VARIANT (TREE_TYPE (field
)) == opaque_type_node
)
438 TREE_TYPE (TREE_TYPE (field
)) = TREE_TYPE (ptr_type_node
);
441 /* Do the type checking necessary to see whether the `rhs' conforms to
442 the lhs's `sig_ty'. Depending on the type of `rhs' return a NULL_TREE,
443 an integer_zero_node, a constructor, or an expression offsetting the
444 `rhs' signature table. */
447 build_signature_table_constructor (sig_ty
, rhs
)
450 tree rhstype
= TREE_TYPE (rhs
);
451 tree sig_field
= TYPE_FIELDS (sig_ty
);
452 tree result
= NULL_TREE
;
453 tree first_rhs_field
= NULL_TREE
;
455 int sig_ptr_p
= IS_SIGNATURE (rhstype
);
456 int offset_p
= sig_ptr_p
;
458 rhstype
= sig_ptr_p
? rhstype
: TREE_TYPE (rhstype
);
460 if (CLASSTYPE_TAGS (sig_ty
))
462 sorry ("conformance check with signature containing class declarations");
463 return error_mark_node
;
466 for (; sig_field
; sig_field
= TREE_CHAIN (sig_field
))
468 tree basetype_path
, baselink
, basetypes
;
469 tree sig_method
, sig_mname
, sig_mtype
;
470 tree rhs_method
, tbl_entry
;
472 if (TREE_CODE (sig_field
) == TYPE_DECL
)
474 tree sig_field_type
= TREE_TYPE (sig_field
);
476 if (TYPE_MAIN_VARIANT (sig_field_type
) == opaque_type_node
)
478 /* We've got an opaque type here. */
479 tree oty_name
= DECL_NAME (sig_field
);
480 tree oty_type
= lookup_field (rhstype
, oty_name
, 1, 1);
482 if (oty_type
== NULL_TREE
|| oty_type
== error_mark_node
)
484 cp_error ("class `%T' does not contain type `%T'",
487 return error_mark_node
;
489 oty_type
= TREE_TYPE (oty_type
);
491 /* Cast `sig_field' to be of type `oty_type'. This will be
492 undone in `undo_casts' by walking over all the TYPE_DECLs. */
493 TREE_TYPE (sig_field_type
) = TREE_TYPE (oty_type
);
495 /* If we don't have an opaque type, we can ignore the `typedef'. */
499 /* Find the signature method corresponding to `sig_field'. */
500 sig_method
= DECL_MEMFUNC_POINTING_TO (sig_field
);
501 sig_mname
= DECL_NAME (sig_method
);
502 sig_mtype
= TREE_TYPE (sig_method
);
504 basetype_path
= TYPE_BINFO (rhstype
);
505 baselink
= lookup_fnfields (basetype_path
, sig_mname
, 0);
506 if (baselink
== NULL_TREE
|| baselink
== error_mark_node
)
508 if (! IS_DEFAULT_IMPLEMENTATION (sig_method
))
510 cp_error ("class `%T' does not contain method `%D'",
513 return error_mark_node
;
517 /* We use the signature's default implementation. */
518 rhs_method
= sig_method
;
523 /* Find the class method of the correct type. */
525 basetypes
= TREE_PURPOSE (baselink
);
526 if (TREE_CODE (basetypes
) == TREE_LIST
)
527 basetypes
= TREE_VALUE (basetypes
);
529 rhs_method
= TREE_VALUE (baselink
);
530 for (; rhs_method
; rhs_method
= TREE_CHAIN (rhs_method
))
531 if (sig_mname
== DECL_NAME (rhs_method
)
532 && ! DECL_STATIC_FUNCTION_P (rhs_method
)
533 && match_method_types (sig_mtype
, TREE_TYPE (rhs_method
)))
536 if (rhs_method
== NULL_TREE
537 || (compute_access (basetypes
, rhs_method
)
538 != access_public_node
))
540 error ("class `%s' does not contain a method conforming to `%s'",
541 TYPE_NAME_STRING (rhstype
),
542 fndecl_as_string (sig_method
, 1));
544 return error_mark_node
;
548 if (sig_ptr_p
&& rhs_method
!= sig_method
)
550 tree rhs_field
= DECL_MEMFUNC_POINTER_TO (rhs_method
);
552 if (first_rhs_field
== NULL_TREE
)
554 first_rhs_field
= rhs_field
;
555 last_rhs_field
= rhs_field
;
557 else if (TREE_CHAIN (last_rhs_field
) == rhs_field
)
558 last_rhs_field
= rhs_field
;
562 tbl_entry
= build_component_ref (rhs
, DECL_NAME (rhs_field
),
567 tree tag
, vb_off
, delta
, idx
, pfn
, vt_off
;
568 tree tag_decl
, vb_off_decl
, delta_decl
, index_decl
;
569 tree pfn_decl
, vt_off_decl
;
571 if (rhs_method
== sig_method
)
573 /* default implementation */
574 tag
= build_unary_op (NEGATE_EXPR
, integer_one_node
, 0);
575 vb_off
= build_unary_op (NEGATE_EXPR
, integer_one_node
, 0);
576 delta
= integer_zero_node
;
577 idx
= integer_zero_node
;
578 pfn
= build_addr_func (rhs_method
);
579 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (rhs_method
)) = 1;
580 TREE_TYPE (pfn
) = ptr_type_node
;
581 TREE_ADDRESSABLE (rhs_method
) = 1;
582 offset_p
= 0; /* we can't offset the rhs sig table */
584 else if (DECL_VINDEX (rhs_method
))
586 /* virtual member function */
587 tag
= integer_one_node
;
588 vb_off
= build_unary_op (NEGATE_EXPR
, integer_one_node
, 0);
589 if (flag_vtable_thunks
)
591 (get_binfo (DECL_CONTEXT (rhs_method
), rhstype
, 1));
594 (get_binfo (DECL_CLASS_CONTEXT (rhs_method
), rhstype
, 1));
595 idx
= DECL_VINDEX (rhs_method
);
596 vt_off
= get_vfield_offset (get_binfo (DECL_CONTEXT (rhs_method
),
601 /* non-virtual member function */
602 tag
= integer_zero_node
;
603 vb_off
= build_unary_op (NEGATE_EXPR
, integer_one_node
, 0);
604 delta
= BINFO_OFFSET (get_binfo (DECL_CLASS_CONTEXT (rhs_method
),
606 idx
= integer_zero_node
;
607 pfn
= build_addr_func (rhs_method
);
608 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (rhs_method
)) = 1;
609 TREE_TYPE (pfn
) = ptr_type_node
;
610 TREE_ADDRESSABLE (rhs_method
) = 1;
613 /* Since digest_init doesn't handle initializing selected fields
614 of a struct (i.e., anonymous union), we build the constructor
615 by hand, without calling digest_init. */
616 tag_decl
= TYPE_FIELDS (sigtable_entry_type
);
617 vb_off_decl
= TREE_CHAIN (tag_decl
);
618 delta_decl
= TREE_CHAIN (vb_off_decl
);
619 index_decl
= TREE_CHAIN (delta_decl
);
620 pfn_decl
= TREE_CHAIN (index_decl
);
621 vt_off_decl
= TREE_CHAIN (pfn_decl
);
623 tag
= cp_convert (TREE_TYPE (tag_decl
), tag
);
624 vb_off
= cp_convert (TREE_TYPE (vb_off_decl
), vb_off
);
625 delta
= cp_convert (TREE_TYPE (delta_decl
), delta
);
626 idx
= cp_convert (TREE_TYPE (index_decl
), idx
);
628 if (DECL_VINDEX (rhs_method
))
630 vt_off
= cp_convert (TREE_TYPE (vt_off_decl
), vt_off
);
632 tbl_entry
= build_tree_list (vt_off_decl
, vt_off
);
636 pfn
= cp_convert (TREE_TYPE (pfn_decl
), pfn
);
638 tbl_entry
= build_tree_list (pfn_decl
, pfn
);
640 tbl_entry
= tree_cons (delta_decl
, delta
,
641 tree_cons (index_decl
, idx
, tbl_entry
));
642 tbl_entry
= tree_cons (tag_decl
, tag
,
643 tree_cons (vb_off_decl
, vb_off
, tbl_entry
));
644 tbl_entry
= build (CONSTRUCTOR
, sigtable_entry_type
,
645 NULL_TREE
, tbl_entry
);
647 TREE_CONSTANT (tbl_entry
) = 1;
650 /* Chain those function address expressions together. */
652 result
= tree_cons (NULL_TREE
, tbl_entry
, result
);
654 result
= build_tree_list (NULL_TREE
, tbl_entry
);
657 if (result
== NULL_TREE
)
659 /* The signature was empty, we don't need a signature table. */
666 if (first_rhs_field
== TYPE_FIELDS (rhstype
))
668 /* The sptr field on the lhs can be copied from the rhs. */
670 return integer_zero_node
;
674 /* The sptr field on the lhs will point into the rhs sigtable. */
676 return build_component_ref (rhs
, DECL_NAME (first_rhs_field
),
681 /* We need to construct a new signature table. */
682 result
= build_nt (CONSTRUCTOR
, NULL_TREE
, nreverse (result
));
683 TREE_HAS_CONSTRUCTOR (result
) = 1;
684 TREE_CONSTANT (result
) = !sig_ptr_p
;
690 /* Build a signature table declaration and initialize it or return an
691 existing one if we built one already. If we don't get a constructor
692 as initialization expression, we don't need a new signature table
693 variable and just hand back the init expression.
695 The declaration processing is done by hand instead of using `cp_finish_decl'
696 so that we can make signature pointers global variables instead of
700 build_sigtable (sig_type
, rhs_type
, init_from
)
701 tree sig_type
, rhs_type
, init_from
;
703 tree name
= NULL_TREE
;
704 tree decl
= NULL_TREE
;
707 push_obstacks_nochange ();
708 end_temporary_allocation ();
710 if (! IS_SIGNATURE (rhs_type
))
712 name
= get_sigtable_name (sig_type
, rhs_type
);
713 decl
= IDENTIFIER_GLOBAL_VALUE (name
);
715 if (decl
== NULL_TREE
)
719 /* We allow only one signature table to be generated for signatures
720 with opaque types. Otherwise we create a loophole in the type
721 system since we could cast data from one classes implementation
722 of the opaque type to that of another class. */
723 if (SIGNATURE_HAS_OPAQUE_TYPEDECLS (sig_type
)
724 && SIGTABLE_HAS_BEEN_GENERATED (sig_type
))
726 error ("signature with opaque type implemented by multiple classes");
727 return error_mark_node
;
729 SIGTABLE_HAS_BEEN_GENERATED (sig_type
) = 1;
731 init_expr
= build_signature_table_constructor (sig_type
, init_from
);
732 if (init_expr
== NULL_TREE
|| TREE_CODE (init_expr
) != CONSTRUCTOR
)
735 if (name
== NULL_TREE
)
736 name
= get_sigtable_name (sig_type
, rhs_type
);
738 tree context
= current_function_decl
;
740 /* Make the signature table global, not just static in whichever
741 function a signature pointer/ref is used for the first time. */
742 current_function_decl
= NULL_TREE
;
743 decl
= pushdecl_top_level (build_decl (VAR_DECL
, name
, sig_type
));
744 current_function_decl
= context
;
746 IDENTIFIER_GLOBAL_VALUE (name
) = decl
;
747 store_init_value (decl
, init_expr
);
748 if (IS_SIGNATURE (rhs_type
))
750 init
= DECL_INITIAL (decl
);
751 DECL_INITIAL (decl
) = error_mark_node
;
754 DECL_ALIGN (decl
) = MAX (TYPE_ALIGN (double_type_node
),
757 /* GDB-4.7 doesn't find the initialization value of a signature table
758 when it is constant. */
759 TREE_READONLY (decl
) = 1;
761 TREE_STATIC (decl
) = 1;
762 TREE_USED (decl
) = 1;
764 make_decl_rtl (decl
, NULL
, 1);
765 if (IS_SIGNATURE (rhs_type
))
766 expand_static_init (decl
, init
);
774 /* Create a constructor or modify expression if the LHS of an assignment
775 is a signature pointer or a signature reference. If LHS is a record
776 type node, we build a constructor, otherwise a compound expression. */
779 build_signature_pointer_constructor (lhs
, rhs
)
782 register struct obstack
*ambient_obstack
= current_obstack
;
783 register struct obstack
*ambient_saveable_obstack
= saveable_obstack
;
784 int initp
= (TREE_CODE (lhs
) == RECORD_TYPE
);
785 tree lhstype
= initp
? lhs
: TREE_TYPE (lhs
);
786 tree rhstype
= TREE_TYPE (rhs
);
787 tree sig_ty
= SIGNATURE_TYPE (lhstype
);
788 tree sig_tbl
, sptr_expr
, optr_expr
;
791 if (! ((TREE_CODE (rhstype
) == POINTER_TYPE
792 && TREE_CODE (TREE_TYPE (rhstype
)) == RECORD_TYPE
)
793 || (TYPE_LANG_SPECIFIC (rhstype
)
794 && (IS_SIGNATURE_POINTER (rhstype
)
795 || IS_SIGNATURE_REFERENCE (rhstype
)))))
797 error ("invalid assignment to signature pointer or reference");
798 return error_mark_node
;
801 if (TYPE_SIZE (sig_ty
) == NULL_TREE
)
803 cp_error ("undefined signature `%T' used in signature %s declaration",
805 IS_SIGNATURE_POINTER (lhstype
) ? "pointer" : "reference");
806 return error_mark_node
;
809 /* If SIG_TY is permanent, make the signature table constructor and
810 the signature pointer/reference constructor permanent too. */
811 if (TREE_PERMANENT (sig_ty
))
813 current_obstack
= &permanent_obstack
;
814 saveable_obstack
= &permanent_obstack
;
817 if (TYPE_LANG_SPECIFIC (rhstype
)
818 && (IS_SIGNATURE_POINTER (rhstype
) || IS_SIGNATURE_REFERENCE (rhstype
)))
820 if (SIGNATURE_TYPE (rhstype
) == sig_ty
)
822 /* LHS and RHS are signature pointers/refs of the same signature. */
823 optr_expr
= build_optr_ref (rhs
);
824 sptr_expr
= build_sptr_ref (rhs
);
828 /* We need to create a new signature table and copy
829 elements from the rhs signature table. */
830 tree rhs_sptr_ref
= build_sptr_ref (rhs
);
831 tree rhs_tbl
= build1 (INDIRECT_REF
, SIGNATURE_TYPE (rhstype
),
834 sig_tbl
= build_sigtable (sig_ty
, SIGNATURE_TYPE (rhstype
), rhs_tbl
);
835 if (sig_tbl
== error_mark_node
)
836 return error_mark_node
;
838 optr_expr
= build_optr_ref (rhs
);
839 if (sig_tbl
== NULL_TREE
)
840 /* The signature was empty. The signature pointer is
841 pretty useless, but the user has been warned. */
842 sptr_expr
= copy_node (null_pointer_node
);
843 else if (sig_tbl
== integer_zero_node
)
844 sptr_expr
= rhs_sptr_ref
;
846 sptr_expr
= build_unary_op (ADDR_EXPR
, sig_tbl
, 0);
847 TREE_TYPE (sptr_expr
) = build_pointer_type (sig_ty
);
852 sig_tbl
= build_sigtable (sig_ty
, TREE_TYPE (rhstype
), rhs
);
853 if (sig_tbl
== error_mark_node
)
854 return error_mark_node
;
857 if (sig_tbl
== NULL_TREE
)
858 /* The signature was empty. The signature pointer is
859 pretty useless, but the user has been warned. */
861 sptr_expr
= copy_node (null_pointer_node
);
862 TREE_TYPE (sptr_expr
) = build_pointer_type (sig_ty
);
865 sptr_expr
= build_unary_op (ADDR_EXPR
, sig_tbl
, 0);
870 result
= tree_cons (NULL_TREE
, optr_expr
,
871 build_tree_list (NULL_TREE
, sptr_expr
));
872 result
= build_nt (CONSTRUCTOR
, NULL_TREE
, result
);
873 TREE_HAS_CONSTRUCTOR (result
) = 1;
874 result
= digest_init (lhstype
, result
, 0);
878 if (TREE_READONLY (lhs
) || TYPE_READONLY (lhstype
))
879 readonly_error (lhs
, "assignment", 0);
881 optr_expr
= build_modify_expr (build_optr_ref (lhs
), NOP_EXPR
,
883 sptr_expr
= build_modify_expr (build_sptr_ref (lhs
), NOP_EXPR
,
886 result
= tree_cons (NULL_TREE
, optr_expr
,
887 tree_cons (NULL_TREE
, sptr_expr
,
888 build_tree_list (NULL_TREE
, lhs
)));
889 result
= build_compound_expr (result
);
892 current_obstack
= ambient_obstack
;
893 saveable_obstack
= ambient_saveable_obstack
;
897 /* Build a temporary variable declaration for the instance of a signature
898 member function call if it isn't a declaration node already. Simply
899 using a SAVE_EXPR doesn't work since we need `this' in both branches
900 of a conditional expression. */
908 if (TREE_CODE_CLASS (TREE_CODE (instance
)) == 'd')
912 decl
= build_decl (VAR_DECL
, NULL_TREE
, TREE_TYPE (instance
));
913 DECL_REGISTER (decl
) = 1;
914 layout_decl (decl
, 0);
921 /* Build a signature member function call. Looks up the signature table
922 entry corresponding to FUNCTION. Depending on the value of the CODE
923 field, either call the function in PFN directly, or use OFFSET to
924 index the object's virtual function table. */
927 build_signature_method_call (function
, parms
)
928 tree function
, parms
;
930 tree instance
= TREE_VALUE (parms
);
931 tree saved_instance
= save_this (instance
); /* Create temp for `this'. */
932 tree object_ptr
= build_optr_ref (saved_instance
);
933 tree new_object_ptr
, new_parms
;
934 tree signature_tbl_ptr
= build_sptr_ref (saved_instance
);
935 tree sig_field_name
= DECL_NAME (DECL_MEMFUNC_POINTER_TO (function
));
936 tree basetype
= DECL_CONTEXT (function
);
937 tree basetype_path
= TYPE_BINFO (basetype
);
938 tree tbl_entry
= build_component_ref (build1 (INDIRECT_REF
, basetype
,
940 sig_field_name
, basetype_path
, 1);
941 tree tag
, delta
, pfn
, vt_off
, idx
, vfn
;
942 tree deflt_call
= NULL_TREE
, direct_call
, virtual_call
, result
;
944 tbl_entry
= save_expr (tbl_entry
);
945 tag
= build_component_ref (tbl_entry
, tag_identifier
, NULL_TREE
, 1);
946 delta
= build_component_ref (tbl_entry
, delta_identifier
, NULL_TREE
, 1);
947 pfn
= build_component_ref (tbl_entry
, pfn_identifier
, NULL_TREE
, 1);
948 vt_off
= build_component_ref (tbl_entry
, vt_off_identifier
, NULL_TREE
, 1);
949 idx
= build_component_ref (tbl_entry
, index_identifier
, NULL_TREE
, 1);
950 TREE_TYPE (pfn
) = build_pointer_type (TREE_TYPE (function
));
952 if (IS_DEFAULT_IMPLEMENTATION (function
))
954 pfn
= save_expr (pfn
);
955 deflt_call
= build_function_call (pfn
, parms
);
958 new_object_ptr
= build (PLUS_EXPR
, build_pointer_type (basetype
),
959 cp_convert (ptrdiff_type_node
, object_ptr
),
960 cp_convert (ptrdiff_type_node
, delta
));
962 parms
= tree_cons (NULL_TREE
,
963 cp_convert (build_pointer_type (basetype
), object_ptr
),
965 new_parms
= tree_cons (NULL_TREE
, new_object_ptr
, TREE_CHAIN (parms
));
968 /* Cast the signature method to have `this' of a normal pointer type. */
969 tree old_this
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn
))));
971 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn
))))
972 = build_type_variant (build_pointer_type (basetype
),
973 TYPE_READONLY (old_this
),
974 TYPE_VOLATILE (old_this
));
976 direct_call
= build_function_call (pfn
, new_parms
);
979 tree vfld
, vtbl
, aref
;
981 vfld
= build (PLUS_EXPR
,
982 build_pointer_type (build_pointer_type (vtbl_type_node
)),
983 cp_convert (ptrdiff_type_node
, object_ptr
),
984 cp_convert (ptrdiff_type_node
, vt_off
));
985 vtbl
= build_indirect_ref (build_indirect_ref (vfld
, NULL_PTR
),
987 aref
= build_array_ref (vtbl
, idx
);
989 if (flag_vtable_thunks
)
992 vfn
= build_component_ref (aref
, pfn_identifier
, NULL_TREE
, 0);
994 TREE_TYPE (vfn
) = build_pointer_type (TREE_TYPE (function
));
996 virtual_call
= build_function_call (vfn
, new_parms
);
999 /* Undo the cast, make `this' a signature pointer again. */
1000 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn
)))) = old_this
;
1003 /* Once the function was found, there should be no reason why we
1004 couldn't build the member function pointer call. */
1005 if (!direct_call
|| direct_call
== error_mark_node
1006 || !virtual_call
|| virtual_call
== error_mark_node
1007 || (IS_DEFAULT_IMPLEMENTATION (function
)
1008 && (!deflt_call
|| deflt_call
== error_mark_node
)))
1010 compiler_error ("cannot build call of signature member function `%s'",
1011 fndecl_as_string (function
, 1));
1012 return error_mark_node
;
1015 if (IS_DEFAULT_IMPLEMENTATION (function
))
1017 tree test
= build_binary_op_nodefault (LT_EXPR
, tag
, integer_zero_node
,
1019 result
= build_conditional_expr (tag
,
1020 build_conditional_expr (test
,
1026 result
= build_conditional_expr (tag
, virtual_call
, direct_call
);
1028 /* If we created a temporary variable for `this', initialize it first. */
1029 if (instance
!= saved_instance
)
1030 result
= build (COMPOUND_EXPR
, TREE_TYPE (result
),
1031 build_modify_expr (saved_instance
, NOP_EXPR
, instance
),
1037 /* Create a COMPONENT_REF expression for referencing the OPTR field
1038 of a signature pointer or reference. */
1041 build_optr_ref (instance
)
1044 tree field
= get_identifier (SIGNATURE_OPTR_NAME
);
1046 return build_component_ref (instance
, field
, NULL_TREE
, 1);
1049 /* Create a COMPONENT_REF expression for referencing the SPTR field
1050 of a signature pointer or reference. */
1053 build_sptr_ref (instance
)
1056 tree field
= get_identifier (SIGNATURE_SPTR_NAME
);
1058 return build_component_ref (instance
, field
, NULL_TREE
, 1);