]> gcc.gnu.org Git - gcc.git/blame - gcc/cp/init.c
(instantiate_class_template): Add the new instantiation to template_classes.
[gcc.git] / gcc / cp / init.c
CommitLineData
8d08fdba 1/* Handle initialization things in C++.
59be0cdd 2 Copyright (C) 1987, 89, 92, 93, 94, 1995 Free Software Foundation, Inc.
8d08fdba
MS
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
e9fa0c7c
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
8d08fdba
MS
21
22
23/* High-level class interface. */
24
25#include "config.h"
26#include "tree.h"
27#include "rtl.h"
28#include "cp-tree.h"
29#include "flags.h"
e8abc66f 30#include "output.h"
8d08fdba
MS
31
32#undef NULL
33#define NULL 0
34
35/* In C++, structures with well-defined constructors are initialized by
36 those constructors, unasked. CURRENT_BASE_INIT_LIST
37 holds a list of stmts for a BASE_INIT term in the grammar.
38 This list has one element for each base class which must be
39 initialized. The list elements are [basename, init], with
40 type basetype. This allows the possibly anachronistic form
41 (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
42 where each successive term can be handed down the constructor
43 line. Perhaps this was not intended. */
44tree current_base_init_list, current_member_init_list;
45
46void emit_base_init ();
47void check_base_init ();
48static void expand_aggr_vbase_init ();
49void expand_member_init ();
50void expand_aggr_init ();
51
52static void expand_aggr_init_1 ();
53static void expand_recursive_init_1 ();
54static void expand_recursive_init ();
7177d104 55static void expand_virtual_init PROTO((tree, tree));
8d08fdba 56tree expand_vec_init ();
8d08fdba
MS
57
58static void add_friend (), add_friends ();
59
60/* Cache _builtin_new and _builtin_delete exprs. */
a28e3c7f 61static tree BIN, BID, BIVN, BIVD;
8d08fdba
MS
62
63/* Cache the identifier nodes for the two magic field of a new cookie. */
64static tree nc_nelts_field_id;
8926095f 65#if 0
8d08fdba 66static tree nc_ptr_2comp_field_id;
8926095f 67#endif
8d08fdba
MS
68
69static tree minus_one;
70
71/* Set up local variable for this file. MUST BE CALLED AFTER
72 INIT_DECL_PROCESSING. */
73
74tree BI_header_type, BI_header_size;
75
76void init_init_processing ()
77{
78 tree fields[1];
79
80 /* Define implicit `operator new' and `operator delete' functions. */
81 BIN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) NEW_EXPR])));
82 TREE_USED (TREE_OPERAND (BIN, 0)) = 0;
83 BID = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) DELETE_EXPR])));
84 TREE_USED (TREE_OPERAND (BID, 0)) = 0;
a28e3c7f
MS
85 BIVN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) VEC_NEW_EXPR])));
86 TREE_USED (TREE_OPERAND (BIVN, 0)) = 0;
87 BIVD = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) VEC_DELETE_EXPR])));
88 TREE_USED (TREE_OPERAND (BIVD, 0)) = 0;
8d08fdba
MS
89 minus_one = build_int_2 (-1, -1);
90
91 /* Define the structure that holds header information for
92 arrays allocated via operator new. */
93 BI_header_type = make_lang_type (RECORD_TYPE);
94 nc_nelts_field_id = get_identifier ("nelts");
95 fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype);
96 finish_builtin_type (BI_header_type, "__new_cookie", fields,
97 0, double_type_node);
98 BI_header_size = size_in_bytes (BI_header_type);
99}
100
101/* Subroutine of emit_base_init. For BINFO, initialize all the
102 virtual function table pointers, except those that come from
103 virtual base classes. Initialize binfo's vtable pointer, if
104 INIT_SELF is true. CAN_ELIDE is true when we know that all virtual
105 function table pointers in all bases have been initialized already,
7177d104
MS
106 probably because their constructors have just be run. ADDR is the
107 pointer to the object whos vtables we are going to initialize.
108
109 REAL_BINFO is usually the same as BINFO, except when addr is not of
110 pointer to the type of the real derived type that we want to
111 initialize for. This is the case when addr is a pointer to a sub
112 object of a complete object, and we only want to do part of the
ddd5a7c1 113 complete object's initialization of vtable pointers. This is done
7177d104
MS
114 for all virtual table pointers in virtual base classes. REAL_BINFO
115 is used to find the BINFO_VTABLE that we initialize with. BINFO is
116 used for conversions of addr to subobjects.
117
118 BINFO_TYPE (real_binfo) must be BINFO_TYPE (binfo).
119
120 Relies upon binfo being inside TYPE_BINFO (TREE_TYPE (TREE_TYPE
121 (addr))). */
8d08fdba 122void
7177d104
MS
123expand_direct_vtbls_init (real_binfo, binfo, init_self, can_elide, addr)
124 tree real_binfo, binfo, addr;
8d08fdba
MS
125 int init_self, can_elide;
126{
7177d104 127 tree real_binfos = BINFO_BASETYPES (real_binfo);
8d08fdba 128 tree binfos = BINFO_BASETYPES (binfo);
7177d104 129 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
8d08fdba
MS
130
131 for (i = 0; i < n_baselinks; i++)
132 {
7177d104 133 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
8d08fdba
MS
134 tree base_binfo = TREE_VEC_ELT (binfos, i);
135 int is_not_base_vtable =
7177d104
MS
136 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
137 if (! TREE_VIA_VIRTUAL (real_base_binfo))
44a8d0b3
MS
138 expand_direct_vtbls_init (real_base_binfo, base_binfo,
139 is_not_base_vtable, can_elide, addr);
8d08fdba
MS
140 }
141#if 0
142 /* Before turning this on, make sure it is correct. */
43f2999d 143 if (can_elide && ! BINFO_MODIFIED (binfo))
8d08fdba
MS
144 return;
145#endif
146 /* Should we use something besides CLASSTYPE_VFIELDS? */
44a8d0b3 147 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
8d08fdba 148 {
7177d104
MS
149 tree base_ptr = convert_pointer_to_real (binfo, addr);
150 expand_virtual_init (real_binfo, base_ptr);
8d08fdba
MS
151 }
152}
153\f
154/* 348 - 351 */
155/* Subroutine of emit_base_init. */
156static void
b7484fbe
MS
157perform_member_init (member, name, init, explicit, protect_list)
158 tree member, name, init, *protect_list;
8d08fdba
MS
159 int explicit;
160{
161 tree decl;
162 tree type = TREE_TYPE (member);
163
164 if (TYPE_NEEDS_CONSTRUCTING (type)
165 || (init && TYPE_HAS_CONSTRUCTOR (type)))
166 {
167 /* Since `init' is already a TREE_LIST on the current_member_init_list,
168 only build it into one if we aren't already a list. */
169 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
170 init = build_tree_list (NULL_TREE, init);
171
172 decl = build_component_ref (C_C_D, name, 0, explicit);
173
174 if (explicit
175 && TREE_CODE (type) == ARRAY_TYPE
176 && init != NULL_TREE
177 && TREE_CHAIN (init) == NULL_TREE
178 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
179 {
180 /* Initialization of one array from another. */
181 expand_vec_init (TREE_OPERAND (decl, 1), decl,
182 array_type_nelts (type), TREE_VALUE (init), 1);
183 }
184 else
6060a796 185 expand_aggr_init (decl, init, 0, 0);
8d08fdba
MS
186 }
187 else
188 {
189 if (init == NULL_TREE)
190 {
191 if (explicit)
192 {
193 cp_error ("incomplete initializer for member `%D' of class `%T' which has no constructor",
194 member, current_class_type);
195 init = error_mark_node;
196 }
197 /* member traversal: note it leaves init NULL */
198 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
199 cp_pedwarn ("uninitialized reference member `%D'", member);
200 }
201 else if (TREE_CODE (init) == TREE_LIST)
202 {
203 /* There was an explicit member initialization. Do some
204 work in that case. */
205 if (TREE_CHAIN (init))
206 {
207 warning ("initializer list treated as compound expression");
208 init = build_compound_expr (init);
209 }
210 else
211 init = TREE_VALUE (init);
212 }
213
214 /* We only build this with a null init if we got it from the
215 current_member_init_list. */
216 if (init || explicit)
217 {
218 decl = build_component_ref (C_C_D, name, 0, explicit);
219 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
220 }
221 }
00595019 222 expand_cleanups_to (NULL_TREE);
b7484fbe
MS
223
224 if (TYPE_NEEDS_DESTRUCTOR (type))
225 {
226 tree expr = build_component_ref (C_C_D, name, 0, explicit);
227 expr = build_delete (type, expr, integer_zero_node,
228 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
229
230 if (expr != error_mark_node)
231 {
232 start_protect ();
233 *protect_list = tree_cons (NULL_TREE, expr, *protect_list);
234 }
235 }
8d08fdba
MS
236}
237
b7484fbe
MS
238extern int warn_reorder;
239
8d08fdba
MS
240/* Subroutine of emit_member_init. */
241static tree
242sort_member_init (t)
243 tree t;
244{
245 tree x, member, name, field, init;
246 tree init_list = NULL_TREE;
247 tree fields_to_unmark = NULL_TREE;
00595019
MS
248 int last_pos = 0;
249 tree last_field;
8d08fdba
MS
250
251 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
252 {
00595019 253 int pos;
b7484fbe
MS
254
255 /* member could be, for example, a CONST_DECL for an enumerated
256 tag; we don't want to try to initialize that, since it already
257 has a value. */
258 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
259 continue;
260
00595019 261 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
8d08fdba
MS
262 {
263 /* If we cleared this out, then pay no attention to it. */
264 if (TREE_PURPOSE (x) == NULL_TREE)
265 continue;
266 name = TREE_PURPOSE (x);
267
268#if 0
269 field = (TREE_CODE (name) == COMPONENT_REF
270 ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
271#else
272 /* Let's find out when this happens. */
273 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348);
274 field = IDENTIFIER_CLASS_VALUE (name);
275#endif
276
277 /* If one member shadows another, get the outermost one. */
278 if (TREE_CODE (field) == TREE_LIST)
279 field = TREE_VALUE (field);
280
281 if (field == member)
282 {
b7484fbe 283 if (warn_reorder)
00595019 284 {
b7484fbe 285 if (pos < last_pos)
00595019
MS
286 {
287 cp_warning_at ("member initializers for `%#D'", last_field);
288 cp_warning_at (" and `%#D'", field);
289 warning (" will be re-ordered to match declaration order");
290 }
291 last_pos = pos;
292 last_field = field;
293 }
8d08fdba 294
8d08fdba
MS
295 /* Make sure we won't try to work on this init again. */
296 TREE_PURPOSE (x) = NULL_TREE;
b7484fbe
MS
297 x = build_tree_list (name, TREE_VALUE (x));
298 goto got_it;
8d08fdba
MS
299 }
300 }
301
302 /* If we didn't find MEMBER in the list, create a dummy entry
303 so the two lists (INIT_LIST and the list of members) will be
304 symmetrical. */
b7484fbe
MS
305 x = build_tree_list (NULL_TREE, NULL_TREE);
306 got_it:
307 init_list = chainon (init_list, x);
8d08fdba
MS
308 }
309
b7484fbe 310 /* Initializers for base members go at the end. */
8d08fdba
MS
311 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
312 {
b7484fbe
MS
313 name = TREE_PURPOSE (x);
314 if (name)
8d08fdba 315 {
b7484fbe 316 if (purpose_member (name, init_list))
8d08fdba 317 {
b7484fbe
MS
318 cp_error ("multiple initializations given for member `%D'",
319 IDENTIFIER_CLASS_VALUE (name));
320 continue;
8d08fdba 321 }
b7484fbe
MS
322
323 init_list = chainon (init_list,
324 build_tree_list (name, TREE_VALUE (x)));
325 TREE_PURPOSE (x) = NULL_TREE;
326 }
327 }
8d08fdba 328
b7484fbe
MS
329 return init_list;
330}
8d08fdba 331
b7484fbe
MS
332static void
333sort_base_init (t, rbase_ptr, vbase_ptr)
334 tree t, *rbase_ptr, *vbase_ptr;
335{
336 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
337 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
338
339 int i;
340 tree x;
341 tree last;
342
343 /* For warn_reorder. */
344 int last_pos = 0;
345 tree last_base = NULL_TREE;
346
347 tree rbases = NULL_TREE;
348 tree vbases = NULL_TREE;
8d08fdba 349
b7484fbe
MS
350 /* First walk through and splice out vbase and invalid initializers.
351 Also replace names with binfos. */
352
353 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list);
354 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
355 {
356 tree basename = TREE_PURPOSE (x);
357 tree binfo;
358
359 if (basename == NULL_TREE)
360 {
361 /* Initializer for single base class. Must not
362 use multiple inheritance or this is ambiguous. */
363 switch (n_baseclasses)
8d08fdba 364 {
b7484fbe
MS
365 case 0:
366 cp_error ("`%T' does not have a base class to initialize",
367 current_class_type);
368 return;
369 case 1:
370 break;
371 default:
372 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
373 current_class_type);
374 return;
8d08fdba 375 }
b7484fbe
MS
376 binfo = TREE_VEC_ELT (binfos, 0);
377 }
378 else if (is_aggr_typedef (basename, 1))
379 {
380 binfo = binfo_or_else (IDENTIFIER_TYPE_VALUE (basename), t);
381 if (binfo == NULL_TREE)
382 continue;
8d08fdba 383
b7484fbe
MS
384 /* Virtual base classes are special cases. Their initializers
385 are recorded with this constructor, and they are used when
386 this constructor is the top-level constructor called. */
387 if (TREE_VIA_VIRTUAL (binfo))
388 {
389 tree v = CLASSTYPE_VBASECLASSES (t);
390 while (BINFO_TYPE (v) != BINFO_TYPE (binfo))
391 v = TREE_CHAIN (v);
8d08fdba 392
b7484fbe
MS
393 vbases = tree_cons (v, TREE_VALUE (x), vbases);
394 continue;
395 }
396 else
397 {
398 /* Otherwise, if it is not an immediate base class, complain. */
399 for (i = n_baseclasses-1; i >= 0; i--)
400 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
401 break;
402 if (i < 0)
403 {
404 cp_error ("`%T' is not an immediate base class of `%T'",
405 IDENTIFIER_TYPE_VALUE (basename),
406 current_class_type);
407 continue;
408 }
409 }
8d08fdba 410 }
b7484fbe
MS
411 else
412 my_friendly_abort (365);
413
414 TREE_PURPOSE (x) = binfo;
415 TREE_CHAIN (last) = x;
416 last = x;
8d08fdba 417 }
b7484fbe 418 TREE_CHAIN (last) = NULL_TREE;
8d08fdba 419
b7484fbe
MS
420 /* Now walk through our regular bases and make sure they're initialized. */
421
422 for (i = 0; i < n_baseclasses; ++i)
8d08fdba 423 {
b7484fbe
MS
424 tree base_binfo = TREE_VEC_ELT (binfos, i);
425 int pos;
426
427 if (TREE_VIA_VIRTUAL (base_binfo))
428 continue;
429
430 for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
431 {
432 tree binfo = TREE_PURPOSE (x);
433
434 if (binfo == NULL_TREE)
435 continue;
436
437 if (binfo == base_binfo)
438 {
439 if (warn_reorder)
440 {
441 if (pos < last_pos)
442 {
443 cp_warning_at ("base initializers for `%#T'", last_base);
444 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
445 warning (" will be re-ordered to match inheritance order");
446 }
447 last_pos = pos;
448 last_base = BINFO_TYPE (binfo);
449 }
450
451 /* Make sure we won't try to work on this init again. */
452 TREE_PURPOSE (x) = NULL_TREE;
453 x = build_tree_list (binfo, TREE_VALUE (x));
454 goto got_it;
455 }
456 }
457
458 /* If we didn't find BASE_BINFO in the list, create a dummy entry
459 so the two lists (RBASES and the list of bases) will be
460 symmetrical. */
461 x = build_tree_list (NULL_TREE, NULL_TREE);
462 got_it:
463 rbases = chainon (rbases, x);
8d08fdba
MS
464 }
465
b7484fbe
MS
466 *rbase_ptr = rbases;
467 *vbase_ptr = vbases;
468}
469
470/* Perform partial cleanups for a base for exception handling. */
471static tree
472build_partial_cleanup_for (binfo)
473 tree binfo;
474{
475 tree expr = convert_pointer_to_real (binfo,
476 build_unary_op (ADDR_EXPR, C_C_D, 0));
477
478 return build_delete (TREE_TYPE (expr),
479 expr,
480 integer_zero_node,
481 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
8d08fdba
MS
482}
483
484/* Perform whatever initializations have yet to be done on the base
485 class of the class variable. These actions are in the global
486 variable CURRENT_BASE_INIT_LIST. Such an action could be
487 NULL_TREE, meaning that the user has explicitly called the base
488 class constructor with no arguments.
489
490 If there is a need for a call to a constructor, we must surround
491 that call with a pushlevel/poplevel pair, since we are technically
492 at the PARM level of scope.
493
494 Argument IMMEDIATELY, if zero, forces a new sequence to be
495 generated to contain these new insns, so it can be emitted later.
a9aedbc2 496 This sequence is saved in the global variable BASE_INIT_EXPR.
8d08fdba
MS
497 Otherwise, the insns are emitted into the current sequence.
498
499 Note that emit_base_init does *not* initialize virtual base
500 classes. That is done specially, elsewhere. */
a9aedbc2
MS
501
502extern tree base_init_expr, rtl_expr_chain;
503
8d08fdba
MS
504void
505emit_base_init (t, immediately)
506 tree t;
507 int immediately;
508{
509 extern tree in_charge_identifier;
510
b7484fbe
MS
511 tree member, x;
512 tree mem_init_list;
513 tree rbase_init_list, vbase_init_list;
8d08fdba
MS
514 tree t_binfo = TYPE_BINFO (t);
515 tree binfos = BINFO_BASETYPES (t_binfo);
516 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
a9aedbc2 517 tree expr = NULL_TREE;
b7484fbe
MS
518
519 my_friendly_assert (protect_list == NULL_TREE, 999);
8d08fdba
MS
520
521 if (! immediately)
522 {
a9aedbc2 523 int momentary;
8d08fdba 524 do_pending_stack_adjust ();
a9aedbc2
MS
525 /* Make the RTL_EXPR node temporary, not momentary,
526 so that rtl_expr_chain doesn't become garbage. */
527 momentary = suspend_momentary ();
528 expr = make_node (RTL_EXPR);
529 resume_momentary (momentary);
530 start_sequence_for_rtl_expr (expr);
8d08fdba
MS
531 }
532
533 if (write_symbols == NO_DEBUG)
534 /* As a matter of principle, `start_sequence' should do this. */
535 emit_note (0, -1);
536 else
537 /* Always emit a line number note so we can step into constructors. */
538 emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
539 DECL_SOURCE_LINE (current_function_decl));
540
b7484fbe
MS
541 mem_init_list = sort_member_init (t);
542 current_member_init_list = NULL_TREE;
8d08fdba 543
b7484fbe
MS
544 sort_base_init (t, &rbase_init_list, &vbase_init_list);
545 current_base_init_list = NULL_TREE;
8d08fdba 546
b7484fbe
MS
547 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
548 {
549 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
8d08fdba 550
b7484fbe
MS
551 expand_start_cond (first_arg, 0);
552 expand_aggr_vbase_init (t_binfo, C_C_D, current_class_decl,
553 vbase_init_list);
554 expand_end_cond ();
8d08fdba 555 }
8d08fdba 556
b7484fbe 557 /* Now, perform initialization of non-virtual base classes. */
8d08fdba
MS
558 for (i = 0; i < n_baseclasses; i++)
559 {
560 tree base = current_class_decl;
561 tree base_binfo = TREE_VEC_ELT (binfos, i);
b7484fbe
MS
562 tree init = void_list_node;
563
564 if (TREE_VIA_VIRTUAL (base_binfo))
565 continue;
8d08fdba 566
8ccc31eb
MS
567#if 0 /* Once unsharing happens soon enough. */
568 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo);
569#else
570 BINFO_INHERITANCE_CHAIN (base_binfo) = t_binfo;
571#endif
572
b7484fbe
MS
573 if (TREE_PURPOSE (rbase_init_list))
574 init = TREE_VALUE (rbase_init_list);
575 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
576 init = NULL_TREE;
8d08fdba 577
b7484fbe
MS
578 if (init != void_list_node)
579 {
28cbf42c 580 member = convert_pointer_to_real (base_binfo, current_class_decl);
b7484fbe
MS
581 expand_aggr_init_1 (base_binfo, 0,
582 build_indirect_ref (member, NULL_PTR), init,
583 BINFO_OFFSET_ZEROP (base_binfo), LOOKUP_NORMAL);
584 expand_cleanups_to (NULL_TREE);
8d08fdba 585 }
8d08fdba 586
b7484fbe 587 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
8d08fdba 588 {
b7484fbe
MS
589 start_protect ();
590 protect_list = tree_cons (NULL_TREE,
591 build_partial_cleanup_for (base_binfo),
592 protect_list);
8d08fdba 593 }
b7484fbe
MS
594
595 rbase_init_list = TREE_CHAIN (rbase_init_list);
8d08fdba
MS
596 }
597
598 /* Initialize all the virtual function table fields that
599 do come from virtual base classes. */
600 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
7177d104 601 expand_indirect_vtbls_init (t_binfo, C_C_D, current_class_decl, 0);
8d08fdba
MS
602
603 /* Initialize all the virtual function table fields that
604 do not come from virtual base classes. */
7177d104 605 expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_decl);
8d08fdba 606
8d08fdba
MS
607 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
608 {
609 tree init, name;
b7484fbe
MS
610 int from_init_list;
611
612 /* member could be, for example, a CONST_DECL for an enumerated
613 tag; we don't want to try to initialize that, since it already
614 has a value. */
615 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
616 continue;
8d08fdba
MS
617
618 /* See if we had a user-specified member initialization. */
b7484fbe 619 if (TREE_PURPOSE (mem_init_list))
8d08fdba 620 {
b7484fbe
MS
621 name = TREE_PURPOSE (mem_init_list);
622 init = TREE_VALUE (mem_init_list);
623 from_init_list = 1;
8d08fdba 624
b7484fbe
MS
625 /* Also see if it's ever a COMPONENT_REF here. If it is, we
626 need to do `expand_assignment (name, init, 0, 0);' and
627 a continue. */
628 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349);
8d08fdba 629 }
b7484fbe 630 else
8d08fdba 631 {
8d08fdba
MS
632 name = DECL_NAME (member);
633 init = DECL_INITIAL (member);
b7484fbe
MS
634
635 from_init_list = 0;
8d08fdba
MS
636 }
637
b7484fbe
MS
638 perform_member_init (member, name, init, from_init_list, &protect_list);
639 mem_init_list = TREE_CHAIN (mem_init_list);
8d08fdba
MS
640 }
641
b7484fbe
MS
642 /* Now initialize any members from our bases. */
643 while (mem_init_list)
644 {
645 tree name, init, field;
646
647 if (TREE_PURPOSE (mem_init_list))
648 {
649 name = TREE_PURPOSE (mem_init_list);
650 init = TREE_VALUE (mem_init_list);
651 /* XXX: this may need the COMPONENT_REF operand 0 check if
652 it turns out we actually get them. */
653 field = IDENTIFIER_CLASS_VALUE (name);
654
655 /* If one member shadows another, get the outermost one. */
656 if (TREE_CODE (field) == TREE_LIST)
657 {
658 field = TREE_VALUE (field);
659 if (decl_type_context (field) != current_class_type)
660 cp_error ("field `%D' not in immediate context", field);
661 }
662
663#if 0
664 /* It turns out if you have an anonymous union in the
665 class, a member from it can end up not being on the
666 list of fields (rather, the type is), and therefore
667 won't be seen by the for loop above. */
668
669 /* The code in this for loop is derived from a general loop
670 which had this check in it. Theoretically, we've hit
671 every initialization for the list of members in T, so
672 we shouldn't have anything but these left in this list. */
673 my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351);
674#endif
675
676 perform_member_init (field, name, init, 1, &protect_list);
677 }
678 mem_init_list = TREE_CHAIN (mem_init_list);
679 }
8d08fdba 680
8d08fdba
MS
681 if (! immediately)
682 {
8d08fdba 683 do_pending_stack_adjust ();
a9aedbc2
MS
684 my_friendly_assert (base_init_expr == 0, 207);
685 base_init_expr = expr;
686 TREE_TYPE (expr) = void_type_node;
687 RTL_EXPR_RTL (expr) = const0_rtx;
688 RTL_EXPR_SEQUENCE (expr) = get_insns ();
689 rtl_expr_chain = tree_cons (NULL_TREE, expr, rtl_expr_chain);
8d08fdba 690 end_sequence ();
a9aedbc2 691 TREE_SIDE_EFFECTS (expr) = 1;
8d08fdba
MS
692 }
693
694 /* All the implicit try blocks we built up will be zapped
695 when we come to a real binding contour boundary. */
696}
697
698/* Check that all fields are properly initialized after
699 an assignment to `this'. */
700void
701check_base_init (t)
702 tree t;
703{
704 tree member;
705 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
706 if (DECL_NAME (member) && TREE_USED (member))
707 cp_error ("field `%D' used before initialized (after assignment to `this')",
708 member);
709}
710
711/* This code sets up the virtual function tables appropriate for
712 the pointer DECL. It is a one-ply initialization.
713
714 BINFO is the exact type that DECL is supposed to be. In
715 multiple inheritance, this might mean "C's A" if C : A, B. */
8926095f 716static void
7177d104
MS
717expand_virtual_init (binfo, decl)
718 tree binfo, decl;
8d08fdba 719{
7177d104 720 tree type = BINFO_TYPE (binfo);
8d08fdba
MS
721 tree vtbl, vtbl_ptr;
722 tree vtype, vtype_binfo;
723
7177d104
MS
724 /* This code is crusty. Should be simple, like:
725 vtbl = BINFO_VTABLE (binfo);
726 */
8d08fdba
MS
727 vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
728 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
7177d104 729 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo));
e3417fcd 730 assemble_external (vtbl);
7177d104
MS
731 TREE_USED (vtbl) = 1;
732 vtbl = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (vtbl)), vtbl);
8d08fdba
MS
733 decl = convert_pointer_to_real (vtype_binfo, decl);
734 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
735 if (vtbl_ptr == error_mark_node)
8926095f 736 return;
8d08fdba
MS
737
738 /* Have to convert VTBL since array sizes may be different. */
6060a796 739 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
7177d104 740 expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
8d08fdba
MS
741}
742
743/* Subroutine of `expand_aggr_vbase_init'.
744 BINFO is the binfo of the type that is being initialized.
745 INIT_LIST is the list of initializers for the virtual baseclass. */
746static void
747expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
748 tree binfo, exp, addr, init_list;
749{
b7484fbe 750 tree init = purpose_member (binfo, init_list);
8d08fdba
MS
751 tree ref = build_indirect_ref (addr, NULL_PTR);
752 if (init)
b7484fbe 753 init = TREE_VALUE (init);
8d08fdba 754 /* Call constructors, but don't set up vtables. */
db5ae43f 755 expand_aggr_init_1 (binfo, exp, ref, init, 0, LOOKUP_COMPLAIN);
00595019 756 expand_cleanups_to (NULL_TREE);
8d08fdba
MS
757}
758
759/* Initialize this object's virtual base class pointers. This must be
760 done only at the top-level of the object being constructed.
761
762 INIT_LIST is list of initialization for constructor to perform. */
763static void
764expand_aggr_vbase_init (binfo, exp, addr, init_list)
765 tree binfo;
766 tree exp;
767 tree addr;
768 tree init_list;
769{
770 tree type = BINFO_TYPE (binfo);
771
772 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
773 {
774 tree result = init_vbase_pointers (type, addr);
775 tree vbases;
776
777 if (result)
778 expand_expr_stmt (build_compound_expr (result));
779
b7484fbe
MS
780 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
781 vbases = TREE_CHAIN (vbases))
782 {
783 tree tmp = purpose_member (vbases, result);
784 expand_aggr_vbase_init_1 (vbases, exp,
785 TREE_OPERAND (TREE_VALUE (tmp), 0),
786 init_list);
787 }
8d08fdba
MS
788 }
789}
790
791/* Subroutine to perform parser actions for member initialization.
792 S_ID is the scoped identifier.
793 NAME is the name of the member.
794 INIT is the initializer, or `void_type_node' if none. */
795void
796do_member_init (s_id, name, init)
797 tree s_id, name, init;
798{
799 tree binfo, base;
800
801 if (current_class_type == NULL_TREE
802 || ! is_aggr_typedef (s_id, 1))
803 return;
804 binfo = get_binfo (IDENTIFIER_TYPE_VALUE (s_id),
805 current_class_type, 1);
806 if (binfo == error_mark_node)
807 return;
808 if (binfo == 0)
809 {
810 error_not_base_type (IDENTIFIER_TYPE_VALUE (s_id), current_class_type);
811 return;
812 }
813
814 base = convert_pointer_to (binfo, current_class_decl);
815 expand_member_init (build_indirect_ref (base, NULL_PTR), name, init);
816}
817
818/* Function to give error message if member initialization specification
819 is erroneous. FIELD is the member we decided to initialize.
820 TYPE is the type for which the initialization is being performed.
821 FIELD must be a member of TYPE, or the base type from which FIELD
822 comes must not need a constructor.
823
824 MEMBER_NAME is the name of the member. */
825
826static int
827member_init_ok_or_else (field, type, member_name)
828 tree field;
829 tree type;
830 char *member_name;
831{
832 if (field == error_mark_node)
833 return 0;
834 if (field == NULL_TREE)
835 {
836 cp_error ("class `%T' does not have any field named `%s'", type,
b7484fbe 837 member_name);
8d08fdba
MS
838 return 0;
839 }
840 if (DECL_CONTEXT (field) != type
841 && TYPE_NEEDS_CONSTRUCTING (DECL_CONTEXT (field)))
842 {
843 cp_error ("member `%D' comes from base class needing constructor",
844 field);
845 return 0;
846 }
b7484fbe
MS
847 if (TREE_STATIC (field))
848 {
849 cp_error ("field `%#D' is static; only point of initialization is its declaration",
850 field);
851 return 0;
852 }
853
8d08fdba
MS
854 return 1;
855}
856
857/* If NAME is a viable field name for the aggregate DECL,
858 and PARMS is a viable parameter list, then expand an _EXPR
859 which describes this initialization.
860
861 Note that we do not need to chase through the class's base classes
862 to look for NAME, because if it's in that list, it will be handled
863 by the constructor for that base class.
864
865 We do not yet have a fixed-point finder to instantiate types
866 being fed to overloaded constructors. If there is a unique
867 constructor, then argument types can be got from that one.
868
869 If INIT is non-NULL, then it the initialization should
870 be placed in `current_base_init_list', where it will be processed
871 by `emit_base_init'. */
872void
873expand_member_init (exp, name, init)
874 tree exp, name, init;
875{
876 extern tree ptr_type_node; /* should be in tree.h */
877
878 tree basetype = NULL_TREE, field;
879 tree parm;
880 tree rval, type;
881 tree actual_name;
882
883 if (exp == NULL_TREE)
884 return; /* complain about this later */
885
886 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
887
888 if (name == NULL_TREE && IS_AGGR_TYPE (type))
889 switch (CLASSTYPE_N_BASECLASSES (type))
890 {
891 case 0:
892 error ("base class initializer specified, but no base class to initialize");
893 return;
894 case 1:
895 basetype = TYPE_BINFO_BASETYPE (type, 0);
896 break;
897 default:
898 error ("initializer for unnamed base class ambiguous");
899 cp_error ("(type `%T' uses multiple inheritance)", type);
900 return;
901 }
902
903 if (init)
904 {
905 /* The grammar should not allow fields which have names
906 that are TYPENAMEs. Therefore, if the field has
907 a non-NULL TREE_TYPE, we may assume that this is an
908 attempt to initialize a base class member of the current
909 type. Otherwise, it is an attempt to initialize a
910 member field. */
911
912 if (init == void_type_node)
913 init = NULL_TREE;
914
915 if (name == NULL_TREE || IDENTIFIER_HAS_TYPE_VALUE (name))
916 {
917 tree base_init;
918
919 if (name == NULL_TREE)
920 {
921/*
922 if (basetype)
923 name = TYPE_IDENTIFIER (basetype);
924 else
925 {
926 error ("no base class to initialize");
927 return;
928 }
929*/
930 }
931 else
932 {
933 basetype = IDENTIFIER_TYPE_VALUE (name);
934 if (basetype != type
935 && ! binfo_member (basetype, TYPE_BINFO (type))
936 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
937 {
938 if (IDENTIFIER_CLASS_VALUE (name))
939 goto try_member;
940 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
941 error ("type `%s' is not an immediate or virtual basetype for `%s'",
942 IDENTIFIER_POINTER (name),
943 TYPE_NAME_STRING (type));
944 else
945 error ("type `%s' is not an immediate basetype for `%s'",
946 IDENTIFIER_POINTER (name),
947 TYPE_NAME_STRING (type));
948 return;
949 }
950 }
951
952 if (purpose_member (name, current_base_init_list))
953 {
954 error ("base class `%s' already initialized",
955 IDENTIFIER_POINTER (name));
956 return;
957 }
958
959 base_init = build_tree_list (name, init);
960 TREE_TYPE (base_init) = basetype;
961 current_base_init_list = chainon (current_base_init_list, base_init);
962 }
963 else
964 {
965 tree member_init;
966
967 try_member:
968 field = lookup_field (type, name, 1, 0);
969
970 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
971 return;
972
973 if (purpose_member (name, current_member_init_list))
974 {
975 error ("field `%s' already initialized", IDENTIFIER_POINTER (name));
976 return;
977 }
978
979 member_init = build_tree_list (name, init);
980 TREE_TYPE (member_init) = TREE_TYPE (field);
981 current_member_init_list = chainon (current_member_init_list, member_init);
982 }
983 return;
984 }
985 else if (name == NULL_TREE)
986 {
987 compiler_error ("expand_member_init: name == NULL_TREE");
988 return;
989 }
990
991 basetype = type;
992 field = lookup_field (basetype, name, 0, 0);
993
994 if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name)))
995 return;
996
997 /* now see if there is a constructor for this type
998 which will take these args. */
999
1000 if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field)))
1001 {
1002 tree parmtypes, fndecl;
1003
1004 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1005 {
1006 /* just know that we've seen something for this node */
1007 DECL_INITIAL (exp) = error_mark_node;
1008 TREE_USED (exp) = 1;
1009 }
1010 type = TYPE_MAIN_VARIANT (TREE_TYPE (field));
1011 actual_name = TYPE_IDENTIFIER (type);
1012 parm = build_component_ref (exp, name, 0, 0);
1013
1014 /* Now get to the constructor. */
1015 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
1016 /* Get past destructor, if any. */
1017 if (TYPE_HAS_DESTRUCTOR (type))
1018 fndecl = DECL_CHAIN (fndecl);
1019
1020 if (fndecl)
1021 my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 209);
1022
1023 /* If the field is unique, we can use the parameter
1024 types to guide possible type instantiation. */
1025 if (DECL_CHAIN (fndecl) == NULL_TREE)
1026 {
1027 /* There was a confusion here between
1028 FIELD and FNDECL. The following code
1029 should be correct, but abort is here
1030 to make sure. */
1031 my_friendly_abort (48);
1032 parmtypes = FUNCTION_ARG_CHAIN (fndecl);
1033 }
1034 else
1035 {
1036 parmtypes = NULL_TREE;
1037 fndecl = NULL_TREE;
1038 }
1039
1040 init = convert_arguments (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL);
1041 if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node)
1042 rval = build_method_call (NULL_TREE, actual_name, init, NULL_TREE, LOOKUP_NORMAL);
1043 else
1044 return;
1045
1046 if (rval != error_mark_node)
1047 {
1048 /* Now, fill in the first parm with our guy */
1049 TREE_VALUE (TREE_OPERAND (rval, 1))
1050 = build_unary_op (ADDR_EXPR, parm, 0);
1051 TREE_TYPE (rval) = ptr_type_node;
1052 TREE_SIDE_EFFECTS (rval) = 1;
1053 }
1054 }
1055 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1056 {
1057 parm = build_component_ref (exp, name, 0, 0);
6060a796 1058 expand_aggr_init (parm, NULL_TREE, 0, 0);
8d08fdba
MS
1059 rval = error_mark_node;
1060 }
1061
1062 /* Now initialize the member. It does not have to
1063 be of aggregate type to receive initialization. */
1064 if (rval != error_mark_node)
1065 expand_expr_stmt (rval);
1066}
1067
1068/* This is like `expand_member_init', only it stores one aggregate
1069 value into another.
1070
1071 INIT comes in two flavors: it is either a value which
1072 is to be stored in EXP, or it is a parameter list
1073 to go to a constructor, which will operate on EXP.
6060a796
MS
1074 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1075 the initializer, if FLAGS is 0, then it is the (init) form.
8d08fdba 1076 If `init' is a CONSTRUCTOR, then we emit a warning message,
59be0cdd 1077 explaining that such initializations are invalid.
8d08fdba
MS
1078
1079 ALIAS_THIS is nonzero iff we are initializing something which is
1080 essentially an alias for C_C_D. In this case, the base constructor
1081 may move it on us, and we must keep track of such deviations.
1082
1083 If INIT resolves to a CALL_EXPR which happens to return
1084 something of the type we are looking for, then we know
1085 that we can safely use that call to perform the
1086 initialization.
1087
1088 The virtual function table pointer cannot be set up here, because
1089 we do not really know its type.
1090
1091 Virtual baseclass pointers are also set up here.
1092
1093 This never calls operator=().
1094
1095 When initializing, nothing is CONST.
1096
1097 A default copy constructor may have to be used to perform the
1098 initialization.
1099
1100 A constructor or a conversion operator may have to be used to
1101 perform the initialization, but not both, as it would be ambiguous.
1102 */
1103
1104void
6060a796 1105expand_aggr_init (exp, init, alias_this, flags)
8d08fdba
MS
1106 tree exp, init;
1107 int alias_this;
6060a796 1108 int flags;
8d08fdba
MS
1109{
1110 tree type = TREE_TYPE (exp);
1111 int was_const = TREE_READONLY (exp);
1112
1113 if (init == error_mark_node)
1114 return;
1115
1116 TREE_READONLY (exp) = 0;
1117
1118 if (TREE_CODE (type) == ARRAY_TYPE)
1119 {
1120 /* Must arrange to initialize each element of EXP
1121 from elements of INIT. */
1122 int was_const_elts = TYPE_READONLY (TREE_TYPE (type));
1123 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1124 if (was_const_elts)
f376e137
MS
1125 {
1126 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1127 if (init)
1128 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1129 }
8d08fdba
MS
1130 if (init && TREE_TYPE (init) == NULL_TREE)
1131 {
1132 /* Handle bad initializers like:
1133 class COMPLEX {
1134 public:
1135 double re, im;
1136 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1137 ~COMPLEX() {};
1138 };
1139
1140 int main(int argc, char **argv) {
1141 COMPLEX zees(1.0, 0.0)[10];
1142 }
1143 */
1144 error ("bad array initializer");
1145 return;
1146 }
1147 expand_vec_init (exp, exp, array_type_nelts (type), init,
1148 init && comptypes (TREE_TYPE (init), TREE_TYPE (exp), 1));
1149 TREE_READONLY (exp) = was_const;
1150 TREE_TYPE (exp) = type;
f376e137
MS
1151 if (init)
1152 TREE_TYPE (init) = itype;
8d08fdba
MS
1153 return;
1154 }
1155
1156 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1157 /* just know that we've seen something for this node */
1158 TREE_USED (exp) = 1;
1159
1160#if 0
1161 /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
1162 constructor as parameters to an implicit GNU C++ constructor. */
1163 if (init && TREE_CODE (init) == CONSTRUCTOR
1164 && TYPE_HAS_CONSTRUCTOR (type)
1165 && TREE_TYPE (init) == type)
1166 init = CONSTRUCTOR_ELTS (init);
1167#endif
1168 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
6060a796 1169 init, alias_this, LOOKUP_NORMAL|flags);
8d08fdba
MS
1170 TREE_READONLY (exp) = was_const;
1171}
1172
1173static void
1174expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags)
1175 tree binfo;
1176 tree true_exp, exp;
1177 tree type;
1178 tree init;
1179 int alias_this;
1180 int flags;
1181{
1182 /* It fails because there may not be a constructor which takes
1183 its own type as the first (or only parameter), but which does
1184 take other types via a conversion. So, if the thing initializing
1185 the expression is a unit element of type X, first try X(X&),
1186 followed by initialization by X. If neither of these work
1187 out, then look hard. */
1188 tree rval;
1189 tree parms;
8d08fdba 1190
b7484fbe
MS
1191 if (init == NULL_TREE
1192 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
8d08fdba
MS
1193 {
1194 parms = init;
db5ae43f
MS
1195 if (parms)
1196 init = TREE_VALUE (parms);
8d08fdba 1197 }
8ccc31eb
MS
1198 else if (TREE_CODE (init) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (init)
1199 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (init)))
8d08fdba
MS
1200 {
1201 rval = convert_for_initialization (exp, type, init, 0, 0, 0, 0);
f376e137 1202 TREE_USED (rval) = 1;
8d08fdba
MS
1203 expand_expr_stmt (rval);
1204 return;
1205 }
1206 else
1207 parms = build_tree_list (NULL_TREE, init);
1208
8d08fdba
MS
1209 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1210 {
1211 if (true_exp == exp)
1212 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1213 else
1214 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1215 flags |= LOOKUP_HAS_IN_CHARGE;
1216 }
1217
db5ae43f 1218 if (init && TREE_CHAIN (parms) == NULL_TREE
e8abc66f 1219 && TYPE_HAS_TRIVIAL_INIT_REF (type)
db5ae43f 1220 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (TREE_TYPE (init)))
8d08fdba 1221 {
db5ae43f
MS
1222 rval = build (INIT_EXPR, type, exp, init);
1223 TREE_SIDE_EFFECTS (rval) = 1;
1224 expand_expr_stmt (rval);
8d08fdba 1225 }
db5ae43f 1226 else
8d08fdba 1227 {
db5ae43f
MS
1228 rval = build_method_call (exp, constructor_name_full (type),
1229 parms, binfo, flags);
8d08fdba 1230
db5ae43f
MS
1231 /* Private, protected, or otherwise unavailable. */
1232 if (rval == error_mark_node)
8d08fdba 1233 {
db5ae43f
MS
1234 if (flags & LOOKUP_COMPLAIN)
1235 cp_error ("in base initialization for %sclass `%T'",
1236 TREE_VIA_VIRTUAL (binfo) ? "virtual base " : "",
1237 binfo);
8d08fdba 1238 }
db5ae43f
MS
1239 else if (rval == NULL_TREE)
1240 my_friendly_abort (361);
8d08fdba
MS
1241 else
1242 {
db5ae43f
MS
1243 /* p. 222: if the base class assigns to `this', then that
1244 value is used in the derived class. */
1245 if ((flag_this_is_variable & 1) && alias_this)
1246 {
1247 TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
1248 expand_assignment (current_class_decl, rval, 0, 0);
1249 }
1250 else
1251 expand_expr_stmt (rval);
8d08fdba 1252 }
8d08fdba 1253 }
8d08fdba
MS
1254}
1255
1256/* This function is responsible for initializing EXP with INIT
1257 (if any).
1258
1259 BINFO is the binfo of the type for who we are performing the
1260 initialization. For example, if W is a virtual base class of A and B,
1261 and C : A, B.
1262 If we are initializing B, then W must contain B's W vtable, whereas
1263 were we initializing C, W must contain C's W vtable.
1264
1265 TRUE_EXP is nonzero if it is the true expression being initialized.
1266 In this case, it may be EXP, or may just contain EXP. The reason we
1267 need this is because if EXP is a base element of TRUE_EXP, we
1268 don't necessarily know by looking at EXP where its virtual
1269 baseclass fields should really be pointing. But we do know
1270 from TRUE_EXP. In constructors, we don't know anything about
1271 the value being initialized.
1272
1273 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1274
1275 FLAGS is just passes to `build_method_call'. See that function for
1276 its description. */
1277
1278static void
1279expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags)
1280 tree binfo;
1281 tree true_exp, exp;
1282 tree init;
1283 int alias_this;
1284 int flags;
1285{
1286 tree type = TREE_TYPE (exp);
1287 tree init_type = NULL_TREE;
8d08fdba
MS
1288
1289 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1290
1291 /* Use a function returning the desired type to initialize EXP for us.
1292 If the function is a constructor, and its first argument is
1293 NULL_TREE, know that it was meant for us--just slide exp on
1294 in and expand the constructor. Constructors now come
1295 as TARGET_EXPRs. */
1296 if (init)
1297 {
1298 tree init_list = NULL_TREE;
1299
1300 if (TREE_CODE (init) == TREE_LIST)
1301 {
1302 init_list = init;
1303 if (TREE_CHAIN (init) == NULL_TREE)
1304 init = TREE_VALUE (init);
1305 }
1306
1307 init_type = TREE_TYPE (init);
1308
1309 if (TREE_CODE (init) != TREE_LIST)
1310 {
1311 if (TREE_CODE (init_type) == ERROR_MARK)
1312 return;
1313
1314#if 0
1315 /* These lines are found troublesome 5/11/89. */
1316 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1317 init_type = TREE_TYPE (init_type);
1318#endif
1319
1320 /* This happens when we use C++'s functional cast notation.
1321 If the types match, then just use the TARGET_EXPR
1322 directly. Otherwise, we need to create the initializer
1323 separately from the object being initialized. */
1324 if (TREE_CODE (init) == TARGET_EXPR)
1325 {
a292b002 1326 if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type))
8d08fdba
MS
1327 {
1328 if (TREE_CODE (exp) == VAR_DECL
1329 || TREE_CODE (exp) == RESULT_DECL)
1330 /* Unify the initialization targets. */
1331 DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp);
1332 else
1333 DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, 0, 0);
1334
1335 expand_expr_stmt (init);
1336 return;
1337 }
1338 else
1339 {
1340 init = TREE_OPERAND (init, 1);
1341 init = build (CALL_EXPR, init_type,
1342 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1343 TREE_SIDE_EFFECTS (init) = 1;
8d08fdba
MS
1344 if (init_list)
1345 TREE_VALUE (init_list) = init;
1346 }
1347 }
1348
1349 if (init_type == type && TREE_CODE (init) == CALL_EXPR
1350#if 0
59be0cdd 1351 /* It is valid to directly initialize from a CALL_EXPR
8d08fdba
MS
1352 without going through X(X&), apparently. */
1353 && ! TYPE_GETS_INIT_REF (type)
1354#endif
1355 )
1356 {
1357 /* A CALL_EXPR is a legitimate form of initialization, so
1358 we should not print this warning message. */
1359#if 0
1360 /* Should have gone away due to 5/11/89 change. */
1361 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
1362 init = convert_from_reference (init);
1363#endif
1364 expand_assignment (exp, init, 0, 0);
1365 if (exp == DECL_RESULT (current_function_decl))
1366 {
1367 /* Failing this assertion means that the return value
1368 from receives multiple initializations. */
1369 my_friendly_assert (DECL_INITIAL (exp) == NULL_TREE
1370 || DECL_INITIAL (exp) == error_mark_node,
1371 212);
1372 DECL_INITIAL (exp) = init;
1373 }
1374 return;
1375 }
1376 else if (init_type == type
1377 && TREE_CODE (init) == COND_EXPR)
1378 {
1379 /* Push value to be initialized into the cond, where possible.
1380 Avoid spurious warning messages when initializing the
1381 result of this function. */
1382 TREE_OPERAND (init, 1)
1383 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
1384 if (exp == DECL_RESULT (current_function_decl))
1385 DECL_INITIAL (exp) = NULL_TREE;
1386 TREE_OPERAND (init, 2)
1387 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
1388 if (exp == DECL_RESULT (current_function_decl))
1389 DECL_INITIAL (exp) = init;
1390 TREE_SIDE_EFFECTS (init) = 1;
1391 expand_expr (init, const0_rtx, VOIDmode, 0);
1392 free_temp_slots ();
1393 return;
1394 }
1395 }
1396
1397 /* We did not know what we were initializing before. Now we do. */
1398 if (TREE_CODE (init) == TARGET_EXPR)
1399 {
1400 tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
1401
1402 if (TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
1403 && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
1404 {
1405 /* In order for this to work for RESULT_DECLs, if their
1406 type has a constructor, then they must be BLKmode
1407 so that they will be meaningfully addressable. */
1408 tree arg = build_unary_op (ADDR_EXPR, exp, 0);
1409 init = TREE_OPERAND (init, 1);
1410 init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
1411 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1412 TREE_SIDE_EFFECTS (init) = 1;
8d08fdba
MS
1413 TREE_VALUE (TREE_OPERAND (init, 1))
1414 = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
1415
1416 if (alias_this)
1417 {
1418 expand_assignment (current_function_decl, init, 0, 0);
1419 return;
1420 }
1421 if (exp == DECL_RESULT (current_function_decl))
1422 {
1423 if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
1424 fatal ("return value from function receives multiple initializations");
1425 DECL_INITIAL (exp) = init;
1426 }
1427 expand_expr_stmt (init);
1428 return;
1429 }
1430 }
1431
1432 if (TREE_CODE (exp) == VAR_DECL
1433 && TREE_CODE (init) == CONSTRUCTOR
1434 && TREE_HAS_CONSTRUCTOR (init))
1435 {
1436 tree t = store_init_value (exp, init);
1437 if (!t)
1438 {
1439 expand_decl_init (exp);
1440 return;
1441 }
1442 t = build (INIT_EXPR, type, exp, init);
1443 TREE_SIDE_EFFECTS (t) = 1;
1444 expand_expr_stmt (t);
1445 return;
1446 }
1447
1448 /* Handle this case: when calling a constructor: xyzzy foo(bar);
1449 which really means: xyzzy foo = bar; Ugh!
1450
1451 More useful for this case: xyzzy *foo = new xyzzy (bar); */
1452
1453 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
1454 {
1455 if (init_list && TREE_CHAIN (init_list))
1456 {
1457 warning ("initializer list being treated as compound expression");
1458 init = convert (type, build_compound_expr (init_list));
1459 if (init == error_mark_node)
1460 return;
1461 }
1462
1463 expand_assignment (exp, init, 0, 0);
1464
1465 return;
1466 }
1467 /* See whether we can go through a type conversion operator.
1468 This wins over going through a non-existent constructor. If
1469 there is a constructor, it is ambiguous. */
1470 if (TREE_CODE (init) != TREE_LIST)
1471 {
1472 tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
1473 ? TREE_TYPE (init_type) : init_type;
1474
1475 if (ttype != type && IS_AGGR_TYPE (ttype))
1476 {
1477 tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
1478
1479 if (rval)
1480 {
1481 /* See if there is a constructor for``type'' that takes a
1482 ``ttype''-typed object. */
1483 tree parms = build_tree_list (NULL_TREE, init);
1484 tree as_cons = NULL_TREE;
1485 if (TYPE_HAS_CONSTRUCTOR (type))
1486 as_cons = build_method_call (exp, constructor_name_full (type),
1487 parms, binfo,
1488 LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION);
1489 if (as_cons != NULL_TREE && as_cons != error_mark_node)
1490 /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
1491 cp_error ("ambiguity between conversion to `%T' and constructor",
1492 type);
1493 else
1494 expand_assignment (exp, rval, 0, 0);
1495 return;
1496 }
1497 }
1498 }
1499 }
1500
1501 /* Handle default copy constructors here, does not matter if there is
1502 a constructor or not. */
1503 if (type == init_type && IS_AGGR_TYPE (type)
1504 && init && TREE_CODE (init) != TREE_LIST)
1505 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1506 /* Not sure why this is here... */
1507 else if (TYPE_HAS_CONSTRUCTOR (type))
1508 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1509 else if (TREE_CODE (type) == ARRAY_TYPE)
1510 {
1511 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1512 expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
1513 else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
1514 sorry ("arrays of objects with virtual functions but no constructors");
1515 }
1516 else
1517 expand_recursive_init (binfo, true_exp, exp, init,
1518 CLASSTYPE_BASE_INIT_LIST (type), alias_this);
1519}
1520
1521/* A pointer which holds the initializer. First call to
1522 expand_aggr_init gets this value pointed to, and sets it to init_null. */
1523static tree *init_ptr, init_null;
1524
1525/* Subroutine of expand_recursive_init:
1526
1527 ADDR is the address of the expression being initialized.
1528 INIT_LIST is the cons-list of initializations to be performed.
1529 ALIAS_THIS is its same, lovable self. */
1530static void
1531expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this)
1532 tree binfo, true_exp, addr;
1533 tree init_list;
1534 int alias_this;
1535{
1536 while (init_list)
1537 {
1538 if (TREE_PURPOSE (init_list))
1539 {
1540 if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
1541 {
1542 tree member = TREE_PURPOSE (init_list);
1543 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1544 tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
1545 if (IS_AGGR_TYPE (TREE_TYPE (member)))
6060a796 1546 expand_aggr_init (member_base, DECL_INITIAL (member), 0, 0);
8d08fdba
MS
1547 else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
1548 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
1549 {
1550 member_base = save_expr (default_conversion (member_base));
1551 expand_vec_init (member, member_base,
1552 array_type_nelts (TREE_TYPE (member)),
1553 DECL_INITIAL (member), 0);
1554 }
1555 else
1556 expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
1557 }
1558 else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
1559 {
1560 expand_recursive_init_1 (binfo, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
1561 expand_recursive_init_1 (binfo, true_exp, addr, TREE_VALUE (init_list), alias_this);
1562 }
1563 else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
1564 {
1565 /* Only initialize the virtual function tables if we
1566 are initializing the ultimate users of those vtables. */
1567 if (TREE_VALUE (init_list))
1568 {
7177d104 1569 /* We have to ensure that the first argment to
8926095f 1570 expand_virtual_init is in binfo's hierarchy. */
7177d104
MS
1571 /* Is it the case that this is exactly the right binfo? */
1572 /* If it is ok, then fixup expand_virtual_init, to make
1573 it much simpler. */
1574 expand_virtual_init (get_binfo (TREE_VALUE (init_list), binfo, 0),
8926095f 1575 addr);
8d08fdba
MS
1576 if (TREE_VALUE (init_list) == binfo
1577 && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7177d104 1578 expand_indirect_vtbls_init (binfo, true_exp, addr, 1);
8d08fdba
MS
1579 }
1580 }
1581 else
1582 my_friendly_abort (49);
1583 }
1584 else if (TREE_VALUE (init_list)
1585 && TREE_CODE (TREE_VALUE (init_list)) == TREE_VEC)
1586 {
1587 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1588 expand_aggr_init_1 (binfo, true_exp, subexp, *init_ptr,
1589 alias_this && BINFO_OFFSET_ZEROP (TREE_VALUE (init_list)),
1590 LOOKUP_COMPLAIN);
1591
1592 /* INIT_PTR is used up. */
1593 init_ptr = &init_null;
1594 }
1595 else
1596 my_friendly_abort (50);
1597 init_list = TREE_CHAIN (init_list);
1598 }
1599}
1600
1601/* Initialize EXP with INIT. Type EXP does not have a constructor,
1602 but it has a baseclass with a constructor or a virtual function
1603 table which needs initializing.
1604
1605 INIT_LIST is a cons-list describing what parts of EXP actually
1606 need to be initialized. INIT is given to the *unique*, first
1607 constructor within INIT_LIST. If there are multiple first
1608 constructors, such as with multiple inheritance, INIT must
1609 be zero or an ambiguity error is reported.
1610
1611 ALIAS_THIS is passed from `expand_aggr_init'. See comments
1612 there. */
1613
1614static void
1615expand_recursive_init (binfo, true_exp, exp, init, init_list, alias_this)
1616 tree binfo, true_exp, exp, init;
1617 tree init_list;
1618 int alias_this;
1619{
1620 tree *old_init_ptr = init_ptr;
1621 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1622 init_ptr = &init;
1623
1624 if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1625 {
1626 expand_aggr_vbase_init (binfo, exp, addr, init_list);
7177d104 1627 expand_indirect_vtbls_init (binfo, true_exp, addr, 1);
8d08fdba
MS
1628 }
1629 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this);
1630
1631 if (*init_ptr)
1632 {
1633 tree type = TREE_TYPE (exp);
1634
1635 if (TREE_CODE (type) == REFERENCE_TYPE)
1636 type = TREE_TYPE (type);
1637 if (IS_AGGR_TYPE (type))
1638 cp_error ("unexpected argument to constructor `%T'", type);
1639 else
1640 error ("unexpected argument to constructor");
1641 }
1642 init_ptr = old_init_ptr;
1643}
1644
1645/* Report an error if NAME is not the name of a user-defined,
1646 aggregate type. If OR_ELSE is nonzero, give an error message. */
1647int
1648is_aggr_typedef (name, or_else)
1649 tree name;
1650 int or_else;
1651{
1652 tree type;
1653
1654 if (name == error_mark_node)
1655 return 0;
1656
1657 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1658 type = IDENTIFIER_TYPE_VALUE (name);
8d08fdba
MS
1659 else
1660 {
1661 if (or_else)
a28e3c7f 1662 cp_error ("`%T' is not an aggregate typedef", name);
8d08fdba
MS
1663 return 0;
1664 }
1665
1666 if (! IS_AGGR_TYPE (type)
1667 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1668 {
1669 if (or_else)
a28e3c7f 1670 cp_error ("`%T' is not an aggregate type", type);
8d08fdba
MS
1671 return 0;
1672 }
1673 return 1;
1674}
1675
1676/* Like is_aggr_typedef, but returns typedef if successful. */
1677tree
1678get_aggr_from_typedef (name, or_else)
1679 tree name;
1680 int or_else;
1681{
1682 tree type;
1683
1684 if (name == error_mark_node)
1685 return NULL_TREE;
1686
1687 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1688 type = IDENTIFIER_TYPE_VALUE (name);
8d08fdba
MS
1689 else
1690 {
1691 if (or_else)
1692 cp_error ("`%T' fails to be an aggregate typedef", name);
1693 return NULL_TREE;
1694 }
1695
1696 if (! IS_AGGR_TYPE (type)
1697 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1698 {
1699 if (or_else)
1700 cp_error ("type `%T' is of non-aggregate type", type);
1701 return NULL_TREE;
1702 }
1703 return type;
1704}
1705
1706tree
1707get_type_value (name)
1708 tree name;
1709{
8d08fdba
MS
1710 if (name == error_mark_node)
1711 return NULL_TREE;
1712
1713 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1714 return IDENTIFIER_TYPE_VALUE (name);
8d08fdba
MS
1715 else
1716 return NULL_TREE;
1717}
1718
1719\f
51c184be 1720/* This code could just as well go in `class.c', but is placed here for
8d08fdba
MS
1721 modularity. */
1722
1723/* For an expression of the form CNAME :: NAME (PARMLIST), build
1724 the appropriate function call. */
1725tree
1726build_member_call (cname, name, parmlist)
1727 tree cname, name, parmlist;
1728{
1729 tree type, t;
1730 tree method_name = name;
1731 int dtor = 0;
1732 int dont_use_this = 0;
1733 tree basetype_path, decl;
1734
1735 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1736 {
1737 method_name = TREE_OPERAND (method_name, 0);
1738 dtor = 1;
1739 }
1740
1741 if (TREE_CODE (cname) == SCOPE_REF)
1742 cname = resolve_scope_to_name (NULL_TREE, cname);
1743
a9aedbc2
MS
1744 /* This shouldn't be here, and build_member_call shouldn't appear in
1745 parse.y! (mrs) */
1746 if (cname && get_aggr_from_typedef (cname, 0) == 0
1747 && TREE_CODE (cname) == IDENTIFIER_NODE)
1748 {
1749 tree ns = lookup_name (cname, 0);
1750 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1751 {
1752 return build_x_function_call (build_offset_ref (cname, name), parmlist, current_class_decl);
1753 }
1754 }
1755
8d08fdba
MS
1756 if (cname == NULL_TREE || ! (type = get_aggr_from_typedef (cname, 1)))
1757 return error_mark_node;
1758
1759 /* An operator we did not like. */
1760 if (name == NULL_TREE)
1761 return error_mark_node;
1762
1763 if (dtor)
1764 {
1765#if 0
1766 /* Everything can explicitly call a destructor; see 12.4 */
1767 if (! TYPE_HAS_DESTRUCTOR (type))
1768 cp_error ("type `%#T' does not have a destructor", type);
1769 else
1770#endif
1771 cp_error ("cannot call destructor `%T::~%T' without object", type,
1772 method_name);
1773 return error_mark_node;
1774 }
1775
1776 /* No object? Then just fake one up, and let build_method_call
1777 figure out what to do. */
1778 if (current_class_type == 0
1779 || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
1780 dont_use_this = 1;
1781
1782 if (dont_use_this)
1783 {
39211cd5 1784 basetype_path = TYPE_BINFO (type);
8d08fdba
MS
1785 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1786 }
1787 else if (current_class_decl == 0)
1788 {
1789 dont_use_this = 1;
1790 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1791 }
1792 else
1793 {
1794 tree olddecl = current_class_decl;
1795 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1796 if (oldtype != type)
1797 {
1798 tree newtype = build_type_variant (type, TYPE_READONLY (oldtype),
1799 TYPE_VOLATILE (oldtype));
6060a796 1800 decl = convert_force (build_pointer_type (newtype), olddecl, 0);
8d08fdba
MS
1801 }
1802 else
1803 decl = olddecl;
1804 }
1805
1806 decl = build_indirect_ref (decl, NULL_PTR);
1807
71851aaa
MS
1808 if (method_name == constructor_name (type)
1809 || method_name == constructor_name_full (type))
1810 return build_functional_cast (type, parmlist);
39211cd5 1811 if (t = lookup_fnfields (basetype_path, method_name, 0))
8d08fdba
MS
1812 return build_method_call (decl, method_name, parmlist, basetype_path,
1813 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1814 if (TREE_CODE (name) == IDENTIFIER_NODE
8926095f 1815 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
8d08fdba
MS
1816 {
1817 if (t == error_mark_node)
1818 return error_mark_node;
1819 if (TREE_CODE (t) == FIELD_DECL)
1820 {
1821 if (dont_use_this)
1822 {
1823 cp_error ("invalid use of non-static field `%D'", t);
1824 return error_mark_node;
1825 }
1826 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1827 }
1828 else if (TREE_CODE (t) == VAR_DECL)
1829 decl = t;
1830 else
1831 {
1832 cp_error ("invalid use of member `%D'", t);
1833 return error_mark_node;
1834 }
1835 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
1836 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl)))
1837 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE);
1838 return build_function_call (decl, parmlist);
1839 }
1840 else
1841 {
1842 cp_error ("no method `%T::%D'", type, name);
1843 return error_mark_node;
1844 }
1845}
1846
1847/* Build a reference to a member of an aggregate. This is not a
1848 C++ `&', but really something which can have its address taken,
1849 and then act as a pointer to member, for example CNAME :: FIELD
1850 can have its address taken by saying & CNAME :: FIELD.
1851
1852 @@ Prints out lousy diagnostics for operator <typename>
1853 @@ fields.
1854
51c184be 1855 @@ This function should be rewritten and placed in search.c. */
8d08fdba
MS
1856tree
1857build_offset_ref (cname, name)
1858 tree cname, name;
1859{
1860 tree decl, type, fnfields, fields, t = error_mark_node;
1861 tree basetypes = NULL_TREE;
1862 int dtor = 0;
1863
1864 if (TREE_CODE (cname) == SCOPE_REF)
1865 cname = resolve_scope_to_name (NULL_TREE, cname);
1866
a9aedbc2
MS
1867 /* Handle namespace names fully here. */
1868 if (TREE_CODE (cname) == IDENTIFIER_NODE
1869 && get_aggr_from_typedef (cname, 0) == 0)
1870 {
1871 tree ns = lookup_name (cname, 0);
1872 tree val;
1873 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1874 {
1875 val = lookup_namespace_name (ns, name);
1876 if (val)
1877 return val;
1878 cp_error ("namespace `%D' has no member named `%D'", ns, name);
1879 return error_mark_node;
1880 }
1881 }
1882
8d08fdba
MS
1883 if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1))
1884 return error_mark_node;
1885
1886 type = IDENTIFIER_TYPE_VALUE (cname);
1887
1888 if (TREE_CODE (name) == BIT_NOT_EXPR)
1889 {
1890 dtor = 1;
1891 name = TREE_OPERAND (name, 0);
1892 }
1893
1894 if (TYPE_SIZE (type) == 0)
1895 {
1896 t = IDENTIFIER_CLASS_VALUE (name);
1897 if (t == 0)
1898 {
1899 cp_error ("incomplete type `%T' does not have member `%D'", type,
1900 name);
1901 return error_mark_node;
1902 }
51c184be
MS
1903 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL
1904 || TREE_CODE (t) == CONST_DECL)
8d08fdba
MS
1905 {
1906 TREE_USED (t) = 1;
1907 return t;
1908 }
1909 if (TREE_CODE (t) == FIELD_DECL)
1910 sorry ("use of member in incomplete aggregate type");
1911 else if (TREE_CODE (t) == FUNCTION_DECL)
1912 sorry ("use of member function in incomplete aggregate type");
1913 else
1914 my_friendly_abort (52);
1915 return error_mark_node;
1916 }
1917
39211cd5 1918#if 0
8d08fdba
MS
1919 if (TREE_CODE (name) == TYPE_EXPR)
1920 /* Pass a TYPE_DECL to build_component_type_expr. */
1921 return build_component_type_expr (TYPE_NAME (TREE_TYPE (cname)),
1922 name, NULL_TREE, 1);
39211cd5 1923#endif
8d08fdba 1924
8d08fdba
MS
1925 if (current_class_type == 0
1926 || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
1927 {
1928 basetypes = TYPE_BINFO (type);
1929 decl = build1 (NOP_EXPR,
1930 IDENTIFIER_TYPE_VALUE (cname),
1931 error_mark_node);
1932 }
1933 else if (current_class_decl == 0)
1934 decl = build1 (NOP_EXPR, IDENTIFIER_TYPE_VALUE (cname),
1935 error_mark_node);
1936 else
1937 decl = C_C_D;
1938
00595019
MS
1939 fnfields = lookup_fnfields (basetypes, name, 1);
1940 fields = lookup_field (basetypes, name, 0, 0);
1941
1942 if (fields == error_mark_node || fnfields == error_mark_node)
1943 return error_mark_node;
1944
8d08fdba
MS
1945 /* A lot of this logic is now handled in lookup_field and
1946 lookup_fnfield. */
1947 if (fnfields)
1948 {
1949 basetypes = TREE_PURPOSE (fnfields);
1950
1951 /* Go from the TREE_BASELINK to the member function info. */
1952 t = TREE_VALUE (fnfields);
1953
1954 if (fields)
1955 {
1956 if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
1957 {
1958 error ("ambiguous member reference: member `%s' defined as both field and function",
1959 IDENTIFIER_POINTER (name));
1960 return error_mark_node;
1961 }
1962 if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t)))
1963 ;
1964 else if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields)))
1965 t = fields;
1966 else
1967 {
1968 error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
1969 return error_mark_node;
1970 }
1971 }
1972
1973 if (t == TREE_VALUE (fnfields))
1974 {
1975 extern int flag_save_memoized_contexts;
1976
8d08fdba
MS
1977 if (DECL_CHAIN (t) == NULL_TREE || dtor)
1978 {
1979 enum access_type access;
1980
1981 /* unique functions are handled easily. */
1982 unique:
1983 access = compute_access (basetypes, t);
1984 if (access == access_protected)
1985 {
1986 cp_error_at ("member function `%#D' is protected", t);
1987 error ("in this context");
1988 return error_mark_node;
1989 }
1990 if (access == access_private)
1991 {
1992 cp_error_at ("member function `%#D' is private", t);
1993 error ("in this context");
1994 return error_mark_node;
1995 }
1996 assemble_external (t);
1997 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
1998 }
1999
2000 /* overloaded functions may need more work. */
2001 if (cname == name)
2002 {
2003 if (TYPE_HAS_DESTRUCTOR (type)
2004 && DECL_CHAIN (DECL_CHAIN (t)) == NULL_TREE)
2005 {
2006 t = DECL_CHAIN (t);
2007 goto unique;
2008 }
2009 }
2010 /* FNFIELDS is most likely allocated on the search_obstack,
2011 which will go away after this class scope. If we need
2012 to save this value for later (either for memoization
2013 or for use as an initializer for a static variable), then
2014 do so here.
2015
2016 ??? The smart thing to do for the case of saving initializers
2017 is to resolve them before we're done with this scope. */
2018 if (!TREE_PERMANENT (fnfields)
2019 && ((flag_save_memoized_contexts && global_bindings_p ())
2020 || ! allocation_temporary_p ()))
2021 fnfields = copy_list (fnfields);
28cbf42c
MS
2022
2023 for (t = TREE_VALUE (fnfields); t; t = DECL_CHAIN (t))
2024 assemble_external (t);
2025
8d08fdba
MS
2026 t = build_tree_list (error_mark_node, fnfields);
2027 TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
2028 return t;
2029 }
2030 }
2031
2032 /* Now that we know we are looking for a field, see if we
2033 have access to that field. Lookup_field will give us the
2034 error message. */
2035
2036 t = lookup_field (basetypes, name, 1, 0);
2037
2038 if (t == error_mark_node)
2039 return error_mark_node;
2040
2041 if (t == NULL_TREE)
2042 {
a4443a08 2043 cp_error ("`%D' is not a member of type `%T'", name, type);
8d08fdba
MS
2044 return error_mark_node;
2045 }
2046
2047 if (TREE_CODE (t) == TYPE_DECL)
2048 {
51c184be
MS
2049 TREE_USED (t) = 1;
2050 return t;
8d08fdba
MS
2051 }
2052 /* static class members and class-specific enum
2053 values can be returned without further ado. */
2054 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
2055 {
2056 assemble_external (t);
2057 TREE_USED (t) = 1;
2058 return t;
2059 }
2060
b7484fbe
MS
2061 if (TREE_CODE (t) == FIELD_DECL && DECL_BIT_FIELD (t))
2062 {
2063 cp_error ("illegal pointer to bit field `%D'", t);
2064 return error_mark_node;
2065 }
2066
8d08fdba
MS
2067 /* static class functions too. */
2068 if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2069 my_friendly_abort (53);
2070
2071 /* In member functions, the form `cname::name' is no longer
2072 equivalent to `this->cname::name'. */
2073 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
2074}
2075
2076/* Given an object EXP and a member function reference MEMBER,
2077 return the address of the actual member function. */
2078tree
2079get_member_function (exp_addr_ptr, exp, member)
2080 tree *exp_addr_ptr;
2081 tree exp, member;
2082{
2083 tree ctype = TREE_TYPE (exp);
2084 tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
2085
2086 if (TYPE_VIRTUAL_P (ctype)
2087 || (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)))
2088 {
2089 tree e0, e1, e3;
2090 tree exp_addr;
2091
2092 /* Save away the unadulterated `this' pointer. */
2093 exp_addr = save_expr (*exp_addr_ptr);
2094
2095 /* Cast function to signed integer. */
2096 e0 = build1 (NOP_EXPR, integer_type_node, function);
2097
8d08fdba
MS
2098 /* There is a hack here that takes advantage of
2099 twos complement arithmetic, and the fact that
2100 there are more than one UNITS to the WORD.
2101 If the high bit is set for the `function',
2102 then we pretend it is a virtual function,
2103 and the array indexing will knock this bit
2104 out the top, leaving a valid index. */
2105 if (UNITS_PER_WORD <= 1)
2106 my_friendly_abort (54);
2107
b7484fbe 2108 e1 = build (GT_EXPR, boolean_type_node, e0, integer_zero_node);
8d08fdba
MS
2109 e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
2110 build_tree_list (NULL_TREE, e1)));
2111 e1 = save_expr (e1);
8d08fdba
MS
2112
2113 if (TREE_SIDE_EFFECTS (*exp_addr_ptr))
2114 {
2115 exp = build_indirect_ref (exp_addr, NULL_PTR);
2116 *exp_addr_ptr = exp_addr;
2117 }
2118
2119 /* This is really hairy: if the function pointer is a pointer
2120 to a non-virtual member function, then we can't go mucking
2121 with the `this' pointer (any more than we already have to
2122 this point). If it is a pointer to a virtual member function,
2123 then we have to adjust the `this' pointer according to
2124 what the virtual function table tells us. */
2125
2126 e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
2127 my_friendly_assert (e3 != error_mark_node, 213);
2128
2129 /* Change this pointer type from `void *' to the
2130 type it is really supposed to be. */
2131 TREE_TYPE (e3) = TREE_TYPE (function);
2132
2133 /* If non-virtual, use what we had originally. Otherwise,
2134 use the value we get from the virtual function table. */
2135 *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
2136
2137 function = build_conditional_expr (e1, function, e3);
2138 }
2139 return build_indirect_ref (function, NULL_PTR);
2140}
2141
2142/* If a OFFSET_REF made it through to here, then it did
2143 not have its address taken. */
2144
2145tree
2146resolve_offset_ref (exp)
2147 tree exp;
2148{
2149 tree type = TREE_TYPE (exp);
2150 tree base = NULL_TREE;
2151 tree member;
2152 tree basetype, addr;
2153
2154 if (TREE_CODE (exp) == TREE_LIST)
2155 return build_unary_op (ADDR_EXPR, exp, 0);
2156
2157 if (TREE_CODE (exp) != OFFSET_REF)
2158 {
2159 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
2160 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
2161 {
2162 error ("object missing in use of pointer-to-member construct");
2163 return error_mark_node;
2164 }
2165 member = exp;
2166 type = TREE_TYPE (type);
2167 base = C_C_D;
2168 }
2169 else
2170 {
2171 member = TREE_OPERAND (exp, 1);
2172 base = TREE_OPERAND (exp, 0);
2173 }
2174
2175 if ((TREE_CODE (member) == VAR_DECL
2176 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2177 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
2178 {
2179 /* These were static members. */
2180 if (mark_addressable (member) == 0)
2181 return error_mark_node;
2182 return member;
2183 }
2184
2185 /* Syntax error can cause a member which should
2186 have been seen as static to be grok'd as non-static. */
2187 if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
2188 {
2189 if (TREE_ADDRESSABLE (member) == 0)
2190 {
2191 cp_error_at ("member `%D' is non-static in static member function context", member);
2192 error ("at this point in file");
2193 TREE_ADDRESSABLE (member) = 1;
2194 }
2195 return error_mark_node;
2196 }
2197
2198 /* The first case is really just a reference to a member of `this'. */
2199 if (TREE_CODE (member) == FIELD_DECL
2200 && (base == C_C_D
2201 || (TREE_CODE (base) == NOP_EXPR
2202 && TREE_OPERAND (base, 0) == error_mark_node)))
2203 {
2204 tree basetype_path;
2205 enum access_type access;
2206
2207 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
39211cd5 2208 basetype = TYPE_OFFSET_BASETYPE (type);
8d08fdba 2209 else
39211cd5
MS
2210 basetype = DECL_CONTEXT (member);
2211
2212 base = current_class_decl;
8d08fdba
MS
2213
2214 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
2215 {
2216 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
2217 return error_mark_node;
2218 }
2219 addr = convert_pointer_to (basetype, base);
2220 access = compute_access (basetype_path, member);
2221 if (access == access_public)
2222 return build (COMPONENT_REF, TREE_TYPE (member),
2223 build_indirect_ref (addr, NULL_PTR), member);
2224 if (access == access_protected)
2225 {
2226 cp_error_at ("member `%D' is protected", member);
2227 error ("in this context");
2228 return error_mark_node;
2229 }
2230 if (access == access_private)
2231 {
2232 cp_error_at ("member `%D' is private", member);
2233 error ("in this context");
2234 return error_mark_node;
2235 }
2236 my_friendly_abort (55);
2237 }
2238
2239 /* If this is a reference to a member function, then return
2240 the address of the member function (which may involve going
2241 through the object's vtable), otherwise, return an expression
2242 for the dereferenced pointer-to-member construct. */
2243 addr = build_unary_op (ADDR_EXPR, base, 0);
2244
2245 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
2246 {
2247 basetype = DECL_CLASS_CONTEXT (member);
2248 addr = convert_pointer_to (basetype, addr);
2249 return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, NULL_PTR), member), 0);
2250 }
2251 else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
2252 {
2253 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
2254 addr = convert_pointer_to (basetype, addr);
fef71f9d
JM
2255 member = convert (ptrdiff_type_node,
2256 build_unary_op (ADDR_EXPR, member, 0));
8d08fdba 2257 return build1 (INDIRECT_REF, type,
fef71f9d
JM
2258 build (PLUS_EXPR, build_pointer_type (type),
2259 addr, member));
8d08fdba
MS
2260 }
2261 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2262 {
b7484fbe 2263 return get_member_function_from_ptrfunc (&addr, member);
8d08fdba
MS
2264 }
2265 my_friendly_abort (56);
2266 /* NOTREACHED */
2267 return NULL_TREE;
2268}
2269
2270/* Return either DECL or its known constant value (if it has one). */
2271
2272tree
2273decl_constant_value (decl)
2274 tree decl;
2275{
2276 if (! TREE_THIS_VOLATILE (decl)
2277#if 0
2278 /* These may be necessary for C, but they break C++. */
2279 ! TREE_PUBLIC (decl)
2280 /* Don't change a variable array bound or initial value to a constant
2281 in a place where a variable is invalid. */
2282 && ! pedantic
2283#endif /* 0 */
2284 && DECL_INITIAL (decl) != 0
2285 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
2286 /* This is invalid if initial value is not constant.
2287 If it has either a function call, a memory reference,
2288 or a variable, then re-evaluating it could give different results. */
2289 && TREE_CONSTANT (DECL_INITIAL (decl))
2290 /* Check for cases where this is sub-optimal, even though valid. */
2291 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
2292#if 0
2293 /* We must allow this to work outside of functions so that
2294 static constants can be used for array sizes. */
2295 && current_function_decl != 0
2296 && DECL_MODE (decl) != BLKmode
2297#endif
2298 )
2299 return DECL_INITIAL (decl);
2300 return decl;
2301}
2302\f
2303/* Friend handling routines. */
2304/* Friend data structures:
2305
2306 Lists of friend functions come from TYPE_DECL nodes. Since all
2307 aggregate types are automatically typedef'd, these nodes are guaranteed
2308 to exist.
2309
2310 The TREE_PURPOSE of a friend list is the name of the friend,
2311 and its TREE_VALUE is another list.
2312
2313 For each element of that list, either the TREE_VALUE or the TREE_PURPOSE
2314 will be filled in, but not both. The TREE_VALUE of that list is an
2315 individual function which is a friend. The TREE_PURPOSE of that list
2316 indicates a type in which all functions by that name are friends.
2317
2318 Lists of friend classes come from _TYPE nodes. Love that consistency
2319 thang. */
2320
2321int
2322is_friend_type (type1, type2)
2323 tree type1, type2;
2324{
2325 return is_friend (type1, type2);
2326}
2327
2328int
2329is_friend (type, supplicant)
2330 tree type, supplicant;
2331{
2332 int declp;
2333 register tree list;
2334
2335 if (supplicant == NULL_TREE || type == NULL_TREE)
2336 return 0;
2337
2338 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
2339
2340 if (declp)
2341 /* It's a function decl. */
2342 {
2343 tree list = DECL_FRIENDLIST (TYPE_NAME (type));
2344 tree name = DECL_NAME (supplicant);
b7484fbe
MS
2345 tree ctype;
2346
2347 if (DECL_FUNCTION_MEMBER_P (supplicant))
2348 ctype = DECL_CLASS_CONTEXT (supplicant);
2349 else
2350 ctype = NULL_TREE;
2351
8d08fdba
MS
2352 for (; list ; list = TREE_CHAIN (list))
2353 {
2354 if (name == TREE_PURPOSE (list))
2355 {
2356 tree friends = TREE_VALUE (list);
2357 name = DECL_ASSEMBLER_NAME (supplicant);
2358 for (; friends ; friends = TREE_CHAIN (friends))
2359 {
2360 if (ctype == TREE_PURPOSE (friends))
2361 return 1;
2362 if (name == DECL_ASSEMBLER_NAME (TREE_VALUE (friends)))
2363 return 1;
2364 }
2365 break;
2366 }
2367 }
2368 }
2369 else
2370 /* It's a type. */
2371 {
2372 if (type == supplicant)
2373 return 1;
2374
2375 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_NAME (type)));
2376 for (; list ; list = TREE_CHAIN (list))
2377 if (supplicant == TREE_VALUE (list))
2378 return 1;
2379 }
2380
2381 {
b7484fbe
MS
2382 tree context;
2383
2384 if (! declp)
2385 context = DECL_CONTEXT (TYPE_NAME (supplicant));
2386 else if (DECL_FUNCTION_MEMBER_P (supplicant))
2387 context = DECL_CLASS_CONTEXT (supplicant);
2388 else
2389 context = NULL_TREE;
8d08fdba
MS
2390
2391 if (context)
2392 return is_friend (type, context);
2393 }
2394
2395 return 0;
2396}
2397
2398/* Add a new friend to the friends of the aggregate type TYPE.
2399 DECL is the FUNCTION_DECL of the friend being added. */
2400static void
2401add_friend (type, decl)
2402 tree type, decl;
2403{
2404 tree typedecl = TYPE_NAME (type);
2405 tree list = DECL_FRIENDLIST (typedecl);
2406 tree name = DECL_NAME (decl);
2407
2408 while (list)
2409 {
2410 if (name == TREE_PURPOSE (list))
2411 {
2412 tree friends = TREE_VALUE (list);
2413 for (; friends ; friends = TREE_CHAIN (friends))
2414 {
2415 if (decl == TREE_VALUE (friends))
2416 {
e1cd6e56 2417 cp_warning ("`%D' is already a friend of class `%T'",
8926095f 2418 decl, type);
e1cd6e56 2419 cp_warning_at ("previous friend declaration of `%D'",
8926095f 2420 TREE_VALUE (friends));
8d08fdba
MS
2421 return;
2422 }
2423 }
2424 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
2425 TREE_VALUE (list));
2426 return;
2427 }
2428 list = TREE_CHAIN (list);
2429 }
2430 DECL_FRIENDLIST (typedecl)
2431 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
2432 DECL_FRIENDLIST (typedecl));
2433 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
2434 {
2435 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
2436 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2437 if (parmtypes && TREE_CHAIN (parmtypes))
2438 {
2439 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
2440 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2441 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
2442 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2443 }
2444 }
2445}
2446
2447/* Declare that every member function NAME in FRIEND_TYPE
2448 (which may be NULL_TREE) is a friend of type TYPE. */
2449static void
2450add_friends (type, name, friend_type)
2451 tree type, name, friend_type;
2452{
2453 tree typedecl = TYPE_NAME (type);
2454 tree list = DECL_FRIENDLIST (typedecl);
2455
2456 while (list)
2457 {
2458 if (name == TREE_PURPOSE (list))
2459 {
2460 tree friends = TREE_VALUE (list);
2461 while (friends && TREE_PURPOSE (friends) != friend_type)
2462 friends = TREE_CHAIN (friends);
2463 if (friends)
2464 if (friend_type)
2465 warning ("method `%s::%s' is already a friend of class",
2466 TYPE_NAME_STRING (friend_type),
2467 IDENTIFIER_POINTER (name));
2468 else
2469 warning ("function `%s' is already a friend of class `%s'",
2470 IDENTIFIER_POINTER (name),
2471 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2472 else
2473 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
2474 TREE_VALUE (list));
2475 return;
2476 }
2477 list = TREE_CHAIN (list);
2478 }
2479 DECL_FRIENDLIST (typedecl) =
2480 tree_cons (name,
2481 build_tree_list (friend_type, NULL_TREE),
2482 DECL_FRIENDLIST (typedecl));
2483 if (! strncmp (IDENTIFIER_POINTER (name),
2484 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
2485 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
2486 {
2487 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2488 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
2489 }
2490}
2491
2492/* Set up a cross reference so that type TYPE will make member function
2493 CTYPE::DECL a friend when CTYPE is finally defined. For more than
2494 one, set up a cross reference so that functions with the name DECL
2495 and type CTYPE know that they are friends of TYPE. */
2496static void
2497xref_friend (type, decl, ctype)
2498 tree type, decl, ctype;
2499{
8d08fdba
MS
2500 tree friend_decl = TYPE_NAME (ctype);
2501#if 0
8926095f 2502 tree typedecl = TYPE_NAME (type);
8d08fdba
MS
2503 tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
2504
2505 DECL_UNDEFINED_FRIENDS (typedecl) = t;
2506#else
2507 tree t = 0;
2508#endif
2509 SET_DECL_WAITING_FRIENDS (friend_decl,
2510 tree_cons (type, t,
2511 DECL_WAITING_FRIENDS (friend_decl)));
2512 TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
2513}
2514
2515/* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
2516 been defined, we make all of its member functions friends of
2517 TYPE. If not, we make it a pending friend, which can later be added
2518 when its definition is seen. If a type is defined, then its TYPE_DECL's
2519 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
2520 classes that are not defined. If a type has not yet been defined,
2521 then the DECL_WAITING_FRIENDS contains a list of types
2522 waiting to make it their friend. Note that these two can both
2523 be in use at the same time! */
2524void
2525make_friend_class (type, friend_type)
2526 tree type, friend_type;
2527{
2528 tree classes;
2529
2530 if (IS_SIGNATURE (type))
2531 {
2532 error ("`friend' declaration in signature definition");
2533 return;
2534 }
2535 if (IS_SIGNATURE (friend_type))
2536 {
2537 error ("signature type `%s' declared `friend'",
2538 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (friend_type))));
2539 return;
2540 }
2541 if (type == friend_type)
2542 {
b7484fbe 2543 pedwarn ("class `%s' is implicitly friends with itself",
8d08fdba
MS
2544 TYPE_NAME_STRING (type));
2545 return;
2546 }
2547
2548 GNU_xref_hier (TYPE_NAME_STRING (type),
2549 TYPE_NAME_STRING (friend_type), 0, 0, 1);
2550
2551 classes = CLASSTYPE_FRIEND_CLASSES (type);
2552 while (classes && TREE_VALUE (classes) != friend_type)
2553 classes = TREE_CHAIN (classes);
2554 if (classes)
2555 warning ("class `%s' is already friends with class `%s'",
2556 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
2557 else
2558 {
2559 CLASSTYPE_FRIEND_CLASSES (type)
2560 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
2561 }
2562}
2563
2564/* Main friend processor. This is large, and for modularity purposes,
2565 has been removed from grokdeclarator. It returns `void_type_node'
2566 to indicate that something happened, though a FIELD_DECL is
2567 not returned.
2568
2569 CTYPE is the class this friend belongs to.
2570
2571 DECLARATOR is the name of the friend.
2572
2573 DECL is the FUNCTION_DECL that the friend is.
2574
2575 In case we are parsing a friend which is part of an inline
2576 definition, we will need to store PARM_DECL chain that comes
2577 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
2578
2579 FLAGS is just used for `grokclassfn'.
2580
2581 QUALS say what special qualifies should apply to the object
2582 pointed to by `this'. */
2583tree
2584do_friend (ctype, declarator, decl, parmdecls, flags, quals)
2585 tree ctype, declarator, decl, parmdecls;
2586 enum overload_flags flags;
2587 tree quals;
2588{
8d08fdba
MS
2589 /* Every decl that gets here is a friend of something. */
2590 DECL_FRIEND_P (decl) = 1;
2591
8d08fdba
MS
2592 if (ctype)
2593 {
2594 tree cname = TYPE_NAME (ctype);
2595 if (TREE_CODE (cname) == TYPE_DECL)
2596 cname = DECL_NAME (cname);
2597
2598 /* A method friend. */
2599 if (TREE_CODE (decl) == FUNCTION_DECL)
2600 {
2601 if (flags == NO_SPECIAL && ctype && declarator == cname)
2602 DECL_CONSTRUCTOR_P (decl) = 1;
2603
2604 /* This will set up DECL_ARGUMENTS for us. */
2605 grokclassfn (ctype, cname, decl, flags, quals);
2606 if (TYPE_SIZE (ctype) != 0)
2607 check_classfn (ctype, cname, decl);
2608
2609 if (TREE_TYPE (decl) != error_mark_node)
2610 {
2611 if (TYPE_SIZE (ctype))
2612 {
2613 /* We don't call pushdecl here yet, or ever on this
2614 actual FUNCTION_DECL. We must preserve its TREE_CHAIN
2615 until the end. */
2616 make_decl_rtl (decl, NULL_PTR, 1);
2617 add_friend (current_class_type, decl);
2618 }
2619 else
2620 {
2621 register char *classname
2622 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (ctype)));
2623
2624 error ("member declared as friend before type `%s' defined",
2625 classname);
2626 }
2627 }
2628 }
2629 else
2630 {
2631 /* Possibly a bunch of method friends. */
2632
2633 /* Get the class they belong to. */
2634 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
2635
2636 /* This class is defined, use its methods now. */
2637 if (TYPE_SIZE (ctype))
2638 {
2639 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
2640 if (fields)
2641 add_friends (current_class_type, declarator, ctype);
2642 else
2643 error ("method `%s' is not a member of class `%s'",
2644 IDENTIFIER_POINTER (declarator),
2645 IDENTIFIER_POINTER (cname));
2646 }
2647 else
2648 /* Note: DECLARATOR actually has more than one; in this
2649 case, we're making sure that fns with the name DECLARATOR
2650 and type CTYPE know they are friends of the current
2651 class type. */
2652 xref_friend (current_class_type, declarator, ctype);
2653 decl = void_type_node;
2654 }
2655 }
8d08fdba
MS
2656 else if (TREE_CODE (decl) == FUNCTION_DECL
2657 && ((IDENTIFIER_LENGTH (declarator) == 4
2658 && IDENTIFIER_POINTER (declarator)[0] == 'm'
2659 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2660 || (IDENTIFIER_LENGTH (declarator) > 10
2661 && IDENTIFIER_POINTER (declarator)[0] == '_'
2662 && IDENTIFIER_POINTER (declarator)[1] == '_'
2663 && strncmp (IDENTIFIER_POINTER (declarator)+2,
a3203465 2664 "builtin_", 8) == 0)))
8d08fdba
MS
2665 {
2666 /* raw "main", and builtin functions never gets overloaded,
2667 but they can become friends. */
8d08fdba
MS
2668 add_friend (current_class_type, decl);
2669 DECL_FRIEND_P (decl) = 1;
8d08fdba
MS
2670 decl = void_type_node;
2671 }
2672 /* A global friend.
2673 @@ or possibly a friend from a base class ?!? */
2674 else if (TREE_CODE (decl) == FUNCTION_DECL)
2675 {
2676 /* Friends must all go through the overload machinery,
2677 even though they may not technically be overloaded.
2678
2679 Note that because classes all wind up being top-level
2680 in their scope, their friend wind up in top-level scope as well. */
2681 DECL_ASSEMBLER_NAME (decl)
2682 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
2683 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
2684 DECL_ARGUMENTS (decl) = parmdecls;
2685 DECL_CLASS_CONTEXT (decl) = current_class_type;
2686
2687 /* We can call pushdecl here, because the TREE_CHAIN of this
2688 FUNCTION_DECL is not needed for other purposes. */
a4443a08 2689 decl = pushdecl (decl);
8d08fdba
MS
2690
2691 make_decl_rtl (decl, NULL_PTR, 1);
2692 add_friend (current_class_type, decl);
2693
8d08fdba 2694 DECL_FRIEND_P (decl) = 1;
7177d104 2695#if 0
8d08fdba 2696 TREE_OVERLOADED (declarator) = 1;
7177d104 2697#endif
8d08fdba
MS
2698 }
2699 else
2700 {
2701 /* @@ Should be able to ingest later definitions of this function
2702 before use. */
700f8a87 2703 tree decl = lookup_name_nonclass (declarator);
8d08fdba
MS
2704 if (decl == NULL_TREE)
2705 {
2706 warning ("implicitly declaring `%s' as struct",
2707 IDENTIFIER_POINTER (declarator));
2708 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
2709 decl = TYPE_NAME (decl);
2710 }
2711
2712 /* Allow abbreviated declarations of overloaded functions,
2713 but not if those functions are really class names. */
2714 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
2715 {
2716 warning ("`friend %s' archaic, use `friend class %s' instead",
2717 IDENTIFIER_POINTER (declarator),
2718 IDENTIFIER_POINTER (declarator));
2719 decl = TREE_TYPE (TREE_PURPOSE (decl));
2720 }
2721
2722 if (TREE_CODE (decl) == TREE_LIST)
2723 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
2724 else
2725 make_friend_class (current_class_type, TREE_TYPE (decl));
2726 decl = void_type_node;
2727 }
2728 return decl;
2729}
2730
2731/* TYPE has now been defined. It may, however, have a number of things
2732 waiting make make it their friend. We resolve these references
2733 here. */
2734void
2735embrace_waiting_friends (type)
2736 tree type;
2737{
2738 tree decl = TYPE_NAME (type);
2739 tree waiters;
2740
2741 if (TREE_CODE (decl) != TYPE_DECL)
2742 return;
2743
2744 for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
2745 waiters = TREE_CHAIN (waiters))
2746 {
2747 tree waiter = TREE_PURPOSE (waiters);
2748#if 0
2749 tree waiter_prev = TREE_VALUE (waiters);
2750#endif
2751 tree decl = TREE_TYPE (waiters);
2752 tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
2753 ? decl : DECL_NAME (decl)) : NULL_TREE;
2754 if (name)
2755 {
2756 /* @@ There may be work to be done since we have not verified
2757 @@ consistency between original and friend declarations
2758 @@ of the functions waiting to become friends. */
2759 tree field = lookup_fnfields (TYPE_BINFO (type), name, 0);
2760 if (field)
2761 if (decl == name)
2762 add_friends (waiter, name, type);
2763 else
2764 add_friend (waiter, decl);
2765 else
2766 error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
2767 DECL_SOURCE_LINE (TYPE_NAME (waiter)),
2768 "no method `%s' defined in class `%s' to be friend",
2769 IDENTIFIER_POINTER (DECL_NAME (TREE_TYPE (waiters))),
2770 TYPE_NAME_STRING (type));
2771 }
2772 else
2773 make_friend_class (type, waiter);
2774
2775#if 0
2776 if (TREE_CHAIN (waiter_prev))
2777 TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
2778 else
2779 DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
2780#endif
2781 }
2782}
2783\f
2784/* Common subroutines of build_new and build_vec_delete. */
2785
2786/* Common interface for calling "builtin" functions that are not
2787 really builtin. */
2788
2789tree
2790build_builtin_call (type, node, arglist)
2791 tree type;
2792 tree node;
2793 tree arglist;
2794{
2795 tree rval = build (CALL_EXPR, type, node, arglist, 0);
2796 TREE_SIDE_EFFECTS (rval) = 1;
2797 assemble_external (TREE_OPERAND (node, 0));
2798 TREE_USED (TREE_OPERAND (node, 0)) = 1;
2799 return rval;
2800}
2801\f
2802/* Generate a C++ "new" expression. DECL is either a TREE_LIST
2803 (which needs to go through some sort of groktypename) or it
2804 is the name of the class we are newing. INIT is an initialization value.
2805 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2806 If INIT is void_type_node, it means do *not* call a constructor
2807 for this instance.
2808
2809 For types with constructors, the data returned is initialized
2810 by the appropriate constructor.
2811
2812 Whether the type has a constructor or not, if it has a pointer
2813 to a virtual function table, then that pointer is set up
2814 here.
2815
2816 Unless I am mistaken, a call to new () will return initialized
2817 data regardless of whether the constructor itself is private or
8926095f 2818 not. NOPE; new fails if the constructor is private (jcm).
8d08fdba
MS
2819
2820 Note that build_new does nothing to assure that any special
2821 alignment requirements of the type are met. Rather, it leaves
2822 it up to malloc to do the right thing. Otherwise, folding to
2823 the right alignment cal cause problems if the user tries to later
2824 free the memory returned by `new'.
2825
2826 PLACEMENT is the `placement' list for user-defined operator new (). */
2827
d18c083e
MS
2828extern int flag_check_new;
2829
8d08fdba
MS
2830tree
2831build_new (placement, decl, init, use_global_new)
2832 tree placement;
2833 tree decl, init;
2834 int use_global_new;
2835{
2836 tree type, true_type, size, rval;
8926095f 2837 tree nelts;
b7484fbe 2838 tree alloc_expr, alloc_temp;
8926095f 2839 int has_array = 0;
a28e3c7f 2840 enum tree_code code = NEW_EXPR;
8d08fdba
MS
2841
2842 tree pending_sizes = NULL_TREE;
2843
2844 if (decl == error_mark_node)
2845 return error_mark_node;
2846
2847 if (TREE_CODE (decl) == TREE_LIST)
2848 {
2849 tree absdcl = TREE_VALUE (decl);
2850 tree last_absdcl = NULL_TREE;
2851 int old_immediate_size_expand;
2852
2853 if (current_function_decl
2854 && DECL_CONSTRUCTOR_P (current_function_decl))
2855 {
2856 old_immediate_size_expand = immediate_size_expand;
2857 immediate_size_expand = 0;
2858 }
2859
2860 nelts = integer_one_node;
2861
2862 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
8926095f 2863 my_friendly_abort (215);
8d08fdba
MS
2864 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
2865 {
2866 last_absdcl = absdcl;
2867 absdcl = TREE_OPERAND (absdcl, 0);
2868 }
2869
2870 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
2871 {
2872 /* probably meant to be a vec new */
2873 tree this_nelts;
2874
51c184be
MS
2875 while (TREE_OPERAND (absdcl, 0)
2876 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
2877 {
2878 last_absdcl = absdcl;
2879 absdcl = TREE_OPERAND (absdcl, 0);
2880 }
2881
8d08fdba
MS
2882 has_array = 1;
2883 this_nelts = TREE_OPERAND (absdcl, 1);
2884 if (this_nelts != error_mark_node)
2885 {
2886 if (this_nelts == NULL_TREE)
2887 error ("new of array type fails to specify size");
2888 else
2889 {
8926095f 2890 this_nelts = save_expr (convert (sizetype, this_nelts));
8d08fdba
MS
2891 absdcl = TREE_OPERAND (absdcl, 0);
2892 if (this_nelts == integer_zero_node)
2893 {
2894 warning ("zero size array reserves no space");
2895 nelts = integer_zero_node;
2896 }
2897 else
2898 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
2899 }
2900 }
2901 else
2902 nelts = integer_zero_node;
2903 }
2904
2905 if (last_absdcl)
2906 TREE_OPERAND (last_absdcl, 0) = absdcl;
2907 else
2908 TREE_VALUE (decl) = absdcl;
2909
2910 type = true_type = groktypename (decl);
8926095f 2911 if (! type || type == error_mark_node)
8d08fdba
MS
2912 {
2913 immediate_size_expand = old_immediate_size_expand;
2914 return error_mark_node;
2915 }
2916
8d08fdba
MS
2917 if (current_function_decl
2918 && DECL_CONSTRUCTOR_P (current_function_decl))
2919 {
2920 pending_sizes = get_pending_sizes ();
2921 immediate_size_expand = old_immediate_size_expand;
2922 }
2923 }
2924 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
2925 {
2926 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
2927 {
2928 /* An aggregate type. */
2929 type = IDENTIFIER_TYPE_VALUE (decl);
2930 decl = TYPE_NAME (type);
2931 }
2932 else
2933 {
2934 /* A builtin type. */
2935 decl = lookup_name (decl, 1);
2936 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2937 type = TREE_TYPE (decl);
2938 }
2939 true_type = type;
2940 }
2941 else if (TREE_CODE (decl) == TYPE_DECL)
2942 {
2943 type = TREE_TYPE (decl);
2944 true_type = type;
2945 }
2946 else
2947 {
2948 type = decl;
2949 true_type = type;
2950 decl = TYPE_NAME (type);
2951 }
2952
8926095f
MS
2953 /* ``A reference cannot be created by the new operator. A reference
2954 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2955 returned by new.'' ARM 5.3.3 */
2956 if (TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba 2957 {
8926095f
MS
2958 error ("new cannot be applied to a reference type");
2959 type = true_type = TREE_TYPE (type);
8d08fdba
MS
2960 }
2961
b7484fbe
MS
2962 if (TREE_CODE (type) == FUNCTION_TYPE)
2963 {
2964 error ("new cannot be applied to a function type");
2965 return error_mark_node;
2966 }
2967
8926095f
MS
2968 /* When the object being created is an array, the new-expression yields a
2969 pointer to the initial element (if any) of the array. For example,
2970 both new int and new int[10] return an int*. 5.3.4. */
2971 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
8d08fdba 2972 {
8926095f
MS
2973 nelts = array_type_nelts_top (type);
2974 has_array = 1;
2975 type = true_type = TREE_TYPE (type);
8d08fdba
MS
2976 }
2977
8926095f 2978 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
8ccc31eb
MS
2979 type = TYPE_MAIN_VARIANT (type);
2980
8d08fdba
MS
2981 /* If our base type is an array, then make sure we know how many elements
2982 it has. */
2983 while (TREE_CODE (true_type) == ARRAY_TYPE)
2984 {
2985 tree this_nelts = array_type_nelts_top (true_type);
8926095f 2986 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
8d08fdba
MS
2987 true_type = TREE_TYPE (true_type);
2988 }
2989 if (has_array)
2990 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2991 nelts, 1));
2992 else
2993 size = size_in_bytes (type);
2994
e1cd6e56
MS
2995 if (true_type == void_type_node)
2996 {
b7484fbe 2997 error ("invalid type `void' for new");
e1cd6e56
MS
2998 return error_mark_node;
2999 }
3000
8926095f
MS
3001 if (TYPE_SIZE (true_type) == 0)
3002 {
e1cd6e56 3003 incomplete_type_error (0, true_type);
8926095f
MS
3004 return error_mark_node;
3005 }
3006
3007 if (TYPE_LANG_SPECIFIC (true_type)
3008 && CLASSTYPE_ABSTRACT_VIRTUALS (true_type))
3009 {
3010 abstract_virtuals_error (NULL_TREE, true_type);
3011 return error_mark_node;
3012 }
3013
3014 if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type))
3015 {
3016 signature_error (NULL_TREE, true_type);
3017 return error_mark_node;
3018 }
8d08fdba
MS
3019
3020 /* Get a little extra space to store a couple of things before the new'ed
3021 array. */
a28e3c7f 3022 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
8d08fdba
MS
3023 {
3024 tree extra = BI_header_size;
3025
3026 size = size_binop (PLUS_EXPR, size, extra);
3027 }
3028
a28e3c7f
MS
3029 if (has_array)
3030 code = VEC_NEW_EXPR;
3031
8d08fdba 3032 /* Allocate the object. */
a28e3c7f
MS
3033 if (! use_global_new && TYPE_LANG_SPECIFIC (true_type)
3034 && (TYPE_GETS_NEW (true_type) & (1 << has_array)))
3035 rval = build_opfncall (code, LOOKUP_NORMAL,
8d08fdba
MS
3036 TYPE_POINTER_TO (true_type), size, placement);
3037 else if (placement)
3038 {
a28e3c7f 3039 rval = build_opfncall (code, LOOKUP_GLOBAL|LOOKUP_COMPLAIN,
8d08fdba 3040 ptr_type_node, size, placement);
71851aaa 3041 rval = convert (build_pointer_type (true_type), rval);
8d08fdba 3042 }
a28e3c7f 3043 else if (! has_array && flag_this_is_variable > 0
8ccc31eb 3044 && TYPE_NEEDS_CONSTRUCTING (true_type) && init != void_type_node)
8d08fdba
MS
3045 {
3046 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
3047 rval = NULL_TREE;
3048 else
3049 {
3050 error ("constructors take parameter lists");
3051 return error_mark_node;
3052 }
3053 }
3054 else
3055 {
3056 rval = build_builtin_call (build_pointer_type (true_type),
a28e3c7f
MS
3057 has_array ? BIVN : BIN,
3058 build_tree_list (NULL_TREE, size));
8d08fdba
MS
3059#if 0
3060 /* See comment above as to why this is disabled. */
3061 if (alignment)
3062 {
3063 rval = build (PLUS_EXPR, TYPE_POINTER_TO (true_type), rval,
3064 alignment);
3065 rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (true_type),
3066 rval, build1 (BIT_NOT_EXPR, integer_type_node,
3067 alignment));
3068 }
3069#endif
3070 TREE_CALLS_NEW (rval) = 1;
8d08fdba
MS
3071 }
3072
b7484fbe 3073 if (flag_check_new && rval)
d18c083e 3074 {
b7484fbe
MS
3075 /* For array new, we need to make sure that the call to new is
3076 not expanded as part of the RTL_EXPR for the initialization,
3077 so we can't just use save_expr here. */
3078
3079 alloc_temp = get_temp_name (TREE_TYPE (rval), 0);
3080 alloc_expr = build (MODIFY_EXPR, TREE_TYPE (rval), alloc_temp, rval);
3081 TREE_SIDE_EFFECTS (alloc_expr) = 1;
3082 rval = alloc_temp;
d18c083e 3083 }
b7484fbe
MS
3084 else
3085 alloc_expr = NULL_TREE;
d18c083e 3086
8d08fdba
MS
3087 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
3088 sure we have some extra bytes in that case for the BI_header_size
3089 cookies? And how does that interact with the code below? (mrs) */
3090 /* Finish up some magic for new'ed arrays */
a28e3c7f 3091 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type) && rval != NULL_TREE)
8d08fdba
MS
3092 {
3093 tree extra = BI_header_size;
3094 tree cookie, exp1;
3095 rval = convert (ptr_type_node, rval); /* convert to void * first */
3096 rval = convert (string_type_node, rval); /* lets not add void* and ints */
3097 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1));
3098 /* Store header info. */
3099 cookie = build_indirect_ref (build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
3100 rval, extra), NULL_PTR);
3101 exp1 = build (MODIFY_EXPR, void_type_node,
3102 build_component_ref (cookie, nc_nelts_field_id, 0, 0),
3103 nelts);
3104 TREE_SIDE_EFFECTS (exp1) = 1;
3105 rval = convert (build_pointer_type (true_type), rval);
3106 TREE_CALLS_NEW (rval) = 1;
3107 TREE_SIDE_EFFECTS (rval) = 1;
3108 rval = build_compound_expr (tree_cons (NULL_TREE, exp1,
3109 build_tree_list (NULL_TREE, rval)));
3110 }
3111
8d08fdba
MS
3112 if (rval == error_mark_node)
3113 return error_mark_node;
8d08fdba
MS
3114
3115 /* Don't call any constructors or do any initialization. */
3116 if (init == void_type_node)
3117 goto done;
3118
8926095f 3119 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
8d08fdba 3120 {
b19b4a78
MS
3121 if (! TYPE_NEEDS_CONSTRUCTING (type)
3122 && ! IS_AGGR_TYPE (type) && ! has_array)
8d08fdba
MS
3123 {
3124 /* New 2.0 interpretation: `new int (10)' means
3125 allocate an int, and initialize it with 10. */
8ccc31eb
MS
3126 tree deref;
3127
3128 rval = save_expr (rval);
3129 deref = build_indirect_ref (rval, NULL_PTR);
3130 TREE_READONLY (deref) = 0;
8d08fdba 3131
8ccc31eb
MS
3132 if (TREE_CHAIN (init) != NULL_TREE)
3133 pedwarn ("initializer list being treated as compound expression");
7215f9a0
MS
3134 else if (TREE_CODE (init) == CONSTRUCTOR)
3135 {
3136 pedwarn ("initializer list appears where operand should be used");
3137 init = TREE_OPERAND (init, 1);
3138 }
8ccc31eb
MS
3139 init = build_compound_expr (init);
3140
3141 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
3142 "new", NULL_TREE, 0);
8d08fdba 3143 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
8ccc31eb 3144 build_modify_expr (deref, NOP_EXPR, init),
8d08fdba 3145 rval);
e3417fcd 3146 TREE_NO_UNUSED_WARNING (rval) = 1;
8d08fdba
MS
3147 TREE_SIDE_EFFECTS (rval) = 1;
3148 TREE_CALLS_NEW (rval) = 1;
3149 }
8ccc31eb
MS
3150 else if (! has_array)
3151 {
3152 tree newrval;
3153 /* Constructors are never virtual. If it has an initialization, we
3154 need to complain if we aren't allowed to use the ctor that took
3155 that argument. */
3156 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
3157
3158 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
3159 {
3160 init = tree_cons (NULL_TREE, integer_one_node, init);
3161 flags |= LOOKUP_HAS_IN_CHARGE;
3162 }
3163
3164 newrval = rval;
3165
3166 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
3167 newrval = build_indirect_ref (newrval, NULL_PTR);
3168
3169 newrval = build_method_call (newrval, constructor_name_full (true_type),
3170 init, NULL_TREE, flags);
3171
3172 if (newrval)
3173 {
3174 rval = newrval;
3175 TREE_HAS_CONSTRUCTOR (rval) = 1;
3176 }
3177 else
3178 rval = error_mark_node;
3179 }
8d08fdba
MS
3180 else if (current_function_decl == NULL_TREE)
3181 {
3182 extern tree static_aggregates;
3183
3184 /* In case of static initialization, SAVE_EXPR is good enough. */
8ccc31eb 3185 rval = save_expr (rval);
8d08fdba
MS
3186 init = copy_to_permanent (init);
3187 rval = copy_to_permanent (rval);
3188 static_aggregates = perm_tree_cons (init, rval, static_aggregates);
3189 }
3190 else
3191 {
3192 /* Have to wrap this in RTL_EXPR for two cases:
3193 in base or member initialization and if we
3194 are a branch of a ?: operator. Since we
3195 can't easily know the latter, just do it always. */
3196 tree xval = make_node (RTL_EXPR);
3197
7215f9a0
MS
3198 /* If we want to check the value of the allocation expression,
3199 and the number of elements in the array is not a constant, we
3200 *must* expand the SAVE_EXPR for nelts in alloc_expr before we
ddd5a7c1 3201 expand it in the actual initialization. So we need to build up
7215f9a0
MS
3202 an RTL_EXPR for alloc_expr. Sigh. */
3203 if (alloc_expr && ! TREE_CONSTANT (nelts))
3204 {
3205 tree xval = make_node (RTL_EXPR);
3206 rtx rtxval;
3207 TREE_TYPE (xval) = TREE_TYPE (alloc_expr);
3208 do_pending_stack_adjust ();
3209 start_sequence_for_rtl_expr (xval);
3210 emit_note (0, -1);
3211 rtxval = expand_expr (alloc_expr, NULL, VOIDmode, 0);
3212 do_pending_stack_adjust ();
3213 TREE_SIDE_EFFECTS (xval) = 1;
3214 RTL_EXPR_SEQUENCE (xval) = get_insns ();
3215 end_sequence ();
3216 RTL_EXPR_RTL (xval) = rtxval;
3217 TREE_TYPE (xval) = TREE_TYPE (alloc_expr);
3218 alloc_expr = xval;
3219 }
3220
8d08fdba
MS
3221 TREE_TYPE (xval) = TREE_TYPE (rval);
3222 do_pending_stack_adjust ();
3223 start_sequence_for_rtl_expr (xval);
3224
3225 /* As a matter of principle, `start_sequence' should do this. */
3226 emit_note (0, -1);
3227
8ccc31eb
MS
3228 rval = save_expr (rval);
3229 rval = expand_vec_init (decl, rval,
3230 build_binary_op (MINUS_EXPR, nelts,
3231 integer_one_node, 1),
3232 init, 0);
8d08fdba
MS
3233
3234 do_pending_stack_adjust ();
3235
3236 TREE_SIDE_EFFECTS (xval) = 1;
3237 TREE_CALLS_NEW (xval) = 1;
3238 RTL_EXPR_SEQUENCE (xval) = get_insns ();
3239 end_sequence ();
3240
3241 if (TREE_CODE (rval) == SAVE_EXPR)
3242 {
3243 /* Errors may cause this to not get evaluated. */
3244 if (SAVE_EXPR_RTL (rval) == 0)
3245 SAVE_EXPR_RTL (rval) = const0_rtx;
3246 RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
3247 }
3248 else
3249 {
3250 my_friendly_assert (TREE_CODE (rval) == VAR_DECL, 217);
3251 RTL_EXPR_RTL (xval) = DECL_RTL (rval);
3252 }
3253 rval = xval;
3254 }
3255 }
8ccc31eb
MS
3256 else if (TYPE_READONLY (true_type))
3257 cp_error ("uninitialized const in `new' of `%#T'", true_type);
3258
8d08fdba 3259 done:
d18c083e 3260
b7484fbe 3261 if (alloc_expr)
d18c083e 3262 {
b7484fbe
MS
3263 /* Did we modify the storage? */
3264 if (rval != alloc_temp)
3265 {
3266 tree ifexp = build_binary_op (NE_EXPR, alloc_expr,
3267 integer_zero_node, 1);
3268 rval = build_conditional_expr (ifexp, rval, alloc_temp);
3269 }
3270 else
3271 rval = alloc_expr;
d18c083e
MS
3272 }
3273
51c184be
MS
3274 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
3275 {
3276 /* The type of new int [3][3] is not int *, but int [3] * */
6060a796 3277 rval = build_c_cast (build_pointer_type (type), rval, 0);
51c184be
MS
3278 }
3279
8d08fdba
MS
3280 if (pending_sizes)
3281 rval = build_compound_expr (chainon (pending_sizes,
3282 build_tree_list (NULL_TREE, rval)));
3283
3284 if (flag_gc)
3285 {
3286 extern tree gc_visible;
3287 tree objbits;
3288 tree update_expr;
3289
3290 rval = save_expr (rval);
3291 /* We don't need a `headof' operation to do this because
3292 we know where the object starts. */
3293 objbits = build1 (INDIRECT_REF, unsigned_type_node,
3294 build (MINUS_EXPR, ptr_type_node,
3295 rval, c_sizeof_nowarn (unsigned_type_node)));
3296 update_expr = build_modify_expr (objbits, BIT_IOR_EXPR, gc_visible);
3297 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
3298 tree_cons (NULL_TREE, update_expr,
3299 build_tree_list (NULL_TREE, rval))));
3300 }
3301
00595019 3302 return rval;
8d08fdba
MS
3303}
3304\f
3305/* `expand_vec_init' performs initialization of a vector of aggregate
3306 types.
3307
3308 DECL is passed only for error reporting, and provides line number
3309 and source file name information.
3310 BASE is the space where the vector will be.
3311 MAXINDEX is the maximum index of the array (one less than the
3312 number of elements).
3313 INIT is the (possibly NULL) initializer.
3314
3315 FROM_ARRAY is 0 if we should init everything with INIT
3316 (i.e., every element initialized from INIT).
3317 FROM_ARRAY is 1 if we should index into INIT in parallel
3318 with initialization of DECL.
3319 FROM_ARRAY is 2 if we should index into INIT in parallel,
3320 but use assignment instead of initialization. */
3321
3322tree
3323expand_vec_init (decl, base, maxindex, init, from_array)
3324 tree decl, base, maxindex, init;
3325 int from_array;
3326{
3327 tree rval;
3328 tree iterator, base2 = NULL_TREE;
3329 tree type = TREE_TYPE (TREE_TYPE (base));
3330 tree size;
3331
3332 maxindex = convert (integer_type_node, maxindex);
3333 if (maxindex == error_mark_node)
3334 return error_mark_node;
3335
3336 if (current_function_decl == NULL_TREE)
3337 {
3338 rval = make_tree_vec (3);
3339 TREE_VEC_ELT (rval, 0) = base;
3340 TREE_VEC_ELT (rval, 1) = maxindex;
3341 TREE_VEC_ELT (rval, 2) = init;
3342 return rval;
3343 }
3344
3345 size = size_in_bytes (type);
3346
3347 /* Set to zero in case size is <= 0. Optimizer will delete this if
3348 it is not needed. */
3349 rval = get_temp_regvar (TYPE_POINTER_TO (type),
3350 convert (TYPE_POINTER_TO (type), null_pointer_node));
3351 base = default_conversion (base);
3352 base = convert (TYPE_POINTER_TO (type), base);
3353 expand_assignment (rval, base, 0, 0);
3354 base = get_temp_regvar (TYPE_POINTER_TO (type), base);
3355
3356 if (init != NULL_TREE
3357 && TREE_CODE (init) == CONSTRUCTOR
3358 && TREE_TYPE (init) == TREE_TYPE (decl))
3359 {
3360 /* Initialization of array from {...}. */
3361 tree elts = CONSTRUCTOR_ELTS (init);
3362 tree baseref = build1 (INDIRECT_REF, type, base);
3363 tree baseinc = build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size);
3364 int host_i = TREE_INT_CST_LOW (maxindex);
3365
3366 if (IS_AGGR_TYPE (type))
3367 {
3368 while (elts)
3369 {
3370 host_i -= 1;
6060a796 3371 expand_aggr_init (baseref, TREE_VALUE (elts), 0, 0);
8d08fdba
MS
3372
3373 expand_assignment (base, baseinc, 0, 0);
3374 elts = TREE_CHAIN (elts);
3375 }
3376 /* Initialize any elements by default if possible. */
3377 if (host_i >= 0)
3378 {
3379 if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
3380 {
3381 if (obey_regdecls)
3382 use_variable (DECL_RTL (base));
3383 goto done_init;
3384 }
3385
3386 iterator = get_temp_regvar (integer_type_node,
3387 build_int_2 (host_i, 0));
3388 init = NULL_TREE;
3389 goto init_by_default;
3390 }
3391 }
3392 else
3393 while (elts)
3394 {
3395 expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
3396
3397 expand_assignment (base, baseinc, 0, 0);
3398 elts = TREE_CHAIN (elts);
3399 }
3400
3401 if (obey_regdecls)
3402 use_variable (DECL_RTL (base));
3403 }
3404 else
3405 {
3406 tree itype;
3407
3408 iterator = get_temp_regvar (integer_type_node, maxindex);
3409
3410 init_by_default:
3411
3412 /* If initializing one array from another,
3413 initialize element by element. */
3414 if (from_array)
3415 {
3416 /* We rely upon the below calls the do argument checking */
3417 if (decl == NULL_TREE)
3418 {
3419 sorry ("initialization of array from dissimilar array type");
3420 return error_mark_node;
3421 }
3422 if (init)
3423 {
3424 base2 = default_conversion (init);
3425 itype = TREE_TYPE (base2);
3426 base2 = get_temp_regvar (itype, base2);
3427 itype = TREE_TYPE (itype);
3428 }
3429 else if (TYPE_LANG_SPECIFIC (type)
3430 && TYPE_NEEDS_CONSTRUCTING (type)
3431 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3432 {
3433 error ("initializer ends prematurely");
3434 return error_mark_node;
3435 }
3436 }
3437
b7484fbe 3438 expand_start_cond (build (GE_EXPR, boolean_type_node,
8d08fdba
MS
3439 iterator, integer_zero_node), 0);
3440 expand_start_loop_continue_elsewhere (1);
3441
3442 if (from_array)
3443 {
3444 tree to = build1 (INDIRECT_REF, type, base);
3445 tree from;
3446
3447 if (base2)
3448 from = build1 (INDIRECT_REF, itype, base2);
3449 else
3450 from = NULL_TREE;
3451
3452 if (from_array == 2)
3453 expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
3454 else if (TYPE_NEEDS_CONSTRUCTING (type))
6060a796 3455 expand_aggr_init (to, from, 0, 0);
8d08fdba
MS
3456 else if (from)
3457 expand_assignment (to, from, 0, 0);
3458 else
3459 my_friendly_abort (57);
3460 }
3461 else if (TREE_CODE (type) == ARRAY_TYPE)
3462 {
3463 if (init != 0)
3464 sorry ("cannot initialize multi-dimensional array with initializer");
3465 expand_vec_init (decl, build1 (NOP_EXPR, TYPE_POINTER_TO (TREE_TYPE (type)), base),
3466 array_type_nelts (type), 0, 0);
3467 }
3468 else
6060a796 3469 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0, 0);
8d08fdba
MS
3470
3471 expand_assignment (base,
3472 build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size),
3473 0, 0);
3474 if (base2)
3475 expand_assignment (base2,
3476 build (PLUS_EXPR, TYPE_POINTER_TO (type), base2, size), 0, 0);
3477 expand_loop_continue_here ();
b7484fbe 3478 expand_exit_loop_if_false (0, build (NE_EXPR, boolean_type_node,
8d08fdba
MS
3479 build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one));
3480
3481 if (obey_regdecls)
3482 {
3483 use_variable (DECL_RTL (base));
3484 if (base2)
3485 use_variable (DECL_RTL (base2));
3486 }
3487 expand_end_loop ();
3488 expand_end_cond ();
3489 if (obey_regdecls)
3490 use_variable (DECL_RTL (iterator));
3491 }
3492 done_init:
3493
3494 if (obey_regdecls)
3495 use_variable (DECL_RTL (rval));
3496 return rval;
3497}
3498
3499/* Free up storage of type TYPE, at address ADDR.
3500
3501 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3502 of pointer.
3503
3504 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3505 used as the second argument to operator delete. It can include
3506 things like padding and magic size cookies. It has virtual in it,
3507 because if you have a base pointer and you delete through a virtual
3508 destructor, it should be the size of the dynamic object, not the
3509 static object, see Free Store 12.5 ANSI C++ WP.
3510
3511 This does not call any destructors. */
3512tree
a28e3c7f 3513build_x_delete (type, addr, which_delete, virtual_size)
8d08fdba 3514 tree type, addr;
a28e3c7f 3515 int which_delete;
8d08fdba
MS
3516 tree virtual_size;
3517{
a28e3c7f
MS
3518 int use_global_delete = which_delete & 1;
3519 int use_vec_delete = !!(which_delete & 2);
8d08fdba 3520 tree rval;
a28e3c7f 3521 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
8d08fdba 3522
a28e3c7f
MS
3523 if (! use_global_delete && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
3524 && (TYPE_GETS_DELETE (TREE_TYPE (type)) & (1 << use_vec_delete)))
3525 rval = build_opfncall (code, LOOKUP_NORMAL, addr, virtual_size, NULL_TREE);
8d08fdba 3526 else
a28e3c7f 3527 rval = build_builtin_call (void_type_node, use_vec_delete ? BIVD : BID,
8d08fdba
MS
3528 build_tree_list (NULL_TREE, addr));
3529 return rval;
3530}
3531
3532/* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3533 ADDR is an expression which yields the store to be destroyed.
3534 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3535 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3536 virtual baseclasses.
3537 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3538
3539 FLAGS is the logical disjunction of zero or more LOOKUP_
3540 flags. See cp-tree.h for more info.
3541
3542 This function does not delete an object's virtual base classes. */
3543tree
3544build_delete (type, addr, auto_delete, flags, use_global_delete)
3545 tree type, addr;
3546 tree auto_delete;
3547 int flags;
3548 int use_global_delete;
3549{
3550 tree function, parms;
3551 tree member;
3552 tree expr;
3553 tree ref;
3554 int ptr;
3555
3556 if (addr == error_mark_node)
3557 return error_mark_node;
3558
3559 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3560 set to `error_mark_node' before it gets properly cleaned up. */
3561 if (type == error_mark_node)
3562 return error_mark_node;
3563
3564 type = TYPE_MAIN_VARIANT (type);
3565
3566 if (TREE_CODE (type) == POINTER_TYPE)
3567 {
2986ae00 3568 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8d08fdba
MS
3569 if (TYPE_SIZE (type) == 0)
3570 {
3571 incomplete_type_error (0, type);
3572 return error_mark_node;
3573 }
3574 if (TREE_CODE (type) == ARRAY_TYPE)
3575 goto handle_array;
3576 if (! IS_AGGR_TYPE (type))
3577 {
3578 /* Call the builtin operator delete. */
3579 return build_builtin_call (void_type_node, BID,
3580 build_tree_list (NULL_TREE, addr));
3581 }
3582 if (TREE_SIDE_EFFECTS (addr))
3583 addr = save_expr (addr);
2986ae00
MS
3584
3585 /* throw away const and volatile on target type of addr */
6060a796 3586 addr = convert_force (build_pointer_type (type), addr, 0);
8d08fdba
MS
3587 ref = build_indirect_ref (addr, NULL_PTR);
3588 ptr = 1;
3589 }
3590 else if (TREE_CODE (type) == ARRAY_TYPE)
3591 {
3592 handle_array:
3593 if (TREE_SIDE_EFFECTS (addr))
3594 addr = save_expr (addr);
3595 return build_vec_delete (addr, array_type_nelts (type),
3596 c_sizeof_nowarn (TREE_TYPE (type)),
a28e3c7f
MS
3597 auto_delete, integer_two_node,
3598 use_global_delete);
8d08fdba
MS
3599 }
3600 else
3601 {
3602 /* Don't check PROTECT here; leave that decision to the
3603 destructor. If the destructor is accessible, call it,
3604 else report error. */
3605 addr = build_unary_op (ADDR_EXPR, addr, 0);
3606 if (TREE_SIDE_EFFECTS (addr))
3607 addr = save_expr (addr);
3608
3609 if (TREE_CONSTANT (addr))
3610 addr = convert_pointer_to (type, addr);
3611 else
6060a796 3612 addr = convert_force (build_pointer_type (type), addr, 0);
8d08fdba
MS
3613
3614 if (TREE_CODE (addr) == NOP_EXPR
3615 && TREE_OPERAND (addr, 0) == current_class_decl)
3616 ref = C_C_D;
3617 else
3618 ref = build_indirect_ref (addr, NULL_PTR);
3619 ptr = 0;
3620 }
3621
3622 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3623
3624 if (! TYPE_NEEDS_DESTRUCTOR (type))
3625 {
8d08fdba
MS
3626 if (auto_delete == integer_zero_node)
3627 return void_zero_node;
3628
3629 /* Pass the size of the object down to the operator delete() in
3630 addition to the ADDR. */
a28e3c7f 3631 if (TYPE_GETS_REG_DELETE (type) && !use_global_delete)
8d08fdba 3632 {
8d08fdba
MS
3633 tree virtual_size = c_sizeof_nowarn (type);
3634 return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3635 virtual_size, NULL_TREE);
3636 }
3637
3638 /* Call the builtin operator delete. */
3639 return build_builtin_call (void_type_node, BID,
3640 build_tree_list (NULL_TREE, addr));
3641 }
3642 parms = build_tree_list (NULL_TREE, addr);
3643
3644 /* Below, we will reverse the order in which these calls are made.
3645 If we have a destructor, then that destructor will take care
3646 of the base classes; otherwise, we must do that here. */
3647 if (TYPE_HAS_DESTRUCTOR (type))
3648 {
3649 tree dtor = DECL_MAIN_VARIANT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0));
3650 tree basetypes = TYPE_BINFO (type);
700f8a87
MS
3651 tree passed_auto_delete;
3652 tree do_delete = NULL_TREE;
3653
3654 if (use_global_delete)
3655 {
3656 tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
3657 auto_delete, integer_one_node));
3658 tree call = build_builtin_call
3659 (void_type_node, BID, build_tree_list (NULL_TREE, addr));
3660
3661 cond = fold (build (COND_EXPR, void_type_node, cond,
3662 call, void_zero_node));
3663 if (cond != void_zero_node)
3664 do_delete = cond;
3665
3666 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node,
3667 auto_delete, integer_two_node));
3668 }
3669 else
863adfc0 3670 passed_auto_delete = auto_delete;
8d08fdba
MS
3671
3672 if (flags & LOOKUP_PROTECT)
3673 {
3674 enum access_type access = compute_access (basetypes, dtor);
3675
3676 if (access == access_private)
3677 {
3678 if (flags & LOOKUP_COMPLAIN)
3679 cp_error ("destructor for type `%T' is private in this scope", type);
3680 return error_mark_node;
3681 }
3682 else if (access == access_protected)
3683 {
3684 if (flags & LOOKUP_COMPLAIN)
3685 cp_error ("destructor for type `%T' is protected in this scope", type);
3686 return error_mark_node;
3687 }
3688 }
3689
3690 /* Once we are in a destructor, try not going through
3691 the virtual function table to find the next destructor. */
3692 if (DECL_VINDEX (dtor)
3693 && ! (flags & LOOKUP_NONVIRTUAL)
3694 && TREE_CODE (auto_delete) != PARM_DECL
3695 && (ptr == 1 || ! resolves_to_fixed_type_p (ref, 0)))
3696 {
3697 tree binfo, basetype;
3698 /* The code below is probably all broken. See call.c for the
3699 complete right way to do this. this offsets may not be right
3700 in the below. (mrs) */
3701 /* This destructor must be called via virtual function table. */
3702 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_CONTEXT (dtor)), 0);
3703 basetype = DECL_CLASS_CONTEXT (dtor);
3704 binfo = get_binfo (basetype,
3705 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3706 0);
3707 expr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3708 if (expr != TREE_VALUE (parms))
3709 {
3710 expr = fold (expr);
3711 ref = build_indirect_ref (expr, NULL_PTR);
3712 TREE_VALUE (parms) = expr;
3713 }
3714 function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (dtor));
3715 if (function == error_mark_node)
3716 return error_mark_node;
3717 TREE_TYPE (function) = build_pointer_type (TREE_TYPE (dtor));
700f8a87 3718 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, passed_auto_delete);
8d08fdba 3719 expr = build_function_call (function, parms);
700f8a87
MS
3720 if (do_delete)
3721 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
8d08fdba
MS
3722 if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0)
3723 {
3724 /* Handle the case where a virtual destructor is
3725 being called on an item that is 0.
3726
3727 @@ Does this really need to be done? */
3728 tree ifexp = build_binary_op(NE_EXPR, addr, integer_zero_node,1);
3729#if 0
3730 if (TREE_CODE (ref) == VAR_DECL
3731 || TREE_CODE (ref) == COMPONENT_REF)
3732 warning ("losing in build_delete");
3733#endif
3734 expr = build (COND_EXPR, void_type_node,
3735 ifexp, expr, void_zero_node);
3736 }
3737 }
3738 else
3739 {
3740 tree ifexp;
3741
3742 if ((flags & LOOKUP_DESTRUCTOR)
3743 || TREE_CODE (ref) == VAR_DECL
3744 || TREE_CODE (ref) == PARM_DECL
3745 || TREE_CODE (ref) == COMPONENT_REF
3746 || TREE_CODE (ref) == ARRAY_REF)
3747 /* These can't be 0. */
3748 ifexp = integer_one_node;
3749 else
3750 /* Handle the case where a non-virtual destructor is
3751 being called on an item that is 0. */
3752 ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node, 1);
3753
3754 /* Used to mean that this destructor was known to be empty,
3755 but that's now obsolete. */
3756 my_friendly_assert (DECL_INITIAL (dtor) != void_type_node, 221);
3757
700f8a87 3758 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, passed_auto_delete);
8d08fdba 3759 expr = build_function_call (dtor, parms);
700f8a87
MS
3760 if (do_delete)
3761 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
8d08fdba
MS
3762
3763 if (ifexp != integer_one_node)
3764 expr = build (COND_EXPR, void_type_node,
3765 ifexp, expr, void_zero_node);
3766 }
3767 return expr;
3768 }
3769 else
3770 {
3771 /* This can get visibilities wrong. */
3772 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3773 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3774 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3775 tree exprstmt = NULL_TREE;
3776 tree parent_auto_delete = auto_delete;
3777 tree cond;
3778
3779 /* If this type does not have a destructor, but does have
3780 operator delete, call the parent parent destructor (if any),
3781 but let this node do the deleting. Otherwise, it is ok
3782 to let the parent destructor do the deleting. */
a28e3c7f 3783 if (TYPE_GETS_REG_DELETE (type) && !use_global_delete)
8d08fdba
MS
3784 {
3785 parent_auto_delete = integer_zero_node;
3786 if (auto_delete == integer_zero_node)
3787 cond = NULL_TREE;
3788 else
3789 {
3790 tree virtual_size;
3791
3792 /* This is probably wrong. It should be the size of the
3793 virtual object being deleted. */
3794 virtual_size = c_sizeof_nowarn (type);
3795
3796 expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3797 virtual_size, NULL_TREE);
3798 if (expr == error_mark_node)
3799 return error_mark_node;
3800 if (auto_delete != integer_one_node)
3801 cond = build (COND_EXPR, void_type_node,
3802 build (BIT_AND_EXPR, integer_type_node,
3803 auto_delete, integer_one_node),
3804 expr, void_zero_node);
3805 else
3806 cond = expr;
3807 }
3808 }
3809 else if (base_binfo == NULL_TREE
3810 || (TREE_VIA_VIRTUAL (base_binfo) == 0
3811 && ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))))
3812 {
3813 tree virtual_size;
3814
3815 /* This is probably wrong. It should be the size of the virtual
3816 object being deleted. */
3817 virtual_size = c_sizeof_nowarn (type);
3818
3819 cond = build (COND_EXPR, void_type_node,
3820 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3821 build_builtin_call (void_type_node, BID,
3822 build_tree_list (NULL_TREE, addr)),
3823 void_zero_node);
3824 }
3825 else
3826 cond = NULL_TREE;
3827
3828 if (cond)
3829 exprstmt = build_tree_list (NULL_TREE, cond);
3830
3831 if (base_binfo
3832 && ! TREE_VIA_VIRTUAL (base_binfo)
3833 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3834 {
3835 tree this_auto_delete;
3836
3837 if (BINFO_OFFSET_ZEROP (base_binfo))
3838 this_auto_delete = parent_auto_delete;
3839 else
3840 this_auto_delete = integer_zero_node;
3841
3842 expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), addr,
3843 this_auto_delete, flags, 0);
3844 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3845 }
3846
3847 /* Take care of the remaining baseclasses. */
3848 for (i = 1; i < n_baseclasses; i++)
3849 {
3850 base_binfo = TREE_VEC_ELT (binfos, i);
3851 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3852 || TREE_VIA_VIRTUAL (base_binfo))
3853 continue;
3854
3855 /* May be zero offset if other baseclasses are virtual. */
3856 expr = fold (build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
3857 addr, BINFO_OFFSET (base_binfo)));
3858
3859 expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), expr,
3860 integer_zero_node,
3861 flags, 0);
3862
3863 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3864 }
3865
3866 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3867 {
3868 if (TREE_CODE (member) != FIELD_DECL)
3869 continue;
3870 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3871 {
3872 tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0);
3873 tree this_type = TREE_TYPE (member);
3874 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3875 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3876 }
3877 }
3878
3879 if (exprstmt)
3880 return build_compound_expr (exprstmt);
3881 /* Virtual base classes make this function do nothing. */
3882 return void_zero_node;
3883 }
3884}
3885
3886/* For type TYPE, delete the virtual baseclass objects of DECL. */
3887
3888tree
3889build_vbase_delete (type, decl)
3890 tree type, decl;
3891{
3892 tree vbases = CLASSTYPE_VBASECLASSES (type);
3893 tree result = NULL_TREE;
3894 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3895
3896 my_friendly_assert (addr != error_mark_node, 222);
3897
3898 while (vbases)
3899 {
3900 tree this_addr = convert_force (TYPE_POINTER_TO (BINFO_TYPE (vbases)),
6060a796 3901 addr, 0);
8d08fdba
MS
3902 result = tree_cons (NULL_TREE,
3903 build_delete (TREE_TYPE (this_addr), this_addr,
3904 integer_zero_node,
3905 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3906 result);
3907 vbases = TREE_CHAIN (vbases);
3908 }
3909 return build_compound_expr (nreverse (result));
3910}
3911
3912/* Build a C++ vector delete expression.
3913 MAXINDEX is the number of elements to be deleted.
3914 ELT_SIZE is the nominal size of each element in the vector.
3915 BASE is the expression that should yield the store to be deleted.
8d08fdba
MS
3916 This function expands (or synthesizes) these calls itself.
3917 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3918 AUTO_DELETE say whether each item in the container should be deallocated.
3919
3920 This also calls delete for virtual baseclasses of elements of the vector.
3921
3922 Update: MAXINDEX is no longer needed. The size can be extracted from the
3923 start of the vector for pointers, and from the type for arrays. We still
3924 use MAXINDEX for arrays because it happens to already have one of the
3925 values we'd have to extract. (We could use MAXINDEX with pointers to
3926 confirm the size, and trap if the numbers differ; not clear that it'd
3927 be worth bothering.) */
3928tree
a28e3c7f
MS
3929build_vec_delete (base, maxindex, elt_size, auto_delete_vec, auto_delete,
3930 use_global_delete)
8d08fdba 3931 tree base, maxindex, elt_size;
8d08fdba 3932 tree auto_delete_vec, auto_delete;
a28e3c7f 3933 int use_global_delete;
8d08fdba
MS
3934{
3935 tree ptype = TREE_TYPE (base);
3936 tree type;
3937 tree virtual_size;
3938 /* Temporary variables used by the loop. */
3939 tree tbase, size_exp, tbase_init;
3940
3941 /* This is the body of the loop that implements the deletion of a
3942 single element, and moves temp variables to next elements. */
3943 tree body;
3944
3945 /* This is the LOOP_EXPR that governs the deletion of the elements. */
3946 tree loop;
3947
3948 /* This is the thing that governs what to do after the loop has run. */
3949 tree deallocate_expr = 0;
3950
3951 /* This is the BIND_EXPR which holds the outermost iterator of the
3952 loop. It is convenient to set this variable up and test it before
3953 executing any other code in the loop.
3954 This is also the containing expression returned by this function. */
3955 tree controller = NULL_TREE;
3956
3957 /* This is the BLOCK to record the symbol binding for debugging. */
3958 tree block;
3959
3960 base = stabilize_reference (base);
3961
3962 /* Since we can use base many times, save_expr it. */
3963 if (TREE_SIDE_EFFECTS (base))
3964 base = save_expr (base);
3965
3966 if (TREE_CODE (ptype) == POINTER_TYPE)
3967 {
3968 /* Step back one from start of vector, and read dimension. */
3969 tree cookie_addr = build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
3970 base, BI_header_size);
3971 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3972 maxindex = build_component_ref (cookie, nc_nelts_field_id, 0, 0);
3973 do
3974 ptype = TREE_TYPE (ptype);
3975 while (TREE_CODE (ptype) == ARRAY_TYPE);
3976 }
3977 else if (TREE_CODE (ptype) == ARRAY_TYPE)
3978 {
3979 /* get the total number of things in the array, maxindex is a bad name */
3980 maxindex = array_type_nelts_total (ptype);
3981 while (TREE_CODE (ptype) == ARRAY_TYPE)
3982 ptype = TREE_TYPE (ptype);
3983 base = build_unary_op (ADDR_EXPR, base, 1);
3984 }
3985 else
3986 {
3987 error ("type to vector delete is neither pointer or array type");
3988 return error_mark_node;
3989 }
3990 type = ptype;
3991 ptype = TYPE_POINTER_TO (type);
3992
3993 size_exp = size_in_bytes (type);
3994
3995 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
3996 {
3997 loop = integer_zero_node;
3998 goto no_destructor;
3999 }
4000
4001 /* The below is short by BI_header_size */
4002 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4003
4004 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
4005 tbase_init = build_modify_expr (tbase, NOP_EXPR,
4006 fold (build (PLUS_EXPR, ptype,
4007 base,
4008 virtual_size)));
4009 DECL_REGISTER (tbase) = 1;
4010 controller = build (BIND_EXPR, void_type_node, tbase, 0, 0);
4011 TREE_SIDE_EFFECTS (controller) = 1;
4012 block = build_block (tbase, 0, 0, 0, 0);
4013 add_block_current_level (block);
4014
4015 if (auto_delete != integer_zero_node
4016 && auto_delete != integer_two_node)
4017 {
4018 tree base_tbd = convert (ptype,
4019 build_binary_op (MINUS_EXPR,
4020 convert (ptr_type_node, base),
4021 BI_header_size,
4022 1));
4023 /* This is the real size */
4024 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4025 body = build_tree_list (NULL_TREE,
a28e3c7f
MS
4026 build_x_delete (ptype, base_tbd,
4027 2 | use_global_delete,
8d08fdba
MS
4028 virtual_size));
4029 body = build (COND_EXPR, void_type_node,
4030 build (BIT_AND_EXPR, integer_type_node,
4031 auto_delete, integer_one_node),
4032 body, integer_zero_node);
4033 }
4034 else
4035 body = NULL_TREE;
4036
4037 body = tree_cons (NULL_TREE,
4038 build_delete (ptype, tbase, auto_delete,
8926095f 4039 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
8d08fdba
MS
4040 body);
4041
4042 body = tree_cons (NULL_TREE,
4043 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
4044 body);
4045
4046 body = tree_cons (NULL_TREE,
4047 build (EXIT_EXPR, void_type_node,
b7484fbe 4048 build (EQ_EXPR, boolean_type_node, base, tbase)),
8d08fdba
MS
4049 body);
4050
4051 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
4052
4053 loop = tree_cons (NULL_TREE, tbase_init,
4054 tree_cons (NULL_TREE, loop, NULL_TREE));
4055 loop = build_compound_expr (loop);
4056
4057 no_destructor:
4058 /* If the delete flag is one, or anything else with the low bit set,
4059 delete the storage. */
4060 if (auto_delete_vec == integer_zero_node
4061 || auto_delete_vec == integer_two_node)
4062 deallocate_expr = integer_zero_node;
4063 else
4064 {
4065 tree base_tbd;
4066
4067 /* The below is short by BI_header_size */
4068 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4069
a28e3c7f 4070 if (! TYPE_VEC_NEW_USES_COOKIE (type))
8d08fdba
MS
4071 /* no header */
4072 base_tbd = base;
4073 else
4074 {
4075 base_tbd = convert (ptype,
4076 build_binary_op (MINUS_EXPR,
4077 convert (string_type_node, base),
4078 BI_header_size,
4079 1));
4080 /* True size with header. */
4081 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4082 }
a28e3c7f
MS
4083 deallocate_expr = build_x_delete (ptype, base_tbd,
4084 2 | use_global_delete,
8d08fdba
MS
4085 virtual_size);
4086 if (auto_delete_vec != integer_one_node)
4087 deallocate_expr = build (COND_EXPR, void_type_node,
4088 build (BIT_AND_EXPR, integer_type_node,
4089 auto_delete_vec, integer_one_node),
4090 deallocate_expr, integer_zero_node);
4091 }
4092
4093 if (loop && deallocate_expr != integer_zero_node)
4094 {
4095 body = tree_cons (NULL_TREE, loop,
4096 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
4097 body = build_compound_expr (body);
4098 }
4099 else
4100 body = loop;
4101
4102 /* Outermost wrapper: If pointer is null, punt. */
4103 body = build (COND_EXPR, void_type_node,
b7484fbe 4104 build (NE_EXPR, boolean_type_node, base, integer_zero_node),
8d08fdba
MS
4105 body, integer_zero_node);
4106 body = build1 (NOP_EXPR, void_type_node, body);
4107
4108 if (controller)
4109 {
4110 TREE_OPERAND (controller, 1) = body;
4111 return controller;
4112 }
4113 else
4114 return convert (void_type_node, body);
4115}
This page took 0.587257 seconds and 5 git commands to generate.