OSDN Git Service

* init.c (expand_vec_init): Fix off-by-one error.
[pf3gnuchains/gcc-fork.git] / gcc / cp / init.c
1 /* Handle initialization things in C++.
2    Copyright (C) 1987, 89, 92-96, 1997 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com)
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 /* High-level class interface.  */
23
24 #include "config.h"
25 #include "system.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "output.h"
31 #include "except.h"
32 #include "expr.h"
33 #include "toplev.h"
34
35 extern void compiler_error ();
36
37 /* In C++, structures with well-defined constructors are initialized by
38    those constructors, unasked.  CURRENT_BASE_INIT_LIST
39    holds a list of stmts for a BASE_INIT term in the grammar.
40    This list has one element for each base class which must be
41    initialized.  The list elements are [basename, init], with
42    type basetype.  This allows the possibly anachronistic form
43    (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
44    where each successive term can be handed down the constructor
45    line.  Perhaps this was not intended.  */
46 tree current_base_init_list, current_member_init_list;
47
48 static void expand_aggr_vbase_init_1 PROTO((tree, tree, tree, tree));
49 static void expand_aggr_vbase_init PROTO((tree, tree, tree, tree));
50 static void expand_aggr_init_1 PROTO((tree, tree, tree, tree, int));
51 static void expand_default_init PROTO((tree, tree, tree, tree, int));
52 static tree build_vec_delete_1 PROTO((tree, tree, tree, tree, tree,
53                                       int));
54 static void perform_member_init PROTO((tree, tree, tree, int));
55 static void sort_base_init PROTO((tree, tree *, tree *));
56 static tree build_builtin_delete_call PROTO((tree));
57 static int member_init_ok_or_else PROTO((tree, tree, char *));
58 static void expand_virtual_init PROTO((tree, tree));
59 static tree sort_member_init PROTO((tree));
60 static tree build_partial_cleanup_for PROTO((tree));
61 static tree initializing_context PROTO((tree));
62 static void expand_vec_init_try_block PROTO((tree));
63 static void expand_vec_init_catch_clause PROTO((tree, tree, tree, tree));
64
65 /* Cache the identifier nodes for the magic field of a new cookie.  */
66 static tree nc_nelts_field_id;
67
68 static tree minus_one;
69
70 /* Set up local variable for this file.  MUST BE CALLED AFTER
71    INIT_DECL_PROCESSING.  */
72
73 static tree BI_header_type, BI_header_size;
74
75 void init_init_processing ()
76 {
77   tree fields[1];
78
79   minus_one = build_int_2 (-1, -1);
80
81   /* Define the structure that holds header information for
82      arrays allocated via operator new.  */
83   BI_header_type = make_lang_type (RECORD_TYPE);
84   nc_nelts_field_id = get_identifier ("nelts");
85   fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype);
86   finish_builtin_type (BI_header_type, "__new_cookie", fields,
87                        0, double_type_node);
88   BI_header_size = size_in_bytes (BI_header_type);
89 }
90
91 /* Subroutine of emit_base_init.  For BINFO, initialize all the
92    virtual function table pointers, except those that come from
93    virtual base classes.  Initialize binfo's vtable pointer, if
94    INIT_SELF is true.  CAN_ELIDE is true when we know that all virtual
95    function table pointers in all bases have been initialized already,
96    probably because their constructors have just be run.  ADDR is the
97    pointer to the object whos vtables we are going to initialize.
98
99    REAL_BINFO is usually the same as BINFO, except when addr is not of
100    pointer to the type of the real derived type that we want to
101    initialize for.  This is the case when addr is a pointer to a sub
102    object of a complete object, and we only want to do part of the
103    complete object's initialization of vtable pointers.  This is done
104    for all virtual table pointers in virtual base classes.  REAL_BINFO
105    is used to find the BINFO_VTABLE that we initialize with.  BINFO is
106    used for conversions of addr to subobjects.
107
108    BINFO_TYPE (real_binfo) must be BINFO_TYPE (binfo).
109
110    Relies upon binfo being inside TYPE_BINFO (TREE_TYPE (TREE_TYPE
111    (addr))).  */
112
113 void
114 expand_direct_vtbls_init (real_binfo, binfo, init_self, can_elide, addr)
115      tree real_binfo, binfo, addr;
116      int init_self, can_elide;
117 {
118   tree real_binfos = BINFO_BASETYPES (real_binfo);
119   tree binfos = BINFO_BASETYPES (binfo);
120   int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
121
122   for (i = 0; i < n_baselinks; i++)
123     {
124       tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
125       tree base_binfo = TREE_VEC_ELT (binfos, i);
126       int is_not_base_vtable
127         = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
128       if (! TREE_VIA_VIRTUAL (real_base_binfo))
129         expand_direct_vtbls_init (real_base_binfo, base_binfo,
130                                   is_not_base_vtable, can_elide, addr);
131     }
132 #if 0
133   /* Before turning this on, make sure it is correct.  */
134   if (can_elide && ! BINFO_MODIFIED (binfo))
135     return;
136 #endif
137   /* Should we use something besides CLASSTYPE_VFIELDS? */
138   if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
139     {
140       tree base_ptr = convert_pointer_to_real (binfo, addr);
141       expand_virtual_init (real_binfo, base_ptr);
142     }
143 }
144 \f
145 /* 348 - 351 */
146 /* Subroutine of emit_base_init.  */
147
148 static void
149 perform_member_init (member, name, init, explicit)
150      tree member, name, init;
151      int explicit;
152 {
153   tree decl;
154   tree type = TREE_TYPE (member);
155
156   expand_start_target_temps ();
157
158   if (TYPE_NEEDS_CONSTRUCTING (type)
159       || (init && TYPE_HAS_CONSTRUCTOR (type)))
160     {
161       /* Since `init' is already a TREE_LIST on the current_member_init_list,
162          only build it into one if we aren't already a list.  */
163       if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
164         init = build_expr_list (NULL_TREE, init);
165
166       decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit);
167
168       if (explicit
169           && TREE_CODE (type) == ARRAY_TYPE
170           && init != NULL_TREE
171           && TREE_CHAIN (init) == NULL_TREE
172           && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
173         {
174           /* Initialization of one array from another.  */
175           expand_vec_init (TREE_OPERAND (decl, 1), decl,
176                            array_type_nelts (type), TREE_VALUE (init), 1);
177         }
178       else
179         expand_aggr_init (decl, init, 0);
180     }
181   else
182     {
183       if (init == NULL_TREE)
184         {
185           if (explicit)
186             {
187               /* default-initialization.  */
188               if (AGGREGATE_TYPE_P (type))
189                 init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
190               else if (TREE_CODE (type) == REFERENCE_TYPE)
191                 {
192                   cp_error ("default-initialization of `%#D', which has reference type",
193                             member);
194                   init = error_mark_node;
195                 }
196               else
197                 init = integer_zero_node;
198             }
199           /* member traversal: note it leaves init NULL */
200           else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
201             cp_pedwarn ("uninitialized reference member `%D'", member);
202         }
203       else if (TREE_CODE (init) == TREE_LIST)
204         {
205           /* There was an explicit member initialization.  Do some
206              work in that case.  */
207           if (TREE_CHAIN (init))
208             {
209               warning ("initializer list treated as compound expression");
210               init = build_compound_expr (init);
211             }
212           else
213             init = TREE_VALUE (init);
214         }
215
216       /* We only build this with a null init if we got it from the
217          current_member_init_list.  */
218       if (init || explicit)
219         {
220           decl = build_component_ref (current_class_ref, name, NULL_TREE,
221                                       explicit);
222           expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
223         }
224     }
225
226   expand_end_target_temps ();
227   free_temp_slots ();
228
229   if (TYPE_NEEDS_DESTRUCTOR (type))
230     {
231       tree expr;
232
233       /* All cleanups must be on the function_obstack.  */
234       push_obstacks_nochange ();
235       resume_temporary_allocation ();
236
237       expr = build_component_ref (current_class_ref, name, NULL_TREE,
238                                   explicit);
239       expr = build_delete (type, expr, integer_zero_node,
240                            LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
241
242       if (expr != error_mark_node)
243         add_partial_entry (expr);
244
245       pop_obstacks ();
246     }
247 }
248
249 extern int warn_reorder;
250
251 /* Subroutine of emit_member_init.  */
252
253 static tree
254 sort_member_init (t)
255      tree t;
256 {
257   tree x, member, name, field;
258   tree init_list = NULL_TREE;
259   int last_pos = 0;
260   tree last_field = NULL_TREE;
261
262   for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
263     {
264       int pos;
265
266       /* member could be, for example, a CONST_DECL for an enumerated
267          tag; we don't want to try to initialize that, since it already
268          has a value.  */
269       if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
270         continue;
271
272       for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
273         {
274           /* If we cleared this out, then pay no attention to it.  */
275           if (TREE_PURPOSE (x) == NULL_TREE)
276             continue;
277           name = TREE_PURPOSE (x);
278
279 #if 0
280           /* This happens in templates, since the IDENTIFIER is replaced
281              with the COMPONENT_REF in tsubst_expr.  */
282           field = (TREE_CODE (name) == COMPONENT_REF
283                    ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
284 #else
285           /* Let's find out when this happens.  */
286           my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348);
287           field = IDENTIFIER_CLASS_VALUE (name);
288 #endif
289
290           /* If one member shadows another, get the outermost one.  */
291           if (TREE_CODE (field) == TREE_LIST)
292             field = TREE_VALUE (field);
293
294           if (field == member)
295             {
296               if (warn_reorder)
297                 {
298                   if (pos < last_pos)
299                     {
300                       cp_warning_at ("member initializers for `%#D'", last_field);
301                       cp_warning_at ("  and `%#D'", field);
302                       warning ("  will be re-ordered to match declaration order");
303                     }
304                   last_pos = pos;
305                   last_field = field;
306                 }
307
308               /* Make sure we won't try to work on this init again.  */
309               TREE_PURPOSE (x) = NULL_TREE;
310               x = build_tree_list (name, TREE_VALUE (x));
311               goto got_it;
312             }
313         }
314
315       /* If we didn't find MEMBER in the list, create a dummy entry
316          so the two lists (INIT_LIST and the list of members) will be
317          symmetrical.  */
318       x = build_tree_list (NULL_TREE, NULL_TREE);
319     got_it:
320       init_list = chainon (init_list, x); 
321     }
322
323   /* Initializers for base members go at the end.  */
324   for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
325     {
326       name = TREE_PURPOSE (x);
327       if (name)
328         {
329           if (purpose_member (name, init_list))
330             {
331               cp_error ("multiple initializations given for member `%D'",
332                         IDENTIFIER_CLASS_VALUE (name));
333               continue;
334             }
335               
336           init_list = chainon (init_list,
337                                build_tree_list (name, TREE_VALUE (x)));
338           TREE_PURPOSE (x) = NULL_TREE;
339         }
340     }
341
342   return init_list;
343 }
344
345 static void
346 sort_base_init (t, rbase_ptr, vbase_ptr)
347      tree t, *rbase_ptr, *vbase_ptr;
348 {
349   tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
350   int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
351
352   int i;
353   tree x;
354   tree last;
355
356   /* For warn_reorder.  */
357   int last_pos = 0;
358   tree last_base = NULL_TREE;
359
360   tree rbases = NULL_TREE;
361   tree vbases = NULL_TREE;
362
363   /* First walk through and splice out vbase and invalid initializers.
364      Also replace names with binfos.  */
365
366   last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list);
367   for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
368     {
369       tree basetype = TREE_PURPOSE (x);
370       tree binfo = NULL_TREE;
371
372       if (basetype == NULL_TREE)
373         {
374           /* Initializer for single base class.  Must not
375              use multiple inheritance or this is ambiguous.  */
376           switch (n_baseclasses)
377             {
378             case 0:
379               cp_error ("`%T' does not have a base class to initialize",
380                         current_class_type);
381               return;
382             case 1:
383               break;
384             default:
385               cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
386                         current_class_type);
387               return;
388             }
389           binfo = TREE_VEC_ELT (binfos, 0);
390         }
391       else if (is_aggr_type (basetype, 1))
392         {
393           binfo = binfo_or_else (basetype, t);
394           if (binfo == NULL_TREE)
395             continue;
396
397           /* Virtual base classes are special cases.  Their initializers
398              are recorded with this constructor, and they are used when
399              this constructor is the top-level constructor called.  */
400           if (TREE_VIA_VIRTUAL (binfo))
401             {
402               tree v = CLASSTYPE_VBASECLASSES (t);
403               while (BINFO_TYPE (v) != BINFO_TYPE (binfo))
404                 v = TREE_CHAIN (v);
405
406               vbases = tree_cons (v, TREE_VALUE (x), vbases);
407               continue;
408             }
409           else
410             {
411               /* Otherwise, if it is not an immediate base class, complain.  */
412               for (i = n_baseclasses-1; i >= 0; i--)
413                 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
414                   break;
415               if (i < 0)
416                 {
417                   cp_error ("`%T' is not an immediate base class of `%T'",
418                             basetype, current_class_type);
419                   continue;
420                 }
421             }
422         }
423       else
424         my_friendly_abort (365);
425
426       TREE_PURPOSE (x) = binfo;
427       TREE_CHAIN (last) = x;
428       last = x;
429     }
430   TREE_CHAIN (last) = NULL_TREE;
431
432   /* Now walk through our regular bases and make sure they're initialized.  */
433
434   for (i = 0; i < n_baseclasses; ++i)
435     {
436       tree base_binfo = TREE_VEC_ELT (binfos, i);
437       int pos;
438
439       if (TREE_VIA_VIRTUAL (base_binfo))
440         continue;
441
442       for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
443         {
444           tree binfo = TREE_PURPOSE (x);
445
446           if (binfo == NULL_TREE)
447             continue;
448
449           if (binfo == base_binfo)
450             {
451               if (warn_reorder)
452                 {
453                   if (pos < last_pos)
454                     {
455                       cp_warning_at ("base initializers for `%#T'", last_base);
456                       cp_warning_at ("  and `%#T'", BINFO_TYPE (binfo));
457                       warning ("  will be re-ordered to match inheritance order");
458                     }
459                   last_pos = pos;
460                   last_base = BINFO_TYPE (binfo);
461                 }
462
463               /* Make sure we won't try to work on this init again.  */
464               TREE_PURPOSE (x) = NULL_TREE;
465               x = build_tree_list (binfo, TREE_VALUE (x));
466               goto got_it;
467             }
468         }
469
470       /* If we didn't find BASE_BINFO in the list, create a dummy entry
471          so the two lists (RBASES and the list of bases) will be
472          symmetrical.  */
473       x = build_tree_list (NULL_TREE, NULL_TREE);
474     got_it:
475       rbases = chainon (rbases, x);
476     }
477
478   *rbase_ptr = rbases;
479   *vbase_ptr = vbases;
480 }
481
482 /* Perform partial cleanups for a base for exception handling.  */
483
484 static tree
485 build_partial_cleanup_for (binfo)
486      tree binfo;
487 {
488   return build_scoped_method_call
489     (current_class_ref, binfo, dtor_identifier,
490      build_expr_list (NULL_TREE, integer_zero_node));
491 }
492
493 /* Perform whatever initializations have yet to be done on the base
494    class of the class variable.  These actions are in the global
495    variable CURRENT_BASE_INIT_LIST.  Such an action could be
496    NULL_TREE, meaning that the user has explicitly called the base
497    class constructor with no arguments.
498
499    If there is a need for a call to a constructor, we must surround
500    that call with a pushlevel/poplevel pair, since we are technically
501    at the PARM level of scope.
502
503    Argument IMMEDIATELY, if zero, forces a new sequence to be
504    generated to contain these new insns, so it can be emitted later.
505    This sequence is saved in the global variable BASE_INIT_EXPR.
506    Otherwise, the insns are emitted into the current sequence.
507
508    Note that emit_base_init does *not* initialize virtual base
509    classes.  That is done specially, elsewhere.  */
510
511 extern tree base_init_expr, rtl_expr_chain;
512
513 void
514 emit_base_init (t, immediately)
515      tree t;
516      int immediately;
517 {
518   tree member;
519   tree mem_init_list;
520   tree rbase_init_list, vbase_init_list;
521   tree t_binfo = TYPE_BINFO (t);
522   tree binfos = BINFO_BASETYPES (t_binfo);
523   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
524   tree expr = NULL_TREE;
525
526   if (! immediately)
527     {
528       int momentary;
529       do_pending_stack_adjust ();
530       /* Make the RTL_EXPR node temporary, not momentary,
531          so that rtl_expr_chain doesn't become garbage.  */
532       momentary = suspend_momentary ();
533       expr = make_node (RTL_EXPR);
534       resume_momentary (momentary);
535       start_sequence_for_rtl_expr (expr); 
536     }
537
538   if (write_symbols == NO_DEBUG)
539     /* As a matter of principle, `start_sequence' should do this.  */
540     emit_note (0, -1);
541   else
542     /* Always emit a line number note so we can step into constructors.  */
543     emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
544                           DECL_SOURCE_LINE (current_function_decl));
545
546   mem_init_list = sort_member_init (t);
547   current_member_init_list = NULL_TREE;
548
549   sort_base_init (t, &rbase_init_list, &vbase_init_list);
550   current_base_init_list = NULL_TREE;
551
552   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
553     {
554       tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
555
556       expand_start_cond (first_arg, 0);
557       expand_aggr_vbase_init (t_binfo, current_class_ref, current_class_ptr,
558                               vbase_init_list);
559       expand_end_cond ();
560     }
561
562   /* Now, perform initialization of non-virtual base classes.  */
563   for (i = 0; i < n_baseclasses; i++)
564     {
565       tree base_binfo = TREE_VEC_ELT (binfos, i);
566       tree init = void_list_node;
567
568       if (TREE_VIA_VIRTUAL (base_binfo))
569         continue;
570
571       my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo,
572                           999);
573
574       if (TREE_PURPOSE (rbase_init_list))
575         init = TREE_VALUE (rbase_init_list);
576       else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
577         {
578           init = NULL_TREE;
579           if (extra_warnings && copy_args_p (current_function_decl))
580             cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor",
581                         BINFO_TYPE (base_binfo));
582         }
583
584       if (init != void_list_node)
585         {
586           expand_start_target_temps ();
587
588           member = convert_pointer_to_real (base_binfo, current_class_ptr);
589           expand_aggr_init_1 (base_binfo, NULL_TREE,
590                               build_indirect_ref (member, NULL_PTR), init,
591                               LOOKUP_NORMAL);
592
593           expand_end_target_temps ();
594           free_temp_slots ();
595         }
596
597       if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
598         {
599           tree expr;
600
601           /* All cleanups must be on the function_obstack.  */
602           push_obstacks_nochange ();
603           resume_temporary_allocation ();
604           expr = build_partial_cleanup_for (base_binfo);
605           pop_obstacks ();
606           add_partial_entry (expr);
607         }
608
609       rbase_init_list = TREE_CHAIN (rbase_init_list);
610     }
611
612   /* Initialize all the virtual function table fields that
613      do come from virtual base classes.  */
614   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
615     expand_indirect_vtbls_init (t_binfo, current_class_ref, current_class_ptr);
616
617   /* Initialize all the virtual function table fields that
618      do not come from virtual base classes.  */
619   expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_ptr);
620
621   for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
622     {
623       tree init, name;
624       int from_init_list;
625
626       /* member could be, for example, a CONST_DECL for an enumerated
627          tag; we don't want to try to initialize that, since it already
628          has a value.  */
629       if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
630         continue;
631
632       /* See if we had a user-specified member initialization.  */
633       if (TREE_PURPOSE (mem_init_list))
634         {
635           name = TREE_PURPOSE (mem_init_list);
636           init = TREE_VALUE (mem_init_list);
637           from_init_list = 1;
638
639 #if 0
640           if (TREE_CODE (name) == COMPONENT_REF)
641             name = DECL_NAME (TREE_OPERAND (name, 1));
642 #else
643           /* Also see if it's ever a COMPONENT_REF here.  If it is, we
644              need to do `expand_assignment (name, init, 0, 0);' and
645              a continue.  */
646           my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349);
647 #endif
648         }
649       else
650         {
651           name = DECL_NAME (member);
652           init = DECL_INITIAL (member);
653
654           from_init_list = 0;
655
656           /* Effective C++ rule 12.  */
657           if (warn_ecpp && init == NULL_TREE
658               && !DECL_ARTIFICIAL (member)
659               && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE)
660             cp_warning ("`%D' should be initialized in the member initialization list", member);            
661         }
662
663       perform_member_init (member, name, init, from_init_list);
664       mem_init_list = TREE_CHAIN (mem_init_list);
665     }
666
667   /* Now initialize any members from our bases.  */
668   while (mem_init_list)
669     {
670       tree name, init, field;
671
672       if (TREE_PURPOSE (mem_init_list))
673         {
674           name = TREE_PURPOSE (mem_init_list);
675           init = TREE_VALUE (mem_init_list);
676           /* XXX: this may need the COMPONENT_REF operand 0 check if
677              it turns out we actually get them.  */
678           field = IDENTIFIER_CLASS_VALUE (name);
679
680           /* If one member shadows another, get the outermost one.  */
681           if (TREE_CODE (field) == TREE_LIST)
682             {
683               field = TREE_VALUE (field);
684               if (decl_type_context (field) != current_class_type)
685                 cp_error ("field `%D' not in immediate context", field);
686             }
687
688 #if 0
689           /* It turns out if you have an anonymous union in the
690              class, a member from it can end up not being on the
691              list of fields (rather, the type is), and therefore
692              won't be seen by the for loop above.  */
693
694           /* The code in this for loop is derived from a general loop
695              which had this check in it.  Theoretically, we've hit
696              every initialization for the list of members in T, so
697              we shouldn't have anything but these left in this list.  */
698           my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351);
699 #endif
700
701           perform_member_init (field, name, init, 1);
702         }
703       mem_init_list = TREE_CHAIN (mem_init_list);
704     }
705
706   if (! immediately)
707     {
708       do_pending_stack_adjust ();
709       my_friendly_assert (base_init_expr == 0, 207);
710       base_init_expr = expr;
711       TREE_TYPE (expr) = void_type_node;
712       RTL_EXPR_RTL (expr) = const0_rtx;
713       RTL_EXPR_SEQUENCE (expr) = get_insns ();
714       rtl_expr_chain = tree_cons (NULL_TREE, expr, rtl_expr_chain);
715       end_sequence ();
716       TREE_SIDE_EFFECTS (expr) = 1;
717     }
718
719   /* All the implicit try blocks we built up will be zapped
720      when we come to a real binding contour boundary.  */
721 }
722
723 /* Check that all fields are properly initialized after
724    an assignment to `this'.  */
725
726 void
727 check_base_init (t)
728      tree t;
729 {
730   tree member;
731   for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
732     if (DECL_NAME (member) && TREE_USED (member))
733       cp_error ("field `%D' used before initialized (after assignment to `this')",
734                 member);
735 }
736
737 /* This code sets up the virtual function tables appropriate for
738    the pointer DECL.  It is a one-ply initialization.
739
740    BINFO is the exact type that DECL is supposed to be.  In
741    multiple inheritance, this might mean "C's A" if C : A, B.  */
742
743 static void
744 expand_virtual_init (binfo, decl)
745      tree binfo, decl;
746 {
747   tree type = BINFO_TYPE (binfo);
748   tree vtbl, vtbl_ptr;
749   tree vtype, vtype_binfo;
750
751   /* This code is crusty.  Should be simple, like:
752      vtbl = BINFO_VTABLE (binfo);
753      */
754   vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
755   vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
756   vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo));
757   assemble_external (vtbl);
758   TREE_USED (vtbl) = 1;
759   vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
760   decl = convert_pointer_to_real (vtype_binfo, decl);
761   vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
762   if (vtbl_ptr == error_mark_node)
763     return;
764
765   /* Have to convert VTBL since array sizes may be different.  */
766   vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
767   expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
768 }
769
770 /* Subroutine of `expand_aggr_vbase_init'.
771    BINFO is the binfo of the type that is being initialized.
772    INIT_LIST is the list of initializers for the virtual baseclass.  */
773
774 static void
775 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
776      tree binfo, exp, addr, init_list;
777 {
778   tree init = purpose_member (binfo, init_list);
779   tree ref = build_indirect_ref (addr, NULL_PTR);
780
781   expand_start_target_temps ();
782
783   if (init)
784     init = TREE_VALUE (init);
785   /* Call constructors, but don't set up vtables.  */
786   expand_aggr_init_1 (binfo, exp, ref, init, LOOKUP_COMPLAIN);
787
788   expand_end_target_temps ();
789   free_temp_slots ();
790 }
791
792 /* Initialize this object's virtual base class pointers.  This must be
793    done only at the top-level of the object being constructed.
794
795    INIT_LIST is list of initialization for constructor to perform.  */
796
797 static void
798 expand_aggr_vbase_init (binfo, exp, addr, init_list)
799      tree binfo;
800      tree exp;
801      tree addr;
802      tree init_list;
803 {
804   tree type = BINFO_TYPE (binfo);
805
806   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
807     {
808       tree result = init_vbase_pointers (type, addr);
809       tree vbases;
810
811       if (result)
812         expand_expr_stmt (build_compound_expr (result));
813
814       for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
815            vbases = TREE_CHAIN (vbases))
816         {
817           tree tmp = purpose_member (vbases, result);
818           expand_aggr_vbase_init_1 (vbases, exp,
819                                     TREE_OPERAND (TREE_VALUE (tmp), 0),
820                                     init_list);
821         }
822     }
823 }
824
825 /* Find the context in which this FIELD can be initialized.  */
826
827 static tree
828 initializing_context (field)
829      tree field;
830 {
831   tree t = DECL_CONTEXT (field);
832
833   /* Anonymous union members can be initialized in the first enclosing
834      non-anonymous union context.  */
835   while (t && ANON_UNION_TYPE_P (t))
836     t = TYPE_CONTEXT (t);
837   return t;
838 }
839
840 /* Function to give error message if member initialization specification
841    is erroneous.  FIELD is the member we decided to initialize.
842    TYPE is the type for which the initialization is being performed.
843    FIELD must be a member of TYPE.
844    
845    MEMBER_NAME is the name of the member.  */
846
847 static int
848 member_init_ok_or_else (field, type, member_name)
849      tree field;
850      tree type;
851      char *member_name;
852 {
853   if (field == error_mark_node)
854     return 0;
855   if (field == NULL_TREE || initializing_context (field) != type)
856     {
857       cp_error ("class `%T' does not have any field named `%s'", type,
858                 member_name);
859       return 0;
860     }
861   if (TREE_STATIC (field))
862     {
863       cp_error ("field `%#D' is static; only point of initialization is its declaration",
864                 field);
865       return 0;
866     }
867
868   return 1;
869 }
870
871 /* If NAME is a viable field name for the aggregate DECL,
872    and PARMS is a viable parameter list, then expand an _EXPR
873    which describes this initialization.
874
875    Note that we do not need to chase through the class's base classes
876    to look for NAME, because if it's in that list, it will be handled
877    by the constructor for that base class.
878
879    We do not yet have a fixed-point finder to instantiate types
880    being fed to overloaded constructors.  If there is a unique
881    constructor, then argument types can be got from that one.
882
883    If INIT is non-NULL, then it the initialization should
884    be placed in `current_base_init_list', where it will be processed
885    by `emit_base_init'.  */
886
887 void
888 expand_member_init (exp, name, init)
889      tree exp, name, init;
890 {
891   tree basetype = NULL_TREE, field;
892   tree type;
893
894   if (exp == NULL_TREE)
895     return;                     /* complain about this later */
896
897   type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
898
899   if (name && TREE_CODE (name) == TYPE_DECL)
900     {
901       basetype = TREE_TYPE (name);
902       name = DECL_NAME (name);
903     }
904
905   if (name == NULL_TREE && IS_AGGR_TYPE (type))
906     switch (CLASSTYPE_N_BASECLASSES (type))
907       {
908       case 0:
909         error ("base class initializer specified, but no base class to initialize");
910         return;
911       case 1:
912         basetype = TYPE_BINFO_BASETYPE (type, 0);
913         break;
914       default:
915         error ("initializer for unnamed base class ambiguous");
916         cp_error ("(type `%T' uses multiple inheritance)", type);
917         return;
918       }
919
920   my_friendly_assert (init != NULL_TREE, 0);
921
922   /* The grammar should not allow fields which have names that are
923      TYPENAMEs.  Therefore, if the field has a non-NULL TREE_TYPE, we
924      may assume that this is an attempt to initialize a base class
925      member of the current type.  Otherwise, it is an attempt to
926      initialize a member field.  */
927
928   if (init == void_type_node)
929     init = NULL_TREE;
930
931   if (name == NULL_TREE || basetype)
932     {
933       tree base_init;
934
935       if (name == NULL_TREE)
936         {
937 #if 0
938           if (basetype)
939             name = TYPE_IDENTIFIER (basetype);
940           else
941             {
942               error ("no base class to initialize");
943               return;
944             }
945 #endif
946         }
947       else if (basetype != type
948                && ! current_template_parms
949                && ! vec_binfo_member (basetype,
950                                       TYPE_BINFO_BASETYPES (type))
951                && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
952         {
953           if (IDENTIFIER_CLASS_VALUE (name))
954             goto try_member;
955           if (TYPE_USES_VIRTUAL_BASECLASSES (type))
956             cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
957                       basetype, type);
958           else
959             cp_error ("type `%T' is not an immediate basetype for `%T'",
960                       basetype, type);
961           return;
962         }
963
964       if (purpose_member (basetype, current_base_init_list))
965         {
966           cp_error ("base class `%T' already initialized", basetype);
967           return;
968         }
969
970       if (warn_reorder && current_member_init_list)
971         {
972           cp_warning ("base initializer for `%T'", basetype);
973           warning ("   will be re-ordered to precede member initializations");
974         }
975
976       base_init = build_tree_list (basetype, init);
977       current_base_init_list = chainon (current_base_init_list, base_init);
978     }
979   else
980     {
981       tree member_init;
982
983     try_member:
984       field = lookup_field (type, name, 1, 0);
985
986       if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
987         return;
988
989       if (purpose_member (name, current_member_init_list))
990         {
991           cp_error ("field `%D' already initialized", field);
992           return;
993         }
994
995       member_init = build_tree_list (name, init);
996       current_member_init_list = chainon (current_member_init_list, member_init);
997     }
998 }
999
1000 /* This is like `expand_member_init', only it stores one aggregate
1001    value into another.
1002
1003    INIT comes in two flavors: it is either a value which
1004    is to be stored in EXP, or it is a parameter list
1005    to go to a constructor, which will operate on EXP.
1006    If INIT is not a parameter list for a constructor, then set
1007    LOOKUP_ONLYCONVERTING.
1008    If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1009    the initializer, if FLAGS is 0, then it is the (init) form.
1010    If `init' is a CONSTRUCTOR, then we emit a warning message,
1011    explaining that such initializations are invalid.
1012
1013    ALIAS_THIS is nonzero iff we are initializing something which is
1014    essentially an alias for current_class_ref.  In this case, the base
1015    constructor may move it on us, and we must keep track of such
1016    deviations.
1017
1018    If INIT resolves to a CALL_EXPR which happens to return
1019    something of the type we are looking for, then we know
1020    that we can safely use that call to perform the
1021    initialization.
1022
1023    The virtual function table pointer cannot be set up here, because
1024    we do not really know its type.
1025
1026    Virtual baseclass pointers are also set up here.
1027
1028    This never calls operator=().
1029
1030    When initializing, nothing is CONST.
1031
1032    A default copy constructor may have to be used to perform the
1033    initialization.
1034
1035    A constructor or a conversion operator may have to be used to
1036    perform the initialization, but not both, as it would be ambiguous.  */
1037
1038 void
1039 expand_aggr_init (exp, init, flags)
1040      tree exp, init;
1041      int flags;
1042 {
1043   tree type = TREE_TYPE (exp);
1044   int was_const = TREE_READONLY (exp);
1045   int was_volatile = TREE_THIS_VOLATILE (exp);
1046
1047   if (init == error_mark_node)
1048     return;
1049
1050   TREE_READONLY (exp) = 0;
1051   TREE_THIS_VOLATILE (exp) = 0;
1052
1053   if (init && TREE_CODE (init) != TREE_LIST)
1054     flags |= LOOKUP_ONLYCONVERTING;
1055
1056   if (TREE_CODE (type) == ARRAY_TYPE)
1057     {
1058       /* Must arrange to initialize each element of EXP
1059          from elements of INIT.  */
1060       tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1061       if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
1062         {
1063           TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1064           if (init)
1065             TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1066         }
1067       if (init && TREE_TYPE (init) == NULL_TREE)
1068         {
1069           /* Handle bad initializers like:
1070              class COMPLEX {
1071              public:
1072                double re, im;
1073                COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1074                ~COMPLEX() {};
1075              };
1076
1077              int main(int argc, char **argv) {
1078                COMPLEX zees(1.0, 0.0)[10];
1079              }
1080           */
1081           error ("bad array initializer");
1082           return;
1083         }
1084       expand_vec_init (exp, exp, array_type_nelts (type), init,
1085                        init && same_type_p (TREE_TYPE (init),
1086                                             TREE_TYPE (exp)));
1087       TREE_READONLY (exp) = was_const;
1088       TREE_THIS_VOLATILE (exp) = was_volatile;
1089       TREE_TYPE (exp) = type;
1090       if (init)
1091         TREE_TYPE (init) = itype;
1092       return;
1093     }
1094
1095   if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1096     /* just know that we've seen something for this node */
1097     TREE_USED (exp) = 1;
1098
1099 #if 0
1100   /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
1101      constructor as parameters to an implicit GNU C++ constructor.  */
1102   if (init && TREE_CODE (init) == CONSTRUCTOR
1103       && TYPE_HAS_CONSTRUCTOR (type)
1104       && TREE_TYPE (init) == type)
1105     init = CONSTRUCTOR_ELTS (init);
1106 #endif
1107
1108   TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1109   expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1110                       init, LOOKUP_NORMAL|flags);
1111   TREE_TYPE (exp) = type;
1112   TREE_READONLY (exp) = was_const;
1113   TREE_THIS_VOLATILE (exp) = was_volatile;
1114 }
1115
1116 static void
1117 expand_default_init (binfo, true_exp, exp, init, flags)
1118      tree binfo;
1119      tree true_exp, exp;
1120      tree init;
1121      int flags;
1122 {
1123   tree type = TREE_TYPE (exp);
1124
1125   /* It fails because there may not be a constructor which takes
1126      its own type as the first (or only parameter), but which does
1127      take other types via a conversion.  So, if the thing initializing
1128      the expression is a unit element of type X, first try X(X&),
1129      followed by initialization by X.  If neither of these work
1130      out, then look hard.  */
1131   tree rval;
1132   tree parms;
1133
1134   if (init && TREE_CODE (init) != TREE_LIST
1135       && (flags & LOOKUP_ONLYCONVERTING))
1136     {
1137       /* Base subobjects should only get direct-initialization.  */
1138       if (true_exp != exp)
1139         abort ();
1140
1141       /* We special-case TARGET_EXPRs here to avoid an error about
1142          private copy constructors for temporaries bound to reference vars.
1143          If the TARGET_EXPR represents a call to a function that has
1144          permission to create such objects, a reference can bind directly
1145          to the return value.  An object variable must be initialized
1146          via the copy constructor, even if the call is elided.  */
1147       if (! (TREE_CODE (exp) == VAR_DECL && DECL_ARTIFICIAL (exp)
1148              && TREE_CODE (init) == TARGET_EXPR && TREE_TYPE (init) == type))
1149         init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1150
1151       if (TREE_CODE (init) == TRY_CATCH_EXPR)
1152         /* We need to protect the initialization of a catch parm
1153            with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1154            around the TARGET_EXPR for the copy constructor.  See
1155            expand_start_catch_block.  */
1156         TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1157                                         TREE_OPERAND (init, 0));
1158       else
1159         init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1160       TREE_SIDE_EFFECTS (init) = 1;
1161       expand_expr_stmt (init);
1162       return;
1163     }
1164
1165   if (init == NULL_TREE
1166       || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1167     {
1168       parms = init;
1169       if (parms)
1170         init = TREE_VALUE (parms);
1171     }
1172   else
1173     parms = build_expr_list (NULL_TREE, init);
1174
1175   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1176     {
1177       if (true_exp == exp)
1178         parms = expr_tree_cons (NULL_TREE, integer_one_node, parms);
1179       else
1180         parms = expr_tree_cons (NULL_TREE, integer_zero_node, parms);
1181       flags |= LOOKUP_HAS_IN_CHARGE;
1182     }
1183
1184   rval = build_method_call (exp, ctor_identifier,
1185                             parms, binfo, flags);
1186   if (TREE_SIDE_EFFECTS (rval))
1187     expand_expr_stmt (rval);
1188 }
1189
1190 /* This function is responsible for initializing EXP with INIT
1191    (if any).
1192
1193    BINFO is the binfo of the type for who we are performing the
1194    initialization.  For example, if W is a virtual base class of A and B,
1195    and C : A, B.
1196    If we are initializing B, then W must contain B's W vtable, whereas
1197    were we initializing C, W must contain C's W vtable.
1198
1199    TRUE_EXP is nonzero if it is the true expression being initialized.
1200    In this case, it may be EXP, or may just contain EXP.  The reason we
1201    need this is because if EXP is a base element of TRUE_EXP, we
1202    don't necessarily know by looking at EXP where its virtual
1203    baseclass fields should really be pointing.  But we do know
1204    from TRUE_EXP.  In constructors, we don't know anything about
1205    the value being initialized.
1206
1207    ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1208
1209    FLAGS is just passes to `build_method_call'.  See that function for
1210    its description.  */
1211
1212 static void
1213 expand_aggr_init_1 (binfo, true_exp, exp, init, flags)
1214      tree binfo;
1215      tree true_exp, exp;
1216      tree init;
1217      int flags;
1218 {
1219   tree type = TREE_TYPE (exp);
1220
1221   my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1222
1223   /* Use a function returning the desired type to initialize EXP for us.
1224      If the function is a constructor, and its first argument is
1225      NULL_TREE, know that it was meant for us--just slide exp on
1226      in and expand the constructor.  Constructors now come
1227      as TARGET_EXPRs.  */
1228
1229   if (init && TREE_CODE (exp) == VAR_DECL
1230       && TREE_CODE (init) == CONSTRUCTOR
1231       && TREE_HAS_CONSTRUCTOR (init))
1232     {
1233       tree t = store_init_value (exp, init);
1234       if (!t)
1235         {
1236           expand_decl_init (exp);
1237           return;
1238         }
1239       t = build (INIT_EXPR, type, exp, init);
1240       TREE_SIDE_EFFECTS (t) = 1;
1241       expand_expr_stmt (t);
1242       return;
1243     }
1244
1245   /* We know that expand_default_init can handle everything we want
1246      at this point.  */
1247   expand_default_init (binfo, true_exp, exp, init, flags);
1248 }
1249
1250 /* Report an error if NAME is not the name of a user-defined,
1251    aggregate type.  If OR_ELSE is nonzero, give an error message.  */
1252
1253 int
1254 is_aggr_typedef (name, or_else)
1255      tree name;
1256      int or_else;
1257 {
1258   tree type;
1259
1260   if (name == error_mark_node)
1261     return 0;
1262
1263   if (IDENTIFIER_HAS_TYPE_VALUE (name))
1264     type = IDENTIFIER_TYPE_VALUE (name);
1265   else
1266     {
1267       if (or_else)
1268         cp_error ("`%T' is not an aggregate typedef", name);
1269       return 0;
1270     }
1271
1272   if (! IS_AGGR_TYPE (type)
1273       && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1274       && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1275     {
1276       if (or_else)
1277         cp_error ("`%T' is not an aggregate type", type);
1278       return 0;
1279     }
1280   return 1;
1281 }
1282
1283 /* Report an error if TYPE is not a user-defined, aggregate type.  If
1284    OR_ELSE is nonzero, give an error message.  */
1285
1286 int
1287 is_aggr_type (type, or_else)
1288      tree type;
1289      int or_else;
1290 {
1291   if (type == error_mark_node)
1292     return 0;
1293
1294   if (! IS_AGGR_TYPE (type)
1295       && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1296       && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1297     {
1298       if (or_else)
1299         cp_error ("`%T' is not an aggregate type", type);
1300       return 0;
1301     }
1302   return 1;
1303 }
1304
1305 /* Like is_aggr_typedef, but returns typedef if successful.  */
1306
1307 tree
1308 get_aggr_from_typedef (name, or_else)
1309      tree name;
1310      int or_else;
1311 {
1312   tree type;
1313
1314   if (name == error_mark_node)
1315     return NULL_TREE;
1316
1317   if (IDENTIFIER_HAS_TYPE_VALUE (name))
1318     type = IDENTIFIER_TYPE_VALUE (name);
1319   else
1320     {
1321       if (or_else)
1322         cp_error ("`%T' fails to be an aggregate typedef", name);
1323       return NULL_TREE;
1324     }
1325
1326   if (! IS_AGGR_TYPE (type)
1327       && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1328       && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1329     {
1330       if (or_else)
1331         cp_error ("type `%T' is of non-aggregate type", type);
1332       return NULL_TREE;
1333     }
1334   return type;
1335 }
1336
1337 tree
1338 get_type_value (name)
1339      tree name;
1340 {
1341   if (name == error_mark_node)
1342     return NULL_TREE;
1343
1344   if (IDENTIFIER_HAS_TYPE_VALUE (name))
1345     return IDENTIFIER_TYPE_VALUE (name);
1346   else
1347     return NULL_TREE;
1348 }
1349   
1350 \f
1351 /* This code could just as well go in `class.c', but is placed here for
1352    modularity.  */
1353
1354 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1355    the appropriate function call.  */
1356
1357 tree
1358 build_member_call (type, name, parmlist)
1359      tree type, name, parmlist;
1360 {
1361   tree t;
1362   tree method_name;
1363   int dtor = 0;
1364   tree basetype_path, decl;
1365
1366   if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1367       && TREE_CODE (type) == NAMESPACE_DECL)
1368     {
1369       /* 'name' already refers to the decls from the namespace, since we
1370          hit do_identifier for template_ids.  */
1371       my_friendly_assert (is_overloaded_fn (TREE_OPERAND (name, 0)), 980519);
1372       return build_x_function_call (name, parmlist, current_class_ref);
1373     }
1374
1375   if (type == std_node)
1376     return build_x_function_call (do_scoped_id (name, 0), parmlist,
1377                                   current_class_ref);
1378   if (TREE_CODE (type) == NAMESPACE_DECL)
1379     return build_x_function_call (lookup_namespace_name (type, name),
1380                                   parmlist, current_class_ref);
1381
1382   if (TREE_CODE (name) != TEMPLATE_ID_EXPR)
1383     method_name = name;
1384   else
1385     method_name = TREE_OPERAND (name, 0);
1386
1387   if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1388     {
1389       method_name = TREE_OPERAND (method_name, 0);
1390       dtor = 1;
1391     }
1392
1393   /* This shouldn't be here, and build_member_call shouldn't appear in
1394      parse.y!  (mrs)  */
1395   if (type && TREE_CODE (type) == IDENTIFIER_NODE
1396       && get_aggr_from_typedef (type, 0) == 0)
1397     {
1398       tree ns = lookup_name (type, 0);
1399       if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1400         {
1401           return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref);
1402         }
1403     }
1404
1405   if (type == NULL_TREE || ! is_aggr_type (type, 1))
1406     return error_mark_node;
1407
1408   /* An operator we did not like.  */
1409   if (name == NULL_TREE)
1410     return error_mark_node;
1411
1412   if (dtor)
1413     {
1414       cp_error ("cannot call destructor `%T::~%T' without object", type,
1415                 method_name);
1416       return error_mark_node;
1417     }
1418
1419   decl = maybe_dummy_object (type, &basetype_path);
1420
1421   /* Convert 'this' to the specified type to disambiguate conversion
1422      to the function's context.  Apparently Standard C++ says that we
1423      shouldn't do this.  */
1424   if (decl == current_class_ref
1425       && ! pedantic
1426       && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
1427     {
1428       tree olddecl = current_class_ptr;
1429       tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1430       if (oldtype != type)
1431         {
1432           tree newtype = build_qualified_type (type, TYPE_QUALS (oldtype));
1433           decl = convert_force (build_pointer_type (newtype), olddecl, 0);
1434           decl = build_indirect_ref (decl, NULL_PTR);
1435         }
1436     }
1437
1438   if (method_name == constructor_name (type)
1439       || method_name == constructor_name_full (type))
1440     return build_functional_cast (type, parmlist);
1441   if ((t = lookup_fnfields (basetype_path, method_name, 0)))
1442     return build_method_call (decl, 
1443                               TREE_CODE (name) == TEMPLATE_ID_EXPR
1444                               ? name : method_name,
1445                               parmlist, basetype_path,
1446                               LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1447   if (TREE_CODE (name) == IDENTIFIER_NODE
1448       && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1449     {
1450       if (t == error_mark_node)
1451         return error_mark_node;
1452       if (TREE_CODE (t) == FIELD_DECL)
1453         {
1454           if (is_dummy_object (decl))
1455             {
1456               cp_error ("invalid use of non-static field `%D'", t);
1457               return error_mark_node;
1458             }
1459           decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1460         }
1461       else if (TREE_CODE (t) == VAR_DECL)
1462         decl = t;
1463       else
1464         {
1465           cp_error ("invalid use of member `%D'", t);
1466           return error_mark_node;
1467         }
1468       if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1469         return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl,
1470                                parmlist, NULL_TREE);
1471       return build_function_call (decl, parmlist);
1472     }
1473   else
1474     {
1475       cp_error ("no method `%T::%D'", type, name);
1476       return error_mark_node;
1477     }
1478 }
1479
1480 /* Build a reference to a member of an aggregate.  This is not a
1481    C++ `&', but really something which can have its address taken,
1482    and then act as a pointer to member, for example TYPE :: FIELD
1483    can have its address taken by saying & TYPE :: FIELD.
1484
1485    @@ Prints out lousy diagnostics for operator <typename>
1486    @@ fields.
1487
1488    @@ This function should be rewritten and placed in search.c.  */
1489
1490 tree
1491 build_offset_ref (type, name)
1492      tree type, name;
1493 {
1494   tree decl, fnfields, fields, t = error_mark_node;
1495   tree basebinfo = NULL_TREE;
1496   tree orig_name = name;
1497
1498   /* class templates can come in as TEMPLATE_DECLs here.  */
1499   if (TREE_CODE (name) == TEMPLATE_DECL)
1500     return name;
1501
1502   if (type == std_node)
1503     return do_scoped_id (name, 0);
1504
1505   if (processing_template_decl || uses_template_parms (type))
1506     return build_min_nt (SCOPE_REF, type, name);
1507
1508   /* Handle namespace names fully here.  */
1509   if (TREE_CODE (type) == NAMESPACE_DECL)
1510     {
1511       t = lookup_namespace_name (type, name);
1512       if (t != error_mark_node && ! type_unknown_p (t))
1513         {
1514           mark_used (t);
1515           t = convert_from_reference (t);
1516         }
1517       return t;
1518     }
1519
1520   if (type == NULL_TREE || ! is_aggr_type (type, 1))
1521     return error_mark_node;
1522
1523   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1524     {
1525       /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1526          something like `a.template f<int>' or the like.  For the most
1527          part, we treat this just like a.f.  We do remember, however,
1528          the template-id that was used.  */
1529       name = TREE_OPERAND (orig_name, 0);
1530
1531       if (TREE_CODE (name) == LOOKUP_EXPR)
1532         /* This can happen during tsubst'ing.  */
1533         name = TREE_OPERAND (name, 0);
1534
1535       my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1536     }
1537
1538   if (TREE_CODE (name) == BIT_NOT_EXPR)
1539     {
1540       if (! check_dtor_name (type, name))
1541         cp_error ("qualified type `%T' does not match destructor name `~%T'",
1542                   type, TREE_OPERAND (name, 0));
1543       name = dtor_identifier;
1544     }
1545 #if 0
1546   /* I think this is wrong, but the draft is unclear.  --jason 6/15/98 */
1547   else if (name == constructor_name_full (type)
1548            || name == constructor_name (type))
1549     name = ctor_identifier;
1550 #endif
1551
1552   if (TYPE_SIZE (complete_type (type)) == 0
1553       && !TYPE_BEING_DEFINED (type))
1554     {
1555       cp_error ("incomplete type `%T' does not have member `%D'", type,
1556                 name);
1557       return error_mark_node;
1558     }
1559
1560   decl = maybe_dummy_object (type, &basebinfo);
1561
1562   fnfields = lookup_fnfields (basebinfo, name, 1);
1563   fields = lookup_field (basebinfo, name, 0, 0);
1564
1565   if (fields == error_mark_node || fnfields == error_mark_node)
1566     return error_mark_node;
1567
1568   /* A lot of this logic is now handled in lookup_field and
1569      lookup_fnfield.  */
1570   if (fnfields)
1571     {
1572       /* Go from the TREE_BASELINK to the member function info.  */
1573       t = TREE_VALUE (fnfields);
1574
1575       if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1576         {
1577           /* The FNFIELDS are going to contain functions that aren't
1578              necessarily templates, and templates that don't
1579              necessarily match the explicit template parameters.  We
1580              save all the functions, and the explicit parameters, and
1581              then figure out exactly what to instantiate with what
1582              arguments in instantiate_type.  */
1583
1584           if (TREE_CODE (t) != OVERLOAD)
1585             /* The code in instantiate_type which will process this
1586                expects to encounter OVERLOADs, not raw functions.  */
1587             t = ovl_cons (t, NULL_TREE);
1588           
1589           return build (OFFSET_REF, 
1590                         build_offset_type (type, unknown_type_node),
1591                         decl,
1592                         build (TEMPLATE_ID_EXPR, 
1593                                TREE_TYPE (t),
1594                                t,
1595                                TREE_OPERAND (orig_name, 1)));
1596         }
1597
1598       if (!really_overloaded_fn (t))
1599         {
1600           tree access;
1601
1602           /* Get rid of a potential OVERLOAD around it */
1603           t = OVL_CURRENT (t);
1604
1605           /* unique functions are handled easily.  */
1606           basebinfo = TREE_PURPOSE (fnfields);
1607           access = compute_access (basebinfo, t);
1608           if (access == access_protected_node)
1609             {
1610               cp_error_at ("member function `%#D' is protected", t);
1611               error ("in this context");
1612               return error_mark_node;
1613             }
1614           if (access == access_private_node)
1615             {
1616               cp_error_at ("member function `%#D' is private", t);
1617               error ("in this context");
1618               return error_mark_node;
1619             }
1620           mark_used (t);
1621           return build (OFFSET_REF, TREE_TYPE (t), decl, t);
1622         }
1623
1624       /* FNFIELDS is most likely allocated on the search_obstack,
1625          which will go away after this class scope.  If we need
1626          to save this value for later (i.e. for use as an initializer
1627          for a static variable), then do so here.
1628
1629          ??? The smart thing to do for the case of saving initializers
1630          is to resolve them before we're done with this scope.  */
1631       if (!TREE_PERMANENT (fnfields)
1632           && ! allocation_temporary_p ())
1633         fnfields = copy_list (fnfields);
1634
1635       t = build_tree_list (error_mark_node, fnfields);
1636       TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
1637       return t;
1638     }
1639
1640   /* Now that we know we are looking for a field, see if we
1641      have access to that field.  Lookup_field will give us the
1642      error message.  */
1643
1644   t = lookup_field (basebinfo, name, 1, 0);
1645
1646   if (t == error_mark_node)
1647     return error_mark_node;
1648
1649   if (t == NULL_TREE)
1650     {
1651       cp_error ("`%D' is not a member of type `%T'", name, type);
1652       return error_mark_node;
1653     }
1654
1655   if (TREE_CODE (t) == TYPE_DECL)
1656     {
1657       TREE_USED (t) = 1;
1658       return t;
1659     }
1660   /* static class members and class-specific enum
1661      values can be returned without further ado.  */
1662   if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1663     {
1664       mark_used (t);
1665       return convert_from_reference (t);
1666     }
1667
1668   if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
1669     {
1670       cp_error ("illegal pointer to bit field `%D'", t);
1671       return error_mark_node;
1672     }
1673
1674   /* static class functions too.  */
1675   if (TREE_CODE (t) == FUNCTION_DECL
1676       && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1677     my_friendly_abort (53);
1678
1679   /* In member functions, the form `type::name' is no longer
1680      equivalent to `this->type::name', at least not until
1681      resolve_offset_ref.  */
1682   return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1683 }
1684
1685 /* If a OFFSET_REF made it through to here, then it did
1686    not have its address taken.  */
1687
1688 tree
1689 resolve_offset_ref (exp)
1690      tree exp;
1691 {
1692   tree type = TREE_TYPE (exp);
1693   tree base = NULL_TREE;
1694   tree member;
1695   tree basetype, addr;
1696
1697   if (TREE_CODE (exp) == TREE_LIST)
1698     {
1699       cp_pedwarn ("assuming & on overloaded member function");
1700       return build_unary_op (ADDR_EXPR, exp, 0);
1701     }
1702
1703   if (TREE_CODE (exp) == OFFSET_REF)
1704     {
1705       member = TREE_OPERAND (exp, 1);
1706       base = TREE_OPERAND (exp, 0);
1707     }
1708   else
1709     {
1710       my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1711       if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1712         {
1713           error ("object missing in use of pointer-to-member construct");
1714           return error_mark_node;
1715         }
1716       member = exp;
1717       type = TREE_TYPE (type);
1718       base = current_class_ref;
1719     }
1720
1721   if ((TREE_CODE (member) == VAR_DECL
1722        && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1723        && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
1724       || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE
1725       || TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1726     {
1727       /* These were static members.  */
1728       if (mark_addressable (member) == 0)
1729         return error_mark_node;
1730       return member;
1731     }
1732
1733   if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1734       && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1735     return member;
1736
1737   /* Syntax error can cause a member which should
1738      have been seen as static to be grok'd as non-static.  */
1739   if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
1740     {
1741       if (TREE_ADDRESSABLE (member) == 0)
1742         {
1743           cp_error_at ("member `%D' is non-static but referenced as a static member",
1744                        member);
1745           error ("at this point in file");
1746           TREE_ADDRESSABLE (member) = 1;
1747         }
1748       return error_mark_node;
1749     }
1750
1751   /* The first case is really just a reference to a member of `this'.  */
1752   if (TREE_CODE (member) == FIELD_DECL
1753       && (base == current_class_ref || is_dummy_object (base)))
1754     {
1755       tree basetype_path;
1756       tree access;
1757       tree expr;
1758
1759       if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
1760         basetype = TYPE_OFFSET_BASETYPE (type);
1761       else
1762         basetype = DECL_CONTEXT (member);
1763
1764       base = current_class_ptr;
1765       
1766       if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
1767         {
1768           error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
1769           return error_mark_node;
1770         }
1771       /* Kludge: we need to use basetype_path now, because
1772          convert_pointer_to will bash it.  */
1773       access = compute_access (basetype_path, member);
1774       addr = convert_pointer_to (basetype, base);
1775
1776       /* Issue errors if there was an access violation.  */
1777       if (access != access_public_node)
1778         {
1779           cp_error_at ("member `%D' is %s", 
1780                        access == access_private_node 
1781                        ? "private" : "protected",
1782                        member);
1783           cp_error ("in this context");
1784         } 
1785
1786       /* Even in the case of illegal access, we form the
1787          COMPONENT_REF; that will allow better error recovery than
1788          just feeding back error_mark_node.  */
1789       expr = build (COMPONENT_REF, TREE_TYPE (member),
1790                     build_indirect_ref (addr, NULL_PTR), member);
1791       return convert_from_reference (expr);
1792     }
1793
1794   /* Ensure that we have an object.  */
1795   if (is_dummy_object (base))
1796     addr = error_mark_node;
1797   else
1798     /* If this is a reference to a member function, then return the
1799        address of the member function (which may involve going
1800        through the object's vtable), otherwise, return an expression
1801        for the dereferenced pointer-to-member construct.  */
1802     addr = build_unary_op (ADDR_EXPR, base, 0);
1803
1804   if (TYPE_PTRMEM_P (TREE_TYPE (member)))
1805     {
1806       if (addr == error_mark_node)
1807         {
1808           cp_error ("object missing in `%E'", exp);
1809           return error_mark_node;
1810         }
1811
1812       basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
1813       addr = convert_pointer_to (basetype, addr);
1814       member = cp_convert (ptrdiff_type_node, member);
1815       
1816       /* Pointer to data members are offset by one, so that a null
1817          pointer with a real value of 0 is distinguishable from an
1818          offset of the first member of a structure.  */
1819       member = build_binary_op (MINUS_EXPR, member,
1820                                 cp_convert (ptrdiff_type_node, integer_one_node),
1821                                 0);
1822
1823       return build1 (INDIRECT_REF, type,
1824                      build (PLUS_EXPR, build_pointer_type (type),
1825                             addr, member));
1826     }
1827   else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
1828     {
1829       return get_member_function_from_ptrfunc (&addr, member);
1830     }
1831   my_friendly_abort (56);
1832   /* NOTREACHED */
1833   return NULL_TREE;
1834 }
1835
1836 /* Return either DECL or its known constant value (if it has one).  */
1837
1838 tree
1839 decl_constant_value (decl)
1840      tree decl;
1841 {
1842   if (! TREE_THIS_VOLATILE (decl)
1843       && DECL_INITIAL (decl)
1844       && DECL_INITIAL (decl) != error_mark_node
1845       /* This is invalid if initial value is not constant.
1846          If it has either a function call, a memory reference,
1847          or a variable, then re-evaluating it could give different results.  */
1848       && TREE_CONSTANT (DECL_INITIAL (decl))
1849       /* Check for cases where this is sub-optimal, even though valid.  */
1850       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1851     return DECL_INITIAL (decl);
1852   return decl;
1853 }
1854 \f
1855 /* Common subroutines of build_new and build_vec_delete.  */
1856
1857 /* Call the global __builtin_delete to delete ADDR.  */
1858
1859 static tree
1860 build_builtin_delete_call (addr)
1861      tree addr;
1862 {
1863   tree BID = get_first_fn
1864     (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) DELETE_EXPR]));
1865
1866   assemble_external (BID);
1867   return build_call (BID, void_type_node, build_expr_list (NULL_TREE, addr));
1868 }
1869 \f
1870 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1871    (which needs to go through some sort of groktypename) or it
1872    is the name of the class we are newing. INIT is an initialization value.
1873    It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1874    If INIT is void_type_node, it means do *not* call a constructor
1875    for this instance.
1876
1877    For types with constructors, the data returned is initialized
1878    by the appropriate constructor.
1879
1880    Whether the type has a constructor or not, if it has a pointer
1881    to a virtual function table, then that pointer is set up
1882    here.
1883
1884    Unless I am mistaken, a call to new () will return initialized
1885    data regardless of whether the constructor itself is private or
1886    not.  NOPE; new fails if the constructor is private (jcm).
1887
1888    Note that build_new does nothing to assure that any special
1889    alignment requirements of the type are met.  Rather, it leaves
1890    it up to malloc to do the right thing.  Otherwise, folding to
1891    the right alignment cal cause problems if the user tries to later
1892    free the memory returned by `new'.
1893
1894    PLACEMENT is the `placement' list for user-defined operator new ().  */
1895
1896 extern int flag_check_new;
1897
1898 tree
1899 build_new (placement, decl, init, use_global_new)
1900      tree placement;
1901      tree decl, init;
1902      int use_global_new;
1903 {
1904   tree type, rval;
1905   tree nelts = NULL_TREE, t;
1906   int has_array = 0;
1907
1908   tree pending_sizes = NULL_TREE;
1909
1910   if (decl == error_mark_node)
1911     return error_mark_node;
1912
1913   if (TREE_CODE (decl) == TREE_LIST)
1914     {
1915       tree absdcl = TREE_VALUE (decl);
1916       tree last_absdcl = NULL_TREE;
1917       int old_immediate_size_expand = 0;
1918
1919       if (current_function_decl
1920           && DECL_CONSTRUCTOR_P (current_function_decl))
1921         {
1922           old_immediate_size_expand = immediate_size_expand;
1923           immediate_size_expand = 0;
1924         }
1925
1926       nelts = integer_one_node;
1927
1928       if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1929         my_friendly_abort (215);
1930       while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1931         {
1932           last_absdcl = absdcl;
1933           absdcl = TREE_OPERAND (absdcl, 0);
1934         }
1935
1936       if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1937         {
1938           /* probably meant to be a vec new */
1939           tree this_nelts;
1940
1941           while (TREE_OPERAND (absdcl, 0)
1942                  && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1943             {
1944               last_absdcl = absdcl;
1945               absdcl = TREE_OPERAND (absdcl, 0);
1946             }
1947
1948           has_array = 1;
1949           this_nelts = TREE_OPERAND (absdcl, 1);
1950           if (this_nelts != error_mark_node)
1951             {
1952               if (this_nelts == NULL_TREE)
1953                 error ("new of array type fails to specify size");
1954               else if (processing_template_decl)
1955                 {
1956                   nelts = this_nelts;
1957                   absdcl = TREE_OPERAND (absdcl, 0);
1958                 }
1959               else
1960                 {
1961                   this_nelts = save_expr (cp_convert (sizetype, this_nelts));
1962                   absdcl = TREE_OPERAND (absdcl, 0);
1963                   if (this_nelts == integer_zero_node)
1964                     {
1965                       warning ("zero size array reserves no space");
1966                       nelts = integer_zero_node;
1967                     }
1968                   else
1969                     nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
1970                 }
1971             }
1972           else
1973             nelts = integer_zero_node;
1974         }
1975
1976       if (last_absdcl)
1977         TREE_OPERAND (last_absdcl, 0) = absdcl;
1978       else
1979         TREE_VALUE (decl) = absdcl;
1980
1981       type = groktypename (decl);
1982       if (! type || type == error_mark_node)
1983         {
1984           immediate_size_expand = old_immediate_size_expand;
1985           return error_mark_node;
1986         }
1987
1988       if (current_function_decl
1989           && DECL_CONSTRUCTOR_P (current_function_decl))
1990         {
1991           pending_sizes = get_pending_sizes ();
1992           immediate_size_expand = old_immediate_size_expand;
1993         }
1994     }
1995   else if (TREE_CODE (decl) == IDENTIFIER_NODE)
1996     {
1997       if (IDENTIFIER_HAS_TYPE_VALUE (decl))
1998         {
1999           /* An aggregate type.  */
2000           type = IDENTIFIER_TYPE_VALUE (decl);
2001           decl = TYPE_MAIN_DECL (type);
2002         }
2003       else
2004         {
2005           /* A builtin type.  */
2006           decl = lookup_name (decl, 1);
2007           my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2008           type = TREE_TYPE (decl);
2009         }
2010     }
2011   else if (TREE_CODE (decl) == TYPE_DECL)
2012     {
2013       type = TREE_TYPE (decl);
2014     }
2015   else
2016     {
2017       type = decl;
2018       decl = TYPE_MAIN_DECL (type);
2019     }
2020
2021   if (processing_template_decl)
2022     {
2023       if (has_array)
2024         t = min_tree_cons (min_tree_cons (NULL_TREE, type, NULL_TREE),
2025                            build_min_nt (ARRAY_REF, NULL_TREE, nelts),
2026                            NULL_TREE);
2027       else
2028         t = type;
2029         
2030       rval = build_min_nt (NEW_EXPR, placement, t, init);
2031       NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2032       return rval;
2033     }
2034
2035   /* ``A reference cannot be created by the new operator.  A reference
2036      is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2037      returned by new.'' ARM 5.3.3 */
2038   if (TREE_CODE (type) == REFERENCE_TYPE)
2039     {
2040       error ("new cannot be applied to a reference type");
2041       type = TREE_TYPE (type);
2042     }
2043
2044   if (TREE_CODE (type) == FUNCTION_TYPE)
2045     {
2046       error ("new cannot be applied to a function type");
2047       return error_mark_node;
2048     }
2049
2050   /* When the object being created is an array, the new-expression yields a
2051      pointer to the initial element (if any) of the array.  For example,
2052      both new int and new int[10] return an int*.  5.3.4.  */
2053   if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2054     {
2055       nelts = array_type_nelts_top (type);
2056       has_array = 1;
2057       type = TREE_TYPE (type);
2058     }
2059
2060   if (has_array)
2061     t = build_nt (ARRAY_REF, type, nelts);
2062   else
2063     t = type;
2064
2065   rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
2066   NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2067   TREE_SIDE_EFFECTS (rval) = 1;
2068
2069   /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain.  */
2070   rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2071   TREE_NO_UNUSED_WARNING (rval) = 1;
2072
2073   if (pending_sizes)
2074     rval = build_compound_expr (chainon (pending_sizes,
2075                                          build_expr_list (NULL_TREE, rval)));
2076
2077   return rval;
2078 }
2079
2080 /* If non-NULL, a POINTER_TYPE equivalent to (java::lang::Class*). */
2081
2082 static tree jclass_node = NULL_TREE;
2083
2084 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2085
2086 tree
2087 build_java_class_ref (type)
2088      tree type;
2089 {
2090   tree name, class_decl;
2091   static tree CL_prefix = NULL_TREE;
2092   if (CL_prefix == NULL_TREE)
2093     CL_prefix = get_identifier("_CL_");
2094   if (jclass_node == NULL_TREE)
2095     {
2096       jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2097       if (jclass_node == NULL_TREE)
2098         fatal("call to Java constructor, while `jclass' undefined");
2099       jclass_node = TREE_TYPE (jclass_node);
2100     }
2101   name = build_overload_with_type (CL_prefix, type);
2102   class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2103   if (class_decl == NULL_TREE)
2104     {
2105       push_obstacks_nochange ();
2106       end_temporary_allocation ();
2107       class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2108       TREE_STATIC (class_decl) = 1;
2109       DECL_EXTERNAL (class_decl) = 1;
2110       TREE_PUBLIC (class_decl) = 1;
2111       DECL_ARTIFICIAL (class_decl) = 1;
2112       DECL_IGNORED_P (class_decl) = 1;
2113       pushdecl_top_level (class_decl);
2114       make_decl_rtl (class_decl, NULL_PTR, 1);
2115       pop_obstacks ();
2116     }
2117   return class_decl;
2118 }
2119
2120 /* Called from cplus_expand_expr when expanding a NEW_EXPR.  The return
2121    value is immediately handed to expand_expr.  */
2122
2123 tree
2124 build_new_1 (exp)
2125      tree exp;
2126 {
2127   tree placement, init;
2128   tree type, true_type, size, rval;
2129   tree nelts = NULL_TREE;
2130   tree alloc_expr, alloc_node = NULL_TREE;
2131   int has_array = 0;
2132   enum tree_code code = NEW_EXPR;
2133   int use_cookie, nothrow, check_new;
2134   int use_global_new;
2135   int use_java_new = 0;
2136
2137   placement = TREE_OPERAND (exp, 0);
2138   type = TREE_OPERAND (exp, 1);
2139   init = TREE_OPERAND (exp, 2);
2140   use_global_new = NEW_EXPR_USE_GLOBAL (exp);
2141
2142   if (TREE_CODE (type) == ARRAY_REF)
2143     {
2144       has_array = 1;
2145       nelts = TREE_OPERAND (type, 1);
2146       type = TREE_OPERAND (type, 0);
2147     }
2148   true_type = type;
2149
2150   if (CP_TYPE_QUALS (type))
2151     type = TYPE_MAIN_VARIANT (type);
2152
2153   /* If our base type is an array, then make sure we know how many elements
2154      it has.  */
2155   while (TREE_CODE (true_type) == ARRAY_TYPE)
2156     {
2157       tree this_nelts = array_type_nelts_top (true_type);
2158       nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
2159       true_type = TREE_TYPE (true_type);
2160     }
2161
2162   if (!complete_type_or_else (true_type))
2163     return error_mark_node;
2164
2165   if (has_array)
2166     size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2167                                   nelts, 1));
2168   else
2169     size = size_in_bytes (type);
2170
2171   if (TREE_CODE (true_type) == VOID_TYPE)
2172     {
2173       error ("invalid type `void' for new");
2174       return error_mark_node;
2175     }
2176
2177   if (TYPE_LANG_SPECIFIC (true_type)
2178       && CLASSTYPE_ABSTRACT_VIRTUALS (true_type))
2179     {
2180       abstract_virtuals_error (NULL_TREE, true_type);
2181       return error_mark_node;
2182     }
2183
2184   if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type))
2185     {
2186       signature_error (NULL_TREE, true_type);
2187       return error_mark_node;
2188     }
2189
2190 #if 1
2191   /* Get a little extra space to store a couple of things before the new'ed
2192      array, if this isn't the default placement new.  */
2193
2194   use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)
2195                 && ! (placement && ! TREE_CHAIN (placement)
2196                       && TREE_TYPE (TREE_VALUE (placement)) == ptr_type_node));
2197 #else
2198   /* Get a little extra space to store a couple of things before the new'ed
2199      array, if this is either non-placement new or new (nothrow).  */
2200   
2201   use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)
2202                 && (! placement || nothrow));
2203 #endif
2204
2205   if (use_cookie)
2206     {
2207       tree extra = BI_header_size;
2208
2209       size = size_binop (PLUS_EXPR, size, extra);
2210     }
2211
2212   if (has_array)
2213     {
2214       code = VEC_NEW_EXPR;
2215
2216       if (init && pedantic)
2217         cp_pedwarn ("initialization in array new");
2218     }
2219
2220   /* Allocate the object.  */
2221   
2222   if (! has_array && ! placement && flag_this_is_variable > 0
2223       && TYPE_NEEDS_CONSTRUCTING (true_type) && init != void_type_node)
2224     {
2225       if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
2226         rval = NULL_TREE;
2227       else
2228         {
2229           error ("constructors take parameter lists");
2230           return error_mark_node;
2231         }
2232     }
2233   else if (! placement && TYPE_FOR_JAVA (true_type))
2234     {
2235       tree class_addr, alloc_decl;
2236       tree class_decl = build_java_class_ref (true_type);
2237       tree class_size = size_in_bytes (true_type);
2238       static char alloc_name[] = "_Jv_AllocObject";
2239       use_java_new = 1;
2240       alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2241       if (alloc_decl == NULL_TREE)
2242         fatal("call to Java constructor, while `%s' undefined", alloc_name);
2243       class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2244       rval = build_function_call (alloc_decl,
2245                                   tree_cons (NULL_TREE, class_addr,
2246                                              build_tree_list (NULL_TREE,
2247                                                               class_size)));
2248       rval = cp_convert (build_pointer_type (true_type), rval);
2249     }
2250   else
2251     {
2252       int susp = 0;
2253
2254       if (flag_exceptions)
2255         /* We will use RVAL when generating an exception handler for
2256            this new-expression, so we must save it.  */
2257         susp = suspend_momentary ();
2258
2259       rval = build_op_new_call
2260         (code, true_type, expr_tree_cons (NULL_TREE, size, placement),
2261          LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL));
2262       rval = cp_convert (build_pointer_type (true_type), rval);
2263
2264       if (flag_exceptions)
2265         resume_momentary (susp);
2266     }
2267
2268   /*        unless an allocation function is declared with an empty  excep-
2269      tion-specification  (_except.spec_),  throw(), it indicates failure to
2270      allocate storage by throwing a bad_alloc exception  (clause  _except_,
2271      _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2272      cation function is declared  with  an  empty  exception-specification,
2273      throw(), it returns null to indicate failure to allocate storage and a
2274      non-null pointer otherwise.
2275
2276      So check for a null exception spec on the op new we just called.  */
2277
2278   nothrow = 0;
2279   if (rval)
2280     {
2281       /* The CALL_EXPR.  */
2282       tree t = TREE_OPERAND (rval, 0);
2283       /* The function.  */
2284       t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2285       t = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (t));
2286
2287       if (t && TREE_VALUE (t) == NULL_TREE)
2288         nothrow = 1;
2289     }
2290   check_new = (flag_check_new || nothrow) && ! use_java_new;
2291
2292   if ((check_new || flag_exceptions) && rval)
2293     {
2294       alloc_expr = get_target_expr (rval);
2295       alloc_node = rval = TREE_OPERAND (alloc_expr, 0);
2296     }
2297   else
2298     alloc_expr = NULL_TREE;
2299
2300   /* if rval is NULL_TREE I don't have to allocate it, but are we totally
2301      sure we have some extra bytes in that case for the BI_header_size
2302      cookies? And how does that interact with the code below? (mrs) */
2303   /* Finish up some magic for new'ed arrays */
2304   if (use_cookie && rval != NULL_TREE)
2305     {
2306       tree extra = BI_header_size;
2307       tree cookie, exp1;
2308       rval = convert (string_type_node, rval); /* for ptr arithmetic */
2309       rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1));
2310       /* Store header info.  */
2311       cookie = build_indirect_ref (build (MINUS_EXPR,
2312                                           build_pointer_type (BI_header_type),
2313                                           rval, extra), NULL_PTR);
2314       exp1 = build (MODIFY_EXPR, void_type_node,
2315                     build_component_ref (cookie, nc_nelts_field_id,
2316                                          NULL_TREE, 0),
2317                     nelts);
2318       TREE_SIDE_EFFECTS (exp1) = 1;
2319       rval = cp_convert (build_pointer_type (true_type), rval);
2320       rval = build_compound_expr
2321         (expr_tree_cons (NULL_TREE, exp1,
2322                          build_expr_list (NULL_TREE, rval)));
2323     }
2324
2325   if (rval == error_mark_node)
2326     return error_mark_node;
2327
2328   /* Don't call any constructors or do any initialization.  */
2329   if (init == void_type_node)
2330     goto done;
2331
2332   if (TYPE_NEEDS_CONSTRUCTING (type) || init)
2333     {
2334       if (! TYPE_NEEDS_CONSTRUCTING (type)
2335           && ! IS_AGGR_TYPE (type) && ! has_array)
2336         {
2337           /* We are processing something like `new int (10)', which
2338              means allocate an int, and initialize it with 10.  */
2339           tree deref;
2340           tree deref_type;
2341
2342           /* At present RVAL is a temporary variable, created to hold
2343              the value from the call to `operator new'.  We transform
2344              it to (*RVAL = INIT, RVAL).  */
2345           rval = save_expr (rval);
2346           deref = build_indirect_ref (rval, NULL_PTR);
2347
2348           /* Even for something like `new const int (10)' we must
2349              allow the expression to be non-const while we do the
2350              initialization.  */
2351           deref_type = TREE_TYPE (deref);
2352           if (CP_TYPE_CONST_P (deref_type))
2353             TREE_TYPE (deref) 
2354               = cp_build_qualified_type (deref_type,
2355                                          CP_TYPE_QUALS (deref_type) 
2356                                          & ~TYPE_QUAL_CONST);
2357           TREE_READONLY (deref) = 0;
2358
2359           if (TREE_CHAIN (init) != NULL_TREE)
2360             pedwarn ("initializer list being treated as compound expression");
2361           else if (TREE_CODE (init) == CONSTRUCTOR)
2362             {
2363               pedwarn ("initializer list appears where operand should be used");
2364               init = TREE_OPERAND (init, 1);
2365             }
2366           init = build_compound_expr (init);
2367
2368           init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
2369                                              "new", NULL_TREE, 0);
2370           rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
2371                         build_modify_expr (deref, NOP_EXPR, init),
2372                         rval);
2373           TREE_NO_UNUSED_WARNING (rval) = 1;
2374           TREE_SIDE_EFFECTS (rval) = 1;
2375         }
2376       else if (! has_array)
2377         {
2378           tree newrval;
2379           /* Constructors are never virtual. If it has an initialization, we
2380              need to complain if we aren't allowed to use the ctor that took
2381              that argument.  */
2382           int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
2383
2384           if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
2385             {
2386               init = expr_tree_cons (NULL_TREE, integer_one_node, init);
2387               flags |= LOOKUP_HAS_IN_CHARGE;
2388             }
2389
2390           if (use_java_new)
2391             rval = save_expr (rval);
2392           newrval = rval;
2393
2394           if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
2395             newrval = build_indirect_ref (newrval, NULL_PTR);
2396
2397           newrval = build_method_call (newrval, ctor_identifier,
2398                                        init, TYPE_BINFO (true_type), flags);
2399
2400           if (newrval == NULL_TREE || newrval == error_mark_node)
2401             return error_mark_node;
2402
2403           /* Java constructors compiled by jc1 do not return this. */
2404           if (use_java_new)
2405             newrval = build (COMPOUND_EXPR, TREE_TYPE (newrval),
2406                              newrval, rval);
2407           rval = newrval;
2408           TREE_HAS_CONSTRUCTOR (rval) = 1;
2409         }
2410       else
2411         rval = build (VEC_INIT_EXPR, TREE_TYPE (rval),
2412                       save_expr (rval), init, nelts);
2413
2414       /* If any part of the object initialization terminates by throwing
2415          an exception and the new-expression does not contain a
2416          new-placement, then the deallocation function is called to free
2417          the memory in which the object was being constructed.  */
2418       if (flag_exceptions && alloc_expr && ! use_java_new)
2419         {
2420           enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2421           tree cleanup, fn = NULL_TREE;
2422           int flags = LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL);
2423
2424           /* All cleanups must last longer than normal.  */
2425           int yes = suspend_momentary ();
2426
2427           if (placement)
2428             {
2429               flags |= LOOKUP_SPECULATIVELY;
2430
2431               /* We expect alloc_expr to look like a TARGET_EXPR around
2432                  a NOP_EXPR around the CALL_EXPR we want.  */
2433               fn = TREE_OPERAND (alloc_expr, 1);
2434               fn = TREE_OPERAND (fn, 0);
2435             }
2436
2437           /* Copy size to the saveable obstack.  */
2438           size = copy_node (size);
2439
2440           cleanup = build_op_delete_call (dcode, alloc_node, size, flags, fn);
2441
2442           resume_momentary (yes);
2443
2444           /* Ack!  First we allocate the memory.  Then we set our sentry
2445              variable to true, and expand a cleanup that deletes the memory
2446              if sentry is true.  Then we run the constructor and store the
2447              returned pointer in buf.  Then we clear sentry and return buf.  */
2448
2449           if (cleanup)
2450             {
2451               tree end, sentry, begin, buf, t = TREE_TYPE (rval);
2452
2453               begin = get_target_expr (boolean_true_node);
2454               sentry = TREE_OPERAND (begin, 0);
2455
2456               yes = suspend_momentary ();
2457               TREE_OPERAND (begin, 2)
2458                 = build (COND_EXPR, void_type_node, sentry,
2459                          cleanup, void_zero_node);
2460               resume_momentary (yes);
2461
2462               rval = get_target_expr (rval);
2463
2464               end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2465                            sentry, boolean_false_node);
2466               TREE_SIDE_EFFECTS (end) = 1;
2467
2468               buf = TREE_OPERAND (rval, 0);
2469
2470               rval = build (COMPOUND_EXPR, t, begin,
2471                             build (COMPOUND_EXPR, t, rval,
2472                                    build (COMPOUND_EXPR, t, end, buf)));
2473             }
2474         }
2475     }
2476   else if (CP_TYPE_CONST_P (true_type))
2477     cp_error ("uninitialized const in `new' of `%#T'", true_type);
2478
2479  done:
2480
2481   if (alloc_expr && rval == alloc_node)
2482     {
2483       rval = TREE_OPERAND (alloc_expr, 1);
2484       alloc_expr = NULL_TREE;
2485     }
2486
2487   if (check_new && alloc_expr)
2488     {
2489       /* Did we modify the storage?  */
2490       tree ifexp = build_binary_op (NE_EXPR, alloc_node,
2491                                     integer_zero_node, 1);
2492       rval = build_conditional_expr (ifexp, rval, alloc_node);
2493     }
2494
2495   if (alloc_expr)
2496     rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2497
2498   if (rval && TREE_TYPE (rval) != build_pointer_type (type))
2499     {
2500       /* The type of new int [3][3] is not int *, but int [3] * */
2501       rval = build_c_cast (build_pointer_type (type), rval);
2502     }
2503
2504   return rval;
2505 }
2506 \f
2507 static tree
2508 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,
2509                     use_global_delete)
2510      tree base, maxindex, type;
2511      tree auto_delete_vec, auto_delete;
2512      int use_global_delete;
2513 {
2514   tree virtual_size;
2515   tree ptype = build_pointer_type (type = complete_type (type));
2516   tree size_exp = size_in_bytes (type);
2517
2518   /* Temporary variables used by the loop.  */
2519   tree tbase, tbase_init;
2520
2521   /* This is the body of the loop that implements the deletion of a
2522      single element, and moves temp variables to next elements.  */
2523   tree body;
2524
2525   /* This is the LOOP_EXPR that governs the deletion of the elements.  */
2526   tree loop;
2527
2528   /* This is the thing that governs what to do after the loop has run.  */
2529   tree deallocate_expr = 0;
2530
2531   /* This is the BIND_EXPR which holds the outermost iterator of the
2532      loop.  It is convenient to set this variable up and test it before
2533      executing any other code in the loop.
2534      This is also the containing expression returned by this function.  */
2535   tree controller = NULL_TREE;
2536
2537   if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
2538     {
2539       loop = integer_zero_node;
2540       goto no_destructor;
2541     }
2542
2543   /* The below is short by BI_header_size */
2544   virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
2545
2546   tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
2547   tbase_init = build_modify_expr (tbase, NOP_EXPR,
2548                                   fold (build (PLUS_EXPR, ptype,
2549                                                base,
2550                                                virtual_size)));
2551   DECL_REGISTER (tbase) = 1;
2552   controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2553   TREE_SIDE_EFFECTS (controller) = 1;
2554
2555   if (auto_delete != integer_zero_node
2556       && auto_delete != integer_two_node)
2557     {
2558       tree base_tbd = cp_convert (ptype,
2559                                   build_binary_op (MINUS_EXPR,
2560                                                    cp_convert (ptr_type_node, base),
2561                                                    BI_header_size,
2562                                                    1));
2563       /* This is the real size */
2564       virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
2565       body = build_expr_list (NULL_TREE,
2566                               build_x_delete (base_tbd,
2567                                               2 | use_global_delete,
2568                                               virtual_size));
2569       body = build (COND_EXPR, void_type_node,
2570                     build (BIT_AND_EXPR, integer_type_node,
2571                            auto_delete, integer_one_node),
2572                     body, integer_zero_node);
2573     }
2574   else
2575     body = NULL_TREE;
2576
2577   body = expr_tree_cons (NULL_TREE,
2578                     build_delete (ptype, tbase, auto_delete,
2579                                   LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2580                     body);
2581
2582   body = expr_tree_cons (NULL_TREE,
2583                     build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2584                     body);
2585
2586   body = expr_tree_cons (NULL_TREE,
2587                     build (EXIT_EXPR, void_type_node,
2588                            build (EQ_EXPR, boolean_type_node, base, tbase)),
2589                     body);
2590
2591   loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2592
2593   loop = expr_tree_cons (NULL_TREE, tbase_init,
2594                     expr_tree_cons (NULL_TREE, loop, NULL_TREE));
2595   loop = build_compound_expr (loop);
2596
2597  no_destructor:
2598   /* If the delete flag is one, or anything else with the low bit set,
2599      delete the storage.  */
2600   if (auto_delete_vec == integer_zero_node)
2601     deallocate_expr = integer_zero_node;
2602   else
2603     {
2604       tree base_tbd;
2605
2606       /* The below is short by BI_header_size */
2607       virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
2608
2609       if (! TYPE_VEC_NEW_USES_COOKIE (type))
2610         /* no header */
2611         base_tbd = base;
2612       else
2613         {
2614           base_tbd = cp_convert (ptype,
2615                                  build_binary_op (MINUS_EXPR,
2616                                                   cp_convert (string_type_node, base),
2617                                                   BI_header_size,
2618                                                   1));
2619           /* True size with header.  */
2620           virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
2621         }
2622       deallocate_expr = build_x_delete (base_tbd,
2623                                         2 | use_global_delete,
2624                                         virtual_size);
2625       if (auto_delete_vec != integer_one_node)
2626         deallocate_expr = build (COND_EXPR, void_type_node,
2627                                  build (BIT_AND_EXPR, integer_type_node,
2628                                         auto_delete_vec, integer_one_node),
2629                                  deallocate_expr, integer_zero_node);
2630     }
2631
2632   if (loop && deallocate_expr != integer_zero_node)
2633     {
2634       body = expr_tree_cons (NULL_TREE, loop,
2635                         expr_tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2636       body = build_compound_expr (body);
2637     }
2638   else
2639     body = loop;
2640
2641   /* Outermost wrapper: If pointer is null, punt.  */
2642   body = build (COND_EXPR, void_type_node,
2643                 build (NE_EXPR, boolean_type_node, base, integer_zero_node),
2644                 body, integer_zero_node);
2645   body = build1 (NOP_EXPR, void_type_node, body);
2646
2647   if (controller)
2648     {
2649       TREE_OPERAND (controller, 1) = body;
2650       return controller;
2651     }
2652   else
2653     return cp_convert (void_type_node, body);
2654 }
2655
2656 /* Protect the vector initialization with a try-block so that we can
2657    destroy the first few elements if constructing a later element
2658    causes an exception to be thrown.  TYPE is the type of the array
2659    elements.  */
2660
2661 static void
2662 expand_vec_init_try_block (type)
2663      tree type;
2664 {
2665   if (!TYPE_NEEDS_DESTRUCTOR (type) || !flag_exceptions)
2666     return;
2667
2668   /* The code we generate looks like:
2669
2670        try {
2671          // Initialize the vector.
2672        } catch (...) {
2673          // Destory the elements that need destroying.
2674          throw;
2675        } 
2676
2677      Here we're just beginning the `try'.  */
2678
2679   expand_eh_region_start ();
2680 }
2681
2682 /* Add code to destroy the array elements constructed so far if the
2683    construction of some element in the array causes an exception to be
2684    thrown.  RVAL is the address of the last element in the array.
2685    TYPE is the type of the array elements.  MAXINDEX is the maximum
2686    allowable index into the array.  ITERATOR is an integer variable
2687    indicating how many elements remain to be constructed.  */
2688
2689 static void
2690 expand_vec_init_catch_clause (rval, type, maxindex, iterator)
2691      tree rval;
2692      tree type;
2693      tree maxindex;
2694      tree iterator;
2695 {
2696   tree e;
2697   tree cleanup;
2698
2699   if (!TYPE_NEEDS_DESTRUCTOR (type) || !flag_exceptions)
2700     return;
2701     
2702   /* We have to ensure that this can live to the cleanup expansion
2703      time, since we know it is only ever needed once, generate code
2704      now.  */
2705   push_obstacks_nochange ();
2706   resume_temporary_allocation ();
2707
2708   cleanup = make_node (RTL_EXPR);
2709   TREE_TYPE (cleanup) = void_type_node;
2710   RTL_EXPR_RTL (cleanup) = const0_rtx;
2711   TREE_SIDE_EFFECTS (cleanup) = 1;
2712   do_pending_stack_adjust ();
2713   start_sequence_for_rtl_expr (cleanup);
2714     
2715   e = build_vec_delete_1 (rval,
2716                           build_binary_op (MINUS_EXPR, maxindex, 
2717                                            iterator, 1),
2718                           type,
2719                           /*auto_delete_vec=*/integer_zero_node,
2720                           /*auto_delete=*/integer_zero_node,
2721                           /*use_global_delete=*/0);
2722   expand_expr (e, const0_rtx, VOIDmode, EXPAND_NORMAL);
2723
2724   do_pending_stack_adjust ();
2725   RTL_EXPR_SEQUENCE (cleanup) = get_insns ();
2726   end_sequence ();
2727   cleanup = protect_with_terminate (cleanup);
2728   expand_eh_region_end (cleanup);
2729   pop_obstacks ();
2730 }
2731
2732 /* `expand_vec_init' performs initialization of a vector of aggregate
2733    types.
2734
2735    DECL is passed only for error reporting, and provides line number
2736    and source file name information.
2737    BASE is the space where the vector will be.
2738    MAXINDEX is the maximum index of the array (one less than the
2739             number of elements).
2740    INIT is the (possibly NULL) initializer.
2741
2742    FROM_ARRAY is 0 if we should init everything with INIT
2743    (i.e., every element initialized from INIT).
2744    FROM_ARRAY is 1 if we should index into INIT in parallel
2745    with initialization of DECL.
2746    FROM_ARRAY is 2 if we should index into INIT in parallel,
2747    but use assignment instead of initialization.  */
2748
2749 tree
2750 expand_vec_init (decl, base, maxindex, init, from_array)
2751      tree decl, base, maxindex, init;
2752      int from_array;
2753 {
2754   tree rval;
2755   tree base2 = NULL_TREE;
2756   tree type = TREE_TYPE (TREE_TYPE (base));
2757   tree size;
2758   tree itype;
2759   tree iterator;
2760   int num_initialized_elts = 0;
2761
2762   maxindex = cp_convert (ptrdiff_type_node, maxindex);
2763   if (maxindex == error_mark_node)
2764     return error_mark_node;
2765
2766   if (current_function_decl == NULL_TREE)
2767     {
2768       rval = make_tree_vec (3);
2769       TREE_VEC_ELT (rval, 0) = base;
2770       TREE_VEC_ELT (rval, 1) = maxindex;
2771       TREE_VEC_ELT (rval, 2) = init;
2772       return rval;
2773     }
2774
2775   size = size_in_bytes (type);
2776
2777   base = default_conversion (base);
2778   base = cp_convert (build_pointer_type (type), base);
2779   rval = get_temp_regvar (build_pointer_type (type), base);
2780   base = get_temp_regvar (build_pointer_type (type), base);
2781   iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2782
2783   /* Protect the entire array initialization so that we can destroy
2784      the partially constructed array if an exception is thrown.  */
2785   expand_vec_init_try_block (type);
2786
2787   if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR
2788       && (!decl || same_type_p (TREE_TYPE (init), TREE_TYPE (decl))))
2789     {
2790       /* Do non-default initialization resulting from brace-enclosed
2791          initializers.  */
2792
2793       tree elts;
2794       tree baseref = build1 (INDIRECT_REF, type, base);
2795
2796       for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2797         {
2798           tree elt = TREE_VALUE (elts);
2799
2800           num_initialized_elts++;
2801
2802           if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2803             expand_aggr_init (baseref, elt, 0);
2804           else
2805             expand_assignment (baseref, elt, 0, 0);
2806
2807           expand_assignment (base, 
2808                              build (PLUS_EXPR, build_pointer_type (type),
2809                                     base, size),
2810                              0, 0);
2811           expand_assignment (iterator,
2812                              build (MINUS_EXPR, ptrdiff_type_node,
2813                                     iterator, integer_one_node),
2814                              0, 0);
2815         }
2816
2817       /* Clear out INIT so that we don't get confused below.  */
2818       init = NULL_TREE;
2819
2820       if (obey_regdecls)
2821         use_variable (DECL_RTL (base));
2822     }
2823   else if (from_array)
2824     {
2825       /* If initializing one array from another, initialize element by
2826          element.  We rely upon the below calls the do argument
2827          checking.  */ 
2828       if (decl == NULL_TREE)
2829         {
2830           sorry ("initialization of array from dissimilar array type");
2831           return error_mark_node;
2832         }
2833       if (init)
2834         {
2835           base2 = default_conversion (init);
2836           itype = TREE_TYPE (base2);
2837           base2 = get_temp_regvar (itype, base2);
2838           itype = TREE_TYPE (itype);
2839         }
2840       else if (TYPE_LANG_SPECIFIC (type)
2841                && TYPE_NEEDS_CONSTRUCTING (type)
2842                && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2843         {
2844           error ("initializer ends prematurely");
2845           return error_mark_node;
2846         }
2847     }
2848
2849   /* Now, default-initialize any remaining elements.  We don't need to
2850      do that if a) the type does not need constructing, or b) we've
2851      already initialized all the elements.  */
2852   if (TYPE_NEEDS_CONSTRUCTING (type)
2853       && !(TREE_CODE (maxindex) == INTEGER_CST
2854            && num_initialized_elts == TREE_INT_CST_LOW (maxindex) + 1))
2855     {
2856       /* If the ITERATOR is equal to -1, then we don't have to loop;
2857          we've already initialized all the elements.  */
2858       expand_start_cond (build (NE_EXPR, boolean_type_node,
2859                                 iterator, minus_one),
2860                          0);
2861
2862       /* Otherwise, loop through the elements.  */
2863       expand_start_loop_continue_elsewhere (1);
2864   
2865       /* The initialization of each array element is a full-expression.  */
2866       expand_start_target_temps ();
2867
2868       if (from_array)
2869         {
2870           tree to = build1 (INDIRECT_REF, type, base);
2871           tree from;
2872
2873           if (base2)
2874             from = build1 (INDIRECT_REF, itype, base2);
2875           else
2876             from = NULL_TREE;
2877
2878           if (from_array == 2)
2879             expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
2880           else if (TYPE_NEEDS_CONSTRUCTING (type))
2881             expand_aggr_init (to, from, 0);
2882           else if (from)
2883             expand_assignment (to, from, 0, 0);
2884           else
2885             my_friendly_abort (57);
2886         }
2887       else if (TREE_CODE (type) == ARRAY_TYPE)
2888         {
2889           if (init != 0)
2890             sorry ("cannot initialize multi-dimensional array with initializer");
2891           expand_vec_init (decl, 
2892                            build1 (NOP_EXPR, 
2893                                    build_pointer_type (TREE_TYPE
2894                                                        (type)),
2895                                    base),
2896                            array_type_nelts (type), 0, 0);
2897         }
2898       else
2899         expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0);
2900
2901       expand_assignment (base,
2902                          build (PLUS_EXPR, build_pointer_type (type), 
2903                                 base, size), 0, 0);
2904       if (base2)
2905         expand_assignment (base2,
2906                            build (PLUS_EXPR, build_pointer_type (type), 
2907                                   base2, size), 0, 0);
2908
2909       /* Cleanup any temporaries needed for the initial value.  */
2910       expand_end_target_temps ();
2911   
2912       expand_loop_continue_here ();
2913       expand_exit_loop_if_false (0, build (NE_EXPR, boolean_type_node,
2914                                            build (PREDECREMENT_EXPR, 
2915                                                   ptrdiff_type_node, 
2916                                                   iterator,
2917                                                   integer_one_node), 
2918                                            minus_one));
2919   
2920       if (obey_regdecls)
2921         {
2922           use_variable (DECL_RTL (base));
2923           if (base2)
2924             use_variable (DECL_RTL (base2));
2925         }
2926
2927       expand_end_loop ();
2928       expand_end_cond ();
2929     }
2930
2931   /* Make sure to cleanup any partially constructed elements.  */
2932   expand_vec_init_catch_clause (rval, type, maxindex, iterator);
2933
2934   if (obey_regdecls)
2935     {
2936       use_variable (DECL_RTL (iterator));
2937       use_variable (DECL_RTL (rval));
2938     }
2939
2940   return rval;
2941 }
2942
2943 /* Free up storage of type TYPE, at address ADDR.
2944
2945    TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
2946    of pointer.
2947
2948    VIRTUAL_SIZE is the amount of storage that was allocated, and is
2949    used as the second argument to operator delete.  It can include
2950    things like padding and magic size cookies.  It has virtual in it,
2951    because if you have a base pointer and you delete through a virtual
2952    destructor, it should be the size of the dynamic object, not the
2953    static object, see Free Store 12.5 ANSI C++ WP.
2954
2955    This does not call any destructors.  */
2956
2957 tree
2958 build_x_delete (addr, which_delete, virtual_size)
2959      tree addr;
2960      int which_delete;
2961      tree virtual_size;
2962 {
2963   int use_global_delete = which_delete & 1;
2964   int use_vec_delete = !!(which_delete & 2);
2965   enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
2966   int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
2967
2968   return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
2969 }
2970
2971 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
2972    ADDR is an expression which yields the store to be destroyed.
2973    AUTO_DELETE is nonzero if a call to DELETE should be made or not.
2974    If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
2975    virtual baseclasses.
2976    If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
2977
2978    FLAGS is the logical disjunction of zero or more LOOKUP_
2979    flags.  See cp-tree.h for more info.
2980
2981    This function does not delete an object's virtual base classes.  */
2982
2983 tree
2984 build_delete (type, addr, auto_delete, flags, use_global_delete)
2985      tree type, addr;
2986      tree auto_delete;
2987      int flags;
2988      int use_global_delete;
2989 {
2990   tree member;
2991   tree expr;
2992   tree ref;
2993
2994   if (addr == error_mark_node)
2995     return error_mark_node;
2996
2997   /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
2998      set to `error_mark_node' before it gets properly cleaned up.  */
2999   if (type == error_mark_node)
3000     return error_mark_node;
3001
3002   type = TYPE_MAIN_VARIANT (type);
3003
3004   if (TREE_CODE (type) == POINTER_TYPE)
3005     {
3006       type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3007       if (!complete_type_or_else (type))
3008         return error_mark_node;
3009       if (TREE_CODE (type) == ARRAY_TYPE)
3010         goto handle_array;
3011       if (! IS_AGGR_TYPE (type))
3012         {
3013           /* Call the builtin operator delete.  */
3014           return build_builtin_delete_call (addr);
3015         }
3016       if (TREE_SIDE_EFFECTS (addr))
3017         addr = save_expr (addr);
3018
3019       /* throw away const and volatile on target type of addr */
3020       addr = convert_force (build_pointer_type (type), addr, 0);
3021       ref = build_indirect_ref (addr, NULL_PTR);
3022     }
3023   else if (TREE_CODE (type) == ARRAY_TYPE)
3024     {
3025     handle_array:
3026       if (TREE_SIDE_EFFECTS (addr))
3027         addr = save_expr (addr);
3028       if (TYPE_DOMAIN (type) == NULL_TREE)
3029         {
3030           error ("unknown array size in delete");
3031           return error_mark_node;
3032         }
3033       return build_vec_delete (addr, array_type_nelts (type),
3034                                auto_delete, integer_zero_node,
3035                                use_global_delete);
3036     }
3037   else
3038     {
3039       /* Don't check PROTECT here; leave that decision to the
3040          destructor.  If the destructor is accessible, call it,
3041          else report error.  */
3042       addr = build_unary_op (ADDR_EXPR, addr, 0);
3043       if (TREE_SIDE_EFFECTS (addr))
3044         addr = save_expr (addr);
3045
3046       if (TREE_CONSTANT (addr))
3047         addr = convert_pointer_to (type, addr);
3048       else
3049         addr = convert_force (build_pointer_type (type), addr, 0);
3050
3051       ref = build_indirect_ref (addr, NULL_PTR);
3052     }
3053
3054   my_friendly_assert (IS_AGGR_TYPE (type), 220);
3055
3056   if (! TYPE_NEEDS_DESTRUCTOR (type))
3057     {
3058       if (auto_delete == integer_zero_node)
3059         return void_zero_node;
3060
3061       return build_op_delete_call
3062         (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3063          LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3064          NULL_TREE);
3065     }
3066
3067   /* Below, we will reverse the order in which these calls are made.
3068      If we have a destructor, then that destructor will take care
3069      of the base classes; otherwise, we must do that here.  */
3070   if (TYPE_HAS_DESTRUCTOR (type))
3071     {
3072       tree passed_auto_delete;
3073       tree do_delete = NULL_TREE;
3074       tree ifexp;
3075
3076       if (use_global_delete)
3077         {
3078           tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
3079                                    auto_delete, integer_one_node));
3080           tree call = build_builtin_delete_call (addr);
3081
3082           cond = fold (build (COND_EXPR, void_type_node, cond,
3083                               call, void_zero_node));
3084           if (cond != void_zero_node)
3085             do_delete = cond;
3086
3087           passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node,
3088                                             auto_delete, integer_two_node));
3089         }
3090       else
3091         passed_auto_delete = auto_delete;
3092
3093       expr = build_method_call
3094         (ref, dtor_identifier, build_expr_list (NULL_TREE, passed_auto_delete),
3095          NULL_TREE, flags);
3096
3097       if (do_delete)
3098         expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3099
3100       if (flags & LOOKUP_DESTRUCTOR)
3101         /* Explicit destructor call; don't check for null pointer.  */
3102         ifexp = integer_one_node;
3103       else
3104         /* Handle deleting a null pointer.  */
3105         ifexp = fold (build_binary_op (NE_EXPR, addr, integer_zero_node, 1));
3106
3107       if (ifexp != integer_one_node)
3108         expr = build (COND_EXPR, void_type_node,
3109                       ifexp, expr, void_zero_node);
3110
3111       return expr;
3112     }
3113   else
3114     {
3115       /* We only get here from finish_function for a destructor.  */
3116       tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3117       int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3118       tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3119       tree exprstmt = NULL_TREE;
3120       tree parent_auto_delete = auto_delete;
3121       tree cond;
3122
3123       /* Set this again before we call anything, as we might get called
3124          recursively.  */
3125       TYPE_HAS_DESTRUCTOR (type) = 1;
3126
3127       /* If we have member delete or vbases, we call delete in
3128          finish_function.  */
3129       if (auto_delete == integer_zero_node)
3130         cond = NULL_TREE;
3131       else if (base_binfo == NULL_TREE
3132                || ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3133         {
3134           cond = build (COND_EXPR, void_type_node,
3135                         build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3136                         build_builtin_delete_call (addr),
3137                         void_zero_node);
3138         }
3139       else
3140         cond = NULL_TREE;
3141
3142       if (cond)
3143         exprstmt = build_expr_list (NULL_TREE, cond);
3144
3145       if (base_binfo
3146           && ! TREE_VIA_VIRTUAL (base_binfo)
3147           && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3148         {
3149           tree this_auto_delete;
3150
3151           if (BINFO_OFFSET_ZEROP (base_binfo))
3152             this_auto_delete = parent_auto_delete;
3153           else
3154             this_auto_delete = integer_zero_node;
3155
3156           expr = build_scoped_method_call
3157             (ref, base_binfo, dtor_identifier,
3158              build_expr_list (NULL_TREE, this_auto_delete));
3159           exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt);
3160         }
3161
3162       /* Take care of the remaining baseclasses.  */
3163       for (i = 1; i < n_baseclasses; i++)
3164         {
3165           base_binfo = TREE_VEC_ELT (binfos, i);
3166           if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3167               || TREE_VIA_VIRTUAL (base_binfo))
3168             continue;
3169
3170           expr = build_scoped_method_call
3171             (ref, base_binfo, dtor_identifier,
3172              build_expr_list (NULL_TREE, integer_zero_node));
3173
3174           exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt);
3175         }
3176
3177       for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3178         {
3179           if (TREE_CODE (member) != FIELD_DECL)
3180             continue;
3181           if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3182             {
3183               tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0);
3184               tree this_type = TREE_TYPE (member);
3185               expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3186               exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt);
3187             }
3188         }
3189
3190       if (exprstmt)
3191         return build_compound_expr (exprstmt);
3192       /* Virtual base classes make this function do nothing.  */
3193       return void_zero_node;
3194     }
3195 }
3196
3197 /* For type TYPE, delete the virtual baseclass objects of DECL.  */
3198
3199 tree
3200 build_vbase_delete (type, decl)
3201      tree type, decl;
3202 {
3203   tree vbases = CLASSTYPE_VBASECLASSES (type);
3204   tree result = NULL_TREE;
3205   tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3206
3207   my_friendly_assert (addr != error_mark_node, 222);
3208
3209   while (vbases)
3210     {
3211       tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)),
3212                                       addr, 0);
3213       result = expr_tree_cons (NULL_TREE,
3214                           build_delete (TREE_TYPE (this_addr), this_addr,
3215                                         integer_zero_node,
3216                                         LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3217                           result);
3218       vbases = TREE_CHAIN (vbases);
3219     }
3220   return build_compound_expr (nreverse (result));
3221 }
3222
3223 /* Build a C++ vector delete expression.
3224    MAXINDEX is the number of elements to be deleted.
3225    ELT_SIZE is the nominal size of each element in the vector.
3226    BASE is the expression that should yield the store to be deleted.
3227    This function expands (or synthesizes) these calls itself.
3228    AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3229    AUTO_DELETE say whether each item in the container should be deallocated.
3230
3231    This also calls delete for virtual baseclasses of elements of the vector.
3232
3233    Update: MAXINDEX is no longer needed.  The size can be extracted from the
3234    start of the vector for pointers, and from the type for arrays.  We still
3235    use MAXINDEX for arrays because it happens to already have one of the
3236    values we'd have to extract.  (We could use MAXINDEX with pointers to
3237    confirm the size, and trap if the numbers differ; not clear that it'd
3238    be worth bothering.)  */
3239
3240 tree
3241 build_vec_delete (base, maxindex, auto_delete_vec, auto_delete,
3242                   use_global_delete)
3243      tree base, maxindex;
3244      tree auto_delete_vec, auto_delete;
3245      int use_global_delete;
3246 {
3247   tree type;
3248
3249   if (TREE_CODE (base) == OFFSET_REF)
3250     base = resolve_offset_ref (base);
3251
3252   type = TREE_TYPE (base);
3253
3254   base = stabilize_reference (base);
3255
3256   /* Since we can use base many times, save_expr it.  */
3257   if (TREE_SIDE_EFFECTS (base))
3258     base = save_expr (base);
3259
3260   if (TREE_CODE (type) == POINTER_TYPE)
3261     {
3262       /* Step back one from start of vector, and read dimension.  */
3263       tree cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type),
3264                                 base, BI_header_size);
3265       tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3266       maxindex = build_component_ref (cookie, nc_nelts_field_id, NULL_TREE, 0);
3267       do
3268         type = TREE_TYPE (type);
3269       while (TREE_CODE (type) == ARRAY_TYPE);
3270     }
3271   else if (TREE_CODE (type) == ARRAY_TYPE)
3272     {
3273       /* get the total number of things in the array, maxindex is a bad name */
3274       maxindex = array_type_nelts_total (type);
3275       while (TREE_CODE (type) == ARRAY_TYPE)
3276         type = TREE_TYPE (type);
3277       base = build_unary_op (ADDR_EXPR, base, 1);
3278     }
3279   else
3280     {
3281       if (base != error_mark_node)
3282         error ("type to vector delete is neither pointer or array type");
3283       return error_mark_node;
3284     }
3285
3286   return build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,
3287                              use_global_delete);
3288 }