OSDN Git Service

758f5f06de6bcbecdc8b5b05fbcb6d4624c5de05
[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
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, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, 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_ignore, 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 *, ...);
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, otherwise cp_error_at. */
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) *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         cp_error_at ("cannot declare variable %q+D to be of abstract "
293                      "type %qT", decl, type);
294       else if (TREE_CODE (decl) == PARM_DECL)
295         cp_error_at ("cannot declare parameter %q+D to be of abstract "
296                      "type %qT", decl, type);
297       else if (TREE_CODE (decl) == FIELD_DECL)
298         cp_error_at ("cannot declare field %q+D to be of abstract "
299                      "type %qT", decl, type);
300       else if (TREE_CODE (decl) == FUNCTION_DECL
301                && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
302         cp_error_at ("invalid abstract return type for member function %q+#D",
303                      decl);
304       else if (TREE_CODE (decl) == FUNCTION_DECL)
305         cp_error_at ("invalid abstract return type for function %q+#D", 
306                      decl);
307       else if (TREE_CODE (decl) == IDENTIFIER_NODE)
308         /* Here we do not have location information, so use error instead
309            of cp_error_at.  */
310         error ("invalid abstract type %qT for %qE", type, decl);
311       else
312         cp_error_at ("invalid abstract type for %q+D", decl);
313     }
314   else
315     error ("cannot allocate an object of abstract type %qT", type);
316
317   /* Only go through this once.  */
318   if (VEC_length (tree, pure))
319     {
320       unsigned ix;
321       tree fn;
322       
323       inform ("%J  because the following virtual functions are pure "
324               "within %qT:", TYPE_MAIN_DECL (type), type);
325
326       for (ix = 0; VEC_iterate (tree, pure, ix, fn); ix++)
327         inform ("%J\t%#D", fn, fn);
328       /* Now truncate the vector.  This leaves it non-null, so we know
329          there are pure virtuals, but empty so we don't list them out
330          again.  */
331       VEC_truncate (tree, pure, 0);
332     }
333   else
334     inform ("%J  since type %qT has pure virtual functions", 
335             TYPE_MAIN_DECL (type), type);
336
337   return 1;
338 }
339
340 /* Print an error message for invalid use of an incomplete type.
341    VALUE is the expression that was used (or 0 if that isn't known)
342    and TYPE is the type that was invalid.  DIAG_TYPE indicates the
343    type of diagnostic:  0 for an error, 1 for a warning, 2 for a
344    pedwarn.  */
345
346 void
347 cxx_incomplete_type_diagnostic (tree value, tree type, int diag_type)
348 {
349   int decl = 0;
350   void (*p_msg) (const char *, ...);
351   void (*p_msg_at) (const char *, ...);
352
353   if (diag_type == 1)
354     {
355       p_msg = warning;
356       p_msg_at = cp_warning_at;
357     }
358   else if (diag_type == 2)
359     {
360       p_msg = pedwarn;
361       p_msg_at = cp_pedwarn_at;
362     }
363   else
364     {
365       p_msg = error;
366       p_msg_at = cp_error_at;
367     }
368   
369   /* Avoid duplicate error message.  */
370   if (TREE_CODE (type) == ERROR_MARK)
371     return;
372
373   if (value != 0 && (TREE_CODE (value) == VAR_DECL
374                      || TREE_CODE (value) == PARM_DECL
375                      || TREE_CODE (value) == FIELD_DECL))
376     {
377       (*p_msg_at) ("%qD has incomplete type", value);
378       decl = 1;
379     }
380  retry:
381   /* We must print an error message.  Be clever about what it says.  */
382
383   switch (TREE_CODE (type))
384     {
385     case RECORD_TYPE:
386     case UNION_TYPE:
387     case ENUMERAL_TYPE:
388       if (!decl)
389         (*p_msg) ("invalid use of undefined type %q#T", type);
390       if (!TYPE_TEMPLATE_INFO (type))
391         (*p_msg_at) ("forward declaration of %q#T", type);
392       else
393         (*p_msg_at) ("declaration of %q#T", type);
394       break;
395
396     case VOID_TYPE:
397       (*p_msg) ("invalid use of %qT", type);
398       break;
399
400     case ARRAY_TYPE:
401       if (TYPE_DOMAIN (type))
402         {
403           type = TREE_TYPE (type);
404           goto retry;
405         }
406       (*p_msg) ("invalid use of array with unspecified bounds");
407       break;
408
409     case OFFSET_TYPE:
410     bad_member:
411       (*p_msg) ("invalid use of member (did you forget the %<&%> ?)");
412       break;
413
414     case TEMPLATE_TYPE_PARM:
415       (*p_msg) ("invalid use of template type parameter");
416       break;
417
418     case UNKNOWN_TYPE:
419       if (value && TREE_CODE (value) == COMPONENT_REF)
420         goto bad_member;
421       else if (value && TREE_CODE (value) == ADDR_EXPR)
422         (*p_msg) ("address of overloaded function with no contextual "
423                   "type information");
424       else if (value && TREE_CODE (value) == OVERLOAD)
425         (*p_msg) ("overloaded function with no contextual type information");
426       else
427         (*p_msg) ("insufficient contextual information to determine type");
428       break;
429     
430     default:
431       gcc_unreachable ();
432     }
433 }
434
435 /* Backward-compatibility interface to incomplete_type_diagnostic;
436    required by ../tree.c.  */
437 #undef cxx_incomplete_type_error
438 void
439 cxx_incomplete_type_error (tree value, tree type)
440 {
441   cxx_incomplete_type_diagnostic (value, type, 0);
442 }
443
444 \f
445 /* The recursive part of split_nonconstant_init.  DEST is an lvalue
446    expression to which INIT should be assigned.  INIT is a CONSTRUCTOR.  */
447
448 static void
449 split_nonconstant_init_1 (tree dest, tree init)
450 {
451   tree *pelt, elt, type = TREE_TYPE (dest);
452   tree sub, code, inner_type = NULL;
453   bool array_type_p = false;
454
455   pelt = &CONSTRUCTOR_ELTS (init);
456   switch (TREE_CODE (type))
457     {
458     case ARRAY_TYPE:
459       inner_type = TREE_TYPE (type);
460       array_type_p = true;
461       /* FALLTHRU */
462
463     case RECORD_TYPE:
464     case UNION_TYPE:
465     case QUAL_UNION_TYPE:
466       while ((elt = *pelt))
467         {
468           tree field_index = TREE_PURPOSE (elt);
469           tree value = TREE_VALUE (elt);
470
471           if (!array_type_p)
472             inner_type = TREE_TYPE (field_index);
473
474           if (TREE_CODE (value) == CONSTRUCTOR)
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               split_nonconstant_init_1 (sub, value);
484             }
485           else if (!initializer_constant_valid_p (value, inner_type))
486             {
487               *pelt = TREE_CHAIN (elt);
488
489               if (array_type_p)
490                 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
491                               NULL_TREE, NULL_TREE);
492               else
493                 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
494                               NULL_TREE);
495
496               code = build2 (MODIFY_EXPR, inner_type, sub, value);
497               code = build_stmt (EXPR_STMT, code);
498               add_stmt (code);
499               continue;
500             }
501
502           pelt = &TREE_CHAIN (elt);
503         }
504       break;
505
506     case VECTOR_TYPE:
507       if (!initializer_constant_valid_p (init, type))
508         {
509           CONSTRUCTOR_ELTS (init) = NULL;
510           code = build2 (MODIFY_EXPR, type, dest, init);
511           code = build_stmt (EXPR_STMT, code);
512           add_stmt (code);
513         }
514       break;
515
516     default:
517       gcc_unreachable ();
518     }
519 }
520
521 /* A subroutine of store_init_value.  Splits non-constant static 
522    initializer INIT into a constant part and generates code to
523    perform the non-constant part of the initialization to DEST.
524    Returns the code for the runtime init.  */
525
526 static tree
527 split_nonconstant_init (tree dest, tree init)
528 {
529   tree code;
530
531   if (TREE_CODE (init) == CONSTRUCTOR)
532     {
533       code = push_stmt_list ();
534       split_nonconstant_init_1 (dest, init);
535       code = pop_stmt_list (code);
536       DECL_INITIAL (dest) = init;
537       TREE_READONLY (dest) = 0;
538     }
539   else
540     code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
541
542   return code;
543 }
544
545 /* Perform appropriate conversions on the initial value of a variable,
546    store it in the declaration DECL,
547    and print any error messages that are appropriate.
548    If the init is invalid, store an ERROR_MARK.
549
550    C++: Note that INIT might be a TREE_LIST, which would mean that it is
551    a base class initializer for some aggregate type, hopefully compatible
552    with DECL.  If INIT is a single element, and DECL is an aggregate
553    type, we silently convert INIT into a TREE_LIST, allowing a constructor
554    to be called.
555
556    If INIT is a TREE_LIST and there is no constructor, turn INIT
557    into a CONSTRUCTOR and use standard initialization techniques.
558    Perhaps a warning should be generated?
559
560    Returns code to be executed if initialization could not be performed
561    for static variable.  In that case, caller must emit the code.  */
562
563 tree
564 store_init_value (tree decl, tree init)
565 {
566   tree value, type;
567
568   /* If variable's type was invalidly declared, just ignore it.  */
569
570   type = TREE_TYPE (decl);
571   if (TREE_CODE (type) == ERROR_MARK)
572     return NULL_TREE;
573
574   if (IS_AGGR_TYPE (type))
575     {
576       gcc_assert (TYPE_HAS_TRIVIAL_INIT_REF (type)
577                   || TREE_CODE (init) == CONSTRUCTOR);
578
579       if (TREE_CODE (init) == TREE_LIST)
580         {
581           error ("constructor syntax used, but no constructor declared "
582                  "for type %qT", type);
583           init = build_constructor (NULL_TREE, nreverse (init));
584         }
585     }
586   else if (TREE_CODE (init) == TREE_LIST
587            && TREE_TYPE (init) != unknown_type_node)
588     {
589       if (TREE_CODE (decl) == RESULT_DECL)
590         init = build_x_compound_expr_from_list (init,
591                                                 "return value initializer");
592       else if (TREE_CODE (init) == TREE_LIST
593                && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
594         {
595           error ("cannot initialize arrays using this syntax");
596           return NULL_TREE;
597         }
598       else
599         /* We get here with code like `int a (2);' */
600         init = build_x_compound_expr_from_list (init, "initializer");
601     }
602
603   /* End of special C++ code.  */
604
605   /* Digest the specified initializer into an expression.  */
606   value = digest_init (type, init, (tree *) 0);
607
608   /* Store the expression if valid; else report error.  */
609
610   if (TREE_CODE (value) == ERROR_MARK)
611     ;
612   /* Other code expects that initializers for objects of types that need
613      constructing never make it into DECL_INITIAL, and passes 'init' to
614      build_aggr_init without checking DECL_INITIAL.  So just return.  */
615   else if (TYPE_NEEDS_CONSTRUCTING (type))
616     return build2 (INIT_EXPR, type, decl, value);
617   else if (TREE_STATIC (decl)
618            && (TREE_SIDE_EFFECTS (value)
619                || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
620     return split_nonconstant_init (decl, value);
621   
622   /* Store the VALUE in DECL_INITIAL.  If we're building a
623      statement-tree we will actually expand the initialization later
624      when we output this function.  */
625   DECL_INITIAL (decl) = value;
626   return NULL_TREE;
627 }
628
629 \f
630 /* Digest the parser output INIT as an initializer for type TYPE.
631    Return a C expression of type TYPE to represent the initial value.
632
633    If TAIL is nonzero, it points to a variable holding a list of elements
634    of which INIT is the first.  We update the list stored there by
635    removing from the head all the elements that we use.
636    Normally this is only one; we use more than one element only if
637    TYPE is an aggregate and INIT is not a constructor.  */
638
639 tree
640 digest_init (tree type, tree init, tree* tail)
641 {
642   enum tree_code code = TREE_CODE (type);
643   tree element = NULL_TREE;
644   tree old_tail_contents = NULL_TREE;
645
646   /* By default, assume we use one element from a list.
647      We correct this later in the sole case where it is not true.  */
648
649   if (tail)
650     {
651       old_tail_contents = *tail;
652       *tail = TREE_CHAIN (*tail);
653     }
654
655   if (init == error_mark_node || (TREE_CODE (init) == TREE_LIST
656                                   && TREE_VALUE (init) == error_mark_node))
657     return error_mark_node;
658
659   if (TREE_CODE (init) == ERROR_MARK)
660     /* __PRETTY_FUNCTION__'s initializer is a bogus expression inside
661        a template function. This gets substituted during instantiation.  */
662     return init;
663
664   /* We must strip the outermost array type when completing the type,
665      because the its bounds might be incomplete at the moment.  */
666   if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
667                               ? TREE_TYPE (type) : type, NULL_TREE))
668     return error_mark_node;
669   
670   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
671   if (TREE_CODE (init) == NON_LVALUE_EXPR)
672     init = TREE_OPERAND (init, 0);
673
674   if (BRACE_ENCLOSED_INITIALIZER_P (init)
675       && CONSTRUCTOR_ELTS (init) != 0
676       && TREE_CHAIN (CONSTRUCTOR_ELTS (init)) == 0)
677     {
678       element = TREE_VALUE (CONSTRUCTOR_ELTS (init));
679       /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue.  */
680       if (element && TREE_CODE (element) == NON_LVALUE_EXPR)
681         element = TREE_OPERAND (element, 0);
682       if (element == error_mark_node)
683         return element;
684     }
685
686   /* Initialization of an array of chars from a string constant
687      optionally enclosed in braces.  */
688
689   if (code == ARRAY_TYPE)
690     {
691       tree typ1;
692
693       if (TREE_CODE (init) == TREE_LIST)
694         {
695           error ("initializing array with parameter list");
696           return error_mark_node;
697         }
698
699       typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
700       if (char_type_p (typ1)
701           && ((init && TREE_CODE (init) == STRING_CST)
702               || (element && TREE_CODE (element) == STRING_CST)))
703         {
704           tree string = element ? element : init;
705
706           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
707                != char_type_node)
708               && TYPE_PRECISION (typ1) == BITS_PER_UNIT)
709             {
710               error ("char-array initialized from wide string");
711               return error_mark_node;
712             }
713           if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string)))
714                == char_type_node)
715               && TYPE_PRECISION (typ1) != BITS_PER_UNIT)
716             {
717               error ("int-array initialized from non-wide string");
718               return error_mark_node;
719             }
720
721           TREE_TYPE (string) = type;
722           if (TYPE_DOMAIN (type) != 0
723               && TREE_CONSTANT (TYPE_SIZE (type)))
724             {
725               int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
726               size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
727               /* In C it is ok to subtract 1 from the length of the string
728                  because it's ok to ignore the terminating null char that is
729                  counted in the length of the constant, but in C++ this would
730                  be invalid.  */
731               if (size < TREE_STRING_LENGTH (string))
732                 pedwarn ("initializer-string for array of chars is too long");
733             }
734           return string;
735         }
736     }
737
738   /* Handle scalar types, including conversions,
739      and signature pointers and references.  */
740
741   if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
742       || code == ENUMERAL_TYPE || code == REFERENCE_TYPE
743       || code == BOOLEAN_TYPE || code == COMPLEX_TYPE
744       || TYPE_PTR_TO_MEMBER_P (type))
745     {
746       if (BRACE_ENCLOSED_INITIALIZER_P (init))
747         {
748           if (element == 0)
749             {
750               error ("initializer for scalar variable requires one element");
751               return error_mark_node;
752             }
753           init = element;
754         }
755       while (BRACE_ENCLOSED_INITIALIZER_P (init))
756         {
757           pedwarn ("braces around scalar initializer for %qT", type);
758           init = CONSTRUCTOR_ELTS (init);
759           if (TREE_CHAIN (init))
760             pedwarn ("ignoring extra initializers for %qT", type);
761           init = TREE_VALUE (init);
762         }
763
764       return convert_for_initialization (0, type, init, LOOKUP_NORMAL,
765                                          "initialization", NULL_TREE, 0);
766     }
767
768   /* Come here only for records and arrays (and unions with constructors).  */
769
770   if (COMPLETE_TYPE_P (type) && ! TREE_CONSTANT (TYPE_SIZE (type)))
771     {
772       error ("variable-sized object of type %qT may not be initialized",
773                 type);
774       return error_mark_node;
775     }
776
777   if (code == ARRAY_TYPE || code == VECTOR_TYPE || IS_AGGR_TYPE_CODE (code))
778     {
779       if (BRACE_ENCLOSED_INITIALIZER_P (init))
780         {
781           if (TYPE_NON_AGGREGATE_CLASS (type))
782             {
783               error ("subobject of type %qT must be initialized by "
784                      "constructor, not by %qE",
785                      type, init);
786               return error_mark_node;
787             }
788           return process_init_constructor (type, init, (tree *)0);
789         }
790       else if (can_convert_arg (type, TREE_TYPE (init), init)
791                || TYPE_NON_AGGREGATE_CLASS (type))
792         /* These are never initialized from multiple constructor elements.  */;
793       else if (tail != 0)
794         {
795           *tail = old_tail_contents;
796           return process_init_constructor (type, 0, tail);
797         }
798
799       if (code != ARRAY_TYPE)
800         {
801           int flags = LOOKUP_NORMAL;
802           /* Initialization from { } is copy-initialization.  */
803           if (tail)
804             flags |= LOOKUP_ONLYCONVERTING;
805
806           return convert_for_initialization (NULL_TREE, type, init, flags,
807                                              "initialization", NULL_TREE, 0);
808         }
809     }
810
811   error ("invalid initializer");
812   return error_mark_node;
813 }
814 \f
815 /* Process a constructor for a variable of type TYPE.
816    The constructor elements may be specified either with INIT or with ELTS,
817    only one of which should be non-null.
818
819    If INIT is specified, it is a CONSTRUCTOR node which is specifically
820    and solely for initializing this datum.
821
822    If ELTS is specified, it is the address of a variable containing
823    a list of expressions.  We take as many elements as we need
824    from the head of the list and update the list.
825
826    In the resulting constructor, TREE_CONSTANT is set if all elts are
827    constant, and TREE_STATIC is set if, in addition, all elts are simple enough
828    constants that the assembler and linker can compute them.  */
829
830 static tree
831 process_init_constructor (tree type, tree init, tree* elts)
832 {
833   tree tail;
834   /* List of the elements of the result constructor,
835      in reverse order.  */
836   tree members = NULL;
837   tree next1;
838   tree result;
839   int allconstant = 1;
840   int allsimple = 1;
841   int erroneous = 0;
842
843   /* Make TAIL be the list of elements to use for the initialization,
844      no matter how the data was given to us.  */
845
846   if (elts)
847     {
848       if (warn_missing_braces)
849         warning ("aggregate has a partly bracketed initializer");
850       tail = *elts;
851     }
852   else
853     tail = CONSTRUCTOR_ELTS (init);
854
855   /* Gobble as many elements as needed, and make a constructor or initial value
856      for each element of this aggregate.  Chain them together in result.
857      If there are too few, use 0 for each scalar ultimate component.  */
858
859   if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
860     {
861       long len;
862       int i;
863
864       if (TREE_CODE (type) == ARRAY_TYPE)
865         {
866           tree domain = TYPE_DOMAIN (type);
867           if (domain)
868             len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
869                    - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
870                    + 1);
871           else
872             len = -1;  /* Take as many as there are.  */
873         }
874       else
875         {
876           /* Vectors are like simple fixed-size arrays.  */
877           len = TYPE_VECTOR_SUBPARTS (type);
878         }
879
880       for (i = 0; len < 0 || i < len; i++)
881         {
882           if (tail)
883             {
884               if (TREE_PURPOSE (tail)
885                   && (TREE_CODE (TREE_PURPOSE (tail)) != INTEGER_CST
886                       || compare_tree_int (TREE_PURPOSE (tail), i) != 0))
887                 sorry ("non-trivial labeled initializers");
888
889               if (TREE_VALUE (tail) != 0)
890                 {
891                   tree tail1 = tail;
892                   next1 = digest_init (TREE_TYPE (type),
893                                        TREE_VALUE (tail), &tail1);
894                   if (next1 == error_mark_node)
895                     return next1;
896                   gcc_assert (same_type_ignoring_top_level_qualifiers_p
897                               (TREE_TYPE (type), TREE_TYPE (next1)));
898                   gcc_assert (!tail1 || TREE_CODE (tail1) == TREE_LIST);
899                   if (tail == tail1 && len < 0)
900                     {
901                       error ("non-empty initializer for array of empty elements");
902                       /* Just ignore what we were supposed to use.  */
903                       tail1 = NULL_TREE;
904                     }
905                   tail = tail1;
906                 }
907               else
908                 {
909                   next1 = error_mark_node;
910                   tail = TREE_CHAIN (tail);
911                 }
912             }
913           else if (len < 0)
914             /* We're done.  */
915             break;
916           else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
917             {
918               /* If this type needs constructors run for
919                  default-initialization, we can't rely on the backend to do it
920                  for us, so build up TARGET_EXPRs.  If the type in question is
921                  a class, just build one up; if it's an array, recurse.  */
922
923               if (IS_AGGR_TYPE (TREE_TYPE (type)))
924                 next1 = build_functional_cast (TREE_TYPE (type), NULL_TREE);
925               else
926                 next1 = build_constructor (NULL_TREE, NULL_TREE);
927               next1 = digest_init (TREE_TYPE (type), next1, 0);
928             }
929           else if (! zero_init_p (TREE_TYPE (type)))
930             next1 = build_zero_init (TREE_TYPE (type),
931                                      /*nelts=*/NULL_TREE,
932                                      /*static_storage_p=*/false);
933           else
934             /* The default zero-initialization is fine for us; don't
935                add anything to the CONSTRUCTOR.  */
936             break;
937
938           if (next1 == error_mark_node)
939             erroneous = 1;
940           else if (!TREE_CONSTANT (next1))
941             allconstant = 0;
942           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
943             allsimple = 0;
944           members = tree_cons (size_int (i), next1, members);
945         }
946     }
947   else if (TREE_CODE (type) == RECORD_TYPE)
948     {
949       tree field;
950
951       if (tail)
952         {
953           gcc_assert (!CLASSTYPE_VBASECLASSES (type));
954           gcc_assert (!TYPE_BINFO (type)
955                       || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
956           gcc_assert (!TYPE_POLYMORPHIC_P (type));
957         }
958
959       for (field = TYPE_FIELDS (type); field;
960            field = TREE_CHAIN (field))
961         {
962           if (! DECL_NAME (field) && DECL_C_BIT_FIELD (field))
963             {
964               members = tree_cons (field, integer_zero_node, members);
965               continue;
966             }
967
968           if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
969             continue;
970
971           if (tail)
972             {
973               if (TREE_PURPOSE (tail)
974                   && TREE_PURPOSE (tail) != field
975                   && TREE_PURPOSE (tail) != DECL_NAME (field))
976                 sorry ("non-trivial labeled initializers");
977
978               if (TREE_VALUE (tail) != 0)
979                 {
980                   tree tail1 = tail;
981
982                   next1 = digest_init (TREE_TYPE (field),
983                                        TREE_VALUE (tail), &tail1);
984                   gcc_assert (!tail1 || TREE_CODE (tail1) == TREE_LIST);
985                   tail = tail1;
986                 }
987               else
988                 {
989                   next1 = error_mark_node;
990                   tail = TREE_CHAIN (tail);
991                 }
992             }
993           else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
994             {
995               /* If this type needs constructors run for
996                  default-initialization, we can't rely on the backend to do it
997                  for us, so build up TARGET_EXPRs.  If the type in question is
998                  a class, just build one up; if it's an array, recurse.  */
999
1000               if (IS_AGGR_TYPE (TREE_TYPE (field)))
1001                 next1 = build_functional_cast (TREE_TYPE (field),
1002                                                NULL_TREE);
1003               else
1004                 {
1005                   next1 = build_constructor (NULL_TREE, NULL_TREE);
1006                   if (init)
1007                     TREE_HAS_CONSTRUCTOR (next1)
1008                        = TREE_HAS_CONSTRUCTOR (init);
1009                 }
1010               next1 = digest_init (TREE_TYPE (field), next1, 0);
1011
1012               /* Warn when some struct elements are implicitly initialized.  */
1013               if (warn_missing_field_initializers
1014                   && (!init || BRACE_ENCLOSED_INITIALIZER_P (init)))
1015                 warning ("missing initializer for member %qD", field);
1016             }
1017           else
1018             {
1019               if (TREE_READONLY (field))
1020                 error ("uninitialized const member %qD", field);
1021               else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1022                 error ("member %qD with uninitialized const fields", field);
1023               else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1024                 error ("member %qD is uninitialized reference", field);
1025
1026               /* Warn when some struct elements are implicitly initialized
1027                  to zero.  */
1028               if (warn_missing_field_initializers
1029                   && (!init || BRACE_ENCLOSED_INITIALIZER_P (init)))
1030                 warning ("missing initializer for member %qD", field);
1031
1032               if (! zero_init_p (TREE_TYPE (field)))
1033                 next1 = build_zero_init (TREE_TYPE (field),
1034                                          /*nelts=*/NULL_TREE,
1035                                          /*static_storage_p=*/false);
1036               else
1037                 /* The default zero-initialization is fine for us; don't
1038                    add anything to the CONSTRUCTOR.  */
1039                 continue;
1040             }
1041
1042           if (next1 == error_mark_node)
1043             erroneous = 1;
1044           else if (!TREE_CONSTANT (next1))
1045             allconstant = 0;
1046           else if (! initializer_constant_valid_p (next1, TREE_TYPE (next1)))
1047             allsimple = 0;
1048           members = tree_cons (field, next1, members);
1049         }
1050     }
1051   else if (TREE_CODE (type) == UNION_TYPE
1052            /* If the initializer was empty, use default zero initialization.  */
1053            && tail)
1054     {
1055       tree field = TYPE_FIELDS (type);
1056
1057       /* Find the first named field.  ANSI decided in September 1990
1058          that only named fields count here.  */
1059       while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1060         field = TREE_CHAIN (field);
1061
1062       /* If this element specifies a field, initialize via that field.  */
1063       if (TREE_PURPOSE (tail) != NULL_TREE)
1064         {
1065           int win = 0;
1066
1067           if (TREE_CODE (TREE_PURPOSE (tail)) == FIELD_DECL)
1068             /* Handle the case of a call by build_c_cast.  */
1069             field = TREE_PURPOSE (tail), win = 1;
1070           else if (TREE_CODE (TREE_PURPOSE (tail)) != IDENTIFIER_NODE)
1071             error ("index value instead of field name in union initializer");
1072           else
1073             {
1074               tree temp;
1075               for (temp = TYPE_FIELDS (type);
1076                    temp;
1077                    temp = TREE_CHAIN (temp))
1078                 if (DECL_NAME (temp) == TREE_PURPOSE (tail))
1079                   break;
1080               if (temp)
1081                 field = temp, win = 1;
1082               else
1083                 error ("no field %qD in union being initialized",
1084                        TREE_PURPOSE (tail));
1085             }
1086           if (!win)
1087             TREE_VALUE (tail) = error_mark_node;
1088         }
1089       else if (field == 0)
1090         {
1091           error ("union %qT with no named members cannot be initialized",
1092                  type);
1093           TREE_VALUE (tail) = error_mark_node;
1094         }
1095
1096       if (TREE_VALUE (tail) != 0)
1097         {
1098           tree tail1 = tail;
1099
1100           next1 = digest_init (TREE_TYPE (field),
1101                                TREE_VALUE (tail), &tail1);
1102           gcc_assert (!tail1 || TREE_CODE (tail1) == TREE_LIST);
1103           tail = tail1;
1104         }
1105       else
1106         {
1107           next1 = error_mark_node;
1108           tail = TREE_CHAIN (tail);
1109         }
1110
1111       if (next1 == error_mark_node)
1112         erroneous = 1;
1113       else if (!TREE_CONSTANT (next1))
1114         allconstant = 0;
1115       else if (initializer_constant_valid_p (next1, TREE_TYPE (next1)) == 0)
1116         allsimple = 0;
1117       members = tree_cons (field, next1, members);
1118     }
1119
1120   /* If arguments were specified as a list, just remove the ones we used.  */
1121   if (elts)
1122     *elts = tail;
1123   /* If arguments were specified as a constructor,
1124      complain unless we used all the elements of the constructor.  */
1125   else if (tail)
1126     pedwarn ("excess elements in aggregate initializer");
1127
1128   if (erroneous)
1129     return error_mark_node;
1130
1131   result = build_constructor (type, nreverse (members));
1132   if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1133     complete_array_type (type, result, /*do_default=*/0);
1134   if (init)
1135     TREE_HAS_CONSTRUCTOR (result) = TREE_HAS_CONSTRUCTOR (init);
1136   if (allconstant)
1137     {
1138       TREE_CONSTANT (result) = 1;
1139       TREE_INVARIANT (result) = 1;
1140       if (allsimple)
1141         TREE_STATIC (result) = 1;
1142     }
1143   return result;
1144 }
1145 \f
1146 /* Given a structure or union value DATUM, construct and return
1147    the structure or union component which results from narrowing
1148    that value to the base specified in BASETYPE.  For example, given the
1149    hierarchy
1150
1151    class L { int ii; };
1152    class A : L { ... };
1153    class B : L { ... };
1154    class C : A, B { ... };
1155
1156    and the declaration
1157
1158    C x;
1159
1160    then the expression
1161
1162    x.A::ii refers to the ii member of the L part of
1163    the A part of the C object named by X.  In this case,
1164    DATUM would be x, and BASETYPE would be A.
1165
1166    I used to think that this was nonconformant, that the standard specified
1167    that first we look up ii in A, then convert x to an L& and pull out the
1168    ii part.  But in fact, it does say that we convert x to an A&; A here
1169    is known as the "naming class".  (jason 2000-12-19)
1170
1171    BINFO_P points to a variable initialized either to NULL_TREE or to the
1172    binfo for the specific base subobject we want to convert to.  */
1173
1174 tree
1175 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1176 {
1177   tree binfo;
1178
1179   if (datum == error_mark_node)
1180     return error_mark_node;
1181   if (*binfo_p)
1182     binfo = *binfo_p;
1183   else
1184     binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
1185
1186   if (!binfo || binfo == error_mark_node)
1187     {
1188       *binfo_p = NULL_TREE;
1189       if (!binfo)
1190         error_not_base_type (basetype, TREE_TYPE (datum));
1191       return error_mark_node;
1192     }
1193
1194   *binfo_p = binfo;
1195   return build_base_path (PLUS_EXPR, datum, binfo, 1);
1196 }
1197
1198 /* Build a reference to an object specified by the C++ `->' operator.
1199    Usually this just involves dereferencing the object, but if the
1200    `->' operator is overloaded, then such overloads must be
1201    performed until an object which does not have the `->' operator
1202    overloaded is found.  An error is reported when circular pointer
1203    delegation is detected.  */
1204
1205 tree
1206 build_x_arrow (tree expr)
1207 {
1208   tree orig_expr = expr;
1209   tree types_memoized = NULL_TREE;
1210   tree type = TREE_TYPE (expr);
1211   tree last_rval = NULL_TREE;
1212
1213   if (type == error_mark_node)
1214     return error_mark_node;
1215
1216   if (processing_template_decl)
1217     {
1218       if (type_dependent_expression_p (expr))
1219         return build_min_nt (ARROW_EXPR, expr);
1220       expr = build_non_dependent_expr (expr);
1221     }
1222
1223   if (TREE_CODE (type) == REFERENCE_TYPE)
1224     {
1225       expr = convert_from_reference (expr);
1226       type = TREE_TYPE (expr);
1227     }
1228
1229   if (IS_AGGR_TYPE (type))
1230     {
1231       while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr,
1232                                    NULL_TREE, NULL_TREE,
1233                                    /*overloaded_p=*/NULL)))
1234         {
1235           if (expr == error_mark_node)
1236             return error_mark_node;
1237
1238           if (value_member (TREE_TYPE (expr), types_memoized))
1239             {
1240               error ("circular pointer delegation detected");
1241               return error_mark_node;
1242             }
1243           else
1244             {
1245               types_memoized = tree_cons (NULL_TREE, TREE_TYPE (expr),
1246                                           types_memoized);
1247             }
1248           last_rval = expr;
1249         }     
1250
1251       if (last_rval == NULL_TREE)
1252         {
1253           error ("base operand of %<->%> has non-pointer type %qT", type);
1254           return error_mark_node;
1255         }
1256
1257       if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1258         last_rval = convert_from_reference (last_rval);
1259     }
1260   else
1261     last_rval = decay_conversion (expr);
1262
1263   if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1264     {
1265       if (processing_template_decl)
1266         {
1267           expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr);
1268           /* It will be dereferenced.  */
1269           TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval));
1270           return expr;
1271         }
1272
1273       return build_indirect_ref (last_rval, NULL);
1274     }
1275
1276   if (types_memoized)
1277     error ("result of %<operator->()%> yields non-pointer result");
1278   else
1279     error ("base operand of %<->%> is not a pointer");
1280   return error_mark_node;
1281 }
1282
1283 /* Return an expression for "DATUM .* COMPONENT".  DATUM has not
1284    already been checked out to be of aggregate type.  */
1285
1286 tree
1287 build_m_component_ref (tree datum, tree component)
1288 {
1289   tree ptrmem_type;
1290   tree objtype;
1291   tree type;
1292   tree binfo;
1293   tree ctype;
1294
1295   datum = decay_conversion (datum);
1296
1297   if (datum == error_mark_node || component == error_mark_node)
1298     return error_mark_node;
1299
1300   ptrmem_type = TREE_TYPE (component);
1301   if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type))
1302     {
1303       error ("%qE cannot be used as a member pointer, since it is of "
1304              "type %qT", 
1305              component, ptrmem_type);
1306       return error_mark_node;
1307     }
1308     
1309   objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));  
1310   if (! IS_AGGR_TYPE (objtype))
1311     {
1312       error ("cannot apply member pointer %qE to %qE, which is of "
1313              "non-aggregate type %qT",
1314              component, datum, objtype);
1315       return error_mark_node;
1316     }
1317
1318   type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1319   ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1320
1321   if (!COMPLETE_TYPE_P (ctype))
1322     {
1323       if (!same_type_p (ctype, objtype))
1324         goto mismatch;
1325       binfo = NULL;
1326     }
1327   else
1328     {
1329       binfo = lookup_base (objtype, ctype, ba_check, NULL);
1330       
1331       if (!binfo)
1332         {
1333         mismatch:
1334           error ("pointer to member type %qT incompatible with object "
1335                  "type %qT",
1336                  type, objtype);
1337           return error_mark_node;
1338         }
1339       else if (binfo == error_mark_node)
1340         return error_mark_node;
1341     }
1342
1343   if (TYPE_PTRMEM_P (ptrmem_type))
1344     {
1345       /* Compute the type of the field, as described in [expr.ref].
1346          There's no such thing as a mutable pointer-to-member, so
1347          things are not as complex as they are for references to
1348          non-static data members.  */
1349       type = cp_build_qualified_type (type,
1350                                       (cp_type_quals (type)  
1351                                        | cp_type_quals (TREE_TYPE (datum))));
1352
1353       datum = build_address (datum);
1354       
1355       /* Convert object to the correct base.  */
1356       if (binfo)
1357         datum = build_base_path (PLUS_EXPR, datum, binfo, 1);
1358       
1359       /* Build an expression for "object + offset" where offset is the
1360          value stored in the pointer-to-data-member.  */
1361       datum = build2 (PLUS_EXPR, build_pointer_type (type),
1362                       datum, build_nop (ptrdiff_type_node, component));
1363       return build_indirect_ref (datum, 0);
1364     }
1365   else
1366     return build2 (OFFSET_REF, type, datum, component);
1367 }
1368
1369 /* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
1370
1371 tree
1372 build_functional_cast (tree exp, tree parms)
1373 {
1374   /* This is either a call to a constructor,
1375      or a C cast in C++'s `functional' notation.  */
1376   tree type;
1377
1378   if (exp == error_mark_node || parms == error_mark_node)
1379     return error_mark_node;
1380
1381   if (TREE_CODE (exp) == TYPE_DECL)
1382     type = TREE_TYPE (exp);
1383   else
1384     type = exp;
1385
1386   if (processing_template_decl)
1387     {
1388       tree t = build_min (CAST_EXPR, type, parms);
1389       /* We don't know if it will or will not have side effects.  */
1390       TREE_SIDE_EFFECTS (t) = 1;
1391       return t;
1392     }
1393
1394   if (! IS_AGGR_TYPE (type))
1395     {
1396       /* This must build a C cast.  */
1397       if (parms == NULL_TREE)
1398         parms = integer_zero_node;
1399       else
1400         parms = build_x_compound_expr_from_list (parms, "functional cast");
1401
1402       return build_c_cast (type, parms);
1403     }
1404
1405   /* Prepare to evaluate as a call to a constructor.  If this expression
1406      is actually used, for example,
1407          
1408      return X (arg1, arg2, ...);
1409          
1410      then the slot being initialized will be filled in.  */
1411
1412   if (!complete_type_or_else (type, NULL_TREE))
1413     return error_mark_node;
1414   if (abstract_virtuals_error (NULL_TREE, type))
1415     return error_mark_node;
1416
1417   if (parms && TREE_CHAIN (parms) == NULL_TREE)
1418     return build_c_cast (type, TREE_VALUE (parms));
1419
1420   /* We need to zero-initialize POD types.  Let's do that for everything
1421      that doesn't need a constructor.  */
1422   if (parms == NULL_TREE && !TYPE_NEEDS_CONSTRUCTING (type)
1423       && TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
1424     {
1425       exp = build_constructor (type, NULL_TREE);
1426       return get_target_expr (exp);
1427     }
1428
1429   exp = build_special_member_call (NULL_TREE, complete_ctor_identifier, parms,
1430                                    type, LOOKUP_NORMAL);
1431
1432   if (exp == error_mark_node)
1433     return error_mark_node;
1434
1435   return build_cplus_new (type, exp);
1436 }
1437 \f
1438
1439 /* Add new exception specifier SPEC, to the LIST we currently have.
1440    If it's already in LIST then do nothing.
1441    Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1442    know what we're doing.  */
1443
1444 tree
1445 add_exception_specifier (tree list, tree spec, int complain)
1446 {
1447   bool ok;
1448   tree core = spec;
1449   bool is_ptr;
1450   int diag_type = -1; /* none */
1451   
1452   if (spec == error_mark_node)
1453     return list;
1454   
1455   gcc_assert (spec && (!list || TREE_VALUE (list)));
1456   
1457   /* [except.spec] 1, type in an exception specifier shall not be
1458      incomplete, or pointer or ref to incomplete other than pointer
1459      to cv void.  */
1460   is_ptr = TREE_CODE (core) == POINTER_TYPE;
1461   if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1462     core = TREE_TYPE (core);
1463   if (complain < 0)
1464     ok = true;
1465   else if (VOID_TYPE_P (core))
1466     ok = is_ptr;
1467   else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1468     ok = true;
1469   else if (processing_template_decl)
1470     ok = true;
1471   else
1472     {
1473       ok = true;
1474       /* 15.4/1 says that types in an exception specifier must be complete,
1475          but it seems more reasonable to only require this on definitions
1476          and calls.  So just give a pedwarn at this point; we will give an
1477          error later if we hit one of those two cases.  */
1478       if (!COMPLETE_TYPE_P (complete_type (core)))
1479         diag_type = 2; /* pedwarn */
1480     }
1481
1482   if (ok)
1483     {
1484       tree probe;
1485       
1486       for (probe = list; probe; probe = TREE_CHAIN (probe))
1487         if (same_type_p (TREE_VALUE (probe), spec))
1488           break;
1489       if (!probe)
1490         list = tree_cons (NULL_TREE, spec, list);
1491     }
1492   else
1493     diag_type = 0; /* error */
1494     
1495   if (diag_type >= 0 && complain)
1496     cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1497
1498   return list;
1499 }
1500
1501 /* Combine the two exceptions specifier lists LIST and ADD, and return
1502    their union.  */
1503
1504 tree
1505 merge_exception_specifiers (tree list, tree add)
1506 {
1507   if (!list || !add)
1508     return NULL_TREE;
1509   else if (!TREE_VALUE (list))
1510     return add;
1511   else if (!TREE_VALUE (add))
1512     return list;
1513   else
1514     {
1515       tree orig_list = list;
1516       
1517       for (; add; add = TREE_CHAIN (add))
1518         {
1519           tree spec = TREE_VALUE (add);
1520           tree probe;
1521           
1522           for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1523             if (same_type_p (TREE_VALUE (probe), spec))
1524               break;
1525           if (!probe)
1526             {
1527               spec = build_tree_list (NULL_TREE, spec);
1528               TREE_CHAIN (spec) = list;
1529               list = spec;
1530             }
1531         }
1532     }
1533   return list;
1534 }
1535
1536 /* Subroutine of build_call.  Ensure that each of the types in the
1537    exception specification is complete.  Technically, 15.4/1 says that
1538    they need to be complete when we see a declaration of the function,
1539    but we should be able to get away with only requiring this when the
1540    function is defined or called.  See also add_exception_specifier.  */
1541
1542 void
1543 require_complete_eh_spec_types (tree fntype, tree decl)
1544 {
1545   tree raises;
1546   /* Don't complain about calls to op new.  */
1547   if (decl && DECL_ARTIFICIAL (decl))
1548     return;
1549   for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1550        raises = TREE_CHAIN (raises))
1551     {
1552       tree type = TREE_VALUE (raises);
1553       if (type && !COMPLETE_TYPE_P (type))
1554         {
1555           if (decl)
1556             error
1557               ("call to function %qD which throws incomplete type %q#T",
1558                decl, type);
1559           else
1560             error ("call to function which throws incomplete type %q#T",
1561                    decl);
1562         }
1563     }
1564 }
1565
1566 \f
1567 #include "gt-cp-typeck2.h"