OSDN Git Service

2005-07-02 Zack Weinberg <zack@codesourcery.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / typeck2.c
1 /* Report error messages, build initializers, and perform
2    some front-end optimizations for C++ compiler.
3    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4    1999, 2000, 2001, 2002, 2004, 2005
5    Free Software Foundation, Inc.
6    Hacked by Michael Tiemann (tiemann@cygnus.com)
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
13 any later version.
14
15 GCC is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING.  If not, write to
22 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 Boston, MA 02110-1301, USA.  */
24
25
26 /* This file is part of the C++ front end.
27    It contains routines to build C++ expressions given their operands,
28    including computing the types of the result, C and C++ specific error
29    checks, and some optimization.  */
30
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "diagnostic.h"
41
42 static tree process_init_constructor (tree, tree, tree *);
43
44 /* Print an error message stemming from an attempt to use
45    BASETYPE as a base class for TYPE.  */
46
47 tree
48 error_not_base_type (tree basetype, tree type)
49 {
50   if (TREE_CODE (basetype) == FUNCTION_DECL)
51     basetype = DECL_CONTEXT (basetype);
52   error ("type %qT is not a base type for type %qT", basetype, type);
53   return error_mark_node;
54 }
55
56 tree
57 binfo_or_else (tree base, tree type)
58 {
59   tree binfo = lookup_base (type, base, ba_unique, NULL);
60
61   if (binfo == error_mark_node)
62     return NULL_TREE;
63   else if (!binfo)
64     error_not_base_type (base, type);
65   return binfo;
66 }
67
68 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
69    value may not be changed thereafter.  Thus, we emit hard errors for these,
70    rather than just pedwarns.  If `SOFT' is 1, then we just pedwarn.  (For
71    example, conversions to references.)  */
72
73 void
74 readonly_error (tree arg, const char* string, int soft)
75 {
76   const char *fmt;
77   void (*fn) (const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
78
79   if (soft)
80     fn = pedwarn;
81   else
82     fn = error;
83
84   if (TREE_CODE (arg) == COMPONENT_REF)
85     {
86       if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
87         fmt = "%s of data-member %qD in read-only structure";
88       else
89         fmt = "%s of read-only data-member %qD";
90       (*fn) (fmt, string, TREE_OPERAND (arg, 1));
91     }
92   else if (TREE_CODE (arg) == VAR_DECL)
93     {
94       if (DECL_LANG_SPECIFIC (arg)
95           && DECL_IN_AGGR_P (arg)
96           && !TREE_STATIC (arg))
97         fmt = "%s of constant field %qD";
98       else
99         fmt = "%s of read-only variable %qD";
100       (*fn) (fmt, string, arg);
101     }
102   else if (TREE_CODE (arg) == PARM_DECL)
103     (*fn) ("%s of read-only parameter %qD", string, arg);
104   else if (TREE_CODE (arg) == INDIRECT_REF
105            && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
106            && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
107                || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
108     (*fn) ("%s of read-only reference %qD", string, TREE_OPERAND (arg, 0));
109   else if (TREE_CODE (arg) == RESULT_DECL)
110     (*fn) ("%s of read-only named return value %qD", string, arg);
111   else if (TREE_CODE (arg) == FUNCTION_DECL)
112     (*fn) ("%s of function %qD", string, arg);
113   else
114     (*fn) ("%s of read-only location", string);
115 }
116
117 \f
118 /* Structure that holds information about declarations whose type was
119    incomplete and we could not check whether it was abstract or not.  */
120
121 struct pending_abstract_type GTY((chain_next ("%h.next")))
122 {
123   /* Declaration which we are checking for abstractness. It is either
124      a DECL node, or an IDENTIFIER_NODE if we do not have a full
125      declaration available.  */
126   tree decl;
127
128   /* Type which will be checked for abstractness.  */
129   tree type;
130
131   /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
132      because DECLs already carry locus information.  */
133   location_t locus;
134
135   /* Link to the next element in list.  */
136   struct pending_abstract_type* next;
137 };
138
139
140 /* Compute the hash value of the node VAL. This function is used by the
141    hash table abstract_pending_vars.  */
142
143 static hashval_t
144 pat_calc_hash (const void* val)
145 {
146   const struct pending_abstract_type* pat = val;
147   return (hashval_t) TYPE_UID (pat->type);
148 }
149
150
151 /* Compare node VAL1 with the type VAL2. This function is used by the
152    hash table abstract_pending_vars.  */
153
154 static int
155 pat_compare (const void* val1, const void* val2)
156 {
157   const struct pending_abstract_type* pat1 = val1;
158   tree type2 = (tree)val2;
159
160   return (pat1->type == type2);
161 }
162
163 /* Hash table that maintains pending_abstract_type nodes, for which we still
164    need to check for type abstractness.  The key of the table is the type
165    of the declaration.  */
166 static GTY ((param_is (struct pending_abstract_type)))
167 htab_t abstract_pending_vars = NULL;
168
169
170 /* This function is called after TYPE is completed, and will check if there
171    are pending declarations for which we still need to verify the abstractness
172    of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
173    turned out to be incomplete.  */
174
175 void
176 complete_type_check_abstract (tree type)
177 {
178   void **slot;
179   struct pending_abstract_type *pat;
180   location_t cur_loc = input_location;
181
182   gcc_assert (COMPLETE_TYPE_P (type));
183
184   if (!abstract_pending_vars)
185     return;
186
187   /* Retrieve the list of pending declarations for this type.  */
188   slot = htab_find_slot_with_hash (abstract_pending_vars, type,
189                                    (hashval_t)TYPE_UID (type), NO_INSERT);
190   if (!slot)
191     return;
192   pat = (struct pending_abstract_type*)*slot;
193   gcc_assert (pat);
194
195   /* If the type is not abstract, do not do anything.  */
196   if (CLASSTYPE_PURE_VIRTUALS (type))
197     {
198       struct pending_abstract_type *prev = 0, *next;
199
200       /* Reverse the list to emit the errors in top-down order.  */
201       for (; pat; pat = next)
202         {
203           next = pat->next;
204           pat->next = prev;
205           prev = pat;
206         }
207       pat = prev;
208
209       /* Go through the list, and call abstract_virtuals_error for each
210         element: it will issue a diagnostic if the type is abstract.  */
211       while (pat)
212         {
213           gcc_assert (type == pat->type);
214
215           /* Tweak input_location so that the diagnostic appears at the correct
216             location. Notice that this is only needed if the decl is an
217             IDENTIFIER_NODE.  */
218           input_location = pat->locus;
219           abstract_virtuals_error (pat->decl, pat->type);
220           pat = pat->next;
221         }
222     }
223
224   htab_clear_slot (abstract_pending_vars, slot);
225
226   input_location = cur_loc;
227 }
228
229
230 /* If TYPE has abstract virtual functions, issue an error about trying
231    to create an object of that type.  DECL is the object declared, or
232    NULL_TREE if the declaration is unavailable.  Returns 1 if an error
233    occurred; zero if all was well.  */
234
235 int
236 abstract_virtuals_error (tree decl, tree type)
237 {
238   VEC(tree,gc) *pure;
239
240   /* This function applies only to classes. Any other entity can never
241      be abstract.  */
242   if (!CLASS_TYPE_P (type))
243     return 0;
244
245   /* If the type is incomplete, we register it within a hash table,
246      so that we can check again once it is completed. This makes sense
247      only for objects for which we have a declaration or at least a
248      name.  */
249   if (!COMPLETE_TYPE_P (type))
250     {
251       void **slot;
252       struct pending_abstract_type *pat;
253
254       gcc_assert (!decl || DECL_P (decl)
255                   || TREE_CODE (decl) == IDENTIFIER_NODE);
256
257       if (!abstract_pending_vars)
258         abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash,
259                                                 &pat_compare, NULL);
260
261       slot = htab_find_slot_with_hash (abstract_pending_vars, type,
262                                       (hashval_t)TYPE_UID (type), INSERT);
263
264       pat = GGC_NEW (struct pending_abstract_type);
265       pat->type = type;
266       pat->decl = decl;
267       pat->locus = ((decl && DECL_P (decl))
268                     ? DECL_SOURCE_LOCATION (decl)
269                     : input_location);
270
271       pat->next = *slot;
272       *slot = pat;
273
274       return 0;
275     }
276
277   if (!TYPE_SIZE (type))
278     /* TYPE is being defined, and during that time
279        CLASSTYPE_PURE_VIRTUALS holds the inline friends.  */
280     return 0;
281
282   pure = CLASSTYPE_PURE_VIRTUALS (type);
283   if (!pure)
284     return 0;
285
286   if (decl)
287     {
288       if (TREE_CODE (decl) == RESULT_DECL)
289         return 0;
290
291       if (TREE_CODE (decl) == VAR_DECL)
292         error ("cannot declare variable %q+D to be of abstract "
293                "type %qT", decl, type);
294       else if (TREE_CODE (decl) == PARM_DECL)
295         error ("cannot declare parameter %q+D to be of abstract type %qT",
296                decl, type);
297       else if (TREE_CODE (decl) == FIELD_DECL)
298         error ("cannot declare field %q+D to be of abstract type %qT",
299                decl, type);
300       else if (TREE_CODE (decl) == FUNCTION_DECL
301                && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
302         error ("invalid abstract return type for member function %q+#D", decl);
303       else if (TREE_CODE (decl) == FUNCTION_DECL)
304         error ("invalid abstract return type for function %q+#D", decl);
305       else if (TREE_CODE (decl) == IDENTIFIER_NODE)
306         /* Here we do not have location information.  */
307         error ("invalid abstract type %qT for %qE", type, decl);
308       else
309         error ("invalid abstract type for %q+D", decl);
310     }
311   else
312     error ("cannot allocate an object of abstract type %qT", type);
313
314   /* Only go through this once.  */
315   if (VEC_length (tree, pure))
316     {
317       unsigned ix;
318       tree fn;
319
320       inform ("%J  because the following virtual functions are pure "
321               "within %qT:", TYPE_MAIN_DECL (type), type);
322
323       for (ix = 0; VEC_iterate (tree, pure, ix, fn); ix++)
324         inform ("\t%+#D", fn);
325       /* Now truncate the vector.  This leaves it non-null, so we know
326          there are pure virtuals, but empty so we don't list them out
327          again.  */
328       VEC_truncate (tree, pure, 0);
329     }
330   else
331     inform ("%J  since type %qT has pure virtual functions",
332             TYPE_MAIN_DECL (type), type);
333
334   return 1;
335 }
336
337 /* Print an error message for invalid use of an incomplete type.
338    VALUE is the expression that was used (or 0 if that isn't known)
339    and TYPE is the type that was invalid.  DIAG_TYPE indicates the
340    type of diagnostic:  0 for an error, 1 for a warning, 2 for a
341    pedwarn.  */
342
343 void
344 cxx_incomplete_type_diagnostic (tree value, tree type, int diag_type)
345 {
346   int decl = 0;
347   void (*p_msg) (const char *, ...) ATTRIBUTE_GCC_CXXDIAG(1,2);
348
349   if (diag_type == 1)
350     p_msg = warning0;
351   else if (diag_type == 2)
352     p_msg = pedwarn;
353   else
354     p_msg = error;
355
356   /* Avoid duplicate error message.  */
357   if (TREE_CODE (type) == ERROR_MARK)
358     return;
359
360   if (value != 0 && (TREE_CODE (value) == VAR_DECL
361                      || TREE_CODE (value) == PARM_DECL
362                      || TREE_CODE (value) == FIELD_DECL))
363     {
364       p_msg ("%q+D has incomplete type", value);
365       decl = 1;
366     }
367  retry:
368   /* We must print an error message.  Be clever about what it says.  */
369
370   switch (TREE_CODE (type))
371     {
372     case RECORD_TYPE:
373     case UNION_TYPE:
374     case ENUMERAL_TYPE:
375       if (!decl)
376         p_msg ("invalid use of undefined type %q#T", type);
377       if (!TYPE_TEMPLATE_INFO (type))
378         p_msg ("forward declaration of %q+#T", type);
379       else
380         p_msg ("declaration of %q+#T", type);
381       break;
382
383     case VOID_TYPE:
384       p_msg ("invalid use of %qT", type);
385       break;
386
387     case ARRAY_TYPE:
388       if (TYPE_DOMAIN (type))
389         {
390           type = TREE_TYPE (type);
391           goto retry;
392         }
393       p_msg ("invalid use of array with unspecified bounds");
394       break;
395
396     case OFFSET_TYPE:
397     bad_member:
398       p_msg ("invalid use of member (did you forget the %<&%> ?)");
399       break;
400
401     case TEMPLATE_TYPE_PARM:
402       p_msg ("invalid use of template type parameter");
403       break;
404
405     case UNKNOWN_TYPE:
406       if (value && TREE_CODE (value) == COMPONENT_REF)
407         goto bad_member;
408       else if (value && TREE_CODE (value) == ADDR_EXPR)
409         p_msg ("address of overloaded function with no contextual "
410                "type information");
411       else if (value && TREE_CODE (value) == OVERLOAD)
412         p_msg ("overloaded function with no contextual type information");
413       else
414         p_msg ("insufficient contextual information to determine type");
415       break;
416
417     default:
418       gcc_unreachable ();
419     }
420 }
421
422 /* Backward-compatibility interface to incomplete_type_diagnostic;
423    required by ../tree.c.  */
424 #undef cxx_incomplete_type_error
425 void
426 cxx_incomplete_type_error (tree value, tree type)
427 {
428   cxx_incomplete_type_diagnostic (value, type, 0);
429 }
430
431 \f
432 /* The recursive part of split_nonconstant_init.  DEST is an lvalue
433    expression to which INIT should be assigned.  INIT is a CONSTRUCTOR.  */
434
435 static void
436 split_nonconstant_init_1 (tree dest, tree init)
437 {
438   tree *pelt, elt, type = TREE_TYPE (dest);
439   tree sub, code, inner_type = NULL;
440   bool array_type_p = false;
441
442   pelt = &CONSTRUCTOR_ELTS (init);
443   switch (TREE_CODE (type))
444     {
445     case ARRAY_TYPE:
446       inner_type = TREE_TYPE (type);
447       array_type_p = true;
448       /* FALLTHRU */
449
450     case RECORD_TYPE:
451     case UNION_TYPE:
452     case QUAL_UNION_TYPE:
453       while ((elt = *pelt))
454         {
455           tree field_index = TREE_PURPOSE (elt);
456           tree value = TREE_VALUE (elt);
457
458           if (!array_type_p)
459             inner_type = TREE_TYPE (field_index);
460
461           if (TREE_CODE (value) == CONSTRUCTOR)
462             {
463               if (array_type_p)
464                 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
465                               NULL_TREE, NULL_TREE);
466               else
467                 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
468                               NULL_TREE);
469
470               split_nonconstant_init_1 (sub, value);
471             }
472           else if (!initializer_constant_valid_p (value, inner_type))
473             {
474               *pelt = TREE_CHAIN (elt);
475
476               if (array_type_p)
477                 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
478                               NULL_TREE, NULL_TREE);
479               else
480                 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
481                               NULL_TREE);
482
483               code = build2 (MODIFY_EXPR, inner_type, sub, value);
484               code = build_stmt (EXPR_STMT, code);
485               add_stmt (code);
486               continue;
487             }
488
489           pelt = &TREE_CHAIN (elt);
490         }
491       break;
492
493     case VECTOR_TYPE:
494       if (!initializer_constant_valid_p (init, type))
495         {
496           tree cons = copy_node (init);
497           CONSTRUCTOR_ELTS (init) = NULL;
498           code = build2 (MODIFY_EXPR, type, dest, cons);
499           code = build_stmt (EXPR_STMT, code);
500           add_stmt (code);
501         }
502       break;
503
504     default:
505       gcc_unreachable ();
506     }
507 }
508
509 /* A subroutine of store_init_value.  Splits non-constant static
510    initializer INIT into a constant part and generates code to
511    perform the non-constant part of the initialization to DEST.
512    Returns the code for the runtime init.  */
513
514 static tree
515 split_nonconstant_init (tree dest, tree init)
516 {
517   tree code;
518
519   if (TREE_CODE (init) == CONSTRUCTOR)
520     {
521       code = push_stmt_list ();
522       split_nonconstant_init_1 (dest, init);
523       code = pop_stmt_list (code);
524       DECL_INITIAL (dest) = init;
525       TREE_READONLY (dest) = 0;
526     }
527   else
528     code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
529
530   return code;
531 }
532
533 /* Perform appropriate conversions on the initial value of a variable,
534    store it in the declaration DECL,
535    and print any error messages that are appropriate.
536    If the init is invalid, store an ERROR_MARK.
537
538    C++: Note that INIT might be a TREE_LIST, which would mean that it is
539    a base class initializer for some aggregate type, hopefully compatible
540    with DECL.  If INIT is a single element, and DECL is an aggregate
541    type, we silently convert INIT into a TREE_LIST, allowing a constructor
542    to be called.
543
544    If INIT is a TREE_LIST and there is no constructor, turn INIT
545    into a CONSTRUCTOR and use standard initialization techniques.
546    Perhaps a warning should be generated?
547
548    Returns code to be executed if initialization could not be performed
549    for static variable.  In that case, caller must emit the code.  */
550
551 tree
552 store_init_value (tree decl, tree init)
553 {
554   tree value, type;
555
556   /* If variable's type was invalidly declared, just ignore it.  */
557
558   type = TREE_TYPE (decl);
559   if (TREE_CODE (type) == ERROR_MARK)
560     return NULL_TREE;
561
562   if (IS_AGGR_TYPE (type))
563     {
564       gcc_assert (TYPE_HAS_TRIVIAL_INIT_REF (type)
565                   || TREE_CODE (init) == CONSTRUCTOR);
566
567       if (TREE_CODE (init) == TREE_LIST)
568         {
569           error ("constructor syntax used, but no constructor declared "
570                  "for type %qT", type);
571           init = build_constructor (NULL_TREE, nreverse (init));
572         }
573     }
574   else if (TREE_CODE (init) == TREE_LIST
575            && TREE_TYPE (init) != unknown_type_node)
576     {
577       if (TREE_CODE (decl) == RESULT_DECL)
578         init = build_x_compound_expr_from_list (init,
579                                                 "return value initializer");
580       else if (TREE_CODE (init) == TREE_LIST
581                && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
582         {
583           error ("cannot initialize arrays using this syntax");
584           return NULL_TREE;
585         }
586       else
587         /* We get here with code like `int a (2);' */
588         init = build_x_compound_expr_from_list (init, "initializer");
589     }
590
591   /* End of special C++ code.  */
592
593   /* Digest the specified initializer into an expression.  */
594   value = digest_init (type, init, (tree *) 0);
595   /* If the initializer is not a constant, fill in DECL_INITIAL with
596      the bits that are constant, and then return an expression that
597      will perform the dynamic initialization.  */
598   if (value != error_mark_node
599       && (TREE_SIDE_EFFECTS (value)
600            || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
601     return split_nonconstant_init (decl, value);
602   /* If the value is a constant, just put it in DECL_INITIAL.  If DECL
603      is an automatic variable, the middle end will turn this into a
604      dynamic initialization later.  */
605   DECL_INITIAL (decl) = value;
606   return NULL_TREE;
607 }
608
609 \f
610 /* Digest the parser output INIT as an initializer for type TYPE.
611    Return a C expression of type TYPE to represent the initial value.
612
613    If TAIL is nonzero, it points to a variable holding a list of elements
614    of which INIT is the first.  We update the list stored there by
615    removing from the head all the elements that we use.
616    Normally this is only one; we use more than one element only if
617    TYPE is an aggregate and INIT is not a constructor.  */
618
619 tree
620 digest_init (tree type, tree init, tree* tail)
621 {
622   enum tree_code code = TREE_CODE (type);
623   tree element = NULL_TREE;
624   tree old_tail_contents = NULL_TREE;
625
626   /* By default, assume we use one element from a list.
627      We correct this later in the sole case where it is not true.  */
628
629   if (tail)
630     {
631       old_tail_contents = *tail;
632       *tail = TREE_CHAIN (*tail);
633     }
634
635   if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
636                                   && TREE_VALUE (init) == error_mark_node))
637     return error_mark_node;
638
639   if (TREE_CODE (init) == ERROR_MARK)
640     /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside
641        a template function. This gets substituted during instantiation.  */
642     return init;
643
644   /* We must strip the outermost array type when completing the type,
645      because the its bounds might be incomplete at the moment.  */
646   if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
647                               ? TREE_TYPE (type) : type, NULL_TREE))
648     return error_mark_node;
649
650   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
651   if (TREE_CODE (init) == NON_LVALUE_EXPR)
652     init = TREE_OPERAND (init, 0);
653
654   if (BRACE_ENCLOSED_INITIALIZER_P (init)
655       && CONSTRUCTOR_ELTS (init) != 0
656       && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
657     {
658       element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
659       /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
660       if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
661         element = TREE_OPERAND (element, 0);
662       if (element == error_mark_node)
663         return element;
664     }
665
666   /* Initialization of an array of chars from a string constant
667      optionally enclosed in braces.  */
668
669   if (code == ARRAY_TYPE)
670     {
671       tree typ1;
672
673       if (TREE_CODE (init) == TREE_LIST)
674         {
675           error ("initializing array with parameter list");
676           return error_mark_node;
677         }
678
679       typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
680       if (char_type_p (typ1)
681           && ((init && TREE_CODE (init) == STRING_CST)
682               || (element && TREE_CODE (element) == STRING_CST)))
683         {
684           tree string = element ? element : init;
685
686           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
687                != char_type_node)
688               && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
689             {
690               error ("char-array initialized from wide string");
691               return error_mark_node;
692             }
693           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
694                == char_type_node)
695               && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
696             {
697               error ("int-array initialized from non-wide string");
698               return error_mark_node;
699             }
700
701           TREE_TYPE (string) = type;
702           if (TYPE_DOMAIN (type) != 0
703               && TREE_CONSTANT (TYPE_SIZE (type)))
704             {
705               int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
706               size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
707               /* In C it is ok to subtract 1 from the length of the string
708                  because it's ok to ignore the terminating null char that is
709                  counted in the length of the constant, but in C++ this would
710                  be invalid.  */
711               if (size < TREE_STRING_LENGTH (string))
712                 pedwarn ("initializer-string for array of chars is too long");
713             }
714           return string;
715         }
716     }
717
718   /* Handle scalar types, including conversions,
719      and signature pointers and references.  */
720
721   if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
722       || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
723       || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
724       || TYPE_PTR_TO_MEMBER_P (type))
725     {
726       if (BRACE_ENCLOSED_INITIALIZER_P (init))
727         {
728           if (element == 0)
729             {
730               error ("initializer for scalar variable requires one element");
731               return error_mark_node;
732             }
733           init = element;
734         }
735       while (BRACE_ENCLOSED_INITIALIZER_P (init))
736         {
737           pedwarn ("braces around scalar initializer for %qT", type);
738           init = CONSTRUCTOR_ELTS (init);
739           if (TREE_CHAIN (init))
740             pedwarn ("ignoring extra initializers for %qT", type);
741           init = TREE_VALUE (init);
742         }
743
744       return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
745                                          "initialization", NULL_TREE, 0);
746     }
747
748   /* Come here only for records and arrays (and unions with constructors).  */
749
750   if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
751     {
752       error ("variable-sized object of type %qT may not be initialized",
753                 type);
754       return error_mark_node;
755     }
756
757   if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code))
758     {
759       if (BRACE_ENCLOSED_INITIALIZER_P (init))
760         {
761           if (TYPE_NON_AGGREGATE_CLASS (type))
762             {
763               error ("subobject of type %qT must be initialized by "
764                      "constructor, not by %qE",
765                      type, init);
766               return error_mark_node;
767             }
768           return process_init_constructor (type, init, (tree *)0);
769         }
770       else if (can_convert_arg (type, TREE_TYPE (init), init)
771                || TYPE_NON_AGGREGATE_CLASS (type))
772         /* These are never initialized from multiple constructor elements.  */;
773       else if (tail != 0)
774         {
775           *tail = old_tail_contents;
776           return process_init_constructor (type, 0, tail);
777         }
778
779       if (code != ARRAY_TYPE)
780         {
781           int flags = LOOKUP_NORMAL;
782           /* Initialization from { } is copy-initialization.  */
783           if (tail)
784             flags |= LOOKUP_ONLYCONVERTING;
785
786           return convert_for_initialization (NULL_TREE, type, init, flags,
787                                              "initialization", NULL_TREE, 0);
788         }
789     }
790
791   error ("invalid initializer");
792   return error_mark_node;
793 }
794 \f
795 /* Process a constructor for a variable of type TYPE.
796    The constructor elements may be specified either with INIT or with ELTS,
797    only one of which should be non-null.
798
799    If INIT is specified, it is a CONSTRUCTOR node which is specifically
800    and solely for initializing this datum.
801
802    If ELTS is specified, it is the address of a variable containing
803    a list of expressions.  We take as many elements as we need
804    from the head of the list and update the list.
805
806    In the resulting constructor, TREE_CONSTANT is set if all elts are
807    constant, and TREE_STATIC is set if, in addition, all elts are simple enough
808    constants that the assembler and linker can compute them.  */
809
810 static tree
811 process_init_constructor (tree type, tree init, tree* elts)
812 {
813   tree tail;
814   /* List of the elements of the result constructor,
815      in reverse order.  */
816   tree members = NULL;
817   tree next1;
818   tree result;
819   int allconstant = 1;
820   int allsimple = 1;
821   int erroneous = 0;
822
823   /* Make TAIL be the list of elements to use for the initialization,
824      no matter how the data was given to us.  */
825
826   if (elts)
827     {
828       if (warn_missing_braces)
829         warning (0, "aggregate has a partly bracketed initializer");
830       tail = *elts;
831     }
832   else
833     tail = CONSTRUCTOR_ELTS (init);
834
835   /* Gobble as many elements as needed, and make a constructor or initial value
836      for each element of this aggregate.  Chain them together in result.
837      If there are too few, use 0 for each scalar ultimate component.  */
838
839   if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
840     {
841       long len;
842       int i;
843
844       if (TREE_CODE (type) == ARRAY_TYPE)
845         {
846           tree domain = TYPE_DOMAIN (type);
847           if (domain)
848             len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
849                    - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
850                    + 1);
851           else
852             len = -1;  /* Take as many as there are.  */
853         }
854       else
855         {
856           /* Vectors are like simple fixed-size arrays.  */
857           len = TYPE_VECTOR_SUBPARTS (type);
858         }
859
860       for (i = 0; len < 0 || i < len; i++)
861         {
862           if (tail)
863             {
864               if (TREE_PURPOSE (tail)
865                   && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
866                       || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
867                 sorry ("non-trivial labeled initializers");
868
869               if (TREE_VALUE (tail) != 0)
870                 {
871                   tree tail1 = tail;
872                   next1 = digest_init (TREE_TYPE (type),
873                                        TREE_VALUE (tail), &tail1);
874                   if (next1 == error_mark_node)
875                     return next1;
876                   gcc_assert (same_type_ignoring_top_level_qualifiers_p
877                               (TREE_TYPE (type), TREE_TYPE (next1)));
878                   gcc_assert (!tail1 || TREE_CODE (tail1) == TREE_LIST);
879                   if (tail == tail1 && len < 0)
880                     {
881                       error ("non-empty initializer for array of empty elements");
882                       /* Just ignore what we were supposed to use.  */
883                       tail1 = NULL_TREE;
884                     }
885                   tail = tail1;
886                 }
887               else
888                 {
889                   next1 = error_mark_node;
890                   tail = TREE_CHAIN (tail);
891                 }
892             }
893           else if (len < 0)
894             /* We're done.  */
895             break;
896           else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
897             {
898               /* If this type needs constructors run for
899                  default-initialization, we can't rely on the backend to do it
900                  for us, so build up TARGET_EXPRs.  If the type in question is
901                  a class, just build one up; if it's an array, recurse.  */
902
903               if (IS_AGGR_TYPE (TREE_TYPE (type)))
904                 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
905               else
906                 next1 = build_constructor (NULL_TREE, NULL_TREE);
907               next1 = digest_init (TREE_TYPE (type), next1, 0);
908             }
909           else if (! zero_init_p (TREE_TYPE (type)))
910             next1 = build_zero_init (TREE_TYPE (type),
911                                      /*nelts=*/NULL_TREE,
912                                      /*static_storage_p=*/false);
913           else
914             /* The default zero-initialization is fine for us; don't
915                add anything to the CONSTRUCTOR.  */
916             break;
917
918           if (next1 == error_mark_node)
919             erroneous = 1;
920           else if (!TREE_CONSTANT (next1))
921             allconstant = 0;
922           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
923             allsimple = 0;
924           members = tree_cons (size_int (i), next1, members);
925         }
926     }
927   else if (TREE_CODE (type) == RECORD_TYPE)
928     {
929       tree field;
930
931       if (tail)
932         {
933           gcc_assert (!CLASSTYPE_VBASECLASSES (type));
934           gcc_assert (!TYPE_BINFO (type)
935                       || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
936           gcc_assert (!TYPE_POLYMORPHIC_P (type));
937         }
938
939       for (field = TYPE_FIELDS (type); field;
940            field = TREE_CHAIN (field))
941         {
942           if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
943             {
944               members = tree_cons (field, integer_zero_node, members);
945               continue;
946             }
947
948           if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
949             continue;
950
951           if (tail)
952             {
953               if (TREE_PURPOSE (tail)
954                   && TREE_PURPOSE (tail) != field
955                   && TREE_PURPOSE (tail) != DECL_NAME (field))
956                 sorry ("non-trivial labeled initializers");
957
958               if (TREE_VALUE (tail) != 0)
959                 {
960                   tree tail1 = tail;
961
962                   next1 = digest_init (TREE_TYPE (field),
963                                        TREE_VALUE (tail), &tail1);
964                   gcc_assert (!tail1 || TREE_CODE (tail1) == TREE_LIST);
965                   tail = tail1;
966                 }
967               else
968                 {
969                   next1 = error_mark_node;
970                   tail = TREE_CHAIN (tail);
971                 }
972             }
973           else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
974             {
975               /* If this type needs constructors run for
976                  default-initialization, we can't rely on the backend to do it
977                  for us, so build up TARGET_EXPRs.  If the type in question is
978                  a class, just build one up; if it's an array, recurse.  */
979
980               if (IS_AGGR_TYPE (TREE_TYPE (field)))
981                 next1 = build_functional_cast (TREE_TYPE (field),
982                                                NULL_TREE);
983               else
984                 {
985                   next1 = build_constructor (NULL_TREE, NULL_TREE);
986                   if (init)
987                     TREE_HAS_CONSTRUCTOR (next1)
988                        = TREE_HAS_CONSTRUCTOR (init);
989                 }
990               next1 = digest_init (TREE_TYPE (field), next1, 0);
991
992               /* Warn when some struct elements are implicitly initialized.  */
993               if (warn_missing_field_initializers
994                   && (!init || BRACE_ENCLOSED_INITIALIZER_P (init)))
995                 warning (0, "missing initializer for member %qD", field);
996             }
997           else
998             {
999               if (TREE_READONLY (field))
1000                 error ("uninitialized const member %qD", field);
1001               else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1002                 error ("member %qD with uninitialized const fields", field);
1003               else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1004                 error ("member %qD is uninitialized reference", field);
1005
1006               /* Warn when some struct elements are implicitly initialized
1007                  to zero.  */
1008               if (warn_missing_field_initializers
1009                   && (!init || BRACE_ENCLOSED_INITIALIZER_P (init)))
1010                 warning (0, "missing initializer for member %qD", field);
1011
1012               if (! zero_init_p (TREE_TYPE (field)))
1013                 next1 = build_zero_init (TREE_TYPE (field),
1014                                          /*nelts=*/NULL_TREE,
1015                                          /*static_storage_p=*/false);
1016               else
1017                 /* The default zero-initialization is fine for us; don't
1018                    add anything to the CONSTRUCTOR.  */
1019                 continue;
1020             }
1021
1022           if (next1 == error_mark_node)
1023             erroneous = 1;
1024           else if (!TREE_CONSTANT (next1))
1025             allconstant = 0;
1026           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1027             allsimple = 0;
1028           members = tree_cons (field, next1, members);
1029         }
1030     }
1031   else if (TREE_CODE (type) == UNION_TYPE
1032            /* If the initializer was empty, use default zero initialization.  */
1033            && tail)
1034     {
1035       tree field = TYPE_FIELDS (type);
1036
1037       /* Find the first named field.  ANSI decided in September 1990
1038          that only named fields count here.  */
1039       while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1040         field = TREE_CHAIN (field);
1041
1042       /* If this element specifies a field, initialize via that field.  */
1043       if (TREE_PURPOSE (tail) != NULL_TREE)
1044         {
1045           int win = 0;
1046
1047           if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
1048             /* Handle the case of a call by build_c_cast.  */
1049             field = TREE_PURPOSE (tail), win = 1;
1050           else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
1051             error ("index value instead of field name in union initializer");
1052           else
1053             {
1054               tree temp;
1055               for (temp = TYPE_FIELDS (type);
1056                    temp;
1057                    temp = TREE_CHAIN (temp))
1058                 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
1059                   break;
1060               if (temp)
1061                 field = temp, win = 1;
1062               else
1063                 error ("no field %qD in union being initialized",
1064                        TREE_PURPOSE (tail));
1065             }
1066           if (!win)
1067             TREE_VALUE (tail) = error_mark_node;
1068         }
1069       else if (field == 0)
1070         {
1071           error ("union %qT with no named members cannot be initialized",
1072                  type);
1073           TREE_VALUE (tail) = error_mark_node;
1074         }
1075
1076       if (TREE_VALUE (tail) != 0)
1077         {
1078           tree tail1 = tail;
1079
1080           next1 = digest_init (TREE_TYPE (field),
1081                                TREE_VALUE (tail), &tail1);
1082           gcc_assert (!tail1 || TREE_CODE (tail1) == TREE_LIST);
1083           tail = tail1;
1084         }
1085       else
1086         {
1087           next1 = error_mark_node;
1088           tail = TREE_CHAIN (tail);
1089         }
1090
1091       if (next1 == error_mark_node)
1092         erroneous = 1;
1093       else if (!TREE_CONSTANT (next1))
1094         allconstant = 0;
1095       else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
1096         allsimple = 0;
1097       members = tree_cons (field, next1, members);
1098     }
1099
1100   /* If arguments were specified as a list, just remove the ones we used.  */
1101   if (elts)
1102     *elts = tail;
1103   /* If arguments were specified as a constructor,
1104      complain unless we used all the elements of the constructor.  */
1105   else if (tail)
1106     pedwarn ("excess elements in aggregate initializer");
1107
1108   if (erroneous)
1109     return error_mark_node;
1110
1111   result = build_constructor (type, nreverse (members));
1112   if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1113     cp_complete_array_type (&TREE_TYPE (result), result, /*do_default=*/0);
1114   if (init)
1115     TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1116   if (allconstant)
1117     {
1118       TREE_CONSTANT (result) = 1;
1119       TREE_INVARIANT (result) = 1;
1120       if (allsimple)
1121         TREE_STATIC (result) = 1;
1122     }
1123   return result;
1124 }
1125 \f
1126 /* Given a structure or union value DATUM, construct and return
1127    the structure or union component which results from narrowing
1128    that value to the base specified in BASETYPE.  For example, given the
1129    hierarchy
1130
1131    class L { int ii; };
1132    class A : L { ... };
1133    class B : L { ... };
1134    class C : A, B { ... };
1135
1136    and the declaration
1137
1138    C x;
1139
1140    then the expression
1141
1142    x.A::ii refers to the ii member of the L part of
1143    the A part of the C object named by X.  In this case,
1144    DATUM would be x, and BASETYPE would be A.
1145
1146    I used to think that this was nonconformant, that the standard specified
1147    that first we look up ii in A, then convert x to an L& and pull out the
1148    ii part.  But in fact, it does say that we convert x to an A&; A here
1149    is known as the "naming class".  (jason 2000-12-19)
1150
1151    BINFO_P points to a variable initialized either to NULL_TREE or to the
1152    binfo for the specific base subobject we want to convert to.  */
1153
1154 tree
1155 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1156 {
1157   tree binfo;
1158
1159   if (datum == error_mark_node)
1160     return error_mark_node;
1161   if (*binfo_p)
1162     binfo = *binfo_p;
1163   else
1164     binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
1165
1166   if (!binfo || binfo == error_mark_node)
1167     {
1168       *binfo_p = NULL_TREE;
1169       if (!binfo)
1170         error_not_base_type (basetype, TREE_TYPE (datum));
1171       return error_mark_node;
1172     }
1173
1174   *binfo_p = binfo;
1175   return build_base_path (PLUS_EXPR, datum, binfo, 1);
1176 }
1177
1178 /* Build a reference to an object specified by the C++ `->' operator.
1179    Usually this just involves dereferencing the object, but if the
1180    `->' operator is overloaded, then such overloads must be
1181    performed until an object which does not have the `->' operator
1182    overloaded is found.  An error is reported when circular pointer
1183    delegation is detected.  */
1184
1185 tree
1186 build_x_arrow (tree expr)
1187 {
1188   tree orig_expr = expr;
1189   tree types_memoized = NULL_TREE;
1190   tree type = TREE_TYPE (expr);
1191   tree last_rval = NULL_TREE;
1192
1193   if (type == error_mark_node)
1194     return error_mark_node;
1195
1196   if (processing_template_decl)
1197     {
1198       if (type_dependent_expression_p (expr))
1199         return build_min_nt (ARROW_EXPR, expr);
1200       expr = build_non_dependent_expr (expr);
1201     }
1202
1203   if (IS_AGGR_TYPE (type))
1204     {
1205       while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr,
1206                                    NULL_TREE, NULL_TREE,
1207                                    /*overloaded_p=*/NULL)))
1208         {
1209           if (expr == error_mark_node)
1210             return error_mark_node;
1211
1212           if (value_member (TREE_TYPE (expr), types_memoized))
1213             {
1214               error ("circular pointer delegation detected");
1215               return error_mark_node;
1216             }
1217           else
1218             {
1219               types_memoized = tree_cons (NULL_TREE, TREE_TYPE (expr),
1220                                           types_memoized);
1221             }
1222           last_rval = expr;
1223         }
1224
1225       if (last_rval == NULL_TREE)
1226         {
1227           error ("base operand of %<->%> has non-pointer type %qT", type);
1228           return error_mark_node;
1229         }
1230
1231       if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1232         last_rval = convert_from_reference (last_rval);
1233     }
1234   else
1235     last_rval = decay_conversion (expr);
1236
1237   if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1238     {
1239       if (processing_template_decl)
1240         {
1241           expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr);
1242           /* It will be dereferenced.  */
1243           TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval));
1244           return expr;
1245         }
1246
1247       return build_indirect_ref (last_rval, NULL);
1248     }
1249
1250   if (types_memoized)
1251     error ("result of %<operator->()%> yields non-pointer result");
1252   else
1253     error ("base operand of %<->%> is not a pointer");
1254   return error_mark_node;
1255 }
1256
1257 /* Return an expression for "DATUM .* COMPONENT".  DATUM has not
1258    already been checked out to be of aggregate type.  */
1259
1260 tree
1261 build_m_component_ref (tree datum, tree component)
1262 {
1263   tree ptrmem_type;
1264   tree objtype;
1265   tree type;
1266   tree binfo;
1267   tree ctype;
1268
1269   datum = decay_conversion (datum);
1270
1271   if (datum == error_mark_node || component == error_mark_node)
1272     return error_mark_node;
1273
1274   ptrmem_type = TREE_TYPE (component);
1275   if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type))
1276     {
1277       error ("%qE cannot be used as a member pointer, since it is of "
1278              "type %qT",
1279              component, ptrmem_type);
1280       return error_mark_node;
1281     }
1282
1283   objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1284   if (! IS_AGGR_TYPE (objtype))
1285     {
1286       error ("cannot apply member pointer %qE to %qE, which is of "
1287              "non-aggregate type %qT",
1288              component, datum, objtype);
1289       return error_mark_node;
1290     }
1291
1292   type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1293   ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1294
1295   if (!COMPLETE_TYPE_P (ctype))
1296     {
1297       if (!same_type_p (ctype, objtype))
1298         goto mismatch;
1299       binfo = NULL;
1300     }
1301   else
1302     {
1303       binfo = lookup_base (objtype, ctype, ba_check, NULL);
1304
1305       if (!binfo)
1306         {
1307         mismatch:
1308           error ("pointer to member type %qT incompatible with object "
1309                  "type %qT",
1310                  type, objtype);
1311           return error_mark_node;
1312         }
1313       else if (binfo == error_mark_node)
1314         return error_mark_node;
1315     }
1316
1317   if (TYPE_PTRMEM_P (ptrmem_type))
1318     {
1319       /* Compute the type of the field, as described in [expr.ref].
1320          There's no such thing as a mutable pointer-to-member, so
1321          things are not as complex as they are for references to
1322          non-static data members.  */
1323       type = cp_build_qualified_type (type,
1324                                       (cp_type_quals (type)
1325                                        | cp_type_quals (TREE_TYPE (datum))));
1326
1327       datum = build_address (datum);
1328
1329       /* Convert object to the correct base.  */
1330       if (binfo)
1331         datum = build_base_path (PLUS_EXPR, datum, binfo, 1);
1332
1333       /* Build an expression for "object + offset" where offset is the
1334          value stored in the pointer-to-data-member.  */
1335       datum = build2 (PLUS_EXPR, build_pointer_type (type),
1336                       datum, build_nop (ptrdiff_type_node, component));
1337       return build_indirect_ref (datum, 0);
1338     }
1339   else
1340     return build2 (OFFSET_REF, type, datum, component);
1341 }
1342
1343 /* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
1344
1345 tree
1346 build_functional_cast (tree exp, tree parms)
1347 {
1348   /* This is either a call to a constructor,
1349      or a C cast in C++'s `functional' notation.  */
1350   tree type;
1351
1352   if (exp == error_mark_node || parms == error_mark_node)
1353     return error_mark_node;
1354
1355   if (TREE_CODE (exp) == TYPE_DECL)
1356     type = TREE_TYPE (exp);
1357   else
1358     type = exp;
1359
1360   if (processing_template_decl)
1361     {
1362       tree t = build_min (CAST_EXPR, type, parms);
1363       /* We don't know if it will or will not have side effects.  */
1364       TREE_SIDE_EFFECTS (t) = 1;
1365       return t;
1366     }
1367
1368   if (! IS_AGGR_TYPE (type))
1369     {
1370       /* This must build a C cast.  */
1371       if (parms == NULL_TREE)
1372         parms = integer_zero_node;
1373       else
1374         parms = build_x_compound_expr_from_list (parms, "functional cast");
1375
1376       return build_c_cast (type, parms);
1377     }
1378
1379   /* Prepare to evaluate as a call to a constructor.  If this expression
1380      is actually used, for example,
1381
1382      return X (arg1, arg2, ...);
1383
1384      then the slot being initialized will be filled in.  */
1385
1386   if (!complete_type_or_else (type, NULL_TREE))
1387     return error_mark_node;
1388   if (abstract_virtuals_error (NULL_TREE, type))
1389     return error_mark_node;
1390
1391   if (parms && TREE_CHAIN (parms) == NULL_TREE)
1392     return build_c_cast (type, TREE_VALUE (parms));
1393
1394   /* We need to zero-initialize POD types.  Let's do that for everything
1395      that doesn't need a constructor.  */
1396   if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1397       && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1398     {
1399       exp = build_constructor (type, NULL_TREE);
1400       return get_target_expr (exp);
1401     }
1402
1403   exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms,
1404                                    type, LOOKUP_NORMAL);
1405
1406   if (exp == error_mark_node)
1407     return error_mark_node;
1408
1409   return build_cplus_new (type, exp);
1410 }
1411 \f
1412
1413 /* Add new exception specifier SPEC, to the LIST we currently have.
1414    If it's already in LIST then do nothing.
1415    Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1416    know what we're doing.  */
1417
1418 tree
1419 add_exception_specifier (tree list, tree spec, int complain)
1420 {
1421   bool ok;
1422   tree core = spec;
1423   bool is_ptr;
1424   int diag_type = -1; /* none */
1425
1426   if (spec == error_mark_node)
1427     return list;
1428
1429   gcc_assert (spec && (!list || TREE_VALUE (list)));
1430
1431   /* [except.spec] 1, type in an exception specifier shall not be
1432      incomplete, or pointer or ref to incomplete other than pointer
1433      to cv void.  */
1434   is_ptr = TREE_CODE (core) == POINTER_TYPE;
1435   if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1436     core = TREE_TYPE (core);
1437   if (complain < 0)
1438     ok = true;
1439   else if (VOID_TYPE_P (core))
1440     ok = is_ptr;
1441   else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1442     ok = true;
1443   else if (processing_template_decl)
1444     ok = true;
1445   else
1446     {
1447       ok = true;
1448       /* 15.4/1 says that types in an exception specifier must be complete,
1449          but it seems more reasonable to only require this on definitions
1450          and calls.  So just give a pedwarn at this point; we will give an
1451          error later if we hit one of those two cases.  */
1452       if (!COMPLETE_TYPE_P (complete_type (core)))
1453         diag_type = 2; /* pedwarn */
1454     }
1455
1456   if (ok)
1457     {
1458       tree probe;
1459
1460       for (probe = list; probe; probe = TREE_CHAIN (probe))
1461         if (same_type_p (TREE_VALUE (probe), spec))
1462           break;
1463       if (!probe)
1464         list = tree_cons (NULL_TREE, spec, list);
1465     }
1466   else
1467     diag_type = 0; /* error */
1468
1469   if (diag_type >= 0 && complain)
1470     cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1471
1472   return list;
1473 }
1474
1475 /* Combine the two exceptions specifier lists LIST and ADD, and return
1476    their union.  */
1477
1478 tree
1479 merge_exception_specifiers (tree list, tree add)
1480 {
1481   if (!list || !add)
1482     return NULL_TREE;
1483   else if (!TREE_VALUE (list))
1484     return add;
1485   else if (!TREE_VALUE (add))
1486     return list;
1487   else
1488     {
1489       tree orig_list = list;
1490
1491       for (; add; add = TREE_CHAIN (add))
1492         {
1493           tree spec = TREE_VALUE (add);
1494           tree probe;
1495
1496           for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1497             if (same_type_p (TREE_VALUE (probe), spec))
1498               break;
1499           if (!probe)
1500             {
1501               spec = build_tree_list (NULL_TREE, spec);
1502               TREE_CHAIN (spec) = list;
1503               list = spec;
1504             }
1505         }
1506     }
1507   return list;
1508 }
1509
1510 /* Subroutine of build_call.  Ensure that each of the types in the
1511    exception specification is complete.  Technically, 15.4/1 says that
1512    they need to be complete when we see a declaration of the function,
1513    but we should be able to get away with only requiring this when the
1514    function is defined or called.  See also add_exception_specifier.  */
1515
1516 void
1517 require_complete_eh_spec_types (tree fntype, tree decl)
1518 {
1519   tree raises;
1520   /* Don't complain about calls to op new.  */
1521   if (decl && DECL_ARTIFICIAL (decl))
1522     return;
1523   for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1524        raises = TREE_CHAIN (raises))
1525     {
1526       tree type = TREE_VALUE (raises);
1527       if (type && !COMPLETE_TYPE_P (type))
1528         {
1529           if (decl)
1530             error
1531               ("call to function %qD which throws incomplete type %q#T",
1532                decl, type);
1533           else
1534             error ("call to function which throws incomplete type %q#T",
1535                    decl);
1536         }
1537     }
1538 }
1539
1540 \f
1541 #include "gt-cp-typeck2.h"