OSDN Git Service

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