]> gcc.gnu.org Git - gcc.git/blob - gcc/cp/sig.c
merging
[gcc.git] / gcc / cp / sig.c
1 /* Functions dealing with signatures and signature pointers/references.
2 Copyright (C) 1992, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Contributed by Gerald Baumgartner (gb@cs.purdue.edu)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include "config.h"
24 #include <stdio.h>
25 #include "obstack.h"
26 #include "tree.h"
27 #include "cp-tree.h"
28 #include "flags.h"
29 #include "assert.h"
30
31 extern struct obstack *current_obstack;
32 extern struct obstack permanent_obstack;
33 extern struct obstack *saveable_obstack;
34
35 extern void error ();
36 extern void sorry ();
37 extern void compiler_error ();
38 extern void make_decl_rtl PROTO((tree, char *, int));
39
40 /* Used to help generate globally unique names for signature tables. */
41
42 static int global_sigtable_name_counter;
43
44 /* Build an identifier for a signature pointer or reference, so we
45 can use it's name in function name mangling. */
46
47 static tree
48 build_signature_pointer_or_reference_name (to_type, constp, volatilep, refp)
49 tree to_type;
50 int constp, volatilep, refp;
51 {
52 char * sig_name = TYPE_NAME_STRING (to_type);
53 int name_len = TYPE_NAME_LENGTH (to_type) + constp + volatilep;
54 char * name;
55
56 if (refp)
57 {
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);
61 }
62 else
63 {
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);
67 }
68 return get_identifier (name);
69 }
70
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. */
75
76 static void
77 build_signature_pointer_or_reference_decl (type, name)
78 tree type, name;
79 {
80 tree decl;
81
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;
86 }
87
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.
95
96 This function is a subroutine of `build_signature_pointer_type' and
97 `build_signature_reference_type'. */
98
99 static tree
100 build_signature_pointer_or_reference_type (to_type, constp, volatilep, refp)
101 tree to_type;
102 int constp, volatilep, refp;
103 {
104 register tree t, m;
105 register struct obstack *ambient_obstack = current_obstack;
106 register struct obstack *ambient_saveable_obstack = saveable_obstack;
107
108 m = refp ? SIGNATURE_REFERENCE_TO (to_type) : SIGNATURE_POINTER_TO (to_type);
109
110 /* If we don't have the main variant yet, construct it. */
111 if (m == NULL_TREE
112 && (constp || volatilep))
113 m = build_signature_pointer_or_reference_type (to_type, 0, 0, refp);
114
115 /* Treat any nonzero argument as 1. */
116 constp = !!constp;
117 volatilep = !!volatilep;
118 refp = !!refp;
119
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.
123
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
127 (existing) type. */
128
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)))))
133 return t;
134
135 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
136 if (TREE_PERMANENT (to_type))
137 {
138 current_obstack = &permanent_obstack;
139 saveable_obstack = &permanent_obstack;
140 }
141
142 /* A signature pointer or reference to a signature `s' looks like this:
143
144 struct {
145 void * optr;
146 const s * sptr;
147 };
148
149 A `const' signature pointer/reference is a
150
151 struct {
152 const void * optr;
153 const s * sptr;
154 };
155
156 Similarly, for `volatile' and `const volatile'. */
157
158 t = make_lang_type (RECORD_TYPE);
159 {
160 tree obj_type = build_type_variant (void_type_node, constp, volatilep);
161 tree optr_type = build_pointer_type (obj_type);
162 tree optr, sptr;
163
164 optr = build_lang_field_decl (FIELD_DECL,
165 get_identifier (SIGNATURE_OPTR_NAME),
166 optr_type);
167 DECL_FIELD_CONTEXT (optr) = t;
168 DECL_CLASS_CONTEXT (optr) = t;
169
170 if (m)
171 /* We can share the `sptr' field among type variants. */
172 sptr = TREE_CHAIN (TYPE_FIELDS (m));
173 else
174 {
175 tree sig_tbl_type = cp_build_type_variant (to_type, 1, 0);
176
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;
183 }
184
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));
191
192 /* A signature pointer/reference type isn't a `real' class type. */
193 IS_AGGR_TYPE (t) = 0;
194 }
195
196 {
197 tree name = build_signature_pointer_or_reference_name (to_type, constp,
198 volatilep, refp);
199
200 /* Build a DECL node for this type, so the debugger has access to it. */
201 build_signature_pointer_or_reference_decl (t, name);
202 }
203
204 CLASSTYPE_GOT_SEMICOLON (t) = 1;
205 IS_SIGNATURE_POINTER (t) = ! refp;
206 IS_SIGNATURE_REFERENCE (t) = refp;
207 SIGNATURE_TYPE (t) = to_type;
208
209 if (m)
210 {
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;
215 }
216 else if (refp)
217 /* Record this type as the reference to TO_TYPE. */
218 SIGNATURE_REFERENCE_TO (to_type) = t;
219 else
220 /* Record this type as the pointer to TO_TYPE. */
221 SIGNATURE_POINTER_TO (to_type) = t;
222
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. */
226 layout_type (t);
227
228 current_obstack = ambient_obstack;
229 saveable_obstack = ambient_saveable_obstack;
230
231 /* Output debug information for this type. */
232 rest_of_type_compilation (t, 1);
233
234 return t;
235 }
236
237 /* Construct, lay out and return the type of pointers to signature TO_TYPE. */
238
239 tree
240 build_signature_pointer_type (to_type, constp, volatilep)
241 tree to_type;
242 int constp, volatilep;
243 {
244 return
245 build_signature_pointer_or_reference_type (to_type, constp, volatilep, 0);
246 }
247
248 /* Construct, lay out and return the type of pointers to signature TO_TYPE. */
249
250 tree
251 build_signature_reference_type (to_type, constp, volatilep)
252 tree to_type;
253 int constp, volatilep;
254 {
255 return
256 build_signature_pointer_or_reference_type (to_type, constp, volatilep, 1);
257 }
258
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. */
261
262 static tree
263 get_sigtable_name (sig_type, rhs_type)
264 tree sig_type, rhs_type;
265 {
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);
273 int i, j;
274
275 for (i = 0; sig_ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++)
276 /* do nothing */;
277 while (sig_ptr[i] >= '0' && sig_ptr[i] <= '9')
278 i += 1;
279
280 for (j = 0; rhs_ptr[j] == OPERATOR_TYPENAME_FORMAT[j]; j++)
281 /* do nothing */;
282 while (rhs_ptr[j] >= '0' && rhs_ptr[j] <= '9')
283 j += 1;
284
285 if (IS_SIGNATURE (rhs_type))
286 sprintf (buf, SIGTABLE_NAME_FORMAT_LONG, sig_ptr+i, rhs_ptr+j,
287 global_sigtable_name_counter++);
288 else
289 sprintf (buf, SIGTABLE_NAME_FORMAT, sig_ptr+i, rhs_ptr+j);
290 return get_identifier (buf);
291 }
292
293 /* Build a field decl that points to a signature member function. */
294
295 static tree
296 build_member_function_pointer (member)
297 tree member;
298 {
299 char *namstr = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (member));
300 int namlen = IDENTIFIER_LENGTH (DECL_ASSEMBLER_NAME (member));
301 char *name;
302 tree entry;
303
304 name = (char *) alloca (namlen + sizeof (SIGNATURE_FIELD_NAME) + 2);
305 sprintf (name, SIGNATURE_FIELD_NAME_FORMAT, namstr);
306
307 /* @@ Do we really want to xref signature table fields? */
308 GNU_xref_ref (current_function_decl, name);
309
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;
314
315 /* @@ Do we really want to xref signature table fields? */
316 GNU_xref_decl (current_function_decl, entry);
317
318 return entry;
319 }
320
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).
327
328 The new FIELD_DECLs are appended at the end of the last (and only)
329 sublist of `list_of_fieldlists.'
330
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. */
333
334 void
335 append_signature_fields (list_of_fieldlists)
336 tree list_of_fieldlists;
337 {
338 tree l, x;
339 tree last_x = NULL_TREE;
340 tree mfptr;
341 tree last_mfptr;
342 tree mfptr_list = NULL_TREE;
343
344 /* For signatures it should actually be only a list with one element. */
345 for (l = list_of_fieldlists; l; l = TREE_CHAIN (l))
346 {
347 for (x = TREE_VALUE (l); x; x = TREE_CHAIN (x))
348 {
349 if (TREE_CODE (x) == FUNCTION_DECL)
350 {
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;
356 if (! mfptr_list)
357 mfptr_list = last_mfptr = mfptr;
358 else
359 {
360 TREE_CHAIN (last_mfptr) = mfptr;
361 last_mfptr = mfptr;
362 }
363 }
364 last_x = x;
365 }
366 }
367
368 /* Append the lists. */
369 if (last_x && mfptr_list)
370 {
371 TREE_CHAIN (last_x) = mfptr_list;
372 TREE_CHAIN (last_mfptr) = NULL_TREE;
373 }
374 }
375
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.
378
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. */
384
385 static int
386 match_method_types (sig_mtype, class_mtype)
387 tree sig_mtype, class_mtype;
388 {
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);
393
394 /* The return types have to be the same. */
395 if (! comptypes (sig_return_type, class_return_type, 1))
396 return 0;
397
398 /* Compare the first argument `this.' */
399 {
400 /* Get the type of what the `optr' is pointing to. */
401 tree sig_this =
402 TREE_TYPE (TREE_TYPE (TYPE_FIELDS (TREE_VALUE (sig_arg_types))));
403 tree class_this = TREE_VALUE (class_arg_types);
404
405 if (TREE_CODE (class_this) == RECORD_TYPE) /* Is `this' a sig ptr? */
406 class_this = TREE_TYPE (TREE_TYPE (TYPE_FIELDS (class_this)));
407 else
408 class_this = TREE_TYPE (class_this);
409
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)))
414 return 0;
415 }
416
417 sig_arg_types = TREE_CHAIN (sig_arg_types);
418 class_arg_types = TREE_CHAIN (class_arg_types);
419
420 /* The number of arguments and the argument types have to be the same. */
421 return compparms (sig_arg_types, class_arg_types, 3);
422 }
423
424 /* Undo casts of opaque type variables to the RHS types. */
425
426 static void
427 undo_casts (sig_ty)
428 tree sig_ty;
429 {
430 tree field = TYPE_FIELDS (sig_ty);
431
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);
437 }
438
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. */
443
444 static tree
445 build_signature_table_constructor (sig_ty, rhs)
446 tree sig_ty, rhs;
447 {
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;
452 tree last_rhs_field;
453 int sig_ptr_p = IS_SIGNATURE (rhstype);
454 int offset_p = sig_ptr_p;
455
456 rhstype = sig_ptr_p ? rhstype : TREE_TYPE (rhstype);
457
458 if (CLASSTYPE_TAGS (sig_ty))
459 {
460 sorry ("conformance check with signature containing class declarations");
461 return error_mark_node;
462 }
463
464 for (; sig_field; sig_field = TREE_CHAIN (sig_field))
465 {
466 tree basetype_path, baselink, basetypes;
467 tree sig_method, sig_mname, sig_mtype;
468 tree rhs_method, tbl_entry;
469
470 if (TREE_CODE (sig_field) == TYPE_DECL)
471 {
472 tree sig_field_type = TREE_TYPE (sig_field);
473
474 if (TYPE_MAIN_VARIANT (sig_field_type) == opaque_type_node)
475 {
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);
479
480 if (oty_type == NULL_TREE || oty_type == error_mark_node)
481 {
482 cp_error ("class `%T' does not contain type `%T'",
483 rhstype, oty_type);
484 undo_casts (sig_ty);
485 return error_mark_node;
486 }
487 oty_type = TREE_TYPE (oty_type);
488
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);
492 }
493 /* If we don't have an opaque type, we can ignore the `typedef'. */
494 continue;
495 }
496
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);
501
502 basetype_path = TYPE_BINFO (rhstype);
503 baselink = lookup_fnfields (basetype_path, sig_mname, 0);
504 if (baselink == NULL_TREE || baselink == error_mark_node)
505 {
506 if (! IS_DEFAULT_IMPLEMENTATION (sig_method))
507 {
508 cp_error ("class `%T' does not contain method `%D'",
509 rhstype, sig_mname);
510 undo_casts (sig_ty);
511 return error_mark_node;
512 }
513 else
514 {
515 /* We use the signature's default implementation. */
516 rhs_method = sig_method;
517 }
518 }
519 else
520 {
521 /* Find the class method of the correct type. */
522
523 basetypes = TREE_PURPOSE (baselink);
524 if (TREE_CODE (basetypes) == TREE_LIST)
525 basetypes = TREE_VALUE (basetypes);
526
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)))
532 break;
533
534 if (rhs_method == NULL_TREE
535 || (compute_access (basetypes, rhs_method)
536 != access_public_node))
537 {
538 error ("class `%s' does not contain a method conforming to `%s'",
539 TYPE_NAME_STRING (rhstype),
540 fndecl_as_string (sig_method, 1));
541 undo_casts (sig_ty);
542 return error_mark_node;
543 }
544 }
545
546 if (sig_ptr_p && rhs_method != sig_method)
547 {
548 tree rhs_field = DECL_MEMFUNC_POINTER_TO (rhs_method);
549
550 if (first_rhs_field == NULL_TREE)
551 {
552 first_rhs_field = rhs_field;
553 last_rhs_field = rhs_field;
554 }
555 else if (TREE_CHAIN (last_rhs_field) == rhs_field)
556 last_rhs_field = rhs_field;
557 else
558 offset_p = 0;
559
560 tbl_entry = build_component_ref (rhs, DECL_NAME (rhs_field),
561 NULL_TREE, 1);
562 }
563 else
564 {
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;
568
569 if (rhs_method == sig_method)
570 {
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 */
581 }
582 else if (DECL_VINDEX (rhs_method))
583 {
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)
588 delta = BINFO_OFFSET
589 (get_binfo (DECL_CONTEXT (rhs_method), rhstype, 1));
590 else
591 delta = BINFO_OFFSET
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),
595 rhstype, 0));
596 }
597 else
598 {
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),
603 rhstype, 1));
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;
609 }
610
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);
620
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);
625
626 if (DECL_VINDEX (rhs_method))
627 {
628 vt_off = convert (TREE_TYPE (vt_off_decl), vt_off);
629
630 tbl_entry = build_tree_list (vt_off_decl, vt_off);
631 }
632 else
633 {
634 pfn = convert (TREE_TYPE (pfn_decl), pfn);
635
636 tbl_entry = build_tree_list (pfn_decl, pfn);
637 }
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);
644
645 TREE_CONSTANT (tbl_entry) = 1;
646 }
647
648 /* Chain those function address expressions together. */
649 if (result)
650 result = tree_cons (NULL_TREE, tbl_entry, result);
651 else
652 result = build_tree_list (NULL_TREE, tbl_entry);
653 }
654
655 if (result == NULL_TREE)
656 {
657 /* The signature was empty, we don't need a signature table. */
658 undo_casts (sig_ty);
659 return NULL_TREE;
660 }
661
662 if (offset_p)
663 {
664 if (first_rhs_field == TYPE_FIELDS (rhstype))
665 {
666 /* The sptr field on the lhs can be copied from the rhs. */
667 undo_casts (sig_ty);
668 return integer_zero_node;
669 }
670 else
671 {
672 /* The sptr field on the lhs will point into the rhs sigtable. */
673 undo_casts (sig_ty);
674 return build_component_ref (rhs, DECL_NAME (first_rhs_field),
675 NULL_TREE, 0);
676 }
677 }
678
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;
683
684 undo_casts (sig_ty);
685 return result;
686 }
687
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.
692
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
695 static ones. */
696
697 static tree
698 build_sigtable (sig_type, rhs_type, init_from)
699 tree sig_type, rhs_type, init_from;
700 {
701 tree name = NULL_TREE;
702 tree decl = NULL_TREE;
703 tree init_expr;
704
705 push_obstacks_nochange ();
706 end_temporary_allocation ();
707
708 if (! IS_SIGNATURE (rhs_type))
709 {
710 name = get_sigtable_name (sig_type, rhs_type);
711 decl = IDENTIFIER_GLOBAL_VALUE (name);
712 }
713 if (decl == NULL_TREE)
714 {
715 tree init;
716
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))
723 {
724 error ("signature with opaque type implemented by multiple classes");
725 return error_mark_node;
726 }
727 SIGTABLE_HAS_BEEN_GENERATED (sig_type) = 1;
728
729 init_expr = build_signature_table_constructor (sig_type, init_from);
730 if (init_expr == NULL_TREE || TREE_CODE (init_expr) != CONSTRUCTOR)
731 return init_expr;
732
733 if (name == NULL_TREE)
734 name = get_sigtable_name (sig_type, rhs_type);
735 {
736 tree context = current_function_decl;
737
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;
743 }
744 IDENTIFIER_GLOBAL_VALUE (name) = decl;
745 store_init_value (decl, init_expr);
746 if (IS_SIGNATURE (rhs_type))
747 {
748 init = DECL_INITIAL (decl);
749 DECL_INITIAL (decl) = error_mark_node;
750 }
751
752 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
753 DECL_ALIGN (decl));
754 #if 0
755 /* GDB-4.7 doesn't find the initialization value of a signature table
756 when it is constant. */
757 TREE_READONLY (decl) = 1;
758 #endif
759 TREE_STATIC (decl) = 1;
760 TREE_USED (decl) = 1;
761
762 make_decl_rtl (decl, NULL, 1);
763 if (IS_SIGNATURE (rhs_type))
764 expand_static_init (decl, init);
765 }
766
767 pop_obstacks ();
768
769 return decl;
770 }
771
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. */
775
776 tree
777 build_signature_pointer_constructor (lhs, rhs)
778 tree lhs, rhs;
779 {
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;
787 tree result;
788
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)))))
794 {
795 error ("invalid assignment to signature pointer or reference");
796 return error_mark_node;
797 }
798
799 if (TYPE_SIZE (sig_ty) == NULL_TREE)
800 {
801 cp_error ("undefined signature `%T' used in signature %s declaration",
802 sig_ty,
803 IS_SIGNATURE_POINTER (lhstype) ? "pointer" : "reference");
804 return error_mark_node;
805 }
806
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))
810 {
811 current_obstack = &permanent_obstack;
812 saveable_obstack = &permanent_obstack;
813 }
814
815 if (TYPE_LANG_SPECIFIC (rhstype) &&
816 (IS_SIGNATURE_POINTER (rhstype) || IS_SIGNATURE_REFERENCE (rhstype)))
817 {
818 if (SIGNATURE_TYPE (rhstype) == sig_ty)
819 {
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);
823 }
824 else
825 {
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),
830 rhs_sptr_ref);
831
832 sig_tbl = build_sigtable (sig_ty, SIGNATURE_TYPE (rhstype), rhs_tbl);
833 if (sig_tbl == error_mark_node)
834 return error_mark_node;
835
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;
843 else
844 sptr_expr = build_unary_op (ADDR_EXPR, sig_tbl, 0);
845 TREE_TYPE (sptr_expr) = build_pointer_type (sig_ty);
846 }
847 }
848 else
849 {
850 sig_tbl = build_sigtable (sig_ty, TREE_TYPE (rhstype), rhs);
851 if (sig_tbl == error_mark_node)
852 return error_mark_node;
853
854 optr_expr = rhs;
855 if (sig_tbl == NULL_TREE)
856 /* The signature was empty. The signature pointer is
857 pretty useless, but the user has been warned. */
858 {
859 sptr_expr = copy_node (null_pointer_node);
860 TREE_TYPE (sptr_expr) = build_pointer_type (sig_ty);
861 }
862 else
863 sptr_expr = build_unary_op (ADDR_EXPR, sig_tbl, 0);
864 }
865
866 if (initp)
867 {
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);
873 }
874 else
875 {
876 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype))
877 readonly_error (lhs, "assignment", 0);
878
879 optr_expr = build_modify_expr (build_optr_ref (lhs), NOP_EXPR,
880 optr_expr);
881 sptr_expr = build_modify_expr (build_sptr_ref (lhs), NOP_EXPR,
882 sptr_expr);
883
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);
888 }
889
890 current_obstack = ambient_obstack;
891 saveable_obstack = ambient_saveable_obstack;
892 return result;
893 }
894
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. */
899
900 static tree
901 save_this (instance)
902 tree instance;
903 {
904 tree decl;
905
906 if (TREE_CODE_CLASS (TREE_CODE (instance)) == 'd')
907 decl = instance;
908 else
909 {
910 decl = build_decl (VAR_DECL, NULL_TREE, TREE_TYPE (instance));
911 DECL_REGISTER (decl) = 1;
912 layout_decl (decl, 0);
913 expand_decl (decl);
914 }
915
916 return decl;
917 }
918
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. */
923
924 tree
925 build_signature_method_call (function, parms)
926 tree function, parms;
927 {
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,
937 signature_tbl_ptr),
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;
941
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));
949
950 if (IS_DEFAULT_IMPLEMENTATION (function))
951 {
952 pfn = save_expr (pfn);
953 deflt_call = build_function_call (pfn, parms);
954 }
955
956 new_object_ptr = build (PLUS_EXPR, build_pointer_type (basetype),
957 convert (ptrdiff_type_node, object_ptr),
958 convert (ptrdiff_type_node, delta));
959
960 parms = tree_cons (NULL_TREE,
961 convert (build_pointer_type (basetype), object_ptr),
962 TREE_CHAIN (parms));
963 new_parms = tree_cons (NULL_TREE, new_object_ptr, TREE_CHAIN (parms));
964
965 {
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))));
968
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));
973
974 direct_call = build_function_call (pfn, new_parms);
975
976 {
977 tree vfld, vtbl, aref;
978
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),
984 NULL_PTR);
985 aref = build_array_ref (vtbl, idx);
986
987 if (flag_vtable_thunks)
988 vfn = aref;
989 else
990 vfn = build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
991
992 TREE_TYPE (vfn) = build_pointer_type (TREE_TYPE (function));
993
994 virtual_call = build_function_call (vfn, new_parms);
995 }
996
997 /* Undo the cast, make `this' a signature pointer again. */
998 TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (TREE_TYPE (pfn)))) = old_this;
999 }
1000
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)))
1007 {
1008 compiler_error ("cannot build call of signature member function `%s'",
1009 fndecl_as_string (function, 1));
1010 return error_mark_node;
1011 }
1012
1013 if (IS_DEFAULT_IMPLEMENTATION (function))
1014 {
1015 tree test = build_binary_op_nodefault (LT_EXPR, tag, integer_zero_node,
1016 LT_EXPR);
1017 result = build_conditional_expr (tag,
1018 build_conditional_expr (test,
1019 deflt_call,
1020 virtual_call),
1021 direct_call);
1022 }
1023 else
1024 result = build_conditional_expr (tag, virtual_call, direct_call);
1025
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),
1030 result);
1031
1032 return result;
1033 }
1034
1035 /* Create a COMPONENT_REF expression for referencing the OPTR field
1036 of a signature pointer or reference. */
1037
1038 tree
1039 build_optr_ref (instance)
1040 tree instance;
1041 {
1042 tree field = get_identifier (SIGNATURE_OPTR_NAME);
1043
1044 return build_component_ref (instance, field, NULL_TREE, 1);
1045 }
1046
1047 /* Create a COMPONENT_REF expression for referencing the SPTR field
1048 of a signature pointer or reference. */
1049
1050 tree
1051 build_sptr_ref (instance)
1052 tree instance;
1053 {
1054 tree field = get_identifier (SIGNATURE_SPTR_NAME);
1055
1056 return build_component_ref (instance, field, NULL_TREE, 1);
1057 }
This page took 0.08692 seconds and 6 git commands to generate.