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