1f4a2713aSLionel Sambuc //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements semantic analysis for initializers.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "clang/Sema/Initialization.h"
15f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
16f4a2713aSLionel Sambuc #include "clang/AST/DeclObjC.h"
17f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
18f4a2713aSLionel Sambuc #include "clang/AST/ExprObjC.h"
19f4a2713aSLionel Sambuc #include "clang/AST/TypeLoc.h"
20*0a6a1f1dSLionel Sambuc #include "clang/Basic/TargetInfo.h"
21f4a2713aSLionel Sambuc #include "clang/Sema/Designator.h"
22f4a2713aSLionel Sambuc #include "clang/Sema/Lookup.h"
23f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
24f4a2713aSLionel Sambuc #include "llvm/ADT/APInt.h"
25f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
26f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
27f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
28f4a2713aSLionel Sambuc #include <map>
29f4a2713aSLionel Sambuc using namespace clang;
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
32f4a2713aSLionel Sambuc // Sema Initialization Checking
33f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
34f4a2713aSLionel Sambuc
35f4a2713aSLionel Sambuc /// \brief Check whether T is compatible with a wide character type (wchar_t,
36f4a2713aSLionel Sambuc /// char16_t or char32_t).
IsWideCharCompatible(QualType T,ASTContext & Context)37f4a2713aSLionel Sambuc static bool IsWideCharCompatible(QualType T, ASTContext &Context) {
38f4a2713aSLionel Sambuc if (Context.typesAreCompatible(Context.getWideCharType(), T))
39f4a2713aSLionel Sambuc return true;
40f4a2713aSLionel Sambuc if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) {
41f4a2713aSLionel Sambuc return Context.typesAreCompatible(Context.Char16Ty, T) ||
42f4a2713aSLionel Sambuc Context.typesAreCompatible(Context.Char32Ty, T);
43f4a2713aSLionel Sambuc }
44f4a2713aSLionel Sambuc return false;
45f4a2713aSLionel Sambuc }
46f4a2713aSLionel Sambuc
47f4a2713aSLionel Sambuc enum StringInitFailureKind {
48f4a2713aSLionel Sambuc SIF_None,
49f4a2713aSLionel Sambuc SIF_NarrowStringIntoWideChar,
50f4a2713aSLionel Sambuc SIF_WideStringIntoChar,
51f4a2713aSLionel Sambuc SIF_IncompatWideStringIntoWideChar,
52f4a2713aSLionel Sambuc SIF_Other
53f4a2713aSLionel Sambuc };
54f4a2713aSLionel Sambuc
55f4a2713aSLionel Sambuc /// \brief Check whether the array of type AT can be initialized by the Init
56f4a2713aSLionel Sambuc /// expression by means of string initialization. Returns SIF_None if so,
57f4a2713aSLionel Sambuc /// otherwise returns a StringInitFailureKind that describes why the
58f4a2713aSLionel Sambuc /// initialization would not work.
IsStringInit(Expr * Init,const ArrayType * AT,ASTContext & Context)59f4a2713aSLionel Sambuc static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT,
60f4a2713aSLionel Sambuc ASTContext &Context) {
61f4a2713aSLionel Sambuc if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT))
62f4a2713aSLionel Sambuc return SIF_Other;
63f4a2713aSLionel Sambuc
64f4a2713aSLionel Sambuc // See if this is a string literal or @encode.
65f4a2713aSLionel Sambuc Init = Init->IgnoreParens();
66f4a2713aSLionel Sambuc
67f4a2713aSLionel Sambuc // Handle @encode, which is a narrow string.
68f4a2713aSLionel Sambuc if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType())
69f4a2713aSLionel Sambuc return SIF_None;
70f4a2713aSLionel Sambuc
71f4a2713aSLionel Sambuc // Otherwise we can only handle string literals.
72f4a2713aSLionel Sambuc StringLiteral *SL = dyn_cast<StringLiteral>(Init);
73*0a6a1f1dSLionel Sambuc if (!SL)
74f4a2713aSLionel Sambuc return SIF_Other;
75f4a2713aSLionel Sambuc
76f4a2713aSLionel Sambuc const QualType ElemTy =
77f4a2713aSLionel Sambuc Context.getCanonicalType(AT->getElementType()).getUnqualifiedType();
78f4a2713aSLionel Sambuc
79f4a2713aSLionel Sambuc switch (SL->getKind()) {
80f4a2713aSLionel Sambuc case StringLiteral::Ascii:
81f4a2713aSLionel Sambuc case StringLiteral::UTF8:
82f4a2713aSLionel Sambuc // char array can be initialized with a narrow string.
83f4a2713aSLionel Sambuc // Only allow char x[] = "foo"; not char x[] = L"foo";
84f4a2713aSLionel Sambuc if (ElemTy->isCharType())
85f4a2713aSLionel Sambuc return SIF_None;
86f4a2713aSLionel Sambuc if (IsWideCharCompatible(ElemTy, Context))
87f4a2713aSLionel Sambuc return SIF_NarrowStringIntoWideChar;
88f4a2713aSLionel Sambuc return SIF_Other;
89f4a2713aSLionel Sambuc // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15:
90f4a2713aSLionel Sambuc // "An array with element type compatible with a qualified or unqualified
91f4a2713aSLionel Sambuc // version of wchar_t, char16_t, or char32_t may be initialized by a wide
92f4a2713aSLionel Sambuc // string literal with the corresponding encoding prefix (L, u, or U,
93f4a2713aSLionel Sambuc // respectively), optionally enclosed in braces.
94f4a2713aSLionel Sambuc case StringLiteral::UTF16:
95f4a2713aSLionel Sambuc if (Context.typesAreCompatible(Context.Char16Ty, ElemTy))
96f4a2713aSLionel Sambuc return SIF_None;
97f4a2713aSLionel Sambuc if (ElemTy->isCharType())
98f4a2713aSLionel Sambuc return SIF_WideStringIntoChar;
99f4a2713aSLionel Sambuc if (IsWideCharCompatible(ElemTy, Context))
100f4a2713aSLionel Sambuc return SIF_IncompatWideStringIntoWideChar;
101f4a2713aSLionel Sambuc return SIF_Other;
102f4a2713aSLionel Sambuc case StringLiteral::UTF32:
103f4a2713aSLionel Sambuc if (Context.typesAreCompatible(Context.Char32Ty, ElemTy))
104f4a2713aSLionel Sambuc return SIF_None;
105f4a2713aSLionel Sambuc if (ElemTy->isCharType())
106f4a2713aSLionel Sambuc return SIF_WideStringIntoChar;
107f4a2713aSLionel Sambuc if (IsWideCharCompatible(ElemTy, Context))
108f4a2713aSLionel Sambuc return SIF_IncompatWideStringIntoWideChar;
109f4a2713aSLionel Sambuc return SIF_Other;
110f4a2713aSLionel Sambuc case StringLiteral::Wide:
111f4a2713aSLionel Sambuc if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy))
112f4a2713aSLionel Sambuc return SIF_None;
113f4a2713aSLionel Sambuc if (ElemTy->isCharType())
114f4a2713aSLionel Sambuc return SIF_WideStringIntoChar;
115f4a2713aSLionel Sambuc if (IsWideCharCompatible(ElemTy, Context))
116f4a2713aSLionel Sambuc return SIF_IncompatWideStringIntoWideChar;
117f4a2713aSLionel Sambuc return SIF_Other;
118f4a2713aSLionel Sambuc }
119f4a2713aSLionel Sambuc
120f4a2713aSLionel Sambuc llvm_unreachable("missed a StringLiteral kind?");
121f4a2713aSLionel Sambuc }
122f4a2713aSLionel Sambuc
IsStringInit(Expr * init,QualType declType,ASTContext & Context)123f4a2713aSLionel Sambuc static StringInitFailureKind IsStringInit(Expr *init, QualType declType,
124f4a2713aSLionel Sambuc ASTContext &Context) {
125f4a2713aSLionel Sambuc const ArrayType *arrayType = Context.getAsArrayType(declType);
126f4a2713aSLionel Sambuc if (!arrayType)
127f4a2713aSLionel Sambuc return SIF_Other;
128f4a2713aSLionel Sambuc return IsStringInit(init, arrayType, Context);
129f4a2713aSLionel Sambuc }
130f4a2713aSLionel Sambuc
131f4a2713aSLionel Sambuc /// Update the type of a string literal, including any surrounding parentheses,
132f4a2713aSLionel Sambuc /// to match the type of the object which it is initializing.
updateStringLiteralType(Expr * E,QualType Ty)133f4a2713aSLionel Sambuc static void updateStringLiteralType(Expr *E, QualType Ty) {
134f4a2713aSLionel Sambuc while (true) {
135f4a2713aSLionel Sambuc E->setType(Ty);
136f4a2713aSLionel Sambuc if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E))
137f4a2713aSLionel Sambuc break;
138f4a2713aSLionel Sambuc else if (ParenExpr *PE = dyn_cast<ParenExpr>(E))
139f4a2713aSLionel Sambuc E = PE->getSubExpr();
140f4a2713aSLionel Sambuc else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E))
141f4a2713aSLionel Sambuc E = UO->getSubExpr();
142f4a2713aSLionel Sambuc else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E))
143f4a2713aSLionel Sambuc E = GSE->getResultExpr();
144f4a2713aSLionel Sambuc else
145f4a2713aSLionel Sambuc llvm_unreachable("unexpected expr in string literal init");
146f4a2713aSLionel Sambuc }
147f4a2713aSLionel Sambuc }
148f4a2713aSLionel Sambuc
CheckStringInit(Expr * Str,QualType & DeclT,const ArrayType * AT,Sema & S)149f4a2713aSLionel Sambuc static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT,
150f4a2713aSLionel Sambuc Sema &S) {
151f4a2713aSLionel Sambuc // Get the length of the string as parsed.
152f4a2713aSLionel Sambuc uint64_t StrLength =
153f4a2713aSLionel Sambuc cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue();
154f4a2713aSLionel Sambuc
155f4a2713aSLionel Sambuc
156f4a2713aSLionel Sambuc if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) {
157f4a2713aSLionel Sambuc // C99 6.7.8p14. We have an array of character type with unknown size
158f4a2713aSLionel Sambuc // being initialized to a string literal.
159f4a2713aSLionel Sambuc llvm::APInt ConstVal(32, StrLength);
160f4a2713aSLionel Sambuc // Return a new array type (C99 6.7.8p22).
161f4a2713aSLionel Sambuc DeclT = S.Context.getConstantArrayType(IAT->getElementType(),
162f4a2713aSLionel Sambuc ConstVal,
163f4a2713aSLionel Sambuc ArrayType::Normal, 0);
164f4a2713aSLionel Sambuc updateStringLiteralType(Str, DeclT);
165f4a2713aSLionel Sambuc return;
166f4a2713aSLionel Sambuc }
167f4a2713aSLionel Sambuc
168f4a2713aSLionel Sambuc const ConstantArrayType *CAT = cast<ConstantArrayType>(AT);
169f4a2713aSLionel Sambuc
170f4a2713aSLionel Sambuc // We have an array of character type with known size. However,
171f4a2713aSLionel Sambuc // the size may be smaller or larger than the string we are initializing.
172f4a2713aSLionel Sambuc // FIXME: Avoid truncation for 64-bit length strings.
173f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus) {
174f4a2713aSLionel Sambuc if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) {
175f4a2713aSLionel Sambuc // For Pascal strings it's OK to strip off the terminating null character,
176f4a2713aSLionel Sambuc // so the example below is valid:
177f4a2713aSLionel Sambuc //
178f4a2713aSLionel Sambuc // unsigned char a[2] = "\pa";
179f4a2713aSLionel Sambuc if (SL->isPascal())
180f4a2713aSLionel Sambuc StrLength--;
181f4a2713aSLionel Sambuc }
182f4a2713aSLionel Sambuc
183f4a2713aSLionel Sambuc // [dcl.init.string]p2
184f4a2713aSLionel Sambuc if (StrLength > CAT->getSize().getZExtValue())
185f4a2713aSLionel Sambuc S.Diag(Str->getLocStart(),
186f4a2713aSLionel Sambuc diag::err_initializer_string_for_char_array_too_long)
187f4a2713aSLionel Sambuc << Str->getSourceRange();
188f4a2713aSLionel Sambuc } else {
189f4a2713aSLionel Sambuc // C99 6.7.8p14.
190f4a2713aSLionel Sambuc if (StrLength-1 > CAT->getSize().getZExtValue())
191f4a2713aSLionel Sambuc S.Diag(Str->getLocStart(),
192*0a6a1f1dSLionel Sambuc diag::ext_initializer_string_for_char_array_too_long)
193f4a2713aSLionel Sambuc << Str->getSourceRange();
194f4a2713aSLionel Sambuc }
195f4a2713aSLionel Sambuc
196f4a2713aSLionel Sambuc // Set the type to the actual size that we are initializing. If we have
197f4a2713aSLionel Sambuc // something like:
198f4a2713aSLionel Sambuc // char x[1] = "foo";
199f4a2713aSLionel Sambuc // then this will set the string literal's type to char[1].
200f4a2713aSLionel Sambuc updateStringLiteralType(Str, DeclT);
201f4a2713aSLionel Sambuc }
202f4a2713aSLionel Sambuc
203f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
204f4a2713aSLionel Sambuc // Semantic checking for initializer lists.
205f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
206f4a2713aSLionel Sambuc
207f4a2713aSLionel Sambuc /// @brief Semantic checking for initializer lists.
208f4a2713aSLionel Sambuc ///
209f4a2713aSLionel Sambuc /// The InitListChecker class contains a set of routines that each
210f4a2713aSLionel Sambuc /// handle the initialization of a certain kind of entity, e.g.,
211f4a2713aSLionel Sambuc /// arrays, vectors, struct/union types, scalars, etc. The
212f4a2713aSLionel Sambuc /// InitListChecker itself performs a recursive walk of the subobject
213f4a2713aSLionel Sambuc /// structure of the type to be initialized, while stepping through
214f4a2713aSLionel Sambuc /// the initializer list one element at a time. The IList and Index
215f4a2713aSLionel Sambuc /// parameters to each of the Check* routines contain the active
216f4a2713aSLionel Sambuc /// (syntactic) initializer list and the index into that initializer
217f4a2713aSLionel Sambuc /// list that represents the current initializer. Each routine is
218f4a2713aSLionel Sambuc /// responsible for moving that Index forward as it consumes elements.
219f4a2713aSLionel Sambuc ///
220f4a2713aSLionel Sambuc /// Each Check* routine also has a StructuredList/StructuredIndex
221f4a2713aSLionel Sambuc /// arguments, which contains the current "structured" (semantic)
222f4a2713aSLionel Sambuc /// initializer list and the index into that initializer list where we
223f4a2713aSLionel Sambuc /// are copying initializers as we map them over to the semantic
224f4a2713aSLionel Sambuc /// list. Once we have completed our recursive walk of the subobject
225f4a2713aSLionel Sambuc /// structure, we will have constructed a full semantic initializer
226f4a2713aSLionel Sambuc /// list.
227f4a2713aSLionel Sambuc ///
228f4a2713aSLionel Sambuc /// C99 designators cause changes in the initializer list traversal,
229f4a2713aSLionel Sambuc /// because they make the initialization "jump" into a specific
230f4a2713aSLionel Sambuc /// subobject and then continue the initialization from that
231f4a2713aSLionel Sambuc /// point. CheckDesignatedInitializer() recursively steps into the
232f4a2713aSLionel Sambuc /// designated subobject and manages backing out the recursion to
233f4a2713aSLionel Sambuc /// initialize the subobjects after the one designated.
234f4a2713aSLionel Sambuc namespace {
235f4a2713aSLionel Sambuc class InitListChecker {
236f4a2713aSLionel Sambuc Sema &SemaRef;
237f4a2713aSLionel Sambuc bool hadError;
238f4a2713aSLionel Sambuc bool VerifyOnly; // no diagnostics, no structure building
239f4a2713aSLionel Sambuc llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic;
240f4a2713aSLionel Sambuc InitListExpr *FullyStructuredList;
241f4a2713aSLionel Sambuc
242f4a2713aSLionel Sambuc void CheckImplicitInitList(const InitializedEntity &Entity,
243f4a2713aSLionel Sambuc InitListExpr *ParentIList, QualType T,
244f4a2713aSLionel Sambuc unsigned &Index, InitListExpr *StructuredList,
245f4a2713aSLionel Sambuc unsigned &StructuredIndex);
246f4a2713aSLionel Sambuc void CheckExplicitInitList(const InitializedEntity &Entity,
247f4a2713aSLionel Sambuc InitListExpr *IList, QualType &T,
248f4a2713aSLionel Sambuc InitListExpr *StructuredList,
249f4a2713aSLionel Sambuc bool TopLevelObject = false);
250f4a2713aSLionel Sambuc void CheckListElementTypes(const InitializedEntity &Entity,
251f4a2713aSLionel Sambuc InitListExpr *IList, QualType &DeclType,
252f4a2713aSLionel Sambuc bool SubobjectIsDesignatorContext,
253f4a2713aSLionel Sambuc unsigned &Index,
254f4a2713aSLionel Sambuc InitListExpr *StructuredList,
255f4a2713aSLionel Sambuc unsigned &StructuredIndex,
256f4a2713aSLionel Sambuc bool TopLevelObject = false);
257f4a2713aSLionel Sambuc void CheckSubElementType(const InitializedEntity &Entity,
258f4a2713aSLionel Sambuc InitListExpr *IList, QualType ElemType,
259f4a2713aSLionel Sambuc unsigned &Index,
260f4a2713aSLionel Sambuc InitListExpr *StructuredList,
261f4a2713aSLionel Sambuc unsigned &StructuredIndex);
262f4a2713aSLionel Sambuc void CheckComplexType(const InitializedEntity &Entity,
263f4a2713aSLionel Sambuc InitListExpr *IList, QualType DeclType,
264f4a2713aSLionel Sambuc unsigned &Index,
265f4a2713aSLionel Sambuc InitListExpr *StructuredList,
266f4a2713aSLionel Sambuc unsigned &StructuredIndex);
267f4a2713aSLionel Sambuc void CheckScalarType(const InitializedEntity &Entity,
268f4a2713aSLionel Sambuc InitListExpr *IList, QualType DeclType,
269f4a2713aSLionel Sambuc unsigned &Index,
270f4a2713aSLionel Sambuc InitListExpr *StructuredList,
271f4a2713aSLionel Sambuc unsigned &StructuredIndex);
272f4a2713aSLionel Sambuc void CheckReferenceType(const InitializedEntity &Entity,
273f4a2713aSLionel Sambuc InitListExpr *IList, QualType DeclType,
274f4a2713aSLionel Sambuc unsigned &Index,
275f4a2713aSLionel Sambuc InitListExpr *StructuredList,
276f4a2713aSLionel Sambuc unsigned &StructuredIndex);
277f4a2713aSLionel Sambuc void CheckVectorType(const InitializedEntity &Entity,
278f4a2713aSLionel Sambuc InitListExpr *IList, QualType DeclType, unsigned &Index,
279f4a2713aSLionel Sambuc InitListExpr *StructuredList,
280f4a2713aSLionel Sambuc unsigned &StructuredIndex);
281f4a2713aSLionel Sambuc void CheckStructUnionTypes(const InitializedEntity &Entity,
282f4a2713aSLionel Sambuc InitListExpr *IList, QualType DeclType,
283f4a2713aSLionel Sambuc RecordDecl::field_iterator Field,
284f4a2713aSLionel Sambuc bool SubobjectIsDesignatorContext, unsigned &Index,
285f4a2713aSLionel Sambuc InitListExpr *StructuredList,
286f4a2713aSLionel Sambuc unsigned &StructuredIndex,
287f4a2713aSLionel Sambuc bool TopLevelObject = false);
288f4a2713aSLionel Sambuc void CheckArrayType(const InitializedEntity &Entity,
289f4a2713aSLionel Sambuc InitListExpr *IList, QualType &DeclType,
290f4a2713aSLionel Sambuc llvm::APSInt elementIndex,
291f4a2713aSLionel Sambuc bool SubobjectIsDesignatorContext, unsigned &Index,
292f4a2713aSLionel Sambuc InitListExpr *StructuredList,
293f4a2713aSLionel Sambuc unsigned &StructuredIndex);
294f4a2713aSLionel Sambuc bool CheckDesignatedInitializer(const InitializedEntity &Entity,
295f4a2713aSLionel Sambuc InitListExpr *IList, DesignatedInitExpr *DIE,
296f4a2713aSLionel Sambuc unsigned DesigIdx,
297f4a2713aSLionel Sambuc QualType &CurrentObjectType,
298f4a2713aSLionel Sambuc RecordDecl::field_iterator *NextField,
299f4a2713aSLionel Sambuc llvm::APSInt *NextElementIndex,
300f4a2713aSLionel Sambuc unsigned &Index,
301f4a2713aSLionel Sambuc InitListExpr *StructuredList,
302f4a2713aSLionel Sambuc unsigned &StructuredIndex,
303f4a2713aSLionel Sambuc bool FinishSubobjectInit,
304f4a2713aSLionel Sambuc bool TopLevelObject);
305f4a2713aSLionel Sambuc InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
306f4a2713aSLionel Sambuc QualType CurrentObjectType,
307f4a2713aSLionel Sambuc InitListExpr *StructuredList,
308f4a2713aSLionel Sambuc unsigned StructuredIndex,
309f4a2713aSLionel Sambuc SourceRange InitRange);
310f4a2713aSLionel Sambuc void UpdateStructuredListElement(InitListExpr *StructuredList,
311f4a2713aSLionel Sambuc unsigned &StructuredIndex,
312f4a2713aSLionel Sambuc Expr *expr);
313f4a2713aSLionel Sambuc int numArrayElements(QualType DeclType);
314f4a2713aSLionel Sambuc int numStructUnionElements(QualType DeclType);
315f4a2713aSLionel Sambuc
316*0a6a1f1dSLionel Sambuc static ExprResult PerformEmptyInit(Sema &SemaRef,
317*0a6a1f1dSLionel Sambuc SourceLocation Loc,
318*0a6a1f1dSLionel Sambuc const InitializedEntity &Entity,
319*0a6a1f1dSLionel Sambuc bool VerifyOnly);
320*0a6a1f1dSLionel Sambuc void FillInEmptyInitForField(unsigned Init, FieldDecl *Field,
321f4a2713aSLionel Sambuc const InitializedEntity &ParentEntity,
322f4a2713aSLionel Sambuc InitListExpr *ILE, bool &RequiresSecondPass);
323*0a6a1f1dSLionel Sambuc void FillInEmptyInitializations(const InitializedEntity &Entity,
324f4a2713aSLionel Sambuc InitListExpr *ILE, bool &RequiresSecondPass);
325f4a2713aSLionel Sambuc bool CheckFlexibleArrayInit(const InitializedEntity &Entity,
326f4a2713aSLionel Sambuc Expr *InitExpr, FieldDecl *Field,
327f4a2713aSLionel Sambuc bool TopLevelObject);
328*0a6a1f1dSLionel Sambuc void CheckEmptyInitializable(const InitializedEntity &Entity,
329*0a6a1f1dSLionel Sambuc SourceLocation Loc);
330f4a2713aSLionel Sambuc
331f4a2713aSLionel Sambuc public:
332f4a2713aSLionel Sambuc InitListChecker(Sema &S, const InitializedEntity &Entity,
333f4a2713aSLionel Sambuc InitListExpr *IL, QualType &T, bool VerifyOnly);
HadError()334f4a2713aSLionel Sambuc bool HadError() { return hadError; }
335f4a2713aSLionel Sambuc
336f4a2713aSLionel Sambuc // @brief Retrieves the fully-structured initializer list used for
337f4a2713aSLionel Sambuc // semantic analysis and code generation.
getFullyStructuredList() const338f4a2713aSLionel Sambuc InitListExpr *getFullyStructuredList() const { return FullyStructuredList; }
339f4a2713aSLionel Sambuc };
340f4a2713aSLionel Sambuc } // end anonymous namespace
341f4a2713aSLionel Sambuc
PerformEmptyInit(Sema & SemaRef,SourceLocation Loc,const InitializedEntity & Entity,bool VerifyOnly)342*0a6a1f1dSLionel Sambuc ExprResult InitListChecker::PerformEmptyInit(Sema &SemaRef,
343*0a6a1f1dSLionel Sambuc SourceLocation Loc,
344*0a6a1f1dSLionel Sambuc const InitializedEntity &Entity,
345*0a6a1f1dSLionel Sambuc bool VerifyOnly) {
346f4a2713aSLionel Sambuc InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc,
347f4a2713aSLionel Sambuc true);
348*0a6a1f1dSLionel Sambuc MultiExprArg SubInit;
349*0a6a1f1dSLionel Sambuc Expr *InitExpr;
350*0a6a1f1dSLionel Sambuc InitListExpr DummyInitList(SemaRef.Context, Loc, None, Loc);
351*0a6a1f1dSLionel Sambuc
352*0a6a1f1dSLionel Sambuc // C++ [dcl.init.aggr]p7:
353*0a6a1f1dSLionel Sambuc // If there are fewer initializer-clauses in the list than there are
354*0a6a1f1dSLionel Sambuc // members in the aggregate, then each member not explicitly initialized
355*0a6a1f1dSLionel Sambuc // ...
356*0a6a1f1dSLionel Sambuc bool EmptyInitList = SemaRef.getLangOpts().CPlusPlus11 &&
357*0a6a1f1dSLionel Sambuc Entity.getType()->getBaseElementTypeUnsafe()->isRecordType();
358*0a6a1f1dSLionel Sambuc if (EmptyInitList) {
359*0a6a1f1dSLionel Sambuc // C++1y / DR1070:
360*0a6a1f1dSLionel Sambuc // shall be initialized [...] from an empty initializer list.
361*0a6a1f1dSLionel Sambuc //
362*0a6a1f1dSLionel Sambuc // We apply the resolution of this DR to C++11 but not C++98, since C++98
363*0a6a1f1dSLionel Sambuc // does not have useful semantics for initialization from an init list.
364*0a6a1f1dSLionel Sambuc // We treat this as copy-initialization, because aggregate initialization
365*0a6a1f1dSLionel Sambuc // always performs copy-initialization on its elements.
366*0a6a1f1dSLionel Sambuc //
367*0a6a1f1dSLionel Sambuc // Only do this if we're initializing a class type, to avoid filling in
368*0a6a1f1dSLionel Sambuc // the initializer list where possible.
369*0a6a1f1dSLionel Sambuc InitExpr = VerifyOnly ? &DummyInitList : new (SemaRef.Context)
370*0a6a1f1dSLionel Sambuc InitListExpr(SemaRef.Context, Loc, None, Loc);
371*0a6a1f1dSLionel Sambuc InitExpr->setType(SemaRef.Context.VoidTy);
372*0a6a1f1dSLionel Sambuc SubInit = InitExpr;
373*0a6a1f1dSLionel Sambuc Kind = InitializationKind::CreateCopy(Loc, Loc);
374*0a6a1f1dSLionel Sambuc } else {
375*0a6a1f1dSLionel Sambuc // C++03:
376*0a6a1f1dSLionel Sambuc // shall be value-initialized.
377*0a6a1f1dSLionel Sambuc }
378*0a6a1f1dSLionel Sambuc
379*0a6a1f1dSLionel Sambuc InitializationSequence InitSeq(SemaRef, Entity, Kind, SubInit);
380*0a6a1f1dSLionel Sambuc // libstdc++4.6 marks the vector default constructor as explicit in
381*0a6a1f1dSLionel Sambuc // _GLIBCXX_DEBUG mode, so recover using the C++03 logic in that case.
382*0a6a1f1dSLionel Sambuc // stlport does so too. Look for std::__debug for libstdc++, and for
383*0a6a1f1dSLionel Sambuc // std:: for stlport. This is effectively a compiler-side implementation of
384*0a6a1f1dSLionel Sambuc // LWG2193.
385*0a6a1f1dSLionel Sambuc if (!InitSeq && EmptyInitList && InitSeq.getFailureKind() ==
386*0a6a1f1dSLionel Sambuc InitializationSequence::FK_ExplicitConstructor) {
387*0a6a1f1dSLionel Sambuc OverloadCandidateSet::iterator Best;
388*0a6a1f1dSLionel Sambuc OverloadingResult O =
389*0a6a1f1dSLionel Sambuc InitSeq.getFailedCandidateSet()
390*0a6a1f1dSLionel Sambuc .BestViableFunction(SemaRef, Kind.getLocation(), Best);
391*0a6a1f1dSLionel Sambuc (void)O;
392*0a6a1f1dSLionel Sambuc assert(O == OR_Success && "Inconsistent overload resolution");
393*0a6a1f1dSLionel Sambuc CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function);
394*0a6a1f1dSLionel Sambuc CXXRecordDecl *R = CtorDecl->getParent();
395*0a6a1f1dSLionel Sambuc
396*0a6a1f1dSLionel Sambuc if (CtorDecl->getMinRequiredArguments() == 0 &&
397*0a6a1f1dSLionel Sambuc CtorDecl->isExplicit() && R->getDeclName() &&
398*0a6a1f1dSLionel Sambuc SemaRef.SourceMgr.isInSystemHeader(CtorDecl->getLocation())) {
399*0a6a1f1dSLionel Sambuc
400*0a6a1f1dSLionel Sambuc
401*0a6a1f1dSLionel Sambuc bool IsInStd = false;
402*0a6a1f1dSLionel Sambuc for (NamespaceDecl *ND = dyn_cast<NamespaceDecl>(R->getDeclContext());
403*0a6a1f1dSLionel Sambuc ND && !IsInStd; ND = dyn_cast<NamespaceDecl>(ND->getParent())) {
404*0a6a1f1dSLionel Sambuc if (SemaRef.getStdNamespace()->InEnclosingNamespaceSetOf(ND))
405*0a6a1f1dSLionel Sambuc IsInStd = true;
406*0a6a1f1dSLionel Sambuc }
407*0a6a1f1dSLionel Sambuc
408*0a6a1f1dSLionel Sambuc if (IsInStd && llvm::StringSwitch<bool>(R->getName())
409*0a6a1f1dSLionel Sambuc .Cases("basic_string", "deque", "forward_list", true)
410*0a6a1f1dSLionel Sambuc .Cases("list", "map", "multimap", "multiset", true)
411*0a6a1f1dSLionel Sambuc .Cases("priority_queue", "queue", "set", "stack", true)
412*0a6a1f1dSLionel Sambuc .Cases("unordered_map", "unordered_set", "vector", true)
413*0a6a1f1dSLionel Sambuc .Default(false)) {
414*0a6a1f1dSLionel Sambuc InitSeq.InitializeFrom(
415*0a6a1f1dSLionel Sambuc SemaRef, Entity,
416*0a6a1f1dSLionel Sambuc InitializationKind::CreateValue(Loc, Loc, Loc, true),
417*0a6a1f1dSLionel Sambuc MultiExprArg(), /*TopLevelOfInitList=*/false);
418*0a6a1f1dSLionel Sambuc // Emit a warning for this. System header warnings aren't shown
419*0a6a1f1dSLionel Sambuc // by default, but people working on system headers should see it.
420*0a6a1f1dSLionel Sambuc if (!VerifyOnly) {
421*0a6a1f1dSLionel Sambuc SemaRef.Diag(CtorDecl->getLocation(),
422*0a6a1f1dSLionel Sambuc diag::warn_invalid_initializer_from_system_header);
423*0a6a1f1dSLionel Sambuc SemaRef.Diag(Entity.getDecl()->getLocation(),
424*0a6a1f1dSLionel Sambuc diag::note_used_in_initialization_here);
425*0a6a1f1dSLionel Sambuc }
426*0a6a1f1dSLionel Sambuc }
427*0a6a1f1dSLionel Sambuc }
428*0a6a1f1dSLionel Sambuc }
429*0a6a1f1dSLionel Sambuc if (!InitSeq) {
430*0a6a1f1dSLionel Sambuc if (!VerifyOnly) {
431*0a6a1f1dSLionel Sambuc InitSeq.Diagnose(SemaRef, Entity, Kind, SubInit);
432*0a6a1f1dSLionel Sambuc if (Entity.getKind() == InitializedEntity::EK_Member)
433*0a6a1f1dSLionel Sambuc SemaRef.Diag(Entity.getDecl()->getLocation(),
434*0a6a1f1dSLionel Sambuc diag::note_in_omitted_aggregate_initializer)
435*0a6a1f1dSLionel Sambuc << /*field*/1 << Entity.getDecl();
436*0a6a1f1dSLionel Sambuc else if (Entity.getKind() == InitializedEntity::EK_ArrayElement)
437*0a6a1f1dSLionel Sambuc SemaRef.Diag(Loc, diag::note_in_omitted_aggregate_initializer)
438*0a6a1f1dSLionel Sambuc << /*array element*/0 << Entity.getElementIndex();
439*0a6a1f1dSLionel Sambuc }
440*0a6a1f1dSLionel Sambuc return ExprError();
441*0a6a1f1dSLionel Sambuc }
442*0a6a1f1dSLionel Sambuc
443*0a6a1f1dSLionel Sambuc return VerifyOnly ? ExprResult(static_cast<Expr *>(nullptr))
444*0a6a1f1dSLionel Sambuc : InitSeq.Perform(SemaRef, Entity, Kind, SubInit);
445*0a6a1f1dSLionel Sambuc }
446*0a6a1f1dSLionel Sambuc
CheckEmptyInitializable(const InitializedEntity & Entity,SourceLocation Loc)447*0a6a1f1dSLionel Sambuc void InitListChecker::CheckEmptyInitializable(const InitializedEntity &Entity,
448*0a6a1f1dSLionel Sambuc SourceLocation Loc) {
449*0a6a1f1dSLionel Sambuc assert(VerifyOnly &&
450*0a6a1f1dSLionel Sambuc "CheckEmptyInitializable is only inteded for verification mode.");
451*0a6a1f1dSLionel Sambuc if (PerformEmptyInit(SemaRef, Loc, Entity, /*VerifyOnly*/true).isInvalid())
452f4a2713aSLionel Sambuc hadError = true;
453f4a2713aSLionel Sambuc }
454f4a2713aSLionel Sambuc
FillInEmptyInitForField(unsigned Init,FieldDecl * Field,const InitializedEntity & ParentEntity,InitListExpr * ILE,bool & RequiresSecondPass)455*0a6a1f1dSLionel Sambuc void InitListChecker::FillInEmptyInitForField(unsigned Init, FieldDecl *Field,
456f4a2713aSLionel Sambuc const InitializedEntity &ParentEntity,
457f4a2713aSLionel Sambuc InitListExpr *ILE,
458f4a2713aSLionel Sambuc bool &RequiresSecondPass) {
459*0a6a1f1dSLionel Sambuc SourceLocation Loc = ILE->getLocEnd();
460f4a2713aSLionel Sambuc unsigned NumInits = ILE->getNumInits();
461f4a2713aSLionel Sambuc InitializedEntity MemberEntity
462f4a2713aSLionel Sambuc = InitializedEntity::InitializeMember(Field, &ParentEntity);
463f4a2713aSLionel Sambuc if (Init >= NumInits || !ILE->getInit(Init)) {
464*0a6a1f1dSLionel Sambuc // C++1y [dcl.init.aggr]p7:
465*0a6a1f1dSLionel Sambuc // If there are fewer initializer-clauses in the list than there are
466*0a6a1f1dSLionel Sambuc // members in the aggregate, then each member not explicitly initialized
467*0a6a1f1dSLionel Sambuc // shall be initialized from its brace-or-equal-initializer [...]
468f4a2713aSLionel Sambuc if (Field->hasInClassInitializer()) {
469*0a6a1f1dSLionel Sambuc ExprResult DIE = SemaRef.BuildCXXDefaultInitExpr(Loc, Field);
470*0a6a1f1dSLionel Sambuc if (DIE.isInvalid()) {
471*0a6a1f1dSLionel Sambuc hadError = true;
472*0a6a1f1dSLionel Sambuc return;
473*0a6a1f1dSLionel Sambuc }
474f4a2713aSLionel Sambuc if (Init < NumInits)
475*0a6a1f1dSLionel Sambuc ILE->setInit(Init, DIE.get());
476f4a2713aSLionel Sambuc else {
477*0a6a1f1dSLionel Sambuc ILE->updateInit(SemaRef.Context, Init, DIE.get());
478f4a2713aSLionel Sambuc RequiresSecondPass = true;
479f4a2713aSLionel Sambuc }
480f4a2713aSLionel Sambuc return;
481f4a2713aSLionel Sambuc }
482f4a2713aSLionel Sambuc
483f4a2713aSLionel Sambuc if (Field->getType()->isReferenceType()) {
484f4a2713aSLionel Sambuc // C++ [dcl.init.aggr]p9:
485f4a2713aSLionel Sambuc // If an incomplete or empty initializer-list leaves a
486f4a2713aSLionel Sambuc // member of reference type uninitialized, the program is
487f4a2713aSLionel Sambuc // ill-formed.
488f4a2713aSLionel Sambuc SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized)
489f4a2713aSLionel Sambuc << Field->getType()
490f4a2713aSLionel Sambuc << ILE->getSyntacticForm()->getSourceRange();
491f4a2713aSLionel Sambuc SemaRef.Diag(Field->getLocation(),
492f4a2713aSLionel Sambuc diag::note_uninit_reference_member);
493f4a2713aSLionel Sambuc hadError = true;
494f4a2713aSLionel Sambuc return;
495f4a2713aSLionel Sambuc }
496f4a2713aSLionel Sambuc
497*0a6a1f1dSLionel Sambuc ExprResult MemberInit = PerformEmptyInit(SemaRef, Loc, MemberEntity,
498*0a6a1f1dSLionel Sambuc /*VerifyOnly*/false);
499f4a2713aSLionel Sambuc if (MemberInit.isInvalid()) {
500f4a2713aSLionel Sambuc hadError = true;
501f4a2713aSLionel Sambuc return;
502f4a2713aSLionel Sambuc }
503f4a2713aSLionel Sambuc
504f4a2713aSLionel Sambuc if (hadError) {
505f4a2713aSLionel Sambuc // Do nothing
506f4a2713aSLionel Sambuc } else if (Init < NumInits) {
507*0a6a1f1dSLionel Sambuc ILE->setInit(Init, MemberInit.getAs<Expr>());
508*0a6a1f1dSLionel Sambuc } else if (!isa<ImplicitValueInitExpr>(MemberInit.get())) {
509*0a6a1f1dSLionel Sambuc // Empty initialization requires a constructor call, so
510f4a2713aSLionel Sambuc // extend the initializer list to include the constructor
511f4a2713aSLionel Sambuc // call and make a note that we'll need to take another pass
512f4a2713aSLionel Sambuc // through the initializer list.
513*0a6a1f1dSLionel Sambuc ILE->updateInit(SemaRef.Context, Init, MemberInit.getAs<Expr>());
514f4a2713aSLionel Sambuc RequiresSecondPass = true;
515f4a2713aSLionel Sambuc }
516f4a2713aSLionel Sambuc } else if (InitListExpr *InnerILE
517f4a2713aSLionel Sambuc = dyn_cast<InitListExpr>(ILE->getInit(Init)))
518*0a6a1f1dSLionel Sambuc FillInEmptyInitializations(MemberEntity, InnerILE,
519f4a2713aSLionel Sambuc RequiresSecondPass);
520f4a2713aSLionel Sambuc }
521f4a2713aSLionel Sambuc
522f4a2713aSLionel Sambuc /// Recursively replaces NULL values within the given initializer list
523f4a2713aSLionel Sambuc /// with expressions that perform value-initialization of the
524f4a2713aSLionel Sambuc /// appropriate type.
525f4a2713aSLionel Sambuc void
FillInEmptyInitializations(const InitializedEntity & Entity,InitListExpr * ILE,bool & RequiresSecondPass)526*0a6a1f1dSLionel Sambuc InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity,
527f4a2713aSLionel Sambuc InitListExpr *ILE,
528f4a2713aSLionel Sambuc bool &RequiresSecondPass) {
529f4a2713aSLionel Sambuc assert((ILE->getType() != SemaRef.Context.VoidTy) &&
530f4a2713aSLionel Sambuc "Should not have void type");
531f4a2713aSLionel Sambuc
532f4a2713aSLionel Sambuc if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) {
533f4a2713aSLionel Sambuc const RecordDecl *RDecl = RType->getDecl();
534f4a2713aSLionel Sambuc if (RDecl->isUnion() && ILE->getInitializedFieldInUnion())
535*0a6a1f1dSLionel Sambuc FillInEmptyInitForField(0, ILE->getInitializedFieldInUnion(),
536f4a2713aSLionel Sambuc Entity, ILE, RequiresSecondPass);
537f4a2713aSLionel Sambuc else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) &&
538f4a2713aSLionel Sambuc cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) {
539*0a6a1f1dSLionel Sambuc for (auto *Field : RDecl->fields()) {
540f4a2713aSLionel Sambuc if (Field->hasInClassInitializer()) {
541*0a6a1f1dSLionel Sambuc FillInEmptyInitForField(0, Field, Entity, ILE, RequiresSecondPass);
542f4a2713aSLionel Sambuc break;
543f4a2713aSLionel Sambuc }
544f4a2713aSLionel Sambuc }
545f4a2713aSLionel Sambuc } else {
546f4a2713aSLionel Sambuc unsigned Init = 0;
547*0a6a1f1dSLionel Sambuc for (auto *Field : RDecl->fields()) {
548f4a2713aSLionel Sambuc if (Field->isUnnamedBitfield())
549f4a2713aSLionel Sambuc continue;
550f4a2713aSLionel Sambuc
551f4a2713aSLionel Sambuc if (hadError)
552f4a2713aSLionel Sambuc return;
553f4a2713aSLionel Sambuc
554*0a6a1f1dSLionel Sambuc FillInEmptyInitForField(Init, Field, Entity, ILE, RequiresSecondPass);
555f4a2713aSLionel Sambuc if (hadError)
556f4a2713aSLionel Sambuc return;
557f4a2713aSLionel Sambuc
558f4a2713aSLionel Sambuc ++Init;
559f4a2713aSLionel Sambuc
560f4a2713aSLionel Sambuc // Only look at the first initialization of a union.
561f4a2713aSLionel Sambuc if (RDecl->isUnion())
562f4a2713aSLionel Sambuc break;
563f4a2713aSLionel Sambuc }
564f4a2713aSLionel Sambuc }
565f4a2713aSLionel Sambuc
566f4a2713aSLionel Sambuc return;
567f4a2713aSLionel Sambuc }
568f4a2713aSLionel Sambuc
569f4a2713aSLionel Sambuc QualType ElementType;
570f4a2713aSLionel Sambuc
571f4a2713aSLionel Sambuc InitializedEntity ElementEntity = Entity;
572f4a2713aSLionel Sambuc unsigned NumInits = ILE->getNumInits();
573f4a2713aSLionel Sambuc unsigned NumElements = NumInits;
574f4a2713aSLionel Sambuc if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
575f4a2713aSLionel Sambuc ElementType = AType->getElementType();
576f4a2713aSLionel Sambuc if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType))
577f4a2713aSLionel Sambuc NumElements = CAType->getSize().getZExtValue();
578f4a2713aSLionel Sambuc ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
579f4a2713aSLionel Sambuc 0, Entity);
580f4a2713aSLionel Sambuc } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) {
581f4a2713aSLionel Sambuc ElementType = VType->getElementType();
582f4a2713aSLionel Sambuc NumElements = VType->getNumElements();
583f4a2713aSLionel Sambuc ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context,
584f4a2713aSLionel Sambuc 0, Entity);
585f4a2713aSLionel Sambuc } else
586f4a2713aSLionel Sambuc ElementType = ILE->getType();
587f4a2713aSLionel Sambuc
588f4a2713aSLionel Sambuc for (unsigned Init = 0; Init != NumElements; ++Init) {
589f4a2713aSLionel Sambuc if (hadError)
590f4a2713aSLionel Sambuc return;
591f4a2713aSLionel Sambuc
592f4a2713aSLionel Sambuc if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement ||
593f4a2713aSLionel Sambuc ElementEntity.getKind() == InitializedEntity::EK_VectorElement)
594f4a2713aSLionel Sambuc ElementEntity.setElementIndex(Init);
595f4a2713aSLionel Sambuc
596*0a6a1f1dSLionel Sambuc Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr);
597f4a2713aSLionel Sambuc if (!InitExpr && !ILE->hasArrayFiller()) {
598*0a6a1f1dSLionel Sambuc ExprResult ElementInit = PerformEmptyInit(SemaRef, ILE->getLocEnd(),
599*0a6a1f1dSLionel Sambuc ElementEntity,
600*0a6a1f1dSLionel Sambuc /*VerifyOnly*/false);
601f4a2713aSLionel Sambuc if (ElementInit.isInvalid()) {
602f4a2713aSLionel Sambuc hadError = true;
603f4a2713aSLionel Sambuc return;
604f4a2713aSLionel Sambuc }
605f4a2713aSLionel Sambuc
606f4a2713aSLionel Sambuc if (hadError) {
607f4a2713aSLionel Sambuc // Do nothing
608f4a2713aSLionel Sambuc } else if (Init < NumInits) {
609f4a2713aSLionel Sambuc // For arrays, just set the expression used for value-initialization
610f4a2713aSLionel Sambuc // of the "holes" in the array.
611f4a2713aSLionel Sambuc if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement)
612*0a6a1f1dSLionel Sambuc ILE->setArrayFiller(ElementInit.getAs<Expr>());
613f4a2713aSLionel Sambuc else
614*0a6a1f1dSLionel Sambuc ILE->setInit(Init, ElementInit.getAs<Expr>());
615f4a2713aSLionel Sambuc } else {
616f4a2713aSLionel Sambuc // For arrays, just set the expression used for value-initialization
617f4a2713aSLionel Sambuc // of the rest of elements and exit.
618f4a2713aSLionel Sambuc if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) {
619*0a6a1f1dSLionel Sambuc ILE->setArrayFiller(ElementInit.getAs<Expr>());
620f4a2713aSLionel Sambuc return;
621f4a2713aSLionel Sambuc }
622f4a2713aSLionel Sambuc
623*0a6a1f1dSLionel Sambuc if (!isa<ImplicitValueInitExpr>(ElementInit.get())) {
624*0a6a1f1dSLionel Sambuc // Empty initialization requires a constructor call, so
625f4a2713aSLionel Sambuc // extend the initializer list to include the constructor
626f4a2713aSLionel Sambuc // call and make a note that we'll need to take another pass
627f4a2713aSLionel Sambuc // through the initializer list.
628*0a6a1f1dSLionel Sambuc ILE->updateInit(SemaRef.Context, Init, ElementInit.getAs<Expr>());
629f4a2713aSLionel Sambuc RequiresSecondPass = true;
630f4a2713aSLionel Sambuc }
631f4a2713aSLionel Sambuc }
632f4a2713aSLionel Sambuc } else if (InitListExpr *InnerILE
633f4a2713aSLionel Sambuc = dyn_cast_or_null<InitListExpr>(InitExpr))
634*0a6a1f1dSLionel Sambuc FillInEmptyInitializations(ElementEntity, InnerILE, RequiresSecondPass);
635f4a2713aSLionel Sambuc }
636f4a2713aSLionel Sambuc }
637f4a2713aSLionel Sambuc
638f4a2713aSLionel Sambuc
InitListChecker(Sema & S,const InitializedEntity & Entity,InitListExpr * IL,QualType & T,bool VerifyOnly)639f4a2713aSLionel Sambuc InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity,
640f4a2713aSLionel Sambuc InitListExpr *IL, QualType &T,
641f4a2713aSLionel Sambuc bool VerifyOnly)
642f4a2713aSLionel Sambuc : SemaRef(S), VerifyOnly(VerifyOnly) {
643f4a2713aSLionel Sambuc hadError = false;
644f4a2713aSLionel Sambuc
645f4a2713aSLionel Sambuc FullyStructuredList =
646*0a6a1f1dSLionel Sambuc getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange());
647f4a2713aSLionel Sambuc CheckExplicitInitList(Entity, IL, T, FullyStructuredList,
648f4a2713aSLionel Sambuc /*TopLevelObject=*/true);
649f4a2713aSLionel Sambuc
650f4a2713aSLionel Sambuc if (!hadError && !VerifyOnly) {
651f4a2713aSLionel Sambuc bool RequiresSecondPass = false;
652*0a6a1f1dSLionel Sambuc FillInEmptyInitializations(Entity, FullyStructuredList, RequiresSecondPass);
653f4a2713aSLionel Sambuc if (RequiresSecondPass && !hadError)
654*0a6a1f1dSLionel Sambuc FillInEmptyInitializations(Entity, FullyStructuredList,
655f4a2713aSLionel Sambuc RequiresSecondPass);
656f4a2713aSLionel Sambuc }
657f4a2713aSLionel Sambuc }
658f4a2713aSLionel Sambuc
numArrayElements(QualType DeclType)659f4a2713aSLionel Sambuc int InitListChecker::numArrayElements(QualType DeclType) {
660f4a2713aSLionel Sambuc // FIXME: use a proper constant
661f4a2713aSLionel Sambuc int maxElements = 0x7FFFFFFF;
662f4a2713aSLionel Sambuc if (const ConstantArrayType *CAT =
663f4a2713aSLionel Sambuc SemaRef.Context.getAsConstantArrayType(DeclType)) {
664f4a2713aSLionel Sambuc maxElements = static_cast<int>(CAT->getSize().getZExtValue());
665f4a2713aSLionel Sambuc }
666f4a2713aSLionel Sambuc return maxElements;
667f4a2713aSLionel Sambuc }
668f4a2713aSLionel Sambuc
numStructUnionElements(QualType DeclType)669f4a2713aSLionel Sambuc int InitListChecker::numStructUnionElements(QualType DeclType) {
670f4a2713aSLionel Sambuc RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl();
671f4a2713aSLionel Sambuc int InitializableMembers = 0;
672*0a6a1f1dSLionel Sambuc for (const auto *Field : structDecl->fields())
673f4a2713aSLionel Sambuc if (!Field->isUnnamedBitfield())
674f4a2713aSLionel Sambuc ++InitializableMembers;
675*0a6a1f1dSLionel Sambuc
676f4a2713aSLionel Sambuc if (structDecl->isUnion())
677f4a2713aSLionel Sambuc return std::min(InitializableMembers, 1);
678f4a2713aSLionel Sambuc return InitializableMembers - structDecl->hasFlexibleArrayMember();
679f4a2713aSLionel Sambuc }
680f4a2713aSLionel Sambuc
681f4a2713aSLionel Sambuc /// Check whether the range of the initializer \p ParentIList from element
682f4a2713aSLionel Sambuc /// \p Index onwards can be used to initialize an object of type \p T. Update
683f4a2713aSLionel Sambuc /// \p Index to indicate how many elements of the list were consumed.
684f4a2713aSLionel Sambuc ///
685f4a2713aSLionel Sambuc /// This also fills in \p StructuredList, from element \p StructuredIndex
686f4a2713aSLionel Sambuc /// onwards, with the fully-braced, desugared form of the initialization.
CheckImplicitInitList(const InitializedEntity & Entity,InitListExpr * ParentIList,QualType T,unsigned & Index,InitListExpr * StructuredList,unsigned & StructuredIndex)687f4a2713aSLionel Sambuc void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity,
688f4a2713aSLionel Sambuc InitListExpr *ParentIList,
689f4a2713aSLionel Sambuc QualType T, unsigned &Index,
690f4a2713aSLionel Sambuc InitListExpr *StructuredList,
691f4a2713aSLionel Sambuc unsigned &StructuredIndex) {
692f4a2713aSLionel Sambuc int maxElements = 0;
693f4a2713aSLionel Sambuc
694f4a2713aSLionel Sambuc if (T->isArrayType())
695f4a2713aSLionel Sambuc maxElements = numArrayElements(T);
696f4a2713aSLionel Sambuc else if (T->isRecordType())
697f4a2713aSLionel Sambuc maxElements = numStructUnionElements(T);
698f4a2713aSLionel Sambuc else if (T->isVectorType())
699f4a2713aSLionel Sambuc maxElements = T->getAs<VectorType>()->getNumElements();
700f4a2713aSLionel Sambuc else
701f4a2713aSLionel Sambuc llvm_unreachable("CheckImplicitInitList(): Illegal type");
702f4a2713aSLionel Sambuc
703f4a2713aSLionel Sambuc if (maxElements == 0) {
704f4a2713aSLionel Sambuc if (!VerifyOnly)
705f4a2713aSLionel Sambuc SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(),
706f4a2713aSLionel Sambuc diag::err_implicit_empty_initializer);
707f4a2713aSLionel Sambuc ++Index;
708f4a2713aSLionel Sambuc hadError = true;
709f4a2713aSLionel Sambuc return;
710f4a2713aSLionel Sambuc }
711f4a2713aSLionel Sambuc
712f4a2713aSLionel Sambuc // Build a structured initializer list corresponding to this subobject.
713f4a2713aSLionel Sambuc InitListExpr *StructuredSubobjectInitList
714f4a2713aSLionel Sambuc = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList,
715f4a2713aSLionel Sambuc StructuredIndex,
716f4a2713aSLionel Sambuc SourceRange(ParentIList->getInit(Index)->getLocStart(),
717f4a2713aSLionel Sambuc ParentIList->getSourceRange().getEnd()));
718f4a2713aSLionel Sambuc unsigned StructuredSubobjectInitIndex = 0;
719f4a2713aSLionel Sambuc
720f4a2713aSLionel Sambuc // Check the element types and build the structural subobject.
721f4a2713aSLionel Sambuc unsigned StartIndex = Index;
722f4a2713aSLionel Sambuc CheckListElementTypes(Entity, ParentIList, T,
723f4a2713aSLionel Sambuc /*SubobjectIsDesignatorContext=*/false, Index,
724f4a2713aSLionel Sambuc StructuredSubobjectInitList,
725f4a2713aSLionel Sambuc StructuredSubobjectInitIndex);
726f4a2713aSLionel Sambuc
727f4a2713aSLionel Sambuc if (!VerifyOnly) {
728f4a2713aSLionel Sambuc StructuredSubobjectInitList->setType(T);
729f4a2713aSLionel Sambuc
730f4a2713aSLionel Sambuc unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1);
731f4a2713aSLionel Sambuc // Update the structured sub-object initializer so that it's ending
732f4a2713aSLionel Sambuc // range corresponds with the end of the last initializer it used.
733f4a2713aSLionel Sambuc if (EndIndex < ParentIList->getNumInits()) {
734f4a2713aSLionel Sambuc SourceLocation EndLoc
735f4a2713aSLionel Sambuc = ParentIList->getInit(EndIndex)->getSourceRange().getEnd();
736f4a2713aSLionel Sambuc StructuredSubobjectInitList->setRBraceLoc(EndLoc);
737f4a2713aSLionel Sambuc }
738f4a2713aSLionel Sambuc
739f4a2713aSLionel Sambuc // Complain about missing braces.
740f4a2713aSLionel Sambuc if (T->isArrayType() || T->isRecordType()) {
741f4a2713aSLionel Sambuc SemaRef.Diag(StructuredSubobjectInitList->getLocStart(),
742f4a2713aSLionel Sambuc diag::warn_missing_braces)
743f4a2713aSLionel Sambuc << StructuredSubobjectInitList->getSourceRange()
744f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(
745f4a2713aSLionel Sambuc StructuredSubobjectInitList->getLocStart(), "{")
746f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(
747*0a6a1f1dSLionel Sambuc SemaRef.getLocForEndOfToken(
748f4a2713aSLionel Sambuc StructuredSubobjectInitList->getLocEnd()),
749f4a2713aSLionel Sambuc "}");
750f4a2713aSLionel Sambuc }
751f4a2713aSLionel Sambuc }
752f4a2713aSLionel Sambuc }
753f4a2713aSLionel Sambuc
754f4a2713aSLionel Sambuc /// Check whether the initializer \p IList (that was written with explicit
755f4a2713aSLionel Sambuc /// braces) can be used to initialize an object of type \p T.
756f4a2713aSLionel Sambuc ///
757f4a2713aSLionel Sambuc /// This also fills in \p StructuredList with the fully-braced, desugared
758f4a2713aSLionel Sambuc /// form of the initialization.
CheckExplicitInitList(const InitializedEntity & Entity,InitListExpr * IList,QualType & T,InitListExpr * StructuredList,bool TopLevelObject)759f4a2713aSLionel Sambuc void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity,
760f4a2713aSLionel Sambuc InitListExpr *IList, QualType &T,
761f4a2713aSLionel Sambuc InitListExpr *StructuredList,
762f4a2713aSLionel Sambuc bool TopLevelObject) {
763f4a2713aSLionel Sambuc if (!VerifyOnly) {
764f4a2713aSLionel Sambuc SyntacticToSemantic[IList] = StructuredList;
765f4a2713aSLionel Sambuc StructuredList->setSyntacticForm(IList);
766f4a2713aSLionel Sambuc }
767f4a2713aSLionel Sambuc
768f4a2713aSLionel Sambuc unsigned Index = 0, StructuredIndex = 0;
769f4a2713aSLionel Sambuc CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true,
770f4a2713aSLionel Sambuc Index, StructuredList, StructuredIndex, TopLevelObject);
771f4a2713aSLionel Sambuc if (!VerifyOnly) {
772f4a2713aSLionel Sambuc QualType ExprTy = T;
773f4a2713aSLionel Sambuc if (!ExprTy->isArrayType())
774f4a2713aSLionel Sambuc ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context);
775f4a2713aSLionel Sambuc IList->setType(ExprTy);
776f4a2713aSLionel Sambuc StructuredList->setType(ExprTy);
777f4a2713aSLionel Sambuc }
778f4a2713aSLionel Sambuc if (hadError)
779f4a2713aSLionel Sambuc return;
780f4a2713aSLionel Sambuc
781f4a2713aSLionel Sambuc if (Index < IList->getNumInits()) {
782f4a2713aSLionel Sambuc // We have leftover initializers
783f4a2713aSLionel Sambuc if (VerifyOnly) {
784f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus ||
785f4a2713aSLionel Sambuc (SemaRef.getLangOpts().OpenCL &&
786f4a2713aSLionel Sambuc IList->getType()->isVectorType())) {
787f4a2713aSLionel Sambuc hadError = true;
788f4a2713aSLionel Sambuc }
789f4a2713aSLionel Sambuc return;
790f4a2713aSLionel Sambuc }
791f4a2713aSLionel Sambuc
792f4a2713aSLionel Sambuc if (StructuredIndex == 1 &&
793f4a2713aSLionel Sambuc IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) ==
794f4a2713aSLionel Sambuc SIF_None) {
795*0a6a1f1dSLionel Sambuc unsigned DK = diag::ext_excess_initializers_in_char_array_initializer;
796f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) {
797f4a2713aSLionel Sambuc DK = diag::err_excess_initializers_in_char_array_initializer;
798f4a2713aSLionel Sambuc hadError = true;
799f4a2713aSLionel Sambuc }
800f4a2713aSLionel Sambuc // Special-case
801f4a2713aSLionel Sambuc SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
802f4a2713aSLionel Sambuc << IList->getInit(Index)->getSourceRange();
803f4a2713aSLionel Sambuc } else if (!T->isIncompleteType()) {
804f4a2713aSLionel Sambuc // Don't complain for incomplete types, since we'll get an error
805f4a2713aSLionel Sambuc // elsewhere
806f4a2713aSLionel Sambuc QualType CurrentObjectType = StructuredList->getType();
807f4a2713aSLionel Sambuc int initKind =
808f4a2713aSLionel Sambuc CurrentObjectType->isArrayType()? 0 :
809f4a2713aSLionel Sambuc CurrentObjectType->isVectorType()? 1 :
810f4a2713aSLionel Sambuc CurrentObjectType->isScalarType()? 2 :
811f4a2713aSLionel Sambuc CurrentObjectType->isUnionType()? 3 :
812f4a2713aSLionel Sambuc 4;
813f4a2713aSLionel Sambuc
814*0a6a1f1dSLionel Sambuc unsigned DK = diag::ext_excess_initializers;
815f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().CPlusPlus) {
816f4a2713aSLionel Sambuc DK = diag::err_excess_initializers;
817f4a2713aSLionel Sambuc hadError = true;
818f4a2713aSLionel Sambuc }
819f4a2713aSLionel Sambuc if (SemaRef.getLangOpts().OpenCL && initKind == 1) {
820f4a2713aSLionel Sambuc DK = diag::err_excess_initializers;
821f4a2713aSLionel Sambuc hadError = true;
822f4a2713aSLionel Sambuc }
823f4a2713aSLionel Sambuc
824f4a2713aSLionel Sambuc SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK)
825f4a2713aSLionel Sambuc << initKind << IList->getInit(Index)->getSourceRange();
826f4a2713aSLionel Sambuc }
827f4a2713aSLionel Sambuc }
828f4a2713aSLionel Sambuc
829f4a2713aSLionel Sambuc if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 &&
830f4a2713aSLionel Sambuc !TopLevelObject)
831f4a2713aSLionel Sambuc SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init)
832f4a2713aSLionel Sambuc << IList->getSourceRange()
833f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(IList->getLocStart())
834f4a2713aSLionel Sambuc << FixItHint::CreateRemoval(IList->getLocEnd());
835f4a2713aSLionel Sambuc }
836f4a2713aSLionel Sambuc
CheckListElementTypes(const InitializedEntity & Entity,InitListExpr * IList,QualType & DeclType,bool SubobjectIsDesignatorContext,unsigned & Index,InitListExpr * StructuredList,unsigned & StructuredIndex,bool TopLevelObject)837f4a2713aSLionel Sambuc void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity,
838f4a2713aSLionel Sambuc InitListExpr *IList,
839f4a2713aSLionel Sambuc QualType &DeclType,
840f4a2713aSLionel Sambuc bool SubobjectIsDesignatorContext,
841f4a2713aSLionel Sambuc unsigned &Index,
842f4a2713aSLionel Sambuc InitListExpr *StructuredList,
843f4a2713aSLionel Sambuc unsigned &StructuredIndex,
844f4a2713aSLionel Sambuc bool TopLevelObject) {
845f4a2713aSLionel Sambuc if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) {
846f4a2713aSLionel Sambuc // Explicitly braced initializer for complex type can be real+imaginary
847f4a2713aSLionel Sambuc // parts.
848f4a2713aSLionel Sambuc CheckComplexType(Entity, IList, DeclType, Index,
849f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
850f4a2713aSLionel Sambuc } else if (DeclType->isScalarType()) {
851f4a2713aSLionel Sambuc CheckScalarType(Entity, IList, DeclType, Index,
852f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
853f4a2713aSLionel Sambuc } else if (DeclType->isVectorType()) {
854f4a2713aSLionel Sambuc CheckVectorType(Entity, IList, DeclType, Index,
855f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
856f4a2713aSLionel Sambuc } else if (DeclType->isRecordType()) {
857f4a2713aSLionel Sambuc assert(DeclType->isAggregateType() &&
858f4a2713aSLionel Sambuc "non-aggregate records should be handed in CheckSubElementType");
859f4a2713aSLionel Sambuc RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
860f4a2713aSLionel Sambuc CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(),
861f4a2713aSLionel Sambuc SubobjectIsDesignatorContext, Index,
862f4a2713aSLionel Sambuc StructuredList, StructuredIndex,
863f4a2713aSLionel Sambuc TopLevelObject);
864f4a2713aSLionel Sambuc } else if (DeclType->isArrayType()) {
865f4a2713aSLionel Sambuc llvm::APSInt Zero(
866f4a2713aSLionel Sambuc SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()),
867f4a2713aSLionel Sambuc false);
868f4a2713aSLionel Sambuc CheckArrayType(Entity, IList, DeclType, Zero,
869f4a2713aSLionel Sambuc SubobjectIsDesignatorContext, Index,
870f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
871f4a2713aSLionel Sambuc } else if (DeclType->isVoidType() || DeclType->isFunctionType()) {
872f4a2713aSLionel Sambuc // This type is invalid, issue a diagnostic.
873f4a2713aSLionel Sambuc ++Index;
874f4a2713aSLionel Sambuc if (!VerifyOnly)
875f4a2713aSLionel Sambuc SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
876f4a2713aSLionel Sambuc << DeclType;
877f4a2713aSLionel Sambuc hadError = true;
878f4a2713aSLionel Sambuc } else if (DeclType->isReferenceType()) {
879f4a2713aSLionel Sambuc CheckReferenceType(Entity, IList, DeclType, Index,
880f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
881f4a2713aSLionel Sambuc } else if (DeclType->isObjCObjectType()) {
882f4a2713aSLionel Sambuc if (!VerifyOnly)
883f4a2713aSLionel Sambuc SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class)
884f4a2713aSLionel Sambuc << DeclType;
885f4a2713aSLionel Sambuc hadError = true;
886f4a2713aSLionel Sambuc } else {
887f4a2713aSLionel Sambuc if (!VerifyOnly)
888f4a2713aSLionel Sambuc SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type)
889f4a2713aSLionel Sambuc << DeclType;
890f4a2713aSLionel Sambuc hadError = true;
891f4a2713aSLionel Sambuc }
892f4a2713aSLionel Sambuc }
893f4a2713aSLionel Sambuc
CheckSubElementType(const InitializedEntity & Entity,InitListExpr * IList,QualType ElemType,unsigned & Index,InitListExpr * StructuredList,unsigned & StructuredIndex)894f4a2713aSLionel Sambuc void InitListChecker::CheckSubElementType(const InitializedEntity &Entity,
895f4a2713aSLionel Sambuc InitListExpr *IList,
896f4a2713aSLionel Sambuc QualType ElemType,
897f4a2713aSLionel Sambuc unsigned &Index,
898f4a2713aSLionel Sambuc InitListExpr *StructuredList,
899f4a2713aSLionel Sambuc unsigned &StructuredIndex) {
900f4a2713aSLionel Sambuc Expr *expr = IList->getInit(Index);
901f4a2713aSLionel Sambuc
902f4a2713aSLionel Sambuc if (ElemType->isReferenceType())
903f4a2713aSLionel Sambuc return CheckReferenceType(Entity, IList, ElemType, Index,
904f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
905f4a2713aSLionel Sambuc
906f4a2713aSLionel Sambuc if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) {
907f4a2713aSLionel Sambuc if (!ElemType->isRecordType() || ElemType->isAggregateType()) {
908f4a2713aSLionel Sambuc InitListExpr *InnerStructuredList
909f4a2713aSLionel Sambuc = getStructuredSubobjectInit(IList, Index, ElemType,
910f4a2713aSLionel Sambuc StructuredList, StructuredIndex,
911f4a2713aSLionel Sambuc SubInitList->getSourceRange());
912f4a2713aSLionel Sambuc CheckExplicitInitList(Entity, SubInitList, ElemType,
913f4a2713aSLionel Sambuc InnerStructuredList);
914f4a2713aSLionel Sambuc ++StructuredIndex;
915f4a2713aSLionel Sambuc ++Index;
916f4a2713aSLionel Sambuc return;
917f4a2713aSLionel Sambuc }
918f4a2713aSLionel Sambuc assert(SemaRef.getLangOpts().CPlusPlus &&
919f4a2713aSLionel Sambuc "non-aggregate records are only possible in C++");
920f4a2713aSLionel Sambuc // C++ initialization is handled later.
921*0a6a1f1dSLionel Sambuc } else if (isa<ImplicitValueInitExpr>(expr)) {
922*0a6a1f1dSLionel Sambuc // This happens during template instantiation when we see an InitListExpr
923*0a6a1f1dSLionel Sambuc // that we've already checked once.
924*0a6a1f1dSLionel Sambuc assert(SemaRef.Context.hasSameType(expr->getType(), ElemType) &&
925*0a6a1f1dSLionel Sambuc "found implicit initialization for the wrong type");
926*0a6a1f1dSLionel Sambuc if (!VerifyOnly)
927*0a6a1f1dSLionel Sambuc UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
928*0a6a1f1dSLionel Sambuc ++Index;
929*0a6a1f1dSLionel Sambuc return;
930f4a2713aSLionel Sambuc }
931f4a2713aSLionel Sambuc
932f4a2713aSLionel Sambuc // FIXME: Need to handle atomic aggregate types with implicit init lists.
933f4a2713aSLionel Sambuc if (ElemType->isScalarType() || ElemType->isAtomicType())
934f4a2713aSLionel Sambuc return CheckScalarType(Entity, IList, ElemType, Index,
935f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
936f4a2713aSLionel Sambuc
937f4a2713aSLionel Sambuc assert((ElemType->isRecordType() || ElemType->isVectorType() ||
938f4a2713aSLionel Sambuc ElemType->isArrayType()) && "Unexpected type");
939f4a2713aSLionel Sambuc
940f4a2713aSLionel Sambuc if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) {
941f4a2713aSLionel Sambuc // arrayType can be incomplete if we're initializing a flexible
942f4a2713aSLionel Sambuc // array member. There's nothing we can do with the completed
943f4a2713aSLionel Sambuc // type here, though.
944f4a2713aSLionel Sambuc
945f4a2713aSLionel Sambuc if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) {
946f4a2713aSLionel Sambuc if (!VerifyOnly) {
947f4a2713aSLionel Sambuc CheckStringInit(expr, ElemType, arrayType, SemaRef);
948f4a2713aSLionel Sambuc UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
949f4a2713aSLionel Sambuc }
950f4a2713aSLionel Sambuc ++Index;
951f4a2713aSLionel Sambuc return;
952f4a2713aSLionel Sambuc }
953f4a2713aSLionel Sambuc
954f4a2713aSLionel Sambuc // Fall through for subaggregate initialization.
955f4a2713aSLionel Sambuc
956f4a2713aSLionel Sambuc } else if (SemaRef.getLangOpts().CPlusPlus) {
957f4a2713aSLionel Sambuc // C++ [dcl.init.aggr]p12:
958f4a2713aSLionel Sambuc // All implicit type conversions (clause 4) are considered when
959f4a2713aSLionel Sambuc // initializing the aggregate member with an initializer from
960f4a2713aSLionel Sambuc // an initializer-list. If the initializer can initialize a
961f4a2713aSLionel Sambuc // member, the member is initialized. [...]
962f4a2713aSLionel Sambuc
963f4a2713aSLionel Sambuc // FIXME: Better EqualLoc?
964f4a2713aSLionel Sambuc InitializationKind Kind =
965f4a2713aSLionel Sambuc InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation());
966f4a2713aSLionel Sambuc InitializationSequence Seq(SemaRef, Entity, Kind, expr);
967f4a2713aSLionel Sambuc
968f4a2713aSLionel Sambuc if (Seq) {
969f4a2713aSLionel Sambuc if (!VerifyOnly) {
970f4a2713aSLionel Sambuc ExprResult Result =
971f4a2713aSLionel Sambuc Seq.Perform(SemaRef, Entity, Kind, expr);
972f4a2713aSLionel Sambuc if (Result.isInvalid())
973f4a2713aSLionel Sambuc hadError = true;
974f4a2713aSLionel Sambuc
975f4a2713aSLionel Sambuc UpdateStructuredListElement(StructuredList, StructuredIndex,
976*0a6a1f1dSLionel Sambuc Result.getAs<Expr>());
977f4a2713aSLionel Sambuc }
978f4a2713aSLionel Sambuc ++Index;
979f4a2713aSLionel Sambuc return;
980f4a2713aSLionel Sambuc }
981f4a2713aSLionel Sambuc
982f4a2713aSLionel Sambuc // Fall through for subaggregate initialization
983f4a2713aSLionel Sambuc } else {
984f4a2713aSLionel Sambuc // C99 6.7.8p13:
985f4a2713aSLionel Sambuc //
986f4a2713aSLionel Sambuc // The initializer for a structure or union object that has
987f4a2713aSLionel Sambuc // automatic storage duration shall be either an initializer
988f4a2713aSLionel Sambuc // list as described below, or a single expression that has
989f4a2713aSLionel Sambuc // compatible structure or union type. In the latter case, the
990f4a2713aSLionel Sambuc // initial value of the object, including unnamed members, is
991f4a2713aSLionel Sambuc // that of the expression.
992*0a6a1f1dSLionel Sambuc ExprResult ExprRes = expr;
993f4a2713aSLionel Sambuc if ((ElemType->isRecordType() || ElemType->isVectorType()) &&
994f4a2713aSLionel Sambuc SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes,
995f4a2713aSLionel Sambuc !VerifyOnly)
996f4a2713aSLionel Sambuc != Sema::Incompatible) {
997f4a2713aSLionel Sambuc if (ExprRes.isInvalid())
998f4a2713aSLionel Sambuc hadError = true;
999f4a2713aSLionel Sambuc else {
1000*0a6a1f1dSLionel Sambuc ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.get());
1001f4a2713aSLionel Sambuc if (ExprRes.isInvalid())
1002f4a2713aSLionel Sambuc hadError = true;
1003f4a2713aSLionel Sambuc }
1004f4a2713aSLionel Sambuc UpdateStructuredListElement(StructuredList, StructuredIndex,
1005*0a6a1f1dSLionel Sambuc ExprRes.getAs<Expr>());
1006f4a2713aSLionel Sambuc ++Index;
1007f4a2713aSLionel Sambuc return;
1008f4a2713aSLionel Sambuc }
1009*0a6a1f1dSLionel Sambuc ExprRes.get();
1010f4a2713aSLionel Sambuc // Fall through for subaggregate initialization
1011f4a2713aSLionel Sambuc }
1012f4a2713aSLionel Sambuc
1013f4a2713aSLionel Sambuc // C++ [dcl.init.aggr]p12:
1014f4a2713aSLionel Sambuc //
1015f4a2713aSLionel Sambuc // [...] Otherwise, if the member is itself a non-empty
1016f4a2713aSLionel Sambuc // subaggregate, brace elision is assumed and the initializer is
1017f4a2713aSLionel Sambuc // considered for the initialization of the first member of
1018f4a2713aSLionel Sambuc // the subaggregate.
1019f4a2713aSLionel Sambuc if (!SemaRef.getLangOpts().OpenCL &&
1020f4a2713aSLionel Sambuc (ElemType->isAggregateType() || ElemType->isVectorType())) {
1021f4a2713aSLionel Sambuc CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList,
1022f4a2713aSLionel Sambuc StructuredIndex);
1023f4a2713aSLionel Sambuc ++StructuredIndex;
1024f4a2713aSLionel Sambuc } else {
1025f4a2713aSLionel Sambuc if (!VerifyOnly) {
1026f4a2713aSLionel Sambuc // We cannot initialize this element, so let
1027f4a2713aSLionel Sambuc // PerformCopyInitialization produce the appropriate diagnostic.
1028*0a6a1f1dSLionel Sambuc SemaRef.PerformCopyInitialization(Entity, SourceLocation(), expr,
1029f4a2713aSLionel Sambuc /*TopLevelOfInitList=*/true);
1030f4a2713aSLionel Sambuc }
1031f4a2713aSLionel Sambuc hadError = true;
1032f4a2713aSLionel Sambuc ++Index;
1033f4a2713aSLionel Sambuc ++StructuredIndex;
1034f4a2713aSLionel Sambuc }
1035f4a2713aSLionel Sambuc }
1036f4a2713aSLionel Sambuc
CheckComplexType(const InitializedEntity & Entity,InitListExpr * IList,QualType DeclType,unsigned & Index,InitListExpr * StructuredList,unsigned & StructuredIndex)1037f4a2713aSLionel Sambuc void InitListChecker::CheckComplexType(const InitializedEntity &Entity,
1038f4a2713aSLionel Sambuc InitListExpr *IList, QualType DeclType,
1039f4a2713aSLionel Sambuc unsigned &Index,
1040f4a2713aSLionel Sambuc InitListExpr *StructuredList,
1041f4a2713aSLionel Sambuc unsigned &StructuredIndex) {
1042f4a2713aSLionel Sambuc assert(Index == 0 && "Index in explicit init list must be zero");
1043f4a2713aSLionel Sambuc
1044f4a2713aSLionel Sambuc // As an extension, clang supports complex initializers, which initialize
1045f4a2713aSLionel Sambuc // a complex number component-wise. When an explicit initializer list for
1046f4a2713aSLionel Sambuc // a complex number contains two two initializers, this extension kicks in:
1047f4a2713aSLionel Sambuc // it exepcts the initializer list to contain two elements convertible to
1048f4a2713aSLionel Sambuc // the element type of the complex type. The first element initializes
1049f4a2713aSLionel Sambuc // the real part, and the second element intitializes the imaginary part.
1050f4a2713aSLionel Sambuc
1051f4a2713aSLionel Sambuc if (IList->getNumInits() != 2)
1052f4a2713aSLionel Sambuc return CheckScalarType(Entity, IList, DeclType, Index, StructuredList,
1053f4a2713aSLionel Sambuc StructuredIndex);
1054f4a2713aSLionel Sambuc
1055f4a2713aSLionel Sambuc // This is an extension in C. (The builtin _Complex type does not exist
1056f4a2713aSLionel Sambuc // in the C++ standard.)
1057f4a2713aSLionel Sambuc if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly)
1058f4a2713aSLionel Sambuc SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init)
1059f4a2713aSLionel Sambuc << IList->getSourceRange();
1060f4a2713aSLionel Sambuc
1061f4a2713aSLionel Sambuc // Initialize the complex number.
1062f4a2713aSLionel Sambuc QualType elementType = DeclType->getAs<ComplexType>()->getElementType();
1063f4a2713aSLionel Sambuc InitializedEntity ElementEntity =
1064f4a2713aSLionel Sambuc InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
1065f4a2713aSLionel Sambuc
1066f4a2713aSLionel Sambuc for (unsigned i = 0; i < 2; ++i) {
1067f4a2713aSLionel Sambuc ElementEntity.setElementIndex(Index);
1068f4a2713aSLionel Sambuc CheckSubElementType(ElementEntity, IList, elementType, Index,
1069f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
1070f4a2713aSLionel Sambuc }
1071f4a2713aSLionel Sambuc }
1072f4a2713aSLionel Sambuc
1073f4a2713aSLionel Sambuc
CheckScalarType(const InitializedEntity & Entity,InitListExpr * IList,QualType DeclType,unsigned & Index,InitListExpr * StructuredList,unsigned & StructuredIndex)1074f4a2713aSLionel Sambuc void InitListChecker::CheckScalarType(const InitializedEntity &Entity,
1075f4a2713aSLionel Sambuc InitListExpr *IList, QualType DeclType,
1076f4a2713aSLionel Sambuc unsigned &Index,
1077f4a2713aSLionel Sambuc InitListExpr *StructuredList,
1078f4a2713aSLionel Sambuc unsigned &StructuredIndex) {
1079f4a2713aSLionel Sambuc if (Index >= IList->getNumInits()) {
1080f4a2713aSLionel Sambuc if (!VerifyOnly)
1081f4a2713aSLionel Sambuc SemaRef.Diag(IList->getLocStart(),
1082f4a2713aSLionel Sambuc SemaRef.getLangOpts().CPlusPlus11 ?
1083f4a2713aSLionel Sambuc diag::warn_cxx98_compat_empty_scalar_initializer :
1084f4a2713aSLionel Sambuc diag::err_empty_scalar_initializer)
1085f4a2713aSLionel Sambuc << IList->getSourceRange();
1086f4a2713aSLionel Sambuc hadError = !SemaRef.getLangOpts().CPlusPlus11;
1087f4a2713aSLionel Sambuc ++Index;
1088f4a2713aSLionel Sambuc ++StructuredIndex;
1089f4a2713aSLionel Sambuc return;
1090f4a2713aSLionel Sambuc }
1091f4a2713aSLionel Sambuc
1092f4a2713aSLionel Sambuc Expr *expr = IList->getInit(Index);
1093f4a2713aSLionel Sambuc if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) {
1094*0a6a1f1dSLionel Sambuc // FIXME: This is invalid, and accepting it causes overload resolution
1095*0a6a1f1dSLionel Sambuc // to pick the wrong overload in some corner cases.
1096f4a2713aSLionel Sambuc if (!VerifyOnly)
1097f4a2713aSLionel Sambuc SemaRef.Diag(SubIList->getLocStart(),
1098*0a6a1f1dSLionel Sambuc diag::ext_many_braces_around_scalar_init)
1099f4a2713aSLionel Sambuc << SubIList->getSourceRange();
1100f4a2713aSLionel Sambuc
1101f4a2713aSLionel Sambuc CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList,
1102f4a2713aSLionel Sambuc StructuredIndex);
1103f4a2713aSLionel Sambuc return;
1104f4a2713aSLionel Sambuc } else if (isa<DesignatedInitExpr>(expr)) {
1105f4a2713aSLionel Sambuc if (!VerifyOnly)
1106f4a2713aSLionel Sambuc SemaRef.Diag(expr->getLocStart(),
1107f4a2713aSLionel Sambuc diag::err_designator_for_scalar_init)
1108f4a2713aSLionel Sambuc << DeclType << expr->getSourceRange();
1109f4a2713aSLionel Sambuc hadError = true;
1110f4a2713aSLionel Sambuc ++Index;
1111f4a2713aSLionel Sambuc ++StructuredIndex;
1112f4a2713aSLionel Sambuc return;
1113f4a2713aSLionel Sambuc }
1114f4a2713aSLionel Sambuc
1115f4a2713aSLionel Sambuc if (VerifyOnly) {
1116*0a6a1f1dSLionel Sambuc if (!SemaRef.CanPerformCopyInitialization(Entity,expr))
1117f4a2713aSLionel Sambuc hadError = true;
1118f4a2713aSLionel Sambuc ++Index;
1119f4a2713aSLionel Sambuc return;
1120f4a2713aSLionel Sambuc }
1121f4a2713aSLionel Sambuc
1122f4a2713aSLionel Sambuc ExprResult Result =
1123*0a6a1f1dSLionel Sambuc SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr,
1124f4a2713aSLionel Sambuc /*TopLevelOfInitList=*/true);
1125f4a2713aSLionel Sambuc
1126*0a6a1f1dSLionel Sambuc Expr *ResultExpr = nullptr;
1127f4a2713aSLionel Sambuc
1128f4a2713aSLionel Sambuc if (Result.isInvalid())
1129f4a2713aSLionel Sambuc hadError = true; // types weren't compatible.
1130f4a2713aSLionel Sambuc else {
1131*0a6a1f1dSLionel Sambuc ResultExpr = Result.getAs<Expr>();
1132f4a2713aSLionel Sambuc
1133f4a2713aSLionel Sambuc if (ResultExpr != expr) {
1134f4a2713aSLionel Sambuc // The type was promoted, update initializer list.
1135f4a2713aSLionel Sambuc IList->setInit(Index, ResultExpr);
1136f4a2713aSLionel Sambuc }
1137f4a2713aSLionel Sambuc }
1138f4a2713aSLionel Sambuc if (hadError)
1139f4a2713aSLionel Sambuc ++StructuredIndex;
1140f4a2713aSLionel Sambuc else
1141f4a2713aSLionel Sambuc UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr);
1142f4a2713aSLionel Sambuc ++Index;
1143f4a2713aSLionel Sambuc }
1144f4a2713aSLionel Sambuc
CheckReferenceType(const InitializedEntity & Entity,InitListExpr * IList,QualType DeclType,unsigned & Index,InitListExpr * StructuredList,unsigned & StructuredIndex)1145f4a2713aSLionel Sambuc void InitListChecker::CheckReferenceType(const InitializedEntity &Entity,
1146f4a2713aSLionel Sambuc InitListExpr *IList, QualType DeclType,
1147f4a2713aSLionel Sambuc unsigned &Index,
1148f4a2713aSLionel Sambuc InitListExpr *StructuredList,
1149f4a2713aSLionel Sambuc unsigned &StructuredIndex) {
1150f4a2713aSLionel Sambuc if (Index >= IList->getNumInits()) {
1151f4a2713aSLionel Sambuc // FIXME: It would be wonderful if we could point at the actual member. In
1152f4a2713aSLionel Sambuc // general, it would be useful to pass location information down the stack,
1153f4a2713aSLionel Sambuc // so that we know the location (or decl) of the "current object" being
1154f4a2713aSLionel Sambuc // initialized.
1155f4a2713aSLionel Sambuc if (!VerifyOnly)
1156f4a2713aSLionel Sambuc SemaRef.Diag(IList->getLocStart(),
1157f4a2713aSLionel Sambuc diag::err_init_reference_member_uninitialized)
1158f4a2713aSLionel Sambuc << DeclType
1159f4a2713aSLionel Sambuc << IList->getSourceRange();
1160f4a2713aSLionel Sambuc hadError = true;
1161f4a2713aSLionel Sambuc ++Index;
1162f4a2713aSLionel Sambuc ++StructuredIndex;
1163f4a2713aSLionel Sambuc return;
1164f4a2713aSLionel Sambuc }
1165f4a2713aSLionel Sambuc
1166f4a2713aSLionel Sambuc Expr *expr = IList->getInit(Index);
1167f4a2713aSLionel Sambuc if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) {
1168f4a2713aSLionel Sambuc if (!VerifyOnly)
1169f4a2713aSLionel Sambuc SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list)
1170f4a2713aSLionel Sambuc << DeclType << IList->getSourceRange();
1171f4a2713aSLionel Sambuc hadError = true;
1172f4a2713aSLionel Sambuc ++Index;
1173f4a2713aSLionel Sambuc ++StructuredIndex;
1174f4a2713aSLionel Sambuc return;
1175f4a2713aSLionel Sambuc }
1176f4a2713aSLionel Sambuc
1177f4a2713aSLionel Sambuc if (VerifyOnly) {
1178*0a6a1f1dSLionel Sambuc if (!SemaRef.CanPerformCopyInitialization(Entity,expr))
1179f4a2713aSLionel Sambuc hadError = true;
1180f4a2713aSLionel Sambuc ++Index;
1181f4a2713aSLionel Sambuc return;
1182f4a2713aSLionel Sambuc }
1183f4a2713aSLionel Sambuc
1184f4a2713aSLionel Sambuc ExprResult Result =
1185*0a6a1f1dSLionel Sambuc SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), expr,
1186f4a2713aSLionel Sambuc /*TopLevelOfInitList=*/true);
1187f4a2713aSLionel Sambuc
1188f4a2713aSLionel Sambuc if (Result.isInvalid())
1189f4a2713aSLionel Sambuc hadError = true;
1190f4a2713aSLionel Sambuc
1191*0a6a1f1dSLionel Sambuc expr = Result.getAs<Expr>();
1192f4a2713aSLionel Sambuc IList->setInit(Index, expr);
1193f4a2713aSLionel Sambuc
1194f4a2713aSLionel Sambuc if (hadError)
1195f4a2713aSLionel Sambuc ++StructuredIndex;
1196f4a2713aSLionel Sambuc else
1197f4a2713aSLionel Sambuc UpdateStructuredListElement(StructuredList, StructuredIndex, expr);
1198f4a2713aSLionel Sambuc ++Index;
1199f4a2713aSLionel Sambuc }
1200f4a2713aSLionel Sambuc
CheckVectorType(const InitializedEntity & Entity,InitListExpr * IList,QualType DeclType,unsigned & Index,InitListExpr * StructuredList,unsigned & StructuredIndex)1201f4a2713aSLionel Sambuc void InitListChecker::CheckVectorType(const InitializedEntity &Entity,
1202f4a2713aSLionel Sambuc InitListExpr *IList, QualType DeclType,
1203f4a2713aSLionel Sambuc unsigned &Index,
1204f4a2713aSLionel Sambuc InitListExpr *StructuredList,
1205f4a2713aSLionel Sambuc unsigned &StructuredIndex) {
1206f4a2713aSLionel Sambuc const VectorType *VT = DeclType->getAs<VectorType>();
1207f4a2713aSLionel Sambuc unsigned maxElements = VT->getNumElements();
1208f4a2713aSLionel Sambuc unsigned numEltsInit = 0;
1209f4a2713aSLionel Sambuc QualType elementType = VT->getElementType();
1210f4a2713aSLionel Sambuc
1211f4a2713aSLionel Sambuc if (Index >= IList->getNumInits()) {
1212f4a2713aSLionel Sambuc // Make sure the element type can be value-initialized.
1213f4a2713aSLionel Sambuc if (VerifyOnly)
1214*0a6a1f1dSLionel Sambuc CheckEmptyInitializable(
1215*0a6a1f1dSLionel Sambuc InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity),
1216*0a6a1f1dSLionel Sambuc IList->getLocEnd());
1217f4a2713aSLionel Sambuc return;
1218f4a2713aSLionel Sambuc }
1219f4a2713aSLionel Sambuc
1220f4a2713aSLionel Sambuc if (!SemaRef.getLangOpts().OpenCL) {
1221f4a2713aSLionel Sambuc // If the initializing element is a vector, try to copy-initialize
1222f4a2713aSLionel Sambuc // instead of breaking it apart (which is doomed to failure anyway).
1223f4a2713aSLionel Sambuc Expr *Init = IList->getInit(Index);
1224f4a2713aSLionel Sambuc if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) {
1225f4a2713aSLionel Sambuc if (VerifyOnly) {
1226*0a6a1f1dSLionel Sambuc if (!SemaRef.CanPerformCopyInitialization(Entity, Init))
1227f4a2713aSLionel Sambuc hadError = true;
1228f4a2713aSLionel Sambuc ++Index;
1229f4a2713aSLionel Sambuc return;
1230f4a2713aSLionel Sambuc }
1231f4a2713aSLionel Sambuc
1232f4a2713aSLionel Sambuc ExprResult Result =
1233*0a6a1f1dSLionel Sambuc SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), Init,
1234f4a2713aSLionel Sambuc /*TopLevelOfInitList=*/true);
1235f4a2713aSLionel Sambuc
1236*0a6a1f1dSLionel Sambuc Expr *ResultExpr = nullptr;
1237f4a2713aSLionel Sambuc if (Result.isInvalid())
1238f4a2713aSLionel Sambuc hadError = true; // types weren't compatible.
1239f4a2713aSLionel Sambuc else {
1240*0a6a1f1dSLionel Sambuc ResultExpr = Result.getAs<Expr>();
1241f4a2713aSLionel Sambuc
1242f4a2713aSLionel Sambuc if (ResultExpr != Init) {
1243f4a2713aSLionel Sambuc // The type was promoted, update initializer list.
1244f4a2713aSLionel Sambuc IList->setInit(Index, ResultExpr);
1245f4a2713aSLionel Sambuc }
1246f4a2713aSLionel Sambuc }
1247f4a2713aSLionel Sambuc if (hadError)
1248f4a2713aSLionel Sambuc ++StructuredIndex;
1249f4a2713aSLionel Sambuc else
1250f4a2713aSLionel Sambuc UpdateStructuredListElement(StructuredList, StructuredIndex,
1251f4a2713aSLionel Sambuc ResultExpr);
1252f4a2713aSLionel Sambuc ++Index;
1253f4a2713aSLionel Sambuc return;
1254f4a2713aSLionel Sambuc }
1255f4a2713aSLionel Sambuc
1256f4a2713aSLionel Sambuc InitializedEntity ElementEntity =
1257f4a2713aSLionel Sambuc InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
1258f4a2713aSLionel Sambuc
1259f4a2713aSLionel Sambuc for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) {
1260f4a2713aSLionel Sambuc // Don't attempt to go past the end of the init list
1261f4a2713aSLionel Sambuc if (Index >= IList->getNumInits()) {
1262f4a2713aSLionel Sambuc if (VerifyOnly)
1263*0a6a1f1dSLionel Sambuc CheckEmptyInitializable(ElementEntity, IList->getLocEnd());
1264f4a2713aSLionel Sambuc break;
1265f4a2713aSLionel Sambuc }
1266f4a2713aSLionel Sambuc
1267f4a2713aSLionel Sambuc ElementEntity.setElementIndex(Index);
1268f4a2713aSLionel Sambuc CheckSubElementType(ElementEntity, IList, elementType, Index,
1269f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
1270f4a2713aSLionel Sambuc }
1271*0a6a1f1dSLionel Sambuc
1272*0a6a1f1dSLionel Sambuc if (VerifyOnly)
1273*0a6a1f1dSLionel Sambuc return;
1274*0a6a1f1dSLionel Sambuc
1275*0a6a1f1dSLionel Sambuc bool isBigEndian = SemaRef.Context.getTargetInfo().isBigEndian();
1276*0a6a1f1dSLionel Sambuc const VectorType *T = Entity.getType()->getAs<VectorType>();
1277*0a6a1f1dSLionel Sambuc if (isBigEndian && (T->getVectorKind() == VectorType::NeonVector ||
1278*0a6a1f1dSLionel Sambuc T->getVectorKind() == VectorType::NeonPolyVector)) {
1279*0a6a1f1dSLionel Sambuc // The ability to use vector initializer lists is a GNU vector extension
1280*0a6a1f1dSLionel Sambuc // and is unrelated to the NEON intrinsics in arm_neon.h. On little
1281*0a6a1f1dSLionel Sambuc // endian machines it works fine, however on big endian machines it
1282*0a6a1f1dSLionel Sambuc // exhibits surprising behaviour:
1283*0a6a1f1dSLionel Sambuc //
1284*0a6a1f1dSLionel Sambuc // uint32x2_t x = {42, 64};
1285*0a6a1f1dSLionel Sambuc // return vget_lane_u32(x, 0); // Will return 64.
1286*0a6a1f1dSLionel Sambuc //
1287*0a6a1f1dSLionel Sambuc // Because of this, explicitly call out that it is non-portable.
1288*0a6a1f1dSLionel Sambuc //
1289*0a6a1f1dSLionel Sambuc SemaRef.Diag(IList->getLocStart(),
1290*0a6a1f1dSLionel Sambuc diag::warn_neon_vector_initializer_non_portable);
1291*0a6a1f1dSLionel Sambuc
1292*0a6a1f1dSLionel Sambuc const char *typeCode;
1293*0a6a1f1dSLionel Sambuc unsigned typeSize = SemaRef.Context.getTypeSize(elementType);
1294*0a6a1f1dSLionel Sambuc
1295*0a6a1f1dSLionel Sambuc if (elementType->isFloatingType())
1296*0a6a1f1dSLionel Sambuc typeCode = "f";
1297*0a6a1f1dSLionel Sambuc else if (elementType->isSignedIntegerType())
1298*0a6a1f1dSLionel Sambuc typeCode = "s";
1299*0a6a1f1dSLionel Sambuc else if (elementType->isUnsignedIntegerType())
1300*0a6a1f1dSLionel Sambuc typeCode = "u";
1301*0a6a1f1dSLionel Sambuc else
1302*0a6a1f1dSLionel Sambuc llvm_unreachable("Invalid element type!");
1303*0a6a1f1dSLionel Sambuc
1304*0a6a1f1dSLionel Sambuc SemaRef.Diag(IList->getLocStart(),
1305*0a6a1f1dSLionel Sambuc SemaRef.Context.getTypeSize(VT) > 64 ?
1306*0a6a1f1dSLionel Sambuc diag::note_neon_vector_initializer_non_portable_q :
1307*0a6a1f1dSLionel Sambuc diag::note_neon_vector_initializer_non_portable)
1308*0a6a1f1dSLionel Sambuc << typeCode << typeSize;
1309*0a6a1f1dSLionel Sambuc }
1310*0a6a1f1dSLionel Sambuc
1311f4a2713aSLionel Sambuc return;
1312f4a2713aSLionel Sambuc }
1313f4a2713aSLionel Sambuc
1314f4a2713aSLionel Sambuc InitializedEntity ElementEntity =
1315f4a2713aSLionel Sambuc InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
1316f4a2713aSLionel Sambuc
1317f4a2713aSLionel Sambuc // OpenCL initializers allows vectors to be constructed from vectors.
1318f4a2713aSLionel Sambuc for (unsigned i = 0; i < maxElements; ++i) {
1319f4a2713aSLionel Sambuc // Don't attempt to go past the end of the init list
1320f4a2713aSLionel Sambuc if (Index >= IList->getNumInits())
1321f4a2713aSLionel Sambuc break;
1322f4a2713aSLionel Sambuc
1323f4a2713aSLionel Sambuc ElementEntity.setElementIndex(Index);
1324f4a2713aSLionel Sambuc
1325f4a2713aSLionel Sambuc QualType IType = IList->getInit(Index)->getType();
1326f4a2713aSLionel Sambuc if (!IType->isVectorType()) {
1327f4a2713aSLionel Sambuc CheckSubElementType(ElementEntity, IList, elementType, Index,
1328f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
1329f4a2713aSLionel Sambuc ++numEltsInit;
1330f4a2713aSLionel Sambuc } else {
1331f4a2713aSLionel Sambuc QualType VecType;
1332f4a2713aSLionel Sambuc const VectorType *IVT = IType->getAs<VectorType>();
1333f4a2713aSLionel Sambuc unsigned numIElts = IVT->getNumElements();
1334f4a2713aSLionel Sambuc
1335f4a2713aSLionel Sambuc if (IType->isExtVectorType())
1336f4a2713aSLionel Sambuc VecType = SemaRef.Context.getExtVectorType(elementType, numIElts);
1337f4a2713aSLionel Sambuc else
1338f4a2713aSLionel Sambuc VecType = SemaRef.Context.getVectorType(elementType, numIElts,
1339f4a2713aSLionel Sambuc IVT->getVectorKind());
1340f4a2713aSLionel Sambuc CheckSubElementType(ElementEntity, IList, VecType, Index,
1341f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
1342f4a2713aSLionel Sambuc numEltsInit += numIElts;
1343f4a2713aSLionel Sambuc }
1344f4a2713aSLionel Sambuc }
1345f4a2713aSLionel Sambuc
1346f4a2713aSLionel Sambuc // OpenCL requires all elements to be initialized.
1347f4a2713aSLionel Sambuc if (numEltsInit != maxElements) {
1348f4a2713aSLionel Sambuc if (!VerifyOnly)
1349f4a2713aSLionel Sambuc SemaRef.Diag(IList->getLocStart(),
1350f4a2713aSLionel Sambuc diag::err_vector_incorrect_num_initializers)
1351f4a2713aSLionel Sambuc << (numEltsInit < maxElements) << maxElements << numEltsInit;
1352f4a2713aSLionel Sambuc hadError = true;
1353f4a2713aSLionel Sambuc }
1354f4a2713aSLionel Sambuc }
1355f4a2713aSLionel Sambuc
CheckArrayType(const InitializedEntity & Entity,InitListExpr * IList,QualType & DeclType,llvm::APSInt elementIndex,bool SubobjectIsDesignatorContext,unsigned & Index,InitListExpr * StructuredList,unsigned & StructuredIndex)1356f4a2713aSLionel Sambuc void InitListChecker::CheckArrayType(const InitializedEntity &Entity,
1357f4a2713aSLionel Sambuc InitListExpr *IList, QualType &DeclType,
1358f4a2713aSLionel Sambuc llvm::APSInt elementIndex,
1359f4a2713aSLionel Sambuc bool SubobjectIsDesignatorContext,
1360f4a2713aSLionel Sambuc unsigned &Index,
1361f4a2713aSLionel Sambuc InitListExpr *StructuredList,
1362f4a2713aSLionel Sambuc unsigned &StructuredIndex) {
1363f4a2713aSLionel Sambuc const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType);
1364f4a2713aSLionel Sambuc
1365f4a2713aSLionel Sambuc // Check for the special-case of initializing an array with a string.
1366f4a2713aSLionel Sambuc if (Index < IList->getNumInits()) {
1367f4a2713aSLionel Sambuc if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) ==
1368f4a2713aSLionel Sambuc SIF_None) {
1369f4a2713aSLionel Sambuc // We place the string literal directly into the resulting
1370f4a2713aSLionel Sambuc // initializer list. This is the only place where the structure
1371f4a2713aSLionel Sambuc // of the structured initializer list doesn't match exactly,
1372f4a2713aSLionel Sambuc // because doing so would involve allocating one character
1373f4a2713aSLionel Sambuc // constant for each string.
1374f4a2713aSLionel Sambuc if (!VerifyOnly) {
1375f4a2713aSLionel Sambuc CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef);
1376f4a2713aSLionel Sambuc UpdateStructuredListElement(StructuredList, StructuredIndex,
1377f4a2713aSLionel Sambuc IList->getInit(Index));
1378f4a2713aSLionel Sambuc StructuredList->resizeInits(SemaRef.Context, StructuredIndex);
1379f4a2713aSLionel Sambuc }
1380f4a2713aSLionel Sambuc ++Index;
1381f4a2713aSLionel Sambuc return;
1382f4a2713aSLionel Sambuc }
1383f4a2713aSLionel Sambuc }
1384f4a2713aSLionel Sambuc if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) {
1385f4a2713aSLionel Sambuc // Check for VLAs; in standard C it would be possible to check this
1386f4a2713aSLionel Sambuc // earlier, but I don't know where clang accepts VLAs (gcc accepts
1387f4a2713aSLionel Sambuc // them in all sorts of strange places).
1388f4a2713aSLionel Sambuc if (!VerifyOnly)
1389f4a2713aSLionel Sambuc SemaRef.Diag(VAT->getSizeExpr()->getLocStart(),
1390f4a2713aSLionel Sambuc diag::err_variable_object_no_init)
1391f4a2713aSLionel Sambuc << VAT->getSizeExpr()->getSourceRange();
1392f4a2713aSLionel Sambuc hadError = true;
1393f4a2713aSLionel Sambuc ++Index;
1394f4a2713aSLionel Sambuc ++StructuredIndex;
1395f4a2713aSLionel Sambuc return;
1396f4a2713aSLionel Sambuc }
1397f4a2713aSLionel Sambuc
1398f4a2713aSLionel Sambuc // We might know the maximum number of elements in advance.
1399f4a2713aSLionel Sambuc llvm::APSInt maxElements(elementIndex.getBitWidth(),
1400f4a2713aSLionel Sambuc elementIndex.isUnsigned());
1401f4a2713aSLionel Sambuc bool maxElementsKnown = false;
1402f4a2713aSLionel Sambuc if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) {
1403f4a2713aSLionel Sambuc maxElements = CAT->getSize();
1404f4a2713aSLionel Sambuc elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth());
1405f4a2713aSLionel Sambuc elementIndex.setIsUnsigned(maxElements.isUnsigned());
1406f4a2713aSLionel Sambuc maxElementsKnown = true;
1407f4a2713aSLionel Sambuc }
1408f4a2713aSLionel Sambuc
1409f4a2713aSLionel Sambuc QualType elementType = arrayType->getElementType();
1410f4a2713aSLionel Sambuc while (Index < IList->getNumInits()) {
1411f4a2713aSLionel Sambuc Expr *Init = IList->getInit(Index);
1412f4a2713aSLionel Sambuc if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
1413f4a2713aSLionel Sambuc // If we're not the subobject that matches up with the '{' for
1414f4a2713aSLionel Sambuc // the designator, we shouldn't be handling the
1415f4a2713aSLionel Sambuc // designator. Return immediately.
1416f4a2713aSLionel Sambuc if (!SubobjectIsDesignatorContext)
1417f4a2713aSLionel Sambuc return;
1418f4a2713aSLionel Sambuc
1419f4a2713aSLionel Sambuc // Handle this designated initializer. elementIndex will be
1420f4a2713aSLionel Sambuc // updated to be the next array element we'll initialize.
1421f4a2713aSLionel Sambuc if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
1422*0a6a1f1dSLionel Sambuc DeclType, nullptr, &elementIndex, Index,
1423f4a2713aSLionel Sambuc StructuredList, StructuredIndex, true,
1424f4a2713aSLionel Sambuc false)) {
1425f4a2713aSLionel Sambuc hadError = true;
1426f4a2713aSLionel Sambuc continue;
1427f4a2713aSLionel Sambuc }
1428f4a2713aSLionel Sambuc
1429f4a2713aSLionel Sambuc if (elementIndex.getBitWidth() > maxElements.getBitWidth())
1430f4a2713aSLionel Sambuc maxElements = maxElements.extend(elementIndex.getBitWidth());
1431f4a2713aSLionel Sambuc else if (elementIndex.getBitWidth() < maxElements.getBitWidth())
1432f4a2713aSLionel Sambuc elementIndex = elementIndex.extend(maxElements.getBitWidth());
1433f4a2713aSLionel Sambuc elementIndex.setIsUnsigned(maxElements.isUnsigned());
1434f4a2713aSLionel Sambuc
1435f4a2713aSLionel Sambuc // If the array is of incomplete type, keep track of the number of
1436f4a2713aSLionel Sambuc // elements in the initializer.
1437f4a2713aSLionel Sambuc if (!maxElementsKnown && elementIndex > maxElements)
1438f4a2713aSLionel Sambuc maxElements = elementIndex;
1439f4a2713aSLionel Sambuc
1440f4a2713aSLionel Sambuc continue;
1441f4a2713aSLionel Sambuc }
1442f4a2713aSLionel Sambuc
1443f4a2713aSLionel Sambuc // If we know the maximum number of elements, and we've already
1444f4a2713aSLionel Sambuc // hit it, stop consuming elements in the initializer list.
1445f4a2713aSLionel Sambuc if (maxElementsKnown && elementIndex == maxElements)
1446f4a2713aSLionel Sambuc break;
1447f4a2713aSLionel Sambuc
1448f4a2713aSLionel Sambuc InitializedEntity ElementEntity =
1449f4a2713aSLionel Sambuc InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex,
1450f4a2713aSLionel Sambuc Entity);
1451f4a2713aSLionel Sambuc // Check this element.
1452f4a2713aSLionel Sambuc CheckSubElementType(ElementEntity, IList, elementType, Index,
1453f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
1454f4a2713aSLionel Sambuc ++elementIndex;
1455f4a2713aSLionel Sambuc
1456f4a2713aSLionel Sambuc // If the array is of incomplete type, keep track of the number of
1457f4a2713aSLionel Sambuc // elements in the initializer.
1458f4a2713aSLionel Sambuc if (!maxElementsKnown && elementIndex > maxElements)
1459f4a2713aSLionel Sambuc maxElements = elementIndex;
1460f4a2713aSLionel Sambuc }
1461f4a2713aSLionel Sambuc if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) {
1462f4a2713aSLionel Sambuc // If this is an incomplete array type, the actual type needs to
1463f4a2713aSLionel Sambuc // be calculated here.
1464f4a2713aSLionel Sambuc llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned());
1465f4a2713aSLionel Sambuc if (maxElements == Zero) {
1466f4a2713aSLionel Sambuc // Sizing an array implicitly to zero is not allowed by ISO C,
1467f4a2713aSLionel Sambuc // but is supported by GNU.
1468f4a2713aSLionel Sambuc SemaRef.Diag(IList->getLocStart(),
1469f4a2713aSLionel Sambuc diag::ext_typecheck_zero_array_size);
1470f4a2713aSLionel Sambuc }
1471f4a2713aSLionel Sambuc
1472f4a2713aSLionel Sambuc DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements,
1473f4a2713aSLionel Sambuc ArrayType::Normal, 0);
1474f4a2713aSLionel Sambuc }
1475f4a2713aSLionel Sambuc if (!hadError && VerifyOnly) {
1476f4a2713aSLionel Sambuc // Check if there are any members of the array that get value-initialized.
1477f4a2713aSLionel Sambuc // If so, check if doing that is possible.
1478f4a2713aSLionel Sambuc // FIXME: This needs to detect holes left by designated initializers too.
1479f4a2713aSLionel Sambuc if (maxElementsKnown && elementIndex < maxElements)
1480*0a6a1f1dSLionel Sambuc CheckEmptyInitializable(InitializedEntity::InitializeElement(
1481*0a6a1f1dSLionel Sambuc SemaRef.Context, 0, Entity),
1482*0a6a1f1dSLionel Sambuc IList->getLocEnd());
1483f4a2713aSLionel Sambuc }
1484f4a2713aSLionel Sambuc }
1485f4a2713aSLionel Sambuc
CheckFlexibleArrayInit(const InitializedEntity & Entity,Expr * InitExpr,FieldDecl * Field,bool TopLevelObject)1486f4a2713aSLionel Sambuc bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity,
1487f4a2713aSLionel Sambuc Expr *InitExpr,
1488f4a2713aSLionel Sambuc FieldDecl *Field,
1489f4a2713aSLionel Sambuc bool TopLevelObject) {
1490f4a2713aSLionel Sambuc // Handle GNU flexible array initializers.
1491f4a2713aSLionel Sambuc unsigned FlexArrayDiag;
1492f4a2713aSLionel Sambuc if (isa<InitListExpr>(InitExpr) &&
1493f4a2713aSLionel Sambuc cast<InitListExpr>(InitExpr)->getNumInits() == 0) {
1494f4a2713aSLionel Sambuc // Empty flexible array init always allowed as an extension
1495f4a2713aSLionel Sambuc FlexArrayDiag = diag::ext_flexible_array_init;
1496f4a2713aSLionel Sambuc } else if (SemaRef.getLangOpts().CPlusPlus) {
1497f4a2713aSLionel Sambuc // Disallow flexible array init in C++; it is not required for gcc
1498f4a2713aSLionel Sambuc // compatibility, and it needs work to IRGen correctly in general.
1499f4a2713aSLionel Sambuc FlexArrayDiag = diag::err_flexible_array_init;
1500f4a2713aSLionel Sambuc } else if (!TopLevelObject) {
1501f4a2713aSLionel Sambuc // Disallow flexible array init on non-top-level object
1502f4a2713aSLionel Sambuc FlexArrayDiag = diag::err_flexible_array_init;
1503f4a2713aSLionel Sambuc } else if (Entity.getKind() != InitializedEntity::EK_Variable) {
1504f4a2713aSLionel Sambuc // Disallow flexible array init on anything which is not a variable.
1505f4a2713aSLionel Sambuc FlexArrayDiag = diag::err_flexible_array_init;
1506f4a2713aSLionel Sambuc } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) {
1507f4a2713aSLionel Sambuc // Disallow flexible array init on local variables.
1508f4a2713aSLionel Sambuc FlexArrayDiag = diag::err_flexible_array_init;
1509f4a2713aSLionel Sambuc } else {
1510f4a2713aSLionel Sambuc // Allow other cases.
1511f4a2713aSLionel Sambuc FlexArrayDiag = diag::ext_flexible_array_init;
1512f4a2713aSLionel Sambuc }
1513f4a2713aSLionel Sambuc
1514f4a2713aSLionel Sambuc if (!VerifyOnly) {
1515f4a2713aSLionel Sambuc SemaRef.Diag(InitExpr->getLocStart(),
1516f4a2713aSLionel Sambuc FlexArrayDiag)
1517f4a2713aSLionel Sambuc << InitExpr->getLocStart();
1518f4a2713aSLionel Sambuc SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
1519f4a2713aSLionel Sambuc << Field;
1520f4a2713aSLionel Sambuc }
1521f4a2713aSLionel Sambuc
1522f4a2713aSLionel Sambuc return FlexArrayDiag != diag::ext_flexible_array_init;
1523f4a2713aSLionel Sambuc }
1524f4a2713aSLionel Sambuc
CheckStructUnionTypes(const InitializedEntity & Entity,InitListExpr * IList,QualType DeclType,RecordDecl::field_iterator Field,bool SubobjectIsDesignatorContext,unsigned & Index,InitListExpr * StructuredList,unsigned & StructuredIndex,bool TopLevelObject)1525f4a2713aSLionel Sambuc void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity,
1526f4a2713aSLionel Sambuc InitListExpr *IList,
1527f4a2713aSLionel Sambuc QualType DeclType,
1528f4a2713aSLionel Sambuc RecordDecl::field_iterator Field,
1529f4a2713aSLionel Sambuc bool SubobjectIsDesignatorContext,
1530f4a2713aSLionel Sambuc unsigned &Index,
1531f4a2713aSLionel Sambuc InitListExpr *StructuredList,
1532f4a2713aSLionel Sambuc unsigned &StructuredIndex,
1533f4a2713aSLionel Sambuc bool TopLevelObject) {
1534f4a2713aSLionel Sambuc RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl();
1535f4a2713aSLionel Sambuc
1536f4a2713aSLionel Sambuc // If the record is invalid, some of it's members are invalid. To avoid
1537f4a2713aSLionel Sambuc // confusion, we forgo checking the intializer for the entire record.
1538f4a2713aSLionel Sambuc if (structDecl->isInvalidDecl()) {
1539f4a2713aSLionel Sambuc // Assume it was supposed to consume a single initializer.
1540f4a2713aSLionel Sambuc ++Index;
1541f4a2713aSLionel Sambuc hadError = true;
1542f4a2713aSLionel Sambuc return;
1543f4a2713aSLionel Sambuc }
1544f4a2713aSLionel Sambuc
1545f4a2713aSLionel Sambuc if (DeclType->isUnionType() && IList->getNumInits() == 0) {
1546f4a2713aSLionel Sambuc RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
1547f4a2713aSLionel Sambuc
1548f4a2713aSLionel Sambuc // If there's a default initializer, use it.
1549f4a2713aSLionel Sambuc if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) {
1550f4a2713aSLionel Sambuc if (VerifyOnly)
1551f4a2713aSLionel Sambuc return;
1552f4a2713aSLionel Sambuc for (RecordDecl::field_iterator FieldEnd = RD->field_end();
1553f4a2713aSLionel Sambuc Field != FieldEnd; ++Field) {
1554f4a2713aSLionel Sambuc if (Field->hasInClassInitializer()) {
1555f4a2713aSLionel Sambuc StructuredList->setInitializedFieldInUnion(*Field);
1556f4a2713aSLionel Sambuc // FIXME: Actually build a CXXDefaultInitExpr?
1557f4a2713aSLionel Sambuc return;
1558f4a2713aSLionel Sambuc }
1559f4a2713aSLionel Sambuc }
1560f4a2713aSLionel Sambuc }
1561f4a2713aSLionel Sambuc
1562*0a6a1f1dSLionel Sambuc // Value-initialize the first member of the union that isn't an unnamed
1563*0a6a1f1dSLionel Sambuc // bitfield.
1564f4a2713aSLionel Sambuc for (RecordDecl::field_iterator FieldEnd = RD->field_end();
1565f4a2713aSLionel Sambuc Field != FieldEnd; ++Field) {
1566*0a6a1f1dSLionel Sambuc if (!Field->isUnnamedBitfield()) {
1567f4a2713aSLionel Sambuc if (VerifyOnly)
1568*0a6a1f1dSLionel Sambuc CheckEmptyInitializable(
1569*0a6a1f1dSLionel Sambuc InitializedEntity::InitializeMember(*Field, &Entity),
1570*0a6a1f1dSLionel Sambuc IList->getLocEnd());
1571f4a2713aSLionel Sambuc else
1572f4a2713aSLionel Sambuc StructuredList->setInitializedFieldInUnion(*Field);
1573f4a2713aSLionel Sambuc break;
1574f4a2713aSLionel Sambuc }
1575f4a2713aSLionel Sambuc }
1576f4a2713aSLionel Sambuc return;
1577f4a2713aSLionel Sambuc }
1578f4a2713aSLionel Sambuc
1579f4a2713aSLionel Sambuc // If structDecl is a forward declaration, this loop won't do
1580f4a2713aSLionel Sambuc // anything except look at designated initializers; That's okay,
1581f4a2713aSLionel Sambuc // because an error should get printed out elsewhere. It might be
1582f4a2713aSLionel Sambuc // worthwhile to skip over the rest of the initializer, though.
1583f4a2713aSLionel Sambuc RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl();
1584f4a2713aSLionel Sambuc RecordDecl::field_iterator FieldEnd = RD->field_end();
1585f4a2713aSLionel Sambuc bool InitializedSomething = false;
1586f4a2713aSLionel Sambuc bool CheckForMissingFields = true;
1587f4a2713aSLionel Sambuc while (Index < IList->getNumInits()) {
1588f4a2713aSLionel Sambuc Expr *Init = IList->getInit(Index);
1589f4a2713aSLionel Sambuc
1590f4a2713aSLionel Sambuc if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) {
1591f4a2713aSLionel Sambuc // If we're not the subobject that matches up with the '{' for
1592f4a2713aSLionel Sambuc // the designator, we shouldn't be handling the
1593f4a2713aSLionel Sambuc // designator. Return immediately.
1594f4a2713aSLionel Sambuc if (!SubobjectIsDesignatorContext)
1595f4a2713aSLionel Sambuc return;
1596f4a2713aSLionel Sambuc
1597f4a2713aSLionel Sambuc // Handle this designated initializer. Field will be updated to
1598f4a2713aSLionel Sambuc // the next field that we'll be initializing.
1599f4a2713aSLionel Sambuc if (CheckDesignatedInitializer(Entity, IList, DIE, 0,
1600*0a6a1f1dSLionel Sambuc DeclType, &Field, nullptr, Index,
1601f4a2713aSLionel Sambuc StructuredList, StructuredIndex,
1602f4a2713aSLionel Sambuc true, TopLevelObject))
1603f4a2713aSLionel Sambuc hadError = true;
1604f4a2713aSLionel Sambuc
1605f4a2713aSLionel Sambuc InitializedSomething = true;
1606f4a2713aSLionel Sambuc
1607f4a2713aSLionel Sambuc // Disable check for missing fields when designators are used.
1608f4a2713aSLionel Sambuc // This matches gcc behaviour.
1609f4a2713aSLionel Sambuc CheckForMissingFields = false;
1610f4a2713aSLionel Sambuc continue;
1611f4a2713aSLionel Sambuc }
1612f4a2713aSLionel Sambuc
1613f4a2713aSLionel Sambuc if (Field == FieldEnd) {
1614f4a2713aSLionel Sambuc // We've run out of fields. We're done.
1615f4a2713aSLionel Sambuc break;
1616f4a2713aSLionel Sambuc }
1617f4a2713aSLionel Sambuc
1618f4a2713aSLionel Sambuc // We've already initialized a member of a union. We're done.
1619f4a2713aSLionel Sambuc if (InitializedSomething && DeclType->isUnionType())
1620f4a2713aSLionel Sambuc break;
1621f4a2713aSLionel Sambuc
1622f4a2713aSLionel Sambuc // If we've hit the flexible array member at the end, we're done.
1623f4a2713aSLionel Sambuc if (Field->getType()->isIncompleteArrayType())
1624f4a2713aSLionel Sambuc break;
1625f4a2713aSLionel Sambuc
1626f4a2713aSLionel Sambuc if (Field->isUnnamedBitfield()) {
1627f4a2713aSLionel Sambuc // Don't initialize unnamed bitfields, e.g. "int : 20;"
1628f4a2713aSLionel Sambuc ++Field;
1629f4a2713aSLionel Sambuc continue;
1630f4a2713aSLionel Sambuc }
1631f4a2713aSLionel Sambuc
1632f4a2713aSLionel Sambuc // Make sure we can use this declaration.
1633f4a2713aSLionel Sambuc bool InvalidUse;
1634f4a2713aSLionel Sambuc if (VerifyOnly)
1635f4a2713aSLionel Sambuc InvalidUse = !SemaRef.CanUseDecl(*Field);
1636f4a2713aSLionel Sambuc else
1637f4a2713aSLionel Sambuc InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field,
1638f4a2713aSLionel Sambuc IList->getInit(Index)->getLocStart());
1639f4a2713aSLionel Sambuc if (InvalidUse) {
1640f4a2713aSLionel Sambuc ++Index;
1641f4a2713aSLionel Sambuc ++Field;
1642f4a2713aSLionel Sambuc hadError = true;
1643f4a2713aSLionel Sambuc continue;
1644f4a2713aSLionel Sambuc }
1645f4a2713aSLionel Sambuc
1646f4a2713aSLionel Sambuc InitializedEntity MemberEntity =
1647f4a2713aSLionel Sambuc InitializedEntity::InitializeMember(*Field, &Entity);
1648f4a2713aSLionel Sambuc CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
1649f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
1650f4a2713aSLionel Sambuc InitializedSomething = true;
1651f4a2713aSLionel Sambuc
1652f4a2713aSLionel Sambuc if (DeclType->isUnionType() && !VerifyOnly) {
1653f4a2713aSLionel Sambuc // Initialize the first field within the union.
1654f4a2713aSLionel Sambuc StructuredList->setInitializedFieldInUnion(*Field);
1655f4a2713aSLionel Sambuc }
1656f4a2713aSLionel Sambuc
1657f4a2713aSLionel Sambuc ++Field;
1658f4a2713aSLionel Sambuc }
1659f4a2713aSLionel Sambuc
1660f4a2713aSLionel Sambuc // Emit warnings for missing struct field initializers.
1661f4a2713aSLionel Sambuc if (!VerifyOnly && InitializedSomething && CheckForMissingFields &&
1662f4a2713aSLionel Sambuc Field != FieldEnd && !Field->getType()->isIncompleteArrayType() &&
1663f4a2713aSLionel Sambuc !DeclType->isUnionType()) {
1664f4a2713aSLionel Sambuc // It is possible we have one or more unnamed bitfields remaining.
1665f4a2713aSLionel Sambuc // Find first (if any) named field and emit warning.
1666f4a2713aSLionel Sambuc for (RecordDecl::field_iterator it = Field, end = RD->field_end();
1667f4a2713aSLionel Sambuc it != end; ++it) {
1668f4a2713aSLionel Sambuc if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) {
1669f4a2713aSLionel Sambuc SemaRef.Diag(IList->getSourceRange().getEnd(),
1670*0a6a1f1dSLionel Sambuc diag::warn_missing_field_initializers) << *it;
1671f4a2713aSLionel Sambuc break;
1672f4a2713aSLionel Sambuc }
1673f4a2713aSLionel Sambuc }
1674f4a2713aSLionel Sambuc }
1675f4a2713aSLionel Sambuc
1676f4a2713aSLionel Sambuc // Check that any remaining fields can be value-initialized.
1677f4a2713aSLionel Sambuc if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() &&
1678f4a2713aSLionel Sambuc !Field->getType()->isIncompleteArrayType()) {
1679f4a2713aSLionel Sambuc // FIXME: Should check for holes left by designated initializers too.
1680f4a2713aSLionel Sambuc for (; Field != FieldEnd && !hadError; ++Field) {
1681f4a2713aSLionel Sambuc if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer())
1682*0a6a1f1dSLionel Sambuc CheckEmptyInitializable(
1683*0a6a1f1dSLionel Sambuc InitializedEntity::InitializeMember(*Field, &Entity),
1684*0a6a1f1dSLionel Sambuc IList->getLocEnd());
1685f4a2713aSLionel Sambuc }
1686f4a2713aSLionel Sambuc }
1687f4a2713aSLionel Sambuc
1688f4a2713aSLionel Sambuc if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() ||
1689f4a2713aSLionel Sambuc Index >= IList->getNumInits())
1690f4a2713aSLionel Sambuc return;
1691f4a2713aSLionel Sambuc
1692f4a2713aSLionel Sambuc if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field,
1693f4a2713aSLionel Sambuc TopLevelObject)) {
1694f4a2713aSLionel Sambuc hadError = true;
1695f4a2713aSLionel Sambuc ++Index;
1696f4a2713aSLionel Sambuc return;
1697f4a2713aSLionel Sambuc }
1698f4a2713aSLionel Sambuc
1699f4a2713aSLionel Sambuc InitializedEntity MemberEntity =
1700f4a2713aSLionel Sambuc InitializedEntity::InitializeMember(*Field, &Entity);
1701f4a2713aSLionel Sambuc
1702f4a2713aSLionel Sambuc if (isa<InitListExpr>(IList->getInit(Index)))
1703f4a2713aSLionel Sambuc CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
1704f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
1705f4a2713aSLionel Sambuc else
1706f4a2713aSLionel Sambuc CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index,
1707f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
1708f4a2713aSLionel Sambuc }
1709f4a2713aSLionel Sambuc
1710f4a2713aSLionel Sambuc /// \brief Expand a field designator that refers to a member of an
1711f4a2713aSLionel Sambuc /// anonymous struct or union into a series of field designators that
1712f4a2713aSLionel Sambuc /// refers to the field within the appropriate subobject.
1713f4a2713aSLionel Sambuc ///
ExpandAnonymousFieldDesignator(Sema & SemaRef,DesignatedInitExpr * DIE,unsigned DesigIdx,IndirectFieldDecl * IndirectField)1714f4a2713aSLionel Sambuc static void ExpandAnonymousFieldDesignator(Sema &SemaRef,
1715f4a2713aSLionel Sambuc DesignatedInitExpr *DIE,
1716f4a2713aSLionel Sambuc unsigned DesigIdx,
1717f4a2713aSLionel Sambuc IndirectFieldDecl *IndirectField) {
1718f4a2713aSLionel Sambuc typedef DesignatedInitExpr::Designator Designator;
1719f4a2713aSLionel Sambuc
1720f4a2713aSLionel Sambuc // Build the replacement designators.
1721f4a2713aSLionel Sambuc SmallVector<Designator, 4> Replacements;
1722f4a2713aSLionel Sambuc for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(),
1723f4a2713aSLionel Sambuc PE = IndirectField->chain_end(); PI != PE; ++PI) {
1724f4a2713aSLionel Sambuc if (PI + 1 == PE)
1725*0a6a1f1dSLionel Sambuc Replacements.push_back(Designator((IdentifierInfo *)nullptr,
1726f4a2713aSLionel Sambuc DIE->getDesignator(DesigIdx)->getDotLoc(),
1727f4a2713aSLionel Sambuc DIE->getDesignator(DesigIdx)->getFieldLoc()));
1728f4a2713aSLionel Sambuc else
1729*0a6a1f1dSLionel Sambuc Replacements.push_back(Designator((IdentifierInfo *)nullptr,
1730*0a6a1f1dSLionel Sambuc SourceLocation(), SourceLocation()));
1731f4a2713aSLionel Sambuc assert(isa<FieldDecl>(*PI));
1732f4a2713aSLionel Sambuc Replacements.back().setField(cast<FieldDecl>(*PI));
1733f4a2713aSLionel Sambuc }
1734f4a2713aSLionel Sambuc
1735f4a2713aSLionel Sambuc // Expand the current designator into the set of replacement
1736f4a2713aSLionel Sambuc // designators, so we have a full subobject path down to where the
1737f4a2713aSLionel Sambuc // member of the anonymous struct/union is actually stored.
1738f4a2713aSLionel Sambuc DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0],
1739f4a2713aSLionel Sambuc &Replacements[0] + Replacements.size());
1740f4a2713aSLionel Sambuc }
1741f4a2713aSLionel Sambuc
CloneDesignatedInitExpr(Sema & SemaRef,DesignatedInitExpr * DIE)1742f4a2713aSLionel Sambuc static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef,
1743f4a2713aSLionel Sambuc DesignatedInitExpr *DIE) {
1744f4a2713aSLionel Sambuc unsigned NumIndexExprs = DIE->getNumSubExprs() - 1;
1745f4a2713aSLionel Sambuc SmallVector<Expr*, 4> IndexExprs(NumIndexExprs);
1746f4a2713aSLionel Sambuc for (unsigned I = 0; I < NumIndexExprs; ++I)
1747f4a2713aSLionel Sambuc IndexExprs[I] = DIE->getSubExpr(I + 1);
1748f4a2713aSLionel Sambuc return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(),
1749f4a2713aSLionel Sambuc DIE->size(), IndexExprs,
1750f4a2713aSLionel Sambuc DIE->getEqualOrColonLoc(),
1751f4a2713aSLionel Sambuc DIE->usesGNUSyntax(), DIE->getInit());
1752f4a2713aSLionel Sambuc }
1753f4a2713aSLionel Sambuc
1754f4a2713aSLionel Sambuc namespace {
1755f4a2713aSLionel Sambuc
1756f4a2713aSLionel Sambuc // Callback to only accept typo corrections that are for field members of
1757f4a2713aSLionel Sambuc // the given struct or union.
1758f4a2713aSLionel Sambuc class FieldInitializerValidatorCCC : public CorrectionCandidateCallback {
1759f4a2713aSLionel Sambuc public:
FieldInitializerValidatorCCC(RecordDecl * RD)1760f4a2713aSLionel Sambuc explicit FieldInitializerValidatorCCC(RecordDecl *RD)
1761f4a2713aSLionel Sambuc : Record(RD) {}
1762f4a2713aSLionel Sambuc
ValidateCandidate(const TypoCorrection & candidate)1763*0a6a1f1dSLionel Sambuc bool ValidateCandidate(const TypoCorrection &candidate) override {
1764f4a2713aSLionel Sambuc FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>();
1765f4a2713aSLionel Sambuc return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record);
1766f4a2713aSLionel Sambuc }
1767f4a2713aSLionel Sambuc
1768f4a2713aSLionel Sambuc private:
1769f4a2713aSLionel Sambuc RecordDecl *Record;
1770f4a2713aSLionel Sambuc };
1771f4a2713aSLionel Sambuc
1772f4a2713aSLionel Sambuc }
1773f4a2713aSLionel Sambuc
1774f4a2713aSLionel Sambuc /// @brief Check the well-formedness of a C99 designated initializer.
1775f4a2713aSLionel Sambuc ///
1776f4a2713aSLionel Sambuc /// Determines whether the designated initializer @p DIE, which
1777f4a2713aSLionel Sambuc /// resides at the given @p Index within the initializer list @p
1778f4a2713aSLionel Sambuc /// IList, is well-formed for a current object of type @p DeclType
1779f4a2713aSLionel Sambuc /// (C99 6.7.8). The actual subobject that this designator refers to
1780f4a2713aSLionel Sambuc /// within the current subobject is returned in either
1781f4a2713aSLionel Sambuc /// @p NextField or @p NextElementIndex (whichever is appropriate).
1782f4a2713aSLionel Sambuc ///
1783f4a2713aSLionel Sambuc /// @param IList The initializer list in which this designated
1784f4a2713aSLionel Sambuc /// initializer occurs.
1785f4a2713aSLionel Sambuc ///
1786f4a2713aSLionel Sambuc /// @param DIE The designated initializer expression.
1787f4a2713aSLionel Sambuc ///
1788f4a2713aSLionel Sambuc /// @param DesigIdx The index of the current designator.
1789f4a2713aSLionel Sambuc ///
1790f4a2713aSLionel Sambuc /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17),
1791f4a2713aSLionel Sambuc /// into which the designation in @p DIE should refer.
1792f4a2713aSLionel Sambuc ///
1793f4a2713aSLionel Sambuc /// @param NextField If non-NULL and the first designator in @p DIE is
1794f4a2713aSLionel Sambuc /// a field, this will be set to the field declaration corresponding
1795f4a2713aSLionel Sambuc /// to the field named by the designator.
1796f4a2713aSLionel Sambuc ///
1797f4a2713aSLionel Sambuc /// @param NextElementIndex If non-NULL and the first designator in @p
1798f4a2713aSLionel Sambuc /// DIE is an array designator or GNU array-range designator, this
1799f4a2713aSLionel Sambuc /// will be set to the last index initialized by this designator.
1800f4a2713aSLionel Sambuc ///
1801f4a2713aSLionel Sambuc /// @param Index Index into @p IList where the designated initializer
1802f4a2713aSLionel Sambuc /// @p DIE occurs.
1803f4a2713aSLionel Sambuc ///
1804f4a2713aSLionel Sambuc /// @param StructuredList The initializer list expression that
1805f4a2713aSLionel Sambuc /// describes all of the subobject initializers in the order they'll
1806f4a2713aSLionel Sambuc /// actually be initialized.
1807f4a2713aSLionel Sambuc ///
1808f4a2713aSLionel Sambuc /// @returns true if there was an error, false otherwise.
1809f4a2713aSLionel Sambuc bool
CheckDesignatedInitializer(const InitializedEntity & Entity,InitListExpr * IList,DesignatedInitExpr * DIE,unsigned DesigIdx,QualType & CurrentObjectType,RecordDecl::field_iterator * NextField,llvm::APSInt * NextElementIndex,unsigned & Index,InitListExpr * StructuredList,unsigned & StructuredIndex,bool FinishSubobjectInit,bool TopLevelObject)1810f4a2713aSLionel Sambuc InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity,
1811f4a2713aSLionel Sambuc InitListExpr *IList,
1812f4a2713aSLionel Sambuc DesignatedInitExpr *DIE,
1813f4a2713aSLionel Sambuc unsigned DesigIdx,
1814f4a2713aSLionel Sambuc QualType &CurrentObjectType,
1815f4a2713aSLionel Sambuc RecordDecl::field_iterator *NextField,
1816f4a2713aSLionel Sambuc llvm::APSInt *NextElementIndex,
1817f4a2713aSLionel Sambuc unsigned &Index,
1818f4a2713aSLionel Sambuc InitListExpr *StructuredList,
1819f4a2713aSLionel Sambuc unsigned &StructuredIndex,
1820f4a2713aSLionel Sambuc bool FinishSubobjectInit,
1821f4a2713aSLionel Sambuc bool TopLevelObject) {
1822f4a2713aSLionel Sambuc if (DesigIdx == DIE->size()) {
1823f4a2713aSLionel Sambuc // Check the actual initialization for the designated object type.
1824f4a2713aSLionel Sambuc bool prevHadError = hadError;
1825f4a2713aSLionel Sambuc
1826f4a2713aSLionel Sambuc // Temporarily remove the designator expression from the
1827f4a2713aSLionel Sambuc // initializer list that the child calls see, so that we don't try
1828f4a2713aSLionel Sambuc // to re-process the designator.
1829f4a2713aSLionel Sambuc unsigned OldIndex = Index;
1830f4a2713aSLionel Sambuc IList->setInit(OldIndex, DIE->getInit());
1831f4a2713aSLionel Sambuc
1832f4a2713aSLionel Sambuc CheckSubElementType(Entity, IList, CurrentObjectType, Index,
1833f4a2713aSLionel Sambuc StructuredList, StructuredIndex);
1834f4a2713aSLionel Sambuc
1835f4a2713aSLionel Sambuc // Restore the designated initializer expression in the syntactic
1836f4a2713aSLionel Sambuc // form of the initializer list.
1837f4a2713aSLionel Sambuc if (IList->getInit(OldIndex) != DIE->getInit())
1838f4a2713aSLionel Sambuc DIE->setInit(IList->getInit(OldIndex));
1839f4a2713aSLionel Sambuc IList->setInit(OldIndex, DIE);
1840f4a2713aSLionel Sambuc
1841f4a2713aSLionel Sambuc return hadError && !prevHadError;
1842f4a2713aSLionel Sambuc }
1843f4a2713aSLionel Sambuc
1844f4a2713aSLionel Sambuc DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx);
1845f4a2713aSLionel Sambuc bool IsFirstDesignator = (DesigIdx == 0);
1846f4a2713aSLionel Sambuc if (!VerifyOnly) {
1847f4a2713aSLionel Sambuc assert((IsFirstDesignator || StructuredList) &&
1848f4a2713aSLionel Sambuc "Need a non-designated initializer list to start from");
1849f4a2713aSLionel Sambuc
1850f4a2713aSLionel Sambuc // Determine the structural initializer list that corresponds to the
1851f4a2713aSLionel Sambuc // current subobject.
1852f4a2713aSLionel Sambuc StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList)
1853f4a2713aSLionel Sambuc : getStructuredSubobjectInit(IList, Index, CurrentObjectType,
1854f4a2713aSLionel Sambuc StructuredList, StructuredIndex,
1855f4a2713aSLionel Sambuc SourceRange(D->getLocStart(),
1856f4a2713aSLionel Sambuc DIE->getLocEnd()));
1857f4a2713aSLionel Sambuc assert(StructuredList && "Expected a structured initializer list");
1858f4a2713aSLionel Sambuc }
1859f4a2713aSLionel Sambuc
1860f4a2713aSLionel Sambuc if (D->isFieldDesignator()) {
1861f4a2713aSLionel Sambuc // C99 6.7.8p7:
1862f4a2713aSLionel Sambuc //
1863f4a2713aSLionel Sambuc // If a designator has the form
1864f4a2713aSLionel Sambuc //
1865f4a2713aSLionel Sambuc // . identifier
1866f4a2713aSLionel Sambuc //
1867f4a2713aSLionel Sambuc // then the current object (defined below) shall have
1868f4a2713aSLionel Sambuc // structure or union type and the identifier shall be the
1869f4a2713aSLionel Sambuc // name of a member of that type.
1870f4a2713aSLionel Sambuc const RecordType *RT = CurrentObjectType->getAs<RecordType>();
1871f4a2713aSLionel Sambuc if (!RT) {
1872f4a2713aSLionel Sambuc SourceLocation Loc = D->getDotLoc();
1873f4a2713aSLionel Sambuc if (Loc.isInvalid())
1874f4a2713aSLionel Sambuc Loc = D->getFieldLoc();
1875f4a2713aSLionel Sambuc if (!VerifyOnly)
1876f4a2713aSLionel Sambuc SemaRef.Diag(Loc, diag::err_field_designator_non_aggr)
1877f4a2713aSLionel Sambuc << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType;
1878f4a2713aSLionel Sambuc ++Index;
1879f4a2713aSLionel Sambuc return true;
1880f4a2713aSLionel Sambuc }
1881f4a2713aSLionel Sambuc
1882f4a2713aSLionel Sambuc FieldDecl *KnownField = D->getField();
1883*0a6a1f1dSLionel Sambuc if (!KnownField) {
1884f4a2713aSLionel Sambuc IdentifierInfo *FieldName = D->getFieldName();
1885*0a6a1f1dSLionel Sambuc DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName);
1886*0a6a1f1dSLionel Sambuc for (NamedDecl *ND : Lookup) {
1887*0a6a1f1dSLionel Sambuc if (auto *FD = dyn_cast<FieldDecl>(ND)) {
1888*0a6a1f1dSLionel Sambuc KnownField = FD;
1889*0a6a1f1dSLionel Sambuc break;
1890*0a6a1f1dSLionel Sambuc }
1891*0a6a1f1dSLionel Sambuc if (auto *IFD = dyn_cast<IndirectFieldDecl>(ND)) {
1892f4a2713aSLionel Sambuc // In verify mode, don't modify the original.
1893f4a2713aSLionel Sambuc if (VerifyOnly)
1894f4a2713aSLionel Sambuc DIE = CloneDesignatedInitExpr(SemaRef, DIE);
1895*0a6a1f1dSLionel Sambuc ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IFD);
1896f4a2713aSLionel Sambuc D = DIE->getDesignator(DesigIdx);
1897*0a6a1f1dSLionel Sambuc KnownField = cast<FieldDecl>(*IFD->chain_begin());
1898f4a2713aSLionel Sambuc break;
1899f4a2713aSLionel Sambuc }
1900f4a2713aSLionel Sambuc }
1901*0a6a1f1dSLionel Sambuc if (!KnownField) {
1902f4a2713aSLionel Sambuc if (VerifyOnly) {
1903f4a2713aSLionel Sambuc ++Index;
1904f4a2713aSLionel Sambuc return true; // No typo correction when just trying this out.
1905f4a2713aSLionel Sambuc }
1906f4a2713aSLionel Sambuc
1907f4a2713aSLionel Sambuc // Name lookup found something, but it wasn't a field.
1908*0a6a1f1dSLionel Sambuc if (!Lookup.empty()) {
1909f4a2713aSLionel Sambuc SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield)
1910f4a2713aSLionel Sambuc << FieldName;
1911f4a2713aSLionel Sambuc SemaRef.Diag(Lookup.front()->getLocation(),
1912f4a2713aSLionel Sambuc diag::note_field_designator_found);
1913f4a2713aSLionel Sambuc ++Index;
1914f4a2713aSLionel Sambuc return true;
1915f4a2713aSLionel Sambuc }
1916f4a2713aSLionel Sambuc
1917*0a6a1f1dSLionel Sambuc // Name lookup didn't find anything.
1918*0a6a1f1dSLionel Sambuc // Determine whether this was a typo for another field name.
1919*0a6a1f1dSLionel Sambuc if (TypoCorrection Corrected = SemaRef.CorrectTypo(
1920*0a6a1f1dSLionel Sambuc DeclarationNameInfo(FieldName, D->getFieldLoc()),
1921*0a6a1f1dSLionel Sambuc Sema::LookupMemberName, /*Scope=*/nullptr, /*SS=*/nullptr,
1922*0a6a1f1dSLionel Sambuc llvm::make_unique<FieldInitializerValidatorCCC>(RT->getDecl()),
1923*0a6a1f1dSLionel Sambuc Sema::CTK_ErrorRecovery, RT->getDecl())) {
1924*0a6a1f1dSLionel Sambuc SemaRef.diagnoseTypo(
1925*0a6a1f1dSLionel Sambuc Corrected,
1926*0a6a1f1dSLionel Sambuc SemaRef.PDiag(diag::err_field_designator_unknown_suggest)
1927*0a6a1f1dSLionel Sambuc << FieldName << CurrentObjectType);
1928*0a6a1f1dSLionel Sambuc KnownField = Corrected.getCorrectionDeclAs<FieldDecl>();
1929*0a6a1f1dSLionel Sambuc hadError = true;
1930*0a6a1f1dSLionel Sambuc } else {
1931*0a6a1f1dSLionel Sambuc // Typo correction didn't find anything.
1932*0a6a1f1dSLionel Sambuc SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown)
1933*0a6a1f1dSLionel Sambuc << FieldName << CurrentObjectType;
1934*0a6a1f1dSLionel Sambuc ++Index;
1935*0a6a1f1dSLionel Sambuc return true;
1936*0a6a1f1dSLionel Sambuc }
1937*0a6a1f1dSLionel Sambuc }
1938*0a6a1f1dSLionel Sambuc }
1939*0a6a1f1dSLionel Sambuc
1940*0a6a1f1dSLionel Sambuc unsigned FieldIndex = 0;
1941*0a6a1f1dSLionel Sambuc for (auto *FI : RT->getDecl()->fields()) {
1942*0a6a1f1dSLionel Sambuc if (FI->isUnnamedBitfield())
1943f4a2713aSLionel Sambuc continue;
1944*0a6a1f1dSLionel Sambuc if (KnownField == FI)
1945f4a2713aSLionel Sambuc break;
1946f4a2713aSLionel Sambuc ++FieldIndex;
1947f4a2713aSLionel Sambuc }
1948*0a6a1f1dSLionel Sambuc
1949*0a6a1f1dSLionel Sambuc RecordDecl::field_iterator Field =
1950*0a6a1f1dSLionel Sambuc RecordDecl::field_iterator(DeclContext::decl_iterator(KnownField));
1951f4a2713aSLionel Sambuc
1952f4a2713aSLionel Sambuc // All of the fields of a union are located at the same place in
1953f4a2713aSLionel Sambuc // the initializer list.
1954f4a2713aSLionel Sambuc if (RT->getDecl()->isUnion()) {
1955f4a2713aSLionel Sambuc FieldIndex = 0;
1956f4a2713aSLionel Sambuc if (!VerifyOnly) {
1957f4a2713aSLionel Sambuc FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion();
1958f4a2713aSLionel Sambuc if (CurrentField && CurrentField != *Field) {
1959f4a2713aSLionel Sambuc assert(StructuredList->getNumInits() == 1
1960f4a2713aSLionel Sambuc && "A union should never have more than one initializer!");
1961f4a2713aSLionel Sambuc
1962f4a2713aSLionel Sambuc // we're about to throw away an initializer, emit warning
1963f4a2713aSLionel Sambuc SemaRef.Diag(D->getFieldLoc(),
1964f4a2713aSLionel Sambuc diag::warn_initializer_overrides)
1965f4a2713aSLionel Sambuc << D->getSourceRange();
1966f4a2713aSLionel Sambuc Expr *ExistingInit = StructuredList->getInit(0);
1967f4a2713aSLionel Sambuc SemaRef.Diag(ExistingInit->getLocStart(),
1968f4a2713aSLionel Sambuc diag::note_previous_initializer)
1969f4a2713aSLionel Sambuc << /*FIXME:has side effects=*/0
1970f4a2713aSLionel Sambuc << ExistingInit->getSourceRange();
1971f4a2713aSLionel Sambuc
1972f4a2713aSLionel Sambuc // remove existing initializer
1973f4a2713aSLionel Sambuc StructuredList->resizeInits(SemaRef.Context, 0);
1974*0a6a1f1dSLionel Sambuc StructuredList->setInitializedFieldInUnion(nullptr);
1975f4a2713aSLionel Sambuc }
1976f4a2713aSLionel Sambuc
1977f4a2713aSLionel Sambuc StructuredList->setInitializedFieldInUnion(*Field);
1978f4a2713aSLionel Sambuc }
1979f4a2713aSLionel Sambuc }
1980f4a2713aSLionel Sambuc
1981f4a2713aSLionel Sambuc // Make sure we can use this declaration.
1982f4a2713aSLionel Sambuc bool InvalidUse;
1983f4a2713aSLionel Sambuc if (VerifyOnly)
1984f4a2713aSLionel Sambuc InvalidUse = !SemaRef.CanUseDecl(*Field);
1985f4a2713aSLionel Sambuc else
1986f4a2713aSLionel Sambuc InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc());
1987f4a2713aSLionel Sambuc if (InvalidUse) {
1988f4a2713aSLionel Sambuc ++Index;
1989f4a2713aSLionel Sambuc return true;
1990f4a2713aSLionel Sambuc }
1991f4a2713aSLionel Sambuc
1992f4a2713aSLionel Sambuc if (!VerifyOnly) {
1993f4a2713aSLionel Sambuc // Update the designator with the field declaration.
1994f4a2713aSLionel Sambuc D->setField(*Field);
1995f4a2713aSLionel Sambuc
1996f4a2713aSLionel Sambuc // Make sure that our non-designated initializer list has space
1997f4a2713aSLionel Sambuc // for a subobject corresponding to this field.
1998f4a2713aSLionel Sambuc if (FieldIndex >= StructuredList->getNumInits())
1999f4a2713aSLionel Sambuc StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1);
2000f4a2713aSLionel Sambuc }
2001f4a2713aSLionel Sambuc
2002f4a2713aSLionel Sambuc // This designator names a flexible array member.
2003f4a2713aSLionel Sambuc if (Field->getType()->isIncompleteArrayType()) {
2004f4a2713aSLionel Sambuc bool Invalid = false;
2005f4a2713aSLionel Sambuc if ((DesigIdx + 1) != DIE->size()) {
2006f4a2713aSLionel Sambuc // We can't designate an object within the flexible array
2007f4a2713aSLionel Sambuc // member (because GCC doesn't allow it).
2008f4a2713aSLionel Sambuc if (!VerifyOnly) {
2009f4a2713aSLionel Sambuc DesignatedInitExpr::Designator *NextD
2010f4a2713aSLionel Sambuc = DIE->getDesignator(DesigIdx + 1);
2011f4a2713aSLionel Sambuc SemaRef.Diag(NextD->getLocStart(),
2012f4a2713aSLionel Sambuc diag::err_designator_into_flexible_array_member)
2013f4a2713aSLionel Sambuc << SourceRange(NextD->getLocStart(),
2014f4a2713aSLionel Sambuc DIE->getLocEnd());
2015f4a2713aSLionel Sambuc SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
2016f4a2713aSLionel Sambuc << *Field;
2017f4a2713aSLionel Sambuc }
2018f4a2713aSLionel Sambuc Invalid = true;
2019f4a2713aSLionel Sambuc }
2020f4a2713aSLionel Sambuc
2021f4a2713aSLionel Sambuc if (!hadError && !isa<InitListExpr>(DIE->getInit()) &&
2022f4a2713aSLionel Sambuc !isa<StringLiteral>(DIE->getInit())) {
2023f4a2713aSLionel Sambuc // The initializer is not an initializer list.
2024f4a2713aSLionel Sambuc if (!VerifyOnly) {
2025f4a2713aSLionel Sambuc SemaRef.Diag(DIE->getInit()->getLocStart(),
2026f4a2713aSLionel Sambuc diag::err_flexible_array_init_needs_braces)
2027f4a2713aSLionel Sambuc << DIE->getInit()->getSourceRange();
2028f4a2713aSLionel Sambuc SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member)
2029f4a2713aSLionel Sambuc << *Field;
2030f4a2713aSLionel Sambuc }
2031f4a2713aSLionel Sambuc Invalid = true;
2032f4a2713aSLionel Sambuc }
2033f4a2713aSLionel Sambuc
2034f4a2713aSLionel Sambuc // Check GNU flexible array initializer.
2035f4a2713aSLionel Sambuc if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field,
2036f4a2713aSLionel Sambuc TopLevelObject))
2037f4a2713aSLionel Sambuc Invalid = true;
2038f4a2713aSLionel Sambuc
2039f4a2713aSLionel Sambuc if (Invalid) {
2040f4a2713aSLionel Sambuc ++Index;
2041f4a2713aSLionel Sambuc return true;
2042f4a2713aSLionel Sambuc }
2043f4a2713aSLionel Sambuc
2044f4a2713aSLionel Sambuc // Initialize the array.
2045f4a2713aSLionel Sambuc bool prevHadError = hadError;
2046f4a2713aSLionel Sambuc unsigned newStructuredIndex = FieldIndex;
2047f4a2713aSLionel Sambuc unsigned OldIndex = Index;
2048f4a2713aSLionel Sambuc IList->setInit(Index, DIE->getInit());
2049f4a2713aSLionel Sambuc
2050f4a2713aSLionel Sambuc InitializedEntity MemberEntity =
2051f4a2713aSLionel Sambuc InitializedEntity::InitializeMember(*Field, &Entity);
2052f4a2713aSLionel Sambuc CheckSubElementType(MemberEntity, IList, Field->getType(), Index,
2053f4a2713aSLionel Sambuc StructuredList, newStructuredIndex);
2054f4a2713aSLionel Sambuc
2055f4a2713aSLionel Sambuc IList->setInit(OldIndex, DIE);
2056f4a2713aSLionel Sambuc if (hadError && !prevHadError) {
2057f4a2713aSLionel Sambuc ++Field;
2058f4a2713aSLionel Sambuc ++FieldIndex;
2059f4a2713aSLionel Sambuc if (NextField)
2060f4a2713aSLionel Sambuc *NextField = Field;
2061f4a2713aSLionel Sambuc StructuredIndex = FieldIndex;
2062f4a2713aSLionel Sambuc return true;
2063f4a2713aSLionel Sambuc }
2064f4a2713aSLionel Sambuc } else {
2065f4a2713aSLionel Sambuc // Recurse to check later designated subobjects.
2066f4a2713aSLionel Sambuc QualType FieldType = Field->getType();
2067f4a2713aSLionel Sambuc unsigned newStructuredIndex = FieldIndex;
2068f4a2713aSLionel Sambuc
2069f4a2713aSLionel Sambuc InitializedEntity MemberEntity =
2070f4a2713aSLionel Sambuc InitializedEntity::InitializeMember(*Field, &Entity);
2071f4a2713aSLionel Sambuc if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1,
2072*0a6a1f1dSLionel Sambuc FieldType, nullptr, nullptr, Index,
2073f4a2713aSLionel Sambuc StructuredList, newStructuredIndex,
2074f4a2713aSLionel Sambuc true, false))
2075f4a2713aSLionel Sambuc return true;
2076f4a2713aSLionel Sambuc }
2077f4a2713aSLionel Sambuc
2078f4a2713aSLionel Sambuc // Find the position of the next field to be initialized in this
2079f4a2713aSLionel Sambuc // subobject.
2080f4a2713aSLionel Sambuc ++Field;
2081f4a2713aSLionel Sambuc ++FieldIndex;
2082f4a2713aSLionel Sambuc
2083f4a2713aSLionel Sambuc // If this the first designator, our caller will continue checking
2084f4a2713aSLionel Sambuc // the rest of this struct/class/union subobject.
2085f4a2713aSLionel Sambuc if (IsFirstDesignator) {
2086f4a2713aSLionel Sambuc if (NextField)
2087f4a2713aSLionel Sambuc *NextField = Field;
2088f4a2713aSLionel Sambuc StructuredIndex = FieldIndex;
2089f4a2713aSLionel Sambuc return false;
2090f4a2713aSLionel Sambuc }
2091f4a2713aSLionel Sambuc
2092f4a2713aSLionel Sambuc if (!FinishSubobjectInit)
2093f4a2713aSLionel Sambuc return false;
2094f4a2713aSLionel Sambuc
2095f4a2713aSLionel Sambuc // We've already initialized something in the union; we're done.
2096f4a2713aSLionel Sambuc if (RT->getDecl()->isUnion())
2097f4a2713aSLionel Sambuc return hadError;
2098f4a2713aSLionel Sambuc
2099f4a2713aSLionel Sambuc // Check the remaining fields within this class/struct/union subobject.
2100f4a2713aSLionel Sambuc bool prevHadError = hadError;
2101f4a2713aSLionel Sambuc
2102f4a2713aSLionel Sambuc CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index,
2103f4a2713aSLionel Sambuc StructuredList, FieldIndex);
2104f4a2713aSLionel Sambuc return hadError && !prevHadError;
2105f4a2713aSLionel Sambuc }
2106f4a2713aSLionel Sambuc
2107f4a2713aSLionel Sambuc // C99 6.7.8p6:
2108f4a2713aSLionel Sambuc //
2109f4a2713aSLionel Sambuc // If a designator has the form
2110f4a2713aSLionel Sambuc //
2111f4a2713aSLionel Sambuc // [ constant-expression ]
2112f4a2713aSLionel Sambuc //
2113f4a2713aSLionel Sambuc // then the current object (defined below) shall have array
2114f4a2713aSLionel Sambuc // type and the expression shall be an integer constant
2115f4a2713aSLionel Sambuc // expression. If the array is of unknown size, any
2116f4a2713aSLionel Sambuc // nonnegative value is valid.
2117f4a2713aSLionel Sambuc //
2118f4a2713aSLionel Sambuc // Additionally, cope with the GNU extension that permits
2119f4a2713aSLionel Sambuc // designators of the form
2120f4a2713aSLionel Sambuc //
2121f4a2713aSLionel Sambuc // [ constant-expression ... constant-expression ]
2122f4a2713aSLionel Sambuc const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType);
2123f4a2713aSLionel Sambuc if (!AT) {
2124f4a2713aSLionel Sambuc if (!VerifyOnly)
2125f4a2713aSLionel Sambuc SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array)
2126f4a2713aSLionel Sambuc << CurrentObjectType;
2127f4a2713aSLionel Sambuc ++Index;
2128f4a2713aSLionel Sambuc return true;
2129f4a2713aSLionel Sambuc }
2130f4a2713aSLionel Sambuc
2131*0a6a1f1dSLionel Sambuc Expr *IndexExpr = nullptr;
2132f4a2713aSLionel Sambuc llvm::APSInt DesignatedStartIndex, DesignatedEndIndex;
2133f4a2713aSLionel Sambuc if (D->isArrayDesignator()) {
2134f4a2713aSLionel Sambuc IndexExpr = DIE->getArrayIndex(*D);
2135f4a2713aSLionel Sambuc DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context);
2136f4a2713aSLionel Sambuc DesignatedEndIndex = DesignatedStartIndex;
2137f4a2713aSLionel Sambuc } else {
2138f4a2713aSLionel Sambuc assert(D->isArrayRangeDesignator() && "Need array-range designator");
2139f4a2713aSLionel Sambuc
2140f4a2713aSLionel Sambuc DesignatedStartIndex =
2141f4a2713aSLionel Sambuc DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context);
2142f4a2713aSLionel Sambuc DesignatedEndIndex =
2143f4a2713aSLionel Sambuc DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context);
2144f4a2713aSLionel Sambuc IndexExpr = DIE->getArrayRangeEnd(*D);
2145f4a2713aSLionel Sambuc
2146f4a2713aSLionel Sambuc // Codegen can't handle evaluating array range designators that have side
2147f4a2713aSLionel Sambuc // effects, because we replicate the AST value for each initialized element.
2148f4a2713aSLionel Sambuc // As such, set the sawArrayRangeDesignator() bit if we initialize multiple
2149f4a2713aSLionel Sambuc // elements with something that has a side effect, so codegen can emit an
2150f4a2713aSLionel Sambuc // "error unsupported" error instead of miscompiling the app.
2151f4a2713aSLionel Sambuc if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&&
2152f4a2713aSLionel Sambuc DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly)
2153f4a2713aSLionel Sambuc FullyStructuredList->sawArrayRangeDesignator();
2154f4a2713aSLionel Sambuc }
2155f4a2713aSLionel Sambuc
2156f4a2713aSLionel Sambuc if (isa<ConstantArrayType>(AT)) {
2157f4a2713aSLionel Sambuc llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false);
2158f4a2713aSLionel Sambuc DesignatedStartIndex
2159f4a2713aSLionel Sambuc = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth());
2160f4a2713aSLionel Sambuc DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned());
2161f4a2713aSLionel Sambuc DesignatedEndIndex
2162f4a2713aSLionel Sambuc = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth());
2163f4a2713aSLionel Sambuc DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned());
2164f4a2713aSLionel Sambuc if (DesignatedEndIndex >= MaxElements) {
2165f4a2713aSLionel Sambuc if (!VerifyOnly)
2166f4a2713aSLionel Sambuc SemaRef.Diag(IndexExpr->getLocStart(),
2167f4a2713aSLionel Sambuc diag::err_array_designator_too_large)
2168f4a2713aSLionel Sambuc << DesignatedEndIndex.toString(10) << MaxElements.toString(10)
2169f4a2713aSLionel Sambuc << IndexExpr->getSourceRange();
2170f4a2713aSLionel Sambuc ++Index;
2171f4a2713aSLionel Sambuc return true;
2172f4a2713aSLionel Sambuc }
2173f4a2713aSLionel Sambuc } else {
2174f4a2713aSLionel Sambuc // Make sure the bit-widths and signedness match.
2175f4a2713aSLionel Sambuc if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth())
2176f4a2713aSLionel Sambuc DesignatedEndIndex
2177f4a2713aSLionel Sambuc = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth());
2178f4a2713aSLionel Sambuc else if (DesignatedStartIndex.getBitWidth() <
2179f4a2713aSLionel Sambuc DesignatedEndIndex.getBitWidth())
2180f4a2713aSLionel Sambuc DesignatedStartIndex
2181f4a2713aSLionel Sambuc = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth());
2182f4a2713aSLionel Sambuc DesignatedStartIndex.setIsUnsigned(true);
2183f4a2713aSLionel Sambuc DesignatedEndIndex.setIsUnsigned(true);
2184f4a2713aSLionel Sambuc }
2185f4a2713aSLionel Sambuc
2186f4a2713aSLionel Sambuc if (!VerifyOnly && StructuredList->isStringLiteralInit()) {
2187f4a2713aSLionel Sambuc // We're modifying a string literal init; we have to decompose the string
2188f4a2713aSLionel Sambuc // so we can modify the individual characters.
2189f4a2713aSLionel Sambuc ASTContext &Context = SemaRef.Context;
2190f4a2713aSLionel Sambuc Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens();
2191f4a2713aSLionel Sambuc
2192f4a2713aSLionel Sambuc // Compute the character type
2193f4a2713aSLionel Sambuc QualType CharTy = AT->getElementType();
2194f4a2713aSLionel Sambuc
2195f4a2713aSLionel Sambuc // Compute the type of the integer literals.
2196f4a2713aSLionel Sambuc QualType PromotedCharTy = CharTy;
2197f4a2713aSLionel Sambuc if (CharTy->isPromotableIntegerType())
2198f4a2713aSLionel Sambuc PromotedCharTy = Context.getPromotedIntegerType(CharTy);
2199f4a2713aSLionel Sambuc unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy);
2200f4a2713aSLionel Sambuc
2201f4a2713aSLionel Sambuc if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) {
2202f4a2713aSLionel Sambuc // Get the length of the string.
2203f4a2713aSLionel Sambuc uint64_t StrLen = SL->getLength();
2204f4a2713aSLionel Sambuc if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen))
2205f4a2713aSLionel Sambuc StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue();
2206f4a2713aSLionel Sambuc StructuredList->resizeInits(Context, StrLen);
2207f4a2713aSLionel Sambuc
2208f4a2713aSLionel Sambuc // Build a literal for each character in the string, and put them into
2209f4a2713aSLionel Sambuc // the init list.
2210f4a2713aSLionel Sambuc for (unsigned i = 0, e = StrLen; i != e; ++i) {
2211f4a2713aSLionel Sambuc llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i));
2212f4a2713aSLionel Sambuc Expr *Init = new (Context) IntegerLiteral(
2213f4a2713aSLionel Sambuc Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc());
2214f4a2713aSLionel Sambuc if (CharTy != PromotedCharTy)
2215f4a2713aSLionel Sambuc Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast,
2216*0a6a1f1dSLionel Sambuc Init, nullptr, VK_RValue);
2217f4a2713aSLionel Sambuc StructuredList->updateInit(Context, i, Init);
2218f4a2713aSLionel Sambuc }
2219f4a2713aSLionel Sambuc } else {
2220f4a2713aSLionel Sambuc ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr);
2221f4a2713aSLionel Sambuc std::string Str;
2222f4a2713aSLionel Sambuc Context.getObjCEncodingForType(E->getEncodedType(), Str);
2223f4a2713aSLionel Sambuc
2224f4a2713aSLionel Sambuc // Get the length of the string.
2225f4a2713aSLionel Sambuc uint64_t StrLen = Str.size();
2226f4a2713aSLionel Sambuc if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen))
2227f4a2713aSLionel Sambuc StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue();
2228f4a2713aSLionel Sambuc StructuredList->resizeInits(Context, StrLen);
2229f4a2713aSLionel Sambuc
2230f4a2713aSLionel Sambuc // Build a literal for each character in the string, and put them into
2231f4a2713aSLionel Sambuc // the init list.
2232f4a2713aSLionel Sambuc for (unsigned i = 0, e = StrLen; i != e; ++i) {
2233f4a2713aSLionel Sambuc llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]);
2234f4a2713aSLionel Sambuc Expr *Init = new (Context) IntegerLiteral(
2235f4a2713aSLionel Sambuc Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc());
2236f4a2713aSLionel Sambuc if (CharTy != PromotedCharTy)
2237f4a2713aSLionel Sambuc Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast,
2238*0a6a1f1dSLionel Sambuc Init, nullptr, VK_RValue);
2239f4a2713aSLionel Sambuc StructuredList->updateInit(Context, i, Init);
2240f4a2713aSLionel Sambuc }
2241f4a2713aSLionel Sambuc }
2242f4a2713aSLionel Sambuc }
2243f4a2713aSLionel Sambuc
2244f4a2713aSLionel Sambuc // Make sure that our non-designated initializer list has space
2245f4a2713aSLionel Sambuc // for a subobject corresponding to this array element.
2246f4a2713aSLionel Sambuc if (!VerifyOnly &&
2247f4a2713aSLionel Sambuc DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits())
2248f4a2713aSLionel Sambuc StructuredList->resizeInits(SemaRef.Context,
2249f4a2713aSLionel Sambuc DesignatedEndIndex.getZExtValue() + 1);
2250f4a2713aSLionel Sambuc
2251f4a2713aSLionel Sambuc // Repeatedly perform subobject initializations in the range
2252f4a2713aSLionel Sambuc // [DesignatedStartIndex, DesignatedEndIndex].
2253f4a2713aSLionel Sambuc
2254f4a2713aSLionel Sambuc // Move to the next designator
2255f4a2713aSLionel Sambuc unsigned ElementIndex = DesignatedStartIndex.getZExtValue();
2256f4a2713aSLionel Sambuc unsigned OldIndex = Index;
2257f4a2713aSLionel Sambuc
2258f4a2713aSLionel Sambuc InitializedEntity ElementEntity =
2259f4a2713aSLionel Sambuc InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity);
2260f4a2713aSLionel Sambuc
2261f4a2713aSLionel Sambuc while (DesignatedStartIndex <= DesignatedEndIndex) {
2262f4a2713aSLionel Sambuc // Recurse to check later designated subobjects.
2263f4a2713aSLionel Sambuc QualType ElementType = AT->getElementType();
2264f4a2713aSLionel Sambuc Index = OldIndex;
2265f4a2713aSLionel Sambuc
2266f4a2713aSLionel Sambuc ElementEntity.setElementIndex(ElementIndex);
2267f4a2713aSLionel Sambuc if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1,
2268*0a6a1f1dSLionel Sambuc ElementType, nullptr, nullptr, Index,
2269f4a2713aSLionel Sambuc StructuredList, ElementIndex,
2270f4a2713aSLionel Sambuc (DesignatedStartIndex == DesignatedEndIndex),
2271f4a2713aSLionel Sambuc false))
2272f4a2713aSLionel Sambuc return true;
2273f4a2713aSLionel Sambuc
2274f4a2713aSLionel Sambuc // Move to the next index in the array that we'll be initializing.
2275f4a2713aSLionel Sambuc ++DesignatedStartIndex;
2276f4a2713aSLionel Sambuc ElementIndex = DesignatedStartIndex.getZExtValue();
2277f4a2713aSLionel Sambuc }
2278f4a2713aSLionel Sambuc
2279f4a2713aSLionel Sambuc // If this the first designator, our caller will continue checking
2280f4a2713aSLionel Sambuc // the rest of this array subobject.
2281f4a2713aSLionel Sambuc if (IsFirstDesignator) {
2282f4a2713aSLionel Sambuc if (NextElementIndex)
2283f4a2713aSLionel Sambuc *NextElementIndex = DesignatedStartIndex;
2284f4a2713aSLionel Sambuc StructuredIndex = ElementIndex;
2285f4a2713aSLionel Sambuc return false;
2286f4a2713aSLionel Sambuc }
2287f4a2713aSLionel Sambuc
2288f4a2713aSLionel Sambuc if (!FinishSubobjectInit)
2289f4a2713aSLionel Sambuc return false;
2290f4a2713aSLionel Sambuc
2291f4a2713aSLionel Sambuc // Check the remaining elements within this array subobject.
2292f4a2713aSLionel Sambuc bool prevHadError = hadError;
2293f4a2713aSLionel Sambuc CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex,
2294f4a2713aSLionel Sambuc /*SubobjectIsDesignatorContext=*/false, Index,
2295f4a2713aSLionel Sambuc StructuredList, ElementIndex);
2296f4a2713aSLionel Sambuc return hadError && !prevHadError;
2297f4a2713aSLionel Sambuc }
2298f4a2713aSLionel Sambuc
2299f4a2713aSLionel Sambuc // Get the structured initializer list for a subobject of type
2300f4a2713aSLionel Sambuc // @p CurrentObjectType.
2301f4a2713aSLionel Sambuc InitListExpr *
getStructuredSubobjectInit(InitListExpr * IList,unsigned Index,QualType CurrentObjectType,InitListExpr * StructuredList,unsigned StructuredIndex,SourceRange InitRange)2302f4a2713aSLionel Sambuc InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index,
2303f4a2713aSLionel Sambuc QualType CurrentObjectType,
2304f4a2713aSLionel Sambuc InitListExpr *StructuredList,
2305f4a2713aSLionel Sambuc unsigned StructuredIndex,
2306f4a2713aSLionel Sambuc SourceRange InitRange) {
2307f4a2713aSLionel Sambuc if (VerifyOnly)
2308*0a6a1f1dSLionel Sambuc return nullptr; // No structured list in verification-only mode.
2309*0a6a1f1dSLionel Sambuc Expr *ExistingInit = nullptr;
2310f4a2713aSLionel Sambuc if (!StructuredList)
2311f4a2713aSLionel Sambuc ExistingInit = SyntacticToSemantic.lookup(IList);
2312f4a2713aSLionel Sambuc else if (StructuredIndex < StructuredList->getNumInits())
2313f4a2713aSLionel Sambuc ExistingInit = StructuredList->getInit(StructuredIndex);
2314f4a2713aSLionel Sambuc
2315f4a2713aSLionel Sambuc if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit))
2316f4a2713aSLionel Sambuc return Result;
2317f4a2713aSLionel Sambuc
2318f4a2713aSLionel Sambuc if (ExistingInit) {
2319f4a2713aSLionel Sambuc // We are creating an initializer list that initializes the
2320f4a2713aSLionel Sambuc // subobjects of the current object, but there was already an
2321f4a2713aSLionel Sambuc // initialization that completely initialized the current
2322f4a2713aSLionel Sambuc // subobject, e.g., by a compound literal:
2323f4a2713aSLionel Sambuc //
2324f4a2713aSLionel Sambuc // struct X { int a, b; };
2325f4a2713aSLionel Sambuc // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 };
2326f4a2713aSLionel Sambuc //
2327f4a2713aSLionel Sambuc // Here, xs[0].a == 0 and xs[0].b == 3, since the second,
2328f4a2713aSLionel Sambuc // designated initializer re-initializes the whole
2329f4a2713aSLionel Sambuc // subobject [0], overwriting previous initializers.
2330f4a2713aSLionel Sambuc SemaRef.Diag(InitRange.getBegin(),
2331f4a2713aSLionel Sambuc diag::warn_subobject_initializer_overrides)
2332f4a2713aSLionel Sambuc << InitRange;
2333f4a2713aSLionel Sambuc SemaRef.Diag(ExistingInit->getLocStart(),
2334f4a2713aSLionel Sambuc diag::note_previous_initializer)
2335f4a2713aSLionel Sambuc << /*FIXME:has side effects=*/0
2336f4a2713aSLionel Sambuc << ExistingInit->getSourceRange();
2337f4a2713aSLionel Sambuc }
2338f4a2713aSLionel Sambuc
2339f4a2713aSLionel Sambuc InitListExpr *Result
2340f4a2713aSLionel Sambuc = new (SemaRef.Context) InitListExpr(SemaRef.Context,
2341f4a2713aSLionel Sambuc InitRange.getBegin(), None,
2342f4a2713aSLionel Sambuc InitRange.getEnd());
2343f4a2713aSLionel Sambuc
2344f4a2713aSLionel Sambuc QualType ResultType = CurrentObjectType;
2345f4a2713aSLionel Sambuc if (!ResultType->isArrayType())
2346f4a2713aSLionel Sambuc ResultType = ResultType.getNonLValueExprType(SemaRef.Context);
2347f4a2713aSLionel Sambuc Result->setType(ResultType);
2348f4a2713aSLionel Sambuc
2349f4a2713aSLionel Sambuc // Pre-allocate storage for the structured initializer list.
2350f4a2713aSLionel Sambuc unsigned NumElements = 0;
2351f4a2713aSLionel Sambuc unsigned NumInits = 0;
2352f4a2713aSLionel Sambuc bool GotNumInits = false;
2353f4a2713aSLionel Sambuc if (!StructuredList) {
2354f4a2713aSLionel Sambuc NumInits = IList->getNumInits();
2355f4a2713aSLionel Sambuc GotNumInits = true;
2356f4a2713aSLionel Sambuc } else if (Index < IList->getNumInits()) {
2357f4a2713aSLionel Sambuc if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) {
2358f4a2713aSLionel Sambuc NumInits = SubList->getNumInits();
2359f4a2713aSLionel Sambuc GotNumInits = true;
2360f4a2713aSLionel Sambuc }
2361f4a2713aSLionel Sambuc }
2362f4a2713aSLionel Sambuc
2363f4a2713aSLionel Sambuc if (const ArrayType *AType
2364f4a2713aSLionel Sambuc = SemaRef.Context.getAsArrayType(CurrentObjectType)) {
2365f4a2713aSLionel Sambuc if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) {
2366f4a2713aSLionel Sambuc NumElements = CAType->getSize().getZExtValue();
2367f4a2713aSLionel Sambuc // Simple heuristic so that we don't allocate a very large
2368f4a2713aSLionel Sambuc // initializer with many empty entries at the end.
2369f4a2713aSLionel Sambuc if (GotNumInits && NumElements > NumInits)
2370f4a2713aSLionel Sambuc NumElements = 0;
2371f4a2713aSLionel Sambuc }
2372f4a2713aSLionel Sambuc } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>())
2373f4a2713aSLionel Sambuc NumElements = VType->getNumElements();
2374f4a2713aSLionel Sambuc else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) {
2375f4a2713aSLionel Sambuc RecordDecl *RDecl = RType->getDecl();
2376f4a2713aSLionel Sambuc if (RDecl->isUnion())
2377f4a2713aSLionel Sambuc NumElements = 1;
2378f4a2713aSLionel Sambuc else
2379*0a6a1f1dSLionel Sambuc NumElements = std::distance(RDecl->field_begin(), RDecl->field_end());
2380f4a2713aSLionel Sambuc }
2381f4a2713aSLionel Sambuc
2382f4a2713aSLionel Sambuc Result->reserveInits(SemaRef.Context, NumElements);
2383f4a2713aSLionel Sambuc
2384f4a2713aSLionel Sambuc // Link this new initializer list into the structured initializer
2385f4a2713aSLionel Sambuc // lists.
2386f4a2713aSLionel Sambuc if (StructuredList)
2387f4a2713aSLionel Sambuc StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result);
2388f4a2713aSLionel Sambuc else {
2389f4a2713aSLionel Sambuc Result->setSyntacticForm(IList);
2390f4a2713aSLionel Sambuc SyntacticToSemantic[IList] = Result;
2391f4a2713aSLionel Sambuc }
2392f4a2713aSLionel Sambuc
2393f4a2713aSLionel Sambuc return Result;
2394f4a2713aSLionel Sambuc }
2395f4a2713aSLionel Sambuc
2396f4a2713aSLionel Sambuc /// Update the initializer at index @p StructuredIndex within the
2397f4a2713aSLionel Sambuc /// structured initializer list to the value @p expr.
UpdateStructuredListElement(InitListExpr * StructuredList,unsigned & StructuredIndex,Expr * expr)2398f4a2713aSLionel Sambuc void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList,
2399f4a2713aSLionel Sambuc unsigned &StructuredIndex,
2400f4a2713aSLionel Sambuc Expr *expr) {
2401f4a2713aSLionel Sambuc // No structured initializer list to update
2402f4a2713aSLionel Sambuc if (!StructuredList)
2403f4a2713aSLionel Sambuc return;
2404f4a2713aSLionel Sambuc
2405f4a2713aSLionel Sambuc if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context,
2406f4a2713aSLionel Sambuc StructuredIndex, expr)) {
2407f4a2713aSLionel Sambuc // This initializer overwrites a previous initializer. Warn.
2408f4a2713aSLionel Sambuc SemaRef.Diag(expr->getLocStart(),
2409f4a2713aSLionel Sambuc diag::warn_initializer_overrides)
2410f4a2713aSLionel Sambuc << expr->getSourceRange();
2411f4a2713aSLionel Sambuc SemaRef.Diag(PrevInit->getLocStart(),
2412f4a2713aSLionel Sambuc diag::note_previous_initializer)
2413f4a2713aSLionel Sambuc << /*FIXME:has side effects=*/0
2414f4a2713aSLionel Sambuc << PrevInit->getSourceRange();
2415f4a2713aSLionel Sambuc }
2416f4a2713aSLionel Sambuc
2417f4a2713aSLionel Sambuc ++StructuredIndex;
2418f4a2713aSLionel Sambuc }
2419f4a2713aSLionel Sambuc
2420f4a2713aSLionel Sambuc /// Check that the given Index expression is a valid array designator
2421f4a2713aSLionel Sambuc /// value. This is essentially just a wrapper around
2422f4a2713aSLionel Sambuc /// VerifyIntegerConstantExpression that also checks for negative values
2423f4a2713aSLionel Sambuc /// and produces a reasonable diagnostic if there is a
2424f4a2713aSLionel Sambuc /// failure. Returns the index expression, possibly with an implicit cast
2425f4a2713aSLionel Sambuc /// added, on success. If everything went okay, Value will receive the
2426f4a2713aSLionel Sambuc /// value of the constant expression.
2427f4a2713aSLionel Sambuc static ExprResult
CheckArrayDesignatorExpr(Sema & S,Expr * Index,llvm::APSInt & Value)2428f4a2713aSLionel Sambuc CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) {
2429f4a2713aSLionel Sambuc SourceLocation Loc = Index->getLocStart();
2430f4a2713aSLionel Sambuc
2431f4a2713aSLionel Sambuc // Make sure this is an integer constant expression.
2432f4a2713aSLionel Sambuc ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value);
2433f4a2713aSLionel Sambuc if (Result.isInvalid())
2434f4a2713aSLionel Sambuc return Result;
2435f4a2713aSLionel Sambuc
2436f4a2713aSLionel Sambuc if (Value.isSigned() && Value.isNegative())
2437f4a2713aSLionel Sambuc return S.Diag(Loc, diag::err_array_designator_negative)
2438f4a2713aSLionel Sambuc << Value.toString(10) << Index->getSourceRange();
2439f4a2713aSLionel Sambuc
2440f4a2713aSLionel Sambuc Value.setIsUnsigned(true);
2441f4a2713aSLionel Sambuc return Result;
2442f4a2713aSLionel Sambuc }
2443f4a2713aSLionel Sambuc
ActOnDesignatedInitializer(Designation & Desig,SourceLocation Loc,bool GNUSyntax,ExprResult Init)2444f4a2713aSLionel Sambuc ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig,
2445f4a2713aSLionel Sambuc SourceLocation Loc,
2446f4a2713aSLionel Sambuc bool GNUSyntax,
2447f4a2713aSLionel Sambuc ExprResult Init) {
2448f4a2713aSLionel Sambuc typedef DesignatedInitExpr::Designator ASTDesignator;
2449f4a2713aSLionel Sambuc
2450f4a2713aSLionel Sambuc bool Invalid = false;
2451f4a2713aSLionel Sambuc SmallVector<ASTDesignator, 32> Designators;
2452f4a2713aSLionel Sambuc SmallVector<Expr *, 32> InitExpressions;
2453f4a2713aSLionel Sambuc
2454f4a2713aSLionel Sambuc // Build designators and check array designator expressions.
2455f4a2713aSLionel Sambuc for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) {
2456f4a2713aSLionel Sambuc const Designator &D = Desig.getDesignator(Idx);
2457f4a2713aSLionel Sambuc switch (D.getKind()) {
2458f4a2713aSLionel Sambuc case Designator::FieldDesignator:
2459f4a2713aSLionel Sambuc Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(),
2460f4a2713aSLionel Sambuc D.getFieldLoc()));
2461f4a2713aSLionel Sambuc break;
2462f4a2713aSLionel Sambuc
2463f4a2713aSLionel Sambuc case Designator::ArrayDesignator: {
2464f4a2713aSLionel Sambuc Expr *Index = static_cast<Expr *>(D.getArrayIndex());
2465f4a2713aSLionel Sambuc llvm::APSInt IndexValue;
2466f4a2713aSLionel Sambuc if (!Index->isTypeDependent() && !Index->isValueDependent())
2467*0a6a1f1dSLionel Sambuc Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).get();
2468f4a2713aSLionel Sambuc if (!Index)
2469f4a2713aSLionel Sambuc Invalid = true;
2470f4a2713aSLionel Sambuc else {
2471f4a2713aSLionel Sambuc Designators.push_back(ASTDesignator(InitExpressions.size(),
2472f4a2713aSLionel Sambuc D.getLBracketLoc(),
2473f4a2713aSLionel Sambuc D.getRBracketLoc()));
2474f4a2713aSLionel Sambuc InitExpressions.push_back(Index);
2475f4a2713aSLionel Sambuc }
2476f4a2713aSLionel Sambuc break;
2477f4a2713aSLionel Sambuc }
2478f4a2713aSLionel Sambuc
2479f4a2713aSLionel Sambuc case Designator::ArrayRangeDesignator: {
2480f4a2713aSLionel Sambuc Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart());
2481f4a2713aSLionel Sambuc Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd());
2482f4a2713aSLionel Sambuc llvm::APSInt StartValue;
2483f4a2713aSLionel Sambuc llvm::APSInt EndValue;
2484f4a2713aSLionel Sambuc bool StartDependent = StartIndex->isTypeDependent() ||
2485f4a2713aSLionel Sambuc StartIndex->isValueDependent();
2486f4a2713aSLionel Sambuc bool EndDependent = EndIndex->isTypeDependent() ||
2487f4a2713aSLionel Sambuc EndIndex->isValueDependent();
2488f4a2713aSLionel Sambuc if (!StartDependent)
2489f4a2713aSLionel Sambuc StartIndex =
2490*0a6a1f1dSLionel Sambuc CheckArrayDesignatorExpr(*this, StartIndex, StartValue).get();
2491f4a2713aSLionel Sambuc if (!EndDependent)
2492*0a6a1f1dSLionel Sambuc EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).get();
2493f4a2713aSLionel Sambuc
2494f4a2713aSLionel Sambuc if (!StartIndex || !EndIndex)
2495f4a2713aSLionel Sambuc Invalid = true;
2496f4a2713aSLionel Sambuc else {
2497f4a2713aSLionel Sambuc // Make sure we're comparing values with the same bit width.
2498f4a2713aSLionel Sambuc if (StartDependent || EndDependent) {
2499f4a2713aSLionel Sambuc // Nothing to compute.
2500f4a2713aSLionel Sambuc } else if (StartValue.getBitWidth() > EndValue.getBitWidth())
2501f4a2713aSLionel Sambuc EndValue = EndValue.extend(StartValue.getBitWidth());
2502f4a2713aSLionel Sambuc else if (StartValue.getBitWidth() < EndValue.getBitWidth())
2503f4a2713aSLionel Sambuc StartValue = StartValue.extend(EndValue.getBitWidth());
2504f4a2713aSLionel Sambuc
2505f4a2713aSLionel Sambuc if (!StartDependent && !EndDependent && EndValue < StartValue) {
2506f4a2713aSLionel Sambuc Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range)
2507f4a2713aSLionel Sambuc << StartValue.toString(10) << EndValue.toString(10)
2508f4a2713aSLionel Sambuc << StartIndex->getSourceRange() << EndIndex->getSourceRange();
2509f4a2713aSLionel Sambuc Invalid = true;
2510f4a2713aSLionel Sambuc } else {
2511f4a2713aSLionel Sambuc Designators.push_back(ASTDesignator(InitExpressions.size(),
2512f4a2713aSLionel Sambuc D.getLBracketLoc(),
2513f4a2713aSLionel Sambuc D.getEllipsisLoc(),
2514f4a2713aSLionel Sambuc D.getRBracketLoc()));
2515f4a2713aSLionel Sambuc InitExpressions.push_back(StartIndex);
2516f4a2713aSLionel Sambuc InitExpressions.push_back(EndIndex);
2517f4a2713aSLionel Sambuc }
2518f4a2713aSLionel Sambuc }
2519f4a2713aSLionel Sambuc break;
2520f4a2713aSLionel Sambuc }
2521f4a2713aSLionel Sambuc }
2522f4a2713aSLionel Sambuc }
2523f4a2713aSLionel Sambuc
2524f4a2713aSLionel Sambuc if (Invalid || Init.isInvalid())
2525f4a2713aSLionel Sambuc return ExprError();
2526f4a2713aSLionel Sambuc
2527f4a2713aSLionel Sambuc // Clear out the expressions within the designation.
2528f4a2713aSLionel Sambuc Desig.ClearExprs(*this);
2529f4a2713aSLionel Sambuc
2530f4a2713aSLionel Sambuc DesignatedInitExpr *DIE
2531f4a2713aSLionel Sambuc = DesignatedInitExpr::Create(Context,
2532f4a2713aSLionel Sambuc Designators.data(), Designators.size(),
2533f4a2713aSLionel Sambuc InitExpressions, Loc, GNUSyntax,
2534*0a6a1f1dSLionel Sambuc Init.getAs<Expr>());
2535f4a2713aSLionel Sambuc
2536f4a2713aSLionel Sambuc if (!getLangOpts().C99)
2537f4a2713aSLionel Sambuc Diag(DIE->getLocStart(), diag::ext_designated_init)
2538f4a2713aSLionel Sambuc << DIE->getSourceRange();
2539f4a2713aSLionel Sambuc
2540*0a6a1f1dSLionel Sambuc return DIE;
2541f4a2713aSLionel Sambuc }
2542f4a2713aSLionel Sambuc
2543f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2544f4a2713aSLionel Sambuc // Initialization entity
2545f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2546f4a2713aSLionel Sambuc
InitializedEntity(ASTContext & Context,unsigned Index,const InitializedEntity & Parent)2547f4a2713aSLionel Sambuc InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index,
2548f4a2713aSLionel Sambuc const InitializedEntity &Parent)
2549f4a2713aSLionel Sambuc : Parent(&Parent), Index(Index)
2550f4a2713aSLionel Sambuc {
2551f4a2713aSLionel Sambuc if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) {
2552f4a2713aSLionel Sambuc Kind = EK_ArrayElement;
2553f4a2713aSLionel Sambuc Type = AT->getElementType();
2554f4a2713aSLionel Sambuc } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) {
2555f4a2713aSLionel Sambuc Kind = EK_VectorElement;
2556f4a2713aSLionel Sambuc Type = VT->getElementType();
2557f4a2713aSLionel Sambuc } else {
2558f4a2713aSLionel Sambuc const ComplexType *CT = Parent.getType()->getAs<ComplexType>();
2559f4a2713aSLionel Sambuc assert(CT && "Unexpected type");
2560f4a2713aSLionel Sambuc Kind = EK_ComplexElement;
2561f4a2713aSLionel Sambuc Type = CT->getElementType();
2562f4a2713aSLionel Sambuc }
2563f4a2713aSLionel Sambuc }
2564f4a2713aSLionel Sambuc
2565f4a2713aSLionel Sambuc InitializedEntity
InitializeBase(ASTContext & Context,const CXXBaseSpecifier * Base,bool IsInheritedVirtualBase)2566f4a2713aSLionel Sambuc InitializedEntity::InitializeBase(ASTContext &Context,
2567f4a2713aSLionel Sambuc const CXXBaseSpecifier *Base,
2568f4a2713aSLionel Sambuc bool IsInheritedVirtualBase) {
2569f4a2713aSLionel Sambuc InitializedEntity Result;
2570f4a2713aSLionel Sambuc Result.Kind = EK_Base;
2571*0a6a1f1dSLionel Sambuc Result.Parent = nullptr;
2572f4a2713aSLionel Sambuc Result.Base = reinterpret_cast<uintptr_t>(Base);
2573f4a2713aSLionel Sambuc if (IsInheritedVirtualBase)
2574f4a2713aSLionel Sambuc Result.Base |= 0x01;
2575f4a2713aSLionel Sambuc
2576f4a2713aSLionel Sambuc Result.Type = Base->getType();
2577f4a2713aSLionel Sambuc return Result;
2578f4a2713aSLionel Sambuc }
2579f4a2713aSLionel Sambuc
getName() const2580f4a2713aSLionel Sambuc DeclarationName InitializedEntity::getName() const {
2581f4a2713aSLionel Sambuc switch (getKind()) {
2582f4a2713aSLionel Sambuc case EK_Parameter:
2583f4a2713aSLionel Sambuc case EK_Parameter_CF_Audited: {
2584f4a2713aSLionel Sambuc ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1);
2585f4a2713aSLionel Sambuc return (D ? D->getDeclName() : DeclarationName());
2586f4a2713aSLionel Sambuc }
2587f4a2713aSLionel Sambuc
2588f4a2713aSLionel Sambuc case EK_Variable:
2589f4a2713aSLionel Sambuc case EK_Member:
2590f4a2713aSLionel Sambuc return VariableOrMember->getDeclName();
2591f4a2713aSLionel Sambuc
2592f4a2713aSLionel Sambuc case EK_LambdaCapture:
2593*0a6a1f1dSLionel Sambuc return DeclarationName(Capture.VarID);
2594f4a2713aSLionel Sambuc
2595f4a2713aSLionel Sambuc case EK_Result:
2596f4a2713aSLionel Sambuc case EK_Exception:
2597f4a2713aSLionel Sambuc case EK_New:
2598f4a2713aSLionel Sambuc case EK_Temporary:
2599f4a2713aSLionel Sambuc case EK_Base:
2600f4a2713aSLionel Sambuc case EK_Delegating:
2601f4a2713aSLionel Sambuc case EK_ArrayElement:
2602f4a2713aSLionel Sambuc case EK_VectorElement:
2603f4a2713aSLionel Sambuc case EK_ComplexElement:
2604f4a2713aSLionel Sambuc case EK_BlockElement:
2605f4a2713aSLionel Sambuc case EK_CompoundLiteralInit:
2606f4a2713aSLionel Sambuc case EK_RelatedResult:
2607f4a2713aSLionel Sambuc return DeclarationName();
2608f4a2713aSLionel Sambuc }
2609f4a2713aSLionel Sambuc
2610f4a2713aSLionel Sambuc llvm_unreachable("Invalid EntityKind!");
2611f4a2713aSLionel Sambuc }
2612f4a2713aSLionel Sambuc
getDecl() const2613f4a2713aSLionel Sambuc DeclaratorDecl *InitializedEntity::getDecl() const {
2614f4a2713aSLionel Sambuc switch (getKind()) {
2615f4a2713aSLionel Sambuc case EK_Variable:
2616f4a2713aSLionel Sambuc case EK_Member:
2617f4a2713aSLionel Sambuc return VariableOrMember;
2618f4a2713aSLionel Sambuc
2619f4a2713aSLionel Sambuc case EK_Parameter:
2620f4a2713aSLionel Sambuc case EK_Parameter_CF_Audited:
2621f4a2713aSLionel Sambuc return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1);
2622f4a2713aSLionel Sambuc
2623f4a2713aSLionel Sambuc case EK_Result:
2624f4a2713aSLionel Sambuc case EK_Exception:
2625f4a2713aSLionel Sambuc case EK_New:
2626f4a2713aSLionel Sambuc case EK_Temporary:
2627f4a2713aSLionel Sambuc case EK_Base:
2628f4a2713aSLionel Sambuc case EK_Delegating:
2629f4a2713aSLionel Sambuc case EK_ArrayElement:
2630f4a2713aSLionel Sambuc case EK_VectorElement:
2631f4a2713aSLionel Sambuc case EK_ComplexElement:
2632f4a2713aSLionel Sambuc case EK_BlockElement:
2633f4a2713aSLionel Sambuc case EK_LambdaCapture:
2634f4a2713aSLionel Sambuc case EK_CompoundLiteralInit:
2635f4a2713aSLionel Sambuc case EK_RelatedResult:
2636*0a6a1f1dSLionel Sambuc return nullptr;
2637f4a2713aSLionel Sambuc }
2638f4a2713aSLionel Sambuc
2639f4a2713aSLionel Sambuc llvm_unreachable("Invalid EntityKind!");
2640f4a2713aSLionel Sambuc }
2641f4a2713aSLionel Sambuc
allowsNRVO() const2642f4a2713aSLionel Sambuc bool InitializedEntity::allowsNRVO() const {
2643f4a2713aSLionel Sambuc switch (getKind()) {
2644f4a2713aSLionel Sambuc case EK_Result:
2645f4a2713aSLionel Sambuc case EK_Exception:
2646f4a2713aSLionel Sambuc return LocAndNRVO.NRVO;
2647f4a2713aSLionel Sambuc
2648f4a2713aSLionel Sambuc case EK_Variable:
2649f4a2713aSLionel Sambuc case EK_Parameter:
2650f4a2713aSLionel Sambuc case EK_Parameter_CF_Audited:
2651f4a2713aSLionel Sambuc case EK_Member:
2652f4a2713aSLionel Sambuc case EK_New:
2653f4a2713aSLionel Sambuc case EK_Temporary:
2654f4a2713aSLionel Sambuc case EK_CompoundLiteralInit:
2655f4a2713aSLionel Sambuc case EK_Base:
2656f4a2713aSLionel Sambuc case EK_Delegating:
2657f4a2713aSLionel Sambuc case EK_ArrayElement:
2658f4a2713aSLionel Sambuc case EK_VectorElement:
2659f4a2713aSLionel Sambuc case EK_ComplexElement:
2660f4a2713aSLionel Sambuc case EK_BlockElement:
2661f4a2713aSLionel Sambuc case EK_LambdaCapture:
2662f4a2713aSLionel Sambuc case EK_RelatedResult:
2663f4a2713aSLionel Sambuc break;
2664f4a2713aSLionel Sambuc }
2665f4a2713aSLionel Sambuc
2666f4a2713aSLionel Sambuc return false;
2667f4a2713aSLionel Sambuc }
2668f4a2713aSLionel Sambuc
dumpImpl(raw_ostream & OS) const2669f4a2713aSLionel Sambuc unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const {
2670f4a2713aSLionel Sambuc assert(getParent() != this);
2671f4a2713aSLionel Sambuc unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0;
2672f4a2713aSLionel Sambuc for (unsigned I = 0; I != Depth; ++I)
2673f4a2713aSLionel Sambuc OS << "`-";
2674f4a2713aSLionel Sambuc
2675f4a2713aSLionel Sambuc switch (getKind()) {
2676f4a2713aSLionel Sambuc case EK_Variable: OS << "Variable"; break;
2677f4a2713aSLionel Sambuc case EK_Parameter: OS << "Parameter"; break;
2678f4a2713aSLionel Sambuc case EK_Parameter_CF_Audited: OS << "CF audited function Parameter";
2679f4a2713aSLionel Sambuc break;
2680f4a2713aSLionel Sambuc case EK_Result: OS << "Result"; break;
2681f4a2713aSLionel Sambuc case EK_Exception: OS << "Exception"; break;
2682f4a2713aSLionel Sambuc case EK_Member: OS << "Member"; break;
2683f4a2713aSLionel Sambuc case EK_New: OS << "New"; break;
2684f4a2713aSLionel Sambuc case EK_Temporary: OS << "Temporary"; break;
2685f4a2713aSLionel Sambuc case EK_CompoundLiteralInit: OS << "CompoundLiteral";break;
2686f4a2713aSLionel Sambuc case EK_RelatedResult: OS << "RelatedResult"; break;
2687f4a2713aSLionel Sambuc case EK_Base: OS << "Base"; break;
2688f4a2713aSLionel Sambuc case EK_Delegating: OS << "Delegating"; break;
2689f4a2713aSLionel Sambuc case EK_ArrayElement: OS << "ArrayElement " << Index; break;
2690f4a2713aSLionel Sambuc case EK_VectorElement: OS << "VectorElement " << Index; break;
2691f4a2713aSLionel Sambuc case EK_ComplexElement: OS << "ComplexElement " << Index; break;
2692f4a2713aSLionel Sambuc case EK_BlockElement: OS << "Block"; break;
2693f4a2713aSLionel Sambuc case EK_LambdaCapture:
2694f4a2713aSLionel Sambuc OS << "LambdaCapture ";
2695*0a6a1f1dSLionel Sambuc OS << DeclarationName(Capture.VarID);
2696f4a2713aSLionel Sambuc break;
2697f4a2713aSLionel Sambuc }
2698f4a2713aSLionel Sambuc
2699f4a2713aSLionel Sambuc if (Decl *D = getDecl()) {
2700f4a2713aSLionel Sambuc OS << " ";
2701f4a2713aSLionel Sambuc cast<NamedDecl>(D)->printQualifiedName(OS);
2702f4a2713aSLionel Sambuc }
2703f4a2713aSLionel Sambuc
2704f4a2713aSLionel Sambuc OS << " '" << getType().getAsString() << "'\n";
2705f4a2713aSLionel Sambuc
2706f4a2713aSLionel Sambuc return Depth + 1;
2707f4a2713aSLionel Sambuc }
2708f4a2713aSLionel Sambuc
dump() const2709f4a2713aSLionel Sambuc void InitializedEntity::dump() const {
2710f4a2713aSLionel Sambuc dumpImpl(llvm::errs());
2711f4a2713aSLionel Sambuc }
2712f4a2713aSLionel Sambuc
2713f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2714f4a2713aSLionel Sambuc // Initialization sequence
2715f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
2716f4a2713aSLionel Sambuc
Destroy()2717f4a2713aSLionel Sambuc void InitializationSequence::Step::Destroy() {
2718f4a2713aSLionel Sambuc switch (Kind) {
2719f4a2713aSLionel Sambuc case SK_ResolveAddressOfOverloadedFunction:
2720f4a2713aSLionel Sambuc case SK_CastDerivedToBaseRValue:
2721f4a2713aSLionel Sambuc case SK_CastDerivedToBaseXValue:
2722f4a2713aSLionel Sambuc case SK_CastDerivedToBaseLValue:
2723f4a2713aSLionel Sambuc case SK_BindReference:
2724f4a2713aSLionel Sambuc case SK_BindReferenceToTemporary:
2725f4a2713aSLionel Sambuc case SK_ExtraneousCopyToTemporary:
2726f4a2713aSLionel Sambuc case SK_UserConversion:
2727f4a2713aSLionel Sambuc case SK_QualificationConversionRValue:
2728f4a2713aSLionel Sambuc case SK_QualificationConversionXValue:
2729f4a2713aSLionel Sambuc case SK_QualificationConversionLValue:
2730*0a6a1f1dSLionel Sambuc case SK_AtomicConversion:
2731f4a2713aSLionel Sambuc case SK_LValueToRValue:
2732f4a2713aSLionel Sambuc case SK_ListInitialization:
2733f4a2713aSLionel Sambuc case SK_UnwrapInitList:
2734f4a2713aSLionel Sambuc case SK_RewrapInitList:
2735f4a2713aSLionel Sambuc case SK_ConstructorInitialization:
2736*0a6a1f1dSLionel Sambuc case SK_ConstructorInitializationFromList:
2737f4a2713aSLionel Sambuc case SK_ZeroInitialization:
2738f4a2713aSLionel Sambuc case SK_CAssignment:
2739f4a2713aSLionel Sambuc case SK_StringInit:
2740f4a2713aSLionel Sambuc case SK_ObjCObjectConversion:
2741f4a2713aSLionel Sambuc case SK_ArrayInit:
2742f4a2713aSLionel Sambuc case SK_ParenthesizedArrayInit:
2743f4a2713aSLionel Sambuc case SK_PassByIndirectCopyRestore:
2744f4a2713aSLionel Sambuc case SK_PassByIndirectRestore:
2745f4a2713aSLionel Sambuc case SK_ProduceObjCObject:
2746f4a2713aSLionel Sambuc case SK_StdInitializerList:
2747*0a6a1f1dSLionel Sambuc case SK_StdInitializerListConstructorCall:
2748f4a2713aSLionel Sambuc case SK_OCLSamplerInit:
2749f4a2713aSLionel Sambuc case SK_OCLZeroEvent:
2750f4a2713aSLionel Sambuc break;
2751f4a2713aSLionel Sambuc
2752f4a2713aSLionel Sambuc case SK_ConversionSequence:
2753f4a2713aSLionel Sambuc case SK_ConversionSequenceNoNarrowing:
2754f4a2713aSLionel Sambuc delete ICS;
2755f4a2713aSLionel Sambuc }
2756f4a2713aSLionel Sambuc }
2757f4a2713aSLionel Sambuc
isDirectReferenceBinding() const2758f4a2713aSLionel Sambuc bool InitializationSequence::isDirectReferenceBinding() const {
2759f4a2713aSLionel Sambuc return !Steps.empty() && Steps.back().Kind == SK_BindReference;
2760f4a2713aSLionel Sambuc }
2761f4a2713aSLionel Sambuc
isAmbiguous() const2762f4a2713aSLionel Sambuc bool InitializationSequence::isAmbiguous() const {
2763f4a2713aSLionel Sambuc if (!Failed())
2764f4a2713aSLionel Sambuc return false;
2765f4a2713aSLionel Sambuc
2766f4a2713aSLionel Sambuc switch (getFailureKind()) {
2767f4a2713aSLionel Sambuc case FK_TooManyInitsForReference:
2768f4a2713aSLionel Sambuc case FK_ArrayNeedsInitList:
2769f4a2713aSLionel Sambuc case FK_ArrayNeedsInitListOrStringLiteral:
2770f4a2713aSLionel Sambuc case FK_ArrayNeedsInitListOrWideStringLiteral:
2771f4a2713aSLionel Sambuc case FK_NarrowStringIntoWideCharArray:
2772f4a2713aSLionel Sambuc case FK_WideStringIntoCharArray:
2773f4a2713aSLionel Sambuc case FK_IncompatWideStringIntoWideChar:
2774f4a2713aSLionel Sambuc case FK_AddressOfOverloadFailed: // FIXME: Could do better
2775f4a2713aSLionel Sambuc case FK_NonConstLValueReferenceBindingToTemporary:
2776f4a2713aSLionel Sambuc case FK_NonConstLValueReferenceBindingToUnrelated:
2777f4a2713aSLionel Sambuc case FK_RValueReferenceBindingToLValue:
2778f4a2713aSLionel Sambuc case FK_ReferenceInitDropsQualifiers:
2779f4a2713aSLionel Sambuc case FK_ReferenceInitFailed:
2780f4a2713aSLionel Sambuc case FK_ConversionFailed:
2781f4a2713aSLionel Sambuc case FK_ConversionFromPropertyFailed:
2782f4a2713aSLionel Sambuc case FK_TooManyInitsForScalar:
2783f4a2713aSLionel Sambuc case FK_ReferenceBindingToInitList:
2784f4a2713aSLionel Sambuc case FK_InitListBadDestinationType:
2785f4a2713aSLionel Sambuc case FK_DefaultInitOfConst:
2786f4a2713aSLionel Sambuc case FK_Incomplete:
2787f4a2713aSLionel Sambuc case FK_ArrayTypeMismatch:
2788f4a2713aSLionel Sambuc case FK_NonConstantArrayInit:
2789f4a2713aSLionel Sambuc case FK_ListInitializationFailed:
2790f4a2713aSLionel Sambuc case FK_VariableLengthArrayHasInitializer:
2791f4a2713aSLionel Sambuc case FK_PlaceholderType:
2792f4a2713aSLionel Sambuc case FK_ExplicitConstructor:
2793f4a2713aSLionel Sambuc return false;
2794f4a2713aSLionel Sambuc
2795f4a2713aSLionel Sambuc case FK_ReferenceInitOverloadFailed:
2796f4a2713aSLionel Sambuc case FK_UserConversionOverloadFailed:
2797f4a2713aSLionel Sambuc case FK_ConstructorOverloadFailed:
2798f4a2713aSLionel Sambuc case FK_ListConstructorOverloadFailed:
2799f4a2713aSLionel Sambuc return FailedOverloadResult == OR_Ambiguous;
2800f4a2713aSLionel Sambuc }
2801f4a2713aSLionel Sambuc
2802f4a2713aSLionel Sambuc llvm_unreachable("Invalid EntityKind!");
2803f4a2713aSLionel Sambuc }
2804f4a2713aSLionel Sambuc
isConstructorInitialization() const2805f4a2713aSLionel Sambuc bool InitializationSequence::isConstructorInitialization() const {
2806f4a2713aSLionel Sambuc return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization;
2807f4a2713aSLionel Sambuc }
2808f4a2713aSLionel Sambuc
2809f4a2713aSLionel Sambuc void
2810f4a2713aSLionel Sambuc InitializationSequence
AddAddressOverloadResolutionStep(FunctionDecl * Function,DeclAccessPair Found,bool HadMultipleCandidates)2811f4a2713aSLionel Sambuc ::AddAddressOverloadResolutionStep(FunctionDecl *Function,
2812f4a2713aSLionel Sambuc DeclAccessPair Found,
2813f4a2713aSLionel Sambuc bool HadMultipleCandidates) {
2814f4a2713aSLionel Sambuc Step S;
2815f4a2713aSLionel Sambuc S.Kind = SK_ResolveAddressOfOverloadedFunction;
2816f4a2713aSLionel Sambuc S.Type = Function->getType();
2817f4a2713aSLionel Sambuc S.Function.HadMultipleCandidates = HadMultipleCandidates;
2818f4a2713aSLionel Sambuc S.Function.Function = Function;
2819f4a2713aSLionel Sambuc S.Function.FoundDecl = Found;
2820f4a2713aSLionel Sambuc Steps.push_back(S);
2821f4a2713aSLionel Sambuc }
2822f4a2713aSLionel Sambuc
AddDerivedToBaseCastStep(QualType BaseType,ExprValueKind VK)2823f4a2713aSLionel Sambuc void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType,
2824f4a2713aSLionel Sambuc ExprValueKind VK) {
2825f4a2713aSLionel Sambuc Step S;
2826f4a2713aSLionel Sambuc switch (VK) {
2827f4a2713aSLionel Sambuc case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break;
2828f4a2713aSLionel Sambuc case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break;
2829f4a2713aSLionel Sambuc case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break;
2830f4a2713aSLionel Sambuc }
2831f4a2713aSLionel Sambuc S.Type = BaseType;
2832f4a2713aSLionel Sambuc Steps.push_back(S);
2833f4a2713aSLionel Sambuc }
2834f4a2713aSLionel Sambuc
AddReferenceBindingStep(QualType T,bool BindingTemporary)2835f4a2713aSLionel Sambuc void InitializationSequence::AddReferenceBindingStep(QualType T,
2836f4a2713aSLionel Sambuc bool BindingTemporary) {
2837f4a2713aSLionel Sambuc Step S;
2838f4a2713aSLionel Sambuc S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference;
2839f4a2713aSLionel Sambuc S.Type = T;
2840f4a2713aSLionel Sambuc Steps.push_back(S);
2841f4a2713aSLionel Sambuc }
2842f4a2713aSLionel Sambuc
AddExtraneousCopyToTemporary(QualType T)2843f4a2713aSLionel Sambuc void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) {
2844f4a2713aSLionel Sambuc Step S;
2845f4a2713aSLionel Sambuc S.Kind = SK_ExtraneousCopyToTemporary;
2846f4a2713aSLionel Sambuc S.Type = T;
2847f4a2713aSLionel Sambuc Steps.push_back(S);
2848f4a2713aSLionel Sambuc }
2849f4a2713aSLionel Sambuc
2850f4a2713aSLionel Sambuc void
AddUserConversionStep(FunctionDecl * Function,DeclAccessPair FoundDecl,QualType T,bool HadMultipleCandidates)2851f4a2713aSLionel Sambuc InitializationSequence::AddUserConversionStep(FunctionDecl *Function,
2852f4a2713aSLionel Sambuc DeclAccessPair FoundDecl,
2853f4a2713aSLionel Sambuc QualType T,
2854f4a2713aSLionel Sambuc bool HadMultipleCandidates) {
2855f4a2713aSLionel Sambuc Step S;
2856f4a2713aSLionel Sambuc S.Kind = SK_UserConversion;
2857f4a2713aSLionel Sambuc S.Type = T;
2858f4a2713aSLionel Sambuc S.Function.HadMultipleCandidates = HadMultipleCandidates;
2859f4a2713aSLionel Sambuc S.Function.Function = Function;
2860f4a2713aSLionel Sambuc S.Function.FoundDecl = FoundDecl;
2861f4a2713aSLionel Sambuc Steps.push_back(S);
2862f4a2713aSLionel Sambuc }
2863f4a2713aSLionel Sambuc
AddQualificationConversionStep(QualType Ty,ExprValueKind VK)2864f4a2713aSLionel Sambuc void InitializationSequence::AddQualificationConversionStep(QualType Ty,
2865f4a2713aSLionel Sambuc ExprValueKind VK) {
2866f4a2713aSLionel Sambuc Step S;
2867f4a2713aSLionel Sambuc S.Kind = SK_QualificationConversionRValue; // work around a gcc warning
2868f4a2713aSLionel Sambuc switch (VK) {
2869f4a2713aSLionel Sambuc case VK_RValue:
2870f4a2713aSLionel Sambuc S.Kind = SK_QualificationConversionRValue;
2871f4a2713aSLionel Sambuc break;
2872f4a2713aSLionel Sambuc case VK_XValue:
2873f4a2713aSLionel Sambuc S.Kind = SK_QualificationConversionXValue;
2874f4a2713aSLionel Sambuc break;
2875f4a2713aSLionel Sambuc case VK_LValue:
2876f4a2713aSLionel Sambuc S.Kind = SK_QualificationConversionLValue;
2877f4a2713aSLionel Sambuc break;
2878f4a2713aSLionel Sambuc }
2879f4a2713aSLionel Sambuc S.Type = Ty;
2880f4a2713aSLionel Sambuc Steps.push_back(S);
2881f4a2713aSLionel Sambuc }
2882f4a2713aSLionel Sambuc
AddAtomicConversionStep(QualType Ty)2883*0a6a1f1dSLionel Sambuc void InitializationSequence::AddAtomicConversionStep(QualType Ty) {
2884*0a6a1f1dSLionel Sambuc Step S;
2885*0a6a1f1dSLionel Sambuc S.Kind = SK_AtomicConversion;
2886*0a6a1f1dSLionel Sambuc S.Type = Ty;
2887*0a6a1f1dSLionel Sambuc Steps.push_back(S);
2888*0a6a1f1dSLionel Sambuc }
2889*0a6a1f1dSLionel Sambuc
AddLValueToRValueStep(QualType Ty)2890f4a2713aSLionel Sambuc void InitializationSequence::AddLValueToRValueStep(QualType Ty) {
2891f4a2713aSLionel Sambuc assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers");
2892f4a2713aSLionel Sambuc
2893f4a2713aSLionel Sambuc Step S;
2894f4a2713aSLionel Sambuc S.Kind = SK_LValueToRValue;
2895f4a2713aSLionel Sambuc S.Type = Ty;
2896f4a2713aSLionel Sambuc Steps.push_back(S);
2897f4a2713aSLionel Sambuc }
2898f4a2713aSLionel Sambuc
AddConversionSequenceStep(const ImplicitConversionSequence & ICS,QualType T,bool TopLevelOfInitList)2899f4a2713aSLionel Sambuc void InitializationSequence::AddConversionSequenceStep(
2900f4a2713aSLionel Sambuc const ImplicitConversionSequence &ICS, QualType T,
2901f4a2713aSLionel Sambuc bool TopLevelOfInitList) {
2902f4a2713aSLionel Sambuc Step S;
2903f4a2713aSLionel Sambuc S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing
2904f4a2713aSLionel Sambuc : SK_ConversionSequence;
2905f4a2713aSLionel Sambuc S.Type = T;
2906f4a2713aSLionel Sambuc S.ICS = new ImplicitConversionSequence(ICS);
2907f4a2713aSLionel Sambuc Steps.push_back(S);
2908f4a2713aSLionel Sambuc }
2909f4a2713aSLionel Sambuc
AddListInitializationStep(QualType T)2910f4a2713aSLionel Sambuc void InitializationSequence::AddListInitializationStep(QualType T) {
2911f4a2713aSLionel Sambuc Step S;
2912f4a2713aSLionel Sambuc S.Kind = SK_ListInitialization;
2913f4a2713aSLionel Sambuc S.Type = T;
2914f4a2713aSLionel Sambuc Steps.push_back(S);
2915f4a2713aSLionel Sambuc }
2916f4a2713aSLionel Sambuc
2917f4a2713aSLionel Sambuc void
2918f4a2713aSLionel Sambuc InitializationSequence
AddConstructorInitializationStep(CXXConstructorDecl * Constructor,AccessSpecifier Access,QualType T,bool HadMultipleCandidates,bool FromInitList,bool AsInitList)2919f4a2713aSLionel Sambuc ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor,
2920f4a2713aSLionel Sambuc AccessSpecifier Access,
2921f4a2713aSLionel Sambuc QualType T,
2922f4a2713aSLionel Sambuc bool HadMultipleCandidates,
2923f4a2713aSLionel Sambuc bool FromInitList, bool AsInitList) {
2924f4a2713aSLionel Sambuc Step S;
2925*0a6a1f1dSLionel Sambuc S.Kind = FromInitList ? AsInitList ? SK_StdInitializerListConstructorCall
2926*0a6a1f1dSLionel Sambuc : SK_ConstructorInitializationFromList
2927f4a2713aSLionel Sambuc : SK_ConstructorInitialization;
2928f4a2713aSLionel Sambuc S.Type = T;
2929f4a2713aSLionel Sambuc S.Function.HadMultipleCandidates = HadMultipleCandidates;
2930f4a2713aSLionel Sambuc S.Function.Function = Constructor;
2931f4a2713aSLionel Sambuc S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access);
2932f4a2713aSLionel Sambuc Steps.push_back(S);
2933f4a2713aSLionel Sambuc }
2934f4a2713aSLionel Sambuc
AddZeroInitializationStep(QualType T)2935f4a2713aSLionel Sambuc void InitializationSequence::AddZeroInitializationStep(QualType T) {
2936f4a2713aSLionel Sambuc Step S;
2937f4a2713aSLionel Sambuc S.Kind = SK_ZeroInitialization;
2938f4a2713aSLionel Sambuc S.Type = T;
2939f4a2713aSLionel Sambuc Steps.push_back(S);
2940f4a2713aSLionel Sambuc }
2941f4a2713aSLionel Sambuc
AddCAssignmentStep(QualType T)2942f4a2713aSLionel Sambuc void InitializationSequence::AddCAssignmentStep(QualType T) {
2943f4a2713aSLionel Sambuc Step S;
2944f4a2713aSLionel Sambuc S.Kind = SK_CAssignment;
2945f4a2713aSLionel Sambuc S.Type = T;
2946f4a2713aSLionel Sambuc Steps.push_back(S);
2947f4a2713aSLionel Sambuc }
2948f4a2713aSLionel Sambuc
AddStringInitStep(QualType T)2949f4a2713aSLionel Sambuc void InitializationSequence::AddStringInitStep(QualType T) {
2950f4a2713aSLionel Sambuc Step S;
2951f4a2713aSLionel Sambuc S.Kind = SK_StringInit;
2952f4a2713aSLionel Sambuc S.Type = T;
2953f4a2713aSLionel Sambuc Steps.push_back(S);
2954f4a2713aSLionel Sambuc }
2955f4a2713aSLionel Sambuc
AddObjCObjectConversionStep(QualType T)2956f4a2713aSLionel Sambuc void InitializationSequence::AddObjCObjectConversionStep(QualType T) {
2957f4a2713aSLionel Sambuc Step S;
2958f4a2713aSLionel Sambuc S.Kind = SK_ObjCObjectConversion;
2959f4a2713aSLionel Sambuc S.Type = T;
2960f4a2713aSLionel Sambuc Steps.push_back(S);
2961f4a2713aSLionel Sambuc }
2962f4a2713aSLionel Sambuc
AddArrayInitStep(QualType T)2963f4a2713aSLionel Sambuc void InitializationSequence::AddArrayInitStep(QualType T) {
2964f4a2713aSLionel Sambuc Step S;
2965f4a2713aSLionel Sambuc S.Kind = SK_ArrayInit;
2966f4a2713aSLionel Sambuc S.Type = T;
2967f4a2713aSLionel Sambuc Steps.push_back(S);
2968f4a2713aSLionel Sambuc }
2969f4a2713aSLionel Sambuc
AddParenthesizedArrayInitStep(QualType T)2970f4a2713aSLionel Sambuc void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) {
2971f4a2713aSLionel Sambuc Step S;
2972f4a2713aSLionel Sambuc S.Kind = SK_ParenthesizedArrayInit;
2973f4a2713aSLionel Sambuc S.Type = T;
2974f4a2713aSLionel Sambuc Steps.push_back(S);
2975f4a2713aSLionel Sambuc }
2976f4a2713aSLionel Sambuc
AddPassByIndirectCopyRestoreStep(QualType type,bool shouldCopy)2977f4a2713aSLionel Sambuc void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type,
2978f4a2713aSLionel Sambuc bool shouldCopy) {
2979f4a2713aSLionel Sambuc Step s;
2980f4a2713aSLionel Sambuc s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore
2981f4a2713aSLionel Sambuc : SK_PassByIndirectRestore);
2982f4a2713aSLionel Sambuc s.Type = type;
2983f4a2713aSLionel Sambuc Steps.push_back(s);
2984f4a2713aSLionel Sambuc }
2985f4a2713aSLionel Sambuc
AddProduceObjCObjectStep(QualType T)2986f4a2713aSLionel Sambuc void InitializationSequence::AddProduceObjCObjectStep(QualType T) {
2987f4a2713aSLionel Sambuc Step S;
2988f4a2713aSLionel Sambuc S.Kind = SK_ProduceObjCObject;
2989f4a2713aSLionel Sambuc S.Type = T;
2990f4a2713aSLionel Sambuc Steps.push_back(S);
2991f4a2713aSLionel Sambuc }
2992f4a2713aSLionel Sambuc
AddStdInitializerListConstructionStep(QualType T)2993f4a2713aSLionel Sambuc void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) {
2994f4a2713aSLionel Sambuc Step S;
2995f4a2713aSLionel Sambuc S.Kind = SK_StdInitializerList;
2996f4a2713aSLionel Sambuc S.Type = T;
2997f4a2713aSLionel Sambuc Steps.push_back(S);
2998f4a2713aSLionel Sambuc }
2999f4a2713aSLionel Sambuc
AddOCLSamplerInitStep(QualType T)3000f4a2713aSLionel Sambuc void InitializationSequence::AddOCLSamplerInitStep(QualType T) {
3001f4a2713aSLionel Sambuc Step S;
3002f4a2713aSLionel Sambuc S.Kind = SK_OCLSamplerInit;
3003f4a2713aSLionel Sambuc S.Type = T;
3004f4a2713aSLionel Sambuc Steps.push_back(S);
3005f4a2713aSLionel Sambuc }
3006f4a2713aSLionel Sambuc
AddOCLZeroEventStep(QualType T)3007f4a2713aSLionel Sambuc void InitializationSequence::AddOCLZeroEventStep(QualType T) {
3008f4a2713aSLionel Sambuc Step S;
3009f4a2713aSLionel Sambuc S.Kind = SK_OCLZeroEvent;
3010f4a2713aSLionel Sambuc S.Type = T;
3011f4a2713aSLionel Sambuc Steps.push_back(S);
3012f4a2713aSLionel Sambuc }
3013f4a2713aSLionel Sambuc
RewrapReferenceInitList(QualType T,InitListExpr * Syntactic)3014f4a2713aSLionel Sambuc void InitializationSequence::RewrapReferenceInitList(QualType T,
3015f4a2713aSLionel Sambuc InitListExpr *Syntactic) {
3016f4a2713aSLionel Sambuc assert(Syntactic->getNumInits() == 1 &&
3017f4a2713aSLionel Sambuc "Can only rewrap trivial init lists.");
3018f4a2713aSLionel Sambuc Step S;
3019f4a2713aSLionel Sambuc S.Kind = SK_UnwrapInitList;
3020f4a2713aSLionel Sambuc S.Type = Syntactic->getInit(0)->getType();
3021f4a2713aSLionel Sambuc Steps.insert(Steps.begin(), S);
3022f4a2713aSLionel Sambuc
3023f4a2713aSLionel Sambuc S.Kind = SK_RewrapInitList;
3024f4a2713aSLionel Sambuc S.Type = T;
3025f4a2713aSLionel Sambuc S.WrappingSyntacticList = Syntactic;
3026f4a2713aSLionel Sambuc Steps.push_back(S);
3027f4a2713aSLionel Sambuc }
3028f4a2713aSLionel Sambuc
SetOverloadFailure(FailureKind Failure,OverloadingResult Result)3029f4a2713aSLionel Sambuc void InitializationSequence::SetOverloadFailure(FailureKind Failure,
3030f4a2713aSLionel Sambuc OverloadingResult Result) {
3031f4a2713aSLionel Sambuc setSequenceKind(FailedSequence);
3032f4a2713aSLionel Sambuc this->Failure = Failure;
3033f4a2713aSLionel Sambuc this->FailedOverloadResult = Result;
3034f4a2713aSLionel Sambuc }
3035f4a2713aSLionel Sambuc
3036f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3037f4a2713aSLionel Sambuc // Attempt initialization
3038f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
3039f4a2713aSLionel Sambuc
MaybeProduceObjCObject(Sema & S,InitializationSequence & Sequence,const InitializedEntity & Entity)3040f4a2713aSLionel Sambuc static void MaybeProduceObjCObject(Sema &S,
3041f4a2713aSLionel Sambuc InitializationSequence &Sequence,
3042f4a2713aSLionel Sambuc const InitializedEntity &Entity) {
3043f4a2713aSLionel Sambuc if (!S.getLangOpts().ObjCAutoRefCount) return;
3044f4a2713aSLionel Sambuc
3045f4a2713aSLionel Sambuc /// When initializing a parameter, produce the value if it's marked
3046f4a2713aSLionel Sambuc /// __attribute__((ns_consumed)).
3047f4a2713aSLionel Sambuc if (Entity.isParameterKind()) {
3048f4a2713aSLionel Sambuc if (!Entity.isParameterConsumed())
3049f4a2713aSLionel Sambuc return;
3050f4a2713aSLionel Sambuc
3051f4a2713aSLionel Sambuc assert(Entity.getType()->isObjCRetainableType() &&
3052f4a2713aSLionel Sambuc "consuming an object of unretainable type?");
3053f4a2713aSLionel Sambuc Sequence.AddProduceObjCObjectStep(Entity.getType());
3054f4a2713aSLionel Sambuc
3055f4a2713aSLionel Sambuc /// When initializing a return value, if the return type is a
3056f4a2713aSLionel Sambuc /// retainable type, then returns need to immediately retain the
3057f4a2713aSLionel Sambuc /// object. If an autorelease is required, it will be done at the
3058f4a2713aSLionel Sambuc /// last instant.
3059f4a2713aSLionel Sambuc } else if (Entity.getKind() == InitializedEntity::EK_Result) {
3060f4a2713aSLionel Sambuc if (!Entity.getType()->isObjCRetainableType())
3061f4a2713aSLionel Sambuc return;
3062f4a2713aSLionel Sambuc
3063f4a2713aSLionel Sambuc Sequence.AddProduceObjCObjectStep(Entity.getType());
3064f4a2713aSLionel Sambuc }
3065f4a2713aSLionel Sambuc }
3066f4a2713aSLionel Sambuc
3067f4a2713aSLionel Sambuc static void TryListInitialization(Sema &S,
3068f4a2713aSLionel Sambuc const InitializedEntity &Entity,
3069f4a2713aSLionel Sambuc const InitializationKind &Kind,
3070f4a2713aSLionel Sambuc InitListExpr *InitList,
3071f4a2713aSLionel Sambuc InitializationSequence &Sequence);
3072f4a2713aSLionel Sambuc
3073f4a2713aSLionel Sambuc /// \brief When initializing from init list via constructor, handle
3074f4a2713aSLionel Sambuc /// initialization of an object of type std::initializer_list<T>.
3075f4a2713aSLionel Sambuc ///
3076f4a2713aSLionel Sambuc /// \return true if we have handled initialization of an object of type
3077f4a2713aSLionel Sambuc /// std::initializer_list<T>, false otherwise.
TryInitializerListConstruction(Sema & S,InitListExpr * List,QualType DestType,InitializationSequence & Sequence)3078f4a2713aSLionel Sambuc static bool TryInitializerListConstruction(Sema &S,
3079f4a2713aSLionel Sambuc InitListExpr *List,
3080f4a2713aSLionel Sambuc QualType DestType,
3081f4a2713aSLionel Sambuc InitializationSequence &Sequence) {
3082f4a2713aSLionel Sambuc QualType E;
3083f4a2713aSLionel Sambuc if (!S.isStdInitializerList(DestType, &E))
3084f4a2713aSLionel Sambuc return false;
3085f4a2713aSLionel Sambuc
3086f4a2713aSLionel Sambuc if (S.RequireCompleteType(List->getExprLoc(), E, 0)) {
3087f4a2713aSLionel Sambuc Sequence.setIncompleteTypeFailure(E);
3088f4a2713aSLionel Sambuc return true;
3089f4a2713aSLionel Sambuc }
3090f4a2713aSLionel Sambuc
3091f4a2713aSLionel Sambuc // Try initializing a temporary array from the init list.
3092f4a2713aSLionel Sambuc QualType ArrayType = S.Context.getConstantArrayType(
3093f4a2713aSLionel Sambuc E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()),
3094f4a2713aSLionel Sambuc List->getNumInits()),
3095f4a2713aSLionel Sambuc clang::ArrayType::Normal, 0);
3096f4a2713aSLionel Sambuc InitializedEntity HiddenArray =
3097f4a2713aSLionel Sambuc InitializedEntity::InitializeTemporary(ArrayType);
3098f4a2713aSLionel Sambuc InitializationKind Kind =
3099f4a2713aSLionel Sambuc InitializationKind::CreateDirectList(List->getExprLoc());
3100f4a2713aSLionel Sambuc TryListInitialization(S, HiddenArray, Kind, List, Sequence);
3101f4a2713aSLionel Sambuc if (Sequence)
3102f4a2713aSLionel Sambuc Sequence.AddStdInitializerListConstructionStep(DestType);
3103f4a2713aSLionel Sambuc return true;
3104f4a2713aSLionel Sambuc }
3105f4a2713aSLionel Sambuc
3106f4a2713aSLionel Sambuc static OverloadingResult
ResolveConstructorOverload(Sema & S,SourceLocation DeclLoc,MultiExprArg Args,OverloadCandidateSet & CandidateSet,ArrayRef<NamedDecl * > Ctors,OverloadCandidateSet::iterator & Best,bool CopyInitializing,bool AllowExplicit,bool OnlyListConstructors,bool InitListSyntax)3107f4a2713aSLionel Sambuc ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc,
3108f4a2713aSLionel Sambuc MultiExprArg Args,
3109f4a2713aSLionel Sambuc OverloadCandidateSet &CandidateSet,
3110f4a2713aSLionel Sambuc ArrayRef<NamedDecl *> Ctors,
3111f4a2713aSLionel Sambuc OverloadCandidateSet::iterator &Best,
3112f4a2713aSLionel Sambuc bool CopyInitializing, bool AllowExplicit,
3113f4a2713aSLionel Sambuc bool OnlyListConstructors, bool InitListSyntax) {
3114f4a2713aSLionel Sambuc CandidateSet.clear();
3115f4a2713aSLionel Sambuc
3116f4a2713aSLionel Sambuc for (ArrayRef<NamedDecl *>::iterator
3117f4a2713aSLionel Sambuc Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) {
3118f4a2713aSLionel Sambuc NamedDecl *D = *Con;
3119f4a2713aSLionel Sambuc DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
3120f4a2713aSLionel Sambuc bool SuppressUserConversions = false;
3121f4a2713aSLionel Sambuc
3122f4a2713aSLionel Sambuc // Find the constructor (which may be a template).
3123*0a6a1f1dSLionel Sambuc CXXConstructorDecl *Constructor = nullptr;
3124f4a2713aSLionel Sambuc FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
3125f4a2713aSLionel Sambuc if (ConstructorTmpl)
3126f4a2713aSLionel Sambuc Constructor = cast<CXXConstructorDecl>(
3127f4a2713aSLionel Sambuc ConstructorTmpl->getTemplatedDecl());
3128f4a2713aSLionel Sambuc else {
3129f4a2713aSLionel Sambuc Constructor = cast<CXXConstructorDecl>(D);
3130f4a2713aSLionel Sambuc
3131f4a2713aSLionel Sambuc // C++11 [over.best.ics]p4:
3132f4a2713aSLionel Sambuc // However, when considering the argument of a constructor or
3133f4a2713aSLionel Sambuc // user-defined conversion function that is a candidate:
3134f4a2713aSLionel Sambuc // -- by 13.3.1.3 when invoked for the copying/moving of a temporary
3135f4a2713aSLionel Sambuc // in the second step of a class copy-initialization,
3136f4a2713aSLionel Sambuc // -- by 13.3.1.7 when passing the initializer list as a single
3137f4a2713aSLionel Sambuc // argument or when the initializer list has exactly one elementand
3138f4a2713aSLionel Sambuc // a conversion to some class X or reference to (possibly
3139f4a2713aSLionel Sambuc // cv-qualified) X is considered for the first parameter of a
3140f4a2713aSLionel Sambuc // constructor of X, or
3141f4a2713aSLionel Sambuc // -- by 13.3.1.4, 13.3.1.5, or 13.3.1.6 in all cases,
3142f4a2713aSLionel Sambuc // only standard conversion sequences and ellipsis conversion sequences
3143f4a2713aSLionel Sambuc // are considered.
3144f4a2713aSLionel Sambuc if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) &&
3145f4a2713aSLionel Sambuc Constructor->isCopyOrMoveConstructor())
3146f4a2713aSLionel Sambuc SuppressUserConversions = true;
3147f4a2713aSLionel Sambuc }
3148f4a2713aSLionel Sambuc
3149f4a2713aSLionel Sambuc if (!Constructor->isInvalidDecl() &&
3150f4a2713aSLionel Sambuc (AllowExplicit || !Constructor->isExplicit()) &&
3151f4a2713aSLionel Sambuc (!OnlyListConstructors || S.isInitListConstructor(Constructor))) {
3152f4a2713aSLionel Sambuc if (ConstructorTmpl)
3153f4a2713aSLionel Sambuc S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
3154*0a6a1f1dSLionel Sambuc /*ExplicitArgs*/ nullptr, Args,
3155f4a2713aSLionel Sambuc CandidateSet, SuppressUserConversions);
3156f4a2713aSLionel Sambuc else {
3157f4a2713aSLionel Sambuc // C++ [over.match.copy]p1:
3158f4a2713aSLionel Sambuc // - When initializing a temporary to be bound to the first parameter
3159f4a2713aSLionel Sambuc // of a constructor that takes a reference to possibly cv-qualified
3160f4a2713aSLionel Sambuc // T as its first argument, called with a single argument in the
3161f4a2713aSLionel Sambuc // context of direct-initialization, explicit conversion functions
3162f4a2713aSLionel Sambuc // are also considered.
3163f4a2713aSLionel Sambuc bool AllowExplicitConv = AllowExplicit && !CopyInitializing &&
3164f4a2713aSLionel Sambuc Args.size() == 1 &&
3165f4a2713aSLionel Sambuc Constructor->isCopyOrMoveConstructor();
3166f4a2713aSLionel Sambuc S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet,
3167f4a2713aSLionel Sambuc SuppressUserConversions,
3168f4a2713aSLionel Sambuc /*PartialOverloading=*/false,
3169f4a2713aSLionel Sambuc /*AllowExplicit=*/AllowExplicitConv);
3170f4a2713aSLionel Sambuc }
3171f4a2713aSLionel Sambuc }
3172f4a2713aSLionel Sambuc }
3173f4a2713aSLionel Sambuc
3174f4a2713aSLionel Sambuc // Perform overload resolution and return the result.
3175f4a2713aSLionel Sambuc return CandidateSet.BestViableFunction(S, DeclLoc, Best);
3176f4a2713aSLionel Sambuc }
3177f4a2713aSLionel Sambuc
3178f4a2713aSLionel Sambuc /// \brief Attempt initialization by constructor (C++ [dcl.init]), which
3179f4a2713aSLionel Sambuc /// enumerates the constructors of the initialized entity and performs overload
3180f4a2713aSLionel Sambuc /// resolution to select the best.
3181f4a2713aSLionel Sambuc /// If InitListSyntax is true, this is list-initialization of a non-aggregate
3182f4a2713aSLionel Sambuc /// class type.
TryConstructorInitialization(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,MultiExprArg Args,QualType DestType,InitializationSequence & Sequence,bool InitListSyntax=false)3183f4a2713aSLionel Sambuc static void TryConstructorInitialization(Sema &S,
3184f4a2713aSLionel Sambuc const InitializedEntity &Entity,
3185f4a2713aSLionel Sambuc const InitializationKind &Kind,
3186f4a2713aSLionel Sambuc MultiExprArg Args, QualType DestType,
3187f4a2713aSLionel Sambuc InitializationSequence &Sequence,
3188f4a2713aSLionel Sambuc bool InitListSyntax = false) {
3189f4a2713aSLionel Sambuc assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) &&
3190f4a2713aSLionel Sambuc "InitListSyntax must come with a single initializer list argument.");
3191f4a2713aSLionel Sambuc
3192f4a2713aSLionel Sambuc // The type we're constructing needs to be complete.
3193f4a2713aSLionel Sambuc if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
3194f4a2713aSLionel Sambuc Sequence.setIncompleteTypeFailure(DestType);
3195f4a2713aSLionel Sambuc return;
3196f4a2713aSLionel Sambuc }
3197f4a2713aSLionel Sambuc
3198f4a2713aSLionel Sambuc const RecordType *DestRecordType = DestType->getAs<RecordType>();
3199f4a2713aSLionel Sambuc assert(DestRecordType && "Constructor initialization requires record type");
3200f4a2713aSLionel Sambuc CXXRecordDecl *DestRecordDecl
3201f4a2713aSLionel Sambuc = cast<CXXRecordDecl>(DestRecordType->getDecl());
3202f4a2713aSLionel Sambuc
3203f4a2713aSLionel Sambuc // Build the candidate set directly in the initialization sequence
3204f4a2713aSLionel Sambuc // structure, so that it will persist if we fail.
3205f4a2713aSLionel Sambuc OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
3206f4a2713aSLionel Sambuc
3207f4a2713aSLionel Sambuc // Determine whether we are allowed to call explicit constructors or
3208f4a2713aSLionel Sambuc // explicit conversion operators.
3209f4a2713aSLionel Sambuc bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax;
3210f4a2713aSLionel Sambuc bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy;
3211f4a2713aSLionel Sambuc
3212f4a2713aSLionel Sambuc // - Otherwise, if T is a class type, constructors are considered. The
3213f4a2713aSLionel Sambuc // applicable constructors are enumerated, and the best one is chosen
3214f4a2713aSLionel Sambuc // through overload resolution.
3215f4a2713aSLionel Sambuc DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl);
3216f4a2713aSLionel Sambuc // The container holding the constructors can under certain conditions
3217f4a2713aSLionel Sambuc // be changed while iterating (e.g. because of deserialization).
3218f4a2713aSLionel Sambuc // To be safe we copy the lookup results to a new container.
3219f4a2713aSLionel Sambuc SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end());
3220f4a2713aSLionel Sambuc
3221f4a2713aSLionel Sambuc OverloadingResult Result = OR_No_Viable_Function;
3222f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
3223f4a2713aSLionel Sambuc bool AsInitializerList = false;
3224f4a2713aSLionel Sambuc
3225f4a2713aSLionel Sambuc // C++11 [over.match.list]p1:
3226f4a2713aSLionel Sambuc // When objects of non-aggregate type T are list-initialized, overload
3227f4a2713aSLionel Sambuc // resolution selects the constructor in two phases:
3228f4a2713aSLionel Sambuc // - Initially, the candidate functions are the initializer-list
3229f4a2713aSLionel Sambuc // constructors of the class T and the argument list consists of the
3230f4a2713aSLionel Sambuc // initializer list as a single argument.
3231f4a2713aSLionel Sambuc if (InitListSyntax) {
3232f4a2713aSLionel Sambuc InitListExpr *ILE = cast<InitListExpr>(Args[0]);
3233f4a2713aSLionel Sambuc AsInitializerList = true;
3234f4a2713aSLionel Sambuc
3235f4a2713aSLionel Sambuc // If the initializer list has no elements and T has a default constructor,
3236f4a2713aSLionel Sambuc // the first phase is omitted.
3237f4a2713aSLionel Sambuc if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor())
3238f4a2713aSLionel Sambuc Result = ResolveConstructorOverload(S, Kind.getLocation(), Args,
3239f4a2713aSLionel Sambuc CandidateSet, Ctors, Best,
3240f4a2713aSLionel Sambuc CopyInitialization, AllowExplicit,
3241f4a2713aSLionel Sambuc /*OnlyListConstructor=*/true,
3242f4a2713aSLionel Sambuc InitListSyntax);
3243f4a2713aSLionel Sambuc
3244f4a2713aSLionel Sambuc // Time to unwrap the init list.
3245f4a2713aSLionel Sambuc Args = MultiExprArg(ILE->getInits(), ILE->getNumInits());
3246f4a2713aSLionel Sambuc }
3247f4a2713aSLionel Sambuc
3248f4a2713aSLionel Sambuc // C++11 [over.match.list]p1:
3249f4a2713aSLionel Sambuc // - If no viable initializer-list constructor is found, overload resolution
3250f4a2713aSLionel Sambuc // is performed again, where the candidate functions are all the
3251f4a2713aSLionel Sambuc // constructors of the class T and the argument list consists of the
3252f4a2713aSLionel Sambuc // elements of the initializer list.
3253f4a2713aSLionel Sambuc if (Result == OR_No_Viable_Function) {
3254f4a2713aSLionel Sambuc AsInitializerList = false;
3255f4a2713aSLionel Sambuc Result = ResolveConstructorOverload(S, Kind.getLocation(), Args,
3256f4a2713aSLionel Sambuc CandidateSet, Ctors, Best,
3257f4a2713aSLionel Sambuc CopyInitialization, AllowExplicit,
3258f4a2713aSLionel Sambuc /*OnlyListConstructors=*/false,
3259f4a2713aSLionel Sambuc InitListSyntax);
3260f4a2713aSLionel Sambuc }
3261f4a2713aSLionel Sambuc if (Result) {
3262f4a2713aSLionel Sambuc Sequence.SetOverloadFailure(InitListSyntax ?
3263f4a2713aSLionel Sambuc InitializationSequence::FK_ListConstructorOverloadFailed :
3264f4a2713aSLionel Sambuc InitializationSequence::FK_ConstructorOverloadFailed,
3265f4a2713aSLionel Sambuc Result);
3266f4a2713aSLionel Sambuc return;
3267f4a2713aSLionel Sambuc }
3268f4a2713aSLionel Sambuc
3269f4a2713aSLionel Sambuc // C++11 [dcl.init]p6:
3270f4a2713aSLionel Sambuc // If a program calls for the default initialization of an object
3271f4a2713aSLionel Sambuc // of a const-qualified type T, T shall be a class type with a
3272f4a2713aSLionel Sambuc // user-provided default constructor.
3273f4a2713aSLionel Sambuc if (Kind.getKind() == InitializationKind::IK_Default &&
3274f4a2713aSLionel Sambuc Entity.getType().isConstQualified() &&
3275f4a2713aSLionel Sambuc !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) {
3276f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
3277f4a2713aSLionel Sambuc return;
3278f4a2713aSLionel Sambuc }
3279f4a2713aSLionel Sambuc
3280f4a2713aSLionel Sambuc // C++11 [over.match.list]p1:
3281f4a2713aSLionel Sambuc // In copy-list-initialization, if an explicit constructor is chosen, the
3282f4a2713aSLionel Sambuc // initializer is ill-formed.
3283f4a2713aSLionel Sambuc CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function);
3284f4a2713aSLionel Sambuc if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) {
3285f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor);
3286f4a2713aSLionel Sambuc return;
3287f4a2713aSLionel Sambuc }
3288f4a2713aSLionel Sambuc
3289f4a2713aSLionel Sambuc // Add the constructor initialization step. Any cv-qualification conversion is
3290f4a2713aSLionel Sambuc // subsumed by the initialization.
3291f4a2713aSLionel Sambuc bool HadMultipleCandidates = (CandidateSet.size() > 1);
3292f4a2713aSLionel Sambuc Sequence.AddConstructorInitializationStep(CtorDecl,
3293f4a2713aSLionel Sambuc Best->FoundDecl.getAccess(),
3294f4a2713aSLionel Sambuc DestType, HadMultipleCandidates,
3295f4a2713aSLionel Sambuc InitListSyntax, AsInitializerList);
3296f4a2713aSLionel Sambuc }
3297f4a2713aSLionel Sambuc
3298f4a2713aSLionel Sambuc static bool
ResolveOverloadedFunctionForReferenceBinding(Sema & S,Expr * Initializer,QualType & SourceType,QualType & UnqualifiedSourceType,QualType UnqualifiedTargetType,InitializationSequence & Sequence)3299f4a2713aSLionel Sambuc ResolveOverloadedFunctionForReferenceBinding(Sema &S,
3300f4a2713aSLionel Sambuc Expr *Initializer,
3301f4a2713aSLionel Sambuc QualType &SourceType,
3302f4a2713aSLionel Sambuc QualType &UnqualifiedSourceType,
3303f4a2713aSLionel Sambuc QualType UnqualifiedTargetType,
3304f4a2713aSLionel Sambuc InitializationSequence &Sequence) {
3305f4a2713aSLionel Sambuc if (S.Context.getCanonicalType(UnqualifiedSourceType) ==
3306f4a2713aSLionel Sambuc S.Context.OverloadTy) {
3307f4a2713aSLionel Sambuc DeclAccessPair Found;
3308f4a2713aSLionel Sambuc bool HadMultipleCandidates = false;
3309f4a2713aSLionel Sambuc if (FunctionDecl *Fn
3310f4a2713aSLionel Sambuc = S.ResolveAddressOfOverloadedFunction(Initializer,
3311f4a2713aSLionel Sambuc UnqualifiedTargetType,
3312f4a2713aSLionel Sambuc false, Found,
3313f4a2713aSLionel Sambuc &HadMultipleCandidates)) {
3314f4a2713aSLionel Sambuc Sequence.AddAddressOverloadResolutionStep(Fn, Found,
3315f4a2713aSLionel Sambuc HadMultipleCandidates);
3316f4a2713aSLionel Sambuc SourceType = Fn->getType();
3317f4a2713aSLionel Sambuc UnqualifiedSourceType = SourceType.getUnqualifiedType();
3318f4a2713aSLionel Sambuc } else if (!UnqualifiedTargetType->isRecordType()) {
3319f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
3320f4a2713aSLionel Sambuc return true;
3321f4a2713aSLionel Sambuc }
3322f4a2713aSLionel Sambuc }
3323f4a2713aSLionel Sambuc return false;
3324f4a2713aSLionel Sambuc }
3325f4a2713aSLionel Sambuc
3326f4a2713aSLionel Sambuc static void TryReferenceInitializationCore(Sema &S,
3327f4a2713aSLionel Sambuc const InitializedEntity &Entity,
3328f4a2713aSLionel Sambuc const InitializationKind &Kind,
3329f4a2713aSLionel Sambuc Expr *Initializer,
3330f4a2713aSLionel Sambuc QualType cv1T1, QualType T1,
3331f4a2713aSLionel Sambuc Qualifiers T1Quals,
3332f4a2713aSLionel Sambuc QualType cv2T2, QualType T2,
3333f4a2713aSLionel Sambuc Qualifiers T2Quals,
3334f4a2713aSLionel Sambuc InitializationSequence &Sequence);
3335f4a2713aSLionel Sambuc
3336f4a2713aSLionel Sambuc static void TryValueInitialization(Sema &S,
3337f4a2713aSLionel Sambuc const InitializedEntity &Entity,
3338f4a2713aSLionel Sambuc const InitializationKind &Kind,
3339f4a2713aSLionel Sambuc InitializationSequence &Sequence,
3340*0a6a1f1dSLionel Sambuc InitListExpr *InitList = nullptr);
3341f4a2713aSLionel Sambuc
3342f4a2713aSLionel Sambuc /// \brief Attempt list initialization of a reference.
TryReferenceListInitialization(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,InitListExpr * InitList,InitializationSequence & Sequence)3343f4a2713aSLionel Sambuc static void TryReferenceListInitialization(Sema &S,
3344f4a2713aSLionel Sambuc const InitializedEntity &Entity,
3345f4a2713aSLionel Sambuc const InitializationKind &Kind,
3346f4a2713aSLionel Sambuc InitListExpr *InitList,
3347f4a2713aSLionel Sambuc InitializationSequence &Sequence) {
3348f4a2713aSLionel Sambuc // First, catch C++03 where this isn't possible.
3349f4a2713aSLionel Sambuc if (!S.getLangOpts().CPlusPlus11) {
3350f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList);
3351f4a2713aSLionel Sambuc return;
3352f4a2713aSLionel Sambuc }
3353f4a2713aSLionel Sambuc
3354f4a2713aSLionel Sambuc QualType DestType = Entity.getType();
3355f4a2713aSLionel Sambuc QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
3356f4a2713aSLionel Sambuc Qualifiers T1Quals;
3357f4a2713aSLionel Sambuc QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
3358f4a2713aSLionel Sambuc
3359f4a2713aSLionel Sambuc // Reference initialization via an initializer list works thus:
3360f4a2713aSLionel Sambuc // If the initializer list consists of a single element that is
3361f4a2713aSLionel Sambuc // reference-related to the referenced type, bind directly to that element
3362f4a2713aSLionel Sambuc // (possibly creating temporaries).
3363f4a2713aSLionel Sambuc // Otherwise, initialize a temporary with the initializer list and
3364f4a2713aSLionel Sambuc // bind to that.
3365f4a2713aSLionel Sambuc if (InitList->getNumInits() == 1) {
3366f4a2713aSLionel Sambuc Expr *Initializer = InitList->getInit(0);
3367f4a2713aSLionel Sambuc QualType cv2T2 = Initializer->getType();
3368f4a2713aSLionel Sambuc Qualifiers T2Quals;
3369f4a2713aSLionel Sambuc QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals);
3370f4a2713aSLionel Sambuc
3371f4a2713aSLionel Sambuc // If this fails, creating a temporary wouldn't work either.
3372f4a2713aSLionel Sambuc if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2,
3373f4a2713aSLionel Sambuc T1, Sequence))
3374f4a2713aSLionel Sambuc return;
3375f4a2713aSLionel Sambuc
3376f4a2713aSLionel Sambuc SourceLocation DeclLoc = Initializer->getLocStart();
3377f4a2713aSLionel Sambuc bool dummy1, dummy2, dummy3;
3378f4a2713aSLionel Sambuc Sema::ReferenceCompareResult RefRelationship
3379f4a2713aSLionel Sambuc = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1,
3380f4a2713aSLionel Sambuc dummy2, dummy3);
3381f4a2713aSLionel Sambuc if (RefRelationship >= Sema::Ref_Related) {
3382f4a2713aSLionel Sambuc // Try to bind the reference here.
3383f4a2713aSLionel Sambuc TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1,
3384f4a2713aSLionel Sambuc T1Quals, cv2T2, T2, T2Quals, Sequence);
3385f4a2713aSLionel Sambuc if (Sequence)
3386f4a2713aSLionel Sambuc Sequence.RewrapReferenceInitList(cv1T1, InitList);
3387f4a2713aSLionel Sambuc return;
3388f4a2713aSLionel Sambuc }
3389f4a2713aSLionel Sambuc
3390f4a2713aSLionel Sambuc // Update the initializer if we've resolved an overloaded function.
3391f4a2713aSLionel Sambuc if (Sequence.step_begin() != Sequence.step_end())
3392f4a2713aSLionel Sambuc Sequence.RewrapReferenceInitList(cv1T1, InitList);
3393f4a2713aSLionel Sambuc }
3394f4a2713aSLionel Sambuc
3395f4a2713aSLionel Sambuc // Not reference-related. Create a temporary and bind to that.
3396f4a2713aSLionel Sambuc InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1);
3397f4a2713aSLionel Sambuc
3398f4a2713aSLionel Sambuc TryListInitialization(S, TempEntity, Kind, InitList, Sequence);
3399f4a2713aSLionel Sambuc if (Sequence) {
3400f4a2713aSLionel Sambuc if (DestType->isRValueReferenceType() ||
3401f4a2713aSLionel Sambuc (T1Quals.hasConst() && !T1Quals.hasVolatile()))
3402f4a2713aSLionel Sambuc Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
3403f4a2713aSLionel Sambuc else
3404f4a2713aSLionel Sambuc Sequence.SetFailed(
3405f4a2713aSLionel Sambuc InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
3406f4a2713aSLionel Sambuc }
3407f4a2713aSLionel Sambuc }
3408f4a2713aSLionel Sambuc
3409f4a2713aSLionel Sambuc /// \brief Attempt list initialization (C++0x [dcl.init.list])
TryListInitialization(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,InitListExpr * InitList,InitializationSequence & Sequence)3410f4a2713aSLionel Sambuc static void TryListInitialization(Sema &S,
3411f4a2713aSLionel Sambuc const InitializedEntity &Entity,
3412f4a2713aSLionel Sambuc const InitializationKind &Kind,
3413f4a2713aSLionel Sambuc InitListExpr *InitList,
3414f4a2713aSLionel Sambuc InitializationSequence &Sequence) {
3415f4a2713aSLionel Sambuc QualType DestType = Entity.getType();
3416f4a2713aSLionel Sambuc
3417f4a2713aSLionel Sambuc // C++ doesn't allow scalar initialization with more than one argument.
3418f4a2713aSLionel Sambuc // But C99 complex numbers are scalars and it makes sense there.
3419f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus && DestType->isScalarType() &&
3420f4a2713aSLionel Sambuc !DestType->isAnyComplexType() && InitList->getNumInits() > 1) {
3421f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar);
3422f4a2713aSLionel Sambuc return;
3423f4a2713aSLionel Sambuc }
3424f4a2713aSLionel Sambuc if (DestType->isReferenceType()) {
3425f4a2713aSLionel Sambuc TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence);
3426f4a2713aSLionel Sambuc return;
3427f4a2713aSLionel Sambuc }
3428f4a2713aSLionel Sambuc if (DestType->isRecordType()) {
3429f4a2713aSLionel Sambuc if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) {
3430f4a2713aSLionel Sambuc Sequence.setIncompleteTypeFailure(DestType);
3431f4a2713aSLionel Sambuc return;
3432f4a2713aSLionel Sambuc }
3433f4a2713aSLionel Sambuc
3434f4a2713aSLionel Sambuc // C++11 [dcl.init.list]p3:
3435f4a2713aSLionel Sambuc // - If T is an aggregate, aggregate initialization is performed.
3436f4a2713aSLionel Sambuc if (!DestType->isAggregateType()) {
3437f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus11) {
3438f4a2713aSLionel Sambuc // - Otherwise, if the initializer list has no elements and T is a
3439f4a2713aSLionel Sambuc // class type with a default constructor, the object is
3440f4a2713aSLionel Sambuc // value-initialized.
3441f4a2713aSLionel Sambuc if (InitList->getNumInits() == 0) {
3442f4a2713aSLionel Sambuc CXXRecordDecl *RD = DestType->getAsCXXRecordDecl();
3443f4a2713aSLionel Sambuc if (RD->hasDefaultConstructor()) {
3444f4a2713aSLionel Sambuc TryValueInitialization(S, Entity, Kind, Sequence, InitList);
3445f4a2713aSLionel Sambuc return;
3446f4a2713aSLionel Sambuc }
3447f4a2713aSLionel Sambuc }
3448f4a2713aSLionel Sambuc
3449f4a2713aSLionel Sambuc // - Otherwise, if T is a specialization of std::initializer_list<E>,
3450f4a2713aSLionel Sambuc // an initializer_list object constructed [...]
3451f4a2713aSLionel Sambuc if (TryInitializerListConstruction(S, InitList, DestType, Sequence))
3452f4a2713aSLionel Sambuc return;
3453f4a2713aSLionel Sambuc
3454f4a2713aSLionel Sambuc // - Otherwise, if T is a class type, constructors are considered.
3455f4a2713aSLionel Sambuc Expr *InitListAsExpr = InitList;
3456f4a2713aSLionel Sambuc TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType,
3457f4a2713aSLionel Sambuc Sequence, /*InitListSyntax*/true);
3458f4a2713aSLionel Sambuc } else
3459f4a2713aSLionel Sambuc Sequence.SetFailed(
3460f4a2713aSLionel Sambuc InitializationSequence::FK_InitListBadDestinationType);
3461f4a2713aSLionel Sambuc return;
3462f4a2713aSLionel Sambuc }
3463f4a2713aSLionel Sambuc }
3464f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() &&
3465f4a2713aSLionel Sambuc InitList->getNumInits() == 1 &&
3466f4a2713aSLionel Sambuc InitList->getInit(0)->getType()->isRecordType()) {
3467f4a2713aSLionel Sambuc // - Otherwise, if the initializer list has a single element of type E
3468f4a2713aSLionel Sambuc // [...references are handled above...], the object or reference is
3469f4a2713aSLionel Sambuc // initialized from that element; if a narrowing conversion is required
3470f4a2713aSLionel Sambuc // to convert the element to T, the program is ill-formed.
3471f4a2713aSLionel Sambuc //
3472f4a2713aSLionel Sambuc // Per core-24034, this is direct-initialization if we were performing
3473f4a2713aSLionel Sambuc // direct-list-initialization and copy-initialization otherwise.
3474f4a2713aSLionel Sambuc // We can't use InitListChecker for this, because it always performs
3475f4a2713aSLionel Sambuc // copy-initialization. This only matters if we might use an 'explicit'
3476f4a2713aSLionel Sambuc // conversion operator, so we only need to handle the cases where the source
3477f4a2713aSLionel Sambuc // is of record type.
3478f4a2713aSLionel Sambuc InitializationKind SubKind =
3479f4a2713aSLionel Sambuc Kind.getKind() == InitializationKind::IK_DirectList
3480f4a2713aSLionel Sambuc ? InitializationKind::CreateDirect(Kind.getLocation(),
3481f4a2713aSLionel Sambuc InitList->getLBraceLoc(),
3482f4a2713aSLionel Sambuc InitList->getRBraceLoc())
3483f4a2713aSLionel Sambuc : Kind;
3484f4a2713aSLionel Sambuc Expr *SubInit[1] = { InitList->getInit(0) };
3485f4a2713aSLionel Sambuc Sequence.InitializeFrom(S, Entity, SubKind, SubInit,
3486f4a2713aSLionel Sambuc /*TopLevelOfInitList*/true);
3487f4a2713aSLionel Sambuc if (Sequence)
3488f4a2713aSLionel Sambuc Sequence.RewrapReferenceInitList(Entity.getType(), InitList);
3489f4a2713aSLionel Sambuc return;
3490f4a2713aSLionel Sambuc }
3491f4a2713aSLionel Sambuc
3492f4a2713aSLionel Sambuc InitListChecker CheckInitList(S, Entity, InitList,
3493f4a2713aSLionel Sambuc DestType, /*VerifyOnly=*/true);
3494f4a2713aSLionel Sambuc if (CheckInitList.HadError()) {
3495f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed);
3496f4a2713aSLionel Sambuc return;
3497f4a2713aSLionel Sambuc }
3498f4a2713aSLionel Sambuc
3499f4a2713aSLionel Sambuc // Add the list initialization step with the built init list.
3500f4a2713aSLionel Sambuc Sequence.AddListInitializationStep(DestType);
3501f4a2713aSLionel Sambuc }
3502f4a2713aSLionel Sambuc
3503f4a2713aSLionel Sambuc /// \brief Try a reference initialization that involves calling a conversion
3504f4a2713aSLionel Sambuc /// function.
TryRefInitWithConversionFunction(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,Expr * Initializer,bool AllowRValues,InitializationSequence & Sequence)3505f4a2713aSLionel Sambuc static OverloadingResult TryRefInitWithConversionFunction(Sema &S,
3506f4a2713aSLionel Sambuc const InitializedEntity &Entity,
3507f4a2713aSLionel Sambuc const InitializationKind &Kind,
3508f4a2713aSLionel Sambuc Expr *Initializer,
3509f4a2713aSLionel Sambuc bool AllowRValues,
3510f4a2713aSLionel Sambuc InitializationSequence &Sequence) {
3511f4a2713aSLionel Sambuc QualType DestType = Entity.getType();
3512f4a2713aSLionel Sambuc QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
3513f4a2713aSLionel Sambuc QualType T1 = cv1T1.getUnqualifiedType();
3514f4a2713aSLionel Sambuc QualType cv2T2 = Initializer->getType();
3515f4a2713aSLionel Sambuc QualType T2 = cv2T2.getUnqualifiedType();
3516f4a2713aSLionel Sambuc
3517f4a2713aSLionel Sambuc bool DerivedToBase;
3518f4a2713aSLionel Sambuc bool ObjCConversion;
3519f4a2713aSLionel Sambuc bool ObjCLifetimeConversion;
3520f4a2713aSLionel Sambuc assert(!S.CompareReferenceRelationship(Initializer->getLocStart(),
3521f4a2713aSLionel Sambuc T1, T2, DerivedToBase,
3522f4a2713aSLionel Sambuc ObjCConversion,
3523f4a2713aSLionel Sambuc ObjCLifetimeConversion) &&
3524f4a2713aSLionel Sambuc "Must have incompatible references when binding via conversion");
3525f4a2713aSLionel Sambuc (void)DerivedToBase;
3526f4a2713aSLionel Sambuc (void)ObjCConversion;
3527f4a2713aSLionel Sambuc (void)ObjCLifetimeConversion;
3528f4a2713aSLionel Sambuc
3529f4a2713aSLionel Sambuc // Build the candidate set directly in the initialization sequence
3530f4a2713aSLionel Sambuc // structure, so that it will persist if we fail.
3531f4a2713aSLionel Sambuc OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
3532f4a2713aSLionel Sambuc CandidateSet.clear();
3533f4a2713aSLionel Sambuc
3534f4a2713aSLionel Sambuc // Determine whether we are allowed to call explicit constructors or
3535f4a2713aSLionel Sambuc // explicit conversion operators.
3536f4a2713aSLionel Sambuc bool AllowExplicit = Kind.AllowExplicit();
3537f4a2713aSLionel Sambuc bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding();
3538f4a2713aSLionel Sambuc
3539*0a6a1f1dSLionel Sambuc const RecordType *T1RecordType = nullptr;
3540f4a2713aSLionel Sambuc if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) &&
3541f4a2713aSLionel Sambuc !S.RequireCompleteType(Kind.getLocation(), T1, 0)) {
3542f4a2713aSLionel Sambuc // The type we're converting to is a class type. Enumerate its constructors
3543f4a2713aSLionel Sambuc // to see if there is a suitable conversion.
3544f4a2713aSLionel Sambuc CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl());
3545f4a2713aSLionel Sambuc
3546f4a2713aSLionel Sambuc DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl);
3547f4a2713aSLionel Sambuc // The container holding the constructors can under certain conditions
3548f4a2713aSLionel Sambuc // be changed while iterating (e.g. because of deserialization).
3549f4a2713aSLionel Sambuc // To be safe we copy the lookup results to a new container.
3550f4a2713aSLionel Sambuc SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end());
3551f4a2713aSLionel Sambuc for (SmallVectorImpl<NamedDecl *>::iterator
3552f4a2713aSLionel Sambuc CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) {
3553f4a2713aSLionel Sambuc NamedDecl *D = *CI;
3554f4a2713aSLionel Sambuc DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
3555f4a2713aSLionel Sambuc
3556f4a2713aSLionel Sambuc // Find the constructor (which may be a template).
3557*0a6a1f1dSLionel Sambuc CXXConstructorDecl *Constructor = nullptr;
3558f4a2713aSLionel Sambuc FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D);
3559f4a2713aSLionel Sambuc if (ConstructorTmpl)
3560f4a2713aSLionel Sambuc Constructor = cast<CXXConstructorDecl>(
3561f4a2713aSLionel Sambuc ConstructorTmpl->getTemplatedDecl());
3562f4a2713aSLionel Sambuc else
3563f4a2713aSLionel Sambuc Constructor = cast<CXXConstructorDecl>(D);
3564f4a2713aSLionel Sambuc
3565f4a2713aSLionel Sambuc if (!Constructor->isInvalidDecl() &&
3566f4a2713aSLionel Sambuc Constructor->isConvertingConstructor(AllowExplicit)) {
3567f4a2713aSLionel Sambuc if (ConstructorTmpl)
3568f4a2713aSLionel Sambuc S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
3569*0a6a1f1dSLionel Sambuc /*ExplicitArgs*/ nullptr,
3570f4a2713aSLionel Sambuc Initializer, CandidateSet,
3571f4a2713aSLionel Sambuc /*SuppressUserConversions=*/true);
3572f4a2713aSLionel Sambuc else
3573f4a2713aSLionel Sambuc S.AddOverloadCandidate(Constructor, FoundDecl,
3574f4a2713aSLionel Sambuc Initializer, CandidateSet,
3575f4a2713aSLionel Sambuc /*SuppressUserConversions=*/true);
3576f4a2713aSLionel Sambuc }
3577f4a2713aSLionel Sambuc }
3578f4a2713aSLionel Sambuc }
3579f4a2713aSLionel Sambuc if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl())
3580f4a2713aSLionel Sambuc return OR_No_Viable_Function;
3581f4a2713aSLionel Sambuc
3582*0a6a1f1dSLionel Sambuc const RecordType *T2RecordType = nullptr;
3583f4a2713aSLionel Sambuc if ((T2RecordType = T2->getAs<RecordType>()) &&
3584f4a2713aSLionel Sambuc !S.RequireCompleteType(Kind.getLocation(), T2, 0)) {
3585f4a2713aSLionel Sambuc // The type we're converting from is a class type, enumerate its conversion
3586f4a2713aSLionel Sambuc // functions.
3587f4a2713aSLionel Sambuc CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl());
3588f4a2713aSLionel Sambuc
3589f4a2713aSLionel Sambuc std::pair<CXXRecordDecl::conversion_iterator,
3590f4a2713aSLionel Sambuc CXXRecordDecl::conversion_iterator>
3591f4a2713aSLionel Sambuc Conversions = T2RecordDecl->getVisibleConversionFunctions();
3592f4a2713aSLionel Sambuc for (CXXRecordDecl::conversion_iterator
3593f4a2713aSLionel Sambuc I = Conversions.first, E = Conversions.second; I != E; ++I) {
3594f4a2713aSLionel Sambuc NamedDecl *D = *I;
3595f4a2713aSLionel Sambuc CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
3596f4a2713aSLionel Sambuc if (isa<UsingShadowDecl>(D))
3597f4a2713aSLionel Sambuc D = cast<UsingShadowDecl>(D)->getTargetDecl();
3598f4a2713aSLionel Sambuc
3599f4a2713aSLionel Sambuc FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
3600f4a2713aSLionel Sambuc CXXConversionDecl *Conv;
3601f4a2713aSLionel Sambuc if (ConvTemplate)
3602f4a2713aSLionel Sambuc Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
3603f4a2713aSLionel Sambuc else
3604f4a2713aSLionel Sambuc Conv = cast<CXXConversionDecl>(D);
3605f4a2713aSLionel Sambuc
3606f4a2713aSLionel Sambuc // If the conversion function doesn't return a reference type,
3607f4a2713aSLionel Sambuc // it can't be considered for this conversion unless we're allowed to
3608f4a2713aSLionel Sambuc // consider rvalues.
3609f4a2713aSLionel Sambuc // FIXME: Do we need to make sure that we only consider conversion
3610f4a2713aSLionel Sambuc // candidates with reference-compatible results? That might be needed to
3611f4a2713aSLionel Sambuc // break recursion.
3612f4a2713aSLionel Sambuc if ((AllowExplicitConvs || !Conv->isExplicit()) &&
3613f4a2713aSLionel Sambuc (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){
3614f4a2713aSLionel Sambuc if (ConvTemplate)
3615f4a2713aSLionel Sambuc S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(),
3616f4a2713aSLionel Sambuc ActingDC, Initializer,
3617*0a6a1f1dSLionel Sambuc DestType, CandidateSet,
3618*0a6a1f1dSLionel Sambuc /*AllowObjCConversionOnExplicit=*/
3619*0a6a1f1dSLionel Sambuc false);
3620f4a2713aSLionel Sambuc else
3621f4a2713aSLionel Sambuc S.AddConversionCandidate(Conv, I.getPair(), ActingDC,
3622*0a6a1f1dSLionel Sambuc Initializer, DestType, CandidateSet,
3623*0a6a1f1dSLionel Sambuc /*AllowObjCConversionOnExplicit=*/false);
3624f4a2713aSLionel Sambuc }
3625f4a2713aSLionel Sambuc }
3626f4a2713aSLionel Sambuc }
3627f4a2713aSLionel Sambuc if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl())
3628f4a2713aSLionel Sambuc return OR_No_Viable_Function;
3629f4a2713aSLionel Sambuc
3630f4a2713aSLionel Sambuc SourceLocation DeclLoc = Initializer->getLocStart();
3631f4a2713aSLionel Sambuc
3632f4a2713aSLionel Sambuc // Perform overload resolution. If it fails, return the failed result.
3633f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
3634f4a2713aSLionel Sambuc if (OverloadingResult Result
3635f4a2713aSLionel Sambuc = CandidateSet.BestViableFunction(S, DeclLoc, Best, true))
3636f4a2713aSLionel Sambuc return Result;
3637f4a2713aSLionel Sambuc
3638f4a2713aSLionel Sambuc FunctionDecl *Function = Best->Function;
3639f4a2713aSLionel Sambuc // This is the overload that will be used for this initialization step if we
3640f4a2713aSLionel Sambuc // use this initialization. Mark it as referenced.
3641f4a2713aSLionel Sambuc Function->setReferenced();
3642f4a2713aSLionel Sambuc
3643f4a2713aSLionel Sambuc // Compute the returned type of the conversion.
3644f4a2713aSLionel Sambuc if (isa<CXXConversionDecl>(Function))
3645*0a6a1f1dSLionel Sambuc T2 = Function->getReturnType();
3646f4a2713aSLionel Sambuc else
3647f4a2713aSLionel Sambuc T2 = cv1T1;
3648f4a2713aSLionel Sambuc
3649f4a2713aSLionel Sambuc // Add the user-defined conversion step.
3650f4a2713aSLionel Sambuc bool HadMultipleCandidates = (CandidateSet.size() > 1);
3651f4a2713aSLionel Sambuc Sequence.AddUserConversionStep(Function, Best->FoundDecl,
3652f4a2713aSLionel Sambuc T2.getNonLValueExprType(S.Context),
3653f4a2713aSLionel Sambuc HadMultipleCandidates);
3654f4a2713aSLionel Sambuc
3655f4a2713aSLionel Sambuc // Determine whether we need to perform derived-to-base or
3656f4a2713aSLionel Sambuc // cv-qualification adjustments.
3657f4a2713aSLionel Sambuc ExprValueKind VK = VK_RValue;
3658f4a2713aSLionel Sambuc if (T2->isLValueReferenceType())
3659f4a2713aSLionel Sambuc VK = VK_LValue;
3660f4a2713aSLionel Sambuc else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>())
3661f4a2713aSLionel Sambuc VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue;
3662f4a2713aSLionel Sambuc
3663f4a2713aSLionel Sambuc bool NewDerivedToBase = false;
3664f4a2713aSLionel Sambuc bool NewObjCConversion = false;
3665f4a2713aSLionel Sambuc bool NewObjCLifetimeConversion = false;
3666f4a2713aSLionel Sambuc Sema::ReferenceCompareResult NewRefRelationship
3667f4a2713aSLionel Sambuc = S.CompareReferenceRelationship(DeclLoc, T1,
3668f4a2713aSLionel Sambuc T2.getNonLValueExprType(S.Context),
3669f4a2713aSLionel Sambuc NewDerivedToBase, NewObjCConversion,
3670f4a2713aSLionel Sambuc NewObjCLifetimeConversion);
3671f4a2713aSLionel Sambuc if (NewRefRelationship == Sema::Ref_Incompatible) {
3672f4a2713aSLionel Sambuc // If the type we've converted to is not reference-related to the
3673f4a2713aSLionel Sambuc // type we're looking for, then there is another conversion step
3674f4a2713aSLionel Sambuc // we need to perform to produce a temporary of the right type
3675f4a2713aSLionel Sambuc // that we'll be binding to.
3676f4a2713aSLionel Sambuc ImplicitConversionSequence ICS;
3677f4a2713aSLionel Sambuc ICS.setStandard();
3678f4a2713aSLionel Sambuc ICS.Standard = Best->FinalConversion;
3679f4a2713aSLionel Sambuc T2 = ICS.Standard.getToType(2);
3680f4a2713aSLionel Sambuc Sequence.AddConversionSequenceStep(ICS, T2);
3681f4a2713aSLionel Sambuc } else if (NewDerivedToBase)
3682f4a2713aSLionel Sambuc Sequence.AddDerivedToBaseCastStep(
3683f4a2713aSLionel Sambuc S.Context.getQualifiedType(T1,
3684f4a2713aSLionel Sambuc T2.getNonReferenceType().getQualifiers()),
3685f4a2713aSLionel Sambuc VK);
3686f4a2713aSLionel Sambuc else if (NewObjCConversion)
3687f4a2713aSLionel Sambuc Sequence.AddObjCObjectConversionStep(
3688f4a2713aSLionel Sambuc S.Context.getQualifiedType(T1,
3689f4a2713aSLionel Sambuc T2.getNonReferenceType().getQualifiers()));
3690f4a2713aSLionel Sambuc
3691f4a2713aSLionel Sambuc if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers())
3692f4a2713aSLionel Sambuc Sequence.AddQualificationConversionStep(cv1T1, VK);
3693f4a2713aSLionel Sambuc
3694f4a2713aSLionel Sambuc Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType());
3695f4a2713aSLionel Sambuc return OR_Success;
3696f4a2713aSLionel Sambuc }
3697f4a2713aSLionel Sambuc
3698f4a2713aSLionel Sambuc static void CheckCXX98CompatAccessibleCopy(Sema &S,
3699f4a2713aSLionel Sambuc const InitializedEntity &Entity,
3700f4a2713aSLionel Sambuc Expr *CurInitExpr);
3701f4a2713aSLionel Sambuc
3702f4a2713aSLionel Sambuc /// \brief Attempt reference initialization (C++0x [dcl.init.ref])
TryReferenceInitialization(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,Expr * Initializer,InitializationSequence & Sequence)3703f4a2713aSLionel Sambuc static void TryReferenceInitialization(Sema &S,
3704f4a2713aSLionel Sambuc const InitializedEntity &Entity,
3705f4a2713aSLionel Sambuc const InitializationKind &Kind,
3706f4a2713aSLionel Sambuc Expr *Initializer,
3707f4a2713aSLionel Sambuc InitializationSequence &Sequence) {
3708f4a2713aSLionel Sambuc QualType DestType = Entity.getType();
3709f4a2713aSLionel Sambuc QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType();
3710f4a2713aSLionel Sambuc Qualifiers T1Quals;
3711f4a2713aSLionel Sambuc QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals);
3712f4a2713aSLionel Sambuc QualType cv2T2 = Initializer->getType();
3713f4a2713aSLionel Sambuc Qualifiers T2Quals;
3714f4a2713aSLionel Sambuc QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals);
3715f4a2713aSLionel Sambuc
3716f4a2713aSLionel Sambuc // If the initializer is the address of an overloaded function, try
3717f4a2713aSLionel Sambuc // to resolve the overloaded function. If all goes well, T2 is the
3718f4a2713aSLionel Sambuc // type of the resulting function.
3719f4a2713aSLionel Sambuc if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2,
3720f4a2713aSLionel Sambuc T1, Sequence))
3721f4a2713aSLionel Sambuc return;
3722f4a2713aSLionel Sambuc
3723f4a2713aSLionel Sambuc // Delegate everything else to a subfunction.
3724f4a2713aSLionel Sambuc TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1,
3725f4a2713aSLionel Sambuc T1Quals, cv2T2, T2, T2Quals, Sequence);
3726f4a2713aSLionel Sambuc }
3727f4a2713aSLionel Sambuc
3728f4a2713aSLionel Sambuc /// Converts the target of reference initialization so that it has the
3729f4a2713aSLionel Sambuc /// appropriate qualifiers and value kind.
3730f4a2713aSLionel Sambuc ///
3731f4a2713aSLionel Sambuc /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'.
3732f4a2713aSLionel Sambuc /// \code
3733f4a2713aSLionel Sambuc /// int x;
3734f4a2713aSLionel Sambuc /// const int &r = x;
3735f4a2713aSLionel Sambuc /// \endcode
3736f4a2713aSLionel Sambuc ///
3737f4a2713aSLionel Sambuc /// In this case the reference is binding to a bitfield lvalue, which isn't
3738f4a2713aSLionel Sambuc /// valid. Perform a load to create a lifetime-extended temporary instead.
3739f4a2713aSLionel Sambuc /// \code
3740f4a2713aSLionel Sambuc /// const int &r = someStruct.bitfield;
3741f4a2713aSLionel Sambuc /// \endcode
3742f4a2713aSLionel Sambuc static ExprValueKind
convertQualifiersAndValueKindIfNecessary(Sema & S,InitializationSequence & Sequence,Expr * Initializer,QualType cv1T1,Qualifiers T1Quals,Qualifiers T2Quals,bool IsLValueRef)3743f4a2713aSLionel Sambuc convertQualifiersAndValueKindIfNecessary(Sema &S,
3744f4a2713aSLionel Sambuc InitializationSequence &Sequence,
3745f4a2713aSLionel Sambuc Expr *Initializer,
3746f4a2713aSLionel Sambuc QualType cv1T1,
3747f4a2713aSLionel Sambuc Qualifiers T1Quals,
3748f4a2713aSLionel Sambuc Qualifiers T2Quals,
3749f4a2713aSLionel Sambuc bool IsLValueRef) {
3750f4a2713aSLionel Sambuc bool IsNonAddressableType = Initializer->refersToBitField() ||
3751f4a2713aSLionel Sambuc Initializer->refersToVectorElement();
3752f4a2713aSLionel Sambuc
3753f4a2713aSLionel Sambuc if (IsNonAddressableType) {
3754f4a2713aSLionel Sambuc // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an
3755f4a2713aSLionel Sambuc // lvalue reference to a non-volatile const type, or the reference shall be
3756f4a2713aSLionel Sambuc // an rvalue reference.
3757f4a2713aSLionel Sambuc //
3758f4a2713aSLionel Sambuc // If not, we can't make a temporary and bind to that. Give up and allow the
3759f4a2713aSLionel Sambuc // error to be diagnosed later.
3760f4a2713aSLionel Sambuc if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) {
3761f4a2713aSLionel Sambuc assert(Initializer->isGLValue());
3762f4a2713aSLionel Sambuc return Initializer->getValueKind();
3763f4a2713aSLionel Sambuc }
3764f4a2713aSLionel Sambuc
3765f4a2713aSLionel Sambuc // Force a load so we can materialize a temporary.
3766f4a2713aSLionel Sambuc Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType());
3767f4a2713aSLionel Sambuc return VK_RValue;
3768f4a2713aSLionel Sambuc }
3769f4a2713aSLionel Sambuc
3770f4a2713aSLionel Sambuc if (T1Quals != T2Quals) {
3771f4a2713aSLionel Sambuc Sequence.AddQualificationConversionStep(cv1T1,
3772f4a2713aSLionel Sambuc Initializer->getValueKind());
3773f4a2713aSLionel Sambuc }
3774f4a2713aSLionel Sambuc
3775f4a2713aSLionel Sambuc return Initializer->getValueKind();
3776f4a2713aSLionel Sambuc }
3777f4a2713aSLionel Sambuc
3778f4a2713aSLionel Sambuc
3779f4a2713aSLionel Sambuc /// \brief Reference initialization without resolving overloaded functions.
TryReferenceInitializationCore(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,Expr * Initializer,QualType cv1T1,QualType T1,Qualifiers T1Quals,QualType cv2T2,QualType T2,Qualifiers T2Quals,InitializationSequence & Sequence)3780f4a2713aSLionel Sambuc static void TryReferenceInitializationCore(Sema &S,
3781f4a2713aSLionel Sambuc const InitializedEntity &Entity,
3782f4a2713aSLionel Sambuc const InitializationKind &Kind,
3783f4a2713aSLionel Sambuc Expr *Initializer,
3784f4a2713aSLionel Sambuc QualType cv1T1, QualType T1,
3785f4a2713aSLionel Sambuc Qualifiers T1Quals,
3786f4a2713aSLionel Sambuc QualType cv2T2, QualType T2,
3787f4a2713aSLionel Sambuc Qualifiers T2Quals,
3788f4a2713aSLionel Sambuc InitializationSequence &Sequence) {
3789f4a2713aSLionel Sambuc QualType DestType = Entity.getType();
3790f4a2713aSLionel Sambuc SourceLocation DeclLoc = Initializer->getLocStart();
3791f4a2713aSLionel Sambuc // Compute some basic properties of the types and the initializer.
3792f4a2713aSLionel Sambuc bool isLValueRef = DestType->isLValueReferenceType();
3793f4a2713aSLionel Sambuc bool isRValueRef = !isLValueRef;
3794f4a2713aSLionel Sambuc bool DerivedToBase = false;
3795f4a2713aSLionel Sambuc bool ObjCConversion = false;
3796f4a2713aSLionel Sambuc bool ObjCLifetimeConversion = false;
3797f4a2713aSLionel Sambuc Expr::Classification InitCategory = Initializer->Classify(S.Context);
3798f4a2713aSLionel Sambuc Sema::ReferenceCompareResult RefRelationship
3799f4a2713aSLionel Sambuc = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase,
3800f4a2713aSLionel Sambuc ObjCConversion, ObjCLifetimeConversion);
3801f4a2713aSLionel Sambuc
3802f4a2713aSLionel Sambuc // C++0x [dcl.init.ref]p5:
3803f4a2713aSLionel Sambuc // A reference to type "cv1 T1" is initialized by an expression of type
3804f4a2713aSLionel Sambuc // "cv2 T2" as follows:
3805f4a2713aSLionel Sambuc //
3806f4a2713aSLionel Sambuc // - If the reference is an lvalue reference and the initializer
3807f4a2713aSLionel Sambuc // expression
3808f4a2713aSLionel Sambuc // Note the analogous bullet points for rvalue refs to functions. Because
3809f4a2713aSLionel Sambuc // there are no function rvalues in C++, rvalue refs to functions are treated
3810f4a2713aSLionel Sambuc // like lvalue refs.
3811f4a2713aSLionel Sambuc OverloadingResult ConvOvlResult = OR_Success;
3812f4a2713aSLionel Sambuc bool T1Function = T1->isFunctionType();
3813f4a2713aSLionel Sambuc if (isLValueRef || T1Function) {
3814f4a2713aSLionel Sambuc if (InitCategory.isLValue() &&
3815f4a2713aSLionel Sambuc (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification ||
3816f4a2713aSLionel Sambuc (Kind.isCStyleOrFunctionalCast() &&
3817f4a2713aSLionel Sambuc RefRelationship == Sema::Ref_Related))) {
3818f4a2713aSLionel Sambuc // - is an lvalue (but is not a bit-field), and "cv1 T1" is
3819f4a2713aSLionel Sambuc // reference-compatible with "cv2 T2," or
3820f4a2713aSLionel Sambuc //
3821f4a2713aSLionel Sambuc // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a
3822f4a2713aSLionel Sambuc // bit-field when we're determining whether the reference initialization
3823f4a2713aSLionel Sambuc // can occur. However, we do pay attention to whether it is a bit-field
3824f4a2713aSLionel Sambuc // to decide whether we're actually binding to a temporary created from
3825f4a2713aSLionel Sambuc // the bit-field.
3826f4a2713aSLionel Sambuc if (DerivedToBase)
3827f4a2713aSLionel Sambuc Sequence.AddDerivedToBaseCastStep(
3828f4a2713aSLionel Sambuc S.Context.getQualifiedType(T1, T2Quals),
3829f4a2713aSLionel Sambuc VK_LValue);
3830f4a2713aSLionel Sambuc else if (ObjCConversion)
3831f4a2713aSLionel Sambuc Sequence.AddObjCObjectConversionStep(
3832f4a2713aSLionel Sambuc S.Context.getQualifiedType(T1, T2Quals));
3833f4a2713aSLionel Sambuc
3834f4a2713aSLionel Sambuc ExprValueKind ValueKind =
3835f4a2713aSLionel Sambuc convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer,
3836f4a2713aSLionel Sambuc cv1T1, T1Quals, T2Quals,
3837f4a2713aSLionel Sambuc isLValueRef);
3838f4a2713aSLionel Sambuc Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue);
3839f4a2713aSLionel Sambuc return;
3840f4a2713aSLionel Sambuc }
3841f4a2713aSLionel Sambuc
3842f4a2713aSLionel Sambuc // - has a class type (i.e., T2 is a class type), where T1 is not
3843f4a2713aSLionel Sambuc // reference-related to T2, and can be implicitly converted to an
3844f4a2713aSLionel Sambuc // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible
3845f4a2713aSLionel Sambuc // with "cv3 T3" (this conversion is selected by enumerating the
3846f4a2713aSLionel Sambuc // applicable conversion functions (13.3.1.6) and choosing the best
3847f4a2713aSLionel Sambuc // one through overload resolution (13.3)),
3848f4a2713aSLionel Sambuc // If we have an rvalue ref to function type here, the rhs must be
3849f4a2713aSLionel Sambuc // an rvalue. DR1287 removed the "implicitly" here.
3850f4a2713aSLionel Sambuc if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() &&
3851f4a2713aSLionel Sambuc (isLValueRef || InitCategory.isRValue())) {
3852f4a2713aSLionel Sambuc ConvOvlResult = TryRefInitWithConversionFunction(
3853f4a2713aSLionel Sambuc S, Entity, Kind, Initializer, /*AllowRValues*/isRValueRef, Sequence);
3854f4a2713aSLionel Sambuc if (ConvOvlResult == OR_Success)
3855f4a2713aSLionel Sambuc return;
3856f4a2713aSLionel Sambuc if (ConvOvlResult != OR_No_Viable_Function)
3857f4a2713aSLionel Sambuc Sequence.SetOverloadFailure(
3858f4a2713aSLionel Sambuc InitializationSequence::FK_ReferenceInitOverloadFailed,
3859f4a2713aSLionel Sambuc ConvOvlResult);
3860f4a2713aSLionel Sambuc }
3861f4a2713aSLionel Sambuc }
3862f4a2713aSLionel Sambuc
3863f4a2713aSLionel Sambuc // - Otherwise, the reference shall be an lvalue reference to a
3864f4a2713aSLionel Sambuc // non-volatile const type (i.e., cv1 shall be const), or the reference
3865f4a2713aSLionel Sambuc // shall be an rvalue reference.
3866f4a2713aSLionel Sambuc if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) {
3867f4a2713aSLionel Sambuc if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy)
3868f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
3869f4a2713aSLionel Sambuc else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
3870f4a2713aSLionel Sambuc Sequence.SetOverloadFailure(
3871f4a2713aSLionel Sambuc InitializationSequence::FK_ReferenceInitOverloadFailed,
3872f4a2713aSLionel Sambuc ConvOvlResult);
3873f4a2713aSLionel Sambuc else
3874f4a2713aSLionel Sambuc Sequence.SetFailed(InitCategory.isLValue()
3875f4a2713aSLionel Sambuc ? (RefRelationship == Sema::Ref_Related
3876f4a2713aSLionel Sambuc ? InitializationSequence::FK_ReferenceInitDropsQualifiers
3877f4a2713aSLionel Sambuc : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated)
3878f4a2713aSLionel Sambuc : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary);
3879f4a2713aSLionel Sambuc
3880f4a2713aSLionel Sambuc return;
3881f4a2713aSLionel Sambuc }
3882f4a2713aSLionel Sambuc
3883f4a2713aSLionel Sambuc // - If the initializer expression
3884f4a2713aSLionel Sambuc // - is an xvalue, class prvalue, array prvalue, or function lvalue and
3885f4a2713aSLionel Sambuc // "cv1 T1" is reference-compatible with "cv2 T2"
3886f4a2713aSLionel Sambuc // Note: functions are handled below.
3887f4a2713aSLionel Sambuc if (!T1Function &&
3888f4a2713aSLionel Sambuc (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification ||
3889f4a2713aSLionel Sambuc (Kind.isCStyleOrFunctionalCast() &&
3890f4a2713aSLionel Sambuc RefRelationship == Sema::Ref_Related)) &&
3891f4a2713aSLionel Sambuc (InitCategory.isXValue() ||
3892f4a2713aSLionel Sambuc (InitCategory.isPRValue() && T2->isRecordType()) ||
3893f4a2713aSLionel Sambuc (InitCategory.isPRValue() && T2->isArrayType()))) {
3894f4a2713aSLionel Sambuc ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue;
3895f4a2713aSLionel Sambuc if (InitCategory.isPRValue() && T2->isRecordType()) {
3896f4a2713aSLionel Sambuc // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the
3897f4a2713aSLionel Sambuc // compiler the freedom to perform a copy here or bind to the
3898f4a2713aSLionel Sambuc // object, while C++0x requires that we bind directly to the
3899f4a2713aSLionel Sambuc // object. Hence, we always bind to the object without making an
3900f4a2713aSLionel Sambuc // extra copy. However, in C++03 requires that we check for the
3901f4a2713aSLionel Sambuc // presence of a suitable copy constructor:
3902f4a2713aSLionel Sambuc //
3903f4a2713aSLionel Sambuc // The constructor that would be used to make the copy shall
3904f4a2713aSLionel Sambuc // be callable whether or not the copy is actually done.
3905f4a2713aSLionel Sambuc if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt)
3906f4a2713aSLionel Sambuc Sequence.AddExtraneousCopyToTemporary(cv2T2);
3907f4a2713aSLionel Sambuc else if (S.getLangOpts().CPlusPlus11)
3908f4a2713aSLionel Sambuc CheckCXX98CompatAccessibleCopy(S, Entity, Initializer);
3909f4a2713aSLionel Sambuc }
3910f4a2713aSLionel Sambuc
3911f4a2713aSLionel Sambuc if (DerivedToBase)
3912f4a2713aSLionel Sambuc Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals),
3913f4a2713aSLionel Sambuc ValueKind);
3914f4a2713aSLionel Sambuc else if (ObjCConversion)
3915f4a2713aSLionel Sambuc Sequence.AddObjCObjectConversionStep(
3916f4a2713aSLionel Sambuc S.Context.getQualifiedType(T1, T2Quals));
3917f4a2713aSLionel Sambuc
3918f4a2713aSLionel Sambuc ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence,
3919f4a2713aSLionel Sambuc Initializer, cv1T1,
3920f4a2713aSLionel Sambuc T1Quals, T2Quals,
3921f4a2713aSLionel Sambuc isLValueRef);
3922f4a2713aSLionel Sambuc
3923f4a2713aSLionel Sambuc Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue);
3924f4a2713aSLionel Sambuc return;
3925f4a2713aSLionel Sambuc }
3926f4a2713aSLionel Sambuc
3927f4a2713aSLionel Sambuc // - has a class type (i.e., T2 is a class type), where T1 is not
3928f4a2713aSLionel Sambuc // reference-related to T2, and can be implicitly converted to an
3929f4a2713aSLionel Sambuc // xvalue, class prvalue, or function lvalue of type "cv3 T3",
3930f4a2713aSLionel Sambuc // where "cv1 T1" is reference-compatible with "cv3 T3",
3931f4a2713aSLionel Sambuc //
3932f4a2713aSLionel Sambuc // DR1287 removes the "implicitly" here.
3933f4a2713aSLionel Sambuc if (T2->isRecordType()) {
3934f4a2713aSLionel Sambuc if (RefRelationship == Sema::Ref_Incompatible) {
3935f4a2713aSLionel Sambuc ConvOvlResult = TryRefInitWithConversionFunction(
3936f4a2713aSLionel Sambuc S, Entity, Kind, Initializer, /*AllowRValues*/true, Sequence);
3937f4a2713aSLionel Sambuc if (ConvOvlResult)
3938f4a2713aSLionel Sambuc Sequence.SetOverloadFailure(
3939f4a2713aSLionel Sambuc InitializationSequence::FK_ReferenceInitOverloadFailed,
3940f4a2713aSLionel Sambuc ConvOvlResult);
3941f4a2713aSLionel Sambuc
3942f4a2713aSLionel Sambuc return;
3943f4a2713aSLionel Sambuc }
3944f4a2713aSLionel Sambuc
3945f4a2713aSLionel Sambuc if ((RefRelationship == Sema::Ref_Compatible ||
3946f4a2713aSLionel Sambuc RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) &&
3947f4a2713aSLionel Sambuc isRValueRef && InitCategory.isLValue()) {
3948f4a2713aSLionel Sambuc Sequence.SetFailed(
3949f4a2713aSLionel Sambuc InitializationSequence::FK_RValueReferenceBindingToLValue);
3950f4a2713aSLionel Sambuc return;
3951f4a2713aSLionel Sambuc }
3952f4a2713aSLionel Sambuc
3953f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
3954f4a2713aSLionel Sambuc return;
3955f4a2713aSLionel Sambuc }
3956f4a2713aSLionel Sambuc
3957f4a2713aSLionel Sambuc // - Otherwise, a temporary of type "cv1 T1" is created and initialized
3958f4a2713aSLionel Sambuc // from the initializer expression using the rules for a non-reference
3959f4a2713aSLionel Sambuc // copy-initialization (8.5). The reference is then bound to the
3960f4a2713aSLionel Sambuc // temporary. [...]
3961f4a2713aSLionel Sambuc
3962f4a2713aSLionel Sambuc InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1);
3963f4a2713aSLionel Sambuc
3964f4a2713aSLionel Sambuc // FIXME: Why do we use an implicit conversion here rather than trying
3965f4a2713aSLionel Sambuc // copy-initialization?
3966f4a2713aSLionel Sambuc ImplicitConversionSequence ICS
3967f4a2713aSLionel Sambuc = S.TryImplicitConversion(Initializer, TempEntity.getType(),
3968f4a2713aSLionel Sambuc /*SuppressUserConversions=*/false,
3969f4a2713aSLionel Sambuc /*AllowExplicit=*/false,
3970f4a2713aSLionel Sambuc /*FIXME:InOverloadResolution=*/false,
3971f4a2713aSLionel Sambuc /*CStyle=*/Kind.isCStyleOrFunctionalCast(),
3972f4a2713aSLionel Sambuc /*AllowObjCWritebackConversion=*/false);
3973f4a2713aSLionel Sambuc
3974f4a2713aSLionel Sambuc if (ICS.isBad()) {
3975f4a2713aSLionel Sambuc // FIXME: Use the conversion function set stored in ICS to turn
3976f4a2713aSLionel Sambuc // this into an overloading ambiguity diagnostic. However, we need
3977f4a2713aSLionel Sambuc // to keep that set as an OverloadCandidateSet rather than as some
3978f4a2713aSLionel Sambuc // other kind of set.
3979f4a2713aSLionel Sambuc if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty())
3980f4a2713aSLionel Sambuc Sequence.SetOverloadFailure(
3981f4a2713aSLionel Sambuc InitializationSequence::FK_ReferenceInitOverloadFailed,
3982f4a2713aSLionel Sambuc ConvOvlResult);
3983f4a2713aSLionel Sambuc else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy)
3984f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
3985f4a2713aSLionel Sambuc else
3986f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed);
3987f4a2713aSLionel Sambuc return;
3988f4a2713aSLionel Sambuc } else {
3989f4a2713aSLionel Sambuc Sequence.AddConversionSequenceStep(ICS, TempEntity.getType());
3990f4a2713aSLionel Sambuc }
3991f4a2713aSLionel Sambuc
3992f4a2713aSLionel Sambuc // [...] If T1 is reference-related to T2, cv1 must be the
3993f4a2713aSLionel Sambuc // same cv-qualification as, or greater cv-qualification
3994f4a2713aSLionel Sambuc // than, cv2; otherwise, the program is ill-formed.
3995f4a2713aSLionel Sambuc unsigned T1CVRQuals = T1Quals.getCVRQualifiers();
3996f4a2713aSLionel Sambuc unsigned T2CVRQuals = T2Quals.getCVRQualifiers();
3997f4a2713aSLionel Sambuc if (RefRelationship == Sema::Ref_Related &&
3998f4a2713aSLionel Sambuc (T1CVRQuals | T2CVRQuals) != T1CVRQuals) {
3999f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
4000f4a2713aSLionel Sambuc return;
4001f4a2713aSLionel Sambuc }
4002f4a2713aSLionel Sambuc
4003f4a2713aSLionel Sambuc // [...] If T1 is reference-related to T2 and the reference is an rvalue
4004f4a2713aSLionel Sambuc // reference, the initializer expression shall not be an lvalue.
4005f4a2713aSLionel Sambuc if (RefRelationship >= Sema::Ref_Related && !isLValueRef &&
4006f4a2713aSLionel Sambuc InitCategory.isLValue()) {
4007f4a2713aSLionel Sambuc Sequence.SetFailed(
4008f4a2713aSLionel Sambuc InitializationSequence::FK_RValueReferenceBindingToLValue);
4009f4a2713aSLionel Sambuc return;
4010f4a2713aSLionel Sambuc }
4011f4a2713aSLionel Sambuc
4012f4a2713aSLionel Sambuc Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
4013f4a2713aSLionel Sambuc return;
4014f4a2713aSLionel Sambuc }
4015f4a2713aSLionel Sambuc
4016f4a2713aSLionel Sambuc /// \brief Attempt character array initialization from a string literal
4017f4a2713aSLionel Sambuc /// (C++ [dcl.init.string], C99 6.7.8).
TryStringLiteralInitialization(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,Expr * Initializer,InitializationSequence & Sequence)4018f4a2713aSLionel Sambuc static void TryStringLiteralInitialization(Sema &S,
4019f4a2713aSLionel Sambuc const InitializedEntity &Entity,
4020f4a2713aSLionel Sambuc const InitializationKind &Kind,
4021f4a2713aSLionel Sambuc Expr *Initializer,
4022f4a2713aSLionel Sambuc InitializationSequence &Sequence) {
4023f4a2713aSLionel Sambuc Sequence.AddStringInitStep(Entity.getType());
4024f4a2713aSLionel Sambuc }
4025f4a2713aSLionel Sambuc
4026f4a2713aSLionel Sambuc /// \brief Attempt value initialization (C++ [dcl.init]p7).
TryValueInitialization(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,InitializationSequence & Sequence,InitListExpr * InitList)4027f4a2713aSLionel Sambuc static void TryValueInitialization(Sema &S,
4028f4a2713aSLionel Sambuc const InitializedEntity &Entity,
4029f4a2713aSLionel Sambuc const InitializationKind &Kind,
4030f4a2713aSLionel Sambuc InitializationSequence &Sequence,
4031f4a2713aSLionel Sambuc InitListExpr *InitList) {
4032f4a2713aSLionel Sambuc assert((!InitList || InitList->getNumInits() == 0) &&
4033f4a2713aSLionel Sambuc "Shouldn't use value-init for non-empty init lists");
4034f4a2713aSLionel Sambuc
4035f4a2713aSLionel Sambuc // C++98 [dcl.init]p5, C++11 [dcl.init]p7:
4036f4a2713aSLionel Sambuc //
4037f4a2713aSLionel Sambuc // To value-initialize an object of type T means:
4038f4a2713aSLionel Sambuc QualType T = Entity.getType();
4039f4a2713aSLionel Sambuc
4040f4a2713aSLionel Sambuc // -- if T is an array type, then each element is value-initialized;
4041f4a2713aSLionel Sambuc T = S.Context.getBaseElementType(T);
4042f4a2713aSLionel Sambuc
4043f4a2713aSLionel Sambuc if (const RecordType *RT = T->getAs<RecordType>()) {
4044f4a2713aSLionel Sambuc if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
4045f4a2713aSLionel Sambuc bool NeedZeroInitialization = true;
4046f4a2713aSLionel Sambuc if (!S.getLangOpts().CPlusPlus11) {
4047f4a2713aSLionel Sambuc // C++98:
4048f4a2713aSLionel Sambuc // -- if T is a class type (clause 9) with a user-declared constructor
4049f4a2713aSLionel Sambuc // (12.1), then the default constructor for T is called (and the
4050f4a2713aSLionel Sambuc // initialization is ill-formed if T has no accessible default
4051f4a2713aSLionel Sambuc // constructor);
4052f4a2713aSLionel Sambuc if (ClassDecl->hasUserDeclaredConstructor())
4053f4a2713aSLionel Sambuc NeedZeroInitialization = false;
4054f4a2713aSLionel Sambuc } else {
4055f4a2713aSLionel Sambuc // C++11:
4056f4a2713aSLionel Sambuc // -- if T is a class type (clause 9) with either no default constructor
4057f4a2713aSLionel Sambuc // (12.1 [class.ctor]) or a default constructor that is user-provided
4058f4a2713aSLionel Sambuc // or deleted, then the object is default-initialized;
4059f4a2713aSLionel Sambuc CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl);
4060f4a2713aSLionel Sambuc if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted())
4061f4a2713aSLionel Sambuc NeedZeroInitialization = false;
4062f4a2713aSLionel Sambuc }
4063f4a2713aSLionel Sambuc
4064f4a2713aSLionel Sambuc // -- if T is a (possibly cv-qualified) non-union class type without a
4065f4a2713aSLionel Sambuc // user-provided or deleted default constructor, then the object is
4066f4a2713aSLionel Sambuc // zero-initialized and, if T has a non-trivial default constructor,
4067f4a2713aSLionel Sambuc // default-initialized;
4068f4a2713aSLionel Sambuc // The 'non-union' here was removed by DR1502. The 'non-trivial default
4069f4a2713aSLionel Sambuc // constructor' part was removed by DR1507.
4070f4a2713aSLionel Sambuc if (NeedZeroInitialization)
4071f4a2713aSLionel Sambuc Sequence.AddZeroInitializationStep(Entity.getType());
4072f4a2713aSLionel Sambuc
4073f4a2713aSLionel Sambuc // C++03:
4074f4a2713aSLionel Sambuc // -- if T is a non-union class type without a user-declared constructor,
4075f4a2713aSLionel Sambuc // then every non-static data member and base class component of T is
4076f4a2713aSLionel Sambuc // value-initialized;
4077f4a2713aSLionel Sambuc // [...] A program that calls for [...] value-initialization of an
4078f4a2713aSLionel Sambuc // entity of reference type is ill-formed.
4079f4a2713aSLionel Sambuc //
4080f4a2713aSLionel Sambuc // C++11 doesn't need this handling, because value-initialization does not
4081f4a2713aSLionel Sambuc // occur recursively there, and the implicit default constructor is
4082f4a2713aSLionel Sambuc // defined as deleted in the problematic cases.
4083f4a2713aSLionel Sambuc if (!S.getLangOpts().CPlusPlus11 &&
4084f4a2713aSLionel Sambuc ClassDecl->hasUninitializedReferenceMember()) {
4085f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference);
4086f4a2713aSLionel Sambuc return;
4087f4a2713aSLionel Sambuc }
4088f4a2713aSLionel Sambuc
4089f4a2713aSLionel Sambuc // If this is list-value-initialization, pass the empty init list on when
4090f4a2713aSLionel Sambuc // building the constructor call. This affects the semantics of a few
4091f4a2713aSLionel Sambuc // things (such as whether an explicit default constructor can be called).
4092f4a2713aSLionel Sambuc Expr *InitListAsExpr = InitList;
4093f4a2713aSLionel Sambuc MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0);
4094f4a2713aSLionel Sambuc bool InitListSyntax = InitList;
4095f4a2713aSLionel Sambuc
4096f4a2713aSLionel Sambuc return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence,
4097f4a2713aSLionel Sambuc InitListSyntax);
4098f4a2713aSLionel Sambuc }
4099f4a2713aSLionel Sambuc }
4100f4a2713aSLionel Sambuc
4101f4a2713aSLionel Sambuc Sequence.AddZeroInitializationStep(Entity.getType());
4102f4a2713aSLionel Sambuc }
4103f4a2713aSLionel Sambuc
4104f4a2713aSLionel Sambuc /// \brief Attempt default initialization (C++ [dcl.init]p6).
TryDefaultInitialization(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,InitializationSequence & Sequence)4105f4a2713aSLionel Sambuc static void TryDefaultInitialization(Sema &S,
4106f4a2713aSLionel Sambuc const InitializedEntity &Entity,
4107f4a2713aSLionel Sambuc const InitializationKind &Kind,
4108f4a2713aSLionel Sambuc InitializationSequence &Sequence) {
4109f4a2713aSLionel Sambuc assert(Kind.getKind() == InitializationKind::IK_Default);
4110f4a2713aSLionel Sambuc
4111f4a2713aSLionel Sambuc // C++ [dcl.init]p6:
4112f4a2713aSLionel Sambuc // To default-initialize an object of type T means:
4113f4a2713aSLionel Sambuc // - if T is an array type, each element is default-initialized;
4114f4a2713aSLionel Sambuc QualType DestType = S.Context.getBaseElementType(Entity.getType());
4115f4a2713aSLionel Sambuc
4116f4a2713aSLionel Sambuc // - if T is a (possibly cv-qualified) class type (Clause 9), the default
4117f4a2713aSLionel Sambuc // constructor for T is called (and the initialization is ill-formed if
4118f4a2713aSLionel Sambuc // T has no accessible default constructor);
4119f4a2713aSLionel Sambuc if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) {
4120f4a2713aSLionel Sambuc TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence);
4121f4a2713aSLionel Sambuc return;
4122f4a2713aSLionel Sambuc }
4123f4a2713aSLionel Sambuc
4124f4a2713aSLionel Sambuc // - otherwise, no initialization is performed.
4125f4a2713aSLionel Sambuc
4126f4a2713aSLionel Sambuc // If a program calls for the default initialization of an object of
4127f4a2713aSLionel Sambuc // a const-qualified type T, T shall be a class type with a user-provided
4128f4a2713aSLionel Sambuc // default constructor.
4129f4a2713aSLionel Sambuc if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) {
4130f4a2713aSLionel Sambuc Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst);
4131f4a2713aSLionel Sambuc return;
4132f4a2713aSLionel Sambuc }
4133f4a2713aSLionel Sambuc
4134f4a2713aSLionel Sambuc // If the destination type has a lifetime property, zero-initialize it.
4135f4a2713aSLionel Sambuc if (DestType.getQualifiers().hasObjCLifetime()) {
4136f4a2713aSLionel Sambuc Sequence.AddZeroInitializationStep(Entity.getType());
4137f4a2713aSLionel Sambuc return;
4138f4a2713aSLionel Sambuc }
4139f4a2713aSLionel Sambuc }
4140f4a2713aSLionel Sambuc
4141f4a2713aSLionel Sambuc /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]),
4142f4a2713aSLionel Sambuc /// which enumerates all conversion functions and performs overload resolution
4143f4a2713aSLionel Sambuc /// to select the best.
TryUserDefinedConversion(Sema & S,QualType DestType,const InitializationKind & Kind,Expr * Initializer,InitializationSequence & Sequence,bool TopLevelOfInitList)4144f4a2713aSLionel Sambuc static void TryUserDefinedConversion(Sema &S,
4145*0a6a1f1dSLionel Sambuc QualType DestType,
4146f4a2713aSLionel Sambuc const InitializationKind &Kind,
4147f4a2713aSLionel Sambuc Expr *Initializer,
4148f4a2713aSLionel Sambuc InitializationSequence &Sequence,
4149f4a2713aSLionel Sambuc bool TopLevelOfInitList) {
4150f4a2713aSLionel Sambuc assert(!DestType->isReferenceType() && "References are handled elsewhere");
4151f4a2713aSLionel Sambuc QualType SourceType = Initializer->getType();
4152f4a2713aSLionel Sambuc assert((DestType->isRecordType() || SourceType->isRecordType()) &&
4153f4a2713aSLionel Sambuc "Must have a class type to perform a user-defined conversion");
4154f4a2713aSLionel Sambuc
4155f4a2713aSLionel Sambuc // Build the candidate set directly in the initialization sequence
4156f4a2713aSLionel Sambuc // structure, so that it will persist if we fail.
4157f4a2713aSLionel Sambuc OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet();
4158f4a2713aSLionel Sambuc CandidateSet.clear();
4159f4a2713aSLionel Sambuc
4160f4a2713aSLionel Sambuc // Determine whether we are allowed to call explicit constructors or
4161f4a2713aSLionel Sambuc // explicit conversion operators.
4162f4a2713aSLionel Sambuc bool AllowExplicit = Kind.AllowExplicit();
4163f4a2713aSLionel Sambuc
4164f4a2713aSLionel Sambuc if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) {
4165f4a2713aSLionel Sambuc // The type we're converting to is a class type. Enumerate its constructors
4166f4a2713aSLionel Sambuc // to see if there is a suitable conversion.
4167f4a2713aSLionel Sambuc CXXRecordDecl *DestRecordDecl
4168f4a2713aSLionel Sambuc = cast<CXXRecordDecl>(DestRecordType->getDecl());
4169f4a2713aSLionel Sambuc
4170f4a2713aSLionel Sambuc // Try to complete the type we're converting to.
4171f4a2713aSLionel Sambuc if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) {
4172f4a2713aSLionel Sambuc DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl);
4173f4a2713aSLionel Sambuc // The container holding the constructors can under certain conditions
4174f4a2713aSLionel Sambuc // be changed while iterating. To be safe we copy the lookup results
4175f4a2713aSLionel Sambuc // to a new container.
4176f4a2713aSLionel Sambuc SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end());
4177f4a2713aSLionel Sambuc for (SmallVectorImpl<NamedDecl *>::iterator
4178f4a2713aSLionel Sambuc Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end();
4179f4a2713aSLionel Sambuc Con != ConEnd; ++Con) {
4180f4a2713aSLionel Sambuc NamedDecl *D = *Con;
4181f4a2713aSLionel Sambuc DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess());
4182f4a2713aSLionel Sambuc
4183f4a2713aSLionel Sambuc // Find the constructor (which may be a template).
4184*0a6a1f1dSLionel Sambuc CXXConstructorDecl *Constructor = nullptr;
4185f4a2713aSLionel Sambuc FunctionTemplateDecl *ConstructorTmpl
4186f4a2713aSLionel Sambuc = dyn_cast<FunctionTemplateDecl>(D);
4187f4a2713aSLionel Sambuc if (ConstructorTmpl)
4188f4a2713aSLionel Sambuc Constructor = cast<CXXConstructorDecl>(
4189f4a2713aSLionel Sambuc ConstructorTmpl->getTemplatedDecl());
4190f4a2713aSLionel Sambuc else
4191f4a2713aSLionel Sambuc Constructor = cast<CXXConstructorDecl>(D);
4192f4a2713aSLionel Sambuc
4193f4a2713aSLionel Sambuc if (!Constructor->isInvalidDecl() &&
4194f4a2713aSLionel Sambuc Constructor->isConvertingConstructor(AllowExplicit)) {
4195f4a2713aSLionel Sambuc if (ConstructorTmpl)
4196f4a2713aSLionel Sambuc S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl,
4197*0a6a1f1dSLionel Sambuc /*ExplicitArgs*/ nullptr,
4198f4a2713aSLionel Sambuc Initializer, CandidateSet,
4199f4a2713aSLionel Sambuc /*SuppressUserConversions=*/true);
4200f4a2713aSLionel Sambuc else
4201f4a2713aSLionel Sambuc S.AddOverloadCandidate(Constructor, FoundDecl,
4202f4a2713aSLionel Sambuc Initializer, CandidateSet,
4203f4a2713aSLionel Sambuc /*SuppressUserConversions=*/true);
4204f4a2713aSLionel Sambuc }
4205f4a2713aSLionel Sambuc }
4206f4a2713aSLionel Sambuc }
4207f4a2713aSLionel Sambuc }
4208f4a2713aSLionel Sambuc
4209f4a2713aSLionel Sambuc SourceLocation DeclLoc = Initializer->getLocStart();
4210f4a2713aSLionel Sambuc
4211f4a2713aSLionel Sambuc if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) {
4212f4a2713aSLionel Sambuc // The type we're converting from is a class type, enumerate its conversion
4213f4a2713aSLionel Sambuc // functions.
4214f4a2713aSLionel Sambuc
4215f4a2713aSLionel Sambuc // We can only enumerate the conversion functions for a complete type; if
4216f4a2713aSLionel Sambuc // the type isn't complete, simply skip this step.
4217f4a2713aSLionel Sambuc if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) {
4218f4a2713aSLionel Sambuc CXXRecordDecl *SourceRecordDecl
4219f4a2713aSLionel Sambuc = cast<CXXRecordDecl>(SourceRecordType->getDecl());
4220f4a2713aSLionel Sambuc
4221f4a2713aSLionel Sambuc std::pair<CXXRecordDecl::conversion_iterator,
4222f4a2713aSLionel Sambuc CXXRecordDecl::conversion_iterator>
4223f4a2713aSLionel Sambuc Conversions = SourceRecordDecl->getVisibleConversionFunctions();
4224f4a2713aSLionel Sambuc for (CXXRecordDecl::conversion_iterator
4225f4a2713aSLionel Sambuc I = Conversions.first, E = Conversions.second; I != E; ++I) {
4226f4a2713aSLionel Sambuc NamedDecl *D = *I;
4227f4a2713aSLionel Sambuc CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext());
4228f4a2713aSLionel Sambuc if (isa<UsingShadowDecl>(D))
4229f4a2713aSLionel Sambuc D = cast<UsingShadowDecl>(D)->getTargetDecl();
4230f4a2713aSLionel Sambuc
4231f4a2713aSLionel Sambuc FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D);
4232f4a2713aSLionel Sambuc CXXConversionDecl *Conv;
4233f4a2713aSLionel Sambuc if (ConvTemplate)
4234f4a2713aSLionel Sambuc Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl());
4235f4a2713aSLionel Sambuc else
4236f4a2713aSLionel Sambuc Conv = cast<CXXConversionDecl>(D);
4237f4a2713aSLionel Sambuc
4238f4a2713aSLionel Sambuc if (AllowExplicit || !Conv->isExplicit()) {
4239f4a2713aSLionel Sambuc if (ConvTemplate)
4240f4a2713aSLionel Sambuc S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(),
4241f4a2713aSLionel Sambuc ActingDC, Initializer, DestType,
4242*0a6a1f1dSLionel Sambuc CandidateSet, AllowExplicit);
4243f4a2713aSLionel Sambuc else
4244f4a2713aSLionel Sambuc S.AddConversionCandidate(Conv, I.getPair(), ActingDC,
4245*0a6a1f1dSLionel Sambuc Initializer, DestType, CandidateSet,
4246*0a6a1f1dSLionel Sambuc AllowExplicit);
4247f4a2713aSLionel Sambuc }
4248f4a2713aSLionel Sambuc }
4249f4a2713aSLionel Sambuc }
4250f4a2713aSLionel Sambuc }
4251f4a2713aSLionel Sambuc
4252f4a2713aSLionel Sambuc // Perform overload resolution. If it fails, return the failed result.
4253f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
4254f4a2713aSLionel Sambuc if (OverloadingResult Result
4255f4a2713aSLionel Sambuc = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) {
4256f4a2713aSLionel Sambuc Sequence.SetOverloadFailure(
4257f4a2713aSLionel Sambuc InitializationSequence::FK_UserConversionOverloadFailed,
4258f4a2713aSLionel Sambuc Result);
4259f4a2713aSLionel Sambuc return;
4260f4a2713aSLionel Sambuc }
4261f4a2713aSLionel Sambuc
4262f4a2713aSLionel Sambuc FunctionDecl *Function = Best->Function;
4263f4a2713aSLionel Sambuc Function->setReferenced();
4264f4a2713aSLionel Sambuc bool HadMultipleCandidates = (CandidateSet.size() > 1);
4265f4a2713aSLionel Sambuc
4266f4a2713aSLionel Sambuc if (isa<CXXConstructorDecl>(Function)) {
4267f4a2713aSLionel Sambuc // Add the user-defined conversion step. Any cv-qualification conversion is
4268f4a2713aSLionel Sambuc // subsumed by the initialization. Per DR5, the created temporary is of the
4269f4a2713aSLionel Sambuc // cv-unqualified type of the destination.
4270f4a2713aSLionel Sambuc Sequence.AddUserConversionStep(Function, Best->FoundDecl,
4271f4a2713aSLionel Sambuc DestType.getUnqualifiedType(),
4272f4a2713aSLionel Sambuc HadMultipleCandidates);
4273f4a2713aSLionel Sambuc return;
4274f4a2713aSLionel Sambuc }
4275f4a2713aSLionel Sambuc
4276f4a2713aSLionel Sambuc // Add the user-defined conversion step that calls the conversion function.
4277f4a2713aSLionel Sambuc QualType ConvType = Function->getCallResultType();
4278f4a2713aSLionel Sambuc if (ConvType->getAs<RecordType>()) {
4279f4a2713aSLionel Sambuc // If we're converting to a class type, there may be an copy of
4280f4a2713aSLionel Sambuc // the resulting temporary object (possible to create an object of
4281f4a2713aSLionel Sambuc // a base class type). That copy is not a separate conversion, so
4282f4a2713aSLionel Sambuc // we just make a note of the actual destination type (possibly a
4283f4a2713aSLionel Sambuc // base class of the type returned by the conversion function) and
4284f4a2713aSLionel Sambuc // let the user-defined conversion step handle the conversion.
4285f4a2713aSLionel Sambuc Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType,
4286f4a2713aSLionel Sambuc HadMultipleCandidates);
4287f4a2713aSLionel Sambuc return;
4288f4a2713aSLionel Sambuc }
4289f4a2713aSLionel Sambuc
4290f4a2713aSLionel Sambuc Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType,
4291f4a2713aSLionel Sambuc HadMultipleCandidates);
4292f4a2713aSLionel Sambuc
4293f4a2713aSLionel Sambuc // If the conversion following the call to the conversion function
4294f4a2713aSLionel Sambuc // is interesting, add it as a separate step.
4295f4a2713aSLionel Sambuc if (Best->FinalConversion.First || Best->FinalConversion.Second ||
4296f4a2713aSLionel Sambuc Best->FinalConversion.Third) {
4297f4a2713aSLionel Sambuc ImplicitConversionSequence ICS;
4298f4a2713aSLionel Sambuc ICS.setStandard();
4299f4a2713aSLionel Sambuc ICS.Standard = Best->FinalConversion;
4300f4a2713aSLionel Sambuc Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList);
4301f4a2713aSLionel Sambuc }
4302f4a2713aSLionel Sambuc }
4303f4a2713aSLionel Sambuc
4304f4a2713aSLionel Sambuc /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>,
4305f4a2713aSLionel Sambuc /// a function with a pointer return type contains a 'return false;' statement.
4306f4a2713aSLionel Sambuc /// In C++11, 'false' is not a null pointer, so this breaks the build of any
4307f4a2713aSLionel Sambuc /// code using that header.
4308f4a2713aSLionel Sambuc ///
4309f4a2713aSLionel Sambuc /// Work around this by treating 'return false;' as zero-initializing the result
4310f4a2713aSLionel Sambuc /// if it's used in a pointer-returning function in a system header.
isLibstdcxxPointerReturnFalseHack(Sema & S,const InitializedEntity & Entity,const Expr * Init)4311f4a2713aSLionel Sambuc static bool isLibstdcxxPointerReturnFalseHack(Sema &S,
4312f4a2713aSLionel Sambuc const InitializedEntity &Entity,
4313f4a2713aSLionel Sambuc const Expr *Init) {
4314f4a2713aSLionel Sambuc return S.getLangOpts().CPlusPlus11 &&
4315f4a2713aSLionel Sambuc Entity.getKind() == InitializedEntity::EK_Result &&
4316f4a2713aSLionel Sambuc Entity.getType()->isPointerType() &&
4317f4a2713aSLionel Sambuc isa<CXXBoolLiteralExpr>(Init) &&
4318f4a2713aSLionel Sambuc !cast<CXXBoolLiteralExpr>(Init)->getValue() &&
4319f4a2713aSLionel Sambuc S.getSourceManager().isInSystemHeader(Init->getExprLoc());
4320f4a2713aSLionel Sambuc }
4321f4a2713aSLionel Sambuc
4322f4a2713aSLionel Sambuc /// The non-zero enum values here are indexes into diagnostic alternatives.
4323f4a2713aSLionel Sambuc enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar };
4324f4a2713aSLionel Sambuc
4325f4a2713aSLionel Sambuc /// Determines whether this expression is an acceptable ICR source.
isInvalidICRSource(ASTContext & C,Expr * e,bool isAddressOf,bool & isWeakAccess)4326f4a2713aSLionel Sambuc static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e,
4327f4a2713aSLionel Sambuc bool isAddressOf, bool &isWeakAccess) {
4328f4a2713aSLionel Sambuc // Skip parens.
4329f4a2713aSLionel Sambuc e = e->IgnoreParens();
4330f4a2713aSLionel Sambuc
4331f4a2713aSLionel Sambuc // Skip address-of nodes.
4332f4a2713aSLionel Sambuc if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) {
4333f4a2713aSLionel Sambuc if (op->getOpcode() == UO_AddrOf)
4334f4a2713aSLionel Sambuc return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true,
4335f4a2713aSLionel Sambuc isWeakAccess);
4336f4a2713aSLionel Sambuc
4337f4a2713aSLionel Sambuc // Skip certain casts.
4338f4a2713aSLionel Sambuc } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) {
4339f4a2713aSLionel Sambuc switch (ce->getCastKind()) {
4340f4a2713aSLionel Sambuc case CK_Dependent:
4341f4a2713aSLionel Sambuc case CK_BitCast:
4342f4a2713aSLionel Sambuc case CK_LValueBitCast:
4343f4a2713aSLionel Sambuc case CK_NoOp:
4344f4a2713aSLionel Sambuc return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess);
4345f4a2713aSLionel Sambuc
4346f4a2713aSLionel Sambuc case CK_ArrayToPointerDecay:
4347f4a2713aSLionel Sambuc return IIK_nonscalar;
4348f4a2713aSLionel Sambuc
4349f4a2713aSLionel Sambuc case CK_NullToPointer:
4350f4a2713aSLionel Sambuc return IIK_okay;
4351f4a2713aSLionel Sambuc
4352f4a2713aSLionel Sambuc default:
4353f4a2713aSLionel Sambuc break;
4354f4a2713aSLionel Sambuc }
4355f4a2713aSLionel Sambuc
4356f4a2713aSLionel Sambuc // If we have a declaration reference, it had better be a local variable.
4357f4a2713aSLionel Sambuc } else if (isa<DeclRefExpr>(e)) {
4358f4a2713aSLionel Sambuc // set isWeakAccess to true, to mean that there will be an implicit
4359f4a2713aSLionel Sambuc // load which requires a cleanup.
4360f4a2713aSLionel Sambuc if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
4361f4a2713aSLionel Sambuc isWeakAccess = true;
4362f4a2713aSLionel Sambuc
4363f4a2713aSLionel Sambuc if (!isAddressOf) return IIK_nonlocal;
4364f4a2713aSLionel Sambuc
4365f4a2713aSLionel Sambuc VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl());
4366f4a2713aSLionel Sambuc if (!var) return IIK_nonlocal;
4367f4a2713aSLionel Sambuc
4368f4a2713aSLionel Sambuc return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal);
4369f4a2713aSLionel Sambuc
4370f4a2713aSLionel Sambuc // If we have a conditional operator, check both sides.
4371f4a2713aSLionel Sambuc } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) {
4372f4a2713aSLionel Sambuc if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf,
4373f4a2713aSLionel Sambuc isWeakAccess))
4374f4a2713aSLionel Sambuc return iik;
4375f4a2713aSLionel Sambuc
4376f4a2713aSLionel Sambuc return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess);
4377f4a2713aSLionel Sambuc
4378f4a2713aSLionel Sambuc // These are never scalar.
4379f4a2713aSLionel Sambuc } else if (isa<ArraySubscriptExpr>(e)) {
4380f4a2713aSLionel Sambuc return IIK_nonscalar;
4381f4a2713aSLionel Sambuc
4382f4a2713aSLionel Sambuc // Otherwise, it needs to be a null pointer constant.
4383f4a2713aSLionel Sambuc } else {
4384f4a2713aSLionel Sambuc return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull)
4385f4a2713aSLionel Sambuc ? IIK_okay : IIK_nonlocal);
4386f4a2713aSLionel Sambuc }
4387f4a2713aSLionel Sambuc
4388f4a2713aSLionel Sambuc return IIK_nonlocal;
4389f4a2713aSLionel Sambuc }
4390f4a2713aSLionel Sambuc
4391f4a2713aSLionel Sambuc /// Check whether the given expression is a valid operand for an
4392f4a2713aSLionel Sambuc /// indirect copy/restore.
checkIndirectCopyRestoreSource(Sema & S,Expr * src)4393f4a2713aSLionel Sambuc static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) {
4394f4a2713aSLionel Sambuc assert(src->isRValue());
4395f4a2713aSLionel Sambuc bool isWeakAccess = false;
4396f4a2713aSLionel Sambuc InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess);
4397f4a2713aSLionel Sambuc // If isWeakAccess to true, there will be an implicit
4398f4a2713aSLionel Sambuc // load which requires a cleanup.
4399f4a2713aSLionel Sambuc if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess)
4400f4a2713aSLionel Sambuc S.ExprNeedsCleanups = true;
4401f4a2713aSLionel Sambuc
4402f4a2713aSLionel Sambuc if (iik == IIK_okay) return;
4403f4a2713aSLionel Sambuc
4404f4a2713aSLionel Sambuc S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback)
4405f4a2713aSLionel Sambuc << ((unsigned) iik - 1) // shift index into diagnostic explanations
4406f4a2713aSLionel Sambuc << src->getSourceRange();
4407f4a2713aSLionel Sambuc }
4408f4a2713aSLionel Sambuc
4409f4a2713aSLionel Sambuc /// \brief Determine whether we have compatible array types for the
4410f4a2713aSLionel Sambuc /// purposes of GNU by-copy array initialization.
hasCompatibleArrayTypes(ASTContext & Context,const ArrayType * Dest,const ArrayType * Source)4411f4a2713aSLionel Sambuc static bool hasCompatibleArrayTypes(ASTContext &Context,
4412f4a2713aSLionel Sambuc const ArrayType *Dest,
4413f4a2713aSLionel Sambuc const ArrayType *Source) {
4414f4a2713aSLionel Sambuc // If the source and destination array types are equivalent, we're
4415f4a2713aSLionel Sambuc // done.
4416f4a2713aSLionel Sambuc if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0)))
4417f4a2713aSLionel Sambuc return true;
4418f4a2713aSLionel Sambuc
4419f4a2713aSLionel Sambuc // Make sure that the element types are the same.
4420f4a2713aSLionel Sambuc if (!Context.hasSameType(Dest->getElementType(), Source->getElementType()))
4421f4a2713aSLionel Sambuc return false;
4422f4a2713aSLionel Sambuc
4423f4a2713aSLionel Sambuc // The only mismatch we allow is when the destination is an
4424f4a2713aSLionel Sambuc // incomplete array type and the source is a constant array type.
4425f4a2713aSLionel Sambuc return Source->isConstantArrayType() && Dest->isIncompleteArrayType();
4426f4a2713aSLionel Sambuc }
4427f4a2713aSLionel Sambuc
tryObjCWritebackConversion(Sema & S,InitializationSequence & Sequence,const InitializedEntity & Entity,Expr * Initializer)4428f4a2713aSLionel Sambuc static bool tryObjCWritebackConversion(Sema &S,
4429f4a2713aSLionel Sambuc InitializationSequence &Sequence,
4430f4a2713aSLionel Sambuc const InitializedEntity &Entity,
4431f4a2713aSLionel Sambuc Expr *Initializer) {
4432f4a2713aSLionel Sambuc bool ArrayDecay = false;
4433f4a2713aSLionel Sambuc QualType ArgType = Initializer->getType();
4434f4a2713aSLionel Sambuc QualType ArgPointee;
4435f4a2713aSLionel Sambuc if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) {
4436f4a2713aSLionel Sambuc ArrayDecay = true;
4437f4a2713aSLionel Sambuc ArgPointee = ArgArrayType->getElementType();
4438f4a2713aSLionel Sambuc ArgType = S.Context.getPointerType(ArgPointee);
4439f4a2713aSLionel Sambuc }
4440f4a2713aSLionel Sambuc
4441f4a2713aSLionel Sambuc // Handle write-back conversion.
4442f4a2713aSLionel Sambuc QualType ConvertedArgType;
4443f4a2713aSLionel Sambuc if (!S.isObjCWritebackConversion(ArgType, Entity.getType(),
4444f4a2713aSLionel Sambuc ConvertedArgType))
4445f4a2713aSLionel Sambuc return false;
4446f4a2713aSLionel Sambuc
4447f4a2713aSLionel Sambuc // We should copy unless we're passing to an argument explicitly
4448f4a2713aSLionel Sambuc // marked 'out'.
4449f4a2713aSLionel Sambuc bool ShouldCopy = true;
4450f4a2713aSLionel Sambuc if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl()))
4451f4a2713aSLionel Sambuc ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out);
4452f4a2713aSLionel Sambuc
4453f4a2713aSLionel Sambuc // Do we need an lvalue conversion?
4454f4a2713aSLionel Sambuc if (ArrayDecay || Initializer->isGLValue()) {
4455f4a2713aSLionel Sambuc ImplicitConversionSequence ICS;
4456f4a2713aSLionel Sambuc ICS.setStandard();
4457f4a2713aSLionel Sambuc ICS.Standard.setAsIdentityConversion();
4458f4a2713aSLionel Sambuc
4459f4a2713aSLionel Sambuc QualType ResultType;
4460f4a2713aSLionel Sambuc if (ArrayDecay) {
4461f4a2713aSLionel Sambuc ICS.Standard.First = ICK_Array_To_Pointer;
4462f4a2713aSLionel Sambuc ResultType = S.Context.getPointerType(ArgPointee);
4463f4a2713aSLionel Sambuc } else {
4464f4a2713aSLionel Sambuc ICS.Standard.First = ICK_Lvalue_To_Rvalue;
4465f4a2713aSLionel Sambuc ResultType = Initializer->getType().getNonLValueExprType(S.Context);
4466f4a2713aSLionel Sambuc }
4467f4a2713aSLionel Sambuc
4468f4a2713aSLionel Sambuc Sequence.AddConversionSequenceStep(ICS, ResultType);
4469f4a2713aSLionel Sambuc }
4470f4a2713aSLionel Sambuc
4471f4a2713aSLionel Sambuc Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy);
4472f4a2713aSLionel Sambuc return true;
4473f4a2713aSLionel Sambuc }
4474f4a2713aSLionel Sambuc
TryOCLSamplerInitialization(Sema & S,InitializationSequence & Sequence,QualType DestType,Expr * Initializer)4475f4a2713aSLionel Sambuc static bool TryOCLSamplerInitialization(Sema &S,
4476f4a2713aSLionel Sambuc InitializationSequence &Sequence,
4477f4a2713aSLionel Sambuc QualType DestType,
4478f4a2713aSLionel Sambuc Expr *Initializer) {
4479f4a2713aSLionel Sambuc if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() ||
4480f4a2713aSLionel Sambuc !Initializer->isIntegerConstantExpr(S.getASTContext()))
4481f4a2713aSLionel Sambuc return false;
4482f4a2713aSLionel Sambuc
4483f4a2713aSLionel Sambuc Sequence.AddOCLSamplerInitStep(DestType);
4484f4a2713aSLionel Sambuc return true;
4485f4a2713aSLionel Sambuc }
4486f4a2713aSLionel Sambuc
4487f4a2713aSLionel Sambuc //
4488f4a2713aSLionel Sambuc // OpenCL 1.2 spec, s6.12.10
4489f4a2713aSLionel Sambuc //
4490f4a2713aSLionel Sambuc // The event argument can also be used to associate the
4491f4a2713aSLionel Sambuc // async_work_group_copy with a previous async copy allowing
4492f4a2713aSLionel Sambuc // an event to be shared by multiple async copies; otherwise
4493f4a2713aSLionel Sambuc // event should be zero.
4494f4a2713aSLionel Sambuc //
TryOCLZeroEventInitialization(Sema & S,InitializationSequence & Sequence,QualType DestType,Expr * Initializer)4495f4a2713aSLionel Sambuc static bool TryOCLZeroEventInitialization(Sema &S,
4496f4a2713aSLionel Sambuc InitializationSequence &Sequence,
4497f4a2713aSLionel Sambuc QualType DestType,
4498f4a2713aSLionel Sambuc Expr *Initializer) {
4499f4a2713aSLionel Sambuc if (!S.getLangOpts().OpenCL || !DestType->isEventT() ||
4500f4a2713aSLionel Sambuc !Initializer->isIntegerConstantExpr(S.getASTContext()) ||
4501f4a2713aSLionel Sambuc (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0))
4502f4a2713aSLionel Sambuc return false;
4503f4a2713aSLionel Sambuc
4504f4a2713aSLionel Sambuc Sequence.AddOCLZeroEventStep(DestType);
4505f4a2713aSLionel Sambuc return true;
4506f4a2713aSLionel Sambuc }
4507f4a2713aSLionel Sambuc
InitializationSequence(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,MultiExprArg Args,bool TopLevelOfInitList)4508f4a2713aSLionel Sambuc InitializationSequence::InitializationSequence(Sema &S,
4509f4a2713aSLionel Sambuc const InitializedEntity &Entity,
4510f4a2713aSLionel Sambuc const InitializationKind &Kind,
4511f4a2713aSLionel Sambuc MultiExprArg Args,
4512f4a2713aSLionel Sambuc bool TopLevelOfInitList)
4513*0a6a1f1dSLionel Sambuc : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) {
4514f4a2713aSLionel Sambuc InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList);
4515f4a2713aSLionel Sambuc }
4516f4a2713aSLionel Sambuc
InitializeFrom(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,MultiExprArg Args,bool TopLevelOfInitList)4517f4a2713aSLionel Sambuc void InitializationSequence::InitializeFrom(Sema &S,
4518f4a2713aSLionel Sambuc const InitializedEntity &Entity,
4519f4a2713aSLionel Sambuc const InitializationKind &Kind,
4520f4a2713aSLionel Sambuc MultiExprArg Args,
4521f4a2713aSLionel Sambuc bool TopLevelOfInitList) {
4522f4a2713aSLionel Sambuc ASTContext &Context = S.Context;
4523f4a2713aSLionel Sambuc
4524f4a2713aSLionel Sambuc // Eliminate non-overload placeholder types in the arguments. We
4525f4a2713aSLionel Sambuc // need to do this before checking whether types are dependent
4526f4a2713aSLionel Sambuc // because lowering a pseudo-object expression might well give us
4527f4a2713aSLionel Sambuc // something of dependent type.
4528f4a2713aSLionel Sambuc for (unsigned I = 0, E = Args.size(); I != E; ++I)
4529f4a2713aSLionel Sambuc if (Args[I]->getType()->isNonOverloadPlaceholderType()) {
4530f4a2713aSLionel Sambuc // FIXME: should we be doing this here?
4531f4a2713aSLionel Sambuc ExprResult result = S.CheckPlaceholderExpr(Args[I]);
4532f4a2713aSLionel Sambuc if (result.isInvalid()) {
4533f4a2713aSLionel Sambuc SetFailed(FK_PlaceholderType);
4534f4a2713aSLionel Sambuc return;
4535f4a2713aSLionel Sambuc }
4536*0a6a1f1dSLionel Sambuc Args[I] = result.get();
4537f4a2713aSLionel Sambuc }
4538f4a2713aSLionel Sambuc
4539f4a2713aSLionel Sambuc // C++0x [dcl.init]p16:
4540f4a2713aSLionel Sambuc // The semantics of initializers are as follows. The destination type is
4541f4a2713aSLionel Sambuc // the type of the object or reference being initialized and the source
4542f4a2713aSLionel Sambuc // type is the type of the initializer expression. The source type is not
4543f4a2713aSLionel Sambuc // defined when the initializer is a braced-init-list or when it is a
4544f4a2713aSLionel Sambuc // parenthesized list of expressions.
4545f4a2713aSLionel Sambuc QualType DestType = Entity.getType();
4546f4a2713aSLionel Sambuc
4547f4a2713aSLionel Sambuc if (DestType->isDependentType() ||
4548f4a2713aSLionel Sambuc Expr::hasAnyTypeDependentArguments(Args)) {
4549f4a2713aSLionel Sambuc SequenceKind = DependentSequence;
4550f4a2713aSLionel Sambuc return;
4551f4a2713aSLionel Sambuc }
4552f4a2713aSLionel Sambuc
4553f4a2713aSLionel Sambuc // Almost everything is a normal sequence.
4554f4a2713aSLionel Sambuc setSequenceKind(NormalSequence);
4555f4a2713aSLionel Sambuc
4556f4a2713aSLionel Sambuc QualType SourceType;
4557*0a6a1f1dSLionel Sambuc Expr *Initializer = nullptr;
4558f4a2713aSLionel Sambuc if (Args.size() == 1) {
4559f4a2713aSLionel Sambuc Initializer = Args[0];
4560*0a6a1f1dSLionel Sambuc if (S.getLangOpts().ObjC1) {
4561*0a6a1f1dSLionel Sambuc if (S.CheckObjCBridgeRelatedConversions(Initializer->getLocStart(),
4562*0a6a1f1dSLionel Sambuc DestType, Initializer->getType(),
4563*0a6a1f1dSLionel Sambuc Initializer) ||
4564*0a6a1f1dSLionel Sambuc S.ConversionToObjCStringLiteralCheck(DestType, Initializer))
4565*0a6a1f1dSLionel Sambuc Args[0] = Initializer;
4566*0a6a1f1dSLionel Sambuc }
4567f4a2713aSLionel Sambuc if (!isa<InitListExpr>(Initializer))
4568f4a2713aSLionel Sambuc SourceType = Initializer->getType();
4569f4a2713aSLionel Sambuc }
4570f4a2713aSLionel Sambuc
4571f4a2713aSLionel Sambuc // - If the initializer is a (non-parenthesized) braced-init-list, the
4572f4a2713aSLionel Sambuc // object is list-initialized (8.5.4).
4573f4a2713aSLionel Sambuc if (Kind.getKind() != InitializationKind::IK_Direct) {
4574f4a2713aSLionel Sambuc if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) {
4575f4a2713aSLionel Sambuc TryListInitialization(S, Entity, Kind, InitList, *this);
4576f4a2713aSLionel Sambuc return;
4577f4a2713aSLionel Sambuc }
4578f4a2713aSLionel Sambuc }
4579f4a2713aSLionel Sambuc
4580f4a2713aSLionel Sambuc // - If the destination type is a reference type, see 8.5.3.
4581f4a2713aSLionel Sambuc if (DestType->isReferenceType()) {
4582f4a2713aSLionel Sambuc // C++0x [dcl.init.ref]p1:
4583f4a2713aSLionel Sambuc // A variable declared to be a T& or T&&, that is, "reference to type T"
4584f4a2713aSLionel Sambuc // (8.3.2), shall be initialized by an object, or function, of type T or
4585f4a2713aSLionel Sambuc // by an object that can be converted into a T.
4586f4a2713aSLionel Sambuc // (Therefore, multiple arguments are not permitted.)
4587f4a2713aSLionel Sambuc if (Args.size() != 1)
4588f4a2713aSLionel Sambuc SetFailed(FK_TooManyInitsForReference);
4589f4a2713aSLionel Sambuc else
4590f4a2713aSLionel Sambuc TryReferenceInitialization(S, Entity, Kind, Args[0], *this);
4591f4a2713aSLionel Sambuc return;
4592f4a2713aSLionel Sambuc }
4593f4a2713aSLionel Sambuc
4594f4a2713aSLionel Sambuc // - If the initializer is (), the object is value-initialized.
4595f4a2713aSLionel Sambuc if (Kind.getKind() == InitializationKind::IK_Value ||
4596f4a2713aSLionel Sambuc (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) {
4597f4a2713aSLionel Sambuc TryValueInitialization(S, Entity, Kind, *this);
4598f4a2713aSLionel Sambuc return;
4599f4a2713aSLionel Sambuc }
4600f4a2713aSLionel Sambuc
4601f4a2713aSLionel Sambuc // Handle default initialization.
4602f4a2713aSLionel Sambuc if (Kind.getKind() == InitializationKind::IK_Default) {
4603f4a2713aSLionel Sambuc TryDefaultInitialization(S, Entity, Kind, *this);
4604f4a2713aSLionel Sambuc return;
4605f4a2713aSLionel Sambuc }
4606f4a2713aSLionel Sambuc
4607f4a2713aSLionel Sambuc // - If the destination type is an array of characters, an array of
4608f4a2713aSLionel Sambuc // char16_t, an array of char32_t, or an array of wchar_t, and the
4609f4a2713aSLionel Sambuc // initializer is a string literal, see 8.5.2.
4610f4a2713aSLionel Sambuc // - Otherwise, if the destination type is an array, the program is
4611f4a2713aSLionel Sambuc // ill-formed.
4612f4a2713aSLionel Sambuc if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) {
4613f4a2713aSLionel Sambuc if (Initializer && isa<VariableArrayType>(DestAT)) {
4614f4a2713aSLionel Sambuc SetFailed(FK_VariableLengthArrayHasInitializer);
4615f4a2713aSLionel Sambuc return;
4616f4a2713aSLionel Sambuc }
4617f4a2713aSLionel Sambuc
4618f4a2713aSLionel Sambuc if (Initializer) {
4619f4a2713aSLionel Sambuc switch (IsStringInit(Initializer, DestAT, Context)) {
4620f4a2713aSLionel Sambuc case SIF_None:
4621f4a2713aSLionel Sambuc TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this);
4622f4a2713aSLionel Sambuc return;
4623f4a2713aSLionel Sambuc case SIF_NarrowStringIntoWideChar:
4624f4a2713aSLionel Sambuc SetFailed(FK_NarrowStringIntoWideCharArray);
4625f4a2713aSLionel Sambuc return;
4626f4a2713aSLionel Sambuc case SIF_WideStringIntoChar:
4627f4a2713aSLionel Sambuc SetFailed(FK_WideStringIntoCharArray);
4628f4a2713aSLionel Sambuc return;
4629f4a2713aSLionel Sambuc case SIF_IncompatWideStringIntoWideChar:
4630f4a2713aSLionel Sambuc SetFailed(FK_IncompatWideStringIntoWideChar);
4631f4a2713aSLionel Sambuc return;
4632f4a2713aSLionel Sambuc case SIF_Other:
4633f4a2713aSLionel Sambuc break;
4634f4a2713aSLionel Sambuc }
4635f4a2713aSLionel Sambuc }
4636f4a2713aSLionel Sambuc
4637f4a2713aSLionel Sambuc // Note: as an GNU C extension, we allow initialization of an
4638f4a2713aSLionel Sambuc // array from a compound literal that creates an array of the same
4639f4a2713aSLionel Sambuc // type, so long as the initializer has no side effects.
4640f4a2713aSLionel Sambuc if (!S.getLangOpts().CPlusPlus && Initializer &&
4641f4a2713aSLionel Sambuc isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) &&
4642f4a2713aSLionel Sambuc Initializer->getType()->isArrayType()) {
4643f4a2713aSLionel Sambuc const ArrayType *SourceAT
4644f4a2713aSLionel Sambuc = Context.getAsArrayType(Initializer->getType());
4645f4a2713aSLionel Sambuc if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT))
4646f4a2713aSLionel Sambuc SetFailed(FK_ArrayTypeMismatch);
4647f4a2713aSLionel Sambuc else if (Initializer->HasSideEffects(S.Context))
4648f4a2713aSLionel Sambuc SetFailed(FK_NonConstantArrayInit);
4649f4a2713aSLionel Sambuc else {
4650f4a2713aSLionel Sambuc AddArrayInitStep(DestType);
4651f4a2713aSLionel Sambuc }
4652f4a2713aSLionel Sambuc }
4653f4a2713aSLionel Sambuc // Note: as a GNU C++ extension, we allow list-initialization of a
4654f4a2713aSLionel Sambuc // class member of array type from a parenthesized initializer list.
4655f4a2713aSLionel Sambuc else if (S.getLangOpts().CPlusPlus &&
4656f4a2713aSLionel Sambuc Entity.getKind() == InitializedEntity::EK_Member &&
4657f4a2713aSLionel Sambuc Initializer && isa<InitListExpr>(Initializer)) {
4658f4a2713aSLionel Sambuc TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer),
4659f4a2713aSLionel Sambuc *this);
4660f4a2713aSLionel Sambuc AddParenthesizedArrayInitStep(DestType);
4661f4a2713aSLionel Sambuc } else if (DestAT->getElementType()->isCharType())
4662f4a2713aSLionel Sambuc SetFailed(FK_ArrayNeedsInitListOrStringLiteral);
4663f4a2713aSLionel Sambuc else if (IsWideCharCompatible(DestAT->getElementType(), Context))
4664f4a2713aSLionel Sambuc SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral);
4665f4a2713aSLionel Sambuc else
4666f4a2713aSLionel Sambuc SetFailed(FK_ArrayNeedsInitList);
4667f4a2713aSLionel Sambuc
4668f4a2713aSLionel Sambuc return;
4669f4a2713aSLionel Sambuc }
4670f4a2713aSLionel Sambuc
4671f4a2713aSLionel Sambuc // Determine whether we should consider writeback conversions for
4672f4a2713aSLionel Sambuc // Objective-C ARC.
4673f4a2713aSLionel Sambuc bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount &&
4674f4a2713aSLionel Sambuc Entity.isParameterKind();
4675f4a2713aSLionel Sambuc
4676f4a2713aSLionel Sambuc // We're at the end of the line for C: it's either a write-back conversion
4677f4a2713aSLionel Sambuc // or it's a C assignment. There's no need to check anything else.
4678f4a2713aSLionel Sambuc if (!S.getLangOpts().CPlusPlus) {
4679f4a2713aSLionel Sambuc // If allowed, check whether this is an Objective-C writeback conversion.
4680f4a2713aSLionel Sambuc if (allowObjCWritebackConversion &&
4681f4a2713aSLionel Sambuc tryObjCWritebackConversion(S, *this, Entity, Initializer)) {
4682f4a2713aSLionel Sambuc return;
4683f4a2713aSLionel Sambuc }
4684f4a2713aSLionel Sambuc
4685f4a2713aSLionel Sambuc if (TryOCLSamplerInitialization(S, *this, DestType, Initializer))
4686f4a2713aSLionel Sambuc return;
4687f4a2713aSLionel Sambuc
4688f4a2713aSLionel Sambuc if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer))
4689f4a2713aSLionel Sambuc return;
4690f4a2713aSLionel Sambuc
4691f4a2713aSLionel Sambuc // Handle initialization in C
4692f4a2713aSLionel Sambuc AddCAssignmentStep(DestType);
4693f4a2713aSLionel Sambuc MaybeProduceObjCObject(S, *this, Entity);
4694f4a2713aSLionel Sambuc return;
4695f4a2713aSLionel Sambuc }
4696f4a2713aSLionel Sambuc
4697f4a2713aSLionel Sambuc assert(S.getLangOpts().CPlusPlus);
4698f4a2713aSLionel Sambuc
4699f4a2713aSLionel Sambuc // - If the destination type is a (possibly cv-qualified) class type:
4700f4a2713aSLionel Sambuc if (DestType->isRecordType()) {
4701f4a2713aSLionel Sambuc // - If the initialization is direct-initialization, or if it is
4702f4a2713aSLionel Sambuc // copy-initialization where the cv-unqualified version of the
4703f4a2713aSLionel Sambuc // source type is the same class as, or a derived class of, the
4704f4a2713aSLionel Sambuc // class of the destination, constructors are considered. [...]
4705f4a2713aSLionel Sambuc if (Kind.getKind() == InitializationKind::IK_Direct ||
4706f4a2713aSLionel Sambuc (Kind.getKind() == InitializationKind::IK_Copy &&
4707f4a2713aSLionel Sambuc (Context.hasSameUnqualifiedType(SourceType, DestType) ||
4708f4a2713aSLionel Sambuc S.IsDerivedFrom(SourceType, DestType))))
4709f4a2713aSLionel Sambuc TryConstructorInitialization(S, Entity, Kind, Args,
4710*0a6a1f1dSLionel Sambuc DestType, *this);
4711f4a2713aSLionel Sambuc // - Otherwise (i.e., for the remaining copy-initialization cases),
4712f4a2713aSLionel Sambuc // user-defined conversion sequences that can convert from the source
4713f4a2713aSLionel Sambuc // type to the destination type or (when a conversion function is
4714f4a2713aSLionel Sambuc // used) to a derived class thereof are enumerated as described in
4715f4a2713aSLionel Sambuc // 13.3.1.4, and the best one is chosen through overload resolution
4716f4a2713aSLionel Sambuc // (13.3).
4717f4a2713aSLionel Sambuc else
4718*0a6a1f1dSLionel Sambuc TryUserDefinedConversion(S, DestType, Kind, Initializer, *this,
4719f4a2713aSLionel Sambuc TopLevelOfInitList);
4720f4a2713aSLionel Sambuc return;
4721f4a2713aSLionel Sambuc }
4722f4a2713aSLionel Sambuc
4723f4a2713aSLionel Sambuc if (Args.size() > 1) {
4724f4a2713aSLionel Sambuc SetFailed(FK_TooManyInitsForScalar);
4725f4a2713aSLionel Sambuc return;
4726f4a2713aSLionel Sambuc }
4727f4a2713aSLionel Sambuc assert(Args.size() == 1 && "Zero-argument case handled above");
4728f4a2713aSLionel Sambuc
4729f4a2713aSLionel Sambuc // - Otherwise, if the source type is a (possibly cv-qualified) class
4730f4a2713aSLionel Sambuc // type, conversion functions are considered.
4731f4a2713aSLionel Sambuc if (!SourceType.isNull() && SourceType->isRecordType()) {
4732*0a6a1f1dSLionel Sambuc // For a conversion to _Atomic(T) from either T or a class type derived
4733*0a6a1f1dSLionel Sambuc // from T, initialize the T object then convert to _Atomic type.
4734*0a6a1f1dSLionel Sambuc bool NeedAtomicConversion = false;
4735*0a6a1f1dSLionel Sambuc if (const AtomicType *Atomic = DestType->getAs<AtomicType>()) {
4736*0a6a1f1dSLionel Sambuc if (Context.hasSameUnqualifiedType(SourceType, Atomic->getValueType()) ||
4737*0a6a1f1dSLionel Sambuc S.IsDerivedFrom(SourceType, Atomic->getValueType())) {
4738*0a6a1f1dSLionel Sambuc DestType = Atomic->getValueType();
4739*0a6a1f1dSLionel Sambuc NeedAtomicConversion = true;
4740*0a6a1f1dSLionel Sambuc }
4741*0a6a1f1dSLionel Sambuc }
4742*0a6a1f1dSLionel Sambuc
4743*0a6a1f1dSLionel Sambuc TryUserDefinedConversion(S, DestType, Kind, Initializer, *this,
4744f4a2713aSLionel Sambuc TopLevelOfInitList);
4745f4a2713aSLionel Sambuc MaybeProduceObjCObject(S, *this, Entity);
4746*0a6a1f1dSLionel Sambuc if (!Failed() && NeedAtomicConversion)
4747*0a6a1f1dSLionel Sambuc AddAtomicConversionStep(Entity.getType());
4748f4a2713aSLionel Sambuc return;
4749f4a2713aSLionel Sambuc }
4750f4a2713aSLionel Sambuc
4751f4a2713aSLionel Sambuc // - Otherwise, the initial value of the object being initialized is the
4752f4a2713aSLionel Sambuc // (possibly converted) value of the initializer expression. Standard
4753f4a2713aSLionel Sambuc // conversions (Clause 4) will be used, if necessary, to convert the
4754f4a2713aSLionel Sambuc // initializer expression to the cv-unqualified version of the
4755f4a2713aSLionel Sambuc // destination type; no user-defined conversions are considered.
4756f4a2713aSLionel Sambuc
4757f4a2713aSLionel Sambuc ImplicitConversionSequence ICS
4758*0a6a1f1dSLionel Sambuc = S.TryImplicitConversion(Initializer, DestType,
4759f4a2713aSLionel Sambuc /*SuppressUserConversions*/true,
4760f4a2713aSLionel Sambuc /*AllowExplicitConversions*/ false,
4761f4a2713aSLionel Sambuc /*InOverloadResolution*/ false,
4762f4a2713aSLionel Sambuc /*CStyle=*/Kind.isCStyleOrFunctionalCast(),
4763f4a2713aSLionel Sambuc allowObjCWritebackConversion);
4764f4a2713aSLionel Sambuc
4765f4a2713aSLionel Sambuc if (ICS.isStandard() &&
4766f4a2713aSLionel Sambuc ICS.Standard.Second == ICK_Writeback_Conversion) {
4767f4a2713aSLionel Sambuc // Objective-C ARC writeback conversion.
4768f4a2713aSLionel Sambuc
4769f4a2713aSLionel Sambuc // We should copy unless we're passing to an argument explicitly
4770f4a2713aSLionel Sambuc // marked 'out'.
4771f4a2713aSLionel Sambuc bool ShouldCopy = true;
4772f4a2713aSLionel Sambuc if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl()))
4773f4a2713aSLionel Sambuc ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out);
4774f4a2713aSLionel Sambuc
4775f4a2713aSLionel Sambuc // If there was an lvalue adjustment, add it as a separate conversion.
4776f4a2713aSLionel Sambuc if (ICS.Standard.First == ICK_Array_To_Pointer ||
4777f4a2713aSLionel Sambuc ICS.Standard.First == ICK_Lvalue_To_Rvalue) {
4778f4a2713aSLionel Sambuc ImplicitConversionSequence LvalueICS;
4779f4a2713aSLionel Sambuc LvalueICS.setStandard();
4780f4a2713aSLionel Sambuc LvalueICS.Standard.setAsIdentityConversion();
4781f4a2713aSLionel Sambuc LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0));
4782f4a2713aSLionel Sambuc LvalueICS.Standard.First = ICS.Standard.First;
4783f4a2713aSLionel Sambuc AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0));
4784f4a2713aSLionel Sambuc }
4785f4a2713aSLionel Sambuc
4786*0a6a1f1dSLionel Sambuc AddPassByIndirectCopyRestoreStep(DestType, ShouldCopy);
4787f4a2713aSLionel Sambuc } else if (ICS.isBad()) {
4788f4a2713aSLionel Sambuc DeclAccessPair dap;
4789f4a2713aSLionel Sambuc if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) {
4790f4a2713aSLionel Sambuc AddZeroInitializationStep(Entity.getType());
4791f4a2713aSLionel Sambuc } else if (Initializer->getType() == Context.OverloadTy &&
4792f4a2713aSLionel Sambuc !S.ResolveAddressOfOverloadedFunction(Initializer, DestType,
4793f4a2713aSLionel Sambuc false, dap))
4794f4a2713aSLionel Sambuc SetFailed(InitializationSequence::FK_AddressOfOverloadFailed);
4795f4a2713aSLionel Sambuc else
4796f4a2713aSLionel Sambuc SetFailed(InitializationSequence::FK_ConversionFailed);
4797f4a2713aSLionel Sambuc } else {
4798*0a6a1f1dSLionel Sambuc AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList);
4799f4a2713aSLionel Sambuc
4800f4a2713aSLionel Sambuc MaybeProduceObjCObject(S, *this, Entity);
4801f4a2713aSLionel Sambuc }
4802f4a2713aSLionel Sambuc }
4803f4a2713aSLionel Sambuc
~InitializationSequence()4804f4a2713aSLionel Sambuc InitializationSequence::~InitializationSequence() {
4805f4a2713aSLionel Sambuc for (SmallVectorImpl<Step>::iterator Step = Steps.begin(),
4806f4a2713aSLionel Sambuc StepEnd = Steps.end();
4807f4a2713aSLionel Sambuc Step != StepEnd; ++Step)
4808f4a2713aSLionel Sambuc Step->Destroy();
4809f4a2713aSLionel Sambuc }
4810f4a2713aSLionel Sambuc
4811f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
4812f4a2713aSLionel Sambuc // Perform initialization
4813f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
4814f4a2713aSLionel Sambuc static Sema::AssignmentAction
getAssignmentAction(const InitializedEntity & Entity,bool Diagnose=false)4815f4a2713aSLionel Sambuc getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) {
4816f4a2713aSLionel Sambuc switch(Entity.getKind()) {
4817f4a2713aSLionel Sambuc case InitializedEntity::EK_Variable:
4818f4a2713aSLionel Sambuc case InitializedEntity::EK_New:
4819f4a2713aSLionel Sambuc case InitializedEntity::EK_Exception:
4820f4a2713aSLionel Sambuc case InitializedEntity::EK_Base:
4821f4a2713aSLionel Sambuc case InitializedEntity::EK_Delegating:
4822f4a2713aSLionel Sambuc return Sema::AA_Initializing;
4823f4a2713aSLionel Sambuc
4824f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter:
4825f4a2713aSLionel Sambuc if (Entity.getDecl() &&
4826f4a2713aSLionel Sambuc isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext()))
4827f4a2713aSLionel Sambuc return Sema::AA_Sending;
4828f4a2713aSLionel Sambuc
4829f4a2713aSLionel Sambuc return Sema::AA_Passing;
4830f4a2713aSLionel Sambuc
4831f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter_CF_Audited:
4832f4a2713aSLionel Sambuc if (Entity.getDecl() &&
4833f4a2713aSLionel Sambuc isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext()))
4834f4a2713aSLionel Sambuc return Sema::AA_Sending;
4835f4a2713aSLionel Sambuc
4836f4a2713aSLionel Sambuc return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited;
4837f4a2713aSLionel Sambuc
4838f4a2713aSLionel Sambuc case InitializedEntity::EK_Result:
4839f4a2713aSLionel Sambuc return Sema::AA_Returning;
4840f4a2713aSLionel Sambuc
4841f4a2713aSLionel Sambuc case InitializedEntity::EK_Temporary:
4842f4a2713aSLionel Sambuc case InitializedEntity::EK_RelatedResult:
4843f4a2713aSLionel Sambuc // FIXME: Can we tell apart casting vs. converting?
4844f4a2713aSLionel Sambuc return Sema::AA_Casting;
4845f4a2713aSLionel Sambuc
4846f4a2713aSLionel Sambuc case InitializedEntity::EK_Member:
4847f4a2713aSLionel Sambuc case InitializedEntity::EK_ArrayElement:
4848f4a2713aSLionel Sambuc case InitializedEntity::EK_VectorElement:
4849f4a2713aSLionel Sambuc case InitializedEntity::EK_ComplexElement:
4850f4a2713aSLionel Sambuc case InitializedEntity::EK_BlockElement:
4851f4a2713aSLionel Sambuc case InitializedEntity::EK_LambdaCapture:
4852f4a2713aSLionel Sambuc case InitializedEntity::EK_CompoundLiteralInit:
4853f4a2713aSLionel Sambuc return Sema::AA_Initializing;
4854f4a2713aSLionel Sambuc }
4855f4a2713aSLionel Sambuc
4856f4a2713aSLionel Sambuc llvm_unreachable("Invalid EntityKind!");
4857f4a2713aSLionel Sambuc }
4858f4a2713aSLionel Sambuc
4859f4a2713aSLionel Sambuc /// \brief Whether we should bind a created object as a temporary when
4860f4a2713aSLionel Sambuc /// initializing the given entity.
shouldBindAsTemporary(const InitializedEntity & Entity)4861f4a2713aSLionel Sambuc static bool shouldBindAsTemporary(const InitializedEntity &Entity) {
4862f4a2713aSLionel Sambuc switch (Entity.getKind()) {
4863f4a2713aSLionel Sambuc case InitializedEntity::EK_ArrayElement:
4864f4a2713aSLionel Sambuc case InitializedEntity::EK_Member:
4865f4a2713aSLionel Sambuc case InitializedEntity::EK_Result:
4866f4a2713aSLionel Sambuc case InitializedEntity::EK_New:
4867f4a2713aSLionel Sambuc case InitializedEntity::EK_Variable:
4868f4a2713aSLionel Sambuc case InitializedEntity::EK_Base:
4869f4a2713aSLionel Sambuc case InitializedEntity::EK_Delegating:
4870f4a2713aSLionel Sambuc case InitializedEntity::EK_VectorElement:
4871f4a2713aSLionel Sambuc case InitializedEntity::EK_ComplexElement:
4872f4a2713aSLionel Sambuc case InitializedEntity::EK_Exception:
4873f4a2713aSLionel Sambuc case InitializedEntity::EK_BlockElement:
4874f4a2713aSLionel Sambuc case InitializedEntity::EK_LambdaCapture:
4875f4a2713aSLionel Sambuc case InitializedEntity::EK_CompoundLiteralInit:
4876f4a2713aSLionel Sambuc return false;
4877f4a2713aSLionel Sambuc
4878f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter:
4879f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter_CF_Audited:
4880f4a2713aSLionel Sambuc case InitializedEntity::EK_Temporary:
4881f4a2713aSLionel Sambuc case InitializedEntity::EK_RelatedResult:
4882f4a2713aSLionel Sambuc return true;
4883f4a2713aSLionel Sambuc }
4884f4a2713aSLionel Sambuc
4885f4a2713aSLionel Sambuc llvm_unreachable("missed an InitializedEntity kind?");
4886f4a2713aSLionel Sambuc }
4887f4a2713aSLionel Sambuc
4888f4a2713aSLionel Sambuc /// \brief Whether the given entity, when initialized with an object
4889f4a2713aSLionel Sambuc /// created for that initialization, requires destruction.
shouldDestroyTemporary(const InitializedEntity & Entity)4890f4a2713aSLionel Sambuc static bool shouldDestroyTemporary(const InitializedEntity &Entity) {
4891f4a2713aSLionel Sambuc switch (Entity.getKind()) {
4892f4a2713aSLionel Sambuc case InitializedEntity::EK_Result:
4893f4a2713aSLionel Sambuc case InitializedEntity::EK_New:
4894f4a2713aSLionel Sambuc case InitializedEntity::EK_Base:
4895f4a2713aSLionel Sambuc case InitializedEntity::EK_Delegating:
4896f4a2713aSLionel Sambuc case InitializedEntity::EK_VectorElement:
4897f4a2713aSLionel Sambuc case InitializedEntity::EK_ComplexElement:
4898f4a2713aSLionel Sambuc case InitializedEntity::EK_BlockElement:
4899f4a2713aSLionel Sambuc case InitializedEntity::EK_LambdaCapture:
4900f4a2713aSLionel Sambuc return false;
4901f4a2713aSLionel Sambuc
4902f4a2713aSLionel Sambuc case InitializedEntity::EK_Member:
4903f4a2713aSLionel Sambuc case InitializedEntity::EK_Variable:
4904f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter:
4905f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter_CF_Audited:
4906f4a2713aSLionel Sambuc case InitializedEntity::EK_Temporary:
4907f4a2713aSLionel Sambuc case InitializedEntity::EK_ArrayElement:
4908f4a2713aSLionel Sambuc case InitializedEntity::EK_Exception:
4909f4a2713aSLionel Sambuc case InitializedEntity::EK_CompoundLiteralInit:
4910f4a2713aSLionel Sambuc case InitializedEntity::EK_RelatedResult:
4911f4a2713aSLionel Sambuc return true;
4912f4a2713aSLionel Sambuc }
4913f4a2713aSLionel Sambuc
4914f4a2713aSLionel Sambuc llvm_unreachable("missed an InitializedEntity kind?");
4915f4a2713aSLionel Sambuc }
4916f4a2713aSLionel Sambuc
4917f4a2713aSLionel Sambuc /// \brief Look for copy and move constructors and constructor templates, for
4918f4a2713aSLionel Sambuc /// copying an object via direct-initialization (per C++11 [dcl.init]p16).
LookupCopyAndMoveConstructors(Sema & S,OverloadCandidateSet & CandidateSet,CXXRecordDecl * Class,Expr * CurInitExpr)4919f4a2713aSLionel Sambuc static void LookupCopyAndMoveConstructors(Sema &S,
4920f4a2713aSLionel Sambuc OverloadCandidateSet &CandidateSet,
4921f4a2713aSLionel Sambuc CXXRecordDecl *Class,
4922f4a2713aSLionel Sambuc Expr *CurInitExpr) {
4923f4a2713aSLionel Sambuc DeclContext::lookup_result R = S.LookupConstructors(Class);
4924f4a2713aSLionel Sambuc // The container holding the constructors can under certain conditions
4925f4a2713aSLionel Sambuc // be changed while iterating (e.g. because of deserialization).
4926f4a2713aSLionel Sambuc // To be safe we copy the lookup results to a new container.
4927f4a2713aSLionel Sambuc SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end());
4928f4a2713aSLionel Sambuc for (SmallVectorImpl<NamedDecl *>::iterator
4929f4a2713aSLionel Sambuc CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) {
4930f4a2713aSLionel Sambuc NamedDecl *D = *CI;
4931*0a6a1f1dSLionel Sambuc CXXConstructorDecl *Constructor = nullptr;
4932f4a2713aSLionel Sambuc
4933f4a2713aSLionel Sambuc if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) {
4934f4a2713aSLionel Sambuc // Handle copy/moveconstructors, only.
4935f4a2713aSLionel Sambuc if (!Constructor || Constructor->isInvalidDecl() ||
4936f4a2713aSLionel Sambuc !Constructor->isCopyOrMoveConstructor() ||
4937f4a2713aSLionel Sambuc !Constructor->isConvertingConstructor(/*AllowExplicit=*/true))
4938f4a2713aSLionel Sambuc continue;
4939f4a2713aSLionel Sambuc
4940f4a2713aSLionel Sambuc DeclAccessPair FoundDecl
4941f4a2713aSLionel Sambuc = DeclAccessPair::make(Constructor, Constructor->getAccess());
4942f4a2713aSLionel Sambuc S.AddOverloadCandidate(Constructor, FoundDecl,
4943f4a2713aSLionel Sambuc CurInitExpr, CandidateSet);
4944f4a2713aSLionel Sambuc continue;
4945f4a2713aSLionel Sambuc }
4946f4a2713aSLionel Sambuc
4947f4a2713aSLionel Sambuc // Handle constructor templates.
4948f4a2713aSLionel Sambuc FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D);
4949f4a2713aSLionel Sambuc if (ConstructorTmpl->isInvalidDecl())
4950f4a2713aSLionel Sambuc continue;
4951f4a2713aSLionel Sambuc
4952f4a2713aSLionel Sambuc Constructor = cast<CXXConstructorDecl>(
4953f4a2713aSLionel Sambuc ConstructorTmpl->getTemplatedDecl());
4954f4a2713aSLionel Sambuc if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true))
4955f4a2713aSLionel Sambuc continue;
4956f4a2713aSLionel Sambuc
4957f4a2713aSLionel Sambuc // FIXME: Do we need to limit this to copy-constructor-like
4958f4a2713aSLionel Sambuc // candidates?
4959f4a2713aSLionel Sambuc DeclAccessPair FoundDecl
4960f4a2713aSLionel Sambuc = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess());
4961*0a6a1f1dSLionel Sambuc S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, nullptr,
4962f4a2713aSLionel Sambuc CurInitExpr, CandidateSet, true);
4963f4a2713aSLionel Sambuc }
4964f4a2713aSLionel Sambuc }
4965f4a2713aSLionel Sambuc
4966f4a2713aSLionel Sambuc /// \brief Get the location at which initialization diagnostics should appear.
getInitializationLoc(const InitializedEntity & Entity,Expr * Initializer)4967f4a2713aSLionel Sambuc static SourceLocation getInitializationLoc(const InitializedEntity &Entity,
4968f4a2713aSLionel Sambuc Expr *Initializer) {
4969f4a2713aSLionel Sambuc switch (Entity.getKind()) {
4970f4a2713aSLionel Sambuc case InitializedEntity::EK_Result:
4971f4a2713aSLionel Sambuc return Entity.getReturnLoc();
4972f4a2713aSLionel Sambuc
4973f4a2713aSLionel Sambuc case InitializedEntity::EK_Exception:
4974f4a2713aSLionel Sambuc return Entity.getThrowLoc();
4975f4a2713aSLionel Sambuc
4976f4a2713aSLionel Sambuc case InitializedEntity::EK_Variable:
4977f4a2713aSLionel Sambuc return Entity.getDecl()->getLocation();
4978f4a2713aSLionel Sambuc
4979f4a2713aSLionel Sambuc case InitializedEntity::EK_LambdaCapture:
4980f4a2713aSLionel Sambuc return Entity.getCaptureLoc();
4981f4a2713aSLionel Sambuc
4982f4a2713aSLionel Sambuc case InitializedEntity::EK_ArrayElement:
4983f4a2713aSLionel Sambuc case InitializedEntity::EK_Member:
4984f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter:
4985f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter_CF_Audited:
4986f4a2713aSLionel Sambuc case InitializedEntity::EK_Temporary:
4987f4a2713aSLionel Sambuc case InitializedEntity::EK_New:
4988f4a2713aSLionel Sambuc case InitializedEntity::EK_Base:
4989f4a2713aSLionel Sambuc case InitializedEntity::EK_Delegating:
4990f4a2713aSLionel Sambuc case InitializedEntity::EK_VectorElement:
4991f4a2713aSLionel Sambuc case InitializedEntity::EK_ComplexElement:
4992f4a2713aSLionel Sambuc case InitializedEntity::EK_BlockElement:
4993f4a2713aSLionel Sambuc case InitializedEntity::EK_CompoundLiteralInit:
4994f4a2713aSLionel Sambuc case InitializedEntity::EK_RelatedResult:
4995f4a2713aSLionel Sambuc return Initializer->getLocStart();
4996f4a2713aSLionel Sambuc }
4997f4a2713aSLionel Sambuc llvm_unreachable("missed an InitializedEntity kind?");
4998f4a2713aSLionel Sambuc }
4999f4a2713aSLionel Sambuc
5000f4a2713aSLionel Sambuc /// \brief Make a (potentially elidable) temporary copy of the object
5001f4a2713aSLionel Sambuc /// provided by the given initializer by calling the appropriate copy
5002f4a2713aSLionel Sambuc /// constructor.
5003f4a2713aSLionel Sambuc ///
5004f4a2713aSLionel Sambuc /// \param S The Sema object used for type-checking.
5005f4a2713aSLionel Sambuc ///
5006f4a2713aSLionel Sambuc /// \param T The type of the temporary object, which must either be
5007f4a2713aSLionel Sambuc /// the type of the initializer expression or a superclass thereof.
5008f4a2713aSLionel Sambuc ///
5009f4a2713aSLionel Sambuc /// \param Entity The entity being initialized.
5010f4a2713aSLionel Sambuc ///
5011f4a2713aSLionel Sambuc /// \param CurInit The initializer expression.
5012f4a2713aSLionel Sambuc ///
5013f4a2713aSLionel Sambuc /// \param IsExtraneousCopy Whether this is an "extraneous" copy that
5014f4a2713aSLionel Sambuc /// is permitted in C++03 (but not C++0x) when binding a reference to
5015f4a2713aSLionel Sambuc /// an rvalue.
5016f4a2713aSLionel Sambuc ///
5017f4a2713aSLionel Sambuc /// \returns An expression that copies the initializer expression into
5018f4a2713aSLionel Sambuc /// a temporary object, or an error expression if a copy could not be
5019f4a2713aSLionel Sambuc /// created.
CopyObject(Sema & S,QualType T,const InitializedEntity & Entity,ExprResult CurInit,bool IsExtraneousCopy)5020f4a2713aSLionel Sambuc static ExprResult CopyObject(Sema &S,
5021f4a2713aSLionel Sambuc QualType T,
5022f4a2713aSLionel Sambuc const InitializedEntity &Entity,
5023f4a2713aSLionel Sambuc ExprResult CurInit,
5024f4a2713aSLionel Sambuc bool IsExtraneousCopy) {
5025f4a2713aSLionel Sambuc // Determine which class type we're copying to.
5026f4a2713aSLionel Sambuc Expr *CurInitExpr = (Expr *)CurInit.get();
5027*0a6a1f1dSLionel Sambuc CXXRecordDecl *Class = nullptr;
5028f4a2713aSLionel Sambuc if (const RecordType *Record = T->getAs<RecordType>())
5029f4a2713aSLionel Sambuc Class = cast<CXXRecordDecl>(Record->getDecl());
5030f4a2713aSLionel Sambuc if (!Class)
5031f4a2713aSLionel Sambuc return CurInit;
5032f4a2713aSLionel Sambuc
5033f4a2713aSLionel Sambuc // C++0x [class.copy]p32:
5034f4a2713aSLionel Sambuc // When certain criteria are met, an implementation is allowed to
5035f4a2713aSLionel Sambuc // omit the copy/move construction of a class object, even if the
5036f4a2713aSLionel Sambuc // copy/move constructor and/or destructor for the object have
5037f4a2713aSLionel Sambuc // side effects. [...]
5038f4a2713aSLionel Sambuc // - when a temporary class object that has not been bound to a
5039f4a2713aSLionel Sambuc // reference (12.2) would be copied/moved to a class object
5040f4a2713aSLionel Sambuc // with the same cv-unqualified type, the copy/move operation
5041f4a2713aSLionel Sambuc // can be omitted by constructing the temporary object
5042f4a2713aSLionel Sambuc // directly into the target of the omitted copy/move
5043f4a2713aSLionel Sambuc //
5044f4a2713aSLionel Sambuc // Note that the other three bullets are handled elsewhere. Copy
5045f4a2713aSLionel Sambuc // elision for return statements and throw expressions are handled as part
5046f4a2713aSLionel Sambuc // of constructor initialization, while copy elision for exception handlers
5047f4a2713aSLionel Sambuc // is handled by the run-time.
5048f4a2713aSLionel Sambuc bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class);
5049f4a2713aSLionel Sambuc SourceLocation Loc = getInitializationLoc(Entity, CurInit.get());
5050f4a2713aSLionel Sambuc
5051f4a2713aSLionel Sambuc // Make sure that the type we are copying is complete.
5052f4a2713aSLionel Sambuc if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete))
5053f4a2713aSLionel Sambuc return CurInit;
5054f4a2713aSLionel Sambuc
5055f4a2713aSLionel Sambuc // Perform overload resolution using the class's copy/move constructors.
5056f4a2713aSLionel Sambuc // Only consider constructors and constructor templates. Per
5057f4a2713aSLionel Sambuc // C++0x [dcl.init]p16, second bullet to class types, this initialization
5058f4a2713aSLionel Sambuc // is direct-initialization.
5059*0a6a1f1dSLionel Sambuc OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal);
5060f4a2713aSLionel Sambuc LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr);
5061f4a2713aSLionel Sambuc
5062f4a2713aSLionel Sambuc bool HadMultipleCandidates = (CandidateSet.size() > 1);
5063f4a2713aSLionel Sambuc
5064f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
5065f4a2713aSLionel Sambuc switch (CandidateSet.BestViableFunction(S, Loc, Best)) {
5066f4a2713aSLionel Sambuc case OR_Success:
5067f4a2713aSLionel Sambuc break;
5068f4a2713aSLionel Sambuc
5069f4a2713aSLionel Sambuc case OR_No_Viable_Function:
5070f4a2713aSLionel Sambuc S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext()
5071f4a2713aSLionel Sambuc ? diag::ext_rvalue_to_reference_temp_copy_no_viable
5072f4a2713aSLionel Sambuc : diag::err_temp_copy_no_viable)
5073f4a2713aSLionel Sambuc << (int)Entity.getKind() << CurInitExpr->getType()
5074f4a2713aSLionel Sambuc << CurInitExpr->getSourceRange();
5075f4a2713aSLionel Sambuc CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr);
5076f4a2713aSLionel Sambuc if (!IsExtraneousCopy || S.isSFINAEContext())
5077f4a2713aSLionel Sambuc return ExprError();
5078f4a2713aSLionel Sambuc return CurInit;
5079f4a2713aSLionel Sambuc
5080f4a2713aSLionel Sambuc case OR_Ambiguous:
5081f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_temp_copy_ambiguous)
5082f4a2713aSLionel Sambuc << (int)Entity.getKind() << CurInitExpr->getType()
5083f4a2713aSLionel Sambuc << CurInitExpr->getSourceRange();
5084f4a2713aSLionel Sambuc CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr);
5085f4a2713aSLionel Sambuc return ExprError();
5086f4a2713aSLionel Sambuc
5087f4a2713aSLionel Sambuc case OR_Deleted:
5088f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_temp_copy_deleted)
5089f4a2713aSLionel Sambuc << (int)Entity.getKind() << CurInitExpr->getType()
5090f4a2713aSLionel Sambuc << CurInitExpr->getSourceRange();
5091f4a2713aSLionel Sambuc S.NoteDeletedFunction(Best->Function);
5092f4a2713aSLionel Sambuc return ExprError();
5093f4a2713aSLionel Sambuc }
5094f4a2713aSLionel Sambuc
5095f4a2713aSLionel Sambuc CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function);
5096f4a2713aSLionel Sambuc SmallVector<Expr*, 8> ConstructorArgs;
5097*0a6a1f1dSLionel Sambuc CurInit.get(); // Ownership transferred into MultiExprArg, below.
5098f4a2713aSLionel Sambuc
5099f4a2713aSLionel Sambuc S.CheckConstructorAccess(Loc, Constructor, Entity,
5100f4a2713aSLionel Sambuc Best->FoundDecl.getAccess(), IsExtraneousCopy);
5101f4a2713aSLionel Sambuc
5102f4a2713aSLionel Sambuc if (IsExtraneousCopy) {
5103f4a2713aSLionel Sambuc // If this is a totally extraneous copy for C++03 reference
5104f4a2713aSLionel Sambuc // binding purposes, just return the original initialization
5105f4a2713aSLionel Sambuc // expression. We don't generate an (elided) copy operation here
5106f4a2713aSLionel Sambuc // because doing so would require us to pass down a flag to avoid
5107f4a2713aSLionel Sambuc // infinite recursion, where each step adds another extraneous,
5108f4a2713aSLionel Sambuc // elidable copy.
5109f4a2713aSLionel Sambuc
5110f4a2713aSLionel Sambuc // Instantiate the default arguments of any extra parameters in
5111f4a2713aSLionel Sambuc // the selected copy constructor, as if we were going to create a
5112f4a2713aSLionel Sambuc // proper call to the copy constructor.
5113f4a2713aSLionel Sambuc for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) {
5114f4a2713aSLionel Sambuc ParmVarDecl *Parm = Constructor->getParamDecl(I);
5115f4a2713aSLionel Sambuc if (S.RequireCompleteType(Loc, Parm->getType(),
5116f4a2713aSLionel Sambuc diag::err_call_incomplete_argument))
5117f4a2713aSLionel Sambuc break;
5118f4a2713aSLionel Sambuc
5119f4a2713aSLionel Sambuc // Build the default argument expression; we don't actually care
5120f4a2713aSLionel Sambuc // if this succeeds or not, because this routine will complain
5121f4a2713aSLionel Sambuc // if there was a problem.
5122f4a2713aSLionel Sambuc S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm);
5123f4a2713aSLionel Sambuc }
5124f4a2713aSLionel Sambuc
5125*0a6a1f1dSLionel Sambuc return CurInitExpr;
5126f4a2713aSLionel Sambuc }
5127f4a2713aSLionel Sambuc
5128f4a2713aSLionel Sambuc // Determine the arguments required to actually perform the
5129f4a2713aSLionel Sambuc // constructor call (we might have derived-to-base conversions, or
5130f4a2713aSLionel Sambuc // the copy constructor may have default arguments).
5131f4a2713aSLionel Sambuc if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs))
5132f4a2713aSLionel Sambuc return ExprError();
5133f4a2713aSLionel Sambuc
5134f4a2713aSLionel Sambuc // Actually perform the constructor call.
5135f4a2713aSLionel Sambuc CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable,
5136f4a2713aSLionel Sambuc ConstructorArgs,
5137f4a2713aSLionel Sambuc HadMultipleCandidates,
5138f4a2713aSLionel Sambuc /*ListInit*/ false,
5139*0a6a1f1dSLionel Sambuc /*StdInitListInit*/ false,
5140f4a2713aSLionel Sambuc /*ZeroInit*/ false,
5141f4a2713aSLionel Sambuc CXXConstructExpr::CK_Complete,
5142f4a2713aSLionel Sambuc SourceRange());
5143f4a2713aSLionel Sambuc
5144f4a2713aSLionel Sambuc // If we're supposed to bind temporaries, do so.
5145f4a2713aSLionel Sambuc if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity))
5146*0a6a1f1dSLionel Sambuc CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>());
5147f4a2713aSLionel Sambuc return CurInit;
5148f4a2713aSLionel Sambuc }
5149f4a2713aSLionel Sambuc
5150f4a2713aSLionel Sambuc /// \brief Check whether elidable copy construction for binding a reference to
5151f4a2713aSLionel Sambuc /// a temporary would have succeeded if we were building in C++98 mode, for
5152f4a2713aSLionel Sambuc /// -Wc++98-compat.
CheckCXX98CompatAccessibleCopy(Sema & S,const InitializedEntity & Entity,Expr * CurInitExpr)5153f4a2713aSLionel Sambuc static void CheckCXX98CompatAccessibleCopy(Sema &S,
5154f4a2713aSLionel Sambuc const InitializedEntity &Entity,
5155f4a2713aSLionel Sambuc Expr *CurInitExpr) {
5156f4a2713aSLionel Sambuc assert(S.getLangOpts().CPlusPlus11);
5157f4a2713aSLionel Sambuc
5158f4a2713aSLionel Sambuc const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>();
5159f4a2713aSLionel Sambuc if (!Record)
5160f4a2713aSLionel Sambuc return;
5161f4a2713aSLionel Sambuc
5162f4a2713aSLionel Sambuc SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr);
5163*0a6a1f1dSLionel Sambuc if (S.Diags.isIgnored(diag::warn_cxx98_compat_temp_copy, Loc))
5164f4a2713aSLionel Sambuc return;
5165f4a2713aSLionel Sambuc
5166f4a2713aSLionel Sambuc // Find constructors which would have been considered.
5167*0a6a1f1dSLionel Sambuc OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal);
5168f4a2713aSLionel Sambuc LookupCopyAndMoveConstructors(
5169f4a2713aSLionel Sambuc S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr);
5170f4a2713aSLionel Sambuc
5171f4a2713aSLionel Sambuc // Perform overload resolution.
5172f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
5173f4a2713aSLionel Sambuc OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best);
5174f4a2713aSLionel Sambuc
5175f4a2713aSLionel Sambuc PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy)
5176f4a2713aSLionel Sambuc << OR << (int)Entity.getKind() << CurInitExpr->getType()
5177f4a2713aSLionel Sambuc << CurInitExpr->getSourceRange();
5178f4a2713aSLionel Sambuc
5179f4a2713aSLionel Sambuc switch (OR) {
5180f4a2713aSLionel Sambuc case OR_Success:
5181f4a2713aSLionel Sambuc S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function),
5182f4a2713aSLionel Sambuc Entity, Best->FoundDecl.getAccess(), Diag);
5183f4a2713aSLionel Sambuc // FIXME: Check default arguments as far as that's possible.
5184f4a2713aSLionel Sambuc break;
5185f4a2713aSLionel Sambuc
5186f4a2713aSLionel Sambuc case OR_No_Viable_Function:
5187f4a2713aSLionel Sambuc S.Diag(Loc, Diag);
5188f4a2713aSLionel Sambuc CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr);
5189f4a2713aSLionel Sambuc break;
5190f4a2713aSLionel Sambuc
5191f4a2713aSLionel Sambuc case OR_Ambiguous:
5192f4a2713aSLionel Sambuc S.Diag(Loc, Diag);
5193f4a2713aSLionel Sambuc CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr);
5194f4a2713aSLionel Sambuc break;
5195f4a2713aSLionel Sambuc
5196f4a2713aSLionel Sambuc case OR_Deleted:
5197f4a2713aSLionel Sambuc S.Diag(Loc, Diag);
5198f4a2713aSLionel Sambuc S.NoteDeletedFunction(Best->Function);
5199f4a2713aSLionel Sambuc break;
5200f4a2713aSLionel Sambuc }
5201f4a2713aSLionel Sambuc }
5202f4a2713aSLionel Sambuc
PrintInitLocationNote(Sema & S,const InitializedEntity & Entity)5203f4a2713aSLionel Sambuc void InitializationSequence::PrintInitLocationNote(Sema &S,
5204f4a2713aSLionel Sambuc const InitializedEntity &Entity) {
5205f4a2713aSLionel Sambuc if (Entity.isParameterKind() && Entity.getDecl()) {
5206f4a2713aSLionel Sambuc if (Entity.getDecl()->getLocation().isInvalid())
5207f4a2713aSLionel Sambuc return;
5208f4a2713aSLionel Sambuc
5209f4a2713aSLionel Sambuc if (Entity.getDecl()->getDeclName())
5210f4a2713aSLionel Sambuc S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here)
5211f4a2713aSLionel Sambuc << Entity.getDecl()->getDeclName();
5212f4a2713aSLionel Sambuc else
5213f4a2713aSLionel Sambuc S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here);
5214f4a2713aSLionel Sambuc }
5215f4a2713aSLionel Sambuc else if (Entity.getKind() == InitializedEntity::EK_RelatedResult &&
5216f4a2713aSLionel Sambuc Entity.getMethodDecl())
5217f4a2713aSLionel Sambuc S.Diag(Entity.getMethodDecl()->getLocation(),
5218f4a2713aSLionel Sambuc diag::note_method_return_type_change)
5219f4a2713aSLionel Sambuc << Entity.getMethodDecl()->getDeclName();
5220f4a2713aSLionel Sambuc }
5221f4a2713aSLionel Sambuc
isReferenceBinding(const InitializationSequence::Step & s)5222f4a2713aSLionel Sambuc static bool isReferenceBinding(const InitializationSequence::Step &s) {
5223f4a2713aSLionel Sambuc return s.Kind == InitializationSequence::SK_BindReference ||
5224f4a2713aSLionel Sambuc s.Kind == InitializationSequence::SK_BindReferenceToTemporary;
5225f4a2713aSLionel Sambuc }
5226f4a2713aSLionel Sambuc
5227f4a2713aSLionel Sambuc /// Returns true if the parameters describe a constructor initialization of
5228f4a2713aSLionel Sambuc /// an explicit temporary object, e.g. "Point(x, y)".
isExplicitTemporary(const InitializedEntity & Entity,const InitializationKind & Kind,unsigned NumArgs)5229f4a2713aSLionel Sambuc static bool isExplicitTemporary(const InitializedEntity &Entity,
5230f4a2713aSLionel Sambuc const InitializationKind &Kind,
5231f4a2713aSLionel Sambuc unsigned NumArgs) {
5232f4a2713aSLionel Sambuc switch (Entity.getKind()) {
5233f4a2713aSLionel Sambuc case InitializedEntity::EK_Temporary:
5234f4a2713aSLionel Sambuc case InitializedEntity::EK_CompoundLiteralInit:
5235f4a2713aSLionel Sambuc case InitializedEntity::EK_RelatedResult:
5236f4a2713aSLionel Sambuc break;
5237f4a2713aSLionel Sambuc default:
5238f4a2713aSLionel Sambuc return false;
5239f4a2713aSLionel Sambuc }
5240f4a2713aSLionel Sambuc
5241f4a2713aSLionel Sambuc switch (Kind.getKind()) {
5242f4a2713aSLionel Sambuc case InitializationKind::IK_DirectList:
5243f4a2713aSLionel Sambuc return true;
5244f4a2713aSLionel Sambuc // FIXME: Hack to work around cast weirdness.
5245f4a2713aSLionel Sambuc case InitializationKind::IK_Direct:
5246f4a2713aSLionel Sambuc case InitializationKind::IK_Value:
5247f4a2713aSLionel Sambuc return NumArgs != 1;
5248f4a2713aSLionel Sambuc default:
5249f4a2713aSLionel Sambuc return false;
5250f4a2713aSLionel Sambuc }
5251f4a2713aSLionel Sambuc }
5252f4a2713aSLionel Sambuc
5253f4a2713aSLionel Sambuc static ExprResult
PerformConstructorInitialization(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,MultiExprArg Args,const InitializationSequence::Step & Step,bool & ConstructorInitRequiresZeroInit,bool IsListInitialization,bool IsStdInitListInitialization,SourceLocation LBraceLoc,SourceLocation RBraceLoc)5254f4a2713aSLionel Sambuc PerformConstructorInitialization(Sema &S,
5255f4a2713aSLionel Sambuc const InitializedEntity &Entity,
5256f4a2713aSLionel Sambuc const InitializationKind &Kind,
5257f4a2713aSLionel Sambuc MultiExprArg Args,
5258f4a2713aSLionel Sambuc const InitializationSequence::Step& Step,
5259f4a2713aSLionel Sambuc bool &ConstructorInitRequiresZeroInit,
5260f4a2713aSLionel Sambuc bool IsListInitialization,
5261*0a6a1f1dSLionel Sambuc bool IsStdInitListInitialization,
5262f4a2713aSLionel Sambuc SourceLocation LBraceLoc,
5263f4a2713aSLionel Sambuc SourceLocation RBraceLoc) {
5264f4a2713aSLionel Sambuc unsigned NumArgs = Args.size();
5265f4a2713aSLionel Sambuc CXXConstructorDecl *Constructor
5266f4a2713aSLionel Sambuc = cast<CXXConstructorDecl>(Step.Function.Function);
5267f4a2713aSLionel Sambuc bool HadMultipleCandidates = Step.Function.HadMultipleCandidates;
5268f4a2713aSLionel Sambuc
5269f4a2713aSLionel Sambuc // Build a call to the selected constructor.
5270f4a2713aSLionel Sambuc SmallVector<Expr*, 8> ConstructorArgs;
5271f4a2713aSLionel Sambuc SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid())
5272f4a2713aSLionel Sambuc ? Kind.getEqualLoc()
5273f4a2713aSLionel Sambuc : Kind.getLocation();
5274f4a2713aSLionel Sambuc
5275f4a2713aSLionel Sambuc if (Kind.getKind() == InitializationKind::IK_Default) {
5276f4a2713aSLionel Sambuc // Force even a trivial, implicit default constructor to be
5277f4a2713aSLionel Sambuc // semantically checked. We do this explicitly because we don't build
5278f4a2713aSLionel Sambuc // the definition for completely trivial constructors.
5279f4a2713aSLionel Sambuc assert(Constructor->getParent() && "No parent class for constructor.");
5280f4a2713aSLionel Sambuc if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() &&
5281f4a2713aSLionel Sambuc Constructor->isTrivial() && !Constructor->isUsed(false))
5282f4a2713aSLionel Sambuc S.DefineImplicitDefaultConstructor(Loc, Constructor);
5283f4a2713aSLionel Sambuc }
5284f4a2713aSLionel Sambuc
5285*0a6a1f1dSLionel Sambuc ExprResult CurInit((Expr *)nullptr);
5286f4a2713aSLionel Sambuc
5287f4a2713aSLionel Sambuc // C++ [over.match.copy]p1:
5288f4a2713aSLionel Sambuc // - When initializing a temporary to be bound to the first parameter
5289f4a2713aSLionel Sambuc // of a constructor that takes a reference to possibly cv-qualified
5290f4a2713aSLionel Sambuc // T as its first argument, called with a single argument in the
5291f4a2713aSLionel Sambuc // context of direct-initialization, explicit conversion functions
5292f4a2713aSLionel Sambuc // are also considered.
5293f4a2713aSLionel Sambuc bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() &&
5294f4a2713aSLionel Sambuc Args.size() == 1 &&
5295f4a2713aSLionel Sambuc Constructor->isCopyOrMoveConstructor();
5296f4a2713aSLionel Sambuc
5297f4a2713aSLionel Sambuc // Determine the arguments required to actually perform the constructor
5298f4a2713aSLionel Sambuc // call.
5299f4a2713aSLionel Sambuc if (S.CompleteConstructorCall(Constructor, Args,
5300f4a2713aSLionel Sambuc Loc, ConstructorArgs,
5301f4a2713aSLionel Sambuc AllowExplicitConv,
5302f4a2713aSLionel Sambuc IsListInitialization))
5303f4a2713aSLionel Sambuc return ExprError();
5304f4a2713aSLionel Sambuc
5305f4a2713aSLionel Sambuc
5306f4a2713aSLionel Sambuc if (isExplicitTemporary(Entity, Kind, NumArgs)) {
5307f4a2713aSLionel Sambuc // An explicitly-constructed temporary, e.g., X(1, 2).
5308f4a2713aSLionel Sambuc S.MarkFunctionReferenced(Loc, Constructor);
5309f4a2713aSLionel Sambuc if (S.DiagnoseUseOfDecl(Constructor, Loc))
5310f4a2713aSLionel Sambuc return ExprError();
5311f4a2713aSLionel Sambuc
5312f4a2713aSLionel Sambuc TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo();
5313f4a2713aSLionel Sambuc if (!TSInfo)
5314f4a2713aSLionel Sambuc TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc);
5315f4a2713aSLionel Sambuc SourceRange ParenOrBraceRange =
5316f4a2713aSLionel Sambuc (Kind.getKind() == InitializationKind::IK_DirectList)
5317f4a2713aSLionel Sambuc ? SourceRange(LBraceLoc, RBraceLoc)
5318f4a2713aSLionel Sambuc : Kind.getParenRange();
5319f4a2713aSLionel Sambuc
5320*0a6a1f1dSLionel Sambuc CurInit = new (S.Context) CXXTemporaryObjectExpr(
5321*0a6a1f1dSLionel Sambuc S.Context, Constructor, TSInfo, ConstructorArgs, ParenOrBraceRange,
5322*0a6a1f1dSLionel Sambuc HadMultipleCandidates, IsListInitialization,
5323*0a6a1f1dSLionel Sambuc IsStdInitListInitialization, ConstructorInitRequiresZeroInit);
5324f4a2713aSLionel Sambuc } else {
5325f4a2713aSLionel Sambuc CXXConstructExpr::ConstructionKind ConstructKind =
5326f4a2713aSLionel Sambuc CXXConstructExpr::CK_Complete;
5327f4a2713aSLionel Sambuc
5328f4a2713aSLionel Sambuc if (Entity.getKind() == InitializedEntity::EK_Base) {
5329f4a2713aSLionel Sambuc ConstructKind = Entity.getBaseSpecifier()->isVirtual() ?
5330f4a2713aSLionel Sambuc CXXConstructExpr::CK_VirtualBase :
5331f4a2713aSLionel Sambuc CXXConstructExpr::CK_NonVirtualBase;
5332f4a2713aSLionel Sambuc } else if (Entity.getKind() == InitializedEntity::EK_Delegating) {
5333f4a2713aSLionel Sambuc ConstructKind = CXXConstructExpr::CK_Delegating;
5334f4a2713aSLionel Sambuc }
5335f4a2713aSLionel Sambuc
5336*0a6a1f1dSLionel Sambuc // Only get the parenthesis or brace range if it is a list initialization or
5337*0a6a1f1dSLionel Sambuc // direct construction.
5338*0a6a1f1dSLionel Sambuc SourceRange ParenOrBraceRange;
5339*0a6a1f1dSLionel Sambuc if (IsListInitialization)
5340*0a6a1f1dSLionel Sambuc ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc);
5341*0a6a1f1dSLionel Sambuc else if (Kind.getKind() == InitializationKind::IK_Direct)
5342*0a6a1f1dSLionel Sambuc ParenOrBraceRange = Kind.getParenRange();
5343f4a2713aSLionel Sambuc
5344f4a2713aSLionel Sambuc // If the entity allows NRVO, mark the construction as elidable
5345f4a2713aSLionel Sambuc // unconditionally.
5346f4a2713aSLionel Sambuc if (Entity.allowsNRVO())
5347f4a2713aSLionel Sambuc CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
5348f4a2713aSLionel Sambuc Constructor, /*Elidable=*/true,
5349f4a2713aSLionel Sambuc ConstructorArgs,
5350f4a2713aSLionel Sambuc HadMultipleCandidates,
5351f4a2713aSLionel Sambuc IsListInitialization,
5352*0a6a1f1dSLionel Sambuc IsStdInitListInitialization,
5353f4a2713aSLionel Sambuc ConstructorInitRequiresZeroInit,
5354f4a2713aSLionel Sambuc ConstructKind,
5355*0a6a1f1dSLionel Sambuc ParenOrBraceRange);
5356f4a2713aSLionel Sambuc else
5357f4a2713aSLionel Sambuc CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(),
5358f4a2713aSLionel Sambuc Constructor,
5359f4a2713aSLionel Sambuc ConstructorArgs,
5360f4a2713aSLionel Sambuc HadMultipleCandidates,
5361f4a2713aSLionel Sambuc IsListInitialization,
5362*0a6a1f1dSLionel Sambuc IsStdInitListInitialization,
5363f4a2713aSLionel Sambuc ConstructorInitRequiresZeroInit,
5364f4a2713aSLionel Sambuc ConstructKind,
5365*0a6a1f1dSLionel Sambuc ParenOrBraceRange);
5366f4a2713aSLionel Sambuc }
5367f4a2713aSLionel Sambuc if (CurInit.isInvalid())
5368f4a2713aSLionel Sambuc return ExprError();
5369f4a2713aSLionel Sambuc
5370f4a2713aSLionel Sambuc // Only check access if all of that succeeded.
5371f4a2713aSLionel Sambuc S.CheckConstructorAccess(Loc, Constructor, Entity,
5372f4a2713aSLionel Sambuc Step.Function.FoundDecl.getAccess());
5373f4a2713aSLionel Sambuc if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc))
5374f4a2713aSLionel Sambuc return ExprError();
5375f4a2713aSLionel Sambuc
5376f4a2713aSLionel Sambuc if (shouldBindAsTemporary(Entity))
5377*0a6a1f1dSLionel Sambuc CurInit = S.MaybeBindToTemporary(CurInit.get());
5378f4a2713aSLionel Sambuc
5379f4a2713aSLionel Sambuc return CurInit;
5380f4a2713aSLionel Sambuc }
5381f4a2713aSLionel Sambuc
5382f4a2713aSLionel Sambuc /// Determine whether the specified InitializedEntity definitely has a lifetime
5383f4a2713aSLionel Sambuc /// longer than the current full-expression. Conservatively returns false if
5384f4a2713aSLionel Sambuc /// it's unclear.
5385f4a2713aSLionel Sambuc static bool
InitializedEntityOutlivesFullExpression(const InitializedEntity & Entity)5386f4a2713aSLionel Sambuc InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) {
5387f4a2713aSLionel Sambuc const InitializedEntity *Top = &Entity;
5388f4a2713aSLionel Sambuc while (Top->getParent())
5389f4a2713aSLionel Sambuc Top = Top->getParent();
5390f4a2713aSLionel Sambuc
5391f4a2713aSLionel Sambuc switch (Top->getKind()) {
5392f4a2713aSLionel Sambuc case InitializedEntity::EK_Variable:
5393f4a2713aSLionel Sambuc case InitializedEntity::EK_Result:
5394f4a2713aSLionel Sambuc case InitializedEntity::EK_Exception:
5395f4a2713aSLionel Sambuc case InitializedEntity::EK_Member:
5396f4a2713aSLionel Sambuc case InitializedEntity::EK_New:
5397f4a2713aSLionel Sambuc case InitializedEntity::EK_Base:
5398f4a2713aSLionel Sambuc case InitializedEntity::EK_Delegating:
5399f4a2713aSLionel Sambuc return true;
5400f4a2713aSLionel Sambuc
5401f4a2713aSLionel Sambuc case InitializedEntity::EK_ArrayElement:
5402f4a2713aSLionel Sambuc case InitializedEntity::EK_VectorElement:
5403f4a2713aSLionel Sambuc case InitializedEntity::EK_BlockElement:
5404f4a2713aSLionel Sambuc case InitializedEntity::EK_ComplexElement:
5405f4a2713aSLionel Sambuc // Could not determine what the full initialization is. Assume it might not
5406f4a2713aSLionel Sambuc // outlive the full-expression.
5407f4a2713aSLionel Sambuc return false;
5408f4a2713aSLionel Sambuc
5409f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter:
5410f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter_CF_Audited:
5411f4a2713aSLionel Sambuc case InitializedEntity::EK_Temporary:
5412f4a2713aSLionel Sambuc case InitializedEntity::EK_LambdaCapture:
5413f4a2713aSLionel Sambuc case InitializedEntity::EK_CompoundLiteralInit:
5414f4a2713aSLionel Sambuc case InitializedEntity::EK_RelatedResult:
5415f4a2713aSLionel Sambuc // The entity being initialized might not outlive the full-expression.
5416f4a2713aSLionel Sambuc return false;
5417f4a2713aSLionel Sambuc }
5418f4a2713aSLionel Sambuc
5419f4a2713aSLionel Sambuc llvm_unreachable("unknown entity kind");
5420f4a2713aSLionel Sambuc }
5421f4a2713aSLionel Sambuc
5422f4a2713aSLionel Sambuc /// Determine the declaration which an initialized entity ultimately refers to,
5423f4a2713aSLionel Sambuc /// for the purpose of lifetime-extending a temporary bound to a reference in
5424f4a2713aSLionel Sambuc /// the initialization of \p Entity.
getEntityForTemporaryLifetimeExtension(const InitializedEntity * Entity,const InitializedEntity * FallbackDecl=nullptr)5425*0a6a1f1dSLionel Sambuc static const InitializedEntity *getEntityForTemporaryLifetimeExtension(
5426*0a6a1f1dSLionel Sambuc const InitializedEntity *Entity,
5427*0a6a1f1dSLionel Sambuc const InitializedEntity *FallbackDecl = nullptr) {
5428f4a2713aSLionel Sambuc // C++11 [class.temporary]p5:
5429*0a6a1f1dSLionel Sambuc switch (Entity->getKind()) {
5430f4a2713aSLionel Sambuc case InitializedEntity::EK_Variable:
5431f4a2713aSLionel Sambuc // The temporary [...] persists for the lifetime of the reference
5432*0a6a1f1dSLionel Sambuc return Entity;
5433f4a2713aSLionel Sambuc
5434f4a2713aSLionel Sambuc case InitializedEntity::EK_Member:
5435f4a2713aSLionel Sambuc // For subobjects, we look at the complete object.
5436*0a6a1f1dSLionel Sambuc if (Entity->getParent())
5437*0a6a1f1dSLionel Sambuc return getEntityForTemporaryLifetimeExtension(Entity->getParent(),
5438*0a6a1f1dSLionel Sambuc Entity);
5439f4a2713aSLionel Sambuc
5440f4a2713aSLionel Sambuc // except:
5441f4a2713aSLionel Sambuc // -- A temporary bound to a reference member in a constructor's
5442f4a2713aSLionel Sambuc // ctor-initializer persists until the constructor exits.
5443*0a6a1f1dSLionel Sambuc return Entity;
5444f4a2713aSLionel Sambuc
5445f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter:
5446f4a2713aSLionel Sambuc case InitializedEntity::EK_Parameter_CF_Audited:
5447f4a2713aSLionel Sambuc // -- A temporary bound to a reference parameter in a function call
5448f4a2713aSLionel Sambuc // persists until the completion of the full-expression containing
5449f4a2713aSLionel Sambuc // the call.
5450f4a2713aSLionel Sambuc case InitializedEntity::EK_Result:
5451f4a2713aSLionel Sambuc // -- The lifetime of a temporary bound to the returned value in a
5452f4a2713aSLionel Sambuc // function return statement is not extended; the temporary is
5453f4a2713aSLionel Sambuc // destroyed at the end of the full-expression in the return statement.
5454f4a2713aSLionel Sambuc case InitializedEntity::EK_New:
5455f4a2713aSLionel Sambuc // -- A temporary bound to a reference in a new-initializer persists
5456f4a2713aSLionel Sambuc // until the completion of the full-expression containing the
5457f4a2713aSLionel Sambuc // new-initializer.
5458*0a6a1f1dSLionel Sambuc return nullptr;
5459f4a2713aSLionel Sambuc
5460f4a2713aSLionel Sambuc case InitializedEntity::EK_Temporary:
5461f4a2713aSLionel Sambuc case InitializedEntity::EK_CompoundLiteralInit:
5462f4a2713aSLionel Sambuc case InitializedEntity::EK_RelatedResult:
5463f4a2713aSLionel Sambuc // We don't yet know the storage duration of the surrounding temporary.
5464f4a2713aSLionel Sambuc // Assume it's got full-expression duration for now, it will patch up our
5465f4a2713aSLionel Sambuc // storage duration if that's not correct.
5466*0a6a1f1dSLionel Sambuc return nullptr;
5467f4a2713aSLionel Sambuc
5468f4a2713aSLionel Sambuc case InitializedEntity::EK_ArrayElement:
5469f4a2713aSLionel Sambuc // For subobjects, we look at the complete object.
5470*0a6a1f1dSLionel Sambuc return getEntityForTemporaryLifetimeExtension(Entity->getParent(),
5471f4a2713aSLionel Sambuc FallbackDecl);
5472f4a2713aSLionel Sambuc
5473f4a2713aSLionel Sambuc case InitializedEntity::EK_Base:
5474f4a2713aSLionel Sambuc case InitializedEntity::EK_Delegating:
5475f4a2713aSLionel Sambuc // We can reach this case for aggregate initialization in a constructor:
5476f4a2713aSLionel Sambuc // struct A { int &&r; };
5477f4a2713aSLionel Sambuc // struct B : A { B() : A{0} {} };
5478f4a2713aSLionel Sambuc // In this case, use the innermost field decl as the context.
5479f4a2713aSLionel Sambuc return FallbackDecl;
5480f4a2713aSLionel Sambuc
5481f4a2713aSLionel Sambuc case InitializedEntity::EK_BlockElement:
5482f4a2713aSLionel Sambuc case InitializedEntity::EK_LambdaCapture:
5483f4a2713aSLionel Sambuc case InitializedEntity::EK_Exception:
5484f4a2713aSLionel Sambuc case InitializedEntity::EK_VectorElement:
5485f4a2713aSLionel Sambuc case InitializedEntity::EK_ComplexElement:
5486*0a6a1f1dSLionel Sambuc return nullptr;
5487f4a2713aSLionel Sambuc }
5488f4a2713aSLionel Sambuc llvm_unreachable("unknown entity kind");
5489f4a2713aSLionel Sambuc }
5490f4a2713aSLionel Sambuc
5491*0a6a1f1dSLionel Sambuc static void performLifetimeExtension(Expr *Init,
5492*0a6a1f1dSLionel Sambuc const InitializedEntity *ExtendingEntity);
5493f4a2713aSLionel Sambuc
5494f4a2713aSLionel Sambuc /// Update a glvalue expression that is used as the initializer of a reference
5495f4a2713aSLionel Sambuc /// to note that its lifetime is extended.
5496f4a2713aSLionel Sambuc /// \return \c true if any temporary had its lifetime extended.
5497*0a6a1f1dSLionel Sambuc static bool
performReferenceExtension(Expr * Init,const InitializedEntity * ExtendingEntity)5498*0a6a1f1dSLionel Sambuc performReferenceExtension(Expr *Init,
5499*0a6a1f1dSLionel Sambuc const InitializedEntity *ExtendingEntity) {
5500*0a6a1f1dSLionel Sambuc // Walk past any constructs which we can lifetime-extend across.
5501*0a6a1f1dSLionel Sambuc Expr *Old;
5502*0a6a1f1dSLionel Sambuc do {
5503*0a6a1f1dSLionel Sambuc Old = Init;
5504*0a6a1f1dSLionel Sambuc
5505f4a2713aSLionel Sambuc if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) {
5506f4a2713aSLionel Sambuc if (ILE->getNumInits() == 1 && ILE->isGLValue()) {
5507f4a2713aSLionel Sambuc // This is just redundant braces around an initializer. Step over it.
5508f4a2713aSLionel Sambuc Init = ILE->getInit(0);
5509f4a2713aSLionel Sambuc }
5510f4a2713aSLionel Sambuc }
5511f4a2713aSLionel Sambuc
5512f4a2713aSLionel Sambuc // Step over any subobject adjustments; we may have a materialized
5513f4a2713aSLionel Sambuc // temporary inside them.
5514f4a2713aSLionel Sambuc SmallVector<const Expr *, 2> CommaLHSs;
5515f4a2713aSLionel Sambuc SmallVector<SubobjectAdjustment, 2> Adjustments;
5516f4a2713aSLionel Sambuc Init = const_cast<Expr *>(
5517f4a2713aSLionel Sambuc Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments));
5518f4a2713aSLionel Sambuc
5519f4a2713aSLionel Sambuc // Per current approach for DR1376, look through casts to reference type
5520f4a2713aSLionel Sambuc // when performing lifetime extension.
5521f4a2713aSLionel Sambuc if (CastExpr *CE = dyn_cast<CastExpr>(Init))
5522f4a2713aSLionel Sambuc if (CE->getSubExpr()->isGLValue())
5523f4a2713aSLionel Sambuc Init = CE->getSubExpr();
5524f4a2713aSLionel Sambuc
5525f4a2713aSLionel Sambuc // FIXME: Per DR1213, subscripting on an array temporary produces an xvalue.
5526f4a2713aSLionel Sambuc // It's unclear if binding a reference to that xvalue extends the array
5527f4a2713aSLionel Sambuc // temporary.
5528f4a2713aSLionel Sambuc } while (Init != Old);
5529f4a2713aSLionel Sambuc
5530f4a2713aSLionel Sambuc if (MaterializeTemporaryExpr *ME = dyn_cast<MaterializeTemporaryExpr>(Init)) {
5531f4a2713aSLionel Sambuc // Update the storage duration of the materialized temporary.
5532f4a2713aSLionel Sambuc // FIXME: Rebuild the expression instead of mutating it.
5533*0a6a1f1dSLionel Sambuc ME->setExtendingDecl(ExtendingEntity->getDecl(),
5534*0a6a1f1dSLionel Sambuc ExtendingEntity->allocateManglingNumber());
5535*0a6a1f1dSLionel Sambuc performLifetimeExtension(ME->GetTemporaryExpr(), ExtendingEntity);
5536f4a2713aSLionel Sambuc return true;
5537f4a2713aSLionel Sambuc }
5538f4a2713aSLionel Sambuc
5539f4a2713aSLionel Sambuc return false;
5540f4a2713aSLionel Sambuc }
5541f4a2713aSLionel Sambuc
5542f4a2713aSLionel Sambuc /// Update a prvalue expression that is going to be materialized as a
5543f4a2713aSLionel Sambuc /// lifetime-extended temporary.
performLifetimeExtension(Expr * Init,const InitializedEntity * ExtendingEntity)5544*0a6a1f1dSLionel Sambuc static void performLifetimeExtension(Expr *Init,
5545*0a6a1f1dSLionel Sambuc const InitializedEntity *ExtendingEntity) {
5546f4a2713aSLionel Sambuc // Dig out the expression which constructs the extended temporary.
5547f4a2713aSLionel Sambuc SmallVector<const Expr *, 2> CommaLHSs;
5548f4a2713aSLionel Sambuc SmallVector<SubobjectAdjustment, 2> Adjustments;
5549f4a2713aSLionel Sambuc Init = const_cast<Expr *>(
5550f4a2713aSLionel Sambuc Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments));
5551f4a2713aSLionel Sambuc
5552f4a2713aSLionel Sambuc if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init))
5553f4a2713aSLionel Sambuc Init = BTE->getSubExpr();
5554f4a2713aSLionel Sambuc
5555f4a2713aSLionel Sambuc if (CXXStdInitializerListExpr *ILE =
5556f4a2713aSLionel Sambuc dyn_cast<CXXStdInitializerListExpr>(Init)) {
5557*0a6a1f1dSLionel Sambuc performReferenceExtension(ILE->getSubExpr(), ExtendingEntity);
5558f4a2713aSLionel Sambuc return;
5559f4a2713aSLionel Sambuc }
5560f4a2713aSLionel Sambuc
5561f4a2713aSLionel Sambuc if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) {
5562f4a2713aSLionel Sambuc if (ILE->getType()->isArrayType()) {
5563f4a2713aSLionel Sambuc for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I)
5564*0a6a1f1dSLionel Sambuc performLifetimeExtension(ILE->getInit(I), ExtendingEntity);
5565f4a2713aSLionel Sambuc return;
5566f4a2713aSLionel Sambuc }
5567f4a2713aSLionel Sambuc
5568f4a2713aSLionel Sambuc if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) {
5569f4a2713aSLionel Sambuc assert(RD->isAggregate() && "aggregate init on non-aggregate");
5570f4a2713aSLionel Sambuc
5571f4a2713aSLionel Sambuc // If we lifetime-extend a braced initializer which is initializing an
5572f4a2713aSLionel Sambuc // aggregate, and that aggregate contains reference members which are
5573f4a2713aSLionel Sambuc // bound to temporaries, those temporaries are also lifetime-extended.
5574f4a2713aSLionel Sambuc if (RD->isUnion() && ILE->getInitializedFieldInUnion() &&
5575f4a2713aSLionel Sambuc ILE->getInitializedFieldInUnion()->getType()->isReferenceType())
5576*0a6a1f1dSLionel Sambuc performReferenceExtension(ILE->getInit(0), ExtendingEntity);
5577f4a2713aSLionel Sambuc else {
5578f4a2713aSLionel Sambuc unsigned Index = 0;
5579*0a6a1f1dSLionel Sambuc for (const auto *I : RD->fields()) {
5580f4a2713aSLionel Sambuc if (Index >= ILE->getNumInits())
5581f4a2713aSLionel Sambuc break;
5582f4a2713aSLionel Sambuc if (I->isUnnamedBitfield())
5583f4a2713aSLionel Sambuc continue;
5584f4a2713aSLionel Sambuc Expr *SubInit = ILE->getInit(Index);
5585f4a2713aSLionel Sambuc if (I->getType()->isReferenceType())
5586*0a6a1f1dSLionel Sambuc performReferenceExtension(SubInit, ExtendingEntity);
5587f4a2713aSLionel Sambuc else if (isa<InitListExpr>(SubInit) ||
5588f4a2713aSLionel Sambuc isa<CXXStdInitializerListExpr>(SubInit))
5589f4a2713aSLionel Sambuc // This may be either aggregate-initialization of a member or
5590f4a2713aSLionel Sambuc // initialization of a std::initializer_list object. Either way,
5591f4a2713aSLionel Sambuc // we should recursively lifetime-extend that initializer.
5592*0a6a1f1dSLionel Sambuc performLifetimeExtension(SubInit, ExtendingEntity);
5593f4a2713aSLionel Sambuc ++Index;
5594f4a2713aSLionel Sambuc }
5595f4a2713aSLionel Sambuc }
5596f4a2713aSLionel Sambuc }
5597f4a2713aSLionel Sambuc }
5598f4a2713aSLionel Sambuc }
5599f4a2713aSLionel Sambuc
warnOnLifetimeExtension(Sema & S,const InitializedEntity & Entity,const Expr * Init,bool IsInitializerList,const ValueDecl * ExtendingDecl)5600f4a2713aSLionel Sambuc static void warnOnLifetimeExtension(Sema &S, const InitializedEntity &Entity,
5601f4a2713aSLionel Sambuc const Expr *Init, bool IsInitializerList,
5602f4a2713aSLionel Sambuc const ValueDecl *ExtendingDecl) {
5603f4a2713aSLionel Sambuc // Warn if a field lifetime-extends a temporary.
5604f4a2713aSLionel Sambuc if (isa<FieldDecl>(ExtendingDecl)) {
5605f4a2713aSLionel Sambuc if (IsInitializerList) {
5606f4a2713aSLionel Sambuc S.Diag(Init->getExprLoc(), diag::warn_dangling_std_initializer_list)
5607f4a2713aSLionel Sambuc << /*at end of constructor*/true;
5608f4a2713aSLionel Sambuc return;
5609f4a2713aSLionel Sambuc }
5610f4a2713aSLionel Sambuc
5611f4a2713aSLionel Sambuc bool IsSubobjectMember = false;
5612f4a2713aSLionel Sambuc for (const InitializedEntity *Ent = Entity.getParent(); Ent;
5613f4a2713aSLionel Sambuc Ent = Ent->getParent()) {
5614f4a2713aSLionel Sambuc if (Ent->getKind() != InitializedEntity::EK_Base) {
5615f4a2713aSLionel Sambuc IsSubobjectMember = true;
5616f4a2713aSLionel Sambuc break;
5617f4a2713aSLionel Sambuc }
5618f4a2713aSLionel Sambuc }
5619f4a2713aSLionel Sambuc S.Diag(Init->getExprLoc(),
5620f4a2713aSLionel Sambuc diag::warn_bind_ref_member_to_temporary)
5621f4a2713aSLionel Sambuc << ExtendingDecl << Init->getSourceRange()
5622f4a2713aSLionel Sambuc << IsSubobjectMember << IsInitializerList;
5623f4a2713aSLionel Sambuc if (IsSubobjectMember)
5624f4a2713aSLionel Sambuc S.Diag(ExtendingDecl->getLocation(),
5625f4a2713aSLionel Sambuc diag::note_ref_subobject_of_member_declared_here);
5626f4a2713aSLionel Sambuc else
5627f4a2713aSLionel Sambuc S.Diag(ExtendingDecl->getLocation(),
5628f4a2713aSLionel Sambuc diag::note_ref_or_ptr_member_declared_here)
5629f4a2713aSLionel Sambuc << /*is pointer*/false;
5630f4a2713aSLionel Sambuc }
5631f4a2713aSLionel Sambuc }
5632f4a2713aSLionel Sambuc
5633f4a2713aSLionel Sambuc static void DiagnoseNarrowingInInitList(Sema &S,
5634f4a2713aSLionel Sambuc const ImplicitConversionSequence &ICS,
5635f4a2713aSLionel Sambuc QualType PreNarrowingType,
5636f4a2713aSLionel Sambuc QualType EntityType,
5637f4a2713aSLionel Sambuc const Expr *PostInit);
5638f4a2713aSLionel Sambuc
5639f4a2713aSLionel Sambuc ExprResult
Perform(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,MultiExprArg Args,QualType * ResultType)5640f4a2713aSLionel Sambuc InitializationSequence::Perform(Sema &S,
5641f4a2713aSLionel Sambuc const InitializedEntity &Entity,
5642f4a2713aSLionel Sambuc const InitializationKind &Kind,
5643f4a2713aSLionel Sambuc MultiExprArg Args,
5644f4a2713aSLionel Sambuc QualType *ResultType) {
5645f4a2713aSLionel Sambuc if (Failed()) {
5646f4a2713aSLionel Sambuc Diagnose(S, Entity, Kind, Args);
5647f4a2713aSLionel Sambuc return ExprError();
5648f4a2713aSLionel Sambuc }
5649f4a2713aSLionel Sambuc
5650f4a2713aSLionel Sambuc if (getKind() == DependentSequence) {
5651f4a2713aSLionel Sambuc // If the declaration is a non-dependent, incomplete array type
5652f4a2713aSLionel Sambuc // that has an initializer, then its type will be completed once
5653f4a2713aSLionel Sambuc // the initializer is instantiated.
5654f4a2713aSLionel Sambuc if (ResultType && !Entity.getType()->isDependentType() &&
5655f4a2713aSLionel Sambuc Args.size() == 1) {
5656f4a2713aSLionel Sambuc QualType DeclType = Entity.getType();
5657f4a2713aSLionel Sambuc if (const IncompleteArrayType *ArrayT
5658f4a2713aSLionel Sambuc = S.Context.getAsIncompleteArrayType(DeclType)) {
5659f4a2713aSLionel Sambuc // FIXME: We don't currently have the ability to accurately
5660f4a2713aSLionel Sambuc // compute the length of an initializer list without
5661f4a2713aSLionel Sambuc // performing full type-checking of the initializer list
5662f4a2713aSLionel Sambuc // (since we have to determine where braces are implicitly
5663f4a2713aSLionel Sambuc // introduced and such). So, we fall back to making the array
5664f4a2713aSLionel Sambuc // type a dependently-sized array type with no specified
5665f4a2713aSLionel Sambuc // bound.
5666f4a2713aSLionel Sambuc if (isa<InitListExpr>((Expr *)Args[0])) {
5667f4a2713aSLionel Sambuc SourceRange Brackets;
5668f4a2713aSLionel Sambuc
5669f4a2713aSLionel Sambuc // Scavange the location of the brackets from the entity, if we can.
5670f4a2713aSLionel Sambuc if (DeclaratorDecl *DD = Entity.getDecl()) {
5671f4a2713aSLionel Sambuc if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) {
5672f4a2713aSLionel Sambuc TypeLoc TL = TInfo->getTypeLoc();
5673f4a2713aSLionel Sambuc if (IncompleteArrayTypeLoc ArrayLoc =
5674f4a2713aSLionel Sambuc TL.getAs<IncompleteArrayTypeLoc>())
5675f4a2713aSLionel Sambuc Brackets = ArrayLoc.getBracketsRange();
5676f4a2713aSLionel Sambuc }
5677f4a2713aSLionel Sambuc }
5678f4a2713aSLionel Sambuc
5679f4a2713aSLionel Sambuc *ResultType
5680f4a2713aSLionel Sambuc = S.Context.getDependentSizedArrayType(ArrayT->getElementType(),
5681*0a6a1f1dSLionel Sambuc /*NumElts=*/nullptr,
5682f4a2713aSLionel Sambuc ArrayT->getSizeModifier(),
5683f4a2713aSLionel Sambuc ArrayT->getIndexTypeCVRQualifiers(),
5684f4a2713aSLionel Sambuc Brackets);
5685f4a2713aSLionel Sambuc }
5686f4a2713aSLionel Sambuc
5687f4a2713aSLionel Sambuc }
5688f4a2713aSLionel Sambuc }
5689f4a2713aSLionel Sambuc if (Kind.getKind() == InitializationKind::IK_Direct &&
5690f4a2713aSLionel Sambuc !Kind.isExplicitCast()) {
5691f4a2713aSLionel Sambuc // Rebuild the ParenListExpr.
5692f4a2713aSLionel Sambuc SourceRange ParenRange = Kind.getParenRange();
5693f4a2713aSLionel Sambuc return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(),
5694f4a2713aSLionel Sambuc Args);
5695f4a2713aSLionel Sambuc }
5696f4a2713aSLionel Sambuc assert(Kind.getKind() == InitializationKind::IK_Copy ||
5697f4a2713aSLionel Sambuc Kind.isExplicitCast() ||
5698f4a2713aSLionel Sambuc Kind.getKind() == InitializationKind::IK_DirectList);
5699f4a2713aSLionel Sambuc return ExprResult(Args[0]);
5700f4a2713aSLionel Sambuc }
5701f4a2713aSLionel Sambuc
5702f4a2713aSLionel Sambuc // No steps means no initialization.
5703f4a2713aSLionel Sambuc if (Steps.empty())
5704*0a6a1f1dSLionel Sambuc return ExprResult((Expr *)nullptr);
5705f4a2713aSLionel Sambuc
5706f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() &&
5707f4a2713aSLionel Sambuc Args.size() == 1 && isa<InitListExpr>(Args[0]) &&
5708f4a2713aSLionel Sambuc !Entity.isParameterKind()) {
5709f4a2713aSLionel Sambuc // Produce a C++98 compatibility warning if we are initializing a reference
5710f4a2713aSLionel Sambuc // from an initializer list. For parameters, we produce a better warning
5711f4a2713aSLionel Sambuc // elsewhere.
5712f4a2713aSLionel Sambuc Expr *Init = Args[0];
5713f4a2713aSLionel Sambuc S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init)
5714f4a2713aSLionel Sambuc << Init->getSourceRange();
5715f4a2713aSLionel Sambuc }
5716f4a2713aSLionel Sambuc
5717f4a2713aSLionel Sambuc // Diagnose cases where we initialize a pointer to an array temporary, and the
5718f4a2713aSLionel Sambuc // pointer obviously outlives the temporary.
5719f4a2713aSLionel Sambuc if (Args.size() == 1 && Args[0]->getType()->isArrayType() &&
5720f4a2713aSLionel Sambuc Entity.getType()->isPointerType() &&
5721f4a2713aSLionel Sambuc InitializedEntityOutlivesFullExpression(Entity)) {
5722f4a2713aSLionel Sambuc Expr *Init = Args[0];
5723f4a2713aSLionel Sambuc Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context);
5724f4a2713aSLionel Sambuc if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary)
5725f4a2713aSLionel Sambuc S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay)
5726f4a2713aSLionel Sambuc << Init->getSourceRange();
5727f4a2713aSLionel Sambuc }
5728f4a2713aSLionel Sambuc
5729f4a2713aSLionel Sambuc QualType DestType = Entity.getType().getNonReferenceType();
5730f4a2713aSLionel Sambuc // FIXME: Ugly hack around the fact that Entity.getType() is not
5731f4a2713aSLionel Sambuc // the same as Entity.getDecl()->getType() in cases involving type merging,
5732f4a2713aSLionel Sambuc // and we want latter when it makes sense.
5733f4a2713aSLionel Sambuc if (ResultType)
5734f4a2713aSLionel Sambuc *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() :
5735f4a2713aSLionel Sambuc Entity.getType();
5736f4a2713aSLionel Sambuc
5737*0a6a1f1dSLionel Sambuc ExprResult CurInit((Expr *)nullptr);
5738f4a2713aSLionel Sambuc
5739f4a2713aSLionel Sambuc // For initialization steps that start with a single initializer,
5740f4a2713aSLionel Sambuc // grab the only argument out the Args and place it into the "current"
5741f4a2713aSLionel Sambuc // initializer.
5742f4a2713aSLionel Sambuc switch (Steps.front().Kind) {
5743f4a2713aSLionel Sambuc case SK_ResolveAddressOfOverloadedFunction:
5744f4a2713aSLionel Sambuc case SK_CastDerivedToBaseRValue:
5745f4a2713aSLionel Sambuc case SK_CastDerivedToBaseXValue:
5746f4a2713aSLionel Sambuc case SK_CastDerivedToBaseLValue:
5747f4a2713aSLionel Sambuc case SK_BindReference:
5748f4a2713aSLionel Sambuc case SK_BindReferenceToTemporary:
5749f4a2713aSLionel Sambuc case SK_ExtraneousCopyToTemporary:
5750f4a2713aSLionel Sambuc case SK_UserConversion:
5751f4a2713aSLionel Sambuc case SK_QualificationConversionLValue:
5752f4a2713aSLionel Sambuc case SK_QualificationConversionXValue:
5753f4a2713aSLionel Sambuc case SK_QualificationConversionRValue:
5754*0a6a1f1dSLionel Sambuc case SK_AtomicConversion:
5755f4a2713aSLionel Sambuc case SK_LValueToRValue:
5756f4a2713aSLionel Sambuc case SK_ConversionSequence:
5757f4a2713aSLionel Sambuc case SK_ConversionSequenceNoNarrowing:
5758f4a2713aSLionel Sambuc case SK_ListInitialization:
5759f4a2713aSLionel Sambuc case SK_UnwrapInitList:
5760f4a2713aSLionel Sambuc case SK_RewrapInitList:
5761f4a2713aSLionel Sambuc case SK_CAssignment:
5762f4a2713aSLionel Sambuc case SK_StringInit:
5763f4a2713aSLionel Sambuc case SK_ObjCObjectConversion:
5764f4a2713aSLionel Sambuc case SK_ArrayInit:
5765f4a2713aSLionel Sambuc case SK_ParenthesizedArrayInit:
5766f4a2713aSLionel Sambuc case SK_PassByIndirectCopyRestore:
5767f4a2713aSLionel Sambuc case SK_PassByIndirectRestore:
5768f4a2713aSLionel Sambuc case SK_ProduceObjCObject:
5769f4a2713aSLionel Sambuc case SK_StdInitializerList:
5770f4a2713aSLionel Sambuc case SK_OCLSamplerInit:
5771f4a2713aSLionel Sambuc case SK_OCLZeroEvent: {
5772f4a2713aSLionel Sambuc assert(Args.size() == 1);
5773f4a2713aSLionel Sambuc CurInit = Args[0];
5774f4a2713aSLionel Sambuc if (!CurInit.get()) return ExprError();
5775f4a2713aSLionel Sambuc break;
5776f4a2713aSLionel Sambuc }
5777f4a2713aSLionel Sambuc
5778f4a2713aSLionel Sambuc case SK_ConstructorInitialization:
5779*0a6a1f1dSLionel Sambuc case SK_ConstructorInitializationFromList:
5780*0a6a1f1dSLionel Sambuc case SK_StdInitializerListConstructorCall:
5781f4a2713aSLionel Sambuc case SK_ZeroInitialization:
5782f4a2713aSLionel Sambuc break;
5783f4a2713aSLionel Sambuc }
5784f4a2713aSLionel Sambuc
5785f4a2713aSLionel Sambuc // Walk through the computed steps for the initialization sequence,
5786f4a2713aSLionel Sambuc // performing the specified conversions along the way.
5787f4a2713aSLionel Sambuc bool ConstructorInitRequiresZeroInit = false;
5788f4a2713aSLionel Sambuc for (step_iterator Step = step_begin(), StepEnd = step_end();
5789f4a2713aSLionel Sambuc Step != StepEnd; ++Step) {
5790f4a2713aSLionel Sambuc if (CurInit.isInvalid())
5791f4a2713aSLionel Sambuc return ExprError();
5792f4a2713aSLionel Sambuc
5793f4a2713aSLionel Sambuc QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType();
5794f4a2713aSLionel Sambuc
5795f4a2713aSLionel Sambuc switch (Step->Kind) {
5796f4a2713aSLionel Sambuc case SK_ResolveAddressOfOverloadedFunction:
5797f4a2713aSLionel Sambuc // Overload resolution determined which function invoke; update the
5798f4a2713aSLionel Sambuc // initializer to reflect that choice.
5799f4a2713aSLionel Sambuc S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl);
5800f4a2713aSLionel Sambuc if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation()))
5801f4a2713aSLionel Sambuc return ExprError();
5802f4a2713aSLionel Sambuc CurInit = S.FixOverloadedFunctionReference(CurInit,
5803f4a2713aSLionel Sambuc Step->Function.FoundDecl,
5804f4a2713aSLionel Sambuc Step->Function.Function);
5805f4a2713aSLionel Sambuc break;
5806f4a2713aSLionel Sambuc
5807f4a2713aSLionel Sambuc case SK_CastDerivedToBaseRValue:
5808f4a2713aSLionel Sambuc case SK_CastDerivedToBaseXValue:
5809f4a2713aSLionel Sambuc case SK_CastDerivedToBaseLValue: {
5810f4a2713aSLionel Sambuc // We have a derived-to-base cast that produces either an rvalue or an
5811f4a2713aSLionel Sambuc // lvalue. Perform that cast.
5812f4a2713aSLionel Sambuc
5813f4a2713aSLionel Sambuc CXXCastPath BasePath;
5814f4a2713aSLionel Sambuc
5815f4a2713aSLionel Sambuc // Casts to inaccessible base classes are allowed with C-style casts.
5816f4a2713aSLionel Sambuc bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast();
5817f4a2713aSLionel Sambuc if (S.CheckDerivedToBaseConversion(SourceType, Step->Type,
5818f4a2713aSLionel Sambuc CurInit.get()->getLocStart(),
5819f4a2713aSLionel Sambuc CurInit.get()->getSourceRange(),
5820f4a2713aSLionel Sambuc &BasePath, IgnoreBaseAccess))
5821f4a2713aSLionel Sambuc return ExprError();
5822f4a2713aSLionel Sambuc
5823f4a2713aSLionel Sambuc if (S.BasePathInvolvesVirtualBase(BasePath)) {
5824f4a2713aSLionel Sambuc QualType T = SourceType;
5825f4a2713aSLionel Sambuc if (const PointerType *Pointer = T->getAs<PointerType>())
5826f4a2713aSLionel Sambuc T = Pointer->getPointeeType();
5827f4a2713aSLionel Sambuc if (const RecordType *RecordTy = T->getAs<RecordType>())
5828f4a2713aSLionel Sambuc S.MarkVTableUsed(CurInit.get()->getLocStart(),
5829f4a2713aSLionel Sambuc cast<CXXRecordDecl>(RecordTy->getDecl()));
5830f4a2713aSLionel Sambuc }
5831f4a2713aSLionel Sambuc
5832f4a2713aSLionel Sambuc ExprValueKind VK =
5833f4a2713aSLionel Sambuc Step->Kind == SK_CastDerivedToBaseLValue ?
5834f4a2713aSLionel Sambuc VK_LValue :
5835f4a2713aSLionel Sambuc (Step->Kind == SK_CastDerivedToBaseXValue ?
5836f4a2713aSLionel Sambuc VK_XValue :
5837f4a2713aSLionel Sambuc VK_RValue);
5838*0a6a1f1dSLionel Sambuc CurInit =
5839*0a6a1f1dSLionel Sambuc ImplicitCastExpr::Create(S.Context, Step->Type, CK_DerivedToBase,
5840*0a6a1f1dSLionel Sambuc CurInit.get(), &BasePath, VK);
5841f4a2713aSLionel Sambuc break;
5842f4a2713aSLionel Sambuc }
5843f4a2713aSLionel Sambuc
5844f4a2713aSLionel Sambuc case SK_BindReference:
5845f4a2713aSLionel Sambuc // References cannot bind to bit-fields (C++ [dcl.init.ref]p5).
5846f4a2713aSLionel Sambuc if (CurInit.get()->refersToBitField()) {
5847f4a2713aSLionel Sambuc // We don't necessarily have an unambiguous source bit-field.
5848f4a2713aSLionel Sambuc FieldDecl *BitField = CurInit.get()->getSourceBitField();
5849f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield)
5850f4a2713aSLionel Sambuc << Entity.getType().isVolatileQualified()
5851f4a2713aSLionel Sambuc << (BitField ? BitField->getDeclName() : DeclarationName())
5852*0a6a1f1dSLionel Sambuc << (BitField != nullptr)
5853f4a2713aSLionel Sambuc << CurInit.get()->getSourceRange();
5854f4a2713aSLionel Sambuc if (BitField)
5855f4a2713aSLionel Sambuc S.Diag(BitField->getLocation(), diag::note_bitfield_decl);
5856f4a2713aSLionel Sambuc
5857f4a2713aSLionel Sambuc return ExprError();
5858f4a2713aSLionel Sambuc }
5859f4a2713aSLionel Sambuc
5860f4a2713aSLionel Sambuc if (CurInit.get()->refersToVectorElement()) {
5861f4a2713aSLionel Sambuc // References cannot bind to vector elements.
5862f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element)
5863f4a2713aSLionel Sambuc << Entity.getType().isVolatileQualified()
5864f4a2713aSLionel Sambuc << CurInit.get()->getSourceRange();
5865f4a2713aSLionel Sambuc PrintInitLocationNote(S, Entity);
5866f4a2713aSLionel Sambuc return ExprError();
5867f4a2713aSLionel Sambuc }
5868f4a2713aSLionel Sambuc
5869f4a2713aSLionel Sambuc // Reference binding does not have any corresponding ASTs.
5870f4a2713aSLionel Sambuc
5871f4a2713aSLionel Sambuc // Check exception specifications
5872f4a2713aSLionel Sambuc if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType))
5873f4a2713aSLionel Sambuc return ExprError();
5874f4a2713aSLionel Sambuc
5875f4a2713aSLionel Sambuc // Even though we didn't materialize a temporary, the binding may still
5876f4a2713aSLionel Sambuc // extend the lifetime of a temporary. This happens if we bind a reference
5877f4a2713aSLionel Sambuc // to the result of a cast to reference type.
5878*0a6a1f1dSLionel Sambuc if (const InitializedEntity *ExtendingEntity =
5879*0a6a1f1dSLionel Sambuc getEntityForTemporaryLifetimeExtension(&Entity))
5880*0a6a1f1dSLionel Sambuc if (performReferenceExtension(CurInit.get(), ExtendingEntity))
5881*0a6a1f1dSLionel Sambuc warnOnLifetimeExtension(S, Entity, CurInit.get(),
5882*0a6a1f1dSLionel Sambuc /*IsInitializerList=*/false,
5883*0a6a1f1dSLionel Sambuc ExtendingEntity->getDecl());
5884f4a2713aSLionel Sambuc
5885f4a2713aSLionel Sambuc break;
5886f4a2713aSLionel Sambuc
5887f4a2713aSLionel Sambuc case SK_BindReferenceToTemporary: {
5888f4a2713aSLionel Sambuc // Make sure the "temporary" is actually an rvalue.
5889f4a2713aSLionel Sambuc assert(CurInit.get()->isRValue() && "not a temporary");
5890f4a2713aSLionel Sambuc
5891f4a2713aSLionel Sambuc // Check exception specifications
5892f4a2713aSLionel Sambuc if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType))
5893f4a2713aSLionel Sambuc return ExprError();
5894f4a2713aSLionel Sambuc
5895f4a2713aSLionel Sambuc // Materialize the temporary into memory.
5896f4a2713aSLionel Sambuc MaterializeTemporaryExpr *MTE = new (S.Context) MaterializeTemporaryExpr(
5897f4a2713aSLionel Sambuc Entity.getType().getNonReferenceType(), CurInit.get(),
5898*0a6a1f1dSLionel Sambuc Entity.getType()->isLValueReferenceType());
5899*0a6a1f1dSLionel Sambuc
5900*0a6a1f1dSLionel Sambuc // Maybe lifetime-extend the temporary's subobjects to match the
5901*0a6a1f1dSLionel Sambuc // entity's lifetime.
5902*0a6a1f1dSLionel Sambuc if (const InitializedEntity *ExtendingEntity =
5903*0a6a1f1dSLionel Sambuc getEntityForTemporaryLifetimeExtension(&Entity))
5904*0a6a1f1dSLionel Sambuc if (performReferenceExtension(MTE, ExtendingEntity))
5905*0a6a1f1dSLionel Sambuc warnOnLifetimeExtension(S, Entity, CurInit.get(), /*IsInitializerList=*/false,
5906*0a6a1f1dSLionel Sambuc ExtendingEntity->getDecl());
5907f4a2713aSLionel Sambuc
5908f4a2713aSLionel Sambuc // If we're binding to an Objective-C object that has lifetime, we
5909f4a2713aSLionel Sambuc // need cleanups. Likewise if we're extending this temporary to automatic
5910f4a2713aSLionel Sambuc // storage duration -- we need to register its cleanup during the
5911f4a2713aSLionel Sambuc // full-expression's cleanups.
5912f4a2713aSLionel Sambuc if ((S.getLangOpts().ObjCAutoRefCount &&
5913f4a2713aSLionel Sambuc MTE->getType()->isObjCLifetimeType()) ||
5914f4a2713aSLionel Sambuc (MTE->getStorageDuration() == SD_Automatic &&
5915f4a2713aSLionel Sambuc MTE->getType().isDestructedType()))
5916f4a2713aSLionel Sambuc S.ExprNeedsCleanups = true;
5917f4a2713aSLionel Sambuc
5918*0a6a1f1dSLionel Sambuc CurInit = MTE;
5919f4a2713aSLionel Sambuc break;
5920f4a2713aSLionel Sambuc }
5921f4a2713aSLionel Sambuc
5922f4a2713aSLionel Sambuc case SK_ExtraneousCopyToTemporary:
5923f4a2713aSLionel Sambuc CurInit = CopyObject(S, Step->Type, Entity, CurInit,
5924f4a2713aSLionel Sambuc /*IsExtraneousCopy=*/true);
5925f4a2713aSLionel Sambuc break;
5926f4a2713aSLionel Sambuc
5927f4a2713aSLionel Sambuc case SK_UserConversion: {
5928f4a2713aSLionel Sambuc // We have a user-defined conversion that invokes either a constructor
5929f4a2713aSLionel Sambuc // or a conversion function.
5930f4a2713aSLionel Sambuc CastKind CastKind;
5931f4a2713aSLionel Sambuc bool IsCopy = false;
5932f4a2713aSLionel Sambuc FunctionDecl *Fn = Step->Function.Function;
5933f4a2713aSLionel Sambuc DeclAccessPair FoundFn = Step->Function.FoundDecl;
5934f4a2713aSLionel Sambuc bool HadMultipleCandidates = Step->Function.HadMultipleCandidates;
5935f4a2713aSLionel Sambuc bool CreatedObject = false;
5936f4a2713aSLionel Sambuc if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) {
5937f4a2713aSLionel Sambuc // Build a call to the selected constructor.
5938f4a2713aSLionel Sambuc SmallVector<Expr*, 8> ConstructorArgs;
5939f4a2713aSLionel Sambuc SourceLocation Loc = CurInit.get()->getLocStart();
5940*0a6a1f1dSLionel Sambuc CurInit.get(); // Ownership transferred into MultiExprArg, below.
5941f4a2713aSLionel Sambuc
5942f4a2713aSLionel Sambuc // Determine the arguments required to actually perform the constructor
5943f4a2713aSLionel Sambuc // call.
5944f4a2713aSLionel Sambuc Expr *Arg = CurInit.get();
5945f4a2713aSLionel Sambuc if (S.CompleteConstructorCall(Constructor,
5946f4a2713aSLionel Sambuc MultiExprArg(&Arg, 1),
5947f4a2713aSLionel Sambuc Loc, ConstructorArgs))
5948f4a2713aSLionel Sambuc return ExprError();
5949f4a2713aSLionel Sambuc
5950f4a2713aSLionel Sambuc // Build an expression that constructs a temporary.
5951f4a2713aSLionel Sambuc CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor,
5952f4a2713aSLionel Sambuc ConstructorArgs,
5953f4a2713aSLionel Sambuc HadMultipleCandidates,
5954f4a2713aSLionel Sambuc /*ListInit*/ false,
5955*0a6a1f1dSLionel Sambuc /*StdInitListInit*/ false,
5956f4a2713aSLionel Sambuc /*ZeroInit*/ false,
5957f4a2713aSLionel Sambuc CXXConstructExpr::CK_Complete,
5958f4a2713aSLionel Sambuc SourceRange());
5959f4a2713aSLionel Sambuc if (CurInit.isInvalid())
5960f4a2713aSLionel Sambuc return ExprError();
5961f4a2713aSLionel Sambuc
5962f4a2713aSLionel Sambuc S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity,
5963f4a2713aSLionel Sambuc FoundFn.getAccess());
5964f4a2713aSLionel Sambuc if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()))
5965f4a2713aSLionel Sambuc return ExprError();
5966f4a2713aSLionel Sambuc
5967f4a2713aSLionel Sambuc CastKind = CK_ConstructorConversion;
5968f4a2713aSLionel Sambuc QualType Class = S.Context.getTypeDeclType(Constructor->getParent());
5969f4a2713aSLionel Sambuc if (S.Context.hasSameUnqualifiedType(SourceType, Class) ||
5970f4a2713aSLionel Sambuc S.IsDerivedFrom(SourceType, Class))
5971f4a2713aSLionel Sambuc IsCopy = true;
5972f4a2713aSLionel Sambuc
5973f4a2713aSLionel Sambuc CreatedObject = true;
5974f4a2713aSLionel Sambuc } else {
5975f4a2713aSLionel Sambuc // Build a call to the conversion function.
5976f4a2713aSLionel Sambuc CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn);
5977*0a6a1f1dSLionel Sambuc S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr,
5978f4a2713aSLionel Sambuc FoundFn);
5979f4a2713aSLionel Sambuc if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation()))
5980f4a2713aSLionel Sambuc return ExprError();
5981f4a2713aSLionel Sambuc
5982f4a2713aSLionel Sambuc // FIXME: Should we move this initialization into a separate
5983f4a2713aSLionel Sambuc // derived-to-base conversion? I believe the answer is "no", because
5984f4a2713aSLionel Sambuc // we don't want to turn off access control here for c-style casts.
5985f4a2713aSLionel Sambuc ExprResult CurInitExprRes =
5986*0a6a1f1dSLionel Sambuc S.PerformObjectArgumentInitialization(CurInit.get(),
5987*0a6a1f1dSLionel Sambuc /*Qualifier=*/nullptr,
5988f4a2713aSLionel Sambuc FoundFn, Conversion);
5989f4a2713aSLionel Sambuc if(CurInitExprRes.isInvalid())
5990f4a2713aSLionel Sambuc return ExprError();
5991f4a2713aSLionel Sambuc CurInit = CurInitExprRes;
5992f4a2713aSLionel Sambuc
5993f4a2713aSLionel Sambuc // Build the actual call to the conversion function.
5994f4a2713aSLionel Sambuc CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion,
5995f4a2713aSLionel Sambuc HadMultipleCandidates);
5996f4a2713aSLionel Sambuc if (CurInit.isInvalid() || !CurInit.get())
5997f4a2713aSLionel Sambuc return ExprError();
5998f4a2713aSLionel Sambuc
5999f4a2713aSLionel Sambuc CastKind = CK_UserDefinedConversion;
6000f4a2713aSLionel Sambuc
6001*0a6a1f1dSLionel Sambuc CreatedObject = Conversion->getReturnType()->isRecordType();
6002f4a2713aSLionel Sambuc }
6003f4a2713aSLionel Sambuc
6004f4a2713aSLionel Sambuc bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back());
6005f4a2713aSLionel Sambuc bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity);
6006f4a2713aSLionel Sambuc
6007f4a2713aSLionel Sambuc if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) {
6008f4a2713aSLionel Sambuc QualType T = CurInit.get()->getType();
6009f4a2713aSLionel Sambuc if (const RecordType *Record = T->getAs<RecordType>()) {
6010f4a2713aSLionel Sambuc CXXDestructorDecl *Destructor
6011f4a2713aSLionel Sambuc = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl()));
6012f4a2713aSLionel Sambuc S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor,
6013f4a2713aSLionel Sambuc S.PDiag(diag::err_access_dtor_temp) << T);
6014f4a2713aSLionel Sambuc S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor);
6015f4a2713aSLionel Sambuc if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart()))
6016f4a2713aSLionel Sambuc return ExprError();
6017f4a2713aSLionel Sambuc }
6018f4a2713aSLionel Sambuc }
6019f4a2713aSLionel Sambuc
6020*0a6a1f1dSLionel Sambuc CurInit = ImplicitCastExpr::Create(S.Context, CurInit.get()->getType(),
6021*0a6a1f1dSLionel Sambuc CastKind, CurInit.get(), nullptr,
6022*0a6a1f1dSLionel Sambuc CurInit.get()->getValueKind());
6023f4a2713aSLionel Sambuc if (MaybeBindToTemp)
6024*0a6a1f1dSLionel Sambuc CurInit = S.MaybeBindToTemporary(CurInit.getAs<Expr>());
6025f4a2713aSLionel Sambuc if (RequiresCopy)
6026f4a2713aSLionel Sambuc CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity,
6027f4a2713aSLionel Sambuc CurInit, /*IsExtraneousCopy=*/false);
6028f4a2713aSLionel Sambuc break;
6029f4a2713aSLionel Sambuc }
6030f4a2713aSLionel Sambuc
6031f4a2713aSLionel Sambuc case SK_QualificationConversionLValue:
6032f4a2713aSLionel Sambuc case SK_QualificationConversionXValue:
6033f4a2713aSLionel Sambuc case SK_QualificationConversionRValue: {
6034f4a2713aSLionel Sambuc // Perform a qualification conversion; these can never go wrong.
6035f4a2713aSLionel Sambuc ExprValueKind VK =
6036f4a2713aSLionel Sambuc Step->Kind == SK_QualificationConversionLValue ?
6037f4a2713aSLionel Sambuc VK_LValue :
6038f4a2713aSLionel Sambuc (Step->Kind == SK_QualificationConversionXValue ?
6039f4a2713aSLionel Sambuc VK_XValue :
6040f4a2713aSLionel Sambuc VK_RValue);
6041*0a6a1f1dSLionel Sambuc CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type, CK_NoOp, VK);
6042*0a6a1f1dSLionel Sambuc break;
6043*0a6a1f1dSLionel Sambuc }
6044*0a6a1f1dSLionel Sambuc
6045*0a6a1f1dSLionel Sambuc case SK_AtomicConversion: {
6046*0a6a1f1dSLionel Sambuc assert(CurInit.get()->isRValue() && "cannot convert glvalue to atomic");
6047*0a6a1f1dSLionel Sambuc CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type,
6048*0a6a1f1dSLionel Sambuc CK_NonAtomicToAtomic, VK_RValue);
6049f4a2713aSLionel Sambuc break;
6050f4a2713aSLionel Sambuc }
6051f4a2713aSLionel Sambuc
6052f4a2713aSLionel Sambuc case SK_LValueToRValue: {
6053f4a2713aSLionel Sambuc assert(CurInit.get()->isGLValue() && "cannot load from a prvalue");
6054*0a6a1f1dSLionel Sambuc CurInit = ImplicitCastExpr::Create(S.Context, Step->Type,
6055*0a6a1f1dSLionel Sambuc CK_LValueToRValue, CurInit.get(),
6056*0a6a1f1dSLionel Sambuc /*BasePath=*/nullptr, VK_RValue);
6057f4a2713aSLionel Sambuc break;
6058f4a2713aSLionel Sambuc }
6059f4a2713aSLionel Sambuc
6060f4a2713aSLionel Sambuc case SK_ConversionSequence:
6061f4a2713aSLionel Sambuc case SK_ConversionSequenceNoNarrowing: {
6062f4a2713aSLionel Sambuc Sema::CheckedConversionKind CCK
6063f4a2713aSLionel Sambuc = Kind.isCStyleCast()? Sema::CCK_CStyleCast
6064f4a2713aSLionel Sambuc : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast
6065f4a2713aSLionel Sambuc : Kind.isExplicitCast()? Sema::CCK_OtherCast
6066f4a2713aSLionel Sambuc : Sema::CCK_ImplicitConversion;
6067f4a2713aSLionel Sambuc ExprResult CurInitExprRes =
6068f4a2713aSLionel Sambuc S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS,
6069f4a2713aSLionel Sambuc getAssignmentAction(Entity), CCK);
6070f4a2713aSLionel Sambuc if (CurInitExprRes.isInvalid())
6071f4a2713aSLionel Sambuc return ExprError();
6072f4a2713aSLionel Sambuc CurInit = CurInitExprRes;
6073f4a2713aSLionel Sambuc
6074f4a2713aSLionel Sambuc if (Step->Kind == SK_ConversionSequenceNoNarrowing &&
6075f4a2713aSLionel Sambuc S.getLangOpts().CPlusPlus && !CurInit.get()->isValueDependent())
6076f4a2713aSLionel Sambuc DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(),
6077f4a2713aSLionel Sambuc CurInit.get());
6078f4a2713aSLionel Sambuc break;
6079f4a2713aSLionel Sambuc }
6080f4a2713aSLionel Sambuc
6081f4a2713aSLionel Sambuc case SK_ListInitialization: {
6082f4a2713aSLionel Sambuc InitListExpr *InitList = cast<InitListExpr>(CurInit.get());
6083f4a2713aSLionel Sambuc // If we're not initializing the top-level entity, we need to create an
6084f4a2713aSLionel Sambuc // InitializeTemporary entity for our target type.
6085f4a2713aSLionel Sambuc QualType Ty = Step->Type;
6086f4a2713aSLionel Sambuc bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty);
6087f4a2713aSLionel Sambuc InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty);
6088f4a2713aSLionel Sambuc InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity;
6089f4a2713aSLionel Sambuc InitListChecker PerformInitList(S, InitEntity,
6090f4a2713aSLionel Sambuc InitList, Ty, /*VerifyOnly=*/false);
6091f4a2713aSLionel Sambuc if (PerformInitList.HadError())
6092f4a2713aSLionel Sambuc return ExprError();
6093f4a2713aSLionel Sambuc
6094f4a2713aSLionel Sambuc // Hack: We must update *ResultType if available in order to set the
6095f4a2713aSLionel Sambuc // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'.
6096f4a2713aSLionel Sambuc // Worst case: 'const int (&arref)[] = {1, 2, 3};'.
6097f4a2713aSLionel Sambuc if (ResultType &&
6098f4a2713aSLionel Sambuc ResultType->getNonReferenceType()->isIncompleteArrayType()) {
6099f4a2713aSLionel Sambuc if ((*ResultType)->isRValueReferenceType())
6100f4a2713aSLionel Sambuc Ty = S.Context.getRValueReferenceType(Ty);
6101f4a2713aSLionel Sambuc else if ((*ResultType)->isLValueReferenceType())
6102f4a2713aSLionel Sambuc Ty = S.Context.getLValueReferenceType(Ty,
6103f4a2713aSLionel Sambuc (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue());
6104f4a2713aSLionel Sambuc *ResultType = Ty;
6105f4a2713aSLionel Sambuc }
6106f4a2713aSLionel Sambuc
6107f4a2713aSLionel Sambuc InitListExpr *StructuredInitList =
6108f4a2713aSLionel Sambuc PerformInitList.getFullyStructuredList();
6109*0a6a1f1dSLionel Sambuc CurInit.get();
6110f4a2713aSLionel Sambuc CurInit = shouldBindAsTemporary(InitEntity)
6111f4a2713aSLionel Sambuc ? S.MaybeBindToTemporary(StructuredInitList)
6112*0a6a1f1dSLionel Sambuc : StructuredInitList;
6113f4a2713aSLionel Sambuc break;
6114f4a2713aSLionel Sambuc }
6115f4a2713aSLionel Sambuc
6116*0a6a1f1dSLionel Sambuc case SK_ConstructorInitializationFromList: {
6117f4a2713aSLionel Sambuc // When an initializer list is passed for a parameter of type "reference
6118f4a2713aSLionel Sambuc // to object", we don't get an EK_Temporary entity, but instead an
6119f4a2713aSLionel Sambuc // EK_Parameter entity with reference type.
6120f4a2713aSLionel Sambuc // FIXME: This is a hack. What we really should do is create a user
6121f4a2713aSLionel Sambuc // conversion step for this case, but this makes it considerably more
6122f4a2713aSLionel Sambuc // complicated. For now, this will do.
6123f4a2713aSLionel Sambuc InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(
6124f4a2713aSLionel Sambuc Entity.getType().getNonReferenceType());
6125f4a2713aSLionel Sambuc bool UseTemporary = Entity.getType()->isReferenceType();
6126f4a2713aSLionel Sambuc assert(Args.size() == 1 && "expected a single argument for list init");
6127f4a2713aSLionel Sambuc InitListExpr *InitList = cast<InitListExpr>(Args[0]);
6128f4a2713aSLionel Sambuc S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init)
6129f4a2713aSLionel Sambuc << InitList->getSourceRange();
6130f4a2713aSLionel Sambuc MultiExprArg Arg(InitList->getInits(), InitList->getNumInits());
6131f4a2713aSLionel Sambuc CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity :
6132f4a2713aSLionel Sambuc Entity,
6133f4a2713aSLionel Sambuc Kind, Arg, *Step,
6134f4a2713aSLionel Sambuc ConstructorInitRequiresZeroInit,
6135f4a2713aSLionel Sambuc /*IsListInitialization*/true,
6136*0a6a1f1dSLionel Sambuc /*IsStdInitListInit*/false,
6137f4a2713aSLionel Sambuc InitList->getLBraceLoc(),
6138f4a2713aSLionel Sambuc InitList->getRBraceLoc());
6139f4a2713aSLionel Sambuc break;
6140f4a2713aSLionel Sambuc }
6141f4a2713aSLionel Sambuc
6142f4a2713aSLionel Sambuc case SK_UnwrapInitList:
6143*0a6a1f1dSLionel Sambuc CurInit = cast<InitListExpr>(CurInit.get())->getInit(0);
6144f4a2713aSLionel Sambuc break;
6145f4a2713aSLionel Sambuc
6146f4a2713aSLionel Sambuc case SK_RewrapInitList: {
6147*0a6a1f1dSLionel Sambuc Expr *E = CurInit.get();
6148f4a2713aSLionel Sambuc InitListExpr *Syntactic = Step->WrappingSyntacticList;
6149f4a2713aSLionel Sambuc InitListExpr *ILE = new (S.Context) InitListExpr(S.Context,
6150f4a2713aSLionel Sambuc Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc());
6151f4a2713aSLionel Sambuc ILE->setSyntacticForm(Syntactic);
6152f4a2713aSLionel Sambuc ILE->setType(E->getType());
6153f4a2713aSLionel Sambuc ILE->setValueKind(E->getValueKind());
6154*0a6a1f1dSLionel Sambuc CurInit = ILE;
6155f4a2713aSLionel Sambuc break;
6156f4a2713aSLionel Sambuc }
6157f4a2713aSLionel Sambuc
6158*0a6a1f1dSLionel Sambuc case SK_ConstructorInitialization:
6159*0a6a1f1dSLionel Sambuc case SK_StdInitializerListConstructorCall: {
6160f4a2713aSLionel Sambuc // When an initializer list is passed for a parameter of type "reference
6161f4a2713aSLionel Sambuc // to object", we don't get an EK_Temporary entity, but instead an
6162f4a2713aSLionel Sambuc // EK_Parameter entity with reference type.
6163f4a2713aSLionel Sambuc // FIXME: This is a hack. What we really should do is create a user
6164f4a2713aSLionel Sambuc // conversion step for this case, but this makes it considerably more
6165f4a2713aSLionel Sambuc // complicated. For now, this will do.
6166f4a2713aSLionel Sambuc InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(
6167f4a2713aSLionel Sambuc Entity.getType().getNonReferenceType());
6168f4a2713aSLionel Sambuc bool UseTemporary = Entity.getType()->isReferenceType();
6169*0a6a1f1dSLionel Sambuc bool IsStdInitListInit =
6170*0a6a1f1dSLionel Sambuc Step->Kind == SK_StdInitializerListConstructorCall;
6171*0a6a1f1dSLionel Sambuc CurInit = PerformConstructorInitialization(
6172*0a6a1f1dSLionel Sambuc S, UseTemporary ? TempEntity : Entity, Kind, Args, *Step,
6173f4a2713aSLionel Sambuc ConstructorInitRequiresZeroInit,
6174*0a6a1f1dSLionel Sambuc /*IsListInitialization*/IsStdInitListInit,
6175*0a6a1f1dSLionel Sambuc /*IsStdInitListInitialization*/IsStdInitListInit,
6176f4a2713aSLionel Sambuc /*LBraceLoc*/SourceLocation(),
6177f4a2713aSLionel Sambuc /*RBraceLoc*/SourceLocation());
6178f4a2713aSLionel Sambuc break;
6179f4a2713aSLionel Sambuc }
6180f4a2713aSLionel Sambuc
6181f4a2713aSLionel Sambuc case SK_ZeroInitialization: {
6182f4a2713aSLionel Sambuc step_iterator NextStep = Step;
6183f4a2713aSLionel Sambuc ++NextStep;
6184f4a2713aSLionel Sambuc if (NextStep != StepEnd &&
6185f4a2713aSLionel Sambuc (NextStep->Kind == SK_ConstructorInitialization ||
6186*0a6a1f1dSLionel Sambuc NextStep->Kind == SK_ConstructorInitializationFromList)) {
6187f4a2713aSLionel Sambuc // The need for zero-initialization is recorded directly into
6188f4a2713aSLionel Sambuc // the call to the object's constructor within the next step.
6189f4a2713aSLionel Sambuc ConstructorInitRequiresZeroInit = true;
6190f4a2713aSLionel Sambuc } else if (Kind.getKind() == InitializationKind::IK_Value &&
6191f4a2713aSLionel Sambuc S.getLangOpts().CPlusPlus &&
6192f4a2713aSLionel Sambuc !Kind.isImplicitValueInit()) {
6193f4a2713aSLionel Sambuc TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo();
6194f4a2713aSLionel Sambuc if (!TSInfo)
6195f4a2713aSLionel Sambuc TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type,
6196f4a2713aSLionel Sambuc Kind.getRange().getBegin());
6197f4a2713aSLionel Sambuc
6198*0a6a1f1dSLionel Sambuc CurInit = new (S.Context) CXXScalarValueInitExpr(
6199*0a6a1f1dSLionel Sambuc TSInfo->getType().getNonLValueExprType(S.Context), TSInfo,
6200*0a6a1f1dSLionel Sambuc Kind.getRange().getEnd());
6201f4a2713aSLionel Sambuc } else {
6202*0a6a1f1dSLionel Sambuc CurInit = new (S.Context) ImplicitValueInitExpr(Step->Type);
6203f4a2713aSLionel Sambuc }
6204f4a2713aSLionel Sambuc break;
6205f4a2713aSLionel Sambuc }
6206f4a2713aSLionel Sambuc
6207f4a2713aSLionel Sambuc case SK_CAssignment: {
6208f4a2713aSLionel Sambuc QualType SourceType = CurInit.get()->getType();
6209f4a2713aSLionel Sambuc ExprResult Result = CurInit;
6210f4a2713aSLionel Sambuc Sema::AssignConvertType ConvTy =
6211f4a2713aSLionel Sambuc S.CheckSingleAssignmentConstraints(Step->Type, Result, true,
6212f4a2713aSLionel Sambuc Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited);
6213f4a2713aSLionel Sambuc if (Result.isInvalid())
6214f4a2713aSLionel Sambuc return ExprError();
6215f4a2713aSLionel Sambuc CurInit = Result;
6216f4a2713aSLionel Sambuc
6217f4a2713aSLionel Sambuc // If this is a call, allow conversion to a transparent union.
6218f4a2713aSLionel Sambuc ExprResult CurInitExprRes = CurInit;
6219f4a2713aSLionel Sambuc if (ConvTy != Sema::Compatible &&
6220f4a2713aSLionel Sambuc Entity.isParameterKind() &&
6221f4a2713aSLionel Sambuc S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes)
6222f4a2713aSLionel Sambuc == Sema::Compatible)
6223f4a2713aSLionel Sambuc ConvTy = Sema::Compatible;
6224f4a2713aSLionel Sambuc if (CurInitExprRes.isInvalid())
6225f4a2713aSLionel Sambuc return ExprError();
6226f4a2713aSLionel Sambuc CurInit = CurInitExprRes;
6227f4a2713aSLionel Sambuc
6228f4a2713aSLionel Sambuc bool Complained;
6229f4a2713aSLionel Sambuc if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(),
6230f4a2713aSLionel Sambuc Step->Type, SourceType,
6231f4a2713aSLionel Sambuc CurInit.get(),
6232f4a2713aSLionel Sambuc getAssignmentAction(Entity, true),
6233f4a2713aSLionel Sambuc &Complained)) {
6234f4a2713aSLionel Sambuc PrintInitLocationNote(S, Entity);
6235f4a2713aSLionel Sambuc return ExprError();
6236f4a2713aSLionel Sambuc } else if (Complained)
6237f4a2713aSLionel Sambuc PrintInitLocationNote(S, Entity);
6238f4a2713aSLionel Sambuc break;
6239f4a2713aSLionel Sambuc }
6240f4a2713aSLionel Sambuc
6241f4a2713aSLionel Sambuc case SK_StringInit: {
6242f4a2713aSLionel Sambuc QualType Ty = Step->Type;
6243f4a2713aSLionel Sambuc CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty,
6244f4a2713aSLionel Sambuc S.Context.getAsArrayType(Ty), S);
6245f4a2713aSLionel Sambuc break;
6246f4a2713aSLionel Sambuc }
6247f4a2713aSLionel Sambuc
6248f4a2713aSLionel Sambuc case SK_ObjCObjectConversion:
6249*0a6a1f1dSLionel Sambuc CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type,
6250f4a2713aSLionel Sambuc CK_ObjCObjectLValueCast,
6251f4a2713aSLionel Sambuc CurInit.get()->getValueKind());
6252f4a2713aSLionel Sambuc break;
6253f4a2713aSLionel Sambuc
6254f4a2713aSLionel Sambuc case SK_ArrayInit:
6255f4a2713aSLionel Sambuc // Okay: we checked everything before creating this step. Note that
6256f4a2713aSLionel Sambuc // this is a GNU extension.
6257f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::ext_array_init_copy)
6258f4a2713aSLionel Sambuc << Step->Type << CurInit.get()->getType()
6259f4a2713aSLionel Sambuc << CurInit.get()->getSourceRange();
6260f4a2713aSLionel Sambuc
6261f4a2713aSLionel Sambuc // If the destination type is an incomplete array type, update the
6262f4a2713aSLionel Sambuc // type accordingly.
6263f4a2713aSLionel Sambuc if (ResultType) {
6264f4a2713aSLionel Sambuc if (const IncompleteArrayType *IncompleteDest
6265f4a2713aSLionel Sambuc = S.Context.getAsIncompleteArrayType(Step->Type)) {
6266f4a2713aSLionel Sambuc if (const ConstantArrayType *ConstantSource
6267f4a2713aSLionel Sambuc = S.Context.getAsConstantArrayType(CurInit.get()->getType())) {
6268f4a2713aSLionel Sambuc *ResultType = S.Context.getConstantArrayType(
6269f4a2713aSLionel Sambuc IncompleteDest->getElementType(),
6270f4a2713aSLionel Sambuc ConstantSource->getSize(),
6271f4a2713aSLionel Sambuc ArrayType::Normal, 0);
6272f4a2713aSLionel Sambuc }
6273f4a2713aSLionel Sambuc }
6274f4a2713aSLionel Sambuc }
6275f4a2713aSLionel Sambuc break;
6276f4a2713aSLionel Sambuc
6277f4a2713aSLionel Sambuc case SK_ParenthesizedArrayInit:
6278f4a2713aSLionel Sambuc // Okay: we checked everything before creating this step. Note that
6279f4a2713aSLionel Sambuc // this is a GNU extension.
6280f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::ext_array_init_parens)
6281f4a2713aSLionel Sambuc << CurInit.get()->getSourceRange();
6282f4a2713aSLionel Sambuc break;
6283f4a2713aSLionel Sambuc
6284f4a2713aSLionel Sambuc case SK_PassByIndirectCopyRestore:
6285f4a2713aSLionel Sambuc case SK_PassByIndirectRestore:
6286f4a2713aSLionel Sambuc checkIndirectCopyRestoreSource(S, CurInit.get());
6287*0a6a1f1dSLionel Sambuc CurInit = new (S.Context) ObjCIndirectCopyRestoreExpr(
6288*0a6a1f1dSLionel Sambuc CurInit.get(), Step->Type,
6289*0a6a1f1dSLionel Sambuc Step->Kind == SK_PassByIndirectCopyRestore);
6290f4a2713aSLionel Sambuc break;
6291f4a2713aSLionel Sambuc
6292f4a2713aSLionel Sambuc case SK_ProduceObjCObject:
6293*0a6a1f1dSLionel Sambuc CurInit =
6294*0a6a1f1dSLionel Sambuc ImplicitCastExpr::Create(S.Context, Step->Type, CK_ARCProduceObject,
6295*0a6a1f1dSLionel Sambuc CurInit.get(), nullptr, VK_RValue);
6296f4a2713aSLionel Sambuc break;
6297f4a2713aSLionel Sambuc
6298f4a2713aSLionel Sambuc case SK_StdInitializerList: {
6299f4a2713aSLionel Sambuc S.Diag(CurInit.get()->getExprLoc(),
6300f4a2713aSLionel Sambuc diag::warn_cxx98_compat_initializer_list_init)
6301f4a2713aSLionel Sambuc << CurInit.get()->getSourceRange();
6302f4a2713aSLionel Sambuc
6303f4a2713aSLionel Sambuc // Materialize the temporary into memory.
6304f4a2713aSLionel Sambuc MaterializeTemporaryExpr *MTE = new (S.Context)
6305f4a2713aSLionel Sambuc MaterializeTemporaryExpr(CurInit.get()->getType(), CurInit.get(),
6306*0a6a1f1dSLionel Sambuc /*BoundToLvalueReference=*/false);
6307*0a6a1f1dSLionel Sambuc
6308*0a6a1f1dSLionel Sambuc // Maybe lifetime-extend the array temporary's subobjects to match the
6309*0a6a1f1dSLionel Sambuc // entity's lifetime.
6310*0a6a1f1dSLionel Sambuc if (const InitializedEntity *ExtendingEntity =
6311*0a6a1f1dSLionel Sambuc getEntityForTemporaryLifetimeExtension(&Entity))
6312*0a6a1f1dSLionel Sambuc if (performReferenceExtension(MTE, ExtendingEntity))
6313*0a6a1f1dSLionel Sambuc warnOnLifetimeExtension(S, Entity, CurInit.get(),
6314*0a6a1f1dSLionel Sambuc /*IsInitializerList=*/true,
6315*0a6a1f1dSLionel Sambuc ExtendingEntity->getDecl());
6316f4a2713aSLionel Sambuc
6317f4a2713aSLionel Sambuc // Wrap it in a construction of a std::initializer_list<T>.
6318*0a6a1f1dSLionel Sambuc CurInit = new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE);
6319f4a2713aSLionel Sambuc
6320f4a2713aSLionel Sambuc // Bind the result, in case the library has given initializer_list a
6321f4a2713aSLionel Sambuc // non-trivial destructor.
6322f4a2713aSLionel Sambuc if (shouldBindAsTemporary(Entity))
6323*0a6a1f1dSLionel Sambuc CurInit = S.MaybeBindToTemporary(CurInit.get());
6324f4a2713aSLionel Sambuc break;
6325f4a2713aSLionel Sambuc }
6326f4a2713aSLionel Sambuc
6327f4a2713aSLionel Sambuc case SK_OCLSamplerInit: {
6328f4a2713aSLionel Sambuc assert(Step->Type->isSamplerT() &&
6329*0a6a1f1dSLionel Sambuc "Sampler initialization on non-sampler type.");
6330f4a2713aSLionel Sambuc
6331f4a2713aSLionel Sambuc QualType SourceType = CurInit.get()->getType();
6332f4a2713aSLionel Sambuc
6333f4a2713aSLionel Sambuc if (Entity.isParameterKind()) {
6334f4a2713aSLionel Sambuc if (!SourceType->isSamplerT())
6335f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_sampler_argument_required)
6336f4a2713aSLionel Sambuc << SourceType;
6337f4a2713aSLionel Sambuc } else if (Entity.getKind() != InitializedEntity::EK_Variable) {
6338f4a2713aSLionel Sambuc llvm_unreachable("Invalid EntityKind!");
6339f4a2713aSLionel Sambuc }
6340f4a2713aSLionel Sambuc
6341f4a2713aSLionel Sambuc break;
6342f4a2713aSLionel Sambuc }
6343f4a2713aSLionel Sambuc case SK_OCLZeroEvent: {
6344f4a2713aSLionel Sambuc assert(Step->Type->isEventT() &&
6345*0a6a1f1dSLionel Sambuc "Event initialization on non-event type.");
6346f4a2713aSLionel Sambuc
6347*0a6a1f1dSLionel Sambuc CurInit = S.ImpCastExprToType(CurInit.get(), Step->Type,
6348f4a2713aSLionel Sambuc CK_ZeroToOCLEvent,
6349f4a2713aSLionel Sambuc CurInit.get()->getValueKind());
6350f4a2713aSLionel Sambuc break;
6351f4a2713aSLionel Sambuc }
6352f4a2713aSLionel Sambuc }
6353f4a2713aSLionel Sambuc }
6354f4a2713aSLionel Sambuc
6355f4a2713aSLionel Sambuc // Diagnose non-fatal problems with the completed initialization.
6356f4a2713aSLionel Sambuc if (Entity.getKind() == InitializedEntity::EK_Member &&
6357f4a2713aSLionel Sambuc cast<FieldDecl>(Entity.getDecl())->isBitField())
6358f4a2713aSLionel Sambuc S.CheckBitFieldInitialization(Kind.getLocation(),
6359f4a2713aSLionel Sambuc cast<FieldDecl>(Entity.getDecl()),
6360f4a2713aSLionel Sambuc CurInit.get());
6361f4a2713aSLionel Sambuc
6362f4a2713aSLionel Sambuc return CurInit;
6363f4a2713aSLionel Sambuc }
6364f4a2713aSLionel Sambuc
6365f4a2713aSLionel Sambuc /// Somewhere within T there is an uninitialized reference subobject.
6366f4a2713aSLionel Sambuc /// Dig it out and diagnose it.
DiagnoseUninitializedReference(Sema & S,SourceLocation Loc,QualType T)6367f4a2713aSLionel Sambuc static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc,
6368f4a2713aSLionel Sambuc QualType T) {
6369f4a2713aSLionel Sambuc if (T->isReferenceType()) {
6370f4a2713aSLionel Sambuc S.Diag(Loc, diag::err_reference_without_init)
6371f4a2713aSLionel Sambuc << T.getNonReferenceType();
6372f4a2713aSLionel Sambuc return true;
6373f4a2713aSLionel Sambuc }
6374f4a2713aSLionel Sambuc
6375f4a2713aSLionel Sambuc CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
6376f4a2713aSLionel Sambuc if (!RD || !RD->hasUninitializedReferenceMember())
6377f4a2713aSLionel Sambuc return false;
6378f4a2713aSLionel Sambuc
6379*0a6a1f1dSLionel Sambuc for (const auto *FI : RD->fields()) {
6380f4a2713aSLionel Sambuc if (FI->isUnnamedBitfield())
6381f4a2713aSLionel Sambuc continue;
6382f4a2713aSLionel Sambuc
6383f4a2713aSLionel Sambuc if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) {
6384f4a2713aSLionel Sambuc S.Diag(Loc, diag::note_value_initialization_here) << RD;
6385f4a2713aSLionel Sambuc return true;
6386f4a2713aSLionel Sambuc }
6387f4a2713aSLionel Sambuc }
6388f4a2713aSLionel Sambuc
6389*0a6a1f1dSLionel Sambuc for (const auto &BI : RD->bases()) {
6390*0a6a1f1dSLionel Sambuc if (DiagnoseUninitializedReference(S, BI.getLocStart(), BI.getType())) {
6391f4a2713aSLionel Sambuc S.Diag(Loc, diag::note_value_initialization_here) << RD;
6392f4a2713aSLionel Sambuc return true;
6393f4a2713aSLionel Sambuc }
6394f4a2713aSLionel Sambuc }
6395f4a2713aSLionel Sambuc
6396f4a2713aSLionel Sambuc return false;
6397f4a2713aSLionel Sambuc }
6398f4a2713aSLionel Sambuc
6399f4a2713aSLionel Sambuc
6400f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
6401f4a2713aSLionel Sambuc // Diagnose initialization failures
6402f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
6403f4a2713aSLionel Sambuc
6404f4a2713aSLionel Sambuc /// Emit notes associated with an initialization that failed due to a
6405f4a2713aSLionel Sambuc /// "simple" conversion failure.
emitBadConversionNotes(Sema & S,const InitializedEntity & entity,Expr * op)6406f4a2713aSLionel Sambuc static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity,
6407f4a2713aSLionel Sambuc Expr *op) {
6408f4a2713aSLionel Sambuc QualType destType = entity.getType();
6409f4a2713aSLionel Sambuc if (destType.getNonReferenceType()->isObjCObjectPointerType() &&
6410f4a2713aSLionel Sambuc op->getType()->isObjCObjectPointerType()) {
6411f4a2713aSLionel Sambuc
6412f4a2713aSLionel Sambuc // Emit a possible note about the conversion failing because the
6413f4a2713aSLionel Sambuc // operand is a message send with a related result type.
6414f4a2713aSLionel Sambuc S.EmitRelatedResultTypeNote(op);
6415f4a2713aSLionel Sambuc
6416f4a2713aSLionel Sambuc // Emit a possible note about a return failing because we're
6417f4a2713aSLionel Sambuc // expecting a related result type.
6418f4a2713aSLionel Sambuc if (entity.getKind() == InitializedEntity::EK_Result)
6419f4a2713aSLionel Sambuc S.EmitRelatedResultTypeNoteForReturn(destType);
6420f4a2713aSLionel Sambuc }
6421f4a2713aSLionel Sambuc }
6422f4a2713aSLionel Sambuc
diagnoseListInit(Sema & S,const InitializedEntity & Entity,InitListExpr * InitList)6423f4a2713aSLionel Sambuc static void diagnoseListInit(Sema &S, const InitializedEntity &Entity,
6424f4a2713aSLionel Sambuc InitListExpr *InitList) {
6425f4a2713aSLionel Sambuc QualType DestType = Entity.getType();
6426f4a2713aSLionel Sambuc
6427f4a2713aSLionel Sambuc QualType E;
6428f4a2713aSLionel Sambuc if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) {
6429f4a2713aSLionel Sambuc QualType ArrayType = S.Context.getConstantArrayType(
6430f4a2713aSLionel Sambuc E.withConst(),
6431f4a2713aSLionel Sambuc llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()),
6432f4a2713aSLionel Sambuc InitList->getNumInits()),
6433f4a2713aSLionel Sambuc clang::ArrayType::Normal, 0);
6434f4a2713aSLionel Sambuc InitializedEntity HiddenArray =
6435f4a2713aSLionel Sambuc InitializedEntity::InitializeTemporary(ArrayType);
6436f4a2713aSLionel Sambuc return diagnoseListInit(S, HiddenArray, InitList);
6437f4a2713aSLionel Sambuc }
6438f4a2713aSLionel Sambuc
6439*0a6a1f1dSLionel Sambuc if (DestType->isReferenceType()) {
6440*0a6a1f1dSLionel Sambuc // A list-initialization failure for a reference means that we tried to
6441*0a6a1f1dSLionel Sambuc // create a temporary of the inner type (per [dcl.init.list]p3.6) and the
6442*0a6a1f1dSLionel Sambuc // inner initialization failed.
6443*0a6a1f1dSLionel Sambuc QualType T = DestType->getAs<ReferenceType>()->getPointeeType();
6444*0a6a1f1dSLionel Sambuc diagnoseListInit(S, InitializedEntity::InitializeTemporary(T), InitList);
6445*0a6a1f1dSLionel Sambuc SourceLocation Loc = InitList->getLocStart();
6446*0a6a1f1dSLionel Sambuc if (auto *D = Entity.getDecl())
6447*0a6a1f1dSLionel Sambuc Loc = D->getLocation();
6448*0a6a1f1dSLionel Sambuc S.Diag(Loc, diag::note_in_reference_temporary_list_initializer) << T;
6449*0a6a1f1dSLionel Sambuc return;
6450*0a6a1f1dSLionel Sambuc }
6451*0a6a1f1dSLionel Sambuc
6452f4a2713aSLionel Sambuc InitListChecker DiagnoseInitList(S, Entity, InitList, DestType,
6453f4a2713aSLionel Sambuc /*VerifyOnly=*/false);
6454f4a2713aSLionel Sambuc assert(DiagnoseInitList.HadError() &&
6455f4a2713aSLionel Sambuc "Inconsistent init list check result.");
6456f4a2713aSLionel Sambuc }
6457f4a2713aSLionel Sambuc
6458*0a6a1f1dSLionel Sambuc /// Prints a fixit for adding a null initializer for |Entity|. Call this only
6459*0a6a1f1dSLionel Sambuc /// right after emitting a diagnostic.
maybeEmitZeroInitializationFixit(Sema & S,InitializationSequence & Sequence,const InitializedEntity & Entity)6460*0a6a1f1dSLionel Sambuc static void maybeEmitZeroInitializationFixit(Sema &S,
6461*0a6a1f1dSLionel Sambuc InitializationSequence &Sequence,
6462*0a6a1f1dSLionel Sambuc const InitializedEntity &Entity) {
6463*0a6a1f1dSLionel Sambuc if (Entity.getKind() != InitializedEntity::EK_Variable)
6464*0a6a1f1dSLionel Sambuc return;
6465*0a6a1f1dSLionel Sambuc
6466*0a6a1f1dSLionel Sambuc VarDecl *VD = cast<VarDecl>(Entity.getDecl());
6467*0a6a1f1dSLionel Sambuc if (VD->getInit() || VD->getLocEnd().isMacroID())
6468*0a6a1f1dSLionel Sambuc return;
6469*0a6a1f1dSLionel Sambuc
6470*0a6a1f1dSLionel Sambuc QualType VariableTy = VD->getType().getCanonicalType();
6471*0a6a1f1dSLionel Sambuc SourceLocation Loc = S.getLocForEndOfToken(VD->getLocEnd());
6472*0a6a1f1dSLionel Sambuc std::string Init = S.getFixItZeroInitializerForType(VariableTy, Loc);
6473*0a6a1f1dSLionel Sambuc
6474*0a6a1f1dSLionel Sambuc S.Diag(Loc, diag::note_add_initializer)
6475*0a6a1f1dSLionel Sambuc << VD << FixItHint::CreateInsertion(Loc, Init);
6476*0a6a1f1dSLionel Sambuc }
6477*0a6a1f1dSLionel Sambuc
Diagnose(Sema & S,const InitializedEntity & Entity,const InitializationKind & Kind,ArrayRef<Expr * > Args)6478f4a2713aSLionel Sambuc bool InitializationSequence::Diagnose(Sema &S,
6479f4a2713aSLionel Sambuc const InitializedEntity &Entity,
6480f4a2713aSLionel Sambuc const InitializationKind &Kind,
6481f4a2713aSLionel Sambuc ArrayRef<Expr *> Args) {
6482f4a2713aSLionel Sambuc if (!Failed())
6483f4a2713aSLionel Sambuc return false;
6484f4a2713aSLionel Sambuc
6485f4a2713aSLionel Sambuc QualType DestType = Entity.getType();
6486f4a2713aSLionel Sambuc switch (Failure) {
6487f4a2713aSLionel Sambuc case FK_TooManyInitsForReference:
6488f4a2713aSLionel Sambuc // FIXME: Customize for the initialized entity?
6489f4a2713aSLionel Sambuc if (Args.empty()) {
6490f4a2713aSLionel Sambuc // Dig out the reference subobject which is uninitialized and diagnose it.
6491f4a2713aSLionel Sambuc // If this is value-initialization, this could be nested some way within
6492f4a2713aSLionel Sambuc // the target type.
6493f4a2713aSLionel Sambuc assert(Kind.getKind() == InitializationKind::IK_Value ||
6494f4a2713aSLionel Sambuc DestType->isReferenceType());
6495f4a2713aSLionel Sambuc bool Diagnosed =
6496f4a2713aSLionel Sambuc DiagnoseUninitializedReference(S, Kind.getLocation(), DestType);
6497f4a2713aSLionel Sambuc assert(Diagnosed && "couldn't find uninitialized reference to diagnose");
6498f4a2713aSLionel Sambuc (void)Diagnosed;
6499f4a2713aSLionel Sambuc } else // FIXME: diagnostic below could be better!
6500f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits)
6501f4a2713aSLionel Sambuc << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd());
6502f4a2713aSLionel Sambuc break;
6503f4a2713aSLionel Sambuc
6504f4a2713aSLionel Sambuc case FK_ArrayNeedsInitList:
6505f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0;
6506f4a2713aSLionel Sambuc break;
6507f4a2713aSLionel Sambuc case FK_ArrayNeedsInitListOrStringLiteral:
6508f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1;
6509f4a2713aSLionel Sambuc break;
6510f4a2713aSLionel Sambuc case FK_ArrayNeedsInitListOrWideStringLiteral:
6511f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2;
6512f4a2713aSLionel Sambuc break;
6513f4a2713aSLionel Sambuc case FK_NarrowStringIntoWideCharArray:
6514f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar);
6515f4a2713aSLionel Sambuc break;
6516f4a2713aSLionel Sambuc case FK_WideStringIntoCharArray:
6517f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char);
6518f4a2713aSLionel Sambuc break;
6519f4a2713aSLionel Sambuc case FK_IncompatWideStringIntoWideChar:
6520f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(),
6521f4a2713aSLionel Sambuc diag::err_array_init_incompat_wide_string_into_wchar);
6522f4a2713aSLionel Sambuc break;
6523f4a2713aSLionel Sambuc case FK_ArrayTypeMismatch:
6524f4a2713aSLionel Sambuc case FK_NonConstantArrayInit:
6525f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(),
6526f4a2713aSLionel Sambuc (Failure == FK_ArrayTypeMismatch
6527f4a2713aSLionel Sambuc ? diag::err_array_init_different_type
6528f4a2713aSLionel Sambuc : diag::err_array_init_non_constant_array))
6529f4a2713aSLionel Sambuc << DestType.getNonReferenceType()
6530f4a2713aSLionel Sambuc << Args[0]->getType()
6531f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6532f4a2713aSLionel Sambuc break;
6533f4a2713aSLionel Sambuc
6534f4a2713aSLionel Sambuc case FK_VariableLengthArrayHasInitializer:
6535f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_variable_object_no_init)
6536f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6537f4a2713aSLionel Sambuc break;
6538f4a2713aSLionel Sambuc
6539f4a2713aSLionel Sambuc case FK_AddressOfOverloadFailed: {
6540f4a2713aSLionel Sambuc DeclAccessPair Found;
6541f4a2713aSLionel Sambuc S.ResolveAddressOfOverloadedFunction(Args[0],
6542f4a2713aSLionel Sambuc DestType.getNonReferenceType(),
6543f4a2713aSLionel Sambuc true,
6544f4a2713aSLionel Sambuc Found);
6545f4a2713aSLionel Sambuc break;
6546f4a2713aSLionel Sambuc }
6547f4a2713aSLionel Sambuc
6548f4a2713aSLionel Sambuc case FK_ReferenceInitOverloadFailed:
6549f4a2713aSLionel Sambuc case FK_UserConversionOverloadFailed:
6550f4a2713aSLionel Sambuc switch (FailedOverloadResult) {
6551f4a2713aSLionel Sambuc case OR_Ambiguous:
6552f4a2713aSLionel Sambuc if (Failure == FK_UserConversionOverloadFailed)
6553f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition)
6554f4a2713aSLionel Sambuc << Args[0]->getType() << DestType
6555f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6556f4a2713aSLionel Sambuc else
6557f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous)
6558f4a2713aSLionel Sambuc << DestType << Args[0]->getType()
6559f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6560f4a2713aSLionel Sambuc
6561f4a2713aSLionel Sambuc FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args);
6562f4a2713aSLionel Sambuc break;
6563f4a2713aSLionel Sambuc
6564f4a2713aSLionel Sambuc case OR_No_Viable_Function:
6565f4a2713aSLionel Sambuc if (!S.RequireCompleteType(Kind.getLocation(),
6566f4a2713aSLionel Sambuc DestType.getNonReferenceType(),
6567f4a2713aSLionel Sambuc diag::err_typecheck_nonviable_condition_incomplete,
6568f4a2713aSLionel Sambuc Args[0]->getType(), Args[0]->getSourceRange()))
6569f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition)
6570f4a2713aSLionel Sambuc << Args[0]->getType() << Args[0]->getSourceRange()
6571f4a2713aSLionel Sambuc << DestType.getNonReferenceType();
6572f4a2713aSLionel Sambuc
6573f4a2713aSLionel Sambuc FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args);
6574f4a2713aSLionel Sambuc break;
6575f4a2713aSLionel Sambuc
6576f4a2713aSLionel Sambuc case OR_Deleted: {
6577f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function)
6578f4a2713aSLionel Sambuc << Args[0]->getType() << DestType.getNonReferenceType()
6579f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6580f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
6581f4a2713aSLionel Sambuc OverloadingResult Ovl
6582f4a2713aSLionel Sambuc = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best,
6583f4a2713aSLionel Sambuc true);
6584f4a2713aSLionel Sambuc if (Ovl == OR_Deleted) {
6585f4a2713aSLionel Sambuc S.NoteDeletedFunction(Best->Function);
6586f4a2713aSLionel Sambuc } else {
6587f4a2713aSLionel Sambuc llvm_unreachable("Inconsistent overload resolution?");
6588f4a2713aSLionel Sambuc }
6589f4a2713aSLionel Sambuc break;
6590f4a2713aSLionel Sambuc }
6591f4a2713aSLionel Sambuc
6592f4a2713aSLionel Sambuc case OR_Success:
6593f4a2713aSLionel Sambuc llvm_unreachable("Conversion did not fail!");
6594f4a2713aSLionel Sambuc }
6595f4a2713aSLionel Sambuc break;
6596f4a2713aSLionel Sambuc
6597f4a2713aSLionel Sambuc case FK_NonConstLValueReferenceBindingToTemporary:
6598f4a2713aSLionel Sambuc if (isa<InitListExpr>(Args[0])) {
6599f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(),
6600f4a2713aSLionel Sambuc diag::err_lvalue_reference_bind_to_initlist)
6601f4a2713aSLionel Sambuc << DestType.getNonReferenceType().isVolatileQualified()
6602f4a2713aSLionel Sambuc << DestType.getNonReferenceType()
6603f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6604f4a2713aSLionel Sambuc break;
6605f4a2713aSLionel Sambuc }
6606f4a2713aSLionel Sambuc // Intentional fallthrough
6607f4a2713aSLionel Sambuc
6608f4a2713aSLionel Sambuc case FK_NonConstLValueReferenceBindingToUnrelated:
6609f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(),
6610f4a2713aSLionel Sambuc Failure == FK_NonConstLValueReferenceBindingToTemporary
6611f4a2713aSLionel Sambuc ? diag::err_lvalue_reference_bind_to_temporary
6612f4a2713aSLionel Sambuc : diag::err_lvalue_reference_bind_to_unrelated)
6613f4a2713aSLionel Sambuc << DestType.getNonReferenceType().isVolatileQualified()
6614f4a2713aSLionel Sambuc << DestType.getNonReferenceType()
6615f4a2713aSLionel Sambuc << Args[0]->getType()
6616f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6617f4a2713aSLionel Sambuc break;
6618f4a2713aSLionel Sambuc
6619f4a2713aSLionel Sambuc case FK_RValueReferenceBindingToLValue:
6620f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref)
6621f4a2713aSLionel Sambuc << DestType.getNonReferenceType() << Args[0]->getType()
6622f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6623f4a2713aSLionel Sambuc break;
6624f4a2713aSLionel Sambuc
6625f4a2713aSLionel Sambuc case FK_ReferenceInitDropsQualifiers:
6626f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals)
6627f4a2713aSLionel Sambuc << DestType.getNonReferenceType()
6628f4a2713aSLionel Sambuc << Args[0]->getType()
6629f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6630f4a2713aSLionel Sambuc break;
6631f4a2713aSLionel Sambuc
6632f4a2713aSLionel Sambuc case FK_ReferenceInitFailed:
6633f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_reference_bind_failed)
6634f4a2713aSLionel Sambuc << DestType.getNonReferenceType()
6635f4a2713aSLionel Sambuc << Args[0]->isLValue()
6636f4a2713aSLionel Sambuc << Args[0]->getType()
6637f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6638f4a2713aSLionel Sambuc emitBadConversionNotes(S, Entity, Args[0]);
6639f4a2713aSLionel Sambuc break;
6640f4a2713aSLionel Sambuc
6641f4a2713aSLionel Sambuc case FK_ConversionFailed: {
6642f4a2713aSLionel Sambuc QualType FromType = Args[0]->getType();
6643f4a2713aSLionel Sambuc PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed)
6644f4a2713aSLionel Sambuc << (int)Entity.getKind()
6645f4a2713aSLionel Sambuc << DestType
6646f4a2713aSLionel Sambuc << Args[0]->isLValue()
6647f4a2713aSLionel Sambuc << FromType
6648f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6649f4a2713aSLionel Sambuc S.HandleFunctionTypeMismatch(PDiag, FromType, DestType);
6650f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), PDiag);
6651f4a2713aSLionel Sambuc emitBadConversionNotes(S, Entity, Args[0]);
6652f4a2713aSLionel Sambuc break;
6653f4a2713aSLionel Sambuc }
6654f4a2713aSLionel Sambuc
6655f4a2713aSLionel Sambuc case FK_ConversionFromPropertyFailed:
6656f4a2713aSLionel Sambuc // No-op. This error has already been reported.
6657f4a2713aSLionel Sambuc break;
6658f4a2713aSLionel Sambuc
6659f4a2713aSLionel Sambuc case FK_TooManyInitsForScalar: {
6660f4a2713aSLionel Sambuc SourceRange R;
6661f4a2713aSLionel Sambuc
6662f4a2713aSLionel Sambuc if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0]))
6663f4a2713aSLionel Sambuc R = SourceRange(InitList->getInit(0)->getLocEnd(),
6664f4a2713aSLionel Sambuc InitList->getLocEnd());
6665f4a2713aSLionel Sambuc else
6666f4a2713aSLionel Sambuc R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd());
6667f4a2713aSLionel Sambuc
6668*0a6a1f1dSLionel Sambuc R.setBegin(S.getLocForEndOfToken(R.getBegin()));
6669f4a2713aSLionel Sambuc if (Kind.isCStyleOrFunctionalCast())
6670f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg)
6671f4a2713aSLionel Sambuc << R;
6672f4a2713aSLionel Sambuc else
6673f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_excess_initializers)
6674f4a2713aSLionel Sambuc << /*scalar=*/2 << R;
6675f4a2713aSLionel Sambuc break;
6676f4a2713aSLionel Sambuc }
6677f4a2713aSLionel Sambuc
6678f4a2713aSLionel Sambuc case FK_ReferenceBindingToInitList:
6679f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list)
6680f4a2713aSLionel Sambuc << DestType.getNonReferenceType() << Args[0]->getSourceRange();
6681f4a2713aSLionel Sambuc break;
6682f4a2713aSLionel Sambuc
6683f4a2713aSLionel Sambuc case FK_InitListBadDestinationType:
6684f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type)
6685f4a2713aSLionel Sambuc << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange();
6686f4a2713aSLionel Sambuc break;
6687f4a2713aSLionel Sambuc
6688f4a2713aSLionel Sambuc case FK_ListConstructorOverloadFailed:
6689f4a2713aSLionel Sambuc case FK_ConstructorOverloadFailed: {
6690f4a2713aSLionel Sambuc SourceRange ArgsRange;
6691f4a2713aSLionel Sambuc if (Args.size())
6692f4a2713aSLionel Sambuc ArgsRange = SourceRange(Args.front()->getLocStart(),
6693f4a2713aSLionel Sambuc Args.back()->getLocEnd());
6694f4a2713aSLionel Sambuc
6695f4a2713aSLionel Sambuc if (Failure == FK_ListConstructorOverloadFailed) {
6696*0a6a1f1dSLionel Sambuc assert(Args.size() == 1 &&
6697*0a6a1f1dSLionel Sambuc "List construction from other than 1 argument.");
6698f4a2713aSLionel Sambuc InitListExpr *InitList = cast<InitListExpr>(Args[0]);
6699f4a2713aSLionel Sambuc Args = MultiExprArg(InitList->getInits(), InitList->getNumInits());
6700f4a2713aSLionel Sambuc }
6701f4a2713aSLionel Sambuc
6702f4a2713aSLionel Sambuc // FIXME: Using "DestType" for the entity we're printing is probably
6703f4a2713aSLionel Sambuc // bad.
6704f4a2713aSLionel Sambuc switch (FailedOverloadResult) {
6705f4a2713aSLionel Sambuc case OR_Ambiguous:
6706f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init)
6707f4a2713aSLionel Sambuc << DestType << ArgsRange;
6708f4a2713aSLionel Sambuc FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args);
6709f4a2713aSLionel Sambuc break;
6710f4a2713aSLionel Sambuc
6711f4a2713aSLionel Sambuc case OR_No_Viable_Function:
6712f4a2713aSLionel Sambuc if (Kind.getKind() == InitializationKind::IK_Default &&
6713f4a2713aSLionel Sambuc (Entity.getKind() == InitializedEntity::EK_Base ||
6714f4a2713aSLionel Sambuc Entity.getKind() == InitializedEntity::EK_Member) &&
6715f4a2713aSLionel Sambuc isa<CXXConstructorDecl>(S.CurContext)) {
6716f4a2713aSLionel Sambuc // This is implicit default initialization of a member or
6717f4a2713aSLionel Sambuc // base within a constructor. If no viable function was
6718f4a2713aSLionel Sambuc // found, notify the user that she needs to explicitly
6719f4a2713aSLionel Sambuc // initialize this base/member.
6720f4a2713aSLionel Sambuc CXXConstructorDecl *Constructor
6721f4a2713aSLionel Sambuc = cast<CXXConstructorDecl>(S.CurContext);
6722f4a2713aSLionel Sambuc if (Entity.getKind() == InitializedEntity::EK_Base) {
6723f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_missing_default_ctor)
6724f4a2713aSLionel Sambuc << (Constructor->getInheritedConstructor() ? 2 :
6725f4a2713aSLionel Sambuc Constructor->isImplicit() ? 1 : 0)
6726f4a2713aSLionel Sambuc << S.Context.getTypeDeclType(Constructor->getParent())
6727f4a2713aSLionel Sambuc << /*base=*/0
6728f4a2713aSLionel Sambuc << Entity.getType();
6729f4a2713aSLionel Sambuc
6730f4a2713aSLionel Sambuc RecordDecl *BaseDecl
6731f4a2713aSLionel Sambuc = Entity.getBaseSpecifier()->getType()->getAs<RecordType>()
6732f4a2713aSLionel Sambuc ->getDecl();
6733f4a2713aSLionel Sambuc S.Diag(BaseDecl->getLocation(), diag::note_previous_decl)
6734f4a2713aSLionel Sambuc << S.Context.getTagDeclType(BaseDecl);
6735f4a2713aSLionel Sambuc } else {
6736f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_missing_default_ctor)
6737f4a2713aSLionel Sambuc << (Constructor->getInheritedConstructor() ? 2 :
6738f4a2713aSLionel Sambuc Constructor->isImplicit() ? 1 : 0)
6739f4a2713aSLionel Sambuc << S.Context.getTypeDeclType(Constructor->getParent())
6740f4a2713aSLionel Sambuc << /*member=*/1
6741f4a2713aSLionel Sambuc << Entity.getName();
6742*0a6a1f1dSLionel Sambuc S.Diag(Entity.getDecl()->getLocation(),
6743*0a6a1f1dSLionel Sambuc diag::note_member_declared_at);
6744f4a2713aSLionel Sambuc
6745f4a2713aSLionel Sambuc if (const RecordType *Record
6746f4a2713aSLionel Sambuc = Entity.getType()->getAs<RecordType>())
6747f4a2713aSLionel Sambuc S.Diag(Record->getDecl()->getLocation(),
6748f4a2713aSLionel Sambuc diag::note_previous_decl)
6749f4a2713aSLionel Sambuc << S.Context.getTagDeclType(Record->getDecl());
6750f4a2713aSLionel Sambuc }
6751f4a2713aSLionel Sambuc break;
6752f4a2713aSLionel Sambuc }
6753f4a2713aSLionel Sambuc
6754f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init)
6755f4a2713aSLionel Sambuc << DestType << ArgsRange;
6756f4a2713aSLionel Sambuc FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args);
6757f4a2713aSLionel Sambuc break;
6758f4a2713aSLionel Sambuc
6759f4a2713aSLionel Sambuc case OR_Deleted: {
6760f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
6761f4a2713aSLionel Sambuc OverloadingResult Ovl
6762f4a2713aSLionel Sambuc = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best);
6763f4a2713aSLionel Sambuc if (Ovl != OR_Deleted) {
6764f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init)
6765f4a2713aSLionel Sambuc << true << DestType << ArgsRange;
6766f4a2713aSLionel Sambuc llvm_unreachable("Inconsistent overload resolution?");
6767f4a2713aSLionel Sambuc break;
6768f4a2713aSLionel Sambuc }
6769f4a2713aSLionel Sambuc
6770f4a2713aSLionel Sambuc // If this is a defaulted or implicitly-declared function, then
6771f4a2713aSLionel Sambuc // it was implicitly deleted. Make it clear that the deletion was
6772f4a2713aSLionel Sambuc // implicit.
6773f4a2713aSLionel Sambuc if (S.isImplicitlyDeleted(Best->Function))
6774f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init)
6775f4a2713aSLionel Sambuc << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function))
6776f4a2713aSLionel Sambuc << DestType << ArgsRange;
6777f4a2713aSLionel Sambuc else
6778f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init)
6779f4a2713aSLionel Sambuc << true << DestType << ArgsRange;
6780f4a2713aSLionel Sambuc
6781f4a2713aSLionel Sambuc S.NoteDeletedFunction(Best->Function);
6782f4a2713aSLionel Sambuc break;
6783f4a2713aSLionel Sambuc }
6784f4a2713aSLionel Sambuc
6785f4a2713aSLionel Sambuc case OR_Success:
6786f4a2713aSLionel Sambuc llvm_unreachable("Conversion did not fail!");
6787f4a2713aSLionel Sambuc }
6788f4a2713aSLionel Sambuc }
6789f4a2713aSLionel Sambuc break;
6790f4a2713aSLionel Sambuc
6791f4a2713aSLionel Sambuc case FK_DefaultInitOfConst:
6792f4a2713aSLionel Sambuc if (Entity.getKind() == InitializedEntity::EK_Member &&
6793f4a2713aSLionel Sambuc isa<CXXConstructorDecl>(S.CurContext)) {
6794f4a2713aSLionel Sambuc // This is implicit default-initialization of a const member in
6795f4a2713aSLionel Sambuc // a constructor. Complain that it needs to be explicitly
6796f4a2713aSLionel Sambuc // initialized.
6797f4a2713aSLionel Sambuc CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext);
6798f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor)
6799f4a2713aSLionel Sambuc << (Constructor->getInheritedConstructor() ? 2 :
6800f4a2713aSLionel Sambuc Constructor->isImplicit() ? 1 : 0)
6801f4a2713aSLionel Sambuc << S.Context.getTypeDeclType(Constructor->getParent())
6802f4a2713aSLionel Sambuc << /*const=*/1
6803f4a2713aSLionel Sambuc << Entity.getName();
6804f4a2713aSLionel Sambuc S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl)
6805f4a2713aSLionel Sambuc << Entity.getName();
6806f4a2713aSLionel Sambuc } else {
6807f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_default_init_const)
6808f4a2713aSLionel Sambuc << DestType << (bool)DestType->getAs<RecordType>();
6809*0a6a1f1dSLionel Sambuc maybeEmitZeroInitializationFixit(S, *this, Entity);
6810f4a2713aSLionel Sambuc }
6811f4a2713aSLionel Sambuc break;
6812f4a2713aSLionel Sambuc
6813f4a2713aSLionel Sambuc case FK_Incomplete:
6814f4a2713aSLionel Sambuc S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType,
6815f4a2713aSLionel Sambuc diag::err_init_incomplete_type);
6816f4a2713aSLionel Sambuc break;
6817f4a2713aSLionel Sambuc
6818f4a2713aSLionel Sambuc case FK_ListInitializationFailed: {
6819f4a2713aSLionel Sambuc // Run the init list checker again to emit diagnostics.
6820f4a2713aSLionel Sambuc InitListExpr *InitList = cast<InitListExpr>(Args[0]);
6821f4a2713aSLionel Sambuc diagnoseListInit(S, Entity, InitList);
6822f4a2713aSLionel Sambuc break;
6823f4a2713aSLionel Sambuc }
6824f4a2713aSLionel Sambuc
6825f4a2713aSLionel Sambuc case FK_PlaceholderType: {
6826f4a2713aSLionel Sambuc // FIXME: Already diagnosed!
6827f4a2713aSLionel Sambuc break;
6828f4a2713aSLionel Sambuc }
6829f4a2713aSLionel Sambuc
6830f4a2713aSLionel Sambuc case FK_ExplicitConstructor: {
6831f4a2713aSLionel Sambuc S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor)
6832f4a2713aSLionel Sambuc << Args[0]->getSourceRange();
6833f4a2713aSLionel Sambuc OverloadCandidateSet::iterator Best;
6834f4a2713aSLionel Sambuc OverloadingResult Ovl
6835f4a2713aSLionel Sambuc = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best);
6836f4a2713aSLionel Sambuc (void)Ovl;
6837f4a2713aSLionel Sambuc assert(Ovl == OR_Success && "Inconsistent overload resolution");
6838f4a2713aSLionel Sambuc CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function);
6839f4a2713aSLionel Sambuc S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here);
6840f4a2713aSLionel Sambuc break;
6841f4a2713aSLionel Sambuc }
6842f4a2713aSLionel Sambuc }
6843f4a2713aSLionel Sambuc
6844f4a2713aSLionel Sambuc PrintInitLocationNote(S, Entity);
6845f4a2713aSLionel Sambuc return true;
6846f4a2713aSLionel Sambuc }
6847f4a2713aSLionel Sambuc
dump(raw_ostream & OS) const6848f4a2713aSLionel Sambuc void InitializationSequence::dump(raw_ostream &OS) const {
6849f4a2713aSLionel Sambuc switch (SequenceKind) {
6850f4a2713aSLionel Sambuc case FailedSequence: {
6851f4a2713aSLionel Sambuc OS << "Failed sequence: ";
6852f4a2713aSLionel Sambuc switch (Failure) {
6853f4a2713aSLionel Sambuc case FK_TooManyInitsForReference:
6854f4a2713aSLionel Sambuc OS << "too many initializers for reference";
6855f4a2713aSLionel Sambuc break;
6856f4a2713aSLionel Sambuc
6857f4a2713aSLionel Sambuc case FK_ArrayNeedsInitList:
6858f4a2713aSLionel Sambuc OS << "array requires initializer list";
6859f4a2713aSLionel Sambuc break;
6860f4a2713aSLionel Sambuc
6861f4a2713aSLionel Sambuc case FK_ArrayNeedsInitListOrStringLiteral:
6862f4a2713aSLionel Sambuc OS << "array requires initializer list or string literal";
6863f4a2713aSLionel Sambuc break;
6864f4a2713aSLionel Sambuc
6865f4a2713aSLionel Sambuc case FK_ArrayNeedsInitListOrWideStringLiteral:
6866f4a2713aSLionel Sambuc OS << "array requires initializer list or wide string literal";
6867f4a2713aSLionel Sambuc break;
6868f4a2713aSLionel Sambuc
6869f4a2713aSLionel Sambuc case FK_NarrowStringIntoWideCharArray:
6870f4a2713aSLionel Sambuc OS << "narrow string into wide char array";
6871f4a2713aSLionel Sambuc break;
6872f4a2713aSLionel Sambuc
6873f4a2713aSLionel Sambuc case FK_WideStringIntoCharArray:
6874f4a2713aSLionel Sambuc OS << "wide string into char array";
6875f4a2713aSLionel Sambuc break;
6876f4a2713aSLionel Sambuc
6877f4a2713aSLionel Sambuc case FK_IncompatWideStringIntoWideChar:
6878f4a2713aSLionel Sambuc OS << "incompatible wide string into wide char array";
6879f4a2713aSLionel Sambuc break;
6880f4a2713aSLionel Sambuc
6881f4a2713aSLionel Sambuc case FK_ArrayTypeMismatch:
6882f4a2713aSLionel Sambuc OS << "array type mismatch";
6883f4a2713aSLionel Sambuc break;
6884f4a2713aSLionel Sambuc
6885f4a2713aSLionel Sambuc case FK_NonConstantArrayInit:
6886f4a2713aSLionel Sambuc OS << "non-constant array initializer";
6887f4a2713aSLionel Sambuc break;
6888f4a2713aSLionel Sambuc
6889f4a2713aSLionel Sambuc case FK_AddressOfOverloadFailed:
6890f4a2713aSLionel Sambuc OS << "address of overloaded function failed";
6891f4a2713aSLionel Sambuc break;
6892f4a2713aSLionel Sambuc
6893f4a2713aSLionel Sambuc case FK_ReferenceInitOverloadFailed:
6894f4a2713aSLionel Sambuc OS << "overload resolution for reference initialization failed";
6895f4a2713aSLionel Sambuc break;
6896f4a2713aSLionel Sambuc
6897f4a2713aSLionel Sambuc case FK_NonConstLValueReferenceBindingToTemporary:
6898f4a2713aSLionel Sambuc OS << "non-const lvalue reference bound to temporary";
6899f4a2713aSLionel Sambuc break;
6900f4a2713aSLionel Sambuc
6901f4a2713aSLionel Sambuc case FK_NonConstLValueReferenceBindingToUnrelated:
6902f4a2713aSLionel Sambuc OS << "non-const lvalue reference bound to unrelated type";
6903f4a2713aSLionel Sambuc break;
6904f4a2713aSLionel Sambuc
6905f4a2713aSLionel Sambuc case FK_RValueReferenceBindingToLValue:
6906f4a2713aSLionel Sambuc OS << "rvalue reference bound to an lvalue";
6907f4a2713aSLionel Sambuc break;
6908f4a2713aSLionel Sambuc
6909f4a2713aSLionel Sambuc case FK_ReferenceInitDropsQualifiers:
6910f4a2713aSLionel Sambuc OS << "reference initialization drops qualifiers";
6911f4a2713aSLionel Sambuc break;
6912f4a2713aSLionel Sambuc
6913f4a2713aSLionel Sambuc case FK_ReferenceInitFailed:
6914f4a2713aSLionel Sambuc OS << "reference initialization failed";
6915f4a2713aSLionel Sambuc break;
6916f4a2713aSLionel Sambuc
6917f4a2713aSLionel Sambuc case FK_ConversionFailed:
6918f4a2713aSLionel Sambuc OS << "conversion failed";
6919f4a2713aSLionel Sambuc break;
6920f4a2713aSLionel Sambuc
6921f4a2713aSLionel Sambuc case FK_ConversionFromPropertyFailed:
6922f4a2713aSLionel Sambuc OS << "conversion from property failed";
6923f4a2713aSLionel Sambuc break;
6924f4a2713aSLionel Sambuc
6925f4a2713aSLionel Sambuc case FK_TooManyInitsForScalar:
6926f4a2713aSLionel Sambuc OS << "too many initializers for scalar";
6927f4a2713aSLionel Sambuc break;
6928f4a2713aSLionel Sambuc
6929f4a2713aSLionel Sambuc case FK_ReferenceBindingToInitList:
6930f4a2713aSLionel Sambuc OS << "referencing binding to initializer list";
6931f4a2713aSLionel Sambuc break;
6932f4a2713aSLionel Sambuc
6933f4a2713aSLionel Sambuc case FK_InitListBadDestinationType:
6934f4a2713aSLionel Sambuc OS << "initializer list for non-aggregate, non-scalar type";
6935f4a2713aSLionel Sambuc break;
6936f4a2713aSLionel Sambuc
6937f4a2713aSLionel Sambuc case FK_UserConversionOverloadFailed:
6938f4a2713aSLionel Sambuc OS << "overloading failed for user-defined conversion";
6939f4a2713aSLionel Sambuc break;
6940f4a2713aSLionel Sambuc
6941f4a2713aSLionel Sambuc case FK_ConstructorOverloadFailed:
6942f4a2713aSLionel Sambuc OS << "constructor overloading failed";
6943f4a2713aSLionel Sambuc break;
6944f4a2713aSLionel Sambuc
6945f4a2713aSLionel Sambuc case FK_DefaultInitOfConst:
6946f4a2713aSLionel Sambuc OS << "default initialization of a const variable";
6947f4a2713aSLionel Sambuc break;
6948f4a2713aSLionel Sambuc
6949f4a2713aSLionel Sambuc case FK_Incomplete:
6950f4a2713aSLionel Sambuc OS << "initialization of incomplete type";
6951f4a2713aSLionel Sambuc break;
6952f4a2713aSLionel Sambuc
6953f4a2713aSLionel Sambuc case FK_ListInitializationFailed:
6954f4a2713aSLionel Sambuc OS << "list initialization checker failure";
6955f4a2713aSLionel Sambuc break;
6956f4a2713aSLionel Sambuc
6957f4a2713aSLionel Sambuc case FK_VariableLengthArrayHasInitializer:
6958f4a2713aSLionel Sambuc OS << "variable length array has an initializer";
6959f4a2713aSLionel Sambuc break;
6960f4a2713aSLionel Sambuc
6961f4a2713aSLionel Sambuc case FK_PlaceholderType:
6962f4a2713aSLionel Sambuc OS << "initializer expression isn't contextually valid";
6963f4a2713aSLionel Sambuc break;
6964f4a2713aSLionel Sambuc
6965f4a2713aSLionel Sambuc case FK_ListConstructorOverloadFailed:
6966f4a2713aSLionel Sambuc OS << "list constructor overloading failed";
6967f4a2713aSLionel Sambuc break;
6968f4a2713aSLionel Sambuc
6969f4a2713aSLionel Sambuc case FK_ExplicitConstructor:
6970f4a2713aSLionel Sambuc OS << "list copy initialization chose explicit constructor";
6971f4a2713aSLionel Sambuc break;
6972f4a2713aSLionel Sambuc }
6973f4a2713aSLionel Sambuc OS << '\n';
6974f4a2713aSLionel Sambuc return;
6975f4a2713aSLionel Sambuc }
6976f4a2713aSLionel Sambuc
6977f4a2713aSLionel Sambuc case DependentSequence:
6978f4a2713aSLionel Sambuc OS << "Dependent sequence\n";
6979f4a2713aSLionel Sambuc return;
6980f4a2713aSLionel Sambuc
6981f4a2713aSLionel Sambuc case NormalSequence:
6982f4a2713aSLionel Sambuc OS << "Normal sequence: ";
6983f4a2713aSLionel Sambuc break;
6984f4a2713aSLionel Sambuc }
6985f4a2713aSLionel Sambuc
6986f4a2713aSLionel Sambuc for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) {
6987f4a2713aSLionel Sambuc if (S != step_begin()) {
6988f4a2713aSLionel Sambuc OS << " -> ";
6989f4a2713aSLionel Sambuc }
6990f4a2713aSLionel Sambuc
6991f4a2713aSLionel Sambuc switch (S->Kind) {
6992f4a2713aSLionel Sambuc case SK_ResolveAddressOfOverloadedFunction:
6993f4a2713aSLionel Sambuc OS << "resolve address of overloaded function";
6994f4a2713aSLionel Sambuc break;
6995f4a2713aSLionel Sambuc
6996f4a2713aSLionel Sambuc case SK_CastDerivedToBaseRValue:
6997f4a2713aSLionel Sambuc OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")";
6998f4a2713aSLionel Sambuc break;
6999f4a2713aSLionel Sambuc
7000f4a2713aSLionel Sambuc case SK_CastDerivedToBaseXValue:
7001f4a2713aSLionel Sambuc OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")";
7002f4a2713aSLionel Sambuc break;
7003f4a2713aSLionel Sambuc
7004f4a2713aSLionel Sambuc case SK_CastDerivedToBaseLValue:
7005f4a2713aSLionel Sambuc OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")";
7006f4a2713aSLionel Sambuc break;
7007f4a2713aSLionel Sambuc
7008f4a2713aSLionel Sambuc case SK_BindReference:
7009f4a2713aSLionel Sambuc OS << "bind reference to lvalue";
7010f4a2713aSLionel Sambuc break;
7011f4a2713aSLionel Sambuc
7012f4a2713aSLionel Sambuc case SK_BindReferenceToTemporary:
7013f4a2713aSLionel Sambuc OS << "bind reference to a temporary";
7014f4a2713aSLionel Sambuc break;
7015f4a2713aSLionel Sambuc
7016f4a2713aSLionel Sambuc case SK_ExtraneousCopyToTemporary:
7017f4a2713aSLionel Sambuc OS << "extraneous C++03 copy to temporary";
7018f4a2713aSLionel Sambuc break;
7019f4a2713aSLionel Sambuc
7020f4a2713aSLionel Sambuc case SK_UserConversion:
7021f4a2713aSLionel Sambuc OS << "user-defined conversion via " << *S->Function.Function;
7022f4a2713aSLionel Sambuc break;
7023f4a2713aSLionel Sambuc
7024f4a2713aSLionel Sambuc case SK_QualificationConversionRValue:
7025f4a2713aSLionel Sambuc OS << "qualification conversion (rvalue)";
7026f4a2713aSLionel Sambuc break;
7027f4a2713aSLionel Sambuc
7028f4a2713aSLionel Sambuc case SK_QualificationConversionXValue:
7029f4a2713aSLionel Sambuc OS << "qualification conversion (xvalue)";
7030f4a2713aSLionel Sambuc break;
7031f4a2713aSLionel Sambuc
7032f4a2713aSLionel Sambuc case SK_QualificationConversionLValue:
7033f4a2713aSLionel Sambuc OS << "qualification conversion (lvalue)";
7034f4a2713aSLionel Sambuc break;
7035f4a2713aSLionel Sambuc
7036*0a6a1f1dSLionel Sambuc case SK_AtomicConversion:
7037*0a6a1f1dSLionel Sambuc OS << "non-atomic-to-atomic conversion";
7038*0a6a1f1dSLionel Sambuc break;
7039*0a6a1f1dSLionel Sambuc
7040f4a2713aSLionel Sambuc case SK_LValueToRValue:
7041f4a2713aSLionel Sambuc OS << "load (lvalue to rvalue)";
7042f4a2713aSLionel Sambuc break;
7043f4a2713aSLionel Sambuc
7044f4a2713aSLionel Sambuc case SK_ConversionSequence:
7045f4a2713aSLionel Sambuc OS << "implicit conversion sequence (";
7046f4a2713aSLionel Sambuc S->ICS->dump(); // FIXME: use OS
7047f4a2713aSLionel Sambuc OS << ")";
7048f4a2713aSLionel Sambuc break;
7049f4a2713aSLionel Sambuc
7050f4a2713aSLionel Sambuc case SK_ConversionSequenceNoNarrowing:
7051f4a2713aSLionel Sambuc OS << "implicit conversion sequence with narrowing prohibited (";
7052f4a2713aSLionel Sambuc S->ICS->dump(); // FIXME: use OS
7053f4a2713aSLionel Sambuc OS << ")";
7054f4a2713aSLionel Sambuc break;
7055f4a2713aSLionel Sambuc
7056f4a2713aSLionel Sambuc case SK_ListInitialization:
7057f4a2713aSLionel Sambuc OS << "list aggregate initialization";
7058f4a2713aSLionel Sambuc break;
7059f4a2713aSLionel Sambuc
7060f4a2713aSLionel Sambuc case SK_UnwrapInitList:
7061f4a2713aSLionel Sambuc OS << "unwrap reference initializer list";
7062f4a2713aSLionel Sambuc break;
7063f4a2713aSLionel Sambuc
7064f4a2713aSLionel Sambuc case SK_RewrapInitList:
7065f4a2713aSLionel Sambuc OS << "rewrap reference initializer list";
7066f4a2713aSLionel Sambuc break;
7067f4a2713aSLionel Sambuc
7068f4a2713aSLionel Sambuc case SK_ConstructorInitialization:
7069f4a2713aSLionel Sambuc OS << "constructor initialization";
7070f4a2713aSLionel Sambuc break;
7071f4a2713aSLionel Sambuc
7072*0a6a1f1dSLionel Sambuc case SK_ConstructorInitializationFromList:
7073*0a6a1f1dSLionel Sambuc OS << "list initialization via constructor";
7074*0a6a1f1dSLionel Sambuc break;
7075*0a6a1f1dSLionel Sambuc
7076f4a2713aSLionel Sambuc case SK_ZeroInitialization:
7077f4a2713aSLionel Sambuc OS << "zero initialization";
7078f4a2713aSLionel Sambuc break;
7079f4a2713aSLionel Sambuc
7080f4a2713aSLionel Sambuc case SK_CAssignment:
7081f4a2713aSLionel Sambuc OS << "C assignment";
7082f4a2713aSLionel Sambuc break;
7083f4a2713aSLionel Sambuc
7084f4a2713aSLionel Sambuc case SK_StringInit:
7085f4a2713aSLionel Sambuc OS << "string initialization";
7086f4a2713aSLionel Sambuc break;
7087f4a2713aSLionel Sambuc
7088f4a2713aSLionel Sambuc case SK_ObjCObjectConversion:
7089f4a2713aSLionel Sambuc OS << "Objective-C object conversion";
7090f4a2713aSLionel Sambuc break;
7091f4a2713aSLionel Sambuc
7092f4a2713aSLionel Sambuc case SK_ArrayInit:
7093f4a2713aSLionel Sambuc OS << "array initialization";
7094f4a2713aSLionel Sambuc break;
7095f4a2713aSLionel Sambuc
7096f4a2713aSLionel Sambuc case SK_ParenthesizedArrayInit:
7097f4a2713aSLionel Sambuc OS << "parenthesized array initialization";
7098f4a2713aSLionel Sambuc break;
7099f4a2713aSLionel Sambuc
7100f4a2713aSLionel Sambuc case SK_PassByIndirectCopyRestore:
7101f4a2713aSLionel Sambuc OS << "pass by indirect copy and restore";
7102f4a2713aSLionel Sambuc break;
7103f4a2713aSLionel Sambuc
7104f4a2713aSLionel Sambuc case SK_PassByIndirectRestore:
7105f4a2713aSLionel Sambuc OS << "pass by indirect restore";
7106f4a2713aSLionel Sambuc break;
7107f4a2713aSLionel Sambuc
7108f4a2713aSLionel Sambuc case SK_ProduceObjCObject:
7109f4a2713aSLionel Sambuc OS << "Objective-C object retension";
7110f4a2713aSLionel Sambuc break;
7111f4a2713aSLionel Sambuc
7112f4a2713aSLionel Sambuc case SK_StdInitializerList:
7113f4a2713aSLionel Sambuc OS << "std::initializer_list from initializer list";
7114f4a2713aSLionel Sambuc break;
7115f4a2713aSLionel Sambuc
7116*0a6a1f1dSLionel Sambuc case SK_StdInitializerListConstructorCall:
7117*0a6a1f1dSLionel Sambuc OS << "list initialization from std::initializer_list";
7118*0a6a1f1dSLionel Sambuc break;
7119*0a6a1f1dSLionel Sambuc
7120f4a2713aSLionel Sambuc case SK_OCLSamplerInit:
7121f4a2713aSLionel Sambuc OS << "OpenCL sampler_t from integer constant";
7122f4a2713aSLionel Sambuc break;
7123f4a2713aSLionel Sambuc
7124f4a2713aSLionel Sambuc case SK_OCLZeroEvent:
7125f4a2713aSLionel Sambuc OS << "OpenCL event_t from zero";
7126f4a2713aSLionel Sambuc break;
7127f4a2713aSLionel Sambuc }
7128f4a2713aSLionel Sambuc
7129f4a2713aSLionel Sambuc OS << " [" << S->Type.getAsString() << ']';
7130f4a2713aSLionel Sambuc }
7131f4a2713aSLionel Sambuc
7132f4a2713aSLionel Sambuc OS << '\n';
7133f4a2713aSLionel Sambuc }
7134f4a2713aSLionel Sambuc
dump() const7135f4a2713aSLionel Sambuc void InitializationSequence::dump() const {
7136f4a2713aSLionel Sambuc dump(llvm::errs());
7137f4a2713aSLionel Sambuc }
7138f4a2713aSLionel Sambuc
DiagnoseNarrowingInInitList(Sema & S,const ImplicitConversionSequence & ICS,QualType PreNarrowingType,QualType EntityType,const Expr * PostInit)7139f4a2713aSLionel Sambuc static void DiagnoseNarrowingInInitList(Sema &S,
7140f4a2713aSLionel Sambuc const ImplicitConversionSequence &ICS,
7141f4a2713aSLionel Sambuc QualType PreNarrowingType,
7142f4a2713aSLionel Sambuc QualType EntityType,
7143f4a2713aSLionel Sambuc const Expr *PostInit) {
7144*0a6a1f1dSLionel Sambuc const StandardConversionSequence *SCS = nullptr;
7145f4a2713aSLionel Sambuc switch (ICS.getKind()) {
7146f4a2713aSLionel Sambuc case ImplicitConversionSequence::StandardConversion:
7147f4a2713aSLionel Sambuc SCS = &ICS.Standard;
7148f4a2713aSLionel Sambuc break;
7149f4a2713aSLionel Sambuc case ImplicitConversionSequence::UserDefinedConversion:
7150f4a2713aSLionel Sambuc SCS = &ICS.UserDefined.After;
7151f4a2713aSLionel Sambuc break;
7152f4a2713aSLionel Sambuc case ImplicitConversionSequence::AmbiguousConversion:
7153f4a2713aSLionel Sambuc case ImplicitConversionSequence::EllipsisConversion:
7154f4a2713aSLionel Sambuc case ImplicitConversionSequence::BadConversion:
7155f4a2713aSLionel Sambuc return;
7156f4a2713aSLionel Sambuc }
7157f4a2713aSLionel Sambuc
7158f4a2713aSLionel Sambuc // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion.
7159f4a2713aSLionel Sambuc APValue ConstantValue;
7160f4a2713aSLionel Sambuc QualType ConstantType;
7161f4a2713aSLionel Sambuc switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue,
7162f4a2713aSLionel Sambuc ConstantType)) {
7163f4a2713aSLionel Sambuc case NK_Not_Narrowing:
7164f4a2713aSLionel Sambuc // No narrowing occurred.
7165f4a2713aSLionel Sambuc return;
7166f4a2713aSLionel Sambuc
7167f4a2713aSLionel Sambuc case NK_Type_Narrowing:
7168f4a2713aSLionel Sambuc // This was a floating-to-integer conversion, which is always considered a
7169f4a2713aSLionel Sambuc // narrowing conversion even if the value is a constant and can be
7170f4a2713aSLionel Sambuc // represented exactly as an integer.
7171f4a2713aSLionel Sambuc S.Diag(PostInit->getLocStart(),
7172f4a2713aSLionel Sambuc (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11)
7173f4a2713aSLionel Sambuc ? diag::warn_init_list_type_narrowing
7174f4a2713aSLionel Sambuc : diag::ext_init_list_type_narrowing)
7175f4a2713aSLionel Sambuc << PostInit->getSourceRange()
7176f4a2713aSLionel Sambuc << PreNarrowingType.getLocalUnqualifiedType()
7177f4a2713aSLionel Sambuc << EntityType.getLocalUnqualifiedType();
7178f4a2713aSLionel Sambuc break;
7179f4a2713aSLionel Sambuc
7180f4a2713aSLionel Sambuc case NK_Constant_Narrowing:
7181f4a2713aSLionel Sambuc // A constant value was narrowed.
7182f4a2713aSLionel Sambuc S.Diag(PostInit->getLocStart(),
7183f4a2713aSLionel Sambuc (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11)
7184f4a2713aSLionel Sambuc ? diag::warn_init_list_constant_narrowing
7185f4a2713aSLionel Sambuc : diag::ext_init_list_constant_narrowing)
7186f4a2713aSLionel Sambuc << PostInit->getSourceRange()
7187f4a2713aSLionel Sambuc << ConstantValue.getAsString(S.getASTContext(), ConstantType)
7188f4a2713aSLionel Sambuc << EntityType.getLocalUnqualifiedType();
7189f4a2713aSLionel Sambuc break;
7190f4a2713aSLionel Sambuc
7191f4a2713aSLionel Sambuc case NK_Variable_Narrowing:
7192f4a2713aSLionel Sambuc // A variable's value may have been narrowed.
7193f4a2713aSLionel Sambuc S.Diag(PostInit->getLocStart(),
7194f4a2713aSLionel Sambuc (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11)
7195f4a2713aSLionel Sambuc ? diag::warn_init_list_variable_narrowing
7196f4a2713aSLionel Sambuc : diag::ext_init_list_variable_narrowing)
7197f4a2713aSLionel Sambuc << PostInit->getSourceRange()
7198f4a2713aSLionel Sambuc << PreNarrowingType.getLocalUnqualifiedType()
7199f4a2713aSLionel Sambuc << EntityType.getLocalUnqualifiedType();
7200f4a2713aSLionel Sambuc break;
7201f4a2713aSLionel Sambuc }
7202f4a2713aSLionel Sambuc
7203f4a2713aSLionel Sambuc SmallString<128> StaticCast;
7204f4a2713aSLionel Sambuc llvm::raw_svector_ostream OS(StaticCast);
7205f4a2713aSLionel Sambuc OS << "static_cast<";
7206f4a2713aSLionel Sambuc if (const TypedefType *TT = EntityType->getAs<TypedefType>()) {
7207f4a2713aSLionel Sambuc // It's important to use the typedef's name if there is one so that the
7208f4a2713aSLionel Sambuc // fixit doesn't break code using types like int64_t.
7209f4a2713aSLionel Sambuc //
7210f4a2713aSLionel Sambuc // FIXME: This will break if the typedef requires qualification. But
7211f4a2713aSLionel Sambuc // getQualifiedNameAsString() includes non-machine-parsable components.
7212f4a2713aSLionel Sambuc OS << *TT->getDecl();
7213f4a2713aSLionel Sambuc } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>())
7214f4a2713aSLionel Sambuc OS << BT->getName(S.getLangOpts());
7215f4a2713aSLionel Sambuc else {
7216f4a2713aSLionel Sambuc // Oops, we didn't find the actual type of the variable. Don't emit a fixit
7217f4a2713aSLionel Sambuc // with a broken cast.
7218f4a2713aSLionel Sambuc return;
7219f4a2713aSLionel Sambuc }
7220f4a2713aSLionel Sambuc OS << ">(";
7221*0a6a1f1dSLionel Sambuc S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_silence)
7222f4a2713aSLionel Sambuc << PostInit->getSourceRange()
7223f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str())
7224f4a2713aSLionel Sambuc << FixItHint::CreateInsertion(
7225*0a6a1f1dSLionel Sambuc S.getLocForEndOfToken(PostInit->getLocEnd()), ")");
7226f4a2713aSLionel Sambuc }
7227f4a2713aSLionel Sambuc
7228f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
7229f4a2713aSLionel Sambuc // Initialization helper functions
7230f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
7231f4a2713aSLionel Sambuc bool
CanPerformCopyInitialization(const InitializedEntity & Entity,ExprResult Init)7232f4a2713aSLionel Sambuc Sema::CanPerformCopyInitialization(const InitializedEntity &Entity,
7233f4a2713aSLionel Sambuc ExprResult Init) {
7234f4a2713aSLionel Sambuc if (Init.isInvalid())
7235f4a2713aSLionel Sambuc return false;
7236f4a2713aSLionel Sambuc
7237f4a2713aSLionel Sambuc Expr *InitE = Init.get();
7238f4a2713aSLionel Sambuc assert(InitE && "No initialization expression");
7239f4a2713aSLionel Sambuc
7240f4a2713aSLionel Sambuc InitializationKind Kind
7241f4a2713aSLionel Sambuc = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation());
7242f4a2713aSLionel Sambuc InitializationSequence Seq(*this, Entity, Kind, InitE);
7243f4a2713aSLionel Sambuc return !Seq.Failed();
7244f4a2713aSLionel Sambuc }
7245f4a2713aSLionel Sambuc
7246f4a2713aSLionel Sambuc ExprResult
PerformCopyInitialization(const InitializedEntity & Entity,SourceLocation EqualLoc,ExprResult Init,bool TopLevelOfInitList,bool AllowExplicit)7247f4a2713aSLionel Sambuc Sema::PerformCopyInitialization(const InitializedEntity &Entity,
7248f4a2713aSLionel Sambuc SourceLocation EqualLoc,
7249f4a2713aSLionel Sambuc ExprResult Init,
7250f4a2713aSLionel Sambuc bool TopLevelOfInitList,
7251f4a2713aSLionel Sambuc bool AllowExplicit) {
7252f4a2713aSLionel Sambuc if (Init.isInvalid())
7253f4a2713aSLionel Sambuc return ExprError();
7254f4a2713aSLionel Sambuc
7255f4a2713aSLionel Sambuc Expr *InitE = Init.get();
7256f4a2713aSLionel Sambuc assert(InitE && "No initialization expression?");
7257f4a2713aSLionel Sambuc
7258f4a2713aSLionel Sambuc if (EqualLoc.isInvalid())
7259f4a2713aSLionel Sambuc EqualLoc = InitE->getLocStart();
7260f4a2713aSLionel Sambuc
7261f4a2713aSLionel Sambuc InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(),
7262f4a2713aSLionel Sambuc EqualLoc,
7263f4a2713aSLionel Sambuc AllowExplicit);
7264f4a2713aSLionel Sambuc InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList);
7265*0a6a1f1dSLionel Sambuc Init.get();
7266f4a2713aSLionel Sambuc
7267f4a2713aSLionel Sambuc ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE);
7268f4a2713aSLionel Sambuc
7269f4a2713aSLionel Sambuc return Result;
7270f4a2713aSLionel Sambuc }
7271