ecfa82c4012353104714908bc509c77e835dfffe
[gcc.git] / gcc / cp / method.c
1 /* Handle the hair of processing (but not expanding) inline functions.
2 Also manage function and variable name overloading.
3 Copyright (C) 1987, 89, 92-97, 1998, 1999 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23
24 #ifndef __GNUC__
25 #define __inline
26 #endif
27
28 #ifndef PARM_CAN_BE_ARRAY_TYPE
29 #define PARM_CAN_BE_ARRAY_TYPE 1
30 #endif
31
32 /* Handle method declarations. */
33 #include "config.h"
34 #include "system.h"
35 #include "tree.h"
36 #include "cp-tree.h"
37 #include "obstack.h"
38 #include "rtl.h"
39 #include "expr.h"
40 #include "output.h"
41 #include "hard-reg-set.h"
42 #include "flags.h"
43 #include "toplev.h"
44
45 /* TREE_LIST of the current inline functions that need to be
46 processed. */
47 struct pending_inline *pending_inlines;
48
49 int static_labelno;
50
51 #define obstack_chunk_alloc xmalloc
52 #define obstack_chunk_free free
53
54 /* Obstack where we build text strings for overloading, etc. */
55 static struct obstack scratch_obstack;
56 static char *scratch_firstobj;
57
58 static void icat PROTO((HOST_WIDE_INT));
59 static void dicat PROTO((HOST_WIDE_INT, HOST_WIDE_INT));
60 static int old_backref_index PROTO((tree));
61 static int flush_repeats PROTO((int, tree));
62 static void build_overload_identifier PROTO((tree));
63 static void build_overload_nested_name PROTO((tree));
64 static void build_overload_int PROTO((tree, int));
65 static void build_overload_identifier PROTO((tree));
66 static void build_qualified_name PROTO((tree));
67 static void build_overload_value PROTO((tree, tree, int));
68 static void issue_nrepeats PROTO((int, tree));
69 static char *build_mangled_name PROTO((tree,int,int));
70 static void process_modifiers PROTO((tree));
71 static void process_overload_item PROTO((tree,int));
72 static void do_build_assign_ref PROTO((tree));
73 static void do_build_copy_constructor PROTO((tree));
74 static tree largest_union_member PROTO((tree));
75 static void build_template_template_parm_names PROTO((tree));
76 static void build_template_parm_names PROTO((tree, tree));
77 static void build_underscore_int PROTO((int));
78 static void start_squangling PROTO((void));
79 static void end_squangling PROTO((void));
80 static int check_ktype PROTO((tree, int));
81 static int issue_ktype PROTO((tree));
82 static void build_overload_scope_ref PROTO((tree));
83 static void build_mangled_template_parm_index PROTO((char *, tree));
84 #if HOST_BITS_PER_WIDE_INT >= 64
85 static void build_mangled_C9x_name PROTO((int));
86 #endif
87 static int is_back_referenceable_type PROTO((tree));
88 static int check_btype PROTO((tree));
89 static void build_mangled_name_for_type PROTO((tree));
90 static void build_mangled_name_for_type_with_Gcode PROTO((tree, int));
91
92 # define OB_INIT() (scratch_firstobj ? (obstack_free (&scratch_obstack, scratch_firstobj), 0) : 0)
93 # define OB_PUTC(C) (obstack_1grow (&scratch_obstack, (C)))
94 # define OB_PUTC2(C1,C2) \
95 (obstack_1grow (&scratch_obstack, (C1)), obstack_1grow (&scratch_obstack, (C2)))
96 # define OB_PUTS(S) (obstack_grow (&scratch_obstack, (S), sizeof (S) - 1))
97 # define OB_PUTID(ID) \
98 (obstack_grow (&scratch_obstack, IDENTIFIER_POINTER (ID), \
99 IDENTIFIER_LENGTH (ID)))
100 # define OB_PUTCP(S) (obstack_grow (&scratch_obstack, (S), strlen (S)))
101 # define OB_FINISH() (obstack_1grow (&scratch_obstack, '\0'))
102 # define OB_LAST() (obstack_next_free (&scratch_obstack)[-1])
103
104 void
105 init_method ()
106 {
107 gcc_obstack_init (&scratch_obstack);
108 scratch_firstobj = (char *)obstack_alloc (&scratch_obstack, 0);
109 }
110
111 /* This must be large enough to hold any printed integer or floating-point
112 value. */
113 static char digit_buffer[128];
114
115 /* Move inline function definitions out of structure so that they
116 can be processed normally. CNAME is the name of the class
117 we are working from, METHOD_LIST is the list of method lists
118 of the structure. We delete friend methods here, after
119 saving away their inline function definitions (if any). */
120
121 void
122 do_inline_function_hair (type, friend_list)
123 tree type, friend_list;
124 {
125 tree method = TYPE_METHODS (type);
126
127 if (method && TREE_CODE (method) == TREE_VEC)
128 {
129 if (TREE_VEC_ELT (method, 1))
130 method = TREE_VEC_ELT (method, 1);
131 else if (TREE_VEC_ELT (method, 0))
132 method = TREE_VEC_ELT (method, 0);
133 else
134 method = TREE_VEC_ELT (method, 2);
135 }
136
137 while (method)
138 {
139 /* Do inline member functions. */
140 struct pending_inline *info = DECL_PENDING_INLINE_INFO (method);
141 if (info)
142 {
143 tree args;
144
145 my_friendly_assert (info->fndecl == method, 238);
146 args = DECL_ARGUMENTS (method);
147 while (args)
148 {
149 DECL_CONTEXT (args) = method;
150 args = TREE_CHAIN (args);
151 }
152 }
153 method = TREE_CHAIN (method);
154 }
155 while (friend_list)
156 {
157 tree fndecl = TREE_VALUE (friend_list);
158 struct pending_inline *info = DECL_PENDING_INLINE_INFO (fndecl);
159 if (info)
160 {
161 tree args;
162
163 my_friendly_assert (info->fndecl == fndecl, 239);
164 args = DECL_ARGUMENTS (fndecl);
165 while (args)
166 {
167 DECL_CONTEXT (args) = fndecl;
168 args = TREE_CHAIN (args);
169 }
170 }
171
172 friend_list = TREE_CHAIN (friend_list);
173 }
174 }
175 \f
176 /* Here is where overload code starts. */
177
178 /* type tables for K and B type compression */
179 static tree *btypelist = NULL;
180 static tree *ktypelist = NULL;
181 static int maxbsize = 0;
182 static int maxksize = 0;
183
184 /* number of each type seen */
185 static int maxbtype = 0;
186 static int maxktype = 0;
187
188 /* Array of types seen so far in top-level call to `build_mangled_name'.
189 Allocated and deallocated by caller. */
190 static tree *typevec = NULL;
191 static int typevec_size;
192
193 /* Number of types interned by `build_mangled_name' so far. */
194 static int maxtype = 0;
195
196 /* Nonzero if we should not try folding parameter types. */
197 static int nofold;
198
199 /* This appears to be set to true if an underscore is required to be
200 comcatenated before another number can be outputed. */
201 static int numeric_output_need_bar;
202
203 static __inline void
204 start_squangling ()
205 {
206 if (flag_do_squangling)
207 {
208 nofold = 0;
209 maxbtype = 0;
210 maxktype = 0;
211 maxbsize = 50;
212 maxksize = 50;
213 btypelist = (tree *)xmalloc (sizeof (tree) * maxbsize);
214 ktypelist = (tree *)xmalloc (sizeof (tree) * maxksize);
215 }
216 }
217
218 static __inline void
219 end_squangling ()
220 {
221 if (flag_do_squangling)
222 {
223 if (ktypelist)
224 free (ktypelist);
225 if (btypelist)
226 free (btypelist);
227 maxbsize = 0;
228 maxksize = 0;
229 maxbtype = 0;
230 maxktype = 0;
231 ktypelist = NULL;
232 btypelist = NULL;
233 }
234 }
235
236 /* Code to concatenate an asciified integer to a string. */
237
238 static __inline void
239 icat (i)
240 HOST_WIDE_INT i;
241 {
242 unsigned HOST_WIDE_INT ui;
243
244 /* Handle this case first, to go really quickly. For many common values,
245 the result of ui/10 below is 1. */
246 if (i == 1)
247 {
248 OB_PUTC ('1');
249 return;
250 }
251
252 if (i >= 0)
253 ui = i;
254 else
255 {
256 OB_PUTC ('m');
257 ui = -i;
258 }
259
260 if (ui >= 10)
261 icat (ui / 10);
262
263 OB_PUTC ('0' + (ui % 10));
264 }
265
266 static void
267 dicat (lo, hi)
268 HOST_WIDE_INT lo, hi;
269 {
270 unsigned HOST_WIDE_INT ulo, uhi, qlo, qhi;
271
272 if (hi >= 0)
273 {
274 uhi = hi;
275 ulo = lo;
276 }
277 else
278 {
279 uhi = (lo == 0 ? -hi : -hi-1);
280 ulo = -lo;
281 }
282 if (uhi == 0
283 && ulo < ((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)))
284 {
285 icat (ulo);
286 return;
287 }
288 /* Divide 2^HOST_WIDE_INT*uhi+ulo by 10. */
289 qhi = uhi / 10;
290 uhi = uhi % 10;
291 qlo = uhi * (((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)) / 5);
292 qlo += ulo / 10;
293 ulo = ulo % 10;
294 ulo += uhi * (((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)) % 5)
295 * 2;
296 qlo += ulo / 10;
297 ulo = ulo % 10;
298 /* Quotient is 2^HOST_WIDE_INT*qhi+qlo, remainder is ulo. */
299 dicat (qlo, qhi);
300 OB_PUTC ('0' + ulo);
301 }
302
303 /* Returns the index of TYPE in the typevec, or -1 if it's not there. */
304
305 static __inline int
306 old_backref_index (type)
307 tree type;
308 {
309 int tindex = 0;
310
311 if (! is_back_referenceable_type (type))
312 return -1;
313
314 /* The entry for this parm is at maxtype-1, so don't look there for
315 something to repeat. */
316 for (tindex = 0; tindex < maxtype - 1; ++tindex)
317 if (same_type_p (typevec[tindex], type))
318 break;
319
320 if (tindex == maxtype - 1)
321 return -1;
322
323 return tindex;
324 }
325
326 /* Old mangling style: If TYPE has already been used in the parameter list,
327 emit a backward reference and return non-zero; otherwise, return 0.
328
329 NREPEATS is the number of repeats we've recorded of this type, or 0 if
330 this is the first time we've seen it and we're just looking to see if
331 it had been used before. */
332
333 static __inline int
334 flush_repeats (nrepeats, type)
335 int nrepeats;
336 tree type;
337 {
338 int tindex = old_backref_index (type);
339
340 if (tindex == -1)
341 {
342 my_friendly_assert (nrepeats == 0, 990316);
343 return 0;
344 }
345
346 if (nrepeats > 1)
347 {
348 OB_PUTC ('N');
349 icat (nrepeats);
350 if (nrepeats > 9)
351 OB_PUTC ('_');
352 }
353 else
354 OB_PUTC ('T');
355 icat (tindex);
356 if (tindex > 9)
357 OB_PUTC ('_');
358
359 return 1;
360 }
361
362 /* Returns nonzero iff this is a type to which we will want to make
363 back-references (using the `B' code). */
364
365 static int
366 is_back_referenceable_type (type)
367 tree type;
368 {
369 /* For some reason, the Java folks don't want back refs on these. */
370 if (TYPE_FOR_JAVA (type))
371 return 0;
372
373 switch (TREE_CODE (type))
374 {
375 case INTEGER_TYPE:
376 case REAL_TYPE:
377 case VOID_TYPE:
378 case BOOLEAN_TYPE:
379 /* These types have single-character manglings, so there's no
380 point in generating back-references. */
381 return 0;
382
383 case TEMPLATE_TYPE_PARM:
384 /* It would be a bit complex to demangle signatures correctly if
385 we generated back-references to these, and the manglings of
386 type parameters are short. */
387 return 0;
388
389 default:
390 return 1;
391 }
392 }
393
394 /* Issue the squangling code indicating NREPEATS repetitions of TYPE,
395 which was the last parameter type output. */
396
397 static void
398 issue_nrepeats (nrepeats, type)
399 int nrepeats;
400 tree type;
401 {
402 if (nrepeats == 1 && !is_back_referenceable_type (type))
403 /* For types whose manglings are short, don't bother using the
404 repetition code if there's only one repetition, since the
405 repetition code will be about as long as the ordinary mangling. */
406 build_mangled_name_for_type (type);
407 else
408 {
409 OB_PUTC ('n');
410 icat (nrepeats);
411 if (nrepeats > 9)
412 OB_PUTC ('_');
413 }
414 }
415
416 /* Check to see if a tree node has been entered into the Kcode typelist.
417 If not, add it. Returns -1 if it isn't found, otherwise returns the
418 index. */
419
420 static int
421 check_ktype (node, add)
422 tree node;
423 int add;
424 {
425 int x;
426 tree localnode = node;
427
428 if (ktypelist == NULL)
429 return -1;
430
431 if (TREE_CODE (node) == TYPE_DECL)
432 localnode = TREE_TYPE (node);
433
434 for (x=0; x < maxktype; x++)
435 {
436 if (same_type_p (localnode, ktypelist[x]))
437 return x;
438 }
439 /* Didn't find it, so add it here. */
440 if (add)
441 {
442 if (maxksize <= maxktype)
443 {
444 maxksize = maxksize* 3 / 2;
445 ktypelist = (tree *)xrealloc (ktypelist, sizeof (tree) * maxksize);
446 }
447 ktypelist[maxktype++] = localnode;
448 }
449 return -1;
450 }
451
452
453 static __inline int
454 issue_ktype (decl)
455 tree decl;
456 {
457 int kindex;
458 kindex = check_ktype (decl, FALSE);
459 if (kindex != -1)
460 {
461 OB_PUTC ('K');
462 icat (kindex);
463 if (kindex > 9)
464 OB_PUTC ('_');
465 return TRUE;
466 }
467 return FALSE;
468 }
469
470 /* Build a representation for DECL, which may be an entity not at
471 global scope. If so, a marker indicating that the name is
472 qualified has already been output, but the qualifying context has
473 not. */
474
475 static void
476 build_overload_nested_name (decl)
477 tree decl;
478 {
479 tree context;
480
481 if (ktypelist && issue_ktype (decl))
482 return;
483
484 if (decl == global_namespace)
485 return;
486
487 context = CP_DECL_CONTEXT (decl);
488
489 /* try to issue a K type, and if we can't continue the normal path */
490 if (!(ktypelist && issue_ktype (context)))
491 {
492 /* For a template type parameter, we want to output an 'Xn'
493 rather than 'T' or some such. */
494 if (TREE_CODE (context) == TEMPLATE_TYPE_PARM
495 || TREE_CODE (context) == TEMPLATE_TEMPLATE_PARM)
496 build_mangled_name_for_type (context);
497 else
498 {
499 if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
500 context = TYPE_NAME (context);
501 build_overload_nested_name (context);
502 }
503 }
504
505 if (TREE_CODE (decl) == FUNCTION_DECL)
506 {
507 tree name = DECL_ASSEMBLER_NAME (decl);
508 char *label;
509
510 ASM_FORMAT_PRIVATE_NAME (label, IDENTIFIER_POINTER (name), static_labelno);
511 static_labelno++;
512
513 if (numeric_output_need_bar)
514 OB_PUTC ('_');
515 icat (strlen (label));
516 OB_PUTCP (label);
517 numeric_output_need_bar = 1;
518 }
519 else if (TREE_CODE (decl) == NAMESPACE_DECL)
520 build_overload_identifier (DECL_NAME (decl));
521 else /* TYPE_DECL */
522 build_overload_identifier (decl);
523 }
524
525 /* Output the decimal representation of I. If I > 9, the decimal
526 representation is preceeded and followed by an underscore. */
527
528 static void
529 build_underscore_int (i)
530 int i;
531 {
532 if (i > 9)
533 OB_PUTC ('_');
534 icat (i);
535 if (i > 9)
536 OB_PUTC ('_');
537 }
538
539 static void
540 build_overload_scope_ref (value)
541 tree value;
542 {
543 OB_PUTC2 ('Q', '2');
544 numeric_output_need_bar = 0;
545 build_mangled_name_for_type (TREE_OPERAND (value, 0));
546 build_overload_identifier (TREE_OPERAND (value, 1));
547 }
548
549 /* Encoding for an INTEGER_CST value. */
550
551 static void
552 build_overload_int (value, in_template)
553 tree value;
554 int in_template;
555 {
556 if (in_template && TREE_CODE (value) != INTEGER_CST)
557 {
558 if (TREE_CODE (value) == SCOPE_REF)
559 {
560 build_overload_scope_ref (value);
561 return;
562 }
563
564 OB_PUTC ('E');
565 numeric_output_need_bar = 0;
566
567 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (value))))
568 {
569 int i;
570 int operands = tree_code_length[(int) TREE_CODE (value)];
571 tree id;
572 char* name;
573
574 id = ansi_opname [(int) TREE_CODE (value)];
575 my_friendly_assert (id != NULL_TREE, 0);
576 name = IDENTIFIER_POINTER (id);
577 if (name[0] != '_' || name[1] != '_')
578 /* On some erroneous inputs, we can get here with VALUE a
579 LOOKUP_EXPR. In that case, the NAME will be the
580 identifier for "<invalid operator>". We must survive
581 this routine in order to issue a sensible error
582 message, so we fall through to the case below. */
583 goto bad_value;
584
585 for (i = 0; i < operands; ++i)
586 {
587 tree operand;
588 enum tree_code tc;
589
590 /* We just outputted either the `E' or the name of the
591 operator. */
592 numeric_output_need_bar = 0;
593
594 if (i != 0)
595 /* Skip the leading underscores. */
596 OB_PUTCP (name + 2);
597
598 operand = TREE_OPERAND (value, i);
599 tc = TREE_CODE (operand);
600
601 if (TREE_CODE_CLASS (tc) == 't')
602 /* We can get here with sizeof, e.g.:
603
604 template <class T> void f(A<sizeof(T)>); */
605 build_mangled_name_for_type (operand);
606 else if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (tc)))
607 build_overload_int (operand, in_template);
608 else
609 build_overload_value (TREE_TYPE (operand),
610 operand,
611 in_template);
612 }
613 }
614 else
615 {
616 /* We don't ever want this output, but it's
617 inconvenient not to be able to build the string.
618 This should cause assembler errors we'll notice. */
619
620 static int n;
621 bad_value:
622 sprintf (digit_buffer, " *%d", n++);
623 OB_PUTCP (digit_buffer);
624 }
625
626 OB_PUTC ('W');
627 numeric_output_need_bar = 0;
628 return;
629 }
630
631 my_friendly_assert (TREE_CODE (value) == INTEGER_CST, 243);
632 if (TYPE_PRECISION (TREE_TYPE (value)) == 2 * HOST_BITS_PER_WIDE_INT)
633 {
634 if (TREE_INT_CST_HIGH (value)
635 != (TREE_INT_CST_LOW (value) >> (HOST_BITS_PER_WIDE_INT - 1)))
636 {
637 /* need to print a DImode value in decimal */
638 dicat (TREE_INT_CST_LOW (value), TREE_INT_CST_HIGH (value));
639 numeric_output_need_bar = 1;
640 return;
641 }
642 /* else fall through to print in smaller mode */
643 }
644 /* Wordsize or smaller */
645 icat (TREE_INT_CST_LOW (value));
646 numeric_output_need_bar = 1;
647 }
648
649
650 /* Output S followed by a representation of the TEMPLATE_PARM_INDEX
651 supplied in INDEX. */
652
653 static void
654 build_mangled_template_parm_index (s, index)
655 char* s;
656 tree index;
657 {
658 OB_PUTCP (s);
659 build_underscore_int (TEMPLATE_PARM_IDX (index));
660 /* We use the LEVEL, not the ORIG_LEVEL, because the mangling is a
661 representation of the function from the point of view of its
662 type. */
663 build_underscore_int (TEMPLATE_PARM_LEVEL (index));
664 }
665
666
667 /* Mangling for C9X integer types (and Cygnus extensions for 128-bit
668 and other types) is based on the letter "I" followed by the hex
669 representations of the bitsize for the type in question. For
670 encodings that result in larger than two digits, a leading and
671 trailing underscore is added.
672
673 Thus:
674 int1_t = 001 = I01
675 int8_t = 008 = I08
676 int16_t = 010 = I10
677 int24_t = 018 = I18
678 int32_t = 020 = I20
679 int64_t = 040 = I40
680 int80_t = 050 = I50
681 int128_t = 080 = I80
682 int256_t = 100 = I_100_
683 int512_t = 200 = I_200_
684
685 Given an integer in decimal format, mangle according to this scheme. */
686
687 #if HOST_BITS_PER_WIDE_INT >= 64
688 static void
689 build_mangled_C9x_name (bits)
690 int bits;
691 {
692 char mangled[10] = "";
693
694 if (bits > 255)
695 sprintf (mangled, "I_%x_", bits);
696 else
697 sprintf (mangled, "I%.2x", bits);
698
699 OB_PUTCP (mangled);
700 }
701 #endif
702
703 static void
704 build_overload_value (type, value, in_template)
705 tree type, value;
706 int in_template;
707 {
708 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (type)) == 't', 0);
709
710 while (TREE_CODE (value) == NON_LVALUE_EXPR
711 || TREE_CODE (value) == NOP_EXPR)
712 value = TREE_OPERAND (value, 0);
713
714 if (numeric_output_need_bar)
715 {
716 OB_PUTC ('_');
717 numeric_output_need_bar = 0;
718 }
719
720 if (TREE_CODE (value) == TEMPLATE_PARM_INDEX)
721 {
722 build_mangled_template_parm_index ("Y", value);
723 return;
724 }
725
726 if (TYPE_PTRMEM_P (type))
727 {
728 if (TREE_CODE (value) != PTRMEM_CST)
729 /* We should have already rejected this pointer to member,
730 since it is not a constant. */
731 my_friendly_abort (0);
732
733 /* Get the actual FIELD_DECL. */
734 value = PTRMEM_CST_MEMBER (value);
735 my_friendly_assert (TREE_CODE (value) == FIELD_DECL, 0);
736
737 /* Output the name of the field. */
738 build_overload_identifier (DECL_NAME (value));
739 return;
740 }
741
742 switch (TREE_CODE (type))
743 {
744 case INTEGER_TYPE:
745 case ENUMERAL_TYPE:
746 case BOOLEAN_TYPE:
747 {
748 build_overload_int (value, in_template);
749 return;
750 }
751 case REAL_TYPE:
752 {
753 REAL_VALUE_TYPE val;
754 char *bufp = digit_buffer;
755
756 pedwarn ("ANSI C++ forbids floating-point template arguments");
757
758 my_friendly_assert (TREE_CODE (value) == REAL_CST, 244);
759 val = TREE_REAL_CST (value);
760 if (REAL_VALUE_ISNAN (val))
761 {
762 sprintf (bufp, "NaN");
763 }
764 else
765 {
766 if (REAL_VALUE_NEGATIVE (val))
767 {
768 val = REAL_VALUE_NEGATE (val);
769 *bufp++ = 'm';
770 }
771 if (REAL_VALUE_ISINF (val))
772 {
773 sprintf (bufp, "Infinity");
774 }
775 else
776 {
777 REAL_VALUE_TO_DECIMAL (val, "%.20e", bufp);
778 bufp = (char *) index (bufp, 'e');
779 if (!bufp)
780 strcat (digit_buffer, "e0");
781 else
782 {
783 char *p;
784 bufp++;
785 if (*bufp == '-')
786 {
787 *bufp++ = 'm';
788 }
789 p = bufp;
790 if (*p == '+')
791 p++;
792 while (*p == '0')
793 p++;
794 if (*p == 0)
795 {
796 *bufp++ = '0';
797 *bufp = 0;
798 }
799 else if (p != bufp)
800 {
801 while (*p)
802 *bufp++ = *p++;
803 *bufp = 0;
804 }
805 }
806 #ifdef NO_DOT_IN_LABEL
807 bufp = (char *) index (bufp, '.');
808 if (bufp)
809 *bufp = '_';
810 #endif
811 }
812 }
813 OB_PUTCP (digit_buffer);
814 numeric_output_need_bar = 1;
815 return;
816 }
817 case POINTER_TYPE:
818 if (TREE_CODE (value) == INTEGER_CST)
819 {
820 build_overload_int (value, in_template);
821 return;
822 }
823 else if (TREE_CODE (value) == TEMPLATE_PARM_INDEX)
824 {
825 build_mangled_template_parm_index ("", value);
826 numeric_output_need_bar = 1;
827 return;
828 }
829
830 value = TREE_OPERAND (value, 0);
831
832 /* Fall through. */
833
834 case REFERENCE_TYPE:
835 if (TREE_CODE (value) == VAR_DECL)
836 {
837 my_friendly_assert (DECL_NAME (value) != 0, 245);
838 build_overload_identifier (DECL_ASSEMBLER_NAME (value));
839 return;
840 }
841 else if (TREE_CODE (value) == FUNCTION_DECL)
842 {
843 my_friendly_assert (DECL_NAME (value) != 0, 246);
844 build_overload_identifier (DECL_ASSEMBLER_NAME (value));
845 return;
846 }
847 else if (TREE_CODE (value) == SCOPE_REF)
848 build_overload_scope_ref (value);
849 else
850 my_friendly_abort (71);
851 break; /* not really needed */
852
853 case RECORD_TYPE:
854 {
855 tree delta;
856 tree idx;
857 tree pfn;
858 tree delta2;
859
860 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 0);
861 my_friendly_assert (TREE_CODE (value) == PTRMEM_CST, 0);
862
863 expand_ptrmemfunc_cst (value, &delta, &idx, &pfn, &delta2);
864 build_overload_int (delta, in_template);
865 OB_PUTC ('_');
866 build_overload_int (idx, in_template);
867 OB_PUTC ('_');
868 if (pfn)
869 {
870 numeric_output_need_bar = 0;
871 build_overload_identifier (DECL_ASSEMBLER_NAME
872 (PTRMEM_CST_MEMBER (value)));
873 }
874 else
875 {
876 OB_PUTC ('i');
877 build_overload_int (delta2, in_template);
878 }
879 }
880 break;
881
882 default:
883 sorry ("conversion of %s as template parameter",
884 tree_code_name [(int) TREE_CODE (type)]);
885 my_friendly_abort (72);
886 }
887 }
888
889
890 /* Add encodings for the declaration of template template parameters.
891 PARMLIST must be a TREE_VEC. */
892
893 static void
894 build_template_template_parm_names (parmlist)
895 tree parmlist;
896 {
897 int i, nparms;
898
899 my_friendly_assert (TREE_CODE (parmlist) == TREE_VEC, 246.5);
900 nparms = TREE_VEC_LENGTH (parmlist);
901 icat (nparms);
902 for (i = 0; i < nparms; i++)
903 {
904 tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
905 if (TREE_CODE (parm) == TYPE_DECL)
906 {
907 /* This parameter is a type. */
908 OB_PUTC ('Z');
909 }
910 else if (TREE_CODE (parm) == TEMPLATE_DECL)
911 {
912 /* This parameter is a template. */
913 OB_PUTC ('z');
914 build_template_template_parm_names (DECL_INNERMOST_TEMPLATE_PARMS (parm));
915 }
916 else
917 /* It's a PARM_DECL. */
918 build_mangled_name_for_type (TREE_TYPE (parm));
919 }
920 }
921
922
923 /* Add encodings for the vector of template parameters in PARMLIST,
924 given the vector of arguments to be substituted in ARGLIST. */
925
926 static void
927 build_template_parm_names (parmlist, arglist)
928 tree parmlist;
929 tree arglist;
930 {
931 int i, nparms;
932 tree inner_args = innermost_args (arglist);
933
934 nparms = TREE_VEC_LENGTH (parmlist);
935 icat (nparms);
936 for (i = 0; i < nparms; i++)
937 {
938 tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
939 tree arg = TREE_VEC_ELT (inner_args, i);
940 if (TREE_CODE (parm) == TYPE_DECL)
941 {
942 /* This parameter is a type. */
943 OB_PUTC ('Z');
944 build_mangled_name_for_type (arg);
945 }
946 else if (TREE_CODE (parm) == TEMPLATE_DECL)
947 {
948 /* This parameter is a template. */
949 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
950 /* Output parameter declaration, argument index and level. */
951 build_mangled_name_for_type (arg);
952 else
953 {
954 /* A TEMPLATE_DECL node, output the parameter declaration
955 and template name */
956
957 OB_PUTC ('z');
958 build_template_template_parm_names
959 (DECL_INNERMOST_TEMPLATE_PARMS (parm));
960 icat (IDENTIFIER_LENGTH (DECL_NAME (arg)));
961 OB_PUTID (DECL_NAME (arg));
962 }
963 }
964 else
965 {
966 parm = tsubst (parm, arglist, /*complain=*/1, NULL_TREE);
967 /* It's a PARM_DECL. */
968 build_mangled_name_for_type (TREE_TYPE (parm));
969 build_overload_value (TREE_TYPE (parm), arg,
970 uses_template_parms (arglist));
971 }
972 }
973 }
974
975 /* Output the representation for NAME, which is either a TYPE_DECL or
976 an IDENTIFIER. */
977
978 static void
979 build_overload_identifier (name)
980 tree name;
981 {
982 if (TREE_CODE (name) == TYPE_DECL
983 && CLASS_TYPE_P (TREE_TYPE (name))
984 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (name))
985 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (TREE_TYPE (name)))
986 || (TREE_CODE (DECL_CONTEXT (CLASSTYPE_TI_TEMPLATE
987 (TREE_TYPE (name))))
988 == FUNCTION_DECL)))
989 {
990 /* NAME is the TYPE_DECL for a template specialization. */
991 tree template, parmlist, arglist, tname;
992 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (name));
993 arglist = CLASSTYPE_TI_ARGS (TREE_TYPE (name));
994 tname = DECL_NAME (template);
995 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
996 OB_PUTC ('t');
997 icat (IDENTIFIER_LENGTH (tname));
998 OB_PUTID (tname);
999 build_template_parm_names (parmlist, arglist);
1000 }
1001 else
1002 {
1003 if (TREE_CODE (name) == TYPE_DECL)
1004 name = DECL_NAME (name);
1005 if (numeric_output_need_bar)
1006 {
1007 OB_PUTC ('_');
1008 numeric_output_need_bar = 0;
1009 }
1010 icat (IDENTIFIER_LENGTH (name));
1011 OB_PUTID (name);
1012 }
1013 }
1014
1015 /* Given DECL, either a class TYPE, TYPE_DECL or FUNCTION_DECL, produce
1016 the mangling for it. Used by build_mangled_name and build_static_name. */
1017
1018 static void
1019 build_qualified_name (decl)
1020 tree decl;
1021 {
1022 tree context;
1023 int i = 1;
1024
1025 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 't')
1026 decl = TYPE_NAME (decl);
1027
1028 /* If DECL_ASSEMBLER_NAME has been set properly, use it. */
1029 if (TREE_CODE (decl) == TYPE_DECL
1030 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl) && !flag_do_squangling)
1031 {
1032 tree id = DECL_ASSEMBLER_NAME (decl);
1033 OB_PUTID (id);
1034 if (ISDIGIT (IDENTIFIER_POINTER (id) [IDENTIFIER_LENGTH (id) - 1]))
1035 numeric_output_need_bar = 1;
1036 return;
1037 }
1038
1039 context = decl;
1040 /* If we can't find a Ktype, do it the hard way. */
1041 if (check_ktype (context, FALSE) == -1)
1042 {
1043 /* Count type and namespace scopes. */
1044 while (1)
1045 {
1046 context = CP_DECL_CONTEXT (context);
1047 if (context == global_namespace)
1048 break;
1049 i += 1;
1050 if (check_ktype (context, FALSE) != -1)
1051 /* Found one! */
1052 break;
1053 if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
1054 context = TYPE_NAME (context);
1055 }
1056 }
1057
1058 if (i > 1)
1059 {
1060 OB_PUTC ('Q');
1061 build_underscore_int (i);
1062 numeric_output_need_bar = 0;
1063 }
1064 build_overload_nested_name (decl);
1065 }
1066
1067 /* Output the mangled representation for TYPE. If EXTRA_GCODE is
1068 non-zero, mangled names for structure/union types are intentionally
1069 mangled differently from the method described in the ARM. */
1070
1071 static void
1072 build_mangled_name_for_type_with_Gcode (type, extra_Gcode)
1073 tree type;
1074 int extra_Gcode;
1075 {
1076 if (TYPE_PTRMEMFUNC_P (type))
1077 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
1078 process_modifiers (type);
1079 process_overload_item (type, extra_Gcode);
1080 }
1081
1082 /* Like build_mangled_name_for_type_with_Gcode, but never outputs the
1083 `G'. */
1084
1085 static void
1086 build_mangled_name_for_type (type)
1087 tree type;
1088 {
1089 build_mangled_name_for_type_with_Gcode (type, 0);
1090 }
1091
1092 /* Given a list of parameters in PARMTYPES, create an unambiguous
1093 overload string. Should distinguish any type that C (or C++) can
1094 distinguish. I.e., pointers to functions are treated correctly.
1095
1096 Caller must deal with whether a final `e' goes on the end or not.
1097
1098 Any default conversions must take place before this function
1099 is called.
1100
1101 BEGIN and END control initialization and finalization of the
1102 obstack where we build the string. */
1103
1104 char *
1105 build_overload_name (parmtypes, begin, end)
1106 tree parmtypes;
1107 int begin, end;
1108 {
1109 char *ret;
1110 start_squangling ();
1111 ret = build_mangled_name (parmtypes, begin, end);
1112 end_squangling ();
1113 return ret ;
1114 }
1115
1116 /* Output the mangled representation for PARMTYPES. If PARMTYPES is a
1117 TREE_LIST, then it is a list of parameter types. Otherwise,
1118 PARMTYPES must be a single type. */
1119
1120 static char *
1121 build_mangled_name (parmtypes, begin, end)
1122 tree parmtypes;
1123 int begin, end;
1124 {
1125 if (begin)
1126 OB_INIT ();
1127
1128 if (TREE_CODE (parmtypes) != TREE_LIST)
1129 /* There is only one type. */
1130 build_mangled_name_for_type (parmtypes);
1131 else
1132 {
1133 /* There are several types in a parameter list. */
1134 int nrepeats = 0;
1135 int old_style_repeats = !flag_do_squangling && !nofold && typevec;
1136 tree last_type = NULL_TREE;
1137
1138 for (; parmtypes && parmtypes != void_list_node;
1139 parmtypes = TREE_CHAIN (parmtypes))
1140 {
1141 /* We used to call canonical_type_variant here, but that isn't
1142 good enough; it doesn't handle pointers to typedef types. So
1143 we can't just set TREE_USED to say we've seen a type already;
1144 we have to check each of the earlier types with same_type_p. */
1145 tree parmtype = TREE_VALUE (parmtypes);
1146
1147 if (old_style_repeats)
1148 {
1149 /* Every argument gets counted. */
1150 my_friendly_assert (maxtype < typevec_size, 387);
1151 typevec[maxtype++] = parmtype;
1152 }
1153
1154 if (last_type && same_type_p (parmtype, last_type))
1155 {
1156 if (flag_do_squangling
1157 || (old_style_repeats
1158 && is_back_referenceable_type (parmtype)))
1159 {
1160 /* The next type is the same as this one. Keep
1161 track of the repetition, and output the repeat
1162 count later. */
1163 nrepeats++;
1164 continue;
1165 }
1166 }
1167 else if (nrepeats != 0)
1168 {
1169 /* Indicate how many times the previous parameter was
1170 repeated. */
1171 if (old_style_repeats)
1172 flush_repeats (nrepeats, last_type);
1173 else
1174 issue_nrepeats (nrepeats, last_type);
1175 nrepeats = 0;
1176 }
1177
1178 last_type = parmtype;
1179
1180 /* Note that for bug-compatibility with 2.7.2, we can't build up
1181 repeats of types other than the most recent one. So we call
1182 flush_repeats every round, if we get this far. */
1183 if (old_style_repeats && flush_repeats (0, parmtype))
1184 continue;
1185
1186 /* Output the PARMTYPE. */
1187 build_mangled_name_for_type_with_Gcode (parmtype, 1);
1188 }
1189
1190 /* Output the repeat count for the last parameter, if
1191 necessary. */
1192 if (nrepeats != 0)
1193 {
1194 if (old_style_repeats)
1195 flush_repeats (nrepeats, last_type);
1196 else
1197 issue_nrepeats (nrepeats, last_type);
1198 nrepeats = 0;
1199 }
1200
1201 if (!parmtypes)
1202 /* The parameter list ends in an ellipsis. */
1203 OB_PUTC ('e');
1204 }
1205
1206 if (end)
1207 OB_FINISH ();
1208 return (char *)obstack_base (&scratch_obstack);
1209 }
1210
1211 /* Emit modifiers such as constant, read-only, and volatile. */
1212
1213 static void
1214 process_modifiers (parmtype)
1215 tree parmtype;
1216 {
1217 /* Note that here we do not use CP_TYPE_CONST_P and friends because
1218 we describe types recursively; we will get the `const' in
1219 `const int ()[10]' when processing the `const int' part. */
1220 if (TYPE_READONLY (parmtype))
1221 OB_PUTC ('C');
1222 if (TREE_CODE (parmtype) == INTEGER_TYPE
1223 && parmtype != char_type_node
1224 && parmtype != wchar_type_node
1225 && (TYPE_MAIN_VARIANT (parmtype)
1226 == unsigned_type (TYPE_MAIN_VARIANT (parmtype)))
1227 && ! TYPE_FOR_JAVA (parmtype))
1228 OB_PUTC ('U');
1229 if (TYPE_VOLATILE (parmtype))
1230 OB_PUTC ('V');
1231 /* It would be better to use `R' for `restrict', but that's already
1232 used for reference types. And `r' is used for `long double'. */
1233 if (TYPE_RESTRICT (parmtype))
1234 OB_PUTC ('u');
1235 }
1236
1237 /* Check to see if TYPE has been entered into the Bcode typelist. If
1238 so, return 1 and emit a backreference to TYPE. Otherwise, add TYPE
1239 to the list of back-referenceable types and return 0. */
1240
1241 static int
1242 check_btype (type)
1243 tree type;
1244 {
1245 int x;
1246
1247 if (btypelist == NULL)
1248 return 0;
1249
1250 if (!is_back_referenceable_type (type))
1251 return 0;
1252
1253 for (x = 0; x < maxbtype; x++)
1254 if (same_type_p (type, btypelist[x]))
1255 {
1256 OB_PUTC ('B');
1257 icat (x);
1258 if (x > 9)
1259 OB_PUTC ('_');
1260 return 1 ;
1261 }
1262
1263 if (maxbsize <= maxbtype)
1264 {
1265 /* Enlarge the table. */
1266 maxbsize = maxbsize * 3 / 2;
1267 btypelist = (tree *)xrealloc (btypelist, sizeof (tree) * maxbsize);
1268 }
1269
1270 /* Register the TYPE. */
1271 btypelist[maxbtype++] = type;
1272
1273 return 0;
1274 }
1275
1276 /* Emit the correct code for various node types. */
1277
1278 static void
1279 process_overload_item (parmtype, extra_Gcode)
1280 tree parmtype;
1281 int extra_Gcode;
1282 {
1283 numeric_output_need_bar = 0;
1284
1285 /* Our caller should have already handed any qualifiers, so pull out the
1286 TYPE_MAIN_VARIANT to avoid typedef confusion. Except we can't do that
1287 for arrays, because they are transparent to qualifiers. Sigh. */
1288 if (TREE_CODE (parmtype) == ARRAY_TYPE)
1289 parmtype = canonical_type_variant (parmtype);
1290 else
1291 parmtype = TYPE_MAIN_VARIANT (parmtype);
1292
1293 /* These tree types are considered modifiers for B code squangling,
1294 and therefore should not get entries in the Btypelist. They are,
1295 however, repeatable types. */
1296
1297 switch (TREE_CODE (parmtype))
1298 {
1299 case REFERENCE_TYPE:
1300 OB_PUTC ('R');
1301 goto more;
1302
1303 case ARRAY_TYPE:
1304 #if PARM_CAN_BE_ARRAY_TYPE
1305 {
1306 OB_PUTC ('A');
1307 if (TYPE_DOMAIN (parmtype) == NULL_TREE)
1308 OB_PUTC ('_');
1309 else
1310 {
1311 tree length = array_type_nelts (parmtype);
1312 if (TREE_CODE (length) != INTEGER_CST || flag_do_squangling)
1313 {
1314 length = fold (build (PLUS_EXPR, TREE_TYPE (length),
1315 length, integer_one_node));
1316 STRIP_NOPS (length);
1317 }
1318 build_overload_value (sizetype, length, 1);
1319 }
1320 if (numeric_output_need_bar && ! flag_do_squangling)
1321 OB_PUTC ('_');
1322 goto more;
1323 }
1324 #else
1325 OB_PUTC ('P');
1326 goto more;
1327 #endif
1328
1329 case POINTER_TYPE:
1330 OB_PUTC ('P');
1331 more:
1332 build_mangled_name_for_type (TREE_TYPE (parmtype));
1333 return;
1334 break;
1335
1336 default:
1337 break;
1338 }
1339
1340 if (flag_do_squangling && check_btype (parmtype))
1341 /* If PARMTYPE is already in the list of back-referenceable types,
1342 then check_btype will output the appropriate reference, and
1343 there's nothing more to do. */
1344 return;
1345
1346 switch (TREE_CODE (parmtype))
1347 {
1348 case OFFSET_TYPE:
1349 OB_PUTC ('O');
1350 build_mangled_name_for_type (TYPE_OFFSET_BASETYPE (parmtype));
1351 OB_PUTC ('_');
1352 build_mangled_name_for_type (TREE_TYPE (parmtype));
1353 break;
1354
1355 case FUNCTION_TYPE:
1356 case METHOD_TYPE:
1357 {
1358 tree parms = TYPE_ARG_TYPES (parmtype);
1359
1360 /* Rather than implementing a reentrant TYPEVEC, we turn off
1361 repeat codes here, unless we're squangling. Squangling
1362 doesn't make use of the TYPEVEC, so there's no reentrancy
1363 problem. */
1364 int old_nofold = nofold;
1365 if (!flag_do_squangling)
1366 nofold = 1;
1367
1368 if (TREE_CODE (parmtype) == METHOD_TYPE)
1369 {
1370 /* Mark this as a method. */
1371 OB_PUTC ('M');
1372 /* Output the class of which this method is a member. */
1373 build_mangled_name_for_type (TYPE_METHOD_BASETYPE (parmtype));
1374 /* Output any qualifiers for the `this' parameter. */
1375 process_modifiers (TREE_TYPE (TREE_VALUE (parms)));
1376 }
1377
1378 /* Output the parameter types. */
1379 OB_PUTC ('F');
1380 if (parms == NULL_TREE)
1381 OB_PUTC ('e');
1382 else if (parms == void_list_node)
1383 OB_PUTC ('v');
1384 else
1385 build_mangled_name (parms, 0, 0);
1386
1387 /* Output the return type. */
1388 OB_PUTC ('_');
1389 build_mangled_name_for_type (TREE_TYPE (parmtype));
1390
1391 nofold = old_nofold;
1392 break;
1393 }
1394
1395 case INTEGER_TYPE:
1396 if (parmtype == integer_type_node
1397 || parmtype == unsigned_type_node
1398 || parmtype == java_int_type_node)
1399 OB_PUTC ('i');
1400 else if (parmtype == long_integer_type_node
1401 || parmtype == long_unsigned_type_node)
1402 OB_PUTC ('l');
1403 else if (parmtype == short_integer_type_node
1404 || parmtype == short_unsigned_type_node
1405 || parmtype == java_short_type_node)
1406 OB_PUTC ('s');
1407 else if (parmtype == signed_char_type_node)
1408 {
1409 OB_PUTC ('S');
1410 OB_PUTC ('c');
1411 }
1412 else if (parmtype == char_type_node
1413 || parmtype == unsigned_char_type_node
1414 || parmtype == java_byte_type_node)
1415 OB_PUTC ('c');
1416 else if (parmtype == wchar_type_node
1417 || parmtype == java_char_type_node)
1418 OB_PUTC ('w');
1419 else if (parmtype == long_long_integer_type_node
1420 || parmtype == long_long_unsigned_type_node
1421 || parmtype == java_long_type_node)
1422 OB_PUTC ('x');
1423 else if (parmtype == java_boolean_type_node)
1424 OB_PUTC ('b');
1425 #if HOST_BITS_PER_WIDE_INT >= 64
1426 else if (parmtype == intTI_type_node
1427 || parmtype == unsigned_intTI_type_node)
1428 {
1429 /* Should just check a flag here instead of specific
1430 *_type_nodes, because all C9x types could use this. */
1431 int bits = TREE_INT_CST_LOW (TYPE_SIZE (parmtype));
1432 build_mangled_C9x_name (bits);
1433 }
1434 #endif
1435 else
1436 my_friendly_abort (73);
1437 break;
1438
1439 case BOOLEAN_TYPE:
1440 OB_PUTC ('b');
1441 break;
1442
1443 case REAL_TYPE:
1444 if (parmtype == long_double_type_node)
1445 OB_PUTC ('r');
1446 else if (parmtype == double_type_node
1447 || parmtype == java_double_type_node)
1448 OB_PUTC ('d');
1449 else if (parmtype == float_type_node
1450 || parmtype == java_float_type_node)
1451 OB_PUTC ('f');
1452 else my_friendly_abort (74);
1453 break;
1454
1455 case COMPLEX_TYPE:
1456 OB_PUTC ('J');
1457 build_mangled_name_for_type (TREE_TYPE (parmtype));
1458 break;
1459
1460 case VOID_TYPE:
1461 OB_PUTC ('v');
1462 break;
1463
1464 case ERROR_MARK: /* not right, but nothing is anyway */
1465 break;
1466
1467 /* have to do these */
1468 case UNION_TYPE:
1469 case RECORD_TYPE:
1470 {
1471 if (extra_Gcode)
1472 OB_PUTC ('G'); /* make it look incompatible with AT&T */
1473 /* drop through into next case */
1474 }
1475 case ENUMERAL_TYPE:
1476 {
1477 tree name = TYPE_NAME (parmtype);
1478
1479 my_friendly_assert (TREE_CODE (name) == TYPE_DECL, 248);
1480
1481 build_qualified_name (name);
1482 break;
1483 }
1484
1485 case UNKNOWN_TYPE:
1486 /* This will take some work. */
1487 OB_PUTC ('?');
1488 break;
1489
1490 case TEMPLATE_TEMPLATE_PARM:
1491 /* Find and output the original template parameter
1492 declaration. */
1493 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parmtype))
1494 {
1495 build_mangled_template_parm_index ("tzX",
1496 TEMPLATE_TYPE_PARM_INDEX
1497 (parmtype));
1498 build_template_parm_names
1499 (DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (parmtype)),
1500 TYPE_TI_ARGS (parmtype));
1501 }
1502 else
1503 {
1504 build_mangled_template_parm_index ("ZzX",
1505 TEMPLATE_TYPE_PARM_INDEX
1506 (parmtype));
1507 build_template_template_parm_names
1508 (DECL_INNERMOST_TEMPLATE_PARMS (TYPE_STUB_DECL (parmtype)));
1509 }
1510 break;
1511
1512 case TEMPLATE_TYPE_PARM:
1513 build_mangled_template_parm_index ("X",
1514 TEMPLATE_TYPE_PARM_INDEX
1515 (parmtype));
1516 break;
1517
1518 case TYPENAME_TYPE:
1519 /* When mangling the type of a function template whose
1520 declaration looks like:
1521
1522 template <class T> void foo(typename T::U)
1523
1524 we have to mangle these. */
1525 build_qualified_name (parmtype);
1526 break;
1527
1528 default:
1529 my_friendly_abort (75);
1530 }
1531
1532 }
1533
1534 /* Produce the mangling for a variable named NAME in CONTEXT, which can
1535 be either a class TYPE or a FUNCTION_DECL. */
1536
1537 tree
1538 build_static_name (context, name)
1539 tree context, name;
1540 {
1541 OB_INIT ();
1542 numeric_output_need_bar = 0;
1543 start_squangling ();
1544 #ifdef JOINER
1545 OB_PUTC ('_');
1546 build_qualified_name (context);
1547 OB_PUTC (JOINER);
1548 #else
1549 OB_PUTS ("__static_");
1550 build_qualified_name (context);
1551 OB_PUTC ('_');
1552 #endif
1553 OB_PUTID (name);
1554 OB_FINISH ();
1555 end_squangling ();
1556
1557 return get_identifier ((char *)obstack_base (&scratch_obstack));
1558 }
1559 \f
1560 /* FOR_METHOD should be 1 if the declaration in question is for a member
1561 of a class (including a static member) and 2 if the declaration is
1562 for a constructor. */
1563 tree
1564 build_decl_overload_real (dname, parms, ret_type, tparms, targs,
1565 for_method)
1566 tree dname;
1567 tree parms;
1568 tree ret_type;
1569 tree tparms;
1570 tree targs;
1571 int for_method;
1572 {
1573 char *name = IDENTIFIER_POINTER (dname);
1574
1575 /* member operators new and delete look like methods at this point. */
1576 if (! for_method && parms != NULL_TREE && TREE_CODE (parms) == TREE_LIST
1577 && TREE_CHAIN (parms) == void_list_node)
1578 {
1579 if (dname == ansi_opname[(int) DELETE_EXPR])
1580 return get_identifier ("__builtin_delete");
1581 else if (dname == ansi_opname[(int) VEC_DELETE_EXPR])
1582 return get_identifier ("__builtin_vec_delete");
1583 if (dname == ansi_opname[(int) NEW_EXPR])
1584 return get_identifier ("__builtin_new");
1585 else if (dname == ansi_opname[(int) VEC_NEW_EXPR])
1586 return get_identifier ("__builtin_vec_new");
1587 }
1588
1589 start_squangling ();
1590 OB_INIT ();
1591 if (for_method != 2)
1592 OB_PUTCP (name);
1593 /* Otherwise, we can divine that this is a constructor,
1594 and figure out its name without any extra encoding. */
1595
1596 OB_PUTC2 ('_', '_');
1597 numeric_output_need_bar = 0;
1598
1599 if (tparms)
1600 {
1601 OB_PUTC ('H');
1602 build_template_parm_names (tparms, targs);
1603 OB_PUTC ('_');
1604 }
1605 else if (!for_method && current_namespace == global_namespace)
1606 /* XXX this works only if we call this in the same namespace
1607 as the declaration. Unfortunately, we don't have the _DECL,
1608 only its name */
1609 OB_PUTC ('F');
1610
1611 if (!for_method && current_namespace != global_namespace)
1612 /* qualify with namespace */
1613 build_qualified_name (current_namespace);
1614
1615 if (parms == NULL_TREE)
1616 OB_PUTC ('e');
1617 else if (parms == void_list_node)
1618 OB_PUTC ('v');
1619 else
1620 {
1621 if (!flag_do_squangling)
1622 {
1623 /* Allocate typevec array. */
1624 maxtype = 0;
1625 typevec_size = list_length (parms);
1626 if (!for_method && current_namespace != global_namespace)
1627 /* The namespace of a global function needs one slot. */
1628 typevec_size++;
1629 typevec = (tree *)alloca (typevec_size * sizeof (tree));
1630 }
1631 nofold = 0;
1632
1633 if (for_method)
1634 {
1635 tree this_type = TREE_VALUE (parms);
1636
1637 if (TREE_CODE (this_type) == RECORD_TYPE) /* a signature pointer */
1638 this_type = SIGNATURE_TYPE (this_type);
1639 else
1640 this_type = TREE_TYPE (this_type);
1641
1642 build_mangled_name_for_type (this_type);
1643
1644 if (!flag_do_squangling)
1645 {
1646 my_friendly_assert (maxtype < typevec_size, 387);
1647 typevec[maxtype++] = this_type;
1648 }
1649
1650 if (TREE_CHAIN (parms))
1651 build_mangled_name (TREE_CHAIN (parms), 0, 0);
1652 else
1653 OB_PUTC ('e');
1654 }
1655 else
1656 {
1657 /* the namespace qualifier for a global function
1658 will count as type */
1659 if (current_namespace != global_namespace
1660 && !flag_do_squangling)
1661 {
1662 my_friendly_assert (maxtype < typevec_size, 387);
1663 typevec[maxtype++] = current_namespace;
1664 }
1665 build_mangled_name (parms, 0, 0);
1666 }
1667
1668 if (!flag_do_squangling)
1669 /* Deallocate typevec array. */
1670 typevec = NULL;
1671 }
1672
1673 if (ret_type != NULL_TREE && for_method != 2)
1674 {
1675 /* Add the return type. */
1676 OB_PUTC ('_');
1677 build_mangled_name_for_type (ret_type);
1678 }
1679
1680 OB_FINISH ();
1681 end_squangling ();
1682 {
1683 tree n = get_identifier (obstack_base (&scratch_obstack));
1684 if (IDENTIFIER_OPNAME_P (dname))
1685 IDENTIFIER_OPNAME_P (n) = 1;
1686 return n;
1687 }
1688 }
1689
1690 /* Change the name of a function definition so that it may be
1691 overloaded. NAME is the name of the function to overload,
1692 PARMS is the parameter list (which determines what name the
1693 final function obtains).
1694
1695 FOR_METHOD is 1 if this overload is being performed
1696 for a method, rather than a function type. It is 2 if
1697 this overload is being performed for a constructor. */
1698
1699 tree
1700 build_decl_overload (dname, parms, for_method)
1701 tree dname;
1702 tree parms;
1703 int for_method;
1704 {
1705 return build_decl_overload_real (dname, parms, NULL_TREE, NULL_TREE,
1706 NULL_TREE, for_method);
1707 }
1708
1709 /* Set the mangled name (DECL_ASSEMBLER_NAME) for DECL. */
1710
1711 void
1712 set_mangled_name_for_decl (decl)
1713 tree decl;
1714 {
1715 tree parm_types;
1716
1717 if (processing_template_decl)
1718 /* There's no need to mangle the name of a template function. */
1719 return;
1720
1721 parm_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1722
1723 if (DECL_STATIC_FUNCTION_P (decl))
1724 parm_types =
1725 hash_tree_chain (build_pointer_type (DECL_CLASS_CONTEXT (decl)),
1726 parm_types);
1727 else
1728 /* The only member functions whose type is a FUNCTION_TYPE, rather
1729 than a METHOD_TYPE, should be static members. */
1730 my_friendly_assert (!DECL_CONTEXT (decl)
1731 || !IS_AGGR_TYPE_CODE (TREE_CODE (DECL_CONTEXT (decl)))
1732 || TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE,
1733 0);
1734
1735 DECL_ASSEMBLER_NAME (decl)
1736 = build_decl_overload (DECL_NAME (decl), parm_types,
1737 DECL_FUNCTION_MEMBER_P (decl)
1738 + DECL_CONSTRUCTOR_P (decl));
1739 }
1740
1741 /* Build an overload name for the type expression TYPE. */
1742
1743 tree
1744 build_typename_overload (type)
1745 tree type;
1746 {
1747 tree id;
1748
1749 OB_INIT ();
1750 OB_PUTID (ansi_opname[(int) TYPE_EXPR]);
1751 nofold = 1;
1752 start_squangling ();
1753 build_mangled_name (type, 0, 1);
1754 id = get_identifier (obstack_base (&scratch_obstack));
1755 IDENTIFIER_OPNAME_P (id) = 1;
1756 #if 0
1757 IDENTIFIER_GLOBAL_VALUE (id) = TYPE_MAIN_DECL (type);
1758 #endif
1759 TREE_TYPE (id) = type;
1760 end_squangling ();
1761 return id;
1762 }
1763
1764 tree
1765 build_overload_with_type (name, type)
1766 tree name, type;
1767 {
1768 OB_INIT ();
1769 OB_PUTID (name);
1770 nofold = 1;
1771
1772 start_squangling ();
1773 build_mangled_name (type, 0, 1);
1774 end_squangling ();
1775 return get_identifier (obstack_base (&scratch_obstack));
1776 }
1777
1778 tree
1779 get_id_2 (name, name2)
1780 char *name;
1781 tree name2;
1782 {
1783 OB_INIT ();
1784 OB_PUTCP (name);
1785 OB_PUTID (name2);
1786 OB_FINISH ();
1787 return get_identifier (obstack_base (&scratch_obstack));
1788 }
1789
1790 /* Returns a DECL_ASSEMBLER_NAME for the destructor of type TYPE. */
1791
1792 tree
1793 build_destructor_name (type)
1794 tree type;
1795 {
1796 return build_overload_with_type (get_identifier (DESTRUCTOR_DECL_PREFIX),
1797 type);
1798 }
1799 \f
1800 /* Given a tree_code CODE, and some arguments (at least one),
1801 attempt to use an overloaded operator on the arguments.
1802
1803 For unary operators, only the first argument need be checked.
1804 For binary operators, both arguments may need to be checked.
1805
1806 Member functions can convert class references to class pointers,
1807 for one-level deep indirection. More than that is not supported.
1808 Operators [](), ()(), and ->() must be member functions.
1809
1810 We call function call building calls with LOOKUP_COMPLAIN if they
1811 are our only hope. This is true when we see a vanilla operator
1812 applied to something of aggregate type. If this fails, we are free
1813 to return `error_mark_node', because we will have reported the
1814 error.
1815
1816 Operators NEW and DELETE overload in funny ways: operator new takes
1817 a single `size' parameter, and operator delete takes a pointer to the
1818 storage being deleted. When overloading these operators, success is
1819 assumed. If there is a failure, report an error message and return
1820 `error_mark_node'. */
1821
1822 /* NOSTRICT */
1823 tree
1824 build_opfncall (code, flags, xarg1, xarg2, arg3)
1825 enum tree_code code;
1826 int flags;
1827 tree xarg1, xarg2, arg3;
1828 {
1829 return build_new_op (code, flags, xarg1, xarg2, arg3);
1830 }
1831 \f
1832 /* This function takes an identifier, ID, and attempts to figure out what
1833 it means. There are a number of possible scenarios, presented in increasing
1834 order of hair:
1835
1836 1) not in a class's scope
1837 2) in class's scope, member name of the class's method
1838 3) in class's scope, but not a member name of the class
1839 4) in class's scope, member name of a class's variable
1840
1841 NAME is $1 from the bison rule. It is an IDENTIFIER_NODE.
1842 VALUE is $$ from the bison rule. It is the value returned by lookup_name ($1)
1843
1844 As a last ditch, try to look up the name as a label and return that
1845 address.
1846
1847 Values which are declared as being of REFERENCE_TYPE are
1848 automatically dereferenced here (as a hack to make the
1849 compiler faster). */
1850
1851 tree
1852 hack_identifier (value, name)
1853 tree value, name;
1854 {
1855 tree type;
1856
1857 if (value == error_mark_node)
1858 {
1859 if (current_class_name)
1860 {
1861 tree fields = lookup_fnfields (TYPE_BINFO (current_class_type), name, 1);
1862 if (fields == error_mark_node)
1863 return error_mark_node;
1864 if (fields)
1865 {
1866 tree fndecl;
1867
1868 fndecl = TREE_VALUE (fields);
1869 my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 251);
1870 /* I could not trigger this code. MvL */
1871 my_friendly_abort (980325);
1872 #ifdef DEAD
1873 if (DECL_CHAIN (fndecl) == NULL_TREE)
1874 {
1875 warning ("methods cannot be converted to function pointers");
1876 return fndecl;
1877 }
1878 else
1879 {
1880 error ("ambiguous request for method pointer `%s'",
1881 IDENTIFIER_POINTER (name));
1882 return error_mark_node;
1883 }
1884 #endif
1885 }
1886 }
1887 if (flag_labels_ok && IDENTIFIER_LABEL_VALUE (name))
1888 {
1889 return IDENTIFIER_LABEL_VALUE (name);
1890 }
1891 return error_mark_node;
1892 }
1893
1894 type = TREE_TYPE (value);
1895 if (TREE_CODE (value) == FIELD_DECL)
1896 {
1897 if (current_class_ptr == NULL_TREE)
1898 {
1899 if (current_function_decl
1900 && DECL_STATIC_FUNCTION_P (current_function_decl))
1901 cp_error ("invalid use of member `%D' in static member function",
1902 value);
1903 else
1904 /* We can get here when processing a bad default
1905 argument, like:
1906 struct S { int a; void f(int i = a); } */
1907 cp_error ("invalid use of member `%D'", value);
1908
1909 return error_mark_node;
1910 }
1911 TREE_USED (current_class_ptr) = 1;
1912
1913 /* Mark so that if we are in a constructor, and then find that
1914 this field was initialized by a base initializer,
1915 we can emit an error message. */
1916 TREE_USED (value) = 1;
1917 value = build_component_ref (current_class_ref, name, NULL_TREE, 1);
1918 }
1919 else if (TREE_CODE (value) == FUNCTION_DECL
1920 && DECL_FUNCTION_MEMBER_P (value))
1921 {
1922 tree decl;
1923
1924 if (IS_SIGNATURE (DECL_CLASS_CONTEXT (value)))
1925 return value;
1926
1927 decl = maybe_dummy_object (DECL_CLASS_CONTEXT (value), 0);
1928 value = build_component_ref (decl, name, NULL_TREE, 1);
1929 }
1930 else if (really_overloaded_fn (value))
1931 {
1932 #if 0
1933 tree t = get_first_fn (value);
1934 for (; t; t = DECL_CHAIN (t))
1935 {
1936 if (TREE_CODE (t) == TEMPLATE_DECL)
1937 continue;
1938
1939 assemble_external (t);
1940 TREE_USED (t) = 1;
1941 }
1942 #endif
1943 }
1944 else if (TREE_CODE (value) == OVERLOAD)
1945 /* not really overloaded function */
1946 mark_used (OVL_FUNCTION (value));
1947 else if (TREE_CODE (value) == TREE_LIST)
1948 {
1949 /* Ambiguous reference to base members, possibly other cases?. */
1950 tree t = value;
1951 while (t && TREE_CODE (t) == TREE_LIST)
1952 {
1953 mark_used (TREE_VALUE (t));
1954 t = TREE_CHAIN (t);
1955 }
1956 }
1957 else if (TREE_CODE (value) == NAMESPACE_DECL)
1958 {
1959 cp_error ("use of namespace `%D' as expression", value);
1960 return error_mark_node;
1961 }
1962 else if (DECL_CLASS_TEMPLATE_P (value))
1963 {
1964 cp_error ("use of class template `%T' as expression", value);
1965 return error_mark_node;
1966 }
1967 else
1968 mark_used (value);
1969
1970 if (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == PARM_DECL
1971 || TREE_CODE (value) == RESULT_DECL)
1972 {
1973 tree context = decl_function_context (value);
1974 if (context != NULL_TREE && context != current_function_decl
1975 && ! TREE_STATIC (value))
1976 {
1977 cp_error ("use of %s from containing function",
1978 (TREE_CODE (value) == VAR_DECL
1979 ? "`auto' variable" : "parameter"));
1980 cp_error_at (" `%#D' declared here", value);
1981 value = error_mark_node;
1982 }
1983 }
1984
1985 if (TREE_CODE_CLASS (TREE_CODE (value)) == 'd' && DECL_NONLOCAL (value))
1986 {
1987 if (DECL_LANG_SPECIFIC (value)
1988 && DECL_CLASS_CONTEXT (value) != current_class_type)
1989 {
1990 tree path;
1991 register tree context
1992 = (TREE_CODE (value) == FUNCTION_DECL && DECL_VIRTUAL_P (value))
1993 ? DECL_CLASS_CONTEXT (value)
1994 : DECL_CONTEXT (value);
1995
1996 get_base_distance (context, current_class_type, 0, &path);
1997 if (path && !enforce_access (current_class_type, value))
1998 return error_mark_node;
1999 }
2000 }
2001 else if (TREE_CODE (value) == TREE_LIST && TREE_NONLOCAL_FLAG (value))
2002 {
2003 error ("request for member `%s' is ambiguous in multiple inheritance lattice",
2004 IDENTIFIER_POINTER (name));
2005 print_candidates (value);
2006 return error_mark_node;
2007 }
2008
2009 if (! processing_template_decl)
2010 value = convert_from_reference (value);
2011 return value;
2012 }
2013
2014 \f
2015 tree
2016 make_thunk (function, delta)
2017 tree function;
2018 int delta;
2019 {
2020 tree thunk_id;
2021 tree thunk;
2022 tree func_decl;
2023
2024 if (TREE_CODE (function) != ADDR_EXPR)
2025 abort ();
2026 func_decl = TREE_OPERAND (function, 0);
2027 if (TREE_CODE (func_decl) != FUNCTION_DECL)
2028 abort ();
2029
2030 OB_INIT ();
2031 OB_PUTS ("__thunk_");
2032 if (delta > 0)
2033 {
2034 OB_PUTC ('n');
2035 icat (delta);
2036 }
2037 else
2038 icat (-delta);
2039 OB_PUTC ('_');
2040 OB_PUTID (DECL_ASSEMBLER_NAME (func_decl));
2041 OB_FINISH ();
2042 thunk_id = get_identifier (obstack_base (&scratch_obstack));
2043
2044 thunk = IDENTIFIER_GLOBAL_VALUE (thunk_id);
2045 if (thunk && TREE_CODE (thunk) != THUNK_DECL)
2046 {
2047 cp_error ("implementation-reserved name `%D' used", thunk_id);
2048 thunk = NULL_TREE;
2049 SET_IDENTIFIER_GLOBAL_VALUE (thunk_id, thunk);
2050 }
2051 if (thunk == NULL_TREE)
2052 {
2053 thunk = build_decl (FUNCTION_DECL, thunk_id, TREE_TYPE (func_decl));
2054 TREE_READONLY (thunk) = TREE_READONLY (func_decl);
2055 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (func_decl);
2056 comdat_linkage (thunk);
2057 TREE_SET_CODE (thunk, THUNK_DECL);
2058 DECL_INITIAL (thunk) = function;
2059 THUNK_DELTA (thunk) = delta;
2060 DECL_EXTERNAL (thunk) = 1;
2061 DECL_ARTIFICIAL (thunk) = 1;
2062 /* So that finish_file can write out any thunks that need to be: */
2063 pushdecl_top_level (thunk);
2064 }
2065 return thunk;
2066 }
2067
2068 /* Emit the definition of a C++ multiple inheritance vtable thunk. */
2069
2070 void
2071 emit_thunk (thunk_fndecl)
2072 tree thunk_fndecl;
2073 {
2074 tree function = TREE_OPERAND (DECL_INITIAL (thunk_fndecl), 0);
2075 int delta = THUNK_DELTA (thunk_fndecl);
2076
2077 if (TREE_ASM_WRITTEN (thunk_fndecl))
2078 return;
2079
2080 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
2081
2082 TREE_ADDRESSABLE (function) = 1;
2083 mark_used (function);
2084
2085 if (current_function_decl)
2086 abort ();
2087
2088 TREE_SET_CODE (thunk_fndecl, FUNCTION_DECL);
2089
2090 {
2091 #ifdef ASM_OUTPUT_MI_THUNK
2092 char *fnname;
2093 current_function_decl = thunk_fndecl;
2094 /* Make sure we build up its RTL before we go onto the
2095 temporary obstack. */
2096 make_function_rtl (thunk_fndecl);
2097 temporary_allocation ();
2098 DECL_RESULT (thunk_fndecl)
2099 = build_decl (RESULT_DECL, 0, integer_type_node);
2100 fnname = XSTR (XEXP (DECL_RTL (thunk_fndecl), 0), 0);
2101 init_function_start (thunk_fndecl, input_filename, lineno);
2102 current_function_is_thunk = 1;
2103 assemble_start_function (thunk_fndecl, fnname);
2104 ASM_OUTPUT_MI_THUNK (asm_out_file, thunk_fndecl, delta, function);
2105 assemble_end_function (thunk_fndecl, fnname);
2106 permanent_allocation (1);
2107 current_function_decl = 0;
2108 #else /* ASM_OUTPUT_MI_THUNK */
2109 /* If we don't have the necessary macro for efficient thunks, generate a
2110 thunk function that just makes a call to the real function.
2111 Unfortunately, this doesn't work for varargs. */
2112
2113 tree a, t;
2114
2115 if (varargs_function_p (function))
2116 cp_error ("generic thunk code fails for method `%#D' which uses `...'",
2117 function);
2118
2119 /* Set up clone argument trees for the thunk. */
2120 t = NULL_TREE;
2121 for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
2122 {
2123 tree x = copy_node (a);
2124 TREE_CHAIN (x) = t;
2125 DECL_CONTEXT (x) = thunk_fndecl;
2126 t = x;
2127 }
2128 a = nreverse (t);
2129 DECL_ARGUMENTS (thunk_fndecl) = a;
2130 DECL_RESULT (thunk_fndecl) = NULL_TREE;
2131 DECL_LANG_SPECIFIC (thunk_fndecl) = DECL_LANG_SPECIFIC (function);
2132 copy_lang_decl (thunk_fndecl);
2133 DECL_INTERFACE_KNOWN (thunk_fndecl) = 1;
2134 DECL_NOT_REALLY_EXTERN (thunk_fndecl) = 1;
2135
2136 start_function (NULL_TREE, thunk_fndecl, NULL_TREE, 1);
2137 store_parm_decls ();
2138 current_function_is_thunk = 1;
2139
2140 /* Build up the call to the real function. */
2141 t = build_int_2 (delta, -1 * (delta < 0));
2142 TREE_TYPE (t) = signed_type (sizetype);
2143 t = fold (build (PLUS_EXPR, TREE_TYPE (a), a, t));
2144 t = expr_tree_cons (NULL_TREE, t, NULL_TREE);
2145 for (a = TREE_CHAIN (a); a; a = TREE_CHAIN (a))
2146 t = expr_tree_cons (NULL_TREE, a, t);
2147 t = nreverse (t);
2148 t = build_call (function, TREE_TYPE (TREE_TYPE (function)), t);
2149 c_expand_return (t);
2150
2151 finish_function (lineno, 0, 0);
2152
2153 /* Don't let the backend defer this function. */
2154 if (DECL_DEFER_OUTPUT (thunk_fndecl))
2155 {
2156 output_inline_function (thunk_fndecl);
2157 permanent_allocation (1);
2158 }
2159 #endif /* ASM_OUTPUT_MI_THUNK */
2160 }
2161
2162 TREE_SET_CODE (thunk_fndecl, THUNK_DECL);
2163 }
2164 \f
2165 /* Code for synthesizing methods which have default semantics defined. */
2166
2167 /* For the anonymous union in TYPE, return the member that is at least as
2168 large as the rest of the members, so we can copy it. */
2169
2170 static tree
2171 largest_union_member (type)
2172 tree type;
2173 {
2174 tree f, type_size = TYPE_SIZE (type);
2175
2176 for (f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
2177 if (simple_cst_equal (DECL_SIZE (f), type_size) == 1)
2178 return f;
2179
2180 /* We should always find one. */
2181 my_friendly_abort (323);
2182 return NULL_TREE;
2183 }
2184
2185 /* Generate code for default X(X&) constructor. */
2186
2187 static void
2188 do_build_copy_constructor (fndecl)
2189 tree fndecl;
2190 {
2191 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
2192 tree t;
2193
2194 clear_last_expr ();
2195 push_momentary ();
2196
2197 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
2198 parm = TREE_CHAIN (parm);
2199 parm = convert_from_reference (parm);
2200
2201 if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type)
2202 && is_empty_class (current_class_type))
2203 /* Don't copy the padding byte; it might not have been allocated
2204 if *this is a base subobject. */;
2205 else if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
2206 {
2207 t = build (INIT_EXPR, void_type_node, current_class_ref, parm);
2208 TREE_SIDE_EFFECTS (t) = 1;
2209 cplus_expand_expr_stmt (t);
2210 }
2211 else
2212 {
2213 tree fields = TYPE_FIELDS (current_class_type);
2214 int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
2215 tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
2216 int i;
2217
2218 for (t = CLASSTYPE_VBASECLASSES (current_class_type); t;
2219 t = TREE_CHAIN (t))
2220 {
2221 tree basetype = BINFO_TYPE (t);
2222 tree p = convert_to_reference
2223 (build_reference_type (basetype), parm,
2224 CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
2225 p = convert_from_reference (p);
2226
2227 if (p == error_mark_node)
2228 cp_error ("in default copy constructor");
2229 else
2230 current_base_init_list = tree_cons (basetype,
2231 p, current_base_init_list);
2232 }
2233
2234 for (i = 0; i < n_bases; ++i)
2235 {
2236 tree p, basetype = TREE_VEC_ELT (binfos, i);
2237 if (TREE_VIA_VIRTUAL (basetype))
2238 continue;
2239
2240 basetype = BINFO_TYPE (basetype);
2241 p = convert_to_reference
2242 (build_reference_type (basetype), parm,
2243 CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
2244
2245 if (p == error_mark_node)
2246 cp_error ("in default copy constructor");
2247 else
2248 {
2249 p = convert_from_reference (p);
2250 current_base_init_list = tree_cons (basetype,
2251 p, current_base_init_list);
2252 }
2253 }
2254 for (; fields; fields = TREE_CHAIN (fields))
2255 {
2256 tree init, t;
2257 tree field = fields;
2258
2259 if (TREE_CODE (field) != FIELD_DECL)
2260 continue;
2261
2262 init = parm;
2263 if (DECL_NAME (field))
2264 {
2265 if (VFIELD_NAME_P (DECL_NAME (field)))
2266 continue;
2267 if (VBASE_NAME_P (DECL_NAME (field)))
2268 continue;
2269
2270 /* True for duplicate members. */
2271 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
2272 continue;
2273 }
2274 else if ((t = TREE_TYPE (field)) != NULL_TREE
2275 && ANON_UNION_TYPE_P (t)
2276 && TYPE_FIELDS (t) != NULL_TREE)
2277 {
2278 do
2279 {
2280 init = build (COMPONENT_REF, t, init, field);
2281 field = largest_union_member (t);
2282 }
2283 while ((t = TREE_TYPE (field)) != NULL_TREE
2284 && ANON_UNION_TYPE_P (t)
2285 && TYPE_FIELDS (t) != NULL_TREE);
2286 }
2287 else
2288 continue;
2289
2290 init = build (COMPONENT_REF, TREE_TYPE (field), init, field);
2291 init = build_tree_list (NULL_TREE, init);
2292
2293 current_member_init_list
2294 = tree_cons (DECL_NAME (field), init, current_member_init_list);
2295 }
2296 current_member_init_list = nreverse (current_member_init_list);
2297 current_base_init_list = nreverse (current_base_init_list);
2298 setup_vtbl_ptr ();
2299 }
2300
2301 pop_momentary ();
2302 }
2303
2304 static void
2305 do_build_assign_ref (fndecl)
2306 tree fndecl;
2307 {
2308 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
2309
2310 clear_last_expr ();
2311 push_momentary ();
2312
2313 parm = convert_from_reference (parm);
2314
2315 if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type)
2316 && is_empty_class (current_class_type))
2317 /* Don't copy the padding byte; it might not have been allocated
2318 if *this is a base subobject. */;
2319 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
2320 {
2321 tree t = build (MODIFY_EXPR, void_type_node, current_class_ref, parm);
2322 TREE_SIDE_EFFECTS (t) = 1;
2323 cplus_expand_expr_stmt (t);
2324 }
2325 else
2326 {
2327 tree fields = TYPE_FIELDS (current_class_type);
2328 int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
2329 tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
2330 int i;
2331
2332 for (i = 0; i < n_bases; ++i)
2333 {
2334 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2335 tree p = convert_to_reference
2336 (build_reference_type (basetype), parm,
2337 CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
2338 p = convert_from_reference (p);
2339 p = build_member_call (basetype, ansi_opname [MODIFY_EXPR],
2340 build_expr_list (NULL_TREE, p));
2341 expand_expr_stmt (p);
2342 }
2343 for (; fields; fields = TREE_CHAIN (fields))
2344 {
2345 tree comp, init, t;
2346 tree field = fields;
2347
2348 if (TREE_CODE (field) != FIELD_DECL)
2349 continue;
2350
2351 if (TREE_READONLY (field))
2352 {
2353 if (DECL_NAME (field))
2354 cp_error ("non-static const member `%#D', can't use default assignment operator", field);
2355 else
2356 cp_error ("non-static const member in type `%T', can't use default assignment operator", current_class_type);
2357 continue;
2358 }
2359 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
2360 {
2361 if (DECL_NAME (field))
2362 cp_error ("non-static reference member `%#D', can't use default assignment operator", field);
2363 else
2364 cp_error ("non-static reference member in type `%T', can't use default assignment operator", current_class_type);
2365 continue;
2366 }
2367
2368 comp = current_class_ref;
2369 init = parm;
2370
2371 if (DECL_NAME (field))
2372 {
2373 if (VFIELD_NAME_P (DECL_NAME (field)))
2374 continue;
2375 if (VBASE_NAME_P (DECL_NAME (field)))
2376 continue;
2377
2378 /* True for duplicate members. */
2379 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
2380 continue;
2381 }
2382 else if ((t = TREE_TYPE (field)) != NULL_TREE
2383 && ANON_UNION_TYPE_P (t)
2384 && TYPE_FIELDS (t) != NULL_TREE)
2385 {
2386 do
2387 {
2388 comp = build (COMPONENT_REF, t, comp, field);
2389 init = build (COMPONENT_REF, t, init, field);
2390 field = largest_union_member (t);
2391 }
2392 while ((t = TREE_TYPE (field)) != NULL_TREE
2393 && ANON_UNION_TYPE_P (t)
2394 && TYPE_FIELDS (t) != NULL_TREE);
2395 }
2396 else
2397 continue;
2398
2399 comp = build (COMPONENT_REF, TREE_TYPE (field), comp, field);
2400 init = build (COMPONENT_REF, TREE_TYPE (field), init, field);
2401
2402 expand_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
2403 }
2404 }
2405 c_expand_return (current_class_ref);
2406 pop_momentary ();
2407 }
2408
2409 void
2410 synthesize_method (fndecl)
2411 tree fndecl;
2412 {
2413 int nested = (current_function_decl != NULL_TREE);
2414 tree context = hack_decl_function_context (fndecl);
2415
2416 if (at_eof)
2417 import_export_decl (fndecl);
2418
2419 if (! context)
2420 push_to_top_level ();
2421 else if (nested)
2422 push_cp_function_context (context);
2423
2424 interface_unknown = 1;
2425 start_function (NULL_TREE, fndecl, NULL_TREE, 1);
2426 store_parm_decls ();
2427
2428 if (DECL_NAME (fndecl) == ansi_opname[MODIFY_EXPR])
2429 do_build_assign_ref (fndecl);
2430 else if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2431 ;
2432 else
2433 {
2434 tree arg_chain = FUNCTION_ARG_CHAIN (fndecl);
2435 if (DECL_CONSTRUCTOR_FOR_VBASE_P (fndecl))
2436 arg_chain = TREE_CHAIN (arg_chain);
2437 if (arg_chain != void_list_node)
2438 do_build_copy_constructor (fndecl);
2439 else if (TYPE_NEEDS_CONSTRUCTING (current_class_type))
2440 setup_vtbl_ptr ();
2441 }
2442
2443 finish_function (lineno, 0, nested);
2444
2445 extract_interface_info ();
2446 if (! context)
2447 pop_from_top_level ();
2448 else if (nested)
2449 pop_cp_function_context (context);
2450 }