35th Cygnus<->FSF merge
[platform/upstream/gcc.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, 1989, 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
21
22
23 #ifndef PARM_CAN_BE_ARRAY_TYPE
24 #define PARM_CAN_BE_ARRAY_TYPE 1
25 #endif
26
27 /* Handle method declarations.  */
28 #include <stdio.h>
29 #include "config.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "class.h"
33 #include "obstack.h"
34 #include <ctype.h>
35 #include "rtl.h"
36 #include "expr.h"
37 #include "output.h"
38 #include "hard-reg-set.h"
39 #include "flags.h"
40
41 /* TREE_LIST of the current inline functions that need to be
42    processed.  */
43 struct pending_inline *pending_inlines;
44
45 #define obstack_chunk_alloc xmalloc
46 #define obstack_chunk_free free
47
48 /* Obstack where we build text strings for overloading, etc.  */
49 static struct obstack scratch_obstack;
50 static char *scratch_firstobj;
51
52 # define OB_INIT() (scratch_firstobj ? (obstack_free (&scratch_obstack, scratch_firstobj), 0) : 0)
53 # define OB_PUTC(C) (obstack_1grow (&scratch_obstack, (C)))
54 # define OB_PUTC2(C1,C2)        \
55   (obstack_1grow (&scratch_obstack, (C1)), obstack_1grow (&scratch_obstack, (C2)))
56 # define OB_PUTS(S) (obstack_grow (&scratch_obstack, (S), sizeof (S) - 1))
57 # define OB_PUTID(ID)  \
58   (obstack_grow (&scratch_obstack, IDENTIFIER_POINTER (ID),     \
59                  IDENTIFIER_LENGTH (ID)))
60 # define OB_PUTCP(S) (obstack_grow (&scratch_obstack, (S), strlen (S)))
61 # define OB_FINISH() (obstack_1grow (&scratch_obstack, '\0'))
62
63 #ifdef NO_AUTO_OVERLOAD
64 int is_overloaded ();
65 #endif
66
67 void
68 init_method ()
69 {
70   gcc_obstack_init (&scratch_obstack);
71   scratch_firstobj = (char *)obstack_alloc (&scratch_obstack, 0);
72 }
73
74 /* This must be large enough to hold any printed integer or floating-point
75    value.  */
76 static char digit_buffer[128];
77
78 /* Move inline function definitions out of structure so that they
79    can be processed normally.  CNAME is the name of the class
80    we are working from, METHOD_LIST is the list of method lists
81    of the structure.  We delete friend methods here, after
82    saving away their inline function definitions (if any).  */
83
84 void
85 do_inline_function_hair (type, friend_list)
86      tree type, friend_list;
87 {
88   tree method = TYPE_METHODS (type);
89
90   if (method && TREE_CODE (method) == TREE_VEC)
91     {
92       if (TREE_VEC_ELT (method, 0))
93         method = TREE_VEC_ELT (method, 0);
94       else
95         method = TREE_VEC_ELT (method, 1);
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 void
149 report_type_mismatch (cp, parmtypes, name_kind)
150      struct candidate *cp;
151      tree parmtypes;
152      char *name_kind;
153 {
154   int i = cp->u.bad_arg;
155   tree ttf, tta;
156   char *tmp_firstobj;
157
158   switch (i)
159     {
160     case -4:
161       my_friendly_assert (TREE_CODE (cp->function) == TEMPLATE_DECL, 240);
162       cp_error ("type unification failed for function template `%#D'",
163                 cp->function);
164       return;
165
166     case -3:
167       if (TYPE_READONLY (TREE_TYPE (TREE_VALUE (parmtypes))))
168         cp_error ("call to const %s `%#D' with non-const object", name_kind,
169                   cp->function);
170       else
171         cp_error ("call to non-const %s `%#D' with const object", name_kind,
172                   cp->function);
173       return;
174     case -2:
175       cp_error ("too few arguments for %s `%#D'", name_kind, cp->function);
176       return;
177     case -1:
178       cp_error ("too many arguments for %s `%#D'", name_kind, cp->function);
179       return;
180     case 0:
181       if (TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
182         {
183           /* Happens when we have an ambiguous base class.  */
184           my_friendly_assert (get_binfo (DECL_CLASS_CONTEXT (cp->function),
185                              TREE_TYPE (TREE_TYPE (TREE_VALUE (parmtypes))), 1) == error_mark_node,
186                               241);
187           return;
188         }
189     }
190
191   ttf = TYPE_ARG_TYPES (TREE_TYPE (cp->function));
192   tta = parmtypes;
193
194   while (i-- > 0)
195     {
196       ttf = TREE_CHAIN (ttf);
197       tta = TREE_CHAIN (tta);
198     }
199
200   OB_INIT ();
201   OB_PUTS ("bad argument ");
202   sprintf (digit_buffer, "%d", cp->u.bad_arg
203            - (TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
204            + 1);
205   OB_PUTCP (digit_buffer);
206
207   OB_PUTS (" for function `");
208   OB_PUTCP (decl_as_string (cp->function, 1));
209   OB_PUTS ("' (type was ");
210
211   /* Reset `i' so that type printing routines do the right thing.  */
212   if (tta)
213     {
214       enum tree_code code = TREE_CODE (TREE_TYPE (TREE_VALUE (tta)));
215       if (code == ERROR_MARK)
216         OB_PUTS ("(failed type instantiation)");
217       else
218         {
219           i = (code == FUNCTION_TYPE || code == METHOD_TYPE);
220           OB_PUTCP (type_as_string (TREE_TYPE (TREE_VALUE (tta)), 1));
221         }
222     }
223   else OB_PUTS ("void");
224   OB_PUTC (')');
225   OB_FINISH ();
226
227   tmp_firstobj = (char *)alloca (obstack_object_size (&scratch_obstack));
228   bcopy (obstack_base (&scratch_obstack), tmp_firstobj,
229          obstack_object_size (&scratch_obstack));
230   error (tmp_firstobj);
231 }
232 \f
233 /* Here is where overload code starts.  */
234
235 /* Array of types seen so far in top-level call to `build_overload_name'.
236    Allocated and deallocated by caller.  */
237 static tree *typevec;
238
239 /* Number of types interned by `build_overload_name' so far.  */
240 static int maxtype;
241
242 /* Number of occurrences of last type seen.  */
243 static int nrepeats;
244
245 /* Nonzero if we should not try folding parameter types.  */
246 static int nofold;
247
248 #define ALLOCATE_TYPEVEC(PARMTYPES) \
249   do { maxtype = 0, nrepeats = 0; \
250        typevec = (tree *)alloca (list_length (PARMTYPES) * sizeof (tree)); } while (0)
251
252 #define DEALLOCATE_TYPEVEC(PARMTYPES) \
253   do { tree t = (PARMTYPES); \
254        while (t) { TREE_USED (TREE_VALUE (t)) = 0; t = TREE_CHAIN (t); } \
255   } while (0)
256
257 /* Code to concatenate an asciified integer to a string.  */
258 static
259 #ifdef __GNUC__
260 __inline
261 #endif
262 void
263 icat (i)
264      int i;
265 {
266   /* Handle this case first, to go really quickly.  For many common values,
267      the result of i/10 below is 1.  */
268   if (i == 1)
269     {
270       OB_PUTC ('1');
271       return;
272     }
273
274   if (i < 0)
275     {
276       OB_PUTC ('m');
277       i = -i;
278     }
279   if (i < 10)
280     OB_PUTC ('0' + i);
281   else
282     {
283       icat (i / 10);
284       OB_PUTC ('0' + (i % 10));
285     }
286 }
287
288 static
289 #ifdef __GNUC__
290 __inline
291 #endif
292 void
293 flush_repeats (type)
294      tree type;
295 {
296   int tindex = 0;
297
298   while (typevec[tindex] != type)
299     tindex++;
300
301   if (nrepeats > 1)
302     {
303       OB_PUTC ('N');
304       icat (nrepeats);
305       if (nrepeats > 9)
306         OB_PUTC ('_');
307     }
308   else
309     OB_PUTC ('T');
310   nrepeats = 0;
311   icat (tindex);
312   if (tindex > 9)
313     OB_PUTC ('_');
314 }
315
316 static void build_overload_identifier ();
317
318 static void
319 build_overload_nested_name (context)
320      tree context;
321 {
322   /* We use DECL_NAME here, because pushtag now sets the DECL_ASSEMBLER_NAME.  */
323   tree name = DECL_NAME (context);
324   if (DECL_CONTEXT (context))
325     {
326       context = DECL_CONTEXT (context);
327       if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
328         context = TYPE_NAME (context);
329       build_overload_nested_name (context);
330     }
331   build_overload_identifier (name);
332 }
333
334 static void
335 build_overload_value (type, value)
336      tree type, value;
337 {
338   while (TREE_CODE (value) == NON_LVALUE_EXPR
339          || TREE_CODE (value) == NOP_EXPR)
340     value = TREE_OPERAND (value, 0);
341   my_friendly_assert (TREE_CODE (type) == PARM_DECL, 242);
342   type = TREE_TYPE (type);
343   switch (TREE_CODE (type))
344     {
345     case INTEGER_TYPE:
346     case ENUMERAL_TYPE:
347       {
348         my_friendly_assert (TREE_CODE (value) == INTEGER_CST, 243);
349         if (TYPE_PRECISION (value) == 2 * HOST_BITS_PER_WIDE_INT)
350           {
351             if (tree_int_cst_lt (value, integer_zero_node))
352               {
353                 OB_PUTC ('m');
354                 value = build_int_2 (~ TREE_INT_CST_LOW (value),
355                                      - TREE_INT_CST_HIGH (value));
356               }
357             if (TREE_INT_CST_HIGH (value)
358                 != (TREE_INT_CST_LOW (value) >> (HOST_BITS_PER_WIDE_INT - 1)))
359               {
360                 /* need to print a DImode value in decimal */
361                 sorry ("conversion of long long as PT parameter");
362               }
363             /* else fall through to print in smaller mode */
364           }
365         /* Wordsize or smaller */
366         icat (TREE_INT_CST_LOW (value));
367         return;
368       }
369 #ifndef REAL_IS_NOT_DOUBLE
370     case REAL_TYPE:
371       {
372         REAL_VALUE_TYPE val;
373         char *bufp = digit_buffer;
374         extern char *index ();
375
376         my_friendly_assert (TREE_CODE (value) == REAL_CST, 244);
377         val = TREE_REAL_CST (value);
378         if (val < 0)
379           {
380             val = -val;
381             *bufp++ = 'm';
382           }
383         sprintf (bufp, "%e", val);
384         bufp = (char *) index (bufp, 'e');
385         if (!bufp)
386           strcat (digit_buffer, "e0");
387         else
388           {
389             char *p;
390             bufp++;
391             if (*bufp == '-')
392               {
393                 *bufp++ = 'm';
394               }
395             p = bufp;
396             if (*p == '+')
397               p++;
398             while (*p == '0')
399               p++;
400             if (*p == 0)
401               {
402                 *bufp++ = '0';
403                 *bufp = 0;
404               }
405             else if (p != bufp)
406               {
407                 while (*p)
408                   *bufp++ = *p++;
409                 *bufp = 0;
410               }
411           }
412         OB_PUTCP (digit_buffer);
413         return;
414       }
415 #endif
416     case POINTER_TYPE:
417       value = TREE_OPERAND (value, 0);
418       if (TREE_CODE (value) == VAR_DECL)
419         {
420           my_friendly_assert (DECL_NAME (value) != 0, 245);
421           build_overload_identifier (DECL_NAME (value));
422           return;
423         }
424       else if (TREE_CODE (value) == FUNCTION_DECL)
425         {
426           my_friendly_assert (DECL_NAME (value) != 0, 246);
427           build_overload_identifier (DECL_NAME (value));
428           return;
429         }
430       else
431         my_friendly_abort (71);
432       break; /* not really needed */
433
434     default:
435       sorry ("conversion of %s as template parameter",
436              tree_code_name [(int) TREE_CODE (type)]);
437       my_friendly_abort (72);
438     }
439 }
440
441 static void
442 build_overload_identifier (name)
443      tree name;
444 {
445   if (IDENTIFIER_TEMPLATE (name))
446     {
447       tree template, parmlist, arglist, tname;
448       int i, nparms;
449       template = IDENTIFIER_TEMPLATE (name);
450       arglist = TREE_VALUE (template);
451       template = TREE_PURPOSE (template);
452       tname = DECL_NAME (template);
453       parmlist = DECL_ARGUMENTS (template);
454       nparms = TREE_VEC_LENGTH (parmlist);
455       OB_PUTC ('t');
456       icat (IDENTIFIER_LENGTH (tname));
457       OB_PUTID (tname);
458       icat (nparms);
459       for (i = 0; i < nparms; i++)
460         {
461           tree parm = TREE_VEC_ELT (parmlist, i);
462           tree arg = TREE_VEC_ELT (arglist, i);
463           if (TREE_CODE (parm) == IDENTIFIER_NODE)
464             {
465               /* This parameter is a type.  */
466               OB_PUTC ('Z');
467               build_overload_name (arg, 0, 0);
468             }
469           else
470             {
471               /* It's a PARM_DECL.  */
472               build_overload_name (TREE_TYPE (parm), 0, 0);
473               build_overload_value (parm, arg);
474             }
475         }
476     }
477   else
478     {
479       icat (IDENTIFIER_LENGTH (name));
480       OB_PUTID (name);
481     }
482 }
483
484 /* Given a list of parameters in PARMTYPES, create an unambiguous
485    overload string. Should distinguish any type that C (or C++) can
486    distinguish. I.e., pointers to functions are treated correctly.
487
488    Caller must deal with whether a final `e' goes on the end or not.
489
490    Any default conversions must take place before this function
491    is called.
492
493    BEGIN and END control initialization and finalization of the
494    obstack where we build the string.  */
495
496 char *
497 build_overload_name (parmtypes, begin, end)
498      tree parmtypes;
499      int begin, end;
500 {
501   int just_one;
502   tree parmtype;
503
504   if (begin) OB_INIT ();
505
506   if ((just_one = (TREE_CODE (parmtypes) != TREE_LIST)))
507     {
508       parmtype = parmtypes;
509       goto only_one;
510     }
511
512   while (parmtypes)
513     {
514       parmtype = TREE_VALUE (parmtypes);
515
516     only_one:
517
518       if (! nofold)
519         {
520           if (! just_one)
521             /* Every argument gets counted.  */
522             typevec[maxtype++] = parmtype;
523
524           if (TREE_USED (parmtype))
525             {
526               if (! just_one && parmtype == typevec[maxtype-2])
527                 nrepeats++;
528               else
529                 {
530                   if (nrepeats)
531                     flush_repeats (parmtype);
532                   if (! just_one && TREE_CHAIN (parmtypes)
533                       && parmtype == TREE_VALUE (TREE_CHAIN (parmtypes)))
534                     nrepeats++;
535                   else
536                     {
537                       int tindex = 0;
538
539                       while (typevec[tindex] != parmtype)
540                         tindex++;
541                       OB_PUTC ('T');
542                       icat (tindex);
543                       if (tindex > 9)
544                         OB_PUTC ('_');
545                     }
546                 }
547               goto next;
548             }
549           if (nrepeats)
550             flush_repeats (typevec[maxtype-2]);
551           if (! just_one
552               /* Only cache types which take more than one character.  */
553               && (parmtype != TYPE_MAIN_VARIANT (parmtype)
554                   || (TREE_CODE (parmtype) != INTEGER_TYPE
555                       && TREE_CODE (parmtype) != REAL_TYPE)))
556             TREE_USED (parmtype) = 1;
557         }
558
559       if (TYPE_PTRMEMFUNC_P (parmtype))
560         parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
561
562       if (TREE_READONLY (parmtype))
563         OB_PUTC ('C');
564       if (TREE_CODE (parmtype) == INTEGER_TYPE
565           && TYPE_MAIN_VARIANT (parmtype) == unsigned_type (TYPE_MAIN_VARIANT (parmtype)))
566         OB_PUTC ('U');
567       if (TYPE_VOLATILE (parmtype))
568         OB_PUTC ('V');
569
570       switch (TREE_CODE (parmtype))
571         {
572         case OFFSET_TYPE:
573           OB_PUTC ('O');
574           build_overload_name (TYPE_OFFSET_BASETYPE (parmtype), 0, 0);
575           OB_PUTC ('_');
576           build_overload_name (TREE_TYPE (parmtype), 0, 0);
577           break;
578
579         case REFERENCE_TYPE:
580           OB_PUTC ('R');
581           goto more;
582
583         case ARRAY_TYPE:
584 #if PARM_CAN_BE_ARRAY_TYPE
585           {
586             tree length;
587
588             OB_PUTC ('A');
589             if (TYPE_DOMAIN (parmtype) == NULL_TREE)
590               {
591                 error ("parameter type with unspecified array bounds invalid");
592                 icat (1);
593               }
594             else
595               {
596                 length = array_type_nelts (parmtype);
597                 if (TREE_CODE (length) == INTEGER_CST)
598                   icat (TREE_INT_CST_LOW (length) + 1);
599               }
600             OB_PUTC ('_');
601             goto more;
602           }
603 #else
604           OB_PUTC ('P');
605           goto more;
606 #endif
607
608         case POINTER_TYPE:
609           OB_PUTC ('P');
610         more:
611           build_overload_name (TREE_TYPE (parmtype), 0, 0);
612           break;
613
614         case FUNCTION_TYPE:
615         case METHOD_TYPE:
616           {
617             tree firstarg = TYPE_ARG_TYPES (parmtype);
618             /* Otherwise have to implement reentrant typevecs,
619                unmark and remark types, etc.  */
620             int old_nofold = nofold;
621             nofold = 1;
622
623             if (nrepeats)
624               flush_repeats (typevec[maxtype-1]);
625
626             /* @@ It may be possible to pass a function type in
627                which is not preceded by a 'P'.  */
628             if (TREE_CODE (parmtype) == FUNCTION_TYPE)
629               {
630                 OB_PUTC ('F');
631                 if (firstarg == NULL_TREE)
632                   OB_PUTC ('e');
633                 else if (firstarg == void_list_node)
634                   OB_PUTC ('v');
635                 else
636                   build_overload_name (firstarg, 0, 0);
637               }
638             else
639               {
640                 int constp = TYPE_READONLY (TREE_TYPE (TREE_VALUE (firstarg)));
641                 int volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_VALUE (firstarg)));
642                 OB_PUTC ('M');
643                 firstarg = TREE_CHAIN (firstarg);
644
645                 build_overload_name (TYPE_METHOD_BASETYPE (parmtype), 0, 0);
646                 if (constp)
647                   OB_PUTC ('C');
648                 if (volatilep)
649                   OB_PUTC ('V');
650
651                 /* For cfront 2.0 compatibility.  */
652                 OB_PUTC ('F');
653
654                 if (firstarg == NULL_TREE)
655                   OB_PUTC ('e');
656                 else if (firstarg == void_list_node)
657                   OB_PUTC ('v');
658                 else
659                   build_overload_name (firstarg, 0, 0);
660               }
661
662             /* Separate args from return type.  */
663             OB_PUTC ('_');
664             build_overload_name (TREE_TYPE (parmtype), 0, 0);
665             nofold = old_nofold;
666             break;
667           }
668
669         case INTEGER_TYPE:
670           parmtype = TYPE_MAIN_VARIANT (parmtype);
671           if (parmtype == integer_type_node
672               || parmtype == unsigned_type_node)
673             OB_PUTC ('i');
674           else if (parmtype == long_integer_type_node
675                    || parmtype == long_unsigned_type_node)
676             OB_PUTC ('l');
677           else if (parmtype == short_integer_type_node
678                    || parmtype == short_unsigned_type_node)
679             OB_PUTC ('s');
680           else if (parmtype == signed_char_type_node)
681             {
682               OB_PUTC ('S');
683               OB_PUTC ('c');
684             }
685           else if (parmtype == char_type_node
686                    || parmtype == unsigned_char_type_node)
687             OB_PUTC ('c');
688           else if (parmtype == wchar_type_node)
689             OB_PUTC ('w');
690           else if (parmtype == long_long_integer_type_node
691               || parmtype == long_long_unsigned_type_node)
692             OB_PUTC ('x');
693 #if 0
694           /* it would seem there is no way to enter these in source code,
695              yet.  (mrs) */
696           else if (parmtype == long_long_long_integer_type_node
697               || parmtype == long_long_long_unsigned_type_node)
698             OB_PUTC ('q');
699 #endif
700           else
701             my_friendly_abort (73);
702           break;
703
704         case REAL_TYPE:
705           parmtype = TYPE_MAIN_VARIANT (parmtype);
706           if (parmtype == long_double_type_node)
707             OB_PUTC ('r');
708           else if (parmtype == double_type_node)
709             OB_PUTC ('d');
710           else if (parmtype == float_type_node)
711             OB_PUTC ('f');
712           else my_friendly_abort (74);
713           break;
714
715         case VOID_TYPE:
716           if (! just_one)
717             {
718 #if 0
719               extern tree void_list_node;
720
721               /* See if anybody is wasting memory.  */
722               my_friendly_assert (parmtypes == void_list_node, 247);
723 #endif
724               /* This is the end of a parameter list.  */
725               if (end) OB_FINISH ();
726               return (char *)obstack_base (&scratch_obstack);
727             }
728           OB_PUTC ('v');
729           break;
730
731         case ERROR_MARK:        /* not right, but nothing is anyway */
732           break;
733
734           /* have to do these */
735         case UNION_TYPE:
736         case RECORD_TYPE:
737           if (! just_one)
738             /* Make this type signature look incompatible
739                with AT&T.  */
740             OB_PUTC ('G');
741           goto common;
742         case ENUMERAL_TYPE:
743         common:
744           {
745             tree name = TYPE_NAME (parmtype);
746             int i = 1;
747
748             if (TREE_CODE (name) == TYPE_DECL)
749               {
750                 tree context = name;
751                 while (DECL_CONTEXT (context))
752                   {
753                     i += 1;
754                     context = DECL_CONTEXT (context);
755                     if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
756                       context = TYPE_NAME (context);
757                   }
758                 name = DECL_NAME (name);
759               }
760             my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 248);
761             if (i > 1)
762               {
763                 OB_PUTC ('Q');
764                 if (i > 9)
765                   OB_PUTC ('_');
766                 icat (i);
767                 if (i > 9)
768                   OB_PUTC ('_');
769                 build_overload_nested_name (TYPE_NAME (parmtype));
770               }
771             else
772               build_overload_identifier (name);
773             break;
774           }
775
776         case UNKNOWN_TYPE:
777           /* This will take some work.  */
778           OB_PUTC ('?');
779           break;
780
781         case TEMPLATE_TYPE_PARM:
782         case TEMPLATE_CONST_PARM:
783         case UNINSTANTIATED_P_TYPE:
784           /* We don't ever want this output, but it's inconvenient not to
785              be able to build the string.  This should cause assembler
786              errors we'll notice.  */
787           {
788             static int n;
789             sprintf (digit_buffer, " *%d", n++);
790             OB_PUTCP (digit_buffer);
791           }
792           break;
793
794         default:
795           my_friendly_abort (75);
796         }
797
798     next:
799       if (just_one) break;
800       parmtypes = TREE_CHAIN (parmtypes);
801     }
802   if (! just_one)
803     {
804       if (nrepeats)
805         flush_repeats (typevec[maxtype-1]);
806
807       /* To get here, parms must end with `...'. */
808       OB_PUTC ('e');
809     }
810
811   if (end) OB_FINISH ();
812   return (char *)obstack_base (&scratch_obstack);
813 }
814 \f
815 /* Generate an identifier that encodes the (ANSI) exception TYPE. */
816
817 /* This should be part of `ansi_opname', or at least be defined by the std.  */
818 #define EXCEPTION_NAME_PREFIX "__ex"
819 #define EXCEPTION_NAME_LENGTH 4
820
821 tree
822 cplus_exception_name (type)
823      tree type;
824 {
825   OB_INIT ();
826   OB_PUTS (EXCEPTION_NAME_PREFIX);
827   return get_identifier (build_overload_name (type, 0, 1));
828 }
829 \f
830 /* Change the name of a function definition so that it may be
831    overloaded. NAME is the name of the function to overload,
832    PARMS is the parameter list (which determines what name the
833    final function obtains).
834
835    FOR_METHOD is 1 if this overload is being performed
836    for a method, rather than a function type.  It is 2 if
837    this overload is being performed for a constructor.  */
838 tree
839 build_decl_overload (dname, parms, for_method)
840      tree dname;
841      tree parms;
842      int for_method;
843 {
844   char *name = IDENTIFIER_POINTER (dname);
845
846   /* member operators new and delete look like methods at this point.  */
847   if (! for_method && parms != NULL_TREE && TREE_CODE (parms) == TREE_LIST)
848     {
849       if (TREE_VALUE (parms) == sizetype
850           && TREE_CHAIN (parms) == void_list_node)
851         {
852           if (dname == ansi_opname[(int) NEW_EXPR])
853             return get_identifier ("__builtin_new");
854           else if (dname == ansi_opname[(int) VEC_NEW_EXPR])
855             return get_identifier ("__builtin_vec_new");
856         }
857       else if (dname == ansi_opname[(int) DELETE_EXPR])
858         return get_identifier ("__builtin_delete");
859       else if (dname == ansi_opname[(int) VEC_DELETE_EXPR])
860         return get_identifier ("__builtin_vec_delete");
861     }
862
863   OB_INIT ();
864   if (for_method != 2)
865     OB_PUTCP (name);
866   /* Otherwise, we can divine that this is a constructor,
867      and figure out its name without any extra encoding.  */
868
869   OB_PUTC2 ('_', '_');
870   if (for_method)
871     {
872 #if 0
873       /* We can get away without doing this.  */
874       OB_PUTC ('M');
875 #endif
876       {
877         tree this_type = TREE_VALUE (parms);
878
879         if (TREE_CODE (this_type) == RECORD_TYPE)  /* a signature pointer */
880           parms = temp_tree_cons (NULL_TREE, SIGNATURE_TYPE (this_type),
881                                   TREE_CHAIN (parms));
882         else
883           parms = temp_tree_cons (NULL_TREE, TREE_TYPE (this_type),
884                                   TREE_CHAIN (parms));
885       }
886     }
887   else
888     OB_PUTC ('F');
889
890   if (parms == NULL_TREE)
891     OB_PUTC2 ('e', '\0');
892   else if (parms == void_list_node)
893     OB_PUTC2 ('v', '\0');
894   else
895     {
896       ALLOCATE_TYPEVEC (parms);
897       nofold = 0;
898       if (for_method)
899         {
900           build_overload_name (TREE_VALUE (parms), 0, 0);
901
902           typevec[maxtype++] = TREE_VALUE (parms);
903           TREE_USED (TREE_VALUE (parms)) = 1;
904
905           if (TREE_CHAIN (parms))
906             build_overload_name (TREE_CHAIN (parms), 0, 1);
907           else
908             OB_PUTC2 ('e', '\0');
909         }
910       else
911         build_overload_name (parms, 0, 1);
912       DEALLOCATE_TYPEVEC (parms);
913     }
914   {
915     tree n = get_identifier (obstack_base (&scratch_obstack));
916     if (IDENTIFIER_OPNAME_P (dname))
917       IDENTIFIER_OPNAME_P (n) = 1;
918     return n;
919   }
920 }
921
922 /* Build an overload name for the type expression TYPE.  */
923 tree
924 build_typename_overload (type)
925      tree type;
926 {
927   tree id;
928
929   OB_INIT ();
930   OB_PUTID (ansi_opname[(int) TYPE_EXPR]);
931   nofold = 1;
932   build_overload_name (type, 0, 1);
933   id = get_identifier (obstack_base (&scratch_obstack));
934   IDENTIFIER_OPNAME_P (id) = 1;
935   IDENTIFIER_GLOBAL_VALUE (id) = TYPE_NAME (type);
936   TREE_TYPE (id) = type;
937   return id;
938 }
939
940 #ifndef NO_DOLLAR_IN_LABEL
941 #define T_DESC_FORMAT "TD$"
942 #define I_DESC_FORMAT "ID$"
943 #define M_DESC_FORMAT "MD$"
944 #else
945 #if !defined(NO_DOT_IN_LABEL)
946 #define T_DESC_FORMAT "TD."
947 #define I_DESC_FORMAT "ID."
948 #define M_DESC_FORMAT "MD."
949 #else
950 #define T_DESC_FORMAT "__t_desc_"
951 #define I_DESC_FORMAT "__i_desc_"
952 #define M_DESC_FORMAT "__m_desc_"
953 #endif
954 #endif
955
956 /* Build an overload name for the type expression TYPE.  */
957 tree
958 build_t_desc_overload (type)
959      tree type;
960 {
961   OB_INIT ();
962   OB_PUTS (T_DESC_FORMAT);
963   nofold = 1;
964
965 #if 0
966   /* Use a different format if the type isn't defined yet.  */
967   if (TYPE_SIZE (type) == NULL_TREE)
968     {
969       char *p;
970       int changed;
971
972       for (p = tname; *p; p++)
973         if (isupper (*p))
974           {
975             changed = 1;
976             *p = tolower (*p);
977           }
978       /* If there's no change, we have an inappropriate T_DESC_FORMAT.  */
979       my_friendly_assert (changed != 0, 249);
980     }
981 #endif
982
983   build_overload_name (type, 0, 1);
984   return get_identifier (obstack_base (&scratch_obstack));
985 }
986
987 /* Top-level interface to explicit overload requests. Allow NAME
988    to be overloaded. Error if NAME is already declared for the current
989    scope. Warning if function is redundantly overloaded. */
990
991 void
992 declare_overloaded (name)
993      tree name;
994 {
995 #ifdef NO_AUTO_OVERLOAD
996   if (is_overloaded (name))
997     warning ("function `%s' already declared overloaded",
998              IDENTIFIER_POINTER (name));
999   else if (IDENTIFIER_GLOBAL_VALUE (name))
1000     error ("overloading function `%s' that is already defined",
1001            IDENTIFIER_POINTER (name));
1002   else
1003     {
1004       TREE_OVERLOADED (name) = 1;
1005       IDENTIFIER_GLOBAL_VALUE (name) = build_tree_list (name, NULL_TREE);
1006       TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (name)) = unknown_type_node;
1007     }
1008 #else
1009   if (current_lang_name == lang_name_cplusplus)
1010     {
1011       if (0)
1012         warning ("functions are implicitly overloaded in C++");
1013     }
1014   else if (current_lang_name == lang_name_c)
1015     error ("overloading function `%s' cannot be done in C language context");
1016   else
1017     my_friendly_abort (76);
1018 #endif
1019 }
1020
1021 #ifdef NO_AUTO_OVERLOAD
1022 /* Check to see if NAME is overloaded. For first approximation,
1023    check to see if its TREE_OVERLOADED is set.  This is used on
1024    IDENTIFIER nodes.  */
1025 int
1026 is_overloaded (name)
1027      tree name;
1028 {
1029   /* @@ */
1030   return (TREE_OVERLOADED (name)
1031           && (! IDENTIFIER_CLASS_VALUE (name) || current_class_type == 0)
1032           && ! IDENTIFIER_LOCAL_VALUE (name));
1033 }
1034 #endif
1035 \f
1036 /* Given a tree_code CODE, and some arguments (at least one),
1037    attempt to use an overloaded operator on the arguments.
1038
1039    For unary operators, only the first argument need be checked.
1040    For binary operators, both arguments may need to be checked.
1041
1042    Member functions can convert class references to class pointers,
1043    for one-level deep indirection.  More than that is not supported.
1044    Operators [](), ()(), and ->() must be member functions.
1045
1046    We call function call building calls with LOOKUP_COMPLAIN if they
1047    are our only hope.  This is true when we see a vanilla operator
1048    applied to something of aggregate type.  If this fails, we are free
1049    to return `error_mark_node', because we will have reported the
1050    error.
1051
1052    Operators NEW and DELETE overload in funny ways: operator new takes
1053    a single `size' parameter, and operator delete takes a pointer to the
1054    storage being deleted.  When overloading these operators, success is
1055    assumed.  If there is a failure, report an error message and return
1056    `error_mark_node'.  */
1057
1058 /* NOSTRICT */
1059 tree
1060 build_opfncall (code, flags, xarg1, xarg2, arg3)
1061      enum tree_code code;
1062      int flags;
1063      tree xarg1, xarg2, arg3;
1064 {
1065   tree rval = 0;
1066   tree arg1, arg2;
1067   tree type1, type2, fnname;
1068   tree fields1 = 0, parms = 0;
1069   tree global_fn;
1070   int try_second;
1071   int binary_is_unary;
1072
1073   if (xarg1 == error_mark_node)
1074     return error_mark_node;
1075
1076   if (code == COND_EXPR)
1077     {
1078       if (TREE_CODE (xarg2) == ERROR_MARK
1079           || TREE_CODE (arg3) == ERROR_MARK)
1080         return error_mark_node;
1081     }
1082   if (code == COMPONENT_REF)
1083     if (TREE_CODE (TREE_TYPE (xarg1)) == POINTER_TYPE)
1084       return rval;
1085
1086   /* First, see if we can work with the first argument */
1087   type1 = TREE_TYPE (xarg1);
1088
1089   /* Some tree codes have length > 1, but we really only want to
1090      overload them if their first argument has a user defined type.  */
1091   switch (code)
1092     {
1093     case PREINCREMENT_EXPR:
1094     case PREDECREMENT_EXPR:
1095     case POSTINCREMENT_EXPR:
1096     case POSTDECREMENT_EXPR:
1097     case COMPONENT_REF:
1098       binary_is_unary = 1;
1099       try_second = 0;
1100       break;
1101
1102       /* ARRAY_REFs and CALL_EXPRs must overload successfully.
1103          If they do not, return error_mark_node instead of NULL_TREE.  */
1104     case ARRAY_REF:
1105       if (xarg2 == error_mark_node)
1106         return error_mark_node;
1107     case CALL_EXPR:
1108       rval = error_mark_node;
1109       binary_is_unary = 0;
1110       try_second = 0;
1111       break;
1112
1113     case VEC_NEW_EXPR:
1114     case NEW_EXPR:
1115       {
1116         tree args = tree_cons (NULL_TREE, xarg2, arg3);
1117         fnname = ansi_opname[(int) code];
1118         if (flags & LOOKUP_GLOBAL)
1119           return build_overload_call (fnname, args, flags & LOOKUP_COMPLAIN,
1120                                       (struct candidate *)0);
1121
1122         rval = build_method_call
1123           (build_indirect_ref (build1 (NOP_EXPR, xarg1, error_mark_node),
1124                                "new"),
1125            fnname, args, NULL_TREE, flags);
1126         if (rval == error_mark_node)
1127           /* User might declare fancy operator new, but invoke it
1128              like standard one.  */
1129           return rval;
1130
1131         TREE_TYPE (rval) = xarg1;
1132         TREE_CALLS_NEW (rval) = 1;
1133         return rval;
1134       }
1135       break;
1136
1137     case VEC_DELETE_EXPR:
1138     case DELETE_EXPR:
1139       {
1140         fnname = ansi_opname[(int) code];
1141         if (flags & LOOKUP_GLOBAL)
1142           return build_overload_call (fnname,
1143                                       build_tree_list (NULL_TREE, xarg1),
1144                                       flags & LOOKUP_COMPLAIN,
1145                                       (struct candidate *)0);
1146
1147         rval = build_method_call
1148           (build_indirect_ref (build1 (NOP_EXPR, TREE_TYPE (xarg1),
1149                                        error_mark_node),
1150                                NULL_PTR),
1151            fnname, tree_cons (NULL_TREE, xarg1,
1152                                build_tree_list (NULL_TREE, xarg2)),
1153            NULL_TREE, flags);
1154         /* This happens when the user mis-declares `operator delete'.
1155            Should now be impossible.  */
1156         my_friendly_assert (rval != error_mark_node, 250);
1157         TREE_TYPE (rval) = void_type_node;
1158         return rval;
1159       }
1160       break;
1161
1162     default:
1163       binary_is_unary = 0;
1164       try_second = tree_code_length [(int) code] == 2;
1165       if (try_second && xarg2 == error_mark_node)
1166         return error_mark_node;
1167       break;
1168     }
1169
1170   if (try_second && xarg2 == error_mark_node)
1171     return error_mark_node;
1172
1173   /* What ever it was, we do not know how to deal with it.  */
1174   if (type1 == NULL_TREE)
1175     return rval;
1176
1177   if (TREE_CODE (type1) == OFFSET_TYPE)
1178     type1 = TREE_TYPE (type1);
1179
1180   if (TREE_CODE (type1) == REFERENCE_TYPE)
1181     {
1182       arg1 = convert_from_reference (xarg1);
1183       type1 = TREE_TYPE (arg1);
1184     }
1185   else
1186     {
1187       arg1 = xarg1;
1188     }
1189
1190   if (!IS_AGGR_TYPE (type1) || TYPE_PTRMEMFUNC_P (type1))
1191     {
1192       /* Try to fail. First, fail if unary */
1193       if (! try_second)
1194         return rval;
1195       /* Second, see if second argument is non-aggregate. */
1196       type2 = TREE_TYPE (xarg2);
1197       if (TREE_CODE (type2) == OFFSET_TYPE)
1198         type2 = TREE_TYPE (type2);
1199       if (TREE_CODE (type2) == REFERENCE_TYPE)
1200         {
1201           arg2 = convert_from_reference (xarg2);
1202           type2 = TREE_TYPE (arg2);
1203         }
1204       else
1205         {
1206           arg2 = xarg2;
1207         }
1208
1209       if (!IS_AGGR_TYPE (type2))
1210         return rval;
1211       try_second = 0;
1212     }
1213
1214   if (try_second)
1215     {
1216       /* First arg may succeed; see whether second should.  */
1217       type2 = TREE_TYPE (xarg2);
1218       if (TREE_CODE (type2) == OFFSET_TYPE)
1219         type2 = TREE_TYPE (type2);
1220       if (TREE_CODE (type2) == REFERENCE_TYPE)
1221         {
1222           arg2 = convert_from_reference (xarg2);
1223           type2 = TREE_TYPE (arg2);
1224         }
1225       else
1226         {
1227           arg2 = xarg2;
1228         }
1229
1230       if (! IS_AGGR_TYPE (type2))
1231         try_second = 0;
1232     }
1233
1234   if (type1 == unknown_type_node
1235       || (try_second && TREE_TYPE (xarg2) == unknown_type_node))
1236     {
1237       /* This will not be implemented in the foreseeable future.  */
1238       return rval;
1239     }
1240
1241   if (code == MODIFY_EXPR)
1242     fnname = ansi_assopname[(int) TREE_CODE (arg3)];
1243   else
1244     fnname = ansi_opname[(int) code];
1245
1246   global_fn = lookup_name_nonclass (fnname);
1247
1248   /* This is the last point where we will accept failure.  This
1249      may be too eager if we wish an overloaded operator not to match,
1250      but would rather a normal operator be called on a type-converted
1251      argument.  */
1252
1253   if (IS_AGGR_TYPE (type1))
1254     {
1255       fields1 = lookup_fnfields (TYPE_BINFO (type1), fnname, 0);
1256       /* ARM $13.4.7, prefix/postfix ++/--.  */
1257       if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1258         {
1259           xarg2 = integer_zero_node;
1260           binary_is_unary = 0;
1261
1262           if (fields1)
1263             {
1264               tree t, t2;
1265               int have_postfix = 0;
1266
1267               /* Look for an `operator++ (int)'.  If they didn't have
1268                  one, then we fall back to the old way of doing things.  */
1269               for (t = TREE_VALUE (fields1); t ; t = TREE_CHAIN (t))
1270                 {
1271                   t2 = TYPE_ARG_TYPES (TREE_TYPE (t));
1272                   if (TREE_CHAIN (t2) != NULL_TREE
1273                       && TREE_VALUE (TREE_CHAIN (t2)) == integer_type_node)
1274                     {
1275                       have_postfix = 1;
1276                       break;
1277                     }
1278                 }
1279
1280               if (! have_postfix)
1281                 {
1282                   char *op = POSTINCREMENT_EXPR ? "++" : "--";
1283
1284                   /* There's probably a LOT of code in the world that
1285                      relies upon this old behavior.  So we'll only give this
1286                      warning when we've been given -pedantic.  A few
1287                      releases after 2.4, we'll convert this to be a pedwarn
1288                      or something else more appropriate.  */
1289                   if (pedantic)
1290                     warning ("no `operator%s (int)' declared for postfix `%s'",
1291                              op, op);
1292                   xarg2 = NULL_TREE;
1293                   binary_is_unary = 1;
1294                 }
1295             }
1296         }
1297     }
1298
1299   if (fields1 == NULL_TREE && global_fn == NULL_TREE)
1300     return rval;
1301
1302   /* If RVAL winds up being `error_mark_node', we will return
1303      that... There is no way that normal semantics of these
1304      operators will succeed.  */
1305
1306   /* This argument may be an uncommitted OFFSET_REF.  This is
1307      the case for example when dealing with static class members
1308      which are referenced from their class name rather than
1309      from a class instance.  */
1310   if (TREE_CODE (xarg1) == OFFSET_REF
1311       && TREE_CODE (TREE_OPERAND (xarg1, 1)) == VAR_DECL)
1312     xarg1 = TREE_OPERAND (xarg1, 1);
1313   if (try_second && xarg2 && TREE_CODE (xarg2) == OFFSET_REF
1314       && TREE_CODE (TREE_OPERAND (xarg2, 1)) == VAR_DECL)
1315     xarg2 = TREE_OPERAND (xarg2, 1);
1316
1317   if (global_fn)
1318     flags |= LOOKUP_GLOBAL;
1319
1320   if (code == CALL_EXPR)
1321     {
1322       /* This can only be a member function.  */
1323       return build_method_call (xarg1, fnname, xarg2,
1324                                 NULL_TREE, LOOKUP_NORMAL);
1325     }
1326   else if (tree_code_length[(int) code] == 1 || binary_is_unary)
1327     {
1328       parms = NULL_TREE;
1329       rval = build_method_call (xarg1, fnname, NULL_TREE, NULL_TREE, flags);
1330     }
1331   else if (code == COND_EXPR)
1332     {
1333       parms = tree_cons (0, xarg2, build_tree_list (NULL_TREE, arg3));
1334       rval = build_method_call (xarg1, fnname, parms, NULL_TREE, flags);
1335     }
1336   else if (code == METHOD_CALL_EXPR)
1337     {
1338       /* must be a member function.  */
1339       parms = tree_cons (NULL_TREE, xarg2, arg3);
1340       return build_method_call (xarg1, fnname, parms, NULL_TREE,
1341                                 LOOKUP_NORMAL);
1342     }
1343   else if (fields1)
1344     {
1345       parms = build_tree_list (NULL_TREE, xarg2);
1346       rval = build_method_call (xarg1, fnname, parms, NULL_TREE, flags);
1347     }
1348   else
1349     {
1350       parms = tree_cons (NULL_TREE, xarg1,
1351                          build_tree_list (NULL_TREE, xarg2));
1352       rval = build_overload_call (fnname, parms, flags,
1353                                   (struct candidate *)0);
1354     }
1355
1356   return rval;
1357 }
1358 \f
1359 /* This function takes an identifier, ID, and attempts to figure out what
1360    it means. There are a number of possible scenarios, presented in increasing
1361    order of hair:
1362
1363    1) not in a class's scope
1364    2) in class's scope, member name of the class's method
1365    3) in class's scope, but not a member name of the class
1366    4) in class's scope, member name of a class's variable
1367
1368    NAME is $1 from the bison rule. It is an IDENTIFIER_NODE.
1369    VALUE is $$ from the bison rule. It is the value returned by lookup_name ($1)
1370    yychar is the pending input character (suitably encoded :-).
1371
1372    As a last ditch, try to look up the name as a label and return that
1373    address.
1374
1375    Values which are declared as being of REFERENCE_TYPE are
1376    automatically dereferenced here (as a hack to make the
1377    compiler faster).  */
1378
1379 tree
1380 hack_identifier (value, name, yychar)
1381      tree value, name;
1382      int yychar;
1383 {
1384   tree type;
1385
1386   if (TREE_CODE (value) == ERROR_MARK)
1387     {
1388       if (current_class_name)
1389         {
1390           tree fields = lookup_fnfields (TYPE_BINFO (current_class_type), name, 1);
1391           if (fields == error_mark_node)
1392             return error_mark_node;
1393           if (fields)
1394             {
1395               tree fndecl;
1396
1397               fndecl = TREE_VALUE (fields);
1398               my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 251);
1399               if (DECL_CHAIN (fndecl) == NULL_TREE)
1400                 {
1401                   warning ("methods cannot be converted to function pointers");
1402                   return fndecl;
1403                 }
1404               else
1405                 {
1406                   error ("ambiguous request for method pointer `%s'",
1407                          IDENTIFIER_POINTER (name));
1408                   return error_mark_node;
1409                 }
1410             }
1411         }
1412       if (flag_labels_ok && IDENTIFIER_LABEL_VALUE (name))
1413         {
1414           return IDENTIFIER_LABEL_VALUE (name);
1415         }
1416       return error_mark_node;
1417     }
1418
1419   type = TREE_TYPE (value);
1420   if (TREE_CODE (value) == FIELD_DECL)
1421     {
1422       if (current_class_decl == NULL_TREE)
1423         {
1424           error ("request for member `%s' in static member function",
1425                  IDENTIFIER_POINTER (DECL_NAME (value)));
1426           return error_mark_node;
1427         }
1428       TREE_USED (current_class_decl) = 1;
1429       if (yychar == '(')
1430         if (! ((TYPE_LANG_SPECIFIC (type)
1431                 && TYPE_OVERLOADS_CALL_EXPR (type))
1432                || (TREE_CODE (type) == REFERENCE_TYPE
1433                    && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
1434                    && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (type))))
1435             && TREE_CODE (type) != FUNCTION_TYPE
1436             && TREE_CODE (type) != METHOD_TYPE
1437             && !TYPE_PTRMEMFUNC_P (type)
1438             && (TREE_CODE (type) != POINTER_TYPE
1439                 || (TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE
1440                     && TREE_CODE (TREE_TYPE (type)) != METHOD_TYPE)))
1441           {
1442             error ("component `%s' is not a method",
1443                    IDENTIFIER_POINTER (name));
1444             return error_mark_node;
1445           }
1446       /* Mark so that if we are in a constructor, and then find that
1447          this field was initialized by a base initializer,
1448          we can emit an error message.  */
1449       TREE_USED (value) = 1;
1450       return build_component_ref (C_C_D, name, 0, 1);
1451     }
1452
1453   if (TREE_CODE (value) == TREE_LIST)
1454     {
1455       tree t = value;
1456       while (t && TREE_CODE (t) == TREE_LIST)
1457         {
1458           assemble_external (TREE_VALUE (t));
1459           TREE_USED (t) = 1;
1460           t = TREE_CHAIN (t);
1461         }
1462     }
1463   else
1464     {
1465       assemble_external (value);
1466       TREE_USED (value) = 1;
1467     }
1468
1469   if (TREE_CODE_CLASS (TREE_CODE (value)) == 'd' && DECL_NONLOCAL (value))
1470     {
1471       if (DECL_LANG_SPECIFIC (value)
1472           && DECL_CLASS_CONTEXT (value) != current_class_type)
1473         {
1474           tree path;
1475           enum access_type access;
1476           register tree context
1477             = (TREE_CODE (value) == FUNCTION_DECL && DECL_VIRTUAL_P (value))
1478               ? DECL_CLASS_CONTEXT (value)
1479               : DECL_CONTEXT (value);
1480
1481           get_base_distance (context, current_class_type, 0, &path);
1482           if (path)
1483             {
1484               access = compute_access (path, value);
1485               if (access != access_public)
1486                 {
1487                   if (TREE_CODE (value) == VAR_DECL)
1488                     error ("static member `%s' is %s",
1489                            IDENTIFIER_POINTER (name),
1490                            TREE_PRIVATE (value) ? "private" :
1491                            "from a private base class");
1492                   else
1493                     error ("enum `%s' is from private base class",
1494                            IDENTIFIER_POINTER (name));
1495                   return error_mark_node;
1496                 }
1497             }
1498         }
1499       return value;
1500     }
1501   if (TREE_CODE (value) == TREE_LIST && TREE_NONLOCAL_FLAG (value))
1502     {
1503       if (type == 0)
1504         {
1505           error ("request for member `%s' is ambiguous in multiple inheritance lattice",
1506                  IDENTIFIER_POINTER (name));
1507           return error_mark_node;
1508         }
1509
1510       return value;
1511     }
1512
1513   if (TREE_CODE (type) == REFERENCE_TYPE)
1514     {
1515       my_friendly_assert (TREE_CODE (value) == VAR_DECL
1516                           || TREE_CODE (value) == PARM_DECL
1517                           || TREE_CODE (value) == RESULT_DECL, 252);
1518       if (DECL_REFERENCE_SLOT (value))
1519         return DECL_REFERENCE_SLOT (value);
1520     }
1521   return value;
1522 }
1523
1524 \f
1525 #if 0
1526 /* Given an object OF, and a type conversion operator COMPONENT
1527    build a call to the conversion operator, if a call is requested,
1528    or return the address (as a pointer to member function) if one is not.
1529
1530    OF can be a TYPE_DECL or any kind of datum that would normally
1531    be passed to `build_component_ref'.  It may also be NULL_TREE,
1532    in which case `current_class_type' and `current_class_decl'
1533    provide default values.
1534
1535    BASETYPE_PATH, if non-null, is the path of basetypes
1536    to go through before we get the the instance of interest.
1537
1538    PROTECT says whether we apply C++ scoping rules or not.  */
1539 tree
1540 build_component_type_expr (of, component, basetype_path, protect)
1541      tree of, component, basetype_path;
1542      int protect;
1543 {
1544   tree cname = NULL_TREE;
1545   tree tmp, last;
1546   tree name;
1547   int flags = protect ? LOOKUP_NORMAL : LOOKUP_COMPLAIN;
1548
1549   if (of)
1550     my_friendly_assert (IS_AGGR_TYPE (TREE_TYPE (of)), 253);
1551   my_friendly_assert (TREE_CODE (component) == TYPE_EXPR, 254);
1552
1553   tmp = TREE_OPERAND (component, 0);
1554   last = NULL_TREE;
1555
1556   while (tmp)
1557     {
1558       switch (TREE_CODE (tmp))
1559         {
1560         case CALL_EXPR:
1561           if (last)
1562             TREE_OPERAND (last, 0) = TREE_OPERAND (tmp, 0);
1563           else
1564             TREE_OPERAND (component, 0) = TREE_OPERAND (tmp, 0);
1565
1566           last = groktypename (build_tree_list (TREE_TYPE (component),
1567                                                 TREE_OPERAND (component, 0)));
1568           name = build_typename_overload (last);
1569           TREE_TYPE (name) = last;
1570
1571           if (TREE_OPERAND (tmp, 0)
1572               && TREE_OPERAND (tmp, 0) != void_list_node)
1573             {
1574               cp_error ("`operator %T' requires empty parameter list", last);
1575               TREE_OPERAND (tmp, 0) = NULL_TREE;
1576             }
1577
1578           if (of && TREE_CODE (of) != TYPE_DECL)
1579             return build_method_call (of, name, NULL_TREE, NULL_TREE, flags);
1580           else if (of)
1581             {
1582               tree this_this;
1583
1584               if (current_class_decl == NULL_TREE)
1585                 {
1586                   cp_error ("object required for `operator %T' call",
1587                             TREE_TYPE (name));
1588                   return error_mark_node;
1589                 }
1590
1591               this_this = convert_pointer_to (TREE_TYPE (of),
1592                                               current_class_decl);
1593               this_this = build_indirect_ref (this_this, NULL_PTR);
1594               return build_method_call (this_this, name, NULL_TREE,
1595                                         NULL_TREE, flags | LOOKUP_NONVIRTUAL);
1596             }
1597           else if (current_class_decl)
1598             return build_method_call (tmp, name, NULL_TREE, NULL_TREE, flags);
1599
1600           cp_error ("object required for `operator %T' call",
1601                     TREE_TYPE (name));
1602           return error_mark_node;
1603
1604         case INDIRECT_REF:
1605         case ADDR_EXPR:
1606         case ARRAY_REF:
1607           break;
1608
1609         case SCOPE_REF:
1610           my_friendly_assert (cname == 0, 255);
1611           cname = TREE_OPERAND (tmp, 0);
1612           tmp = TREE_OPERAND (tmp, 1);
1613           break;
1614
1615         default:
1616           my_friendly_abort (77);
1617         }
1618       last = tmp;
1619       tmp = TREE_OPERAND (tmp, 0);
1620     }
1621
1622   last = groktypename (build_tree_list (TREE_TYPE (component), TREE_OPERAND (component, 0)));
1623   name = build_typename_overload (last);
1624   TREE_TYPE (name) = last;
1625   if (of && TREE_CODE (of) == TYPE_DECL)
1626     {
1627       if (cname == NULL_TREE)
1628         {
1629           cname = DECL_NAME (of);
1630           of = NULL_TREE;
1631         }
1632       else my_friendly_assert (cname == DECL_NAME (of), 256);
1633     }
1634
1635   if (of)
1636     {
1637       tree this_this;
1638
1639       if (current_class_decl == NULL_TREE)
1640         {
1641           cp_error ("object required for `operator %T' call",
1642                     TREE_TYPE (name));
1643           return error_mark_node;
1644         }
1645
1646       this_this = convert_pointer_to (TREE_TYPE (of), current_class_decl);
1647       return build_component_ref (this_this, name, 0, protect);
1648     }
1649   else if (cname)
1650     return build_offset_ref (cname, name);
1651   else if (current_class_name)
1652     return build_offset_ref (current_class_name, name);
1653
1654   cp_error ("object required for `operator %T' member reference",
1655             TREE_TYPE (name));
1656   return error_mark_node;
1657 }
1658 #endif
1659 \f
1660 static char *
1661 thunk_printable_name (decl)
1662      tree decl;
1663 {
1664   return "<thunk function>";
1665 }
1666
1667 tree
1668 make_thunk (function, delta)
1669      tree function;
1670      int delta;
1671 {
1672   char buffer[250];
1673   tree thunk_fndecl;
1674   tree thunk;
1675   static int thunk_number = 0;
1676   tree func_decl;
1677   if (TREE_CODE (function) != ADDR_EXPR)
1678     abort ();
1679   func_decl = TREE_OPERAND (function, 0);
1680   if (TREE_CODE (func_decl) != FUNCTION_DECL)
1681     abort ();
1682   sprintf (buffer, "__thunk_%d_%d", -delta, thunk_number++);
1683   thunk = build_decl (THUNK_DECL, get_identifier (buffer),
1684                       TREE_TYPE (func_decl));
1685   DECL_RESULT (thunk)
1686     = build_decl (RESULT_DECL, NULL_TREE, TREE_TYPE (vtable_entry_type));
1687   make_function_rtl (thunk);
1688   DECL_INITIAL (thunk) = function;
1689   THUNK_DELTA (thunk) = delta;
1690   return thunk;
1691 }
1692
1693 void
1694 emit_thunk (thunk_fndecl)
1695   tree thunk_fndecl;
1696 {
1697   rtx insns;
1698   char *fnname;
1699   char buffer[250];
1700   tree argp;
1701   struct args_size stack_args_size;
1702   tree function = TREE_OPERAND (DECL_INITIAL (thunk_fndecl), 0);
1703   int delta = THUNK_DELTA (thunk_fndecl);
1704   int tem;
1705   int failure = 0;
1706   extern int current_call_is_indirect; /* Needed for (at least) HPPA. */
1707
1708   /* Used to remember which regs we need to emit a USE rtx for. */
1709   rtx need_use[FIRST_PSEUDO_REGISTER];
1710   int need_use_count = 0;
1711
1712   /* rtx for the 'this' parameter. */
1713   rtx this_rtx = 0, this_reg_rtx = 0, fixed_this_rtx;
1714
1715   char *(*save_decl_printable_name) () = decl_printable_name;
1716   /* Data on reg parms scanned so far.  */
1717   CUMULATIVE_ARGS args_so_far;
1718
1719   if (TREE_ASM_WRITTEN (thunk_fndecl))
1720     return;
1721
1722   decl_printable_name = thunk_printable_name;
1723   if (current_function_decl)
1724     abort ();
1725   current_function_decl = thunk_fndecl;
1726   init_function_start (thunk_fndecl, input_filename, lineno);
1727   pushlevel (0);
1728   expand_start_bindings (1);
1729
1730   /* Start updating where the next arg would go.  */
1731   INIT_CUMULATIVE_ARGS (args_so_far, TREE_TYPE (function), NULL_RTX);
1732   stack_args_size.constant = 0;
1733   stack_args_size.var = 0;
1734   /* SETUP for possible structure return address FIXME */
1735
1736   /* Now look through all the parameters, make sure that we
1737      don't clobber any registers used for parameters.
1738      Also, pick up an rtx for the first "this" parameter. */
1739   for (argp = TYPE_ARG_TYPES (TREE_TYPE (function));
1740        argp != NULL_TREE;
1741        argp = TREE_CHAIN (argp))
1742
1743     {
1744       tree passed_type = TREE_VALUE (argp);
1745       register rtx entry_parm;
1746       int named = 1; /* FIXME */
1747       struct args_size stack_offset;
1748       struct args_size arg_size;
1749
1750       if (passed_type == void_type_node)
1751         break;
1752
1753       if ((TREE_CODE (TYPE_SIZE (passed_type)) != INTEGER_CST
1754            && contains_placeholder_p (TYPE_SIZE (passed_type)))
1755 #ifdef FUNCTION_ARG_PASS_BY_REFERENCE
1756           || FUNCTION_ARG_PASS_BY_REFERENCE (args_so_far,
1757                                              TYPE_MODE (passed_type),
1758                                              passed_type, named)
1759 #endif
1760           )
1761         passed_type = build_pointer_type (passed_type);
1762
1763       entry_parm = FUNCTION_ARG (args_so_far,
1764                                  TYPE_MODE (passed_type),
1765                                  passed_type,
1766                                  named);
1767       if (entry_parm != 0)
1768         need_use[need_use_count++] = entry_parm;
1769
1770       locate_and_pad_parm (TYPE_MODE (passed_type), passed_type,
1771 #ifdef STACK_PARMS_IN_REG_PARM_AREA
1772                            1,
1773 #else
1774                            entry_parm != 0,
1775 #endif
1776                            thunk_fndecl,
1777                            &stack_args_size, &stack_offset, &arg_size);
1778
1779 /*    REGNO (entry_parm);*/
1780       if (this_rtx == 0)
1781         {
1782           this_reg_rtx = entry_parm;
1783           if (!entry_parm)
1784             {
1785               rtx offset_rtx = ARGS_SIZE_RTX (stack_offset);
1786
1787               rtx internal_arg_pointer, stack_parm;
1788
1789               if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
1790                    || ! (fixed_regs[ARG_POINTER_REGNUM]
1791                          || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
1792                 internal_arg_pointer = copy_to_reg (virtual_incoming_args_rtx);
1793               else
1794                 internal_arg_pointer = virtual_incoming_args_rtx;
1795
1796               if (offset_rtx == const0_rtx)
1797                 entry_parm = gen_rtx (MEM, TYPE_MODE (passed_type),
1798                                       internal_arg_pointer);
1799               else
1800                 entry_parm = gen_rtx (MEM, TYPE_MODE (passed_type),
1801                                       gen_rtx (PLUS, Pmode,
1802                                                internal_arg_pointer, 
1803                                                offset_rtx));
1804             }
1805           
1806           this_rtx = entry_parm;
1807         }
1808
1809       FUNCTION_ARG_ADVANCE (args_so_far,
1810                             TYPE_MODE (passed_type),
1811                             passed_type,
1812                             named);
1813     }
1814
1815   fixed_this_rtx = plus_constant (this_rtx, delta);
1816   if (this_rtx != fixed_this_rtx)
1817     emit_move_insn (this_rtx, fixed_this_rtx);
1818
1819   if (this_reg_rtx)
1820     emit_insn (gen_rtx (USE, VOIDmode, this_reg_rtx));
1821
1822   emit_indirect_jump (XEXP (DECL_RTL (function), 0));
1823
1824   while (need_use_count > 0)
1825     emit_insn (gen_rtx (USE, VOIDmode, need_use[--need_use_count]));
1826
1827   expand_end_bindings (NULL, 1, 0);
1828   poplevel (0, 0, 0);
1829
1830   TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1831
1832   /* From now on, allocate rtl in current_obstack, not in saveable_obstack.
1833      Note that that may have been done above, in save_for_inline_copying.
1834      The call to resume_temporary_allocation near the end of this function
1835      goes back to the usual state of affairs.  */
1836
1837   rtl_in_current_obstack ();
1838
1839   insns = get_insns ();
1840
1841   /* Copy any shared structure that should not be shared.  */
1842
1843   unshare_all_rtl (insns);
1844
1845   /* Instantiate all virtual registers.  */
1846
1847   instantiate_virtual_regs (current_function_decl, get_insns ());
1848
1849   /* We are no longer anticipating cse in this function, at least.  */
1850
1851   cse_not_expected = 1;
1852
1853   /* Now we choose between stupid (pcc-like) register allocation
1854      (if we got the -noreg switch and not -opt)
1855      and smart register allocation.  */
1856
1857   if (optimize > 0)                     /* Stupid allocation probably won't work */
1858     obey_regdecls = 0;          /* if optimizations being done.  */
1859
1860   regclass_init ();
1861
1862   regclass (insns, max_reg_num ());
1863   if (obey_regdecls)
1864     {
1865       stupid_life_analysis (insns, max_reg_num (), NULL);
1866       failure = reload (insns, 0, NULL);
1867     }
1868   else
1869     {
1870       /* Do control and data flow analysis,
1871          and write some of the results to dump file.  */
1872
1873       flow_analysis (insns, max_reg_num (), NULL);
1874       local_alloc ();
1875       failure = global_alloc (NULL);
1876     }
1877
1878   reload_completed = 1;
1879
1880 #ifdef LEAF_REGISTERS
1881   leaf_function = 0;
1882   if (optimize > 0 && only_leaf_regs_used () && leaf_function_p ())
1883     leaf_function = 1;
1884 #endif
1885
1886   /* If a machine dependent reorganization is needed, call it.  */
1887 #ifdef MACHINE_DEPENDENT_REORG
1888    MACHINE_DEPENDENT_REORG (insns);
1889 #endif
1890
1891   /* Now turn the rtl into assembler code.  */
1892
1893     {
1894       char *fnname = XSTR (XEXP (DECL_RTL (thunk_fndecl), 0), 0);
1895       assemble_start_function (thunk_fndecl, fnname);
1896       final (insns, asm_out_file, optimize, 0);
1897       assemble_end_function (thunk_fndecl, fnname);
1898     };
1899
1900  exit_rest_of_compilation:
1901
1902   reload_completed = 0;
1903
1904   /* Cancel the effect of rtl_in_current_obstack.  */
1905
1906   resume_temporary_allocation ();
1907
1908   decl_printable_name = save_decl_printable_name;
1909   current_function_decl = 0;
1910 }