gcc50: Disconnect from buildworld.
[dragonfly.git] / contrib / gcc-5.0 / gcc / cp / typeck2.c
1 /* Report error messages, build initializers, and perform
2    some front-end optimizations for C++ compiler.
3    Copyright (C) 1987-2015 Free Software Foundation, Inc.
4    Hacked by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22
23 /* This file is part of the C++ front end.
24    It contains routines to build C++ expressions given their operands,
25    including computing the types of the result, C and C++ specific error
26    checks, and some optimization.  */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "hash-set.h"
33 #include "machmode.h"
34 #include "vec.h"
35 #include "double-int.h"
36 #include "input.h"
37 #include "alias.h"
38 #include "symtab.h"
39 #include "wide-int.h"
40 #include "inchash.h"
41 #include "tree.h"
42 #include "stor-layout.h"
43 #include "varasm.h"
44 #include "intl.h"
45 #include "cp-tree.h"
46 #include "flags.h"
47 #include "diagnostic-core.h"
48 #include "wide-int.h"
49
50 static tree
51 process_init_constructor (tree type, tree init, tsubst_flags_t complain);
52
53
54 /* Print an error message stemming from an attempt to use
55    BASETYPE as a base class for TYPE.  */
56
57 tree
58 error_not_base_type (tree basetype, tree type)
59 {
60   if (TREE_CODE (basetype) == FUNCTION_DECL)
61     basetype = DECL_CONTEXT (basetype);
62   error ("type %qT is not a base type for type %qT", basetype, type);
63   return error_mark_node;
64 }
65
66 tree
67 binfo_or_else (tree base, tree type)
68 {
69   tree binfo = lookup_base (type, base, ba_unique,
70                             NULL, tf_warning_or_error);
71
72   if (binfo == error_mark_node)
73     return NULL_TREE;
74   else if (!binfo)
75     error_not_base_type (base, type);
76   return binfo;
77 }
78
79 /* According to ARM $7.1.6, "A `const' object may be initialized, but its
80    value may not be changed thereafter.  */
81
82 void
83 cxx_readonly_error (tree arg, enum lvalue_use errstring)
84 {
85  
86 /* This macro is used to emit diagnostics to ensure that all format
87    strings are complete sentences, visible to gettext and checked at
88    compile time.  */
89  
90 #define ERROR_FOR_ASSIGNMENT(AS, ASM, IN, DE, ARG)                      \
91   do {                                                                  \
92     switch (errstring)                                                  \
93       {                                                                 \
94       case lv_assign:                                                   \
95         error(AS, ARG);                                                 \
96         break;                                                          \
97       case lv_asm:                                                      \
98         error(ASM, ARG);                                                \
99         break;                                                          \
100       case lv_increment:                                                \
101         error (IN, ARG);                                                \
102         break;                                                          \
103       case lv_decrement:                                               \
104         error (DE, ARG);                                                \
105         break;                                                          \
106       default:                                                          \
107         gcc_unreachable ();                                             \
108       }                                                                 \
109   } while (0)
110
111   /* Handle C++-specific things first.  */
112
113   if (VAR_P (arg)
114       && DECL_LANG_SPECIFIC (arg)
115       && DECL_IN_AGGR_P (arg)
116       && !TREE_STATIC (arg))
117     ERROR_FOR_ASSIGNMENT (G_("assignment of "
118                              "constant field %qD"),
119                           G_("constant field %qD "
120                              "used as %<asm%> output"),
121                           G_("increment of "
122                              "constant field %qD"),
123                           G_("decrement of "
124                              "constant field %qD"),
125                           arg);
126   else if (INDIRECT_REF_P (arg)
127            && TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REFERENCE_TYPE
128            && (VAR_P (TREE_OPERAND (arg, 0))
129                || TREE_CODE (TREE_OPERAND (arg, 0)) == PARM_DECL))
130     ERROR_FOR_ASSIGNMENT (G_("assignment of "
131                              "read-only reference %qD"),
132                           G_("read-only reference %qD "
133                              "used as %<asm%> output"), 
134                           G_("increment of "
135                              "read-only reference %qD"),
136                           G_("decrement of "
137                              "read-only reference %qD"),
138                           TREE_OPERAND (arg, 0));
139   else
140     readonly_error (input_location, arg, errstring);
141 }
142
143 \f
144 /* Structure that holds information about declarations whose type was
145    incomplete and we could not check whether it was abstract or not.  */
146
147 struct GTY((chain_next ("%h.next"), for_user)) pending_abstract_type {
148   /* Declaration which we are checking for abstractness. It is either
149      a DECL node, or an IDENTIFIER_NODE if we do not have a full
150      declaration available.  */
151   tree decl;
152
153   /* Type which will be checked for abstractness.  */
154   tree type;
155
156   /* Kind of use in an unnamed declarator.  */
157   abstract_class_use use;
158
159   /* Position of the declaration. This is only needed for IDENTIFIER_NODEs,
160      because DECLs already carry locus information.  */
161   location_t locus;
162
163   /* Link to the next element in list.  */
164   struct pending_abstract_type* next;
165 };
166
167 struct abstract_type_hasher : ggc_hasher<pending_abstract_type *>
168 {
169   typedef tree compare_type;
170   static hashval_t hash (pending_abstract_type *);
171   static bool equal (pending_abstract_type *, tree);
172 };
173
174 /* Compute the hash value of the node VAL. This function is used by the
175    hash table abstract_pending_vars.  */
176
177 hashval_t
178 abstract_type_hasher::hash (pending_abstract_type *pat)
179 {
180   return (hashval_t) TYPE_UID (pat->type);
181 }
182
183
184 /* Compare node VAL1 with the type VAL2. This function is used by the
185    hash table abstract_pending_vars.  */
186
187 bool
188 abstract_type_hasher::equal (pending_abstract_type *pat1, tree type2)
189 {
190   return (pat1->type == type2);
191 }
192
193 /* Hash table that maintains pending_abstract_type nodes, for which we still
194    need to check for type abstractness.  The key of the table is the type
195    of the declaration.  */
196 static GTY (()) hash_table<abstract_type_hasher> *abstract_pending_vars = NULL;
197
198 static int abstract_virtuals_error_sfinae (tree, tree, abstract_class_use, tsubst_flags_t);
199
200 /* This function is called after TYPE is completed, and will check if there
201    are pending declarations for which we still need to verify the abstractness
202    of TYPE, and emit a diagnostic (through abstract_virtuals_error) if TYPE
203    turned out to be incomplete.  */
204
205 void
206 complete_type_check_abstract (tree type)
207 {
208   struct pending_abstract_type *pat;
209   location_t cur_loc = input_location;
210
211   gcc_assert (COMPLETE_TYPE_P (type));
212
213   if (!abstract_pending_vars)
214     return;
215
216   /* Retrieve the list of pending declarations for this type.  */
217   pending_abstract_type **slot
218     = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
219                                                   NO_INSERT);
220   if (!slot)
221     return;
222   pat = *slot;
223   gcc_assert (pat);
224
225   /* If the type is not abstract, do not do anything.  */
226   if (CLASSTYPE_PURE_VIRTUALS (type))
227     {
228       struct pending_abstract_type *prev = 0, *next;
229
230       /* Reverse the list to emit the errors in top-down order.  */
231       for (; pat; pat = next)
232         {
233           next = pat->next;
234           pat->next = prev;
235           prev = pat;
236         }
237       pat = prev;
238
239       /* Go through the list, and call abstract_virtuals_error for each
240         element: it will issue a diagnostic if the type is abstract.  */
241       while (pat)
242         {
243           gcc_assert (type == pat->type);
244
245           /* Tweak input_location so that the diagnostic appears at the correct
246             location. Notice that this is only needed if the decl is an
247             IDENTIFIER_NODE.  */
248           input_location = pat->locus;
249           abstract_virtuals_error_sfinae (pat->decl, pat->type, pat->use,
250                                           tf_warning_or_error);
251           pat = pat->next;
252         }
253     }
254
255   abstract_pending_vars->clear_slot (slot);
256
257   input_location = cur_loc;
258 }
259
260
261 /* If TYPE has abstract virtual functions, issue an error about trying
262    to create an object of that type.  DECL is the object declared, or
263    NULL_TREE if the declaration is unavailable, in which case USE specifies
264    the kind of invalid use.  Returns 1 if an error occurred; zero if
265    all was well.  */
266
267 static int
268 abstract_virtuals_error_sfinae (tree decl, tree type, abstract_class_use use,
269                                 tsubst_flags_t complain)
270 {
271   vec<tree, va_gc> *pure;
272
273   /* This function applies only to classes. Any other entity can never
274      be abstract.  */
275   if (!CLASS_TYPE_P (type))
276     return 0;
277   type = TYPE_MAIN_VARIANT (type);
278
279 #if 0
280   /* Instantiation here seems to be required by the standard,
281      but breaks e.g. boost::bind.  FIXME!  */
282   /* In SFINAE, non-N3276 context, force instantiation.  */
283   if (!(complain & (tf_error|tf_decltype)))
284     complete_type (type);
285 #endif
286
287   /* If the type is incomplete, we register it within a hash table,
288      so that we can check again once it is completed. This makes sense
289      only for objects for which we have a declaration or at least a
290      name.  */
291   if (!COMPLETE_TYPE_P (type) && (complain & tf_error))
292     {
293       struct pending_abstract_type *pat;
294
295       gcc_assert (!decl || DECL_P (decl) || identifier_p (decl));
296
297       if (!abstract_pending_vars)
298         abstract_pending_vars
299           = hash_table<abstract_type_hasher>::create_ggc (31);
300
301       pending_abstract_type **slot
302         = abstract_pending_vars->find_slot_with_hash (type, TYPE_UID (type),
303                                                       INSERT);
304
305       pat = ggc_alloc<pending_abstract_type> ();
306       pat->type = type;
307       pat->decl = decl;
308       pat->use = use;
309       pat->locus = ((decl && DECL_P (decl))
310                     ? DECL_SOURCE_LOCATION (decl)
311                     : input_location);
312
313       pat->next = *slot;
314       *slot = pat;
315
316       return 0;
317     }
318
319   if (!TYPE_SIZE (type))
320     /* TYPE is being defined, and during that time
321        CLASSTYPE_PURE_VIRTUALS holds the inline friends.  */
322     return 0;
323
324   pure = CLASSTYPE_PURE_VIRTUALS (type);
325   if (!pure)
326     return 0;
327
328   if (!(complain & tf_error))
329     return 1;
330
331   if (decl)
332     {
333       if (VAR_P (decl))
334         error ("cannot declare variable %q+D to be of abstract "
335                "type %qT", decl, type);
336       else if (TREE_CODE (decl) == PARM_DECL)
337         {
338           if (DECL_NAME (decl))
339             error ("cannot declare parameter %q+D to be of abstract type %qT",
340                    decl, type);
341           else
342             error ("cannot declare parameter to be of abstract type %qT",
343                    type);
344         }
345       else if (TREE_CODE (decl) == FIELD_DECL)
346         error ("cannot declare field %q+D to be of abstract type %qT",
347                decl, type);
348       else if (TREE_CODE (decl) == FUNCTION_DECL
349                && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
350         error ("invalid abstract return type for member function %q+#D", decl);
351       else if (TREE_CODE (decl) == FUNCTION_DECL)
352         error ("invalid abstract return type for function %q+#D", decl);
353       else if (identifier_p (decl))
354         /* Here we do not have location information.  */
355         error ("invalid abstract type %qT for %qE", type, decl);
356       else
357         error ("invalid abstract type for %q+D", decl);
358     }
359   else switch (use)
360     {
361     case ACU_ARRAY:
362       error ("creating array of %qT, which is an abstract class type", type);
363       break;
364     case ACU_CAST:
365       error ("invalid cast to abstract class type %qT", type);
366       break;
367     case ACU_NEW:
368       error ("invalid new-expression of abstract class type %qT", type);
369       break;
370     case ACU_RETURN:
371       error ("invalid abstract return type %qT", type);
372       break;
373     case ACU_PARM:
374       error ("invalid abstract parameter type %qT", type);
375       break;
376     case ACU_THROW:
377       error ("expression of abstract class type %qT cannot "
378              "be used in throw-expression", type);
379       break;
380     case ACU_CATCH:
381       error ("cannot declare catch parameter to be of abstract "
382              "class type %qT", type);
383       break;
384     default:
385       error ("cannot allocate an object of abstract type %qT", type);
386     }
387
388   /* Only go through this once.  */
389   if (pure->length ())
390     {
391       unsigned ix;
392       tree fn;
393
394       inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
395               "  because the following virtual functions are pure within %qT:",
396               type);
397
398       FOR_EACH_VEC_ELT (*pure, ix, fn)
399         if (! DECL_CLONED_FUNCTION_P (fn)
400             || DECL_COMPLETE_DESTRUCTOR_P (fn))
401           inform (input_location, "\t%+#D", fn);
402
403       /* Now truncate the vector.  This leaves it non-null, so we know
404          there are pure virtuals, but empty so we don't list them out
405          again.  */
406       pure->truncate (0);
407     }
408
409   return 1;
410 }
411
412 int
413 abstract_virtuals_error_sfinae (tree decl, tree type, tsubst_flags_t complain)
414 {
415   return abstract_virtuals_error_sfinae (decl, type, ACU_UNKNOWN, complain);
416 }
417
418 int
419 abstract_virtuals_error_sfinae (abstract_class_use use, tree type,
420                                 tsubst_flags_t complain)
421 {
422   return abstract_virtuals_error_sfinae (NULL_TREE, type, use, complain);
423 }
424
425
426 /* Wrapper for the above function in the common case of wanting errors.  */
427
428 int
429 abstract_virtuals_error (tree decl, tree type)
430 {
431   return abstract_virtuals_error_sfinae (decl, type, tf_warning_or_error);
432 }
433
434 int
435 abstract_virtuals_error (abstract_class_use use, tree type)
436 {
437   return abstract_virtuals_error_sfinae (use, type, tf_warning_or_error);
438 }
439
440 /* Print an inform about the declaration of the incomplete type TYPE.  */
441
442 void
443 cxx_incomplete_type_inform (const_tree type)
444 {
445   if (!TYPE_MAIN_DECL (type))
446     return;
447
448   location_t loc = DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type));
449   tree ptype = strip_top_quals (CONST_CAST_TREE (type));
450
451   if (current_class_type
452       && TYPE_BEING_DEFINED (current_class_type)
453       && same_type_p (ptype, current_class_type))
454     inform (loc, "definition of %q#T is not complete until "
455             "the closing brace", ptype);
456   else if (!TYPE_TEMPLATE_INFO (ptype))
457     inform (loc, "forward declaration of %q#T", ptype);
458   else
459     inform (loc, "declaration of %q#T", ptype);
460 }
461
462 /* Print an error message for invalid use of an incomplete type.
463    VALUE is the expression that was used (or 0 if that isn't known)
464    and TYPE is the type that was invalid.  DIAG_KIND indicates the
465    type of diagnostic (see diagnostic.def).  */
466
467 void
468 cxx_incomplete_type_diagnostic (const_tree value, const_tree type, 
469                                 diagnostic_t diag_kind)
470 {
471   bool is_decl = false, complained = false;
472
473   gcc_assert (diag_kind == DK_WARNING 
474               || diag_kind == DK_PEDWARN 
475               || diag_kind == DK_ERROR);
476
477   /* Avoid duplicate error message.  */
478   if (TREE_CODE (type) == ERROR_MARK)
479     return;
480
481   if (value != 0 && (VAR_P (value)
482                      || TREE_CODE (value) == PARM_DECL
483                      || TREE_CODE (value) == FIELD_DECL))
484     {
485       complained = emit_diagnostic (diag_kind, input_location, 0,
486                                     "%q+D has incomplete type", value);
487       is_decl = true;
488     } 
489  retry:
490   /* We must print an error message.  Be clever about what it says.  */
491
492   switch (TREE_CODE (type))
493     {
494     case RECORD_TYPE:
495     case UNION_TYPE:
496     case ENUMERAL_TYPE:
497       if (!is_decl)
498         complained = emit_diagnostic (diag_kind, input_location, 0,
499                                       "invalid use of incomplete type %q#T",
500                                       type);
501       if (complained)
502         cxx_incomplete_type_inform (type);
503       break;
504
505     case VOID_TYPE:
506       emit_diagnostic (diag_kind, input_location, 0,
507                        "invalid use of %qT", type);
508       break;
509
510     case ARRAY_TYPE:
511       if (TYPE_DOMAIN (type))
512         {
513           type = TREE_TYPE (type);
514           goto retry;
515         }
516       emit_diagnostic (diag_kind, input_location, 0,
517                        "invalid use of array with unspecified bounds");
518       break;
519
520     case OFFSET_TYPE:
521     bad_member:
522       {
523         tree member = TREE_OPERAND (value, 1);
524         if (is_overloaded_fn (member))
525           member = get_first_fn (member);
526         if (DECL_FUNCTION_MEMBER_P (member)
527             && ! flag_ms_extensions)
528           emit_diagnostic (diag_kind, input_location, 0,
529                            "invalid use of member function "
530                            "(did you forget the %<()%> ?)");
531         else
532           emit_diagnostic (diag_kind, input_location, 0,
533                            "invalid use of member "
534                            "(did you forget the %<&%> ?)");
535       }
536       break;
537
538     case TEMPLATE_TYPE_PARM:
539       if (is_auto (type))
540         emit_diagnostic (diag_kind, input_location, 0,
541                          "invalid use of %<auto%>");
542       else
543         emit_diagnostic (diag_kind, input_location, 0,
544                          "invalid use of template type parameter %qT", type);
545       break;
546
547     case BOUND_TEMPLATE_TEMPLATE_PARM:
548       emit_diagnostic (diag_kind, input_location, 0,
549                        "invalid use of template template parameter %qT",
550                        TYPE_NAME (type));
551       break;
552
553     case TYPENAME_TYPE:
554       emit_diagnostic (diag_kind, input_location, 0,
555                        "invalid use of dependent type %qT", type);
556       break;
557
558     case LANG_TYPE:
559       if (type == init_list_type_node)
560         {
561           emit_diagnostic (diag_kind, input_location, 0,
562                            "invalid use of brace-enclosed initializer list");
563           break;
564         }
565       gcc_assert (type == unknown_type_node);
566       if (value && TREE_CODE (value) == COMPONENT_REF)
567         goto bad_member;
568       else if (value && TREE_CODE (value) == ADDR_EXPR)
569         emit_diagnostic (diag_kind, input_location, 0,
570                          "address of overloaded function with no contextual "
571                          "type information");
572       else if (value && TREE_CODE (value) == OVERLOAD)
573         emit_diagnostic (diag_kind, input_location, 0,
574                          "overloaded function with no contextual type information");
575       else
576         emit_diagnostic (diag_kind, input_location, 0,
577                          "insufficient contextual information to determine type");
578       break;
579
580     default:
581       gcc_unreachable ();
582     }
583 }
584
585 /* Backward-compatibility interface to incomplete_type_diagnostic;
586    required by ../tree.c.  */
587 #undef cxx_incomplete_type_error
588 void
589 cxx_incomplete_type_error (const_tree value, const_tree type)
590 {
591   cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
592 }
593
594 \f
595 /* The recursive part of split_nonconstant_init.  DEST is an lvalue
596    expression to which INIT should be assigned.  INIT is a CONSTRUCTOR.
597    Return true if the whole of the value was initialized by the
598    generated statements.  */
599
600 static bool
601 split_nonconstant_init_1 (tree dest, tree init)
602 {
603   unsigned HOST_WIDE_INT idx;
604   tree field_index, value;
605   tree type = TREE_TYPE (dest);
606   tree inner_type = NULL;
607   bool array_type_p = false;
608   bool complete_p = true;
609   HOST_WIDE_INT num_split_elts = 0;
610
611   switch (TREE_CODE (type))
612     {
613     case ARRAY_TYPE:
614       inner_type = TREE_TYPE (type);
615       array_type_p = true;
616       if ((TREE_SIDE_EFFECTS (init)
617            && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
618           || array_of_runtime_bound_p (type))
619         {
620           /* For an array, we only need/want a single cleanup region rather
621              than one per element.  */
622           tree code = build_vec_init (dest, NULL_TREE, init, false, 1,
623                                       tf_warning_or_error);
624           add_stmt (code);
625           return true;
626         }
627       /* FALLTHRU */
628
629     case RECORD_TYPE:
630     case UNION_TYPE:
631     case QUAL_UNION_TYPE:
632       FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), idx,
633                                 field_index, value)
634         {
635           /* The current implementation of this algorithm assumes that
636              the field was set for all the elements. This is usually done
637              by process_init_constructor.  */
638           gcc_assert (field_index);
639
640           if (!array_type_p)
641             inner_type = TREE_TYPE (field_index);
642
643           if (TREE_CODE (value) == CONSTRUCTOR)
644             {
645               tree sub;
646
647               if (array_type_p)
648                 sub = build4 (ARRAY_REF, inner_type, dest, field_index,
649                               NULL_TREE, NULL_TREE);
650               else
651                 sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
652                               NULL_TREE);
653
654               if (!split_nonconstant_init_1 (sub, value))
655                 complete_p = false;
656               else
657                 CONSTRUCTOR_ELTS (init)->ordered_remove (idx--);
658               num_split_elts++;
659             }
660           else if (!initializer_constant_valid_p (value, inner_type))
661             {
662               tree code;
663               tree sub;
664
665               /* FIXME: Ordered removal is O(1) so the whole function is
666                  worst-case quadratic. This could be fixed using an aside
667                  bitmap to record which elements must be removed and remove
668                  them all at the same time. Or by merging
669                  split_non_constant_init into process_init_constructor_array,
670                  that is separating constants from non-constants while building
671                  the vector.  */
672               CONSTRUCTOR_ELTS (init)->ordered_remove (idx);
673               --idx;
674
675               if (TREE_CODE (field_index) == RANGE_EXPR)
676                 {
677                   /* Use build_vec_init to initialize a range.  */
678                   tree low = TREE_OPERAND (field_index, 0);
679                   tree hi = TREE_OPERAND (field_index, 1);
680                   sub = build4 (ARRAY_REF, inner_type, dest, low,
681                                 NULL_TREE, NULL_TREE);
682                   sub = cp_build_addr_expr (sub, tf_warning_or_error);
683                   tree max = size_binop (MINUS_EXPR, hi, low);
684                   code = build_vec_init (sub, max, value, false, 0,
685                                          tf_warning_or_error);
686                   add_stmt (code);
687                   if (tree_fits_shwi_p (max))
688                     num_split_elts += tree_to_shwi (max);
689                 }
690               else
691                 {
692                   if (array_type_p)
693                     sub = build4 (ARRAY_REF, inner_type, dest, field_index,
694                                   NULL_TREE, NULL_TREE);
695                   else
696                     sub = build3 (COMPONENT_REF, inner_type, dest, field_index,
697                                   NULL_TREE);
698
699                   code = build2 (INIT_EXPR, inner_type, sub, value);
700                   code = build_stmt (input_location, EXPR_STMT, code);
701                   code = maybe_cleanup_point_expr_void (code);
702                   add_stmt (code);
703                   if (tree cleanup
704                       = cxx_maybe_build_cleanup (sub, tf_warning_or_error))
705                     finish_eh_cleanup (cleanup);
706                 }
707
708               num_split_elts++;
709             }
710         }
711       break;
712
713     case VECTOR_TYPE:
714       if (!initializer_constant_valid_p (init, type))
715         {
716           tree code;
717           tree cons = copy_node (init);
718           CONSTRUCTOR_ELTS (init) = NULL;
719           code = build2 (MODIFY_EXPR, type, dest, cons);
720           code = build_stmt (input_location, EXPR_STMT, code);
721           add_stmt (code);
722           num_split_elts += CONSTRUCTOR_NELTS (init);
723         }
724       break;
725
726     default:
727       gcc_unreachable ();
728     }
729
730   /* The rest of the initializer is now a constant. */
731   TREE_CONSTANT (init) = 1;
732   return complete_p && complete_ctor_at_level_p (TREE_TYPE (init),
733                                                  num_split_elts, inner_type);
734 }
735
736 /* A subroutine of store_init_value.  Splits non-constant static
737    initializer INIT into a constant part and generates code to
738    perform the non-constant part of the initialization to DEST.
739    Returns the code for the runtime init.  */
740
741 tree
742 split_nonconstant_init (tree dest, tree init)
743 {
744   tree code;
745
746   if (TREE_CODE (init) == TARGET_EXPR)
747     init = TARGET_EXPR_INITIAL (init);
748   if (TREE_CODE (init) == CONSTRUCTOR)
749     {
750       code = push_stmt_list ();
751       if (split_nonconstant_init_1 (dest, init))
752         init = NULL_TREE;
753       code = pop_stmt_list (code);
754       DECL_INITIAL (dest) = init;
755       TREE_READONLY (dest) = 0;
756     }
757   else
758     code = build2 (INIT_EXPR, TREE_TYPE (dest), dest, init);
759
760   return code;
761 }
762
763 /* Perform appropriate conversions on the initial value of a variable,
764    store it in the declaration DECL,
765    and print any error messages that are appropriate.
766    If the init is invalid, store an ERROR_MARK.
767
768    C++: Note that INIT might be a TREE_LIST, which would mean that it is
769    a base class initializer for some aggregate type, hopefully compatible
770    with DECL.  If INIT is a single element, and DECL is an aggregate
771    type, we silently convert INIT into a TREE_LIST, allowing a constructor
772    to be called.
773
774    If INIT is a TREE_LIST and there is no constructor, turn INIT
775    into a CONSTRUCTOR and use standard initialization techniques.
776    Perhaps a warning should be generated?
777
778    Returns code to be executed if initialization could not be performed
779    for static variable.  In that case, caller must emit the code.  */
780
781 tree
782 store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags)
783 {
784   tree value, type;
785
786   /* If variable's type was invalidly declared, just ignore it.  */
787
788   type = TREE_TYPE (decl);
789   if (TREE_CODE (type) == ERROR_MARK)
790     return NULL_TREE;
791
792   if (MAYBE_CLASS_TYPE_P (type))
793     {
794       if (TREE_CODE (init) == TREE_LIST)
795         {
796           error ("constructor syntax used, but no constructor declared "
797                  "for type %qT", type);
798           init = build_constructor_from_list (init_list_type_node, nreverse (init));
799         }
800     }
801
802   /* End of special C++ code.  */
803
804   if (flags & LOOKUP_ALREADY_DIGESTED)
805     value = init;
806   else
807     /* Digest the specified initializer into an expression.  */
808     value = digest_init_flags (type, init, flags);
809
810   value = extend_ref_init_temps (decl, value, cleanups);
811
812   /* In C++11 constant expression is a semantic, not syntactic, property.
813      In C++98, make sure that what we thought was a constant expression at
814      template definition time is still constant and otherwise perform this
815      as optimization, e.g. to fold SIZEOF_EXPRs in the initializer.  */
816   if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
817     {
818       bool const_init;
819       value = instantiate_non_dependent_expr (value);
820       if (DECL_DECLARED_CONSTEXPR_P (decl)
821           || DECL_IN_AGGR_P (decl))
822         {
823           /* Diagnose a non-constant initializer for constexpr.  */
824           if (processing_template_decl
825               && !require_potential_constant_expression (value))
826             value = error_mark_node;
827           else
828             value = cxx_constant_value (value, decl);
829         }
830       value = maybe_constant_init (value, decl);
831       if (TREE_CODE (value) == CONSTRUCTOR && cp_has_mutable_p (type))
832         /* Poison this CONSTRUCTOR so it can't be copied to another
833            constexpr variable.  */
834         CONSTRUCTOR_MUTABLE_POISON (value) = true;
835       const_init = (reduced_constant_expression_p (value)
836                     || error_operand_p (value));
837       DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = const_init;
838       TREE_CONSTANT (decl) = const_init && decl_maybe_constant_var_p (decl);
839     }
840
841   if (cxx_dialect >= cxx14 && CLASS_TYPE_P (strip_array_types (type)))
842     /* Handle aggregate NSDMI in non-constant initializers, too.  */
843     value = replace_placeholders (value, decl);
844
845   /* If the initializer is not a constant, fill in DECL_INITIAL with
846      the bits that are constant, and then return an expression that
847      will perform the dynamic initialization.  */
848   if (value != error_mark_node
849       && (TREE_SIDE_EFFECTS (value)
850           || array_of_runtime_bound_p (type)
851           || ! reduced_constant_expression_p (value)))
852     return split_nonconstant_init (decl, value);
853   /* If the value is a constant, just put it in DECL_INITIAL.  If DECL
854      is an automatic variable, the middle end will turn this into a
855      dynamic initialization later.  */
856   DECL_INITIAL (decl) = value;
857   return NULL_TREE;
858 }
859
860 \f
861 /* Give diagnostic about narrowing conversions within { }.  */
862
863 bool
864 check_narrowing (tree type, tree init, tsubst_flags_t complain)
865 {
866   tree ftype = unlowered_expr_type (init);
867   bool ok = true;
868   REAL_VALUE_TYPE d;
869
870   if (((!warn_narrowing || !(complain & tf_warning))
871        && cxx_dialect == cxx98)
872       || !ARITHMETIC_TYPE_P (type))
873     return ok;
874
875   if (BRACE_ENCLOSED_INITIALIZER_P (init)
876       && TREE_CODE (type) == COMPLEX_TYPE)
877     {
878       tree elttype = TREE_TYPE (type);
879       if (CONSTRUCTOR_NELTS (init) > 0)
880         ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 0)->value,
881                                complain);
882       if (CONSTRUCTOR_NELTS (init) > 1)
883         ok &= check_narrowing (elttype, CONSTRUCTOR_ELT (init, 1)->value,
884                                complain);
885       return ok;
886     }
887
888   init = fold_non_dependent_expr (init);
889
890   if (TREE_CODE (type) == INTEGER_TYPE
891       && TREE_CODE (ftype) == REAL_TYPE)
892     ok = false;
893   else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
894            && CP_INTEGRAL_TYPE_P (type))
895     {
896       if (TREE_CODE (ftype) == ENUMERAL_TYPE)
897         /* Check for narrowing based on the values of the enumeration. */
898         ftype = ENUM_UNDERLYING_TYPE (ftype);
899       if ((tree_int_cst_lt (TYPE_MAX_VALUE (type),
900                             TYPE_MAX_VALUE (ftype))
901            || tree_int_cst_lt (TYPE_MIN_VALUE (ftype),
902                                TYPE_MIN_VALUE (type)))
903           && (TREE_CODE (init) != INTEGER_CST
904               || !int_fits_type_p (init, type)))
905         ok = false;
906     }
907   else if (TREE_CODE (ftype) == REAL_TYPE
908            && TREE_CODE (type) == REAL_TYPE)
909     {
910       if (TYPE_PRECISION (type) < TYPE_PRECISION (ftype))
911         {
912           if (TREE_CODE (init) == REAL_CST)
913             {
914               /* Issue 703: Loss of precision is OK as long as the value is
915                  within the representable range of the new type.  */
916               REAL_VALUE_TYPE r;
917               d = TREE_REAL_CST (init);
918               real_convert (&r, TYPE_MODE (type), &d);
919               if (real_isinf (&r))
920                 ok = false;
921             }
922           else
923             ok = false;
924         }
925     }
926   else if (INTEGRAL_OR_ENUMERATION_TYPE_P (ftype)
927            && TREE_CODE (type) == REAL_TYPE)
928     {
929       ok = false;
930       if (TREE_CODE (init) == INTEGER_CST)
931         {
932           d = real_value_from_int_cst (0, init);
933           if (exact_real_truncate (TYPE_MODE (type), &d))
934             ok = true;
935         }
936     }
937
938   if (!ok)
939     {
940       if (cxx_dialect == cxx98)
941         warning_at (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
942                     "narrowing conversion of %qE from %qT to %qT inside { } "
943                     "is ill-formed in C++11", init, ftype, type);
944       else if (!TREE_CONSTANT (init))
945         {
946           if (complain & tf_warning_or_error)
947             {
948               pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
949                        "narrowing conversion of %qE from %qT to %qT inside { }",
950                        init, ftype, type);
951               ok = true;
952             }
953         }
954       else if (complain & tf_error)
955         {
956           int savederrorcount = errorcount;
957           global_dc->pedantic_errors = 1;
958           pedwarn (EXPR_LOC_OR_LOC (init, input_location), OPT_Wnarrowing,
959                    "narrowing conversion of %qE from %qT to %qT "
960                    "inside { }", init, ftype, type);
961           if (errorcount == savederrorcount)
962             ok = true;
963           global_dc->pedantic_errors = flag_pedantic_errors;
964         }
965     }
966
967   return cxx_dialect == cxx98 || ok; 
968 }
969
970 /* Process the initializer INIT for a variable of type TYPE, emitting
971    diagnostics for invalid initializers and converting the initializer as
972    appropriate.
973
974    For aggregate types, it assumes that reshape_init has already run, thus the
975    initializer will have the right shape (brace elision has been undone).
976
977    NESTED is true iff we are being called for an element of a CONSTRUCTOR.  */
978
979 static tree
980 digest_init_r (tree type, tree init, bool nested, int flags,
981                tsubst_flags_t complain)
982 {
983   enum tree_code code = TREE_CODE (type);
984
985   if (error_operand_p (init))
986     return error_mark_node;
987
988   gcc_assert (init);
989
990   /* We must strip the outermost array type when completing the type,
991      because the its bounds might be incomplete at the moment.  */
992   if (!complete_type_or_maybe_complain (TREE_CODE (type) == ARRAY_TYPE
993                                         ? TREE_TYPE (type) : type, NULL_TREE,
994                                         complain))
995     return error_mark_node;
996
997   /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue
998      (g++.old-deja/g++.law/casts2.C).  */
999   if (TREE_CODE (init) == NON_LVALUE_EXPR)
1000     init = TREE_OPERAND (init, 0);
1001
1002   /* Initialization of an array of chars from a string constant. The initializer
1003      can be optionally enclosed in braces, but reshape_init has already removed
1004      them if they were present.  */
1005   if (code == ARRAY_TYPE)
1006     {
1007       tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
1008       if (char_type_p (typ1)
1009           /*&& init */
1010           && TREE_CODE (init) == STRING_CST)
1011         {
1012           tree char_type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (init)));
1013
1014           if (TYPE_PRECISION (typ1) == BITS_PER_UNIT)
1015             {
1016               if (char_type != char_type_node)
1017                 {
1018                   if (complain & tf_error)
1019                     error ("char-array initialized from wide string");
1020                   return error_mark_node;
1021                 }
1022             }
1023           else
1024             {
1025               if (char_type == char_type_node)
1026                 {
1027                   if (complain & tf_error)
1028                     error ("int-array initialized from non-wide string");
1029                   return error_mark_node;
1030                 }
1031               else if (char_type != typ1)
1032                 {
1033                   if (complain & tf_error)
1034                     error ("int-array initialized from incompatible "
1035                            "wide string");
1036                   return error_mark_node;
1037                 }
1038             }
1039
1040           if (type != TREE_TYPE (init))
1041             {
1042               init = copy_node (init);
1043               TREE_TYPE (init) = type;
1044             }
1045           if (TYPE_DOMAIN (type) != 0 && TREE_CONSTANT (TYPE_SIZE (type)))
1046             {
1047               int size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1048               size = (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1049               /* In C it is ok to subtract 1 from the length of the string
1050                  because it's ok to ignore the terminating null char that is
1051                  counted in the length of the constant, but in C++ this would
1052                  be invalid.  */
1053               if (size < TREE_STRING_LENGTH (init))
1054                 permerror (input_location, "initializer-string for array "
1055                            "of chars is too long");
1056             }
1057           return init;
1058         }
1059     }
1060
1061   /* Handle scalar types (including conversions) and references.  */
1062   if ((TREE_CODE (type) != COMPLEX_TYPE
1063        || BRACE_ENCLOSED_INITIALIZER_P (init))
1064       && (SCALAR_TYPE_P (type) || code == REFERENCE_TYPE))
1065     {
1066       tree *exp;
1067
1068       if (nested)
1069         flags |= LOOKUP_NO_NARROWING;
1070       init = convert_for_initialization (0, type, init, flags,
1071                                          ICR_INIT, NULL_TREE, 0,
1072                                          complain);
1073       exp = &init;
1074
1075       /* Skip any conversions since we'll be outputting the underlying
1076          constant.  */
1077       while (CONVERT_EXPR_P (*exp)
1078              || TREE_CODE (*exp) == NON_LVALUE_EXPR)
1079         exp = &TREE_OPERAND (*exp, 0);
1080
1081       *exp = cplus_expand_constant (*exp);
1082
1083       return init;
1084     }
1085
1086   /* Come here only for aggregates: records, arrays, unions, complex numbers
1087      and vectors.  */
1088   gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1089               || TREE_CODE (type) == VECTOR_TYPE
1090               || TREE_CODE (type) == RECORD_TYPE
1091               || TREE_CODE (type) == UNION_TYPE
1092               || TREE_CODE (type) == COMPLEX_TYPE);
1093
1094   /* "If T is a class type and the initializer list has a single
1095      element of type cv U, where U is T or a class derived from T,
1096      the object is initialized from that element."  */
1097   if (cxx_dialect >= cxx11
1098       && BRACE_ENCLOSED_INITIALIZER_P (init)
1099       && CONSTRUCTOR_NELTS (init) == 1
1100       && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type)))
1101     {
1102       tree elt = CONSTRUCTOR_ELT (init, 0)->value;
1103       if (reference_related_p (type, TREE_TYPE (elt)))
1104         init = elt;
1105     }
1106
1107   if (BRACE_ENCLOSED_INITIALIZER_P (init)
1108       && !TYPE_NON_AGGREGATE_CLASS (type))
1109     return process_init_constructor (type, init, complain);
1110   else
1111     {
1112       if (COMPOUND_LITERAL_P (init) && TREE_CODE (type) == ARRAY_TYPE)
1113         {
1114           if (complain & tf_error)
1115             error ("cannot initialize aggregate of type %qT with "
1116                    "a compound literal", type);
1117
1118           return error_mark_node;
1119         }
1120
1121       if (TREE_CODE (type) == ARRAY_TYPE
1122           && !BRACE_ENCLOSED_INITIALIZER_P (init))
1123         {
1124           /* Allow the result of build_array_copy and of
1125              build_value_init_noctor.  */
1126           if ((TREE_CODE (init) == VEC_INIT_EXPR
1127                || TREE_CODE (init) == CONSTRUCTOR)
1128               && (same_type_ignoring_top_level_qualifiers_p
1129                   (type, TREE_TYPE (init))))
1130             return init;
1131
1132           if (complain & tf_error)
1133             error ("array must be initialized with a brace-enclosed"
1134                    " initializer");
1135           return error_mark_node;
1136         }
1137
1138       return convert_for_initialization (NULL_TREE, type, init,
1139                                          flags,
1140                                          ICR_INIT, NULL_TREE, 0,
1141                                          complain);
1142     }
1143 }
1144
1145 tree
1146 digest_init (tree type, tree init, tsubst_flags_t complain)
1147 {
1148   return digest_init_r (type, init, false, LOOKUP_IMPLICIT, complain);
1149 }
1150
1151 tree
1152 digest_init_flags (tree type, tree init, int flags)
1153 {
1154   return digest_init_r (type, init, false, flags, tf_warning_or_error);
1155 }
1156
1157 /* Process the initializer INIT for an NSDMI DECL (a FIELD_DECL).  */
1158 tree
1159 digest_nsdmi_init (tree decl, tree init)
1160 {
1161   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
1162
1163   int flags = LOOKUP_IMPLICIT;
1164   if (DIRECT_LIST_INIT_P (init))
1165     flags = LOOKUP_NORMAL;
1166   init = digest_init_flags (TREE_TYPE (decl), init, flags);
1167   if (TREE_CODE (init) == TARGET_EXPR)
1168     /* This represents the whole initialization.  */
1169     TARGET_EXPR_DIRECT_INIT_P (init) = true;
1170   return init;
1171 }
1172 \f
1173 /* Set of flags used within process_init_constructor to describe the
1174    initializers.  */
1175 #define PICFLAG_ERRONEOUS 1
1176 #define PICFLAG_NOT_ALL_CONSTANT 2
1177 #define PICFLAG_NOT_ALL_SIMPLE 4
1178 #define PICFLAG_SIDE_EFFECTS 8
1179
1180 /* Given an initializer INIT, return the flag (PICFLAG_*) which better
1181    describe it.  */
1182
1183 static int
1184 picflag_from_initializer (tree init)
1185 {
1186   if (init == error_mark_node)
1187     return PICFLAG_ERRONEOUS;
1188   else if (!TREE_CONSTANT (init))
1189     {
1190       if (TREE_SIDE_EFFECTS (init))
1191         return PICFLAG_SIDE_EFFECTS;
1192       else
1193         return PICFLAG_NOT_ALL_CONSTANT;
1194     }
1195   else if (!initializer_constant_valid_p (init, TREE_TYPE (init)))
1196     return PICFLAG_NOT_ALL_SIMPLE;
1197   return 0;
1198 }
1199
1200 /* Adjust INIT for going into a CONSTRUCTOR.  */
1201
1202 static tree
1203 massage_init_elt (tree type, tree init, tsubst_flags_t complain)
1204 {
1205   init = digest_init_r (type, init, true, LOOKUP_IMPLICIT, complain);
1206   /* Strip a simple TARGET_EXPR when we know this is an initializer.  */
1207   if (TREE_CODE (init) == TARGET_EXPR
1208       && !VOID_TYPE_P (TREE_TYPE (TARGET_EXPR_INITIAL (init))))
1209     init = TARGET_EXPR_INITIAL (init);
1210   /* When we defer constant folding within a statement, we may want to
1211      defer this folding as well.  */
1212   tree t = fold_non_dependent_expr (init);
1213   t = maybe_constant_init (t);
1214   if (TREE_CONSTANT (t))
1215     init = t;
1216   return init;
1217 }
1218
1219 /* Subroutine of process_init_constructor, which will process an initializer
1220    INIT for an array or vector of type TYPE. Returns the flags (PICFLAG_*)
1221    which describe the initializers.  */
1222
1223 static int
1224 process_init_constructor_array (tree type, tree init,
1225                                 tsubst_flags_t complain)
1226 {
1227   unsigned HOST_WIDE_INT i, len = 0;
1228   int flags = 0;
1229   bool unbounded = false;
1230   constructor_elt *ce;
1231   vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (init);
1232
1233   gcc_assert (TREE_CODE (type) == ARRAY_TYPE
1234               || TREE_CODE (type) == VECTOR_TYPE);
1235
1236   if (TREE_CODE (type) == ARRAY_TYPE)
1237     {
1238       tree domain = TYPE_DOMAIN (type);
1239       if (domain && TREE_CONSTANT (TYPE_MAX_VALUE (domain)))
1240         len = wi::ext (wi::to_offset (TYPE_MAX_VALUE (domain))
1241                        - wi::to_offset (TYPE_MIN_VALUE (domain)) + 1,
1242                        TYPE_PRECISION (TREE_TYPE (domain)),
1243                        TYPE_SIGN (TREE_TYPE (domain))).to_uhwi ();
1244       else
1245         unbounded = true;  /* Take as many as there are.  */
1246     }
1247   else
1248     /* Vectors are like simple fixed-size arrays.  */
1249     len = TYPE_VECTOR_SUBPARTS (type);
1250
1251   /* There must not be more initializers than needed.  */
1252   if (!unbounded && vec_safe_length (v) > len)
1253     {
1254       if (complain & tf_error)
1255         error ("too many initializers for %qT", type);
1256       else
1257         return PICFLAG_ERRONEOUS;
1258     }
1259
1260   FOR_EACH_VEC_SAFE_ELT (v, i, ce)
1261     {
1262       if (ce->index)
1263         {
1264           gcc_assert (TREE_CODE (ce->index) == INTEGER_CST);
1265           if (compare_tree_int (ce->index, i) != 0)
1266             {
1267               ce->value = error_mark_node;
1268               sorry ("non-trivial designated initializers not supported");
1269             }
1270         }
1271       else
1272         ce->index = size_int (i);
1273       gcc_assert (ce->value);
1274       ce->value = massage_init_elt (TREE_TYPE (type), ce->value, complain);
1275
1276       if (ce->value != error_mark_node)
1277         gcc_assert (same_type_ignoring_top_level_qualifiers_p
1278                       (TREE_TYPE (type), TREE_TYPE (ce->value)));
1279
1280       flags |= picflag_from_initializer (ce->value);
1281     }
1282
1283   /* No more initializers. If the array is unbounded, we are done. Otherwise,
1284      we must add initializers ourselves.  */
1285   if (!unbounded)
1286     for (; i < len; ++i)
1287       {
1288         tree next;
1289
1290         if (type_build_ctor_call (TREE_TYPE (type)))
1291           {
1292             /* If this type needs constructors run for default-initialization,
1293                we can't rely on the back end to do it for us, so make the
1294                initialization explicit by list-initializing from T{}.  */
1295             next = build_constructor (init_list_type_node, NULL);
1296             CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
1297             next = massage_init_elt (TREE_TYPE (type), next, complain);
1298             if (initializer_zerop (next))
1299               /* The default zero-initialization is fine for us; don't
1300                  add anything to the CONSTRUCTOR.  */
1301               next = NULL_TREE;
1302           }
1303         else if (!zero_init_p (TREE_TYPE (type)))
1304           next = build_zero_init (TREE_TYPE (type),
1305                                   /*nelts=*/NULL_TREE,
1306                                   /*static_storage_p=*/false);
1307         else
1308           /* The default zero-initialization is fine for us; don't
1309              add anything to the CONSTRUCTOR.  */
1310           next = NULL_TREE;
1311
1312         if (next)
1313           {
1314             flags |= picflag_from_initializer (next);
1315             CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
1316           }
1317       }
1318
1319   CONSTRUCTOR_ELTS (init) = v;
1320   return flags;
1321 }
1322
1323 /* Subroutine of process_init_constructor, which will process an initializer
1324    INIT for a class of type TYPE. Returns the flags (PICFLAG_*) which describe
1325    the initializers.  */
1326
1327 static int
1328 process_init_constructor_record (tree type, tree init,
1329                                  tsubst_flags_t complain)
1330 {
1331   vec<constructor_elt, va_gc> *v = NULL;
1332   tree field;
1333   int skipped = 0;
1334
1335   gcc_assert (TREE_CODE (type) == RECORD_TYPE);
1336   gcc_assert (!CLASSTYPE_VBASECLASSES (type));
1337   gcc_assert (!TYPE_BINFO (type)
1338               || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
1339   gcc_assert (!TYPE_POLYMORPHIC_P (type));
1340
1341  restart:
1342   int flags = 0;
1343   unsigned HOST_WIDE_INT idx = 0;
1344   /* Generally, we will always have an index for each initializer (which is
1345      a FIELD_DECL, put by reshape_init), but compound literals don't go trough
1346      reshape_init. So we need to handle both cases.  */
1347   for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
1348     {
1349       tree next;
1350       tree type;
1351
1352       if (!DECL_NAME (field) && DECL_C_BIT_FIELD (field))
1353         continue;
1354
1355       if (TREE_CODE (field) != FIELD_DECL || DECL_ARTIFICIAL (field))
1356         continue;
1357
1358       /* If this is a bitfield, first convert to the declared type.  */
1359       type = TREE_TYPE (field);
1360       if (DECL_BIT_FIELD_TYPE (field))
1361         type = DECL_BIT_FIELD_TYPE (field);
1362       if (type == error_mark_node)
1363         return PICFLAG_ERRONEOUS;
1364
1365       if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1366         {
1367           constructor_elt *ce = &(*CONSTRUCTOR_ELTS (init))[idx];
1368           if (ce->index)
1369             {
1370               /* We can have either a FIELD_DECL or an IDENTIFIER_NODE. The
1371                  latter case can happen in templates where lookup has to be
1372                  deferred.  */
1373               gcc_assert (TREE_CODE (ce->index) == FIELD_DECL
1374                           || identifier_p (ce->index));
1375               if (ce->index != field
1376                   && ce->index != DECL_NAME (field))
1377                 {
1378                   ce->value = error_mark_node;
1379                   sorry ("non-trivial designated initializers not supported");
1380                 }
1381             }
1382
1383           gcc_assert (ce->value);
1384           next = massage_init_elt (type, ce->value, complain);
1385           ++idx;
1386         }
1387       else if (DECL_INITIAL (field))
1388         {
1389           if (skipped > 0)
1390             {
1391               /* We're using an NSDMI past a field with implicit
1392                  zero-init.  Go back and make it explicit.  */
1393               skipped = -1;
1394               vec_safe_truncate (v, 0);
1395               goto restart;
1396             }
1397           /* C++14 aggregate NSDMI.  */
1398           next = get_nsdmi (field, /*ctor*/false);
1399         }
1400       else if (type_build_ctor_call (TREE_TYPE (field)))
1401         {
1402           /* If this type needs constructors run for
1403              default-initialization, we can't rely on the back end to do it
1404              for us, so build up TARGET_EXPRs.  If the type in question is
1405              a class, just build one up; if it's an array, recurse.  */
1406           next = build_constructor (init_list_type_node, NULL);
1407           /* Call this direct-initialization pending DR 1518 resolution so
1408              that explicit default ctors don't break valid C++03 code.  */
1409           CONSTRUCTOR_IS_DIRECT_INIT (next) = true;
1410           next = massage_init_elt (TREE_TYPE (field), next, complain);
1411
1412           /* Warn when some struct elements are implicitly initialized.  */
1413           if ((complain & tf_warning)
1414               && !EMPTY_CONSTRUCTOR_P (init))
1415             warning (OPT_Wmissing_field_initializers,
1416                      "missing initializer for member %qD", field);
1417         }
1418       else
1419         {
1420           if (TREE_CODE (TREE_TYPE (field)) == REFERENCE_TYPE)
1421             {
1422               if (complain & tf_error)
1423                 error ("member %qD is uninitialized reference", field);
1424               else
1425                 return PICFLAG_ERRONEOUS;
1426             }
1427           else if (CLASSTYPE_REF_FIELDS_NEED_INIT (TREE_TYPE (field)))
1428             {
1429               if (complain & tf_error)
1430                 error ("member %qD with uninitialized reference fields", field);
1431               else
1432                 return PICFLAG_ERRONEOUS;
1433             }
1434
1435           /* Warn when some struct elements are implicitly initialized
1436              to zero.  */
1437           if ((complain & tf_warning)
1438               && !EMPTY_CONSTRUCTOR_P (init))
1439             warning (OPT_Wmissing_field_initializers,
1440                      "missing initializer for member %qD", field);
1441
1442           if (!zero_init_p (TREE_TYPE (field))
1443               || skipped < 0)
1444             next = build_zero_init (TREE_TYPE (field), /*nelts=*/NULL_TREE,
1445                                     /*static_storage_p=*/false);
1446           else
1447             {
1448               /* The default zero-initialization is fine for us; don't
1449                  add anything to the CONSTRUCTOR.  */
1450               skipped = 1;
1451               continue;
1452             }
1453         }
1454
1455       /* If this is a bitfield, now convert to the lowered type.  */
1456       if (type != TREE_TYPE (field))
1457         next = cp_convert_and_check (TREE_TYPE (field), next, complain);
1458       flags |= picflag_from_initializer (next);
1459       CONSTRUCTOR_APPEND_ELT (v, field, next);
1460     }
1461
1462   if (idx < vec_safe_length (CONSTRUCTOR_ELTS (init)))
1463     {
1464       if (complain & tf_error)
1465         error ("too many initializers for %qT", type);
1466       else
1467         return PICFLAG_ERRONEOUS;
1468     }
1469
1470   CONSTRUCTOR_ELTS (init) = v;
1471   return flags;
1472 }
1473
1474 /* Subroutine of process_init_constructor, which will process a single
1475    initializer INIT for a union of type TYPE. Returns the flags (PICFLAG_*)
1476    which describe the initializer.  */
1477
1478 static int
1479 process_init_constructor_union (tree type, tree init,
1480                                 tsubst_flags_t complain)
1481 {
1482   constructor_elt *ce;
1483   int len;
1484
1485   /* If the initializer was empty, use the union's NSDMI if it has one.
1486      Otherwise use default zero initialization.  */
1487   if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1488     {
1489       for (tree field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1490         {
1491           if (DECL_INITIAL (field))
1492             {
1493               CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (init),
1494                                       field,
1495                                       get_nsdmi (field, /*in_ctor=*/false));
1496               break;
1497             }
1498         }
1499
1500       if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
1501         return 0;
1502     }
1503
1504   len = CONSTRUCTOR_ELTS (init)->length ();
1505   if (len > 1)
1506     {
1507       if (!(complain & tf_error))
1508         return PICFLAG_ERRONEOUS;
1509       error ("too many initializers for %qT", type);
1510       CONSTRUCTOR_ELTS (init)->block_remove (1, len-1);
1511     }
1512
1513   ce = &(*CONSTRUCTOR_ELTS (init))[0];
1514
1515   /* If this element specifies a field, initialize via that field.  */
1516   if (ce->index)
1517     {
1518       if (TREE_CODE (ce->index) == FIELD_DECL)
1519         ;
1520       else if (identifier_p (ce->index))
1521         {
1522           /* This can happen within a cast, see g++.dg/opt/cse2.C.  */
1523           tree name = ce->index;
1524           tree field;
1525           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1526             if (DECL_NAME (field) == name)
1527               break;
1528           if (!field)
1529             {
1530               if (complain & tf_error)
1531                 error ("no field %qD found in union being initialized",
1532                        field);
1533               ce->value = error_mark_node;
1534             }
1535           ce->index = field;
1536         }
1537       else
1538         {
1539           gcc_assert (TREE_CODE (ce->index) == INTEGER_CST
1540                       || TREE_CODE (ce->index) == RANGE_EXPR);
1541           if (complain & tf_error)
1542             error ("index value instead of field name in union initializer");
1543           ce->value = error_mark_node;
1544         }
1545     }
1546   else
1547     {
1548       /* Find the first named field.  ANSI decided in September 1990
1549          that only named fields count here.  */
1550       tree field = TYPE_FIELDS (type);
1551       while (field && (!DECL_NAME (field) || TREE_CODE (field) != FIELD_DECL))
1552         field = TREE_CHAIN (field);
1553       if (field == NULL_TREE)
1554         {
1555           if (complain & tf_error)
1556             error ("too many initializers for %qT", type);
1557           ce->value = error_mark_node;
1558         }
1559       ce->index = field;
1560     }
1561
1562   if (ce->value && ce->value != error_mark_node)
1563     ce->value = massage_init_elt (TREE_TYPE (ce->index), ce->value, complain);
1564
1565   return picflag_from_initializer (ce->value);
1566 }
1567
1568 /* Process INIT, a constructor for a variable of aggregate type TYPE. The
1569    constructor is a brace-enclosed initializer, and will be modified in-place.
1570
1571    Each element is converted to the right type through digest_init, and
1572    missing initializers are added following the language rules (zero-padding,
1573    etc.).
1574
1575    After the execution, the initializer will have TREE_CONSTANT if all elts are
1576    constant, and TREE_STATIC set if, in addition, all elts are simple enough
1577    constants that the assembler and linker can compute them.
1578
1579    The function returns the initializer itself, or error_mark_node in case
1580    of error.  */
1581
1582 static tree
1583 process_init_constructor (tree type, tree init, tsubst_flags_t complain)
1584 {
1585   int flags;
1586
1587   gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init));
1588
1589   if (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == VECTOR_TYPE)
1590     flags = process_init_constructor_array (type, init, complain);
1591   else if (TREE_CODE (type) == RECORD_TYPE)
1592     flags = process_init_constructor_record (type, init, complain);
1593   else if (TREE_CODE (type) == UNION_TYPE)
1594     flags = process_init_constructor_union (type, init, complain);
1595   else
1596     gcc_unreachable ();
1597
1598   if (flags & PICFLAG_ERRONEOUS)
1599     return error_mark_node;
1600
1601   TREE_TYPE (init) = type;
1602   if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type) == NULL_TREE)
1603     cp_complete_array_type (&TREE_TYPE (init), init, /*do_default=*/0);
1604   if (flags & PICFLAG_SIDE_EFFECTS)
1605     {
1606       TREE_CONSTANT (init) = false;
1607       TREE_SIDE_EFFECTS (init) = true;
1608     }
1609   else if (flags & PICFLAG_NOT_ALL_CONSTANT)
1610     /* Make sure TREE_CONSTANT isn't set from build_constructor.  */
1611     TREE_CONSTANT (init) = false;
1612   else
1613     {
1614       TREE_CONSTANT (init) = 1;
1615       if (!(flags & PICFLAG_NOT_ALL_SIMPLE))
1616         TREE_STATIC (init) = 1;
1617     }
1618   return init;
1619 }
1620 \f
1621 /* Given a structure or union value DATUM, construct and return
1622    the structure or union component which results from narrowing
1623    that value to the base specified in BASETYPE.  For example, given the
1624    hierarchy
1625
1626    class L { int ii; };
1627    class A : L { ... };
1628    class B : L { ... };
1629    class C : A, B { ... };
1630
1631    and the declaration
1632
1633    C x;
1634
1635    then the expression
1636
1637    x.A::ii refers to the ii member of the L part of
1638    the A part of the C object named by X.  In this case,
1639    DATUM would be x, and BASETYPE would be A.
1640
1641    I used to think that this was nonconformant, that the standard specified
1642    that first we look up ii in A, then convert x to an L& and pull out the
1643    ii part.  But in fact, it does say that we convert x to an A&; A here
1644    is known as the "naming class".  (jason 2000-12-19)
1645
1646    BINFO_P points to a variable initialized either to NULL_TREE or to the
1647    binfo for the specific base subobject we want to convert to.  */
1648
1649 tree
1650 build_scoped_ref (tree datum, tree basetype, tree* binfo_p)
1651 {
1652   tree binfo;
1653
1654   if (datum == error_mark_node)
1655     return error_mark_node;
1656   if (*binfo_p)
1657     binfo = *binfo_p;
1658   else
1659     binfo = lookup_base (TREE_TYPE (datum), basetype, ba_check,
1660                          NULL, tf_warning_or_error);
1661
1662   if (!binfo || binfo == error_mark_node)
1663     {
1664       *binfo_p = NULL_TREE;
1665       if (!binfo)
1666         error_not_base_type (basetype, TREE_TYPE (datum));
1667       return error_mark_node;
1668     }
1669
1670   *binfo_p = binfo;
1671   return build_base_path (PLUS_EXPR, datum, binfo, 1,
1672                           tf_warning_or_error);
1673 }
1674
1675 /* Build a reference to an object specified by the C++ `->' operator.
1676    Usually this just involves dereferencing the object, but if the
1677    `->' operator is overloaded, then such overloads must be
1678    performed until an object which does not have the `->' operator
1679    overloaded is found.  An error is reported when circular pointer
1680    delegation is detected.  */
1681
1682 tree
1683 build_x_arrow (location_t loc, tree expr, tsubst_flags_t complain)
1684 {
1685   tree orig_expr = expr;
1686   tree type = TREE_TYPE (expr);
1687   tree last_rval = NULL_TREE;
1688   vec<tree, va_gc> *types_memoized = NULL;
1689
1690   if (type == error_mark_node)
1691     return error_mark_node;
1692
1693   if (processing_template_decl)
1694     {
1695       if (type_dependent_expression_p (expr))
1696         return build_min_nt_loc (loc, ARROW_EXPR, expr);
1697       expr = build_non_dependent_expr (expr);
1698     }
1699
1700   if (MAYBE_CLASS_TYPE_P (type))
1701     {
1702       struct tinst_level *actual_inst = current_instantiation ();
1703       tree fn = NULL;
1704
1705       while ((expr = build_new_op (loc, COMPONENT_REF,
1706                                    LOOKUP_NORMAL, expr, NULL_TREE, NULL_TREE,
1707                                    &fn, complain)))
1708         {
1709           if (expr == error_mark_node)
1710             return error_mark_node;
1711
1712           /* This provides a better instantiation backtrace in case of
1713              error.  */
1714           if (fn && DECL_USE_TEMPLATE (fn))
1715             push_tinst_level_loc (fn, 
1716                                   (current_instantiation () != actual_inst)
1717                                   ? DECL_SOURCE_LOCATION (fn)
1718                                   : input_location);
1719           fn = NULL;
1720
1721           if (vec_member (TREE_TYPE (expr), types_memoized))
1722             {
1723               if (complain & tf_error)
1724                 error ("circular pointer delegation detected");
1725               return error_mark_node;
1726             }
1727
1728           vec_safe_push (types_memoized, TREE_TYPE (expr));
1729           last_rval = expr;
1730         }
1731
1732       while (current_instantiation () != actual_inst)
1733         pop_tinst_level ();
1734
1735       if (last_rval == NULL_TREE)
1736         {
1737           if (complain & tf_error)
1738             error ("base operand of %<->%> has non-pointer type %qT", type);
1739           return error_mark_node;
1740         }
1741
1742       if (TREE_CODE (TREE_TYPE (last_rval)) == REFERENCE_TYPE)
1743         last_rval = convert_from_reference (last_rval);
1744     }
1745   else
1746     last_rval = decay_conversion (expr, complain);
1747
1748   if (TYPE_PTR_P (TREE_TYPE (last_rval)))
1749     {
1750       if (processing_template_decl)
1751         {
1752           expr = build_min (ARROW_EXPR, TREE_TYPE (TREE_TYPE (last_rval)),
1753                             orig_expr);
1754           TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (last_rval);
1755           return expr;
1756         }
1757
1758       return cp_build_indirect_ref (last_rval, RO_NULL, complain);
1759     }
1760
1761   if (complain & tf_error)
1762     {
1763       if (types_memoized)
1764         error ("result of %<operator->()%> yields non-pointer result");
1765       else
1766         error ("base operand of %<->%> is not a pointer");
1767     }
1768   return error_mark_node;
1769 }
1770
1771 /* Return an expression for "DATUM .* COMPONENT".  DATUM has not
1772    already been checked out to be of aggregate type.  */
1773
1774 tree
1775 build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
1776 {
1777   tree ptrmem_type;
1778   tree objtype;
1779   tree type;
1780   tree binfo;
1781   tree ctype;
1782
1783   if (error_operand_p (datum) || error_operand_p (component))
1784     return error_mark_node;
1785
1786   datum = mark_lvalue_use (datum);
1787   component = mark_rvalue_use (component);
1788
1789   ptrmem_type = TREE_TYPE (component);
1790   if (!TYPE_PTRMEM_P (ptrmem_type))
1791     {
1792       if (complain & tf_error)
1793         error ("%qE cannot be used as a member pointer, since it is of "
1794                "type %qT", component, ptrmem_type);
1795       return error_mark_node;
1796     }
1797
1798   objtype = TYPE_MAIN_VARIANT (TREE_TYPE (datum));
1799   if (! MAYBE_CLASS_TYPE_P (objtype))
1800     {
1801       if (complain & tf_error)
1802         error ("cannot apply member pointer %qE to %qE, which is of "
1803                "non-class type %qT", component, datum, objtype);
1804       return error_mark_node;
1805     }
1806
1807   type = TYPE_PTRMEM_POINTED_TO_TYPE (ptrmem_type);
1808   ctype = complete_type (TYPE_PTRMEM_CLASS_TYPE (ptrmem_type));
1809
1810   if (!COMPLETE_TYPE_P (ctype))
1811     {
1812       if (!same_type_p (ctype, objtype))
1813         goto mismatch;
1814       binfo = NULL;
1815     }
1816   else
1817     {
1818       binfo = lookup_base (objtype, ctype, ba_check, NULL, complain);
1819
1820       if (!binfo)
1821         {
1822         mismatch:
1823           if (complain & tf_error)
1824             error ("pointer to member type %qT incompatible with object "
1825                    "type %qT", type, objtype);
1826           return error_mark_node;
1827         }
1828       else if (binfo == error_mark_node)
1829         return error_mark_node;
1830     }
1831
1832   if (TYPE_PTRDATAMEM_P (ptrmem_type))
1833     {
1834       cp_lvalue_kind kind = lvalue_kind (datum);
1835       tree ptype;
1836
1837       /* Compute the type of the field, as described in [expr.ref].
1838          There's no such thing as a mutable pointer-to-member, so
1839          things are not as complex as they are for references to
1840          non-static data members.  */
1841       type = cp_build_qualified_type (type,
1842                                       (cp_type_quals (type)
1843                                        | cp_type_quals (TREE_TYPE (datum))));
1844
1845       datum = build_address (datum);
1846
1847       /* Convert object to the correct base.  */
1848       if (binfo)
1849         {
1850           datum = build_base_path (PLUS_EXPR, datum, binfo, 1, complain);
1851           if (datum == error_mark_node)
1852             return error_mark_node;
1853         }
1854
1855       /* Build an expression for "object + offset" where offset is the
1856          value stored in the pointer-to-data-member.  */
1857       ptype = build_pointer_type (type);
1858       datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
1859       datum = cp_build_indirect_ref (datum, RO_NULL, complain);
1860       if (datum == error_mark_node)
1861         return error_mark_node;
1862
1863       /* If the object expression was an rvalue, return an rvalue.  */
1864       if (kind & clk_class)
1865         datum = rvalue (datum);
1866       else if (kind & clk_rvalueref)
1867         datum = move (datum);
1868       return datum;
1869     }
1870   else
1871     {
1872       /* 5.5/6: In a .* expression whose object expression is an rvalue, the
1873          program is ill-formed if the second operand is a pointer to member
1874          function with ref-qualifier &. In a .* expression whose object
1875          expression is an lvalue, the program is ill-formed if the second
1876          operand is a pointer to member function with ref-qualifier &&.  */
1877       if (FUNCTION_REF_QUALIFIED (type))
1878         {
1879           bool lval = real_lvalue_p (datum);
1880           if (lval && FUNCTION_RVALUE_QUALIFIED (type))
1881             {
1882               if (complain & tf_error)
1883                 error ("pointer-to-member-function type %qT requires an rvalue",
1884                        ptrmem_type);
1885               return error_mark_node;
1886             }
1887           else if (!lval && !FUNCTION_RVALUE_QUALIFIED (type))
1888             {
1889               if (complain & tf_error)
1890                 error ("pointer-to-member-function type %qT requires an lvalue",
1891                        ptrmem_type);
1892               return error_mark_node;
1893             }
1894         }
1895       return build2 (OFFSET_REF, type, datum, component);
1896     }
1897 }
1898
1899 /* Return a tree node for the expression TYPENAME '(' PARMS ')'.  */
1900
1901 tree
1902 build_functional_cast (tree exp, tree parms, tsubst_flags_t complain)
1903 {
1904   /* This is either a call to a constructor,
1905      or a C cast in C++'s `functional' notation.  */
1906
1907   /* The type to which we are casting.  */
1908   tree type;
1909   vec<tree, va_gc> *parmvec;
1910
1911   if (error_operand_p (exp) || parms == error_mark_node)
1912     return error_mark_node;
1913
1914   if (TREE_CODE (exp) == TYPE_DECL)
1915     {
1916       type = TREE_TYPE (exp);
1917
1918       if (complain & tf_warning
1919           && TREE_DEPRECATED (type)
1920           && DECL_ARTIFICIAL (exp))
1921         warn_deprecated_use (type, NULL_TREE);
1922     }
1923   else
1924     type = exp;
1925
1926   /* We need to check this explicitly, since value-initialization of
1927      arrays is allowed in other situations.  */
1928   if (TREE_CODE (type) == ARRAY_TYPE)
1929     {
1930       if (complain & tf_error)
1931         error ("functional cast to array type %qT", type);
1932       return error_mark_node;
1933     }
1934
1935   if (type_uses_auto (type))
1936     {
1937       if (complain & tf_error)
1938         error ("invalid use of %<auto%>");
1939       return error_mark_node;
1940     }
1941
1942   if (processing_template_decl)
1943     {
1944       tree t;
1945
1946       /* Diagnose this even in a template.  We could also try harder
1947          to give all the usual errors when the type and args are
1948          non-dependent...  */
1949       if (TREE_CODE (type) == REFERENCE_TYPE && !parms)
1950         {
1951           if (complain & tf_error)
1952             error ("invalid value-initialization of reference type");
1953           return error_mark_node;
1954         }
1955
1956       t = build_min (CAST_EXPR, type, parms);
1957       /* We don't know if it will or will not have side effects.  */
1958       TREE_SIDE_EFFECTS (t) = 1;
1959       return t;
1960     }
1961
1962   if (! MAYBE_CLASS_TYPE_P (type))
1963     {
1964       if (parms == NULL_TREE)
1965         {
1966           if (VOID_TYPE_P (type))
1967             return void_node;
1968           return build_value_init (cv_unqualified (type), complain);
1969         }
1970
1971       /* This must build a C cast.  */
1972       parms = build_x_compound_expr_from_list (parms, ELK_FUNC_CAST, complain);
1973       return cp_build_c_cast (type, parms, complain);
1974     }
1975
1976   /* Prepare to evaluate as a call to a constructor.  If this expression
1977      is actually used, for example,
1978
1979      return X (arg1, arg2, ...);
1980
1981      then the slot being initialized will be filled in.  */
1982
1983   if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
1984     return error_mark_node;
1985   if (abstract_virtuals_error_sfinae (ACU_CAST, type, complain))
1986     return error_mark_node;
1987
1988   /* [expr.type.conv]
1989
1990      If the expression list is a single-expression, the type
1991      conversion is equivalent (in definedness, and if defined in
1992      meaning) to the corresponding cast expression.  */
1993   if (parms && TREE_CHAIN (parms) == NULL_TREE)
1994     return cp_build_c_cast (type, TREE_VALUE (parms), complain);
1995
1996   /* [expr.type.conv]
1997
1998      The expression T(), where T is a simple-type-specifier for a
1999      non-array complete object type or the (possibly cv-qualified)
2000      void type, creates an rvalue of the specified type, which is
2001      value-initialized.  */
2002
2003   if (parms == NULL_TREE)
2004     {
2005       exp = build_value_init (type, complain);
2006       exp = get_target_expr_sfinae (exp, complain);
2007       return exp;
2008     }
2009
2010   /* Call the constructor.  */
2011   parmvec = make_tree_vector ();
2012   for (; parms != NULL_TREE; parms = TREE_CHAIN (parms))
2013     vec_safe_push (parmvec, TREE_VALUE (parms));
2014   exp = build_special_member_call (NULL_TREE, complete_ctor_identifier,
2015                                    &parmvec, type, LOOKUP_NORMAL, complain);
2016   release_tree_vector (parmvec);
2017
2018   if (exp == error_mark_node)
2019     return error_mark_node;
2020
2021   return build_cplus_new (type, exp, complain);
2022 }
2023 \f
2024
2025 /* Add new exception specifier SPEC, to the LIST we currently have.
2026    If it's already in LIST then do nothing.
2027    Moan if it's bad and we're allowed to. COMPLAIN < 0 means we
2028    know what we're doing.  */
2029
2030 tree
2031 add_exception_specifier (tree list, tree spec, int complain)
2032 {
2033   bool ok;
2034   tree core = spec;
2035   bool is_ptr;
2036   diagnostic_t diag_type = DK_UNSPECIFIED; /* none */
2037
2038   if (spec == error_mark_node)
2039     return list;
2040
2041   gcc_assert (spec && (!list || TREE_VALUE (list)));
2042
2043   /* [except.spec] 1, type in an exception specifier shall not be
2044      incomplete, or pointer or ref to incomplete other than pointer
2045      to cv void.  */
2046   is_ptr = TYPE_PTR_P (core);
2047   if (is_ptr || TREE_CODE (core) == REFERENCE_TYPE)
2048     core = TREE_TYPE (core);
2049   if (complain < 0)
2050     ok = true;
2051   else if (VOID_TYPE_P (core))
2052     ok = is_ptr;
2053   else if (TREE_CODE (core) == TEMPLATE_TYPE_PARM)
2054     ok = true;
2055   else if (processing_template_decl)
2056     ok = true;
2057   else
2058     {
2059       ok = true;
2060       /* 15.4/1 says that types in an exception specifier must be complete,
2061          but it seems more reasonable to only require this on definitions
2062          and calls.  So just give a pedwarn at this point; we will give an
2063          error later if we hit one of those two cases.  */
2064       if (!COMPLETE_TYPE_P (complete_type (core)))
2065         diag_type = DK_PEDWARN; /* pedwarn */
2066     }
2067
2068   if (ok)
2069     {
2070       tree probe;
2071
2072       for (probe = list; probe; probe = TREE_CHAIN (probe))
2073         if (same_type_p (TREE_VALUE (probe), spec))
2074           break;
2075       if (!probe)
2076         list = tree_cons (NULL_TREE, spec, list);
2077     }
2078   else
2079     diag_type = DK_ERROR; /* error */
2080
2081   if (diag_type != DK_UNSPECIFIED
2082       && (complain & tf_warning_or_error))
2083     cxx_incomplete_type_diagnostic (NULL_TREE, core, diag_type);
2084
2085   return list;
2086 }
2087
2088 /* Like nothrow_spec_p, but don't abort on deferred noexcept.  */
2089
2090 static bool
2091 nothrow_spec_p_uninst (const_tree spec)
2092 {
2093   if (DEFERRED_NOEXCEPT_SPEC_P (spec))
2094     return false;
2095   return nothrow_spec_p (spec);
2096 }
2097
2098 /* Combine the two exceptions specifier lists LIST and ADD, and return
2099    their union.  */
2100
2101 tree
2102 merge_exception_specifiers (tree list, tree add)
2103 {
2104   tree noex, orig_list;
2105
2106   /* No exception-specifier or noexcept(false) are less strict than
2107      anything else.  Prefer the newer variant (LIST).  */
2108   if (!list || list == noexcept_false_spec)
2109     return list;
2110   else if (!add || add == noexcept_false_spec)
2111     return add;
2112
2113   /* noexcept(true) and throw() are stricter than anything else.
2114      As above, prefer the more recent one (LIST).  */
2115   if (nothrow_spec_p_uninst (add))
2116     return list;
2117
2118   /* Two implicit noexcept specs (e.g. on a destructor) are equivalent.  */
2119   if (UNEVALUATED_NOEXCEPT_SPEC_P (add)
2120       && UNEVALUATED_NOEXCEPT_SPEC_P (list))
2121     return list;
2122   /* We should have instantiated other deferred noexcept specs by now.  */
2123   gcc_assert (!DEFERRED_NOEXCEPT_SPEC_P (add));
2124
2125   if (nothrow_spec_p_uninst (list))
2126     return add;
2127   noex = TREE_PURPOSE (list);
2128   gcc_checking_assert (!TREE_PURPOSE (add)
2129                        || cp_tree_equal (noex, TREE_PURPOSE (add)));
2130
2131   /* Combine the dynamic-exception-specifiers, if any.  */
2132   orig_list = list;
2133   for (; add && TREE_VALUE (add); add = TREE_CHAIN (add))
2134     {
2135       tree spec = TREE_VALUE (add);
2136       tree probe;
2137
2138       for (probe = orig_list; probe && TREE_VALUE (probe);
2139            probe = TREE_CHAIN (probe))
2140         if (same_type_p (TREE_VALUE (probe), spec))
2141           break;
2142       if (!probe)
2143         {
2144           spec = build_tree_list (NULL_TREE, spec);
2145           TREE_CHAIN (spec) = list;
2146           list = spec;
2147         }
2148     }
2149
2150   /* Keep the noexcept-specifier at the beginning of the list.  */
2151   if (noex != TREE_PURPOSE (list))
2152     list = tree_cons (noex, TREE_VALUE (list), TREE_CHAIN (list));
2153
2154   return list;
2155 }
2156
2157 /* Subroutine of build_call.  Ensure that each of the types in the
2158    exception specification is complete.  Technically, 15.4/1 says that
2159    they need to be complete when we see a declaration of the function,
2160    but we should be able to get away with only requiring this when the
2161    function is defined or called.  See also add_exception_specifier.  */
2162
2163 void
2164 require_complete_eh_spec_types (tree fntype, tree decl)
2165 {
2166   tree raises;
2167   /* Don't complain about calls to op new.  */
2168   if (decl && DECL_ARTIFICIAL (decl))
2169     return;
2170   for (raises = TYPE_RAISES_EXCEPTIONS (fntype); raises;
2171        raises = TREE_CHAIN (raises))
2172     {
2173       tree type = TREE_VALUE (raises);
2174       if (type && !COMPLETE_TYPE_P (type))
2175         {
2176           if (decl)
2177             error
2178               ("call to function %qD which throws incomplete type %q#T",
2179                decl, type);
2180           else
2181             error ("call to function which throws incomplete type %q#T",
2182                    decl);
2183         }
2184     }
2185 }
2186
2187 \f
2188 #include "gt-cp-typeck2.h"