No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / usr.bin / g++ / cc1plus / cplus-init.c
blob51a13d1e192448e43bad6acd26844adf8c9d1f5d
1 /* Handle initialization things in C++.
2 Copyright (C) 1987 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@mcc.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 1, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "tree.h"
26 #include "cplus-tree.h"
27 #include "flags.h"
28 #include "assert.h"
30 /* For expand_asm_operands. */
31 extern char *input_filename;
32 extern int lineno;
34 #define NULL 0
36 /* In C++, structures with well-defined constructors are initialized by
37 those constructors, unasked. CURRENT_BASE_INIT_LIST
38 holds a list of stmts for a BASE_INIT term in the grammar.
39 This list has one element for each base class which must be
40 initialized. The list elements are [basename, init], with
41 type basetype. This allows the possibly anachronistic form
42 (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
43 where each successive term can be handed down the constructor
44 line. Perhaps this was not intended. */
45 tree current_base_init_list, current_member_init_list;
47 void init_init_processing ();
48 void emit_base_init ();
49 void check_base_init ();
50 static void expand_aggr_vbase_init ();
51 void expand_member_init ();
52 void expand_aggr_init ();
53 tree build_virtual_init ();
54 tree build_vbase_delete ();
56 static void expand_aggr_init_1 ();
57 static void expand_recursive_init_1 ();
58 static void expand_recursive_init ();
59 tree expand_vec_init ();
60 tree build_vec_delete ();
62 static void add_friend (), add_friends ();
64 int is_aggr_typedef ();
65 /* Cache _builtin_new and _builtin_delete exprs. */
66 static tree BIN, BIVN, BID;
68 #ifdef SOS
69 tree get_linktable_name (), get_dtable_name (), get_sos_dtable ();
70 static tree __sosFindCode, __sosLookup, __sosImport;
71 static tree build_dynamic_new ();
72 #endif
73 static tree minus_one;
75 extern struct rtx_def *start_sequence (), *get_insns (), *get_last_insn ();
76 extern struct rtx_def *const0_rtx;
78 /* Set up local variable for this file. MUST BE CALLED AFTER
79 INIT_DECL_PROCESSING. */
81 void init_init_processing ()
83 BIN = default_conversion (TREE_VALUE (lookup_name (get_identifier ("__builtin_new"))));
84 BIVN = default_conversion (TREE_VALUE (lookup_name (get_identifier ("__builtin_vec_new"))));
85 BID = default_conversion (TREE_VALUE (lookup_name (get_identifier ("__builtin_delete"))));
86 minus_one = build_int_2 (-1, -1);
87 #ifdef SOS
88 if (flag_all_virtual == 2)
90 __sosFindCode = default_conversion (lookup_name (get_identifier ("sosFindCode")));
91 __sosLookup = default_conversion (lookup_name (get_identifier ("sosLookup")));
92 __sosImport = default_conversion (lookup_name (get_identifier ("sosImport")));
94 #endif
97 /* Perform whatever initialization have yet to be done on the
98 base class of the class variable. These actions are in
99 the global variable CURRENT_BASE_INIT_LIST. Such an
100 action could be NULL_TREE, meaning that the user has explicitly
101 called the base class constructor with no arguments.
103 If there is a need for a call to a constructor, we
104 must surround that call with a pushlevel/poplevel pair,
105 since we are technically at the PARM level of scope.
107 Argument ASSIGNS_THIS_P is nonzero if the current function assigns
108 `this' explicitly. We cannot get this value by checking
109 `current_function_assigns_this', since it is set up after this
110 function is called. (although I don't know if its really
111 necessary to wait until afterward to do that.)
113 Note that emit_base_init does *not* initialize virtual
114 base classes. That is done specially, elsewhere. */
116 void
117 emit_base_init (t, immediately)
118 tree t;
119 int immediately;
121 extern tree in_charge_identifier;
123 tree member, decl;
124 tree init_list, basetype;
125 int pass, start;
126 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
127 tree fields_to_unmark = NULL_TREE;
128 tree vfields;
130 if (! immediately)
132 do_pending_stack_adjust ();
133 start_sequence ();
134 /* As a matter of principle, `start_sequence' should do this. */
135 emit_note (0, -1);
138 /* In this case, we always need IN_CHARGE_NODE, because we have
139 to know whether to deallocate or not before exiting. */
140 if (flag_handle_exceptions == 2
141 && lookup_name (in_charge_identifier) == NULL_TREE)
143 tree in_charge_node = pushdecl (build_decl (VAR_DECL, in_charge_identifier,
144 integer_type_node));
145 store_init_value (in_charge_node, build (EQ_EXPR, integer_type_node,
146 current_class_decl,
147 integer_zero_node));
148 expand_decl (in_charge_node);
149 expand_decl_init (in_charge_node);
152 start = ! TYPE_USES_VIRTUAL_BASECLASSES (t);
153 for (pass = start; pass < 2; pass++)
155 tree vbase_init_list = NULL_TREE;
156 for (init_list = current_base_init_list; init_list;
157 init_list = TREE_CHAIN (init_list))
159 tree basename = TREE_PURPOSE (init_list);
160 tree basetype;
161 tree init = TREE_VALUE (init_list);
163 if (basename == NULL_TREE)
165 /* Initializer for single base class. Must not
166 use multiple inheritance or this is ambiguous. */
167 switch (n_baseclasses)
169 case 0:
170 error ("type `%s' does not have a base class to initialize",
171 IDENTIFIER_POINTER (current_class_name));
172 return;
173 case 1:
174 break;
175 default:
176 error ("unnamed initializer ambiguous for type `%s' which uses multiple inheritance", IDENTIFIER_POINTER (current_class_name));
177 return;
179 basetype = CLASSTYPE_BASECLASS (t, 1);
181 else if (is_aggr_typedef (basename, 1))
183 basetype = basetype_or_else (TREE_TYPE (TREE_TYPE (basename)), t);
184 if (basetype == NULL_TREE)
185 continue;
187 /* Virtual base classes are special cases. Their initializers
188 are recorded with this constructor, and they are used when
189 this constructor is the top-level constructor called. */
190 if (! TREE_VIA_VIRTUAL (basetype))
192 /* Otherwise, if it is not an immediate base class, complain. */
193 for (i = n_baseclasses; i > 0; i--)
194 if (basetype == CLASSTYPE_BASECLASS (t, i))
195 break;
196 if (i == 0)
198 error ("type `%s' is not an immediate base class of type `%s'",
199 IDENTIFIER_POINTER (basename),
200 IDENTIFIER_POINTER (current_class_name));
201 continue;
205 else
206 continue;
208 /* The base initialization list goes up to the first
209 base class which can actually use it. */
211 if (CLASSTYPE_MARKED6 (basetype))
213 error ("class `%s' initializer already specified",
214 IDENTIFIER_POINTER (basename));
215 continue;
218 if (pass == start)
220 char *msgp = (! TYPE_HAS_CONSTRUCTOR (basetype))
221 ? "cannot pass initialization up to class `%s'" : 0;
223 while (! TYPE_HAS_CONSTRUCTOR (basetype)
224 && CLASSTYPE_N_BASECLASSES (basetype) == 1)
225 basetype = CLASSTYPE_BASECLASS (basetype, 1);
227 if (basetype)
229 if (msgp)
230 if (pedantic)
232 error_with_aggr_type (basetype, msgp);
233 continue;
235 else if (! TYPE_HAS_CONSTRUCTOR (basetype))
237 if (CLASSTYPE_N_BASECLASSES (basetype) == 0)
238 error_with_aggr_type (basetype, msgp);
239 else
240 sorry ("passing initializations up multiple inheritance lattice");
241 continue;
244 else
246 error ("no constructor found for initialization of `%s'",
247 IDENTIFIER_POINTER (basename));
248 continue;
251 if (TREE_VIA_VIRTUAL (basetype))
253 CLASSTYPE_MARKED6 (basetype) = 1;
254 vbase_init_list = tree_cons (init, basetype, vbase_init_list);
256 if (pass == 0)
257 continue;
259 else if (TREE_VIA_VIRTUAL (basetype))
260 continue;
262 CLASSTYPE_MARKED6 (basetype) = 1;
263 member = convert_pointer_to (basetype, current_class_decl);
264 expand_aggr_init_1 (t, 0,
265 build_indirect_ref (member, 0), init,
266 ! DECL_OFFSET (TYPE_NAME (basetype)),
267 LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN);
268 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (basetype))
270 cplus_expand_start_try (1);
271 push_exception_cleanup (member);
275 if (pass == 0)
277 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
278 tree vbases;
280 if (DECL_COMPILER_GENERATED_P (current_function_decl)
281 && TREE_CHAIN (first_arg) != NULL_TREE)
283 /* If there are virtual baseclasses without initialization
284 specified, and this is a default X(X&) construcotr,
285 build the initialization list so that each virtual baseclass
286 of the new object is initialized from the virtual baseclass
287 of the incoming arg. */
288 tree init_arg = build_unary_op (ADDR_EXPR, TREE_CHAIN (first_arg), 0);
289 for (vbases = CLASSTYPE_VBASECLASSES (t);
290 vbases; vbases = TREE_CHAIN (vbases))
292 basetype = TREE_TYPE (vbases);
293 if (CLASSTYPE_MARKED6 (basetype) == 0)
295 member = convert_pointer_to (basetype, init_arg);
296 if (member == init_arg)
297 member = TREE_CHAIN (first_arg);
298 else
299 TREE_TYPE (member) = build_reference_type (basetype);
300 vbase_init_list = tree_cons (convert_from_reference (member),
301 basetype, vbase_init_list);
302 CLASSTYPE_MARKED6 (basetype) = 1;
306 expand_start_cond (first_arg, 0);
307 expand_aggr_vbase_init (t, C_C_D, current_class_decl,
308 vbase_init_list);
309 expand_expr_stmt (build_vbase_vtables_init (t, t, C_C_D,
310 current_class_decl, 1));
311 #ifdef sparc
312 expand_asm_operands (build_string (32, "! end of vtable initialization"), 0, 0, 0, 0, input_filename, lineno);
313 #endif
314 expand_end_cond ();
317 current_base_init_list = NULL_TREE;
319 /* Now, perform default initialization of all base classes which
320 have not yet been initialized, and unmark baseclasses which
321 have been initialized. */
322 for (i = 1; i <= n_baseclasses; i++)
324 tree base = current_class_decl;
326 basetype = CLASSTYPE_BASECLASS (t, i);
327 if (TYPE_NEEDS_CONSTRUCTING (basetype))
329 if (! TREE_VIA_VIRTUAL (basetype)
330 && ! CLASSTYPE_MARKED6 (basetype))
332 tree ref;
334 if (CLASSTYPE_OFFSET (basetype) == integer_zero_node)
335 base = build1 (NOP_EXPR, TYPE_POINTER_TO (basetype), current_class_decl);
336 else
337 base = build (PLUS_EXPR, TYPE_POINTER_TO (basetype), current_class_decl, CLASSTYPE_OFFSET (basetype));
339 ref = build_indirect_ref (base, 0);
340 expand_aggr_init_1 (t, 0, ref, NULL_TREE,
341 ! DECL_OFFSET (TYPE_NAME (basetype)),
342 LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN);
343 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (basetype))
345 cplus_expand_start_try (1);
346 push_exception_cleanup (base);
350 CLASSTYPE_MARKED6 (basetype) = 0;
353 /* Initialize all the virtual function table fields that
354 do not come from virtual base classes. */
355 vfields = CLASSTYPE_VFIELDS (t);
356 while (vfields)
358 /* If the vtable installed by the constructor was not
359 the right one, fix that here. */
360 if (TREE_ADDRESSABLE (vfields)
361 && (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (vfields))
362 /* BASE_INIT_LIST has already initialized these guys. */
363 || ! value_member (TREE_VALUE (vfields), CLASSTYPE_ASSOC (t))))
365 tree basetype = TYPE_MAIN_VARIANT (TREE_TYPE (vfields));
366 tree assoc = assoc_value (basetype, t);
367 if (ASSOC_VTABLE (assoc) != CLASS_ASSOC_VTABLE (basetype))
369 tree ptr = convert_pointer_to (basetype, current_class_decl);
370 expand_expr_stmt (build_virtual_init (t, basetype, ptr));
373 vfields = TREE_CHAIN (vfields);
376 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (t)
377 #ifdef SOS
378 || TYPE_DYNAMIC (t)
379 #endif
381 expand_expr_stmt (build_virtual_init (t, t, current_class_decl));
383 /* Members we through expand_member_init. We initialize all the members
384 needing initialization that did not get it so far. */
385 for (; current_member_init_list;
386 current_member_init_list = TREE_CHAIN (current_member_init_list))
388 tree name = TREE_PURPOSE (current_member_init_list);
389 tree init = TREE_VALUE (current_member_init_list);
390 tree field = (TREE_CODE (name) == COMPONENT_REF
391 ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
392 tree type;
394 /* If one member shadows another, get the outermost one. */
395 if (TREE_CODE (field) == TREE_LIST)
397 field = TREE_VALUE (field);
398 if (decl_type_context (field) != current_class_type)
399 error ("field `%s' not in immediate context");
402 type = TREE_TYPE (field);
404 if (TREE_STATIC (field))
406 error_with_aggr_type (DECL_FIELD_CONTEXT (field),
407 "field `%s::%s' is static; only point of initialization is its declaration", IDENTIFIER_POINTER (name));
408 continue;
411 if (DECL_NAME (field))
413 if (TREE_HAS_CONSTRUCTOR (field))
414 error ("multiple initializations given for member `%s'",
415 IDENTIFIER_POINTER (DECL_NAME (field)));
418 /* Mark this node as having been initialized. */
419 TREE_HAS_CONSTRUCTOR (field) = 1;
420 if (DECL_FIELD_CONTEXT (field) != t)
421 fields_to_unmark = tree_cons (NULL_TREE, field, fields_to_unmark);
423 if (TREE_CODE (name) == COMPONENT_REF)
425 /* Initialization of anonymous union. */
426 expand_assignment (name, init, 0, 0);
427 continue;
429 decl = build_component_ref (C_C_D, name, 0, 1);
431 if (TYPE_NEEDS_CONSTRUCTING (type))
433 if (TREE_CODE (type) == ARRAY_TYPE
434 && TREE_CHAIN (init) == NULL_TREE
435 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
437 /* Initialization of one array from another. */
438 expand_vec_init (TREE_OPERAND (decl, 1), decl,
439 array_type_nelts (type), TREE_VALUE (init), 1);
441 else
442 expand_aggr_init (decl, init, 0);
444 else
446 if (init == NULL_TREE)
448 error ("types without constructors must have complete initializers");
449 init = error_mark_node;
451 else if (TREE_CHAIN (init))
453 warning ("initializer list treated as compound expression");
454 init = build_compound_expr (init);
456 else
457 init = TREE_VALUE (init);
459 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
461 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (type))
463 cplus_expand_start_try (1);
464 push_exception_cleanup (build_unary_op (ADDR_EXPR, decl, 0));
468 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
470 /* All we care about is this unique member. It contains
471 all the information we need to know, and that right early. */
472 tree type = TREE_TYPE (member);
473 tree init = TREE_HAS_CONSTRUCTOR (member)
474 ? error_mark_node : DECL_INITIAL (member);
476 /* Unmark this field. If it is from an anonymous union,
477 then unmark the field recursively. */
478 TREE_HAS_CONSTRUCTOR (member) = 0;
479 if (TREE_ANON_UNION_ELEM (member))
480 emit_base_init (TREE_TYPE (member), 1);
482 /* Member had explicit initializer. */
483 if (init == error_mark_node)
484 continue;
486 if (TREE_CODE (member) != FIELD_DECL)
487 continue;
489 if (type == error_mark_node)
490 continue;
492 if (TYPE_NEEDS_CONSTRUCTING (type))
494 if (init)
495 init = build_tree_list (NULL_TREE, init);
496 decl = build_component_ref (C_C_D, DECL_NAME (member), 0, 0);
497 expand_aggr_init (decl, init, 0);
499 else
501 if (init)
503 decl = build_component_ref (C_C_D, DECL_NAME (member), 0, 0);
504 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
506 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
507 warning ("uninitialized reference member `%s'",
508 IDENTIFIER_POINTER (DECL_NAME (member)));
510 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (type))
512 cplus_expand_start_try (1);
513 push_exception_cleanup (build_unary_op (ADDR_EXPR, decl, 0));
516 /* Unmark fields which are initialized for the base class. */
517 while (fields_to_unmark)
519 TREE_HAS_CONSTRUCTOR (TREE_VALUE (fields_to_unmark)) = 0;
520 fields_to_unmark = TREE_CHAIN (fields_to_unmark);
523 /* It is possible for the initializers to need cleanups.
524 Expand those cleanups now that all the initialization
525 has been done. */
526 expand_cleanups_to (NULL_TREE);
528 if (! immediately)
530 extern struct rtx_def *base_init_insns;
532 do_pending_stack_adjust ();
533 assert (base_init_insns == 0);
534 base_init_insns = get_insns ();
535 end_sequence ();
538 /* All the implicit try blocks we built up will be zapped
539 when we come to a real binding contour boundary. */
542 /* Check that all fields are properly initialized after
543 an assignment to `this'. */
544 void
545 check_base_init (t)
546 tree t;
548 tree member;
549 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
551 if (DECL_NAME (member) && TREE_USED (member))
552 error ("field `%s' used before initialized (after assignment to `this')",
553 IDENTIFIER_POINTER (DECL_NAME (member)));
557 /* This code sets up the virtual function tables appropriate for
558 the pointer DECL. It is a one-ply initialization.
560 TYPE is the exact type that DECL is supposed to be. In
561 muliple inheritance, this might mean "C's A" if C : A, B. */
562 tree
563 build_virtual_init (for_type, type, decl)
564 tree for_type, type;
565 tree decl;
567 tree vtbl, vtbl_ptr;
568 tree vtype;
570 #ifdef SOS
571 if (TYPE_DYNAMIC (type))
572 vtbl = build1 (NOP_EXPR, ptr_type_node, lookup_name (get_identifier (AUTO_VTABLE_NAME)));
573 else
574 #endif
576 #if 1
577 vtbl = ASSOC_VTABLE (assoc_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)),
578 for_type));
579 #else
580 assert (for_type == type);
581 vtbl = CLASS_ASSOC_VTABLE (for_type);
582 #endif /* 1 */
583 TREE_USED (vtbl) = 1;
584 vtbl = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (vtbl)), vtbl);
586 vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
587 decl = convert_pointer_to (vtype, decl);
588 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, 0), vtype);
590 /* Have to convert VTBL since array sizes may be different. */
591 return build_modify_expr (vtbl_ptr, NOP_EXPR,
592 convert (TREE_TYPE (vtbl_ptr), vtbl));
595 /* Subroutine of `expand_aggr_vbase_init'.
596 BASETYPE is type that is being initialized. INIT_LIST
597 is the list of initializers for the virtual baseclass. */
598 static void
599 expand_aggr_vbase_init_1 (basetype, exp, addr, init_list)
600 tree basetype, init_list;
602 tree init = value_member (basetype, init_list);
603 tree ref = build_indirect_ref (addr, 0);
604 if (init)
605 init = TREE_PURPOSE (init);
606 /* Call constructors, but don't set up vtables. */
607 expand_aggr_init_1 (basetype, exp, ref, init, 0,
608 LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY);
609 CLASSTYPE_MARKED6 (basetype) = 0;
612 /* Initialize this object's virtual base class pointers. This must be
613 done only at the top-level of the object being constructed.
615 INIT_LIST is list of initialization for constructor to perform. */
616 static void
617 expand_aggr_vbase_init (type, exp, addr, init_list)
618 tree type;
619 tree exp;
620 tree addr;
621 tree init_list;
623 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
625 tree result = init_vbase_pointers (type, addr);
626 tree basetype, vbases;
628 if (result)
629 expand_expr_stmt (build_compound_expr (result));
631 /* Mark everything as having an initializer
632 (either explicit or default). */
633 for (vbases = CLASSTYPE_VBASECLASSES (type);
634 vbases; vbases = TREE_CHAIN (vbases))
635 CLASSTYPE_MARKED6 (TREE_TYPE (vbases)) = 1;
637 /* First, initialize baseclasses which could be baseclasses
638 for other virtual baseclasses. */
639 for (vbases = CLASSTYPE_VBASECLASSES (type);
640 vbases; vbases = TREE_CHAIN (vbases))
641 /* Don't initialize twice. */
642 if (CLASSTYPE_MARKED6 (TREE_TYPE (vbases)))
644 tree assoc = purpose_member (TREE_TYPE (vbases), result);
645 assert (assoc != NULL_TREE);
646 expand_aggr_vbase_init_1 (TREE_PURPOSE (assoc), exp,
647 TREE_OPERAND (TREE_VALUE (assoc), 0),
648 init_list);
651 /* Now initialize the baseclasses which don't have virtual baseclasses. */
652 for (; result; result = TREE_CHAIN (result))
653 /* Don't initialize twice. */
654 if (CLASSTYPE_MARKED6 (TREE_PURPOSE (result)))
656 abort ();
657 expand_aggr_vbase_init_1 (TREE_PURPOSE (result), exp,
658 TREE_OPERAND (TREE_VALUE (result), 0),
659 init_list);
662 #ifdef sparc
663 expand_asm_operands (build_string (30, "! end of vbase initialization"), 0, 0, 0, 0, input_filename, lineno);
664 #endif
668 /* Function to give error message if member initialization specification
669 is erroneous. FIELD is the member we decided to initialize.
670 TYPE is the type for which the initialization is being performed.
671 FIELD must be a member of TYPE, or the base type from which FIELD
672 comes must not need a constructor.
674 MEMBER_NAME is the name of the member. */
676 static int
677 member_init_ok_or_else (field, type, member_name)
678 tree field;
679 tree type;
680 char *member_name;
682 if (field == error_mark_node) return 0;
683 if (field == NULL_TREE)
685 error_with_aggr_type (type, "class `%s' does not have any field named `%s'",
686 member_name);
687 return 0;
689 if (DECL_CONTEXT (field) != type
690 && TYPE_NEEDS_CONSTRUCTOR (DECL_CONTEXT (field)))
692 error ("member `%s' comes from base class needing constructor", member_name);
693 return 0;
695 return 1;
698 /* If NAME is a viable field name for the aggregate DECL,
699 and PARMS is a viable parameter list, then expand an _EXPR
700 which describes this initialization.
702 Note that we do not need to chase through the class's base classes
703 to look for NAME, because if it's in that list, it will be handled
704 by the constructor for that base class.
706 We do not yet have a fixed-point finder to instantiate types
707 being fed to overloaded constructors. If there is a unique
708 constructor, then argument types can be got from that one.
710 If INIT is non-NULL, then it the initialization should
711 be placed in `current_base_init_list', where it will be processed
712 by `emit_base_init'. */
713 void
714 expand_member_init (exp, name, init)
715 tree exp, name, init;
717 extern tree ptr_type_node; /* should be in tree.h */
719 tree basetype = NULL_TREE, field;
720 tree parm;
721 tree rval, type;
722 tree actual_name;
724 if (exp == NULL_TREE)
725 return; /* complain about this later */
727 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
729 if (name == NULL_TREE && IS_AGGR_TYPE (type))
730 switch (CLASSTYPE_N_BASECLASSES (type))
732 case 0:
733 error ("base class initializer specified, but no base class to initialize");
734 return;
735 case 1:
736 basetype = CLASSTYPE_BASECLASS (type, 1);
737 break;
738 default:
739 error ("initializer for unnamed base class ambiguous");
740 error_with_aggr_type (type, "(type `%s' uses multiple inheritance)");
741 return;
744 if (init)
746 /* The grammar should not allow fields which have names
747 that are TYPENAMEs. Therefore, if the field has
748 a non-NULL TREE_TYPE, we may assume that this is an
749 attempt to initialize a base class member of the current
750 type. Otherwise, it is an attempt to initialize a
751 member field. */
753 if (init == void_type_node)
754 init = NULL_TREE;
756 if (name == NULL_TREE || TREE_TYPE (name))
758 tree base_init;
760 if (name == NULL_TREE)
761 if (basetype)
762 name = DECL_NAME (TYPE_NAME (basetype));
763 else
765 error ("no base class to initialize");
766 return;
768 else
770 basetype = TREE_TYPE (TREE_TYPE (name));
771 if (basetype != type
772 && ! value_member (basetype, TYPE_BASETYPES (type))
773 && ! value_member (basetype, CLASSTYPE_VBASECLASSES (type)))
775 if (IDENTIFIER_CLASS_VALUE (name))
776 goto try_member;
777 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
778 error ("type `%s' is not an immediate or virtual basetype for `%s'",
779 IDENTIFIER_POINTER (name),
780 TYPE_NAME_STRING (type));
781 else
782 error ("type `%s' is not an immediate basetype for `%s'",
783 IDENTIFIER_POINTER (name),
784 TYPE_NAME_STRING (type));
785 return;
789 if (purpose_member (name, current_base_init_list))
791 error ("base class `%s' already initialized",
792 IDENTIFIER_POINTER (name));
793 return;
796 base_init = build_tree_list (name, init);
797 TREE_TYPE (base_init) = basetype;
798 current_base_init_list = chainon (current_base_init_list, base_init);
800 else
802 tree member_init;
804 try_member:
805 field = lookup_field (type, name, 1);
807 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
808 return;
810 if (purpose_member (name, current_member_init_list))
812 error ("field `%s' already initialized", DECL_NAME (name));
813 return;
816 member_init = build_tree_list (name, init);
817 TREE_TYPE (member_init) = TREE_TYPE (field);
818 current_member_init_list = chainon (current_member_init_list, member_init);
820 return;
822 else if (name == NULL_TREE)
824 compiler_error ("expand_member_init: name == NULL_TREE");
825 return;
828 basetype = type;
829 field = lookup_field (basetype, name, 0);
831 if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name)))
832 return;
834 /* now see if there is a constructor for this type
835 which will take these args. */
837 if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field)))
839 tree parmtypes, fndecl;
841 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
843 /* just know that we've seen something for this node */
844 DECL_INITIAL (exp) = error_mark_node;
845 TREE_USED (exp) = 1;
847 type = TYPE_MAIN_VARIANT (TREE_TYPE (field));
848 actual_name = DECL_NAME (TYPE_NAME (type));
849 parm = build_component_ref (exp, name, 0, 0);
851 /* Now get to the constructor. */
852 field = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
853 /* Get past destructor, if any. */
854 if (TYPE_HAS_DESTRUCTOR (type))
855 field = TREE_CHAIN (field);
857 /* If the field is unique, we can use the parameter
858 types to guide possible type instantiation. */
859 if (TREE_CHAIN (field) == NULL_TREE)
861 fndecl = TREE_TYPE (field);
862 parmtypes = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
864 else
866 parmtypes = NULL_TREE;
867 fndecl = NULL_TREE;
870 init = actualparameterlist (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL);
871 if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node)
872 rval = build_method_call (NULL_TREE, actual_name, init, NULL_TREE, LOOKUP_NORMAL);
873 else
874 return;
876 if (rval != error_mark_node)
878 /* Now, fill in the first parm with our guy */
879 TREE_VALUE (TREE_OPERAND (rval, 1))
880 = build_unary_op (ADDR_EXPR, parm, 0);
881 TREE_TYPE (rval) = ptr_type_node;
882 TREE_VOLATILE (rval) = 1;
885 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
887 parm = build_component_ref (exp, name, 0, 0);
888 expand_aggr_init (parm, NULL_TREE, 0);
889 rval = error_mark_node;
892 /* Now initialize the member. It does not have to
893 be of aggregate type to receive initialization. */
894 if (rval != error_mark_node)
895 expand_expr_stmt (rval);
898 /* This is like `expand_member_init', only it stores one aggregate
899 value into another.
901 INIT comes in two flavors: it is either a value which
902 is to be stored in EXP, or it is a parameter list
903 to go to a constructor, which will operate on EXP.
904 If `init' is a CONSTRUCTOR, then we emit a warning message,
905 explaining that such initializaitions are illegal.
907 ALIAS_THIS is nonzero iff we are initializing something which is
908 essentially an alias for C_C_D. In this case, the base constructor
909 may move it on us, and we must keep track of such deviations.
911 If INIT resolves to a CALL_EXPR which happens to return
912 something of the type we are looking for, then we know
913 that we can safely use that call to perform the
914 initialization.
916 The virtual function table pointer cannot be set up here, because
917 we do not really know its type.
919 Virtual baseclass pointers are also set up here.
921 This never calls operator=().
923 When initializing, nothing is CONST. */
925 void
926 expand_aggr_init (exp, init, alias_this)
927 tree exp, init;
928 int alias_this;
930 tree type = TREE_TYPE (exp);
931 tree addr;
932 int was_const = TREE_READONLY (exp);
934 if (init == error_mark_node)
935 return;
937 TREE_READONLY (exp) = 0;
939 if (TREE_CODE (type) == ARRAY_TYPE)
941 /* Must arrange to initialize each element of EXP
942 from elements of INIT. */
943 int was_const_elts = TREE_READONLY (TREE_TYPE (type));
944 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
945 if (was_const_elts)
947 tree atype = build_cplus_array_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
948 TYPE_DOMAIN (type));
949 if (TREE_TYPE (exp) == TREE_TYPE (init))
950 TREE_TYPE (init) = atype;
951 TREE_TYPE (exp) = atype;
953 expand_vec_init (exp, exp, array_type_nelts (type), init,
954 init && TREE_TYPE (init) == TREE_TYPE (exp));
955 TREE_READONLY (exp) = was_const;
956 TREE_TYPE (exp) = type;
957 if (init) TREE_TYPE (init) = itype;
958 return;
961 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
962 /* just know that we've seen something for this node */
963 TREE_USED (exp) = 1;
965 /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
966 constructor as parameters to an implicit GNU C++ constructor. */
967 if (init && TREE_CODE (init) == CONSTRUCTOR
968 && TYPE_HAS_CONSTRUCTOR (type)
969 && TREE_TYPE (init) == type)
970 init = CONSTRUCTOR_ELTS (init);
971 expand_aggr_init_1 (type, exp, exp, init, alias_this, LOOKUP_NORMAL);
972 TREE_READONLY (exp) = was_const;
975 /* This function is responsible for initializing EXP with INIT
976 (if any).
978 FOR_TYPE is the type for who we are performing the initialization.
979 For example, if W is a virtual base class of A and B, and C : A, B
980 if we are initializing B, then W must contain B's W vtable, whereas
981 were we initializing C, W must contain C's W vtable.
983 TRUE_EXP is nonzero if it is the true expression being initialized.
984 In this case, it may be EXP, or may just contain EXP. The reason we
985 need this is because if EXP is a base element of TRUE_EXP, we
986 don't necessarily know by looking at EXP where its virtual
987 baseclass fields should really be pointing. But we do know
988 from TRUE_EXP. In constructors, we don't know anything about
989 the value being initialized.
991 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
993 FLAGS is just passes to `build_method_call'. See that function for
994 its description. */
996 static void
997 expand_aggr_init_1 (for_type, true_exp, exp, init, alias_this, flags)
998 tree for_type;
999 tree true_exp, exp;
1000 tree init;
1001 int alias_this;
1002 int flags;
1004 tree type = TREE_TYPE (exp);
1005 tree init_type = NULL_TREE;
1006 tree rval;
1008 assert (init != error_mark_node && type != error_mark_node);
1010 /* Use a function returning the desired type to initialize EXP for us.
1011 If the function is a constructor, and its first argument is
1012 NULL_TREE, know that it was meant for us--just slide exp on
1013 in and expand the constructor. Constructors now come
1014 as NEW_EXPRs. */
1015 if (init)
1017 tree init_list = NULL_TREE;
1019 if (TREE_CODE (init) == TREE_LIST)
1021 init_list = init;
1022 if (TREE_CHAIN (init) == NULL_TREE)
1023 init = TREE_VALUE (init);
1026 init_type = TREE_TYPE (init);
1028 if (TREE_CODE (init) != TREE_LIST)
1030 if (TREE_CODE (init_type) == ERROR_MARK)
1031 return;
1033 #if 0
1034 /* These lines are found troublesome 5/11/89. */
1035 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1036 init_type = TREE_TYPE (init_type);
1037 #endif
1039 /* This happens when we use C++'s functional cast notation
1040 to act as the initializer for something not of that same
1041 type. In that case, we need to create the initializer
1042 separately from the object being initialized. */
1043 if (TREE_CODE (init) == NEW_EXPR && init_type != type)
1045 init = TREE_OPERAND (init, 1);
1046 init = build (CALL_EXPR, init_type,
1047 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1048 TREE_VOLATILE (init) = 1;
1049 #if 0
1050 TREE_RAISES (init) = ??
1051 #endif
1052 if (init_list)
1053 TREE_VALUE (init_list) = init;
1056 if (init_type == type && TREE_CODE (init) == CALL_EXPR
1057 #if 0
1058 /* It is legal to directly initialize from a CALL_EXPR
1059 without going through X(X&), apparently. */
1060 && ! TYPE_GETS_INIT_REF (type)
1061 #endif
1064 /* A CALL_EXPR is a legitmate form of initialization, so
1065 we should not print this warning message. */
1066 #if 0
1067 /* Should have gone away due to 5/11/89 change. */
1068 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
1069 init = convert_from_reference (init);
1070 #endif
1071 #if 0
1072 if (TYPE_GETS_ASSIGNMENT (type))
1073 warning ("bitwise copy: `%s' defines operator=()",
1074 TYPE_NAME_STRING (type));
1075 #endif
1076 expand_assignment (exp, init, 0, 0);
1077 if (exp == DECL_RESULT (current_function_decl))
1079 /* Failing this assertion means that the return value
1080 from receives multiple initializations. */
1081 assert (DECL_INITIAL (exp) == NULL_TREE || DECL_INITIAL (exp) == error_mark_node);
1082 DECL_INITIAL (exp) = init;
1084 return;
1086 else if (init_type == type
1087 && TREE_CODE (init) == COND_EXPR)
1089 /* Push value to be initialized into the cond, where possible.
1090 Avoid spurious warning messages when initializing the
1091 result of this function. */
1092 TREE_OPERAND (init, 1)
1093 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
1094 if (exp == DECL_RESULT (current_function_decl))
1095 DECL_INITIAL (exp) = NULL_TREE;
1096 TREE_OPERAND (init, 2)
1097 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
1098 if (exp == DECL_RESULT (current_function_decl))
1099 DECL_INITIAL (exp) = init;
1100 expand_expr (init, const0_rtx, VOIDmode, 0);
1101 return;
1105 /* We did not know what we were initializing before. Now we do. */
1106 if (TREE_CODE (init) == NEW_EXPR)
1108 tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
1110 assert (tmp != NULL_TREE);
1112 if (TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
1113 && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
1115 /* In order for this to work for RESULT_DECLs, if their
1116 type has a constructor, then they must be BLKmode
1117 so that they will be meaningfully addressable. */
1118 tree arg = build_unary_op (ADDR_EXPR, exp, 0);
1119 init = TREE_OPERAND (init, 1);
1120 init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
1121 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1122 TREE_VOLATILE (init) = 1;
1123 #if 0
1124 TREE_RAISES (init) = ??
1125 #endif
1126 TREE_VALUE (TREE_OPERAND (init, 1))
1127 = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
1129 if (alias_this)
1131 expand_assignment (current_function_decl, init, 0, 0);
1132 return;
1134 if (exp == DECL_RESULT (current_function_decl))
1136 if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
1137 fatal ("return value from function receives multiple initializations");
1138 DECL_INITIAL (exp) = init;
1140 expand_expr_stmt (init);
1141 return;
1145 /* Handle this case: when calling a constructor: xyzzy foo(bar);
1146 which really means: xyzzy foo = bar; Ugh!
1148 We can also be called with an initializer for an object
1149 which has virtual functions, but no constructors. In that
1150 case, we perform the assignment first, then initialize
1151 the virtual function table pointer fields. */
1153 if (! TYPE_NEEDS_CONSTRUCTING (type))
1155 if (init_list && TREE_CHAIN (init_list))
1157 warning ("initializer list being treated as compound expression");
1158 init = convert (TREE_TYPE (exp), build_compound_expr (init_list));
1159 if (init == error_mark_node)
1160 return;
1162 if (TREE_CODE (exp) == VAR_DECL
1163 && TREE_CODE (init) == CONSTRUCTOR
1164 && TREE_HAS_CONSTRUCTOR (init))
1165 store_init_value (exp, init);
1166 else
1167 expand_assignment (exp, init, 0, 0);
1169 if (TYPE_VIRTUAL_P (type))
1170 expand_recursive_init (for_type, true_exp, exp, init, CLASSTYPE_BASE_INIT_LIST (type), alias_this);
1171 return;
1174 /* See whether we can go through a type conversion operator.
1175 This wins over going through a constructor because we may be
1176 able to avoid an X(X&) constructor. */
1177 if (TREE_CODE (init) != TREE_LIST)
1179 tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
1180 ? TREE_TYPE (init_type) : init_type;
1182 if (ttype != type && IS_AGGR_TYPE (ttype))
1184 tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
1186 if (rval)
1188 expand_assignment (exp, rval, 0, 0);
1189 return;
1195 if (TYPE_HAS_CONSTRUCTOR (type))
1197 /* It fails because there may not be a constructor which takes
1198 its own type as the first (or only parameter), but which does
1199 take other types via a conversion. So, if the thing initializing
1200 the expression is a unit element of type X, first try X(X&),
1201 followed by initialization by X. If neither of these work
1202 out, then look hard. */
1203 tree parms = (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
1204 ? init : build_tree_list (NULL_TREE, init);
1205 int xxref_init_possible;
1207 if (parms) init = TREE_VALUE (parms);
1209 if (TYPE_HAS_INIT_REF (type)
1210 || init == NULL_TREE
1211 || TREE_CHAIN (parms) != NULL_TREE)
1212 xxref_init_possible = 0;
1213 else
1215 xxref_init_possible = LOOKUP_SPECULATIVELY;
1216 flags &= ~LOOKUP_COMPLAIN;
1219 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1221 assert (TYPE_USES_VIRTUAL_BASECLASSES (for_type));
1222 if (true_exp == exp)
1223 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1224 else
1225 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1226 flags |= LOOKUP_HAS_IN_CHARGE;
1228 rval = build_method_call (exp, DECL_NAME (TYPE_NAME (type)), parms,
1229 CLASSTYPE_AS_LIST (for_type), flags|xxref_init_possible);
1230 if (rval == NULL_TREE && xxref_init_possible)
1232 tree init_type = TREE_TYPE (init);
1233 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1234 init_type = TREE_TYPE (init_type);
1235 if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type)
1236 || (IS_AGGR_TYPE (init_type)
1237 && get_base_type (type, init_type, 0)))
1239 if (type == for_type
1240 && TYPE_USES_VIRTUAL_BASECLASSES (type))
1242 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1243 expand_aggr_vbase_init (type, exp, addr, NULL_TREE);
1245 expand_expr_stmt (build_vbase_vtables_init (type, type, exp, addr, 1));
1246 #ifdef sparc
1247 expand_asm_operands (build_string (32, "! end of vtable initialization"), 0, 0, 0, 0, input_filename, lineno);
1248 #endif
1250 expand_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
1251 return;
1253 else
1254 rval = build_method_call (exp, DECL_NAME (TYPE_NAME (type)), parms,
1255 CLASSTYPE_AS_LIST (for_type), flags);
1258 /* Private, protected, or otherwise unavailable. */
1259 if (rval == error_mark_node && (flags&LOOKUP_COMPLAIN))
1260 error_with_aggr_type (for_type, "in base initialization for class `%s'");
1261 /* A valid initialization using constructor. */
1262 else if (rval != error_mark_node && rval != NULL_TREE)
1264 /* p. 222: if the base class assigns to `this', then that
1265 value is used in the derived class. */
1266 if ((flag_this_is_variable & 1) && alias_this)
1268 TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
1269 expand_assignment (current_class_decl, rval, 0, 0);
1271 else
1272 expand_expr_stmt (rval);
1274 else if (parms && TREE_CHAIN (parms) == NULL_TREE)
1276 /* If we are initializing one aggregate value
1277 from another, and though there are constructors,
1278 and none accept the initializer, just do a bitwise
1279 copy.
1281 @@ This should reject initializer which a constructor
1282 @@ rejected on visibility gounds, but there is
1283 @@ no way right now to recognize that case with
1284 @@ just `error_mark_node'. */
1285 tree itype;
1286 init = TREE_VALUE (parms);
1287 itype = TREE_TYPE (init);
1288 if (TREE_CODE (itype) == REFERENCE_TYPE)
1290 init = convert_from_reference (init);
1291 itype = TREE_TYPE (init);
1293 itype = TYPE_MAIN_VARIANT (itype);
1294 if (comptypes (TYPE_MAIN_VARIANT (type), itype, 0))
1296 warning ("bitwise copy in initialization of type `%s'",
1297 TYPE_NAME_STRING (type));
1298 rval = build (INIT_EXPR, type, exp, init);
1299 expand_expr_stmt (rval);
1301 else
1303 error_with_aggr_type (for_type, "in base initialization for class `%s',");
1304 error_with_aggr_type (type, "invalid initializer to constructor for type `%s'");
1305 return;
1308 else
1310 if (init == NULL_TREE)
1311 assert (parms == NULL_TREE);
1312 if (parms == NULL_TREE && TREE_VIA_VIRTUAL (for_type))
1313 error_with_aggr_type (for_type, "virtual baseclass `%s' does not have default initializer");
1314 else
1316 error_with_aggr_type (for_type, "in base initialization for class `%s',");
1317 /* This will make an error message for us. */
1318 build_method_call (exp, DECL_NAME (TYPE_NAME (type)), parms,
1319 CLASSTYPE_AS_LIST (for_type),
1320 (TYPE_USES_VIRTUAL_BASECLASSES (type)
1321 ? LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE
1322 : LOOKUP_NORMAL));
1324 return;
1326 /* Constructor has been called, but vtables may be for TYPE
1327 rather than for FOR_TYPE. */
1329 else if (TREE_CODE (type) == ARRAY_TYPE)
1331 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1332 expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
1333 else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
1334 sorry ("arrays of objects with virtual functions but no constructors");
1336 else
1337 expand_recursive_init (for_type, true_exp, exp, init, CLASSTYPE_BASE_INIT_LIST (type), alias_this);
1340 /* A pointer which holds the initializer. First call to
1341 expand_aggr_init gets this value pointed to, and sets it to init_null. */
1342 static tree *init_ptr, init_null;
1344 /* Subroutine of expand_recursive_init:
1346 ADDR is the address of the expression being initialized.
1347 INIT_LIST is the cons-list of initializations to be performed.
1348 ALIAS_THIS is its same, lovable self. */
1349 static void
1350 expand_recursive_init_1 (for_type, true_exp, addr, init_list, alias_this)
1351 tree for_type, true_exp, addr;
1352 tree init_list;
1353 int alias_this;
1355 while (init_list)
1357 if (TREE_PURPOSE (init_list))
1359 if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
1361 tree member = TREE_PURPOSE (init_list);
1362 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), 0);
1363 tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
1364 if (IS_AGGR_TYPE (TREE_TYPE (member)))
1365 expand_aggr_init (member_base, DECL_INITIAL (member), 0);
1366 else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
1367 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
1369 member_base = save_expr (default_conversion (member_base));
1370 expand_vec_init (member, member_base,
1371 array_type_nelts (TREE_TYPE (member)),
1372 DECL_INITIAL (member), 0);
1374 else expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
1376 else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
1378 expand_recursive_init_1 (for_type, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
1379 expand_recursive_init_1 (for_type, true_exp, addr, TREE_VALUE (init_list), alias_this);
1381 else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
1383 /* Only initialize the virtual function tables if we
1384 are initializing the ultimate users of those vtables. */
1385 if (TREE_VALUE (init_list))
1387 expand_expr_stmt (build_virtual_init (for_type, TREE_VALUE (init_list), addr));
1388 if (TREE_VALUE (init_list) == for_type
1389 && TYPE_USES_VIRTUAL_BASECLASSES (for_type))
1390 expand_expr_stmt (build_vbase_vtables_init (for_type, TREE_VALUE (init_list), true_exp, addr, 1));
1391 #ifdef sparc
1392 expand_asm_operands (build_string (32, "! end of vtable initialization"), 0, 0, 0, 0, input_filename, lineno);
1393 #endif
1396 else abort ();
1398 else if (TREE_VALUE (init_list)
1399 && TREE_CODE (TREE_VALUE (init_list)) == RECORD_TYPE)
1401 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), 0);
1402 expand_aggr_init_1 (for_type, true_exp, subexp, *init_ptr,
1403 alias_this && ! DECL_OFFSET (TYPE_NAME (TREE_VALUE (init_list))),
1404 LOOKUP_PROTECTED_OK|LOOKUP_COMPLAIN);
1406 /* INIT_PTR is used up. */
1407 init_ptr = &init_null;
1409 else
1410 abort ();
1411 init_list = TREE_CHAIN (init_list);
1415 /* Initialize EXP with INIT. Type EXP does not have a constructor,
1416 but it has a baseclass with a constructor or a virtual function
1417 table which needs initializing.
1419 INIT_LIST is a cons-list describing what parts of EXP actually
1420 need to be initialized. INIT is given to the *unique*, first
1421 constructor within INIT_LIST. If there are multiple first
1422 constructors, such as with multiple inheritance, INIT must
1423 be zero or an ambiguity error is reported.
1425 ALIAS_THIS is passed from `expand_aggr_init'. See comments
1426 there. */
1428 static void
1429 expand_recursive_init (for_type, true_exp, exp, init, init_list, alias_this)
1430 tree for_type, true_exp, exp, init;
1431 tree init_list;
1432 int alias_this;
1434 tree *old_init_ptr = init_ptr;
1435 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1436 init_ptr = &init;
1438 if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (for_type))
1440 expand_aggr_vbase_init (for_type, exp, addr, init_list);
1441 expand_expr_stmt (build_vbase_vtables_init (for_type, for_type, true_exp, addr, 1));
1443 expand_recursive_init_1 (for_type, true_exp, addr, init_list, alias_this);
1445 if (*init_ptr)
1447 tree type = TREE_TYPE (exp);
1449 if (TREE_CODE (type) == REFERENCE_TYPE)
1450 type = TREE_TYPE (type);
1451 if (IS_AGGR_TYPE (type))
1452 error_with_aggr_type (type, "unexpected argument to constructor `%s'");
1453 else
1454 error ("unexpected argument to constructor");
1456 init_ptr = old_init_ptr;
1459 /* Report an error if NAME is not the name of a user-defined,
1460 aggregate type. If OR_ELSE is nonzero, give an error message. */
1462 is_aggr_typedef (name, or_else)
1463 tree name;
1465 tree type = TREE_TYPE (name);
1467 if (type == NULL_TREE || TREE_CODE (type) != TYPE_DECL)
1469 if (or_else)
1470 error ("`%s' fails to be an aggregate typedef",
1471 IDENTIFIER_POINTER (name));
1472 return 0;
1474 type = TREE_TYPE (type);
1475 if (! IS_AGGR_TYPE (type))
1477 fatal ("type `%s' is of non-aggregate type",
1478 IDENTIFIER_POINTER (name));
1479 return 0;
1481 return 1;
1484 /* This code could just as well go in `cplus-class.c', but is placed here for
1485 modularity. */
1487 /* For an expression of the form CNAME :: NAME (PARMLIST), build
1488 the appropriate function call. */
1489 tree
1490 build_member_call (cname, name, parmlist)
1491 tree cname, name, parmlist;
1493 tree type, t;
1494 tree method_name = name;
1495 int dtor = 0;
1496 int dont_use_this = 0;
1497 tree basetype_path, decl;
1499 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1501 method_name = TREE_OPERAND (method_name, 0);
1502 dtor = 1;
1505 if (TREE_CODE (cname) == SCOPE_REF)
1507 sorry ("multiple scope qualifications in build_member_call");
1508 return error_mark_node;
1511 if (! is_aggr_typedef (cname, 1))
1512 return error_mark_node;
1514 /* An operator we did not like. */
1515 if (name == NULL_TREE)
1516 return error_mark_node;
1518 if (dtor)
1520 if (! TYPE_HAS_DESTRUCTOR (TREE_TYPE (TREE_TYPE (cname))))
1521 error ("type `%s' does not have a destructor",
1522 IDENTIFIER_POINTER (cname));
1523 else
1524 error ("destructor specification error");
1525 return error_mark_node;
1528 if (TREE_CODE (name) == OP_IDENTIFIER)
1529 method_name = build_operator_fnname (&name, parmlist, 1);
1530 type = TREE_TYPE (TREE_TYPE (cname));
1532 /* No object? Then just fake one up, and let build_method_call
1533 figure out what to do. */
1534 if (current_class_type == 0
1535 || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
1536 dont_use_this = 1;
1538 if (dont_use_this)
1540 basetype_path = NULL_TREE;
1541 decl = build1 (NOP_EXPR,
1542 TYPE_POINTER_TO (TREE_TYPE (TREE_TYPE (cname))),
1543 error_mark_node);
1545 else if (current_class_decl == 0)
1547 dont_use_this = 1;
1548 decl = build1 (NOP_EXPR,
1549 TYPE_POINTER_TO (TREE_TYPE (TREE_TYPE (cname))),
1550 error_mark_node);
1552 else decl = current_class_decl;
1554 if (t = lookup_fnfields (CLASSTYPE_AS_LIST (type), method_name, 0))
1555 return build_method_call (decl, method_name, parmlist, basetype_path,
1556 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1557 if (TREE_CODE (name) == IDENTIFIER_NODE
1558 && (t = lookup_field (CLASSTYPE_AS_LIST (type), name, 1)))
1560 if (t == error_mark_node)
1561 return error_mark_node;
1562 if (TREE_CODE (t) == FIELD_DECL)
1564 if (dont_use_this)
1566 error ("invalid use of non-static field `%s'",
1567 IDENTIFIER_POINTER (name));
1568 return error_mark_node;
1570 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1572 else if (TREE_CODE (t) == VAR_DECL)
1573 decl = t;
1574 else
1576 error ("invalid use of member `%s::%s'",
1577 IDENTIFIER_POINTER (cname), name);
1578 return error_mark_node;
1580 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist);
1582 else
1584 char *err_name;
1585 if (TREE_CODE (name) == OP_IDENTIFIER)
1587 char *op_name = operator_name_string (method_name);
1588 err_name = (char *)alloca (13 + strlen (op_name));
1589 sprintf (err_name, "operator %s", op_name);
1591 else if (TREE_CODE (name) == IDENTIFIER_NODE)
1592 err_name = IDENTIFIER_POINTER (name);
1593 else
1594 abort ();
1596 error ("no method `%s::%s'", IDENTIFIER_POINTER (cname), err_name);
1597 return error_mark_node;
1601 /* Build a reference to a member of an aggregate. This is not a
1602 C++ `&', but really something which can have its address taken,
1603 and then act as a pointer to member, for example CNAME :: FIELD
1604 can have its address taken by saying & CNAME :: FIELD.
1606 @@ Prints out lousy diagnostics for operator <typename>
1607 @@ fields.
1609 @@ This function should be rewritten and placed in cplus-search.c. */
1610 tree
1611 build_offset_ref (cname, name)
1612 tree cname, name;
1614 tree decl, type, fnfields, fields, t = error_mark_node;
1615 tree basetypes = NULL_TREE;
1616 int dtor = 0;
1618 if (TREE_CODE (cname) == SCOPE_REF)
1620 sorry ("multiple scope qualifications in build_offset_ref");
1621 return error_mark_node;
1624 if (! is_aggr_typedef (cname, 1))
1625 return error_mark_node;
1627 type = TREE_TYPE (TREE_TYPE (cname));
1629 if (TREE_CODE (name) == BIT_NOT_EXPR)
1631 dtor = 1;
1632 name = TREE_OPERAND (name, 0);
1635 if (TYPE_SIZE (type) == 0)
1637 t = IDENTIFIER_CLASS_VALUE (name);
1638 if (t == 0)
1640 error_with_aggr_type (type, "incomplete type `%s' does not have member `%s'", IDENTIFIER_POINTER (name));
1641 return error_mark_node;
1643 if (TREE_CODE (t) == TYPE_DECL)
1645 error_with_decl (t, "member `%s' is just a type declaration");
1646 return error_mark_node;
1648 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1650 TREE_USED (t) = 1;
1651 return t;
1653 if (TREE_CODE (t) == FIELD_DECL)
1654 sorry ("use of member in incomplete aggregate type");
1655 else if (TREE_CODE (t) == FUNCTION_DECL)
1656 sorry ("use of member function in incomplete aggregate type");
1657 else
1658 abort ();
1659 return error_mark_node;
1662 /* Unresolved multi-arity operator. */
1663 if (TREE_CODE (name) == OP_IDENTIFIER)
1665 t = copy_node (name);
1666 TREE_TYPE (t) = unknown_type_node;
1667 return t;
1669 if (TREE_CODE (name) == TYPE_EXPR)
1670 /* Pass a TYPE_DECL to build_component_type_expr. */
1671 return build_component_type_expr (TREE_TYPE (cname), name, NULL_TREE, 1);
1673 fnfields = lookup_fnfields (CLASSTYPE_AS_LIST (type), name, 0);
1674 fields = lookup_field (type, name, 0);
1676 if (fields == error_mark_node)
1677 return error_mark_node;
1679 if (fnfields)
1681 basetypes = TREE_PURPOSE (fnfields);
1683 /* Go from the TREE_BASELINK to the member function info. */
1684 t = TREE_VALUE (fnfields);
1686 if (fields)
1688 if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
1690 error ("ambiguous member reference: member `%s' defined as both field and function",
1691 IDENTIFIER_POINTER (name));
1692 return error_mark_node;
1694 if (get_base_type (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t), 0))
1696 else if (get_base_type (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields), 0))
1697 t = fields;
1698 else
1700 error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
1701 return error_mark_node;
1705 if (t == TREE_VALUE (fnfields))
1707 extern int flag_save_memoized_contexts;
1709 /* This does not handle visibility checking yet. */
1710 if (TREE_CHAIN (t) == NULL_TREE || dtor)
1712 enum visibility_type visibility;
1714 /* unique functions are handled easily. */
1715 unique:
1716 visibility = compute_visibility (basetypes, t);
1717 if (visibility == visibility_protected)
1719 error_with_decl (t, "member function `%s' is protected");
1720 error ("in this context");
1721 return error_mark_node;
1723 if (visibility == visibility_private)
1725 error_with_decl (t, "member function `%s' is private");
1726 error ("in this context");
1727 return error_mark_node;
1729 return build (OFFSET_REF, TREE_TYPE (t), NULL_TREE, t);
1732 /* overloaded functions may need more work. */
1733 if (cname == name)
1735 if (TYPE_HAS_DESTRUCTOR (type)
1736 && TREE_CHAIN (TREE_CHAIN (t)) == NULL_TREE)
1738 t = TREE_CHAIN (t);
1739 goto unique;
1742 if (flag_save_memoized_contexts
1743 && !TREE_PERMANENT (fnfields)
1744 && global_bindings_p ())
1745 fnfields = copy_list (fnfields);
1746 t = build_tree_list (error_mark_node, fnfields);
1747 TREE_TYPE (t) = build_member_type (type, unknown_type_node);
1748 return t;
1752 /* Now that we know we are looking for a field, see if we
1753 have access to that field. Lookup_field will give us the
1754 error message. */
1756 if (current_class_type == 0
1757 || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
1759 basetypes = CLASSTYPE_AS_LIST (type);
1760 decl = build1 (NOP_EXPR,
1761 TREE_TYPE (TREE_TYPE (cname)),
1762 error_mark_node);
1764 else if (current_class_decl == 0)
1765 decl = build1 (NOP_EXPR, TREE_TYPE (TREE_TYPE (cname)),
1766 error_mark_node);
1767 else decl = C_C_D;
1769 t = lookup_field (basetypes, name, 1);
1771 if (t == error_mark_node)
1772 return error_mark_node;
1774 if (t == NULL_TREE)
1776 if (OPERATOR_TYPENAME_P (name))
1777 error ("type conversion operator not a member of type `%s'",
1778 IDENTIFIER_POINTER (cname));
1779 else
1780 error ("field `%s' is not a member of type `%s'",
1781 IDENTIFIER_POINTER (name),
1782 IDENTIFIER_POINTER (cname));
1783 return error_mark_node;
1786 if (TREE_CODE (t) == TYPE_DECL)
1788 error_with_decl (t, "member `%s' is just a type declaration");
1789 return error_mark_node;
1791 /* static class members and class-specific enum
1792 values can be returned without further ado. */
1793 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1795 TREE_USED (t) = 1;
1796 return t;
1799 /* static class functions too. */
1800 if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1801 abort ();
1803 /* In member functions, the form `cname::name' is no longer
1804 equivalent to `this->cname::name'. */
1805 return build (OFFSET_REF, build_member_type (type, TREE_TYPE (t)), decl, t);
1808 /* Given an object EXP and a member function reference MEMBER,
1809 return the address of the actual member function. */
1810 tree
1811 get_member_function (exp_addr_ptr, exp, member)
1812 tree *exp_addr_ptr;
1813 tree exp, member;
1815 tree ctype = TREE_TYPE (exp);
1816 tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
1818 if (TYPE_VIRTUAL_P (ctype)
1819 || (flag_all_virtual == 1
1820 && (TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)
1821 || TYPE_NEEDS_WRAPPER (ctype))))
1823 tree e0, e1, e3;
1824 tree exp_addr;
1826 /* Save away the unadulterated `this' pointer. */
1827 exp_addr = save_expr (*exp_addr_ptr);
1829 /* Cast function to signed integer. */
1830 e0 = build1 (NOP_EXPR, integer_type_node, function);
1832 #ifdef VTABLE_USES_MASK
1833 /* If we are willing to limit the number of
1834 virtual functions a class may have to some
1835 *small* number, then if, for a function address,
1836 we are passed some small number, we know that
1837 it is a virtual function index, and work from there. */
1838 e1 = build (BIT_AND_EXPR, integer_type_node, e0, vtbl_mask);
1839 #else
1840 /* There is a hack here that takes advantage of
1841 twos complement arithmetic, and the fact that
1842 there are more than one UNITS to the WORD.
1843 If the high bit is set for the `function',
1844 then we pretend it is a virtual function,
1845 and the array indexing will knock this bit
1846 out the top, leaving a valid index. */
1847 #if UNITS_PER_WORD <= 1
1848 virtual_functions_lose !;
1849 #endif
1850 e1 = build (GT_EXPR, integer_type_node, e0, integer_zero_node);
1851 e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
1852 build_tree_list (NULL_TREE, e1)));
1853 e1 = save_expr (e1);
1854 #endif
1856 if (TREE_VOLATILE (*exp_addr_ptr))
1858 exp = build_indirect_ref (exp_addr, 0);
1859 *exp_addr_ptr = exp_addr;
1862 /* This is really hairy: if the function pointer is a pointer
1863 to a non-virtual member function, then we can't go mucking
1864 with the `this' pointer (any more than we aleady have to
1865 this point). If it is a pointer to a virtual member function,
1866 then we have to adjust the `this' pointer according to
1867 what the virtual function table tells us. */
1869 e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
1870 assert (e3 != error_mark_node);
1872 /* Change this pointer type from `void *' to the
1873 type it is really supposed to be. */
1874 TREE_TYPE (e3) = TREE_TYPE (function);
1876 /* If non-virtual, use what we had originally. Otherwise,
1877 use the value we get from the virtual function table. */
1878 *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
1880 function = build_conditional_expr (e1, function, e3);
1882 return build_indirect_ref (function, 0);
1885 /* If a OFFSET_REF made it through to here, then it did
1886 not have its address taken. */
1888 tree
1889 resolve_offset_ref (exp)
1890 tree exp;
1892 tree base;
1893 tree member;
1894 tree basetype, addr;
1896 if (TREE_CODE (exp) == TREE_LIST)
1897 return build_unary_op (ADDR_EXPR, exp, 0);
1899 assert (TREE_CODE (exp) == OFFSET_REF);
1900 member = TREE_OPERAND (exp, 1);
1901 if (TREE_STATIC (member))
1903 /* These were static members. */
1904 if (mark_addressable (member) == 0)
1905 return error_mark_node;
1906 return member;
1909 /* Syntax error can cause a member which should
1910 have been seen as static to be grok'd as non-static. */
1911 if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
1913 if (TREE_ADDRESSABLE (member) == 0)
1915 error_with_decl (member, "member `%s' is non-static in static member function context");
1916 TREE_ADDRESSABLE (member) = 1;
1918 return error_mark_node;
1921 base = TREE_OPERAND (exp, 0);
1922 assert (base != NULL_TREE);
1924 /* The first case is really just a reference to a member of `this'. */
1925 if (TREE_CODE (member) == FIELD_DECL
1926 && (base == C_C_D
1927 || (TREE_CODE (base) == NOP_EXPR
1928 && TREE_OPERAND (base, 0) == error_mark_node)))
1930 tree basetype_path;
1931 enum visibility_type visibility;
1933 basetype = DECL_CONTEXT (member);
1934 if (get_base_distance (basetype, current_class_type, 0, &basetype_path) < 0)
1936 error_not_base_type (basetype, current_class_type);
1937 return error_mark_node;
1939 addr = convert_pointer_to (basetype, current_class_decl);
1940 visibility = compute_visibility (basetype_path, member);
1941 if (visibility == visibility_public)
1942 return build (COMPONENT_REF, TREE_TYPE (member),
1943 build_indirect_ref (addr, 0), member);
1944 if (visibility == visibility_protected)
1946 error_with_decl ("member `%s' is protected");
1947 error ("in this context");
1948 return error_mark_node;
1950 if (visibility == visibility_private)
1952 error_with_decl ("member `%s' is private");
1953 error ("in this context");
1954 return error_mark_node;
1956 abort ();
1958 /* If this is a reference to a member function, then return
1959 the address of the member function (which may involve going
1960 through the object's vtable), otherwise, return an expression
1961 for the derefernced pointer-to-member construct. */
1962 addr = build_unary_op (ADDR_EXPR, base, 0);
1963 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1965 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (member));
1966 addr = convert_pointer_to (basetype, addr);
1967 return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, 0), member), 0);
1969 else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
1971 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
1972 addr = convert_pointer_to (basetype, addr);
1973 member = convert (ptr_type_node, build_unary_op (ADDR_EXPR, member, 0));
1974 return build1 (INDIRECT_REF, TREE_TYPE (exp),
1975 build (PLUS_EXPR, ptr_type_node, addr, member));
1977 abort ();
1980 /* Return either DECL or its known constant value (if it has one). */
1982 tree
1983 decl_constant_value (decl)
1984 tree decl;
1986 if (
1987 #if 0
1988 /* These may be necessary for C, but they break C++. */
1989 ! TREE_PUBLIC (decl)
1990 /* Don't change a variable array bound or initial value to a constant
1991 in a place where a variable is invalid. */
1992 && current_function_decl != 0
1993 && ! pedantic
1995 #endif /* 0 */
1996 ! TREE_THIS_VOLATILE (decl)
1997 && DECL_INITIAL (decl) != 0
1998 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1999 /* This is invalid if initial value is not constant.
2000 If it has either a function call, a memory reference,
2001 or a variable, then re-evaluating it could give different results. */
2002 && TREE_LITERAL (DECL_INITIAL (decl))
2003 #if 0
2004 /* Check for cases where this is sub-optimal, even though valid. */
2005 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
2006 && DECL_MODE (decl) != BLKmode
2007 #endif
2009 return DECL_INITIAL (decl);
2010 return decl;
2013 /* Friend handling routines. */
2014 /* Friend data structures:
2016 Friend lists come from TYPE_DECL nodes. Since all aggregate
2017 types are automatically typedef'd, these node are guaranteed
2018 to exist.
2020 The TREE_PURPOSE of a friend list is the name of the friend,
2021 and its TREE_VALUE is another list.
2023 The TREE_PURPOSE of that list is a type, which allows
2024 all functions of a given type to be friends.
2025 The TREE_VALUE of that list is an individual function
2026 which is a friend.
2028 Non-member friends will match only by their DECL. Their
2029 member type is NULL_TREE, while the type of the inner
2030 list will either be of aggregate type or error_mark_node. */
2032 /* Tell if this function specified by FUNCTION_DECL
2033 can be a friend of type TYPE.
2034 Return nonzero if friend, zero otherwise.
2036 DECL can be zero if we are calling a constructor or accessing a
2037 member in global scope. */
2039 is_friend (type, decl)
2040 tree type, decl;
2042 tree typedecl = TYPE_NAME (type);
2043 tree ctype = NULL_TREE;
2044 tree list;
2045 tree name;
2047 if (decl == NULL_TREE)
2048 return 0;
2050 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
2051 ctype = TYPE_METHOD_BASETYPE (TREE_TYPE (decl));
2052 else if (DECL_STATIC_FUNCTION_P (decl))
2053 ctype = DECL_CONTEXT (decl);
2054 if (ctype)
2056 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (typedecl));
2057 while (list)
2059 if (ctype == TREE_VALUE (list))
2060 return 1;
2061 list = TREE_CHAIN (list);
2065 list = DECL_FRIENDLIST (typedecl);
2066 name = DECL_ORIGINAL_NAME (decl);
2067 while (list)
2069 if (name == TREE_PURPOSE (list))
2071 tree friends = TREE_VALUE (list);
2072 name = DECL_NAME (decl);
2073 while (friends)
2075 if (ctype == TREE_PURPOSE (friends))
2076 return 1;
2077 if (name == DECL_NAME (TREE_VALUE (friends)))
2078 return 1;
2079 friends = TREE_CHAIN (friends);
2081 return 0;
2083 list = TREE_CHAIN (list);
2085 return 0;
2088 /* Add a new friend to the friends of the aggregate type TYPE.
2089 DECL is the FUNCTION_DECL of the friend being added. */
2090 static void
2091 add_friend (type, decl)
2092 tree type, decl;
2094 tree typedecl = TYPE_NAME (type);
2095 tree list = DECL_FRIENDLIST (typedecl);
2096 tree name = DECL_ORIGINAL_NAME (decl);
2097 tree ctype = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
2098 ? TYPE_METHOD_BASETYPE (TREE_TYPE (decl)) : error_mark_node;
2100 while (list)
2102 if (name == TREE_PURPOSE (list))
2104 tree friends = TREE_VALUE (list);
2105 while (friends)
2107 if (decl == TREE_VALUE (friends))
2109 warning_with_decl (decl, "`%s' is already a friend of class `%s'", IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2110 return;
2112 friends = TREE_CHAIN (friends);
2114 TREE_VALUE (list) = tree_cons (ctype, decl, TREE_VALUE (list));
2115 return;
2117 list = TREE_CHAIN (list);
2119 DECL_FRIENDLIST (typedecl)
2120 = tree_cons (DECL_ORIGINAL_NAME (decl),
2121 build_tree_list (error_mark_node, decl),
2122 DECL_FRIENDLIST (typedecl));
2123 if (! strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)),
2124 OPERATOR_MODIFY_FORMAT,
2125 OPERATOR_MODIFY_LENGTH))
2127 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
2128 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2129 TYPE_GETS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2130 if (parmtypes && TREE_CHAIN (parmtypes))
2132 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
2133 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2134 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
2136 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2137 TYPE_GETS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2143 /* Declare that every member function NAME in FRIEND_TYPE
2144 (which may be NULL_TREE) is a friend of type TYPE. */
2145 static void
2146 add_friends (type, name, friend_type)
2147 tree type, name, friend_type;
2149 tree typedecl = TYPE_NAME (type);
2150 tree list = DECL_FRIENDLIST (typedecl);
2152 while (list)
2154 if (name == TREE_PURPOSE (list))
2156 tree friends = TREE_VALUE (list);
2157 while (friends && TREE_PURPOSE (friends) != friend_type)
2158 friends = TREE_CHAIN (friends);
2159 if (friends)
2160 if (friend_type)
2161 warning ("method `%s::%s' is already a friend of class",
2162 TYPE_NAME_STRING (friend_type),
2163 IDENTIFIER_POINTER (name));
2164 else
2165 warning ("function `%s' is already a friend of class `%s'",
2166 IDENTIFIER_POINTER (name),
2167 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2168 else
2169 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
2170 TREE_VALUE (list));
2171 return;
2173 list = TREE_CHAIN (list);
2175 DECL_FRIENDLIST (typedecl) =
2176 tree_cons (name,
2177 build_tree_list (friend_type, NULL_TREE),
2178 DECL_FRIENDLIST (typedecl));
2179 if (! strncmp (name, OPERATOR_MODIFY_FORMAT, OPERATOR_MODIFY_LENGTH))
2181 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2182 TYPE_GETS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2183 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
2187 /* Set up a cross reference so that type TYPE will
2188 make member function CTYPE::DECL a friend when CTYPE
2189 is finally defined. */
2190 void
2191 xref_friend (type, decl, ctype)
2192 tree type, decl, ctype;
2194 tree typedecl = TYPE_NAME (type);
2195 tree friend_decl = TYPE_NAME (ctype);
2196 tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
2198 DECL_UNDEFINED_FRIENDS (typedecl) = t;
2199 SET_DECL_WAITING_FRIENDS (friend_decl, tree_cons (type, t, DECL_WAITING_FRIENDS (friend_decl)));
2200 TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
2203 /* Set up a cross reference so that functions with name NAME and
2204 type CTYPE know that they are friends of TYPE. */
2205 void
2206 xref_friends (type, name, ctype)
2207 tree type, name, ctype;
2209 tree typedecl = TYPE_NAME (type);
2210 tree friend_decl = TYPE_NAME (ctype);
2211 tree t = tree_cons (NULL_TREE, ctype,
2212 DECL_UNDEFINED_FRIENDS (typedecl));
2214 DECL_UNDEFINED_FRIENDS (typedecl) = t;
2215 SET_DECL_WAITING_FRIENDS (friend_decl, tree_cons (type, t, DECL_WAITING_FRIENDS (friend_decl)));
2216 TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = name;
2219 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
2220 been defined, we make all of its member functions friends of
2221 TYPE. If not, we make it a pending friend, which can later be added
2222 when its definition is seen. If a type is defined, then its TYPE_DECL's
2223 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
2224 classes that are not defined. If a type has not yet been defined,
2225 then the DECL_WAITING_FRIENDS contains a list of types
2226 waiting to make it their friend. Note that these two can both
2227 be in use at the same time! */
2228 void
2229 make_friend_class (type, friend_type)
2230 tree type, friend_type;
2232 tree classes;
2234 if (type == friend_type)
2236 warning ("class `%s' is implicitly friends with itself",
2237 TYPE_NAME_STRING (type));
2238 return;
2241 #ifdef FIELD_XREF
2242 FIELD_xref_hier(TYPE_NAME_STRING(type),
2243 TYPE_NAME_STRING(friend_type),0,0,1);
2244 #endif
2246 classes = CLASSTYPE_FRIEND_CLASSES (type);
2247 while (classes && TREE_VALUE (classes) != friend_type)
2248 classes = TREE_CHAIN (classes);
2249 if (classes)
2250 warning ("class `%s' is already friends with class `%s'",
2251 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
2252 else
2254 CLASSTYPE_FRIEND_CLASSES (type)
2255 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
2259 /* Main friend processor. This is large, and for modularity purposes,
2260 has been removed from grokdeclarator. It returns `void_type_node'
2261 to indicate that something happened, though a FIELD_DECL is
2262 not returned.
2264 CTYPE is the class this friend belongs to.
2266 DECLARATOR is the name of the friend.
2268 DECL is the FUNCTION_DECL that the friend is.
2270 In case we are parsing a friend which is part of an inline
2271 definition, we will need to store PARM_DECL chain that comes
2272 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
2274 FLAGS is just used for `grokclassfn'.
2276 QUALS say what special qualifies should apply to the object
2277 pointed to by `this'. */
2278 tree
2279 do_friend (ctype, declarator, decl, parmdecls, flags, quals)
2280 tree ctype, declarator, decl, parmdecls;
2281 enum overload_flags flags;
2282 tree quals;
2284 if (ctype)
2286 tree cname = TYPE_NAME (ctype);
2287 if (TREE_CODE (cname) == TYPE_DECL)
2288 cname = DECL_NAME (cname);
2290 /* A method friend. */
2291 if (TREE_CODE (decl) == FUNCTION_DECL)
2293 if (flags == NO_SPECIAL && ctype && declarator == cname)
2294 DECL_CONSTRUCTOR_P (decl) = 1;
2296 /* This will set up DECL_ARGUMENTS for us. */
2297 grokclassfn (ctype, cname, decl, flags, TYPE_SIZE (ctype) != 0, quals);
2298 if (TREE_TYPE (decl) != error_mark_node)
2300 if (TYPE_SIZE (ctype))
2302 /* We don't call pushdecl here yet, or ever on this
2303 actual FUNCTION_DECL. We must preserve its TREE_CHAIN
2304 until the end. */
2305 make_decl_rtl (decl, NULL_TREE, 1);
2306 add_friend (current_class_type, decl);
2308 else
2309 xref_friend (current_class_type, decl, ctype);
2310 DECL_FRIEND_P (decl) = 1;
2313 else
2315 /* Possibly a bunch of method friends. */
2317 /* Get the class they belong to. */
2318 tree ctype = TREE_TYPE (TREE_TYPE (cname));
2320 /* This class is defined, use its methods now. */
2321 if (TYPE_SIZE (ctype))
2323 tree fields = lookup_fnfields (CLASSTYPE_AS_LIST (ctype), declarator, 0);
2324 if (fields)
2325 add_friends (current_class_type, declarator, ctype);
2326 else
2327 error ("method `%s' is not a member of class `%s'",
2328 IDENTIFIER_POINTER (declarator),
2329 IDENTIFIER_POINTER (cname));
2331 else
2332 xref_friends (current_class_type, declarator, ctype);
2333 decl = void_type_node;
2336 else if (TREE_CODE (decl) == FUNCTION_DECL
2337 && ((IDENTIFIER_LENGTH (declarator) == 4
2338 && IDENTIFIER_POINTER (declarator)[0] == 'm'
2339 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2340 || (IDENTIFIER_LENGTH (declarator) > 10
2341 && IDENTIFIER_POINTER (declarator)[0] == '_'
2342 && IDENTIFIER_POINTER (declarator)[1] == '_'
2343 && strncmp (IDENTIFIER_POINTER (declarator)+2,
2344 "builtin_", 8) == 0)))
2346 /* raw "main", and builtin functions never gets overloaded,
2347 but they can become friends. */
2348 TREE_PUBLIC (decl) = 1;
2349 add_friend (current_class_type, decl);
2350 DECL_FRIEND_P (decl) = 1;
2351 if (IDENTIFIER_POINTER (declarator)[0] == '_')
2353 if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "new"))
2354 TREE_GETS_NEW (current_class_type) = 0;
2355 else if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "delete"))
2356 TREE_GETS_DELETE (current_class_type) = 0;
2358 decl = void_type_node;
2360 /* A global friend.
2361 @@ or possibly a friend from a base class ?!? */
2362 else if (TREE_CODE (decl) == FUNCTION_DECL)
2364 /* Friends must all go through the overload machinery,
2365 even though they may not technically be overloaded.
2367 Note that because classes all wind up being top-level
2368 in their scope, their friend wind up in top-level scope as well. */
2369 DECL_NAME (decl) =
2370 build_decl_overload (IDENTIFIER_POINTER (declarator),
2371 TYPE_ARG_TYPES (TREE_TYPE (decl)),
2372 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
2373 DECL_ASSEMBLER_NAME (decl) = IDENTIFIER_POINTER (DECL_NAME (decl));
2374 DECL_ARGUMENTS (decl) = parmdecls;
2375 /* We can call pushdecl here, because the TREE_CHAIN of this
2376 FUNCTION_DECL is not needed for other purposes. */
2377 decl = pushdecl_top_level (decl);
2379 make_decl_rtl (decl, NULL_TREE, 1);
2380 add_friend (current_class_type, decl);
2382 if (! TREE_OVERLOADED (declarator)
2383 && IDENTIFIER_GLOBAL_VALUE (declarator)
2384 && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (declarator)) == FUNCTION_DECL)
2386 error ("friend `%s' implicitly overloaded",
2387 IDENTIFIER_POINTER (declarator));
2388 error_with_decl (IDENTIFIER_GLOBAL_VALUE (declarator),
2389 "after declaration of non-overloaded `%s'");
2391 DECL_FRIEND_P (decl) = 1;
2392 DECL_OVERLOADED (decl) = 1;
2393 TREE_OVERLOADED (declarator) = 1;
2394 push_overloaded_decl (decl);
2396 else
2398 /* @@ Should be able to ingest later definitions of this function
2399 before use. */
2400 tree decl = IDENTIFIER_GLOBAL_VALUE (declarator);
2401 if (decl == NULL_TREE)
2403 warning ("implicitly declaring `%s' as struct",
2404 IDENTIFIER_POINTER (declarator));
2405 decl = xref_tag (record_type_node, declarator, NULL_TREE);
2406 decl = TYPE_NAME (decl);
2409 /* Allow abbreviated declarations of overloaded functions,
2410 but not if those functions are really class names. */
2411 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
2413 warning ("`friend %s' archaic, use `friend class %s' instead",
2414 IDENTIFIER_POINTER (declarator),
2415 IDENTIFIER_POINTER (declarator));
2416 decl = TREE_TYPE (TREE_PURPOSE (decl));
2419 if (TREE_CODE (decl) == TREE_LIST)
2420 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
2421 else
2422 make_friend_class (current_class_type, TREE_TYPE (decl));
2423 decl = void_type_node;
2425 return decl;
2428 /* TYPE has now been defined. It may, however, have a number of things
2429 waiting make make it their friend. We resolve these references
2430 here. */
2431 void
2432 embrace_waiting_friends (type)
2433 tree type;
2435 tree decl = TYPE_NAME (type);
2436 tree waiters;
2438 if (TREE_CODE (decl) != TYPE_DECL)
2439 return;
2441 for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
2442 waiters = TREE_CHAIN (waiters))
2444 tree waiter = TREE_PURPOSE (waiters);
2445 tree waiter_prev = TREE_VALUE (waiters);
2446 tree decl = TREE_TYPE (waiters);
2447 tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
2448 ? decl : DECL_ORIGINAL_NAME (decl)) : NULL_TREE;
2449 if (name)
2451 /* @@ There may be work to be done since we have not verified
2452 @@ consistency between original and friend declarations
2453 @@ of the functions waiting to become friends. */
2454 tree field = lookup_fnfields (CLASSTYPE_AS_LIST (type), name, 0);
2455 if (field)
2456 if (decl == name)
2457 add_friends (waiter, name, type);
2458 else
2459 add_friend (waiter, decl);
2460 else
2461 error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
2462 DECL_SOURCE_LINE (TYPE_NAME (waiter)),
2463 "no method `%s' defined in class `%s' to be friend",
2464 IDENTIFIER_POINTER (DECL_ORIGINAL_NAME (TREE_TYPE (waiters))),
2465 TYPE_NAME_STRING (type));
2467 else
2468 make_friend_class (type, waiter);
2470 if (TREE_CHAIN (waiter_prev))
2471 TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
2472 else
2473 DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
2477 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
2478 (which needs to go through some sort of groktypename) or it
2479 is the name of the class we are newing. INIT is an initialization value.
2480 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2481 If INIT is void_type_node, it means do *not* call a constructor
2482 for this instance.
2484 For types with constructors, the data returned is initialized
2485 by the approriate constructor.
2487 Whether the type has a constructor or not, if it has a pointer
2488 to a virtual function table, then that pointer is set up
2489 here.
2491 Unless I am mistaken, a call to new () will return initialized
2492 data regardless of whether the constructor itself is private or
2493 not.
2495 PLACEMENT is the `placement' list for user-defined operator new (). */
2497 tree
2498 build_new (placement, decl, init, use_global_new)
2499 tree placement;
2500 tree decl, init;
2501 int use_global_new;
2503 extern tree require_complete_type (); /* typecheck.c */
2504 tree type, true_type, size, rval;
2505 tree init1 = NULL_TREE, nelts;
2506 int has_call = 0, has_array = 0;
2507 tree alignment = NULL_TREE;
2508 tree pending_sizes = NULL_TREE;
2510 if (decl == error_mark_node)
2511 return error_mark_node;
2513 if (TREE_CODE (decl) == TREE_LIST)
2515 tree absdcl = TREE_VALUE (decl);
2516 tree last_absdcl = NULL_TREE;
2517 int old_immediate_size_expand;
2519 if (current_function_decl
2520 && DECL_CONSTRUCTOR_P (current_function_decl))
2522 old_immediate_size_expand = immediate_size_expand;
2523 immediate_size_expand = 0;
2526 nelts = integer_one_node;
2528 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
2530 /* probably meant to be a call */
2531 has_call = 1;
2532 init1 = TREE_OPERAND (absdcl, 1);
2533 absdcl = TREE_OPERAND (absdcl, 0);
2534 TREE_VALUE (decl) = absdcl;
2536 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
2538 last_absdcl = absdcl;
2539 absdcl = TREE_OPERAND (absdcl, 0);
2542 if (last_absdcl)
2544 tree save_absdecl = TREE_VALUE (decl);
2545 TREE_VALUE (decl) = last_absdcl;
2546 true_type = groktypename (decl);
2547 TREE_VALUE (decl) = save_absdecl;
2549 else
2550 true_type = groktypename (decl);
2552 while (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
2554 /* probably meant to be a vec new */
2555 tree this_nelts;
2557 has_array = 1;
2558 this_nelts = save_expr (TREE_OPERAND (absdcl, 1));
2559 absdcl = TREE_OPERAND (absdcl, 0);
2560 if (this_nelts == NULL_TREE)
2561 error ("new of array type fails to specify size");
2562 else if (this_nelts == integer_zero_node)
2564 warning ("zero size array reserves no space");
2565 nelts = integer_zero_node;
2567 else
2568 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
2571 if (last_absdcl)
2572 TREE_OPERAND (last_absdcl, 0) = absdcl;
2573 else
2574 TREE_VALUE (decl) = absdcl;
2576 type = groktypename (decl);
2577 if (! type || type == error_mark_node
2578 || true_type == error_mark_node)
2579 return error_mark_node;
2581 type = TYPE_MAIN_VARIANT (type);
2582 if (type == void_type_node)
2584 error ("invalid type: `void []'");
2585 return error_mark_node;
2587 if (current_function_decl
2588 && DECL_CONSTRUCTOR_P (current_function_decl))
2590 pending_sizes = get_pending_sizes ();
2591 immediate_size_expand = old_immediate_size_expand;
2594 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
2596 if (TREE_TYPE (decl))
2598 /* An aggregate type. */
2599 decl = TREE_TYPE (decl);
2600 type = TREE_TYPE (decl);
2602 else
2604 /* A builtin type. */
2605 decl = lookup_name (decl);
2606 assert (TREE_CODE (decl) == TYPE_DECL);
2607 type = TREE_TYPE (decl);
2609 true_type = type;
2611 else if (TREE_CODE (decl) == TYPE_DECL)
2613 type = TREE_TYPE (decl);
2614 true_type = type;
2616 else
2618 type = decl;
2619 true_type = type;
2620 decl = TYPE_NAME (type);
2623 if (TYPE_SIZE (type) == 0)
2625 if (type == void_type_node)
2626 error ("invalid type for new: `void'");
2627 else
2628 incomplete_type_error (0, type);
2629 return error_mark_node;
2632 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_ABSTRACT_VIRTUALS (type))
2634 abstract_virtuals_error (NULL_TREE, type);
2635 return error_mark_node;
2638 size = size_in_bytes (type);
2639 if (has_array)
2640 size = fold (build_binary_op (MULT_EXPR, size, nelts));
2642 /* If this type has special alignment requirements, deal with them here. */
2643 if (TYPE_ALIGN (type) > BITS_PER_WORD)
2645 alignment = fold (build (MINUS_EXPR, integer_type_node,
2646 c_alignof (type), integer_one_node));
2647 size = fold (build (PLUS_EXPR, integer_type_node, size, alignment));
2650 if (has_call)
2651 init = init1;
2653 if (TREE_CODE (true_type) == ARRAY_TYPE)
2654 type = TREE_TYPE (true_type);
2655 else
2656 type = true_type;
2658 #ifdef SOS
2659 rval = NULL_TREE;
2660 if (placement == void_type_node)
2662 /* Simple "new dynamic" construct. */
2663 if (! IS_AGGR_TYPE (type))
2665 error ("dynamic new can only allocate objects of aggregate type");
2666 return error_mark_node;
2668 else if (! is_aggr_typedef (DECL_NAME (TYPE_NAME (type)), 1))
2669 return error_mark_node;
2670 else
2671 rval = build_dynamic_new (type, size, NULL_TREE, init);
2673 else if (placement && TREE_CODE (placement) == STRING_CST)
2675 /* A "new dynamic" construct with filename argument. */
2676 if (! IS_AGGR_TYPE (type))
2678 error ("dynamic new can only allocate objects of aggregate type");
2679 return error_mark_node;
2681 else if (! is_aggr_typedef (DECL_NAME (TYPE_NAME (type)), 1))
2682 return error_mark_node;
2683 else
2684 rval = build_dynamic_new (type, size, placement, init);
2686 if (rval)
2688 if (alignment)
2690 rval = build (PLUS_EXPR, TYPE_POINTER_TO (type), rval, alignment);
2691 rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (type),
2692 rval, build1 (BIT_NOT_EXPR, integer_type_node, alignment));
2694 goto done;
2696 #endif
2698 if (has_array)
2700 if (placement)
2702 error ("placement syntax invalid for arrays");
2703 return error_mark_node;
2705 rval = build (CALL_EXPR, build_pointer_type (type),
2706 BIN, build_tree_list (NULL_TREE, size), 0);
2707 if (alignment)
2709 rval = build (PLUS_EXPR, TYPE_POINTER_TO (type), rval, alignment);
2710 rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (type),
2711 rval, build1 (BIT_NOT_EXPR, integer_type_node, alignment));
2713 TREE_CALLS_NEW (rval) = 1;
2714 TREE_VOLATILE (rval) = 1;
2716 else
2718 if (TYPE_LANG_SPECIFIC (type)
2719 && (TREE_GETS_NEW (type) && !use_global_new))
2720 rval = build_opfncall (NEW_EXPR, LOOKUP_NORMAL, TYPE_POINTER_TO (type), size, placement);
2721 else if (placement)
2723 rval = build_opfncall (NEW_EXPR, LOOKUP_GLOBAL|LOOKUP_COMPLAIN, ptr_type_node, size, placement);
2725 else if (flag_this_is_variable
2726 && TYPE_HAS_CONSTRUCTOR (type) && init != void_type_node)
2728 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
2729 rval = NULL_TREE;
2730 else
2732 error ("constructors take parameter lists");
2733 return error_mark_node;
2736 else
2738 rval = build (CALL_EXPR, build_pointer_type (type),
2739 BIN, build_tree_list (NULL_TREE, size), 0);
2740 if (alignment)
2742 rval = build (PLUS_EXPR, TYPE_POINTER_TO (type), rval, alignment);
2743 rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (type),
2744 rval, build1 (BIT_NOT_EXPR, integer_type_node, alignment));
2746 TREE_CALLS_NEW (rval) = 1;
2747 TREE_VOLATILE (rval) = 1;
2749 /* We've figured out where the allocation is to go.
2750 If we're not eliding constructors, then if a constructor
2751 is defined, we must go through it. */
2752 if ((rval == NULL_TREE || !flag_elide_constructors)
2753 && TYPE_HAS_CONSTRUCTOR (type) && init != void_type_node)
2755 int flags = LOOKUP_NORMAL;
2757 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (type))
2759 init = tree_cons (NULL_TREE, integer_one_node, init);
2760 flags |= LOOKUP_HAS_IN_CHARGE;
2762 rval = build_method_call (rval, DECL_NAME (TYPE_NAME (type)), init,
2763 NULL_TREE, flags);
2764 goto done;
2767 if (rval == error_mark_node)
2768 return error_mark_node;
2769 rval = save_expr (rval);
2770 TREE_HAS_CONSTRUCTOR (rval) = 1;
2772 /* Don't call any constructors or do any initialization. */
2773 if (init == void_type_node)
2774 goto done;
2776 if (TYPE_NEEDS_CONSTRUCTING (type))
2778 extern tree static_aggregates;
2780 if (current_function_decl == NULL_TREE)
2782 /* In case of static initialization, SAVE_EXPR is good enough. */
2783 init = copy_to_permanent (init);
2784 rval = copy_to_permanent (rval);
2785 static_aggregates = perm_tree_cons (init, rval, static_aggregates);
2787 else
2789 /* Have to wrap this in RTL_EXPR for two cases:
2790 in base or member initialization and if we
2791 are a branch of a ?: operator. Since we
2792 can't easily know the latter, just do it always. */
2793 tree xval = make_node (RTL_EXPR);
2795 TREE_TYPE (xval) = TREE_TYPE (rval);
2796 do_pending_stack_adjust ();
2797 start_sequence ();
2799 /* As a matter of principle, `start_sequence' should do this. */
2800 emit_note (0, -1);
2802 if (has_array)
2803 rval = expand_vec_init (decl, rval,
2804 build_binary_op (MINUS_EXPR, nelts, integer_one_node),
2805 init, 0);
2806 else
2807 expand_aggr_init (build_indirect_ref (rval, 0), init, 0);
2809 do_pending_stack_adjust ();
2811 TREE_VOLATILE (xval) = 1;
2812 RTL_EXPR_SEQUENCE (xval) = get_insns ();
2813 end_sequence ();
2815 if (TREE_CODE (rval) == SAVE_EXPR)
2817 /* Errors may cause this to not get evaluated. */
2818 if (SAVE_EXPR_RTL (rval) == 0)
2819 SAVE_EXPR_RTL (rval) = const0_rtx;
2820 RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
2822 else
2824 assert (TREE_CODE (rval) == VAR_DECL);
2825 RTL_EXPR_RTL (xval) = DECL_RTL (rval);
2827 rval = xval;
2830 else if (has_call || init)
2832 if (IS_AGGR_TYPE (type))
2834 error_with_aggr_type (type, "no constructor for type `%s'");
2835 rval = error_mark_node;
2837 else
2839 /* New 2.0 interpretation: `new int (10)' means
2840 allocate an int, and initialize it with 10. */
2841 init = build_c_cast (type, init);
2842 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
2843 build_modify_expr (build_indirect_ref (rval, 0),
2844 NOP_EXPR, init),
2845 rval);
2846 TREE_VOLATILE (rval) = 1;
2849 done:
2850 if (pending_sizes)
2851 rval = build_compound_expr (chainon (pending_sizes,
2852 build_tree_list (NULL_TREE, rval)));
2854 return rval;
2857 #ifdef SOS
2858 /* Build a "new dynamic" call for type TYPE. The size
2859 of the object we are newing is SIZE. If "new dynamic" was
2860 given with an argument, that argument is in NAME.
2861 PARMS contains the parameters to the constructor.
2862 The first parameter must be an `ImportRequest *'.
2864 This is slightly hairy, because we must find the correct
2865 constructor by hand. */
2866 static tree
2867 build_dynamic_new (type, size, name, parms)
2868 tree type, size;
2869 tree name, parms;
2871 tree import_parms, inner_parms;
2872 tree import_ptr = integer_zero_node;
2873 /* This variable is supposed to be the address of a "struct ref"
2874 object, but how and where should it be defined? */
2875 tree lookup_tmp = integer_zero_node;
2876 tree import_tmp = build_unary_op (ADDR_EXPR, get_temp_name (ptr_type_node, 0), 0);
2878 if (name)
2880 inner_parms = tree_cons (NULL_TREE, name,
2881 build_tree_list (NULL_TREE, integer_zero_node));
2882 inner_parms = tree_cons (NULL_TREE, lookup_tmp, inner_parms);
2883 inner_parms = build_function_call (__sosLookup, inner_parms);
2885 else
2886 inner_parms = integer_zero_node;
2888 import_parms = build_tree_list (NULL_TREE, inner_parms);
2890 if (CLASSTYPE_DYNAMIC_FILENAME (type))
2892 inner_parms = tree_cons (NULL_TREE, CLASSTYPE_DYNAMIC_FILENAME (type),
2893 build_tree_list (NULL_TREE, integer_zero_node));
2894 inner_parms = tree_cons (NULL_TREE, lookup_tmp, inner_parms);
2895 inner_parms = build_function_call (__sosLookup, inner_parms);
2897 else
2898 inner_parms = integer_zero_node;
2900 import_parms = tree_cons (NULL_TREE, inner_parms, import_parms);
2901 import_parms = tree_cons (NULL_TREE, CLASSTYPE_TYPENAME_AS_STRING (type), import_parms);
2902 /* This is one parameter which could be (but should not be) evaluated twice. */
2903 TREE_VALUE (parms) = save_expr (TREE_VALUE (parms));
2905 import_parms = tree_cons (NULL_TREE, TREE_VALUE (parms), import_parms);
2907 /* SOS?? Pass the address of a temporary which can hold the pointer
2908 to dynamic class table, but how and where is it defined? */
2909 import_parms = tree_cons (NULL_TREE, import_tmp, import_parms);
2911 import_ptr = build_function_call (__sosImport, import_parms);
2913 /* SOS?? Now, generate call to ctor, but using `import_ptr' as the function
2914 table. Return the result of the call to the ctor. */
2915 import_ptr = build1 (NOP_EXPR, TYPE_POINTER_TO (type), import_ptr);
2916 return build_method_call (import_ptr, DECL_NAME (TYPE_NAME (type)),
2917 tree_cons (NULL_TREE, import_tmp, parms),
2918 NULL_TREE, LOOKUP_DYNAMIC);
2921 /* Return the name of the link table (as an IDENTIFIER_NODE)
2922 for the given TYPE. */
2923 tree
2924 get_linktable_name (type)
2925 tree type;
2927 char *buf = (char *)alloca (4 + TYPE_NAME_LENGTH (type) + 1);
2928 tree name;
2930 assert (TYPE_DYNAMIC (type));
2931 sprintf (buf, "ZN_%s_", TYPE_NAME_STRING (type));
2932 return get_identifier (buf);
2935 /* For a given type TYPE, grovel for a function table which
2936 can be used to support dynamic linking. */
2937 tree
2938 get_sos_dtable (type, parms)
2939 tree type, parms;
2941 tree classname = CLASSTYPE_TYPENAME_AS_STRING (type);
2942 tree filename = CLASSTYPE_DYNAMIC_FILENAME (type);
2943 tree dyn_vtbl;
2944 /* This variable is supposed to be the address of a "struct ref"
2945 object, but how and where should it be defined? */
2946 tree lookup_tmp = integer_zero_node;
2948 assert (TYPE_DYNAMIC (type));
2950 if (filename)
2952 tree inner_parms = tree_cons (NULL_TREE, filename,
2953 build_tree_list (NULL_TREE, integer_zero_node));
2954 inner_parms = tree_cons (NULL_TREE, lookup_tmp, inner_parms);
2955 parms = build_tree_list (NULL_TREE, build_function_call (__sosLookup, inner_parms));
2957 else
2958 parms = build_tree_list (NULL_TREE, integer_zero_node);
2960 parms = tree_cons (NULL_TREE, classname, parms);
2962 dyn_vtbl = build_function_call (__sosFindCode, parms);
2963 TREE_TYPE (dyn_vtbl) = build_pointer_type (ptr_type_node);
2964 return dyn_vtbl;
2966 #endif
2968 /* `expand_vec_init' performs initialization of a vector of aggregate
2969 types.
2971 DECL is passed only for error reporting, and provides line number
2972 and source file name information.
2973 BASE is the space where the vector will be.
2974 MAXINDEX is the maximum index of the array (one less than the
2975 number of elements).
2976 INIT is the (possibly NULL) initializer.
2978 FROM_ARRAY is 0 if we should init everything with INIT
2979 (i.e., every element initialized from INIT).
2980 FROM_ARRAY is 1 if we should index into INIT in parallel
2981 with initialization of DECL.
2982 FROM_ARRAY is 2 if we should index into INIT in parallel,
2983 but use assignment instead of initialization. */
2985 tree
2986 expand_vec_init (decl, base, maxindex, init, from_array)
2987 tree decl, base, maxindex, init;
2989 tree rval;
2990 tree iterator, base2 = NULL_TREE;
2991 tree type = TREE_TYPE (TREE_TYPE (base));
2992 tree size;
2994 maxindex = convert (integer_type_node, maxindex);
2995 if (maxindex == error_mark_node)
2996 return error_mark_node;
2998 if (current_function_decl == NULL_TREE)
3000 rval = make_tree_vec (3);
3001 TREE_VEC_ELT (rval, 0) = base;
3002 TREE_VEC_ELT (rval, 1) = maxindex;
3003 TREE_VEC_ELT (rval, 2) = init;
3004 return rval;
3007 size = size_in_bytes (type);
3009 /* Set to zero in case size is <= 0. Optimizer will delete this if
3010 it is not needed. */
3011 rval = get_temp_regvar (TYPE_POINTER_TO (type), null_pointer_node);
3012 base = default_conversion (base);
3013 expand_assignment (rval, base, 0, 0);
3014 base = get_temp_regvar (TYPE_POINTER_TO (type), base);
3016 if (init != NULL_TREE
3017 && TREE_CODE (init) == CONSTRUCTOR
3018 && TREE_TYPE (init) == TREE_TYPE (decl))
3020 /* Initialization of array from {...}. */
3021 tree elts = CONSTRUCTOR_ELTS (init);
3022 tree baseref = build1 (INDIRECT_REF, type, base);
3023 tree baseinc = build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size);
3024 int host_i = TREE_INT_CST_LOW (maxindex);
3026 if (IS_AGGR_TYPE (type))
3028 while (elts)
3030 host_i -= 1;
3031 expand_aggr_init (baseref, TREE_VALUE (elts), 0);
3033 expand_assignment (base, baseinc, 0, 0);
3034 elts = TREE_CHAIN (elts);
3036 /* Initialize any elements by default if possible. */
3037 if (host_i >= 0)
3039 if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
3041 if (obey_regdecls)
3042 use_variable (DECL_RTL (base));
3043 goto done_init;
3046 iterator = get_temp_regvar (integer_type_node,
3047 build_int_2 (host_i, 0));
3048 init = NULL_TREE;
3049 goto init_by_default;
3052 else
3053 while (elts)
3055 expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
3057 expand_assignment (base, baseinc, 0, 0);
3058 elts = TREE_CHAIN (elts);
3061 if (obey_regdecls)
3062 use_variable (DECL_RTL (base));
3064 else
3066 iterator = get_temp_regvar (integer_type_node, maxindex);
3068 init_by_default:
3070 /* If initializing one array from another,
3071 initialize element by element. */
3072 if (from_array)
3074 if (decl == NULL_TREE
3075 || (init && TREE_TYPE (init) != TREE_TYPE (decl)))
3077 sorry ("initialization of array from dissimilar array type");
3078 return error_mark_node;
3080 if (init)
3082 base2 = default_conversion (init);
3083 base2 = get_temp_regvar (TYPE_POINTER_TO (type), base2);
3085 else if (TYPE_LANG_SPECIFIC (type)
3086 && TYPE_NEEDS_CONSTRUCTING (type)
3087 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3089 error ("initializer ends prematurely");
3090 return;
3094 expand_start_cond (build (GE_EXPR, integer_type_node,
3095 iterator, integer_zero_node), 0);
3096 expand_start_loop_continue_elsewhere (1);
3098 if (from_array)
3100 tree to = build1 (INDIRECT_REF, type, base);
3101 tree from;
3103 if (base2)
3104 from = build1 (INDIRECT_REF, type, base2);
3105 else
3106 from = NULL_TREE;
3108 if (from_array == 2)
3109 expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
3110 else if (TYPE_NEEDS_CONSTRUCTING (type))
3111 expand_aggr_init (to, from, 0);
3112 else if (from)
3113 expand_assignment (to, from, 0, 0);
3114 else abort ();
3116 else if (TREE_CODE (type) == ARRAY_TYPE)
3118 if (init != 0)
3119 sorry ("cannot initialize multi-dimensional array with initializer");
3120 expand_vec_init (decl, build1 (NOP_EXPR, TYPE_POINTER_TO (TREE_TYPE (type)), base),
3121 array_type_nelts (type), 0, 0);
3123 else
3124 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0);
3126 expand_assignment (base,
3127 build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size),
3128 0, 0);
3129 if (base2)
3130 expand_assignment (base2,
3131 build (PLUS_EXPR, TYPE_POINTER_TO (type), base2, size), 0, 0);
3132 expand_loop_continue_here ();
3133 expand_exit_loop_if_false (build (NE_EXPR, integer_type_node,
3134 build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one));
3136 if (obey_regdecls)
3138 use_variable (DECL_RTL (base));
3139 if (base2)
3140 use_variable (DECL_RTL (base2));
3142 expand_end_loop ();
3143 expand_end_cond ();
3144 if (obey_regdecls)
3145 use_variable (DECL_RTL (iterator));
3147 done_init:
3149 if (obey_regdecls)
3150 use_variable (DECL_RTL (rval));
3151 return rval;
3154 /* Free up storage of type TYPE, at address ADDR.
3155 TYPE is a POINTER_TYPE.
3157 This does not call any destructors. */
3158 tree
3159 build_x_delete (type, addr, use_global_delete)
3160 tree type, addr;
3161 int use_global_delete;
3163 tree rval;
3165 if (!use_global_delete
3166 && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
3167 && TREE_GETS_DELETE (TREE_TYPE (type)))
3168 rval = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr);
3169 else
3171 rval = build (CALL_EXPR, void_type_node, BID,
3172 build_tree_list (NULL_TREE, addr), 0);
3173 TREE_VOLATILE (rval) = 1;
3175 return rval;
3178 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3179 ADDR is an expression which yields the store to be destroyed.
3180 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3181 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3182 virtual baseclasses.
3183 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3185 FLAGS is the logical disjunction of zero or more LOOKUP_
3186 flags. See cplus-tree.h for more info.
3188 This function does not delete an object's virtual base classes. */
3189 tree
3190 build_delete (type, addr, auto_delete, flags, use_global_delete)
3191 tree type, addr;
3192 tree auto_delete;
3193 int flags;
3194 int use_global_delete;
3196 tree function, parms;
3197 tree member;
3198 tree expr;
3199 tree ref;
3200 int ptr;
3202 if (addr == error_mark_node)
3203 return error_mark_node;
3205 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3206 set to `error_mark_node' before it gets properly cleaned up. */
3207 if (type == error_mark_node)
3208 return error_mark_node;
3210 type = TYPE_MAIN_VARIANT (type);
3212 if (TREE_CODE (type) == POINTER_TYPE)
3214 type = TREE_TYPE (type);
3215 if (TYPE_SIZE (type) == 0)
3217 incomplete_type_error (0, type);
3218 return error_mark_node;
3220 if (! IS_AGGR_TYPE (type))
3222 expr = build (CALL_EXPR, void_type_node,
3223 BID, build_tree_list (NULL_TREE, addr), 0);
3224 TREE_VOLATILE (expr) = 1;
3225 return expr;
3227 if (TREE_VOLATILE (addr))
3228 addr = save_expr (addr);
3229 ref = build_indirect_ref (addr, 0);
3230 ptr = 1;
3232 else if (TREE_CODE (type) == ARRAY_TYPE)
3234 if (TREE_VOLATILE (addr))
3235 addr = save_expr (addr);
3236 return build_vec_delete (addr, array_type_nelts (type), c_sizeof (TREE_TYPE (type)),
3237 NULL_TREE, auto_delete, integer_two_node);
3239 else
3241 /* Don't check PROTECT here; leave that decision to the
3242 destructor. If the destructor is visible, call it,
3243 else report error. */
3244 addr = build_unary_op (ADDR_EXPR, addr, 0);
3245 if (TREE_VOLATILE (addr))
3246 addr = save_expr (addr);
3248 if (TREE_LITERAL (addr))
3249 addr = convert_pointer_to (type, addr);
3250 else
3251 addr = convert_force (build_pointer_type (type), addr);
3253 if (TREE_CODE (addr) == NOP_EXPR
3254 && TREE_OPERAND (addr, 0) == current_class_decl)
3255 ref = C_C_D;
3256 else
3257 ref = build_indirect_ref (addr, 0);
3258 ptr = 0;
3261 assert (IS_AGGR_TYPE (type));
3263 if (! TYPE_NEEDS_DESTRUCTOR (type))
3265 if (auto_delete == integer_zero_node)
3266 return build1 (NOP_EXPR, void_type_node, integer_zero_node);
3267 else if (TREE_GETS_DELETE (type) && !use_global_delete)
3268 return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr);
3269 parms = build_tree_list (NULL_TREE, addr);
3270 expr = build (CALL_EXPR, void_type_node, BID, parms, 0);
3271 TREE_VOLATILE (expr) = 1;
3272 return expr;
3274 parms = build_tree_list (NULL_TREE, addr);
3276 /* Below, we will reverse the order in which these calls are made.
3277 If we have a destructor, then that destructor will take care
3278 of the base classes; otherwise, we must do that here. */
3279 if (TYPE_HAS_DESTRUCTOR (type))
3281 tree field = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
3282 tree basetypes = CLASSTYPE_AS_LIST (type);
3284 if (flags & LOOKUP_PROTECT)
3286 enum visibility_type visibility = compute_visibility (basetypes, field);
3288 if (visibility == visibility_private)
3290 if (flags & LOOKUP_COMPLAIN)
3291 error_with_aggr_type (type, "destructor for type `%s' is private in this scope");
3292 return error_mark_node;
3294 else if (visibility == visibility_protected
3295 && (flags & LOOKUP_PROTECTED_OK) == 0)
3297 if (flags & LOOKUP_COMPLAIN)
3298 error_with_aggr_type (type, "destructor for type `%s' is protected in this scope");
3299 return error_mark_node;
3303 /* Once we are in a destructor, try not going through
3304 the virtual function table to find the next destructor. */
3305 if (DECL_VIRTUAL_P (field)
3306 && ! (flags & LOOKUP_NONVIRTUAL)
3307 && TREE_CODE (auto_delete) != PARM_DECL
3308 && (ptr == 1 || ! resolves_to_fixed_type_p (ref)))
3310 /* This destructor must be called via virtual function table. */
3311 field = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_VCONTEXT (field)), 0);
3312 expr = convert_pointer_to (DECL_CONTEXT (field), TREE_VALUE (parms));
3313 if (expr != TREE_VALUE (parms))
3315 ref = build_indirect_ref (expr, 0);
3316 TREE_VALUE (parms) = expr;
3318 function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (field));
3319 if (function == error_mark_node)
3320 return error_mark_node;
3321 TREE_TYPE (function) = build_pointer_type (TREE_TYPE (field));
3322 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3323 expr = build_function_call (function, parms);
3324 if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0)
3326 /* Handle the case where a virtual destructor is
3327 being called on an item that is 0.
3329 @@ Does this really need to be done? */
3330 tree ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node);
3331 #if 0
3332 if (TREE_CODE (ref) == VAR_DECL
3333 || TREE_CODE (ref) == COMPONENT_REF)
3334 warning ("losing in build_delete");
3335 #endif
3336 expr = build (COND_EXPR, void_type_node,
3337 ifexp, expr,
3338 build1 (NOP_EXPR, void_type_node, integer_zero_node));
3341 else
3343 tree ifexp;
3345 if ((flags & LOOKUP_DESTRUCTOR)
3346 || TREE_CODE (ref) == VAR_DECL
3347 || TREE_CODE (ref) == PARM_DECL
3348 || TREE_CODE (ref) == COMPONENT_REF
3349 || TREE_CODE (ref) == ARRAY_REF)
3350 /* These can't be 0. */
3351 ifexp = integer_one_node;
3352 else
3353 /* Handle the case where a non-virtual destructor is
3354 being called on an item that is 0. */
3355 ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node);
3357 function = field;
3359 /* Used to mean that this destructor was known to be empty,
3360 but that's now obsolete. */
3361 assert (DECL_INITIAL (function) != void_type_node);
3363 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3364 expr = build_function_call (function, parms);
3366 if (ifexp != integer_one_node)
3367 expr = build (COND_EXPR, void_type_node,
3368 ifexp, expr,
3369 build1 (NOP_EXPR, void_type_node, integer_zero_node));
3371 return expr;
3373 else
3375 /* This can get visibilties wrong. */
3376 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (type);
3377 tree basetype = n_baseclasses > 0 ? CLASSTYPE_BASECLASS (type, 1) : NULL_TREE;
3378 tree exprstmt = NULL_TREE;
3379 tree parent_auto_delete = auto_delete;
3380 tree cond;
3382 /* If this type does not have a destructor, but does have
3383 operator delete, call the parent parent destructor (if any),
3384 but let this node do the deleting. Otherwise, it is ok
3385 to let the parent destructor do the deleting. */
3386 if (TREE_GETS_DELETE (type) && !use_global_delete)
3388 parent_auto_delete = integer_zero_node;
3389 if (auto_delete == integer_zero_node)
3390 cond = NULL_TREE;
3391 else
3393 expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr);
3394 if (expr == error_mark_node)
3395 return error_mark_node;
3396 if (auto_delete != integer_one_node)
3397 cond = build (COND_EXPR, void_type_node,
3398 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3399 expr,
3400 build1 (NOP_EXPR, void_type_node, integer_zero_node));
3401 else cond = expr;
3404 else if (basetype == NULL_TREE
3405 || (CLASSTYPE_VIA_VIRTUAL (type, 1) == 0
3406 && ! TYPE_NEEDS_DESTRUCTOR (basetype)))
3408 cond = build (COND_EXPR, void_type_node,
3409 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3410 build_function_call (BID, build_tree_list (NULL_TREE, addr)),
3411 build1 (NOP_EXPR, void_type_node, integer_zero_node));
3413 else cond = NULL_TREE;
3415 if (cond)
3416 exprstmt = build_tree_list (NULL_TREE, cond);
3418 if (basetype
3419 && ! CLASSTYPE_VIA_VIRTUAL (type, 1)
3420 && TYPE_NEEDS_DESTRUCTOR (basetype))
3422 tree this_auto_delete;
3424 if (CLASSTYPE_OFFSET (basetype) == integer_zero_node)
3425 this_auto_delete = parent_auto_delete;
3426 else
3427 this_auto_delete = integer_zero_node;
3429 expr = build_delete (TYPE_POINTER_TO (basetype), addr,
3430 this_auto_delete, flags|LOOKUP_PROTECTED_OK, 0);
3431 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3434 for (i = 2; i <= n_baseclasses; i++)
3436 basetype = CLASSTYPE_BASECLASS (type, i);
3437 if (! TYPE_NEEDS_DESTRUCTOR (basetype)
3438 || CLASSTYPE_VIA_VIRTUAL (type, i))
3439 continue;
3441 /* May be zero offset if other baseclasses are virtual. */
3442 expr = fold (build (PLUS_EXPR, TYPE_POINTER_TO (basetype),
3443 addr, CLASSTYPE_OFFSET (basetype)));
3445 expr = build_delete (TYPE_POINTER_TO (basetype), expr,
3446 integer_zero_node,
3447 flags|LOOKUP_PROTECTED_OK, 0);
3449 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3452 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3454 if (TREE_CODE (member) != FIELD_DECL)
3455 continue;
3456 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3458 tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0);
3459 tree this_type = TREE_TYPE (member);
3460 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3461 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3465 if (exprstmt)
3466 return build_compound_expr (exprstmt);
3467 /* Virtual base classes make this function do nothing. */
3468 return build1 (NOP_EXPR, void_type_node, integer_zero_node);
3472 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3474 tree
3475 build_vbase_delete (type, decl)
3476 tree type, decl;
3478 tree vbases = CLASSTYPE_VBASECLASSES (type);
3479 tree result = NULL_TREE;
3480 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3481 assert (addr != error_mark_node);
3482 while (vbases)
3484 tree this_addr = convert_force (TYPE_POINTER_TO (TREE_VALUE (vbases)), addr);
3485 result = tree_cons (NULL_TREE,
3486 build_delete (TREE_TYPE (this_addr), this_addr,
3487 integer_zero_node, LOOKUP_NORMAL, 0),
3488 result);
3489 vbases = TREE_CHAIN (vbases);
3491 return build_compound_expr (nreverse (result));
3494 /* Build a C++ vector delete expression.
3495 MAXINDEX is the number of elements to be deleted.
3496 ELT_SIZE is the nominal size of each element in the vector.
3497 BASE is the expression that should yield the store to be deleted.
3498 DTOR_DUMMY is a placeholder for a destructor. The library function
3499 __builtin_vec_delete has a pointer to function in this position.
3500 This function expands (or synthesizes) these calls itself.
3501 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3502 AUTO_DELETE say whether each item in the container should be deallocated.
3504 This also calls delete for virtual baseclasses of elements of the vector. */
3505 tree
3506 build_vec_delete (base, maxindex, elt_size, dtor_dummy, auto_delete_vec, auto_delete)
3507 tree base, maxindex, elt_size;
3508 tree dtor_dummy;
3509 tree auto_delete_vec, auto_delete;
3511 tree ptype = TREE_TYPE (base);
3512 tree type;
3513 tree rval;
3514 /* Temporary variables used by the loop. */
3515 tree iterator, tbase;
3516 tree size_exp;
3518 /* This is the body of the loop that implements the deletion of a
3519 single element, and moves temp variables to next elements. */
3520 tree body;
3522 /* This is the LOOP_STMT that governs the deletetion of the elements. */
3523 tree loop;
3525 /* This is the IF_STMT that governs whether the loop is executed at all. */
3526 tree if_stmt;
3528 /* This is the ELSE_STMT that governs what to do after the loop has run. */
3529 tree else_stmt = 0;
3531 /* This is the LET_STMT which holds the outermost iterator of the
3532 loop. It is convenient to set this variable up and test it before
3533 executing any other code in the loop. */
3534 tree block;
3536 if (TREE_CODE (ptype) == POINTER_TYPE)
3538 if (maxindex == 0)
3540 error ("must specify size for non array type");
3541 return error_mark_node;
3543 maxindex = convert (integer_type_node, maxindex);
3544 if (maxindex == error_mark_node)
3545 return error_mark_node;
3547 else if (TREE_CODE (ptype) == ARRAY_TYPE)
3549 tree amaxindex = array_type_nelts (ptype);
3550 int multi = 0;
3552 maxindex = fold (convert (integer_type_node, maxindex));
3554 while (1)
3556 if (maxindex == error_mark_node || integer_zerop (maxindex))
3557 return error_mark_node;
3558 ptype = TREE_TYPE (ptype);
3559 if (TREE_CODE (ptype) != ARRAY_TYPE)
3560 break;
3562 if (multi == 0)
3564 /* Convert from 0-based to 1-based indexing. */
3565 maxindex = fold (build (PLUS_EXPR, integer_type_node,
3566 maxindex, integer_one_node));
3567 multi = 1;
3569 amaxindex = array_type_nelts (ptype);
3570 maxindex = fold (build (MULT_EXPR, integer_type_node,
3571 fold (build (PLUS_EXPR, integer_type_node,
3572 amaxindex, integer_one_node)),
3573 maxindex));
3576 if (multi)
3578 /* Convert back to 0-based indexing. */
3579 maxindex = fold (build (MINUS_EXPR, integer_type_node,
3580 maxindex, integer_one_node));
3581 ptype = TYPE_POINTER_TO (ptype);
3582 base = build1 (NOP_EXPR, ptype, build_unary_op (ADDR_EXPR, base, 0));
3584 else
3586 if (amaxindex != 0
3587 && (TREE_CODE (maxindex) == INTEGER_CST || TREE_CODE (amaxindex) == INTEGER_CST)
3588 && ! tree_int_cst_equal (maxindex, amaxindex))
3589 warning ("argument to vector delete disagrees with declared type of array");
3590 base = default_conversion (base);
3591 ptype = TREE_TYPE (base);
3594 else
3596 error ("type to vector delete is neither pointer or array type");
3597 return error_mark_node;
3599 type = TREE_TYPE (ptype);
3601 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
3603 if (extra_warnings)
3604 warning ("array size expression for delete ignored");
3605 rval = build (CALL_EXPR, void_type_node,
3606 BID, build_tree_list (NULL_TREE, base), 0);
3607 TREE_VOLATILE (rval) = 1;
3608 return rval;
3611 size_exp = size_in_bytes (type);
3612 iterator = build_decl (VAR_DECL, NULL_TREE, integer_type_node);
3613 TREE_REGDECL (iterator) = 1;
3614 DECL_INITIAL (iterator) = maxindex;
3615 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
3616 TREE_REGDECL (tbase) = 1;
3617 DECL_INITIAL (tbase) = build (PLUS_EXPR, ptype, base,
3618 build (MULT_EXPR, integer_type_node, size_exp,
3619 build (PLUS_EXPR, integer_type_node, maxindex, integer_one_node)));
3621 block = build_let (input_filename, lineno, iterator, 0, 0, 0);
3622 TREE_TYPE (block) = void_type_node;
3624 if (auto_delete != integer_zero_node
3625 && auto_delete != integer_two_node)
3627 body = build_tree_list (NULL_TREE,
3628 build (CALL_EXPR, void_type_node,
3629 BID, build_tree_list (NULL_TREE, base), 0));
3630 body = build (COND_EXPR, void_type_node,
3631 build (BIT_AND_EXPR, integer_type_node,
3632 auto_delete, integer_one_node),
3633 body, integer_zero_node);
3635 else
3636 body = NULL_TREE;
3638 body = tree_cons (NULL_TREE,
3639 build_delete (ptype, tbase, auto_delete, LOOKUP_NORMAL, 0),
3640 body);
3642 body = tree_cons (NULL_TREE,
3643 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
3644 body);
3646 loop = build_loop (input_filename, lineno,
3647 tbase,
3648 build (NE_EXPR, integer_type_node,
3649 build (PREDECREMENT_EXPR, integer_type_node,
3650 iterator, integer_one_node),
3651 minus_one),
3652 build_compound_expr (body));
3653 TREE_TYPE (loop) = void_type_node;
3654 TREE_VOLATILE (loop) = 1;
3656 /* ELSE_STMT holds the code which deletes the container.
3657 If you don't want to delete the container, make ELSE_STMT
3658 zero here. */
3659 if (auto_delete_vec == integer_one_node)
3660 else_stmt = build_delete (ptr_type_node, base, integer_one_node, 0, 1);
3661 else if (auto_delete_vec != integer_zero_node
3662 && auto_delete_vec != integer_two_node)
3664 else_stmt = build (COND_EXPR, void_type_node,
3665 build (BIT_AND_EXPR, integer_type_node, auto_delete_vec, integer_one_node),
3666 build_delete (ptr_type_node, base, integer_one_node, 0, 1),
3667 integer_zero_node);
3668 TREE_VOLATILE (else_stmt) = 1;
3670 else
3671 else_stmt = 0;
3673 if_stmt = build_if (input_filename, lineno,
3674 build (GE_EXPR, integer_type_node,
3675 iterator, integer_zero_node),
3676 loop, else_stmt);
3678 TREE_TYPE (if_stmt) = void_type_node;
3679 TREE_VOLATILE (if_stmt) = 1;
3681 /* BASE may be a SAVE_EXPR which must be evaluated before either
3682 branch of the IF/THEN/ELSE. */
3683 STMT_BODY (block) = build (COMPOUND_EXPR, void_type_node, base, if_stmt);
3684 TREE_VOLATILE (block) = 1;
3686 return block;