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