OSDN Git Service

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