SemaInit.cpp revision 3c394c54511b27be0ff6968f159bba3521ab3c3e
10cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===//
20cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//
30cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//                     The LLVM Compiler Infrastructure
40cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//
50cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff// This file is distributed under the University of Illinois Open Source
60cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff// License. See LICENSE.TXT for details.
70cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//
80cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//===----------------------------------------------------------------------===//
90cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//
105d3d41d0873d51b405972baf38e1f3a7ef5b49e0Sebastian Redl// This file implements semantic analysis for initializers.
11dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//
120cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff//===----------------------------------------------------------------------===//
130cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
1419510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/Designator.h"
15e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Initialization.h"
16e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Lookup.h"
172d88708cbe4e4ec5e04e4acb6bd7f5be68557379John McCall#include "clang/Sema/SemaInternal.h"
181e1d396dc55002a2a6355048db542a38b5137002Tanya Lattner#include "clang/Lex/Preprocessor.h"
190cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff#include "clang/AST/ASTContext.h"
207cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/AST/DeclObjC.h"
212078bb9c9336da56ea521e98e718556b227541f6Anders Carlsson#include "clang/AST/ExprCXX.h"
2279e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner#include "clang/AST/ExprObjC.h"
23d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor#include "clang/AST/TypeLoc.h"
242b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl#include "llvm/ADT/APInt.h"
258fe83e1df954d72c0f4ffc15d20a5222ec151c21Benjamin Kramer#include "llvm/ADT/SmallString.h"
2620093b4bf698f292c664676987541d5103b65b15Douglas Gregor#include "llvm/Support/ErrorHandling.h"
27191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin#include "llvm/Support/raw_ostream.h"
28c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor#include <map>
2905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregorusing namespace clang;
300cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
31dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
32dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// Sema Initialization Checking
33dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
34dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
35ce6c9b7da219c8640a0559f75770cfc6622de083John McCallstatic Expr *IsStringInit(Expr *Init, const ArrayType *AT,
36ce6c9b7da219c8640a0559f75770cfc6622de083John McCall                          ASTContext &Context) {
378718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
388718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman    return 0;
398718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman
408879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // See if this is a string literal or @encode.
418879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  Init = Init->IgnoreParens();
421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
438879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Handle @encode, which is a narrow string.
448879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
458879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner    return Init;
468879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
478879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  // Otherwise we can only handle string literals.
488879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner  StringLiteral *SL = dyn_cast<StringLiteral>(Init);
49220b6369d7717bfe6894b46cef055d3e763827f2Chris Lattner  if (SL == 0) return 0;
50bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman
51bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman  QualType ElemTy = Context.getCanonicalType(AT->getElementType());
525cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor
535cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  switch (SL->getKind()) {
545cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case StringLiteral::Ascii:
555cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case StringLiteral::UTF8:
565cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // char array can be initialized with a narrow string.
575cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // Only allow char x[] = "foo";  not char x[] = L"foo";
58bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman    return ElemTy->isCharType() ? Init : 0;
595cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case StringLiteral::UTF16:
605cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return ElemTy->isChar16Type() ? Init : 0;
615cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case StringLiteral::UTF32:
625cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return ElemTy->isChar32Type() ? Init : 0;
635cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  case StringLiteral::Wide:
645cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // wchar_t array can be initialized with a wide string: C99 6.7.8p15 (with
655cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // correction from DR343): "An array with element type compatible with a
665cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // qualified or unqualified version of wchar_t may be initialized by a wide
675cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    // string literal, optionally enclosed in braces."
685cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    if (Context.typesAreCompatible(Context.getWCharType(),
695cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor                                   ElemTy.getUnqualifiedType()))
705cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor      return Init;
718879e3b29d2527260c401bce0ed0e401901ef601Chris Lattner
725cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor    return 0;
735cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  }
741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
755cee1195584fa8672253139c86e922daeda69b9eDouglas Gregor  llvm_unreachable("missed a StringLiteral kind?");
76dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
77dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
78ce6c9b7da219c8640a0559f75770cfc6622de083John McCallstatic Expr *IsStringInit(Expr *init, QualType declType, ASTContext &Context) {
79ce6c9b7da219c8640a0559f75770cfc6622de083John McCall  const ArrayType *arrayType = Context.getAsArrayType(declType);
80ce6c9b7da219c8640a0559f75770cfc6622de083John McCall  if (!arrayType) return 0;
81ce6c9b7da219c8640a0559f75770cfc6622de083John McCall
82ce6c9b7da219c8640a0559f75770cfc6622de083John McCall  return IsStringInit(init, arrayType, Context);
83ce6c9b7da219c8640a0559f75770cfc6622de083John McCall}
84ce6c9b7da219c8640a0559f75770cfc6622de083John McCall
85fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCallstatic void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT,
86fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall                            Sema &S) {
8779e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  // Get the length of the string as parsed.
8879e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner  uint64_t StrLength =
8979e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner    cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
9079e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner
911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
92dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // C99 6.7.8p14. We have an array of character type with unknown size
94dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // being initialized to a string literal.
95dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    llvm::APSInt ConstVal(32);
9619da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    ConstVal = StrLength;
97dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner    // Return a new array type (C99 6.7.8p22).
9846a617a792bfab0d9b1e057371ea3b9540802226John McCall    DeclT = S.Context.getConstantArrayType(IAT->getElementType(),
9946a617a792bfab0d9b1e057371ea3b9540802226John McCall                                           ConstVal,
10046a617a792bfab0d9b1e057371ea3b9540802226John McCall                                           ArrayType::Normal, 0);
10119da8cdfb3d5cd31e06d02c7bab1eb1bd41a7949Chris Lattner    return;
102dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner  }
1031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1048718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
1051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
106bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman  // We have an array of character type with known size.  However,
1078718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // the size may be smaller or larger than the string we are initializing.
1088718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // FIXME: Avoid truncation for 64-bit length strings.
1094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (S.getLangOpts().CPlusPlus) {
110b8fc45f8d0fdcc7908590115942d425bf4a924f1Anders Carlsson    if (StringLiteral *SL = dyn_cast<StringLiteral>(Str)) {
111b8fc45f8d0fdcc7908590115942d425bf4a924f1Anders Carlsson      // For Pascal strings it's OK to strip off the terminating null character,
112b8fc45f8d0fdcc7908590115942d425bf4a924f1Anders Carlsson      // so the example below is valid:
113b8fc45f8d0fdcc7908590115942d425bf4a924f1Anders Carlsson      //
114b8fc45f8d0fdcc7908590115942d425bf4a924f1Anders Carlsson      // unsigned char a[2] = "\pa";
115b8fc45f8d0fdcc7908590115942d425bf4a924f1Anders Carlsson      if (SL->isPascal())
116b8fc45f8d0fdcc7908590115942d425bf4a924f1Anders Carlsson        StrLength--;
117b8fc45f8d0fdcc7908590115942d425bf4a924f1Anders Carlsson    }
118b8fc45f8d0fdcc7908590115942d425bf4a924f1Anders Carlsson
119bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman    // [dcl.init.string]p2
120bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman    if (StrLength > CAT->getSize().getZExtValue())
12196a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      S.Diag(Str->getLocStart(),
122bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman             diag::err_initializer_string_for_char_array_too_long)
123bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman        << Str->getSourceRange();
124bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman  } else {
125bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman    // C99 6.7.8p14.
126bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman    if (StrLength-1 > CAT->getSize().getZExtValue())
12796a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      S.Diag(Str->getLocStart(),
128bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman             diag::warn_initializer_string_for_char_array_too_long)
129bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman        << Str->getSourceRange();
130bc34b1d7f4f371bfdebf68d66086f54586d68abeEli Friedman  }
1311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1328718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // Set the type to the actual size that we are initializing.  If we have
1338718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // something like:
1348718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  //   char x[1] = "foo";
1358718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  // then this will set the string literal's type to char[1].
1368718a6a02ccc53fea758677781a8df3a8b0c41c9Eli Friedman  Str->setType(DeclT);
137dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner}
138dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
139dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
140dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner// Semantic checking for initializer lists.
141dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner//===----------------------------------------------------------------------===//
142dd8e0065207e953bb28b95ad9cb6b2c13f56b3b8Chris Lattner
1439e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// @brief Semantic checking for initializer lists.
1449e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
1459e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// The InitListChecker class contains a set of routines that each
1469e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// handle the initialization of a certain kind of entity, e.g.,
1479e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// arrays, vectors, struct/union types, scalars, etc. The
1489e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// InitListChecker itself performs a recursive walk of the subobject
1499e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// structure of the type to be initialized, while stepping through
1509e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// the initializer list one element at a time. The IList and Index
1519e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// parameters to each of the Check* routines contain the active
1529e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// (syntactic) initializer list and the index into that initializer
1539e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list that represents the current initializer. Each routine is
1549e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// responsible for moving that Index forward as it consumes elements.
1559e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
1569e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// Each Check* routine also has a StructuredList/StructuredIndex
15763e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara/// arguments, which contains the current "structured" (semantic)
1589e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// initializer list and the index into that initializer list where we
1599e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// are copying initializers as we map them over to the semantic
1609e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list. Once we have completed our recursive walk of the subobject
1619e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// structure, we will have constructed a full semantic initializer
1629e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// list.
1639e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor///
1649e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// C99 designators cause changes in the initializer list traversal,
1659e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// because they make the initialization "jump" into a specific
1669e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// subobject and then continue the initialization from that
1679e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// point. CheckDesignatedInitializer() recursively steps into the
1689e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// designated subobject and manages backing out the recursion to
1699e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor/// initialize the subobjects after the one designated.
1708b419b9b5f21cf948cf6fe788f67bf1efd97524cChris Lattnernamespace {
171c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregorclass InitListChecker {
1720820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  Sema &SemaRef;
173c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  bool hadError;
17414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  bool VerifyOnly; // no diagnostics, no structure building
175c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl  bool AllowBraceElision;
176a789416a497b2e14e7c2fd721d03e541120bac8dBenjamin Kramer  llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic;
177c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *FullyStructuredList;
1781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1798ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  void CheckImplicitInitList(const InitializedEntity &Entity,
180987dc6a3fa0917de269c9d60b7d4f21c68b8b09fAnders Carlsson                             InitListExpr *ParentIList, QualType T,
1819e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             unsigned &Index, InitListExpr *StructuredList,
182629f118e6928f69178fab8fad6ff755b33a69f42Eli Friedman                             unsigned &StructuredIndex);
1838ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  void CheckExplicitInitList(const InitializedEntity &Entity,
18446f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                             InitListExpr *IList, QualType &T,
1859e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             unsigned &Index, InitListExpr *StructuredList,
186eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
187eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
1888ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  void CheckListElementTypes(const InitializedEntity &Entity,
18946f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                             InitListExpr *IList, QualType &DeclType,
1901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             bool SubobjectIsDesignatorContext,
191c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             unsigned &Index,
1929e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             InitListExpr *StructuredList,
193eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
194eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
1958ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  void CheckSubElementType(const InitializedEntity &Entity,
19646f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                           InitListExpr *IList, QualType ElemType,
197c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                           unsigned &Index,
1989e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                           InitListExpr *StructuredList,
1999e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                           unsigned &StructuredIndex);
2000c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  void CheckComplexType(const InitializedEntity &Entity,
2010c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                        InitListExpr *IList, QualType DeclType,
2020c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                        unsigned &Index,
2030c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                        InitListExpr *StructuredList,
2040c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                        unsigned &StructuredIndex);
2058ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  void CheckScalarType(const InitializedEntity &Entity,
20646f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                       InitListExpr *IList, QualType DeclType,
207c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                       unsigned &Index,
2089e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       InitListExpr *StructuredList,
2099e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       unsigned &StructuredIndex);
2108ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  void CheckReferenceType(const InitializedEntity &Entity,
2118ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson                          InitListExpr *IList, QualType DeclType,
212930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          unsigned &Index,
213930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          InitListExpr *StructuredList,
214930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                          unsigned &StructuredIndex);
2158ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  void CheckVectorType(const InitializedEntity &Entity,
21646f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                       InitListExpr *IList, QualType DeclType, unsigned &Index,
2179e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       InitListExpr *StructuredList,
2189e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                       unsigned &StructuredIndex);
2198ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  void CheckStructUnionTypes(const InitializedEntity &Entity,
2202bbae5de98f486d03e10c039668182075b5569ddAnders Carlsson                             InitListExpr *IList, QualType DeclType,
2211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                             RecordDecl::field_iterator Field,
222c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                             bool SubobjectIsDesignatorContext, unsigned &Index,
2239e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                             InitListExpr *StructuredList,
224eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             unsigned &StructuredIndex,
225eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                             bool TopLevelObject = false);
2268ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  void CheckArrayType(const InitializedEntity &Entity,
227784f69940755dd66cf244dd84f57a57d358e5c43Anders Carlsson                      InitListExpr *IList, QualType &DeclType,
2281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                      llvm::APSInt elementIndex,
229c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                      bool SubobjectIsDesignatorContext, unsigned &Index,
2309e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                      InitListExpr *StructuredList,
2319e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                      unsigned &StructuredIndex);
2328ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  bool CheckDesignatedInitializer(const InitializedEntity &Entity,
2339a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson                                  InitListExpr *IList, DesignatedInitExpr *DIE,
234711997184366d584c9c437102cae1e9d9927b986Douglas Gregor                                  unsigned DesigIdx,
2351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                  QualType &CurrentObjectType,
236c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  RecordDecl::field_iterator *NextField,
237c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  llvm::APSInt *NextElementIndex,
238c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  unsigned &Index,
239c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  InitListExpr *StructuredList,
240c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                  unsigned &StructuredIndex,
241eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                  bool FinishSubobjectInit,
242eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                  bool TopLevelObject);
243c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
244c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           QualType CurrentObjectType,
245c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           InitListExpr *StructuredList,
246c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           unsigned StructuredIndex,
247c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                           SourceRange InitRange);
2489e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor  void UpdateStructuredListElement(InitListExpr *StructuredList,
2499e80f7252ec1b91142e41790e4491c61e14b9472Douglas Gregor                                   unsigned &StructuredIndex,
250c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor                                   Expr *expr);
251c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  int numArrayElements(QualType DeclType);
252c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  int numStructUnionElements(QualType DeclType);
253930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
254d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor  void FillInValueInitForField(unsigned Init, FieldDecl *Field,
255d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                               const InitializedEntity &ParentEntity,
256d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                               InitListExpr *ILE, bool &RequiresSecondPass);
257cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  void FillInValueInitializations(const InitializedEntity &Entity,
258cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                  InitListExpr *ILE, bool &RequiresSecondPass);
259f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman  bool CheckFlexibleArrayInit(const InitializedEntity &Entity,
260f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman                              Expr *InitExpr, FieldDecl *Field,
261f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman                              bool TopLevelObject);
2623ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  void CheckValueInitializable(const InitializedEntity &Entity);
2633ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl
264c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregorpublic:
265cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  InitListChecker(Sema &S, const InitializedEntity &Entity,
266c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl                  InitListExpr *IL, QualType &T, bool VerifyOnly,
267c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl                  bool AllowBraceElision);
268c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  bool HadError() { return hadError; }
269c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
270c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  // @brief Retrieves the fully-structured initializer list used for
271c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  // semantic analysis and code generation.
272c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor  InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
273c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor};
2748b419b9b5f21cf948cf6fe788f67bf1efd97524cChris Lattner} // end anonymous namespace
27568355a57bb9d5daccd3fc73e92370ba2b1a6eafbChris Lattner
2763ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redlvoid InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) {
2773ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  assert(VerifyOnly &&
2783ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl         "CheckValueInitializable is only inteded for verification mode.");
2793ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl
2803ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  SourceLocation Loc;
2813ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
2823ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl                                                            true);
2833ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  InitializationSequence InitSeq(SemaRef, Entity, Kind, 0, 0);
2843ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  if (InitSeq.Failed())
2853ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    hadError = true;
2863ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl}
2873ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl
288d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregorvoid InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field,
289d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                                        const InitializedEntity &ParentEntity,
290dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                              InitListExpr *ILE,
291d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                                              bool &RequiresSecondPass) {
29296a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar  SourceLocation Loc = ILE->getLocStart();
293d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor  unsigned NumInits = ILE->getNumInits();
294dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  InitializedEntity MemberEntity
295d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    = InitializedEntity::InitializeMember(Field, &ParentEntity);
296d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor  if (Init >= NumInits || !ILE->getInit(Init)) {
297d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    // FIXME: We probably don't need to handle references
298d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    // specially here, since value-initialization of references is
299d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    // handled in InitializationSequence.
300d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    if (Field->getType()->isReferenceType()) {
301d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // C++ [dcl.init.aggr]p9:
302d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      //   If an incomplete or empty initializer-list leaves a
303d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      //   member of reference type uninitialized, the program is
304d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      //   ill-formed.
305d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
306d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        << Field->getType()
307d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        << ILE->getSyntacticForm()->getSourceRange();
308d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      SemaRef.Diag(Field->getLocation(),
309d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                   diag::note_uninit_reference_member);
310d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      hadError = true;
311d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      return;
312d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    }
313dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
314d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
315d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                                                              true);
316d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, 0, 0);
317d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    if (!InitSeq) {
318d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      InitSeq.Diagnose(SemaRef, MemberEntity, Kind, 0, 0);
319d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      hadError = true;
320d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      return;
321d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    }
322dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32360d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult MemberInit
324f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      = InitSeq.Perform(SemaRef, MemberEntity, Kind, MultiExprArg());
325d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    if (MemberInit.isInvalid()) {
326d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      hadError = true;
327d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      return;
328d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    }
329dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
330d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    if (hadError) {
331d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // Do nothing
332d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    } else if (Init < NumInits) {
333d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      ILE->setInit(Init, MemberInit.takeAs<Expr>());
3347491c499e826682e128a400038361ebcbde30eecSebastian Redl    } else if (InitSeq.isConstructorInitialization()) {
335d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // Value-initialization requires a constructor call, so
336d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // extend the initializer list to include the constructor
337d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // call and make a note that we'll need to take another pass
338d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      // through the initializer list.
339709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek      ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>());
340d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      RequiresSecondPass = true;
341d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    }
342d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor  } else if (InitListExpr *InnerILE
343d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor               = dyn_cast<InitListExpr>(ILE->getInit(Init)))
344dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    FillInValueInitializations(MemberEntity, InnerILE,
345dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                               RequiresSecondPass);
346d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor}
347d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor
3484c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// Recursively replaces NULL values within the given initializer list
3494c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// with expressions that perform value-initialization of the
3504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// appropriate type.
351dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumivoid
352cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas GregorInitListChecker::FillInValueInitializations(const InitializedEntity &Entity,
353cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                            InitListExpr *ILE,
354cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                            bool &RequiresSecondPass) {
3551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert((ILE->getType() != SemaRef.Context.VoidTy) &&
356930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor         "Should not have void type");
35796a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar  SourceLocation Loc = ILE->getLocStart();
35887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  if (ILE->getSyntacticForm())
35996a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    Loc = ILE->getSyntacticForm()->getLocStart();
3601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3616217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
362d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    if (RType->getDecl()->isUnion() &&
363d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        ILE->getInitializedFieldInUnion())
364d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      FillInValueInitForField(0, ILE->getInitializedFieldInUnion(),
365d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor                              Entity, ILE, RequiresSecondPass);
366d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor    else {
367d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      unsigned Init = 0;
368d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      for (RecordDecl::field_iterator
369d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor             Field = RType->getDecl()->field_begin(),
370d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor             FieldEnd = RType->getDecl()->field_end();
371d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor           Field != FieldEnd; ++Field) {
372d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        if (Field->isUnnamedBitfield())
373d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor          continue;
374d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor
375d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        if (hadError)
37687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor          return;
37787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
378581deb3da481053c4993c7600f97acf7768caac5David Blaikie        FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass);
379d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        if (hadError)
380cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor          return;
381cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
382d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        ++Init;
383d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor
384d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        // Only look at the first initialization of a union.
385d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor        if (RType->getDecl()->isUnion())
386d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor          break;
387d6d37dee48cfc5bbcc998bd9d151e4fb3a9437e8Douglas Gregor      }
3884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    }
3894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
3904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return;
3911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
3924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
3934c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  QualType ElementType;
3941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
395cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  InitializedEntity ElementEntity = Entity;
39687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned NumInits = ILE->getNumInits();
39787fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned NumElements = NumInits;
3980820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
3994c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = AType->getElementType();
40087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
40187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      NumElements = CAType->getSize().getZExtValue();
402dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
403cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                                         0, Entity);
404183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) {
4054c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = VType->getElementType();
40687fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor    NumElements = VType->getNumElements();
407dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
408cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                                         0, Entity);
4091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  } else
4104c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ElementType = ILE->getType();
4111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
412dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
41387fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  for (unsigned Init = 0; Init != NumElements; ++Init) {
41416006c901315fa12a108b4e571f187f4b676e426Douglas Gregor    if (hadError)
41516006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      return;
41616006c901315fa12a108b4e571f187f4b676e426Douglas Gregor
417d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson    if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement ||
418d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson        ElementEntity.getKind() == InitializedEntity::EK_VectorElement)
419cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      ElementEntity.setElementIndex(Init);
420cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
42121f77cd0c3da8a1dbaf6245cae43baf4c0b80ea4Argyrios Kyrtzidis    Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0);
42221f77cd0c3da8a1dbaf6245cae43baf4c0b80ea4Argyrios Kyrtzidis    if (!InitExpr && !ILE->hasArrayFiller()) {
423cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
424cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                                                true);
425cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, 0, 0);
426cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      if (!InitSeq) {
427cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        InitSeq.Diagnose(SemaRef, ElementEntity, Kind, 0, 0);
42887fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        hadError = true;
42987fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor        return;
43087fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor      }
43187fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor
43260d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult ElementInit
433f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall        = InitSeq.Perform(SemaRef, ElementEntity, Kind, MultiExprArg());
434cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      if (ElementInit.isInvalid()) {
43516006c901315fa12a108b4e571f187f4b676e426Douglas Gregor        hadError = true;
436cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        return;
437cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      }
438cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor
439cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      if (hadError) {
440cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor        // Do nothing
441cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      } else if (Init < NumInits) {
4423e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis        // For arrays, just set the expression used for value-initialization
4433e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis        // of the "holes" in the array.
4443e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis        if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement)
4453e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis          ILE->setArrayFiller(ElementInit.takeAs<Expr>());
4463e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis        else
4473e8dc2ac8926dfbebd8f2f6b74ceba4befccd4d2Argyrios Kyrtzidis          ILE->setInit(Init, ElementInit.takeAs<Expr>());
4484423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis      } else {
4494423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis        // For arrays, just set the expression used for value-initialization
4504423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis        // of the rest of elements and exit.
4514423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis        if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) {
4524423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis          ILE->setArrayFiller(ElementInit.takeAs<Expr>());
4534423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis          return;
4544423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis        }
4554423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis
4567491c499e826682e128a400038361ebcbde30eecSebastian Redl        if (InitSeq.isConstructorInitialization()) {
4574423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis          // Value-initialization requires a constructor call, so
4584423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis          // extend the initializer list to include the constructor
4594423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis          // call and make a note that we'll need to take another pass
4604423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis          // through the initializer list.
4614423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis          ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>());
4624423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis          RequiresSecondPass = true;
4634423ac0282acb8ba801eb05b38712438dc0c1e3eArgyrios Kyrtzidis        }
464cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      }
465ac5fc7c6bcb494b60fee7ce615ac931c5db6135eMike Stump    } else if (InitListExpr *InnerILE
46621f77cd0c3da8a1dbaf6245cae43baf4c0b80ea4Argyrios Kyrtzidis                 = dyn_cast_or_null<InitListExpr>(InitExpr))
467cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor      FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass);
4684c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
4694c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
4704c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
47168355a57bb9d5daccd3fc73e92370ba2b1a6eafbChris Lattner
472cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas GregorInitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity,
47314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                 InitListExpr *IL, QualType &T,
474c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl                                 bool VerifyOnly, bool AllowBraceElision)
475b6f8d28411955bc5349217a657ad5f274f72293aRichard Smith  : SemaRef(S), VerifyOnly(VerifyOnly), AllowBraceElision(AllowBraceElision) {
4760cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  hadError = false;
477c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
478b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  unsigned newIndex = 0;
4794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  unsigned newStructuredIndex = 0;
4801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  FullyStructuredList
481ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor    = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange());
482dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  CheckExplicitInitList(Entity, IL, T, newIndex,
48346f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                        FullyStructuredList, newStructuredIndex,
484eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                        /*TopLevelObject=*/true);
485c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
48614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (!hadError && !VerifyOnly) {
487cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    bool RequiresSecondPass = false;
488cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor    FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass);
48916006c901315fa12a108b4e571f187f4b676e426Douglas Gregor    if (RequiresSecondPass && !hadError)
490dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      FillInValueInitializations(Entity, FullyStructuredList,
491cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                 RequiresSecondPass);
492cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor  }
4930cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
4940cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
4950cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffint InitListChecker::numArrayElements(QualType DeclType) {
496638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  // FIXME: use a proper constant
497638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  int maxElements = 0x7FFFFFFF;
498c63a1f276f7b324fd9a4be82098b1c8f7bf30733Chris Lattner  if (const ConstantArrayType *CAT =
4990820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Context.getAsConstantArrayType(DeclType)) {
5000cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = static_cast<int>(CAT->getSize().getZExtValue());
5010cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
5020cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  return maxElements;
5030cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
5040cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
5050cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroffint InitListChecker::numStructUnionElements(QualType DeclType) {
5066217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
5074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  int InitializableMembers = 0;
5081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  for (RecordDecl::field_iterator
50917945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         Field = structDecl->field_begin(),
51017945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis         FieldEnd = structDecl->field_end();
5114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor       Field != FieldEnd; ++Field) {
512d61db33331c264d6361283602b248a7423040597Douglas Gregor    if (!Field->isUnnamedBitfield())
5134c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++InitializableMembers;
5144c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
51539ba4aeca296b1c9f04bde7d9d3cbbf129f1abd3Argyrios Kyrtzidis  if (structDecl->isUnion())
516f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedman    return std::min(InitializableMembers, 1);
517f84eda37251c679e2f20343c47a4a3586d9a8e21Eli Friedman  return InitializableMembers - structDecl->hasFlexibleArrayMember();
5180cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
5190cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
5208ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlssonvoid InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity,
521987dc6a3fa0917de269c9d60b7d4f21c68b8b09fAnders Carlsson                                            InitListExpr *ParentIList,
5224c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            QualType T, unsigned &Index,
5234c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
524629f118e6928f69178fab8fad6ff755b33a69f42Eli Friedman                                            unsigned &StructuredIndex) {
5250cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  int maxElements = 0;
5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5270cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (T->isArrayType())
5280cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = numArrayElements(T);
529fb87b89fc9eb103e19fb8e4b925c23f0bd091b99Douglas Gregor  else if (T->isRecordType())
5300cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    maxElements = numStructUnionElements(T);
531b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  else if (T->isVectorType())
532183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall    maxElements = T->getAs<VectorType>()->getNumElements();
5330cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  else
534b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie    llvm_unreachable("CheckImplicitInitList(): Illegal type");
535b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman
536402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman  if (maxElements == 0) {
53714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly)
53814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
53914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                   diag::err_implicit_empty_initializer);
5404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
541402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman    hadError = true;
542402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman    return;
543402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman  }
544402256fc665ba179873ffcb4d630e28cbea42f27Eli Friedman
5454c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Build a structured initializer list corresponding to this subobject.
5464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  InitListExpr *StructuredSubobjectInitList
5471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                 StructuredIndex,
54996a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar          SourceRange(ParentIList->getInit(Index)->getLocStart(),
550ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                      ParentIList->getSourceRange().getEnd()));
5514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  unsigned StructuredSubobjectInitIndex = 0;
5524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
5534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Check the element types and build the structural subobject.
55487fd703e097c27d63479cb83b687d4000a22bbb1Douglas Gregor  unsigned StartIndex = Index;
555dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  CheckListElementTypes(Entity, ParentIList, T,
556987dc6a3fa0917de269c9d60b7d4f21c68b8b09fAnders Carlsson                        /*SubobjectIsDesignatorContext=*/false, Index,
5571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                        StructuredSubobjectInitList,
558629f118e6928f69178fab8fad6ff755b33a69f42Eli Friedman                        StructuredSubobjectInitIndex);
559c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl
560c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl  if (VerifyOnly) {
561c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl    if (!AllowBraceElision && (T->isArrayType() || T->isRecordType()))
562c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl      hadError = true;
563c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl  } else {
56414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    StructuredSubobjectInitList->setType(T);
56514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
566c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl    unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
56714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    // Update the structured sub-object initializer so that it's ending
56814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    // range corresponds with the end of the last initializer it used.
56914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (EndIndex < ParentIList->getNumInits()) {
57014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SourceLocation EndLoc
57114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
57214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      StructuredSubobjectInitList->setRBraceLoc(EndLoc);
57314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    }
57414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
575c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl    // Complain about missing braces.
57614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (T->isArrayType() || T->isRecordType()) {
57714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.Diag(StructuredSubobjectInitList->getLocStart(),
578c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl                    AllowBraceElision ? diag::warn_missing_braces :
579c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl                                        diag::err_missing_braces)
58014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << StructuredSubobjectInitList->getSourceRange()
58114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << FixItHint::CreateInsertion(
58214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl              StructuredSubobjectInitList->getLocStart(), "{")
58314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << FixItHint::CreateInsertion(
58414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl              SemaRef.PP.getLocForEndOfToken(
585dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                      StructuredSubobjectInitList->getLocEnd()),
58614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl              "}");
587c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl      if (!AllowBraceElision)
588c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl        hadError = true;
58914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    }
5901e1d396dc55002a2a6355048db542a38b5137002Tanya Lattner  }
5910cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
5920cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
5938ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlssonvoid InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity,
59446f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                                            InitListExpr *IList, QualType &T,
5954c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
5964c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
597eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
598eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
599c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  assert(IList->isExplicit() && "Illegal Implicit InitListExpr");
60014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (!VerifyOnly) {
60114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    SyntacticToSemantic[IList] = StructuredList;
60214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    StructuredList->setSyntacticForm(IList);
60314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
604dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true,
60546f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                        Index, StructuredList, StructuredIndex, TopLevelObject);
60614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (!VerifyOnly) {
6075c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman    QualType ExprTy = T;
6085c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman    if (!ExprTy->isArrayType())
6095c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman      ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context);
61014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    IList->setType(ExprTy);
61114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    StructuredList->setType(ExprTy);
61214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
613638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  if (hadError)
614638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    return;
615c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman
616638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  if (Index < IList->getNumInits()) {
617c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    // We have leftover initializers
61814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (VerifyOnly) {
6194e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (SemaRef.getLangOpts().CPlusPlus ||
6204e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          (SemaRef.getLangOpts().OpenCL &&
62114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl           IList->getType()->isVectorType())) {
62214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        hadError = true;
62314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      }
62414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      return;
62514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    }
62614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
627e540858b289b23653bcb23646f135729203635cbEli Friedman    if (StructuredIndex == 1 &&
628e540858b289b23653bcb23646f135729203635cbEli Friedman        IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) {
6297c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor      unsigned DK = diag::warn_excess_initializers_in_char_array_initializer;
6304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (SemaRef.getLangOpts().CPlusPlus) {
6317c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor        DK = diag::err_excess_initializers_in_char_array_initializer;
632e540858b289b23653bcb23646f135729203635cbEli Friedman        hadError = true;
633e540858b289b23653bcb23646f135729203635cbEli Friedman      }
634bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman      // Special-case
6350820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
636dcd5ef12488e4c7ea844327835896ca86b609a97Chris Lattner        << IList->getInit(Index)->getSourceRange();
637d8dc2100487640d8f5ce53201fdcfac7b5ca32b2Eli Friedman    } else if (!T->isIncompleteType()) {
638b574e5630d66629ccc8f2432e60b59ae42f1f363Douglas Gregor      // Don't complain for incomplete types, since we'll get an error
639b574e5630d66629ccc8f2432e60b59ae42f1f363Douglas Gregor      // elsewhere
640eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      QualType CurrentObjectType = StructuredList->getType();
6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      int initKind =
642eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isArrayType()? 0 :
643eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isVectorType()? 1 :
644eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isScalarType()? 2 :
645eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        CurrentObjectType->isUnionType()? 3 :
646eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        4;
6477c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor
6487c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor      unsigned DK = diag::warn_excess_initializers;
6494e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (SemaRef.getLangOpts().CPlusPlus) {
650e540858b289b23653bcb23646f135729203635cbEli Friedman        DK = diag::err_excess_initializers;
6510863452c21e3e6da5b2613fd455495486c1ad5dfNate Begeman        hadError = true;
6520863452c21e3e6da5b2613fd455495486c1ad5dfNate Begeman      }
6534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (SemaRef.getLangOpts().OpenCL && initKind == 1) {
6540863452c21e3e6da5b2613fd455495486c1ad5dfNate Begeman        DK = diag::err_excess_initializers;
655e540858b289b23653bcb23646f135729203635cbEli Friedman        hadError = true;
656e540858b289b23653bcb23646f135729203635cbEli Friedman      }
6577c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor
6580820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
659eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        << initKind << IList->getInit(Index)->getSourceRange();
660c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman    }
661c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  }
662cda25a977e4b7fe4e080b87586410eaeab7b62f6Eli Friedman
66314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 &&
66414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      !TopLevelObject)
6650820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner    SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
666a3a835149ed4b183e3b009a1f94a6123779d696bDouglas Gregor      << IList->getSourceRange()
667849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateRemoval(IList->getLocStart())
668849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor      << FixItHint::CreateRemoval(IList->getLocEnd());
6690cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
6700cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
6718ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlssonvoid InitListChecker::CheckListElementTypes(const InitializedEntity &Entity,
67246f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                                            InitListExpr *IList,
6731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            QualType &DeclType,
67487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                            bool SubobjectIsDesignatorContext,
6754c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
6764c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
677eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
678eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
6790c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) {
6800c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    // Explicitly braced initializer for complex type can be real+imaginary
6810c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    // parts.
6820c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    CheckComplexType(Entity, IList, DeclType, Index,
6830c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                     StructuredList, StructuredIndex);
6840c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  } else if (DeclType->isScalarType()) {
68546f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson    CheckScalarType(Entity, IList, DeclType, Index,
68646f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                    StructuredList, StructuredIndex);
687c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  } else if (DeclType->isVectorType()) {
688dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    CheckVectorType(Entity, IList, DeclType, Index,
68946f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                    StructuredList, StructuredIndex);
69020599392a99956eaac4cf351a0935574090cb6c3Richard Smith  } else if (DeclType->isRecordType()) {
69120599392a99956eaac4cf351a0935574090cb6c3Richard Smith    assert(DeclType->isAggregateType() &&
69220599392a99956eaac4cf351a0935574090cb6c3Richard Smith           "non-aggregate records should be handed in CheckSubElementType");
69320599392a99956eaac4cf351a0935574090cb6c3Richard Smith    RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
69420599392a99956eaac4cf351a0935574090cb6c3Richard Smith    CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(),
69520599392a99956eaac4cf351a0935574090cb6c3Richard Smith                          SubobjectIsDesignatorContext, Index,
69620599392a99956eaac4cf351a0935574090cb6c3Richard Smith                          StructuredList, StructuredIndex,
69720599392a99956eaac4cf351a0935574090cb6c3Richard Smith                          TopLevelObject);
69820599392a99956eaac4cf351a0935574090cb6c3Richard Smith  } else if (DeclType->isArrayType()) {
69920599392a99956eaac4cf351a0935574090cb6c3Richard Smith    llvm::APSInt Zero(
70020599392a99956eaac4cf351a0935574090cb6c3Richard Smith                    SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
70120599392a99956eaac4cf351a0935574090cb6c3Richard Smith                    false);
70220599392a99956eaac4cf351a0935574090cb6c3Richard Smith    CheckArrayType(Entity, IList, DeclType, Zero,
70320599392a99956eaac4cf351a0935574090cb6c3Richard Smith                   SubobjectIsDesignatorContext, Index,
70420599392a99956eaac4cf351a0935574090cb6c3Richard Smith                   StructuredList, StructuredIndex);
705613535273b90dc5cbd0f9fa056dedc93801ea35aSteve Naroff  } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
706613535273b90dc5cbd0f9fa056dedc93801ea35aSteve Naroff    // This type is invalid, issue a diagnostic.
7074c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
70814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly)
70914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
71014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << DeclType;
711d8dc2100487640d8f5ce53201fdcfac7b5ca32b2Eli Friedman    hadError = true;
712930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (DeclType->isReferenceType()) {
7138ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson    CheckReferenceType(Entity, IList, DeclType, Index,
7148ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson                       StructuredList, StructuredIndex);
715c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  } else if (DeclType->isObjCObjectType()) {
71614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly)
71714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class)
71814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << DeclType;
7194d9e7388ccdfdab97f92caac5db9b87a530742f4Douglas Gregor    hadError = true;
7200cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  } else {
72114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly)
72214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
72314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << DeclType;
7244d9e7388ccdfdab97f92caac5db9b87a530742f4Douglas Gregor    hadError = true;
7250cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
7260cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
7270cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
7288ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlssonvoid InitListChecker::CheckSubElementType(const InitializedEntity &Entity,
72946f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                                          InitListExpr *IList,
7301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                          QualType ElemType,
7314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          unsigned &Index,
7324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          InitListExpr *StructuredList,
7334c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                          unsigned &StructuredIndex) {
7346fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor  Expr *expr = IList->getInit(Index);
735c9c0ea6576666eb7e96508f6b8ce2b4d33af3f02Eli Friedman  if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
73620599392a99956eaac4cf351a0935574090cb6c3Richard Smith    if (!ElemType->isRecordType() || ElemType->isAggregateType()) {
73720599392a99956eaac4cf351a0935574090cb6c3Richard Smith      unsigned newIndex = 0;
73820599392a99956eaac4cf351a0935574090cb6c3Richard Smith      unsigned newStructuredIndex = 0;
73920599392a99956eaac4cf351a0935574090cb6c3Richard Smith      InitListExpr *newStructuredList
74020599392a99956eaac4cf351a0935574090cb6c3Richard Smith        = getStructuredSubobjectInit(IList, Index, ElemType,
74120599392a99956eaac4cf351a0935574090cb6c3Richard Smith                                     StructuredList, StructuredIndex,
74220599392a99956eaac4cf351a0935574090cb6c3Richard Smith                                     SubInitList->getSourceRange());
74320599392a99956eaac4cf351a0935574090cb6c3Richard Smith      CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex,
74420599392a99956eaac4cf351a0935574090cb6c3Richard Smith                            newStructuredList, newStructuredIndex);
74520599392a99956eaac4cf351a0935574090cb6c3Richard Smith      ++StructuredIndex;
74620599392a99956eaac4cf351a0935574090cb6c3Richard Smith      ++Index;
74720599392a99956eaac4cf351a0935574090cb6c3Richard Smith      return;
74820599392a99956eaac4cf351a0935574090cb6c3Richard Smith    }
74920599392a99956eaac4cf351a0935574090cb6c3Richard Smith    assert(SemaRef.getLangOpts().CPlusPlus &&
75020599392a99956eaac4cf351a0935574090cb6c3Richard Smith           "non-aggregate records are only possible in C++");
75120599392a99956eaac4cf351a0935574090cb6c3Richard Smith    // C++ initialization is handled later.
75220599392a99956eaac4cf351a0935574090cb6c3Richard Smith  }
75320599392a99956eaac4cf351a0935574090cb6c3Richard Smith
75420599392a99956eaac4cf351a0935574090cb6c3Richard Smith  if (ElemType->isScalarType()) {
755fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    return CheckScalarType(Entity, IList, ElemType, Index,
756fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall                           StructuredList, StructuredIndex);
757930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  } else if (ElemType->isReferenceType()) {
758fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    return CheckReferenceType(Entity, IList, ElemType, Index,
759fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall                              StructuredList, StructuredIndex);
760fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  }
761930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
762fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) {
763fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    // arrayType can be incomplete if we're initializing a flexible
764fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    // array member.  There's nothing we can do with the completed
765fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    // type here, though.
766930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
767fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    if (Expr *Str = IsStringInit(expr, arrayType, SemaRef.Context)) {
7688a5d929268beeb69b654120fb0b4f8893ad230ebEli Friedman      if (!VerifyOnly) {
7698a5d929268beeb69b654120fb0b4f8893ad230ebEli Friedman        CheckStringInit(Str, ElemType, arrayType, SemaRef);
7708a5d929268beeb69b654120fb0b4f8893ad230ebEli Friedman        UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
7718a5d929268beeb69b654120fb0b4f8893ad230ebEli Friedman      }
772fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall      ++Index;
773fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall      return;
774930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
775930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
776fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    // Fall through for subaggregate initialization.
777fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall
7784e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  } else if (SemaRef.getLangOpts().CPlusPlus) {
779930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    // C++ [dcl.init.aggr]p12:
780fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    //   All implicit type conversions (clause 4) are considered when
7815d3d41d0873d51b405972baf38e1f3a7ef5b49e0Sebastian Redl    //   initializing the aggregate member with an initializer from
782fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    //   an initializer-list. If the initializer can initialize a
783fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    //   member, the member is initialized. [...]
784fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall
785fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    // FIXME: Better EqualLoc?
786fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    InitializationKind Kind =
787fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall      InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation());
788fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    InitializationSequence Seq(SemaRef, Entity, Kind, &expr, 1);
789fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall
790fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    if (Seq) {
79114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      if (!VerifyOnly) {
792b6f8d28411955bc5349217a657ad5f274f72293aRichard Smith        ExprResult Result =
793b6f8d28411955bc5349217a657ad5f274f72293aRichard Smith          Seq.Perform(SemaRef, Entity, Kind, MultiExprArg(&expr, 1));
794b6f8d28411955bc5349217a657ad5f274f72293aRichard Smith        if (Result.isInvalid())
795b6f8d28411955bc5349217a657ad5f274f72293aRichard Smith          hadError = true;
79614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
79714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        UpdateStructuredListElement(StructuredList, StructuredIndex,
798b6f8d28411955bc5349217a657ad5f274f72293aRichard Smith                                    Result.takeAs<Expr>());
79914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      }
800fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall      ++Index;
801fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall      return;
802fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    }
803fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall
804fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    // Fall through for subaggregate initialization
805fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  } else {
806fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    // C99 6.7.8p13:
8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
808fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    //   The initializer for a structure or union object that has
809fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    //   automatic storage duration shall be either an initializer
810fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    //   list as described below, or a single expression that has
811fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    //   compatible structure or union type. In the latter case, the
812fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    //   initial value of the object, including unnamed members, is
813fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    //   that of the expression.
814429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprResult ExprRes = SemaRef.Owned(expr);
815fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
81614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes,
81714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                                 !VerifyOnly)
818fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall          == Sema::Compatible) {
819429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (ExprRes.isInvalid())
820429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        hadError = true;
821429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      else {
822429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take());
823429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley	      if (ExprRes.isInvalid())
824429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley	        hadError = true;
825429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      }
826429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      UpdateStructuredListElement(StructuredList, StructuredIndex,
827429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                  ExprRes.takeAs<Expr>());
828930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor      ++Index;
829fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall      return;
830930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    }
831429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    ExprRes.release();
832fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    // Fall through for subaggregate initialization
833fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  }
834fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall
835fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  // C++ [dcl.init.aggr]p12:
836fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  //
837fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  //   [...] Otherwise, if the member is itself a non-empty
838fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  //   subaggregate, brace elision is assumed and the initializer is
839fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  //   considered for the initialization of the first member of
840fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  //   the subaggregate.
8414e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!SemaRef.getLangOpts().OpenCL &&
84261b4bc80e943578ae855810918a1dc9697dbd15bTanya Lattner      (ElemType->isAggregateType() || ElemType->isVectorType())) {
843fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList,
844fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall                          StructuredIndex);
845fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    ++StructuredIndex;
846fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall  } else {
84714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly) {
84814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      // We cannot initialize this element, so let
84914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      // PerformCopyInitialization produce the appropriate diagnostic.
85014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.PerformCopyInitialization(Entity, SourceLocation(),
85114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                        SemaRef.Owned(expr),
85214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                        /*TopLevelOfInitList=*/true);
85314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    }
854fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    hadError = true;
855fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    ++Index;
856fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall    ++StructuredIndex;
857930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
858b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman}
859b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman
8600c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedmanvoid InitListChecker::CheckComplexType(const InitializedEntity &Entity,
8610c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                                       InitListExpr *IList, QualType DeclType,
8620c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                                       unsigned &Index,
8630c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                                       InitListExpr *StructuredList,
8640c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                                       unsigned &StructuredIndex) {
8650c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  assert(Index == 0 && "Index in explicit init list must be zero");
8660c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman
8670c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  // As an extension, clang supports complex initializers, which initialize
8680c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  // a complex number component-wise.  When an explicit initializer list for
8690c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  // a complex number contains two two initializers, this extension kicks in:
8700c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  // it exepcts the initializer list to contain two elements convertible to
8710c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  // the element type of the complex type. The first element initializes
8720c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  // the real part, and the second element intitializes the imaginary part.
8730c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman
8740c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  if (IList->getNumInits() != 2)
8750c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    return CheckScalarType(Entity, IList, DeclType, Index, StructuredList,
8760c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                           StructuredIndex);
8770c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman
8780c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  // This is an extension in C.  (The builtin _Complex type does not exist
8790c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  // in the C++ standard.)
8804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly)
8810c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init)
8820c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman      << IList->getSourceRange();
8830c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman
8840c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  // Initialize the complex number.
8850c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  QualType elementType = DeclType->getAs<ComplexType>()->getElementType();
8860c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  InitializedEntity ElementEntity =
8870c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
8880c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman
8890c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  for (unsigned i = 0; i < 2; ++i) {
8900c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    ElementEntity.setElementIndex(Index);
8910c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    CheckSubElementType(ElementEntity, IList, elementType, Index,
8920c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman                        StructuredList, StructuredIndex);
8930c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  }
8940c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman}
8950c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman
8960c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman
8978ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlssonvoid InitListChecker::CheckScalarType(const InitializedEntity &Entity,
89846f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                                      InitListExpr *IList, QualType DeclType,
8996fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor                                      unsigned &Index,
9004c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
9014c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &StructuredIndex) {
902b934c2d77144471f35c5ebae9e925b056dac8112John McCall  if (Index >= IList->getNumInits()) {
9036b13022faef260c8f49d04310f4a2c0a57f9103bRichard Smith    if (!VerifyOnly)
9046b13022faef260c8f49d04310f4a2c0a57f9103bRichard Smith      SemaRef.Diag(IList->getLocStart(),
9054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                   SemaRef.getLangOpts().CPlusPlus0x ?
9066b13022faef260c8f49d04310f4a2c0a57f9103bRichard Smith                     diag::warn_cxx98_compat_empty_scalar_initializer :
9076b13022faef260c8f49d04310f4a2c0a57f9103bRichard Smith                     diag::err_empty_scalar_initializer)
9086b13022faef260c8f49d04310f4a2c0a57f9103bRichard Smith        << IList->getSourceRange();
9094e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    hadError = !SemaRef.getLangOpts().CPlusPlus0x;
9104c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
9114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
912bb504d3a63e0e4b2439900ba6d77b620fb6de857Eli Friedman    return;
9130cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
914b934c2d77144471f35c5ebae9e925b056dac8112John McCall
915b934c2d77144471f35c5ebae9e925b056dac8112John McCall  Expr *expr = IList->getInit(Index);
916b934c2d77144471f35c5ebae9e925b056dac8112John McCall  if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) {
91714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly)
91814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.Diag(SubIList->getLocStart(),
91914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                   diag::warn_many_braces_around_scalar_init)
92014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << SubIList->getSourceRange();
921b934c2d77144471f35c5ebae9e925b056dac8112John McCall
922b934c2d77144471f35c5ebae9e925b056dac8112John McCall    CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList,
923b934c2d77144471f35c5ebae9e925b056dac8112John McCall                    StructuredIndex);
924b934c2d77144471f35c5ebae9e925b056dac8112John McCall    return;
925b934c2d77144471f35c5ebae9e925b056dac8112John McCall  } else if (isa<DesignatedInitExpr>(expr)) {
92614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly)
92796a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SemaRef.Diag(expr->getLocStart(),
92814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                   diag::err_designator_for_scalar_init)
92914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << DeclType << expr->getSourceRange();
930b934c2d77144471f35c5ebae9e925b056dac8112John McCall    hadError = true;
931b934c2d77144471f35c5ebae9e925b056dac8112John McCall    ++Index;
932b934c2d77144471f35c5ebae9e925b056dac8112John McCall    ++StructuredIndex;
933b934c2d77144471f35c5ebae9e925b056dac8112John McCall    return;
934b934c2d77144471f35c5ebae9e925b056dac8112John McCall  }
935b934c2d77144471f35c5ebae9e925b056dac8112John McCall
93614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (VerifyOnly) {
93714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr)))
93814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      hadError = true;
93914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    ++Index;
94014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    return;
94114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
94214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
943b934c2d77144471f35c5ebae9e925b056dac8112John McCall  ExprResult Result =
944b934c2d77144471f35c5ebae9e925b056dac8112John McCall    SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(),
945191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin                                      SemaRef.Owned(expr),
946191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin                                      /*TopLevelOfInitList=*/true);
947b934c2d77144471f35c5ebae9e925b056dac8112John McCall
948b934c2d77144471f35c5ebae9e925b056dac8112John McCall  Expr *ResultExpr = 0;
949b934c2d77144471f35c5ebae9e925b056dac8112John McCall
950b934c2d77144471f35c5ebae9e925b056dac8112John McCall  if (Result.isInvalid())
951b934c2d77144471f35c5ebae9e925b056dac8112John McCall    hadError = true; // types weren't compatible.
952b934c2d77144471f35c5ebae9e925b056dac8112John McCall  else {
953b934c2d77144471f35c5ebae9e925b056dac8112John McCall    ResultExpr = Result.takeAs<Expr>();
954dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
955b934c2d77144471f35c5ebae9e925b056dac8112John McCall    if (ResultExpr != expr) {
956b934c2d77144471f35c5ebae9e925b056dac8112John McCall      // The type was promoted, update initializer list.
957b934c2d77144471f35c5ebae9e925b056dac8112John McCall      IList->setInit(Index, ResultExpr);
958b934c2d77144471f35c5ebae9e925b056dac8112John McCall    }
959b934c2d77144471f35c5ebae9e925b056dac8112John McCall  }
960b934c2d77144471f35c5ebae9e925b056dac8112John McCall  if (hadError)
961b934c2d77144471f35c5ebae9e925b056dac8112John McCall    ++StructuredIndex;
962b934c2d77144471f35c5ebae9e925b056dac8112John McCall  else
963b934c2d77144471f35c5ebae9e925b056dac8112John McCall    UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
964b934c2d77144471f35c5ebae9e925b056dac8112John McCall  ++Index;
9650cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
9660cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
9678ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlssonvoid InitListChecker::CheckReferenceType(const InitializedEntity &Entity,
9688ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson                                         InitListExpr *IList, QualType DeclType,
969930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         unsigned &Index,
970930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         InitListExpr *StructuredList,
971930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor                                         unsigned &StructuredIndex) {
97214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (Index >= IList->getNumInits()) {
973390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // FIXME: It would be wonderful if we could point at the actual member. In
974390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // general, it would be useful to pass location information down the stack,
975390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // so that we know the location (or decl) of the "current object" being
976390b4cc8b45a05612349269ef08faab3e4688f06Mike Stump    // initialized.
97714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly)
97814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.Diag(IList->getLocStart(),
97914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                    diag::err_init_reference_member_uninitialized)
98014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << DeclType
98114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << IList->getSourceRange();
982930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    hadError = true;
983930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++Index;
984930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    ++StructuredIndex;
985930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor    return;
986930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor  }
98714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
98814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  Expr *expr = IList->getInit(Index);
9894e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus0x) {
99014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly)
99114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
99214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << DeclType << IList->getSourceRange();
99314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    hadError = true;
99414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    ++Index;
99514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    ++StructuredIndex;
99614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    return;
99714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
99814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
99914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (VerifyOnly) {
100014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr)))
100114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      hadError = true;
100214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    ++Index;
100314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    return;
100414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
100514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
100614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  ExprResult Result =
100714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(),
100814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                      SemaRef.Owned(expr),
100914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                      /*TopLevelOfInitList=*/true);
101014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
101114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (Result.isInvalid())
101214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    hadError = true;
101314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
101414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  expr = Result.takeAs<Expr>();
101514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  IList->setInit(Index, expr);
101614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
101714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (hadError)
101814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    ++StructuredIndex;
101914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  else
102014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
102114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  ++Index;
1022930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor}
1023930d8b5ecc074cca01ecd9a522a55f55f3b72396Douglas Gregor
10248ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlssonvoid InitListChecker::CheckVectorType(const InitializedEntity &Entity,
102546f4659f9d012ca2e2050c1fc39a59666114b3f9Anders Carlsson                                      InitListExpr *IList, QualType DeclType,
10264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &Index,
10274c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      InitListExpr *StructuredList,
10284c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                      unsigned &StructuredIndex) {
102920e047abc5f19dc948436c0fb891450d9cd40667John McCall  const VectorType *VT = DeclType->getAs<VectorType>();
103020e047abc5f19dc948436c0fb891450d9cd40667John McCall  unsigned maxElements = VT->getNumElements();
103120e047abc5f19dc948436c0fb891450d9cd40667John McCall  unsigned numEltsInit = 0;
103220e047abc5f19dc948436c0fb891450d9cd40667John McCall  QualType elementType = VT->getElementType();
103320e047abc5f19dc948436c0fb891450d9cd40667John McCall
10343ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  if (Index >= IList->getNumInits()) {
10353ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    // Make sure the element type can be value-initialized.
10363ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    if (VerifyOnly)
10373ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl      CheckValueInitializable(
10383ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl          InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity));
10393ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    return;
10403ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  }
10413ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl
10424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!SemaRef.getLangOpts().OpenCL) {
104320e047abc5f19dc948436c0fb891450d9cd40667John McCall    // If the initializing element is a vector, try to copy-initialize
104420e047abc5f19dc948436c0fb891450d9cd40667John McCall    // instead of breaking it apart (which is doomed to failure anyway).
104520e047abc5f19dc948436c0fb891450d9cd40667John McCall    Expr *Init = IList->getInit(Index);
104620e047abc5f19dc948436c0fb891450d9cd40667John McCall    if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) {
104714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      if (VerifyOnly) {
104814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init)))
104914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl          hadError = true;
105014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        ++Index;
105114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        return;
105214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      }
105314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
105420e047abc5f19dc948436c0fb891450d9cd40667John McCall      ExprResult Result =
105520e047abc5f19dc948436c0fb891450d9cd40667John McCall        SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(),
1056191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin                                          SemaRef.Owned(Init),
1057191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin                                          /*TopLevelOfInitList=*/true);
105820e047abc5f19dc948436c0fb891450d9cd40667John McCall
105920e047abc5f19dc948436c0fb891450d9cd40667John McCall      Expr *ResultExpr = 0;
106020e047abc5f19dc948436c0fb891450d9cd40667John McCall      if (Result.isInvalid())
106120e047abc5f19dc948436c0fb891450d9cd40667John McCall        hadError = true; // types weren't compatible.
106220e047abc5f19dc948436c0fb891450d9cd40667John McCall      else {
106320e047abc5f19dc948436c0fb891450d9cd40667John McCall        ResultExpr = Result.takeAs<Expr>();
1064dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
106520e047abc5f19dc948436c0fb891450d9cd40667John McCall        if (ResultExpr != Init) {
106620e047abc5f19dc948436c0fb891450d9cd40667John McCall          // The type was promoted, update initializer list.
106720e047abc5f19dc948436c0fb891450d9cd40667John McCall          IList->setInit(Index, ResultExpr);
10682ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman        }
10692ef13e5abef0570a9f567b4671367275c05d4d34Nate Begeman      }
107020e047abc5f19dc948436c0fb891450d9cd40667John McCall      if (hadError)
107120e047abc5f19dc948436c0fb891450d9cd40667John McCall        ++StructuredIndex;
107220e047abc5f19dc948436c0fb891450d9cd40667John McCall      else
107314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        UpdateStructuredListElement(StructuredList, StructuredIndex,
107414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                    ResultExpr);
107520e047abc5f19dc948436c0fb891450d9cd40667John McCall      ++Index;
107620e047abc5f19dc948436c0fb891450d9cd40667John McCall      return;
10770cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
10781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
107920e047abc5f19dc948436c0fb891450d9cd40667John McCall    InitializedEntity ElementEntity =
108020e047abc5f19dc948436c0fb891450d9cd40667John McCall      InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
1081dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
108220e047abc5f19dc948436c0fb891450d9cd40667John McCall    for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
108320e047abc5f19dc948436c0fb891450d9cd40667John McCall      // Don't attempt to go past the end of the init list
10843ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl      if (Index >= IList->getNumInits()) {
10853ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl        if (VerifyOnly)
10863ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl          CheckValueInitializable(ElementEntity);
108720e047abc5f19dc948436c0fb891450d9cd40667John McCall        break;
10883ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl      }
1089dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
109020e047abc5f19dc948436c0fb891450d9cd40667John McCall      ElementEntity.setElementIndex(Index);
109120e047abc5f19dc948436c0fb891450d9cd40667John McCall      CheckSubElementType(ElementEntity, IList, elementType, Index,
109220e047abc5f19dc948436c0fb891450d9cd40667John McCall                          StructuredList, StructuredIndex);
109320e047abc5f19dc948436c0fb891450d9cd40667John McCall    }
109420e047abc5f19dc948436c0fb891450d9cd40667John McCall    return;
109520e047abc5f19dc948436c0fb891450d9cd40667John McCall  }
109620e047abc5f19dc948436c0fb891450d9cd40667John McCall
109720e047abc5f19dc948436c0fb891450d9cd40667John McCall  InitializedEntity ElementEntity =
109820e047abc5f19dc948436c0fb891450d9cd40667John McCall    InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
1099dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
110020e047abc5f19dc948436c0fb891450d9cd40667John McCall  // OpenCL initializers allows vectors to be constructed from vectors.
110120e047abc5f19dc948436c0fb891450d9cd40667John McCall  for (unsigned i = 0; i < maxElements; ++i) {
110220e047abc5f19dc948436c0fb891450d9cd40667John McCall    // Don't attempt to go past the end of the init list
110320e047abc5f19dc948436c0fb891450d9cd40667John McCall    if (Index >= IList->getNumInits())
110420e047abc5f19dc948436c0fb891450d9cd40667John McCall      break;
1105dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
110620e047abc5f19dc948436c0fb891450d9cd40667John McCall    ElementEntity.setElementIndex(Index);
110720e047abc5f19dc948436c0fb891450d9cd40667John McCall
110820e047abc5f19dc948436c0fb891450d9cd40667John McCall    QualType IType = IList->getInit(Index)->getType();
110920e047abc5f19dc948436c0fb891450d9cd40667John McCall    if (!IType->isVectorType()) {
111020e047abc5f19dc948436c0fb891450d9cd40667John McCall      CheckSubElementType(ElementEntity, IList, elementType, Index,
111120e047abc5f19dc948436c0fb891450d9cd40667John McCall                          StructuredList, StructuredIndex);
111220e047abc5f19dc948436c0fb891450d9cd40667John McCall      ++numEltsInit;
111320e047abc5f19dc948436c0fb891450d9cd40667John McCall    } else {
111420e047abc5f19dc948436c0fb891450d9cd40667John McCall      QualType VecType;
111520e047abc5f19dc948436c0fb891450d9cd40667John McCall      const VectorType *IVT = IType->getAs<VectorType>();
111620e047abc5f19dc948436c0fb891450d9cd40667John McCall      unsigned numIElts = IVT->getNumElements();
1117dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
111820e047abc5f19dc948436c0fb891450d9cd40667John McCall      if (IType->isExtVectorType())
111920e047abc5f19dc948436c0fb891450d9cd40667John McCall        VecType = SemaRef.Context.getExtVectorType(elementType, numIElts);
112020e047abc5f19dc948436c0fb891450d9cd40667John McCall      else
112120e047abc5f19dc948436c0fb891450d9cd40667John McCall        VecType = SemaRef.Context.getVectorType(elementType, numIElts,
1122e86d78cf4754a6aef2cf9a33d847aa15338e276fBob Wilson                                                IVT->getVectorKind());
112320e047abc5f19dc948436c0fb891450d9cd40667John McCall      CheckSubElementType(ElementEntity, IList, VecType, Index,
112420e047abc5f19dc948436c0fb891450d9cd40667John McCall                          StructuredList, StructuredIndex);
112520e047abc5f19dc948436c0fb891450d9cd40667John McCall      numEltsInit += numIElts;
112620e047abc5f19dc948436c0fb891450d9cd40667John McCall    }
11270cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
112820e047abc5f19dc948436c0fb891450d9cd40667John McCall
112920e047abc5f19dc948436c0fb891450d9cd40667John McCall  // OpenCL requires all elements to be initialized.
11303ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  if (numEltsInit != maxElements) {
11313ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    if (!VerifyOnly)
113296a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      SemaRef.Diag(IList->getLocStart(),
11333ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl                   diag::err_vector_incorrect_num_initializers)
11343ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl        << (numEltsInit < maxElements) << maxElements << numEltsInit;
11353ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    hadError = true;
11363ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  }
11370cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
11380cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
11398ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlssonvoid InitListChecker::CheckArrayType(const InitializedEntity &Entity,
1140784f69940755dd66cf244dd84f57a57d358e5c43Anders Carlsson                                     InitListExpr *IList, QualType &DeclType,
114187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                     llvm::APSInt elementIndex,
11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                     bool SubobjectIsDesignatorContext,
11434c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     unsigned &Index,
11444c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     InitListExpr *StructuredList,
11454c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     unsigned &StructuredIndex) {
1146ce6c9b7da219c8640a0559f75770cfc6622de083John McCall  const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType);
1147ce6c9b7da219c8640a0559f75770cfc6622de083John McCall
11480cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  // Check for the special-case of initializing an array with a string.
11490cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  if (Index < IList->getNumInits()) {
1150ce6c9b7da219c8640a0559f75770cfc6622de083John McCall    if (Expr *Str = IsStringInit(IList->getInit(Index), arrayType,
115179e079d3caecc0ddd7128dc038d3f8960bbab62eChris Lattner                                 SemaRef.Context)) {
11524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // We place the string literal directly into the resulting
11534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // initializer list. This is the only place where the structure
11544c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // of the structured initializer list doesn't match exactly,
11554c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // because doing so would involve allocating one character
11564c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // constant for each string.
115714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      if (!VerifyOnly) {
11588a5d929268beeb69b654120fb0b4f8893ad230ebEli Friedman        CheckStringInit(Str, DeclType, arrayType, SemaRef);
115914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        UpdateStructuredListElement(StructuredList, StructuredIndex, Str);
116014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
116114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      }
11620cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      ++Index;
11630cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      return;
11640cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
11650cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
1166ce6c9b7da219c8640a0559f75770cfc6622de083John McCall  if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) {
1167638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // Check for VLAs; in standard C it would be possible to check this
1168638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // earlier, but I don't know where clang accepts VLAs (gcc accepts
1169638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    // them in all sorts of strange places).
117014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly)
117114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
117214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                    diag::err_variable_object_no_init)
117314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << VAT->getSizeExpr()->getSourceRange();
1174638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    hadError = true;
11754c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++Index;
11764c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++StructuredIndex;
1177638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman    return;
1178638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman  }
1179638e14413a4557c399fa2b7da2be5e4e9c1330a2Eli Friedman
118005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // We might know the maximum number of elements in advance.
11814c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  llvm::APSInt maxElements(elementIndex.getBitWidth(),
11824c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                           elementIndex.isUnsigned());
118305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool maxElementsKnown = false;
1184ce6c9b7da219c8640a0559f75770cfc6622de083John McCall  if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) {
118505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    maxElements = CAT->getSize();
11869f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth());
1187e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor    elementIndex.setIsUnsigned(maxElements.isUnsigned());
118805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    maxElementsKnown = true;
118905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
119005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
1191ce6c9b7da219c8640a0559f75770cfc6622de083John McCall  QualType elementType = arrayType->getElementType();
119205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  while (Index < IList->getNumInits()) {
119305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    Expr *Init = IList->getInit(Index);
119405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
119587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If we're not the subobject that matches up with the '{' for
119687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the designator, we shouldn't be handling the
119787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // designator. Return immediately.
119887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!SubobjectIsDesignatorContext)
119987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        return;
120087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
120187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // Handle this designated initializer. elementIndex will be
120287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // updated to be the next array element we'll initialize.
12039a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson      if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
12044c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     DeclType, 0, &elementIndex, Index,
1205eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     StructuredList, StructuredIndex, true,
1206eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     false)) {
120705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        hadError = true;
120887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        continue;
120987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      }
121087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
1211f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      if (elementIndex.getBitWidth() > maxElements.getBitWidth())
12129f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad        maxElements = maxElements.extend(elementIndex.getBitWidth());
1213f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor      else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
12149f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad        elementIndex = elementIndex.extend(maxElements.getBitWidth());
1215e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor      elementIndex.setIsUnsigned(maxElements.isUnsigned());
1216f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor
121787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If the array is of incomplete type, keep track of the number of
121887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // elements in the initializer.
121987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!maxElementsKnown && elementIndex > maxElements)
122087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        maxElements = elementIndex;
122105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
122205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      continue;
122305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
122405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
122505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If we know the maximum number of elements, and we've already
122605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // hit it, stop consuming elements in the initializer list.
122705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (maxElementsKnown && elementIndex == maxElements)
12280cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff      break;
122905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
12308ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson    InitializedEntity ElementEntity =
1231dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex,
12328ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson                                           Entity);
12338ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson    // Check this element.
12348ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson    CheckSubElementType(ElementEntity, IList, elementType, Index,
12358ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson                        StructuredList, StructuredIndex);
123605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    ++elementIndex;
123705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
123805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If the array is of incomplete type, keep track of the number of
123905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // elements in the initializer.
124005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (!maxElementsKnown && elementIndex > maxElements)
124105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      maxElements = elementIndex;
12420cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
124314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) {
12440cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    // If this is an incomplete array type, the actual type needs to
1245396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar    // be calculated here.
1246e3fa2de90ea1666bf4504b8fd557b09a57aac222Douglas Gregor    llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
124705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (maxElements == Zero) {
1248396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar      // Sizing an array implicitly to zero is not allowed by ISO C,
1249396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar      // but is supported by GNU.
12500820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner      SemaRef.Diag(IList->getLocStart(),
1251396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar                    diag::ext_typecheck_zero_array_size);
12520cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
1253396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar
12541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
1255396f0bfd4b2189452914893ce69f5fb068d0ec22Daniel Dunbar                                                     ArrayType::Normal, 0);
12560cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
12573ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  if (!hadError && VerifyOnly) {
12583ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    // Check if there are any members of the array that get value-initialized.
12593ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    // If so, check if doing that is possible.
12603ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    // FIXME: This needs to detect holes left by designated initializers too.
12613ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    if (maxElementsKnown && elementIndex < maxElements)
12623ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl      CheckValueInitializable(InitializedEntity::InitializeElement(
12633ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl                                                  SemaRef.Context, 0, Entity));
12643ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  }
12650cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
12660cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
1267f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedmanbool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity,
1268f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman                                             Expr *InitExpr,
1269f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman                                             FieldDecl *Field,
1270f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman                                             bool TopLevelObject) {
1271f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman  // Handle GNU flexible array initializers.
1272f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman  unsigned FlexArrayDiag;
1273f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman  if (isa<InitListExpr>(InitExpr) &&
1274f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman      cast<InitListExpr>(InitExpr)->getNumInits() == 0) {
1275f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    // Empty flexible array init always allowed as an extension
1276f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    FlexArrayDiag = diag::ext_flexible_array_init;
12774e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  } else if (SemaRef.getLangOpts().CPlusPlus) {
1278f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    // Disallow flexible array init in C++; it is not required for gcc
1279f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    // compatibility, and it needs work to IRGen correctly in general.
1280f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    FlexArrayDiag = diag::err_flexible_array_init;
1281f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman  } else if (!TopLevelObject) {
1282f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    // Disallow flexible array init on non-top-level object
1283f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    FlexArrayDiag = diag::err_flexible_array_init;
1284f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman  } else if (Entity.getKind() != InitializedEntity::EK_Variable) {
1285f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    // Disallow flexible array init on anything which is not a variable.
1286f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    FlexArrayDiag = diag::err_flexible_array_init;
1287f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman  } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) {
1288f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    // Disallow flexible array init on local variables.
1289f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    FlexArrayDiag = diag::err_flexible_array_init;
1290f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman  } else {
1291f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    // Allow other cases.
1292f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman    FlexArrayDiag = diag::ext_flexible_array_init;
1293f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman  }
129414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
129514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (!VerifyOnly) {
129696a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    SemaRef.Diag(InitExpr->getLocStart(),
129714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                 FlexArrayDiag)
129896a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar      << InitExpr->getLocStart();
129914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
130014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      << Field;
130114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
1302f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman
1303f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman  return FlexArrayDiag != diag::ext_flexible_array_init;
1304f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman}
1305f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman
13068ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlssonvoid InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity,
13072bbae5de98f486d03e10c039668182075b5569ddAnders Carlsson                                            InitListExpr *IList,
13081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            QualType DeclType,
130987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor                                            RecordDecl::field_iterator Field,
13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            bool SubobjectIsDesignatorContext,
13114c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned &Index,
13124c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
1313eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            unsigned &StructuredIndex,
1314eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
13156217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
13161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1317b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  // If the record is invalid, some of it's members are invalid. To avoid
1318b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  // confusion, we forgo checking the intializer for the entire record.
1319b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman  if (structDecl->isInvalidDecl()) {
1320b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman    hadError = true;
1321b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman    return;
13221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  }
13233498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
13243498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  if (DeclType->isUnionType() && IList->getNumInits() == 0) {
13253ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    // Value-initialize the first named member of the union.
13263ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
13273ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    for (RecordDecl::field_iterator FieldEnd = RD->field_end();
13283ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl         Field != FieldEnd; ++Field) {
13293ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl      if (Field->getDeclName()) {
13303ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl        if (VerifyOnly)
13313ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl          CheckValueInitializable(
1332581deb3da481053c4993c7600f97acf7768caac5David Blaikie              InitializedEntity::InitializeMember(*Field, &Entity));
13333ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl        else
1334581deb3da481053c4993c7600f97acf7768caac5David Blaikie          StructuredList->setInitializedFieldInUnion(*Field);
13353ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl        break;
13363498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor      }
13373498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    }
13383498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor    return;
13393498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor  }
13403498bdb9e9cb300de74c7b51c92608e2902b2348Douglas Gregor
134105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // If structDecl is a forward declaration, this loop won't do
134205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // anything except look at designated initializers; That's okay,
134305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // because an error should get printed out elsewhere. It might be
134405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // worthwhile to skip over the rest of the initializer, though.
13456217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
134617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis  RecordDecl::field_iterator FieldEnd = RD->field_end();
1347dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor  bool InitializedSomething = false;
134880639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall  bool CheckForMissingFields = true;
134905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  while (Index < IList->getNumInits()) {
135005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    Expr *Init = IList->getInit(Index);
135105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
135205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
135387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // If we're not the subobject that matches up with the '{' for
135487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the designator, we shouldn't be handling the
135587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // designator. Return immediately.
135687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (!SubobjectIsDesignatorContext)
135787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        return;
135887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
135987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // Handle this designated initializer. Field will be updated to
136087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      // the next field that we'll be initializing.
13619a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson      if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
13624c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                     DeclType, &Field, 0, Index,
1363eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     StructuredList, StructuredIndex,
1364eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     true, TopLevelObject))
136505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        hadError = true;
136605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
1367dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor      InitializedSomething = true;
136880639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall
136980639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall      // Disable check for missing fields when designators are used.
137080639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall      // This matches gcc behaviour.
137180639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall      CheckForMissingFields = false;
137205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      continue;
137305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
137405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
137505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (Field == FieldEnd) {
137605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      // We've run out of fields. We're done.
137744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor      break;
137805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
137944b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
1380dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    // We've already initialized a member of a union. We're done.
1381dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    if (InitializedSomething && DeclType->isUnionType())
1382dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor      break;
1383dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor
138405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    // If we've hit the flexible array member at the end, we're done.
138505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    if (Field->getType()->isIncompleteArrayType())
1386b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman      break;
138744b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
13880bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (Field->isUnnamedBitfield()) {
13894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      // Don't initialize unnamed bitfields, e.g. "int : 20;"
139005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      ++Field;
1391b85f70719da9ce5a3ca9c801ee0748732e2660eeEli Friedman      continue;
13920cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff    }
139344b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
139454001c1e6a87b792b0711b822def2b1bfe1ad4a1Douglas Gregor    // Make sure we can use this declaration.
139514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    bool InvalidUse;
139614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (VerifyOnly)
1397581deb3da481053c4993c7600f97acf7768caac5David Blaikie      InvalidUse = !SemaRef.CanUseDecl(*Field);
139814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    else
1399581deb3da481053c4993c7600f97acf7768caac5David Blaikie      InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field,
140014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                          IList->getInit(Index)->getLocStart());
140114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (InvalidUse) {
140254001c1e6a87b792b0711b822def2b1bfe1ad4a1Douglas Gregor      ++Index;
140354001c1e6a87b792b0711b822def2b1bfe1ad4a1Douglas Gregor      ++Field;
140454001c1e6a87b792b0711b822def2b1bfe1ad4a1Douglas Gregor      hadError = true;
140554001c1e6a87b792b0711b822def2b1bfe1ad4a1Douglas Gregor      continue;
140614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    }
140754001c1e6a87b792b0711b822def2b1bfe1ad4a1Douglas Gregor
14088ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson    InitializedEntity MemberEntity =
1409581deb3da481053c4993c7600f97acf7768caac5David Blaikie      InitializedEntity::InitializeMember(*Field, &Entity);
14108ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson    CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
14118ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson                        StructuredList, StructuredIndex);
1412dfb5e597e033c8fa09c0e178bd93cfcdf060862eDouglas Gregor    InitializedSomething = true;
14130bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor
141414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (DeclType->isUnionType() && !VerifyOnly) {
14150bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor      // Initialize the first field within the union.
1416581deb3da481053c4993c7600f97acf7768caac5David Blaikie      StructuredList->setInitializedFieldInUnion(*Field);
14170bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    }
141805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
141905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    ++Field;
14200cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff  }
142144b4321feab46299d3f5cfd404680884752a0fcfDouglas Gregor
142280639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall  // Emit warnings for missing struct field initializers.
142314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (!VerifyOnly && InitializedSomething && CheckForMissingFields &&
142414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      Field != FieldEnd && !Field->getType()->isIncompleteArrayType() &&
142514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      !DeclType->isUnionType()) {
142680639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall    // It is possible we have one or more unnamed bitfields remaining.
142780639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall    // Find first (if any) named field and emit warning.
142880639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall    for (RecordDecl::field_iterator it = Field, end = RD->field_end();
142980639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall         it != end; ++it) {
143080639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall      if (!it->isUnnamedBitfield()) {
143180639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall        SemaRef.Diag(IList->getSourceRange().getEnd(),
143280639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall                     diag::warn_missing_field_initializers) << it->getName();
143380639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall        break;
143480639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall      }
143580639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall    }
143680639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall  }
143780639debfb2d90b2f1ffdbcd9a391f744cb0a393John McCall
14383ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  // Check that any remaining fields can be value-initialized.
14393ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() &&
14403ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl      !Field->getType()->isIncompleteArrayType()) {
14413ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    // FIXME: Should check for holes left by designated initializers too.
14423ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    for (; Field != FieldEnd && !hadError; ++Field) {
14433ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl      if (!Field->isUnnamedBitfield())
14443ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl        CheckValueInitializable(
1445581deb3da481053c4993c7600f97acf7768caac5David Blaikie            InitializedEntity::InitializeMember(*Field, &Entity));
14463ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl    }
14473ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl  }
14483ff5c86d409a55d0e64a28c07197ee3e791166c2Sebastian Redl
14491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
1450a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor      Index >= IList->getNumInits())
1451eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    return;
1452eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1453581deb3da481053c4993c7600f97acf7768caac5David Blaikie  if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field,
1454f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman                             TopLevelObject)) {
1455eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    hadError = true;
1456a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    ++Index;
1457a6457963cf7ffe71498c408dd590d9d1acb9513cDouglas Gregor    return;
1458eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor  }
1459eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
14608ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  InitializedEntity MemberEntity =
1461581deb3da481053c4993c7600f97acf7768caac5David Blaikie    InitializedEntity::InitializeMember(*Field, &Entity);
1462dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
14638ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  if (isa<InitListExpr>(IList->getInit(Index)))
1464dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
14658ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson                        StructuredList, StructuredIndex);
14668ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson  else
1467dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index,
1468987dc6a3fa0917de269c9d60b7d4f21c68b8b09fAnders Carlsson                          StructuredList, StructuredIndex);
14690cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff}
14700cca749f64ff54476df3a4fc084821b8a8d712d5Steve Naroff
1471ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// \brief Expand a field designator that refers to a member of an
1472ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// anonymous struct or union into a series of field designators that
1473ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor/// refers to the field within the appropriate subobject.
1474ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor///
1475ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregorstatic void ExpandAnonymousFieldDesignator(Sema &SemaRef,
14761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           DesignatedInitExpr *DIE,
14771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           unsigned DesigIdx,
1478a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet                                           IndirectFieldDecl *IndirectField) {
1479ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  typedef DesignatedInitExpr::Designator Designator;
1480ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1481ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // Build the replacement designators.
14825f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Designator, 4> Replacements;
1483a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet  for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(),
1484a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet       PE = IndirectField->chain_end(); PI != PE; ++PI) {
1485a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet    if (PI + 1 == PE)
14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Replacements.push_back(Designator((IdentifierInfo *)0,
1487ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                    DIE->getDesignator(DesigIdx)->getDotLoc(),
1488ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                DIE->getDesignator(DesigIdx)->getFieldLoc()));
1489ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    else
1490ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(),
1491ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                                        SourceLocation()));
1492a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet    assert(isa<FieldDecl>(*PI));
1493a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet    Replacements.back().setField(cast<FieldDecl>(*PI));
1494ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
1495ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
1496ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // Expand the current designator into the set of replacement
1497ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // designators, so we have a full subobject path down to where the
1498ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  // member of the anonymous struct/union is actually stored.
1499319d57f21600dd2c4d52ccc27bd12ce260b174e7Douglas Gregor  DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0],
1500ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor                        &Replacements[0] + Replacements.size());
1501a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet}
15021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1503dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Given an implicit anonymous field, search the IndirectField that
1504a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet///  corresponds to FieldName.
1505a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichetstatic IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField,
1506a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet                                                 IdentifierInfo *FieldName) {
1507a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet  assert(AnonField->isAnonymousStructOrUnion());
1508a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet  Decl *NextDecl = AnonField->getNextDeclInContext();
15093e78b19e51034dd27c6e0d811522148345e299e4Aaron Ballman  while (IndirectFieldDecl *IF =
15103e78b19e51034dd27c6e0d811522148345e299e4Aaron Ballman          dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) {
1511a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet    if (FieldName && FieldName == IF->getAnonField()->getIdentifier())
1512a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet      return IF;
1513a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet    NextDecl = NextDecl->getNextDeclInContext();
1514ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor  }
1515a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet  return 0;
1516ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor}
1517ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
151814b0c194b356a1204d081765b3e6699687bed97cSebastian Redlstatic DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef,
151914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                                   DesignatedInitExpr *DIE) {
152014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  unsigned NumIndexExprs = DIE->getNumSubExprs() - 1;
152114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  SmallVector<Expr*, 4> IndexExprs(NumIndexExprs);
152214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  for (unsigned I = 0; I < NumIndexExprs; ++I)
152314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    IndexExprs[I] = DIE->getSubExpr(I + 1);
152414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(),
152514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                    DIE->size(), IndexExprs.data(),
152614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                    NumIndexExprs, DIE->getEqualOrColonLoc(),
152714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                    DIE->usesGNUSyntax(), DIE->getInit());
152814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl}
152914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
1530425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrainnamespace {
1531425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain
1532425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain// Callback to only accept typo corrections that are for field members of
1533425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain// the given struct or union.
1534425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrainclass FieldInitializerValidatorCCC : public CorrectionCandidateCallback {
1535425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain public:
1536425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain  explicit FieldInitializerValidatorCCC(RecordDecl *RD)
1537425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain      : Record(RD) {}
1538425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain
1539425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain  virtual bool ValidateCandidate(const TypoCorrection &candidate) {
1540425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain    FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>();
1541425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain    return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record);
1542425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain  }
1543425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain
1544425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain private:
1545425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain  RecordDecl *Record;
1546425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain};
1547425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain
1548425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain}
1549425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain
155005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @brief Check the well-formedness of a C99 designated initializer.
155105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
155205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// Determines whether the designated initializer @p DIE, which
155305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// resides at the given @p Index within the initializer list @p
155405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// IList, is well-formed for a current object of type @p DeclType
155505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// (C99 6.7.8). The actual subobject that this designator refers to
15561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// within the current subobject is returned in either
15574c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// @p NextField or @p NextElementIndex (whichever is appropriate).
155805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
155905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param IList  The initializer list in which this designated
156005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// initializer occurs.
156105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
1562711997184366d584c9c437102cae1e9d9927b986Douglas Gregor/// @param DIE The designated initializer expression.
1563711997184366d584c9c437102cae1e9d9927b986Douglas Gregor///
1564711997184366d584c9c437102cae1e9d9927b986Douglas Gregor/// @param DesigIdx  The index of the current designator.
156505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
156605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param DeclType  The type of the "current object" (C99 6.7.8p17),
156705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// into which the designation in @p DIE should refer.
156805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
156987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// @param NextField  If non-NULL and the first designator in @p DIE is
157087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// a field, this will be set to the field declaration corresponding
157187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// to the field named by the designator.
157205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
157387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// @param NextElementIndex  If non-NULL and the first designator in @p
157487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// DIE is an array designator or GNU array-range designator, this
157587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor/// will be set to the last index initialized by this designator.
157605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
157705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @param Index  Index into @p IList where the designated initializer
157805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @p DIE occurs.
157905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor///
15804c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// @param StructuredList  The initializer list expression that
15814c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// describes all of the subobject initializers in the order they'll
15824c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// actually be initialized.
15834c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor///
158405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// @returns true if there was an error, false otherwise.
15851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpbool
15868ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders CarlssonInitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
15879a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson                                            InitListExpr *IList,
158814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                            DesignatedInitExpr *DIE,
158914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                            unsigned DesigIdx,
159014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                            QualType &CurrentObjectType,
159114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                          RecordDecl::field_iterator *NextField,
159214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                            llvm::APSInt *NextElementIndex,
159314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                            unsigned &Index,
159414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                            InitListExpr *StructuredList,
159514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                            unsigned &StructuredIndex,
1596eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool FinishSubobjectInit,
1597eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                            bool TopLevelObject) {
1598711997184366d584c9c437102cae1e9d9927b986Douglas Gregor  if (DesigIdx == DIE->size()) {
159987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Check the actual initialization for the designated object type.
160087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    bool prevHadError = hadError;
16016fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
16026fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // Temporarily remove the designator expression from the
16036fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // initializer list that the child calls see, so that we don't try
16046fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // to re-process the designator.
16056fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    unsigned OldIndex = Index;
16066fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    IList->setInit(OldIndex, DIE->getInit());
16076fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
16089a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson    CheckSubElementType(Entity, IList, CurrentObjectType, Index,
16094c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                        StructuredList, StructuredIndex);
16106fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
16116fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // Restore the designated initializer expression in the syntactic
16126fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    // form of the initializer list.
16136fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    if (IList->getInit(OldIndex) != DIE->getInit())
16146fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor      DIE->setInit(IList->getInit(OldIndex));
16156fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor    IList->setInit(OldIndex, DIE);
16166fbdc6bd38cc51232223ae3539c32f23c45ea852Douglas Gregor
161787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return hadError && !prevHadError;
161887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
161905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
1620711997184366d584c9c437102cae1e9d9927b986Douglas Gregor  DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
162114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  bool IsFirstDesignator = (DesigIdx == 0);
162214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (!VerifyOnly) {
162314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    assert((IsFirstDesignator || StructuredList) &&
162414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl           "Need a non-designated initializer list to start from");
162514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
162614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    // Determine the structural initializer list that corresponds to the
162714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    // current subobject.
1628a789416a497b2e14e7c2fd721d03e541120bac8dBenjamin Kramer    StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList)
162914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
163014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                   StructuredList, StructuredIndex,
163114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                   SourceRange(D->getStartLocation(),
163214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                                               DIE->getSourceRange().getEnd()));
163314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    assert(StructuredList && "Expected a structured initializer list");
163414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
16354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
163687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (D->isFieldDesignator()) {
163787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // C99 6.7.8p7:
163887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
163987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   If a designator has the form
164087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
164187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //      . identifier
164287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //
164387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   then the current object (defined below) shall have
164487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    //   structure or union type and the identifier shall be the
16451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   name of a member of that type.
16466217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    const RecordType *RT = CurrentObjectType->getAs<RecordType>();
164787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (!RT) {
164887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      SourceLocation Loc = D->getDotLoc();
164987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (Loc.isInvalid())
165087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        Loc = D->getFieldLoc();
165114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      if (!VerifyOnly)
165214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
16534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie          << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType;
165487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      ++Index;
165587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return true;
165687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
165787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
16584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Note: we perform a linear search of the fields here, despite
16594c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // the fact that we have a faster lookup method, because we always
16604c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // need to compute the field's index.
1661ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    FieldDecl *KnownField = D->getField();
166287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IdentifierInfo *FieldName = D->getFieldName();
16634c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    unsigned FieldIndex = 0;
16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    RecordDecl::field_iterator
166517945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      Field = RT->getDecl()->field_begin(),
166617945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      FieldEnd = RT->getDecl()->field_end();
16674c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    for (; Field != FieldEnd; ++Field) {
16684c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Field->isUnnamedBitfield())
16694c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        continue;
1670dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1671a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet      // If we find a field representing an anonymous field, look in the
1672a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet      // IndirectFieldDecl that follow for the designated initializer.
1673a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet      if (!KnownField && Field->isAnonymousStructOrUnion()) {
1674a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet        if (IndirectFieldDecl *IF =
1675581deb3da481053c4993c7600f97acf7768caac5David Blaikie            FindIndirectFieldDesignator(*Field, FieldName)) {
167614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl          // In verify mode, don't modify the original.
167714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl          if (VerifyOnly)
167814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl            DIE = CloneDesignatedInitExpr(SemaRef, DIE);
1679a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet          ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF);
1680a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet          D = DIE->getDesignator(DesigIdx);
1681a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet          break;
1682a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet        }
1683a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet      }
1684581deb3da481053c4993c7600f97acf7768caac5David Blaikie      if (KnownField && KnownField == *Field)
1685022d13de47a5f02d1e4089fa3360fae8bcb17666Douglas Gregor        break;
1686022d13de47a5f02d1e4089fa3360fae8bcb17666Douglas Gregor      if (FieldName && FieldName == Field->getIdentifier())
16874c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        break;
16884c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
16894c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      ++FieldIndex;
169087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
169187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
16924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    if (Field == FieldEnd) {
1693a41ee4921f0b42861cbb795752919ee3eddaf143Benjamin Kramer      if (VerifyOnly) {
1694a41ee4921f0b42861cbb795752919ee3eddaf143Benjamin Kramer        ++Index;
169514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        return true; // No typo correction when just trying this out.
1696a41ee4921f0b42861cbb795752919ee3eddaf143Benjamin Kramer      }
169714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
1698ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // There was no normal field in the struct with the designated
1699ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // name. Perform another lookup for this name, which may find
1700ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // something that we can't designate (e.g., a member function),
1701ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      // may find nothing, or may find a member of an anonymous
17021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // struct/union.
170317945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis      DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
1704c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      FieldDecl *ReplacementField = 0;
17054c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      if (Lookup.first == Lookup.second) {
1706c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // Name lookup didn't find anything. Determine whether this
1707c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // was a typo for another field name.
1708425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain        FieldInitializerValidatorCCC Validator(RT->getDecl());
1709d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor        TypoCorrection Corrected = SemaRef.CorrectTypo(
1710d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor            DeclarationNameInfo(FieldName, D->getFieldLoc()),
171116e46dd0c284296cea819dfbf67942ecef02894dKaelyn Uhrain            Sema::LookupMemberName, /*Scope=*/0, /*SS=*/0, Validator,
1712425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain            RT->getDecl());
1713425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain        if (Corrected) {
1714d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor          std::string CorrectedStr(
17154e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie              Corrected.getAsString(SemaRef.getLangOpts()));
1716d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor          std::string CorrectedQuotedStr(
17174e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie              Corrected.getQuoted(SemaRef.getLangOpts()));
1718425d631082699b37624a7b1d6abb87c7abd5abe6Kaelyn Uhrain          ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>();
1719dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          SemaRef.Diag(D->getFieldLoc(),
1720c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor                       diag::err_field_designator_unknown_suggest)
1721d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor            << FieldName << CurrentObjectType << CorrectedQuotedStr
1722d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor            << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr);
1723dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          SemaRef.Diag(ReplacementField->getLocation(),
1724d8bba9c15230d2b1b3893e272106aa79efc50251Douglas Gregor                       diag::note_previous_decl) << CorrectedQuotedStr;
1725a41ee4921f0b42861cbb795752919ee3eddaf143Benjamin Kramer          hadError = true;
1726c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        } else {
1727c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
1728c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor            << FieldName << CurrentObjectType;
1729c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          ++Index;
1730c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          return true;
1731c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        }
1732c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      }
1733dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1734c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      if (!ReplacementField) {
17354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor        // Name lookup found something, but it wasn't a field.
17360820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner        SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
17374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor          << FieldName;
17381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        SemaRef.Diag((*Lookup.first)->getLocation(),
17394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                      diag::note_field_designator_found);
1740ba79fc2d1b742e34df104aadb2780725c2a882fcEli Friedman        ++Index;
1741ba79fc2d1b742e34df104aadb2780725c2a882fcEli Friedman        return true;
1742ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      }
1743c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor
1744a0e27f00158c9306d53b0003b94182e415380ea9Francois Pichet      if (!KnownField) {
1745c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // The replacement field comes from typo correction; find it
1746c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        // in the list of fields.
1747c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        FieldIndex = 0;
1748c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        Field = RT->getDecl()->field_begin();
1749c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        for (; Field != FieldEnd; ++Field) {
1750c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          if (Field->isUnnamedBitfield())
1751c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor            continue;
1752c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor
1753581deb3da481053c4993c7600f97acf7768caac5David Blaikie          if (ReplacementField == *Field ||
1754c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor              Field->getIdentifier() == ReplacementField->getIdentifier())
1755c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor            break;
1756c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor
1757c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor          ++FieldIndex;
1758c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor        }
1759c171e3b192a372669cf622ff0b6a847f8e5b4220Douglas Gregor      }
176087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
17614c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
17624c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // All of the fields of a union are located at the same place in
17634c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // the initializer list.
17640bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    if (RT->getDecl()->isUnion()) {
17654c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      FieldIndex = 0;
176614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      if (!VerifyOnly)
1767581deb3da481053c4993c7600f97acf7768caac5David Blaikie        StructuredList->setInitializedFieldInUnion(*Field);
17680bb76897bedb8b747efc6523efb432fc24966118Douglas Gregor    }
17694c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
177054001c1e6a87b792b0711b822def2b1bfe1ad4a1Douglas Gregor    // Make sure we can use this declaration.
177114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    bool InvalidUse;
177214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (VerifyOnly)
1773581deb3da481053c4993c7600f97acf7768caac5David Blaikie      InvalidUse = !SemaRef.CanUseDecl(*Field);
177414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    else
1775581deb3da481053c4993c7600f97acf7768caac5David Blaikie      InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc());
177614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (InvalidUse) {
177754001c1e6a87b792b0711b822def2b1bfe1ad4a1Douglas Gregor      ++Index;
177854001c1e6a87b792b0711b822def2b1bfe1ad4a1Douglas Gregor      return true;
177914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    }
178054001c1e6a87b792b0711b822def2b1bfe1ad4a1Douglas Gregor
178114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly) {
178214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      // Update the designator with the field declaration.
1783581deb3da481053c4993c7600f97acf7768caac5David Blaikie      D->setField(*Field);
17841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
178514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      // Make sure that our non-designated initializer list has space
178614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      // for a subobject corresponding to this field.
178714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      if (FieldIndex >= StructuredList->getNumInits())
178814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
178914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    }
17904c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
1791eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    // This designator names a flexible array member.
1792eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    if (Field->getType()->isIncompleteArrayType()) {
1793eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      bool Invalid = false;
1794711997184366d584c9c437102cae1e9d9927b986Douglas Gregor      if ((DesigIdx + 1) != DIE->size()) {
1795eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // We can't designate an object within the flexible array
1796eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // member (because GCC doesn't allow it).
179714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        if (!VerifyOnly) {
179814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl          DesignatedInitExpr::Designator *NextD
179914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl            = DIE->getDesignator(DesigIdx + 1);
180014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl          SemaRef.Diag(NextD->getStartLocation(),
180114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                        diag::err_designator_into_flexible_array_member)
180214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl            << SourceRange(NextD->getStartLocation(),
180314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                           DIE->getSourceRange().getEnd());
180414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl          SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1805581deb3da481053c4993c7600f97acf7768caac5David Blaikie            << *Field;
180614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        }
1807eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1808eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1809eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
18109046c224f16be3cef84f03b96a6c00a621c8383eChris Lattner      if (!hadError && !isa<InitListExpr>(DIE->getInit()) &&
18119046c224f16be3cef84f03b96a6c00a621c8383eChris Lattner          !isa<StringLiteral>(DIE->getInit())) {
1812eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        // The initializer is not an initializer list.
181314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        if (!VerifyOnly) {
181496a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar          SemaRef.Diag(DIE->getInit()->getLocStart(),
181514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                        diag::err_flexible_array_init_needs_braces)
181614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl            << DIE->getInit()->getSourceRange();
181714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl          SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1818581deb3da481053c4993c7600f97acf7768caac5David Blaikie            << *Field;
181914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        }
1820eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1821eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1822eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1823f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman      // Check GNU flexible array initializer.
1824581deb3da481053c4993c7600f97acf7768caac5David Blaikie      if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field,
1825f40fd6bbb2684db5efb18627ea355613c235d23fEli Friedman                                             TopLevelObject))
1826eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        Invalid = true;
1827eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1828eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (Invalid) {
1829eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++Index;
1830eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1831eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1832eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor
1833eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Initialize the array.
1834eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      bool prevHadError = hadError;
1835eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned newStructuredIndex = FieldIndex;
1836eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned OldIndex = Index;
1837eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      IList->setInit(Index, DIE->getInit());
18388ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson
18398ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson      InitializedEntity MemberEntity =
1840581deb3da481053c4993c7600f97acf7768caac5David Blaikie        InitializedEntity::InitializeMember(*Field, &Entity);
18418ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson      CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
1842eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                          StructuredList, newStructuredIndex);
18438ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson
1844eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      IList->setInit(OldIndex, DIE);
1845eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      if (hadError && !prevHadError) {
1846eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++Field;
1847eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        ++FieldIndex;
1848eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        if (NextField)
1849eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor          *NextField = Field;
1850eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        StructuredIndex = FieldIndex;
1851eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1852eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      }
1853eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    } else {
1854eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      // Recurse to check later designated subobjects.
1855262bc18e32500558af7cb0afa205b34bd37bafedDavid Blaikie      QualType FieldType = Field->getType();
1856eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor      unsigned newStructuredIndex = FieldIndex;
1857dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
18589a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson      InitializedEntity MemberEntity =
1859581deb3da481053c4993c7600f97acf7768caac5David Blaikie        InitializedEntity::InitializeMember(*Field, &Entity);
1860dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1,
1861dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                     FieldType, 0, 0, Index,
18629a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson                                     StructuredList, newStructuredIndex,
1863eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                     true, false))
1864eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor        return true;
1865eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor    }
186687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
186787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Find the position of the next field to be initialized in this
186887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // subobject.
186987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    ++Field;
18704c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ++FieldIndex;
187105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
187287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // If this the first designator, our caller will continue checking
187387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // the rest of this struct/class/union subobject.
187487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (IsFirstDesignator) {
187587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      if (NextField)
187687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor        *NextField = Field;
18774c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      StructuredIndex = FieldIndex;
187887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return false;
187987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    }
188005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
188134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (!FinishSubobjectInit)
188234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      return false;
188334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
1884ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    // We've already initialized something in the union; we're done.
1885ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor    if (RT->getDecl()->isUnion())
1886ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor      return hadError;
1887ffb4b6e299069139908540ce97be4462e16b53a4Douglas Gregor
188887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    // Check the remaining fields within this class/struct/union subobject.
188987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    bool prevHadError = hadError;
1890dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
18918ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson    CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index,
18924c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                          StructuredList, FieldIndex);
189387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return hadError && !prevHadError;
189487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
189505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
189687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // C99 6.7.8p6:
189787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
189887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   If a designator has the form
189987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
190087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //      [ constant-expression ]
190187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
190287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   then the current object (defined below) shall have array
190387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   type and the expression shall be an integer constant
190487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   expression. If the array is of unknown size, any
190587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //   nonnegative value is valid.
190687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
190787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // Additionally, cope with the GNU extension that permits
190887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // designators of the form
190987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //
191087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  //      [ constant-expression ... constant-expression ]
19110820254f97bb8925d933a3664ea1c6fca3997b97Chris Lattner  const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
191287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (!AT) {
191314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    if (!VerifyOnly)
191414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
191514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        << CurrentObjectType;
191687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    ++Index;
191787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return true;
191887f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
191905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
192087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  Expr *IndexExpr = 0;
192134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
192234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (D->isArrayDesignator()) {
192387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IndexExpr = DIE->getArrayIndex(*D);
1924a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith    DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context);
192534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex = DesignatedStartIndex;
192634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  } else {
192787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    assert(D->isArrayRangeDesignator() && "Need array-range designator");
19283bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner
19291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    DesignatedStartIndex =
1930a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith      DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context);
19311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    DesignatedEndIndex =
1932a6b8b2c09610b8bc4330e948ece8b940c2386406Richard Smith      DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context);
193387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    IndexExpr = DIE->getArrayRangeEnd(*D);
193434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
1935e0fd832b076eb5a1e4a4549687af0dbf2ad57181Chris Lattner    // Codegen can't handle evaluating array range designators that have side
1936e0fd832b076eb5a1e4a4549687af0dbf2ad57181Chris Lattner    // effects, because we replicate the AST value for each initialized element.
1937e0fd832b076eb5a1e4a4549687af0dbf2ad57181Chris Lattner    // As such, set the sawArrayRangeDesignator() bit if we initialize multiple
1938e0fd832b076eb5a1e4a4549687af0dbf2ad57181Chris Lattner    // elements with something that has a side effect, so codegen can emit an
1939e0fd832b076eb5a1e4a4549687af0dbf2ad57181Chris Lattner    // "error unsupported" error instead of miscompiling the app.
1940e0fd832b076eb5a1e4a4549687af0dbf2ad57181Chris Lattner    if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&&
194114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl        DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly)
1942a9c878086036de36482cc21e35a33cabe9699b0aDouglas Gregor      FullyStructuredList->sawArrayRangeDesignator();
194387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
194405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
194587f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (isa<ConstantArrayType>(AT)) {
194687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
19479f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    DesignatedStartIndex
19489f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad      = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
194934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
19509f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    DesignatedEndIndex
19519f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad      = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
195234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
195334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (DesignatedEndIndex >= MaxElements) {
1954a4e20e1e43df39e39328035c84c46fdcbbf3a01eEli Friedman      if (!VerifyOnly)
195596a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar        SemaRef.Diag(IndexExpr->getLocStart(),
195614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl                      diag::err_array_designator_too_large)
195714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl          << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
195814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl          << IndexExpr->getSourceRange();
195987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      ++Index;
196087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor      return true;
196105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
196234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  } else {
196334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Make sure the bit-widths and signedness match.
196434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
19659f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad      DesignatedEndIndex
19669f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad        = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
19673bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    else if (DesignatedStartIndex.getBitWidth() <
19683bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner             DesignatedEndIndex.getBitWidth())
19699f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad      DesignatedStartIndex
19709f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad        = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
197134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedStartIndex.setIsUnsigned(true);
197234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    DesignatedEndIndex.setIsUnsigned(true);
197305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
19741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19754c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Make sure that our non-designated initializer list has space
19764c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // for a subobject corresponding to this array element.
197714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (!VerifyOnly &&
197814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
19791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    StructuredList->resizeInits(SemaRef.Context,
198034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor                                DesignatedEndIndex.getZExtValue() + 1);
198134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
198234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // Repeatedly perform subobject initializations in the range
198334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // [DesignatedStartIndex, DesignatedEndIndex].
198434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
198534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  // Move to the next designator
198634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
198734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  unsigned OldIndex = Index;
1988dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
19899a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson  InitializedEntity ElementEntity =
19908ff9e86c492c784b3ce2abe5b5d870cd6db365a8Anders Carlsson    InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
19919a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson
199234e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  while (DesignatedStartIndex <= DesignatedEndIndex) {
199334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Recurse to check later designated subobjects.
199434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    QualType ElementType = AT->getElementType();
199534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    Index = OldIndex;
1996dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
19979a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson    ElementEntity.setElementIndex(ElementIndex);
1998dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1,
1999dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                   ElementType, 0, 0, Index,
20009a8a70ef79f68db5b17d505eb9681bf161384fe8Anders Carlsson                                   StructuredList, ElementIndex,
2001eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                   (DesignatedStartIndex == DesignatedEndIndex),
2002eeb15d499f032bb89773ddaca2d17475122a37bbDouglas Gregor                                   false))
200334e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      return true;
200487f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
200534e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    // Move to the next index in the array that we'll be initializing.
200634e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    ++DesignatedStartIndex;
200734e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    ElementIndex = DesignatedStartIndex.getZExtValue();
200834e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  }
200987f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor
201087f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // If this the first designator, our caller will continue checking
201187f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // the rest of this array subobject.
201287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  if (IsFirstDesignator) {
201387f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    if (NextElementIndex)
201434e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor      *NextElementIndex = DesignatedStartIndex;
20154c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    StructuredIndex = ElementIndex;
201687f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor    return false;
201787f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  }
20181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201934e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor  if (!FinishSubobjectInit)
202034e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor    return false;
202134e7946831a63f96d3ba3478c74ca8e25ee52d7eDouglas Gregor
202287f55cf59e82f246d8605733e9300d0c5f6830a6Douglas Gregor  // Check the remaining elements within this array subobject.
202305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool prevHadError = hadError;
2024dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex,
2025784f69940755dd66cf244dd84f57a57d358e5c43Anders Carlsson                 /*SubobjectIsDesignatorContext=*/false, Index,
20264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                 StructuredList, ElementIndex);
20271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return hadError && !prevHadError;
202805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
202905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
20304c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor// Get the structured initializer list for a subobject of type
20314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor// @p CurrentObjectType.
20324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas GregorInitListExpr *
20334c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas GregorInitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
20344c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            QualType CurrentObjectType,
20354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            InitListExpr *StructuredList,
20364c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            unsigned StructuredIndex,
20374c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                            SourceRange InitRange) {
203814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (VerifyOnly)
203914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    return 0; // No structured list in verification-only mode.
20404c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  Expr *ExistingInit = 0;
20414c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (!StructuredList)
2042a789416a497b2e14e7c2fd721d03e541120bac8dBenjamin Kramer    ExistingInit = SyntacticToSemantic.lookup(IList);
20434c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  else if (StructuredIndex < StructuredList->getNumInits())
20444c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    ExistingInit = StructuredList->getInit(StructuredIndex);
20451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
20474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return Result;
20484c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
20494c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (ExistingInit) {
20504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // We are creating an initializer list that initializes the
20514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobjects of the current object, but there was already an
20524c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // initialization that completely initialized the current
20534c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobject, e.g., by a compound literal:
20541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
20554c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // struct X { int a, b; };
20564c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
20571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //
20584c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
20594c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // designated initializer re-initializes the whole
20604c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // subobject [0], overwriting previous initializers.
20611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    SemaRef.Diag(InitRange.getBegin(),
2062ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor                 diag::warn_subobject_initializer_overrides)
20634c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << InitRange;
206496a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    SemaRef.Diag(ExistingInit->getLocStart(),
20654c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::note_previous_initializer)
206654f0728c2ab0f967e976300478b2f5cdfed78415Douglas Gregor      << /*FIXME:has side effects=*/0
20674c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << ExistingInit->getSourceRange();
20684c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
20694c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
20701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InitListExpr *Result
2071709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    = new (SemaRef.Context) InitListExpr(SemaRef.Context,
2072709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek                                         InitRange.getBegin(), 0, 0,
2073ba7bc5584b8d46f4e8deb3a9d363256908fa86eaTed Kremenek                                         InitRange.getEnd());
2074ed8a93d17b8936dc7978cdc37f3f00fc49d24f71Douglas Gregor
20755c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman  QualType ResultType = CurrentObjectType;
20765c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman  if (!ResultType->isArrayType())
20775c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman    ResultType = ResultType.getNonLValueExprType(SemaRef.Context);
20785c89c399ba0a171e3312a74e008d61d174d961f3Eli Friedman  Result->setType(ResultType);
20794c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
2080fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  // Pre-allocate storage for the structured initializer list.
2081fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  unsigned NumElements = 0;
208208457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  unsigned NumInits = 0;
2083f8b1771677743d24c7391305cad8853c513ec0f8Argyrios Kyrtzidis  bool GotNumInits = false;
2084f8b1771677743d24c7391305cad8853c513ec0f8Argyrios Kyrtzidis  if (!StructuredList) {
208508457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor    NumInits = IList->getNumInits();
2086f8b1771677743d24c7391305cad8853c513ec0f8Argyrios Kyrtzidis    GotNumInits = true;
2087f8b1771677743d24c7391305cad8853c513ec0f8Argyrios Kyrtzidis  } else if (Index < IList->getNumInits()) {
2088f8b1771677743d24c7391305cad8853c513ec0f8Argyrios Kyrtzidis    if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) {
208908457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor      NumInits = SubList->getNumInits();
2090f8b1771677743d24c7391305cad8853c513ec0f8Argyrios Kyrtzidis      GotNumInits = true;
2091f8b1771677743d24c7391305cad8853c513ec0f8Argyrios Kyrtzidis    }
209208457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor  }
209308457737b60ba2e7f58ecf3062010843268fc6eaDouglas Gregor
20941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const ArrayType *AType
2095fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
2096fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
2097fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      NumElements = CAType->getSize().getZExtValue();
2098fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      // Simple heuristic so that we don't allocate a very large
2099fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      // initializer with many empty entries at the end.
2100f8b1771677743d24c7391305cad8853c513ec0f8Argyrios Kyrtzidis      if (GotNumInits && NumElements > NumInits)
2101fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor        NumElements = 0;
2102fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    }
2103183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall  } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())
2104fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    NumElements = VType->getNumElements();
21056217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek  else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
2106fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    RecordDecl *RDecl = RType->getDecl();
2107fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    if (RDecl->isUnion())
2108fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor      NumElements = 1;
2109fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor    else
21101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NumElements = std::distance(RDecl->field_begin(),
211117945a0f64fe03ff6ec0c2146005a87636e3ac12Argyrios Kyrtzidis                                  RDecl->field_end());
2112fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor  }
2113fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
2114709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek  Result->reserveInits(SemaRef.Context, NumElements);
2115fa2192042f223b5122a9e17719930f77634fd31fDouglas Gregor
21164c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // Link this new initializer list into the structured initializer
21174c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // lists.
21184c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (StructuredList)
2119709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek    StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result);
21204c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  else {
21214c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    Result->setSyntacticForm(IList);
21224c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    SyntacticToSemantic[IList] = Result;
21234c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
21244c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
21254c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  return Result;
21264c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
21274c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
21284c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// Update the initializer at index @p StructuredIndex within the
21294c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor/// structured initializer list to the value @p expr.
21304c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregorvoid InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
21314c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                                  unsigned &StructuredIndex,
21324c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                                                  Expr *expr) {
21334c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  // No structured initializer list to update
21344c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  if (!StructuredList)
21354c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    return;
21364c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
2137709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek  if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
2138709210feee317b8d6690dd1d15c2b74cfe55e261Ted Kremenek                                                  StructuredIndex, expr)) {
21394c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor    // This initializer overwrites a previous initializer. Warn.
214096a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    SemaRef.Diag(expr->getLocStart(),
21414c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::warn_initializer_overrides)
21424c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << expr->getSourceRange();
214396a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar    SemaRef.Diag(PrevInit->getLocStart(),
21444c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor                  diag::note_previous_initializer)
214554f0728c2ab0f967e976300478b2f5cdfed78415Douglas Gregor      << /*FIXME:has side effects=*/0
21464c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor      << PrevInit->getSourceRange();
21474c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  }
21481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21494c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor  ++StructuredIndex;
21504c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor}
21514c67834407ca6ab344dcf44fc599ad4938cfa96dDouglas Gregor
215205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// Check that the given Index expression is a valid array designator
2153282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith/// value. This is essentially just a wrapper around
21543bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner/// VerifyIntegerConstantExpression that also checks for negative values
215505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor/// and produces a reasonable diagnostic if there is a
2156282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith/// failure. Returns the index expression, possibly with an implicit cast
2157282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith/// added, on success.  If everything went okay, Value will receive the
2158282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith/// value of the constant expression.
2159282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smithstatic ExprResult
21603bf6893b77c30cb774100e0fa7ae029331675ec1Chris LattnerCheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
216196a0014f9b963d8a987f1cccd48808a47f9c6331Daniel Dunbar  SourceLocation Loc = Index->getLocStart();
216205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
216305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Make sure this is an integer constant expression.
2164282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value);
2165282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  if (Result.isInvalid())
2166282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith    return Result;
21673bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner
21683bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner  if (Value.isSigned() && Value.isNegative())
21693bf6893b77c30cb774100e0fa7ae029331675ec1Chris Lattner    return S.Diag(Loc, diag::err_array_designator_negative)
217005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      << Value.toString(10) << Index->getSourceRange();
217105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
217253d3d8e0662197f7245d8f5ff697a72a2b4b3f54Douglas Gregor  Value.setIsUnsigned(true);
2173282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith  return Result;
217405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
217505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
217660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
21777663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                            SourceLocation Loc,
21787663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                            bool GNUSyntax,
21797663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky                                            ExprResult Init) {
218005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  typedef DesignatedInitExpr::Designator ASTDesignator;
218105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
218205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  bool Invalid = false;
21835f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<ASTDesignator, 32> Designators;
21845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Expr *, 32> InitExpressions;
218505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
218605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Build designators and check array designator expressions.
218705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
218805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    const Designator &D = Desig.getDesignator(Idx);
218905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    switch (D.getKind()) {
219005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::FieldDesignator:
21911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
219205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                          D.getFieldLoc()));
219305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
219405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
219505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::ArrayDesignator: {
219605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *Index = static_cast<Expr *>(D.getArrayIndex());
219705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt IndexValue;
2198282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      if (!Index->isTypeDependent() && !Index->isValueDependent())
2199282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take();
2200282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      if (!Index)
220105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Invalid = true;
220205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      else {
220305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Designators.push_back(ASTDesignator(InitExpressions.size(),
22041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                            D.getLBracketLoc(),
220505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor                                            D.getRBracketLoc()));
220605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        InitExpressions.push_back(Index);
220705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      }
220805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
220905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
221005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
221105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    case Designator::ArrayRangeDesignator: {
221205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
221305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
221405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt StartValue;
221505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      llvm::APSInt EndValue;
22169ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      bool StartDependent = StartIndex->isTypeDependent() ||
22179ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                            StartIndex->isValueDependent();
22189ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor      bool EndDependent = EndIndex->isTypeDependent() ||
22199ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor                          EndIndex->isValueDependent();
2220282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      if (!StartDependent)
2221282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        StartIndex =
2222282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith            CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take();
2223282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      if (!EndDependent)
2224282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith        EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take();
2225282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith
2226282e7e66748cc6dd14d6f7f2cb52e5373c531e61Richard Smith      if (!StartIndex || !EndIndex)
222705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor        Invalid = true;
2228d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor      else {
2229d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        // Make sure we're comparing values with the same bit width.
22309ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor        if (StartDependent || EndDependent) {
22319ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor          // Nothing to compute.
22329ea62768fca25d829d80199cf4f8cf0f4dd39251Douglas Gregor        } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
22339f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad          EndValue = EndValue.extend(StartValue.getBitWidth());
2234d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        else if (StartValue.getBitWidth() < EndValue.getBitWidth())
22359f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad          StartValue = StartValue.extend(EndValue.getBitWidth());
2236d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor
2237c4bb7bfe8328f79004517dd268f6146c8066c205Douglas Gregor        if (!StartDependent && !EndDependent && EndValue < StartValue) {
2238d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
22391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump            << StartValue.toString(10) << EndValue.toString(10)
2240d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor            << StartIndex->getSourceRange() << EndIndex->getSourceRange();
2241d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Invalid = true;
2242d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        } else {
2243d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          Designators.push_back(ASTDesignator(InitExpressions.size(),
22441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                              D.getLBracketLoc(),
2245d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor                                              D.getEllipsisLoc(),
2246d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor                                              D.getRBracketLoc()));
2247d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          InitExpressions.push_back(StartIndex);
2248d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor          InitExpressions.push_back(EndIndex);
2249d6f584ff262f51b40f4c9e317b13f1f21db29755Douglas Gregor        }
225005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      }
225105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor      break;
225205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
225305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    }
225405c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  }
225505c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
225605c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  if (Invalid || Init.isInvalid())
225705c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor    return ExprError();
225805c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
225905c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  // Clear out the expressions within the designation.
226005c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  Desig.ClearExprs(*this);
226105c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor
226205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  DesignatedInitExpr *DIE
2263beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    = DesignatedInitExpr::Create(Context,
2264beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                 Designators.data(), Designators.size(),
2265beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                 InitExpressions.data(), InitExpressions.size(),
2266e9146f2e9f1c4e281544e8c080934c72d41012caAnders Carlsson                                 Loc, GNUSyntax, Init.takeAs<Expr>());
2267dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
22684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().C99)
22692d75bbd36e273f7b958aefc766424220ad158bd9Douglas Gregor    Diag(DIE->getLocStart(), diag::ext_designated_init)
22702d75bbd36e273f7b958aefc766424220ad158bd9Douglas Gregor      << DIE->getSourceRange();
2271dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
227205c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor  return Owned(DIE);
227305c13a3411782108d65aab3c77b1a231a4963bc0Douglas Gregor}
2274c34ee5ef2b267a683c432ba0c342f7c3a14889d6Douglas Gregor
2275cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor//===----------------------------------------------------------------------===//
2276cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor// Initialization entity
2277cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor//===----------------------------------------------------------------------===//
227839da0b8145eaec7da7004f9b3645c5c9f4f63b1dDouglas Gregor
2279dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiInitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index,
2280cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor                                     const InitializedEntity &Parent)
2281dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  : Parent(&Parent), Index(Index)
2282cb57fb9f91e0976f4a3382b89a2734ffa50eb6fbDouglas Gregor{
2283d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) {
2284d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson    Kind = EK_ArrayElement;
2285d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    Type = AT->getElementType();
22860c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) {
2287d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson    Kind = EK_VectorElement;
22880c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    Type = VT->getElementType();
22890c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  } else {
22900c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    const ComplexType *CT = Parent.getType()->getAs<ComplexType>();
22910c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    assert(CT && "Unexpected type");
22920c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    Kind = EK_ComplexElement;
22930c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    Type = CT->getElementType();
2294d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  }
229520093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
229620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2297dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiInitializedEntity InitializedEntity::InitializeBase(ASTContext &Context,
2298711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson                                                    CXXBaseSpecifier *Base,
2299711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson                                                    bool IsInheritedVirtualBase)
230020093b4bf698f292c664676987541d5103b65b15Douglas Gregor{
230120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  InitializedEntity Result;
230220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Result.Kind = EK_Base;
2303711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson  Result.Base = reinterpret_cast<uintptr_t>(Base);
2304711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson  if (IsInheritedVirtualBase)
2305711f34adb886cce8ba86c7b1b6513a1eaaf63bb5Anders Carlsson    Result.Base |= 0x01;
2306dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2307d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  Result.Type = Base->getType();
230820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  return Result;
230920093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
231020093b4bf698f292c664676987541d5103b65b15Douglas Gregor
231199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas GregorDeclarationName InitializedEntity::getName() const {
231299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  switch (getKind()) {
2313f85e193739c953358c865005855253af4f68a497John McCall  case EK_Parameter: {
2314f85e193739c953358c865005855253af4f68a497John McCall    ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1);
2315f85e193739c953358c865005855253af4f68a497John McCall    return (D ? D->getDeclName() : DeclarationName());
2316f85e193739c953358c865005855253af4f68a497John McCall  }
2317a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor
2318a188ff2d8a18140541fcd5884deda4552dac71a7Douglas Gregor  case EK_Variable:
231999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Member:
232099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return VariableOrMember->getDeclName();
232199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
23224773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor  case EK_LambdaCapture:
23234773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor    return Capture.Var->getDeclName();
23244773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor
232599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Result:
232699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Exception:
232718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case EK_New:
232899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Temporary:
232999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case EK_Base:
2330059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  case EK_Delegating:
2331d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case EK_ArrayElement:
2332d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case EK_VectorElement:
23330c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  case EK_ComplexElement:
2334310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian  case EK_BlockElement:
233599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return DeclarationName();
233699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  }
2337dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
23387530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid EntityKind!");
233999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor}
234099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
23417abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas GregorDeclaratorDecl *InitializedEntity::getDecl() const {
23427abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  switch (getKind()) {
23437abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Variable:
23447abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Member:
23457abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor    return VariableOrMember;
23467abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor
2347f85e193739c953358c865005855253af4f68a497John McCall  case EK_Parameter:
2348f85e193739c953358c865005855253af4f68a497John McCall    return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1);
2349f85e193739c953358c865005855253af4f68a497John McCall
23507abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Result:
23517abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Exception:
23527abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_New:
23537abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Temporary:
23547abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  case EK_Base:
2355059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  case EK_Delegating:
2356d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case EK_ArrayElement:
2357d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case EK_VectorElement:
23580c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  case EK_ComplexElement:
2359310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian  case EK_BlockElement:
23604773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor  case EK_LambdaCapture:
23617abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor    return 0;
23627abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  }
2363dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
23647530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid EntityKind!");
23657abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor}
23667abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor
23673c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregorbool InitializedEntity::allowsNRVO() const {
23683c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  switch (getKind()) {
23693c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  case EK_Result:
23703c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  case EK_Exception:
23713c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    return LocAndNRVO.NRVO;
2372dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
23733c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  case EK_Variable:
23743c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  case EK_Parameter:
23753c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  case EK_Member:
23763c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  case EK_New:
23773c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  case EK_Temporary:
23783c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  case EK_Base:
2379059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  case EK_Delegating:
23803c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  case EK_ArrayElement:
23813c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  case EK_VectorElement:
23820c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  case EK_ComplexElement:
2383310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian  case EK_BlockElement:
23844773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor  case EK_LambdaCapture:
23853c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor    break;
23863c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  }
23873c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor
23883c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  return false;
23893c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor}
23903c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor
239120093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
239220093b4bf698f292c664676987541d5103b65b15Douglas Gregor// Initialization sequence
239320093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
239420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
239520093b4bf698f292c664676987541d5103b65b15Douglas Gregorvoid InitializationSequence::Step::Destroy() {
239620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  switch (Kind) {
239720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_ResolveAddressOfOverloadedFunction:
239820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_CastDerivedToBaseRValue:
2399906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  case SK_CastDerivedToBaseXValue:
240020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_CastDerivedToBaseLValue:
240120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_BindReference:
240220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_BindReferenceToTemporary:
2403523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  case SK_ExtraneousCopyToTemporary:
240420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_UserConversion:
240520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_QualificationConversionRValue:
2406906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  case SK_QualificationConversionXValue:
240720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_QualificationConversionLValue:
2408d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  case SK_ListInitialization:
24098713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl  case SK_ListConstructorCall:
241013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  case SK_UnwrapInitList:
241113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  case SK_RewrapInitList:
241251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  case SK_ConstructorInitialization:
241371d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  case SK_ZeroInitialization:
241418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_CAssignment:
2415cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  case SK_StringInit:
2416569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  case SK_ObjCObjectConversion:
2417cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  case SK_ArrayInit:
24180f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith  case SK_ParenthesizedArrayInit:
2419f85e193739c953358c865005855253af4f68a497John McCall  case SK_PassByIndirectCopyRestore:
2420f85e193739c953358c865005855253af4f68a497John McCall  case SK_PassByIndirectRestore:
2421f85e193739c953358c865005855253af4f68a497John McCall  case SK_ProduceObjCObject:
24222b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl  case SK_StdInitializerList:
242320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
2424dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
242520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case SK_ConversionSequence:
242620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    delete ICS;
242720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
242820093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
242920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2430b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregorbool InitializationSequence::isDirectReferenceBinding() const {
24313b80232b50c29b245e674f5aa02047b408e41018Sebastian Redl  return !Steps.empty() && Steps.back().Kind == SK_BindReference;
2432b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor}
2433b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor
2434b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregorbool InitializationSequence::isAmbiguous() const {
2435d695d6bb7323672e29dbb1556a3dafde3d3b2732Sebastian Redl  if (!Failed())
2436b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    return false;
2437dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2438b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  switch (getFailureKind()) {
2439b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_TooManyInitsForReference:
2440b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_ArrayNeedsInitList:
2441b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_ArrayNeedsInitListOrStringLiteral:
2442b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_AddressOfOverloadFailed: // FIXME: Could do better
2443b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_NonConstLValueReferenceBindingToTemporary:
2444b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_NonConstLValueReferenceBindingToUnrelated:
2445b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_RValueReferenceBindingToLValue:
2446b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_ReferenceInitDropsQualifiers:
2447b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_ReferenceInitFailed:
2448b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_ConversionFailed:
2449429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  case FK_ConversionFromPropertyFailed:
2450b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_TooManyInitsForScalar:
2451b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_ReferenceBindingToInitList:
2452b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_InitListBadDestinationType:
2453b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_DefaultInitOfConst:
245472a43bbf6802c8fcfd04dcb2be8eafcb0b8fe29cDouglas Gregor  case FK_Incomplete:
2455cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  case FK_ArrayTypeMismatch:
2456cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  case FK_NonConstantArrayInit:
24578713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl  case FK_ListInitializationFailed:
245873076431605556fdbf28d287d084a73a24a8b8d4John McCall  case FK_VariableLengthArrayHasInitializer:
24595acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall  case FK_PlaceholderType:
24602b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl  case FK_InitListElementCopyFailure:
246170e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl  case FK_ExplicitConstructor:
2462b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    return false;
2463dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2464b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_ReferenceInitOverloadFailed:
2465b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_UserConversionOverloadFailed:
2466b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  case FK_ConstructorOverloadFailed:
2467cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl  case FK_ListConstructorOverloadFailed:
2468b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor    return FailedOverloadResult == OR_Ambiguous;
2469b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor  }
2470dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
24717530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid EntityKind!");
2472b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor}
2473b70cf44bf1b1956e0c6b98373c4f69b23afa0052Douglas Gregor
2474d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregorbool InitializationSequence::isConstructorInitialization() const {
2475d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor  return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization;
2476d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor}
2477d6e44a3c4193bd422bfa78c8086fb16bb2168e34Douglas Gregor
247822c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnaravoid
247922c107b2b99887b5aec6d1fd38210031e944e31fAbramo BagnaraInitializationSequence
248022c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara::AddAddressOverloadResolutionStep(FunctionDecl *Function,
248122c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                   DeclAccessPair Found,
248222c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                   bool HadMultipleCandidates) {
248320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
248420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Kind = SK_ResolveAddressOfOverloadedFunction;
248520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Type = Function->getType();
248622c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara  S.Function.HadMultipleCandidates = HadMultipleCandidates;
24879aa472c45d2bd81b7b52c225e8acc560d716db97John McCall  S.Function.Function = Function;
24886bb8017bb9e828d118e15e59d71c66bba323c364John McCall  S.Function.FoundDecl = Found;
248920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
249020093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
249120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2492dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumivoid InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType,
24935baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                                      ExprValueKind VK) {
249420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
24955baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  switch (VK) {
24965baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break;
24975baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break;
24985baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break;
2499906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  }
250020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Type = BaseType;
250120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
250220093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
250320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2504dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumivoid InitializationSequence::AddReferenceBindingStep(QualType T,
250520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                     bool BindingTemporary) {
250620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
250720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference;
250820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Type = T;
250920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
251020093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
251120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2512523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregorvoid InitializationSequence::AddExtraneousCopyToTemporary(QualType T) {
2513523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  Step S;
2514523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  S.Kind = SK_ExtraneousCopyToTemporary;
2515523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  S.Type = T;
2516523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  Steps.push_back(S);
2517523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor}
2518523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
251922c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnaravoid
252022c107b2b99887b5aec6d1fd38210031e944e31fAbramo BagnaraInitializationSequence::AddUserConversionStep(FunctionDecl *Function,
252122c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                              DeclAccessPair FoundDecl,
252222c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                              QualType T,
252322c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                              bool HadMultipleCandidates) {
252420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
252520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Kind = SK_UserConversion;
252603981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman  S.Type = T;
252722c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara  S.Function.HadMultipleCandidates = HadMultipleCandidates;
25289aa472c45d2bd81b7b52c225e8acc560d716db97John McCall  S.Function.Function = Function;
25299aa472c45d2bd81b7b52c225e8acc560d716db97John McCall  S.Function.FoundDecl = FoundDecl;
253020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
253120093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
253220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
253320093b4bf698f292c664676987541d5103b65b15Douglas Gregorvoid InitializationSequence::AddQualificationConversionStep(QualType Ty,
25345baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                                            ExprValueKind VK) {
253520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
253638a4ffe5244e6be46f1b1f8394342a77836be776John McCall  S.Kind = SK_QualificationConversionRValue; // work around a gcc warning
25375baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  switch (VK) {
25385baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  case VK_RValue:
2539906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    S.Kind = SK_QualificationConversionRValue;
2540906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    break;
25415baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  case VK_XValue:
2542906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    S.Kind = SK_QualificationConversionXValue;
2543906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    break;
25445baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  case VK_LValue:
2545906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    S.Kind = SK_QualificationConversionLValue;
2546906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    break;
2547906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  }
254820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Type = Ty;
254920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
255020093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
255120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
255220093b4bf698f292c664676987541d5103b65b15Douglas Gregorvoid InitializationSequence::AddConversionSequenceStep(
255320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       const ImplicitConversionSequence &ICS,
255420093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       QualType T) {
255520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Step S;
255620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Kind = SK_ConversionSequence;
255720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.Type = T;
255820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  S.ICS = new ImplicitConversionSequence(ICS);
255920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Steps.push_back(S);
256020093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
256120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2562d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregorvoid InitializationSequence::AddListInitializationStep(QualType T) {
2563d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  Step S;
2564d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  S.Kind = SK_ListInitialization;
2565d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  S.Type = T;
2566d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  Steps.push_back(S);
2567d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor}
2568d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
2569dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumivoid
257022c107b2b99887b5aec6d1fd38210031e944e31fAbramo BagnaraInitializationSequence
257122c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara::AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
257222c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                   AccessSpecifier Access,
257322c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                   QualType T,
257410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                   bool HadMultipleCandidates,
25756cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl                                   bool FromInitList, bool AsInitList) {
257651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  Step S;
25776cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall
25786cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl                                       : SK_ConstructorInitialization;
257951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  S.Type = T;
258022c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara  S.Function.HadMultipleCandidates = HadMultipleCandidates;
25819aa472c45d2bd81b7b52c225e8acc560d716db97John McCall  S.Function.Function = Constructor;
25829aa472c45d2bd81b7b52c225e8acc560d716db97John McCall  S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access);
258351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  Steps.push_back(S);
258451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor}
258551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
258671d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregorvoid InitializationSequence::AddZeroInitializationStep(QualType T) {
258771d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  Step S;
258871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  S.Kind = SK_ZeroInitialization;
258971d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  S.Type = T;
259071d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  Steps.push_back(S);
259171d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor}
259271d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
259318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregorvoid InitializationSequence::AddCAssignmentStep(QualType T) {
259418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  Step S;
259518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  S.Kind = SK_CAssignment;
259618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  S.Type = T;
259718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  Steps.push_back(S);
259818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor}
259918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
2600cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedmanvoid InitializationSequence::AddStringInitStep(QualType T) {
2601cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  Step S;
2602cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  S.Kind = SK_StringInit;
2603cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  S.Type = T;
2604cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  Steps.push_back(S);
2605cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman}
2606cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
2607569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregorvoid InitializationSequence::AddObjCObjectConversionStep(QualType T) {
2608569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  Step S;
2609569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  S.Kind = SK_ObjCObjectConversion;
2610569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  S.Type = T;
2611569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  Steps.push_back(S);
2612569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor}
2613569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
2614cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregorvoid InitializationSequence::AddArrayInitStep(QualType T) {
2615cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  Step S;
2616cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  S.Kind = SK_ArrayInit;
2617cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  S.Type = T;
2618cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  Steps.push_back(S);
2619cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor}
2620cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
26210f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smithvoid InitializationSequence::AddParenthesizedArrayInitStep(QualType T) {
26220f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith  Step S;
26230f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith  S.Kind = SK_ParenthesizedArrayInit;
26240f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith  S.Type = T;
26250f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith  Steps.push_back(S);
26260f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith}
26270f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith
2628f85e193739c953358c865005855253af4f68a497John McCallvoid InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type,
2629f85e193739c953358c865005855253af4f68a497John McCall                                                              bool shouldCopy) {
2630f85e193739c953358c865005855253af4f68a497John McCall  Step s;
2631f85e193739c953358c865005855253af4f68a497John McCall  s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore
2632f85e193739c953358c865005855253af4f68a497John McCall                       : SK_PassByIndirectRestore);
2633f85e193739c953358c865005855253af4f68a497John McCall  s.Type = type;
2634f85e193739c953358c865005855253af4f68a497John McCall  Steps.push_back(s);
2635f85e193739c953358c865005855253af4f68a497John McCall}
2636f85e193739c953358c865005855253af4f68a497John McCall
2637f85e193739c953358c865005855253af4f68a497John McCallvoid InitializationSequence::AddProduceObjCObjectStep(QualType T) {
2638f85e193739c953358c865005855253af4f68a497John McCall  Step S;
2639f85e193739c953358c865005855253af4f68a497John McCall  S.Kind = SK_ProduceObjCObject;
2640f85e193739c953358c865005855253af4f68a497John McCall  S.Type = T;
2641f85e193739c953358c865005855253af4f68a497John McCall  Steps.push_back(S);
2642f85e193739c953358c865005855253af4f68a497John McCall}
2643f85e193739c953358c865005855253af4f68a497John McCall
26442b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redlvoid InitializationSequence::AddStdInitializerListConstructionStep(QualType T) {
26452b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl  Step S;
26462b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl  S.Kind = SK_StdInitializerList;
26472b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl  S.Type = T;
26482b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl  Steps.push_back(S);
26492b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl}
26502b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl
265113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redlvoid InitializationSequence::RewrapReferenceInitList(QualType T,
265213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                                     InitListExpr *Syntactic) {
265313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  assert(Syntactic->getNumInits() == 1 &&
265413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl         "Can only rewrap trivial init lists.");
265513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  Step S;
265613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  S.Kind = SK_UnwrapInitList;
265713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  S.Type = Syntactic->getInit(0)->getType();
265813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  Steps.insert(Steps.begin(), S);
265913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
266013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  S.Kind = SK_RewrapInitList;
266113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  S.Type = T;
266213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  S.WrappingSyntacticList = Syntactic;
266313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  Steps.push_back(S);
266413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl}
266513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
2666dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumivoid InitializationSequence::SetOverloadFailure(FailureKind Failure,
266720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                OverloadingResult Result) {
26687491c499e826682e128a400038361ebcbde30eecSebastian Redl  setSequenceKind(FailedSequence);
266920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  this->Failure = Failure;
267020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  this->FailedOverloadResult = Result;
267120093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
267220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
267320093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
267420093b4bf698f292c664676987541d5103b65b15Douglas Gregor// Attempt initialization
267520093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
267620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
2677f85e193739c953358c865005855253af4f68a497John McCallstatic void MaybeProduceObjCObject(Sema &S,
2678f85e193739c953358c865005855253af4f68a497John McCall                                   InitializationSequence &Sequence,
2679f85e193739c953358c865005855253af4f68a497John McCall                                   const InitializedEntity &Entity) {
26804e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().ObjCAutoRefCount) return;
2681f85e193739c953358c865005855253af4f68a497John McCall
2682f85e193739c953358c865005855253af4f68a497John McCall  /// When initializing a parameter, produce the value if it's marked
2683f85e193739c953358c865005855253af4f68a497John McCall  /// __attribute__((ns_consumed)).
2684f85e193739c953358c865005855253af4f68a497John McCall  if (Entity.getKind() == InitializedEntity::EK_Parameter) {
2685f85e193739c953358c865005855253af4f68a497John McCall    if (!Entity.isParameterConsumed())
2686f85e193739c953358c865005855253af4f68a497John McCall      return;
2687f85e193739c953358c865005855253af4f68a497John McCall
2688f85e193739c953358c865005855253af4f68a497John McCall    assert(Entity.getType()->isObjCRetainableType() &&
2689f85e193739c953358c865005855253af4f68a497John McCall           "consuming an object of unretainable type?");
2690f85e193739c953358c865005855253af4f68a497John McCall    Sequence.AddProduceObjCObjectStep(Entity.getType());
2691f85e193739c953358c865005855253af4f68a497John McCall
2692f85e193739c953358c865005855253af4f68a497John McCall  /// When initializing a return value, if the return type is a
2693f85e193739c953358c865005855253af4f68a497John McCall  /// retainable type, then returns need to immediately retain the
2694f85e193739c953358c865005855253af4f68a497John McCall  /// object.  If an autorelease is required, it will be done at the
2695f85e193739c953358c865005855253af4f68a497John McCall  /// last instant.
2696f85e193739c953358c865005855253af4f68a497John McCall  } else if (Entity.getKind() == InitializedEntity::EK_Result) {
2697f85e193739c953358c865005855253af4f68a497John McCall    if (!Entity.getType()->isObjCRetainableType())
2698f85e193739c953358c865005855253af4f68a497John McCall      return;
2699f85e193739c953358c865005855253af4f68a497John McCall
2700f85e193739c953358c865005855253af4f68a497John McCall    Sequence.AddProduceObjCObjectStep(Entity.getType());
2701f85e193739c953358c865005855253af4f68a497John McCall  }
2702f85e193739c953358c865005855253af4f68a497John McCall}
2703f85e193739c953358c865005855253af4f68a497John McCall
2704f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith/// \brief When initializing from init list via constructor, handle
2705f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith/// initialization of an object of type std::initializer_list<T>.
270610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl///
2707f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith/// \return true if we have handled initialization of an object of type
2708f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith/// std::initializer_list<T>, false otherwise.
2709f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smithstatic bool TryInitializerListConstruction(Sema &S,
2710f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                           InitListExpr *List,
2711f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                           QualType DestType,
2712f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                           InitializationSequence &Sequence) {
2713f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  QualType E;
2714f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  if (!S.isStdInitializerList(DestType, &E))
27151d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith    return false;
27161d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith
2717f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  // Check that each individual element can be copy-constructed. But since we
2718f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  // have no place to store further information, we'll recalculate everything
2719f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  // later.
2720f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary(
2721f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      S.Context.getConstantArrayType(E,
2722f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith          llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()),
2723f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                      List->getNumInits()),
2724f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith          ArrayType::Normal, 0));
2725f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  InitializedEntity Element = InitializedEntity::InitializeElement(S.Context,
2726f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      0, HiddenArray);
2727f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  for (unsigned i = 0, n = List->getNumInits(); i < n; ++i) {
2728f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith    Element.setElementIndex(i);
2729f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith    if (!S.CanPerformCopyInitialization(Element, List->getInit(i))) {
2730f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      Sequence.SetFailed(
2731f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith          InitializationSequence::FK_InitListElementCopyFailure);
273210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      return true;
273310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    }
273410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  }
2735f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  Sequence.AddStdInitializerListConstructionStep(DestType);
2736f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  return true;
273710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl}
273810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
273996715b288060c8e168cbca62f366c12e88c5483dSebastian Redlstatic OverloadingResult
274096715b288060c8e168cbca62f366c12e88c5483dSebastian RedlResolveConstructorOverload(Sema &S, SourceLocation DeclLoc,
274196715b288060c8e168cbca62f366c12e88c5483dSebastian Redl                           Expr **Args, unsigned NumArgs,
274296715b288060c8e168cbca62f366c12e88c5483dSebastian Redl                           OverloadCandidateSet &CandidateSet,
274396715b288060c8e168cbca62f366c12e88c5483dSebastian Redl                           DeclContext::lookup_iterator Con,
274496715b288060c8e168cbca62f366c12e88c5483dSebastian Redl                           DeclContext::lookup_iterator ConEnd,
274596715b288060c8e168cbca62f366c12e88c5483dSebastian Redl                           OverloadCandidateSet::iterator &Best,
274696715b288060c8e168cbca62f366c12e88c5483dSebastian Redl                           bool CopyInitializing, bool AllowExplicit,
274751ad9cd0644c9acf442f049aed98b66f7b1041a2Sebastian Redl                           bool OnlyListConstructors, bool InitListSyntax) {
274896715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  CandidateSet.clear();
274996715b288060c8e168cbca62f366c12e88c5483dSebastian Redl
275096715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  for (; Con != ConEnd; ++Con) {
275196715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    NamedDecl *D = *Con;
275296715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
275396715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    bool SuppressUserConversions = false;
275496715b288060c8e168cbca62f366c12e88c5483dSebastian Redl
275596715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    // Find the constructor (which may be a template).
275696715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    CXXConstructorDecl *Constructor = 0;
275796715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
275896715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    if (ConstructorTmpl)
275996715b288060c8e168cbca62f366c12e88c5483dSebastian Redl      Constructor = cast<CXXConstructorDecl>(
276096715b288060c8e168cbca62f366c12e88c5483dSebastian Redl                                           ConstructorTmpl->getTemplatedDecl());
276196715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    else {
276296715b288060c8e168cbca62f366c12e88c5483dSebastian Redl      Constructor = cast<CXXConstructorDecl>(D);
276396715b288060c8e168cbca62f366c12e88c5483dSebastian Redl
276496715b288060c8e168cbca62f366c12e88c5483dSebastian Redl      // If we're performing copy initialization using a copy constructor, we
276551ad9cd0644c9acf442f049aed98b66f7b1041a2Sebastian Redl      // suppress user-defined conversions on the arguments. We do the same for
276651ad9cd0644c9acf442f049aed98b66f7b1041a2Sebastian Redl      // move constructors.
276751ad9cd0644c9acf442f049aed98b66f7b1041a2Sebastian Redl      if ((CopyInitializing || (InitListSyntax && NumArgs == 1)) &&
276851ad9cd0644c9acf442f049aed98b66f7b1041a2Sebastian Redl          Constructor->isCopyOrMoveConstructor())
276996715b288060c8e168cbca62f366c12e88c5483dSebastian Redl        SuppressUserConversions = true;
277096715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    }
277196715b288060c8e168cbca62f366c12e88c5483dSebastian Redl
277296715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    if (!Constructor->isInvalidDecl() &&
277396715b288060c8e168cbca62f366c12e88c5483dSebastian Redl        (AllowExplicit || !Constructor->isExplicit()) &&
27746cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl        (!OnlyListConstructors || S.isInitListConstructor(Constructor))) {
277596715b288060c8e168cbca62f366c12e88c5483dSebastian Redl      if (ConstructorTmpl)
277696715b288060c8e168cbca62f366c12e88c5483dSebastian Redl        S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
277796715b288060c8e168cbca62f366c12e88c5483dSebastian Redl                                       /*ExplicitArgs*/ 0,
277851ad9cd0644c9acf442f049aed98b66f7b1041a2Sebastian Redl                                       llvm::makeArrayRef(Args, NumArgs),
277951ad9cd0644c9acf442f049aed98b66f7b1041a2Sebastian Redl                                       CandidateSet, SuppressUserConversions);
2780ed878af7914df535b32d64f555fa118413186672Douglas Gregor      else {
2781ed878af7914df535b32d64f555fa118413186672Douglas Gregor        // C++ [over.match.copy]p1:
2782ed878af7914df535b32d64f555fa118413186672Douglas Gregor        //   - When initializing a temporary to be bound to the first parameter
2783ed878af7914df535b32d64f555fa118413186672Douglas Gregor        //     of a constructor that takes a reference to possibly cv-qualified
2784ed878af7914df535b32d64f555fa118413186672Douglas Gregor        //     T as its first argument, called with a single argument in the
2785ed878af7914df535b32d64f555fa118413186672Douglas Gregor        //     context of direct-initialization, explicit conversion functions
2786ed878af7914df535b32d64f555fa118413186672Douglas Gregor        //     are also considered.
2787ed878af7914df535b32d64f555fa118413186672Douglas Gregor        bool AllowExplicitConv = AllowExplicit && !CopyInitializing &&
2788ed878af7914df535b32d64f555fa118413186672Douglas Gregor                                 NumArgs == 1 &&
2789ed878af7914df535b32d64f555fa118413186672Douglas Gregor                                 Constructor->isCopyOrMoveConstructor();
279096715b288060c8e168cbca62f366c12e88c5483dSebastian Redl        S.AddOverloadCandidate(Constructor, FoundDecl,
279113a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                               llvm::makeArrayRef(Args, NumArgs), CandidateSet,
2792ed878af7914df535b32d64f555fa118413186672Douglas Gregor                               SuppressUserConversions,
2793ed878af7914df535b32d64f555fa118413186672Douglas Gregor                               /*PartialOverloading=*/false,
2794ed878af7914df535b32d64f555fa118413186672Douglas Gregor                               /*AllowExplicit=*/AllowExplicitConv);
2795ed878af7914df535b32d64f555fa118413186672Douglas Gregor      }
279696715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    }
279796715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  }
279896715b288060c8e168cbca62f366c12e88c5483dSebastian Redl
279996715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  // Perform overload resolution and return the result.
280096715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  return CandidateSet.BestViableFunction(S, DeclLoc, Best);
280196715b288060c8e168cbca62f366c12e88c5483dSebastian Redl}
280296715b288060c8e168cbca62f366c12e88c5483dSebastian Redl
280310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl/// \brief Attempt initialization by constructor (C++ [dcl.init]), which
280410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl/// enumerates the constructors of the initialized entity and performs overload
280510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl/// resolution to select the best.
280608ae369fbe870cfb5077fbd1c652836b8dbeab8bSebastian Redl/// If InitListSyntax is true, this is list-initialization of a non-aggregate
280710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl/// class type.
280810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redlstatic void TryConstructorInitialization(Sema &S,
280910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                         const InitializedEntity &Entity,
281010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                         const InitializationKind &Kind,
281110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                         Expr **Args, unsigned NumArgs,
281210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                         QualType DestType,
281310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                         InitializationSequence &Sequence,
281408ae369fbe870cfb5077fbd1c652836b8dbeab8bSebastian Redl                                         bool InitListSyntax = false) {
281508ae369fbe870cfb5077fbd1c652836b8dbeab8bSebastian Redl  assert((!InitListSyntax || (NumArgs == 1 && isa<InitListExpr>(Args[0]))) &&
281608ae369fbe870cfb5077fbd1c652836b8dbeab8bSebastian Redl         "InitListSyntax must come with a single initializer list argument.");
281708ae369fbe870cfb5077fbd1c652836b8dbeab8bSebastian Redl
281810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  // Check constructor arguments for self reference.
281910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  if (DeclaratorDecl *DD = Entity.getDecl())
282010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    // Parameters arguments are occassionially constructed with itself,
282110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    // for instance, in recursive functions.  Skip them.
282210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    if (!isa<ParmVarDecl>(DD))
282310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      for (unsigned i = 0; i < NumArgs; ++i)
282410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl        S.CheckSelfReference(DD, Args[i]);
282510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
282610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  // The type we're constructing needs to be complete.
282710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
282869a30b838c723cb1850de55cfa48a402cfeeb6e0Douglas Gregor    Sequence.setIncompleteTypeFailure(DestType);
282996715b288060c8e168cbca62f366c12e88c5483dSebastian Redl    return;
283010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  }
283110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
283210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  const RecordType *DestRecordType = DestType->getAs<RecordType>();
283310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  assert(DestRecordType && "Constructor initialization requires record type");
283410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  CXXRecordDecl *DestRecordDecl
283510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    = cast<CXXRecordDecl>(DestRecordType->getDecl());
283610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
283796715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  // Build the candidate set directly in the initialization sequence
283896715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  // structure, so that it will persist if we fail.
283996715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
284096715b288060c8e168cbca62f366c12e88c5483dSebastian Redl
284196715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  // Determine whether we are allowed to call explicit constructors or
284296715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  // explicit conversion operators.
284370e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl  bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax;
28446cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy;
284508ae369fbe870cfb5077fbd1c652836b8dbeab8bSebastian Redl
284610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  //   - Otherwise, if T is a class type, constructors are considered. The
284710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  //     applicable constructors are enumerated, and the best one is chosen
284810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  //     through overload resolution.
284996715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  DeclContext::lookup_iterator ConStart, ConEnd;
285096715b288060c8e168cbca62f366c12e88c5483dSebastian Redl  llvm::tie(ConStart, ConEnd) = S.LookupConstructors(DestRecordDecl);
285110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
28526cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  OverloadingResult Result = OR_No_Viable_Function;
285310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  OverloadCandidateSet::iterator Best;
28546cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  bool AsInitializerList = false;
28556cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl
28566cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  // C++11 [over.match.list]p1:
28576cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  //   When objects of non-aggregate type T are list-initialized, overload
28586cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  //   resolution selects the constructor in two phases:
28596cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  //   - Initially, the candidate functions are the initializer-list
28606cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  //     constructors of the class T and the argument list consists of the
28616cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  //     initializer list as a single argument.
28626cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  if (InitListSyntax) {
2863f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith    InitListExpr *ILE = cast<InitListExpr>(Args[0]);
28646cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl    AsInitializerList = true;
2865f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith
2866f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith    // If the initializer list has no elements and T has a default constructor,
2867f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith    // the first phase is omitted.
2868f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith    if (ILE->getNumInits() != 0 ||
2869f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        (!DestRecordDecl->hasDeclaredDefaultConstructor() &&
2870f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith         !DestRecordDecl->needsImplicitDefaultConstructor()))
2871f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, NumArgs,
2872f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                          CandidateSet, ConStart, ConEnd, Best,
2873f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                          CopyInitialization, AllowExplicit,
2874f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                          /*OnlyListConstructor=*/true,
2875f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                          InitListSyntax);
28766cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl
28776cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl    // Time to unwrap the init list.
28786cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl    Args = ILE->getInits();
28796cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl    NumArgs = ILE->getNumInits();
28806cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  }
28816cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl
28826cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  // C++11 [over.match.list]p1:
28836cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  //   - If no viable initializer-list constructor is found, overload resolution
28846cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  //     is performed again, where the candidate functions are all the
2885f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  //     constructors of the class T and the argument list consists of the
28866cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  //     elements of the initializer list.
28876cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  if (Result == OR_No_Viable_Function) {
28886cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl    AsInitializerList = false;
28896cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl    Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, NumArgs,
28906cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl                                        CandidateSet, ConStart, ConEnd, Best,
28916cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl                                        CopyInitialization, AllowExplicit,
289251ad9cd0644c9acf442f049aed98b66f7b1041a2Sebastian Redl                                        /*OnlyListConstructors=*/false,
289351ad9cd0644c9acf442f049aed98b66f7b1041a2Sebastian Redl                                        InitListSyntax);
28946cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  }
28956cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl  if (Result) {
289608ae369fbe870cfb5077fbd1c652836b8dbeab8bSebastian Redl    Sequence.SetOverloadFailure(InitListSyntax ?
2897cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl                      InitializationSequence::FK_ListConstructorOverloadFailed :
2898cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl                      InitializationSequence::FK_ConstructorOverloadFailed,
289910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                Result);
290010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    return;
290110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  }
290210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
2903f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  // C++11 [dcl.init]p6:
290410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  //   If a program calls for the default initialization of an object
290510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  //   of a const-qualified type T, T shall be a class type with a
290610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  //   user-provided default constructor.
290710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  if (Kind.getKind() == InitializationKind::IK_Default &&
290810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      Entity.getType().isConstQualified() &&
290910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      cast<CXXConstructorDecl>(Best->Function)->isImplicit()) {
291010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
291110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    return;
291210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  }
291310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
291470e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl  // C++11 [over.match.list]p1:
291570e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl  //   In copy-list-initialization, if an explicit constructor is chosen, the
291670e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl  //   initializer is ill-formed.
291770e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl  CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function);
291870e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl  if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) {
291970e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl    Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor);
292070e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl    return;
292170e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl  }
292270e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl
292310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  // Add the constructor initialization step. Any cv-qualification conversion is
292410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  // subsumed by the initialization.
292510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  bool HadMultipleCandidates = (CandidateSet.size() > 1);
292610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  Sequence.AddConstructorInitializationStep(CtorDecl,
292710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                            Best->FoundDecl.getAccess(),
292810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                            DestType, HadMultipleCandidates,
29296cd03dbb310f472b14b8d6d69d8c9b5b7f1200e2Sebastian Redl                                            InitListSyntax, AsInitializerList);
293010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl}
293110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
293213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redlstatic bool
293313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian RedlResolveOverloadedFunctionForReferenceBinding(Sema &S,
293413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                             Expr *Initializer,
293513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                             QualType &SourceType,
293613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                             QualType &UnqualifiedSourceType,
293713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                             QualType UnqualifiedTargetType,
293813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                             InitializationSequence &Sequence) {
293913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  if (S.Context.getCanonicalType(UnqualifiedSourceType) ==
294013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl        S.Context.OverloadTy) {
294113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    DeclAccessPair Found;
294213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    bool HadMultipleCandidates = false;
294313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    if (FunctionDecl *Fn
294413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl        = S.ResolveAddressOfOverloadedFunction(Initializer,
294513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                               UnqualifiedTargetType,
294613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                               false, Found,
294713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                               &HadMultipleCandidates)) {
294813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      Sequence.AddAddressOverloadResolutionStep(Fn, Found,
294913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                                HadMultipleCandidates);
295013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      SourceType = Fn->getType();
295113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      UnqualifiedSourceType = SourceType.getUnqualifiedType();
295213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    } else if (!UnqualifiedTargetType->isRecordType()) {
295313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
295413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      return true;
295513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    }
295613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  }
295713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  return false;
295813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl}
295913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
296013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redlstatic void TryReferenceInitializationCore(Sema &S,
296113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           const InitializedEntity &Entity,
296213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           const InitializationKind &Kind,
296313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           Expr *Initializer,
296413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           QualType cv1T1, QualType T1,
296513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           Qualifiers T1Quals,
296613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           QualType cv2T2, QualType T2,
296713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           Qualifiers T2Quals,
296813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           InitializationSequence &Sequence);
296913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
2970f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smithstatic void TryValueInitialization(Sema &S,
2971f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                   const InitializedEntity &Entity,
2972f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                   const InitializationKind &Kind,
2973f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                   InitializationSequence &Sequence,
2974f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                   InitListExpr *InitList = 0);
2975f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith
297613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redlstatic void TryListInitialization(Sema &S,
297713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                  const InitializedEntity &Entity,
297813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                  const InitializationKind &Kind,
297913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                  InitListExpr *InitList,
298013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                  InitializationSequence &Sequence);
298113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
298213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl/// \brief Attempt list initialization of a reference.
298313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redlstatic void TryReferenceListInitialization(Sema &S,
298413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           const InitializedEntity &Entity,
298513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           const InitializationKind &Kind,
298613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           InitListExpr *InitList,
298713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           InitializationSequence &Sequence)
298813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl{
298913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  // First, catch C++03 where this isn't possible.
29904e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().CPlusPlus0x) {
299113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList);
299213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    return;
299313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  }
299413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
299513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  QualType DestType = Entity.getType();
299613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
299713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  Qualifiers T1Quals;
299813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
299913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
300013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  // Reference initialization via an initializer list works thus:
300113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  // If the initializer list consists of a single element that is
300213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  // reference-related to the referenced type, bind directly to that element
300313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  // (possibly creating temporaries).
300413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  // Otherwise, initialize a temporary with the initializer list and
300513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  // bind to that.
300613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  if (InitList->getNumInits() == 1) {
300713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    Expr *Initializer = InitList->getInit(0);
300813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    QualType cv2T2 = Initializer->getType();
300913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    Qualifiers T2Quals;
301013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals);
301113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
301213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    // If this fails, creating a temporary wouldn't work either.
301313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2,
301413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                                     T1, Sequence))
301513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      return;
301613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
301713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    SourceLocation DeclLoc = Initializer->getLocStart();
301813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    bool dummy1, dummy2, dummy3;
301913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    Sema::ReferenceCompareResult RefRelationship
302013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1,
302113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                       dummy2, dummy3);
302213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    if (RefRelationship >= Sema::Ref_Related) {
302313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      // Try to bind the reference here.
302413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1,
302513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                     T1Quals, cv2T2, T2, T2Quals, Sequence);
302613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      if (Sequence)
302713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl        Sequence.RewrapReferenceInitList(cv1T1, InitList);
302813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      return;
302913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    }
303013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  }
303113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
303213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  // Not reference-related. Create a temporary and bind to that.
303313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1);
303413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
303513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  TryListInitialization(S, TempEntity, Kind, InitList, Sequence);
303613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  if (Sequence) {
303713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    if (DestType->isRValueReferenceType() ||
303813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl        (T1Quals.hasConst() && !T1Quals.hasVolatile()))
303913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
304013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    else
304113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      Sequence.SetFailed(
304213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl          InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
304313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  }
304413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl}
304513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
304612ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola/// \brief Attempt list initialization (C++0x [dcl.init.list])
304712ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindolastatic void TryListInitialization(Sema &S,
304812ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola                                  const InitializedEntity &Entity,
304912ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola                                  const InitializationKind &Kind,
305012ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola                                  InitListExpr *InitList,
305112ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola                                  InitializationSequence &Sequence) {
305212ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola  QualType DestType = Entity.getType();
305312ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola
305414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  // C++ doesn't allow scalar initialization with more than one argument.
305514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  // But C99 complex numbers are scalars and it makes sense there.
30564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (S.getLangOpts().CPlusPlus && DestType->isScalarType() &&
305714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      !DestType->isAnyComplexType() && InitList->getNumInits() > 1) {
305814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar);
305914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    return;
306014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
306114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (DestType->isReferenceType()) {
306213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence);
306312ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    return;
306414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
3065d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl  if (DestType->isRecordType()) {
3066d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor    if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) {
306769a30b838c723cb1850de55cfa48a402cfeeb6e0Douglas Gregor      Sequence.setIncompleteTypeFailure(DestType);
3068d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl      return;
3069d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl    }
3070d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl
3071f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith    // C++11 [dcl.init.list]p3:
3072f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith    //   - If T is an aggregate, aggregate initialization is performed.
3073d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl    if (!DestType->isAggregateType()) {
30744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (S.getLangOpts().CPlusPlus0x) {
3075f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        //   - Otherwise, if the initializer list has no elements and T is a
3076f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        //     class type with a default constructor, the object is
3077f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        //     value-initialized.
3078f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        if (InitList->getNumInits() == 0) {
3079f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith          CXXRecordDecl *RD = DestType->getAsCXXRecordDecl();
3080f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith          if (RD->hasDeclaredDefaultConstructor() ||
3081f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith              RD->needsImplicitDefaultConstructor()) {
3082f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith            TryValueInitialization(S, Entity, Kind, Sequence, InitList);
3083f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith            return;
3084f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith          }
3085f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        }
3086f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith
3087f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        //   - Otherwise, if T is a specialization of std::initializer_list<E>,
3088f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        //     an initializer_list object constructed [...]
3089f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        if (TryInitializerListConstruction(S, InitList, DestType, Sequence))
3090f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith          return;
3091f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith
3092f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        //   - Otherwise, if T is a class type, constructors are considered.
3093d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl        Expr *Arg = InitList;
3094f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        TryConstructorInitialization(S, Entity, Kind, &Arg, 1, DestType,
3095f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                     Sequence, /*InitListSyntax*/true);
3096d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl      } else
3097d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl        Sequence.SetFailed(
3098d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl            InitializationSequence::FK_InitListBadDestinationType);
3099d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl      return;
3100d2231c9424d6cbbcecb6f89cc2de5b987327aeaaSebastian Redl    }
310114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
310214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl
310314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  InitListChecker CheckInitList(S, Entity, InitList,
3104c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl          DestType, /*VerifyOnly=*/true,
3105168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl          Kind.getKind() != InitializationKind::IK_DirectList ||
31064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie            !S.getLangOpts().CPlusPlus0x);
310714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  if (CheckInitList.HadError()) {
310814b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed);
310914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    return;
311012ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola  }
311112ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola
311214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  // Add the list initialization step with the built init list.
311312ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola  Sequence.AddListInitializationStep(DestType);
311412ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola}
311520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
311620093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Try a reference initialization that involves calling a conversion
311720093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// function.
311820093b4bf698f292c664676987541d5103b65b15Douglas Gregorstatic OverloadingResult TryRefInitWithConversionFunction(Sema &S,
311920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                             const InitializedEntity &Entity,
312020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                             const InitializationKind &Kind,
3121ed878af7914df535b32d64f555fa118413186672Douglas Gregor                                             Expr *Initializer,
3122ed878af7914df535b32d64f555fa118413186672Douglas Gregor                                             bool AllowRValues,
312320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                             InitializationSequence &Sequence) {
3124d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
312520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
312620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType T1 = cv1T1.getUnqualifiedType();
312720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType cv2T2 = Initializer->getType();
312820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType T2 = cv2T2.getUnqualifiedType();
312920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
313020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool DerivedToBase;
3131569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  bool ObjCConversion;
3132f85e193739c953358c865005855253af4f68a497John McCall  bool ObjCLifetimeConversion;
3133dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  assert(!S.CompareReferenceRelationship(Initializer->getLocStart(),
3134569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                                         T1, T2, DerivedToBase,
3135f85e193739c953358c865005855253af4f68a497John McCall                                         ObjCConversion,
3136f85e193739c953358c865005855253af4f68a497John McCall                                         ObjCLifetimeConversion) &&
313720093b4bf698f292c664676987541d5103b65b15Douglas Gregor         "Must have incompatible references when binding via conversion");
313860cfcecaf48e4310339dcfbdb0e3f0e6d2853855Chandler Carruth  (void)DerivedToBase;
3139569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  (void)ObjCConversion;
3140f85e193739c953358c865005855253af4f68a497John McCall  (void)ObjCLifetimeConversion;
3141f85e193739c953358c865005855253af4f68a497John McCall
314220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Build the candidate set directly in the initialization sequence
314320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // structure, so that it will persist if we fail.
314420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
314520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  CandidateSet.clear();
314620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
314720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Determine whether we are allowed to call explicit constructors or
314820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // explicit conversion operators.
3149168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl  bool AllowExplicit = Kind.AllowExplicit();
3150ed878af7914df535b32d64f555fa118413186672Douglas Gregor  bool AllowExplicitConvs = Kind.allowExplicitConversionFunctions();
3151ed878af7914df535b32d64f555fa118413186672Douglas Gregor
315220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  const RecordType *T1RecordType = 0;
31536b6d01fbc297d703f7ad1e605aa97afc6289a64fDouglas Gregor  if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) &&
31546b6d01fbc297d703f7ad1e605aa97afc6289a64fDouglas Gregor      !S.RequireCompleteType(Kind.getLocation(), T1, 0)) {
315520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // The type we're converting to is a class type. Enumerate its constructors
315620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // to see if there is a suitable conversion.
315720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl());
3158572fc62b52b5b113cbaf528bd3ec00fcde19a46eJohn McCall
315920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    DeclContext::lookup_iterator Con, ConEnd;
3160e5eee5a52d98021de862b7965c88577598d52ccbDouglas Gregor    for (llvm::tie(Con, ConEnd) = S.LookupConstructors(T1RecordDecl);
316120093b4bf698f292c664676987541d5103b65b15Douglas Gregor         Con != ConEnd; ++Con) {
31629aa472c45d2bd81b7b52c225e8acc560d716db97John McCall      NamedDecl *D = *Con;
31639aa472c45d2bd81b7b52c225e8acc560d716db97John McCall      DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
31649aa472c45d2bd81b7b52c225e8acc560d716db97John McCall
316520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Find the constructor (which may be a template).
316620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CXXConstructorDecl *Constructor = 0;
31679aa472c45d2bd81b7b52c225e8acc560d716db97John McCall      FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
316820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (ConstructorTmpl)
316920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Constructor = cast<CXXConstructorDecl>(
317020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         ConstructorTmpl->getTemplatedDecl());
317120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      else
31729aa472c45d2bd81b7b52c225e8acc560d716db97John McCall        Constructor = cast<CXXConstructorDecl>(D);
3173dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
317420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (!Constructor->isInvalidDecl() &&
317520093b4bf698f292c664676987541d5103b65b15Douglas Gregor          Constructor->isConvertingConstructor(AllowExplicit)) {
317620093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (ConstructorTmpl)
31779aa472c45d2bd81b7b52c225e8acc560d716db97John McCall          S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
317886820f58e077bfd8fdf7309129b6ff2c5c4eb0e4John McCall                                         /*ExplicitArgs*/ 0,
317913a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                         Initializer, CandidateSet,
3180b72db8977ed032d6425045d7e9d36e49d9b2d3f8Argyrios Kyrtzidis                                         /*SuppressUserConversions=*/true);
318120093b4bf698f292c664676987541d5103b65b15Douglas Gregor        else
31829aa472c45d2bd81b7b52c225e8acc560d716db97John McCall          S.AddOverloadCandidate(Constructor, FoundDecl,
318313a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                 Initializer, CandidateSet,
3184b72db8977ed032d6425045d7e9d36e49d9b2d3f8Argyrios Kyrtzidis                                 /*SuppressUserConversions=*/true);
318520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      }
3186dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    }
318720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
3188572fc62b52b5b113cbaf528bd3ec00fcde19a46eJohn McCall  if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl())
3189572fc62b52b5b113cbaf528bd3ec00fcde19a46eJohn McCall    return OR_No_Viable_Function;
3190dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
31916b6d01fbc297d703f7ad1e605aa97afc6289a64fDouglas Gregor  const RecordType *T2RecordType = 0;
31926b6d01fbc297d703f7ad1e605aa97afc6289a64fDouglas Gregor  if ((T2RecordType = T2->getAs<RecordType>()) &&
31936b6d01fbc297d703f7ad1e605aa97afc6289a64fDouglas Gregor      !S.RequireCompleteType(Kind.getLocation(), T2, 0)) {
319420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // The type we're converting from is a class type, enumerate its conversion
319520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // functions.
319620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl());
319720093b4bf698f292c664676987541d5103b65b15Douglas Gregor
3198eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall    const UnresolvedSetImpl *Conversions
319920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      = T2RecordDecl->getVisibleConversionFunctions();
3200eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall    for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
3201eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall           E = Conversions->end(); I != E; ++I) {
320220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      NamedDecl *D = *I;
320320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
320420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (isa<UsingShadowDecl>(D))
320520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        D = cast<UsingShadowDecl>(D)->getTargetDecl();
3206dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
320720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
320820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      CXXConversionDecl *Conv;
320920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (ConvTemplate)
321020093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
321120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      else
32124680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl        Conv = cast<CXXConversionDecl>(D);
3213dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
321420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // If the conversion function doesn't return a reference type,
321520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // it can't be considered for this conversion unless we're allowed to
321620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // consider rvalues.
3217dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // FIXME: Do we need to make sure that we only consider conversion
3218dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // candidates with reference-compatible results? That might be needed to
321920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // break recursion.
3220ed878af7914df535b32d64f555fa118413186672Douglas Gregor      if ((AllowExplicitConvs || !Conv->isExplicit()) &&
322120093b4bf698f292c664676987541d5103b65b15Douglas Gregor          (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){
322220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (ConvTemplate)
32239aa472c45d2bd81b7b52c225e8acc560d716db97John McCall          S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(),
322486820f58e077bfd8fdf7309129b6ff2c5c4eb0e4John McCall                                           ActingDC, Initializer,
3225564cb06b1f010ab3c5e316ac79ba6cfdd72e9c1dDouglas Gregor                                           DestType, CandidateSet);
322620093b4bf698f292c664676987541d5103b65b15Douglas Gregor        else
32279aa472c45d2bd81b7b52c225e8acc560d716db97John McCall          S.AddConversionCandidate(Conv, I.getPair(), ActingDC,
3228564cb06b1f010ab3c5e316ac79ba6cfdd72e9c1dDouglas Gregor                                   Initializer, DestType, CandidateSet);
322920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      }
323020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
323120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
3232572fc62b52b5b113cbaf528bd3ec00fcde19a46eJohn McCall  if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl())
3233572fc62b52b5b113cbaf528bd3ec00fcde19a46eJohn McCall    return OR_No_Viable_Function;
3234dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
323520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  SourceLocation DeclLoc = Initializer->getLocStart();
323620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
3237dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // Perform overload resolution. If it fails, return the failed result.
323820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  OverloadCandidateSet::iterator Best;
3239dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (OverloadingResult Result
32408fcc516a5d5b2fec0023a9dced29285a38b95355Douglas Gregor        = CandidateSet.BestViableFunction(S, DeclLoc, Best, true))
324120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return Result;
324203981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman
324320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  FunctionDecl *Function = Best->Function;
324403981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman
324525ca421a6049350a2748c8fd0c19a052eba6ae99Chandler Carruth  // This is the overload that will actually be used for the initialization, so
324625ca421a6049350a2748c8fd0c19a052eba6ae99Chandler Carruth  // mark it as used.
32475f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  S.MarkFunctionReferenced(DeclLoc, Function);
324825ca421a6049350a2748c8fd0c19a052eba6ae99Chandler Carruth
324903981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman  // Compute the returned type of the conversion.
325020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (isa<CXXConversionDecl>(Function))
325120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    T2 = Function->getResultType();
325220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  else
325320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    T2 = cv1T1;
325403981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman
325503981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman  // Add the user-defined conversion step.
325622c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara  bool HadMultipleCandidates = (CandidateSet.size() > 1);
32579aa472c45d2bd81b7b52c225e8acc560d716db97John McCall  Sequence.AddUserConversionStep(Function, Best->FoundDecl,
325822c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                 T2.getNonLValueExprType(S.Context),
325922c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                 HadMultipleCandidates);
326003981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman
3261dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // Determine whether we need to perform derived-to-base or
326203981014e4f0c3b4e935872dda96a28c2f1874eaEli Friedman  // cv-qualification adjustments.
32635baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall  ExprValueKind VK = VK_RValue;
3264906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  if (T2->isLValueReferenceType())
32655baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    VK = VK_LValue;
3266906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>())
32675baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue;
3268906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
326920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool NewDerivedToBase = false;
3270569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  bool NewObjCConversion = false;
3271f85e193739c953358c865005855253af4f68a497John McCall  bool NewObjCLifetimeConversion = false;
327220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Sema::ReferenceCompareResult NewRefRelationship
3273dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    = S.CompareReferenceRelationship(DeclLoc, T1,
32746398235d7890a81b785ea5af3b6e66d86bf184ccDouglas Gregor                                     T2.getNonLValueExprType(S.Context),
3275f85e193739c953358c865005855253af4f68a497John McCall                                     NewDerivedToBase, NewObjCConversion,
3276f85e193739c953358c865005855253af4f68a497John McCall                                     NewObjCLifetimeConversion);
3277a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor  if (NewRefRelationship == Sema::Ref_Incompatible) {
3278a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor    // If the type we've converted to is not reference-related to the
3279a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor    // type we're looking for, then there is another conversion step
3280a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor    // we need to perform to produce a temporary of the right type
3281a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor    // that we'll be binding to.
3282a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor    ImplicitConversionSequence ICS;
3283a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor    ICS.setStandard();
3284a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor    ICS.Standard = Best->FinalConversion;
3285a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor    T2 = ICS.Standard.getToType(2);
3286a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor    Sequence.AddConversionSequenceStep(ICS, T2);
3287a1a9f035852e35ed88f4902a855eb952cf08bebbDouglas Gregor  } else if (NewDerivedToBase)
328820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Sequence.AddDerivedToBaseCastStep(
328920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                S.Context.getQualifiedType(T1,
3290dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                  T2.getNonReferenceType().getQualifiers()),
32915baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                      VK);
3292569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  else if (NewObjCConversion)
3293569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    Sequence.AddObjCObjectConversionStep(
3294569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                                S.Context.getQualifiedType(T1,
3295569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                                  T2.getNonReferenceType().getQualifiers()));
3296569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
329720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers())
32985baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall    Sequence.AddQualificationConversionStep(cv1T1, VK);
3299dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
330020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType());
330120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  return OR_Success;
330220093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
3303dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
330483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smithstatic void CheckCXX98CompatAccessibleCopy(Sema &S,
330583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith                                           const InitializedEntity &Entity,
330683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith                                           Expr *CurInitExpr);
330783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
3308dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Attempt reference initialization (C++0x [dcl.init.ref])
3309dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic void TryReferenceInitialization(Sema &S,
331020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       const InitializedEntity &Entity,
331120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       const InitializationKind &Kind,
331220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       Expr *Initializer,
331320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       InitializationSequence &Sequence) {
3314d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
331520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
33165535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  Qualifiers T1Quals;
33175535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
331820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType cv2T2 = Initializer->getType();
33195535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  Qualifiers T2Quals;
33205535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals);
33214680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl
332220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // If the initializer is the address of an overloaded function, try
332320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // to resolve the overloaded function. If all goes well, T2 is the
332420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // type of the resulting function.
332513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2,
332613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                                   T1, Sequence))
332713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    return;
332813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
332913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  // Delegate everything else to a subfunction.
333013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1,
333113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                 T1Quals, cv2T2, T2, T2Quals, Sequence);
333213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl}
33334680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl
333413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl/// \brief Reference initialization without resolving overloaded functions.
333513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redlstatic void TryReferenceInitializationCore(Sema &S,
333613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           const InitializedEntity &Entity,
333713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           const InitializationKind &Kind,
333813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           Expr *Initializer,
333913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           QualType cv1T1, QualType T1,
334013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           Qualifiers T1Quals,
334113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           QualType cv2T2, QualType T2,
334213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           Qualifiers T2Quals,
334313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl                                           InitializationSequence &Sequence) {
334413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  QualType DestType = Entity.getType();
334513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  SourceLocation DeclLoc = Initializer->getLocStart();
334620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Compute some basic properties of the types and the initializer.
334720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool isLValueRef = DestType->isLValueReferenceType();
334820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool isRValueRef = !isLValueRef;
334920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  bool DerivedToBase = false;
3350569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor  bool ObjCConversion = false;
3351f85e193739c953358c865005855253af4f68a497John McCall  bool ObjCLifetimeConversion = false;
33524680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl  Expr::Classification InitCategory = Initializer->Classify(S.Context);
335320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Sema::ReferenceCompareResult RefRelationship
3354569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase,
3355f85e193739c953358c865005855253af4f68a497John McCall                                     ObjCConversion, ObjCLifetimeConversion);
33564680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl
335720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // C++0x [dcl.init.ref]p5:
3358dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   A reference to type "cv1 T1" is initialized by an expression of type
335920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //   "cv2 T2" as follows:
336020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //
3361dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //     - If the reference is an lvalue reference and the initializer
336220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       expression
33634680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl  // Note the analogous bullet points for rvlaue refs to functions. Because
33644680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl  // there are no function rvalues in C++, rvalue refs to functions are treated
33654680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl  // like lvalue refs.
336620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  OverloadingResult ConvOvlResult = OR_Success;
33674680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl  bool T1Function = T1->isFunctionType();
33684680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl  if (isLValueRef || T1Function) {
3369dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (InitCategory.isLValue() &&
337014d0aee957f11b9613fa4312919bec3cc5456a1cDouglas Gregor        (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification ||
3371dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi         (Kind.isCStyleOrFunctionalCast() &&
337214d0aee957f11b9613fa4312919bec3cc5456a1cDouglas Gregor          RefRelationship == Sema::Ref_Related))) {
3373dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   - is an lvalue (but is not a bit-field), and "cv1 T1" is
337420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      //     reference-compatible with "cv2 T2," or
337520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      //
3376dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a
337720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // bit-field when we're determining whether the reference initialization
3378de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      // can occur. However, we do pay attention to whether it is a bit-field
3379de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      // to decide whether we're actually binding to a temporary created from
3380de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      // the bit-field.
338120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (DerivedToBase)
338220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Sequence.AddDerivedToBaseCastStep(
3383dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                         S.Context.getQualifiedType(T1, T2Quals),
33845baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                         VK_LValue);
3385569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor      else if (ObjCConversion)
3386569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor        Sequence.AddObjCObjectConversionStep(
3387569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor                                     S.Context.getQualifiedType(T1, T2Quals));
3388569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
33895535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth      if (T1Quals != T2Quals)
33905baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall        Sequence.AddQualificationConversionStep(cv1T1, VK_LValue);
3391de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      bool BindingTemporary = T1Quals.hasConst() && !T1Quals.hasVolatile() &&
3392093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson        (Initializer->getBitField() || Initializer->refersToVectorElement());
3393de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      Sequence.AddReferenceBindingStep(cv1T1, BindingTemporary);
339420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      return;
339520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
3396dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3397dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - has a class type (i.e., T2 is a class type), where T1 is not
3398dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //       reference-related to T2, and can be implicitly converted to an
3399dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //       lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible
3400dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //       with "cv3 T3" (this conversion is selected by enumerating the
340120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       applicable conversion functions (13.3.1.6) and choosing the best
340220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       one through overload resolution (13.3)),
34034680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl    // If we have an rvalue ref to function type here, the rhs must be
34044680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl    // an rvalue.
34054680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl    if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() &&
34064680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl        (isLValueRef || InitCategory.isRValue())) {
3407dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind,
340820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       Initializer,
34094680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl                                                   /*AllowRValues=*/isRValueRef,
341020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       Sequence);
341120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (ConvOvlResult == OR_Success)
341220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        return;
34131d31833450e6d2947a33cb0840d87661d92eec07John McCall      if (ConvOvlResult != OR_No_Viable_Function) {
34141d31833450e6d2947a33cb0840d87661d92eec07John McCall        Sequence.SetOverloadFailure(
34151d31833450e6d2947a33cb0840d87661d92eec07John McCall                      InitializationSequence::FK_ReferenceInitOverloadFailed,
34161d31833450e6d2947a33cb0840d87661d92eec07John McCall                                    ConvOvlResult);
34171d31833450e6d2947a33cb0840d87661d92eec07John McCall      }
341820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
341920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
34204680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl
3421dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //     - Otherwise, the reference shall be an lvalue reference to a
342220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       non-volatile const type (i.e., cv1 shall be const), or the reference
342369d831645f429d3b806d2ae220aee45ca44f8c6cDouglas Gregor  //       shall be an rvalue reference.
3424b2855ad68d93824faf47c09bbef90ba74157f0f4Douglas Gregor  if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) {
34253afb97700200f629d6036e437267af9c1fd37c90Douglas Gregor    if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy)
34263afb97700200f629d6036e437267af9c1fd37c90Douglas Gregor      Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
34273afb97700200f629d6036e437267af9c1fd37c90Douglas Gregor    else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
342820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      Sequence.SetOverloadFailure(
342920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                        InitializationSequence::FK_ReferenceInitOverloadFailed,
343020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                  ConvOvlResult);
3431b2855ad68d93824faf47c09bbef90ba74157f0f4Douglas Gregor    else
34324680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl      Sequence.SetFailed(InitCategory.isLValue()
343320093b4bf698f292c664676987541d5103b65b15Douglas Gregor        ? (RefRelationship == Sema::Ref_Related
343420093b4bf698f292c664676987541d5103b65b15Douglas Gregor             ? InitializationSequence::FK_ReferenceInitDropsQualifiers
343520093b4bf698f292c664676987541d5103b65b15Douglas Gregor             : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated)
343620093b4bf698f292c664676987541d5103b65b15Douglas Gregor        : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
34374680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl
343820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
343920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
34404680bf233caeebe89aa297eb5a25709dd15a4b11Sebastian Redl
3441c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor  //    - If the initializer expression
3442c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor  //      - is an xvalue, class prvalue, array prvalue, or function lvalue and
3443c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor  //        "cv1 T1" is reference-compatible with "cv2 T2"
3444c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor  // Note: functions are handled below.
3445c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor  if (!T1Function &&
344614d0aee957f11b9613fa4312919bec3cc5456a1cDouglas Gregor      (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification ||
3447dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi       (Kind.isCStyleOrFunctionalCast() &&
344814d0aee957f11b9613fa4312919bec3cc5456a1cDouglas Gregor        RefRelationship == Sema::Ref_Related)) &&
3449c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor      (InitCategory.isXValue() ||
3450c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor       (InitCategory.isPRValue() && T2->isRecordType()) ||
3451c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor       (InitCategory.isPRValue() && T2->isArrayType()))) {
3452c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor    ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue;
3453c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor    if (InitCategory.isPRValue() && T2->isRecordType()) {
3454523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the
3455523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      // compiler the freedom to perform a copy here or bind to the
3456523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      // object, while C++0x requires that we bind directly to the
3457523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      // object. Hence, we always bind to the object without making an
3458523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      // extra copy. However, in C++03 requires that we check for the
3459523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      // presence of a suitable copy constructor:
3460523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      //
3461523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      //   The constructor that would be used to make the copy shall
3462523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      //   be callable whether or not the copy is actually done.
34634e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!S.getLangOpts().CPlusPlus0x && !S.getLangOpts().MicrosoftExt)
3464523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor        Sequence.AddExtraneousCopyToTemporary(cv2T2);
34654e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      else if (S.getLangOpts().CPlusPlus0x)
346683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith        CheckCXX98CompatAccessibleCopy(S, Entity, Initializer);
346720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
3468dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3469c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor    if (DerivedToBase)
3470c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor      Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals),
3471c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor                                        ValueKind);
3472c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor    else if (ObjCConversion)
3473c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor      Sequence.AddObjCObjectConversionStep(
3474c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor                                       S.Context.getQualifiedType(T1, T2Quals));
3475dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3476c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor    if (T1Quals != T2Quals)
3477c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor      Sequence.AddQualificationConversionStep(cv1T1, ValueKind);
3478dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Sequence.AddReferenceBindingStep(cv1T1,
347965bfd682010f599a9a04fc7523e95e28c27508baPeter Collingbourne                                 /*bindingTemporary=*/InitCategory.isPRValue());
3480dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return;
3481c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor  }
3482dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3483dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       - has a class type (i.e., T2 is a class type), where T1 is not
3484dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //         reference-related to T2, and can be implicitly converted to an
3485c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor  //         xvalue, class prvalue, or function lvalue of type "cv3 T3",
3486c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor  //         where "cv1 T1" is reference-compatible with "cv3 T3",
3487c5db24dfbc860a9fd1501c72f398bf6b6d5b6e0eDouglas Gregor  if (T2->isRecordType()) {
348820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (RefRelationship == Sema::Ref_Incompatible) {
348920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      ConvOvlResult = TryRefInitWithConversionFunction(S, Entity,
349020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       Kind, Initializer,
349120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       /*AllowRValues=*/true,
349220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                                       Sequence);
349320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (ConvOvlResult)
349420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        Sequence.SetOverloadFailure(
349520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                      InitializationSequence::FK_ReferenceInitOverloadFailed,
349620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                    ConvOvlResult);
3497dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
349820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      return;
349920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
3500dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
350120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
350220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
350320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
35040099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi
35050099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //      - Otherwise, a temporary of type "cv1 T1" is created and initialized
350620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        from the initializer expression using the rules for a non-reference
3507dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //        copy initialization (8.5). The reference is then bound to the
350820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        temporary. [...]
3509369371ce0c2b0d8051a8089e620eb41513fce9a2John McCall
351020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // Determine whether we are allowed to call explicit constructors or
351120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // explicit conversion operators.
3512168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl  bool AllowExplicit = Kind.AllowExplicit();
3513369371ce0c2b0d8051a8089e620eb41513fce9a2John McCall
3514369371ce0c2b0d8051a8089e620eb41513fce9a2John McCall  InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1);
3515369371ce0c2b0d8051a8089e620eb41513fce9a2John McCall
3516f85e193739c953358c865005855253af4f68a497John McCall  ImplicitConversionSequence ICS
3517f85e193739c953358c865005855253af4f68a497John McCall    = S.TryImplicitConversion(Initializer, TempEntity.getType(),
3518369371ce0c2b0d8051a8089e620eb41513fce9a2John McCall                              /*SuppressUserConversions*/ false,
3519369371ce0c2b0d8051a8089e620eb41513fce9a2John McCall                              AllowExplicit,
352014d0aee957f11b9613fa4312919bec3cc5456a1cDouglas Gregor                              /*FIXME:InOverloadResolution=*/false,
3521f85e193739c953358c865005855253af4f68a497John McCall                              /*CStyle=*/Kind.isCStyleOrFunctionalCast(),
3522f85e193739c953358c865005855253af4f68a497John McCall                              /*AllowObjCWritebackConversion=*/false);
3523f85e193739c953358c865005855253af4f68a497John McCall
3524f85e193739c953358c865005855253af4f68a497John McCall  if (ICS.isBad()) {
352520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // FIXME: Use the conversion function set stored in ICS to turn
352620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // this into an overloading ambiguity diagnostic. However, we need
352720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // to keep that set as an OverloadCandidateSet rather than as some
352820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // other kind of set.
352918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
353018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      Sequence.SetOverloadFailure(
353118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                        InitializationSequence::FK_ReferenceInitOverloadFailed,
353218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                  ConvOvlResult);
35333afb97700200f629d6036e437267af9c1fd37c90Douglas Gregor    else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy)
35343afb97700200f629d6036e437267af9c1fd37c90Douglas Gregor      Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
353518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    else
353618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed);
353720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
3538f85e193739c953358c865005855253af4f68a497John McCall  } else {
3539f85e193739c953358c865005855253af4f68a497John McCall    Sequence.AddConversionSequenceStep(ICS, TempEntity.getType());
354020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
354120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
354220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        [...] If T1 is reference-related to T2, cv1 must be the
354320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        same cv-qualification as, or greater cv-qualification
354420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //        than, cv2; otherwise, the program is ill-formed.
35455535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  unsigned T1CVRQuals = T1Quals.getCVRQualifiers();
35465535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth  unsigned T2CVRQuals = T2Quals.getCVRQualifiers();
3547dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (RefRelationship == Sema::Ref_Related &&
35485535c38a2fcface6c13bc8bbeca66882de2fa227Chandler Carruth      (T1CVRQuals | T2CVRQuals) != T1CVRQuals) {
354920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
355020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
355120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
355220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
3553dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   [...] If T1 is reference-related to T2 and the reference is an rvalue
3554b2855ad68d93824faf47c09bbef90ba74157f0f4Douglas Gregor  //   reference, the initializer expression shall not be an lvalue.
3555dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (RefRelationship >= Sema::Ref_Related && !isLValueRef &&
3556b2855ad68d93824faf47c09bbef90ba74157f0f4Douglas Gregor      InitCategory.isLValue()) {
3557b2855ad68d93824faf47c09bbef90ba74157f0f4Douglas Gregor    Sequence.SetFailed(
3558b2855ad68d93824faf47c09bbef90ba74157f0f4Douglas Gregor                    InitializationSequence::FK_RValueReferenceBindingToLValue);
3559b2855ad68d93824faf47c09bbef90ba74157f0f4Douglas Gregor    return;
3560b2855ad68d93824faf47c09bbef90ba74157f0f4Douglas Gregor  }
3561dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
356220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
356320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  return;
356420093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
356520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
356620093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Attempt character array initialization from a string literal
3567dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// (C++ [dcl.init.string], C99 6.7.8).
3568dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic void TryStringLiteralInitialization(Sema &S,
356920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                           const InitializedEntity &Entity,
357020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                           const InitializationKind &Kind,
357120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                           Expr *Initializer,
357220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                       InitializationSequence &Sequence) {
3573d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  Sequence.AddStringInitStep(Entity.getType());
357420093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
357520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
357671d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor/// \brief Attempt value initialization (C++ [dcl.init]p7).
3577dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic void TryValueInitialization(Sema &S,
357871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor                                   const InitializedEntity &Entity,
357971d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor                                   const InitializationKind &Kind,
3580f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                   InitializationSequence &Sequence,
3581f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                   InitListExpr *InitList) {
3582f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  assert((!InitList || InitList->getNumInits() == 0) &&
3583f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith         "Shouldn't use value-init for non-empty init lists");
3584f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith
35851d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith  // C++98 [dcl.init]p5, C++11 [dcl.init]p7:
358671d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  //
358771d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  //   To value-initialize an object of type T means:
3588d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType T = Entity.getType();
3589dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
359071d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  //     -- if T is an array type, then each element is value-initialized;
35911d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith  T = S.Context.getBaseElementType(T);
3592dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
359371d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  if (const RecordType *RT = T->getAs<RecordType>()) {
359471d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
3595f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      bool NeedZeroInitialization = true;
35964e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!S.getLangOpts().CPlusPlus0x) {
3597f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        // C++98:
3598f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        // -- if T is a class type (clause 9) with a user-declared constructor
3599f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        //    (12.1), then the default constructor for T is called (and the
3600f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        //    initialization is ill-formed if T has no accessible default
3601f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        //    constructor);
36021d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith        if (ClassDecl->hasUserDeclaredConstructor())
3603f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith          NeedZeroInitialization = false;
36041d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith      } else {
36051d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith        // C++11:
36061d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith        // -- if T is a class type (clause 9) with either no default constructor
36071d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith        //    (12.1 [class.ctor]) or a default constructor that is user-provided
36081d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith        //    or deleted, then the object is default-initialized;
36091d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith        CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl);
36101d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith        if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted())
3611f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith          NeedZeroInitialization = false;
36121d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith      }
3613dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
36141d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith      // -- if T is a (possibly cv-qualified) non-union class type without a
36151d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith      //    user-provided or deleted default constructor, then the object is
36161d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith      //    zero-initialized and, if T has a non-trivial default constructor,
36171d0c9a8d0573d1f670f484cc17aa94f06be971a5Richard Smith      //    default-initialized;
3618d079abfb5eefaf7da232e39a6564f561402cf4feRichard Smith      // FIXME: The 'non-union' here is a defect (not yet assigned an issue
3619d079abfb5eefaf7da232e39a6564f561402cf4feRichard Smith      // number). Update the quotation when the defect is resolved.
3620f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      if (NeedZeroInitialization)
3621f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith        Sequence.AddZeroInitializationStep(Entity.getType());
3622f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith
3623f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      // If this is list-value-initialization, pass the empty init list on when
3624f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      // building the constructor call. This affects the semantics of a few
3625f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      // things (such as whether an explicit default constructor can be called).
3626f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      Expr *InitListAsExpr = InitList;
3627f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      Expr **Args = InitList ? &InitListAsExpr : 0;
3628f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      unsigned NumArgs = InitList ? 1 : 0;
3629f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      bool InitListSyntax = InitList;
3630f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith
3631f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      return TryConstructorInitialization(S, Entity, Kind, Args, NumArgs, T,
3632f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith                                          Sequence, InitListSyntax);
363371d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    }
363471d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor  }
363571d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
3636d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  Sequence.AddZeroInitializationStep(Entity.getType());
363771d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor}
363871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor
363999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor/// \brief Attempt default initialization (C++ [dcl.init]p6).
364099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregorstatic void TryDefaultInitialization(Sema &S,
364199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor                                     const InitializedEntity &Entity,
364299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor                                     const InitializationKind &Kind,
364399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor                                     InitializationSequence &Sequence) {
364499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  assert(Kind.getKind() == InitializationKind::IK_Default);
3645dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
364699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  // C++ [dcl.init]p6:
364799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //   To default-initialize an object of type T means:
364899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //     - if T is an array type, each element is default-initialized;
3649f85e193739c953358c865005855253af4f68a497John McCall  QualType DestType = S.Context.getBaseElementType(Entity.getType());
3650f85e193739c953358c865005855253af4f68a497John McCall
365199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //     - if T is a (possibly cv-qualified) class type (Clause 9), the default
365299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //       constructor for T is called (and the initialization is ill-formed if
365399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //       T has no accessible default constructor);
36544e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) {
36554e6fbce4277fbc7c574fb2393c7dfe06f014c21aChandler Carruth    TryConstructorInitialization(S, Entity, Kind, 0, 0, DestType, Sequence);
36564e6fbce4277fbc7c574fb2393c7dfe06f014c21aChandler Carruth    return;
365799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  }
3658dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
365999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //     - otherwise, no initialization is performed.
3660dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
366199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //   If a program calls for the default initialization of an object of
3662dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   a const-qualified type T, T shall be a class type with a user-provided
366399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  //   default constructor.
36644e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) {
366599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
3666f85e193739c953358c865005855253af4f68a497John McCall    return;
3667f85e193739c953358c865005855253af4f68a497John McCall  }
3668f85e193739c953358c865005855253af4f68a497John McCall
3669f85e193739c953358c865005855253af4f68a497John McCall  // If the destination type has a lifetime property, zero-initialize it.
3670f85e193739c953358c865005855253af4f68a497John McCall  if (DestType.getQualifiers().hasObjCLifetime()) {
3671f85e193739c953358c865005855253af4f68a497John McCall    Sequence.AddZeroInitializationStep(Entity.getType());
3672f85e193739c953358c865005855253af4f68a497John McCall    return;
3673f85e193739c953358c865005855253af4f68a497John McCall  }
367499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor}
367599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
367620093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]),
367720093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// which enumerates all conversion functions and performs overload resolution
367820093b4bf698f292c664676987541d5103b65b15Douglas Gregor/// to select the best.
3679dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic void TryUserDefinedConversion(Sema &S,
368020093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                     const InitializedEntity &Entity,
368120093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                     const InitializationKind &Kind,
368220093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                     Expr *Initializer,
368320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                     InitializationSequence &Sequence) {
3684d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
36854a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  assert(!DestType->isReferenceType() && "References are handled elsewhere");
36864a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  QualType SourceType = Initializer->getType();
36874a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  assert((DestType->isRecordType() || SourceType->isRecordType()) &&
36884a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor         "Must have a class type to perform a user-defined conversion");
3689dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
36904a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // Build the candidate set directly in the initialization sequence
36914a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // structure, so that it will persist if we fail.
36924a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
36934a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  CandidateSet.clear();
3694dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
36954a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // Determine whether we are allowed to call explicit constructors or
36964a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // explicit conversion operators.
3697168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl  bool AllowExplicit = Kind.AllowExplicit();
3698dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
36994a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) {
37004a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // The type we're converting to is a class type. Enumerate its constructors
37014a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // to see if there is a suitable conversion.
37024a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    CXXRecordDecl *DestRecordDecl
37034a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      = cast<CXXRecordDecl>(DestRecordType->getDecl());
3704dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3705087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor    // Try to complete the type we're converting to.
3706dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
3707087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor      DeclContext::lookup_iterator Con, ConEnd;
3708e5eee5a52d98021de862b7965c88577598d52ccbDouglas Gregor      for (llvm::tie(Con, ConEnd) = S.LookupConstructors(DestRecordDecl);
3709087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor           Con != ConEnd; ++Con) {
3710087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor        NamedDecl *D = *Con;
3711087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor        DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
3712dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3713087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor        // Find the constructor (which may be a template).
3714087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor        CXXConstructorDecl *Constructor = 0;
3715087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor        FunctionTemplateDecl *ConstructorTmpl
3716087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor          = dyn_cast<FunctionTemplateDecl>(D);
37174a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        if (ConstructorTmpl)
3718087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor          Constructor = cast<CXXConstructorDecl>(
3719087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor                                           ConstructorTmpl->getTemplatedDecl());
37204712c02c1550a72612c07f5aaee455dcd8273b96Douglas Gregor        else
3721087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor          Constructor = cast<CXXConstructorDecl>(D);
3722dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3723087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor        if (!Constructor->isInvalidDecl() &&
3724087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor            Constructor->isConvertingConstructor(AllowExplicit)) {
3725087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor          if (ConstructorTmpl)
3726087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor            S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
3727087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor                                           /*ExplicitArgs*/ 0,
372813a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                           Initializer, CandidateSet,
37294712c02c1550a72612c07f5aaee455dcd8273b96Douglas Gregor                                           /*SuppressUserConversions=*/true);
3730087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor          else
3731087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor            S.AddOverloadCandidate(Constructor, FoundDecl,
373213a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                   Initializer, CandidateSet,
37334712c02c1550a72612c07f5aaee455dcd8273b96Douglas Gregor                                   /*SuppressUserConversions=*/true);
3734087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor        }
3735dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      }
3736087fb7d0b5e08f34b2e28a09c376f66449f30886Douglas Gregor    }
37374a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  }
3738cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
3739cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  SourceLocation DeclLoc = Initializer->getLocStart();
3740cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
37414a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) {
37424a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // The type we're converting from is a class type, enumerate its conversion
37434a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // functions.
3744cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
374533c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman    // We can only enumerate the conversion functions for a complete type; if
374633c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman    // the type isn't complete, simply skip this step.
374733c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman    if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) {
374833c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman      CXXRecordDecl *SourceRecordDecl
374933c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        = cast<CXXRecordDecl>(SourceRecordType->getDecl());
3750dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3751eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall      const UnresolvedSetImpl *Conversions
375233c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        = SourceRecordDecl->getVisibleConversionFunctions();
3753eec51cf1ba5f0e62c9cdb81b5c63babdd6e649abJohn McCall      for (UnresolvedSetImpl::const_iterator I = Conversions->begin(),
3754dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi           E = Conversions->end();
375533c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman           I != E; ++I) {
375633c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        NamedDecl *D = *I;
375733c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
375833c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        if (isa<UsingShadowDecl>(D))
375933c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman          D = cast<UsingShadowDecl>(D)->getTargetDecl();
3760dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
376133c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
376233c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        CXXConversionDecl *Conv;
37634a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        if (ConvTemplate)
376433c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman          Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
37654a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor        else
376632daa4223ccb2c0afe5fbe151c6eb1ab64816957John McCall          Conv = cast<CXXConversionDecl>(D);
3767dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
376833c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        if (AllowExplicit || !Conv->isExplicit()) {
376933c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman          if (ConvTemplate)
37709aa472c45d2bd81b7b52c225e8acc560d716db97John McCall            S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(),
377186820f58e077bfd8fdf7309129b6ff2c5c4eb0e4John McCall                                             ActingDC, Initializer, DestType,
377233c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman                                             CandidateSet);
377333c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman          else
37749aa472c45d2bd81b7b52c225e8acc560d716db97John McCall            S.AddConversionCandidate(Conv, I.getPair(), ActingDC,
377586820f58e077bfd8fdf7309129b6ff2c5c4eb0e4John McCall                                     Initializer, DestType, CandidateSet);
377633c2da9b3abdade4f0df4f90962fb8c518967fc4Eli Friedman        }
37774a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      }
37784a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    }
37794a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  }
3780dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3781dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // Perform overload resolution. If it fails, return the failed result.
37824a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  OverloadCandidateSet::iterator Best;
37831d31833450e6d2947a33cb0840d87661d92eec07John McCall  if (OverloadingResult Result
37848fcc516a5d5b2fec0023a9dced29285a38b95355Douglas Gregor        = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) {
37854a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    Sequence.SetOverloadFailure(
3786dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                        InitializationSequence::FK_UserConversionOverloadFailed,
37874a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor                                Result);
37884a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    return;
37894a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  }
37901d31833450e6d2947a33cb0840d87661d92eec07John McCall
37914a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  FunctionDecl *Function = Best->Function;
37925f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  S.MarkFunctionReferenced(DeclLoc, Function);
379322c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara  bool HadMultipleCandidates = (CandidateSet.size() > 1);
3794dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
37954a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  if (isa<CXXConstructorDecl>(Function)) {
37964a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    // Add the user-defined conversion step. Any cv-qualification conversion is
3797f2e4dfcd325db2c1a960697be2c750f277dcafceRichard Smith    // subsumed by the initialization. Per DR5, the created temporary is of the
3798f2e4dfcd325db2c1a960697be2c750f277dcafceRichard Smith    // cv-unqualified type of the destination.
3799f2e4dfcd325db2c1a960697be2c750f277dcafceRichard Smith    Sequence.AddUserConversionStep(Function, Best->FoundDecl,
3800f2e4dfcd325db2c1a960697be2c750f277dcafceRichard Smith                                   DestType.getUnqualifiedType(),
380122c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                   HadMultipleCandidates);
38024a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    return;
38034a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  }
38044a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
38054a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  // Add the user-defined conversion step that calls the conversion function.
38065291c3cec0dbe8ad1d8e7e67e93af2b1586d5400Douglas Gregor  QualType ConvType = Function->getCallResultType();
38073fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor  if (ConvType->getAs<RecordType>()) {
3808f2e4dfcd325db2c1a960697be2c750f277dcafceRichard Smith    // If we're converting to a class type, there may be an copy of
38093fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor    // the resulting temporary object (possible to create an object of
38103fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor    // a base class type). That copy is not a separate conversion, so
38113fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor    // we just make a note of the actual destination type (possibly a
38123fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor    // base class of the type returned by the conversion function) and
38133fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor    // let the user-defined conversion step handle the conversion.
381422c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara    Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType,
381522c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                   HadMultipleCandidates);
38163fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor    return;
38173fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor  }
38184a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor
381922c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara  Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType,
382022c107b2b99887b5aec6d1fd38210031e944e31fAbramo Bagnara                                 HadMultipleCandidates);
3821dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
38223fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor  // If the conversion following the call to the conversion function
38233fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor  // is interesting, add it as a separate step.
38244a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  if (Best->FinalConversion.First || Best->FinalConversion.Second ||
38254a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor      Best->FinalConversion.Third) {
38264a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    ImplicitConversionSequence ICS;
38271d31833450e6d2947a33cb0840d87661d92eec07John McCall    ICS.setStandard();
38284a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    ICS.Standard = Best->FinalConversion;
38294a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor    Sequence.AddConversionSequenceStep(ICS, DestType);
38304a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  }
383120093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
383220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
3833f85e193739c953358c865005855253af4f68a497John McCall/// The non-zero enum values here are indexes into diagnostic alternatives.
3834f85e193739c953358c865005855253af4f68a497John McCallenum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar };
3835f85e193739c953358c865005855253af4f68a497John McCall
3836f85e193739c953358c865005855253af4f68a497John McCall/// Determines whether this expression is an acceptable ICR source.
3837c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCallstatic InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e,
3838c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall                                         bool isAddressOf) {
3839f85e193739c953358c865005855253af4f68a497John McCall  // Skip parens.
3840f85e193739c953358c865005855253af4f68a497John McCall  e = e->IgnoreParens();
3841f85e193739c953358c865005855253af4f68a497John McCall
3842f85e193739c953358c865005855253af4f68a497John McCall  // Skip address-of nodes.
3843f85e193739c953358c865005855253af4f68a497John McCall  if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) {
3844f85e193739c953358c865005855253af4f68a497John McCall    if (op->getOpcode() == UO_AddrOf)
3845c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall      return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true);
3846f85e193739c953358c865005855253af4f68a497John McCall
3847f85e193739c953358c865005855253af4f68a497John McCall  // Skip certain casts.
3848c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall  } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) {
3849c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall    switch (ce->getCastKind()) {
3850f85e193739c953358c865005855253af4f68a497John McCall    case CK_Dependent:
3851f85e193739c953358c865005855253af4f68a497John McCall    case CK_BitCast:
3852f85e193739c953358c865005855253af4f68a497John McCall    case CK_LValueBitCast:
3853f85e193739c953358c865005855253af4f68a497John McCall    case CK_NoOp:
3854c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall      return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf);
3855f85e193739c953358c865005855253af4f68a497John McCall
3856f85e193739c953358c865005855253af4f68a497John McCall    case CK_ArrayToPointerDecay:
3857f85e193739c953358c865005855253af4f68a497John McCall      return IIK_nonscalar;
3858f85e193739c953358c865005855253af4f68a497John McCall
3859f85e193739c953358c865005855253af4f68a497John McCall    case CK_NullToPointer:
3860f85e193739c953358c865005855253af4f68a497John McCall      return IIK_okay;
3861f85e193739c953358c865005855253af4f68a497John McCall
3862f85e193739c953358c865005855253af4f68a497John McCall    default:
3863f85e193739c953358c865005855253af4f68a497John McCall      break;
3864f85e193739c953358c865005855253af4f68a497John McCall    }
3865f85e193739c953358c865005855253af4f68a497John McCall
3866f85e193739c953358c865005855253af4f68a497John McCall  // If we have a declaration reference, it had better be a local variable.
3867f4b88a45902af1802a1cb42ba48b1c474474f228John McCall  } else if (isa<DeclRefExpr>(e)) {
3868c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall    if (!isAddressOf) return IIK_nonlocal;
3869c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall
3870f4b88a45902af1802a1cb42ba48b1c474474f228John McCall    VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl());
3871f4b88a45902af1802a1cb42ba48b1c474474f228John McCall    if (!var) return IIK_nonlocal;
3872c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall
3873c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall    return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal);
3874f85e193739c953358c865005855253af4f68a497John McCall
3875f85e193739c953358c865005855253af4f68a497John McCall  // If we have a conditional operator, check both sides.
3876f85e193739c953358c865005855253af4f68a497John McCall  } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) {
3877c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall    if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf))
3878f85e193739c953358c865005855253af4f68a497John McCall      return iik;
3879f85e193739c953358c865005855253af4f68a497John McCall
3880c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall    return isInvalidICRSource(C, cond->getRHS(), isAddressOf);
3881f85e193739c953358c865005855253af4f68a497John McCall
3882f85e193739c953358c865005855253af4f68a497John McCall  // These are never scalar.
3883f85e193739c953358c865005855253af4f68a497John McCall  } else if (isa<ArraySubscriptExpr>(e)) {
3884f85e193739c953358c865005855253af4f68a497John McCall    return IIK_nonscalar;
3885f85e193739c953358c865005855253af4f68a497John McCall
3886f85e193739c953358c865005855253af4f68a497John McCall  // Otherwise, it needs to be a null pointer constant.
3887f85e193739c953358c865005855253af4f68a497John McCall  } else {
3888f85e193739c953358c865005855253af4f68a497John McCall    return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull)
3889f85e193739c953358c865005855253af4f68a497John McCall            ? IIK_okay : IIK_nonlocal);
3890f85e193739c953358c865005855253af4f68a497John McCall  }
3891f85e193739c953358c865005855253af4f68a497John McCall
3892f85e193739c953358c865005855253af4f68a497John McCall  return IIK_nonlocal;
3893f85e193739c953358c865005855253af4f68a497John McCall}
3894f85e193739c953358c865005855253af4f68a497John McCall
3895f85e193739c953358c865005855253af4f68a497John McCall/// Check whether the given expression is a valid operand for an
3896f85e193739c953358c865005855253af4f68a497John McCall/// indirect copy/restore.
3897f85e193739c953358c865005855253af4f68a497John McCallstatic void checkIndirectCopyRestoreSource(Sema &S, Expr *src) {
3898f85e193739c953358c865005855253af4f68a497John McCall  assert(src->isRValue());
3899f85e193739c953358c865005855253af4f68a497John McCall
3900c03fa498b9598fe88ef8fb97315f27d09e56aa50John McCall  InvalidICRKind iik = isInvalidICRSource(S.Context, src, false);
3901f85e193739c953358c865005855253af4f68a497John McCall  if (iik == IIK_okay) return;
3902f85e193739c953358c865005855253af4f68a497John McCall
3903f85e193739c953358c865005855253af4f68a497John McCall  S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback)
3904f85e193739c953358c865005855253af4f68a497John McCall    << ((unsigned) iik - 1)  // shift index into diagnostic explanations
3905f85e193739c953358c865005855253af4f68a497John McCall    << src->getSourceRange();
3906f85e193739c953358c865005855253af4f68a497John McCall}
3907f85e193739c953358c865005855253af4f68a497John McCall
3908cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor/// \brief Determine whether we have compatible array types for the
3909cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor/// purposes of GNU by-copy array initialization.
3910cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregorstatic bool hasCompatibleArrayTypes(ASTContext &Context,
3911cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor                                    const ArrayType *Dest,
3912cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor                                    const ArrayType *Source) {
3913cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  // If the source and destination array types are equivalent, we're
3914cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  // done.
3915cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0)))
3916cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    return true;
3917cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
3918cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  // Make sure that the element types are the same.
3919cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  if (!Context.hasSameType(Dest->getElementType(), Source->getElementType()))
3920cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    return false;
3921cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
3922cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  // The only mismatch we allow is when the destination is an
3923cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  // incomplete array type and the source is a constant array type.
3924cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  return Source->isConstantArrayType() && Dest->isIncompleteArrayType();
3925cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor}
3926cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
3927f85e193739c953358c865005855253af4f68a497John McCallstatic bool tryObjCWritebackConversion(Sema &S,
3928f85e193739c953358c865005855253af4f68a497John McCall                                       InitializationSequence &Sequence,
3929f85e193739c953358c865005855253af4f68a497John McCall                                       const InitializedEntity &Entity,
3930f85e193739c953358c865005855253af4f68a497John McCall                                       Expr *Initializer) {
3931f85e193739c953358c865005855253af4f68a497John McCall  bool ArrayDecay = false;
3932f85e193739c953358c865005855253af4f68a497John McCall  QualType ArgType = Initializer->getType();
3933f85e193739c953358c865005855253af4f68a497John McCall  QualType ArgPointee;
3934f85e193739c953358c865005855253af4f68a497John McCall  if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) {
3935f85e193739c953358c865005855253af4f68a497John McCall    ArrayDecay = true;
3936f85e193739c953358c865005855253af4f68a497John McCall    ArgPointee = ArgArrayType->getElementType();
3937f85e193739c953358c865005855253af4f68a497John McCall    ArgType = S.Context.getPointerType(ArgPointee);
3938f85e193739c953358c865005855253af4f68a497John McCall  }
3939f85e193739c953358c865005855253af4f68a497John McCall
3940f85e193739c953358c865005855253af4f68a497John McCall  // Handle write-back conversion.
3941f85e193739c953358c865005855253af4f68a497John McCall  QualType ConvertedArgType;
3942f85e193739c953358c865005855253af4f68a497John McCall  if (!S.isObjCWritebackConversion(ArgType, Entity.getType(),
3943f85e193739c953358c865005855253af4f68a497John McCall                                   ConvertedArgType))
3944f85e193739c953358c865005855253af4f68a497John McCall    return false;
3945f85e193739c953358c865005855253af4f68a497John McCall
3946f85e193739c953358c865005855253af4f68a497John McCall  // We should copy unless we're passing to an argument explicitly
3947f85e193739c953358c865005855253af4f68a497John McCall  // marked 'out'.
3948f85e193739c953358c865005855253af4f68a497John McCall  bool ShouldCopy = true;
3949f85e193739c953358c865005855253af4f68a497John McCall  if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl()))
3950f85e193739c953358c865005855253af4f68a497John McCall    ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out);
3951f85e193739c953358c865005855253af4f68a497John McCall
3952f85e193739c953358c865005855253af4f68a497John McCall  // Do we need an lvalue conversion?
3953f85e193739c953358c865005855253af4f68a497John McCall  if (ArrayDecay || Initializer->isGLValue()) {
3954f85e193739c953358c865005855253af4f68a497John McCall    ImplicitConversionSequence ICS;
3955f85e193739c953358c865005855253af4f68a497John McCall    ICS.setStandard();
3956f85e193739c953358c865005855253af4f68a497John McCall    ICS.Standard.setAsIdentityConversion();
3957f85e193739c953358c865005855253af4f68a497John McCall
3958f85e193739c953358c865005855253af4f68a497John McCall    QualType ResultType;
3959f85e193739c953358c865005855253af4f68a497John McCall    if (ArrayDecay) {
3960f85e193739c953358c865005855253af4f68a497John McCall      ICS.Standard.First = ICK_Array_To_Pointer;
3961f85e193739c953358c865005855253af4f68a497John McCall      ResultType = S.Context.getPointerType(ArgPointee);
3962f85e193739c953358c865005855253af4f68a497John McCall    } else {
3963f85e193739c953358c865005855253af4f68a497John McCall      ICS.Standard.First = ICK_Lvalue_To_Rvalue;
3964f85e193739c953358c865005855253af4f68a497John McCall      ResultType = Initializer->getType().getNonLValueExprType(S.Context);
3965f85e193739c953358c865005855253af4f68a497John McCall    }
3966f85e193739c953358c865005855253af4f68a497John McCall
3967f85e193739c953358c865005855253af4f68a497John McCall    Sequence.AddConversionSequenceStep(ICS, ResultType);
3968f85e193739c953358c865005855253af4f68a497John McCall  }
3969f85e193739c953358c865005855253af4f68a497John McCall
3970f85e193739c953358c865005855253af4f68a497John McCall  Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy);
3971f85e193739c953358c865005855253af4f68a497John McCall  return true;
3972f85e193739c953358c865005855253af4f68a497John McCall}
3973f85e193739c953358c865005855253af4f68a497John McCall
397420093b4bf698f292c664676987541d5103b65b15Douglas GregorInitializationSequence::InitializationSequence(Sema &S,
397520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                               const InitializedEntity &Entity,
397620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                               const InitializationKind &Kind,
397720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                               Expr **Args,
39785769d6195087229770d7ac90449443e026c47103John McCall                                               unsigned NumArgs)
39795769d6195087229770d7ac90449443e026c47103John McCall    : FailedCandidateSet(Kind.getLocation()) {
398012ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola  ASTContext &Context = S.Context;
3981dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
398220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // C++0x [dcl.init]p16:
3983dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   The semantics of initializers are as follows. The destination type is
3984dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the type of the object or reference being initialized and the source
398520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //   type is the type of the initializer expression. The source type is not
3986dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   defined when the initializer is a braced-init-list or when it is a
398720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //   parenthesized list of expressions.
398812ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola  QualType DestType = Entity.getType();
398920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
399012ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola  if (DestType->isDependentType() ||
399113a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles      Expr::hasAnyTypeDependentArguments(llvm::makeArrayRef(Args, NumArgs))) {
399220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    SequenceKind = DependentSequence;
399320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
399420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
399520093b4bf698f292c664676987541d5103b65b15Douglas Gregor
39967491c499e826682e128a400038361ebcbde30eecSebastian Redl  // Almost everything is a normal sequence.
39977491c499e826682e128a400038361ebcbde30eecSebastian Redl  setSequenceKind(NormalSequence);
39987491c499e826682e128a400038361ebcbde30eecSebastian Redl
3999241d5580d2f3969d2cd2a94b4a92c762b5dc1e04John McCall  for (unsigned I = 0; I != NumArgs; ++I)
400032509f1e60451d86e9fbc473b6e853ba10b5fd1eJohn McCall    if (Args[I]->getType()->isNonOverloadPlaceholderType()) {
40015acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall      // FIXME: should we be doing this here?
400232509f1e60451d86e9fbc473b6e853ba10b5fd1eJohn McCall      ExprResult result = S.CheckPlaceholderExpr(Args[I]);
400332509f1e60451d86e9fbc473b6e853ba10b5fd1eJohn McCall      if (result.isInvalid()) {
400432509f1e60451d86e9fbc473b6e853ba10b5fd1eJohn McCall        SetFailed(FK_PlaceholderType);
400532509f1e60451d86e9fbc473b6e853ba10b5fd1eJohn McCall        return;
40065acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall      }
400732509f1e60451d86e9fbc473b6e853ba10b5fd1eJohn McCall      Args[I] = result.take();
4008429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    }
4009241d5580d2f3969d2cd2a94b4a92c762b5dc1e04John McCall
40105acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall
401120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  QualType SourceType;
401220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  Expr *Initializer = 0;
401399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (NumArgs == 1) {
401420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Initializer = Args[0];
401520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (!isa<InitListExpr>(Initializer))
401620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      SourceType = Initializer->getType();
401720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
4018dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
40193a45c0e61dfc19f27b8ebcb15dd70159a36f1f9aSebastian Redl  //     - If the initializer is a (non-parenthesized) braced-init-list, the
40203a45c0e61dfc19f27b8ebcb15dd70159a36f1f9aSebastian Redl  //       object is list-initialized (8.5.4).
40213a45c0e61dfc19f27b8ebcb15dd70159a36f1f9aSebastian Redl  if (Kind.getKind() != InitializationKind::IK_Direct) {
40223a45c0e61dfc19f27b8ebcb15dd70159a36f1f9aSebastian Redl    if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) {
40233a45c0e61dfc19f27b8ebcb15dd70159a36f1f9aSebastian Redl      TryListInitialization(S, Entity, Kind, InitList, *this);
40243a45c0e61dfc19f27b8ebcb15dd70159a36f1f9aSebastian Redl      return;
40253a45c0e61dfc19f27b8ebcb15dd70159a36f1f9aSebastian Redl    }
402620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
4027dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
402820093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - If the destination type is a reference type, see 8.5.3.
402920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (DestType->isReferenceType()) {
403020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // C++0x [dcl.init.ref]p1:
403120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //   A variable declared to be a T& or T&&, that is, "reference to type T"
403220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //   (8.3.2), shall be initialized by an object, or function, of type T or
403320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //   by an object that can be converted into a T.
403420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    // (Therefore, multiple arguments are not permitted.)
403520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (NumArgs != 1)
403612ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola      SetFailed(FK_TooManyInitsForReference);
403720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    else
403812ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola      TryReferenceInitialization(S, Entity, Kind, Args[0], *this);
403920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
404020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
4041dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
404220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - If the initializer is (), the object is value-initialized.
404399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (Kind.getKind() == InitializationKind::IK_Value ||
404499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor      (Kind.getKind() == InitializationKind::IK_Direct && NumArgs == 0)) {
404512ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    TryValueInitialization(S, Entity, Kind, *this);
404620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
404720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
4048dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
404999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  // Handle default initialization.
40507663f396651716c82280f8fdcf97ad8e27c1ce5aNick Lewycky  if (Kind.getKind() == InitializationKind::IK_Default) {
405112ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    TryDefaultInitialization(S, Entity, Kind, *this);
405299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return;
405399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  }
405418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
4055ce6c9b7da219c8640a0559f75770cfc6622de083John McCall  //     - If the destination type is an array of characters, an array of
4056ce6c9b7da219c8640a0559f75770cfc6622de083John McCall  //       char16_t, an array of char32_t, or an array of wchar_t, and the
4057ce6c9b7da219c8640a0559f75770cfc6622de083John McCall  //       initializer is a string literal, see 8.5.2.
4058dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //     - Otherwise, if the destination type is an array, the program is
405920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //       ill-formed.
4060cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) {
406173076431605556fdbf28d287d084a73a24a8b8d4John McCall    if (Initializer && isa<VariableArrayType>(DestAT)) {
406273076431605556fdbf28d287d084a73a24a8b8d4John McCall      SetFailed(FK_VariableLengthArrayHasInitializer);
406373076431605556fdbf28d287d084a73a24a8b8d4John McCall      return;
406473076431605556fdbf28d287d084a73a24a8b8d4John McCall    }
406573076431605556fdbf28d287d084a73a24a8b8d4John McCall
4066cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    if (Initializer && IsStringInit(Initializer, DestAT, Context)) {
406712ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola      TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this);
4068ce6c9b7da219c8640a0559f75770cfc6622de083John McCall      return;
4069ce6c9b7da219c8640a0559f75770cfc6622de083John McCall    }
4070ce6c9b7da219c8640a0559f75770cfc6622de083John McCall
4071cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    // Note: as an GNU C extension, we allow initialization of an
4072cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    // array from a compound literal that creates an array of the same
4073cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    // type, so long as the initializer has no side effects.
40744e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (!S.getLangOpts().CPlusPlus && Initializer &&
4075cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor        isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) &&
4076cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor        Initializer->getType()->isArrayType()) {
4077cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      const ArrayType *SourceAT
4078cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor        = Context.getAsArrayType(Initializer->getType());
4079cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT))
408012ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola        SetFailed(FK_ArrayTypeMismatch);
4081cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      else if (Initializer->HasSideEffects(S.Context))
408212ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola        SetFailed(FK_NonConstantArrayInit);
4083cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      else {
408412ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola        AddArrayInitStep(DestType);
4085cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      }
40860f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith    }
4087f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith    // Note: as a GNU C++ extension, we allow list-initialization of a
4088f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith    // class member of array type from a parenthesized initializer list.
40894e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    else if (S.getLangOpts().CPlusPlus &&
40900f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith             Entity.getKind() == InitializedEntity::EK_Member &&
40910f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith             Initializer && isa<InitListExpr>(Initializer)) {
40920f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith      TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer),
40930f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith                            *this);
40940f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith      AddParenthesizedArrayInitStep(DestType);
4095cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    } else if (DestAT->getElementType()->isAnyCharacterType())
409612ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola      SetFailed(FK_ArrayNeedsInitListOrStringLiteral);
409720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    else
409812ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola      SetFailed(FK_ArrayNeedsInitList);
4099dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
410020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
410120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
4102cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
4103f85e193739c953358c865005855253af4f68a497John McCall  // Determine whether we should consider writeback conversions for
4104f85e193739c953358c865005855253af4f68a497John McCall  // Objective-C ARC.
41054e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount &&
4106f85e193739c953358c865005855253af4f68a497John McCall    Entity.getKind() == InitializedEntity::EK_Parameter;
4107f85e193739c953358c865005855253af4f68a497John McCall
4108f85e193739c953358c865005855253af4f68a497John McCall  // We're at the end of the line for C: it's either a write-back conversion
4109f85e193739c953358c865005855253af4f68a497John McCall  // or it's a C assignment. There's no need to check anything else.
41104e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!S.getLangOpts().CPlusPlus) {
4111f85e193739c953358c865005855253af4f68a497John McCall    // If allowed, check whether this is an Objective-C writeback conversion.
4112f85e193739c953358c865005855253af4f68a497John McCall    if (allowObjCWritebackConversion &&
411312ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola        tryObjCWritebackConversion(S, *this, Entity, Initializer)) {
4114f85e193739c953358c865005855253af4f68a497John McCall      return;
4115f85e193739c953358c865005855253af4f68a497John McCall    }
4116f85e193739c953358c865005855253af4f68a497John McCall
4117f85e193739c953358c865005855253af4f68a497John McCall    // Handle initialization in C
411812ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    AddCAssignmentStep(DestType);
411912ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    MaybeProduceObjCObject(S, *this, Entity);
4120cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman    return;
4121cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  }
4122dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
41234e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(S.getLangOpts().CPlusPlus);
4124f85e193739c953358c865005855253af4f68a497John McCall
412520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //     - If the destination type is a (possibly cv-qualified) class type:
412620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  if (DestType->isRecordType()) {
4127dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - If the initialization is direct-initialization, or if it is
4128dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //       copy-initialization where the cv-unqualified version of the
4129dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //       source type is the same class as, or a derived class of, the
413020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       class of the destination, constructors are considered. [...]
413120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (Kind.getKind() == InitializationKind::IK_Direct ||
413220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        (Kind.getKind() == InitializationKind::IK_Copy &&
413320093b4bf698f292c664676987541d5103b65b15Douglas Gregor         (Context.hasSameUnqualifiedType(SourceType, DestType) ||
413420093b4bf698f292c664676987541d5103b65b15Douglas Gregor          S.IsDerivedFrom(SourceType, DestType))))
4135dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      TryConstructorInitialization(S, Entity, Kind, Args, NumArgs,
413612ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola                                   Entity.getType(), *this);
4137dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - Otherwise (i.e., for the remaining copy-initialization cases),
413820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       user-defined conversion sequences that can convert from the source
4139dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //       type to the destination type or (when a conversion function is
414020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       used) to a derived class thereof are enumerated as described in
414120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       13.3.1.4, and the best one is chosen through overload resolution
414220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    //       (13.3).
414320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    else
414412ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola      TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
414520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
414620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
4147dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
414899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (NumArgs > 1) {
414912ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    SetFailed(FK_TooManyInitsForScalar);
415099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return;
415199a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  }
415299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  assert(NumArgs == 1 && "Zero-argument case handled above");
4153dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4154dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //    - Otherwise, if the source type is a (possibly cv-qualified) class
415520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //      type, conversion functions are considered.
415699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  if (!SourceType.isNull() && SourceType->isRecordType()) {
415712ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    TryUserDefinedConversion(S, Entity, Kind, Initializer, *this);
415812ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    MaybeProduceObjCObject(S, *this, Entity);
415920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return;
416020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
4161dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
416220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //    - Otherwise, the initial value of the object being initialized is the
41634a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  //      (possibly converted) value of the initializer expression. Standard
416420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //      conversions (Clause 4) will be used, if necessary, to convert the
4165dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //      initializer expression to the cv-unqualified version of the
416620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  //      destination type; no user-defined conversions are considered.
4167f85e193739c953358c865005855253af4f68a497John McCall
4168f85e193739c953358c865005855253af4f68a497John McCall  ImplicitConversionSequence ICS
4169f85e193739c953358c865005855253af4f68a497John McCall    = S.TryImplicitConversion(Initializer, Entity.getType(),
4170f85e193739c953358c865005855253af4f68a497John McCall                              /*SuppressUserConversions*/true,
4171369371ce0c2b0d8051a8089e620eb41513fce9a2John McCall                              /*AllowExplicitConversions*/ false,
417214d0aee957f11b9613fa4312919bec3cc5456a1cDouglas Gregor                              /*InOverloadResolution*/ false,
4173f85e193739c953358c865005855253af4f68a497John McCall                              /*CStyle=*/Kind.isCStyleOrFunctionalCast(),
4174f85e193739c953358c865005855253af4f68a497John McCall                              allowObjCWritebackConversion);
4175f85e193739c953358c865005855253af4f68a497John McCall
4176f85e193739c953358c865005855253af4f68a497John McCall  if (ICS.isStandard() &&
4177f85e193739c953358c865005855253af4f68a497John McCall      ICS.Standard.Second == ICK_Writeback_Conversion) {
4178f85e193739c953358c865005855253af4f68a497John McCall    // Objective-C ARC writeback conversion.
4179f85e193739c953358c865005855253af4f68a497John McCall
4180f85e193739c953358c865005855253af4f68a497John McCall    // We should copy unless we're passing to an argument explicitly
4181f85e193739c953358c865005855253af4f68a497John McCall    // marked 'out'.
4182f85e193739c953358c865005855253af4f68a497John McCall    bool ShouldCopy = true;
4183f85e193739c953358c865005855253af4f68a497John McCall    if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl()))
4184f85e193739c953358c865005855253af4f68a497John McCall      ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out);
4185f85e193739c953358c865005855253af4f68a497John McCall
4186f85e193739c953358c865005855253af4f68a497John McCall    // If there was an lvalue adjustment, add it as a separate conversion.
4187f85e193739c953358c865005855253af4f68a497John McCall    if (ICS.Standard.First == ICK_Array_To_Pointer ||
4188f85e193739c953358c865005855253af4f68a497John McCall        ICS.Standard.First == ICK_Lvalue_To_Rvalue) {
4189f85e193739c953358c865005855253af4f68a497John McCall      ImplicitConversionSequence LvalueICS;
4190f85e193739c953358c865005855253af4f68a497John McCall      LvalueICS.setStandard();
4191f85e193739c953358c865005855253af4f68a497John McCall      LvalueICS.Standard.setAsIdentityConversion();
4192f85e193739c953358c865005855253af4f68a497John McCall      LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0));
4193f85e193739c953358c865005855253af4f68a497John McCall      LvalueICS.Standard.First = ICS.Standard.First;
419412ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola      AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0));
4195f85e193739c953358c865005855253af4f68a497John McCall    }
419612ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola
419712ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy);
4198f85e193739c953358c865005855253af4f68a497John McCall  } else if (ICS.isBad()) {
41991be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    DeclAccessPair dap;
42001be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    if (Initializer->getType() == Context.OverloadTy &&
42011be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor          !S.ResolveAddressOfOverloadedFunction(Initializer
42021be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor                      , DestType, false, dap))
420312ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola      SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
42048e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor    else
420512ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola      SetFailed(InitializationSequence::FK_ConversionFailed);
4206f85e193739c953358c865005855253af4f68a497John McCall  } else {
420712ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    AddConversionSequenceStep(ICS, Entity.getType());
4208856d3798af7c2f7251e4a295f3da7a09ce4c62abJohn McCall
420912ce0a085f89f07c76bf034aa6b838ef50542241Rafael Espindola    MaybeProduceObjCObject(S, *this, Entity);
42108e960435696b4ccf6a8ad0ed0530e3280b77af8bDouglas Gregor  }
421120093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
421220093b4bf698f292c664676987541d5103b65b15Douglas Gregor
421320093b4bf698f292c664676987541d5103b65b15Douglas GregorInitializationSequence::~InitializationSequence() {
42145f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  for (SmallVectorImpl<Step>::iterator Step = Steps.begin(),
421520093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                          StepEnd = Steps.end();
421620093b4bf698f292c664676987541d5103b65b15Douglas Gregor       Step != StepEnd; ++Step)
421720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Step->Destroy();
421820093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
421920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
422020093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
422120093b4bf698f292c664676987541d5103b65b15Douglas Gregor// Perform initialization
422220093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
4223dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic Sema::AssignmentAction
422418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas GregorgetAssignmentAction(const InitializedEntity &Entity) {
422518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  switch(Entity.getKind()) {
422618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Variable:
422718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_New:
4228a3998bd364f8c6b4a520f479083da1d6e30482a1Douglas Gregor  case InitializedEntity::EK_Exception:
4229a3998bd364f8c6b4a520f479083da1d6e30482a1Douglas Gregor  case InitializedEntity::EK_Base:
4230059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  case InitializedEntity::EK_Delegating:
423118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return Sema::AA_Initializing;
423218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
423318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Parameter:
4234dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (Entity.getDecl() &&
4235688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor        isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext()))
4236688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor      return Sema::AA_Sending;
4237688fc9b9b4323a294f5bf4f8a83f7c365edec573Douglas Gregor
423818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return Sema::AA_Passing;
423918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
424018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Result:
424118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return Sema::AA_Returning;
424218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
424318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Temporary:
424418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    // FIXME: Can we tell apart casting vs. converting?
424518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return Sema::AA_Casting;
4246dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
424718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Member:
4248d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case InitializedEntity::EK_ArrayElement:
4249d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case InitializedEntity::EK_VectorElement:
42500c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  case InitializedEntity::EK_ComplexElement:
4251310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian  case InitializedEntity::EK_BlockElement:
42524773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor  case InitializedEntity::EK_LambdaCapture:
425318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return Sema::AA_Initializing;
425418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  }
425518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
42567530c034c0c71a64c5a9173206d9742ae847af8bDavid Blaikie  llvm_unreachable("Invalid EntityKind!");
425718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor}
425818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
42594154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor/// \brief Whether we should binding a created object as a temporary when
42604154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor/// initializing the given entity.
42612f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregorstatic bool shouldBindAsTemporary(const InitializedEntity &Entity) {
426218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  switch (Entity.getKind()) {
42631b36a2fcb2cea310c01fc2a80a9da915e5d2ceb0Anders Carlsson  case InitializedEntity::EK_ArrayElement:
42641b36a2fcb2cea310c01fc2a80a9da915e5d2ceb0Anders Carlsson  case InitializedEntity::EK_Member:
42652f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  case InitializedEntity::EK_Result:
426618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_New:
426718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Variable:
426818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Base:
4269059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt  case InitializedEntity::EK_Delegating:
4270d3d824d45c32d457493e7cb79cc34a4761afb760Anders Carlsson  case InitializedEntity::EK_VectorElement:
42710c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman  case InitializedEntity::EK_ComplexElement:
4272a508b7de6c5246ab04ed69d0ab4e9977ec1fb4d4Anders Carlsson  case InitializedEntity::EK_Exception:
4273310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian  case InitializedEntity::EK_BlockElement:
42744773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor  case InitializedEntity::EK_LambdaCapture:
427518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return false;
4276dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
427718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Parameter:
427818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case InitializedEntity::EK_Temporary:
427918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return true;
428018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  }
4281dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
428218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  llvm_unreachable("missed an InitializedEntity kind?");
428318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor}
428418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
42854154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor/// \brief Whether the given entity, when initialized with an object
42864154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor/// created for that initialization, requires destruction.
42874154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregorstatic bool shouldDestroyTemporary(const InitializedEntity &Entity) {
42884154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor  switch (Entity.getKind()) {
42894154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor    case InitializedEntity::EK_Member:
42904154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor    case InitializedEntity::EK_Result:
42914154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor    case InitializedEntity::EK_New:
42924154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor    case InitializedEntity::EK_Base:
4293059ce0d92eb5a7da900ae735dc0a2ea3d64f4b0bSean Hunt    case InitializedEntity::EK_Delegating:
42944154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor    case InitializedEntity::EK_VectorElement:
42950c706c29f20b6fa36759fa41333b9c3ec0bd2969Eli Friedman    case InitializedEntity::EK_ComplexElement:
4296310b1c421665241d9b135c517d5031716d4a3221Fariborz Jahanian    case InitializedEntity::EK_BlockElement:
42974773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor    case InitializedEntity::EK_LambdaCapture:
42984154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor      return false;
4299dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
43004154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor    case InitializedEntity::EK_Variable:
43014154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor    case InitializedEntity::EK_Parameter:
43024154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor    case InitializedEntity::EK_Temporary:
43034154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor    case InitializedEntity::EK_ArrayElement:
43044154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor    case InitializedEntity::EK_Exception:
43054154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor      return true;
43064154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor  }
4307dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4308dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  llvm_unreachable("missed an InitializedEntity kind?");
43094154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor}
43104154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor
431183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith/// \brief Look for copy and move constructors and constructor templates, for
431283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith/// copying an object via direct-initialization (per C++11 [dcl.init]p16).
431383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smithstatic void LookupCopyAndMoveConstructors(Sema &S,
431483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith                                          OverloadCandidateSet &CandidateSet,
431583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith                                          CXXRecordDecl *Class,
431683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith                                          Expr *CurInitExpr) {
431783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  DeclContext::lookup_iterator Con, ConEnd;
431883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  for (llvm::tie(Con, ConEnd) = S.LookupConstructors(Class);
431983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith       Con != ConEnd; ++Con) {
432083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    CXXConstructorDecl *Constructor = 0;
432183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
432283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    if ((Constructor = dyn_cast<CXXConstructorDecl>(*Con))) {
432383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith      // Handle copy/moveconstructors, only.
432483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith      if (!Constructor || Constructor->isInvalidDecl() ||
432583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith          !Constructor->isCopyOrMoveConstructor() ||
432683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith          !Constructor->isConvertingConstructor(/*AllowExplicit=*/true))
432783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith        continue;
432883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
432983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith      DeclAccessPair FoundDecl
433083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith        = DeclAccessPair::make(Constructor, Constructor->getAccess());
433183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith      S.AddOverloadCandidate(Constructor, FoundDecl,
433213a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                             CurInitExpr, CandidateSet);
433383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith      continue;
433483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    }
433583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
433683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    // Handle constructor templates.
433783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(*Con);
433883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    if (ConstructorTmpl->isInvalidDecl())
433983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith      continue;
434083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
434183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    Constructor = cast<CXXConstructorDecl>(
434283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith                                         ConstructorTmpl->getTemplatedDecl());
434383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true))
434483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith      continue;
434583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
434683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    // FIXME: Do we need to limit this to copy-constructor-like
434783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    // candidates?
434883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    DeclAccessPair FoundDecl
434983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith      = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess());
435083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0,
435113a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                   CurInitExpr, CandidateSet, true);
435283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  }
435383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith}
435483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
435583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith/// \brief Get the location at which initialization diagnostics should appear.
435683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smithstatic SourceLocation getInitializationLoc(const InitializedEntity &Entity,
435783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith                                           Expr *Initializer) {
435883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  switch (Entity.getKind()) {
435983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_Result:
436083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    return Entity.getReturnLoc();
436183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
436283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_Exception:
436383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    return Entity.getThrowLoc();
436483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
436583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_Variable:
436683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    return Entity.getDecl()->getLocation();
436783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
43684773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor  case InitializedEntity::EK_LambdaCapture:
43694773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor    return Entity.getCaptureLoc();
43704773654f2700d6fbb20612fbb6763b35860fa74dDouglas Gregor
437183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_ArrayElement:
437283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_Member:
437383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_Parameter:
437483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_Temporary:
437583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_New:
437683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_Base:
437783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_Delegating:
437883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_VectorElement:
437983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_ComplexElement:
438083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case InitializedEntity::EK_BlockElement:
438183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    return Initializer->getLocStart();
438283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  }
438383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  llvm_unreachable("missed an InitializedEntity kind?");
438483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith}
438583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
4386523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// \brief Make a (potentially elidable) temporary copy of the object
4387523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// provided by the given initializer by calling the appropriate copy
4388523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// constructor.
4389523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor///
4390523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// \param S The Sema object used for type-checking.
4391523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor///
439263e7d25d2e6036616b42f744fd4a39cd5f911960Abramo Bagnara/// \param T The type of the temporary object, which must either be
4393523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// the type of the initializer expression or a superclass thereof.
4394523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor///
43951dfbd92c83699820bfaa352e83083124e34fc9dcJames Dennett/// \param Entity The entity being initialized.
4396523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor///
4397523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// \param CurInit The initializer expression.
4398523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor///
4399523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// \param IsExtraneousCopy Whether this is an "extraneous" copy that
4400523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// is permitted in C++03 (but not C++0x) when binding a reference to
4401523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// an rvalue.
4402523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor///
4403523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// \returns An expression that copies the initializer expression into
4404523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// a temporary object, or an error expression if a copy could not be
4405523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor/// created.
440660d7b3a319d84d688752be3870615ac0f111fb16John McCallstatic ExprResult CopyObject(Sema &S,
44078fcc516a5d5b2fec0023a9dced29285a38b95355Douglas Gregor                             QualType T,
44088fcc516a5d5b2fec0023a9dced29285a38b95355Douglas Gregor                             const InitializedEntity &Entity,
44098fcc516a5d5b2fec0023a9dced29285a38b95355Douglas Gregor                             ExprResult CurInit,
44108fcc516a5d5b2fec0023a9dced29285a38b95355Douglas Gregor                             bool IsExtraneousCopy) {
44113fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor  // Determine which class type we're copying to.
44121b36a2fcb2cea310c01fc2a80a9da915e5d2ceb0Anders Carlsson  Expr *CurInitExpr = (Expr *)CurInit.get();
4413dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  CXXRecordDecl *Class = 0;
4414523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  if (const RecordType *Record = T->getAs<RecordType>())
44152f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    Class = cast<CXXRecordDecl>(Record->getDecl());
44162f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  if (!Class)
44172f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor    return move(CurInit);
44182f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor
4419f5d8f466c3eebaffc51468812bdcbe7f0fe4891aDouglas Gregor  // C++0x [class.copy]p32:
44202f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  //   When certain criteria are met, an implementation is allowed to
44212f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  //   omit the copy/move construction of a class object, even if the
44222f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  //   copy/move constructor and/or destructor for the object have
44232f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  //   side effects. [...]
44242f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  //     - when a temporary class object that has not been bound to a
44252f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  //       reference (12.2) would be copied/moved to a class object
44262f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  //       with the same cv-unqualified type, the copy/move operation
44272f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  //       can be omitted by constructing the temporary object
44282f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  //       directly into the target of the omitted copy/move
4429dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //
44302f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor  // Note that the other three bullets are handled elsewhere. Copy
44313c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // elision for return statements and throw expressions are handled as part
4432dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // of constructor initialization, while copy elision for exception handlers
44333c9034cb7ff1d6c1e4ecd1b44c98f553df013c7cDouglas Gregor  // is handled by the run-time.
4434558d2abc7f9fd6801cc7677200992313ae90b5d8John McCall  bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class);
443583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  SourceLocation Loc = getInitializationLoc(Entity, CurInit.get());
4436f86fcb341116e72c5497e6b57695f46eca5f29e9Douglas Gregor
4437dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // Make sure that the type we are copying is complete.
4438d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor  if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete))
4439f86fcb341116e72c5497e6b57695f46eca5f29e9Douglas Gregor    return move(CurInit);
4440f86fcb341116e72c5497e6b57695f46eca5f29e9Douglas Gregor
4441cc15f010672a13b38104a32e3cefc7adc07ffbf7Douglas Gregor  // Perform overload resolution using the class's copy/move constructors.
444283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  // Only consider constructors and constructor templates. Per
444383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  // C++0x [dcl.init]p16, second bullet to class types, this initialization
444483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  // is direct-initialization.
44455769d6195087229770d7ac90449443e026c47103John McCall  OverloadCandidateSet CandidateSet(Loc);
444683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr);
4447dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
44487cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara  bool HadMultipleCandidates = (CandidateSet.size() > 1);
44497cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara
445018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  OverloadCandidateSet::iterator Best;
445125ca421a6049350a2748c8fd0c19a052eba6ae99Chandler Carruth  switch (CandidateSet.BestViableFunction(S, Loc, Best)) {
445218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case OR_Success:
445318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    break;
4454dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
445518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case OR_No_Viable_Function:
445657d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin    S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext()
445757d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin           ? diag::ext_rvalue_to_reference_temp_copy_no_viable
445857d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin           : diag::err_temp_copy_no_viable)
44597abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor      << (int)Entity.getKind() << CurInitExpr->getType()
446018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      << CurInitExpr->getSourceRange();
446113a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles    CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr);
446257d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin    if (!IsExtraneousCopy || S.isSFINAEContext())
4463f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      return ExprError();
446457d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin    return move(CurInit);
4465dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
446618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case OR_Ambiguous:
446718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    S.Diag(Loc, diag::err_temp_copy_ambiguous)
44687abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor      << (int)Entity.getKind() << CurInitExpr->getType()
446918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      << CurInitExpr->getSourceRange();
447013a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles    CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr);
4471f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    return ExprError();
4472dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
447318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case OR_Deleted:
447418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    S.Diag(Loc, diag::err_temp_copy_deleted)
44757abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor      << (int)Entity.getKind() << CurInitExpr->getType()
447618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      << CurInitExpr->getSourceRange();
44776c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith    S.NoteDeletedFunction(Best->Function);
4478f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    return ExprError();
447918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  }
448018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
44813fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor  CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function);
4482ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall  ASTOwningVector<Expr*> ConstructorArgs(S);
44833fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor  CurInit.release(); // Ownership transferred into MultiExprArg, below.
4484523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
44859a68a67c6ae4982001815cc04f69b8781058263aAnders Carlsson  S.CheckConstructorAccess(Loc, Constructor, Entity,
448657d12fd4a2bc739c4a4d62a364b7f08cd483c59eJeffrey Yasskin                           Best->FoundDecl.getAccess(), IsExtraneousCopy);
4487523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
4488523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  if (IsExtraneousCopy) {
4489523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor    // If this is a totally extraneous copy for C++03 reference
4490523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor    // binding purposes, just return the original initialization
44912559a7045a74679c80376305397a5953d038e1d0Douglas Gregor    // expression. We don't generate an (elided) copy operation here
44922559a7045a74679c80376305397a5953d038e1d0Douglas Gregor    // because doing so would require us to pass down a flag to avoid
44932559a7045a74679c80376305397a5953d038e1d0Douglas Gregor    // infinite recursion, where each step adds another extraneous,
44942559a7045a74679c80376305397a5953d038e1d0Douglas Gregor    // elidable copy.
44952559a7045a74679c80376305397a5953d038e1d0Douglas Gregor
44962559a7045a74679c80376305397a5953d038e1d0Douglas Gregor    // Instantiate the default arguments of any extra parameters in
44972559a7045a74679c80376305397a5953d038e1d0Douglas Gregor    // the selected copy constructor, as if we were going to create a
44982559a7045a74679c80376305397a5953d038e1d0Douglas Gregor    // proper call to the copy constructor.
44992559a7045a74679c80376305397a5953d038e1d0Douglas Gregor    for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) {
45002559a7045a74679c80376305397a5953d038e1d0Douglas Gregor      ParmVarDecl *Parm = Constructor->getParamDecl(I);
45012559a7045a74679c80376305397a5953d038e1d0Douglas Gregor      if (S.RequireCompleteType(Loc, Parm->getType(),
4502d10099e5c8238fa0327f03921cf2e3c8975c881eDouglas Gregor                                diag::err_call_incomplete_argument))
45032559a7045a74679c80376305397a5953d038e1d0Douglas Gregor        break;
45042559a7045a74679c80376305397a5953d038e1d0Douglas Gregor
45052559a7045a74679c80376305397a5953d038e1d0Douglas Gregor      // Build the default argument expression; we don't actually care
45062559a7045a74679c80376305397a5953d038e1d0Douglas Gregor      // if this succeeds or not, because this routine will complain
45072559a7045a74679c80376305397a5953d038e1d0Douglas Gregor      // if there was a problem.
45082559a7045a74679c80376305397a5953d038e1d0Douglas Gregor      S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm);
45092559a7045a74679c80376305397a5953d038e1d0Douglas Gregor    }
4510523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
4511523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor    return S.Owned(CurInitExpr);
4512523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  }
4513dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
45145f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman  S.MarkFunctionReferenced(Loc, Constructor);
451525ca421a6049350a2748c8fd0c19a052eba6ae99Chandler Carruth
45163fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor  // Determine the arguments required to actually perform the
4517523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  // constructor call (we might have derived-to-base conversions, or
4518523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  // the copy constructor may have default arguments).
4519f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  if (S.CompleteConstructorCall(Constructor, MultiExprArg(&CurInitExpr, 1),
45203fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor                                Loc, ConstructorArgs))
4521f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    return ExprError();
45223fbaf3e5d524bfff219d1e3e9ac4801a8411590fDouglas Gregor
4523b86cf0c11712fa89f14197f3f0ed862e7b2add84Douglas Gregor  // Actually perform the constructor call.
4524b86cf0c11712fa89f14197f3f0ed862e7b2add84Douglas Gregor  CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable,
45257a1fad38256eb4c5129359be85ba1ea1678eb5c9John McCall                                    move_arg(ConstructorArgs),
45267cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                    HadMultipleCandidates,
45277a1fad38256eb4c5129359be85ba1ea1678eb5c9John McCall                                    /*ZeroInit*/ false,
4528428edafa9eb80e01dd40aab31d4166a787a741e1Chandler Carruth                                    CXXConstructExpr::CK_Complete,
4529428edafa9eb80e01dd40aab31d4166a787a741e1Chandler Carruth                                    SourceRange());
4530dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4531b86cf0c11712fa89f14197f3f0ed862e7b2add84Douglas Gregor  // If we're supposed to bind temporaries, do so.
4532b86cf0c11712fa89f14197f3f0ed862e7b2add84Douglas Gregor  if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity))
4533b86cf0c11712fa89f14197f3f0ed862e7b2add84Douglas Gregor    CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
4534b86cf0c11712fa89f14197f3f0ed862e7b2add84Douglas Gregor  return move(CurInit);
453518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor}
453620093b4bf698f292c664676987541d5103b65b15Douglas Gregor
453783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith/// \brief Check whether elidable copy construction for binding a reference to
453883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith/// a temporary would have succeeded if we were building in C++98 mode, for
453983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith/// -Wc++98-compat.
454083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smithstatic void CheckCXX98CompatAccessibleCopy(Sema &S,
454183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith                                           const InitializedEntity &Entity,
454283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith                                           Expr *CurInitExpr) {
45434e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(S.getLangOpts().CPlusPlus0x);
454483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
454583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>();
454683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  if (!Record)
454783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    return;
454883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
454983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr);
455083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc)
455183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith        == DiagnosticsEngine::Ignored)
455283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    return;
455383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
455483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  // Find constructors which would have been considered.
455583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  OverloadCandidateSet CandidateSet(Loc);
455683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  LookupCopyAndMoveConstructors(
455783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith      S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr);
455883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
455983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  // Perform overload resolution.
456083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  OverloadCandidateSet::iterator Best;
456183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best);
456283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
456383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy)
456483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    << OR << (int)Entity.getKind() << CurInitExpr->getType()
456583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    << CurInitExpr->getSourceRange();
456683da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
456783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  switch (OR) {
456883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case OR_Success:
456983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function),
4570b9abd87283ac6e929b7e12a577663bc99e61d020John McCall                             Entity, Best->FoundDecl.getAccess(), Diag);
457183da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    // FIXME: Check default arguments as far as that's possible.
457283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    break;
457383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
457483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case OR_No_Viable_Function:
457583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    S.Diag(Loc, Diag);
457613a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles    CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr);
457783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    break;
457883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
457983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case OR_Ambiguous:
458083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    S.Diag(Loc, Diag);
458113a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles    CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr);
458283da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    break;
458383da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
458483da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  case OR_Deleted:
458583da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    S.Diag(Loc, Diag);
45866c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith    S.NoteDeletedFunction(Best->Function);
458783da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith    break;
458883da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith  }
458983da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith}
459083da2e711902c4c54f5601c9000d646dfff06aeaRichard Smith
4591a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregorvoid InitializationSequence::PrintInitLocationNote(Sema &S,
4592a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor                                              const InitializedEntity &Entity) {
4593a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor  if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) {
4594a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor    if (Entity.getDecl()->getLocation().isInvalid())
4595a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor      return;
4596a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor
4597a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor    if (Entity.getDecl()->getDeclName())
4598a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor      S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here)
4599a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor        << Entity.getDecl()->getDeclName();
4600a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor    else
4601a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor      S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here);
4602a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor  }
4603a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor}
4604a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor
46053b80232b50c29b245e674f5aa02047b408e41018Sebastian Redlstatic bool isReferenceBinding(const InitializationSequence::Step &s) {
46063b80232b50c29b245e674f5aa02047b408e41018Sebastian Redl  return s.Kind == InitializationSequence::SK_BindReference ||
46073b80232b50c29b245e674f5aa02047b408e41018Sebastian Redl         s.Kind == InitializationSequence::SK_BindReferenceToTemporary;
46083b80232b50c29b245e674f5aa02047b408e41018Sebastian Redl}
46093b80232b50c29b245e674f5aa02047b408e41018Sebastian Redl
461010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redlstatic ExprResult
461110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian RedlPerformConstructorInitialization(Sema &S,
461210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                 const InitializedEntity &Entity,
461310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                 const InitializationKind &Kind,
461410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                 MultiExprArg Args,
461510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                 const InitializationSequence::Step& Step,
461610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                 bool &ConstructorInitRequiresZeroInit) {
461710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  unsigned NumArgs = Args.size();
461810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  CXXConstructorDecl *Constructor
461910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    = cast<CXXConstructorDecl>(Step.Function.Function);
462010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  bool HadMultipleCandidates = Step.Function.HadMultipleCandidates;
462110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
462210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  // Build a call to the selected constructor.
462310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  ASTOwningVector<Expr*> ConstructorArgs(S);
462410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid())
462510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                         ? Kind.getEqualLoc()
462610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                         : Kind.getLocation();
462710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
462810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  if (Kind.getKind() == InitializationKind::IK_Default) {
462910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    // Force even a trivial, implicit default constructor to be
463010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    // semantically checked. We do this explicitly because we don't build
463110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    // the definition for completely trivial constructors.
463228e4702a1f978213ff945fe4369e3be7444bf320Matt Beaumont-Gay    assert(Constructor->getParent() && "No parent class for constructor.");
463310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() &&
46345d86f61b9c54429fc3f9149fc0f7009010a644a1Douglas Gregor        Constructor->isTrivial() && !Constructor->isUsed(false))
463510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      S.DefineImplicitDefaultConstructor(Loc, Constructor);
463610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  }
463710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
463810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  ExprResult CurInit = S.Owned((Expr *)0);
463910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
4640ed878af7914df535b32d64f555fa118413186672Douglas Gregor  // C++ [over.match.copy]p1:
4641ed878af7914df535b32d64f555fa118413186672Douglas Gregor  //   - When initializing a temporary to be bound to the first parameter
4642ed878af7914df535b32d64f555fa118413186672Douglas Gregor  //     of a constructor that takes a reference to possibly cv-qualified
4643ed878af7914df535b32d64f555fa118413186672Douglas Gregor  //     T as its first argument, called with a single argument in the
4644ed878af7914df535b32d64f555fa118413186672Douglas Gregor  //     context of direct-initialization, explicit conversion functions
4645ed878af7914df535b32d64f555fa118413186672Douglas Gregor  //     are also considered.
4646ed878af7914df535b32d64f555fa118413186672Douglas Gregor  bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() &&
4647ed878af7914df535b32d64f555fa118413186672Douglas Gregor                           Args.size() == 1 &&
4648ed878af7914df535b32d64f555fa118413186672Douglas Gregor                           Constructor->isCopyOrMoveConstructor();
4649ed878af7914df535b32d64f555fa118413186672Douglas Gregor
465010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  // Determine the arguments required to actually perform the constructor
465110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  // call.
465210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  if (S.CompleteConstructorCall(Constructor, move(Args),
4653ed878af7914df535b32d64f555fa118413186672Douglas Gregor                                Loc, ConstructorArgs,
4654ed878af7914df535b32d64f555fa118413186672Douglas Gregor                                AllowExplicitConv))
465510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    return ExprError();
465610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
465710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
465810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  if (Entity.getKind() == InitializedEntity::EK_Temporary &&
4659188158db29f50443b6e412f2a40c800b2669c957Sebastian Redl      (Kind.getKind() == InitializationKind::IK_DirectList ||
4660188158db29f50443b6e412f2a40c800b2669c957Sebastian Redl       (NumArgs != 1 && // FIXME: Hack to work around cast weirdness
4661188158db29f50443b6e412f2a40c800b2669c957Sebastian Redl        (Kind.getKind() == InitializationKind::IK_Direct ||
4662188158db29f50443b6e412f2a40c800b2669c957Sebastian Redl         Kind.getKind() == InitializationKind::IK_Value)))) {
466310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    // An explicitly-constructed temporary, e.g., X(1, 2).
466410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    unsigned NumExprs = ConstructorArgs.size();
466510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    Expr **Exprs = (Expr **)ConstructorArgs.take();
46665f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman    S.MarkFunctionReferenced(Loc, Constructor);
466710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    S.DiagnoseUseOfDecl(Constructor, Loc);
466810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
466910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo();
467010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    if (!TSInfo)
467110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc);
4672188158db29f50443b6e412f2a40c800b2669c957Sebastian Redl    SourceRange ParenRange;
4673188158db29f50443b6e412f2a40c800b2669c957Sebastian Redl    if (Kind.getKind() != InitializationKind::IK_DirectList)
4674188158db29f50443b6e412f2a40c800b2669c957Sebastian Redl      ParenRange = Kind.getParenRange();
467510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
467610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    CurInit = S.Owned(new (S.Context) CXXTemporaryObjectExpr(S.Context,
467710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                                             Constructor,
467810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                                             TSInfo,
467910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                                             Exprs,
468010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                                             NumExprs,
4681188158db29f50443b6e412f2a40c800b2669c957Sebastian Redl                                                             ParenRange,
468210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                                     HadMultipleCandidates,
468310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                         ConstructorInitRequiresZeroInit));
468410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  } else {
468510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    CXXConstructExpr::ConstructionKind ConstructKind =
468610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      CXXConstructExpr::CK_Complete;
468710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
468810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    if (Entity.getKind() == InitializedEntity::EK_Base) {
468910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      ConstructKind = Entity.getBaseSpecifier()->isVirtual() ?
469010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl        CXXConstructExpr::CK_VirtualBase :
469110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl        CXXConstructExpr::CK_NonVirtualBase;
469210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    } else if (Entity.getKind() == InitializedEntity::EK_Delegating) {
469310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      ConstructKind = CXXConstructExpr::CK_Delegating;
469410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    }
469510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
469610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    // Only get the parenthesis range if it is a direct construction.
469710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    SourceRange parenRange =
469810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl        Kind.getKind() == InitializationKind::IK_Direct ?
469910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl        Kind.getParenRange() : SourceRange();
470010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
470110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    // If the entity allows NRVO, mark the construction as elidable
470210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    // unconditionally.
470310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    if (Entity.allowsNRVO())
470410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
470510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        Constructor, /*Elidable=*/true,
470610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        move_arg(ConstructorArgs),
470710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        HadMultipleCandidates,
470810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        ConstructorInitRequiresZeroInit,
470910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        ConstructKind,
471010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        parenRange);
471110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    else
471210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
471310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        Constructor,
471410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        move_arg(ConstructorArgs),
471510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        HadMultipleCandidates,
471610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        ConstructorInitRequiresZeroInit,
471710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        ConstructKind,
471810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                        parenRange);
471910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  }
472010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  if (CurInit.isInvalid())
472110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    return ExprError();
472210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
472310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  // Only check access if all of that succeeded.
472410f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  S.CheckConstructorAccess(Loc, Constructor, Entity,
472510f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                           Step.Function.FoundDecl.getAccess());
472610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc);
472710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
472810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  if (shouldBindAsTemporary(Entity))
472910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
473010f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
473110f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl  return move(CurInit);
473210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl}
473310f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl
473436d02af300a207242f0486b4255420d8be796b21Richard Smith/// Determine whether the specified InitializedEntity definitely has a lifetime
473536d02af300a207242f0486b4255420d8be796b21Richard Smith/// longer than the current full-expression. Conservatively returns false if
473636d02af300a207242f0486b4255420d8be796b21Richard Smith/// it's unclear.
473736d02af300a207242f0486b4255420d8be796b21Richard Smithstatic bool
473836d02af300a207242f0486b4255420d8be796b21Richard SmithInitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) {
473936d02af300a207242f0486b4255420d8be796b21Richard Smith  const InitializedEntity *Top = &Entity;
474036d02af300a207242f0486b4255420d8be796b21Richard Smith  while (Top->getParent())
474136d02af300a207242f0486b4255420d8be796b21Richard Smith    Top = Top->getParent();
474236d02af300a207242f0486b4255420d8be796b21Richard Smith
474336d02af300a207242f0486b4255420d8be796b21Richard Smith  switch (Top->getKind()) {
474436d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_Variable:
474536d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_Result:
474636d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_Exception:
474736d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_Member:
474836d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_New:
474936d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_Base:
475036d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_Delegating:
475136d02af300a207242f0486b4255420d8be796b21Richard Smith    return true;
475236d02af300a207242f0486b4255420d8be796b21Richard Smith
475336d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_ArrayElement:
475436d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_VectorElement:
475536d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_BlockElement:
475636d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_ComplexElement:
475736d02af300a207242f0486b4255420d8be796b21Richard Smith    // Could not determine what the full initialization is. Assume it might not
475836d02af300a207242f0486b4255420d8be796b21Richard Smith    // outlive the full-expression.
475936d02af300a207242f0486b4255420d8be796b21Richard Smith    return false;
476036d02af300a207242f0486b4255420d8be796b21Richard Smith
476136d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_Parameter:
476236d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_Temporary:
476336d02af300a207242f0486b4255420d8be796b21Richard Smith  case InitializedEntity::EK_LambdaCapture:
476436d02af300a207242f0486b4255420d8be796b21Richard Smith    // The entity being initialized might not outlive the full-expression.
476536d02af300a207242f0486b4255420d8be796b21Richard Smith    return false;
476636d02af300a207242f0486b4255420d8be796b21Richard Smith  }
476736d02af300a207242f0486b4255420d8be796b21Richard Smith
476836d02af300a207242f0486b4255420d8be796b21Richard Smith  llvm_unreachable("unknown entity kind");
476936d02af300a207242f0486b4255420d8be796b21Richard Smith}
477036d02af300a207242f0486b4255420d8be796b21Richard Smith
4771dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiExprResult
477220093b4bf698f292c664676987541d5103b65b15Douglas GregorInitializationSequence::Perform(Sema &S,
477320093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                const InitializedEntity &Entity,
477420093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                const InitializationKind &Kind,
4775f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                MultiExprArg Args,
4776d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                QualType *ResultType) {
4777d695d6bb7323672e29dbb1556a3dafde3d3b2732Sebastian Redl  if (Failed()) {
477820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    unsigned NumArgs = Args.size();
477920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    Diagnose(S, Entity, Kind, (Expr **)Args.release(), NumArgs);
4780f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    return ExprError();
478120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
4782dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
47837491c499e826682e128a400038361ebcbde30eecSebastian Redl  if (getKind() == DependentSequence) {
4784d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // If the declaration is a non-dependent, incomplete array type
4785d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // that has an initializer, then its type will be completed once
4786d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    // the initializer is instantiated.
4787d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor    if (ResultType && !Entity.getType()->isDependentType() &&
4788d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        Args.size() == 1) {
4789d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor      QualType DeclType = Entity.getType();
4790d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      if (const IncompleteArrayType *ArrayT
4791d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                           = S.Context.getAsIncompleteArrayType(DeclType)) {
4792d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // FIXME: We don't currently have the ability to accurately
4793d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // compute the length of an initializer list without
4794d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // performing full type-checking of the initializer list
4795d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // (since we have to determine where braces are implicitly
4796d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // introduced and such).  So, we fall back to making the array
4797d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // type a dependently-sized array type with no specified
4798d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        // bound.
4799d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        if (isa<InitListExpr>((Expr *)Args.get()[0])) {
4800d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor          SourceRange Brackets;
4801d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor
4802d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor          // Scavange the location of the brackets from the entity, if we can.
4803d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor          if (DeclaratorDecl *DD = Entity.getDecl()) {
4804d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor            if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) {
4805d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor              TypeLoc TL = TInfo->getTypeLoc();
4806d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor              if (IncompleteArrayTypeLoc *ArrayLoc
4807d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor                                      = dyn_cast<IncompleteArrayTypeLoc>(&TL))
4808d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor              Brackets = ArrayLoc->getBracketsRange();
4809d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor            }
4810d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor          }
4811d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
4812d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor          *ResultType
4813d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor            = S.Context.getDependentSizedArrayType(ArrayT->getElementType(),
4814d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                                   /*NumElts=*/0,
4815d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                                   ArrayT->getSizeModifier(),
4816d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                       ArrayT->getIndexTypeCVRQualifiers(),
4817d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor                                                   Brackets);
4818d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor        }
4819d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
4820d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      }
4821d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    }
48225b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    if (Kind.getKind() == InitializationKind::IK_Direct &&
48235b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl        !Kind.isExplicitCast()) {
48245b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      // Rebuild the ParenListExpr.
48255b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      SourceRange ParenRange = Kind.getParenRange();
48265b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl      return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(),
48275b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl                                  move(Args));
48285b9cc5df25c2198f270dd1d5c438fdce70d4051dSebastian Redl    }
48290d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek    assert(Kind.getKind() == InitializationKind::IK_Copy ||
4830a9b55a499a8b5ae0c4b373f751ef62af74ec494eDouglas Gregor           Kind.isExplicitCast() ||
4831a9b55a499a8b5ae0c4b373f751ef62af74ec494eDouglas Gregor           Kind.getKind() == InitializationKind::IK_DirectList);
48320d9106fc97cde979a995e26b18bcd2643f8afb55Manuel Klimek    return ExprResult(Args.release()[0]);
483320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
483420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
48357491c499e826682e128a400038361ebcbde30eecSebastian Redl  // No steps means no initialization.
48367491c499e826682e128a400038361ebcbde30eecSebastian Redl  if (Steps.empty())
483799a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    return S.Owned((Expr *)0);
4838dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
483903544fccd1782418c1bf2528111683e18f43b913Richard Smith  if (S.getLangOpts().CPlusPlus0x && Entity.getType()->isReferenceType() &&
484003544fccd1782418c1bf2528111683e18f43b913Richard Smith      Args.size() == 1 && isa<InitListExpr>(Args.get()[0]) &&
484103544fccd1782418c1bf2528111683e18f43b913Richard Smith      Entity.getKind() != InitializedEntity::EK_Parameter) {
484203544fccd1782418c1bf2528111683e18f43b913Richard Smith    // Produce a C++98 compatibility warning if we are initializing a reference
484303544fccd1782418c1bf2528111683e18f43b913Richard Smith    // from an initializer list. For parameters, we produce a better warning
484403544fccd1782418c1bf2528111683e18f43b913Richard Smith    // elsewhere.
484503544fccd1782418c1bf2528111683e18f43b913Richard Smith    Expr *Init = Args.get()[0];
484603544fccd1782418c1bf2528111683e18f43b913Richard Smith    S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init)
484703544fccd1782418c1bf2528111683e18f43b913Richard Smith      << Init->getSourceRange();
484836d02af300a207242f0486b4255420d8be796b21Richard Smith  }
484936d02af300a207242f0486b4255420d8be796b21Richard Smith
485036d02af300a207242f0486b4255420d8be796b21Richard Smith  // Diagnose cases where we initialize a pointer to an array temporary, and the
485136d02af300a207242f0486b4255420d8be796b21Richard Smith  // pointer obviously outlives the temporary.
485236d02af300a207242f0486b4255420d8be796b21Richard Smith  if (Args.size() == 1 && Args.get()[0]->getType()->isArrayType() &&
485336d02af300a207242f0486b4255420d8be796b21Richard Smith      Entity.getType()->isPointerType() &&
485436d02af300a207242f0486b4255420d8be796b21Richard Smith      InitializedEntityOutlivesFullExpression(Entity)) {
485536d02af300a207242f0486b4255420d8be796b21Richard Smith    Expr *Init = Args.get()[0];
485636d02af300a207242f0486b4255420d8be796b21Richard Smith    Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context);
485736d02af300a207242f0486b4255420d8be796b21Richard Smith    if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary)
485836d02af300a207242f0486b4255420d8be796b21Richard Smith      S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay)
485936d02af300a207242f0486b4255420d8be796b21Richard Smith        << Init->getSourceRange();
486003544fccd1782418c1bf2528111683e18f43b913Richard Smith  }
486103544fccd1782418c1bf2528111683e18f43b913Richard Smith
4862d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType().getNonReferenceType();
4863d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  // FIXME: Ugly hack around the fact that Entity.getType() is not
4864a91eb541a6adf81acf872e7315bc6b814c3241ebEli Friedman  // the same as Entity.getDecl()->getType() in cases involving type merging,
4865a91eb541a6adf81acf872e7315bc6b814c3241ebEli Friedman  //  and we want latter when it makes sense.
4866d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  if (ResultType)
4867a91eb541a6adf81acf872e7315bc6b814c3241ebEli Friedman    *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() :
4868d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor                                     Entity.getType();
486920093b4bf698f292c664676987541d5103b65b15Douglas Gregor
487060d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult CurInit = S.Owned((Expr *)0);
4871dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4872dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // For initialization steps that start with a single initializer,
487399a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  // grab the only argument out the Args and place it into the "current"
487499a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  // initializer.
487599a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  switch (Steps.front().Kind) {
487618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_ResolveAddressOfOverloadedFunction:
487718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_CastDerivedToBaseRValue:
4878906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  case SK_CastDerivedToBaseXValue:
487918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_CastDerivedToBaseLValue:
488018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_BindReference:
488118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_BindReferenceToTemporary:
4882523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor  case SK_ExtraneousCopyToTemporary:
488318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_UserConversion:
488418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_QualificationConversionLValue:
4885906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl  case SK_QualificationConversionXValue:
488618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_QualificationConversionRValue:
488718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_ConversionSequence:
488818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_ListInitialization:
488913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  case SK_UnwrapInitList:
489013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl  case SK_RewrapInitList:
489118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_CAssignment:
4892cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  case SK_StringInit:
4893cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  case SK_ObjCObjectConversion:
4894f85e193739c953358c865005855253af4f68a497John McCall  case SK_ArrayInit:
48950f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith  case SK_ParenthesizedArrayInit:
4896f85e193739c953358c865005855253af4f68a497John McCall  case SK_PassByIndirectCopyRestore:
4897f85e193739c953358c865005855253af4f68a497John McCall  case SK_PassByIndirectRestore:
48982b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl  case SK_ProduceObjCObject:
48992b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl  case SK_StdInitializerList: {
490018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    assert(Args.size() == 1);
4901429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    CurInit = Args.get()[0];
4902429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    if (!CurInit.get()) return ExprError();
490318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    break;
4904f6a1648197562e0b133440d612d9af297d0a86ccJohn McCall  }
4905dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
490618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_ConstructorInitialization:
4907f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith  case SK_ListConstructorCall:
490818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  case SK_ZeroInitialization:
490918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    break;
491020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
4911dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4912dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // Walk through the computed steps for the initialization sequence,
491320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  // performing the specified conversions along the way.
491416006c901315fa12a108b4e571f187f4b676e426Douglas Gregor  bool ConstructorInitRequiresZeroInit = false;
491520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  for (step_iterator Step = step_begin(), StepEnd = step_end();
491620093b4bf698f292c664676987541d5103b65b15Douglas Gregor       Step != StepEnd; ++Step) {
491720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    if (CurInit.isInvalid())
4918f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall      return ExprError();
4919dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4920429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType();
4921dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
492220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    switch (Step->Kind) {
492320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_ResolveAddressOfOverloadedFunction:
4924dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // Overload resolution determined which function invoke; update the
492520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // initializer to reflect that choice.
4926429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl);
4927b697e084341185a08b3946df032fbc75f76a66e0John McCall      S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation());
4928b13b737a2450167c82e148590e8019b839ce6b98John McCall      CurInit = S.FixOverloadedFunctionReference(move(CurInit),
49296bb8017bb9e828d118e15e59d71c66bba323c364John McCall                                                 Step->Function.FoundDecl,
49309aa472c45d2bd81b7b52c225e8acc560d716db97John McCall                                                 Step->Function.Function);
493120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
4932dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
493320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_CastDerivedToBaseRValue:
4934906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    case SK_CastDerivedToBaseXValue:
493520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_CastDerivedToBaseLValue: {
493620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // We have a derived-to-base cast that produces either an rvalue or an
493720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // lvalue. Perform that cast.
4938dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4939f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      CXXCastPath BasePath;
49405cf86ba6b5a724bf91cb52feade1158f1fbeb605Anders Carlsson
494120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Casts to inaccessible base classes are allowed with C-style casts.
494220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast();
494320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (S.CheckDerivedToBaseConversion(SourceType, Step->Type,
4944429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                         CurInit.get()->getLocStart(),
4945429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                         CurInit.get()->getSourceRange(),
49465cf86ba6b5a724bf91cb52feade1158f1fbeb605Anders Carlsson                                         &BasePath, IgnoreBaseAccess))
4947f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall        return ExprError();
4948dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
49496fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      if (S.BasePathInvolvesVirtualBase(BasePath)) {
49506fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor        QualType T = SourceType;
49516fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor        if (const PointerType *Pointer = T->getAs<PointerType>())
49526fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor          T = Pointer->getPointeeType();
49536fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor        if (const RecordType *RecordTy = T->getAs<RecordType>())
4954429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          S.MarkVTableUsed(CurInit.get()->getLocStart(),
49556fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor                           cast<CXXRecordDecl>(RecordTy->getDecl()));
49566fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor      }
49576fb745bdf1ff1e32caf07e42093a7920726892c1Douglas Gregor
49585baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall      ExprValueKind VK =
4959906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl          Step->Kind == SK_CastDerivedToBaseLValue ?
49605baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall              VK_LValue :
4961906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl              (Step->Kind == SK_CastDerivedToBaseXValue ?
49625baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                   VK_XValue :
49635baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                   VK_RValue);
4964f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      CurInit = S.Owned(ImplicitCastExpr::Create(S.Context,
4965f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall                                                 Step->Type,
49662de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall                                                 CK_DerivedToBase,
49675baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                                 CurInit.get(),
49685baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                                                 &BasePath, VK));
496920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
497020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
4971dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
497220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_BindReference:
4973429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (FieldDecl *BitField = CurInit.get()->getBitField()) {
497420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // References cannot bind to bit fields (C++ [dcl.init.ref]p5).
497520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield)
4976d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor          << Entity.getType().isVolatileQualified()
497720093b4bf698f292c664676987541d5103b65b15Douglas Gregor          << BitField->getDeclName()
4978429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          << CurInit.get()->getSourceRange();
497920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        S.Diag(BitField->getLocation(), diag::note_bitfield_decl);
4980f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall        return ExprError();
498120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      }
4982a6fe0bf89db4372c9e8e8a5c2a50593c2977df29Anders Carlsson
4983429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (CurInit.get()->refersToVectorElement()) {
498441593e3b7da967b52bc3c7f488b8752dee63f38eJohn McCall        // References cannot bind to vector elements.
4985093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson        S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element)
4986093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson          << Entity.getType().isVolatileQualified()
4987429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          << CurInit.get()->getSourceRange();
4988a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor        PrintInitLocationNote(S, Entity);
4989f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall        return ExprError();
4990093802675b1548f2a5f44c29938d65cce00d58bbAnders Carlsson      }
4991dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
499220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Reference binding does not have any corresponding ASTs.
499320093b4bf698f292c664676987541d5103b65b15Douglas Gregor
499420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Check exception specifications
4995429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType))
4996f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall        return ExprError();
49973aba09376c5f49c4c8d176109ea4835bc2c528eeAnders Carlsson
499820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
49993aba09376c5f49c4c8d176109ea4835bc2c528eeAnders Carlsson
500020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_BindReferenceToTemporary:
500120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Check exception specifications
5002429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType))
5003f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall        return ExprError();
500420093b4bf698f292c664676987541d5103b65b15Douglas Gregor
500503e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor      // Materialize the temporary into memory.
5006b4b7b5034bb8304ed03416635bf64c75c39889fcDouglas Gregor      CurInit = new (S.Context) MaterializeTemporaryExpr(
5007b4b7b5034bb8304ed03416635bf64c75c39889fcDouglas Gregor                                         Entity.getType().getNonReferenceType(),
5008b4b7b5034bb8304ed03416635bf64c75c39889fcDouglas Gregor                                                         CurInit.get(),
500903e80030515c800d1ab44125b9052dfffd1bd04cDouglas Gregor                                     Entity.getType()->isLValueReferenceType());
5010d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor
5011d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor      // If we're binding to an Objective-C object that has lifetime, we
5012d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor      // need cleanups.
50134e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (S.getLangOpts().ObjCAutoRefCount &&
5014d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor          CurInit.get()->getType()->isObjCLifetimeType())
5015d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor        S.ExprNeedsCleanups = true;
5016d7b23167763f43d3e76b0de26141b1e52613ee3cDouglas Gregor
501720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
5018dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5019523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor    case SK_ExtraneousCopyToTemporary:
5020dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      CurInit = CopyObject(S, Step->Type, Entity, move(CurInit),
5021523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor                           /*IsExtraneousCopy=*/true);
5022523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      break;
5023523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
502420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_UserConversion: {
502520093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // We have a user-defined conversion that invokes either a constructor
502620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // or a conversion function.
5027daa8e4e888758d55a7a759dd4a91b83921cef222John McCall      CastKind CastKind;
502818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      bool IsCopy = false;
50299aa472c45d2bd81b7b52c225e8acc560d716db97John McCall      FunctionDecl *Fn = Step->Function.Function;
50309aa472c45d2bd81b7b52c225e8acc560d716db97John McCall      DeclAccessPair FoundFn = Step->Function.FoundDecl;
50317cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara      bool HadMultipleCandidates = Step->Function.HadMultipleCandidates;
50324154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor      bool CreatedObject = false;
5033b13b737a2450167c82e148590e8019b839ce6b98John McCall      if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) {
503420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // Build a call to the selected constructor.
5035ca0408fb49c1370430672acf2d770b7151cf71deJohn McCall        ASTOwningVector<Expr*> ConstructorArgs(S);
5036429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        SourceLocation Loc = CurInit.get()->getLocStart();
503720093b4bf698f292c664676987541d5103b65b15Douglas Gregor        CurInit.release(); // Ownership transferred into MultiExprArg, below.
5038b13b737a2450167c82e148590e8019b839ce6b98John McCall
503920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // Determine the arguments required to actually perform the constructor
504020093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // call.
5041429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        Expr *Arg = CurInit.get();
504220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (S.CompleteConstructorCall(Constructor,
5043429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                      MultiExprArg(&Arg, 1),
504420093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                      Loc, ConstructorArgs))
5045f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall          return ExprError();
5046dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5047f2e4dfcd325db2c1a960697be2c750f277dcafceRichard Smith        // Build an expression that constructs a temporary.
5048dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor,
50497a1fad38256eb4c5129359be85ba1ea1678eb5c9John McCall                                          move_arg(ConstructorArgs),
50507cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                          HadMultipleCandidates,
50517a1fad38256eb4c5129359be85ba1ea1678eb5c9John McCall                                          /*ZeroInit*/ false,
5052428edafa9eb80e01dd40aab31d4166a787a741e1Chandler Carruth                                          CXXConstructExpr::CK_Complete,
5053428edafa9eb80e01dd40aab31d4166a787a741e1Chandler Carruth                                          SourceRange());
505420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (CurInit.isInvalid())
5055f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall          return ExprError();
5056b13b737a2450167c82e148590e8019b839ce6b98John McCall
50579a68a67c6ae4982001815cc04f69b8781058263aAnders Carlsson        S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity,
50589aa472c45d2bd81b7b52c225e8acc560d716db97John McCall                                 FoundFn.getAccess());
5059b697e084341185a08b3946df032fbc75f76a66e0John McCall        S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation());
5060dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
50612de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        CastKind = CK_ConstructorConversion;
506218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        QualType Class = S.Context.getTypeDeclType(Constructor->getParent());
506318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        if (S.Context.hasSameUnqualifiedType(SourceType, Class) ||
506418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor            S.IsDerivedFrom(SourceType, Class))
506518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor          IsCopy = true;
5066dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
50674154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor        CreatedObject = true;
506820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      } else {
506920093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // Build a call to the conversion function.
5070b13b737a2450167c82e148590e8019b839ce6b98John McCall        CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn);
5071429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0,
50729aa472c45d2bd81b7b52c225e8acc560d716db97John McCall                                    FoundFn);
5073b697e084341185a08b3946df032fbc75f76a66e0John McCall        S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation());
5074dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5075dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        // FIXME: Should we move this initialization into a separate
507620093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // derived-to-base conversion? I believe the answer is "no", because
507720093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // we don't want to turn off access control here for c-style casts.
5078429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        ExprResult CurInitExprRes =
5079429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0,
5080429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                                FoundFn, Conversion);
5081429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        if(CurInitExprRes.isInvalid())
5082f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall          return ExprError();
5083429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        CurInit = move(CurInitExprRes);
5084dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
508520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        // Build the actual call to the conversion function.
50867cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara        CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion,
50877cc58b4c927fca539d43eaa58e00dca95946eb7cAbramo Bagnara                                           HadMultipleCandidates);
508820093b4bf698f292c664676987541d5103b65b15Douglas Gregor        if (CurInit.isInvalid() || !CurInit.get())
5089f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall          return ExprError();
5090dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
50912de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall        CastKind = CK_UserDefinedConversion;
5092dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
50934154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor        CreatedObject = Conversion->getResultType()->isRecordType();
509420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      }
5095dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
50963b80232b50c29b245e674f5aa02047b408e41018Sebastian Redl      bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back());
5097960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara      bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity);
5098960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara
5099960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara      if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) {
5100429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        QualType T = CurInit.get()->getType();
51014154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor        if (const RecordType *Record = T->getAs<RecordType>()) {
5102dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          CXXDestructorDecl *Destructor
5103db89f289a22897f1031c92f3e49f7a9ba7bae61eDouglas Gregor            = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl()));
5104429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor,
51054154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor                                  S.PDiag(diag::err_access_dtor_temp) << T);
51065f2987c11491edb186401d4e8eced275f0ea7c5eEli Friedman          S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor);
5107429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart());
51084154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor        }
51094154e0b1a5d03cbe4836e381c7d6187b7a0a200cDouglas Gregor      }
5110dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5111f871d0cc377a1367b519a6cce26be74607566ebaJohn McCall      CurInit = S.Owned(ImplicitCastExpr::Create(S.Context,
5112429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                                 CurInit.get()->getType(),
5113429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                                 CastKind, CurInit.get(), 0,
5114104be6fc8de206c9b79b241b36ebaf9407b3be08Eli Friedman                                                CurInit.get()->getValueKind()));
5115960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara      if (MaybeBindToTemp)
5116960809e7e9f4a6e949797d20bc081da80495c0e1Abramo Bagnara        CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>());
51172f59979a7cc7929f53c9984423b0abeb83113442Douglas Gregor      if (RequiresCopy)
5118523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor        CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity,
5119523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor                             move(CurInit), /*IsExtraneousCopy=*/false);
512020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
512120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
5122906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
512320093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case SK_QualificationConversionLValue:
5124906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    case SK_QualificationConversionXValue:
5125906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    case SK_QualificationConversionRValue: {
512620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      // Perform a qualification conversion; these can never go wrong.
51275baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall      ExprValueKind VK =
5128906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl          Step->Kind == SK_QualificationConversionLValue ?
51295baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall              VK_LValue :
5130906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl              (Step->Kind == SK_QualificationConversionXValue ?
51315baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                   VK_XValue :
51325baba9d98364a3525d6afa15a04cdad82fd6dd30John McCall                   VK_RValue);
5133429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK);
513420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
5135906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    }
5136906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
5137f0e43e5c4634870b8ac7bf65d5ffa5f292d4c8a5Douglas Gregor    case SK_ConversionSequence: {
5138f85e193739c953358c865005855253af4f68a497John McCall      Sema::CheckedConversionKind CCK
5139f85e193739c953358c865005855253af4f68a497John McCall        = Kind.isCStyleCast()? Sema::CCK_CStyleCast
5140f85e193739c953358c865005855253af4f68a497John McCall        : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast
5141c8d7f586180995ba33d03c0f6115b6a7bdefe326Richard Smith        : Kind.isExplicitCast()? Sema::CCK_OtherCast
5142f85e193739c953358c865005855253af4f68a497John McCall        : Sema::CCK_ImplicitConversion;
5143429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      ExprResult CurInitExprRes =
5144429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS,
5145f85e193739c953358c865005855253af4f68a497John McCall                                    getAssignmentAction(Entity), CCK);
5146429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (CurInitExprRes.isInvalid())
5147f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall        return ExprError();
5148429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      CurInit = move(CurInitExprRes);
514920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
5150f0e43e5c4634870b8ac7bf65d5ffa5f292d4c8a5Douglas Gregor    }
5151dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5152d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    case SK_ListInitialization: {
5153429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      InitListExpr *InitList = cast<InitListExpr>(CurInit.get());
515413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      // Hack: We must pass *ResultType if available in order to set the type
515513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      // of arrays, e.g. in 'int ar[] = {1, 2, 3};'.
515613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      // But in 'const X &x = {1, 2, 3};' we're supposed to initialize a
515713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      // temporary, not a reference, so we should pass Ty.
515813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      // Worst case: 'const int (&arref)[] = {1, 2, 3};'.
515913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      // Since this step is never used for a reference directly, we explicitly
516013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      // unwrap references here and rewrap them afterwards.
516113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      // We also need to create a InitializeTemporary entity for this.
516213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      QualType Ty = ResultType ? ResultType->getNonReferenceType() : Step->Type;
5163cbf82092cdb9e3a2e8528384c878f2084a14981cSebastian Redl      bool IsTemporary = Entity.getType()->isReferenceType();
516413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty);
516513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      InitListChecker PerformInitList(S, IsTemporary ? TempEntity : Entity,
516613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl          InitList, Ty, /*VerifyOnly=*/false,
5167168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl          Kind.getKind() != InitializationKind::IK_DirectList ||
51684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie            !S.getLangOpts().CPlusPlus0x);
516914b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      if (PerformInitList.HadError())
5170f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall        return ExprError();
5171d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
517213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      if (ResultType) {
517313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl        if ((*ResultType)->isRValueReferenceType())
517413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl          Ty = S.Context.getRValueReferenceType(Ty);
517513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl        else if ((*ResultType)->isLValueReferenceType())
517613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl          Ty = S.Context.getLValueReferenceType(Ty,
517713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl            (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue());
517813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl        *ResultType = Ty;
517913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      }
518013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
518113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      InitListExpr *StructuredInitList =
518213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl          PerformInitList.getFullyStructuredList();
5183d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      CurInit.release();
518413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      CurInit = S.Owned(StructuredInitList);
5185d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      break;
5186d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    }
518751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor
518810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    case SK_ListConstructorCall: {
5189168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl      // When an initializer list is passed for a parameter of type "reference
5190168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl      // to object", we don't get an EK_Temporary entity, but instead an
5191168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl      // EK_Parameter entity with reference type.
5192bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      // FIXME: This is a hack. What we really should do is create a user
5193bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      // conversion step for this case, but this makes it considerably more
5194bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      // complicated. For now, this will do.
5195168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl      InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(
5196168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl                                        Entity.getType().getNonReferenceType());
5197168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl      bool UseTemporary = Entity.getType()->isReferenceType();
5198f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      assert(Args.size() == 1 && "expected a single argument for list init");
5199f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith      InitListExpr *InitList = cast<InitListExpr>(Args.get()[0]);
520003544fccd1782418c1bf2528111683e18f43b913Richard Smith      S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init)
520103544fccd1782418c1bf2528111683e18f43b913Richard Smith        << InitList->getSourceRange();
520210f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      MultiExprArg Arg(InitList->getInits(), InitList->getNumInits());
5203168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl      CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity :
5204168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl                                                                   Entity,
5205168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl                                                 Kind, move(Arg), *Step,
520610f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                               ConstructorInitRequiresZeroInit);
520710f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl      break;
520810f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl    }
52098713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl
521013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    case SK_UnwrapInitList:
521113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0));
521213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      break;
521313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
521413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    case SK_RewrapInitList: {
521513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      Expr *E = CurInit.take();
521613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      InitListExpr *Syntactic = Step->WrappingSyntacticList;
521713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      InitListExpr *ILE = new (S.Context) InitListExpr(S.Context,
521813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl          Syntactic->getLBraceLoc(), &E, 1, Syntactic->getRBraceLoc());
521913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      ILE->setSyntacticForm(Syntactic);
522013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      ILE->setType(E->getType());
522113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      ILE->setValueKind(E->getValueKind());
522213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      CurInit = S.Owned(ILE);
522313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      break;
522413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    }
522513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
5226bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl    case SK_ConstructorInitialization: {
5227bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      // When an initializer list is passed for a parameter of type "reference
5228bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      // to object", we don't get an EK_Temporary entity, but instead an
5229bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      // EK_Parameter entity with reference type.
5230bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      // FIXME: This is a hack. What we really should do is create a user
5231bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      // conversion step for this case, but this makes it considerably more
5232bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      // complicated. For now, this will do.
5233bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(
5234bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl                                        Entity.getType().getNonReferenceType());
5235bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      bool UseTemporary = Entity.getType()->isReferenceType();
5236bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl      CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity
5237bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl                                                                 : Entity,
5238bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl                                                 Kind, move(Args), *Step,
523910f04a6267eb07d3be00db1fd0369e1398f5d0a8Sebastian Redl                                               ConstructorInitRequiresZeroInit);
524051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      break;
5241bac5cf4110c1c9ba0992fad4fd9f66cedc27f3daSebastian Redl    }
5242dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
524371d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    case SK_ZeroInitialization: {
524416006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      step_iterator NextStep = Step;
524516006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      ++NextStep;
5246dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (NextStep != StepEnd &&
5247f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith          (NextStep->Kind == SK_ConstructorInitialization ||
5248f4bb8d06c4f1665f89a9e9ddd61f2a2d26904da0Richard Smith           NextStep->Kind == SK_ListConstructorCall)) {
524916006c901315fa12a108b4e571f187f4b676e426Douglas Gregor        // The need for zero-initialization is recorded directly into
525016006c901315fa12a108b4e571f187f4b676e426Douglas Gregor        // the call to the object's constructor within the next step.
525116006c901315fa12a108b4e571f187f4b676e426Douglas Gregor        ConstructorInitRequiresZeroInit = true;
525216006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      } else if (Kind.getKind() == InitializationKind::IK_Value &&
52534e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie                 S.getLangOpts().CPlusPlus &&
525416006c901315fa12a108b4e571f187f4b676e426Douglas Gregor                 !Kind.isImplicitValueInit()) {
5255ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor        TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo();
5256ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor        if (!TSInfo)
5257dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type,
5258ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor                                                    Kind.getRange().getBegin());
5259ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor
5260ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor        CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr(
5261ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor                              TSInfo->getType().getNonLValueExprType(S.Context),
5262ab6677ec401cfd2c82b34e4cdfebd55a9dc25778Douglas Gregor                                                                 TSInfo,
526371d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor                                                    Kind.getRange().getEnd()));
526416006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      } else {
526571d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor        CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type));
526616006c901315fa12a108b4e571f187f4b676e426Douglas Gregor      }
526771d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor      break;
526871d1740c94060b424bb745d6c6973ff27cfdee06Douglas Gregor    }
526918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
527018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    case SK_CAssignment: {
5271429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      QualType SourceType = CurInit.get()->getType();
5272429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      ExprResult Result = move(CurInit);
527318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      Sema::AssignConvertType ConvTy =
5274429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        S.CheckSingleAssignmentConstraints(Step->Type, Result);
5275429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (Result.isInvalid())
5276429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        return ExprError();
5277429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      CurInit = move(Result);
5278aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor
5279aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor      // If this is a call, allow conversion to a transparent union.
5280429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      ExprResult CurInitExprRes = move(CurInit);
5281aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor      if (ConvTy != Sema::Compatible &&
5282aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor          Entity.getKind() == InitializedEntity::EK_Parameter &&
5283429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley          S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes)
5284aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor            == Sema::Compatible)
5285aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor        ConvTy = Sema::Compatible;
5286429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      if (CurInitExprRes.isInvalid())
5287429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        return ExprError();
5288429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      CurInit = move(CurInitExprRes);
5289aa0373107968aa7a26bf63f4a2673b8325b800afDouglas Gregor
5290a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor      bool Complained;
529118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(),
529218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                     Step->Type, SourceType,
5293429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                                     CurInit.get(),
5294a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor                                     getAssignmentAction(Entity),
5295a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor                                     &Complained)) {
5296a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor        PrintInitLocationNote(S, Entity);
5297f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall        return ExprError();
5298a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor      } else if (Complained)
5299a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor        PrintInitLocationNote(S, Entity);
530018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      break;
530118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    }
5302cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman
5303cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman    case SK_StringInit: {
5304cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman      QualType Ty = Step->Type;
5305429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty,
5306fef8b344ea299a1717cc94486f5df6facf3e337cJohn McCall                      S.Context.getAsArrayType(Ty), S);
5307cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman      break;
5308cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman    }
5309569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
5310569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    case SK_ObjCObjectConversion:
5311429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type,
53122de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall                          CK_ObjCObjectLValueCast,
5313c1c0dfb376b829b94d4c61e9f358ce23e6aa3169Eli Friedman                          CurInit.get()->getValueKind());
5314569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor      break;
5315cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
5316cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    case SK_ArrayInit:
5317cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      // Okay: we checked everything before creating this step. Note that
5318cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      // this is a GNU extension.
5319cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      S.Diag(Kind.getLocation(), diag::ext_array_init_copy)
5320429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << Step->Type << CurInit.get()->getType()
5321429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley        << CurInit.get()->getSourceRange();
5322cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
5323cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      // If the destination type is an incomplete array type, update the
5324cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      // type accordingly.
5325cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      if (ResultType) {
5326cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor        if (const IncompleteArrayType *IncompleteDest
5327cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor                           = S.Context.getAsIncompleteArrayType(Step->Type)) {
5328cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor          if (const ConstantArrayType *ConstantSource
5329429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley                 = S.Context.getAsConstantArrayType(CurInit.get()->getType())) {
5330cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor            *ResultType = S.Context.getConstantArrayType(
5331cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor                                             IncompleteDest->getElementType(),
5332cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor                                             ConstantSource->getSize(),
5333cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor                                             ArrayType::Normal, 0);
5334cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor          }
5335cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor        }
5336cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      }
5337f85e193739c953358c865005855253af4f68a497John McCall      break;
5338cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
53390f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith    case SK_ParenthesizedArrayInit:
53400f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith      // Okay: we checked everything before creating this step. Note that
53410f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith      // this is a GNU extension.
53420f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith      S.Diag(Kind.getLocation(), diag::ext_array_init_parens)
53430f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith        << CurInit.get()->getSourceRange();
53440f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith      break;
53450f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith
5346f85e193739c953358c865005855253af4f68a497John McCall    case SK_PassByIndirectCopyRestore:
5347f85e193739c953358c865005855253af4f68a497John McCall    case SK_PassByIndirectRestore:
5348f85e193739c953358c865005855253af4f68a497John McCall      checkIndirectCopyRestoreSource(S, CurInit.get());
5349f85e193739c953358c865005855253af4f68a497John McCall      CurInit = S.Owned(new (S.Context)
5350f85e193739c953358c865005855253af4f68a497John McCall                        ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type,
5351f85e193739c953358c865005855253af4f68a497John McCall                                Step->Kind == SK_PassByIndirectCopyRestore));
5352f85e193739c953358c865005855253af4f68a497John McCall      break;
5353f85e193739c953358c865005855253af4f68a497John McCall
5354f85e193739c953358c865005855253af4f68a497John McCall    case SK_ProduceObjCObject:
5355f85e193739c953358c865005855253af4f68a497John McCall      CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type,
535633e56f3273457bfa22c7c50bc46cf5a18216863dJohn McCall                                                 CK_ARCProduceObject,
5357f85e193739c953358c865005855253af4f68a497John McCall                                                 CurInit.take(), 0, VK_RValue));
5358cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      break;
53592b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl
53602b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    case SK_StdInitializerList: {
53612b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      QualType Dest = Step->Type;
53622b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      QualType E;
53632b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      bool Success = S.isStdInitializerList(Dest, &E);
53642b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      (void)Success;
53652b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      assert(Success && "Destination type changed?");
53662835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl
53672835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl      // If the element type has a destructor, check it.
53682835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl      if (CXXRecordDecl *RD = E->getAsCXXRecordDecl()) {
53692835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl        if (!RD->hasIrrelevantDestructor()) {
53702835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl          if (CXXDestructorDecl *Destructor = S.LookupDestructor(RD)) {
53712835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl            S.MarkFunctionReferenced(Kind.getLocation(), Destructor);
53722835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl            S.CheckDestructorAccess(Kind.getLocation(), Destructor,
53732835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl                                    S.PDiag(diag::err_access_dtor_temp) << E);
53742835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl            S.DiagnoseUseOfDecl(Destructor, Kind.getLocation());
53752835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl          }
53762835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl        }
53772835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl      }
53782835745a451002798fed9800aeb19277f6a8fcb3Sebastian Redl
53792b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      InitListExpr *ILE = cast<InitListExpr>(CurInit.take());
538003544fccd1782418c1bf2528111683e18f43b913Richard Smith      S.Diag(ILE->getExprLoc(), diag::warn_cxx98_compat_initializer_list_init)
538103544fccd1782418c1bf2528111683e18f43b913Richard Smith        << ILE->getSourceRange();
53822b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      unsigned NumInits = ILE->getNumInits();
53832b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      SmallVector<Expr*, 16> Converted(NumInits);
53842b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary(
53852b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl          S.Context.getConstantArrayType(E,
53862b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl              llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()),
53872b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl                          NumInits),
53882b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl              ArrayType::Normal, 0));
53892b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      InitializedEntity Element =InitializedEntity::InitializeElement(S.Context,
53902b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl          0, HiddenArray);
53912b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      for (unsigned i = 0; i < NumInits; ++i) {
53922b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl        Element.setElementIndex(i);
53932b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl        ExprResult Init = S.Owned(ILE->getInit(i));
53942b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl        ExprResult Res = S.PerformCopyInitialization(Element,
53952b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl                                                     Init.get()->getExprLoc(),
53962b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl                                                     Init);
53972b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl        assert(!Res.isInvalid() && "Result changed since try phase.");
53982b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl        Converted[i] = Res.take();
53992b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      }
54002b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      InitListExpr *Semantic = new (S.Context)
54012b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl          InitListExpr(S.Context, ILE->getLBraceLoc(),
54022b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl                       Converted.data(), NumInits, ILE->getRBraceLoc());
54032b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      Semantic->setSyntacticForm(ILE);
54042b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      Semantic->setType(Dest);
540532cf1f27ae8620e7b79bb4e81a067187c0aab7aeSebastian Redl      Semantic->setInitializesStdInitializerList();
54062b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      CurInit = S.Owned(Semantic);
54072b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      break;
54082b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    }
540920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
541020093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
541115d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall
541215d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall  // Diagnose non-fatal problems with the completed initialization.
541315d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall  if (Entity.getKind() == InitializedEntity::EK_Member &&
541415d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall      cast<FieldDecl>(Entity.getDecl())->isBitField())
541515d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall    S.CheckBitFieldInitialization(Kind.getLocation(),
541615d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall                                  cast<FieldDecl>(Entity.getDecl()),
541715d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall                                  CurInit.get());
5418dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
541920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  return move(CurInit);
542020093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
542120093b4bf698f292c664676987541d5103b65b15Douglas Gregor
542220093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
542320093b4bf698f292c664676987541d5103b65b15Douglas Gregor// Diagnose initialization failures
542420093b4bf698f292c664676987541d5103b65b15Douglas Gregor//===----------------------------------------------------------------------===//
5425dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumibool InitializationSequence::Diagnose(Sema &S,
542620093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                      const InitializedEntity &Entity,
542720093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                      const InitializationKind &Kind,
542820093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                      Expr **Args, unsigned NumArgs) {
5429d695d6bb7323672e29dbb1556a3dafde3d3b2732Sebastian Redl  if (!Failed())
543020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    return false;
5431dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5432d6542d8efcf8389c3aab764f9e29ac284e16eda6Douglas Gregor  QualType DestType = Entity.getType();
543320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  switch (Failure) {
543420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_TooManyInitsForReference:
54359db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    // FIXME: Customize for the initialized entity?
54369db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (NumArgs == 0)
54379db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      S.Diag(Kind.getLocation(), diag::err_reference_without_init)
54389db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        << DestType.getNonReferenceType();
54399db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    else  // FIXME: diagnostic below could be better!
54409db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits)
54419db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        << SourceRange(Args[0]->getLocStart(), Args[NumArgs - 1]->getLocEnd());
544220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
5443dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
544420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ArrayNeedsInitList:
544520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ArrayNeedsInitListOrStringLiteral:
544620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list)
544720093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << (Failure == FK_ArrayNeedsInitListOrStringLiteral);
544820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
5449dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5450cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  case FK_ArrayTypeMismatch:
5451cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor  case FK_NonConstantArrayInit:
5452cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    S.Diag(Kind.getLocation(),
5453cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor           (Failure == FK_ArrayTypeMismatch
5454cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor              ? diag::err_array_init_different_type
5455cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor              : diag::err_array_init_non_constant_array))
5456cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      << DestType.getNonReferenceType()
5457cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      << Args[0]->getType()
5458cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      << Args[0]->getSourceRange();
5459cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    break;
5460cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
546173076431605556fdbf28d287d084a73a24a8b8d4John McCall  case FK_VariableLengthArrayHasInitializer:
546273076431605556fdbf28d287d084a73a24a8b8d4John McCall    S.Diag(Kind.getLocation(), diag::err_variable_object_no_init)
546373076431605556fdbf28d287d084a73a24a8b8d4John McCall      << Args[0]->getSourceRange();
546473076431605556fdbf28d287d084a73a24a8b8d4John McCall    break;
546573076431605556fdbf28d287d084a73a24a8b8d4John McCall
54666bb8017bb9e828d118e15e59d71c66bba323c364John McCall  case FK_AddressOfOverloadFailed: {
54676bb8017bb9e828d118e15e59d71c66bba323c364John McCall    DeclAccessPair Found;
5468dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    S.ResolveAddressOfOverloadedFunction(Args[0],
546920093b4bf698f292c664676987541d5103b65b15Douglas Gregor                                         DestType.getNonReferenceType(),
54706bb8017bb9e828d118e15e59d71c66bba323c364John McCall                                         true,
54716bb8017bb9e828d118e15e59d71c66bba323c364John McCall                                         Found);
547220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
54736bb8017bb9e828d118e15e59d71c66bba323c364John McCall  }
5474dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
547520093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ReferenceInitOverloadFailed:
54764a520a2bd8a6b79fa5d4771f02e34e7bd6bc461dDouglas Gregor  case FK_UserConversionOverloadFailed:
547720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    switch (FailedOverloadResult) {
547820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_Ambiguous:
547918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      if (Failure == FK_UserConversionOverloadFailed)
548018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition)
548118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor          << Args[0]->getType() << DestType
548218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor          << Args[0]->getSourceRange();
548318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      else
548418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor        S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous)
548518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor          << DestType << Args[0]->getType()
548618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor          << Args[0]->getSourceRange();
548718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
548813a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles      FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates,
548913a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                        llvm::makeArrayRef(Args, NumArgs));
549020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
5491dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
549220093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_No_Viable_Function:
549320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition)
549420093b4bf698f292c664676987541d5103b65b15Douglas Gregor        << Args[0]->getType() << DestType.getNonReferenceType()
549520093b4bf698f292c664676987541d5103b65b15Douglas Gregor        << Args[0]->getSourceRange();
549613a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles      FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates,
549713a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                        llvm::makeArrayRef(Args, NumArgs));
549820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
5499dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
550020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_Deleted: {
550120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function)
550220093b4bf698f292c664676987541d5103b65b15Douglas Gregor        << Args[0]->getType() << DestType.getNonReferenceType()
550320093b4bf698f292c664676987541d5103b65b15Douglas Gregor        << Args[0]->getSourceRange();
550420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      OverloadCandidateSet::iterator Best;
5505120d63cd4465230c2cd56508c7cd8e0ad00848e7John McCall      OverloadingResult Ovl
55068fcc516a5d5b2fec0023a9dced29285a38b95355Douglas Gregor        = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best,
55078fcc516a5d5b2fec0023a9dced29285a38b95355Douglas Gregor                                                true);
550820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      if (Ovl == OR_Deleted) {
55096c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith        S.NoteDeletedFunction(Best->Function);
551020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      } else {
55119f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin        llvm_unreachable("Inconsistent overload resolution?");
551220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      }
551320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      break;
551420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
5515dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
551620093b4bf698f292c664676987541d5103b65b15Douglas Gregor    case OR_Success:
55179f61aa9e280adea9fbf3365f0e4f6ed568c9885aJeffrey Yasskin      llvm_unreachable("Conversion did not fail!");
551820093b4bf698f292c664676987541d5103b65b15Douglas Gregor    }
551920093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
5520dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
552120093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_NonConstLValueReferenceBindingToTemporary:
552213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    if (isa<InitListExpr>(Args[0])) {
552313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      S.Diag(Kind.getLocation(),
552413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl             diag::err_lvalue_reference_bind_to_initlist)
552513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      << DestType.getNonReferenceType().isVolatileQualified()
552613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      << DestType.getNonReferenceType()
552713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      << Args[0]->getSourceRange();
552813dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      break;
552913dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    }
553013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    // Intentional fallthrough
553113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
553220093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_NonConstLValueReferenceBindingToUnrelated:
5533dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    S.Diag(Kind.getLocation(),
553420093b4bf698f292c664676987541d5103b65b15Douglas Gregor           Failure == FK_NonConstLValueReferenceBindingToTemporary
553520093b4bf698f292c664676987541d5103b65b15Douglas Gregor             ? diag::err_lvalue_reference_bind_to_temporary
553620093b4bf698f292c664676987541d5103b65b15Douglas Gregor             : diag::err_lvalue_reference_bind_to_unrelated)
5537ef06e2451c3069569ba543e7b1c5e0de7a257005Douglas Gregor      << DestType.getNonReferenceType().isVolatileQualified()
553820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << DestType.getNonReferenceType()
553920093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getType()
554020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getSourceRange();
554120093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
5542dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
554320093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_RValueReferenceBindingToLValue:
554420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref)
5545fb5d7efc279335bde2cdc0a73da525d6b28201e9Douglas Gregor      << DestType.getNonReferenceType() << Args[0]->getType()
554620093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getSourceRange();
554720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
5548dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
554920093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ReferenceInitDropsQualifiers:
555020093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals)
555120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << DestType.getNonReferenceType()
555220093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getType()
555320093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getSourceRange();
555420093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
5555dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
555620093b4bf698f292c664676987541d5103b65b15Douglas Gregor  case FK_ReferenceInitFailed:
555720093b4bf698f292c664676987541d5103b65b15Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_reference_bind_failed)
555820093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << DestType.getNonReferenceType()
55597eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall      << Args[0]->isLValue()
556020093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getType()
556120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getSourceRange();
5562926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    if (DestType.getNonReferenceType()->isObjCObjectPointerType() &&
5563926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor        Args[0]->getType()->isObjCObjectPointerType())
5564926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      S.EmitRelatedResultTypeNote(Args[0]);
556520093b4bf698f292c664676987541d5103b65b15Douglas Gregor    break;
5566dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
55671be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor  case FK_ConversionFailed: {
55681be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    QualType FromType = Args[0]->getType();
55696efd4c55a1a481d92966a91141c03e8145234cf6Richard Trieu    PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed)
557018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor      << (int)Entity.getKind()
557120093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << DestType
55727eb0a9eb0cde8444b97f9c5b713d9be7a6f1e607John McCall      << Args[0]->isLValue()
55731be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor      << FromType
557420093b4bf698f292c664676987541d5103b65b15Douglas Gregor      << Args[0]->getSourceRange();
55756efd4c55a1a481d92966a91141c03e8145234cf6Richard Trieu    S.HandleFunctionTypeMismatch(PDiag, FromType, DestType);
55766efd4c55a1a481d92966a91141c03e8145234cf6Richard Trieu    S.Diag(Kind.getLocation(), PDiag);
5577926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor    if (DestType.getNonReferenceType()->isObjCObjectPointerType() &&
5578926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor        Args[0]->getType()->isObjCObjectPointerType())
5579926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor      S.EmitRelatedResultTypeNote(Args[0]);
5580d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    break;
55811be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor  }
5582429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
5583429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley  case FK_ConversionFromPropertyFailed:
5584429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    // No-op. This error has already been reported.
5585429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    break;
5586429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
5587d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  case FK_TooManyInitsForScalar: {
558899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    SourceRange R;
558999a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor
559099a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0]))
559119311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor      R = SourceRange(InitList->getInit(0)->getLocEnd(),
559299a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor                      InitList->getLocEnd());
5593dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    else
559419311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor      R = SourceRange(Args[0]->getLocEnd(), Args[NumArgs - 1]->getLocEnd());
5595d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
559619311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor    R.setBegin(S.PP.getLocForEndOfToken(R.getBegin()));
559719311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor    if (Kind.isCStyleOrFunctionalCast())
559819311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor      S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg)
559919311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor        << R;
560019311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor    else
560119311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor      S.Diag(Kind.getLocation(), diag::err_excess_initializers)
560219311e70edaa2d7bb0d709344aebea4fbbae2da4Douglas Gregor        << /*scalar=*/2 << R;
5603d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    break;
5604d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  }
5605d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
5606d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  case FK_ReferenceBindingToInitList:
5607d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list)
5608d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      << DestType.getNonReferenceType() << Args[0]->getSourceRange();
5609d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    break;
5610d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor
5611d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor  case FK_InitListBadDestinationType:
5612d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type)
5613d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor      << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange();
5614d87b61f6398bab21176f73818a8d11ca1c3632c8Douglas Gregor    break;
5615dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5616cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl  case FK_ListConstructorOverloadFailed:
561751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  case FK_ConstructorOverloadFailed: {
561851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    SourceRange ArgsRange;
561951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    if (NumArgs)
5620dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ArgsRange = SourceRange(Args[0]->getLocStart(),
562151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor                              Args[NumArgs - 1]->getLocEnd());
5622dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5623cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl    if (Failure == FK_ListConstructorOverloadFailed) {
5624cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl      assert(NumArgs == 1 && "List construction from other than 1 argument.");
5625cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl      InitListExpr *InitList = cast<InitListExpr>(Args[0]);
5626cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl      Args = InitList->getInits();
5627cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl      NumArgs = InitList->getNumInits();
5628cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl    }
5629cf15cef8447e8b3ae08e81ad25ae9eb443038acfSebastian Redl
563051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    // FIXME: Using "DestType" for the entity we're printing is probably
563151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    // bad.
563251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    switch (FailedOverloadResult) {
563351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      case OR_Ambiguous:
563451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init)
563551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor          << DestType << ArgsRange;
5636120d63cd4465230c2cd56508c7cd8e0ad00848e7John McCall        FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates,
563713a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                          llvm::makeArrayRef(Args, NumArgs));
563851c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        break;
5639dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
564051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      case OR_No_Viable_Function:
56419db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        if (Kind.getKind() == InitializationKind::IK_Default &&
56429db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor            (Entity.getKind() == InitializedEntity::EK_Base ||
56439db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor             Entity.getKind() == InitializedEntity::EK_Member) &&
56449db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor            isa<CXXConstructorDecl>(S.CurContext)) {
56459db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor          // This is implicit default initialization of a member or
56469db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor          // base within a constructor. If no viable function was
56479db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor          // found, notify the user that she needs to explicitly
56489db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor          // initialize this base/member.
56499db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor          CXXConstructorDecl *Constructor
56509db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor            = cast<CXXConstructorDecl>(S.CurContext);
56519db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor          if (Entity.getKind() == InitializedEntity::EK_Base) {
56529db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor            S.Diag(Kind.getLocation(), diag::err_missing_default_ctor)
56539db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor              << Constructor->isImplicit()
56549db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor              << S.Context.getTypeDeclType(Constructor->getParent())
56559db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor              << /*base=*/0
56569db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor              << Entity.getType();
56579db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
56589db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor            RecordDecl *BaseDecl
56599db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor              = Entity.getBaseSpecifier()->getType()->getAs<RecordType>()
56609db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                                                                  ->getDecl();
56619db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor            S.Diag(BaseDecl->getLocation(), diag::note_previous_decl)
56629db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor              << S.Context.getTagDeclType(BaseDecl);
56639db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor          } else {
56649db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor            S.Diag(Kind.getLocation(), diag::err_missing_default_ctor)
56659db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor              << Constructor->isImplicit()
56669db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor              << S.Context.getTypeDeclType(Constructor->getParent())
56679db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor              << /*member=*/1
56689db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor              << Entity.getName();
56699db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor            S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl);
56709db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
56719db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor            if (const RecordType *Record
56729db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                                 = Entity.getType()->getAs<RecordType>())
5673dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi              S.Diag(Record->getDecl()->getLocation(),
56749db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                     diag::note_previous_decl)
56759db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor                << S.Context.getTagDeclType(Record->getDecl());
56769db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor          }
56779db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor          break;
56789db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        }
56799db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor
568051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init)
568151c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor          << DestType << ArgsRange;
568213a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles        FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates,
568313a140caba448a66ffcc5ff0d32a87d6e4f4ad3fAhmed Charles                                          llvm::makeArrayRef(Args, NumArgs));
568451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        break;
5685dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
568651c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      case OR_Deleted: {
568751c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        OverloadCandidateSet::iterator Best;
5688120d63cd4465230c2cd56508c7cd8e0ad00848e7John McCall        OverloadingResult Ovl
5689120d63cd4465230c2cd56508c7cd8e0ad00848e7John McCall          = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best);
5690e4e68d45f89ff4899d30cbd196603d09b7fbc150Douglas Gregor        if (Ovl != OR_Deleted) {
5691e4e68d45f89ff4899d30cbd196603d09b7fbc150Douglas Gregor          S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init)
5692e4e68d45f89ff4899d30cbd196603d09b7fbc150Douglas Gregor            << true << DestType << ArgsRange;
569351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor          llvm_unreachable("Inconsistent overload resolution?");
5694e4e68d45f89ff4899d30cbd196603d09b7fbc150Douglas Gregor          break;
569551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        }
5696e4e68d45f89ff4899d30cbd196603d09b7fbc150Douglas Gregor
5697e4e68d45f89ff4899d30cbd196603d09b7fbc150Douglas Gregor        // If this is a defaulted or implicitly-declared function, then
5698e4e68d45f89ff4899d30cbd196603d09b7fbc150Douglas Gregor        // it was implicitly deleted. Make it clear that the deletion was
5699e4e68d45f89ff4899d30cbd196603d09b7fbc150Douglas Gregor        // implicit.
57006c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith        if (S.isImplicitlyDeleted(Best->Function))
5701e4e68d45f89ff4899d30cbd196603d09b7fbc150Douglas Gregor          S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init)
57026c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith            << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function))
5703e4e68d45f89ff4899d30cbd196603d09b7fbc150Douglas Gregor            << DestType << ArgsRange;
57046c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith        else
57056c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith          S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init)
57066c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith            << true << DestType << ArgsRange;
57076c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith
57086c4c36c4ed1007143f5b8655eb68b313a7e12e76Richard Smith        S.NoteDeletedFunction(Best->Function);
570951c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        break;
571051c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      }
5711dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
571251c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor      case OR_Success:
571351c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor        llvm_unreachable("Conversion did not fail!");
571451c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor    }
571551c56d6c8a944c2e49dd714db65a780d9f627e15Douglas Gregor  }
57169fdefb338ead088dd7a5dd4ff3df807586b26d41David Blaikie  break;
5717dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
571899a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor  case FK_DefaultInitOfConst:
57199db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    if (Entity.getKind() == InitializedEntity::EK_Member &&
57209db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        isa<CXXConstructorDecl>(S.CurContext)) {
57219db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      // This is implicit default-initialization of a const member in
57229db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      // a constructor. Complain that it needs to be explicitly
57239db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      // initialized.
57249db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext);
57259db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor)
57269db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        << Constructor->isImplicit()
57279db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        << S.Context.getTypeDeclType(Constructor->getParent())
57289db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        << /*const=*/1
57299db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        << Entity.getName();
57309db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl)
57319db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        << Entity.getName();
57329db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    } else {
57339db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor      S.Diag(Kind.getLocation(), diag::err_default_init_const)
57349db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor        << DestType << (bool)DestType->getAs<RecordType>();
57359db7dbb918ca49f4ee6c181e4917e7b6ec547353Douglas Gregor    }
573699a2e600f9e2e51d3ce10fb6f27191677ac65b2aDouglas Gregor    break;
5737dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
57388713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl  case FK_Incomplete:
573969a30b838c723cb1850de55cfa48a402cfeeb6e0Douglas Gregor    S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType,
57408713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl                          diag::err_init_incomplete_type);
57418713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl    break;
57428713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl
574314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  case FK_ListInitializationFailed: {
574414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    // Run the init list checker again to emit diagnostics.
574514b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    InitListExpr* InitList = cast<InitListExpr>(Args[0]);
574614b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    QualType DestType = Entity.getType();
574714b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    InitListChecker DiagnoseInitList(S, Entity, InitList,
5748c223518ebf72e1709dc8938cd9384161ea5ec0bbSebastian Redl            DestType, /*VerifyOnly=*/false,
5749168319c81b8f4e7addf36ad15ef24919faf23504Sebastian Redl            Kind.getKind() != InitializationKind::IK_DirectList ||
57504e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie              !S.getLangOpts().CPlusPlus0x);
575114b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    assert(DiagnoseInitList.HadError() &&
575214b0c194b356a1204d081765b3e6699687bed97cSebastian Redl           "Inconsistent init list check result.");
575314b0c194b356a1204d081765b3e6699687bed97cSebastian Redl    break;
575414b0c194b356a1204d081765b3e6699687bed97cSebastian Redl  }
57555acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall
57565acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall  case FK_PlaceholderType: {
57575acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall    // FIXME: Already diagnosed!
57585acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall    break;
57595acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall  }
57602b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl
57612b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl  case FK_InitListElementCopyFailure: {
57622b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    // Try to perform all copies again.
57632b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    InitListExpr* InitList = cast<InitListExpr>(Args[0]);
57642b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    unsigned NumInits = InitList->getNumInits();
57652b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    QualType DestType = Entity.getType();
57662b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    QualType E;
57672b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    bool Success = S.isStdInitializerList(DestType, &E);
57682b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    (void)Success;
57692b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    assert(Success && "Where did the std::initializer_list go?");
57702b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary(
57712b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl        S.Context.getConstantArrayType(E,
57722b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl            llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()),
57732b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl                        NumInits),
57742b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl            ArrayType::Normal, 0));
57752b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    InitializedEntity Element = InitializedEntity::InitializeElement(S.Context,
57762b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl        0, HiddenArray);
57772b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    // Show at most 3 errors. Otherwise, you'd get a lot of errors for errors
57782b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    // where the init list type is wrong, e.g.
57792b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    //   std::initializer_list<void*> list = { 1, 2, 3, 4, 5, 6, 7, 8 };
57802b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    // FIXME: Emit a note if we hit the limit?
57812b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    int ErrorCount = 0;
57822b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    for (unsigned i = 0; i < NumInits && ErrorCount < 3; ++i) {
57832b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      Element.setElementIndex(i);
57842b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      ExprResult Init = S.Owned(InitList->getInit(i));
57852b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      if (S.PerformCopyInitialization(Element, Init.get()->getExprLoc(), Init)
57862b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl           .isInvalid())
57872b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl        ++ErrorCount;
57882b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    }
57892b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    break;
57902b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl  }
579170e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl
579270e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl  case FK_ExplicitConstructor: {
579370e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl    S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor)
579470e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl      << Args[0]->getSourceRange();
579570e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl    OverloadCandidateSet::iterator Best;
579670e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl    OverloadingResult Ovl
579770e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl      = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best);
5798e7d0bbfa101d0f0b955c04c417b5e3283497a7edMatt Beaumont-Gay    (void)Ovl;
579970e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl    assert(Ovl == OR_Success && "Inconsistent overload resolution");
580070e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl    CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function);
580170e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl    S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here);
580270e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl    break;
580370e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl  }
580420093b4bf698f292c664676987541d5103b65b15Douglas Gregor  }
5805dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5806a41a8c5972c2632247ae7913cf6ce65d45f7e702Douglas Gregor  PrintInitLocationNote(S, Entity);
580720093b4bf698f292c664676987541d5103b65b15Douglas Gregor  return true;
580820093b4bf698f292c664676987541d5103b65b15Douglas Gregor}
580918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
58105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid InitializationSequence::dump(raw_ostream &OS) const {
5811de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  switch (SequenceKind) {
5812de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  case FailedSequence: {
5813de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    OS << "Failed sequence: ";
5814de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    switch (Failure) {
5815de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_TooManyInitsForReference:
5816de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "too many initializers for reference";
5817de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5818dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5819de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_ArrayNeedsInitList:
5820de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "array requires initializer list";
5821de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5822dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5823de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_ArrayNeedsInitListOrStringLiteral:
5824de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "array requires initializer list or string literal";
5825de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5826dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5827cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    case FK_ArrayTypeMismatch:
5828cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      OS << "array type mismatch";
5829cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      break;
5830cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
5831cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    case FK_NonConstantArrayInit:
5832cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      OS << "non-constant array initializer";
5833cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      break;
5834cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
5835de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_AddressOfOverloadFailed:
5836de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "address of overloaded function failed";
5837de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5838dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5839de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_ReferenceInitOverloadFailed:
5840de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "overload resolution for reference initialization failed";
5841de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5842dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5843de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_NonConstLValueReferenceBindingToTemporary:
5844de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "non-const lvalue reference bound to temporary";
5845de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5846dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5847de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_NonConstLValueReferenceBindingToUnrelated:
5848de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "non-const lvalue reference bound to unrelated type";
5849de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5850dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5851de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_RValueReferenceBindingToLValue:
5852de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "rvalue reference bound to an lvalue";
5853de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5854dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5855de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_ReferenceInitDropsQualifiers:
5856de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "reference initialization drops qualifiers";
5857de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5858dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5859de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_ReferenceInitFailed:
5860de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "reference initialization failed";
5861de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5862dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5863de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_ConversionFailed:
5864de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "conversion failed";
5865de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5866dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5867429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley    case FK_ConversionFromPropertyFailed:
5868429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      OS << "conversion from property failed";
5869429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley      break;
5870429bb276991ff2dbc7c5b438828b9b7737cb15ebJohn Wiegley
5871de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_TooManyInitsForScalar:
5872de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "too many initializers for scalar";
5873de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5874dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5875de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_ReferenceBindingToInitList:
5876de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "referencing binding to initializer list";
5877de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5878dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5879de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_InitListBadDestinationType:
5880de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "initializer list for non-aggregate, non-scalar type";
5881de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5882dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5883de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_UserConversionOverloadFailed:
5884de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "overloading failed for user-defined conversion";
5885de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5886dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5887de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_ConstructorOverloadFailed:
5888de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "constructor overloading failed";
5889de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5890dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5891de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case FK_DefaultInitOfConst:
5892de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "default initialization of a const variable";
5893de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5894dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
589572a43bbf6802c8fcfd04dcb2be8eafcb0b8fe29cDouglas Gregor    case FK_Incomplete:
589672a43bbf6802c8fcfd04dcb2be8eafcb0b8fe29cDouglas Gregor      OS << "initialization of incomplete type";
589772a43bbf6802c8fcfd04dcb2be8eafcb0b8fe29cDouglas Gregor      break;
58988713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl
58998713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl    case FK_ListInitializationFailed:
590014b0c194b356a1204d081765b3e6699687bed97cSebastian Redl      OS << "list initialization checker failure";
59015acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall      break;
59025acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall
590373076431605556fdbf28d287d084a73a24a8b8d4John McCall    case FK_VariableLengthArrayHasInitializer:
590473076431605556fdbf28d287d084a73a24a8b8d4John McCall      OS << "variable length array has an initializer";
590573076431605556fdbf28d287d084a73a24a8b8d4John McCall      break;
590673076431605556fdbf28d287d084a73a24a8b8d4John McCall
59075acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall    case FK_PlaceholderType:
59085acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall      OS << "initializer expression isn't contextually valid";
59095acb0c98b363400f6ade0ae7250f0102224e806bJohn McCall      break;
5910b0c6c33c3479d2cb4c3b9f5a6e58c58d07c731e7Nick Lewycky
5911b0c6c33c3479d2cb4c3b9f5a6e58c58d07c731e7Nick Lewycky    case FK_ListConstructorOverloadFailed:
5912b0c6c33c3479d2cb4c3b9f5a6e58c58d07c731e7Nick Lewycky      OS << "list constructor overloading failed";
5913b0c6c33c3479d2cb4c3b9f5a6e58c58d07c731e7Nick Lewycky      break;
59142b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl
59152b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    case FK_InitListElementCopyFailure:
59162b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      OS << "copy construction of initializer list element failed";
59172b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      break;
591870e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl
591970e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl    case FK_ExplicitConstructor:
592070e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl      OS << "list copy initialization chose explicit constructor";
592170e24fccc8ef4aa8be03a778e9655bfcfa79dd14Sebastian Redl      break;
5922dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    }
5923de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    OS << '\n';
5924de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    return;
5925de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  }
5926dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5927de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  case DependentSequence:
59287491c499e826682e128a400038361ebcbde30eecSebastian Redl    OS << "Dependent sequence\n";
5929de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    return;
5930dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
59317491c499e826682e128a400038361ebcbde30eecSebastian Redl  case NormalSequence:
59327491c499e826682e128a400038361ebcbde30eecSebastian Redl    OS << "Normal sequence: ";
5933de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    break;
5934de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  }
5935dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5936de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) {
5937de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    if (S != step_begin()) {
5938de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << " -> ";
5939de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    }
5940dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5941de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    switch (S->Kind) {
5942de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_ResolveAddressOfOverloadedFunction:
5943de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "resolve address of overloaded function";
5944de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5945dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5946de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_CastDerivedToBaseRValue:
5947de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")";
5948de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5949dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5950906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    case SK_CastDerivedToBaseXValue:
5951906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl      OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")";
5952906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl      break;
5953dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5954de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_CastDerivedToBaseLValue:
5955de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")";
5956de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5957dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5958de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_BindReference:
5959de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "bind reference to lvalue";
5960de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5961dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5962de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_BindReferenceToTemporary:
5963de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "bind reference to a temporary";
5964de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5965dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5966523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor    case SK_ExtraneousCopyToTemporary:
5967523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      OS << "extraneous C++03 copy to temporary";
5968523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor      break;
5969523d46af407f32fc53861e6f068e8076d4fe84a8Douglas Gregor
5970de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_UserConversion:
5971b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer      OS << "user-defined conversion via " << *S->Function.Function;
5972de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5973906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
5974de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_QualificationConversionRValue:
5975de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "qualification conversion (rvalue)";
597613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      break;
5977de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
5978906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl    case SK_QualificationConversionXValue:
5979906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl      OS << "qualification conversion (xvalue)";
598013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      break;
5981906082edf2aea1c6de2926f93a8d7121e49d2a54Sebastian Redl
5982de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_QualificationConversionLValue:
5983de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "qualification conversion (lvalue)";
5984de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5985dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5986de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_ConversionSequence:
5987de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "implicit conversion sequence (";
5988de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      S->ICS->DebugPrint(); // FIXME: use OS
5989de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << ")";
5990de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5991dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5992de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_ListInitialization:
59938713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl      OS << "list aggregate initialization";
59948713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl      break;
59958713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl
59968713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl    case SK_ListConstructorCall:
59978713d4e874f2adc2928ebfb86c845574a14e3b3eSebastian Redl      OS << "list initialization via constructor";
5998de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
5999dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
600013dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    case SK_UnwrapInitList:
600113dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      OS << "unwrap reference initializer list";
600213dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      break;
600313dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
600413dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl    case SK_RewrapInitList:
600513dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      OS << "rewrap reference initializer list";
600613dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl      break;
600713dc8f98f6108dca8aaa9721567ed5a2d9911e0fSebastian Redl
6008de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_ConstructorInitialization:
6009de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "constructor initialization";
6010de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
6011dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6012de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_ZeroInitialization:
6013de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "zero initialization";
6014de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
6015dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6016de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_CAssignment:
6017de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "C assignment";
6018de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
6019dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6020de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    case SK_StringInit:
6021de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      OS << "string initialization";
6022de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor      break;
6023569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor
6024569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor    case SK_ObjCObjectConversion:
6025569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor      OS << "Objective-C object conversion";
6026569c3166874324c24011f8ade6978421f0d39b3cDouglas Gregor      break;
6027cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor
6028cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor    case SK_ArrayInit:
6029cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      OS << "array initialization";
6030cd9ec3b4fb3d042f89aa5b572de7df3ef9ee4a80Douglas Gregor      break;
6031f85e193739c953358c865005855253af4f68a497John McCall
60320f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith    case SK_ParenthesizedArrayInit:
60330f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith      OS << "parenthesized array initialization";
60340f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith      break;
60350f163e964289bc18e9bc1ec37a6a01018ba62640Richard Smith
6036f85e193739c953358c865005855253af4f68a497John McCall    case SK_PassByIndirectCopyRestore:
6037f85e193739c953358c865005855253af4f68a497John McCall      OS << "pass by indirect copy and restore";
6038f85e193739c953358c865005855253af4f68a497John McCall      break;
6039f85e193739c953358c865005855253af4f68a497John McCall
6040f85e193739c953358c865005855253af4f68a497John McCall    case SK_PassByIndirectRestore:
6041f85e193739c953358c865005855253af4f68a497John McCall      OS << "pass by indirect restore";
6042f85e193739c953358c865005855253af4f68a497John McCall      break;
6043f85e193739c953358c865005855253af4f68a497John McCall
6044f85e193739c953358c865005855253af4f68a497John McCall    case SK_ProduceObjCObject:
6045f85e193739c953358c865005855253af4f68a497John McCall      OS << "Objective-C object retension";
6046f85e193739c953358c865005855253af4f68a497John McCall      break;
60472b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl
60482b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl    case SK_StdInitializerList:
60492b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      OS << "std::initializer_list from initializer list";
60502b916b8b55aaf0152ab9ad630c8454bf6373b085Sebastian Redl      break;
6051de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor    }
6052de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  }
6053de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor}
6054de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
6055de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregorvoid InitializationSequence::dump() const {
6056de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor  dump(llvm::errs());
6057de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor}
6058de4b1d86bf48bc2a84bddf6b188f6da53eaea845Douglas Gregor
60594c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smithstatic void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq,
60604c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith                                        QualType EntityType,
60614c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith                                        const Expr *PreInit,
60624c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith                                        const Expr *PostInit) {
60634c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (Seq.step_begin() == Seq.step_end() || PreInit->isValueDependent())
60644c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    return;
60654c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
60664c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // A narrowing conversion can only appear as the final implicit conversion in
60674c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // an initialization sequence.
60684c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  const InitializationSequence::Step &LastStep = Seq.step_end()[-1];
60694c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (LastStep.Kind != InitializationSequence::SK_ConversionSequence)
60704c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    return;
60714c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
60724c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  const ImplicitConversionSequence &ICS = *LastStep.ICS;
60734c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  const StandardConversionSequence *SCS = 0;
60744c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  switch (ICS.getKind()) {
60754c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  case ImplicitConversionSequence::StandardConversion:
60764c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    SCS = &ICS.Standard;
60774c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    break;
60784c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  case ImplicitConversionSequence::UserDefinedConversion:
60794c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    SCS = &ICS.UserDefined.After;
60804c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    break;
60814c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  case ImplicitConversionSequence::AmbiguousConversion:
60824c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  case ImplicitConversionSequence::EllipsisConversion:
60834c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  case ImplicitConversionSequence::BadConversion:
60844c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    return;
60854c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  }
60864c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
60874c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // Determine the type prior to the narrowing conversion. If a conversion
60884c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // operator was used, this may be different from both the type of the entity
60894c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // and of the pre-initialization expression.
60904c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  QualType PreNarrowingType = PreInit->getType();
60914c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (Seq.step_begin() + 1 != Seq.step_end())
60924c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    PreNarrowingType = Seq.step_end()[-2].Type;
60934c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
60944c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion.
60954c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  APValue ConstantValue;
6096f602806965531ee06fd8664b9d7a8912c4af2870Richard Smith  QualType ConstantType;
6097f602806965531ee06fd8664b9d7a8912c4af2870Richard Smith  switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue,
6098f602806965531ee06fd8664b9d7a8912c4af2870Richard Smith                                ConstantType)) {
60994c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  case NK_Not_Narrowing:
61004c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    // No narrowing occurred.
61014c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    return;
61024c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
61034c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  case NK_Type_Narrowing:
61044c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    // This was a floating-to-integer conversion, which is always considered a
61054c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    // narrowing conversion even if the value is a constant and can be
61064c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    // represented exactly as an integer.
61074c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    S.Diag(PostInit->getLocStart(),
61084e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus0x?
6109f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor             diag::warn_init_list_type_narrowing
6110f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor           : S.isSFINAEContext()?
6111f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor             diag::err_init_list_type_narrowing_sfinae
6112f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor           : diag::err_init_list_type_narrowing)
61134c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      << PostInit->getSourceRange()
61144c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      << PreNarrowingType.getLocalUnqualifiedType()
61154c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      << EntityType.getLocalUnqualifiedType();
61164c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    break;
61174c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
61184c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  case NK_Constant_Narrowing:
61194c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    // A constant value was narrowed.
61204c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    S.Diag(PostInit->getLocStart(),
61214e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus0x?
6122f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor             diag::warn_init_list_constant_narrowing
6123f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor           : S.isSFINAEContext()?
6124f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor             diag::err_init_list_constant_narrowing_sfinae
6125f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor           : diag::err_init_list_constant_narrowing)
61264c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      << PostInit->getSourceRange()
6127f602806965531ee06fd8664b9d7a8912c4af2870Richard Smith      << ConstantValue.getAsString(S.getASTContext(), ConstantType)
61289906149972906e340f512a60f72a8676748f24d8Jeffrey Yasskin      << EntityType.getLocalUnqualifiedType();
61294c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    break;
61304c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
61314c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  case NK_Variable_Narrowing:
61324c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    // A variable's value may have been narrowed.
61334c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    S.Diag(PostInit->getLocStart(),
61344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie           S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus0x?
6135f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor             diag::warn_init_list_variable_narrowing
6136f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor           : S.isSFINAEContext()?
6137f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor             diag::err_init_list_variable_narrowing_sfinae
6138f3c82c5f5c0321babf054983c29ad84cc90244f7Douglas Gregor           : diag::err_init_list_variable_narrowing)
61394c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      << PostInit->getSourceRange()
61404c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      << PreNarrowingType.getLocalUnqualifiedType()
61419906149972906e340f512a60f72a8676748f24d8Jeffrey Yasskin      << EntityType.getLocalUnqualifiedType();
61424c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    break;
61434c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  }
6144191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin
6145f7ccbad5d9949e7ddd1cbef43d482553b811e026Dylan Noblesmith  SmallString<128> StaticCast;
6146191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin  llvm::raw_svector_ostream OS(StaticCast);
6147191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin  OS << "static_cast<";
6148191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin  if (const TypedefType *TT = EntityType->getAs<TypedefType>()) {
6149191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin    // It's important to use the typedef's name if there is one so that the
6150191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin    // fixit doesn't break code using types like int64_t.
6151191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin    //
6152191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin    // FIXME: This will break if the typedef requires qualification.  But
6153191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin    // getQualifiedNameAsString() includes non-machine-parsable components.
6154b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer    OS << *TT->getDecl();
6155191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin  } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>())
61564e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    OS << BT->getName(S.getLangOpts());
6157191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin  else {
6158191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin    // Oops, we didn't find the actual type of the variable.  Don't emit a fixit
6159191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin    // with a broken cast.
6160191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin    return;
6161191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin  }
6162191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin  OS << ">(";
61634c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override)
61644c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    << PostInit->getSourceRange()
61654c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str())
6166191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin    << FixItHint::CreateInsertion(
61674c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith      S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")");
6168191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin}
6169191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin
617018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor//===----------------------------------------------------------------------===//
617118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor// Initialization helper functions
617218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor//===----------------------------------------------------------------------===//
61732be7e90b81509204b99b7bbf9753ad17b894a12aSean Huntbool
61742be7e90b81509204b99b7bbf9753ad17b894a12aSean HuntSema::CanPerformCopyInitialization(const InitializedEntity &Entity,
61752be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt                                   ExprResult Init) {
61762be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt  if (Init.isInvalid())
61772be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt    return false;
61782be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt
61792be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt  Expr *InitE = Init.get();
61802be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt  assert(InitE && "No initialization expression");
61812be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt
61823c394c54511b27be0ff6968f159bba3521ab3c3eDouglas Gregor  InitializationKind Kind
61833c394c54511b27be0ff6968f159bba3521ab3c3eDouglas Gregor    = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation());
61842be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt  InitializationSequence Seq(*this, Entity, Kind, &InitE, 1);
6185383616cd2e61131a534afd9364ef53f643e1f834Sebastian Redl  return !Seq.Failed();
61862be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt}
61872be7e90b81509204b99b7bbf9753ad17b894a12aSean Hunt
6188dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiExprResult
618918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas GregorSema::PerformCopyInitialization(const InitializedEntity &Entity,
619018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor                                SourceLocation EqualLoc,
6191191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin                                ExprResult Init,
6192ed878af7914df535b32d64f555fa118413186672Douglas Gregor                                bool TopLevelOfInitList,
6193ed878af7914df535b32d64f555fa118413186672Douglas Gregor                                bool AllowExplicit) {
619418ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  if (Init.isInvalid())
619518ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    return ExprError();
619618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
619715d7d12226f83de24f96f4bf4e27ebba30fef51eJohn McCall  Expr *InitE = Init.get();
619818ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  assert(InitE && "No initialization expression?");
619918ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
620018ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  if (EqualLoc.isInvalid())
620118ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor    EqualLoc = InitE->getLocStart();
620218ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor
620318ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(),
6204ed878af7914df535b32d64f555fa118413186672Douglas Gregor                                                           EqualLoc,
6205ed878af7914df535b32d64f555fa118413186672Douglas Gregor                                                           AllowExplicit);
620618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  InitializationSequence Seq(*this, Entity, Kind, &InitE, 1);
620718ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor  Init.release();
6208191591336f639dad1504e863733fb831645c1644Jeffrey Yasskin
62094c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  ExprResult Result = Seq.Perform(*this, Entity, Kind, MultiExprArg(&InitE, 1));
62104c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
62114c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  if (!Result.isInvalid() && TopLevelOfInitList)
62124c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith    DiagnoseNarrowingInInitList(*this, Seq, Entity.getType(),
62134c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith                                InitE, Result.get());
62144c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith
62154c3fc9b38d3723f73e4ded594cebf38c76f91d93Richard Smith  return Result;
621618ef5e28a9a2677f8b1dce1fb2638d66e0a1621fDouglas Gregor}
6217