OSDN Git Service

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