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