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