OSDN Git Service

PR c++/48450
[pf3gnuchains/gcc-fork.git] / gcc / cp / typeck2.c
1 /* Report error messages, build initializers, and perform
2    some front-end optimizations for C++ compiler.
3    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4    1999, 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 3, 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 COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24
25 /* This file is part of the C++ front end.
26    It contains routines to build C++ expressions given their operands,
27    including computing the types of the result, C and C++ specific error
28    checks, and some optimization.  */
29
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "tree.h"
35 #include "intl.h"
36 #include "cp-tree.h"
37 #include "flags.h"
38 #include "output.h"
39 #include "diagnostic-core.h"
40
41 static tree
42 process_init_constructor (tree type, tree init);
43
44
45 /* Print an error message stemming from an attempt to use
46    BASETYPE as a base class for TYPE.  */
47
48 tree
49 error_not_base_type (tree basetype, tree type)
50 {
51   if (TREE_CODE (basetype) == FUNCTION_DECL)
52     basetype = DECL_CONTEXT (basetype);
53   error ("type %qT is not a base type for type %qT", basetype, type);
54   return error_mark_node;
55 }
56
57 tree
58 binfo_or_else (tree base, tree type)
59 {
60   tree binfo = lookup_base (type, base, ba_unique, NULL);
61
62   if (binfo == error_mark_node)
63     return NULL_TREE;
64   else if (!binfo)
65     error_not_base_type (base, type);
66   return binfo;
67 }
68
69 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
70    value may not be changed thereafter.  */
71
72 void
73 cxx_readonly_error (tree arg, enum lvalue_use errstring)
74 {
75  
76 /* This macro is used to emit diagnostics to ensure that all format
77    strings are complete sentences, visible to gettext and checked at
78    compile time.  */
79  
80 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG)                      \
81   do {                                                                  \
82     switch (errstring)                                                  \
83       {                                                                 \
84       case lv_assign:                                                   \
85         error(AS, ARG);                                                 \
86         break;                                                          \
87       case lv_asm:                                                      \
88         error(ASM, ARG);                                                \
89         break;                                                          \
90       case lv_increment:                                                \
91         error (IN, ARG);                                                \
92         break;                                                          \
93       case lv_decrement:                                               \
94         error (DE, ARG);                                                \
95         break;                                                          \
96       default:                                                          \
97         gcc_unreachable ();                                             \
98       }                                                                 \
99   } while (0)
100
101   /* Handle C++-specific things first.  */
102
103   if (TREE_CODE (arg) == VAR_DECL
104       && DECL_LANG_SPECIFIC (arg)
105       && DECL_IN_AGGR_P (arg)
106       && !TREE_STATIC (arg))
107     ERROR_FOR_ASSIGNMENT (G_("assignment of "
108                              "constant field %qD"),
109                           G_("constant field %qD "
110                              "used as %<asm%> output"),
111                           G_("increment of "
112                              "constant field %qD"),
113                           G_("decrement of "
114                              "constant field %qD"),
115                           arg);
116   else if (TREE_CODE (arg) == INDIRECT_REF
117            && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
118            && (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL
119                || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
120     ERROR_FOR_ASSIGNMENT (G_("assignment of "
121                              "read-only reference %qD"),
122                           G_("read-only reference %qD "
123                              "used as %<asm%> output"), 
124                           G_("increment of "
125                              "read-only reference %qD"),
126                           G_("decrement of "
127                              "read-only reference %qD"),
128                           TREE_OPERAND (arg, 0));
129   else
130     readonly_error (arg, errstring);
131 }
132
133 \f
134 /* Structure that holds information about declarations whose type was
135    incomplete and we could not check whether it was abstract or not.  */
136
137 struct GTY((chain_next ("%h.next"))) pending_abstract_type {
138   /* Declaration which we are checking for abstractness. It is either
139      a DECL node, or an IDENTIFIER_NODE if we do not have a full
140      declaration available.  */
141   tree decl;
142
143   /* Type which will be checked for abstractness.  */
144   tree type;
145
146   /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
147      because DECLs already carry locus information.  */
148   location_t locus;
149
150   /* Link to the next element in list.  */
151   struct pending_abstract_type* next;
152 };
153
154
155 /* Compute the hash value of the node VAL. This function is used by the
156    hash table abstract_pending_vars.  */
157
158 static hashval_t
159 pat_calc_hash (const void* val)
160 {
161   const struct pending_abstract_type *pat =
162      (const struct pending_abstract_type *) val;
163   return (hashval_t) TYPE_UID (pat->type);
164 }
165
166
167 /* Compare node VAL1 with the type VAL2. This function is used by the
168    hash table abstract_pending_vars.  */
169
170 static int
171 pat_compare (const void* val1, const void* val2)
172 {
173   const struct pending_abstract_type *const pat1 =
174      (const struct pending_abstract_type *) val1;
175   const_tree const type2 = (const_tree)val2;
176
177   return (pat1->type == type2);
178 }
179
180 /* Hash table that maintains pending_abstract_type nodes, for which we still
181    need to check for type abstractness.  The key of the table is the type
182    of the declaration.  */
183 static GTY ((param_is (struct pending_abstract_type)))
184 htab_t abstract_pending_vars = NULL;
185
186
187 /* This function is called after TYPE is completed, and will check if there
188    are pending declarations for which we still need to verify the abstractness
189    of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
190    turned out to be incomplete.  */
191
192 void
193 complete_type_check_abstract (tree type)
194 {
195   void **slot;
196   struct pending_abstract_type *pat;
197   location_t cur_loc = input_location;
198
199   gcc_assert (COMPLETE_TYPE_P (type));
200
201   if (!abstract_pending_vars)
202     return;
203
204   /* Retrieve the list of pending declarations for this type.  */
205   slot = htab_find_slot_with_hash (abstract_pending_vars, type,
206                                    (hashval_t)TYPE_UID (type), NO_INSERT);
207   if (!slot)
208     return;
209   pat = (struct pending_abstract_type*)*slot;
210   gcc_assert (pat);
211
212   /* If the type is not abstract, do not do anything.  */
213   if (CLASSTYPE_PURE_VIRTUALS (type))
214     {
215       struct pending_abstract_type *prev = 0, *next;
216
217       /* Reverse the list to emit the errors in top-down order.  */
218       for (; pat; pat = next)
219         {
220           next = pat->next;
221           pat->next = prev;
222           prev = pat;
223         }
224       pat = prev;
225
226       /* Go through the list, and call abstract_virtuals_error for each
227         element: it will issue a diagnostic if the type is abstract.  */
228       while (pat)
229         {
230           gcc_assert (type == pat->type);
231
232           /* Tweak input_location so that the diagnostic appears at the correct
233             location. Notice that this is only needed if the decl is an
234             IDENTIFIER_NODE.  */
235           input_location = pat->locus;
236           abstract_virtuals_error (pat->decl, pat->type);
237           pat = pat->next;
238         }
239     }
240
241   htab_clear_slot (abstract_pending_vars, slot);
242
243   input_location = cur_loc;
244 }
245
246
247 /* If TYPE has abstract virtual functions, issue an error about trying
248    to create an object of that type.  DECL is the object declared, or
249    NULL_TREE if the declaration is unavailable.  Returns 1 if an error
250    occurred; zero if all was well.  */
251
252 int
253 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
254 {
255   VEC(tree,gc) *pure;
256
257   /* This function applies only to classes. Any other entity can never
258      be abstract.  */
259   if (!CLASS_TYPE_P (type))
260     return 0;
261   type = TYPE_MAIN_VARIANT (type);
262
263   /* If the type is incomplete, we register it within a hash table,
264      so that we can check again once it is completed. This makes sense
265      only for objects for which we have a declaration or at least a
266      name.  */
267   if (!COMPLETE_TYPE_P (type))
268     {
269       void **slot;
270       struct pending_abstract_type *pat;
271
272       gcc_assert (!decl || DECL_P (decl)
273                   || TREE_CODE (decl) == IDENTIFIER_NODE);
274
275       if (!abstract_pending_vars)
276         abstract_pending_vars = htab_create_ggc (31, &pat_calc_hash,
277                                                 &pat_compare, NULL);
278
279       slot = htab_find_slot_with_hash (abstract_pending_vars, type,
280                                       (hashval_t)TYPE_UID (type), INSERT);
281
282       pat = ggc_alloc_pending_abstract_type ();
283       pat->type = type;
284       pat->decl = decl;
285       pat->locus = ((decl && DECL_P (decl))
286                     ? DECL_SOURCE_LOCATION (decl)
287                     : input_location);
288
289       pat->next = (struct pending_abstract_type *) *slot;
290       *slot = pat;
291
292       return 0;
293     }
294
295   if (!TYPE_SIZE (type))
296     /* TYPE is being defined, and during that time
297        CLASSTYPE_PURE_VIRTUALS holds the inline friends.  */
298     return 0;
299
300   pure = CLASSTYPE_PURE_VIRTUALS (type);
301   if (!pure)
302     return 0;
303
304   if (!(complain & tf_error))
305     return 1;
306
307   if (decl)
308     {
309       if (TREE_CODE (decl) == VAR_DECL)
310         error ("cannot declare variable %q+D to be of abstract "
311                "type %qT", decl, type);
312       else if (TREE_CODE (decl) == PARM_DECL)
313         error ("cannot declare parameter %q+D to be of abstract type %qT",
314                decl, type);
315       else if (TREE_CODE (decl) == FIELD_DECL)
316         error ("cannot declare field %q+D to be of abstract type %qT",
317                decl, type);
318       else if (TREE_CODE (decl) == FUNCTION_DECL
319                && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
320         error ("invalid abstract return type for member function %q+#D", decl);
321       else if (TREE_CODE (decl) == FUNCTION_DECL)
322         error ("invalid abstract return type for function %q+#D", decl);
323       else if (TREE_CODE (decl) == IDENTIFIER_NODE)
324         /* Here we do not have location information.  */
325         error ("invalid abstract type %qT for %qE", type, decl);
326       else
327         error ("invalid abstract type for %q+D", decl);
328     }
329   else
330     error ("cannot allocate an object of abstract type %qT", type);
331
332   /* Only go through this once.  */
333   if (VEC_length (tree, pure))
334     {
335       unsigned ix;
336       tree fn;
337
338       inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
339               "  because the following virtual functions are pure within %qT:",
340               type);
341
342       FOR_EACH_VEC_ELT (tree, pure, ix, fn)
343         inform (input_location, "\t%+#D", fn);
344       /* Now truncate the vector.  This leaves it non-null, so we know
345          there are pure virtuals, but empty so we don't list them out
346          again.  */
347       VEC_truncate (tree, pure, 0);
348     }
349   else
350     inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
351             "  since type %qT has pure virtual functions",
352             type);
353
354   return 1;
355 }
356
357 /* Wrapper for the above function in the common case of wanting errors.  */
358
359 int
360 abstract_virtuals_error (tree decl, tree type)
361 {
362   return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
363 }
364
365 /* Print an error message for invalid use of an incomplete type.
366    VALUE is the expression that was used (or 0 if that isn't known)
367    and TYPE is the type that was invalid.  DIAG_KIND indicates the
368    type of diagnostic (see diagnostic.def).  */
369
370 void
371 cxx_incomplete_type_diagnostic (const_tree value, const_tree type, 
372                                 diagnostic_t diag_kind)
373 {
374   int decl = 0;
375
376   gcc_assert (diag_kind == DK_WARNING 
377               || diag_kind == DK_PEDWARN 
378               || diag_kind == DK_ERROR);
379
380   /* Avoid duplicate error message.  */
381   if (TREE_CODE (type) == ERROR_MARK)
382     return;
383
384   if (value != 0 && (TREE_CODE (value) == VAR_DECL
385                      || TREE_CODE (value) == PARM_DECL
386                      || TREE_CODE (value) == FIELD_DECL))
387     {
388       emit_diagnostic (diag_kind, input_location, 0,
389                        "%q+D has incomplete type", value);
390       decl = 1;
391     }
392  retry:
393   /* We must print an error message.  Be clever about what it says.  */
394
395   switch (TREE_CODE (type))
396     {
397     case RECORD_TYPE:
398     case UNION_TYPE:
399     case ENUMERAL_TYPE:
400       if (!decl)
401         emit_diagnostic (diag_kind, input_location, 0,
402                          "invalid use of incomplete type %q#T", type);
403       if (!TYPE_TEMPLATE_INFO (type))
404         emit_diagnostic (diag_kind, input_location, 0,
405                          "forward declaration of %q+#T", type);
406       else
407         emit_diagnostic (diag_kind, input_location, 0,
408                          "declaration of %q+#T", type);
409       break;
410
411     case VOID_TYPE:
412       emit_diagnostic (diag_kind, input_location, 0,
413                        "invalid use of %qT", type);
414       break;
415
416     case ARRAY_TYPE:
417       if (TYPE_DOMAIN (type))
418         {
419           type = TREE_TYPE (type);
420           goto retry;
421         }
422       emit_diagnostic (diag_kind, input_location, 0,
423                        "invalid use of array with unspecified bounds");
424       break;
425
426     case OFFSET_TYPE:
427     bad_member:
428       emit_diagnostic (diag_kind, input_location, 0,
429                        "invalid use of member (did you forget the %<&%> ?)");
430       break;
431
432     case TEMPLATE_TYPE_PARM:
433       if (is_auto (type))
434         emit_diagnostic (diag_kind, input_location, 0,
435                          "invalid use of %<auto%>");
436       else
437         emit_diagnostic (diag_kind, input_location, 0,
438                          "invalid use of template type parameter %qT", type);
439       break;
440
441     case BOUND_TEMPLATE_TEMPLATE_PARM:
442       emit_diagnostic (diag_kind, input_location, 0,
443                        "invalid use of template template parameter %qT",
444                        TYPE_NAME (type));
445       break;
446
447     case TYPENAME_TYPE:
448       emit_diagnostic (diag_kind, input_location, 0,
449                        "invalid use of dependent type %qT", type);
450       break;
451
452     case LANG_TYPE:
453       gcc_assert (type == unknown_type_node);
454       if (value && TREE_CODE (value) == COMPONENT_REF)
455         goto bad_member;
456       else if (value && TREE_CODE (value) == ADDR_EXPR)
457         emit_diagnostic (diag_kind, input_location, 0,
458                          "address of overloaded function with no contextual "
459                          "type information");
460       else if (value && TREE_CODE (value) == OVERLOAD)
461         emit_diagnostic (diag_kind, input_location, 0,
462                          "overloaded function with no contextual type information");
463       else
464         emit_diagnostic (diag_kind, input_location, 0,
465                          "insufficient contextual information to determine type");
466       break;
467
468     default:
469       gcc_unreachable ();
470     }
471 }
472
473 /* Backward-compatibility interface to incomplete_type_diagnostic;
474    required by ../tree.c.  */
475 #undef cxx_incomplete_type_error
476 void
477 cxx_incomplete_type_error (const_tree value, const_tree type)
478 {
479   cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
480 }
481
482 \f
483 /* The recursive part of split_nonconstant_init.  DEST is an lvalue
484    expression to which INIT should be assigned.  INIT is a CONSTRUCTOR.  */
485
486 static void
487 split_nonconstant_init_1 (tree dest, tree *initp)
488 {
489   unsigned HOST_WIDE_INT idx;
490   tree init = *initp;
491   tree field_index, value;
492   tree type = TREE_TYPE (dest);
493   tree inner_type = NULL;
494   bool array_type_p = false;
495   HOST_WIDE_INT num_type_elements, num_initialized_elements;
496
497   switch (TREE_CODE (type))
498     {
499     case ARRAY_TYPE:
500       inner_type = TREE_TYPE (type);
501       array_type_p = true;
502       /* FALLTHRU */
503
504     case RECORD_TYPE:
505     case UNION_TYPE:
506     case QUAL_UNION_TYPE:
507       num_initialized_elements = 0;
508       FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
509                                 field_index, value)
510         {
511           /* The current implementation of this algorithm assumes that
512              the field was set for all the elements. This is usually done
513              by process_init_constructor.  */
514           gcc_assert (field_index);
515
516           if (!array_type_p)
517             inner_type = TREE_TYPE (field_index);
518
519           if (TREE_CODE (value) == CONSTRUCTOR)
520             {
521               tree sub;
522
523               if (array_type_p)
524                 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
525                               NULL_TREE, NULL_TREE);
526               else
527                 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
528                               NULL_TREE);
529
530               split_nonconstant_init_1 (sub, &value);
531             }
532           else if (!initializer_constant_valid_p (value, inner_type))
533             {
534               tree code;
535               tree sub;
536               HOST_WIDE_INT inner_elements;
537
538               /* FIXME: Ordered removal is O(1) so the whole function is
539                  worst-case quadratic. This could be fixed using an aside
540                  bitmap to record which elements must be removed and remove
541                  them all at the same time. Or by merging
542                  split_non_constant_init into process_init_constructor_array,
543                  that is separating constants from non-constants while building
544                  the vector.  */
545               VEC_ordered_remove (constructor_elt, CONSTRUCTOR_ELTS (init),
546                                   idx);
547               --idx;
548
549               if (array_type_p)
550                 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
551                               NULL_TREE, NULL_TREE);
552               else
553                 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
554                               NULL_TREE);
555
556               code = build2 (INIT_EXPR, inner_type, sub, value);
557               code = build_stmt (input_location, EXPR_STMT, code);
558               add_stmt (code);
559
560               inner_elements = count_type_elements (inner_type, true);
561               if (inner_elements < 0)
562                 num_initialized_elements = -1;
563               else if (num_initialized_elements >= 0)
564                 num_initialized_elements += inner_elements;
565               continue;
566             }
567         }
568
569       num_type_elements = count_type_elements (type, true);
570       /* If all elements of the initializer are non-constant and
571          have been split out, we don't need the empty CONSTRUCTOR.  */
572       if (num_type_elements > 0
573           && num_type_elements == num_initialized_elements)
574         *initp = NULL;
575       break;
576
577     case VECTOR_TYPE:
578       if (!initializer_constant_valid_p (init, type))
579         {
580           tree code;
581           tree cons = copy_node (init);
582           CONSTRUCTOR_ELTS (init) = NULL;
583           code = build2 (MODIFY_EXPR, type, dest, cons);
584           code = build_stmt (input_location, EXPR_STMT, code);
585           add_stmt (code);
586         }
587       break;
588
589     default:
590       gcc_unreachable ();
591     }
592
593   /* The rest of the initializer is now a constant. */
594   TREE_CONSTANT (init) = 1;
595 }
596
597 /* A subroutine of store_init_value.  Splits non-constant static
598    initializer INIT into a constant part and generates code to
599    perform the non-constant part of the initialization to DEST.
600    Returns the code for the runtime init.  */
601
602 static tree
603 split_nonconstant_init (tree dest, tree init)
604 {
605   tree code;
606
607   if (TREE_CODE (init) == CONSTRUCTOR)
608     {
609       code = push_stmt_list ();
610       split_nonconstant_init_1 (dest, &init);
611       code = pop_stmt_list (code);
612       DECL_INITIAL (dest) = init;
613       TREE_READONLY (dest) = 0;
614     }
615   else
616     code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
617
618   return code;
619 }
620
621 /* Perform appropriate conversions on the initial value of a variable,
622    store it in the declaration DECL,
623    and print any error messages that are appropriate.
624    If the init is invalid, store an ERROR_MARK.
625
626    C++: Note that INIT might be a TREE_LIST, which would mean that it is
627    a base class initializer for some aggregate type, hopefully compatible
628    with DECL.  If INIT is a single element, and DECL is an aggregate
629    type, we silently convert INIT into a TREE_LIST, allowing a constructor
630    to be called.
631
632    If INIT is a TREE_LIST and there is no constructor, turn INIT
633    into a CONSTRUCTOR and use standard initialization techniques.
634    Perhaps a warning should be generated?
635
636    Returns code to be executed if initialization could not be performed
637    for static variable.  In that case, caller must emit the code.  */
638
639 tree
640 store_init_value (tree decl, tree init, int flags)
641 {
642   tree value, type;
643
644   /* If variable's type was invalidly declared, just ignore it.  */
645
646   type = TREE_TYPE (decl);
647   if (TREE_CODE (type) == ERROR_MARK)
648     return NULL_TREE;
649
650   if (MAYBE_CLASS_TYPE_P (type))
651     {
652       if (TREE_CODE (init) == TREE_LIST)
653         {
654           error ("constructor syntax used, but no constructor declared "
655                  "for type %qT", type);
656           init = build_constructor_from_list (init_list_type_node, nreverse (init));
657         }
658     }
659   else if (TREE_CODE (init) == TREE_LIST
660            && TREE_TYPE (init) != unknown_type_node)
661     {
662       gcc_assert (TREE_CODE (decl) != RESULT_DECL);
663
664       if (TREE_CODE (init) == TREE_LIST
665                && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
666         {
667           error ("cannot initialize arrays using this syntax");
668           return NULL_TREE;
669         }
670       else
671         /* We get here with code like `int a (2);' */
672         init = build_x_compound_expr_from_list (init, ELK_INIT,
673                                                 tf_warning_or_error);
674     }
675
676   /* End of special C++ code.  */
677
678   if (flags & LOOKUP_ALREADY_DIGESTED)
679     value = init;
680   else
681     /* Digest the specified initializer into an expression.  */
682     value = digest_init_flags (type, init, flags);
683
684   /* In C++0x constant expression is a semantic, not syntactic, property.
685      In C++98, make sure that what we thought was a constant expression at
686      template definition time is still constant.  */
687   if ((cxx_dialect >= cxx0x
688        || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
689       && (decl_maybe_constant_var_p (decl)
690           || TREE_STATIC (decl)))
691     {
692       bool const_init;
693       value = fold_non_dependent_expr (value);
694       value = maybe_constant_init (value);
695       if (DECL_DECLARED_CONSTEXPR_P (decl))
696         /* Diagnose a non-constant initializer for constexpr.  */
697         value = cxx_constant_value (value);
698       const_init = (reduced_constant_expression_p (value)
699                     || error_operand_p (value));
700       DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
701       TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
702     }
703
704   /* If the initializer is not a constant, fill in DECL_INITIAL with
705      the bits that are constant, and then return an expression that
706      will perform the dynamic initialization.  */
707   if (value != error_mark_node
708       && (TREE_SIDE_EFFECTS (value)
709            || ! initializer_constant_valid_p (value, TREE_TYPE (value))))
710     return split_nonconstant_init (decl, value);
711   /* If the value is a constant, just put it in DECL_INITIAL.  If DECL
712      is an automatic variable, the middle end will turn this into a
713      dynamic initialization later.  */
714   DECL_INITIAL (decl) = value;
715   return NULL_TREE;
716 }
717
718 \f
719 /* Give errors about narrowing conversions within { }.  */
720
721 void
722 check_narrowing (tree type, tree init)
723 {
724   tree ftype = unlowered_expr_type (init);
725   bool ok = true;
726   REAL_VALUE_TYPE d;
727
728   if (!ARITHMETIC_TYPE_P (type))
729     return;
730
731   init = maybe_constant_value (init);
732
733   if (TREE_CODE (type) == INTEGER_TYPE
734       && TREE_CODE (ftype) == REAL_TYPE)
735     ok = false;
736   else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
737            && CP_INTEGRAL_TYPE_P (type))
738     {
739       if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype)
740           && (TREE_CODE (init) != INTEGER_CST
741               || !int_fits_type_p (init, type)))
742         ok = false;
743     }
744   else if (TREE_CODE (ftype) == REAL_TYPE
745            && TREE_CODE (type) == REAL_TYPE)
746     {
747       if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
748         {
749           if (TREE_CODE (init) == REAL_CST)
750             {
751               /* Issue 703: Loss of precision is OK as long as the value is
752                  within the representable range of the new type.  */
753               REAL_VALUE_TYPE r;
754               d = TREE_REAL_CST (init);
755               real_convert (&r, TYPE_MODE (type), &d);
756               if (real_isinf (&r))
757                 ok = false;
758             }
759           else
760             ok = false;
761         }
762     }
763   else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
764            && TREE_CODE (type) == REAL_TYPE)
765     {
766       ok = false;
767       if (TREE_CODE (init) == INTEGER_CST)
768         {
769           d = real_value_from_int_cst (0, init);
770           if (exact_real_truncate (TYPE_MODE (type), &d))
771             ok = true;
772         }
773     }
774
775   if (!ok)
776     permerror (input_location, "narrowing conversion of %qE from %qT to %qT inside { }",
777                init, ftype, type);
778 }
779
780 /* Process the initializer INIT for a variable of type TYPE, emitting
781    diagnostics for invalid initializers and converting the initializer as
782    appropriate.
783
784    For aggregate types, it assumes that reshape_init has already run, thus the
785    initializer will have the right shape (brace elision has been undone).
786
787    NESTED is true iff we are being called for an element of a CONSTRUCTOR.  */
788
789 static tree
790 digest_init_r (tree type, tree init, bool nested, int flags)
791 {
792   enum tree_code code = TREE_CODE (type);
793
794   if (error_operand_p (init))
795     return error_mark_node;
796
797   gcc_assert (init);
798
799   /* We must strip the outermost array type when completing the type,
800      because the its bounds might be incomplete at the moment.  */
801   if (!complete_type_or_else (TREE_CODE (type) == ARRAY_TYPE
802                               ? TREE_TYPE (type) : type, NULL_TREE))
803     return error_mark_node;
804
805   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
806      (g++.old-deja/g++.law/casts2.C).  */
807   if (TREE_CODE (init) == NON_LVALUE_EXPR)
808     init = TREE_OPERAND (init, 0);
809
810   /* Initialization of an array of chars from a string constant. The initializer
811      can be optionally enclosed in braces, but reshape_init has already removed
812      them if they were present.  */
813   if (code == ARRAY_TYPE)
814     {
815       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
816       if (char_type_p (typ1)
817           /*&& init */
818           && TREE_CODE (init) == STRING_CST)
819         {
820           tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
821
822           if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
823             {
824               if (char_type != char_type_node)
825                 {
826                   error ("char-array initialized from wide string");
827                   return error_mark_node;
828                 }
829             }
830           else
831             {
832               if (char_type == char_type_node)
833                 {
834                   error ("int-array initialized from non-wide string");
835                   return error_mark_node;
836                 }
837               else if (char_type != typ1)
838                 {
839                   error ("int-array initialized from incompatible wide string");
840                   return error_mark_node;
841                 }
842             }
843
844           TREE_TYPE (init) = type;
845           if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
846             {
847               int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
848               size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
849               /* In C it is ok to subtract 1 from the length of the string
850                  because it's ok to ignore the terminating null char that is
851                  counted in the length of the constant, but in C++ this would
852                  be invalid.  */
853               if (size < TREE_STRING_LENGTH (init))
854                 permerror (input_location, "initializer-string for array of chars is too long");
855             }
856           return init;
857         }
858     }
859
860   /* Handle scalar types (including conversions) and references.  */
861   if ((TREE_CODE (type) != COMPLEX_TYPE
862        || BRACE_ENCLOSED_INITIALIZER_P (init))
863       && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
864     {
865       tree *exp;
866
867       if (cxx_dialect != cxx98 && nested)
868         check_narrowing (type, init);
869       init = convert_for_initialization (0, type, init, flags,
870                                          ICR_INIT, NULL_TREE, 0,
871                                          tf_warning_or_error);
872       exp = &init;
873
874       /* Skip any conversions since we'll be outputting the underlying
875          constant.  */
876       while (CONVERT_EXPR_P (*exp)
877              || TREE_CODE (*exp) == NON_LVALUE_EXPR)
878         exp = &TREE_OPERAND (*exp, 0);
879
880       *exp = cplus_expand_constant (*exp);
881
882       return init;
883     }
884
885   /* Come here only for aggregates: records, arrays, unions, complex numbers
886      and vectors.  */
887   gcc_assert (TREE_CODE (type) == ARRAY_TYPE
888               || TREE_CODE (type) == VECTOR_TYPE
889               || TREE_CODE (type) == RECORD_TYPE
890               || TREE_CODE (type) == UNION_TYPE
891               || TREE_CODE (type) == COMPLEX_TYPE);
892
893   if (BRACE_ENCLOSED_INITIALIZER_P (init)
894       && !TYPE_NON_AGGREGATE_CLASS (type))
895     return process_init_constructor (type, init);
896   else
897     {
898       if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
899         {
900           error ("cannot initialize aggregate of type %qT with "
901                  "a compound literal", type);
902
903           return error_mark_node;
904         }
905
906       if (TREE_CODE (type) == ARRAY_TYPE
907           && !BRACE_ENCLOSED_INITIALIZER_P (init))
908         {
909           /* Allow the result of build_array_copy and of
910              build_value_init_noctor.  */
911           if ((TREE_CODE (init) == TARGET_EXPR
912                || TREE_CODE (init) == CONSTRUCTOR)
913               && (same_type_ignoring_top_level_qualifiers_p
914                   (type, TREE_TYPE (init))))
915             return init;
916
917           error ("array must be initialized with a brace-enclosed"
918                  " initializer");
919           return error_mark_node;
920         }
921
922       return convert_for_initialization (NULL_TREE, type, init,
923                                          flags,
924                                          ICR_INIT, NULL_TREE, 0,
925                                          tf_warning_or_error);
926     }
927 }
928
929 tree
930 digest_init (tree type, tree init)
931 {
932   return digest_init_r (type, init, false, LOOKUP_IMPLICIT);
933 }
934
935 tree
936 digest_init_flags (tree type, tree init, int flags)
937 {
938   return digest_init_r (type, init, false, flags);
939 }
940 \f
941 /* Set of flags used within process_init_constructor to describe the
942    initializers.  */
943 #define PICFLAG_ERRONEOUS 1
944 #define PICFLAG_NOT_ALL_CONSTANT 2
945 #define PICFLAG_NOT_ALL_SIMPLE 4
946
947 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
948    describe it.  */
949
950 static int
951 picflag_from_initializer (tree init)
952 {
953   if (init == error_mark_node)
954     return PICFLAG_ERRONEOUS;
955   else if (!TREE_CONSTANT (init))
956     return PICFLAG_NOT_ALL_CONSTANT;
957   else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
958     return PICFLAG_NOT_ALL_SIMPLE;
959   return 0;
960 }
961
962 /* Subroutine of process_init_constructor, which will process an initializer
963    INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
964    which describe the initializers.  */
965
966 static int
967 process_init_constructor_array (tree type, tree init)
968 {
969   unsigned HOST_WIDE_INT i, len = 0;
970   int flags = 0;
971   bool unbounded = false;
972   constructor_elt *ce;
973   VEC(constructor_elt,gc) *v = CONSTRUCTOR_ELTS (init);
974
975   gcc_assert (TREE_CODE (type) == ARRAY_TYPE
976               || TREE_CODE (type) == VECTOR_TYPE);
977
978   if (TREE_CODE (type) == ARRAY_TYPE)
979     {
980       tree domain = TYPE_DOMAIN (type);
981       if (domain)
982         len = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (domain))
983               - TREE_INT_CST_LOW (TYPE_MIN_VALUE (domain))
984               + 1);
985       else
986         unbounded = true;  /* Take as many as there are.  */
987     }
988   else
989     /* Vectors are like simple fixed-size arrays.  */
990     len = TYPE_VECTOR_SUBPARTS (type);
991
992   /* There must not be more initializers than needed.  */
993   if (!unbounded && VEC_length (constructor_elt, v)  > len)
994     error ("too many initializers for %qT", type);
995
996   FOR_EACH_VEC_ELT (constructor_elt, v, i, ce)
997     {
998       if (ce->index)
999         {
1000           gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1001           if (compare_tree_int (ce->index, i) != 0)
1002             {
1003               ce->value = error_mark_node;
1004               sorry ("non-trivial designated initializers not supported");
1005             }
1006         }
1007       else
1008         ce->index = size_int (i);
1009       gcc_assert (ce->value);
1010       ce->value = digest_init_r (TREE_TYPE (type), ce->value, true, LOOKUP_IMPLICIT);
1011
1012       if (ce->value != error_mark_node)
1013         gcc_assert (same_type_ignoring_top_level_qualifiers_p
1014                       (TREE_TYPE (type), TREE_TYPE (ce->value)));
1015
1016       flags |= picflag_from_initializer (ce->value);
1017     }
1018
1019   /* No more initializers. If the array is unbounded, we are done. Otherwise,
1020      we must add initializers ourselves.  */
1021   if (!unbounded)
1022     for (; i < len; ++i)
1023       {
1024         tree next;
1025
1026         if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1027           {
1028             /* If this type needs constructors run for default-initialization,
1029               we can't rely on the back end to do it for us, so build up
1030               TARGET_EXPRs.  If the type in question is a class, just build
1031               one up; if it's an array, recurse.  */
1032             if (MAYBE_CLASS_TYPE_P (TREE_TYPE (type)))
1033               next = build_functional_cast (TREE_TYPE (type), NULL_TREE,
1034                                             tf_warning_or_error);
1035             else
1036               next = build_constructor (init_list_type_node, NULL);
1037             next = digest_init (TREE_TYPE (type), next);
1038           }
1039         else if (!zero_init_p (TREE_TYPE (type)))
1040           next = build_zero_init (TREE_TYPE (type),
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           break;
1047
1048         flags |= picflag_from_initializer (next);
1049         CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1050       }
1051
1052   CONSTRUCTOR_ELTS (init) = v;
1053   return flags;
1054 }
1055
1056 /* Subroutine of process_init_constructor, which will process an initializer
1057    INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1058    the initializers.  */
1059
1060 static int
1061 process_init_constructor_record (tree type, tree init)
1062 {
1063   VEC(constructor_elt,gc) *v = NULL;
1064   int flags = 0;
1065   tree field;
1066   unsigned HOST_WIDE_INT idx = 0;
1067
1068   gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1069   gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1070   gcc_assert (!TYPE_BINFO (type)
1071               || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1072   gcc_assert (!TYPE_POLYMORPHIC_P (type));
1073
1074   /* Generally, we will always have an index for each initializer (which is
1075      a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1076      reshape_init. So we need to handle both cases.  */
1077   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1078     {
1079       tree next;
1080       tree type;
1081
1082       if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1083         {
1084           flags |= picflag_from_initializer (integer_zero_node);
1085           CONSTRUCTOR_APPEND_ELT (v, field, integer_zero_node);
1086           continue;
1087         }
1088
1089       if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1090         continue;
1091
1092       /* If this is a bitfield, first convert to the declared type.  */
1093       type = TREE_TYPE (field);
1094       if (DECL_BIT_FIELD_TYPE (field))
1095         type = DECL_BIT_FIELD_TYPE (field);
1096
1097       if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)))
1098         {
1099           constructor_elt *ce = VEC_index (constructor_elt,
1100                                            CONSTRUCTOR_ELTS (init), idx);
1101           if (ce->index)
1102             {
1103               /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1104                  latter case can happen in templates where lookup has to be
1105                  deferred.  */
1106               gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1107                           || TREE_CODE (ce->index) == IDENTIFIER_NODE);
1108               if (ce->index != field
1109                   && ce->index != DECL_NAME (field))
1110                 {
1111                   ce->value = error_mark_node;
1112                   sorry ("non-trivial designated initializers not supported");
1113                 }
1114             }
1115
1116           gcc_assert (ce->value);
1117           next = digest_init_r (type, ce->value, true, LOOKUP_IMPLICIT);
1118           ++idx;
1119         }
1120       else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1121         {
1122           /* If this type needs constructors run for
1123              default-initialization, we can't rely on the back end to do it
1124              for us, so build up TARGET_EXPRs.  If the type in question is
1125              a class, just build one up; if it's an array, recurse.  */
1126           next = build_constructor (init_list_type_node, NULL);
1127           if (MAYBE_CLASS_TYPE_P (TREE_TYPE (field)))
1128             {
1129               next = finish_compound_literal (TREE_TYPE (field), next);
1130               /* direct-initialize the target. No temporary is going
1131                   to be involved.  */
1132               if (TREE_CODE (next) == TARGET_EXPR)
1133                 TARGET_EXPR_DIRECT_INIT_P (next) = true;
1134             }
1135
1136           next = digest_init_r (TREE_TYPE (field), next, true, LOOKUP_IMPLICIT);
1137
1138           /* Warn when some struct elements are implicitly initialized.  */
1139           warning (OPT_Wmissing_field_initializers,
1140                    "missing initializer for member %qD", field);
1141         }
1142       else
1143         {
1144           if (TREE_READONLY (field))
1145             error ("uninitialized const member %qD", field);
1146           else if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (TREE_TYPE (field)))
1147             error ("member %qD with uninitialized const fields", field);
1148           else if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1149             error ("member %qD is uninitialized reference", field);
1150
1151           /* Warn when some struct elements are implicitly initialized
1152              to zero.  */
1153           warning (OPT_Wmissing_field_initializers,
1154                    "missing initializer for member %qD", field);
1155
1156           if (!zero_init_p (TREE_TYPE (field)))
1157             next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1158                                     /*static_storage_p=*/false);
1159           else
1160             /* The default zero-initialization is fine for us; don't
1161             add anything to the CONSTRUCTOR.  */
1162             continue;
1163         }
1164
1165       /* If this is a bitfield, now convert to the lowered type.  */
1166       if (type != TREE_TYPE (field))
1167         next = cp_convert_and_check (TREE_TYPE (field), next);
1168       flags |= picflag_from_initializer (next);
1169       CONSTRUCTOR_APPEND_ELT (v, field, next);
1170     }
1171
1172   if (idx < VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)))
1173     error ("too many initializers for %qT", type);
1174     
1175   CONSTRUCTOR_ELTS (init) = v;
1176   return flags;
1177 }
1178
1179 /* Subroutine of process_init_constructor, which will process a single
1180    initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1181    which describe the initializer.  */
1182
1183 static int
1184 process_init_constructor_union (tree type, tree init)
1185 {
1186   constructor_elt *ce;
1187   int len;
1188
1189   /* If the initializer was empty, use default zero initialization.  */
1190   if (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init)))
1191     return 0;
1192
1193   len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
1194   if (len > 1)
1195     {
1196       error ("too many initializers for %qT", type);
1197       VEC_block_remove (constructor_elt, CONSTRUCTOR_ELTS (init), 1, len-1);
1198     }
1199
1200   ce = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (init), 0);
1201
1202   /* If this element specifies a field, initialize via that field.  */
1203   if (ce->index)
1204     {
1205       if (TREE_CODE (ce->index) == FIELD_DECL)
1206         ;
1207       else if (TREE_CODE (ce->index) == IDENTIFIER_NODE)
1208         {
1209           /* This can happen within a cast, see g++.dg/opt/cse2.C.  */
1210           tree name = ce->index;
1211           tree field;
1212           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1213             if (DECL_NAME (field) == name)
1214               break;
1215           if (!field)
1216             {
1217               error ("no field %qD found in union being initialized", field);
1218               ce->value = error_mark_node;
1219             }
1220           ce->index = field;
1221         }
1222       else
1223         {
1224           gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1225                       || TREE_CODE (ce->index) == RANGE_EXPR);
1226           error ("index value instead of field name in union initializer");
1227           ce->value = error_mark_node;
1228         }
1229     }
1230   else
1231     {
1232       /* Find the first named field.  ANSI decided in September 1990
1233          that only named fields count here.  */
1234       tree field = TYPE_FIELDS (type);
1235       while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1236         field = TREE_CHAIN (field);
1237       if (field == NULL_TREE)
1238         {
1239           error ("too many initializers for %qT", type);
1240           ce->value = error_mark_node;
1241         }
1242       ce->index = field;
1243     }
1244
1245   if (ce->value && ce->value != error_mark_node)
1246     ce->value = digest_init_r (TREE_TYPE (ce->index), ce->value, true, LOOKUP_IMPLICIT);
1247
1248   return picflag_from_initializer (ce->value);
1249 }
1250
1251 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1252    constructor is a brace-enclosed initializer, and will be modified in-place.
1253
1254    Each element is converted to the right type through digest_init, and
1255    missing initializers are added following the language rules (zero-padding,
1256    etc.).
1257
1258    After the execution, the initializer will have TREE_CONSTANT if all elts are
1259    constant, and TREE_STATIC set if, in addition, all elts are simple enough
1260    constants that the assembler and linker can compute them.
1261
1262    The function returns the initializer itself, or error_mark_node in case
1263    of error.  */
1264
1265 static tree
1266 process_init_constructor (tree type, tree init)
1267 {
1268   int flags;
1269
1270   gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1271
1272   if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1273     flags = process_init_constructor_array (type, init);
1274   else if (TREE_CODE (type) == RECORD_TYPE)
1275     flags = process_init_constructor_record (type, init);
1276   else if (TREE_CODE (type) == UNION_TYPE)
1277     flags = process_init_constructor_union (type, init);
1278   else
1279     gcc_unreachable ();
1280
1281   if (flags & PICFLAG_ERRONEOUS)
1282     return error_mark_node;
1283
1284   TREE_TYPE (init) = type;
1285   if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1286     cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1287   if (!(flags & PICFLAG_NOT_ALL_CONSTANT))
1288     {
1289       TREE_CONSTANT (init) = 1;
1290       if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1291         TREE_STATIC (init) = 1;
1292     }
1293   return init;
1294 }
1295 \f
1296 /* Given a structure or union value DATUM, construct and return
1297    the structure or union component which results from narrowing
1298    that value to the base specified in BASETYPE.  For example, given the
1299    hierarchy
1300
1301    class L { int ii; };
1302    class A : L { ... };
1303    class B : L { ... };
1304    class C : A, B { ... };
1305
1306    and the declaration
1307
1308    C x;
1309
1310    then the expression
1311
1312    x.A::ii refers to the ii member of the L part of
1313    the A part of the C object named by X.  In this case,
1314    DATUM would be x, and BASETYPE would be A.
1315
1316    I used to think that this was nonconformant, that the standard specified
1317    that first we look up ii in A, then convert x to an L& and pull out the
1318    ii part.  But in fact, it does say that we convert x to an A&; A here
1319    is known as the "naming class".  (jason 2000-12-19)
1320
1321    BINFO_P points to a variable initialized either to NULL_TREE or to the
1322    binfo for the specific base subobject we want to convert to.  */
1323
1324 tree
1325 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1326 {
1327   tree binfo;
1328
1329   if (datum == error_mark_node)
1330     return error_mark_node;
1331   if (*binfo_p)
1332     binfo = *binfo_p;
1333   else
1334     binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check, NULL);
1335
1336   if (!binfo || binfo == error_mark_node)
1337     {
1338       *binfo_p = NULL_TREE;
1339       if (!binfo)
1340         error_not_base_type (basetype, TREE_TYPE (datum));
1341       return error_mark_node;
1342     }
1343
1344   *binfo_p = binfo;
1345   return build_base_path (PLUS_EXPR, datum, binfo, 1);
1346 }
1347
1348 /* Build a reference to an object specified by the C++ `->' operator.
1349    Usually this just involves dereferencing the object, but if the
1350    `->' operator is overloaded, then such overloads must be
1351    performed until an object which does not have the `->' operator
1352    overloaded is found.  An error is reported when circular pointer
1353    delegation is detected.  */
1354
1355 tree
1356 build_x_arrow (tree expr)
1357 {
1358   tree orig_expr = expr;
1359   tree type = TREE_TYPE (expr);
1360   tree last_rval = NULL_TREE;
1361   VEC(tree,gc) *types_memoized = NULL;
1362
1363   if (type == error_mark_node)
1364     return error_mark_node;
1365
1366   if (processing_template_decl)
1367     {
1368       if (type_dependent_expression_p (expr))
1369         return build_min_nt (ARROW_EXPR, expr);
1370       expr = build_non_dependent_expr (expr);
1371     }
1372
1373   if (MAYBE_CLASS_TYPE_P (type))
1374     {
1375       while ((expr = build_new_op (COMPONENT_REF, LOOKUP_NORMAL, expr,
1376                                    NULL_TREE, NULL_TREE,
1377                                    /*overloaded_p=*/NULL, 
1378                                    tf_warning_or_error)))
1379         {
1380           if (expr == error_mark_node)
1381             return error_mark_node;
1382
1383           if (vec_member (TREE_TYPE (expr), types_memoized))
1384             {
1385               error ("circular pointer delegation detected");
1386               return error_mark_node;
1387             }
1388
1389           VEC_safe_push (tree, gc, types_memoized, TREE_TYPE (expr));
1390           last_rval = expr;
1391         }
1392
1393       if (last_rval == NULL_TREE)
1394         {
1395           error ("base operand of %<->%> has non-pointer type %qT", type);
1396           return error_mark_node;
1397         }
1398
1399       if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1400         last_rval = convert_from_reference (last_rval);
1401     }
1402   else
1403     last_rval = decay_conversion (expr);
1404
1405   if (TREE_CODE (TREE_TYPE (last_rval)) == POINTER_TYPE)
1406     {
1407       if (processing_template_decl)
1408         {
1409           expr = build_min_non_dep (ARROW_EXPR, last_rval, orig_expr);
1410           /* It will be dereferenced.  */
1411           TREE_TYPE (expr) = TREE_TYPE (TREE_TYPE (last_rval));
1412           return expr;
1413         }
1414
1415       return cp_build_indirect_ref (last_rval, RO_NULL, tf_warning_or_error);
1416     }
1417
1418   if (types_memoized)
1419     error ("result of %<operator->()%> yields non-pointer result");
1420   else
1421     error ("base operand of %<->%> is not a pointer");
1422   return error_mark_node;
1423 }
1424
1425 /* Return an expression for "DATUM .* COMPONENT".  DATUM has not
1426    already been checked out to be of aggregate type.  */
1427
1428 tree
1429 build_m_component_ref (tree datum, tree component)
1430 {
1431   tree ptrmem_type;
1432   tree objtype;
1433   tree type;
1434   tree binfo;
1435   tree ctype;
1436
1437   if (error_operand_p (datum) || error_operand_p (component))
1438     return error_mark_node;
1439
1440   datum = mark_lvalue_use (datum);
1441   component = mark_rvalue_use (component);
1442
1443   ptrmem_type = TREE_TYPE (component);
1444   if (!TYPE_PTR_TO_MEMBER_P (ptrmem_type))
1445     {
1446       error ("%qE cannot be used as a member pointer, since it is of "
1447              "type %qT",
1448              component, ptrmem_type);
1449       return error_mark_node;
1450     }
1451
1452   objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1453   if (! MAYBE_CLASS_TYPE_P (objtype))
1454     {
1455       error ("cannot apply member pointer %qE to %qE, which is of "
1456              "non-class type %qT",
1457              component, datum, objtype);
1458       return error_mark_node;
1459     }
1460
1461   type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1462   ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1463
1464   if (!COMPLETE_TYPE_P (ctype))
1465     {
1466       if (!same_type_p (ctype, objtype))
1467         goto mismatch;
1468       binfo = NULL;
1469     }
1470   else
1471     {
1472       binfo = lookup_base (objtype, ctype, ba_check, NULL);
1473
1474       if (!binfo)
1475         {
1476         mismatch:
1477           error ("pointer to member type %qT incompatible with object "
1478                  "type %qT",
1479                  type, objtype);
1480           return error_mark_node;
1481         }
1482       else if (binfo == error_mark_node)
1483         return error_mark_node;
1484     }
1485
1486   if (TYPE_PTRMEM_P (ptrmem_type))
1487     {
1488       tree ptype;
1489
1490       /* Compute the type of the field, as described in [expr.ref].
1491          There's no such thing as a mutable pointer-to-member, so
1492          things are not as complex as they are for references to
1493          non-static data members.  */
1494       type = cp_build_qualified_type (type,
1495                                       (cp_type_quals (type)
1496                                        | cp_type_quals (TREE_TYPE (datum))));
1497
1498       datum = build_address (datum);
1499
1500       /* Convert object to the correct base.  */
1501       if (binfo)
1502         datum = build_base_path (PLUS_EXPR, datum, binfo, 1);
1503
1504       /* Build an expression for "object + offset" where offset is the
1505          value stored in the pointer-to-data-member.  */
1506       ptype = build_pointer_type (type);
1507       datum = build2 (POINTER_PLUS_EXPR, ptype,
1508                       fold_convert (ptype, datum),
1509                       build_nop (sizetype, component));
1510       return cp_build_indirect_ref (datum, RO_NULL, tf_warning_or_error);
1511     }
1512   else
1513     return build2 (OFFSET_REF, type, datum, component);
1514 }
1515
1516 /* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
1517
1518 tree
1519 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1520 {
1521   /* This is either a call to a constructor,
1522      or a C cast in C++'s `functional' notation.  */
1523
1524   /* The type to which we are casting.  */
1525   tree type;
1526   VEC(tree,gc) *parmvec;
1527
1528   if (exp == error_mark_node || parms == error_mark_node)
1529     return error_mark_node;
1530
1531   if (TREE_CODE (exp) == TYPE_DECL)
1532     type = TREE_TYPE (exp);
1533   else
1534     type = exp;
1535
1536   if (processing_template_decl)
1537     {
1538       tree t;
1539
1540       /* Diagnose this even in a template.  We could also try harder
1541          to give all the usual errors when the type and args are
1542          non-dependent...  */
1543       if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1544         {
1545           if (complain & tf_error)
1546             error ("invalid value-initialization of reference type");
1547           return error_mark_node;
1548         }
1549
1550       t = build_min (CAST_EXPR, type, parms);
1551       /* We don't know if it will or will not have side effects.  */
1552       TREE_SIDE_EFFECTS (t) = 1;
1553       return t;
1554     }
1555
1556   if (! MAYBE_CLASS_TYPE_P (type))
1557     {
1558       if (parms == NULL_TREE)
1559         return build_value_init (type, complain);
1560
1561       /* This must build a C cast.  */
1562       parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1563       return cp_build_c_cast (type, parms, complain);
1564     }
1565
1566   /* Prepare to evaluate as a call to a constructor.  If this expression
1567      is actually used, for example,
1568
1569      return X (arg1, arg2, ...);
1570
1571      then the slot being initialized will be filled in.  */
1572
1573   if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1574     return error_mark_node;
1575   if (abstract_virtuals_error_sfinae (NULL_TREE, type, complain))
1576     return error_mark_node;
1577
1578   /* [expr.type.conv]
1579
1580      If the expression list is a single-expression, the type
1581      conversion is equivalent (in definedness, and if defined in
1582      meaning) to the corresponding cast expression.  */
1583   if (parms && TREE_CHAIN (parms) == NULL_TREE)
1584     return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1585
1586   /* [expr.type.conv]
1587
1588      The expression T(), where T is a simple-type-specifier for a
1589      non-array complete object type or the (possibly cv-qualified)
1590      void type, creates an rvalue of the specified type, which is
1591      value-initialized.  */
1592
1593   if (parms == NULL_TREE
1594       /* If there's a user-defined constructor, value-initialization is
1595          just calling the constructor, so fall through.  */
1596       && !TYPE_HAS_USER_CONSTRUCTOR (type))
1597     {
1598       exp = build_value_init (type, complain);
1599       exp = get_target_expr (exp);
1600       /* FIXME this is wrong */
1601       if (literal_type_p (type))
1602         TREE_CONSTANT (exp) = true;
1603       return exp;
1604     }
1605
1606   /* Call the constructor.  */
1607   parmvec = make_tree_vector ();
1608   for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
1609     VEC_safe_push (tree, gc, parmvec, TREE_VALUE (parms));
1610   exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
1611                                    &parmvec, type, LOOKUP_NORMAL, complain);
1612   release_tree_vector (parmvec);
1613
1614   if (exp == error_mark_node)
1615     return error_mark_node;
1616
1617   return build_cplus_new (type, exp, complain);
1618 }
1619 \f
1620
1621 /* Add new exception specifier SPEC, to the LIST we currently have.
1622    If it's already in LIST then do nothing.
1623    Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
1624    know what we're doing.  */
1625
1626 tree
1627 add_exception_specifier (tree list, tree spec, int complain)
1628 {
1629   bool ok;
1630   tree core = spec;
1631   bool is_ptr;
1632   diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
1633
1634   if (spec == error_mark_node)
1635     return list;
1636
1637   gcc_assert (spec && (!list || TREE_VALUE (list)));
1638
1639   /* [except.spec] 1, type in an exception specifier shall not be
1640      incomplete, or pointer or ref to incomplete other than pointer
1641      to cv void.  */
1642   is_ptr = TREE_CODE (core) == POINTER_TYPE;
1643   if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
1644     core = TREE_TYPE (core);
1645   if (complain < 0)
1646     ok = true;
1647   else if (VOID_TYPE_P (core))
1648     ok = is_ptr;
1649   else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
1650     ok = true;
1651   else if (processing_template_decl)
1652     ok = true;
1653   else
1654     {
1655       ok = true;
1656       /* 15.4/1 says that types in an exception specifier must be complete,
1657          but it seems more reasonable to only require this on definitions
1658          and calls.  So just give a pedwarn at this point; we will give an
1659          error later if we hit one of those two cases.  */
1660       if (!COMPLETE_TYPE_P (complete_type (core)))
1661         diag_type = DK_PEDWARN; /* pedwarn */
1662     }
1663
1664   if (ok)
1665     {
1666       tree probe;
1667
1668       for (probe = list; probe; probe = TREE_CHAIN (probe))
1669         if (same_type_p (TREE_VALUE (probe), spec))
1670           break;
1671       if (!probe)
1672         list = tree_cons (NULL_TREE, spec, list);
1673     }
1674   else
1675     diag_type = DK_ERROR; /* error */
1676
1677   if (diag_type != DK_UNSPECIFIED && complain)
1678     cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
1679
1680   return list;
1681 }
1682
1683 /* Combine the two exceptions specifier lists LIST and ADD, and return
1684    their union.  */
1685
1686 tree
1687 merge_exception_specifiers (tree list, tree add)
1688 {
1689   if (!list || !add)
1690     return NULL_TREE;
1691   /* For merging noexcept(true) and throw(), take the more recent one (LIST).
1692      A throw(type-list) spec takes precedence over a noexcept(false) spec.
1693      Any other noexcept-spec should only be merged with an equivalent one.
1694      So the !TREE_VALUE code below is correct for all cases.  */
1695   else if (!TREE_VALUE (add))
1696     return list;
1697   else if (!TREE_VALUE (list))
1698     return add;
1699   else
1700     {
1701       tree orig_list = list;
1702
1703       for (; add; add = TREE_CHAIN (add))
1704         {
1705           tree spec = TREE_VALUE (add);
1706           tree probe;
1707
1708           for (probe = orig_list; probe; probe = TREE_CHAIN (probe))
1709             if (same_type_p (TREE_VALUE (probe), spec))
1710               break;
1711           if (!probe)
1712             {
1713               spec = build_tree_list (NULL_TREE, spec);
1714               TREE_CHAIN (spec) = list;
1715               list = spec;
1716             }
1717         }
1718     }
1719   return list;
1720 }
1721
1722 /* Subroutine of build_call.  Ensure that each of the types in the
1723    exception specification is complete.  Technically, 15.4/1 says that
1724    they need to be complete when we see a declaration of the function,
1725    but we should be able to get away with only requiring this when the
1726    function is defined or called.  See also add_exception_specifier.  */
1727
1728 void
1729 require_complete_eh_spec_types (tree fntype, tree decl)
1730 {
1731   tree raises;
1732   /* Don't complain about calls to op new.  */
1733   if (decl && DECL_ARTIFICIAL (decl))
1734     return;
1735   for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
1736        raises = TREE_CHAIN (raises))
1737     {
1738       tree type = TREE_VALUE (raises);
1739       if (type && !COMPLETE_TYPE_P (type))
1740         {
1741           if (decl)
1742             error
1743               ("call to function %qD which throws incomplete type %q#T",
1744                decl, type);
1745           else
1746             error ("call to function which throws incomplete type %q#T",
1747                    decl);
1748         }
1749     }
1750 }
1751
1752 \f
1753 #include "gt-cp-typeck2.h"