OSDN Git Service

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