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