OSDN Git Service

92th Cygnus<->FSF merge
[pf3gnuchains/gcc-fork.git] / gcc / cp / method.c
1 /* Handle the hair of processing (but not expanding) inline functions.
2    Also manage function and variable name overloading.
3    Copyright (C) 1987, 89, 92, 93, 94, 95, 1996 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 PARM_CAN_BE_ARRAY_TYPE
25 #define PARM_CAN_BE_ARRAY_TYPE 1
26 #endif
27
28 /* Handle method declarations.  */
29 #include <stdio.h>
30 #include "config.h"
31 #include "tree.h"
32 #include "cp-tree.h"
33 #include "class.h"
34 #include "obstack.h"
35 #include <ctype.h>
36 #include "rtl.h"
37 #include "expr.h"
38 #include "output.h"
39 #include "hard-reg-set.h"
40 #include "flags.h"
41
42 /* TREE_LIST of the current inline functions that need to be
43    processed.  */
44 struct pending_inline *pending_inlines;
45
46 #define obstack_chunk_alloc xmalloc
47 #define obstack_chunk_free free
48
49 /* Obstack where we build text strings for overloading, etc.  */
50 static struct obstack scratch_obstack;
51 static char *scratch_firstobj;
52
53 # define OB_INIT() (scratch_firstobj ? (obstack_free (&scratch_obstack, scratch_firstobj), 0) : 0)
54 # define OB_PUTC(C) (obstack_1grow (&scratch_obstack, (C)))
55 # define OB_PUTC2(C1,C2)        \
56   (obstack_1grow (&scratch_obstack, (C1)), obstack_1grow (&scratch_obstack, (C2)))
57 # define OB_PUTS(S) (obstack_grow (&scratch_obstack, (S), sizeof (S) - 1))
58 # define OB_PUTID(ID)  \
59   (obstack_grow (&scratch_obstack, IDENTIFIER_POINTER (ID),     \
60                  IDENTIFIER_LENGTH (ID)))
61 # define OB_PUTCP(S) (obstack_grow (&scratch_obstack, (S), strlen (S)))
62 # define OB_FINISH() (obstack_1grow (&scratch_obstack, '\0'))
63 # define OB_LAST() (obstack_next_free (&scratch_obstack)[-1])
64
65 void
66 init_method ()
67 {
68   gcc_obstack_init (&scratch_obstack);
69   scratch_firstobj = (char *)obstack_alloc (&scratch_obstack, 0);
70 }
71
72 /* This must be large enough to hold any printed integer or floating-point
73    value.  */
74 static char digit_buffer[128];
75
76 /* Move inline function definitions out of structure so that they
77    can be processed normally.  CNAME is the name of the class
78    we are working from, METHOD_LIST is the list of method lists
79    of the structure.  We delete friend methods here, after
80    saving away their inline function definitions (if any).  */
81
82 void
83 do_inline_function_hair (type, friend_list)
84      tree type, friend_list;
85 {
86   tree method = TYPE_METHODS (type);
87
88   if (method && TREE_CODE (method) == TREE_VEC)
89     {
90       if (TREE_VEC_ELT (method, 1))
91         method = TREE_VEC_ELT (method, 1);
92       else if (TREE_VEC_ELT (method, 0))
93         method = TREE_VEC_ELT (method, 0);
94       else
95         method = TREE_VEC_ELT (method, 2);
96     }
97
98   while (method)
99     {
100       /* Do inline member functions.  */
101       struct pending_inline *info = DECL_PENDING_INLINE_INFO (method);
102       if (info)
103         {
104           tree args;
105
106           my_friendly_assert (info->fndecl == method, 238);
107           args = DECL_ARGUMENTS (method);
108           while (args)
109             {
110               DECL_CONTEXT (args) = method;
111               args = TREE_CHAIN (args);
112             }
113
114           /* Allow this decl to be seen in global scope.  Don't do this for
115              local class methods, though.  */
116           if (! current_function_decl)
117             IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (method)) = method;
118         }
119       method = TREE_CHAIN (method);
120     }
121   while (friend_list)
122     {
123       tree fndecl = TREE_VALUE (friend_list);
124       struct pending_inline *info = DECL_PENDING_INLINE_INFO (fndecl);
125       if (info)
126         {
127           tree args;
128
129           my_friendly_assert (info->fndecl == fndecl, 239);
130           args = DECL_ARGUMENTS (fndecl);
131           while (args)
132             {
133               DECL_CONTEXT (args) = fndecl;
134               args = TREE_CHAIN (args);
135             }
136
137           /* Allow this decl to be seen in global scope */
138           if (! current_function_decl)
139             IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (fndecl)) = fndecl;
140         }
141
142       friend_list = TREE_CHAIN (friend_list);
143     }
144 }
145 \f
146 /* Report an argument type mismatch between the best declared function
147    we could find and the current argument list that we have.  */
148
149 void
150 report_type_mismatch (cp, parmtypes, name_kind)
151      struct candidate *cp;
152      tree parmtypes;
153      char *name_kind;
154 {
155   int i = cp->u.bad_arg;
156   tree ttf, tta;
157   char *tmp_firstobj;
158
159   switch (i)
160     {
161     case -4:
162       my_friendly_assert (TREE_CODE (cp->function) == TEMPLATE_DECL, 240);
163       cp_error ("type unification failed for function template `%#D'",
164                 cp->function);
165       return;
166
167     case -2:
168       cp_error ("too few arguments for %s `%#D'", name_kind, cp->function);
169       return;
170     case -1:
171       cp_error ("too many arguments for %s `%#D'", name_kind, cp->function);
172       return;
173     case 0:
174       if (TREE_CODE (TREE_TYPE (cp->function)) != METHOD_TYPE)
175         break;
176     case -3:
177       /* Happens when the implicit object parameter is rejected.  */
178       my_friendly_assert (! TYPE_READONLY (TREE_TYPE (TREE_VALUE (parmtypes))),
179                           241);
180       if (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (TREE_VALUE (parmtypes))))
181           && ! TYPE_VOLATILE (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (cp->function))))))
182         cp_error ("call to non-volatile %s `%#D' with volatile object",
183                   name_kind, cp->function);
184       else
185         cp_error ("call to non-const %s `%#D' with const object",
186                   name_kind, cp->function);
187       return;
188     }
189
190   ttf = TYPE_ARG_TYPES (TREE_TYPE (cp->function));
191   tta = parmtypes;
192
193   while (i-- > 0)
194     {
195       ttf = TREE_CHAIN (ttf);
196       tta = TREE_CHAIN (tta);
197     }
198
199   OB_INIT ();
200   OB_PUTS ("bad argument ");
201   sprintf (digit_buffer, "%d", cp->u.bad_arg
202            - (TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
203            + 1);
204   OB_PUTCP (digit_buffer);
205
206   OB_PUTS (" for function `");
207   OB_PUTCP (decl_as_string (cp->function, 1));
208   OB_PUTS ("' (type was ");
209
210   /* Reset `i' so that type printing routines do the right thing.  */
211   if (tta)
212     {
213       enum tree_code code = TREE_CODE (TREE_TYPE (TREE_VALUE (tta)));
214       if (code == ERROR_MARK)
215         OB_PUTS ("(failed type instantiation)");
216       else
217         {
218           i = (code == FUNCTION_TYPE || code == METHOD_TYPE);
219           OB_PUTCP (type_as_string (TREE_TYPE (TREE_VALUE (tta)), 1));
220         }
221     }
222   else OB_PUTS ("void");
223   OB_PUTC (')');
224   OB_FINISH ();
225
226   tmp_firstobj = (char *)alloca (obstack_object_size (&scratch_obstack));
227   bcopy (obstack_base (&scratch_obstack), tmp_firstobj,
228          obstack_object_size (&scratch_obstack));
229   error (tmp_firstobj);
230 }
231 \f
232 /* Here is where overload code starts.  */
233
234 /* Array of types seen so far in top-level call to `build_overload_name'.
235    Allocated and deallocated by caller.  */
236 static tree *typevec;
237
238 /* Number of types interned by `build_overload_name' so far.  */
239 static int maxtype;
240
241 /* Number of occurrences of last type seen.  */
242 static int nrepeats;
243
244 /* Nonzero if we should not try folding parameter types.  */
245 static int nofold;
246
247 #define ALLOCATE_TYPEVEC(PARMTYPES) \
248   do { maxtype = 0, nrepeats = 0; \
249        typevec = (tree *)alloca (list_length (PARMTYPES) * sizeof (tree)); } while (0)
250
251 #define DEALLOCATE_TYPEVEC(PARMTYPES) \
252   do { tree t = (PARMTYPES); \
253        while (t) { TREE_USED (TREE_VALUE (t)) = 0; t = TREE_CHAIN (t); } \
254   } while (0)
255
256 /* Code to concatenate an asciified integer to a string.  */
257
258 static
259 #ifdef __GNUC__
260 __inline
261 #endif
262 void
263 icat (i)
264      HOST_WIDE_INT i;
265 {
266   unsigned HOST_WIDE_INT ui;
267
268   /* Handle this case first, to go really quickly.  For many common values,
269      the result of ui/10 below is 1.  */
270   if (i == 1)
271     {
272       OB_PUTC ('1');
273       return;
274     }
275
276   if (i >= 0)
277     ui = i;
278   else
279     {
280       OB_PUTC ('m');
281       ui = -i;
282     }
283
284   if (ui >= 10)
285     icat (ui / 10);
286
287   OB_PUTC ('0' + (ui % 10));
288 }
289
290 static void
291 dicat (lo, hi)
292      HOST_WIDE_INT lo, hi;
293 {
294   unsigned HOST_WIDE_INT ulo, uhi, qlo, qhi;
295
296   if (hi >= 0)
297     {
298       uhi = hi;
299       ulo = lo;
300     }
301   else
302     {
303       uhi = (lo == 0 ? -hi : -hi-1);
304       ulo = -lo;
305     }
306   if (uhi == 0
307       && ulo < ((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)))
308     {
309       icat (ulo);
310       return;
311     }
312   /* Divide 2^HOST_WIDE_INT*uhi+ulo by 10. */
313   qhi = uhi / 10;
314   uhi = uhi % 10;
315   qlo = uhi * (((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)) / 5);
316   qlo += ulo / 10;
317   ulo = ulo % 10;
318   ulo += uhi * (((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)) % 5)
319          * 2;
320   qlo += ulo / 10;
321   ulo = ulo % 10;
322   /* Quotient is 2^HOST_WIDE_INT*qhi+qlo, remainder is ulo. */
323   dicat (qlo, qhi);
324   OB_PUTC ('0' + ulo);
325 }
326
327 static
328 #ifdef __GNUC__
329 __inline
330 #endif
331 void
332 flush_repeats (type)
333      tree type;
334 {
335   int tindex = 0;
336
337   while (typevec[tindex] != type)
338     tindex++;
339
340   if (nrepeats > 1)
341     {
342       OB_PUTC ('N');
343       icat (nrepeats);
344       if (nrepeats > 9)
345         OB_PUTC ('_');
346     }
347   else
348     OB_PUTC ('T');
349   nrepeats = 0;
350   icat (tindex);
351   if (tindex > 9)
352     OB_PUTC ('_');
353 }
354
355 static int numeric_output_need_bar;
356 static void build_overload_identifier ();
357
358 static void
359 build_overload_nested_name (decl)
360      tree decl;
361 {
362   if (DECL_CONTEXT (decl))
363     {
364       tree context = DECL_CONTEXT (decl);
365       if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
366         context = TYPE_NAME (context);
367       build_overload_nested_name (context);
368     }
369
370   if (TREE_CODE (decl) == FUNCTION_DECL)
371     {
372       tree name = DECL_ASSEMBLER_NAME (decl);
373       char *label;
374       extern int var_labelno;
375
376       ASM_FORMAT_PRIVATE_NAME (label, IDENTIFIER_POINTER (name), var_labelno);
377       var_labelno++;
378
379       if (numeric_output_need_bar)
380         {
381           OB_PUTC ('_');
382           numeric_output_need_bar = 0;
383         }
384       icat (strlen (label));
385       OB_PUTCP (label);
386     }
387   else                          /* TYPE_DECL */
388     build_overload_identifier (decl);
389 }
390
391 /* Encoding for an INTEGER_CST value.  */
392
393 static void
394 build_overload_int (value)
395      tree value;
396 {
397   if (TREE_CODE (value) == TEMPLATE_CONST_PARM)
398     {
399       OB_PUTC ('Y');
400       if (TEMPLATE_CONST_IDX (value) > 9)
401         OB_PUTC ('_');
402       icat (TEMPLATE_CONST_IDX (value)); 
403       if (TEMPLATE_CONST_IDX (value) > 9)
404         OB_PUTC ('_');
405       return;
406     }
407   else if (processing_template_decl
408            && TREE_CODE (value) != INTEGER_CST)
409     /* We don't ever want this output, but it's inconvenient not to
410        be able to build the string.  This should cause assembler
411        errors we'll notice.  */
412     {
413       static int n;
414       sprintf (digit_buffer, " *%d", n++);
415       OB_PUTCP (digit_buffer);
416       return;
417     }
418
419   my_friendly_assert (TREE_CODE (value) == INTEGER_CST, 243);
420   if (TYPE_PRECISION (TREE_TYPE (value)) == 2 * HOST_BITS_PER_WIDE_INT)
421     {
422       if (TREE_INT_CST_HIGH (value)
423           != (TREE_INT_CST_LOW (value) >> (HOST_BITS_PER_WIDE_INT - 1)))
424         {
425           /* need to print a DImode value in decimal */
426           dicat (TREE_INT_CST_LOW (value), TREE_INT_CST_HIGH (value));
427           return;
428         }
429       /* else fall through to print in smaller mode */
430     }
431   /* Wordsize or smaller */
432   icat (TREE_INT_CST_LOW (value));
433 }
434
435 static void
436 build_overload_value (type, value)
437      tree type, value;
438 {
439   while (TREE_CODE (value) == NON_LVALUE_EXPR
440          || TREE_CODE (value) == NOP_EXPR)
441     value = TREE_OPERAND (value, 0);
442   my_friendly_assert (TREE_CODE (type) == PARM_DECL, 242);
443   type = TREE_TYPE (type);
444
445   if (numeric_output_need_bar)
446     {
447       OB_PUTC ('_');
448       numeric_output_need_bar = 0;
449     }
450
451   if (TREE_CODE (type) == POINTER_TYPE
452       && TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE)
453     {
454       /* Handle a pointer to data member as a template instantiation
455          parameter, boy, what fun!  */
456       type = integer_type_node;
457       if (TREE_CODE (value) != INTEGER_CST)
458         {
459           sorry ("unknown pointer to member constant");
460           return;
461         }
462     }
463
464   if (TYPE_PTRMEMFUNC_P (type))
465     type = TYPE_PTRMEMFUNC_FN_TYPE (type);
466
467   switch (TREE_CODE (type))
468     {
469     case INTEGER_TYPE:
470     case ENUMERAL_TYPE:
471     case BOOLEAN_TYPE:
472       {
473         build_overload_int (value);
474         numeric_output_need_bar = 1;
475         return;
476       }
477     case REAL_TYPE:
478       {
479         REAL_VALUE_TYPE val;
480         char *bufp = digit_buffer;
481         extern char *index ();
482
483         pedwarn ("ANSI C++ forbids floating-point template arguments");
484
485         my_friendly_assert (TREE_CODE (value) == REAL_CST, 244);
486         val = TREE_REAL_CST (value);
487         if (REAL_VALUE_ISNAN (val))
488           {
489             sprintf (bufp, "NaN");
490           }
491         else
492           {
493             if (REAL_VALUE_NEGATIVE (val))
494               {
495                 val = REAL_VALUE_NEGATE (val);
496                 *bufp++ = 'm';
497               }
498             if (REAL_VALUE_ISINF (val))
499               {
500                 sprintf (bufp, "Infinity");
501               }
502             else
503               {
504                 ereal_to_decimal (val, bufp);
505                 bufp = (char *) index (bufp, 'e');
506                 if (!bufp)
507                   strcat (digit_buffer, "e0");
508                 else
509                   {
510                     char *p;
511                     bufp++;
512                     if (*bufp == '-')
513                       {
514                         *bufp++ = 'm';
515                       }
516                     p = bufp;
517                     if (*p == '+')
518                       p++;
519                     while (*p == '0')
520                       p++;
521                     if (*p == 0)
522                       {
523                         *bufp++ = '0';
524                         *bufp = 0;
525                       }
526                     else if (p != bufp)
527                       {
528                         while (*p)
529                           *bufp++ = *p++;
530                         *bufp = 0;
531                       }
532                   }
533 #ifdef NO_DOT_IN_LABEL
534                 bufp = (char *) index (bufp, '.');
535                 if (bufp)
536                   *bufp = '_';
537 #endif
538               }
539           }
540         OB_PUTCP (digit_buffer);
541         numeric_output_need_bar = 1;
542         return;
543       }
544     case POINTER_TYPE:
545       if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
546           && TREE_CODE (value) != ADDR_EXPR)
547         {
548           if (TREE_CODE (value) == CONSTRUCTOR)
549             {
550               /* This is dangerous code, crack built up pointer to members.  */
551               tree args = CONSTRUCTOR_ELTS (value);
552               tree a1 = TREE_VALUE (args);
553               tree a2 = TREE_VALUE (TREE_CHAIN (args));
554               tree a3 = CONSTRUCTOR_ELTS (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args))));
555               a3 = TREE_VALUE (a3);
556               STRIP_NOPS (a3);
557               if (TREE_CODE (a1) == INTEGER_CST
558                   && TREE_CODE (a2) == INTEGER_CST)
559                 {
560                   build_overload_int (a1);
561                   OB_PUTC ('_');
562                   build_overload_int (a2);
563                   OB_PUTC ('_');
564                   if (TREE_CODE (a3) == ADDR_EXPR)
565                     {
566                       a3 = TREE_OPERAND (a3, 0);
567                       if (TREE_CODE (a3) == FUNCTION_DECL)
568                         {
569                           numeric_output_need_bar = 0;
570                           build_overload_identifier (DECL_ASSEMBLER_NAME (a3));
571                           return;
572                         }
573                     }
574                   else if (TREE_CODE (a3) == INTEGER_CST)
575                     {
576                       OB_PUTC ('i');
577                       build_overload_int (a3);
578                       numeric_output_need_bar = 1;
579                       return;
580                     }
581                 }
582             }
583           sorry ("template instantiation with pointer to method that is too complex");
584           return;
585         }
586       if (TREE_CODE (value) == INTEGER_CST
587           || TREE_CODE (value) == TEMPLATE_CONST_PARM)
588         {
589           build_overload_int (value);
590           numeric_output_need_bar = 1;
591           return;
592         }
593       value = TREE_OPERAND (value, 0);
594       if (TREE_CODE (value) == VAR_DECL)
595         {
596           my_friendly_assert (DECL_NAME (value) != 0, 245);
597           build_overload_identifier (DECL_ASSEMBLER_NAME (value));
598           return;
599         }
600       else if (TREE_CODE (value) == FUNCTION_DECL)
601         {
602           my_friendly_assert (DECL_NAME (value) != 0, 246);
603           build_overload_identifier (DECL_ASSEMBLER_NAME (value));
604           return;
605         }
606       else
607         my_friendly_abort (71);
608       break; /* not really needed */
609
610     default:
611       sorry ("conversion of %s as template parameter",
612              tree_code_name [(int) TREE_CODE (type)]);
613       my_friendly_abort (72);
614     }
615 }
616
617 static void
618 build_overload_identifier (name)
619      tree name;
620 {
621   if (TREE_CODE (name) == TYPE_DECL
622       && IS_AGGR_TYPE (TREE_TYPE (name))
623       && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (name))
624       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (TREE_TYPE (name))))
625     {
626       tree template, parmlist, arglist, tname;
627       int i, nparms;
628       template = CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (name));
629       arglist = TREE_VALUE (template);
630       template = TREE_PURPOSE (template);
631       tname = DECL_NAME (template);
632       parmlist = DECL_ARGUMENTS (template);
633       nparms = TREE_VEC_LENGTH (parmlist);
634       OB_PUTC ('t');
635       icat (IDENTIFIER_LENGTH (tname));
636       OB_PUTID (tname);
637       icat (nparms);
638       for (i = 0; i < nparms; i++)
639         {
640           tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
641           tree arg = TREE_VEC_ELT (arglist, i);
642           if (TREE_CODE (parm) == TYPE_DECL)
643             {
644               /* This parameter is a type.  */
645               OB_PUTC ('Z');
646               build_overload_name (arg, 0, 0);
647             }
648           else
649             {
650               parm = tsubst (parm, &TREE_VEC_ELT (arglist, 0),
651                              TREE_VEC_LENGTH (arglist), NULL_TREE);
652               /* It's a PARM_DECL.  */
653               build_overload_name (TREE_TYPE (parm), 0, 0);
654               build_overload_value (parm, arg);
655             }
656         }
657     }
658   else
659     {
660       if (TREE_CODE (name) == TYPE_DECL)
661         name = DECL_NAME (name);
662       if (numeric_output_need_bar)
663         {
664           OB_PUTC ('_');
665           numeric_output_need_bar = 0;
666         }
667       icat (IDENTIFIER_LENGTH (name));
668       OB_PUTID (name);
669     }
670 }
671
672 /* Given a list of parameters in PARMTYPES, create an unambiguous
673    overload string. Should distinguish any type that C (or C++) can
674    distinguish. I.e., pointers to functions are treated correctly.
675
676    Caller must deal with whether a final `e' goes on the end or not.
677
678    Any default conversions must take place before this function
679    is called.
680
681    BEGIN and END control initialization and finalization of the
682    obstack where we build the string.  */
683
684 char *
685 build_overload_name (parmtypes, begin, end)
686      tree parmtypes;
687      int begin, end;
688 {
689   int just_one;
690   tree parmtype;
691
692   if (begin) OB_INIT ();
693   numeric_output_need_bar = 0;
694
695   if ((just_one = (TREE_CODE (parmtypes) != TREE_LIST)))
696     {
697       parmtype = parmtypes;
698       goto only_one;
699     }
700
701   while (parmtypes)
702     {
703       parmtype = TREE_VALUE (parmtypes);
704
705     only_one:
706
707       if (! nofold && ! just_one)
708         {
709           /* Every argument gets counted.  */
710           typevec[maxtype++] = parmtype;
711
712           if (TREE_USED (parmtype) && parmtype == typevec[maxtype-2])
713             {
714               nrepeats++;
715               goto next;
716             }
717
718           if (nrepeats)
719             flush_repeats (typevec[maxtype-2]);
720
721           if (TREE_USED (parmtype))
722             {
723 #if 0
724               /* We can turn this on at some point when we want
725                  improved symbol mangling.  */
726               nrepeats++;
727 #else
728               /* This is bug compatible with 2.7.x  */
729               flush_repeats (parmtype);
730 #endif
731               goto next;
732             }
733
734           /* Only cache types which take more than one character.  */
735           if (parmtype != TYPE_MAIN_VARIANT (parmtype)
736               || (TREE_CODE (parmtype) != INTEGER_TYPE
737                   && TREE_CODE (parmtype) != REAL_TYPE))
738             TREE_USED (parmtype) = 1;
739         }
740
741       if (TYPE_PTRMEMFUNC_P (parmtype))
742         parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
743
744       if (TREE_READONLY (parmtype))
745         OB_PUTC ('C');
746       if (TREE_CODE (parmtype) == INTEGER_TYPE
747           && TYPE_MAIN_VARIANT (parmtype) == unsigned_type (TYPE_MAIN_VARIANT (parmtype)))
748         OB_PUTC ('U');
749       if (TYPE_VOLATILE (parmtype))
750         OB_PUTC ('V');
751
752       switch (TREE_CODE (parmtype))
753         {
754         case OFFSET_TYPE:
755           OB_PUTC ('O');
756           build_overload_name (TYPE_OFFSET_BASETYPE (parmtype), 0, 0);
757           OB_PUTC ('_');
758           build_overload_name (TREE_TYPE (parmtype), 0, 0);
759           break;
760
761         case REFERENCE_TYPE:
762           OB_PUTC ('R');
763           goto more;
764
765         case ARRAY_TYPE:
766 #if PARM_CAN_BE_ARRAY_TYPE
767           {
768             tree length;
769
770             OB_PUTC ('A');
771             if (TYPE_DOMAIN (parmtype) == NULL_TREE)
772               error ("pointer or reference to array of unknown bound in parm type");
773             else
774               {
775                 length = array_type_nelts (parmtype);
776                 if (TREE_CODE (length) == INTEGER_CST)
777                   icat (TREE_INT_CST_LOW (length) + 1);
778               }
779             OB_PUTC ('_');
780             goto more;
781           }
782 #else
783           OB_PUTC ('P');
784           goto more;
785 #endif
786
787         case POINTER_TYPE:
788           OB_PUTC ('P');
789         more:
790           build_overload_name (TREE_TYPE (parmtype), 0, 0);
791           break;
792
793         case FUNCTION_TYPE:
794         case METHOD_TYPE:
795           {
796             tree firstarg = TYPE_ARG_TYPES (parmtype);
797             /* Otherwise have to implement reentrant typevecs,
798                unmark and remark types, etc.  */
799             int old_nofold = nofold;
800             nofold = 1;
801
802             if (nrepeats)
803               flush_repeats (typevec[maxtype-1]);
804
805             /* @@ It may be possible to pass a function type in
806                which is not preceded by a 'P'.  */
807             if (TREE_CODE (parmtype) == FUNCTION_TYPE)
808               {
809                 OB_PUTC ('F');
810                 if (firstarg == NULL_TREE)
811                   OB_PUTC ('e');
812                 else if (firstarg == void_list_node)
813                   OB_PUTC ('v');
814                 else
815                   build_overload_name (firstarg, 0, 0);
816               }
817             else
818               {
819                 int constp = TYPE_READONLY (TREE_TYPE (TREE_VALUE (firstarg)));
820                 int volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_VALUE (firstarg)));
821                 OB_PUTC ('M');
822                 firstarg = TREE_CHAIN (firstarg);
823
824                 build_overload_name (TYPE_METHOD_BASETYPE (parmtype), 0, 0);
825                 if (constp)
826                   OB_PUTC ('C');
827                 if (volatilep)
828                   OB_PUTC ('V');
829
830                 /* For cfront 2.0 compatibility.  */
831                 OB_PUTC ('F');
832
833                 if (firstarg == NULL_TREE)
834                   OB_PUTC ('e');
835                 else if (firstarg == void_list_node)
836                   OB_PUTC ('v');
837                 else
838                   build_overload_name (firstarg, 0, 0);
839               }
840
841             /* Separate args from return type.  */
842             OB_PUTC ('_');
843             build_overload_name (TREE_TYPE (parmtype), 0, 0);
844             nofold = old_nofold;
845             break;
846           }
847
848         case INTEGER_TYPE:
849           parmtype = TYPE_MAIN_VARIANT (parmtype);
850           if (parmtype == integer_type_node
851               || parmtype == unsigned_type_node)
852             OB_PUTC ('i');
853           else if (parmtype == long_integer_type_node
854                    || parmtype == long_unsigned_type_node)
855             OB_PUTC ('l');
856           else if (parmtype == short_integer_type_node
857                    || parmtype == short_unsigned_type_node)
858             OB_PUTC ('s');
859           else if (parmtype == signed_char_type_node)
860             {
861               OB_PUTC ('S');
862               OB_PUTC ('c');
863             }
864           else if (parmtype == char_type_node
865                    || parmtype == unsigned_char_type_node)
866             OB_PUTC ('c');
867           else if (parmtype == wchar_type_node)
868             OB_PUTC ('w');
869           else if (parmtype == long_long_integer_type_node
870               || parmtype == long_long_unsigned_type_node)
871             OB_PUTC ('x');
872 #if 0
873           /* it would seem there is no way to enter these in source code,
874              yet.  (mrs) */
875           else if (parmtype == long_long_long_integer_type_node
876               || parmtype == long_long_long_unsigned_type_node)
877             OB_PUTC ('q');
878 #endif
879           else
880             my_friendly_abort (73);
881           break;
882
883         case BOOLEAN_TYPE:
884           OB_PUTC ('b');
885           break;
886
887         case REAL_TYPE:
888           parmtype = TYPE_MAIN_VARIANT (parmtype);
889           if (parmtype == long_double_type_node)
890             OB_PUTC ('r');
891           else if (parmtype == double_type_node)
892             OB_PUTC ('d');
893           else if (parmtype == float_type_node)
894             OB_PUTC ('f');
895           else my_friendly_abort (74);
896           break;
897
898         case COMPLEX_TYPE:
899           OB_PUTC ('J');
900           build_overload_name (TREE_TYPE (parmtype), 0, 0);
901           break;
902
903         case VOID_TYPE:
904           if (! just_one)
905             {
906 #if 0
907               extern tree void_list_node;
908
909               /* See if anybody is wasting memory.  */
910               my_friendly_assert (parmtypes == void_list_node, 247);
911 #endif
912               /* This is the end of a parameter list.  */
913               if (end) OB_FINISH ();
914               return (char *)obstack_base (&scratch_obstack);
915             }
916           OB_PUTC ('v');
917           break;
918
919         case ERROR_MARK:        /* not right, but nothing is anyway */
920           break;
921
922           /* have to do these */
923         case UNION_TYPE:
924         case RECORD_TYPE:
925           if (! just_one)
926             /* Make this type signature look incompatible
927                with AT&T.  */
928             OB_PUTC ('G');
929           goto common;
930         case ENUMERAL_TYPE:
931         common:
932           {
933             tree name = TYPE_NAME (parmtype);
934             int i = 1;
935
936             if (TREE_CODE (name) == TYPE_DECL)
937               {
938                 tree context = name;
939
940                 /* If DECL_ASSEMBLER_NAME has been set properly, use it.  */
941                 if (DECL_ASSEMBLER_NAME (context) != DECL_NAME (context))
942                   {
943                     OB_PUTID (DECL_ASSEMBLER_NAME (context));
944                     break;
945                   }
946                 while (DECL_CONTEXT (context))
947                   {
948                     i += 1;
949                     context = DECL_CONTEXT (context);
950                     if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
951                       context = TYPE_NAME (context);
952                   }
953                 name = DECL_NAME (name);
954               }
955             my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 248);
956             if (i > 1)
957               {
958                 OB_PUTC ('Q');
959                 if (i > 9)
960                   OB_PUTC ('_');
961                 icat (i);
962                 if (i > 9)
963                   OB_PUTC ('_');
964                 numeric_output_need_bar = 0;
965                 build_overload_nested_name (TYPE_NAME (parmtype));
966               }
967             else              
968               build_overload_identifier (TYPE_NAME (parmtype));
969             break;
970           }
971
972         case UNKNOWN_TYPE:
973           /* This will take some work.  */
974           OB_PUTC ('?');
975           break;
976
977         case TEMPLATE_TYPE_PARM:
978           OB_PUTC ('X');
979           if (TEMPLATE_TYPE_IDX (parmtype) > 9)
980             OB_PUTC ('_');
981           icat (TEMPLATE_TYPE_IDX (parmtype)); 
982           if (TEMPLATE_TYPE_IDX (parmtype) > 9)
983             OB_PUTC ('_');
984           break;
985             
986         case TYPENAME_TYPE:
987           /* We don't ever want this output, but it's inconvenient not to
988              be able to build the string.  This should cause assembler
989              errors we'll notice.  */
990           {
991             static int n;
992             sprintf (digit_buffer, " *%d", n++);
993             OB_PUTCP (digit_buffer);
994           }
995           break;
996
997         default:
998           my_friendly_abort (75);
999         }
1000
1001     next:
1002       if (just_one) break;
1003       parmtypes = TREE_CHAIN (parmtypes);
1004     }
1005   if (! just_one)
1006     {
1007       if (nrepeats)
1008         flush_repeats (typevec[maxtype-1]);
1009
1010       /* To get here, parms must end with `...'.  */
1011       OB_PUTC ('e');
1012     }
1013
1014   if (end) OB_FINISH ();
1015   return (char *)obstack_base (&scratch_obstack);
1016 }
1017
1018 tree
1019 build_static_name (basetype, name)
1020   tree basetype, name;
1021 {
1022   char *basename  = build_overload_name (basetype, 1, 1);
1023   char *buf = (char *) alloca (IDENTIFIER_LENGTH (name)
1024                                + sizeof (STATIC_NAME_FORMAT)
1025                                + strlen (basename));
1026   sprintf (buf, STATIC_NAME_FORMAT, basename, IDENTIFIER_POINTER (name));
1027   return get_identifier (buf);
1028 }  
1029 \f
1030 /* Change the name of a function definition so that it may be
1031    overloaded. NAME is the name of the function to overload,
1032    PARMS is the parameter list (which determines what name the
1033    final function obtains).
1034
1035    FOR_METHOD is 1 if this overload is being performed
1036    for a method, rather than a function type.  It is 2 if
1037    this overload is being performed for a constructor.  */
1038
1039 tree
1040 build_decl_overload (dname, parms, for_method)
1041      tree dname;
1042      tree parms;
1043      int for_method;
1044 {
1045   char *name = IDENTIFIER_POINTER (dname);
1046
1047   /* member operators new and delete look like methods at this point.  */
1048   if (! for_method && parms != NULL_TREE && TREE_CODE (parms) == TREE_LIST)
1049     {
1050       if (dname == ansi_opname[(int) DELETE_EXPR])
1051         return get_identifier ("__builtin_delete");
1052       else if (dname == ansi_opname[(int) VEC_DELETE_EXPR])
1053         return get_identifier ("__builtin_vec_delete");
1054       else if (TREE_CHAIN (parms) == void_list_node)
1055         {
1056           if (dname == ansi_opname[(int) NEW_EXPR])
1057             return get_identifier ("__builtin_new");
1058           else if (dname == ansi_opname[(int) VEC_NEW_EXPR])
1059             return get_identifier ("__builtin_vec_new");
1060         }
1061     }
1062
1063   OB_INIT ();
1064   if (for_method != 2)
1065     OB_PUTCP (name);
1066   /* Otherwise, we can divine that this is a constructor,
1067      and figure out its name without any extra encoding.  */
1068
1069   OB_PUTC2 ('_', '_');
1070   if (for_method)
1071     {
1072 #if 0
1073       /* We can get away without doing this.  */
1074       OB_PUTC ('M');
1075 #endif
1076       {
1077         tree this_type = TREE_VALUE (parms);
1078
1079         if (TREE_CODE (this_type) == RECORD_TYPE)  /* a signature pointer */
1080           parms = temp_tree_cons (NULL_TREE, SIGNATURE_TYPE (this_type),
1081                                   TREE_CHAIN (parms));
1082         else
1083           parms = temp_tree_cons (NULL_TREE, TREE_TYPE (this_type),
1084                                   TREE_CHAIN (parms));
1085       }
1086     }
1087   else
1088     OB_PUTC ('F');
1089
1090   if (parms == NULL_TREE)
1091     OB_PUTC2 ('e', '\0');
1092   else if (parms == void_list_node)
1093     OB_PUTC2 ('v', '\0');
1094   else
1095     {
1096       ALLOCATE_TYPEVEC (parms);
1097       nofold = 0;
1098       if (for_method)
1099         {
1100           build_overload_name (TREE_VALUE (parms), 0, 0);
1101
1102           typevec[maxtype++] = TREE_VALUE (parms);
1103           TREE_USED (TREE_VALUE (parms)) = 1;
1104
1105           if (TREE_CHAIN (parms))
1106             build_overload_name (TREE_CHAIN (parms), 0, 1);
1107           else
1108             OB_PUTC2 ('e', '\0');
1109         }
1110       else
1111         build_overload_name (parms, 0, 1);
1112       DEALLOCATE_TYPEVEC (parms);
1113     }
1114   {
1115     tree n = get_identifier (obstack_base (&scratch_obstack));
1116     if (IDENTIFIER_OPNAME_P (dname))
1117       IDENTIFIER_OPNAME_P (n) = 1;
1118     return n;
1119   }
1120 }
1121
1122 /* Build an overload name for the type expression TYPE.  */
1123
1124 tree
1125 build_typename_overload (type)
1126      tree type;
1127 {
1128   tree id;
1129
1130   OB_INIT ();
1131   OB_PUTID (ansi_opname[(int) TYPE_EXPR]);
1132   nofold = 1;
1133   build_overload_name (type, 0, 1);
1134   id = get_identifier (obstack_base (&scratch_obstack));
1135   IDENTIFIER_OPNAME_P (id) = 1;
1136 #if 0
1137   IDENTIFIER_GLOBAL_VALUE (id) = TYPE_MAIN_DECL (type);
1138 #endif
1139   TREE_TYPE (id) = type;
1140   return id;
1141 }
1142
1143 tree
1144 build_overload_with_type (name, type)
1145      tree name, type;
1146 {
1147   OB_INIT ();
1148   OB_PUTID (name);
1149   nofold = 1;
1150
1151   build_overload_name (type, 0, 1);
1152   return get_identifier (obstack_base (&scratch_obstack));
1153 }
1154
1155 tree
1156 get_id_2 (name, name2)
1157      char *name;
1158      tree name2;
1159 {
1160   OB_INIT ();
1161   OB_PUTCP (name);
1162   OB_PUTID (name2);
1163   OB_FINISH ();
1164   return get_identifier (obstack_base (&scratch_obstack));
1165 }
1166 \f
1167 /* Given a tree_code CODE, and some arguments (at least one),
1168    attempt to use an overloaded operator on the arguments.
1169
1170    For unary operators, only the first argument need be checked.
1171    For binary operators, both arguments may need to be checked.
1172
1173    Member functions can convert class references to class pointers,
1174    for one-level deep indirection.  More than that is not supported.
1175    Operators [](), ()(), and ->() must be member functions.
1176
1177    We call function call building calls with LOOKUP_COMPLAIN if they
1178    are our only hope.  This is true when we see a vanilla operator
1179    applied to something of aggregate type.  If this fails, we are free
1180    to return `error_mark_node', because we will have reported the
1181    error.
1182
1183    Operators NEW and DELETE overload in funny ways: operator new takes
1184    a single `size' parameter, and operator delete takes a pointer to the
1185    storage being deleted.  When overloading these operators, success is
1186    assumed.  If there is a failure, report an error message and return
1187    `error_mark_node'.  */
1188
1189 /* NOSTRICT */
1190 tree
1191 build_opfncall (code, flags, xarg1, xarg2, arg3)
1192      enum tree_code code;
1193      int flags;
1194      tree xarg1, xarg2, arg3;
1195 {
1196   tree rval = 0;
1197   tree arg1, arg2;
1198   tree type1, type2, fnname;
1199   tree fields1 = 0, parms = 0;
1200   tree global_fn;
1201   int try_second;
1202   int binary_is_unary;
1203
1204   if (flag_ansi_overloading)
1205     return build_new_op (code, flags, xarg1, xarg2, arg3);
1206
1207   if (xarg1 == error_mark_node)
1208     return error_mark_node;
1209
1210   if (code == COND_EXPR)
1211     {
1212       if (xarg2 == error_mark_node
1213           || arg3 == error_mark_node)
1214         return error_mark_node;
1215     }
1216   if (code == COMPONENT_REF)
1217     if (TREE_CODE (TREE_TYPE (xarg1)) == POINTER_TYPE)
1218       return rval;
1219
1220   /* First, see if we can work with the first argument */
1221   type1 = TREE_TYPE (xarg1);
1222
1223   /* Some tree codes have length > 1, but we really only want to
1224      overload them if their first argument has a user defined type.  */
1225   switch (code)
1226     {
1227     case PREINCREMENT_EXPR:
1228     case PREDECREMENT_EXPR:
1229     case POSTINCREMENT_EXPR:
1230     case POSTDECREMENT_EXPR:
1231     case COMPONENT_REF:
1232       binary_is_unary = 1;
1233       try_second = 0;
1234       break;
1235
1236       /* ARRAY_REFs and CALL_EXPRs must overload successfully.
1237          If they do not, return error_mark_node instead of NULL_TREE.  */
1238     case ARRAY_REF:
1239       if (xarg2 == error_mark_node)
1240         return error_mark_node;
1241     case CALL_EXPR:
1242       rval = error_mark_node;
1243       binary_is_unary = 0;
1244       try_second = 0;
1245       break;
1246
1247     case VEC_NEW_EXPR:
1248     case NEW_EXPR:
1249       {
1250         tree args = tree_cons (NULL_TREE, xarg2, arg3);
1251         fnname = ansi_opname[(int) code];
1252         if (flags & LOOKUP_GLOBAL)
1253           return build_overload_call (fnname, args, flags & LOOKUP_COMPLAIN);
1254
1255         rval = build_method_call
1256           (build_indirect_ref (build1 (NOP_EXPR, xarg1, error_mark_node),
1257                                "new"),
1258            fnname, args, NULL_TREE, flags);
1259         if (rval == error_mark_node)
1260           /* User might declare fancy operator new, but invoke it
1261              like standard one.  */
1262           return rval;
1263
1264         TREE_TYPE (rval) = xarg1;
1265         TREE_CALLS_NEW (rval) = 1;
1266         return rval;
1267       }
1268       break;
1269
1270     case VEC_DELETE_EXPR:
1271     case DELETE_EXPR:
1272       {
1273         fnname = ansi_opname[(int) code];
1274         if (flags & LOOKUP_GLOBAL)
1275           return build_overload_call (fnname,
1276                                       build_tree_list (NULL_TREE, xarg1),
1277                                       flags & LOOKUP_COMPLAIN);
1278         arg1 = TREE_TYPE (xarg1);
1279
1280         /* This handles the case where we're trying to delete
1281            X (*a)[10];
1282            a=new X[5][10];
1283            delete[] a; */
1284            
1285         if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
1286           {
1287             /* Strip off the pointer and the array.  */
1288             arg1 = TREE_TYPE (TREE_TYPE (arg1));
1289
1290             while (TREE_CODE (arg1) == ARRAY_TYPE)
1291                 arg1 = (TREE_TYPE (arg1));
1292
1293             arg1 = build_pointer_type (arg1);
1294           }
1295
1296         rval = build_method_call
1297           (build_indirect_ref (build1 (NOP_EXPR, arg1,
1298                                        error_mark_node),
1299                                NULL_PTR),
1300            fnname, tree_cons (NULL_TREE, xarg1,
1301                                build_tree_list (NULL_TREE, xarg2)),
1302            NULL_TREE, flags);
1303 #if 0
1304         /* This can happen when operator delete is protected.  */
1305         my_friendly_assert (rval != error_mark_node, 250);
1306         TREE_TYPE (rval) = void_type_node;
1307 #endif
1308         return rval;
1309       }
1310       break;
1311
1312     default:
1313       binary_is_unary = 0;
1314       try_second = tree_code_length [(int) code] == 2;
1315       if (try_second && xarg2 == error_mark_node)
1316         return error_mark_node;
1317       break;
1318     }
1319
1320   if (try_second && xarg2 == error_mark_node)
1321     return error_mark_node;
1322
1323   /* What ever it was, we do not know how to deal with it.  */
1324   if (type1 == NULL_TREE)
1325     return rval;
1326
1327   if (TREE_CODE (type1) == OFFSET_TYPE)
1328     type1 = TREE_TYPE (type1);
1329
1330   if (TREE_CODE (type1) == REFERENCE_TYPE)
1331     {
1332       arg1 = convert_from_reference (xarg1);
1333       type1 = TREE_TYPE (arg1);
1334     }
1335   else
1336     {
1337       arg1 = xarg1;
1338     }
1339
1340   if (!IS_AGGR_TYPE (type1) || TYPE_PTRMEMFUNC_P (type1))
1341     {
1342       /* Try to fail. First, fail if unary */
1343       if (! try_second)
1344         return rval;
1345       /* Second, see if second argument is non-aggregate.  */
1346       type2 = TREE_TYPE (xarg2);
1347       if (TREE_CODE (type2) == OFFSET_TYPE)
1348         type2 = TREE_TYPE (type2);
1349       if (TREE_CODE (type2) == REFERENCE_TYPE)
1350         {
1351           arg2 = convert_from_reference (xarg2);
1352           type2 = TREE_TYPE (arg2);
1353         }
1354       else
1355         {
1356           arg2 = xarg2;
1357         }
1358
1359       if (!IS_AGGR_TYPE (type2))
1360         return rval;
1361       try_second = 0;
1362     }
1363
1364   if (try_second)
1365     {
1366       /* First arg may succeed; see whether second should.  */
1367       type2 = TREE_TYPE (xarg2);
1368       if (TREE_CODE (type2) == OFFSET_TYPE)
1369         type2 = TREE_TYPE (type2);
1370       if (TREE_CODE (type2) == REFERENCE_TYPE)
1371         {
1372           arg2 = convert_from_reference (xarg2);
1373           type2 = TREE_TYPE (arg2);
1374         }
1375       else
1376         {
1377           arg2 = xarg2;
1378         }
1379
1380       if (! IS_AGGR_TYPE (type2))
1381         try_second = 0;
1382     }
1383
1384   if (type1 == unknown_type_node
1385       || (try_second && TREE_TYPE (xarg2) == unknown_type_node))
1386     {
1387       /* This will not be implemented in the foreseeable future.  */
1388       return rval;
1389     }
1390
1391   if (code == MODIFY_EXPR)
1392     fnname = ansi_assopname[(int) TREE_CODE (arg3)];
1393   else
1394     fnname = ansi_opname[(int) code];
1395
1396   global_fn = lookup_name_nonclass (fnname);
1397
1398   /* This is the last point where we will accept failure.  This
1399      may be too eager if we wish an overloaded operator not to match,
1400      but would rather a normal operator be called on a type-converted
1401      argument.  */
1402
1403   if (IS_AGGR_TYPE (type1))
1404     {
1405       fields1 = lookup_fnfields (TYPE_BINFO (type1), fnname, 0);
1406       /* ARM $13.4.7, prefix/postfix ++/--.  */
1407       if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1408         {
1409           xarg2 = integer_zero_node;
1410           binary_is_unary = 0;
1411
1412           if (fields1)
1413             {
1414               tree t, t2;
1415               int have_postfix = 0;
1416
1417               /* Look for an `operator++ (int)'.  If they didn't have
1418                  one, then we fall back to the old way of doing things.  */
1419               for (t = TREE_VALUE (fields1); t ; t = DECL_CHAIN (t))
1420                 {
1421                   t2 = TYPE_ARG_TYPES (TREE_TYPE (t));
1422                   if (TREE_CHAIN (t2) != NULL_TREE
1423                       && TREE_VALUE (TREE_CHAIN (t2)) == integer_type_node)
1424                     {
1425                       have_postfix = 1;
1426                       break;
1427                     }
1428                 }
1429
1430               if (! have_postfix)
1431                 {
1432                   char *op = POSTINCREMENT_EXPR ? "++" : "--";
1433
1434                   /* There's probably a LOT of code in the world that
1435                      relies upon this old behavior.  */
1436                   pedwarn ("no `operator%s (int)' declared for postfix `%s', using prefix operator instead",
1437                            op, op);
1438                   xarg2 = NULL_TREE;
1439                   binary_is_unary = 1;
1440                 }
1441             }
1442         }
1443     }
1444
1445   if (fields1 == NULL_TREE && global_fn == NULL_TREE)
1446     return rval;
1447
1448   /* If RVAL winds up being `error_mark_node', we will return
1449      that... There is no way that normal semantics of these
1450      operators will succeed.  */
1451
1452   /* This argument may be an uncommitted OFFSET_REF.  This is
1453      the case for example when dealing with static class members
1454      which are referenced from their class name rather than
1455      from a class instance.  */
1456   if (TREE_CODE (xarg1) == OFFSET_REF
1457       && TREE_CODE (TREE_OPERAND (xarg1, 1)) == VAR_DECL)
1458     xarg1 = TREE_OPERAND (xarg1, 1);
1459   if (try_second && xarg2 && TREE_CODE (xarg2) == OFFSET_REF
1460       && TREE_CODE (TREE_OPERAND (xarg2, 1)) == VAR_DECL)
1461     xarg2 = TREE_OPERAND (xarg2, 1);
1462
1463   if (global_fn)
1464     flags |= LOOKUP_GLOBAL;
1465
1466   if (code == CALL_EXPR)
1467     {
1468       /* This can only be a member function.  */
1469       return build_method_call (xarg1, fnname, xarg2,
1470                                 NULL_TREE, LOOKUP_NORMAL);
1471     }
1472   else if (tree_code_length[(int) code] == 1 || binary_is_unary)
1473     {
1474       parms = NULL_TREE;
1475       rval = build_method_call (xarg1, fnname, NULL_TREE, NULL_TREE, flags);
1476     }
1477   else if (code == COND_EXPR)
1478     {
1479       parms = tree_cons (NULL_TREE, xarg2, build_tree_list (NULL_TREE, arg3));
1480       rval = build_method_call (xarg1, fnname, parms, NULL_TREE, flags);
1481     }
1482   else if (code == METHOD_CALL_EXPR)
1483     {
1484       /* must be a member function.  */
1485       parms = tree_cons (NULL_TREE, xarg2, arg3);
1486       return build_method_call (xarg1, fnname, parms, NULL_TREE,
1487                                 LOOKUP_NORMAL);
1488     }
1489   else if (fields1)
1490     {
1491       parms = build_tree_list (NULL_TREE, xarg2);
1492       rval = build_method_call (xarg1, fnname, parms, NULL_TREE, flags);
1493     }
1494   else
1495     {
1496       parms = tree_cons (NULL_TREE, xarg1,
1497                          build_tree_list (NULL_TREE, xarg2));
1498       rval = build_overload_call (fnname, parms, flags);
1499     }
1500
1501   return rval;
1502 }
1503 \f
1504 /* This function takes an identifier, ID, and attempts to figure out what
1505    it means. There are a number of possible scenarios, presented in increasing
1506    order of hair:
1507
1508    1) not in a class's scope
1509    2) in class's scope, member name of the class's method
1510    3) in class's scope, but not a member name of the class
1511    4) in class's scope, member name of a class's variable
1512
1513    NAME is $1 from the bison rule. It is an IDENTIFIER_NODE.
1514    VALUE is $$ from the bison rule. It is the value returned by lookup_name ($1)
1515
1516    As a last ditch, try to look up the name as a label and return that
1517    address.
1518
1519    Values which are declared as being of REFERENCE_TYPE are
1520    automatically dereferenced here (as a hack to make the
1521    compiler faster).  */
1522
1523 tree
1524 hack_identifier (value, name)
1525      tree value, name;
1526 {
1527   tree type;
1528
1529   if (value == error_mark_node)
1530     {
1531       if (current_class_name)
1532         {
1533           tree fields = lookup_fnfields (TYPE_BINFO (current_class_type), name, 1);
1534           if (fields == error_mark_node)
1535             return error_mark_node;
1536           if (fields)
1537             {
1538               tree fndecl;
1539
1540               fndecl = TREE_VALUE (fields);
1541               my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 251);
1542               if (DECL_CHAIN (fndecl) == NULL_TREE)
1543                 {
1544                   warning ("methods cannot be converted to function pointers");
1545                   return fndecl;
1546                 }
1547               else
1548                 {
1549                   error ("ambiguous request for method pointer `%s'",
1550                          IDENTIFIER_POINTER (name));
1551                   return error_mark_node;
1552                 }
1553             }
1554         }
1555       if (flag_labels_ok && IDENTIFIER_LABEL_VALUE (name))
1556         {
1557           return IDENTIFIER_LABEL_VALUE (name);
1558         }
1559       return error_mark_node;
1560     }
1561
1562   type = TREE_TYPE (value);
1563   if (TREE_CODE (value) == FIELD_DECL)
1564     {
1565       if (current_class_ptr == NULL_TREE)
1566         {
1567           error ("request for member `%s' in static member function",
1568                  IDENTIFIER_POINTER (DECL_NAME (value)));
1569           return error_mark_node;
1570         }
1571       TREE_USED (current_class_ptr) = 1;
1572
1573       /* Mark so that if we are in a constructor, and then find that
1574          this field was initialized by a base initializer,
1575          we can emit an error message.  */
1576       TREE_USED (value) = 1;
1577       value = build_component_ref (current_class_ref, name, NULL_TREE, 1);
1578     }
1579   else if (really_overloaded_fn (value))
1580     {
1581 #if 0
1582       tree t = get_first_fn (value);
1583       for (; t; t = DECL_CHAIN (t))
1584         {
1585           if (TREE_CODE (t) == TEMPLATE_DECL)
1586             continue;
1587
1588           assemble_external (t);
1589           TREE_USED (t) = 1;
1590         }
1591 #endif
1592     }
1593   else if (TREE_CODE (value) == TREE_LIST)
1594     {
1595       /* Ambiguous reference to base members, possibly other cases?.  */
1596       tree t = value;
1597       while (t && TREE_CODE (t) == TREE_LIST)
1598         {
1599           mark_used (TREE_VALUE (t));
1600           t = TREE_CHAIN (t);
1601         }
1602     }
1603   else
1604     mark_used (value);
1605
1606   if (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == PARM_DECL)
1607     {
1608       tree context = decl_function_context (value);
1609       if (context != NULL_TREE && context != current_function_decl
1610           && ! TREE_STATIC (value))
1611         {
1612           cp_error ("use of %s from containing function",
1613                       (TREE_CODE (value) == VAR_DECL
1614                        ? "`auto' variable" : "parameter"));
1615           cp_error_at ("  `%#D' declared here", value);
1616           value = error_mark_node;
1617         }
1618     }
1619
1620   if (TREE_CODE_CLASS (TREE_CODE (value)) == 'd' && DECL_NONLOCAL (value))
1621     {
1622       if (DECL_LANG_SPECIFIC (value)
1623           && DECL_CLASS_CONTEXT (value) != current_class_type)
1624         {
1625           tree path, access;
1626           register tree context
1627             = (TREE_CODE (value) == FUNCTION_DECL && DECL_VIRTUAL_P (value))
1628               ? DECL_CLASS_CONTEXT (value)
1629               : DECL_CONTEXT (value);
1630
1631           get_base_distance (context, current_class_type, 0, &path);
1632           if (path)
1633             {
1634               access = compute_access (path, value);
1635               if (access != access_public_node)
1636                 {
1637                   if (TREE_CODE (value) == VAR_DECL)
1638                     error ("static member `%s' is %s",
1639                            IDENTIFIER_POINTER (name),
1640                            TREE_PRIVATE (value) ? "private"
1641                                                 : "from a private base class");
1642                   else
1643                     error ("enum `%s' is from private base class",
1644                            IDENTIFIER_POINTER (name));
1645                   return error_mark_node;
1646                 }
1647             }
1648         }
1649     }
1650   else if (TREE_CODE (value) == TREE_LIST && TREE_NONLOCAL_FLAG (value))
1651     {
1652       if (type == 0)
1653         {
1654           error ("request for member `%s' is ambiguous in multiple inheritance lattice",
1655                  IDENTIFIER_POINTER (name));
1656           return error_mark_node;
1657         }
1658
1659       return value;
1660     }
1661
1662   if (TREE_CODE (type) == REFERENCE_TYPE && ! processing_template_decl)
1663     value = convert_from_reference (value);
1664   return value;
1665 }
1666
1667 \f
1668 static char *
1669 thunk_printable_name (decl)
1670      tree decl;
1671 {
1672   return "<thunk function>";
1673 }
1674
1675 tree
1676 make_thunk (function, delta)
1677      tree function;
1678      int delta;
1679 {
1680   char buffer[250];
1681   tree thunk_id;
1682   tree thunk;
1683   char *func_name;
1684   tree func_decl;
1685   if (TREE_CODE (function) != ADDR_EXPR)
1686     abort ();
1687   func_decl = TREE_OPERAND (function, 0);
1688   if (TREE_CODE (func_decl) != FUNCTION_DECL)
1689     abort ();
1690   func_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func_decl));
1691   if (delta<=0)
1692     sprintf (buffer, "__thunk_%d_%s", -delta, func_name);
1693   else
1694     sprintf (buffer, "__thunk_n%d_%s", delta, func_name);
1695   thunk_id = get_identifier (buffer);
1696   thunk = IDENTIFIER_GLOBAL_VALUE (thunk_id);
1697   if (thunk && TREE_CODE (thunk) != THUNK_DECL)
1698     {
1699       cp_error ("implementation-reserved name `%D' used", thunk_id);
1700       IDENTIFIER_GLOBAL_VALUE (thunk_id) = thunk = NULL_TREE;
1701     }
1702   if (thunk == NULL_TREE)
1703     {
1704       thunk = build_decl (FUNCTION_DECL, thunk_id, TREE_TYPE (func_decl));
1705       TREE_READONLY (thunk) = TREE_READONLY (func_decl);
1706       TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (func_decl);
1707       comdat_linkage (thunk);
1708       TREE_SET_CODE (thunk, THUNK_DECL);
1709       DECL_INITIAL (thunk) = function;
1710       THUNK_DELTA (thunk) = delta;
1711       DECL_EXTERNAL (thunk) = 1;
1712       DECL_ARTIFICIAL (thunk) = 1;
1713       /* So that finish_file can write out any thunks that need to be: */
1714       pushdecl_top_level (thunk);
1715     }
1716   return thunk;
1717 }
1718
1719 /* Emit the definition of a C++ multiple inheritance vtable thunk.  */
1720
1721 void
1722 emit_thunk (thunk_fndecl)
1723      tree thunk_fndecl;
1724 {
1725   tree function = TREE_OPERAND (DECL_INITIAL (thunk_fndecl), 0);
1726   int delta = THUNK_DELTA (thunk_fndecl);
1727
1728   if (TREE_ASM_WRITTEN (thunk_fndecl))
1729     return;
1730
1731   TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1732
1733   TREE_ADDRESSABLE (function) = 1;
1734   mark_used (function);
1735
1736   if (current_function_decl)
1737     abort ();
1738
1739   TREE_SET_CODE (thunk_fndecl, FUNCTION_DECL);
1740
1741   {
1742 #ifdef ASM_OUTPUT_MI_THUNK
1743     char *fnname;
1744     current_function_decl = thunk_fndecl;
1745     /* Make sure we build up its RTL before we go onto the
1746        temporary obstack.  */
1747     make_function_rtl (thunk_fndecl);
1748     temporary_allocation ();
1749     DECL_RESULT (thunk_fndecl)
1750       = build_decl (RESULT_DECL, 0, integer_type_node);
1751     fnname = XSTR (XEXP (DECL_RTL (thunk_fndecl), 0), 0);
1752     assemble_start_function (thunk_fndecl, fnname);
1753     ASM_OUTPUT_MI_THUNK (asm_out_file, thunk_fndecl, delta, function);
1754     assemble_end_function (thunk_fndecl, fnname);
1755     permanent_allocation (1);
1756     current_function_decl = 0;
1757 #else /* ASM_OUTPUT_MI_THUNK */
1758   /* If we don't have the necessary macro for efficient thunks, generate a
1759      thunk function that just makes a call to the real function.
1760      Unfortunately, this doesn't work for varargs.  */
1761
1762     tree a, t;
1763
1764     if (varargs_function_p (function))
1765       cp_error ("generic thunk code fails for method `%#D' which uses `...'",
1766                 function);
1767
1768     /* Set up clone argument trees for the thunk.  */
1769     t = NULL_TREE;
1770     for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
1771       {
1772         tree x = copy_node (a);
1773         TREE_CHAIN (x) = t;
1774         DECL_CONTEXT (x) = thunk_fndecl;
1775         t = x;
1776       }
1777     a = nreverse (t);
1778     DECL_ARGUMENTS (thunk_fndecl) = a;
1779     DECL_RESULT (thunk_fndecl) = NULL_TREE;
1780     DECL_LANG_SPECIFIC (thunk_fndecl) = DECL_LANG_SPECIFIC (function);
1781     copy_lang_decl (thunk_fndecl);
1782     DECL_INTERFACE_KNOWN (thunk_fndecl) = 1;
1783     DECL_NOT_REALLY_EXTERN (thunk_fndecl) = 1;
1784
1785     start_function (NULL_TREE, thunk_fndecl, NULL_TREE, 1);
1786     store_parm_decls ();
1787     current_function_is_thunk = 1;
1788
1789     /* Build up the call to the real function.  */
1790     t = build_int_2 (delta, -1 * (delta < 0));
1791     TREE_TYPE (t) = signed_type (sizetype);
1792     t = fold (build (PLUS_EXPR, TREE_TYPE (a), a, t));
1793     t = tree_cons (NULL_TREE, t, NULL_TREE);
1794     for (a = TREE_CHAIN (a); a; a = TREE_CHAIN (a))
1795       t = tree_cons (NULL_TREE, a, t);
1796     t = nreverse (t);
1797     t = build_call (function, TREE_TYPE (TREE_TYPE (function)), t);
1798     c_expand_return (t);
1799
1800     finish_function (lineno, 0, 0);
1801 #endif /* ASM_OUTPUT_MI_THUNK */
1802   }
1803
1804   TREE_SET_CODE (thunk_fndecl, THUNK_DECL);
1805 }
1806 \f
1807 /* Code for synthesizing methods which have default semantics defined.  */
1808
1809 /* For the anonymous union in TYPE, return the member that is at least as
1810    large as the rest of the members, so we can copy it.  */
1811
1812 static tree
1813 largest_union_member (type)
1814      tree type;
1815 {
1816   tree f, type_size = TYPE_SIZE (type);
1817
1818   for (f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
1819     if (simple_cst_equal (DECL_SIZE (f), type_size) == 1)
1820       return f;
1821
1822   /* We should always find one.  */
1823   my_friendly_abort (323);
1824   return NULL_TREE;
1825 }
1826
1827 /* Generate code for default X(X&) constructor.  */
1828
1829 static void
1830 do_build_copy_constructor (fndecl)
1831      tree fndecl;
1832 {
1833   tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1834   tree t;
1835
1836   clear_last_expr ();
1837   push_momentary ();
1838
1839   if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1840     parm = TREE_CHAIN (parm);
1841   parm = convert_from_reference (parm);
1842
1843   if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
1844     {
1845       t = build (INIT_EXPR, void_type_node, current_class_ref, parm);
1846       TREE_SIDE_EFFECTS (t) = 1;
1847       cplus_expand_expr_stmt (t);
1848     }
1849   else
1850     {
1851       tree fields = TYPE_FIELDS (current_class_type);
1852       int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
1853       tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
1854       int i;
1855
1856       for (t = CLASSTYPE_VBASECLASSES (current_class_type); t;
1857            t = TREE_CHAIN (t))
1858         {
1859           tree basetype = BINFO_TYPE (t);
1860           tree p = convert_to_reference
1861             (build_reference_type (basetype), parm,
1862              CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
1863           p = convert_from_reference (p);
1864
1865           if (p == error_mark_node)
1866             cp_error ("in default copy constructor");
1867           else 
1868             current_base_init_list = tree_cons (basetype,
1869                                                 p, current_base_init_list);
1870         }
1871         
1872       for (i = 0; i < n_bases; ++i)
1873         {
1874           tree p, basetype = TREE_VEC_ELT (binfos, i);
1875           if (TREE_VIA_VIRTUAL (basetype))
1876             continue; 
1877
1878           basetype = BINFO_TYPE (basetype);
1879           p = convert_to_reference
1880             (build_reference_type (basetype), parm,
1881              CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
1882
1883           if (p == error_mark_node) 
1884             cp_error ("in default copy constructor");
1885           else 
1886             {
1887               p = convert_from_reference (p);
1888               current_base_init_list = tree_cons (basetype,
1889                                                   p, current_base_init_list);
1890             }
1891         }
1892       for (; fields; fields = TREE_CHAIN (fields))
1893         {
1894           tree init, t;
1895           tree field = fields;
1896
1897           if (TREE_CODE (field) != FIELD_DECL)
1898             continue;
1899           if (DECL_NAME (field))
1900             {
1901               if (VFIELD_NAME_P (DECL_NAME (field)))
1902                 continue;
1903               if (VBASE_NAME_P (DECL_NAME (field)))
1904                 continue;
1905
1906               /* True for duplicate members.  */
1907               if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
1908                 continue;
1909             }
1910           else if ((t = TREE_TYPE (field)) != NULL_TREE
1911                    && TREE_CODE (t) == UNION_TYPE
1912                    && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))
1913                    && TYPE_FIELDS (t) != NULL_TREE)
1914             field = largest_union_member (t);
1915           else
1916             continue;
1917
1918           init = build (COMPONENT_REF, TREE_TYPE (field), parm, field);
1919           init = build_tree_list (NULL_TREE, init);
1920
1921           current_member_init_list
1922             = tree_cons (DECL_NAME (field), init, current_member_init_list);
1923         }
1924       current_member_init_list = nreverse (current_member_init_list);
1925       current_base_init_list = nreverse (current_base_init_list);
1926       setup_vtbl_ptr ();
1927     }
1928
1929   pop_momentary ();
1930 }
1931
1932 static void
1933 do_build_assign_ref (fndecl)
1934      tree fndecl;
1935 {
1936   tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1937
1938   clear_last_expr ();
1939   push_momentary ();
1940
1941   parm = convert_from_reference (parm);
1942
1943   if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
1944     {
1945       tree t = build (MODIFY_EXPR, void_type_node, current_class_ref, parm);
1946       TREE_SIDE_EFFECTS (t) = 1;
1947       cplus_expand_expr_stmt (t);
1948     }
1949   else
1950     {
1951       tree fields = TYPE_FIELDS (current_class_type);
1952       int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
1953       tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
1954       int i;
1955
1956       for (i = 0; i < n_bases; ++i)
1957         {
1958           tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1959           tree p = convert_to_reference
1960             (build_reference_type (basetype), parm,
1961              CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
1962           p = convert_from_reference (p);
1963           p = build_member_call (basetype, ansi_opname [MODIFY_EXPR],
1964                                  build_tree_list (NULL_TREE, p));
1965           expand_expr_stmt (p);
1966         }
1967       for (; fields; fields = TREE_CHAIN (fields))
1968         {
1969           tree comp, init, t;
1970           tree field = fields;
1971
1972           if (TREE_CODE (field) != FIELD_DECL)
1973             continue;
1974
1975           if (TREE_READONLY (field))
1976             {
1977               if (DECL_NAME (field))
1978                 cp_error ("non-static const member `%#D', can't use default assignment operator", field);
1979               else
1980                 cp_error ("non-static const member in type `%T', can't use default assignment operator", current_class_type);
1981               continue;
1982             }
1983           else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1984             {
1985               if (DECL_NAME (field))
1986                 cp_error ("non-static reference member `%#D', can't use default assignment operator", field);
1987               else
1988                 cp_error ("non-static reference member in type `%T', can't use default assignment operator", current_class_type);
1989               continue;
1990             }
1991
1992           if (DECL_NAME (field))
1993             {
1994               if (VFIELD_NAME_P (DECL_NAME (field)))
1995                 continue;
1996               if (VBASE_NAME_P (DECL_NAME (field)))
1997                 continue;
1998
1999               /* True for duplicate members.  */
2000               if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
2001                 continue;
2002             }
2003           else if ((t = TREE_TYPE (field)) != NULL_TREE
2004                    && TREE_CODE (t) == UNION_TYPE
2005                    && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))
2006                    && TYPE_FIELDS (t) != NULL_TREE)
2007             field = largest_union_member (t);
2008           else
2009             continue;
2010
2011           comp = build (COMPONENT_REF, TREE_TYPE (field), current_class_ref, field);
2012           init = build (COMPONENT_REF, TREE_TYPE (field), parm, field);
2013
2014           expand_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
2015         }
2016     }
2017   c_expand_return (current_class_ref);
2018   pop_momentary ();
2019 }
2020
2021 void
2022 synthesize_method (fndecl)
2023      tree fndecl;
2024 {
2025   int nested = (current_function_decl != NULL_TREE);
2026   tree context = hack_decl_function_context (fndecl);
2027
2028   if (! context)
2029     push_to_top_level ();
2030   else if (nested)
2031     push_cp_function_context (context);
2032
2033   interface_unknown = 1;
2034   start_function (NULL_TREE, fndecl, NULL_TREE, 1);
2035   store_parm_decls ();
2036
2037   if (DECL_NAME (fndecl) == ansi_opname[MODIFY_EXPR])
2038     do_build_assign_ref (fndecl);
2039   else if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2040     ;
2041   else
2042     {
2043       tree arg_chain = FUNCTION_ARG_CHAIN (fndecl);
2044       if (DECL_CONSTRUCTOR_FOR_VBASE_P (fndecl))
2045         arg_chain = TREE_CHAIN (arg_chain);
2046       if (arg_chain != void_list_node)
2047         do_build_copy_constructor (fndecl);
2048       else if (TYPE_NEEDS_CONSTRUCTING (current_class_type))
2049         setup_vtbl_ptr ();
2050     }
2051
2052   finish_function (lineno, 0, nested);
2053
2054   extract_interface_info ();
2055   if (! context)
2056     pop_from_top_level ();
2057   else if (nested)
2058     pop_cp_function_context (context);
2059 }