OSDN Git Service

(ASM_COMMENT_START): Add default definition.
[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 VOID_TYPE:
899           if (! just_one)
900             {
901 #if 0
902               extern tree void_list_node;
903
904               /* See if anybody is wasting memory.  */
905               my_friendly_assert (parmtypes == void_list_node, 247);
906 #endif
907               /* This is the end of a parameter list.  */
908               if (end) OB_FINISH ();
909               return (char *)obstack_base (&scratch_obstack);
910             }
911           OB_PUTC ('v');
912           break;
913
914         case ERROR_MARK:        /* not right, but nothing is anyway */
915           break;
916
917           /* have to do these */
918         case UNION_TYPE:
919         case RECORD_TYPE:
920           if (! just_one)
921             /* Make this type signature look incompatible
922                with AT&T.  */
923             OB_PUTC ('G');
924           goto common;
925         case ENUMERAL_TYPE:
926         common:
927           {
928             tree name = TYPE_NAME (parmtype);
929             int i = 1;
930
931             if (TREE_CODE (name) == TYPE_DECL)
932               {
933                 tree context = name;
934
935                 /* If DECL_ASSEMBLER_NAME has been set properly, use it.  */
936                 if (DECL_ASSEMBLER_NAME (context) != DECL_NAME (context))
937                   {
938                     OB_PUTID (DECL_ASSEMBLER_NAME (context));
939                     break;
940                   }
941                 while (DECL_CONTEXT (context))
942                   {
943                     i += 1;
944                     context = DECL_CONTEXT (context);
945                     if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
946                       context = TYPE_NAME (context);
947                   }
948                 name = DECL_NAME (name);
949               }
950             my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 248);
951             if (i > 1)
952               {
953                 OB_PUTC ('Q');
954                 if (i > 9)
955                   OB_PUTC ('_');
956                 icat (i);
957                 if (i > 9)
958                   OB_PUTC ('_');
959                 numeric_output_need_bar = 0;
960                 build_overload_nested_name (TYPE_NAME (parmtype));
961               }
962             else              
963               build_overload_identifier (TYPE_NAME (parmtype));
964             break;
965           }
966
967         case UNKNOWN_TYPE:
968           /* This will take some work.  */
969           OB_PUTC ('?');
970           break;
971
972         case TEMPLATE_TYPE_PARM:
973           OB_PUTC ('X');
974           if (TEMPLATE_TYPE_IDX (parmtype) > 9)
975             OB_PUTC ('_');
976           icat (TEMPLATE_TYPE_IDX (parmtype)); 
977           if (TEMPLATE_TYPE_IDX (parmtype) > 9)
978             OB_PUTC ('_');
979           break;
980             
981         case TYPENAME_TYPE:
982           /* We don't ever want this output, but it's inconvenient not to
983              be able to build the string.  This should cause assembler
984              errors we'll notice.  */
985           {
986             static int n;
987             sprintf (digit_buffer, " *%d", n++);
988             OB_PUTCP (digit_buffer);
989           }
990           break;
991
992         default:
993           my_friendly_abort (75);
994         }
995
996     next:
997       if (just_one) break;
998       parmtypes = TREE_CHAIN (parmtypes);
999     }
1000   if (! just_one)
1001     {
1002       if (nrepeats)
1003         flush_repeats (typevec[maxtype-1]);
1004
1005       /* To get here, parms must end with `...'.  */
1006       OB_PUTC ('e');
1007     }
1008
1009   if (end) OB_FINISH ();
1010   return (char *)obstack_base (&scratch_obstack);
1011 }
1012
1013 tree
1014 build_static_name (basetype, name)
1015   tree basetype, name;
1016 {
1017   char *basename  = build_overload_name (basetype, 1, 1);
1018   char *buf = (char *) alloca (IDENTIFIER_LENGTH (name)
1019                                + sizeof (STATIC_NAME_FORMAT)
1020                                + strlen (basename));
1021   sprintf (buf, STATIC_NAME_FORMAT, basename, IDENTIFIER_POINTER (name));
1022   return get_identifier (buf);
1023 }  
1024 \f
1025 /* Change the name of a function definition so that it may be
1026    overloaded. NAME is the name of the function to overload,
1027    PARMS is the parameter list (which determines what name the
1028    final function obtains).
1029
1030    FOR_METHOD is 1 if this overload is being performed
1031    for a method, rather than a function type.  It is 2 if
1032    this overload is being performed for a constructor.  */
1033
1034 tree
1035 build_decl_overload (dname, parms, for_method)
1036      tree dname;
1037      tree parms;
1038      int for_method;
1039 {
1040   char *name = IDENTIFIER_POINTER (dname);
1041
1042   /* member operators new and delete look like methods at this point.  */
1043   if (! for_method && parms != NULL_TREE && TREE_CODE (parms) == TREE_LIST)
1044     {
1045       if (dname == ansi_opname[(int) DELETE_EXPR])
1046         return get_identifier ("__builtin_delete");
1047       else if (dname == ansi_opname[(int) VEC_DELETE_EXPR])
1048         return get_identifier ("__builtin_vec_delete");
1049       else if (TREE_CHAIN (parms) == void_list_node)
1050         {
1051           if (dname == ansi_opname[(int) NEW_EXPR])
1052             return get_identifier ("__builtin_new");
1053           else if (dname == ansi_opname[(int) VEC_NEW_EXPR])
1054             return get_identifier ("__builtin_vec_new");
1055         }
1056     }
1057
1058   OB_INIT ();
1059   if (for_method != 2)
1060     OB_PUTCP (name);
1061   /* Otherwise, we can divine that this is a constructor,
1062      and figure out its name without any extra encoding.  */
1063
1064   OB_PUTC2 ('_', '_');
1065   if (for_method)
1066     {
1067 #if 0
1068       /* We can get away without doing this.  */
1069       OB_PUTC ('M');
1070 #endif
1071       {
1072         tree this_type = TREE_VALUE (parms);
1073
1074         if (TREE_CODE (this_type) == RECORD_TYPE)  /* a signature pointer */
1075           parms = temp_tree_cons (NULL_TREE, SIGNATURE_TYPE (this_type),
1076                                   TREE_CHAIN (parms));
1077         else
1078           parms = temp_tree_cons (NULL_TREE, TREE_TYPE (this_type),
1079                                   TREE_CHAIN (parms));
1080       }
1081     }
1082   else
1083     OB_PUTC ('F');
1084
1085   if (parms == NULL_TREE)
1086     OB_PUTC2 ('e', '\0');
1087   else if (parms == void_list_node)
1088     OB_PUTC2 ('v', '\0');
1089   else
1090     {
1091       ALLOCATE_TYPEVEC (parms);
1092       nofold = 0;
1093       if (for_method)
1094         {
1095           build_overload_name (TREE_VALUE (parms), 0, 0);
1096
1097           typevec[maxtype++] = TREE_VALUE (parms);
1098           TREE_USED (TREE_VALUE (parms)) = 1;
1099
1100           if (TREE_CHAIN (parms))
1101             build_overload_name (TREE_CHAIN (parms), 0, 1);
1102           else
1103             OB_PUTC2 ('e', '\0');
1104         }
1105       else
1106         build_overload_name (parms, 0, 1);
1107       DEALLOCATE_TYPEVEC (parms);
1108     }
1109   {
1110     tree n = get_identifier (obstack_base (&scratch_obstack));
1111     if (IDENTIFIER_OPNAME_P (dname))
1112       IDENTIFIER_OPNAME_P (n) = 1;
1113     return n;
1114   }
1115 }
1116
1117 /* Build an overload name for the type expression TYPE.  */
1118
1119 tree
1120 build_typename_overload (type)
1121      tree type;
1122 {
1123   tree id;
1124
1125   OB_INIT ();
1126   OB_PUTID (ansi_opname[(int) TYPE_EXPR]);
1127   nofold = 1;
1128   build_overload_name (type, 0, 1);
1129   id = get_identifier (obstack_base (&scratch_obstack));
1130   IDENTIFIER_OPNAME_P (id) = 1;
1131 #if 0
1132   IDENTIFIER_GLOBAL_VALUE (id) = TYPE_MAIN_DECL (type);
1133 #endif
1134   TREE_TYPE (id) = type;
1135   return id;
1136 }
1137
1138 tree
1139 build_overload_with_type (name, type)
1140      tree name, type;
1141 {
1142   OB_INIT ();
1143   OB_PUTID (name);
1144   nofold = 1;
1145
1146   build_overload_name (type, 0, 1);
1147   return get_identifier (obstack_base (&scratch_obstack));
1148 }
1149
1150 tree
1151 get_id_2 (name, name2)
1152      char *name;
1153      tree name2;
1154 {
1155   OB_INIT ();
1156   OB_PUTCP (name);
1157   OB_PUTID (name2);
1158   OB_FINISH ();
1159   return get_identifier (obstack_base (&scratch_obstack));
1160 }
1161 \f
1162 /* Given a tree_code CODE, and some arguments (at least one),
1163    attempt to use an overloaded operator on the arguments.
1164
1165    For unary operators, only the first argument need be checked.
1166    For binary operators, both arguments may need to be checked.
1167
1168    Member functions can convert class references to class pointers,
1169    for one-level deep indirection.  More than that is not supported.
1170    Operators [](), ()(), and ->() must be member functions.
1171
1172    We call function call building calls with LOOKUP_COMPLAIN if they
1173    are our only hope.  This is true when we see a vanilla operator
1174    applied to something of aggregate type.  If this fails, we are free
1175    to return `error_mark_node', because we will have reported the
1176    error.
1177
1178    Operators NEW and DELETE overload in funny ways: operator new takes
1179    a single `size' parameter, and operator delete takes a pointer to the
1180    storage being deleted.  When overloading these operators, success is
1181    assumed.  If there is a failure, report an error message and return
1182    `error_mark_node'.  */
1183
1184 /* NOSTRICT */
1185 tree
1186 build_opfncall (code, flags, xarg1, xarg2, arg3)
1187      enum tree_code code;
1188      int flags;
1189      tree xarg1, xarg2, arg3;
1190 {
1191   tree rval = 0;
1192   tree arg1, arg2;
1193   tree type1, type2, fnname;
1194   tree fields1 = 0, parms = 0;
1195   tree global_fn;
1196   int try_second;
1197   int binary_is_unary;
1198
1199   if (flag_ansi_overloading)
1200     return build_new_op (code, flags, xarg1, xarg2, arg3);
1201
1202   if (xarg1 == error_mark_node)
1203     return error_mark_node;
1204
1205   if (code == COND_EXPR)
1206     {
1207       if (xarg2 == error_mark_node
1208           || arg3 == error_mark_node)
1209         return error_mark_node;
1210     }
1211   if (code == COMPONENT_REF)
1212     if (TREE_CODE (TREE_TYPE (xarg1)) == POINTER_TYPE)
1213       return rval;
1214
1215   /* First, see if we can work with the first argument */
1216   type1 = TREE_TYPE (xarg1);
1217
1218   /* Some tree codes have length > 1, but we really only want to
1219      overload them if their first argument has a user defined type.  */
1220   switch (code)
1221     {
1222     case PREINCREMENT_EXPR:
1223     case PREDECREMENT_EXPR:
1224     case POSTINCREMENT_EXPR:
1225     case POSTDECREMENT_EXPR:
1226     case COMPONENT_REF:
1227       binary_is_unary = 1;
1228       try_second = 0;
1229       break;
1230
1231       /* ARRAY_REFs and CALL_EXPRs must overload successfully.
1232          If they do not, return error_mark_node instead of NULL_TREE.  */
1233     case ARRAY_REF:
1234       if (xarg2 == error_mark_node)
1235         return error_mark_node;
1236     case CALL_EXPR:
1237       rval = error_mark_node;
1238       binary_is_unary = 0;
1239       try_second = 0;
1240       break;
1241
1242     case VEC_NEW_EXPR:
1243     case NEW_EXPR:
1244       {
1245         tree args = tree_cons (NULL_TREE, xarg2, arg3);
1246         fnname = ansi_opname[(int) code];
1247         if (flags & LOOKUP_GLOBAL)
1248           return build_overload_call (fnname, args, flags & LOOKUP_COMPLAIN);
1249
1250         rval = build_method_call
1251           (build_indirect_ref (build1 (NOP_EXPR, xarg1, error_mark_node),
1252                                "new"),
1253            fnname, args, NULL_TREE, flags);
1254         if (rval == error_mark_node)
1255           /* User might declare fancy operator new, but invoke it
1256              like standard one.  */
1257           return rval;
1258
1259         TREE_TYPE (rval) = xarg1;
1260         TREE_CALLS_NEW (rval) = 1;
1261         return rval;
1262       }
1263       break;
1264
1265     case VEC_DELETE_EXPR:
1266     case DELETE_EXPR:
1267       {
1268         fnname = ansi_opname[(int) code];
1269         if (flags & LOOKUP_GLOBAL)
1270           return build_overload_call (fnname,
1271                                       build_tree_list (NULL_TREE, xarg1),
1272                                       flags & LOOKUP_COMPLAIN);
1273         arg1 = TREE_TYPE (xarg1);
1274
1275         /* This handles the case where we're trying to delete
1276            X (*a)[10];
1277            a=new X[5][10];
1278            delete[] a; */
1279            
1280         if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
1281           {
1282             /* Strip off the pointer and the array.  */
1283             arg1 = TREE_TYPE (TREE_TYPE (arg1));
1284
1285             while (TREE_CODE (arg1) == ARRAY_TYPE)
1286                 arg1 = (TREE_TYPE (arg1));
1287
1288             arg1 = build_pointer_type (arg1);
1289           }
1290
1291         rval = build_method_call
1292           (build_indirect_ref (build1 (NOP_EXPR, arg1,
1293                                        error_mark_node),
1294                                NULL_PTR),
1295            fnname, tree_cons (NULL_TREE, xarg1,
1296                                build_tree_list (NULL_TREE, xarg2)),
1297            NULL_TREE, flags);
1298 #if 0
1299         /* This can happen when operator delete is protected.  */
1300         my_friendly_assert (rval != error_mark_node, 250);
1301         TREE_TYPE (rval) = void_type_node;
1302 #endif
1303         return rval;
1304       }
1305       break;
1306
1307     default:
1308       binary_is_unary = 0;
1309       try_second = tree_code_length [(int) code] == 2;
1310       if (try_second && xarg2 == error_mark_node)
1311         return error_mark_node;
1312       break;
1313     }
1314
1315   if (try_second && xarg2 == error_mark_node)
1316     return error_mark_node;
1317
1318   /* What ever it was, we do not know how to deal with it.  */
1319   if (type1 == NULL_TREE)
1320     return rval;
1321
1322   if (TREE_CODE (type1) == OFFSET_TYPE)
1323     type1 = TREE_TYPE (type1);
1324
1325   if (TREE_CODE (type1) == REFERENCE_TYPE)
1326     {
1327       arg1 = convert_from_reference (xarg1);
1328       type1 = TREE_TYPE (arg1);
1329     }
1330   else
1331     {
1332       arg1 = xarg1;
1333     }
1334
1335   if (!IS_AGGR_TYPE (type1) || TYPE_PTRMEMFUNC_P (type1))
1336     {
1337       /* Try to fail. First, fail if unary */
1338       if (! try_second)
1339         return rval;
1340       /* Second, see if second argument is non-aggregate.  */
1341       type2 = TREE_TYPE (xarg2);
1342       if (TREE_CODE (type2) == OFFSET_TYPE)
1343         type2 = TREE_TYPE (type2);
1344       if (TREE_CODE (type2) == REFERENCE_TYPE)
1345         {
1346           arg2 = convert_from_reference (xarg2);
1347           type2 = TREE_TYPE (arg2);
1348         }
1349       else
1350         {
1351           arg2 = xarg2;
1352         }
1353
1354       if (!IS_AGGR_TYPE (type2))
1355         return rval;
1356       try_second = 0;
1357     }
1358
1359   if (try_second)
1360     {
1361       /* First arg may succeed; see whether second should.  */
1362       type2 = TREE_TYPE (xarg2);
1363       if (TREE_CODE (type2) == OFFSET_TYPE)
1364         type2 = TREE_TYPE (type2);
1365       if (TREE_CODE (type2) == REFERENCE_TYPE)
1366         {
1367           arg2 = convert_from_reference (xarg2);
1368           type2 = TREE_TYPE (arg2);
1369         }
1370       else
1371         {
1372           arg2 = xarg2;
1373         }
1374
1375       if (! IS_AGGR_TYPE (type2))
1376         try_second = 0;
1377     }
1378
1379   if (type1 == unknown_type_node
1380       || (try_second && TREE_TYPE (xarg2) == unknown_type_node))
1381     {
1382       /* This will not be implemented in the foreseeable future.  */
1383       return rval;
1384     }
1385
1386   if (code == MODIFY_EXPR)
1387     fnname = ansi_assopname[(int) TREE_CODE (arg3)];
1388   else
1389     fnname = ansi_opname[(int) code];
1390
1391   global_fn = lookup_name_nonclass (fnname);
1392
1393   /* This is the last point where we will accept failure.  This
1394      may be too eager if we wish an overloaded operator not to match,
1395      but would rather a normal operator be called on a type-converted
1396      argument.  */
1397
1398   if (IS_AGGR_TYPE (type1))
1399     {
1400       fields1 = lookup_fnfields (TYPE_BINFO (type1), fnname, 0);
1401       /* ARM $13.4.7, prefix/postfix ++/--.  */
1402       if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1403         {
1404           xarg2 = integer_zero_node;
1405           binary_is_unary = 0;
1406
1407           if (fields1)
1408             {
1409               tree t, t2;
1410               int have_postfix = 0;
1411
1412               /* Look for an `operator++ (int)'.  If they didn't have
1413                  one, then we fall back to the old way of doing things.  */
1414               for (t = TREE_VALUE (fields1); t ; t = DECL_CHAIN (t))
1415                 {
1416                   t2 = TYPE_ARG_TYPES (TREE_TYPE (t));
1417                   if (TREE_CHAIN (t2) != NULL_TREE
1418                       && TREE_VALUE (TREE_CHAIN (t2)) == integer_type_node)
1419                     {
1420                       have_postfix = 1;
1421                       break;
1422                     }
1423                 }
1424
1425               if (! have_postfix)
1426                 {
1427                   char *op = POSTINCREMENT_EXPR ? "++" : "--";
1428
1429                   /* There's probably a LOT of code in the world that
1430                      relies upon this old behavior.  */
1431                   pedwarn ("no `operator%s (int)' declared for postfix `%s', using prefix operator instead",
1432                            op, op);
1433                   xarg2 = NULL_TREE;
1434                   binary_is_unary = 1;
1435                 }
1436             }
1437         }
1438     }
1439
1440   if (fields1 == NULL_TREE && global_fn == NULL_TREE)
1441     return rval;
1442
1443   /* If RVAL winds up being `error_mark_node', we will return
1444      that... There is no way that normal semantics of these
1445      operators will succeed.  */
1446
1447   /* This argument may be an uncommitted OFFSET_REF.  This is
1448      the case for example when dealing with static class members
1449      which are referenced from their class name rather than
1450      from a class instance.  */
1451   if (TREE_CODE (xarg1) == OFFSET_REF
1452       && TREE_CODE (TREE_OPERAND (xarg1, 1)) == VAR_DECL)
1453     xarg1 = TREE_OPERAND (xarg1, 1);
1454   if (try_second && xarg2 && TREE_CODE (xarg2) == OFFSET_REF
1455       && TREE_CODE (TREE_OPERAND (xarg2, 1)) == VAR_DECL)
1456     xarg2 = TREE_OPERAND (xarg2, 1);
1457
1458   if (global_fn)
1459     flags |= LOOKUP_GLOBAL;
1460
1461   if (code == CALL_EXPR)
1462     {
1463       /* This can only be a member function.  */
1464       return build_method_call (xarg1, fnname, xarg2,
1465                                 NULL_TREE, LOOKUP_NORMAL);
1466     }
1467   else if (tree_code_length[(int) code] == 1 || binary_is_unary)
1468     {
1469       parms = NULL_TREE;
1470       rval = build_method_call (xarg1, fnname, NULL_TREE, NULL_TREE, flags);
1471     }
1472   else if (code == COND_EXPR)
1473     {
1474       parms = tree_cons (NULL_TREE, xarg2, build_tree_list (NULL_TREE, arg3));
1475       rval = build_method_call (xarg1, fnname, parms, NULL_TREE, flags);
1476     }
1477   else if (code == METHOD_CALL_EXPR)
1478     {
1479       /* must be a member function.  */
1480       parms = tree_cons (NULL_TREE, xarg2, arg3);
1481       return build_method_call (xarg1, fnname, parms, NULL_TREE,
1482                                 LOOKUP_NORMAL);
1483     }
1484   else if (fields1)
1485     {
1486       parms = build_tree_list (NULL_TREE, xarg2);
1487       rval = build_method_call (xarg1, fnname, parms, NULL_TREE, flags);
1488     }
1489   else
1490     {
1491       parms = tree_cons (NULL_TREE, xarg1,
1492                          build_tree_list (NULL_TREE, xarg2));
1493       rval = build_overload_call (fnname, parms, flags);
1494     }
1495
1496   return rval;
1497 }
1498 \f
1499 /* This function takes an identifier, ID, and attempts to figure out what
1500    it means. There are a number of possible scenarios, presented in increasing
1501    order of hair:
1502
1503    1) not in a class's scope
1504    2) in class's scope, member name of the class's method
1505    3) in class's scope, but not a member name of the class
1506    4) in class's scope, member name of a class's variable
1507
1508    NAME is $1 from the bison rule. It is an IDENTIFIER_NODE.
1509    VALUE is $$ from the bison rule. It is the value returned by lookup_name ($1)
1510
1511    As a last ditch, try to look up the name as a label and return that
1512    address.
1513
1514    Values which are declared as being of REFERENCE_TYPE are
1515    automatically dereferenced here (as a hack to make the
1516    compiler faster).  */
1517
1518 tree
1519 hack_identifier (value, name)
1520      tree value, name;
1521 {
1522   tree type;
1523
1524   if (value == error_mark_node)
1525     {
1526       if (current_class_name)
1527         {
1528           tree fields = lookup_fnfields (TYPE_BINFO (current_class_type), name, 1);
1529           if (fields == error_mark_node)
1530             return error_mark_node;
1531           if (fields)
1532             {
1533               tree fndecl;
1534
1535               fndecl = TREE_VALUE (fields);
1536               my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 251);
1537               if (DECL_CHAIN (fndecl) == NULL_TREE)
1538                 {
1539                   warning ("methods cannot be converted to function pointers");
1540                   return fndecl;
1541                 }
1542               else
1543                 {
1544                   error ("ambiguous request for method pointer `%s'",
1545                          IDENTIFIER_POINTER (name));
1546                   return error_mark_node;
1547                 }
1548             }
1549         }
1550       if (flag_labels_ok && IDENTIFIER_LABEL_VALUE (name))
1551         {
1552           return IDENTIFIER_LABEL_VALUE (name);
1553         }
1554       return error_mark_node;
1555     }
1556
1557   type = TREE_TYPE (value);
1558   if (TREE_CODE (value) == FIELD_DECL)
1559     {
1560       if (current_class_ptr == NULL_TREE)
1561         {
1562           error ("request for member `%s' in static member function",
1563                  IDENTIFIER_POINTER (DECL_NAME (value)));
1564           return error_mark_node;
1565         }
1566       TREE_USED (current_class_ptr) = 1;
1567
1568       /* Mark so that if we are in a constructor, and then find that
1569          this field was initialized by a base initializer,
1570          we can emit an error message.  */
1571       TREE_USED (value) = 1;
1572       value = build_component_ref (current_class_ref, name, NULL_TREE, 1);
1573     }
1574   else if (really_overloaded_fn (value))
1575     {
1576 #if 0
1577       tree t = get_first_fn (value);
1578       for (; t; t = DECL_CHAIN (t))
1579         {
1580           if (TREE_CODE (t) == TEMPLATE_DECL)
1581             continue;
1582
1583           assemble_external (t);
1584           TREE_USED (t) = 1;
1585         }
1586 #endif
1587     }
1588   else if (TREE_CODE (value) == TREE_LIST)
1589     {
1590       /* Ambiguous reference to base members, possibly other cases?.  */
1591       tree t = value;
1592       while (t && TREE_CODE (t) == TREE_LIST)
1593         {
1594           mark_used (TREE_VALUE (t));
1595           t = TREE_CHAIN (t);
1596         }
1597     }
1598   else
1599     mark_used (value);
1600
1601   if (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == PARM_DECL)
1602     {
1603       tree context = decl_function_context (value);
1604       if (context != NULL_TREE && context != current_function_decl
1605           && ! TREE_STATIC (value))
1606         {
1607           cp_error ("use of %s from containing function",
1608                       (TREE_CODE (value) == VAR_DECL
1609                        ? "`auto' variable" : "parameter"));
1610           cp_error_at ("  `%#D' declared here", value);
1611           value = error_mark_node;
1612         }
1613     }
1614
1615   if (TREE_CODE_CLASS (TREE_CODE (value)) == 'd' && DECL_NONLOCAL (value))
1616     {
1617       if (DECL_LANG_SPECIFIC (value)
1618           && DECL_CLASS_CONTEXT (value) != current_class_type)
1619         {
1620           tree path, access;
1621           register tree context
1622             = (TREE_CODE (value) == FUNCTION_DECL && DECL_VIRTUAL_P (value))
1623               ? DECL_CLASS_CONTEXT (value)
1624               : DECL_CONTEXT (value);
1625
1626           get_base_distance (context, current_class_type, 0, &path);
1627           if (path)
1628             {
1629               access = compute_access (path, value);
1630               if (access != access_public_node)
1631                 {
1632                   if (TREE_CODE (value) == VAR_DECL)
1633                     error ("static member `%s' is %s",
1634                            IDENTIFIER_POINTER (name),
1635                            TREE_PRIVATE (value) ? "private"
1636                                                 : "from a private base class");
1637                   else
1638                     error ("enum `%s' is from private base class",
1639                            IDENTIFIER_POINTER (name));
1640                   return error_mark_node;
1641                 }
1642             }
1643         }
1644       return value;
1645     }
1646   if (TREE_CODE (value) == TREE_LIST && TREE_NONLOCAL_FLAG (value))
1647     {
1648       if (type == 0)
1649         {
1650           error ("request for member `%s' is ambiguous in multiple inheritance lattice",
1651                  IDENTIFIER_POINTER (name));
1652           return error_mark_node;
1653         }
1654
1655       return value;
1656     }
1657
1658   if (TREE_CODE (type) == REFERENCE_TYPE && ! processing_template_decl)
1659     value = convert_from_reference (value);
1660   return value;
1661 }
1662
1663 \f
1664 static char *
1665 thunk_printable_name (decl)
1666      tree decl;
1667 {
1668   return "<thunk function>";
1669 }
1670
1671 tree
1672 make_thunk (function, delta)
1673      tree function;
1674      int delta;
1675 {
1676   char buffer[250];
1677   tree thunk_fndecl, thunk_id;
1678   tree thunk;
1679   char *func_name;
1680   static int thunk_number = 0;
1681   tree func_decl;
1682   if (TREE_CODE (function) != ADDR_EXPR)
1683     abort ();
1684   func_decl = TREE_OPERAND (function, 0);
1685   if (TREE_CODE (func_decl) != FUNCTION_DECL)
1686     abort ();
1687   func_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func_decl));
1688   if (delta<=0)
1689     sprintf (buffer, "__thunk_%d_%s", -delta, func_name);
1690   else
1691     sprintf (buffer, "__thunk_n%d_%s", delta, func_name);
1692   thunk_id = get_identifier (buffer);
1693   thunk = IDENTIFIER_GLOBAL_VALUE (thunk_id);
1694   if (thunk && TREE_CODE (thunk) != THUNK_DECL)
1695     {
1696       cp_error ("implementation-reserved name `%D' used", thunk_id);
1697       IDENTIFIER_GLOBAL_VALUE (thunk_id) = thunk = NULL_TREE;
1698     }
1699   if (thunk == NULL_TREE)
1700     {
1701       thunk = build_decl (FUNCTION_DECL, thunk_id, TREE_TYPE (func_decl));
1702       TREE_READONLY (thunk) = TREE_READONLY (func_decl);
1703       TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (func_decl);
1704       comdat_linkage (thunk);
1705       TREE_SET_CODE (thunk, THUNK_DECL);
1706       DECL_INITIAL (thunk) = function;
1707       THUNK_DELTA (thunk) = delta;
1708       DECL_EXTERNAL (thunk) = 1;
1709       DECL_ARTIFICIAL (thunk) = 1;
1710       /* So that finish_file can write out any thunks that need to be: */
1711       pushdecl_top_level (thunk);
1712     }
1713   return thunk;
1714 }
1715
1716 /* Emit the definition of a C++ multiple inheritance vtable thunk.  */
1717
1718 void
1719 emit_thunk (thunk_fndecl)
1720      tree thunk_fndecl;
1721 {
1722   tree function = TREE_OPERAND (DECL_INITIAL (thunk_fndecl), 0);
1723   int delta = THUNK_DELTA (thunk_fndecl);
1724
1725   if (TREE_ASM_WRITTEN (thunk_fndecl))
1726     return;
1727
1728   TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1729
1730   TREE_ADDRESSABLE (function) = 1;
1731   mark_used (function);
1732
1733   if (current_function_decl)
1734     abort ();
1735
1736   TREE_SET_CODE (thunk_fndecl, FUNCTION_DECL);
1737
1738   {
1739 #ifdef ASM_OUTPUT_MI_THUNK
1740     char *fnname;
1741     current_function_decl = thunk_fndecl;
1742     temporary_allocation ();
1743     DECL_RESULT (thunk_fndecl)
1744       = build_decl (RESULT_DECL, 0, integer_type_node);
1745     make_function_rtl (thunk_fndecl);
1746     fnname = XSTR (XEXP (DECL_RTL (thunk_fndecl), 0), 0);
1747     assemble_start_function (thunk_fndecl, fnname);
1748     ASM_OUTPUT_MI_THUNK (asm_out_file, thunk_fndecl, delta, function);
1749     assemble_end_function (thunk_fndecl, fnname);
1750     permanent_allocation (1);
1751     current_function_decl = 0;
1752 #else /* ASM_OUTPUT_MI_THUNK */
1753   /* If we don't have the necessary macro for efficient thunks, generate a
1754      thunk function that just makes a call to the real function.
1755      Unfortunately, this doesn't work for varargs.  */
1756
1757     tree a, t;
1758
1759     if (varargs_function_p (function))
1760       cp_error ("generic thunk code fails for method `%#D' which uses `...'",
1761                 function);
1762
1763     /* Set up clone argument trees for the thunk.  */
1764     t = NULL_TREE;
1765     for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
1766       {
1767         tree x = copy_node (a);
1768         TREE_CHAIN (x) = t;
1769         DECL_CONTEXT (x) = thunk_fndecl;
1770         t = x;
1771       }
1772     a = nreverse (t);
1773     DECL_ARGUMENTS (thunk_fndecl) = a;
1774     DECL_RESULT (thunk_fndecl) = NULL_TREE;
1775     DECL_LANG_SPECIFIC (thunk_fndecl) = DECL_LANG_SPECIFIC (function);
1776     copy_lang_decl (thunk_fndecl);
1777     DECL_INTERFACE_KNOWN (thunk_fndecl) = 1;
1778     DECL_NOT_REALLY_EXTERN (thunk_fndecl) = 1;
1779
1780     start_function (NULL_TREE, thunk_fndecl, NULL_TREE, 1);
1781     store_parm_decls ();
1782     current_function_is_thunk = 1;
1783
1784     /* Build up the call to the real function.  */
1785     t = build_int_2 (delta, -1 * (delta < 0));
1786     TREE_TYPE (t) = signed_type (sizetype);
1787     t = fold (build (PLUS_EXPR, TREE_TYPE (a), a, t));
1788     t = tree_cons (NULL_TREE, t, NULL_TREE);
1789     for (a = TREE_CHAIN (a); a; a = TREE_CHAIN (a))
1790       t = tree_cons (NULL_TREE, a, t);
1791     t = nreverse (t);
1792     t = build_call (function, TREE_TYPE (TREE_TYPE (function)), t);
1793     c_expand_return (t);
1794
1795     finish_function (lineno, 0, 0);
1796 #endif /* ASM_OUTPUT_MI_THUNK */
1797   }
1798
1799   TREE_SET_CODE (thunk_fndecl, THUNK_DECL);
1800 }
1801 \f
1802 /* Code for synthesizing methods which have default semantics defined.  */
1803
1804 /* For the anonymous union in TYPE, return the member that is at least as
1805    large as the rest of the members, so we can copy it.  */
1806
1807 static tree
1808 largest_union_member (type)
1809      tree type;
1810 {
1811   tree f, type_size = TYPE_SIZE (type);
1812
1813   for (f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
1814     if (simple_cst_equal (DECL_SIZE (f), type_size) == 1)
1815       return f;
1816
1817   /* We should always find one.  */
1818   my_friendly_abort (323);
1819   return NULL_TREE;
1820 }
1821
1822 /* Generate code for default X(X&) constructor.  */
1823
1824 static void
1825 do_build_copy_constructor (fndecl)
1826      tree fndecl;
1827 {
1828   tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1829   tree t;
1830
1831   clear_last_expr ();
1832   push_momentary ();
1833
1834   if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1835     parm = TREE_CHAIN (parm);
1836   parm = convert_from_reference (parm);
1837
1838   if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
1839     {
1840       t = build (INIT_EXPR, void_type_node, current_class_ref, parm);
1841       TREE_SIDE_EFFECTS (t) = 1;
1842       cplus_expand_expr_stmt (t);
1843     }
1844   else
1845     {
1846       tree fields = TYPE_FIELDS (current_class_type);
1847       int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
1848       tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
1849       int i;
1850
1851       for (t = CLASSTYPE_VBASECLASSES (current_class_type); t;
1852            t = TREE_CHAIN (t))
1853         {
1854           tree basetype = BINFO_TYPE (t);
1855           tree p = convert_to_reference
1856             (build_reference_type (basetype), parm,
1857              CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
1858           p = convert_from_reference (p);
1859
1860           if (p == error_mark_node)
1861             cp_error ("in default copy constructor");
1862           else 
1863             current_base_init_list = tree_cons (basetype,
1864                                                 p, current_base_init_list);
1865         }
1866         
1867       for (i = 0; i < n_bases; ++i)
1868         {
1869           tree p, basetype = TREE_VEC_ELT (binfos, i);
1870           if (TREE_VIA_VIRTUAL (basetype))
1871             continue; 
1872
1873           basetype = BINFO_TYPE (basetype);
1874           p = convert_to_reference
1875             (build_reference_type (basetype), parm,
1876              CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
1877
1878           if (p == error_mark_node) 
1879             cp_error ("in default copy constructor");
1880           else 
1881             {
1882               p = convert_from_reference (p);
1883               current_base_init_list = tree_cons (basetype,
1884                                                   p, current_base_init_list);
1885             }
1886         }
1887       for (; fields; fields = TREE_CHAIN (fields))
1888         {
1889           tree init, t;
1890           tree field = fields;
1891
1892           if (TREE_CODE (field) != FIELD_DECL)
1893             continue;
1894           if (DECL_NAME (field))
1895             {
1896               if (VFIELD_NAME_P (DECL_NAME (field)))
1897                 continue;
1898               if (VBASE_NAME_P (DECL_NAME (field)))
1899                 continue;
1900
1901               /* True for duplicate members.  */
1902               if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
1903                 continue;
1904             }
1905           else if ((t = TREE_TYPE (field)) != NULL_TREE
1906                    && TREE_CODE (t) == UNION_TYPE
1907                    && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))
1908                    && TYPE_FIELDS (t) != NULL_TREE)
1909             field = largest_union_member (t);
1910           else
1911             continue;
1912
1913           init = build (COMPONENT_REF, TREE_TYPE (field), parm, field);
1914           init = build_tree_list (NULL_TREE, init);
1915
1916           current_member_init_list
1917             = tree_cons (DECL_NAME (field), init, current_member_init_list);
1918         }
1919       current_member_init_list = nreverse (current_member_init_list);
1920       current_base_init_list = nreverse (current_base_init_list);
1921       setup_vtbl_ptr ();
1922     }
1923
1924   pop_momentary ();
1925 }
1926
1927 static void
1928 do_build_assign_ref (fndecl)
1929      tree fndecl;
1930 {
1931   tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1932
1933   clear_last_expr ();
1934   push_momentary ();
1935
1936   parm = convert_from_reference (parm);
1937
1938   if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
1939     {
1940       tree t = build (MODIFY_EXPR, void_type_node, current_class_ref, parm);
1941       TREE_SIDE_EFFECTS (t) = 1;
1942       cplus_expand_expr_stmt (t);
1943     }
1944   else
1945     {
1946       tree fields = TYPE_FIELDS (current_class_type);
1947       int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
1948       tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
1949       int i;
1950
1951       for (i = 0; i < n_bases; ++i)
1952         {
1953           tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1954           tree p = convert_to_reference
1955             (build_reference_type (basetype), parm,
1956              CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
1957           p = convert_from_reference (p);
1958           p = build_member_call (basetype, ansi_opname [MODIFY_EXPR],
1959                                  build_tree_list (NULL_TREE, p));
1960           expand_expr_stmt (p);
1961         }
1962       for (; fields; fields = TREE_CHAIN (fields))
1963         {
1964           tree comp, init, t;
1965           tree field = fields;
1966
1967           if (TREE_CODE (field) != FIELD_DECL)
1968             continue;
1969
1970           if (TREE_READONLY (field))
1971             {
1972               if (DECL_NAME (field))
1973                 cp_error ("non-static const member `%#D', can't use default assignment operator", field);
1974               else
1975                 cp_error ("non-static const member in type `%T', can't use default assignment operator", current_class_type);
1976               continue;
1977             }
1978           else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1979             {
1980               if (DECL_NAME (field))
1981                 cp_error ("non-static reference member `%#D', can't use default assignment operator", field);
1982               else
1983                 cp_error ("non-static reference member in type `%T', can't use default assignment operator", current_class_type);
1984               continue;
1985             }
1986
1987           if (DECL_NAME (field))
1988             {
1989               if (VFIELD_NAME_P (DECL_NAME (field)))
1990                 continue;
1991               if (VBASE_NAME_P (DECL_NAME (field)))
1992                 continue;
1993
1994               /* True for duplicate members.  */
1995               if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
1996                 continue;
1997             }
1998           else if ((t = TREE_TYPE (field)) != NULL_TREE
1999                    && TREE_CODE (t) == UNION_TYPE
2000                    && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))
2001                    && TYPE_FIELDS (t) != NULL_TREE)
2002             field = largest_union_member (t);
2003           else
2004             continue;
2005
2006           comp = build (COMPONENT_REF, TREE_TYPE (field), current_class_ref, field);
2007           init = build (COMPONENT_REF, TREE_TYPE (field), parm, field);
2008
2009           expand_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
2010         }
2011     }
2012   c_expand_return (current_class_ref);
2013   pop_momentary ();
2014 }
2015
2016 void
2017 synthesize_method (fndecl)
2018      tree fndecl;
2019 {
2020   int nested = (current_function_decl != NULL_TREE);
2021   tree context = hack_decl_function_context (fndecl);
2022
2023   if (! context)
2024     push_to_top_level ();
2025   else if (nested)
2026     push_cp_function_context (context);
2027
2028   interface_unknown = 1;
2029   start_function (NULL_TREE, fndecl, NULL_TREE, 1);
2030   store_parm_decls ();
2031
2032   if (DECL_NAME (fndecl) == ansi_opname[MODIFY_EXPR])
2033     do_build_assign_ref (fndecl);
2034   else if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2035     ;
2036   else
2037     {
2038       tree arg_chain = FUNCTION_ARG_CHAIN (fndecl);
2039       if (DECL_CONSTRUCTOR_FOR_VBASE_P (fndecl))
2040         arg_chain = TREE_CHAIN (arg_chain);
2041       if (arg_chain != void_list_node)
2042         do_build_copy_constructor (fndecl);
2043       else if (TYPE_NEEDS_CONSTRUCTING (current_class_type))
2044         setup_vtbl_ptr ();
2045     }
2046
2047   finish_function (lineno, 0, nested);
2048
2049   /* Do we really *want* to inline this function?  */
2050   if (DECL_INLINE (fndecl))
2051     {
2052       /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
2053          will check our size.  */
2054       DECL_INLINE (fndecl) = 0;
2055
2056       /* We say !at_eof because at the end of the file some of the rtl
2057          for fndecl may have been allocated on the temporary obstack.
2058          (The function_obstack is the temporary one if we're not in a
2059          function). */
2060       if ((! at_eof) && function_cannot_inline_p (fndecl) == 0)
2061         DECL_INLINE (fndecl) = 1;
2062     }
2063
2064   extract_interface_info ();
2065   if (! context)
2066     pop_from_top_level ();
2067   else if (nested)
2068     pop_cp_function_context (context);
2069 }