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