OSDN Git Service

91th Cygnus<->FSF quick 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       return value;
1650     }
1651   if (TREE_CODE (value) == TREE_LIST && TREE_NONLOCAL_FLAG (value))
1652     {
1653       if (type == 0)
1654         {
1655           error ("request for member `%s' is ambiguous in multiple inheritance lattice",
1656                  IDENTIFIER_POINTER (name));
1657           return error_mark_node;
1658         }
1659
1660       return value;
1661     }
1662
1663   if (TREE_CODE (type) == REFERENCE_TYPE && ! processing_template_decl)
1664     value = convert_from_reference (value);
1665   return value;
1666 }
1667
1668 \f
1669 static char *
1670 thunk_printable_name (decl)
1671      tree decl;
1672 {
1673   return "<thunk function>";
1674 }
1675
1676 tree
1677 make_thunk (function, delta)
1678      tree function;
1679      int delta;
1680 {
1681   char buffer[250];
1682   tree thunk_id;
1683   tree thunk;
1684   char *func_name;
1685   tree func_decl;
1686   if (TREE_CODE (function) != ADDR_EXPR)
1687     abort ();
1688   func_decl = TREE_OPERAND (function, 0);
1689   if (TREE_CODE (func_decl) != FUNCTION_DECL)
1690     abort ();
1691   func_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func_decl));
1692   if (delta<=0)
1693     sprintf (buffer, "__thunk_%d_%s", -delta, func_name);
1694   else
1695     sprintf (buffer, "__thunk_n%d_%s", delta, func_name);
1696   thunk_id = get_identifier (buffer);
1697   thunk = IDENTIFIER_GLOBAL_VALUE (thunk_id);
1698   if (thunk && TREE_CODE (thunk) != THUNK_DECL)
1699     {
1700       cp_error ("implementation-reserved name `%D' used", thunk_id);
1701       IDENTIFIER_GLOBAL_VALUE (thunk_id) = thunk = NULL_TREE;
1702     }
1703   if (thunk == NULL_TREE)
1704     {
1705       thunk = build_decl (FUNCTION_DECL, thunk_id, TREE_TYPE (func_decl));
1706       TREE_READONLY (thunk) = TREE_READONLY (func_decl);
1707       TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (func_decl);
1708       comdat_linkage (thunk);
1709       TREE_SET_CODE (thunk, THUNK_DECL);
1710       DECL_INITIAL (thunk) = function;
1711       THUNK_DELTA (thunk) = delta;
1712       DECL_EXTERNAL (thunk) = 1;
1713       DECL_ARTIFICIAL (thunk) = 1;
1714       /* So that finish_file can write out any thunks that need to be: */
1715       pushdecl_top_level (thunk);
1716     }
1717   return thunk;
1718 }
1719
1720 /* Emit the definition of a C++ multiple inheritance vtable thunk.  */
1721
1722 void
1723 emit_thunk (thunk_fndecl)
1724      tree thunk_fndecl;
1725 {
1726   tree function = TREE_OPERAND (DECL_INITIAL (thunk_fndecl), 0);
1727   int delta = THUNK_DELTA (thunk_fndecl);
1728
1729   if (TREE_ASM_WRITTEN (thunk_fndecl))
1730     return;
1731
1732   TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1733
1734   TREE_ADDRESSABLE (function) = 1;
1735   mark_used (function);
1736
1737   if (current_function_decl)
1738     abort ();
1739
1740   TREE_SET_CODE (thunk_fndecl, FUNCTION_DECL);
1741
1742   {
1743 #ifdef ASM_OUTPUT_MI_THUNK
1744     char *fnname;
1745     current_function_decl = thunk_fndecl;
1746     /* Make sure we build up its RTL before we go onto the
1747        temporary obstack.  */
1748     make_function_rtl (thunk_fndecl);
1749     temporary_allocation ();
1750     DECL_RESULT (thunk_fndecl)
1751       = build_decl (RESULT_DECL, 0, integer_type_node);
1752     fnname = XSTR (XEXP (DECL_RTL (thunk_fndecl), 0), 0);
1753     assemble_start_function (thunk_fndecl, fnname);
1754     ASM_OUTPUT_MI_THUNK (asm_out_file, thunk_fndecl, delta, function);
1755     assemble_end_function (thunk_fndecl, fnname);
1756     permanent_allocation (1);
1757     current_function_decl = 0;
1758 #else /* ASM_OUTPUT_MI_THUNK */
1759   /* If we don't have the necessary macro for efficient thunks, generate a
1760      thunk function that just makes a call to the real function.
1761      Unfortunately, this doesn't work for varargs.  */
1762
1763     tree a, t;
1764
1765     if (varargs_function_p (function))
1766       cp_error ("generic thunk code fails for method `%#D' which uses `...'",
1767                 function);
1768
1769     /* Set up clone argument trees for the thunk.  */
1770     t = NULL_TREE;
1771     for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
1772       {
1773         tree x = copy_node (a);
1774         TREE_CHAIN (x) = t;
1775         DECL_CONTEXT (x) = thunk_fndecl;
1776         t = x;
1777       }
1778     a = nreverse (t);
1779     DECL_ARGUMENTS (thunk_fndecl) = a;
1780     DECL_RESULT (thunk_fndecl) = NULL_TREE;
1781     DECL_LANG_SPECIFIC (thunk_fndecl) = DECL_LANG_SPECIFIC (function);
1782     copy_lang_decl (thunk_fndecl);
1783     DECL_INTERFACE_KNOWN (thunk_fndecl) = 1;
1784     DECL_NOT_REALLY_EXTERN (thunk_fndecl) = 1;
1785
1786     start_function (NULL_TREE, thunk_fndecl, NULL_TREE, 1);
1787     store_parm_decls ();
1788     current_function_is_thunk = 1;
1789
1790     /* Build up the call to the real function.  */
1791     t = build_int_2 (delta, -1 * (delta < 0));
1792     TREE_TYPE (t) = signed_type (sizetype);
1793     t = fold (build (PLUS_EXPR, TREE_TYPE (a), a, t));
1794     t = tree_cons (NULL_TREE, t, NULL_TREE);
1795     for (a = TREE_CHAIN (a); a; a = TREE_CHAIN (a))
1796       t = tree_cons (NULL_TREE, a, t);
1797     t = nreverse (t);
1798     t = build_call (function, TREE_TYPE (TREE_TYPE (function)), t);
1799     c_expand_return (t);
1800
1801     finish_function (lineno, 0, 0);
1802 #endif /* ASM_OUTPUT_MI_THUNK */
1803   }
1804
1805   TREE_SET_CODE (thunk_fndecl, THUNK_DECL);
1806 }
1807 \f
1808 /* Code for synthesizing methods which have default semantics defined.  */
1809
1810 /* For the anonymous union in TYPE, return the member that is at least as
1811    large as the rest of the members, so we can copy it.  */
1812
1813 static tree
1814 largest_union_member (type)
1815      tree type;
1816 {
1817   tree f, type_size = TYPE_SIZE (type);
1818
1819   for (f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
1820     if (simple_cst_equal (DECL_SIZE (f), type_size) == 1)
1821       return f;
1822
1823   /* We should always find one.  */
1824   my_friendly_abort (323);
1825   return NULL_TREE;
1826 }
1827
1828 /* Generate code for default X(X&) constructor.  */
1829
1830 static void
1831 do_build_copy_constructor (fndecl)
1832      tree fndecl;
1833 {
1834   tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1835   tree t;
1836
1837   clear_last_expr ();
1838   push_momentary ();
1839
1840   if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1841     parm = TREE_CHAIN (parm);
1842   parm = convert_from_reference (parm);
1843
1844   if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
1845     {
1846       t = build (INIT_EXPR, void_type_node, current_class_ref, parm);
1847       TREE_SIDE_EFFECTS (t) = 1;
1848       cplus_expand_expr_stmt (t);
1849     }
1850   else
1851     {
1852       tree fields = TYPE_FIELDS (current_class_type);
1853       int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
1854       tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
1855       int i;
1856
1857       for (t = CLASSTYPE_VBASECLASSES (current_class_type); t;
1858            t = TREE_CHAIN (t))
1859         {
1860           tree basetype = BINFO_TYPE (t);
1861           tree p = convert_to_reference
1862             (build_reference_type (basetype), parm,
1863              CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
1864           p = convert_from_reference (p);
1865
1866           if (p == error_mark_node)
1867             cp_error ("in default copy constructor");
1868           else 
1869             current_base_init_list = tree_cons (basetype,
1870                                                 p, current_base_init_list);
1871         }
1872         
1873       for (i = 0; i < n_bases; ++i)
1874         {
1875           tree p, basetype = TREE_VEC_ELT (binfos, i);
1876           if (TREE_VIA_VIRTUAL (basetype))
1877             continue; 
1878
1879           basetype = BINFO_TYPE (basetype);
1880           p = convert_to_reference
1881             (build_reference_type (basetype), parm,
1882              CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
1883
1884           if (p == error_mark_node) 
1885             cp_error ("in default copy constructor");
1886           else 
1887             {
1888               p = convert_from_reference (p);
1889               current_base_init_list = tree_cons (basetype,
1890                                                   p, current_base_init_list);
1891             }
1892         }
1893       for (; fields; fields = TREE_CHAIN (fields))
1894         {
1895           tree init, t;
1896           tree field = fields;
1897
1898           if (TREE_CODE (field) != FIELD_DECL)
1899             continue;
1900           if (DECL_NAME (field))
1901             {
1902               if (VFIELD_NAME_P (DECL_NAME (field)))
1903                 continue;
1904               if (VBASE_NAME_P (DECL_NAME (field)))
1905                 continue;
1906
1907               /* True for duplicate members.  */
1908               if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
1909                 continue;
1910             }
1911           else if ((t = TREE_TYPE (field)) != NULL_TREE
1912                    && TREE_CODE (t) == UNION_TYPE
1913                    && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))
1914                    && TYPE_FIELDS (t) != NULL_TREE)
1915             field = largest_union_member (t);
1916           else
1917             continue;
1918
1919           init = build (COMPONENT_REF, TREE_TYPE (field), parm, field);
1920           init = build_tree_list (NULL_TREE, init);
1921
1922           current_member_init_list
1923             = tree_cons (DECL_NAME (field), init, current_member_init_list);
1924         }
1925       current_member_init_list = nreverse (current_member_init_list);
1926       current_base_init_list = nreverse (current_base_init_list);
1927       setup_vtbl_ptr ();
1928     }
1929
1930   pop_momentary ();
1931 }
1932
1933 static void
1934 do_build_assign_ref (fndecl)
1935      tree fndecl;
1936 {
1937   tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1938
1939   clear_last_expr ();
1940   push_momentary ();
1941
1942   parm = convert_from_reference (parm);
1943
1944   if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
1945     {
1946       tree t = build (MODIFY_EXPR, void_type_node, current_class_ref, parm);
1947       TREE_SIDE_EFFECTS (t) = 1;
1948       cplus_expand_expr_stmt (t);
1949     }
1950   else
1951     {
1952       tree fields = TYPE_FIELDS (current_class_type);
1953       int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
1954       tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
1955       int i;
1956
1957       for (i = 0; i < n_bases; ++i)
1958         {
1959           tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1960           tree p = convert_to_reference
1961             (build_reference_type (basetype), parm,
1962              CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
1963           p = convert_from_reference (p);
1964           p = build_member_call (basetype, ansi_opname [MODIFY_EXPR],
1965                                  build_tree_list (NULL_TREE, p));
1966           expand_expr_stmt (p);
1967         }
1968       for (; fields; fields = TREE_CHAIN (fields))
1969         {
1970           tree comp, init, t;
1971           tree field = fields;
1972
1973           if (TREE_CODE (field) != FIELD_DECL)
1974             continue;
1975
1976           if (TREE_READONLY (field))
1977             {
1978               if (DECL_NAME (field))
1979                 cp_error ("non-static const member `%#D', can't use default assignment operator", field);
1980               else
1981                 cp_error ("non-static const member in type `%T', can't use default assignment operator", current_class_type);
1982               continue;
1983             }
1984           else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1985             {
1986               if (DECL_NAME (field))
1987                 cp_error ("non-static reference member `%#D', can't use default assignment operator", field);
1988               else
1989                 cp_error ("non-static reference member in type `%T', can't use default assignment operator", current_class_type);
1990               continue;
1991             }
1992
1993           if (DECL_NAME (field))
1994             {
1995               if (VFIELD_NAME_P (DECL_NAME (field)))
1996                 continue;
1997               if (VBASE_NAME_P (DECL_NAME (field)))
1998                 continue;
1999
2000               /* True for duplicate members.  */
2001               if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
2002                 continue;
2003             }
2004           else if ((t = TREE_TYPE (field)) != NULL_TREE
2005                    && TREE_CODE (t) == UNION_TYPE
2006                    && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))
2007                    && TYPE_FIELDS (t) != NULL_TREE)
2008             field = largest_union_member (t);
2009           else
2010             continue;
2011
2012           comp = build (COMPONENT_REF, TREE_TYPE (field), current_class_ref, field);
2013           init = build (COMPONENT_REF, TREE_TYPE (field), parm, field);
2014
2015           expand_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
2016         }
2017     }
2018   c_expand_return (current_class_ref);
2019   pop_momentary ();
2020 }
2021
2022 void
2023 synthesize_method (fndecl)
2024      tree fndecl;
2025 {
2026   int nested = (current_function_decl != NULL_TREE);
2027   tree context = hack_decl_function_context (fndecl);
2028
2029   if (! context)
2030     push_to_top_level ();
2031   else if (nested)
2032     push_cp_function_context (context);
2033
2034   interface_unknown = 1;
2035   start_function (NULL_TREE, fndecl, NULL_TREE, 1);
2036   store_parm_decls ();
2037
2038   if (DECL_NAME (fndecl) == ansi_opname[MODIFY_EXPR])
2039     do_build_assign_ref (fndecl);
2040   else if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2041     ;
2042   else
2043     {
2044       tree arg_chain = FUNCTION_ARG_CHAIN (fndecl);
2045       if (DECL_CONSTRUCTOR_FOR_VBASE_P (fndecl))
2046         arg_chain = TREE_CHAIN (arg_chain);
2047       if (arg_chain != void_list_node)
2048         do_build_copy_constructor (fndecl);
2049       else if (TYPE_NEEDS_CONSTRUCTING (current_class_type))
2050         setup_vtbl_ptr ();
2051     }
2052
2053   finish_function (lineno, 0, nested);
2054
2055   /* Do we really *want* to inline this function?  */
2056   if (DECL_INLINE (fndecl))
2057     {
2058       /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
2059          will check our size.  */
2060       DECL_INLINE (fndecl) = 0;
2061
2062       /* We say !at_eof because at the end of the file some of the rtl
2063          for fndecl may have been allocated on the temporary obstack.
2064          (The function_obstack is the temporary one if we're not in a
2065          function). */
2066       if ((! at_eof) && function_cannot_inline_p (fndecl) == 0)
2067         DECL_INLINE (fndecl) = 1;
2068     }
2069
2070   extract_interface_info ();
2071   if (! context)
2072     pop_from_top_level ();
2073   else if (nested)
2074     pop_cp_function_context (context);
2075 }