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