OSDN Git Service

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