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