* decl.c (push_overloaded_decl_1): Use pushdecl.
[official-gcc.git] / gcc / cp / method.c
blob805cf15d797ce473637650f898013aff4b886dff
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)
6 This file is part of GNU CC.
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.
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.
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. */
24 /* Handle method declarations. */
25 #include "config.h"
26 #include "system.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "obstack.h"
30 #include "rtl.h"
31 #include "expr.h"
32 #include "output.h"
33 #include "hard-reg-set.h"
34 #include "flags.h"
35 #include "toplev.h"
36 #include "ggc.h"
37 #include "tm_p.h"
39 /* Various flags to control the mangling process. */
41 enum mangling_flags
43 /* No flags. */
44 mf_none = 0,
45 /* The thing we are presently mangling is part of a template type,
46 rather than a fully instantiated type. Therefore, we may see
47 complex expressions where we would normally expect to see a
48 simple integer constant. */
49 mf_maybe_uninstantiated = 1,
50 /* When mangling a numeric value, use the form `_XX_' (instead of
51 just `XX') if the value has more than one digit. */
52 mf_use_underscores_around_value = 2,
55 typedef enum mangling_flags mangling_flags;
57 /* TREE_LIST of the current inline functions that need to be
58 processed. */
59 struct pending_inline *pending_inlines;
61 #define obstack_chunk_alloc xmalloc
62 #define obstack_chunk_free free
64 /* Obstack where we build text strings for overloading, etc. */
65 static struct obstack scratch_obstack;
66 static char *scratch_firstobj;
68 static void icat PROTO((HOST_WIDE_INT));
69 static void dicat PROTO((HOST_WIDE_INT, HOST_WIDE_INT));
70 static int old_backref_index PROTO((tree));
71 static int flush_repeats PROTO((int, tree));
72 static void build_overload_identifier PROTO((tree));
73 static void build_overload_nested_name PROTO((tree));
74 static void mangle_expression PROTO((tree));
75 static void build_overload_int PROTO((tree, mangling_flags));
76 static void build_overload_identifier PROTO((tree));
77 static void build_qualified_name PROTO((tree));
78 static void build_overload_value PROTO((tree, tree, mangling_flags));
79 static void issue_nrepeats PROTO((int, tree));
80 static char *build_mangled_name PROTO((tree,int,int));
81 static void process_modifiers PROTO((tree));
82 static void process_overload_item PROTO((tree,int));
83 static void do_build_assign_ref PROTO((tree));
84 static void do_build_copy_constructor PROTO((tree));
85 static void build_template_template_parm_names PROTO((tree));
86 static void build_template_parm_names PROTO((tree, tree));
87 static void build_underscore_int PROTO((int));
88 static void start_squangling PROTO((void));
89 static void end_squangling PROTO((void));
90 static int check_ktype PROTO((tree, int));
91 static int issue_ktype PROTO((tree));
92 static void build_overload_scope_ref PROTO((tree));
93 static void build_mangled_template_parm_index PROTO((const char *, tree));
94 #if HOST_BITS_PER_WIDE_INT >= 64
95 static void build_mangled_C9x_name PROTO((int));
96 #endif
97 static int is_back_referenceable_type PROTO((tree));
98 static int check_btype PROTO((tree));
99 static void build_mangled_name_for_type PROTO((tree));
100 static void build_mangled_name_for_type_with_Gcode PROTO((tree, int));
102 # define OB_INIT() (scratch_firstobj ? (obstack_free (&scratch_obstack, scratch_firstobj), 0) : 0)
103 # define OB_PUTC(C) (obstack_1grow (&scratch_obstack, (C)))
104 # define OB_PUTC2(C1,C2) \
105 (obstack_1grow (&scratch_obstack, (C1)), obstack_1grow (&scratch_obstack, (C2)))
106 # define OB_PUTS(S) (obstack_grow (&scratch_obstack, (S), sizeof (S) - 1))
107 # define OB_PUTID(ID) \
108 (obstack_grow (&scratch_obstack, IDENTIFIER_POINTER (ID), \
109 IDENTIFIER_LENGTH (ID)))
110 # define OB_PUTCP(S) (obstack_grow (&scratch_obstack, (S), strlen (S)))
111 # define OB_FINISH() (obstack_1grow (&scratch_obstack, '\0'))
112 # define OB_LAST() (obstack_next_free (&scratch_obstack)[-1])
114 /* type tables for K and B type compression */
115 static varray_type btypelist;
116 static varray_type ktypelist;
118 /* number of each type seen */
119 static size_t maxbtype;
120 static size_t maxktype;
122 /* Array of types seen so far in top-level call to `build_mangled_name'.
123 Allocated and deallocated by caller. */
124 static varray_type typevec;
126 /* Number of types interned by `build_mangled_name' so far. */
127 static size_t maxtype;
129 /* Called once to initialize method.c. */
131 void
132 init_method ()
134 gcc_obstack_init (&scratch_obstack);
135 scratch_firstobj = (char *)obstack_alloc (&scratch_obstack, 0);
136 ggc_add_tree_varray_root (&btypelist, 1);
137 ggc_add_tree_varray_root (&ktypelist, 1);
138 ggc_add_tree_varray_root (&typevec, 1);
141 /* This must be large enough to hold any printed integer or floating-point
142 value. */
143 static char digit_buffer[128];
145 /* Move inline function definitions out of structure so that they
146 can be processed normally. CNAME is the name of the class
147 we are working from, METHOD_LIST is the list of method lists
148 of the structure. We delete friend methods here, after
149 saving away their inline function definitions (if any). */
151 void
152 do_inline_function_hair (type, friend_list)
153 tree type, friend_list;
155 tree method = TYPE_METHODS (type);
157 if (method && TREE_CODE (method) == TREE_VEC)
159 if (TREE_VEC_ELT (method, 1))
160 method = TREE_VEC_ELT (method, 1);
161 else if (TREE_VEC_ELT (method, 0))
162 method = TREE_VEC_ELT (method, 0);
163 else
164 method = TREE_VEC_ELT (method, 2);
167 while (method)
169 /* Do inline member functions. */
170 struct pending_inline *info = DECL_PENDING_INLINE_INFO (method);
171 if (info)
173 tree args;
175 my_friendly_assert (info->fndecl == method, 238);
176 args = DECL_ARGUMENTS (method);
177 while (args)
179 DECL_CONTEXT (args) = method;
180 args = TREE_CHAIN (args);
183 method = TREE_CHAIN (method);
185 while (friend_list)
187 tree fndecl = TREE_VALUE (friend_list);
188 struct pending_inline *info = DECL_PENDING_INLINE_INFO (fndecl);
189 if (info)
191 tree args;
193 my_friendly_assert (info->fndecl == fndecl, 239);
194 args = DECL_ARGUMENTS (fndecl);
195 while (args)
197 DECL_CONTEXT (args) = fndecl;
198 args = TREE_CHAIN (args);
202 friend_list = TREE_CHAIN (friend_list);
206 /* Here is where overload code starts. */
208 /* Nonzero if we should not try folding parameter types. */
209 static int nofold;
211 /* Nonzero if an underscore is required before adding a digit to the
212 mangled name currently being built. */
213 static int numeric_output_need_bar;
215 static inline void
216 start_squangling ()
218 if (flag_do_squangling)
220 nofold = 0;
221 maxbtype = 0;
222 maxktype = 0;
223 VARRAY_TREE_INIT (btypelist, 50, "btypelist");
224 VARRAY_TREE_INIT (ktypelist, 50, "ktypelist");
228 static inline void
229 end_squangling ()
231 if (flag_do_squangling)
233 VARRAY_FREE (ktypelist);
234 VARRAY_FREE (btypelist);
235 maxbtype = 0;
236 maxktype = 0;
240 /* Code to concatenate an asciified integer to a string. */
242 static inline void
243 icat (i)
244 HOST_WIDE_INT i;
246 unsigned HOST_WIDE_INT ui;
248 /* Handle this case first, to go really quickly. For many common values,
249 the result of ui/10 below is 1. */
250 if (i == 1)
252 OB_PUTC ('1');
253 return;
256 if (i >= 0)
257 ui = i;
258 else
260 OB_PUTC ('m');
261 ui = -i;
264 if (ui >= 10)
265 icat (ui / 10);
267 OB_PUTC ('0' + (ui % 10));
270 static void
271 dicat (lo, hi)
272 HOST_WIDE_INT lo, hi;
274 unsigned HOST_WIDE_INT ulo, uhi, qlo, qhi;
276 if (hi >= 0)
278 uhi = hi;
279 ulo = lo;
281 else
283 uhi = (lo == 0 ? -hi : -hi-1);
284 ulo = -lo;
286 if (uhi == 0
287 && ulo < ((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)))
289 icat (ulo);
290 return;
292 /* Divide 2^HOST_WIDE_INT*uhi+ulo by 10. */
293 qhi = uhi / 10;
294 uhi = uhi % 10;
295 qlo = uhi * (((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)) / 5);
296 qlo += ulo / 10;
297 ulo = ulo % 10;
298 ulo += uhi * (((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)) % 5)
299 * 2;
300 qlo += ulo / 10;
301 ulo = ulo % 10;
302 /* Quotient is 2^HOST_WIDE_INT*qhi+qlo, remainder is ulo. */
303 dicat (qlo, qhi);
304 OB_PUTC ('0' + ulo);
307 /* Returns the index of TYPE in the typevec, or -1 if it's not there. */
309 static inline int
310 old_backref_index (type)
311 tree type;
313 size_t tindex;
315 if (! is_back_referenceable_type (type))
316 return -1;
318 /* The entry for this parm is at maxtype-1, so don't look there for
319 something to repeat. */
320 for (tindex = 0; tindex < maxtype - 1; ++tindex)
321 if (same_type_p (VARRAY_TREE (typevec, tindex), type))
322 break;
324 if (tindex == maxtype - 1)
325 return -1;
327 return tindex;
330 /* Old mangling style: If TYPE has already been used in the parameter list,
331 emit a backward reference and return non-zero; otherwise, return 0.
333 NREPEATS is the number of repeats we've recorded of this type, or 0 if
334 this is the first time we've seen it and we're just looking to see if
335 it had been used before. */
337 static inline int
338 flush_repeats (nrepeats, type)
339 int nrepeats;
340 tree type;
342 int tindex = old_backref_index (type);
344 if (tindex == -1)
346 my_friendly_assert (nrepeats == 0, 990316);
347 return 0;
350 if (nrepeats > 1)
352 OB_PUTC ('N');
353 icat (nrepeats);
354 if (nrepeats > 9)
355 OB_PUTC ('_');
357 else
358 OB_PUTC ('T');
359 icat (tindex);
360 if (tindex > 9)
361 OB_PUTC ('_');
363 return 1;
366 /* Returns nonzero iff this is a type to which we will want to make
367 back-references (using the `B' code). */
369 static int
370 is_back_referenceable_type (type)
371 tree type;
373 /* For some reason, the Java folks don't want back refs on these. */
374 if (TYPE_FOR_JAVA (type))
375 return 0;
377 switch (TREE_CODE (type))
379 case BOOLEAN_TYPE:
380 if (!flag_do_squangling)
381 /* Even though the mangling of this is just `b', we did
382 historically generate back-references for it. */
383 return 1;
384 /* Fall through. */
386 case INTEGER_TYPE:
387 case REAL_TYPE:
388 case VOID_TYPE:
389 /* These types have single-character manglings, so there's no
390 point in generating back-references. */
391 return 0;
393 case TEMPLATE_TYPE_PARM:
394 /* It would be a bit complex to demangle signatures correctly if
395 we generated back-references to these, and the manglings of
396 type parameters are short. */
397 return 0;
399 default:
400 return 1;
404 /* Issue the squangling code indicating NREPEATS repetitions of TYPE,
405 which was the last parameter type output. */
407 static void
408 issue_nrepeats (nrepeats, type)
409 int nrepeats;
410 tree type;
412 if (nrepeats == 1 && !is_back_referenceable_type (type))
413 /* For types whose manglings are short, don't bother using the
414 repetition code if there's only one repetition, since the
415 repetition code will be about as long as the ordinary mangling. */
416 build_mangled_name_for_type (type);
417 else
419 OB_PUTC ('n');
420 icat (nrepeats);
421 if (nrepeats > 9)
422 OB_PUTC ('_');
426 /* Check to see if a tree node has been entered into the Kcode typelist.
427 If not, add it. Returns -1 if it isn't found, otherwise returns the
428 index. */
430 static int
431 check_ktype (node, add)
432 tree node;
433 int add;
435 size_t x;
436 tree localnode = node;
438 if (ktypelist == NULL)
439 return -1;
441 if (TREE_CODE (node) == TYPE_DECL)
442 localnode = TREE_TYPE (node);
444 for (x = 0; x < maxktype; x++)
446 if (same_type_p (localnode, VARRAY_TREE (ktypelist, x)))
447 return x;
449 /* Didn't find it, so add it here. */
450 if (add)
452 if (VARRAY_SIZE (ktypelist) <= maxktype)
453 VARRAY_GROW (ktypelist,
454 VARRAY_SIZE (ktypelist) * 3 / 2);
455 VARRAY_TREE (ktypelist, maxktype) = localnode;
456 maxktype++;
458 return -1;
462 static inline int
463 issue_ktype (decl)
464 tree decl;
466 int kindex;
467 kindex = check_ktype (decl, FALSE);
468 if (kindex != -1)
470 OB_PUTC ('K');
471 icat (kindex);
472 if (kindex > 9)
473 OB_PUTC ('_');
474 return TRUE;
476 return FALSE;
479 /* Build a representation for DECL, which may be an entity not at
480 global scope. If so, a marker indicating that the name is
481 qualified has already been output, but the qualifying context has
482 not. */
484 static void
485 build_overload_nested_name (decl)
486 tree decl;
488 tree context;
490 if (ktypelist && issue_ktype (decl))
491 return;
493 if (decl == global_namespace)
494 return;
496 context = CP_DECL_CONTEXT (decl);
498 /* try to issue a K type, and if we can't continue the normal path */
499 if (!(ktypelist && issue_ktype (context)))
501 /* For a template type parameter, we want to output an 'Xn'
502 rather than 'T' or some such. */
503 if (TREE_CODE (context) == TEMPLATE_TYPE_PARM
504 || TREE_CODE (context) == TEMPLATE_TEMPLATE_PARM)
505 build_mangled_name_for_type (context);
506 else
508 if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
509 context = TYPE_NAME (context);
510 build_overload_nested_name (context);
514 if (TREE_CODE (decl) == FUNCTION_DECL)
516 static int static_labelno;
518 tree name = DECL_ASSEMBLER_NAME (decl);
519 char *label;
521 ASM_FORMAT_PRIVATE_NAME (label, IDENTIFIER_POINTER (name), static_labelno);
522 static_labelno++;
524 if (numeric_output_need_bar)
525 OB_PUTC ('_');
526 icat (strlen (label));
527 OB_PUTCP (label);
528 numeric_output_need_bar = 1;
530 else if (TREE_CODE (decl) == NAMESPACE_DECL)
531 build_overload_identifier (DECL_NAME (decl));
532 else /* TYPE_DECL */
533 build_overload_identifier (decl);
536 /* Output the decimal representation of I. If I > 9, the decimal
537 representation is preceeded and followed by an underscore. */
539 static void
540 build_underscore_int (i)
541 int i;
543 if (i > 9)
544 OB_PUTC ('_');
545 icat (i);
546 if (i > 9)
547 OB_PUTC ('_');
550 static void
551 build_overload_scope_ref (value)
552 tree value;
554 OB_PUTC2 ('Q', '2');
555 numeric_output_need_bar = 0;
556 build_mangled_name_for_type (TREE_OPERAND (value, 0));
557 build_overload_identifier (TREE_OPERAND (value, 1));
560 /* VALUE is a complex expression. Produce an appropriate mangling.
561 (We are forced to mangle complex expressions when dealing with
562 templates, and an expression involving template parameters appears
563 in the type of a function parameter.) */
565 static void
566 mangle_expression (value)
567 tree value;
569 if (TREE_CODE (value) == SCOPE_REF)
571 build_overload_scope_ref (value);
572 return;
575 OB_PUTC ('E');
576 numeric_output_need_bar = 0;
578 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (value))))
580 int i;
581 int operands = tree_code_length[(int) TREE_CODE (value)];
582 tree id;
583 const char *name;
585 id = ansi_opname [(int) TREE_CODE (value)];
586 my_friendly_assert (id != NULL_TREE, 0);
587 name = IDENTIFIER_POINTER (id);
588 if (name[0] != '_' || name[1] != '_')
589 /* On some erroneous inputs, we can get here with VALUE a
590 LOOKUP_EXPR. In that case, the NAME will be the
591 identifier for "<invalid operator>". We must survive
592 this routine in order to issue a sensible error
593 message, so we fall through to the case below. */
594 goto bad_value;
596 for (i = 0; i < operands; ++i)
598 tree operand;
599 enum tree_code tc;
601 /* We just outputted either the `E' or the name of the
602 operator. */
603 numeric_output_need_bar = 0;
605 if (i != 0)
606 /* Skip the leading underscores. */
607 OB_PUTCP (name + 2);
609 operand = TREE_OPERAND (value, i);
610 tc = TREE_CODE (operand);
612 if (TREE_CODE_CLASS (tc) == 't')
613 /* We can get here with sizeof, e.g.:
615 template <class T> void f(A<sizeof(T)>); */
616 build_mangled_name_for_type (operand);
617 else
618 build_overload_value (TREE_TYPE (operand),
619 operand,
620 mf_maybe_uninstantiated);
623 else
625 /* We don't ever want this output, but it's
626 inconvenient not to be able to build the string.
627 This should cause assembler errors we'll notice. */
629 static int n;
630 bad_value:
631 sprintf (digit_buffer, " *%d", n++);
632 OB_PUTCP (digit_buffer);
635 OB_PUTC ('W');
636 numeric_output_need_bar = 0;
639 /* Encoding for an INTEGER_CST value. */
641 static void
642 build_overload_int (value, flags)
643 tree value;
644 mangling_flags flags;
646 int multiple_words_p = 0;
647 int multiple_digits_p = 0;
649 if ((flags & mf_maybe_uninstantiated) && TREE_CODE (value) != INTEGER_CST)
651 mangle_expression (value);
652 return;
655 /* Unless we were looking at an uninstantiated template, integers
656 should always be represented by constants. */
657 my_friendly_assert (TREE_CODE (value) == INTEGER_CST, 243);
659 /* If the high-order word is not merely a sign-extension of the
660 low-order word, we must use a special output routine that can
661 deal with this. */
662 if (TREE_INT_CST_HIGH (value)
663 != (TREE_INT_CST_LOW (value) >> (HOST_BITS_PER_WIDE_INT - 1)))
665 multiple_words_p = 1;
666 /* And there is certainly going to be more than one digit. */
667 multiple_digits_p = 1;
669 else
670 multiple_digits_p = (TREE_INT_CST_LOW (value) > 9
671 || TREE_INT_CST_LOW (value) < -9);
673 /* If necessary, add a leading underscore. */
674 if (multiple_digits_p && (flags & mf_use_underscores_around_value))
675 OB_PUTC ('_');
677 /* Output the number itself. */
678 if (multiple_words_p)
679 dicat (TREE_INT_CST_LOW (value), TREE_INT_CST_HIGH (value));
680 else
681 icat (TREE_INT_CST_LOW (value));
683 if (flags & mf_use_underscores_around_value)
685 if (multiple_digits_p)
686 OB_PUTC ('_');
687 /* Whether or not there were multiple digits, we don't need an
688 underscore. We've either terminated the number with an
689 underscore, or else it only had one digit. */
690 numeric_output_need_bar = 0;
692 else
693 /* We just output a numeric value. */
694 numeric_output_need_bar = 1;
698 /* Output S followed by a representation of the TEMPLATE_PARM_INDEX
699 supplied in INDEX. */
701 static void
702 build_mangled_template_parm_index (s, index)
703 const char *s;
704 tree index;
706 OB_PUTCP (s);
707 build_underscore_int (TEMPLATE_PARM_IDX (index));
708 /* We use the LEVEL, not the ORIG_LEVEL, because the mangling is a
709 representation of the function from the point of view of its
710 type. */
711 build_underscore_int (TEMPLATE_PARM_LEVEL (index));
715 /* Mangling for C9X integer types (and Cygnus extensions for 128-bit
716 and other types) is based on the letter "I" followed by the hex
717 representations of the bitsize for the type in question. For
718 encodings that result in larger than two digits, a leading and
719 trailing underscore is added.
721 Thus:
722 int1_t = 001 = I01
723 int8_t = 008 = I08
724 int16_t = 010 = I10
725 int24_t = 018 = I18
726 int32_t = 020 = I20
727 int64_t = 040 = I40
728 int80_t = 050 = I50
729 int128_t = 080 = I80
730 int256_t = 100 = I_100_
731 int512_t = 200 = I_200_
733 Given an integer in decimal format, mangle according to this scheme. */
735 #if HOST_BITS_PER_WIDE_INT >= 64
736 static void
737 build_mangled_C9x_name (bits)
738 int bits;
740 char mangled[10] = "";
742 if (bits > 255)
743 sprintf (mangled, "I_%x_", bits);
744 else
745 sprintf (mangled, "I%.2x", bits);
747 OB_PUTCP (mangled);
749 #endif
751 static void
752 build_overload_value (type, value, flags)
753 tree type, value;
754 mangling_flags flags;
756 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (type)) == 't', 0);
758 while (TREE_CODE (value) == NON_LVALUE_EXPR
759 || TREE_CODE (value) == NOP_EXPR)
760 value = TREE_OPERAND (value, 0);
762 if (numeric_output_need_bar)
764 OB_PUTC ('_');
765 numeric_output_need_bar = 0;
768 if (TREE_CODE (value) == TEMPLATE_PARM_INDEX)
770 build_mangled_template_parm_index ("Y", value);
771 return;
774 if (TYPE_PTRMEM_P (type))
776 if (TREE_CODE (value) != PTRMEM_CST)
777 /* We should have already rejected this pointer to member,
778 since it is not a constant. */
779 my_friendly_abort (0);
781 /* Get the actual FIELD_DECL. */
782 value = PTRMEM_CST_MEMBER (value);
783 my_friendly_assert (TREE_CODE (value) == FIELD_DECL, 0);
785 /* Output the name of the field. */
786 build_overload_identifier (DECL_NAME (value));
787 return;
789 else if (INTEGRAL_TYPE_P (type))
791 build_overload_int (value, flags);
792 return;
795 /* The only case where we use the extra underscores here is when
796 forming the mangling for an integral non-type template argument.
797 If that didn't happen, stop now. */
798 flags &= ~mf_use_underscores_around_value;
800 switch (TREE_CODE (type))
802 case REAL_TYPE:
804 REAL_VALUE_TYPE val;
805 char *bufp = digit_buffer;
807 /* We must handle non-constants in templates. */
808 if (TREE_CODE (value) != REAL_CST)
810 mangle_expression (value);
811 break;
814 val = TREE_REAL_CST (value);
815 if (REAL_VALUE_ISNAN (val))
817 sprintf (bufp, "NaN");
819 else
821 if (REAL_VALUE_NEGATIVE (val))
823 val = REAL_VALUE_NEGATE (val);
824 *bufp++ = 'm';
826 if (REAL_VALUE_ISINF (val))
828 sprintf (bufp, "Infinity");
830 else
832 REAL_VALUE_TO_DECIMAL (val, "%.20e", bufp);
833 bufp = (char *) index (bufp, 'e');
834 if (!bufp)
835 strcat (digit_buffer, "e0");
836 else
838 char *p;
839 bufp++;
840 if (*bufp == '-')
842 *bufp++ = 'm';
844 p = bufp;
845 if (*p == '+')
846 p++;
847 while (*p == '0')
848 p++;
849 if (*p == 0)
851 *bufp++ = '0';
852 *bufp = 0;
854 else if (p != bufp)
856 while (*p)
857 *bufp++ = *p++;
858 *bufp = 0;
861 #ifdef NO_DOT_IN_LABEL
862 bufp = (char *) index (bufp, '.');
863 if (bufp)
864 *bufp = '_';
865 #endif
868 OB_PUTCP (digit_buffer);
869 numeric_output_need_bar = 1;
870 return;
872 case POINTER_TYPE:
873 if (TREE_CODE (value) == INTEGER_CST)
875 build_overload_int (value, flags);
876 return;
878 else if (TREE_CODE (value) == TEMPLATE_PARM_INDEX)
880 build_mangled_template_parm_index ("", value);
881 numeric_output_need_bar = 1;
882 return;
885 value = TREE_OPERAND (value, 0);
887 /* Fall through. */
889 case REFERENCE_TYPE:
890 if (TREE_CODE (value) == VAR_DECL)
892 my_friendly_assert (DECL_NAME (value) != 0, 245);
893 build_overload_identifier (DECL_ASSEMBLER_NAME (value));
894 return;
896 else if (TREE_CODE (value) == FUNCTION_DECL)
898 my_friendly_assert (DECL_NAME (value) != 0, 246);
899 build_overload_identifier (DECL_ASSEMBLER_NAME (value));
900 return;
902 else if (TREE_CODE (value) == SCOPE_REF)
903 build_overload_scope_ref (value);
904 else
905 my_friendly_abort (71);
906 break; /* not really needed */
908 case RECORD_TYPE:
910 tree delta;
911 tree idx;
912 tree pfn;
913 tree delta2;
915 my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 0);
917 /* We'll get a ADDR_EXPR of a SCOPE_REF here if we're
918 mangling, an instantiation of something like:
920 template <class T, void (T::*fp)()> class C {};
921 template <class T> C<T, &T::f> x();
923 We mangle the return type of the function, and that
924 contains template parameters. */
925 if (TREE_CODE (value) == ADDR_EXPR
926 && TREE_CODE (TREE_OPERAND (value, 0)) == SCOPE_REF)
928 build_overload_scope_ref (TREE_OPERAND (value, 0));
929 break;
932 my_friendly_assert (TREE_CODE (value) == PTRMEM_CST, 0);
934 expand_ptrmemfunc_cst (value, &delta, &idx, &pfn, &delta2);
935 build_overload_int (delta, flags);
936 OB_PUTC ('_');
937 build_overload_int (idx, flags);
938 OB_PUTC ('_');
939 if (pfn)
941 numeric_output_need_bar = 0;
942 build_overload_identifier (DECL_ASSEMBLER_NAME
943 (PTRMEM_CST_MEMBER (value)));
945 else
947 OB_PUTC ('i');
948 build_overload_int (delta2, flags);
951 break;
953 default:
954 sorry ("conversion of %s as template parameter",
955 tree_code_name [(int) TREE_CODE (type)]);
956 my_friendly_abort (72);
961 /* Add encodings for the declaration of template template parameters.
962 PARMLIST must be a TREE_VEC. */
964 static void
965 build_template_template_parm_names (parmlist)
966 tree parmlist;
968 int i, nparms;
970 my_friendly_assert (TREE_CODE (parmlist) == TREE_VEC, 246.5);
971 nparms = TREE_VEC_LENGTH (parmlist);
972 icat (nparms);
973 for (i = 0; i < nparms; i++)
975 tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
976 if (TREE_CODE (parm) == TYPE_DECL)
978 /* This parameter is a type. */
979 OB_PUTC ('Z');
981 else if (TREE_CODE (parm) == TEMPLATE_DECL)
983 /* This parameter is a template. */
984 OB_PUTC ('z');
985 build_template_template_parm_names (DECL_INNERMOST_TEMPLATE_PARMS (parm));
987 else
988 /* It's a PARM_DECL. */
989 build_mangled_name_for_type (TREE_TYPE (parm));
994 /* Add encodings for the vector of template parameters in PARMLIST,
995 given the vector of arguments to be substituted in ARGLIST. */
997 static void
998 build_template_parm_names (parmlist, arglist)
999 tree parmlist;
1000 tree arglist;
1002 int i, nparms;
1003 tree inner_args = innermost_args (arglist);
1005 nparms = TREE_VEC_LENGTH (parmlist);
1006 icat (nparms);
1007 for (i = 0; i < nparms; i++)
1009 tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
1010 tree arg = TREE_VEC_ELT (inner_args, i);
1011 if (TREE_CODE (parm) == TYPE_DECL)
1013 /* This parameter is a type. */
1014 OB_PUTC ('Z');
1015 build_mangled_name_for_type (arg);
1017 else if (TREE_CODE (parm) == TEMPLATE_DECL)
1019 /* This parameter is a template. */
1020 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
1021 /* Output parameter declaration, argument index and level. */
1022 build_mangled_name_for_type (arg);
1023 else
1025 /* A TEMPLATE_DECL node, output the parameter declaration
1026 and template name */
1028 OB_PUTC ('z');
1029 build_template_template_parm_names
1030 (DECL_INNERMOST_TEMPLATE_PARMS (parm));
1031 icat (IDENTIFIER_LENGTH (DECL_NAME (arg)));
1032 OB_PUTID (DECL_NAME (arg));
1035 else
1037 parm = tsubst (parm, arglist, /*complain=*/1, NULL_TREE);
1038 /* It's a PARM_DECL. */
1039 build_mangled_name_for_type (TREE_TYPE (parm));
1040 build_overload_value (TREE_TYPE (parm), arg,
1041 ((mf_maybe_uninstantiated
1042 * uses_template_parms (arglist))
1043 | mf_use_underscores_around_value));
1048 /* Output the representation for NAME, which is either a TYPE_DECL or
1049 an IDENTIFIER. */
1051 static void
1052 build_overload_identifier (name)
1053 tree name;
1055 if (TREE_CODE (name) == TYPE_DECL
1056 && CLASS_TYPE_P (TREE_TYPE (name))
1057 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (name))
1058 && (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (TREE_TYPE (name)))
1059 || (TREE_CODE (CP_DECL_CONTEXT (CLASSTYPE_TI_TEMPLATE
1060 (TREE_TYPE (name))))
1061 == FUNCTION_DECL)))
1063 /* NAME is the TYPE_DECL for a template specialization. */
1064 tree template, parmlist, arglist, tname;
1065 template = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (name));
1066 arglist = CLASSTYPE_TI_ARGS (TREE_TYPE (name));
1067 tname = DECL_NAME (template);
1068 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
1069 OB_PUTC ('t');
1070 icat (IDENTIFIER_LENGTH (tname));
1071 OB_PUTID (tname);
1072 build_template_parm_names (parmlist, arglist);
1074 else
1076 if (TREE_CODE (name) == TYPE_DECL)
1077 name = DECL_NAME (name);
1078 if (numeric_output_need_bar)
1080 OB_PUTC ('_');
1081 numeric_output_need_bar = 0;
1083 icat (IDENTIFIER_LENGTH (name));
1084 OB_PUTID (name);
1088 /* Given DECL, either a class TYPE, TYPE_DECL or FUNCTION_DECL, produce
1089 the mangling for it. Used by build_mangled_name and build_static_name. */
1091 static void
1092 build_qualified_name (decl)
1093 tree decl;
1095 tree context;
1096 int i = 1;
1098 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 't')
1099 decl = TYPE_NAME (decl);
1101 /* If DECL_ASSEMBLER_NAME has been set properly, use it. */
1102 if (TREE_CODE (decl) == TYPE_DECL
1103 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl) && !flag_do_squangling)
1105 tree id = DECL_ASSEMBLER_NAME (decl);
1106 OB_PUTID (id);
1107 if (ISDIGIT (IDENTIFIER_POINTER (id) [IDENTIFIER_LENGTH (id) - 1]))
1108 numeric_output_need_bar = 1;
1109 return;
1112 context = decl;
1113 /* If we can't find a Ktype, do it the hard way. */
1114 if (check_ktype (context, FALSE) == -1)
1116 /* Count type and namespace scopes. */
1117 while (1)
1119 context = CP_DECL_CONTEXT (context);
1120 if (context == global_namespace)
1121 break;
1122 i += 1;
1123 if (check_ktype (context, FALSE) != -1)
1124 /* Found one! */
1125 break;
1126 if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
1127 context = TYPE_NAME (context);
1131 if (i > 1)
1133 OB_PUTC ('Q');
1134 build_underscore_int (i);
1135 numeric_output_need_bar = 0;
1137 build_overload_nested_name (decl);
1140 /* Output the mangled representation for TYPE. If EXTRA_GCODE is
1141 non-zero, mangled names for structure/union types are intentionally
1142 mangled differently from the method described in the ARM. */
1144 static void
1145 build_mangled_name_for_type_with_Gcode (type, extra_Gcode)
1146 tree type;
1147 int extra_Gcode;
1149 if (TYPE_PTRMEMFUNC_P (type))
1150 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
1151 process_modifiers (type);
1152 process_overload_item (type, extra_Gcode);
1155 /* Like build_mangled_name_for_type_with_Gcode, but never outputs the
1156 `G'. */
1158 static void
1159 build_mangled_name_for_type (type)
1160 tree type;
1162 build_mangled_name_for_type_with_Gcode (type, 0);
1165 /* Given a list of parameters in PARMTYPES, create an unambiguous
1166 overload string. Should distinguish any type that C (or C++) can
1167 distinguish. I.e., pointers to functions are treated correctly.
1169 Caller must deal with whether a final `e' goes on the end or not.
1171 Any default conversions must take place before this function
1172 is called.
1174 BEGIN and END control initialization and finalization of the
1175 obstack where we build the string. */
1177 char *
1178 build_overload_name (parmtypes, begin, end)
1179 tree parmtypes;
1180 int begin, end;
1182 char *ret;
1183 start_squangling ();
1184 ret = build_mangled_name (parmtypes, begin, end);
1185 end_squangling ();
1186 return ret ;
1189 /* Output the mangled representation for PARMTYPES. If PARMTYPES is a
1190 TREE_LIST, then it is a list of parameter types. Otherwise,
1191 PARMTYPES must be a single type. */
1193 static char *
1194 build_mangled_name (parmtypes, begin, end)
1195 tree parmtypes;
1196 int begin, end;
1198 if (begin)
1199 OB_INIT ();
1201 if (TREE_CODE (parmtypes) != TREE_LIST)
1202 /* There is only one type. */
1203 build_mangled_name_for_type (parmtypes);
1204 else
1206 /* There are several types in a parameter list. */
1207 int nrepeats = 0;
1208 int old_style_repeats = !flag_do_squangling && !nofold && typevec;
1209 tree last_type = NULL_TREE;
1211 for (; parmtypes && parmtypes != void_list_node;
1212 parmtypes = TREE_CHAIN (parmtypes))
1214 /* We used to call canonical_type_variant here, but that isn't
1215 good enough; it doesn't handle pointers to typedef types. So
1216 we can't just set TREE_USED to say we've seen a type already;
1217 we have to check each of the earlier types with same_type_p. */
1218 tree parmtype = TREE_VALUE (parmtypes);
1220 if (old_style_repeats)
1222 /* Every argument gets counted. */
1223 my_friendly_assert (maxtype < VARRAY_SIZE (typevec), 387);
1224 VARRAY_TREE (typevec, maxtype) = parmtype;
1225 maxtype++;
1228 if (last_type && same_type_p (parmtype, last_type))
1230 if (flag_do_squangling
1231 || (old_style_repeats
1232 && is_back_referenceable_type (parmtype)))
1234 /* The next type is the same as this one. Keep
1235 track of the repetition, and output the repeat
1236 count later. */
1237 nrepeats++;
1238 continue;
1241 else if (nrepeats != 0)
1243 /* Indicate how many times the previous parameter was
1244 repeated. */
1245 if (old_style_repeats)
1246 flush_repeats (nrepeats, last_type);
1247 else
1248 issue_nrepeats (nrepeats, last_type);
1249 nrepeats = 0;
1252 last_type = parmtype;
1254 /* Note that for bug-compatibility with 2.7.2, we can't build up
1255 repeats of types other than the most recent one. So we call
1256 flush_repeats every round, if we get this far. */
1257 if (old_style_repeats && flush_repeats (0, parmtype))
1258 continue;
1260 /* Output the PARMTYPE. */
1261 build_mangled_name_for_type_with_Gcode (parmtype, 1);
1264 /* Output the repeat count for the last parameter, if
1265 necessary. */
1266 if (nrepeats != 0)
1268 if (old_style_repeats)
1269 flush_repeats (nrepeats, last_type);
1270 else
1271 issue_nrepeats (nrepeats, last_type);
1272 nrepeats = 0;
1275 if (!parmtypes)
1276 /* The parameter list ends in an ellipsis. */
1277 OB_PUTC ('e');
1280 if (end)
1281 OB_FINISH ();
1282 return (char *)obstack_base (&scratch_obstack);
1285 /* Emit modifiers such as constant, read-only, and volatile. */
1287 static void
1288 process_modifiers (parmtype)
1289 tree parmtype;
1291 /* Note that here we do not use CP_TYPE_CONST_P and friends because
1292 we describe types recursively; we will get the `const' in
1293 `const int ()[10]' when processing the `const int' part. */
1294 if (TYPE_READONLY (parmtype))
1295 OB_PUTC ('C');
1296 if (TREE_CODE (parmtype) == INTEGER_TYPE
1297 && parmtype != char_type_node
1298 && parmtype != wchar_type_node
1299 && (TYPE_MAIN_VARIANT (parmtype)
1300 == unsigned_type (TYPE_MAIN_VARIANT (parmtype)))
1301 && ! TYPE_FOR_JAVA (parmtype))
1302 OB_PUTC ('U');
1303 if (TYPE_VOLATILE (parmtype))
1304 OB_PUTC ('V');
1305 /* It would be better to use `R' for `restrict', but that's already
1306 used for reference types. And `r' is used for `long double'. */
1307 if (TYPE_RESTRICT (parmtype))
1308 OB_PUTC ('u');
1311 /* Check to see if TYPE has been entered into the Bcode typelist. If
1312 so, return 1 and emit a backreference to TYPE. Otherwise, add TYPE
1313 to the list of back-referenceable types and return 0. */
1315 static int
1316 check_btype (type)
1317 tree type;
1319 size_t x;
1321 if (btypelist == NULL)
1322 return 0;
1324 if (!is_back_referenceable_type (type))
1325 return 0;
1327 for (x = 0; x < maxbtype; x++)
1328 if (same_type_p (type, VARRAY_TREE (btypelist, x)))
1330 OB_PUTC ('B');
1331 icat (x);
1332 if (x > 9)
1333 OB_PUTC ('_');
1334 return 1 ;
1337 if (VARRAY_SIZE (btypelist) <= maxbtype)
1338 /* Enlarge the table. */
1339 VARRAY_GROW (btypelist,
1340 VARRAY_SIZE (btypelist) * 3 / 2);
1342 /* Register the TYPE. */
1343 VARRAY_TREE (btypelist, maxbtype) = type;
1344 maxbtype++;
1346 return 0;
1349 /* Emit the correct code for various node types. */
1351 static void
1352 process_overload_item (parmtype, extra_Gcode)
1353 tree parmtype;
1354 int extra_Gcode;
1356 numeric_output_need_bar = 0;
1358 /* Our caller should have already handed any qualifiers, so pull out the
1359 TYPE_MAIN_VARIANT to avoid typedef confusion. Except we can't do that
1360 for arrays, because they are transparent to qualifiers. Sigh. */
1361 if (TREE_CODE (parmtype) == ARRAY_TYPE)
1362 parmtype = canonical_type_variant (parmtype);
1363 else
1364 parmtype = TYPE_MAIN_VARIANT (parmtype);
1366 /* These tree types are considered modifiers for B code squangling,
1367 and therefore should not get entries in the Btypelist. They are,
1368 however, repeatable types. */
1370 switch (TREE_CODE (parmtype))
1372 case REFERENCE_TYPE:
1373 OB_PUTC ('R');
1374 goto more;
1376 case ARRAY_TYPE:
1378 OB_PUTC ('A');
1379 if (TYPE_DOMAIN (parmtype) == NULL_TREE)
1380 OB_PUTC ('_');
1381 else
1383 tree length = array_type_nelts (parmtype);
1384 if (TREE_CODE (length) != INTEGER_CST || flag_do_squangling)
1386 length = fold (build (PLUS_EXPR, TREE_TYPE (length),
1387 length, integer_one_node));
1388 STRIP_NOPS (length);
1390 build_overload_value (sizetype, length, 1);
1392 if (numeric_output_need_bar && ! flag_do_squangling)
1393 OB_PUTC ('_');
1394 goto more;
1397 case POINTER_TYPE:
1398 OB_PUTC ('P');
1399 more:
1400 build_mangled_name_for_type (TREE_TYPE (parmtype));
1401 return;
1402 break;
1404 default:
1405 break;
1408 if (flag_do_squangling && check_btype (parmtype))
1409 /* If PARMTYPE is already in the list of back-referenceable types,
1410 then check_btype will output the appropriate reference, and
1411 there's nothing more to do. */
1412 return;
1414 switch (TREE_CODE (parmtype))
1416 case OFFSET_TYPE:
1417 OB_PUTC ('O');
1418 build_mangled_name_for_type (TYPE_OFFSET_BASETYPE (parmtype));
1419 OB_PUTC ('_');
1420 build_mangled_name_for_type (TREE_TYPE (parmtype));
1421 break;
1423 case FUNCTION_TYPE:
1424 case METHOD_TYPE:
1426 tree parms = TYPE_ARG_TYPES (parmtype);
1428 /* Rather than implementing a reentrant TYPEVEC, we turn off
1429 repeat codes here, unless we're squangling. Squangling
1430 doesn't make use of the TYPEVEC, so there's no reentrancy
1431 problem. */
1432 int old_nofold = nofold;
1433 if (!flag_do_squangling)
1434 nofold = 1;
1436 if (TREE_CODE (parmtype) == METHOD_TYPE)
1438 /* Mark this as a method. */
1439 OB_PUTC ('M');
1440 /* Output the class of which this method is a member. */
1441 build_mangled_name_for_type (TYPE_METHOD_BASETYPE (parmtype));
1442 /* Output any qualifiers for the `this' parameter. */
1443 process_modifiers (TREE_TYPE (TREE_VALUE (parms)));
1446 /* Output the parameter types. */
1447 OB_PUTC ('F');
1448 if (parms == NULL_TREE)
1449 OB_PUTC ('e');
1450 else if (parms == void_list_node)
1451 OB_PUTC ('v');
1452 else
1453 build_mangled_name (parms, 0, 0);
1455 /* Output the return type. */
1456 OB_PUTC ('_');
1457 build_mangled_name_for_type (TREE_TYPE (parmtype));
1459 nofold = old_nofold;
1460 break;
1463 case INTEGER_TYPE:
1464 if (parmtype == integer_type_node
1465 || parmtype == unsigned_type_node
1466 || parmtype == java_int_type_node)
1467 OB_PUTC ('i');
1468 else if (parmtype == long_integer_type_node
1469 || parmtype == long_unsigned_type_node)
1470 OB_PUTC ('l');
1471 else if (parmtype == short_integer_type_node
1472 || parmtype == short_unsigned_type_node
1473 || parmtype == java_short_type_node)
1474 OB_PUTC ('s');
1475 else if (parmtype == signed_char_type_node)
1477 OB_PUTC ('S');
1478 OB_PUTC ('c');
1480 else if (parmtype == char_type_node
1481 || parmtype == unsigned_char_type_node
1482 || parmtype == java_byte_type_node)
1483 OB_PUTC ('c');
1484 else if (parmtype == wchar_type_node
1485 || parmtype == java_char_type_node)
1486 OB_PUTC ('w');
1487 else if (parmtype == long_long_integer_type_node
1488 || parmtype == long_long_unsigned_type_node
1489 || parmtype == java_long_type_node)
1490 OB_PUTC ('x');
1491 else if (parmtype == java_boolean_type_node)
1492 OB_PUTC ('b');
1493 #if HOST_BITS_PER_WIDE_INT >= 64
1494 else
1496 int bits = TREE_INT_CST_LOW (TYPE_SIZE (parmtype));
1497 build_mangled_C9x_name (bits);
1499 #else
1500 else
1501 my_friendly_abort (73);
1502 #endif
1503 break;
1505 case BOOLEAN_TYPE:
1506 OB_PUTC ('b');
1507 break;
1509 case REAL_TYPE:
1510 if (parmtype == long_double_type_node)
1511 OB_PUTC ('r');
1512 else if (parmtype == double_type_node
1513 || parmtype == java_double_type_node)
1514 OB_PUTC ('d');
1515 else if (parmtype == float_type_node
1516 || parmtype == java_float_type_node)
1517 OB_PUTC ('f');
1518 else my_friendly_abort (74);
1519 break;
1521 case COMPLEX_TYPE:
1522 OB_PUTC ('J');
1523 build_mangled_name_for_type (TREE_TYPE (parmtype));
1524 break;
1526 case VOID_TYPE:
1527 OB_PUTC ('v');
1528 break;
1530 case ERROR_MARK: /* not right, but nothing is anyway */
1531 break;
1533 /* have to do these */
1534 case UNION_TYPE:
1535 case RECORD_TYPE:
1537 if (extra_Gcode)
1538 OB_PUTC ('G'); /* make it look incompatible with AT&T */
1539 /* drop through into next case */
1541 case ENUMERAL_TYPE:
1543 tree name = TYPE_NAME (parmtype);
1545 my_friendly_assert (TREE_CODE (name) == TYPE_DECL, 248);
1547 build_qualified_name (name);
1548 break;
1551 case UNKNOWN_TYPE:
1552 /* This will take some work. */
1553 OB_PUTC ('?');
1554 break;
1556 case TEMPLATE_TEMPLATE_PARM:
1557 /* Find and output the original template parameter
1558 declaration. */
1559 if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parmtype))
1561 build_mangled_template_parm_index ("tzX",
1562 TEMPLATE_TYPE_PARM_INDEX
1563 (parmtype));
1564 build_template_parm_names
1565 (DECL_INNERMOST_TEMPLATE_PARMS (TYPE_TI_TEMPLATE (parmtype)),
1566 TYPE_TI_ARGS (parmtype));
1568 else
1570 build_mangled_template_parm_index ("ZzX",
1571 TEMPLATE_TYPE_PARM_INDEX
1572 (parmtype));
1573 build_template_template_parm_names
1574 (DECL_INNERMOST_TEMPLATE_PARMS (TYPE_STUB_DECL (parmtype)));
1576 break;
1578 case TEMPLATE_TYPE_PARM:
1579 build_mangled_template_parm_index ("X",
1580 TEMPLATE_TYPE_PARM_INDEX
1581 (parmtype));
1582 break;
1584 case TYPENAME_TYPE:
1585 /* When mangling the type of a function template whose
1586 declaration looks like:
1588 template <class T> void foo(typename T::U)
1590 we have to mangle these. */
1591 build_qualified_name (parmtype);
1592 break;
1594 default:
1595 my_friendly_abort (75);
1600 /* Produce the mangling for a variable named NAME in CONTEXT, which can
1601 be either a class TYPE or a FUNCTION_DECL. */
1603 tree
1604 build_static_name (context, name)
1605 tree context, name;
1607 OB_INIT ();
1608 numeric_output_need_bar = 0;
1609 start_squangling ();
1610 #ifdef JOINER
1611 OB_PUTC ('_');
1612 build_qualified_name (context);
1613 OB_PUTC (JOINER);
1614 #else
1615 OB_PUTS ("__static_");
1616 build_qualified_name (context);
1617 OB_PUTC ('_');
1618 #endif
1619 OB_PUTID (name);
1620 OB_FINISH ();
1621 end_squangling ();
1623 return get_identifier ((char *)obstack_base (&scratch_obstack));
1626 /* FOR_METHOD should be 1 if the declaration in question is for a member
1627 of a class (including a static member) and 2 if the declaration is
1628 for a constructor. */
1629 tree
1630 build_decl_overload_real (dname, parms, ret_type, tparms, targs,
1631 for_method)
1632 tree dname;
1633 tree parms;
1634 tree ret_type;
1635 tree tparms;
1636 tree targs;
1637 int for_method;
1639 const char *name = IDENTIFIER_POINTER (dname);
1641 /* member operators new and delete look like methods at this point. */
1642 if (! for_method && parms != NULL_TREE && TREE_CODE (parms) == TREE_LIST
1643 && TREE_CHAIN (parms) == void_list_node)
1645 if (dname == ansi_opname[(int) DELETE_EXPR])
1646 return get_identifier ("__builtin_delete");
1647 else if (dname == ansi_opname[(int) VEC_DELETE_EXPR])
1648 return get_identifier ("__builtin_vec_delete");
1649 if (dname == ansi_opname[(int) NEW_EXPR])
1650 return get_identifier ("__builtin_new");
1651 else if (dname == ansi_opname[(int) VEC_NEW_EXPR])
1652 return get_identifier ("__builtin_vec_new");
1655 start_squangling ();
1656 OB_INIT ();
1657 if (for_method != 2)
1658 OB_PUTCP (name);
1659 /* Otherwise, we can divine that this is a constructor,
1660 and figure out its name without any extra encoding. */
1662 OB_PUTC2 ('_', '_');
1663 numeric_output_need_bar = 0;
1665 if (tparms)
1667 OB_PUTC ('H');
1668 build_template_parm_names (tparms, targs);
1669 OB_PUTC ('_');
1671 else if (!for_method && current_namespace == global_namespace)
1672 /* XXX this works only if we call this in the same namespace
1673 as the declaration. Unfortunately, we don't have the _DECL,
1674 only its name */
1675 OB_PUTC ('F');
1677 if (!for_method && current_namespace != global_namespace)
1678 /* qualify with namespace */
1679 build_qualified_name (current_namespace);
1681 if (parms == NULL_TREE)
1682 OB_PUTC ('e');
1683 else if (parms == void_list_node)
1684 OB_PUTC ('v');
1685 else
1687 if (!flag_do_squangling)
1689 /* Allocate typevec array. */
1690 size_t typevec_size = list_length (parms);
1691 maxtype = 0;
1692 if (!for_method && current_namespace != global_namespace)
1693 /* The namespace of a global function needs one slot. */
1694 typevec_size++;
1695 VARRAY_TREE_INIT (typevec, typevec_size, "typevec");
1697 nofold = 0;
1699 if (for_method)
1701 tree this_type = TREE_TYPE (TREE_VALUE (parms));
1703 build_mangled_name_for_type (this_type);
1705 if (!flag_do_squangling)
1707 my_friendly_assert (maxtype < VARRAY_SIZE (typevec), 387);
1708 VARRAY_TREE (typevec, maxtype) = this_type;
1709 maxtype++;
1712 if (TREE_CHAIN (parms))
1713 build_mangled_name (TREE_CHAIN (parms), 0, 0);
1714 else
1715 OB_PUTC ('e');
1717 else
1719 /* the namespace qualifier for a global function
1720 will count as type */
1721 if (current_namespace != global_namespace
1722 && !flag_do_squangling)
1724 my_friendly_assert (maxtype < VARRAY_SIZE (typevec), 387);
1725 VARRAY_TREE (typevec, maxtype) = current_namespace;
1726 maxtype++;
1728 build_mangled_name (parms, 0, 0);
1731 if (!flag_do_squangling)
1732 /* Deallocate typevec array. */
1733 VARRAY_FREE (typevec);
1736 if (ret_type != NULL_TREE && for_method != 2)
1738 /* Add the return type. */
1739 OB_PUTC ('_');
1740 build_mangled_name_for_type (ret_type);
1743 OB_FINISH ();
1744 end_squangling ();
1746 tree n = get_identifier (obstack_base (&scratch_obstack));
1747 if (IDENTIFIER_OPNAME_P (dname))
1748 IDENTIFIER_OPNAME_P (n) = 1;
1749 return n;
1753 /* Change the name of a function definition so that it may be
1754 overloaded. NAME is the name of the function to overload,
1755 PARMS is the parameter list (which determines what name the
1756 final function obtains).
1758 FOR_METHOD is 1 if this overload is being performed
1759 for a method, rather than a function type. It is 2 if
1760 this overload is being performed for a constructor. */
1762 tree
1763 build_decl_overload (dname, parms, for_method)
1764 tree dname;
1765 tree parms;
1766 int for_method;
1768 return build_decl_overload_real (dname, parms, NULL_TREE, NULL_TREE,
1769 NULL_TREE, for_method);
1772 /* Set the mangled name (DECL_ASSEMBLER_NAME) for DECL. */
1774 void
1775 set_mangled_name_for_decl (decl)
1776 tree decl;
1778 tree parm_types;
1780 if (processing_template_decl)
1781 /* There's no need to mangle the name of a template function. */
1782 return;
1784 parm_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
1786 if (DECL_STATIC_FUNCTION_P (decl))
1787 parm_types =
1788 hash_tree_chain (build_pointer_type (DECL_CLASS_CONTEXT (decl)),
1789 parm_types);
1790 else
1791 /* The only member functions whose type is a FUNCTION_TYPE, rather
1792 than a METHOD_TYPE, should be static members. */
1793 my_friendly_assert (!DECL_CONTEXT (decl)
1794 || !IS_AGGR_TYPE_CODE (TREE_CODE (DECL_CONTEXT (decl)))
1795 || TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE,
1798 DECL_ASSEMBLER_NAME (decl)
1799 = build_decl_overload (DECL_NAME (decl), parm_types,
1800 DECL_FUNCTION_MEMBER_P (decl)
1801 + DECL_CONSTRUCTOR_P (decl));
1804 /* Build an overload name for the type expression TYPE. */
1806 tree
1807 build_typename_overload (type)
1808 tree type;
1810 tree id;
1812 OB_INIT ();
1813 OB_PUTID (ansi_opname[(int) TYPE_EXPR]);
1814 nofold = 1;
1815 start_squangling ();
1816 build_mangled_name (type, 0, 1);
1817 id = get_identifier (obstack_base (&scratch_obstack));
1818 IDENTIFIER_OPNAME_P (id) = 1;
1819 #if 0
1820 IDENTIFIER_GLOBAL_VALUE (id) = TYPE_MAIN_DECL (type);
1821 #endif
1822 TREE_TYPE (id) = type;
1823 end_squangling ();
1824 return id;
1827 tree
1828 build_overload_with_type (name, type)
1829 tree name, type;
1831 OB_INIT ();
1832 OB_PUTID (name);
1833 nofold = 1;
1835 start_squangling ();
1836 build_mangled_name (type, 0, 1);
1837 end_squangling ();
1838 return get_identifier (obstack_base (&scratch_obstack));
1841 tree
1842 get_id_2 (name, name2)
1843 const char *name;
1844 tree name2;
1846 OB_INIT ();
1847 OB_PUTCP (name);
1848 OB_PUTID (name2);
1849 OB_FINISH ();
1850 return get_identifier (obstack_base (&scratch_obstack));
1853 /* Returns a DECL_ASSEMBLER_NAME for the destructor of type TYPE. */
1855 tree
1856 build_destructor_name (type)
1857 tree type;
1859 return build_overload_with_type (get_identifier (DESTRUCTOR_DECL_PREFIX),
1860 type);
1863 /* Given a tree_code CODE, and some arguments (at least one),
1864 attempt to use an overloaded operator on the arguments.
1866 For unary operators, only the first argument need be checked.
1867 For binary operators, both arguments may need to be checked.
1869 Member functions can convert class references to class pointers,
1870 for one-level deep indirection. More than that is not supported.
1871 Operators [](), ()(), and ->() must be member functions.
1873 We call function call building calls with LOOKUP_COMPLAIN if they
1874 are our only hope. This is true when we see a vanilla operator
1875 applied to something of aggregate type. If this fails, we are free
1876 to return `error_mark_node', because we will have reported the
1877 error.
1879 Operators NEW and DELETE overload in funny ways: operator new takes
1880 a single `size' parameter, and operator delete takes a pointer to the
1881 storage being deleted. When overloading these operators, success is
1882 assumed. If there is a failure, report an error message and return
1883 `error_mark_node'. */
1885 /* NOSTRICT */
1886 tree
1887 build_opfncall (code, flags, xarg1, xarg2, arg3)
1888 enum tree_code code;
1889 int flags;
1890 tree xarg1, xarg2, arg3;
1892 return build_new_op (code, flags, xarg1, xarg2, arg3);
1895 /* This function takes an identifier, ID, and attempts to figure out what
1896 it means. There are a number of possible scenarios, presented in increasing
1897 order of hair:
1899 1) not in a class's scope
1900 2) in class's scope, member name of the class's method
1901 3) in class's scope, but not a member name of the class
1902 4) in class's scope, member name of a class's variable
1904 NAME is $1 from the bison rule. It is an IDENTIFIER_NODE.
1905 VALUE is $$ from the bison rule. It is the value returned by lookup_name ($1)
1907 As a last ditch, try to look up the name as a label and return that
1908 address.
1910 Values which are declared as being of REFERENCE_TYPE are
1911 automatically dereferenced here (as a hack to make the
1912 compiler faster). */
1914 tree
1915 hack_identifier (value, name)
1916 tree value, name;
1918 tree type;
1920 if (value == error_mark_node)
1922 if (current_class_name)
1924 tree fields = lookup_fnfields (TYPE_BINFO (current_class_type), name, 1);
1925 if (fields == error_mark_node)
1926 return error_mark_node;
1927 if (fields)
1929 tree fndecl;
1931 fndecl = TREE_VALUE (fields);
1932 my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 251);
1933 /* I could not trigger this code. MvL */
1934 my_friendly_abort (980325);
1935 #ifdef DEAD
1936 if (DECL_CHAIN (fndecl) == NULL_TREE)
1938 warning ("methods cannot be converted to function pointers");
1939 return fndecl;
1941 else
1943 error ("ambiguous request for method pointer `%s'",
1944 IDENTIFIER_POINTER (name));
1945 return error_mark_node;
1947 #endif
1950 if (flag_labels_ok && IDENTIFIER_LABEL_VALUE (name))
1952 return IDENTIFIER_LABEL_VALUE (name);
1954 return error_mark_node;
1957 type = TREE_TYPE (value);
1958 if (TREE_CODE (value) == FIELD_DECL)
1960 if (current_class_ptr == NULL_TREE)
1962 if (current_function_decl
1963 && DECL_STATIC_FUNCTION_P (current_function_decl))
1964 cp_error ("invalid use of member `%D' in static member function",
1965 value);
1966 else
1967 /* We can get here when processing a bad default
1968 argument, like:
1969 struct S { int a; void f(int i = a); } */
1970 cp_error ("invalid use of member `%D'", value);
1972 return error_mark_node;
1974 TREE_USED (current_class_ptr) = 1;
1976 /* Mark so that if we are in a constructor, and then find that
1977 this field was initialized by a base initializer,
1978 we can emit an error message. */
1979 TREE_USED (value) = 1;
1980 value = build_component_ref (current_class_ref, name, NULL_TREE, 1);
1982 else if ((TREE_CODE (value) == FUNCTION_DECL
1983 && DECL_FUNCTION_MEMBER_P (value))
1984 || (TREE_CODE (value) == OVERLOAD
1985 && DECL_FUNCTION_MEMBER_P (OVL_CURRENT (value))))
1987 tree decl;
1989 if (TREE_CODE (value) == OVERLOAD)
1990 value = OVL_CURRENT (value);
1992 decl = maybe_dummy_object (DECL_CLASS_CONTEXT (value), 0);
1993 value = build_component_ref (decl, name, NULL_TREE, 1);
1995 else if (really_overloaded_fn (value))
1997 else if (TREE_CODE (value) == OVERLOAD)
1998 /* not really overloaded function */
1999 mark_used (OVL_FUNCTION (value));
2000 else if (TREE_CODE (value) == TREE_LIST)
2002 /* Ambiguous reference to base members, possibly other cases?. */
2003 tree t = value;
2004 while (t && TREE_CODE (t) == TREE_LIST)
2006 mark_used (TREE_VALUE (t));
2007 t = TREE_CHAIN (t);
2010 else if (TREE_CODE (value) == NAMESPACE_DECL)
2012 cp_error ("use of namespace `%D' as expression", value);
2013 return error_mark_node;
2015 else if (DECL_CLASS_TEMPLATE_P (value))
2017 cp_error ("use of class template `%T' as expression", value);
2018 return error_mark_node;
2020 else
2021 mark_used (value);
2023 if (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == PARM_DECL
2024 || TREE_CODE (value) == RESULT_DECL)
2026 tree context = decl_function_context (value);
2027 if (context != NULL_TREE && context != current_function_decl
2028 && ! TREE_STATIC (value))
2030 cp_error ("use of %s from containing function",
2031 (TREE_CODE (value) == VAR_DECL
2032 ? "`auto' variable" : "parameter"));
2033 cp_error_at (" `%#D' declared here", value);
2034 value = error_mark_node;
2038 if (TREE_CODE_CLASS (TREE_CODE (value)) == 'd' && DECL_NONLOCAL (value))
2040 if (DECL_LANG_SPECIFIC (value)
2041 && DECL_CLASS_CONTEXT (value) != current_class_type)
2043 tree path;
2044 register tree context
2045 = (TREE_CODE (value) == FUNCTION_DECL && DECL_VIRTUAL_P (value))
2046 ? DECL_CLASS_CONTEXT (value)
2047 : DECL_CONTEXT (value);
2049 get_base_distance (context, current_class_type, 0, &path);
2050 if (path && !enforce_access (current_class_type, value))
2051 return error_mark_node;
2054 else if (TREE_CODE (value) == TREE_LIST
2055 && TREE_TYPE (value) == error_mark_node)
2057 error ("request for member `%s' is ambiguous in multiple inheritance lattice",
2058 IDENTIFIER_POINTER (name));
2059 print_candidates (value);
2060 return error_mark_node;
2063 if (! processing_template_decl)
2064 value = convert_from_reference (value);
2065 return value;
2069 tree
2070 make_thunk (function, delta)
2071 tree function;
2072 int delta;
2074 tree thunk_id;
2075 tree thunk;
2076 tree func_decl;
2078 if (TREE_CODE (function) != ADDR_EXPR)
2079 abort ();
2080 func_decl = TREE_OPERAND (function, 0);
2081 if (TREE_CODE (func_decl) != FUNCTION_DECL)
2082 abort ();
2084 OB_INIT ();
2085 OB_PUTS ("__thunk_");
2086 if (delta > 0)
2088 OB_PUTC ('n');
2089 icat (delta);
2091 else
2092 icat (-delta);
2093 OB_PUTC ('_');
2094 OB_PUTID (DECL_ASSEMBLER_NAME (func_decl));
2095 OB_FINISH ();
2096 thunk_id = get_identifier (obstack_base (&scratch_obstack));
2098 thunk = IDENTIFIER_GLOBAL_VALUE (thunk_id);
2099 if (thunk && TREE_CODE (thunk) != THUNK_DECL)
2101 cp_error ("implementation-reserved name `%D' used", thunk_id);
2102 thunk = NULL_TREE;
2103 SET_IDENTIFIER_GLOBAL_VALUE (thunk_id, thunk);
2105 if (thunk == NULL_TREE)
2107 thunk = build_decl (FUNCTION_DECL, thunk_id, TREE_TYPE (func_decl));
2108 TREE_READONLY (thunk) = TREE_READONLY (func_decl);
2109 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (func_decl);
2110 comdat_linkage (thunk);
2111 TREE_SET_CODE (thunk, THUNK_DECL);
2112 DECL_INITIAL (thunk) = function;
2113 THUNK_DELTA (thunk) = delta;
2114 DECL_EXTERNAL (thunk) = 1;
2115 DECL_ARTIFICIAL (thunk) = 1;
2116 /* So that finish_file can write out any thunks that need to be: */
2117 pushdecl_top_level (thunk);
2119 return thunk;
2122 /* Emit the definition of a C++ multiple inheritance vtable thunk. */
2124 void
2125 emit_thunk (thunk_fndecl)
2126 tree thunk_fndecl;
2128 tree function = TREE_OPERAND (DECL_INITIAL (thunk_fndecl), 0);
2129 int delta = THUNK_DELTA (thunk_fndecl);
2131 if (TREE_ASM_WRITTEN (thunk_fndecl))
2132 return;
2134 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
2136 TREE_ADDRESSABLE (function) = 1;
2137 mark_used (function);
2139 if (current_function_decl)
2140 abort ();
2142 TREE_SET_CODE (thunk_fndecl, FUNCTION_DECL);
2144 #ifdef ASM_OUTPUT_MI_THUNK
2145 if (!flag_syntax_only)
2147 char *fnname;
2148 current_function_decl = thunk_fndecl;
2149 /* Make sure we build up its RTL before we go onto the
2150 temporary obstack. */
2151 make_function_rtl (thunk_fndecl);
2152 temporary_allocation ();
2153 DECL_RESULT (thunk_fndecl)
2154 = build_decl (RESULT_DECL, 0, integer_type_node);
2155 fnname = XSTR (XEXP (DECL_RTL (thunk_fndecl), 0), 0);
2156 init_function_start (thunk_fndecl, input_filename, lineno);
2157 current_function_is_thunk = 1;
2158 assemble_start_function (thunk_fndecl, fnname);
2159 ASM_OUTPUT_MI_THUNK (asm_out_file, thunk_fndecl, delta, function);
2160 assemble_end_function (thunk_fndecl, fnname);
2161 permanent_allocation (1);
2162 current_function_decl = 0;
2163 current_function = 0;
2165 #else /* ASM_OUTPUT_MI_THUNK */
2167 /* If we don't have the necessary macro for efficient thunks, generate a
2168 thunk function that just makes a call to the real function.
2169 Unfortunately, this doesn't work for varargs. */
2171 tree a, t;
2173 if (varargs_function_p (function))
2174 cp_error ("generic thunk code fails for method `%#D' which uses `...'",
2175 function);
2177 /* Set up clone argument trees for the thunk. */
2178 t = NULL_TREE;
2179 for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
2181 tree x = copy_node (a);
2182 TREE_CHAIN (x) = t;
2183 DECL_CONTEXT (x) = thunk_fndecl;
2184 t = x;
2186 a = nreverse (t);
2187 DECL_ARGUMENTS (thunk_fndecl) = a;
2188 DECL_RESULT (thunk_fndecl) = NULL_TREE;
2189 DECL_LANG_SPECIFIC (thunk_fndecl) = DECL_LANG_SPECIFIC (function);
2190 copy_lang_decl (thunk_fndecl);
2191 DECL_INTERFACE_KNOWN (thunk_fndecl) = 1;
2192 DECL_NOT_REALLY_EXTERN (thunk_fndecl) = 1;
2194 start_function (NULL_TREE, thunk_fndecl, NULL_TREE,
2195 SF_DEFAULT | SF_PRE_PARSED);
2196 store_parm_decls ();
2197 current_function_is_thunk = 1;
2199 /* Build up the call to the real function. */
2200 t = build_int_2 (delta, -1 * (delta < 0));
2201 TREE_TYPE (t) = signed_type (sizetype);
2202 t = fold (build (PLUS_EXPR, TREE_TYPE (a), a, t));
2203 t = tree_cons (NULL_TREE, t, NULL_TREE);
2204 for (a = TREE_CHAIN (a); a; a = TREE_CHAIN (a))
2205 t = tree_cons (NULL_TREE, a, t);
2206 t = nreverse (t);
2207 t = build_call (function, TREE_TYPE (TREE_TYPE (function)), t);
2208 finish_return_stmt (t);
2210 expand_body (finish_function (lineno, 0));
2212 /* Don't let the backend defer this function. */
2213 if (DECL_DEFER_OUTPUT (thunk_fndecl))
2215 output_inline_function (thunk_fndecl);
2216 permanent_allocation (1);
2219 #endif /* ASM_OUTPUT_MI_THUNK */
2221 TREE_SET_CODE (thunk_fndecl, THUNK_DECL);
2224 /* Code for synthesizing methods which have default semantics defined. */
2226 /* Generate code for default X(X&) constructor. */
2228 static void
2229 do_build_copy_constructor (fndecl)
2230 tree fndecl;
2232 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
2233 tree t;
2235 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
2236 parm = TREE_CHAIN (parm);
2237 parm = convert_from_reference (parm);
2239 if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type)
2240 && is_empty_class (current_class_type))
2241 /* Don't copy the padding byte; it might not have been allocated
2242 if *this is a base subobject. */;
2243 else if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
2245 t = build (INIT_EXPR, void_type_node, current_class_ref, parm);
2246 finish_expr_stmt (t);
2248 else
2250 tree fields = TYPE_FIELDS (current_class_type);
2251 int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
2252 tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
2253 int i;
2255 /* Initialize all the base-classes. */
2256 for (t = CLASSTYPE_VBASECLASSES (current_class_type); t;
2257 t = TREE_CHAIN (t))
2258 current_base_init_list
2259 = tree_cons (BINFO_TYPE (t), parm, current_base_init_list);
2260 for (i = 0; i < n_bases; ++i)
2262 t = TREE_VEC_ELT (binfos, i);
2263 if (TREE_VIA_VIRTUAL (t))
2264 continue;
2266 current_base_init_list
2267 = tree_cons (BINFO_TYPE (t), parm, current_base_init_list);
2270 for (; fields; fields = TREE_CHAIN (fields))
2272 tree init, t;
2273 tree field = fields;
2275 if (TREE_CODE (field) != FIELD_DECL)
2276 continue;
2278 init = parm;
2279 if (DECL_NAME (field))
2281 if (VFIELD_NAME_P (DECL_NAME (field)))
2282 continue;
2283 if (VBASE_NAME_P (DECL_NAME (field)))
2284 continue;
2286 /* True for duplicate members. */
2287 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
2288 continue;
2290 else if ((t = TREE_TYPE (field)) != NULL_TREE
2291 && ANON_AGGR_TYPE_P (t)
2292 && TYPE_FIELDS (t) != NULL_TREE)
2293 /* Just use the field; anonymous types can't have
2294 nontrivial copy ctors or assignment ops. */;
2295 else
2296 continue;
2298 init = build (COMPONENT_REF, TREE_TYPE (field), init, field);
2299 init = build_tree_list (NULL_TREE, init);
2301 current_member_init_list
2302 = tree_cons (field, init, current_member_init_list);
2304 current_member_init_list = nreverse (current_member_init_list);
2305 current_base_init_list = nreverse (current_base_init_list);
2306 setup_vtbl_ptr ();
2310 static void
2311 do_build_assign_ref (fndecl)
2312 tree fndecl;
2314 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
2315 tree compound_stmt;
2317 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
2318 parm = convert_from_reference (parm);
2320 if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type)
2321 && is_empty_class (current_class_type))
2322 /* Don't copy the padding byte; it might not have been allocated
2323 if *this is a base subobject. */;
2324 else if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
2326 tree t = build (MODIFY_EXPR, void_type_node, current_class_ref, parm);
2327 finish_expr_stmt (t);
2329 else
2331 tree fields = TYPE_FIELDS (current_class_type);
2332 int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
2333 tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
2334 int i;
2336 for (i = 0; i < n_bases; ++i)
2338 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2339 tree p = convert_to_reference
2340 (build_reference_type (basetype), parm,
2341 CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
2342 p = convert_from_reference (p);
2343 p = build_member_call (basetype, ansi_opname [MODIFY_EXPR],
2344 build_expr_list (NULL_TREE, p));
2345 finish_expr_stmt (p);
2347 for (; fields; fields = TREE_CHAIN (fields))
2349 tree comp, init, t;
2350 tree field = fields;
2352 if (TREE_CODE (field) != FIELD_DECL)
2353 continue;
2355 if (CP_TYPE_CONST_P (TREE_TYPE (field)))
2357 if (DECL_NAME (field))
2358 cp_error ("non-static const member `%#D', can't use default assignment operator", field);
2359 else
2360 cp_error ("non-static const member in type `%T', can't use default assignment operator", current_class_type);
2361 continue;
2363 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
2365 if (DECL_NAME (field))
2366 cp_error ("non-static reference member `%#D', can't use default assignment operator", field);
2367 else
2368 cp_error ("non-static reference member in type `%T', can't use default assignment operator", current_class_type);
2369 continue;
2372 comp = current_class_ref;
2373 init = parm;
2375 if (DECL_NAME (field))
2377 if (VFIELD_NAME_P (DECL_NAME (field)))
2378 continue;
2379 if (VBASE_NAME_P (DECL_NAME (field)))
2380 continue;
2382 /* True for duplicate members. */
2383 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
2384 continue;
2386 else if ((t = TREE_TYPE (field)) != NULL_TREE
2387 && ANON_AGGR_TYPE_P (t)
2388 && TYPE_FIELDS (t) != NULL_TREE)
2389 /* Just use the field; anonymous types can't have
2390 nontrivial copy ctors or assignment ops. */;
2391 else
2392 continue;
2394 comp = build (COMPONENT_REF, TREE_TYPE (field), comp, field);
2395 init = build (COMPONENT_REF, TREE_TYPE (field), init, field);
2397 finish_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
2400 finish_return_stmt (current_class_ref);
2401 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
2404 void
2405 synthesize_method (fndecl)
2406 tree fndecl;
2408 int nested = (current_function_decl != NULL_TREE);
2409 tree context = hack_decl_function_context (fndecl);
2410 int need_body = 1;
2412 if (at_eof)
2413 import_export_decl (fndecl);
2415 if (! context)
2416 push_to_top_level ();
2417 else if (nested)
2418 push_function_context_to (context);
2420 /* Put the function definition at the position where it is needed,
2421 rather than within the body of the class. That way, an error
2422 during the generation of the implicit body points at the place
2423 where the attempt to generate the function occurs, giving the
2424 user a hint as to why we are attempting to generate the
2425 function. */
2426 DECL_SOURCE_LINE (fndecl) = lineno;
2427 DECL_SOURCE_FILE (fndecl) = input_filename;
2429 interface_unknown = 1;
2430 start_function (NULL_TREE, fndecl, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED);
2431 store_parm_decls ();
2432 clear_last_expr ();
2434 if (DECL_NAME (fndecl) == ansi_opname[MODIFY_EXPR])
2436 do_build_assign_ref (fndecl);
2437 need_body = 0;
2439 else if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2440 setup_vtbl_ptr ();
2441 else
2443 tree arg_chain = FUNCTION_ARG_CHAIN (fndecl);
2444 if (DECL_CONSTRUCTOR_FOR_VBASE_P (fndecl))
2445 arg_chain = TREE_CHAIN (arg_chain);
2446 if (arg_chain != void_list_node)
2447 do_build_copy_constructor (fndecl);
2448 else if (TYPE_NEEDS_CONSTRUCTING (current_class_type))
2449 setup_vtbl_ptr ();
2452 /* If we haven't yet generated the body of the function, just
2453 generate an empty compound statement. */
2454 if (need_body)
2456 tree compound_stmt;
2457 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
2458 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
2461 expand_body (finish_function (lineno, 0));
2463 extract_interface_info ();
2464 if (! context)
2465 pop_from_top_level ();
2466 else if (nested)
2467 pop_function_context_from (context);