import of gcc-2.8
[official-gcc.git] / gcc / cp / method.c
blob0e407fe1ab8c88ab1432490baedcec2b3e8c009d
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)
6 This file is part of GNU CC.
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.
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.
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. */
24 #ifndef PARM_CAN_BE_ARRAY_TYPE
25 #define PARM_CAN_BE_ARRAY_TYPE 1
26 #endif
28 /* Handle method declarations. */
29 #include "config.h"
30 #include <stdio.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"
42 #ifdef HAVE_STRING_H
43 #include <string.h>
44 #else
45 extern char *index ();
46 #endif
48 /* TREE_LIST of the current inline functions that need to be
49 processed. */
50 struct pending_inline *pending_inlines;
52 int static_labelno;
54 #define obstack_chunk_alloc xmalloc
55 #define obstack_chunk_free free
57 /* Obstack where we build text strings for overloading, etc. */
58 static struct obstack scratch_obstack;
59 static char *scratch_firstobj;
61 static void icat PROTO((HOST_WIDE_INT));
62 static void dicat PROTO((HOST_WIDE_INT, HOST_WIDE_INT));
63 static void flush_repeats PROTO((tree));
64 static void build_overload_identifier PROTO((tree));
65 static void build_overload_nested_name PROTO((tree));
66 static void build_overload_int PROTO((tree, int));
67 static void build_overload_identifier PROTO((tree));
68 static void build_qualified_name PROTO((tree));
69 static void build_overload_value PROTO((tree, tree, int));
70 static char *thunk_printable_name PROTO((tree));
71 static void do_build_assign_ref PROTO((tree));
72 static void do_build_copy_constructor PROTO((tree));
73 static tree largest_union_member PROTO((tree));
74 static tree build_decl_overload_real PROTO((tree, tree, tree, tree,
75 tree, int));
76 static void build_template_parm_names PROTO((tree, tree));
77 static void build_underscore_int PROTO((int));
79 # define OB_INIT() (scratch_firstobj ? (obstack_free (&scratch_obstack, scratch_firstobj), 0) : 0)
80 # define OB_PUTC(C) (obstack_1grow (&scratch_obstack, (C)))
81 # define OB_PUTC2(C1,C2) \
82 (obstack_1grow (&scratch_obstack, (C1)), obstack_1grow (&scratch_obstack, (C2)))
83 # define OB_PUTS(S) (obstack_grow (&scratch_obstack, (S), sizeof (S) - 1))
84 # define OB_PUTID(ID) \
85 (obstack_grow (&scratch_obstack, IDENTIFIER_POINTER (ID), \
86 IDENTIFIER_LENGTH (ID)))
87 # define OB_PUTCP(S) (obstack_grow (&scratch_obstack, (S), strlen (S)))
88 # define OB_FINISH() (obstack_1grow (&scratch_obstack, '\0'))
89 # define OB_LAST() (obstack_next_free (&scratch_obstack)[-1])
91 void
92 init_method ()
94 gcc_obstack_init (&scratch_obstack);
95 scratch_firstobj = (char *)obstack_alloc (&scratch_obstack, 0);
98 /* This must be large enough to hold any printed integer or floating-point
99 value. */
100 static char digit_buffer[128];
102 /* Move inline function definitions out of structure so that they
103 can be processed normally. CNAME is the name of the class
104 we are working from, METHOD_LIST is the list of method lists
105 of the structure. We delete friend methods here, after
106 saving away their inline function definitions (if any). */
108 void
109 do_inline_function_hair (type, friend_list)
110 tree type, friend_list;
112 tree method = TYPE_METHODS (type);
114 if (method && TREE_CODE (method) == TREE_VEC)
116 if (TREE_VEC_ELT (method, 1))
117 method = TREE_VEC_ELT (method, 1);
118 else if (TREE_VEC_ELT (method, 0))
119 method = TREE_VEC_ELT (method, 0);
120 else
121 method = TREE_VEC_ELT (method, 2);
124 while (method)
126 /* Do inline member functions. */
127 struct pending_inline *info = DECL_PENDING_INLINE_INFO (method);
128 if (info)
130 tree args;
132 my_friendly_assert (info->fndecl == method, 238);
133 args = DECL_ARGUMENTS (method);
134 while (args)
136 DECL_CONTEXT (args) = method;
137 args = TREE_CHAIN (args);
140 /* Allow this decl to be seen in global scope. Don't do this for
141 local class methods, though. */
142 if (! current_function_decl)
143 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (method)) = method;
145 method = TREE_CHAIN (method);
147 while (friend_list)
149 tree fndecl = TREE_VALUE (friend_list);
150 struct pending_inline *info = DECL_PENDING_INLINE_INFO (fndecl);
151 if (info)
153 tree args;
155 my_friendly_assert (info->fndecl == fndecl, 239);
156 args = DECL_ARGUMENTS (fndecl);
157 while (args)
159 DECL_CONTEXT (args) = fndecl;
160 args = TREE_CHAIN (args);
163 /* Allow this decl to be seen in global scope */
164 if (! current_function_decl)
165 IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (fndecl)) = fndecl;
168 friend_list = TREE_CHAIN (friend_list);
172 /* Report an argument type mismatch between the best declared function
173 we could find and the current argument list that we have. */
175 void
176 report_type_mismatch (cp, parmtypes, name_kind)
177 struct candidate *cp;
178 tree parmtypes;
179 char *name_kind;
181 int i = cp->u.bad_arg;
182 tree ttf, tta;
183 char *tmp_firstobj;
185 switch (i)
187 case -4:
188 my_friendly_assert (TREE_CODE (cp->function) == TEMPLATE_DECL, 240);
189 cp_error ("type unification failed for function template `%#D'",
190 cp->function);
191 return;
193 case -2:
194 cp_error ("too few arguments for %s `%#D'", name_kind, cp->function);
195 return;
196 case -1:
197 cp_error ("too many arguments for %s `%#D'", name_kind, cp->function);
198 return;
199 case 0:
200 if (TREE_CODE (TREE_TYPE (cp->function)) != METHOD_TYPE)
201 break;
202 case -3:
203 /* Happens when the implicit object parameter is rejected. */
204 my_friendly_assert (! TYPE_READONLY (TREE_TYPE (TREE_VALUE (parmtypes))),
205 241);
206 if (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (TREE_VALUE (parmtypes))))
207 && ! TYPE_VOLATILE (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (cp->function))))))
208 cp_error ("call to non-volatile %s `%#D' with volatile object",
209 name_kind, cp->function);
210 else
211 cp_error ("call to non-const %s `%#D' with const object",
212 name_kind, cp->function);
213 return;
216 ttf = TYPE_ARG_TYPES (TREE_TYPE (cp->function));
217 tta = parmtypes;
219 while (i-- > 0)
221 ttf = TREE_CHAIN (ttf);
222 tta = TREE_CHAIN (tta);
225 OB_INIT ();
226 OB_PUTS ("bad argument ");
227 sprintf (digit_buffer, "%d", cp->u.bad_arg
228 - (TREE_CODE (TREE_TYPE (cp->function)) == METHOD_TYPE)
229 + 1);
230 OB_PUTCP (digit_buffer);
232 OB_PUTS (" for function `");
233 OB_PUTCP (decl_as_string (cp->function, 1));
234 OB_PUTS ("' (type was ");
236 /* Reset `i' so that type printing routines do the right thing. */
237 if (tta)
239 enum tree_code code = TREE_CODE (TREE_TYPE (TREE_VALUE (tta)));
240 if (code == ERROR_MARK)
241 OB_PUTS ("(failed type instantiation)");
242 else
244 i = (code == FUNCTION_TYPE || code == METHOD_TYPE);
245 OB_PUTCP (type_as_string (TREE_TYPE (TREE_VALUE (tta)), 1));
248 else OB_PUTS ("void");
249 OB_PUTC (')');
250 OB_FINISH ();
252 tmp_firstobj = (char *)alloca (obstack_object_size (&scratch_obstack));
253 bcopy (obstack_base (&scratch_obstack), tmp_firstobj,
254 obstack_object_size (&scratch_obstack));
255 error (tmp_firstobj);
258 /* Here is where overload code starts. */
260 /* Array of types seen so far in top-level call to `build_overload_name'.
261 Allocated and deallocated by caller. */
262 static tree *typevec;
264 /* Number of types interned by `build_overload_name' so far. */
265 static int maxtype;
267 /* Number of occurrences of last type seen. */
268 static int nrepeats;
270 /* Nonzero if we should not try folding parameter types. */
271 static int nofold;
273 #define ALLOCATE_TYPEVEC(PARMTYPES) \
274 do { maxtype = 0, nrepeats = 0; \
275 typevec = (tree *)alloca (list_length (PARMTYPES) * sizeof (tree)); } while (0)
277 #define DEALLOCATE_TYPEVEC(PARMTYPES) \
278 do { tree t = (PARMTYPES); \
279 while (t) { TREE_USED (TREE_VALUE (t)) = 0; t = TREE_CHAIN (t); } \
280 } while (0)
282 /* Code to concatenate an asciified integer to a string. */
284 static
285 #ifdef __GNUC__
286 __inline
287 #endif
288 void
289 icat (i)
290 HOST_WIDE_INT i;
292 unsigned HOST_WIDE_INT ui;
294 /* Handle this case first, to go really quickly. For many common values,
295 the result of ui/10 below is 1. */
296 if (i == 1)
298 OB_PUTC ('1');
299 return;
302 if (i >= 0)
303 ui = i;
304 else
306 OB_PUTC ('m');
307 ui = -i;
310 if (ui >= 10)
311 icat (ui / 10);
313 OB_PUTC ('0' + (ui % 10));
316 static void
317 dicat (lo, hi)
318 HOST_WIDE_INT lo, hi;
320 unsigned HOST_WIDE_INT ulo, uhi, qlo, qhi;
322 if (hi >= 0)
324 uhi = hi;
325 ulo = lo;
327 else
329 uhi = (lo == 0 ? -hi : -hi-1);
330 ulo = -lo;
332 if (uhi == 0
333 && ulo < ((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)))
335 icat (ulo);
336 return;
338 /* Divide 2^HOST_WIDE_INT*uhi+ulo by 10. */
339 qhi = uhi / 10;
340 uhi = uhi % 10;
341 qlo = uhi * (((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)) / 5);
342 qlo += ulo / 10;
343 ulo = ulo % 10;
344 ulo += uhi * (((unsigned HOST_WIDE_INT)1 << (HOST_BITS_PER_WIDE_INT - 1)) % 5)
345 * 2;
346 qlo += ulo / 10;
347 ulo = ulo % 10;
348 /* Quotient is 2^HOST_WIDE_INT*qhi+qlo, remainder is ulo. */
349 dicat (qlo, qhi);
350 OB_PUTC ('0' + ulo);
353 static
354 #ifdef __GNUC__
355 __inline
356 #endif
357 void
358 flush_repeats (type)
359 tree type;
361 int tindex = 0;
363 while (typevec[tindex] != type)
364 tindex++;
366 if (nrepeats > 1)
368 OB_PUTC ('N');
369 icat (nrepeats);
370 if (nrepeats > 9)
371 OB_PUTC ('_');
373 else
374 OB_PUTC ('T');
375 nrepeats = 0;
376 icat (tindex);
377 if (tindex > 9)
378 OB_PUTC ('_');
381 static int numeric_output_need_bar;
383 static void
384 build_overload_nested_name (decl)
385 tree decl;
387 if (DECL_CONTEXT (decl))
389 tree context = DECL_CONTEXT (decl);
390 /* For a template type parameter, we want to output an 'Xn'
391 rather than 'T' or some such. */
392 if (TREE_CODE (context) == TEMPLATE_TYPE_PARM)
393 build_overload_name (context, 0, 0);
394 else
396 if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
397 context = TYPE_NAME (context);
398 build_overload_nested_name (context);
402 if (TREE_CODE (decl) == FUNCTION_DECL)
404 tree name = DECL_ASSEMBLER_NAME (decl);
405 char *label;
407 ASM_FORMAT_PRIVATE_NAME (label, IDENTIFIER_POINTER (name), static_labelno);
408 static_labelno++;
410 if (numeric_output_need_bar)
411 OB_PUTC ('_');
412 icat (strlen (label));
413 OB_PUTCP (label);
414 numeric_output_need_bar = 1;
416 else /* TYPE_DECL */
417 build_overload_identifier (decl);
420 static void
421 build_underscore_int (i)
422 int i;
424 if (i > 9)
425 OB_PUTC ('_');
426 icat (i);
427 if (i > 9)
428 OB_PUTC ('_');
431 /* Encoding for an INTEGER_CST value. */
433 static void
434 build_overload_int (value, in_template)
435 tree value;
436 int in_template;
438 if (in_template && TREE_CODE (value) != INTEGER_CST)
439 /* We don't ever want this output, but it's inconvenient not to
440 be able to build the string. This should cause assembler
441 errors we'll notice. */
443 static int n;
444 sprintf (digit_buffer, " *%d", n++);
445 OB_PUTCP (digit_buffer);
446 return;
449 my_friendly_assert (TREE_CODE (value) == INTEGER_CST, 243);
450 if (TYPE_PRECISION (TREE_TYPE (value)) == 2 * HOST_BITS_PER_WIDE_INT)
452 if (TREE_INT_CST_HIGH (value)
453 != (TREE_INT_CST_LOW (value) >> (HOST_BITS_PER_WIDE_INT - 1)))
455 /* need to print a DImode value in decimal */
456 dicat (TREE_INT_CST_LOW (value), TREE_INT_CST_HIGH (value));
457 return;
459 /* else fall through to print in smaller mode */
461 /* Wordsize or smaller */
462 icat (TREE_INT_CST_LOW (value));
465 static void
466 build_overload_value (type, value, in_template)
467 tree type, value;
468 int in_template;
470 while (TREE_CODE (value) == NON_LVALUE_EXPR
471 || TREE_CODE (value) == NOP_EXPR)
472 value = TREE_OPERAND (value, 0);
473 my_friendly_assert (TREE_CODE (type) == PARM_DECL, 242);
474 type = TREE_TYPE (type);
476 if (numeric_output_need_bar)
478 OB_PUTC ('_');
479 numeric_output_need_bar = 0;
482 if (TREE_CODE (value) == TEMPLATE_CONST_PARM)
484 OB_PUTC ('Y');
485 build_underscore_int (TEMPLATE_CONST_IDX (value));
486 build_underscore_int (TEMPLATE_CONST_LEVEL (value));
487 return;
490 if (TREE_CODE (type) == POINTER_TYPE
491 && TREE_CODE (TREE_TYPE (type)) == OFFSET_TYPE)
493 /* Handle a pointer to data member as a template instantiation
494 parameter, boy, what fun! */
495 type = integer_type_node;
496 if (TREE_CODE (value) != INTEGER_CST)
498 sorry ("unknown pointer to member constant");
499 return;
503 if (TYPE_PTRMEMFUNC_P (type))
504 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
506 switch (TREE_CODE (type))
508 case INTEGER_TYPE:
509 case ENUMERAL_TYPE:
510 case BOOLEAN_TYPE:
512 build_overload_int (value, in_template);
513 numeric_output_need_bar = 1;
514 return;
516 case REAL_TYPE:
518 REAL_VALUE_TYPE val;
519 char *bufp = digit_buffer;
521 pedwarn ("ANSI C++ forbids floating-point template arguments");
523 my_friendly_assert (TREE_CODE (value) == REAL_CST, 244);
524 val = TREE_REAL_CST (value);
525 if (REAL_VALUE_ISNAN (val))
527 sprintf (bufp, "NaN");
529 else
531 if (REAL_VALUE_NEGATIVE (val))
533 val = REAL_VALUE_NEGATE (val);
534 *bufp++ = 'm';
536 if (REAL_VALUE_ISINF (val))
538 sprintf (bufp, "Infinity");
540 else
542 REAL_VALUE_TO_DECIMAL (val, "%.20e", bufp);
543 bufp = (char *) index (bufp, 'e');
544 if (!bufp)
545 strcat (digit_buffer, "e0");
546 else
548 char *p;
549 bufp++;
550 if (*bufp == '-')
552 *bufp++ = 'm';
554 p = bufp;
555 if (*p == '+')
556 p++;
557 while (*p == '0')
558 p++;
559 if (*p == 0)
561 *bufp++ = '0';
562 *bufp = 0;
564 else if (p != bufp)
566 while (*p)
567 *bufp++ = *p++;
568 *bufp = 0;
571 #ifdef NO_DOT_IN_LABEL
572 bufp = (char *) index (bufp, '.');
573 if (bufp)
574 *bufp = '_';
575 #endif
578 OB_PUTCP (digit_buffer);
579 numeric_output_need_bar = 1;
580 return;
582 case POINTER_TYPE:
583 if (TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE
584 && TREE_CODE (value) != ADDR_EXPR)
586 if (TREE_CODE (value) == CONSTRUCTOR)
588 /* This is dangerous code, crack built up pointer to members. */
589 tree args = CONSTRUCTOR_ELTS (value);
590 tree a1 = TREE_VALUE (args);
591 tree a2 = TREE_VALUE (TREE_CHAIN (args));
592 tree a3 = CONSTRUCTOR_ELTS (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args))));
593 a3 = TREE_VALUE (a3);
594 STRIP_NOPS (a3);
595 if (TREE_CODE (a1) == INTEGER_CST
596 && TREE_CODE (a2) == INTEGER_CST)
598 build_overload_int (a1, in_template);
599 OB_PUTC ('_');
600 build_overload_int (a2, in_template);
601 OB_PUTC ('_');
602 if (TREE_CODE (a3) == ADDR_EXPR)
604 a3 = TREE_OPERAND (a3, 0);
605 if (TREE_CODE (a3) == FUNCTION_DECL)
607 numeric_output_need_bar = 0;
608 build_overload_identifier (DECL_ASSEMBLER_NAME (a3));
609 return;
612 else if (TREE_CODE (a3) == INTEGER_CST)
614 OB_PUTC ('i');
615 build_overload_int (a3, in_template);
616 numeric_output_need_bar = 1;
617 return;
621 sorry ("template instantiation with pointer to method that is too complex");
622 return;
624 if (TREE_CODE (value) == INTEGER_CST
625 || TREE_CODE (value) == TEMPLATE_CONST_PARM)
627 build_overload_int (value, in_template);
628 numeric_output_need_bar = 1;
629 return;
631 value = TREE_OPERAND (value, 0);
632 if (TREE_CODE (value) == VAR_DECL)
634 my_friendly_assert (DECL_NAME (value) != 0, 245);
635 build_overload_identifier (DECL_ASSEMBLER_NAME (value));
636 return;
638 else if (TREE_CODE (value) == FUNCTION_DECL)
640 my_friendly_assert (DECL_NAME (value) != 0, 246);
641 build_overload_identifier (DECL_ASSEMBLER_NAME (value));
642 return;
644 else
645 my_friendly_abort (71);
646 break; /* not really needed */
648 default:
649 sorry ("conversion of %s as template parameter",
650 tree_code_name [(int) TREE_CODE (type)]);
651 my_friendly_abort (72);
656 /* Add encodings for the vector of template parameters in PARMLIST,
657 given the vector of arguments to be substituted in ARGLIST. */
659 static void
660 build_template_parm_names (parmlist, arglist)
661 tree parmlist;
662 tree arglist;
664 int i, nparms;
666 nparms = TREE_VEC_LENGTH (parmlist);
667 icat (nparms);
668 for (i = 0; i < nparms; i++)
670 tree parm = TREE_VALUE (TREE_VEC_ELT (parmlist, i));
671 tree arg = TREE_VEC_ELT (arglist, i);
672 if (TREE_CODE (parm) == TYPE_DECL)
674 /* This parameter is a type. */
675 OB_PUTC ('Z');
676 build_overload_name (arg, 0, 0);
678 else
680 parm = tsubst (parm, arglist,
681 TREE_VEC_LENGTH (arglist), NULL_TREE);
682 /* It's a PARM_DECL. */
683 build_overload_name (TREE_TYPE (parm), 0, 0);
684 build_overload_value (parm, arg, uses_template_parms (arglist));
690 static void
691 build_overload_identifier (name)
692 tree name;
694 if (TREE_CODE (name) == TYPE_DECL
695 && IS_AGGR_TYPE (TREE_TYPE (name))
696 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (name))
697 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (TREE_TYPE (name))))
699 tree template, parmlist, arglist, tname;
700 template = CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (name));
701 arglist = TREE_VALUE (template);
702 template = TREE_PURPOSE (template);
703 tname = DECL_NAME (template);
704 parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
705 OB_PUTC ('t');
706 icat (IDENTIFIER_LENGTH (tname));
707 OB_PUTID (tname);
708 build_template_parm_names (parmlist, arglist);
710 else
712 if (TREE_CODE (name) == TYPE_DECL)
713 name = DECL_NAME (name);
714 if (numeric_output_need_bar)
716 OB_PUTC ('_');
717 numeric_output_need_bar = 0;
719 icat (IDENTIFIER_LENGTH (name));
720 OB_PUTID (name);
724 /* Given DECL, either a class TYPE, TYPE_DECL or FUNCTION_DECL, produce
725 the mangling for it. Used by build_overload_name and build_static_name. */
727 static void
728 build_qualified_name (decl)
729 tree decl;
731 tree context;
732 int i = 1;
734 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 't')
735 decl = TYPE_NAME (decl);
737 /* If DECL_ASSEMBLER_NAME has been set properly, use it. */
738 if (TREE_CODE (decl) == TYPE_DECL
739 && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
741 OB_PUTID (DECL_ASSEMBLER_NAME (decl));
742 return;
745 context = decl;
746 while (DECL_CONTEXT (context))
748 i += 1;
749 context = DECL_CONTEXT (context);
750 if (TREE_CODE_CLASS (TREE_CODE (context)) == 't')
751 context = TYPE_NAME (context);
754 if (i > 1)
756 OB_PUTC ('Q');
757 if (i > 9)
758 OB_PUTC ('_');
759 icat (i);
760 if (i > 9)
761 OB_PUTC ('_');
762 numeric_output_need_bar = 0;
764 build_overload_nested_name (decl);
767 /* Given a list of parameters in PARMTYPES, create an unambiguous
768 overload string. Should distinguish any type that C (or C++) can
769 distinguish. I.e., pointers to functions are treated correctly.
771 Caller must deal with whether a final `e' goes on the end or not.
773 Any default conversions must take place before this function
774 is called.
776 BEGIN and END control initialization and finalization of the
777 obstack where we build the string. */
779 char *
780 build_overload_name (parmtypes, begin, end)
781 tree parmtypes;
782 int begin, end;
784 int just_one;
785 tree parmtype;
787 if (begin) OB_INIT ();
788 numeric_output_need_bar = 0;
790 if ((just_one = (TREE_CODE (parmtypes) != TREE_LIST)))
792 parmtype = parmtypes;
793 goto only_one;
796 while (parmtypes)
798 parmtype = TREE_VALUE (parmtypes);
800 only_one:
802 if (! nofold && ! just_one)
804 /* Every argument gets counted. */
805 typevec[maxtype++] = parmtype;
807 if (TREE_USED (parmtype) && parmtype == typevec[maxtype-2])
809 nrepeats++;
810 goto next;
813 if (nrepeats)
814 flush_repeats (typevec[maxtype-2]);
816 if (TREE_USED (parmtype))
818 #if 0
819 /* We can turn this on at some point when we want
820 improved symbol mangling. */
821 nrepeats++;
822 #else
823 /* This is bug compatible with 2.7.x */
824 flush_repeats (parmtype);
825 #endif
826 goto next;
829 /* Only cache types which take more than one character. */
830 if (parmtype != TYPE_MAIN_VARIANT (parmtype)
831 || (TREE_CODE (parmtype) != INTEGER_TYPE
832 && TREE_CODE (parmtype) != REAL_TYPE))
833 TREE_USED (parmtype) = 1;
836 if (TYPE_PTRMEMFUNC_P (parmtype))
837 parmtype = TYPE_PTRMEMFUNC_FN_TYPE (parmtype);
839 if (TREE_READONLY (parmtype))
840 OB_PUTC ('C');
841 if (TREE_CODE (parmtype) == INTEGER_TYPE
842 && TYPE_MAIN_VARIANT (parmtype) == unsigned_type (TYPE_MAIN_VARIANT (parmtype)))
843 OB_PUTC ('U');
844 if (TYPE_VOLATILE (parmtype))
845 OB_PUTC ('V');
847 switch (TREE_CODE (parmtype))
849 case OFFSET_TYPE:
850 OB_PUTC ('O');
851 build_overload_name (TYPE_OFFSET_BASETYPE (parmtype), 0, 0);
852 OB_PUTC ('_');
853 build_overload_name (TREE_TYPE (parmtype), 0, 0);
854 break;
856 case REFERENCE_TYPE:
857 OB_PUTC ('R');
858 goto more;
860 case ARRAY_TYPE:
861 #if PARM_CAN_BE_ARRAY_TYPE
863 tree length;
865 OB_PUTC ('A');
866 if (TYPE_DOMAIN (parmtype) == NULL_TREE)
867 error ("pointer or reference to array of unknown bound in parm type");
868 else
870 length = array_type_nelts (parmtype);
871 if (TREE_CODE (length) == INTEGER_CST)
872 icat (TREE_INT_CST_LOW (length) + 1);
874 OB_PUTC ('_');
875 goto more;
877 #else
878 OB_PUTC ('P');
879 goto more;
880 #endif
882 case POINTER_TYPE:
883 OB_PUTC ('P');
884 more:
885 build_overload_name (TREE_TYPE (parmtype), 0, 0);
886 break;
888 case FUNCTION_TYPE:
889 case METHOD_TYPE:
891 tree firstarg = TYPE_ARG_TYPES (parmtype);
892 /* Otherwise have to implement reentrant typevecs,
893 unmark and remark types, etc. */
894 int old_nofold = nofold;
895 nofold = 1;
897 if (nrepeats)
898 flush_repeats (typevec[maxtype-1]);
900 /* @@ It may be possible to pass a function type in
901 which is not preceded by a 'P'. */
902 if (TREE_CODE (parmtype) == FUNCTION_TYPE)
904 OB_PUTC ('F');
905 if (firstarg == NULL_TREE)
906 OB_PUTC ('e');
907 else if (firstarg == void_list_node)
908 OB_PUTC ('v');
909 else
910 build_overload_name (firstarg, 0, 0);
912 else
914 int constp = TYPE_READONLY (TREE_TYPE (TREE_VALUE (firstarg)));
915 int volatilep = TYPE_VOLATILE (TREE_TYPE (TREE_VALUE (firstarg)));
916 OB_PUTC ('M');
917 firstarg = TREE_CHAIN (firstarg);
919 build_overload_name (TYPE_METHOD_BASETYPE (parmtype), 0, 0);
920 if (constp)
921 OB_PUTC ('C');
922 if (volatilep)
923 OB_PUTC ('V');
925 /* For cfront 2.0 compatibility. */
926 OB_PUTC ('F');
928 if (firstarg == NULL_TREE)
929 OB_PUTC ('e');
930 else if (firstarg == void_list_node)
931 OB_PUTC ('v');
932 else
933 build_overload_name (firstarg, 0, 0);
936 /* Separate args from return type. */
937 OB_PUTC ('_');
938 build_overload_name (TREE_TYPE (parmtype), 0, 0);
939 nofold = old_nofold;
940 break;
943 case INTEGER_TYPE:
944 parmtype = TYPE_MAIN_VARIANT (parmtype);
945 if (parmtype == integer_type_node
946 || parmtype == unsigned_type_node)
947 OB_PUTC ('i');
948 else if (parmtype == long_integer_type_node
949 || parmtype == long_unsigned_type_node)
950 OB_PUTC ('l');
951 else if (parmtype == short_integer_type_node
952 || parmtype == short_unsigned_type_node)
953 OB_PUTC ('s');
954 else if (parmtype == signed_char_type_node)
956 OB_PUTC ('S');
957 OB_PUTC ('c');
959 else if (parmtype == char_type_node
960 || parmtype == unsigned_char_type_node)
961 OB_PUTC ('c');
962 else if (parmtype == wchar_type_node)
963 OB_PUTC ('w');
964 else if (parmtype == long_long_integer_type_node
965 || parmtype == long_long_unsigned_type_node)
966 OB_PUTC ('x');
967 #if 0
968 /* it would seem there is no way to enter these in source code,
969 yet. (mrs) */
970 else if (parmtype == long_long_long_integer_type_node
971 || parmtype == long_long_long_unsigned_type_node)
972 OB_PUTC ('q');
973 #endif
974 else
975 my_friendly_abort (73);
976 break;
978 case BOOLEAN_TYPE:
979 OB_PUTC ('b');
980 break;
982 case REAL_TYPE:
983 parmtype = TYPE_MAIN_VARIANT (parmtype);
984 if (parmtype == long_double_type_node)
985 OB_PUTC ('r');
986 else if (parmtype == double_type_node)
987 OB_PUTC ('d');
988 else if (parmtype == float_type_node)
989 OB_PUTC ('f');
990 else my_friendly_abort (74);
991 break;
993 case COMPLEX_TYPE:
994 OB_PUTC ('J');
995 build_overload_name (TREE_TYPE (parmtype), 0, 0);
996 break;
998 case VOID_TYPE:
999 if (! just_one)
1001 #if 0
1002 extern tree void_list_node;
1004 /* See if anybody is wasting memory. */
1005 my_friendly_assert (parmtypes == void_list_node, 247);
1006 #endif
1007 /* This is the end of a parameter list. */
1008 if (end) OB_FINISH ();
1009 return (char *)obstack_base (&scratch_obstack);
1011 OB_PUTC ('v');
1012 break;
1014 case ERROR_MARK: /* not right, but nothing is anyway */
1015 break;
1017 /* have to do these */
1018 case UNION_TYPE:
1019 case RECORD_TYPE:
1020 if (! just_one)
1021 /* Make this type signature look incompatible
1022 with AT&T. */
1023 OB_PUTC ('G');
1024 goto common;
1025 case ENUMERAL_TYPE:
1026 common:
1028 tree name = TYPE_NAME (parmtype);
1030 if (TREE_CODE (name) == IDENTIFIER_NODE)
1032 build_overload_identifier (TYPE_NAME (parmtype));
1033 break;
1035 my_friendly_assert (TREE_CODE (name) == TYPE_DECL, 248);
1037 build_qualified_name (name);
1038 break;
1041 case UNKNOWN_TYPE:
1042 /* This will take some work. */
1043 OB_PUTC ('?');
1044 break;
1046 case TEMPLATE_TYPE_PARM:
1047 OB_PUTC ('X');
1048 build_underscore_int (TEMPLATE_TYPE_IDX (parmtype));
1049 build_underscore_int (TEMPLATE_TYPE_LEVEL (parmtype));
1050 break;
1052 case TYPENAME_TYPE:
1053 /* When mangling the type of a function template whose
1054 declaration looks like:
1056 template <class T> void foo(typename T::U)
1058 we have to mangle these. */
1059 build_qualified_name (parmtype);
1060 break;
1062 default:
1063 my_friendly_abort (75);
1066 next:
1067 if (just_one) break;
1068 parmtypes = TREE_CHAIN (parmtypes);
1070 if (! just_one)
1072 if (nrepeats)
1073 flush_repeats (typevec[maxtype-1]);
1075 /* To get here, parms must end with `...'. */
1076 OB_PUTC ('e');
1079 if (end) OB_FINISH ();
1080 return (char *)obstack_base (&scratch_obstack);
1083 /* Produce the mangling for a variable named NAME in CONTEXT, which can
1084 be either a class TYPE or a FUNCTION_DECL. */
1086 tree
1087 build_static_name (context, name)
1088 tree context, name;
1090 OB_INIT ();
1091 numeric_output_need_bar = 0;
1092 #ifdef JOINER
1093 OB_PUTC ('_');
1094 build_qualified_name (context);
1095 OB_PUTC (JOINER);
1096 #else
1097 OB_PUTS ("__static_");
1098 build_qualified_name (context);
1099 OB_PUTC ('_');
1100 #endif
1101 OB_PUTID (name);
1102 OB_FINISH ();
1104 return get_identifier ((char *)obstack_base (&scratch_obstack));
1107 static tree
1108 build_decl_overload_real (dname, parms, ret_type, tparms, targs,
1109 for_method)
1110 tree dname;
1111 tree parms;
1112 tree ret_type;
1113 tree tparms;
1114 tree targs;
1115 int for_method;
1117 char *name = IDENTIFIER_POINTER (dname);
1119 /* member operators new and delete look like methods at this point. */
1120 if (! for_method && parms != NULL_TREE && TREE_CODE (parms) == TREE_LIST)
1122 if (dname == ansi_opname[(int) DELETE_EXPR])
1123 return get_identifier ("__builtin_delete");
1124 else if (dname == ansi_opname[(int) VEC_DELETE_EXPR])
1125 return get_identifier ("__builtin_vec_delete");
1126 else if (TREE_CHAIN (parms) == void_list_node)
1128 if (dname == ansi_opname[(int) NEW_EXPR])
1129 return get_identifier ("__builtin_new");
1130 else if (dname == ansi_opname[(int) VEC_NEW_EXPR])
1131 return get_identifier ("__builtin_vec_new");
1135 OB_INIT ();
1136 if (for_method != 2)
1137 OB_PUTCP (name);
1138 /* Otherwise, we can divine that this is a constructor,
1139 and figure out its name without any extra encoding. */
1141 OB_PUTC2 ('_', '_');
1142 if (for_method)
1144 #if 0
1145 /* We can get away without doing this. */
1146 OB_PUTC ('M');
1147 #endif
1148 if (tparms != NULL_TREE)
1149 OB_PUTC ('H');
1151 tree this_type = TREE_VALUE (parms);
1153 if (TREE_CODE (this_type) == RECORD_TYPE) /* a signature pointer */
1154 parms = temp_tree_cons (NULL_TREE, SIGNATURE_TYPE (this_type),
1155 TREE_CHAIN (parms));
1156 else
1157 parms = temp_tree_cons (NULL_TREE, TREE_TYPE (this_type),
1158 TREE_CHAIN (parms));
1161 else if (tparms)
1162 OB_PUTC ('H');
1163 else
1164 OB_PUTC ('F');
1166 if (tparms)
1168 build_template_parm_names (tparms, targs);
1169 OB_PUTC ('_');
1172 if (parms == NULL_TREE)
1173 OB_PUTC ('e');
1174 else if (parms == void_list_node)
1175 OB_PUTC ('v');
1176 else
1178 ALLOCATE_TYPEVEC (parms);
1179 nofold = 0;
1180 if (for_method)
1182 build_overload_name (TREE_VALUE (parms), 0, 0);
1184 typevec[maxtype++] = TREE_VALUE (parms);
1185 TREE_USED (TREE_VALUE (parms)) = 1;
1187 if (TREE_CHAIN (parms))
1188 build_overload_name (TREE_CHAIN (parms), 0, 0);
1189 else
1190 OB_PUTC ('e');
1192 else
1193 build_overload_name (parms, 0, 0);
1194 DEALLOCATE_TYPEVEC (parms);
1197 if (ret_type != NULL_TREE && for_method != 2)
1199 /* Add the return type. */
1200 OB_PUTC ('_');
1201 build_overload_name (ret_type, 0, 0);
1204 OB_FINISH ();
1206 tree n = get_identifier (obstack_base (&scratch_obstack));
1207 if (IDENTIFIER_OPNAME_P (dname))
1208 IDENTIFIER_OPNAME_P (n) = 1;
1209 return n;
1213 /* Change the name of a function definition so that it may be
1214 overloaded. NAME is the name of the function to overload,
1215 PARMS is the parameter list (which determines what name the
1216 final function obtains).
1218 FOR_METHOD is 1 if this overload is being performed
1219 for a method, rather than a function type. It is 2 if
1220 this overload is being performed for a constructor. */
1222 tree
1223 build_decl_overload (dname, parms, for_method)
1224 tree dname;
1225 tree parms;
1226 int for_method;
1228 return build_decl_overload_real (dname, parms, NULL_TREE, NULL_TREE,
1229 NULL_TREE, for_method);
1233 /* Like build_decl_overload, but for template functions. */
1235 tree
1236 build_template_decl_overload (dname, parms, ret_type, tparms, targs,
1237 for_method)
1238 tree dname;
1239 tree parms;
1240 tree ret_type;
1241 tree tparms;
1242 tree targs;
1243 int for_method;
1245 return build_decl_overload_real (dname, parms, ret_type, tparms, targs,
1246 for_method);
1250 /* Build an overload name for the type expression TYPE. */
1252 tree
1253 build_typename_overload (type)
1254 tree type;
1256 tree id;
1258 OB_INIT ();
1259 OB_PUTID (ansi_opname[(int) TYPE_EXPR]);
1260 nofold = 1;
1261 build_overload_name (type, 0, 1);
1262 id = get_identifier (obstack_base (&scratch_obstack));
1263 IDENTIFIER_OPNAME_P (id) = 1;
1264 #if 0
1265 IDENTIFIER_GLOBAL_VALUE (id) = TYPE_MAIN_DECL (type);
1266 #endif
1267 TREE_TYPE (id) = type;
1268 return id;
1271 tree
1272 build_overload_with_type (name, type)
1273 tree name, type;
1275 OB_INIT ();
1276 OB_PUTID (name);
1277 nofold = 1;
1279 build_overload_name (type, 0, 1);
1280 return get_identifier (obstack_base (&scratch_obstack));
1283 tree
1284 get_id_2 (name, name2)
1285 char *name;
1286 tree name2;
1288 OB_INIT ();
1289 OB_PUTCP (name);
1290 OB_PUTID (name2);
1291 OB_FINISH ();
1292 return get_identifier (obstack_base (&scratch_obstack));
1295 /* Given a tree_code CODE, and some arguments (at least one),
1296 attempt to use an overloaded operator on the arguments.
1298 For unary operators, only the first argument need be checked.
1299 For binary operators, both arguments may need to be checked.
1301 Member functions can convert class references to class pointers,
1302 for one-level deep indirection. More than that is not supported.
1303 Operators [](), ()(), and ->() must be member functions.
1305 We call function call building calls with LOOKUP_COMPLAIN if they
1306 are our only hope. This is true when we see a vanilla operator
1307 applied to something of aggregate type. If this fails, we are free
1308 to return `error_mark_node', because we will have reported the
1309 error.
1311 Operators NEW and DELETE overload in funny ways: operator new takes
1312 a single `size' parameter, and operator delete takes a pointer to the
1313 storage being deleted. When overloading these operators, success is
1314 assumed. If there is a failure, report an error message and return
1315 `error_mark_node'. */
1317 /* NOSTRICT */
1318 tree
1319 build_opfncall (code, flags, xarg1, xarg2, arg3)
1320 enum tree_code code;
1321 int flags;
1322 tree xarg1, xarg2, arg3;
1324 tree rval = 0;
1325 tree arg1, arg2;
1326 tree type1, type2, fnname;
1327 tree fields1 = 0, parms = 0;
1328 tree global_fn;
1329 int try_second;
1330 int binary_is_unary;
1332 if (flag_ansi_overloading)
1333 return build_new_op (code, flags, xarg1, xarg2, arg3);
1335 if (xarg1 == error_mark_node)
1336 return error_mark_node;
1338 if (code == COND_EXPR)
1340 if (xarg2 == error_mark_node
1341 || arg3 == error_mark_node)
1342 return error_mark_node;
1344 if (code == COMPONENT_REF)
1345 if (TREE_CODE (TREE_TYPE (xarg1)) == POINTER_TYPE)
1346 return rval;
1348 /* First, see if we can work with the first argument */
1349 type1 = TREE_TYPE (xarg1);
1351 /* Some tree codes have length > 1, but we really only want to
1352 overload them if their first argument has a user defined type. */
1353 switch (code)
1355 case PREINCREMENT_EXPR:
1356 case PREDECREMENT_EXPR:
1357 case POSTINCREMENT_EXPR:
1358 case POSTDECREMENT_EXPR:
1359 case COMPONENT_REF:
1360 binary_is_unary = 1;
1361 try_second = 0;
1362 break;
1364 /* ARRAY_REFs and CALL_EXPRs must overload successfully.
1365 If they do not, return error_mark_node instead of NULL_TREE. */
1366 case ARRAY_REF:
1367 if (xarg2 == error_mark_node)
1368 return error_mark_node;
1369 case CALL_EXPR:
1370 rval = error_mark_node;
1371 binary_is_unary = 0;
1372 try_second = 0;
1373 break;
1375 case VEC_NEW_EXPR:
1376 case NEW_EXPR:
1378 tree args = expr_tree_cons (NULL_TREE, xarg2, arg3);
1379 fnname = ansi_opname[(int) code];
1380 if (flags & LOOKUP_GLOBAL)
1381 return build_overload_call (fnname, args, flags & LOOKUP_COMPLAIN);
1383 rval = build_method_call
1384 (build_indirect_ref (build1 (NOP_EXPR, xarg1, error_mark_node),
1385 "new"),
1386 fnname, args, NULL_TREE, flags);
1387 if (rval == error_mark_node)
1388 /* User might declare fancy operator new, but invoke it
1389 like standard one. */
1390 return rval;
1392 TREE_TYPE (rval) = xarg1;
1393 TREE_CALLS_NEW (rval) = 1;
1394 return rval;
1396 break;
1398 case VEC_DELETE_EXPR:
1399 case DELETE_EXPR:
1401 fnname = ansi_opname[(int) code];
1402 if (flags & LOOKUP_GLOBAL)
1403 return build_overload_call (fnname,
1404 build_expr_list (NULL_TREE, xarg1),
1405 flags & LOOKUP_COMPLAIN);
1406 arg1 = TREE_TYPE (xarg1);
1408 /* This handles the case where we're trying to delete
1409 X (*a)[10];
1410 a=new X[5][10];
1411 delete[] a; */
1413 if (TREE_CODE (TREE_TYPE (arg1)) == ARRAY_TYPE)
1415 /* Strip off the pointer and the array. */
1416 arg1 = TREE_TYPE (TREE_TYPE (arg1));
1418 while (TREE_CODE (arg1) == ARRAY_TYPE)
1419 arg1 = (TREE_TYPE (arg1));
1421 arg1 = build_pointer_type (arg1);
1424 rval = build_method_call
1425 (build_indirect_ref (build1 (NOP_EXPR, arg1,
1426 error_mark_node),
1427 NULL_PTR),
1428 fnname, expr_tree_cons (NULL_TREE, xarg1,
1429 build_expr_list (NULL_TREE, xarg2)),
1430 NULL_TREE, flags);
1431 #if 0
1432 /* This can happen when operator delete is protected. */
1433 my_friendly_assert (rval != error_mark_node, 250);
1434 TREE_TYPE (rval) = void_type_node;
1435 #endif
1436 return rval;
1438 break;
1440 default:
1441 binary_is_unary = 0;
1442 try_second = tree_code_length [(int) code] == 2;
1443 if (try_second && xarg2 == error_mark_node)
1444 return error_mark_node;
1445 break;
1448 if (try_second && xarg2 == error_mark_node)
1449 return error_mark_node;
1451 /* What ever it was, we do not know how to deal with it. */
1452 if (type1 == NULL_TREE)
1453 return rval;
1455 if (TREE_CODE (type1) == OFFSET_TYPE)
1456 type1 = TREE_TYPE (type1);
1458 if (TREE_CODE (type1) == REFERENCE_TYPE)
1460 arg1 = convert_from_reference (xarg1);
1461 type1 = TREE_TYPE (arg1);
1463 else
1465 arg1 = xarg1;
1468 if (!IS_AGGR_TYPE (type1) || TYPE_PTRMEMFUNC_P (type1))
1470 /* Try to fail. First, fail if unary */
1471 if (! try_second)
1472 return rval;
1473 /* Second, see if second argument is non-aggregate. */
1474 type2 = TREE_TYPE (xarg2);
1475 if (TREE_CODE (type2) == OFFSET_TYPE)
1476 type2 = TREE_TYPE (type2);
1477 if (TREE_CODE (type2) == REFERENCE_TYPE)
1479 arg2 = convert_from_reference (xarg2);
1480 type2 = TREE_TYPE (arg2);
1482 else
1484 arg2 = xarg2;
1487 if (!IS_AGGR_TYPE (type2))
1488 return rval;
1489 try_second = 0;
1492 if (try_second)
1494 /* First arg may succeed; see whether second should. */
1495 type2 = TREE_TYPE (xarg2);
1496 if (TREE_CODE (type2) == OFFSET_TYPE)
1497 type2 = TREE_TYPE (type2);
1498 if (TREE_CODE (type2) == REFERENCE_TYPE)
1500 arg2 = convert_from_reference (xarg2);
1501 type2 = TREE_TYPE (arg2);
1503 else
1505 arg2 = xarg2;
1508 if (! IS_AGGR_TYPE (type2))
1509 try_second = 0;
1512 if (type1 == unknown_type_node
1513 || (try_second && TREE_TYPE (xarg2) == unknown_type_node))
1515 /* This will not be implemented in the foreseeable future. */
1516 return rval;
1519 if (code == MODIFY_EXPR)
1520 fnname = ansi_assopname[(int) TREE_CODE (arg3)];
1521 else
1522 fnname = ansi_opname[(int) code];
1524 global_fn = lookup_name_nonclass (fnname);
1526 /* This is the last point where we will accept failure. This
1527 may be too eager if we wish an overloaded operator not to match,
1528 but would rather a normal operator be called on a type-converted
1529 argument. */
1531 if (IS_AGGR_TYPE (type1))
1533 fields1 = lookup_fnfields (TYPE_BINFO (type1), fnname, 0);
1534 /* ARM $13.4.7, prefix/postfix ++/--. */
1535 if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
1537 xarg2 = integer_zero_node;
1538 binary_is_unary = 0;
1540 if (fields1)
1542 tree t, t2;
1543 int have_postfix = 0;
1545 /* Look for an `operator++ (int)'. If they didn't have
1546 one, then we fall back to the old way of doing things. */
1547 for (t = TREE_VALUE (fields1); t ; t = DECL_CHAIN (t))
1549 t2 = TYPE_ARG_TYPES (TREE_TYPE (t));
1550 if (TREE_CHAIN (t2) != NULL_TREE
1551 && TREE_VALUE (TREE_CHAIN (t2)) == integer_type_node)
1553 have_postfix = 1;
1554 break;
1558 if (! have_postfix)
1560 char *op = POSTINCREMENT_EXPR ? "++" : "--";
1562 /* There's probably a LOT of code in the world that
1563 relies upon this old behavior. */
1564 pedwarn ("no `operator%s (int)' declared for postfix `%s', using prefix operator instead",
1565 op, op);
1566 xarg2 = NULL_TREE;
1567 binary_is_unary = 1;
1573 if (fields1 == NULL_TREE && global_fn == NULL_TREE)
1574 return rval;
1576 /* If RVAL winds up being `error_mark_node', we will return
1577 that... There is no way that normal semantics of these
1578 operators will succeed. */
1580 /* This argument may be an uncommitted OFFSET_REF. This is
1581 the case for example when dealing with static class members
1582 which are referenced from their class name rather than
1583 from a class instance. */
1584 if (TREE_CODE (xarg1) == OFFSET_REF
1585 && TREE_CODE (TREE_OPERAND (xarg1, 1)) == VAR_DECL)
1586 xarg1 = TREE_OPERAND (xarg1, 1);
1587 if (try_second && xarg2 && TREE_CODE (xarg2) == OFFSET_REF
1588 && TREE_CODE (TREE_OPERAND (xarg2, 1)) == VAR_DECL)
1589 xarg2 = TREE_OPERAND (xarg2, 1);
1591 if (global_fn)
1592 flags |= LOOKUP_GLOBAL;
1594 if (code == CALL_EXPR)
1596 /* This can only be a member function. */
1597 return build_method_call (xarg1, fnname, xarg2,
1598 NULL_TREE, LOOKUP_NORMAL);
1600 else if (tree_code_length[(int) code] == 1 || binary_is_unary)
1602 parms = NULL_TREE;
1603 rval = build_method_call (xarg1, fnname, NULL_TREE, NULL_TREE, flags);
1605 else if (code == COND_EXPR)
1607 parms = expr_tree_cons (NULL_TREE, xarg2, build_expr_list (NULL_TREE, arg3));
1608 rval = build_method_call (xarg1, fnname, parms, NULL_TREE, flags);
1610 else if (code == METHOD_CALL_EXPR)
1612 /* must be a member function. */
1613 parms = expr_tree_cons (NULL_TREE, xarg2, arg3);
1614 return build_method_call (xarg1, fnname, parms, NULL_TREE,
1615 LOOKUP_NORMAL);
1617 else if (fields1)
1619 parms = build_expr_list (NULL_TREE, xarg2);
1620 rval = build_method_call (xarg1, fnname, parms, NULL_TREE, flags);
1622 else
1624 parms = expr_tree_cons (NULL_TREE, xarg1,
1625 build_expr_list (NULL_TREE, xarg2));
1626 rval = build_overload_call (fnname, parms, flags);
1629 return rval;
1632 /* This function takes an identifier, ID, and attempts to figure out what
1633 it means. There are a number of possible scenarios, presented in increasing
1634 order of hair:
1636 1) not in a class's scope
1637 2) in class's scope, member name of the class's method
1638 3) in class's scope, but not a member name of the class
1639 4) in class's scope, member name of a class's variable
1641 NAME is $1 from the bison rule. It is an IDENTIFIER_NODE.
1642 VALUE is $$ from the bison rule. It is the value returned by lookup_name ($1)
1644 As a last ditch, try to look up the name as a label and return that
1645 address.
1647 Values which are declared as being of REFERENCE_TYPE are
1648 automatically dereferenced here (as a hack to make the
1649 compiler faster). */
1651 tree
1652 hack_identifier (value, name)
1653 tree value, name;
1655 tree type;
1657 if (value == error_mark_node)
1659 if (current_class_name)
1661 tree fields = lookup_fnfields (TYPE_BINFO (current_class_type), name, 1);
1662 if (fields == error_mark_node)
1663 return error_mark_node;
1664 if (fields)
1666 tree fndecl;
1668 fndecl = TREE_VALUE (fields);
1669 my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 251);
1670 if (DECL_CHAIN (fndecl) == NULL_TREE)
1672 warning ("methods cannot be converted to function pointers");
1673 return fndecl;
1675 else
1677 error ("ambiguous request for method pointer `%s'",
1678 IDENTIFIER_POINTER (name));
1679 return error_mark_node;
1683 if (flag_labels_ok && IDENTIFIER_LABEL_VALUE (name))
1685 return IDENTIFIER_LABEL_VALUE (name);
1687 return error_mark_node;
1690 type = TREE_TYPE (value);
1691 if (TREE_CODE (value) == FIELD_DECL)
1693 if (current_class_ptr == NULL_TREE)
1695 error ("request for member `%s' in static member function",
1696 IDENTIFIER_POINTER (DECL_NAME (value)));
1697 return error_mark_node;
1699 TREE_USED (current_class_ptr) = 1;
1701 /* Mark so that if we are in a constructor, and then find that
1702 this field was initialized by a base initializer,
1703 we can emit an error message. */
1704 TREE_USED (value) = 1;
1705 value = build_component_ref (current_class_ref, name, NULL_TREE, 1);
1707 else if (really_overloaded_fn (value))
1709 #if 0
1710 tree t = get_first_fn (value);
1711 for (; t; t = DECL_CHAIN (t))
1713 if (TREE_CODE (t) == TEMPLATE_DECL)
1714 continue;
1716 assemble_external (t);
1717 TREE_USED (t) = 1;
1719 #endif
1721 else if (TREE_CODE (value) == TREE_LIST)
1723 /* Ambiguous reference to base members, possibly other cases?. */
1724 tree t = value;
1725 while (t && TREE_CODE (t) == TREE_LIST)
1727 mark_used (TREE_VALUE (t));
1728 t = TREE_CHAIN (t);
1731 else
1732 mark_used (value);
1734 if (TREE_CODE (value) == VAR_DECL || TREE_CODE (value) == PARM_DECL)
1736 tree context = decl_function_context (value);
1737 if (context != NULL_TREE && context != current_function_decl
1738 && ! TREE_STATIC (value))
1740 cp_error ("use of %s from containing function",
1741 (TREE_CODE (value) == VAR_DECL
1742 ? "`auto' variable" : "parameter"));
1743 cp_error_at (" `%#D' declared here", value);
1744 value = error_mark_node;
1748 if (TREE_CODE_CLASS (TREE_CODE (value)) == 'd' && DECL_NONLOCAL (value))
1750 if (DECL_LANG_SPECIFIC (value)
1751 && DECL_CLASS_CONTEXT (value) != current_class_type)
1753 tree path, access;
1754 register tree context
1755 = (TREE_CODE (value) == FUNCTION_DECL && DECL_VIRTUAL_P (value))
1756 ? DECL_CLASS_CONTEXT (value)
1757 : DECL_CONTEXT (value);
1759 get_base_distance (context, current_class_type, 0, &path);
1760 if (path)
1762 access = compute_access (path, value);
1763 if (access != access_public_node)
1765 if (TREE_CODE (value) == VAR_DECL)
1766 error ("static member `%s' is %s",
1767 IDENTIFIER_POINTER (name),
1768 TREE_PRIVATE (value) ? "private"
1769 : "from a private base class");
1770 else
1771 error ("enum `%s' is from private base class",
1772 IDENTIFIER_POINTER (name));
1773 return error_mark_node;
1778 else if (TREE_CODE (value) == TREE_LIST && TREE_NONLOCAL_FLAG (value))
1780 if (type == 0)
1782 error ("request for member `%s' is ambiguous in multiple inheritance lattice",
1783 IDENTIFIER_POINTER (name));
1784 return error_mark_node;
1787 return value;
1790 if (TREE_CODE (type) == REFERENCE_TYPE && ! processing_template_decl)
1791 value = convert_from_reference (value);
1792 return value;
1796 static char *
1797 thunk_printable_name (decl)
1798 tree decl;
1800 return "<thunk function>";
1803 tree
1804 make_thunk (function, delta)
1805 tree function;
1806 int delta;
1808 char buffer[250];
1809 tree thunk_id;
1810 tree thunk;
1811 char *func_name;
1812 tree func_decl;
1813 if (TREE_CODE (function) != ADDR_EXPR)
1814 abort ();
1815 func_decl = TREE_OPERAND (function, 0);
1816 if (TREE_CODE (func_decl) != FUNCTION_DECL)
1817 abort ();
1818 func_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (func_decl));
1819 if (delta<=0)
1820 sprintf (buffer, "__thunk_%d_%s", -delta, func_name);
1821 else
1822 sprintf (buffer, "__thunk_n%d_%s", delta, func_name);
1823 thunk_id = get_identifier (buffer);
1824 thunk = IDENTIFIER_GLOBAL_VALUE (thunk_id);
1825 if (thunk && TREE_CODE (thunk) != THUNK_DECL)
1827 cp_error ("implementation-reserved name `%D' used", thunk_id);
1828 IDENTIFIER_GLOBAL_VALUE (thunk_id) = thunk = NULL_TREE;
1830 if (thunk == NULL_TREE)
1832 thunk = build_decl (FUNCTION_DECL, thunk_id, TREE_TYPE (func_decl));
1833 TREE_READONLY (thunk) = TREE_READONLY (func_decl);
1834 TREE_THIS_VOLATILE (thunk) = TREE_THIS_VOLATILE (func_decl);
1835 comdat_linkage (thunk);
1836 TREE_SET_CODE (thunk, THUNK_DECL);
1837 DECL_INITIAL (thunk) = function;
1838 THUNK_DELTA (thunk) = delta;
1839 DECL_EXTERNAL (thunk) = 1;
1840 DECL_ARTIFICIAL (thunk) = 1;
1841 /* So that finish_file can write out any thunks that need to be: */
1842 pushdecl_top_level (thunk);
1844 return thunk;
1847 /* Emit the definition of a C++ multiple inheritance vtable thunk. */
1849 void
1850 emit_thunk (thunk_fndecl)
1851 tree thunk_fndecl;
1853 tree function = TREE_OPERAND (DECL_INITIAL (thunk_fndecl), 0);
1854 int delta = THUNK_DELTA (thunk_fndecl);
1856 if (TREE_ASM_WRITTEN (thunk_fndecl))
1857 return;
1859 TREE_ASM_WRITTEN (thunk_fndecl) = 1;
1861 TREE_ADDRESSABLE (function) = 1;
1862 mark_used (function);
1864 if (current_function_decl)
1865 abort ();
1867 TREE_SET_CODE (thunk_fndecl, FUNCTION_DECL);
1870 #ifdef ASM_OUTPUT_MI_THUNK
1871 char *fnname;
1872 current_function_decl = thunk_fndecl;
1873 /* Make sure we build up its RTL before we go onto the
1874 temporary obstack. */
1875 make_function_rtl (thunk_fndecl);
1876 temporary_allocation ();
1877 DECL_RESULT (thunk_fndecl)
1878 = build_decl (RESULT_DECL, 0, integer_type_node);
1879 fnname = XSTR (XEXP (DECL_RTL (thunk_fndecl), 0), 0);
1880 init_function_start (thunk_fndecl, input_filename, lineno);
1881 assemble_start_function (thunk_fndecl, fnname);
1882 ASM_OUTPUT_MI_THUNK (asm_out_file, thunk_fndecl, delta, function);
1883 assemble_end_function (thunk_fndecl, fnname);
1884 permanent_allocation (1);
1885 current_function_decl = 0;
1886 #else /* ASM_OUTPUT_MI_THUNK */
1887 /* If we don't have the necessary macro for efficient thunks, generate a
1888 thunk function that just makes a call to the real function.
1889 Unfortunately, this doesn't work for varargs. */
1891 tree a, t;
1893 if (varargs_function_p (function))
1894 cp_error ("generic thunk code fails for method `%#D' which uses `...'",
1895 function);
1897 /* Set up clone argument trees for the thunk. */
1898 t = NULL_TREE;
1899 for (a = DECL_ARGUMENTS (function); a; a = TREE_CHAIN (a))
1901 tree x = copy_node (a);
1902 TREE_CHAIN (x) = t;
1903 DECL_CONTEXT (x) = thunk_fndecl;
1904 t = x;
1906 a = nreverse (t);
1907 DECL_ARGUMENTS (thunk_fndecl) = a;
1908 DECL_RESULT (thunk_fndecl) = NULL_TREE;
1909 DECL_LANG_SPECIFIC (thunk_fndecl) = DECL_LANG_SPECIFIC (function);
1910 copy_lang_decl (thunk_fndecl);
1911 DECL_INTERFACE_KNOWN (thunk_fndecl) = 1;
1912 DECL_NOT_REALLY_EXTERN (thunk_fndecl) = 1;
1914 start_function (NULL_TREE, thunk_fndecl, NULL_TREE, 1);
1915 store_parm_decls ();
1916 current_function_is_thunk = 1;
1918 /* Build up the call to the real function. */
1919 t = build_int_2 (delta, -1 * (delta < 0));
1920 TREE_TYPE (t) = signed_type (sizetype);
1921 t = fold (build (PLUS_EXPR, TREE_TYPE (a), a, t));
1922 t = expr_tree_cons (NULL_TREE, t, NULL_TREE);
1923 for (a = TREE_CHAIN (a); a; a = TREE_CHAIN (a))
1924 t = expr_tree_cons (NULL_TREE, a, t);
1925 t = nreverse (t);
1926 t = build_call (function, TREE_TYPE (TREE_TYPE (function)), t);
1927 c_expand_return (t);
1929 finish_function (lineno, 0, 0);
1931 /* Don't let the backend defer this function. */
1932 if (DECL_DEFER_OUTPUT (thunk_fndecl))
1934 output_inline_function (thunk_fndecl);
1935 permanent_allocation (1);
1937 #endif /* ASM_OUTPUT_MI_THUNK */
1940 TREE_SET_CODE (thunk_fndecl, THUNK_DECL);
1943 /* Code for synthesizing methods which have default semantics defined. */
1945 /* For the anonymous union in TYPE, return the member that is at least as
1946 large as the rest of the members, so we can copy it. */
1948 static tree
1949 largest_union_member (type)
1950 tree type;
1952 tree f, type_size = TYPE_SIZE (type);
1954 for (f = TYPE_FIELDS (type); f; f = TREE_CHAIN (f))
1955 if (simple_cst_equal (DECL_SIZE (f), type_size) == 1)
1956 return f;
1958 /* We should always find one. */
1959 my_friendly_abort (323);
1960 return NULL_TREE;
1963 /* Generate code for default X(X&) constructor. */
1965 static void
1966 do_build_copy_constructor (fndecl)
1967 tree fndecl;
1969 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
1970 tree t;
1972 clear_last_expr ();
1973 push_momentary ();
1975 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1976 parm = TREE_CHAIN (parm);
1977 parm = convert_from_reference (parm);
1979 if (TYPE_HAS_TRIVIAL_INIT_REF (current_class_type))
1981 t = build (INIT_EXPR, void_type_node, current_class_ref, parm);
1982 TREE_SIDE_EFFECTS (t) = 1;
1983 cplus_expand_expr_stmt (t);
1985 else
1987 tree fields = TYPE_FIELDS (current_class_type);
1988 int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
1989 tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
1990 int i;
1992 for (t = CLASSTYPE_VBASECLASSES (current_class_type); t;
1993 t = TREE_CHAIN (t))
1995 tree basetype = BINFO_TYPE (t);
1996 tree p = convert_to_reference
1997 (build_reference_type (basetype), parm,
1998 CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
1999 p = convert_from_reference (p);
2001 if (p == error_mark_node)
2002 cp_error ("in default copy constructor");
2003 else
2004 current_base_init_list = tree_cons (basetype,
2005 p, current_base_init_list);
2008 for (i = 0; i < n_bases; ++i)
2010 tree p, basetype = TREE_VEC_ELT (binfos, i);
2011 if (TREE_VIA_VIRTUAL (basetype))
2012 continue;
2014 basetype = BINFO_TYPE (basetype);
2015 p = convert_to_reference
2016 (build_reference_type (basetype), parm,
2017 CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
2019 if (p == error_mark_node)
2020 cp_error ("in default copy constructor");
2021 else
2023 p = convert_from_reference (p);
2024 current_base_init_list = tree_cons (basetype,
2025 p, current_base_init_list);
2028 for (; fields; fields = TREE_CHAIN (fields))
2030 tree init, t;
2031 tree field = fields;
2033 if (TREE_CODE (field) != FIELD_DECL)
2034 continue;
2036 init = parm;
2037 if (DECL_NAME (field))
2039 if (VFIELD_NAME_P (DECL_NAME (field)))
2040 continue;
2041 if (VBASE_NAME_P (DECL_NAME (field)))
2042 continue;
2044 /* True for duplicate members. */
2045 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
2046 continue;
2048 else if ((t = TREE_TYPE (field)) != NULL_TREE
2049 && TREE_CODE (t) == UNION_TYPE
2050 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))
2051 && TYPE_FIELDS (t) != NULL_TREE)
2055 init = build (COMPONENT_REF, t, init, field);
2056 field = largest_union_member (t);
2058 while ((t = TREE_TYPE (field)) != NULL_TREE
2059 && TREE_CODE (t) == UNION_TYPE
2060 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))
2061 && TYPE_FIELDS (t) != NULL_TREE);
2063 else
2064 continue;
2066 init = build (COMPONENT_REF, TREE_TYPE (field), init, field);
2067 init = build_tree_list (NULL_TREE, init);
2069 current_member_init_list
2070 = tree_cons (DECL_NAME (field), init, current_member_init_list);
2072 current_member_init_list = nreverse (current_member_init_list);
2073 current_base_init_list = nreverse (current_base_init_list);
2074 setup_vtbl_ptr ();
2077 pop_momentary ();
2080 static void
2081 do_build_assign_ref (fndecl)
2082 tree fndecl;
2084 tree parm = TREE_CHAIN (DECL_ARGUMENTS (fndecl));
2086 clear_last_expr ();
2087 push_momentary ();
2089 parm = convert_from_reference (parm);
2091 if (TYPE_HAS_TRIVIAL_ASSIGN_REF (current_class_type))
2093 tree t = build (MODIFY_EXPR, void_type_node, current_class_ref, parm);
2094 TREE_SIDE_EFFECTS (t) = 1;
2095 cplus_expand_expr_stmt (t);
2097 else
2099 tree fields = TYPE_FIELDS (current_class_type);
2100 int n_bases = CLASSTYPE_N_BASECLASSES (current_class_type);
2101 tree binfos = TYPE_BINFO_BASETYPES (current_class_type);
2102 int i;
2104 for (i = 0; i < n_bases; ++i)
2106 tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2107 tree p = convert_to_reference
2108 (build_reference_type (basetype), parm,
2109 CONV_IMPLICIT|CONV_CONST, LOOKUP_COMPLAIN, NULL_TREE);
2110 p = convert_from_reference (p);
2111 p = build_member_call (basetype, ansi_opname [MODIFY_EXPR],
2112 build_expr_list (NULL_TREE, p));
2113 expand_expr_stmt (p);
2115 for (; fields; fields = TREE_CHAIN (fields))
2117 tree comp, init, t;
2118 tree field = fields;
2120 if (TREE_CODE (field) != FIELD_DECL)
2121 continue;
2123 if (TREE_READONLY (field))
2125 if (DECL_NAME (field))
2126 cp_error ("non-static const member `%#D', can't use default assignment operator", field);
2127 else
2128 cp_error ("non-static const member in type `%T', can't use default assignment operator", current_class_type);
2129 continue;
2131 else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
2133 if (DECL_NAME (field))
2134 cp_error ("non-static reference member `%#D', can't use default assignment operator", field);
2135 else
2136 cp_error ("non-static reference member in type `%T', can't use default assignment operator", current_class_type);
2137 continue;
2140 comp = current_class_ref;
2141 init = parm;
2143 if (DECL_NAME (field))
2145 if (VFIELD_NAME_P (DECL_NAME (field)))
2146 continue;
2147 if (VBASE_NAME_P (DECL_NAME (field)))
2148 continue;
2150 /* True for duplicate members. */
2151 if (IDENTIFIER_CLASS_VALUE (DECL_NAME (field)) != field)
2152 continue;
2154 else if ((t = TREE_TYPE (field)) != NULL_TREE
2155 && TREE_CODE (t) == UNION_TYPE
2156 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))
2157 && TYPE_FIELDS (t) != NULL_TREE)
2161 comp = build (COMPONENT_REF, t, comp, field);
2162 init = build (COMPONENT_REF, t, init, field);
2163 field = largest_union_member (t);
2165 while ((t = TREE_TYPE (field)) != NULL_TREE
2166 && TREE_CODE (t) == UNION_TYPE
2167 && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t))
2168 && TYPE_FIELDS (t) != NULL_TREE);
2170 else
2171 continue;
2173 comp = build (COMPONENT_REF, TREE_TYPE (field), comp, field);
2174 init = build (COMPONENT_REF, TREE_TYPE (field), init, field);
2176 expand_expr_stmt (build_modify_expr (comp, NOP_EXPR, init));
2179 c_expand_return (current_class_ref);
2180 pop_momentary ();
2183 void
2184 synthesize_method (fndecl)
2185 tree fndecl;
2187 int nested = (current_function_decl != NULL_TREE);
2188 tree context = hack_decl_function_context (fndecl);
2190 if (at_eof)
2191 import_export_decl (fndecl);
2193 if (! context)
2194 push_to_top_level ();
2195 else if (nested)
2196 push_cp_function_context (context);
2198 interface_unknown = 1;
2199 start_function (NULL_TREE, fndecl, NULL_TREE, 1);
2200 store_parm_decls ();
2202 if (DECL_NAME (fndecl) == ansi_opname[MODIFY_EXPR])
2203 do_build_assign_ref (fndecl);
2204 else if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2206 else
2208 tree arg_chain = FUNCTION_ARG_CHAIN (fndecl);
2209 if (DECL_CONSTRUCTOR_FOR_VBASE_P (fndecl))
2210 arg_chain = TREE_CHAIN (arg_chain);
2211 if (arg_chain != void_list_node)
2212 do_build_copy_constructor (fndecl);
2213 else if (TYPE_NEEDS_CONSTRUCTING (current_class_type))
2214 setup_vtbl_ptr ();
2217 finish_function (lineno, 0, nested);
2219 extract_interface_info ();
2220 if (! context)
2221 pop_from_top_level ();
2222 else if (nested)
2223 pop_cp_function_context (context);