Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Initialization.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Sema/Designator.h" |
| 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 35 | /// \brief Check whether T is compatible with a wide character type (wchar_t, |
| 36 | /// char16_t or char32_t). |
| 37 | static bool IsWideCharCompatible(QualType T, ASTContext &Context) { |
| 38 | if (Context.typesAreCompatible(Context.getWideCharType(), T)) |
| 39 | return true; |
| 40 | if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) { |
| 41 | return Context.typesAreCompatible(Context.Char16Ty, T) || |
| 42 | Context.typesAreCompatible(Context.Char32Ty, T); |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | enum StringInitFailureKind { |
| 48 | SIF_None, |
| 49 | SIF_NarrowStringIntoWideChar, |
| 50 | SIF_WideStringIntoChar, |
| 51 | SIF_IncompatWideStringIntoWideChar, |
| 52 | SIF_Other |
| 53 | }; |
| 54 | |
| 55 | /// \brief Check whether the array of type AT can be initialized by the Init |
| 56 | /// expression by means of string initialization. Returns SIF_None if so, |
| 57 | /// otherwise returns a StringInitFailureKind that describes why the |
| 58 | /// initialization would not work. |
| 59 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 60 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 61 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 62 | return SIF_Other; |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 64 | // See if this is a string literal or @encode. |
| 65 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 67 | // Handle @encode, which is a narrow string. |
| 68 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 69 | return SIF_None; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 70 | |
| 71 | // Otherwise we can only handle string literals. |
| 72 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 73 | if (SL == 0) |
| 74 | return SIF_Other; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 75 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 76 | const QualType ElemTy = |
| 77 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 78 | |
| 79 | switch (SL->getKind()) { |
| 80 | case StringLiteral::Ascii: |
| 81 | case StringLiteral::UTF8: |
| 82 | // char array can be initialized with a narrow string. |
| 83 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 84 | if (ElemTy->isCharType()) |
| 85 | return SIF_None; |
| 86 | if (IsWideCharCompatible(ElemTy, Context)) |
| 87 | return SIF_NarrowStringIntoWideChar; |
| 88 | return SIF_Other; |
| 89 | // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15: |
| 90 | // "An array with element type compatible with a qualified or unqualified |
| 91 | // version of wchar_t, char16_t, or char32_t may be initialized by a wide |
| 92 | // string literal with the corresponding encoding prefix (L, u, or U, |
| 93 | // respectively), optionally enclosed in braces. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 94 | case StringLiteral::UTF16: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 95 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 96 | return SIF_None; |
| 97 | if (ElemTy->isCharType()) |
| 98 | return SIF_WideStringIntoChar; |
| 99 | if (IsWideCharCompatible(ElemTy, Context)) |
| 100 | return SIF_IncompatWideStringIntoWideChar; |
| 101 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 102 | case StringLiteral::UTF32: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 103 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 104 | return SIF_None; |
| 105 | if (ElemTy->isCharType()) |
| 106 | return SIF_WideStringIntoChar; |
| 107 | if (IsWideCharCompatible(ElemTy, Context)) |
| 108 | return SIF_IncompatWideStringIntoWideChar; |
| 109 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 110 | case StringLiteral::Wide: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 111 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 112 | return SIF_None; |
| 113 | if (ElemTy->isCharType()) |
| 114 | return SIF_WideStringIntoChar; |
| 115 | if (IsWideCharCompatible(ElemTy, Context)) |
| 116 | return SIF_IncompatWideStringIntoWideChar; |
| 117 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 120 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 123 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 124 | ASTContext &Context) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 125 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 126 | if (!arrayType) |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 127 | return SIF_Other; |
| 128 | return IsStringInit(init, arrayType, Context); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 131 | /// Update the type of a string literal, including any surrounding parentheses, |
| 132 | /// to match the type of the object which it is initializing. |
| 133 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 134 | while (true) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 135 | E->setType(Ty); |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 136 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 137 | break; |
| 138 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 139 | E = PE->getSubExpr(); |
| 140 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 141 | E = UO->getSubExpr(); |
| 142 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 143 | E = GSE->getResultExpr(); |
| 144 | else |
| 145 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 146 | } |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 149 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 150 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 151 | // Get the length of the string as parsed. |
| 152 | uint64_t StrLength = |
| 153 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 154 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 156 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 158 | // being initialized to a string literal. |
Benjamin Kramer | 65263b4 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 159 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 160 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 161 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 162 | ConstVal, |
| 163 | ArrayType::Normal, 0); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 164 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 165 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 168 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 170 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 171 | // the size may be smaller or larger than the string we are initializing. |
| 172 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 173 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 174 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 175 | // For Pascal strings it's OK to strip off the terminating null character, |
| 176 | // so the example below is valid: |
| 177 | // |
| 178 | // unsigned char a[2] = "\pa"; |
| 179 | if (SL->isPascal()) |
| 180 | StrLength--; |
| 181 | } |
| 182 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 183 | // [dcl.init.string]p2 |
| 184 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 185 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 186 | diag::err_initializer_string_for_char_array_too_long) |
| 187 | << Str->getSourceRange(); |
| 188 | } else { |
| 189 | // C99 6.7.8p14. |
| 190 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 191 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 192 | diag::warn_initializer_string_for_char_array_too_long) |
| 193 | << Str->getSourceRange(); |
| 194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 196 | // Set the type to the actual size that we are initializing. If we have |
| 197 | // something like: |
| 198 | // char x[1] = "foo"; |
| 199 | // then this will set the string literal's type to char[1]. |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 200 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 203 | //===----------------------------------------------------------------------===// |
| 204 | // Semantic checking for initializer lists. |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 207 | /// @brief Semantic checking for initializer lists. |
| 208 | /// |
| 209 | /// The InitListChecker class contains a set of routines that each |
| 210 | /// handle the initialization of a certain kind of entity, e.g., |
| 211 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 212 | /// InitListChecker itself performs a recursive walk of the subobject |
| 213 | /// structure of the type to be initialized, while stepping through |
| 214 | /// the initializer list one element at a time. The IList and Index |
| 215 | /// parameters to each of the Check* routines contain the active |
| 216 | /// (syntactic) initializer list and the index into that initializer |
| 217 | /// list that represents the current initializer. Each routine is |
| 218 | /// responsible for moving that Index forward as it consumes elements. |
| 219 | /// |
| 220 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 221 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 222 | /// initializer list and the index into that initializer list where we |
| 223 | /// are copying initializers as we map them over to the semantic |
| 224 | /// list. Once we have completed our recursive walk of the subobject |
| 225 | /// structure, we will have constructed a full semantic initializer |
| 226 | /// list. |
| 227 | /// |
| 228 | /// C99 designators cause changes in the initializer list traversal, |
| 229 | /// because they make the initialization "jump" into a specific |
| 230 | /// subobject and then continue the initialization from that |
| 231 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 232 | /// designated subobject and manages backing out the recursion to |
| 233 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 234 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 235 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 236 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 237 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 238 | bool VerifyOnly; // no diagnostics, no structure building |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 239 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 242 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 243 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 244 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 245 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 246 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 247 | InitListExpr *IList, QualType &T, |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 248 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 249 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 250 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 251 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 253 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 254 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 255 | unsigned &StructuredIndex, |
| 256 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 257 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 258 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 259 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 260 | InitListExpr *StructuredList, |
| 261 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 262 | void CheckComplexType(const InitializedEntity &Entity, |
| 263 | InitListExpr *IList, QualType DeclType, |
| 264 | unsigned &Index, |
| 265 | InitListExpr *StructuredList, |
| 266 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 267 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 268 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 269 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 270 | InitListExpr *StructuredList, |
| 271 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 272 | void CheckReferenceType(const InitializedEntity &Entity, |
| 273 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 274 | unsigned &Index, |
| 275 | InitListExpr *StructuredList, |
| 276 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 277 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 278 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 279 | InitListExpr *StructuredList, |
| 280 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 281 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 282 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 284 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 285 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 286 | unsigned &StructuredIndex, |
| 287 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 288 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 289 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 291 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 292 | InitListExpr *StructuredList, |
| 293 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 294 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 295 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 296 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 298 | RecordDecl::field_iterator *NextField, |
| 299 | llvm::APSInt *NextElementIndex, |
| 300 | unsigned &Index, |
| 301 | InitListExpr *StructuredList, |
| 302 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 303 | bool FinishSubobjectInit, |
| 304 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 305 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 306 | QualType CurrentObjectType, |
| 307 | InitListExpr *StructuredList, |
| 308 | unsigned StructuredIndex, |
| 309 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 310 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 311 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 312 | Expr *expr); |
| 313 | int numArrayElements(QualType DeclType); |
| 314 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 315 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 316 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 317 | const InitializedEntity &ParentEntity, |
| 318 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 319 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 320 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 321 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 322 | Expr *InitExpr, FieldDecl *Field, |
| 323 | bool TopLevelObject); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 324 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 325 | |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 326 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 327 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 328 | InitListExpr *IL, QualType &T, bool VerifyOnly); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 329 | bool HadError() { return hadError; } |
| 330 | |
| 331 | // @brief Retrieves the fully-structured initializer list used for |
| 332 | // semantic analysis and code generation. |
| 333 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 334 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 335 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 336 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 337 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 338 | assert(VerifyOnly && |
| 339 | "CheckValueInitializable is only inteded for verification mode."); |
| 340 | |
| 341 | SourceLocation Loc; |
| 342 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 343 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 344 | InitializationSequence InitSeq(SemaRef, Entity, Kind, None); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 345 | if (InitSeq.Failed()) |
| 346 | hadError = true; |
| 347 | } |
| 348 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 349 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 350 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 351 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 352 | bool &RequiresSecondPass) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 353 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 354 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 355 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 356 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 357 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 358 | // If there's no explicit initializer but we have a default initializer, use |
| 359 | // that. This only happens in C++1y, since classes with default |
| 360 | // initializers are not aggregates in C++11. |
| 361 | if (Field->hasInClassInitializer()) { |
| 362 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 363 | ILE->getRBraceLoc(), Field); |
| 364 | if (Init < NumInits) |
| 365 | ILE->setInit(Init, DIE); |
| 366 | else { |
| 367 | ILE->updateInit(SemaRef.Context, Init, DIE); |
| 368 | RequiresSecondPass = true; |
| 369 | } |
| 370 | return; |
| 371 | } |
| 372 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 373 | // FIXME: We probably don't need to handle references |
| 374 | // specially here, since value-initialization of references is |
| 375 | // handled in InitializationSequence. |
| 376 | if (Field->getType()->isReferenceType()) { |
| 377 | // C++ [dcl.init.aggr]p9: |
| 378 | // If an incomplete or empty initializer-list leaves a |
| 379 | // member of reference type uninitialized, the program is |
| 380 | // ill-formed. |
| 381 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 382 | << Field->getType() |
| 383 | << ILE->getSyntacticForm()->getSourceRange(); |
| 384 | SemaRef.Diag(Field->getLocation(), |
| 385 | diag::note_uninit_reference_member); |
| 386 | hadError = true; |
| 387 | return; |
| 388 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 390 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 391 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 392 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 393 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 394 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 395 | hadError = true; |
| 396 | return; |
| 397 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 398 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 399 | ExprResult MemberInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 400 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 401 | if (MemberInit.isInvalid()) { |
| 402 | hadError = true; |
| 403 | return; |
| 404 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 406 | if (hadError) { |
| 407 | // Do nothing |
| 408 | } else if (Init < NumInits) { |
| 409 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 410 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 411 | // Value-initialization requires a constructor call, so |
| 412 | // extend the initializer list to include the constructor |
| 413 | // call and make a note that we'll need to take another pass |
| 414 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 415 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 416 | RequiresSecondPass = true; |
| 417 | } |
| 418 | } else if (InitListExpr *InnerILE |
| 419 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 420 | FillInValueInitializations(MemberEntity, InnerILE, |
| 421 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 424 | /// Recursively replaces NULL values within the given initializer list |
| 425 | /// with expressions that perform value-initialization of the |
| 426 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 427 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 428 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 429 | InitListExpr *ILE, |
| 430 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 432 | "Should not have void type"); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 433 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 434 | if (ILE->getSyntacticForm()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 435 | Loc = ILE->getSyntacticForm()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 437 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 438 | const RecordDecl *RDecl = RType->getDecl(); |
| 439 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 440 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 441 | Entity, ILE, RequiresSecondPass); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 442 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 443 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 444 | for (auto *Field : RDecl->fields()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 445 | if (Field->hasInClassInitializer()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 446 | FillInValueInitForField(0, Field, Entity, ILE, RequiresSecondPass); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } else { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 451 | unsigned Init = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 452 | for (auto *Field : RDecl->fields()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 453 | if (Field->isUnnamedBitfield()) |
| 454 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 455 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 456 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 457 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 458 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 459 | FillInValueInitForField(Init, Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 460 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 461 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 462 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 463 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 464 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 465 | // Only look at the first initialization of a union. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 466 | if (RDecl->isUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 467 | break; |
| 468 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 473 | |
| 474 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 476 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 477 | unsigned NumInits = ILE->getNumInits(); |
| 478 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 479 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 480 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 481 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 482 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 483 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 484 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 485 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 486 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 487 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 488 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 489 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 491 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 493 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 494 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 495 | if (hadError) |
| 496 | return; |
| 497 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 498 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 499 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 500 | ElementEntity.setElementIndex(Init); |
| 501 | |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 502 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0); |
| 503 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 504 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 505 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 506 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 507 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 508 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 509 | hadError = true; |
| 510 | return; |
| 511 | } |
| 512 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 513 | ExprResult ElementInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 514 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 515 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 516 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 517 | return; |
| 518 | } |
| 519 | |
| 520 | if (hadError) { |
| 521 | // Do nothing |
| 522 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 523 | // For arrays, just set the expression used for value-initialization |
| 524 | // of the "holes" in the array. |
| 525 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 526 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 527 | else |
| 528 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 529 | } else { |
| 530 | // For arrays, just set the expression used for value-initialization |
| 531 | // of the rest of elements and exit. |
| 532 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 533 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 534 | return; |
| 535 | } |
| 536 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 537 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 538 | // Value-initialization requires a constructor call, so |
| 539 | // extend the initializer list to include the constructor |
| 540 | // call and make a note that we'll need to take another pass |
| 541 | // through the initializer list. |
| 542 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 543 | RequiresSecondPass = true; |
| 544 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 545 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 546 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 547 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 548 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 549 | } |
| 550 | } |
| 551 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 552 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 553 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 554 | InitListExpr *IL, QualType &T, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 555 | bool VerifyOnly) |
| 556 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 557 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 558 | |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 559 | FullyStructuredList = |
| 560 | getStructuredSubobjectInit(IL, 0, T, 0, 0, IL->getSourceRange()); |
| 561 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 562 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 563 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 564 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 565 | bool RequiresSecondPass = false; |
| 566 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 567 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 568 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 569 | RequiresSecondPass); |
| 570 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 574 | // FIXME: use a proper constant |
| 575 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 576 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 577 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 578 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 579 | } |
| 580 | return maxElements; |
| 581 | } |
| 582 | |
| 583 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 584 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 585 | int InitializableMembers = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 586 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 587 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 588 | ++InitializableMembers; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 589 | |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 590 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 591 | return std::min(InitializableMembers, 1); |
| 592 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 595 | /// Check whether the range of the initializer \p ParentIList from element |
| 596 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 597 | /// \p Index to indicate how many elements of the list were consumed. |
| 598 | /// |
| 599 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 600 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 601 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 602 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 603 | QualType T, unsigned &Index, |
| 604 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 605 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 606 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 607 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 608 | if (T->isArrayType()) |
| 609 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 610 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 611 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 612 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 613 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 614 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 615 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 616 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 617 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 618 | if (!VerifyOnly) |
| 619 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 620 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 621 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 622 | hadError = true; |
| 623 | return; |
| 624 | } |
| 625 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 626 | // Build a structured initializer list corresponding to this subobject. |
| 627 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 628 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 629 | StructuredIndex, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 630 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 631 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 632 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 633 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 634 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 635 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 636 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 637 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 639 | StructuredSubobjectInitIndex); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 640 | |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 641 | if (!VerifyOnly) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 642 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 643 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 644 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 645 | // Update the structured sub-object initializer so that it's ending |
| 646 | // range corresponds with the end of the last initializer it used. |
| 647 | if (EndIndex < ParentIList->getNumInits()) { |
| 648 | SourceLocation EndLoc |
| 649 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 650 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 651 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 652 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 653 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 654 | if (T->isArrayType() || T->isRecordType()) { |
| 655 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 656 | diag::warn_missing_braces) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 657 | << StructuredSubobjectInitList->getSourceRange() |
| 658 | << FixItHint::CreateInsertion( |
| 659 | StructuredSubobjectInitList->getLocStart(), "{") |
| 660 | << FixItHint::CreateInsertion( |
| 661 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 662 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 663 | "}"); |
| 664 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 665 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 668 | /// Check whether the initializer \p IList (that was written with explicit |
| 669 | /// braces) can be used to initialize an object of type \p T. |
| 670 | /// |
| 671 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 672 | /// form of the initialization. |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 673 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 674 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 675 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 676 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 677 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 678 | if (!VerifyOnly) { |
| 679 | SyntacticToSemantic[IList] = StructuredList; |
| 680 | StructuredList->setSyntacticForm(IList); |
| 681 | } |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 682 | |
| 683 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 684 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 685 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 686 | if (!VerifyOnly) { |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 687 | QualType ExprTy = T; |
| 688 | if (!ExprTy->isArrayType()) |
| 689 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 690 | IList->setType(ExprTy); |
| 691 | StructuredList->setType(ExprTy); |
| 692 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 693 | if (hadError) |
| 694 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 695 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 696 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 697 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 698 | if (VerifyOnly) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 699 | if (SemaRef.getLangOpts().CPlusPlus || |
| 700 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 701 | IList->getType()->isVectorType())) { |
| 702 | hadError = true; |
| 703 | } |
| 704 | return; |
| 705 | } |
| 706 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 707 | if (StructuredIndex == 1 && |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 708 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 709 | SIF_None) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 710 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 711 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 712 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 713 | hadError = true; |
| 714 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 715 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 716 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 717 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 718 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 719 | // Don't complain for incomplete types, since we'll get an error |
| 720 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 721 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 723 | CurrentObjectType->isArrayType()? 0 : |
| 724 | CurrentObjectType->isVectorType()? 1 : |
| 725 | CurrentObjectType->isScalarType()? 2 : |
| 726 | CurrentObjectType->isUnionType()? 3 : |
| 727 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 728 | |
| 729 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 730 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 731 | DK = diag::err_excess_initializers; |
| 732 | hadError = true; |
| 733 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 734 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 735 | DK = diag::err_excess_initializers; |
| 736 | hadError = true; |
| 737 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 738 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 739 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 740 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 743 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 744 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 745 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 746 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 747 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 748 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 749 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 752 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 753 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 755 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 756 | unsigned &Index, |
| 757 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 758 | unsigned &StructuredIndex, |
| 759 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 760 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 761 | // Explicitly braced initializer for complex type can be real+imaginary |
| 762 | // parts. |
| 763 | CheckComplexType(Entity, IList, DeclType, Index, |
| 764 | StructuredList, StructuredIndex); |
| 765 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 766 | CheckScalarType(Entity, IList, DeclType, Index, |
| 767 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 768 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 769 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 770 | StructuredList, StructuredIndex); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 771 | } else if (DeclType->isRecordType()) { |
| 772 | assert(DeclType->isAggregateType() && |
| 773 | "non-aggregate records should be handed in CheckSubElementType"); |
| 774 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 775 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 776 | SubobjectIsDesignatorContext, Index, |
| 777 | StructuredList, StructuredIndex, |
| 778 | TopLevelObject); |
| 779 | } else if (DeclType->isArrayType()) { |
| 780 | llvm::APSInt Zero( |
| 781 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 782 | false); |
| 783 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 784 | SubobjectIsDesignatorContext, Index, |
| 785 | StructuredList, StructuredIndex); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 786 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 787 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 788 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 789 | if (!VerifyOnly) |
| 790 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 791 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 792 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 793 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 794 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 795 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 796 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 797 | if (!VerifyOnly) |
| 798 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 799 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 800 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 801 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 802 | if (!VerifyOnly) |
| 803 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 804 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 805 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 809 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 810 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 812 | unsigned &Index, |
| 813 | InitListExpr *StructuredList, |
| 814 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 815 | Expr *expr = IList->getInit(Index); |
Richard Smith | 6242a45 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 816 | |
| 817 | if (ElemType->isReferenceType()) |
| 818 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 819 | StructuredList, StructuredIndex); |
| 820 | |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 821 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 822 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 823 | InitListExpr *InnerStructuredList |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 824 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 825 | StructuredList, StructuredIndex, |
| 826 | SubInitList->getSourceRange()); |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 827 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 828 | InnerStructuredList); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 829 | ++StructuredIndex; |
| 830 | ++Index; |
| 831 | return; |
| 832 | } |
| 833 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 834 | "non-aggregate records are only possible in C++"); |
| 835 | // C++ initialization is handled later. |
| 836 | } |
| 837 | |
Eli Friedman | 48a2a3a | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 838 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
| 839 | if (ElemType->isScalarType() || ElemType->isAtomicType()) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 840 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 841 | StructuredList, StructuredIndex); |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 842 | |
Eli Friedman | 48a2a3a | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 843 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
| 844 | ElemType->isArrayType()) && "Unexpected type"); |
| 845 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 846 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 847 | // arrayType can be incomplete if we're initializing a flexible |
| 848 | // array member. There's nothing we can do with the completed |
| 849 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 850 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 851 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 852 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 853 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 854 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 855 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 856 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 857 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 858 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 859 | |
| 860 | // Fall through for subaggregate initialization. |
| 861 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 862 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 863 | // C++ [dcl.init.aggr]p12: |
| 864 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 865 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 866 | // an initializer-list. If the initializer can initialize a |
| 867 | // member, the member is initialized. [...] |
| 868 | |
| 869 | // FIXME: Better EqualLoc? |
| 870 | InitializationKind Kind = |
| 871 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 872 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 873 | |
| 874 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 875 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 876 | ExprResult Result = |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 877 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 878 | if (Result.isInvalid()) |
| 879 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 880 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 881 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 882 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 883 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 884 | ++Index; |
| 885 | return; |
| 886 | } |
| 887 | |
| 888 | // Fall through for subaggregate initialization |
| 889 | } else { |
| 890 | // C99 6.7.8p13: |
| 891 | // |
| 892 | // The initializer for a structure or union object that has |
| 893 | // automatic storage duration shall be either an initializer |
| 894 | // list as described below, or a single expression that has |
| 895 | // compatible structure or union type. In the latter case, the |
| 896 | // initial value of the object, including unnamed members, is |
| 897 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 898 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 899 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 900 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 901 | !VerifyOnly) |
Eli Friedman | 08f0bbc | 2013-09-17 04:07:04 +0000 | [diff] [blame] | 902 | != Sema::Incompatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 903 | if (ExprRes.isInvalid()) |
| 904 | hadError = true; |
| 905 | else { |
| 906 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 907 | if (ExprRes.isInvalid()) |
| 908 | hadError = true; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 909 | } |
| 910 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 911 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 912 | ++Index; |
| 913 | return; |
| 914 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 915 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 916 | // Fall through for subaggregate initialization |
| 917 | } |
| 918 | |
| 919 | // C++ [dcl.init.aggr]p12: |
| 920 | // |
| 921 | // [...] Otherwise, if the member is itself a non-empty |
| 922 | // subaggregate, brace elision is assumed and the initializer is |
| 923 | // considered for the initialization of the first member of |
| 924 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 925 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 926 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 927 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 928 | StructuredIndex); |
| 929 | ++StructuredIndex; |
| 930 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 931 | if (!VerifyOnly) { |
| 932 | // We cannot initialize this element, so let |
| 933 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 934 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 935 | SemaRef.Owned(expr), |
| 936 | /*TopLevelOfInitList=*/true); |
| 937 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 938 | hadError = true; |
| 939 | ++Index; |
| 940 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 941 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 944 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 945 | InitListExpr *IList, QualType DeclType, |
| 946 | unsigned &Index, |
| 947 | InitListExpr *StructuredList, |
| 948 | unsigned &StructuredIndex) { |
| 949 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 950 | |
| 951 | // As an extension, clang supports complex initializers, which initialize |
| 952 | // a complex number component-wise. When an explicit initializer list for |
| 953 | // a complex number contains two two initializers, this extension kicks in: |
| 954 | // it exepcts the initializer list to contain two elements convertible to |
| 955 | // the element type of the complex type. The first element initializes |
| 956 | // the real part, and the second element intitializes the imaginary part. |
| 957 | |
| 958 | if (IList->getNumInits() != 2) |
| 959 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 960 | StructuredIndex); |
| 961 | |
| 962 | // This is an extension in C. (The builtin _Complex type does not exist |
| 963 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 964 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 965 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 966 | << IList->getSourceRange(); |
| 967 | |
| 968 | // Initialize the complex number. |
| 969 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 970 | InitializedEntity ElementEntity = |
| 971 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 972 | |
| 973 | for (unsigned i = 0; i < 2; ++i) { |
| 974 | ElementEntity.setElementIndex(Index); |
| 975 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 976 | StructuredList, StructuredIndex); |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 981 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 982 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 983 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 984 | InitListExpr *StructuredList, |
| 985 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 986 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 987 | if (!VerifyOnly) |
| 988 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 989 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 990 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 991 | diag::err_empty_scalar_initializer) |
| 992 | << IList->getSourceRange(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 993 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 994 | ++Index; |
| 995 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 996 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 997 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 998 | |
| 999 | Expr *expr = IList->getInit(Index); |
| 1000 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1001 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1002 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1003 | if (!VerifyOnly) |
| 1004 | SemaRef.Diag(SubIList->getLocStart(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1005 | diag::ext_many_braces_around_scalar_init) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1006 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1007 | |
| 1008 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1009 | StructuredIndex); |
| 1010 | return; |
| 1011 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1012 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1013 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1014 | diag::err_designator_for_scalar_init) |
| 1015 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1016 | hadError = true; |
| 1017 | ++Index; |
| 1018 | ++StructuredIndex; |
| 1019 | return; |
| 1020 | } |
| 1021 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1022 | if (VerifyOnly) { |
| 1023 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1024 | hadError = true; |
| 1025 | ++Index; |
| 1026 | return; |
| 1027 | } |
| 1028 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1029 | ExprResult Result = |
| 1030 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1031 | SemaRef.Owned(expr), |
| 1032 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1033 | |
| 1034 | Expr *ResultExpr = 0; |
| 1035 | |
| 1036 | if (Result.isInvalid()) |
| 1037 | hadError = true; // types weren't compatible. |
| 1038 | else { |
| 1039 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1040 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1041 | if (ResultExpr != expr) { |
| 1042 | // The type was promoted, update initializer list. |
| 1043 | IList->setInit(Index, ResultExpr); |
| 1044 | } |
| 1045 | } |
| 1046 | if (hadError) |
| 1047 | ++StructuredIndex; |
| 1048 | else |
| 1049 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1050 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1053 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1054 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1055 | unsigned &Index, |
| 1056 | InitListExpr *StructuredList, |
| 1057 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1058 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1059 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1060 | // general, it would be useful to pass location information down the stack, |
| 1061 | // so that we know the location (or decl) of the "current object" being |
| 1062 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1063 | if (!VerifyOnly) |
| 1064 | SemaRef.Diag(IList->getLocStart(), |
| 1065 | diag::err_init_reference_member_uninitialized) |
| 1066 | << DeclType |
| 1067 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1068 | hadError = true; |
| 1069 | ++Index; |
| 1070 | ++StructuredIndex; |
| 1071 | return; |
| 1072 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1073 | |
| 1074 | Expr *expr = IList->getInit(Index); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1075 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1076 | if (!VerifyOnly) |
| 1077 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1078 | << DeclType << IList->getSourceRange(); |
| 1079 | hadError = true; |
| 1080 | ++Index; |
| 1081 | ++StructuredIndex; |
| 1082 | return; |
| 1083 | } |
| 1084 | |
| 1085 | if (VerifyOnly) { |
| 1086 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1087 | hadError = true; |
| 1088 | ++Index; |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | ExprResult Result = |
| 1093 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1094 | SemaRef.Owned(expr), |
| 1095 | /*TopLevelOfInitList=*/true); |
| 1096 | |
| 1097 | if (Result.isInvalid()) |
| 1098 | hadError = true; |
| 1099 | |
| 1100 | expr = Result.takeAs<Expr>(); |
| 1101 | IList->setInit(Index, expr); |
| 1102 | |
| 1103 | if (hadError) |
| 1104 | ++StructuredIndex; |
| 1105 | else |
| 1106 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1107 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1110 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1111 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1112 | unsigned &Index, |
| 1113 | InitListExpr *StructuredList, |
| 1114 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1115 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1116 | unsigned maxElements = VT->getNumElements(); |
| 1117 | unsigned numEltsInit = 0; |
| 1118 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1119 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1120 | if (Index >= IList->getNumInits()) { |
| 1121 | // Make sure the element type can be value-initialized. |
| 1122 | if (VerifyOnly) |
| 1123 | CheckValueInitializable( |
| 1124 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1125 | return; |
| 1126 | } |
| 1127 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1128 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1129 | // If the initializing element is a vector, try to copy-initialize |
| 1130 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1131 | Expr *Init = IList->getInit(Index); |
| 1132 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1133 | if (VerifyOnly) { |
| 1134 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1135 | hadError = true; |
| 1136 | ++Index; |
| 1137 | return; |
| 1138 | } |
| 1139 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1140 | ExprResult Result = |
| 1141 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1142 | SemaRef.Owned(Init), |
| 1143 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1144 | |
| 1145 | Expr *ResultExpr = 0; |
| 1146 | if (Result.isInvalid()) |
| 1147 | hadError = true; // types weren't compatible. |
| 1148 | else { |
| 1149 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1150 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1151 | if (ResultExpr != Init) { |
| 1152 | // The type was promoted, update initializer list. |
| 1153 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1154 | } |
| 1155 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1156 | if (hadError) |
| 1157 | ++StructuredIndex; |
| 1158 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1159 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1160 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1161 | ++Index; |
| 1162 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1163 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1164 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1165 | InitializedEntity ElementEntity = |
| 1166 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1167 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1168 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1169 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1170 | if (Index >= IList->getNumInits()) { |
| 1171 | if (VerifyOnly) |
| 1172 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1173 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1174 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1175 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1176 | ElementEntity.setElementIndex(Index); |
| 1177 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1178 | StructuredList, StructuredIndex); |
| 1179 | } |
| 1180 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1181 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1182 | |
| 1183 | InitializedEntity ElementEntity = |
| 1184 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1185 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1186 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1187 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1188 | // Don't attempt to go past the end of the init list |
| 1189 | if (Index >= IList->getNumInits()) |
| 1190 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1191 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1192 | ElementEntity.setElementIndex(Index); |
| 1193 | |
| 1194 | QualType IType = IList->getInit(Index)->getType(); |
| 1195 | if (!IType->isVectorType()) { |
| 1196 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1197 | StructuredList, StructuredIndex); |
| 1198 | ++numEltsInit; |
| 1199 | } else { |
| 1200 | QualType VecType; |
| 1201 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1202 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1203 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1204 | if (IType->isExtVectorType()) |
| 1205 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1206 | else |
| 1207 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1208 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1209 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1210 | StructuredList, StructuredIndex); |
| 1211 | numEltsInit += numIElts; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1216 | if (numEltsInit != maxElements) { |
| 1217 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1218 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1219 | diag::err_vector_incorrect_num_initializers) |
| 1220 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1221 | hadError = true; |
| 1222 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1225 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1226 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1227 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1228 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1229 | unsigned &Index, |
| 1230 | InitListExpr *StructuredList, |
| 1231 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1232 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1233 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1234 | // Check for the special-case of initializing an array with a string. |
| 1235 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1236 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1237 | SIF_None) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1238 | // We place the string literal directly into the resulting |
| 1239 | // initializer list. This is the only place where the structure |
| 1240 | // of the structured initializer list doesn't match exactly, |
| 1241 | // because doing so would involve allocating one character |
| 1242 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1243 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1244 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1245 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1246 | IList->getInit(Index)); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1247 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1248 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1249 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1250 | return; |
| 1251 | } |
| 1252 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1253 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1254 | // Check for VLAs; in standard C it would be possible to check this |
| 1255 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1256 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1257 | if (!VerifyOnly) |
| 1258 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1259 | diag::err_variable_object_no_init) |
| 1260 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1261 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1262 | ++Index; |
| 1263 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1264 | return; |
| 1265 | } |
| 1266 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1267 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1268 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1269 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1270 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1271 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1272 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1273 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1274 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1275 | maxElementsKnown = true; |
| 1276 | } |
| 1277 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1278 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1279 | while (Index < IList->getNumInits()) { |
| 1280 | Expr *Init = IList->getInit(Index); |
| 1281 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1282 | // If we're not the subobject that matches up with the '{' for |
| 1283 | // the designator, we shouldn't be handling the |
| 1284 | // designator. Return immediately. |
| 1285 | if (!SubobjectIsDesignatorContext) |
| 1286 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1287 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1288 | // Handle this designated initializer. elementIndex will be |
| 1289 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1290 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1291 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1292 | StructuredList, StructuredIndex, true, |
| 1293 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1294 | hadError = true; |
| 1295 | continue; |
| 1296 | } |
| 1297 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1298 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1299 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1300 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1301 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1302 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1303 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1304 | // If the array is of incomplete type, keep track of the number of |
| 1305 | // elements in the initializer. |
| 1306 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1307 | maxElements = elementIndex; |
| 1308 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1309 | continue; |
| 1310 | } |
| 1311 | |
| 1312 | // If we know the maximum number of elements, and we've already |
| 1313 | // hit it, stop consuming elements in the initializer list. |
| 1314 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1315 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1316 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1317 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1318 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1319 | Entity); |
| 1320 | // Check this element. |
| 1321 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1322 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1323 | ++elementIndex; |
| 1324 | |
| 1325 | // If the array is of incomplete type, keep track of the number of |
| 1326 | // elements in the initializer. |
| 1327 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1328 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1329 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1330 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1331 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1332 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1333 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1334 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1335 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1336 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1337 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1338 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1339 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1340 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1341 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1342 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1343 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1344 | if (!hadError && VerifyOnly) { |
| 1345 | // Check if there are any members of the array that get value-initialized. |
| 1346 | // If so, check if doing that is possible. |
| 1347 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1348 | if (maxElementsKnown && elementIndex < maxElements) |
| 1349 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1350 | SemaRef.Context, 0, Entity)); |
| 1351 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1354 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1355 | Expr *InitExpr, |
| 1356 | FieldDecl *Field, |
| 1357 | bool TopLevelObject) { |
| 1358 | // Handle GNU flexible array initializers. |
| 1359 | unsigned FlexArrayDiag; |
| 1360 | if (isa<InitListExpr>(InitExpr) && |
| 1361 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1362 | // Empty flexible array init always allowed as an extension |
| 1363 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1364 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1365 | // Disallow flexible array init in C++; it is not required for gcc |
| 1366 | // compatibility, and it needs work to IRGen correctly in general. |
| 1367 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1368 | } else if (!TopLevelObject) { |
| 1369 | // Disallow flexible array init on non-top-level object |
| 1370 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1371 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1372 | // Disallow flexible array init on anything which is not a variable. |
| 1373 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1374 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1375 | // Disallow flexible array init on local variables. |
| 1376 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1377 | } else { |
| 1378 | // Allow other cases. |
| 1379 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1380 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1381 | |
| 1382 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1383 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1384 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1385 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1386 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1387 | << Field; |
| 1388 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1389 | |
| 1390 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1391 | } |
| 1392 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1393 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1394 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1396 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1397 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1398 | unsigned &Index, |
| 1399 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1400 | unsigned &StructuredIndex, |
| 1401 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1402 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1403 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1404 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1405 | // confusion, we forgo checking the intializer for the entire record. |
| 1406 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 72ab277 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1407 | // Assume it was supposed to consume a single initializer. |
| 1408 | ++Index; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1409 | hadError = true; |
| 1410 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1411 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1414 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1415 | |
| 1416 | // If there's a default initializer, use it. |
| 1417 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1418 | if (VerifyOnly) |
| 1419 | return; |
| 1420 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1421 | Field != FieldEnd; ++Field) { |
| 1422 | if (Field->hasInClassInitializer()) { |
| 1423 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1424 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1425 | return; |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | |
| 1430 | // Value-initialize the first named member of the union. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1431 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1432 | Field != FieldEnd; ++Field) { |
| 1433 | if (Field->getDeclName()) { |
| 1434 | if (VerifyOnly) |
| 1435 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1436 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1437 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1438 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1439 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | return; |
| 1443 | } |
| 1444 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1445 | // If structDecl is a forward declaration, this loop won't do |
| 1446 | // anything except look at designated initializers; That's okay, |
| 1447 | // because an error should get printed out elsewhere. It might be |
| 1448 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1449 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1450 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1451 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1452 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1453 | while (Index < IList->getNumInits()) { |
| 1454 | Expr *Init = IList->getInit(Index); |
| 1455 | |
| 1456 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1457 | // If we're not the subobject that matches up with the '{' for |
| 1458 | // the designator, we shouldn't be handling the |
| 1459 | // designator. Return immediately. |
| 1460 | if (!SubobjectIsDesignatorContext) |
| 1461 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1462 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1463 | // Handle this designated initializer. Field will be updated to |
| 1464 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1465 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1466 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1467 | StructuredList, StructuredIndex, |
| 1468 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1469 | hadError = true; |
| 1470 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1471 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Disable check for missing fields when designators are used. |
| 1474 | // This matches gcc behaviour. |
| 1475 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1476 | continue; |
| 1477 | } |
| 1478 | |
| 1479 | if (Field == FieldEnd) { |
| 1480 | // We've run out of fields. We're done. |
| 1481 | break; |
| 1482 | } |
| 1483 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1484 | // We've already initialized a member of a union. We're done. |
| 1485 | if (InitializedSomething && DeclType->isUnionType()) |
| 1486 | break; |
| 1487 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1488 | // If we've hit the flexible array member at the end, we're done. |
| 1489 | if (Field->getType()->isIncompleteArrayType()) |
| 1490 | break; |
| 1491 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1492 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1493 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1494 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1495 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1496 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1497 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1498 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1499 | bool InvalidUse; |
| 1500 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1501 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1502 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1503 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1504 | IList->getInit(Index)->getLocStart()); |
| 1505 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1506 | ++Index; |
| 1507 | ++Field; |
| 1508 | hadError = true; |
| 1509 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1510 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1511 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1512 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1513 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1514 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1515 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1516 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1517 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1518 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1519 | // Initialize the first field within the union. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1520 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1521 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1522 | |
| 1523 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1524 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1525 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1526 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1527 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1528 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1529 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1530 | // It is possible we have one or more unnamed bitfields remaining. |
| 1531 | // Find first (if any) named field and emit warning. |
| 1532 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1533 | it != end; ++it) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1534 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1535 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1536 | diag::warn_missing_field_initializers) << *it; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1537 | break; |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1542 | // Check that any remaining fields can be value-initialized. |
| 1543 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1544 | !Field->getType()->isIncompleteArrayType()) { |
| 1545 | // FIXME: Should check for holes left by designated initializers too. |
| 1546 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1547 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1548 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1549 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1550 | } |
| 1551 | } |
| 1552 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1553 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1554 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1555 | return; |
| 1556 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1557 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1558 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1559 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1560 | ++Index; |
| 1561 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1564 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1565 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1566 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1567 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1568 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1569 | StructuredList, StructuredIndex); |
| 1570 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1571 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1572 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1573 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1574 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1575 | /// \brief Expand a field designator that refers to a member of an |
| 1576 | /// anonymous struct or union into a series of field designators that |
| 1577 | /// refers to the field within the appropriate subobject. |
| 1578 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1579 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | DesignatedInitExpr *DIE, |
| 1581 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1582 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1583 | typedef DesignatedInitExpr::Designator Designator; |
| 1584 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1585 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1586 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1587 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1588 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1589 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1590 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1591 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1592 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1593 | else |
| 1594 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1595 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1596 | assert(isa<FieldDecl>(*PI)); |
| 1597 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | // Expand the current designator into the set of replacement |
| 1601 | // designators, so we have a full subobject path down to where the |
| 1602 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1603 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1604 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1605 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1606 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1607 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1608 | /// corresponds to FieldName. |
| 1609 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1610 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1611 | if (!FieldName) |
| 1612 | return 0; |
| 1613 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1614 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1615 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1616 | while (IndirectFieldDecl *IF = |
| 1617 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1618 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1619 | return IF; |
| 1620 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1621 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1622 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1625 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1626 | DesignatedInitExpr *DIE) { |
| 1627 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1628 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1629 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1630 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1631 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1632 | DIE->size(), IndexExprs, |
| 1633 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1634 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1635 | } |
| 1636 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1637 | namespace { |
| 1638 | |
| 1639 | // Callback to only accept typo corrections that are for field members of |
| 1640 | // the given struct or union. |
| 1641 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1642 | public: |
| 1643 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1644 | : Record(RD) {} |
| 1645 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1646 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1647 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1648 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1649 | } |
| 1650 | |
| 1651 | private: |
| 1652 | RecordDecl *Record; |
| 1653 | }; |
| 1654 | |
| 1655 | } |
| 1656 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1657 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1658 | /// |
| 1659 | /// Determines whether the designated initializer @p DIE, which |
| 1660 | /// resides at the given @p Index within the initializer list @p |
| 1661 | /// IList, is well-formed for a current object of type @p DeclType |
| 1662 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1663 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1664 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1665 | /// |
| 1666 | /// @param IList The initializer list in which this designated |
| 1667 | /// initializer occurs. |
| 1668 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1669 | /// @param DIE The designated initializer expression. |
| 1670 | /// |
| 1671 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1672 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1673 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1674 | /// into which the designation in @p DIE should refer. |
| 1675 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1676 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1677 | /// a field, this will be set to the field declaration corresponding |
| 1678 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1679 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1680 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1681 | /// DIE is an array designator or GNU array-range designator, this |
| 1682 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1683 | /// |
| 1684 | /// @param Index Index into @p IList where the designated initializer |
| 1685 | /// @p DIE occurs. |
| 1686 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1687 | /// @param StructuredList The initializer list expression that |
| 1688 | /// describes all of the subobject initializers in the order they'll |
| 1689 | /// actually be initialized. |
| 1690 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1691 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1692 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1693 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1694 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1695 | DesignatedInitExpr *DIE, |
| 1696 | unsigned DesigIdx, |
| 1697 | QualType &CurrentObjectType, |
| 1698 | RecordDecl::field_iterator *NextField, |
| 1699 | llvm::APSInt *NextElementIndex, |
| 1700 | unsigned &Index, |
| 1701 | InitListExpr *StructuredList, |
| 1702 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1703 | bool FinishSubobjectInit, |
| 1704 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1705 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1706 | // Check the actual initialization for the designated object type. |
| 1707 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1708 | |
| 1709 | // Temporarily remove the designator expression from the |
| 1710 | // initializer list that the child calls see, so that we don't try |
| 1711 | // to re-process the designator. |
| 1712 | unsigned OldIndex = Index; |
| 1713 | IList->setInit(OldIndex, DIE->getInit()); |
| 1714 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1715 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1716 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1717 | |
| 1718 | // Restore the designated initializer expression in the syntactic |
| 1719 | // form of the initializer list. |
| 1720 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1721 | DIE->setInit(IList->getInit(OldIndex)); |
| 1722 | IList->setInit(OldIndex, DIE); |
| 1723 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1724 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1727 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1728 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1729 | if (!VerifyOnly) { |
| 1730 | assert((IsFirstDesignator || StructuredList) && |
| 1731 | "Need a non-designated initializer list to start from"); |
| 1732 | |
| 1733 | // Determine the structural initializer list that corresponds to the |
| 1734 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1735 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1736 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1737 | StructuredList, StructuredIndex, |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1738 | SourceRange(D->getLocStart(), |
| 1739 | DIE->getLocEnd())); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1740 | assert(StructuredList && "Expected a structured initializer list"); |
| 1741 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1742 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1743 | if (D->isFieldDesignator()) { |
| 1744 | // C99 6.7.8p7: |
| 1745 | // |
| 1746 | // If a designator has the form |
| 1747 | // |
| 1748 | // . identifier |
| 1749 | // |
| 1750 | // then the current object (defined below) shall have |
| 1751 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1752 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1753 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1754 | if (!RT) { |
| 1755 | SourceLocation Loc = D->getDotLoc(); |
| 1756 | if (Loc.isInvalid()) |
| 1757 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1758 | if (!VerifyOnly) |
| 1759 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1760 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1761 | ++Index; |
| 1762 | return true; |
| 1763 | } |
| 1764 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1765 | // Note: we perform a linear search of the fields here, despite |
| 1766 | // the fact that we have a faster lookup method, because we always |
| 1767 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1768 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1769 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1770 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1772 | Field = RT->getDecl()->field_begin(), |
| 1773 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1774 | for (; Field != FieldEnd; ++Field) { |
| 1775 | if (Field->isUnnamedBitfield()) |
| 1776 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1777 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1778 | // If we find a field representing an anonymous field, look in the |
| 1779 | // IndirectFieldDecl that follow for the designated initializer. |
| 1780 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1781 | if (IndirectFieldDecl *IF = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1782 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1783 | // In verify mode, don't modify the original. |
| 1784 | if (VerifyOnly) |
| 1785 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1786 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1787 | D = DIE->getDesignator(DesigIdx); |
| 1788 | break; |
| 1789 | } |
| 1790 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1791 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1792 | break; |
| 1793 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1794 | break; |
| 1795 | |
| 1796 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1799 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1800 | if (VerifyOnly) { |
| 1801 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1802 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1803 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1804 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1805 | // There was no normal field in the struct with the designated |
| 1806 | // name. Perform another lookup for this name, which may find |
| 1807 | // something that we can't designate (e.g., a member function), |
| 1808 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1809 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1810 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1811 | FieldDecl *ReplacementField = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1812 | if (Lookup.empty()) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1813 | // Name lookup didn't find anything. Determine whether this |
| 1814 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1815 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1816 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1817 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1818 | Sema::LookupMemberName, /*Scope=*/ 0, /*SS=*/ 0, Validator, |
| 1819 | RT->getDecl())) { |
| 1820 | SemaRef.diagnoseTypo( |
| 1821 | Corrected, |
| 1822 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
| 1823 | << FieldName << CurrentObjectType); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1824 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1825 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1826 | } else { |
| 1827 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1828 | << FieldName << CurrentObjectType; |
| 1829 | ++Index; |
| 1830 | return true; |
| 1831 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1832 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1833 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1834 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1835 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1836 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1837 | << FieldName; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1838 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1839 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1840 | ++Index; |
| 1841 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1842 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1843 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1844 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1845 | // The replacement field comes from typo correction; find it |
| 1846 | // in the list of fields. |
| 1847 | FieldIndex = 0; |
| 1848 | Field = RT->getDecl()->field_begin(); |
| 1849 | for (; Field != FieldEnd; ++Field) { |
| 1850 | if (Field->isUnnamedBitfield()) |
| 1851 | continue; |
| 1852 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1853 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1854 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1855 | break; |
| 1856 | |
| 1857 | ++FieldIndex; |
| 1858 | } |
| 1859 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1860 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1861 | |
| 1862 | // All of the fields of a union are located at the same place in |
| 1863 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1864 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1865 | FieldIndex = 0; |
Matthew Curtis | 4e49952 | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1866 | if (!VerifyOnly) { |
| 1867 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
| 1868 | if (CurrentField && CurrentField != *Field) { |
| 1869 | assert(StructuredList->getNumInits() == 1 |
| 1870 | && "A union should never have more than one initializer!"); |
| 1871 | |
| 1872 | // we're about to throw away an initializer, emit warning |
| 1873 | SemaRef.Diag(D->getFieldLoc(), |
| 1874 | diag::warn_initializer_overrides) |
| 1875 | << D->getSourceRange(); |
| 1876 | Expr *ExistingInit = StructuredList->getInit(0); |
| 1877 | SemaRef.Diag(ExistingInit->getLocStart(), |
| 1878 | diag::note_previous_initializer) |
| 1879 | << /*FIXME:has side effects=*/0 |
| 1880 | << ExistingInit->getSourceRange(); |
| 1881 | |
| 1882 | // remove existing initializer |
| 1883 | StructuredList->resizeInits(SemaRef.Context, 0); |
| 1884 | StructuredList->setInitializedFieldInUnion(0); |
| 1885 | } |
| 1886 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1887 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 4e49952 | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1888 | } |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1889 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1890 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1891 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1892 | bool InvalidUse; |
| 1893 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1894 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1895 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1896 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1897 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1898 | ++Index; |
| 1899 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1900 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1901 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1902 | if (!VerifyOnly) { |
| 1903 | // Update the designator with the field declaration. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1904 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1905 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1906 | // Make sure that our non-designated initializer list has space |
| 1907 | // for a subobject corresponding to this field. |
| 1908 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1909 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1910 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1911 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1912 | // This designator names a flexible array member. |
| 1913 | if (Field->getType()->isIncompleteArrayType()) { |
| 1914 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1915 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1916 | // We can't designate an object within the flexible array |
| 1917 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1918 | if (!VerifyOnly) { |
| 1919 | DesignatedInitExpr::Designator *NextD |
| 1920 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1921 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1922 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1923 | << SourceRange(NextD->getLocStart(), |
| 1924 | DIE->getLocEnd()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1925 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1926 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1927 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1928 | Invalid = true; |
| 1929 | } |
| 1930 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1931 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1932 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1933 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1934 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1935 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1936 | diag::err_flexible_array_init_needs_braces) |
| 1937 | << DIE->getInit()->getSourceRange(); |
| 1938 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1939 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1940 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1941 | Invalid = true; |
| 1942 | } |
| 1943 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1944 | // Check GNU flexible array initializer. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1945 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1946 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1947 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1948 | |
| 1949 | if (Invalid) { |
| 1950 | ++Index; |
| 1951 | return true; |
| 1952 | } |
| 1953 | |
| 1954 | // Initialize the array. |
| 1955 | bool prevHadError = hadError; |
| 1956 | unsigned newStructuredIndex = FieldIndex; |
| 1957 | unsigned OldIndex = Index; |
| 1958 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1959 | |
| 1960 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1961 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1962 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1963 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1965 | IList->setInit(OldIndex, DIE); |
| 1966 | if (hadError && !prevHadError) { |
| 1967 | ++Field; |
| 1968 | ++FieldIndex; |
| 1969 | if (NextField) |
| 1970 | *NextField = Field; |
| 1971 | StructuredIndex = FieldIndex; |
| 1972 | return true; |
| 1973 | } |
| 1974 | } else { |
| 1975 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1976 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1977 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1978 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1979 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1980 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1981 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1982 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1983 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1984 | true, false)) |
| 1985 | return true; |
| 1986 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1987 | |
| 1988 | // Find the position of the next field to be initialized in this |
| 1989 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1990 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1991 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1992 | |
| 1993 | // If this the first designator, our caller will continue checking |
| 1994 | // the rest of this struct/class/union subobject. |
| 1995 | if (IsFirstDesignator) { |
| 1996 | if (NextField) |
| 1997 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1998 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1999 | return false; |
| 2000 | } |
| 2001 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2002 | if (!FinishSubobjectInit) |
| 2003 | return false; |
| 2004 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2005 | // We've already initialized something in the union; we're done. |
| 2006 | if (RT->getDecl()->isUnion()) |
| 2007 | return hadError; |
| 2008 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2009 | // Check the remaining fields within this class/struct/union subobject. |
| 2010 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2011 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2012 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2013 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2014 | return hadError && !prevHadError; |
| 2015 | } |
| 2016 | |
| 2017 | // C99 6.7.8p6: |
| 2018 | // |
| 2019 | // If a designator has the form |
| 2020 | // |
| 2021 | // [ constant-expression ] |
| 2022 | // |
| 2023 | // then the current object (defined below) shall have array |
| 2024 | // type and the expression shall be an integer constant |
| 2025 | // expression. If the array is of unknown size, any |
| 2026 | // nonnegative value is valid. |
| 2027 | // |
| 2028 | // Additionally, cope with the GNU extension that permits |
| 2029 | // designators of the form |
| 2030 | // |
| 2031 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2032 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2033 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2034 | if (!VerifyOnly) |
| 2035 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2036 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2037 | ++Index; |
| 2038 | return true; |
| 2039 | } |
| 2040 | |
| 2041 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2042 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2043 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2044 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2045 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2046 | DesignatedEndIndex = DesignatedStartIndex; |
| 2047 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2048 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2049 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2050 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2051 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2053 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2054 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2055 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2056 | // Codegen can't handle evaluating array range designators that have side |
| 2057 | // effects, because we replicate the AST value for each initialized element. |
| 2058 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2059 | // elements with something that has a side effect, so codegen can emit an |
| 2060 | // "error unsupported" error instead of miscompiling the app. |
| 2061 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2062 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2063 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2064 | } |
| 2065 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2066 | if (isa<ConstantArrayType>(AT)) { |
| 2067 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2068 | DesignatedStartIndex |
| 2069 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2070 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2071 | DesignatedEndIndex |
| 2072 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2073 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2074 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2075 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2076 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2077 | diag::err_array_designator_too_large) |
| 2078 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2079 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2080 | ++Index; |
| 2081 | return true; |
| 2082 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2083 | } else { |
| 2084 | // Make sure the bit-widths and signedness match. |
| 2085 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2086 | DesignatedEndIndex |
| 2087 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2088 | else if (DesignatedStartIndex.getBitWidth() < |
| 2089 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2090 | DesignatedStartIndex |
| 2091 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2092 | DesignatedStartIndex.setIsUnsigned(true); |
| 2093 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2094 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2095 | |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2096 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2097 | // We're modifying a string literal init; we have to decompose the string |
| 2098 | // so we can modify the individual characters. |
| 2099 | ASTContext &Context = SemaRef.Context; |
| 2100 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2101 | |
| 2102 | // Compute the character type |
| 2103 | QualType CharTy = AT->getElementType(); |
| 2104 | |
| 2105 | // Compute the type of the integer literals. |
| 2106 | QualType PromotedCharTy = CharTy; |
| 2107 | if (CharTy->isPromotableIntegerType()) |
| 2108 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2109 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2110 | |
| 2111 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2112 | // Get the length of the string. |
| 2113 | uint64_t StrLen = SL->getLength(); |
| 2114 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2115 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2116 | StructuredList->resizeInits(Context, StrLen); |
| 2117 | |
| 2118 | // Build a literal for each character in the string, and put them into |
| 2119 | // the init list. |
| 2120 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2121 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2122 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 81359b0 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2123 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2124 | if (CharTy != PromotedCharTy) |
| 2125 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
| 2126 | Init, 0, VK_RValue); |
| 2127 | StructuredList->updateInit(Context, i, Init); |
| 2128 | } |
| 2129 | } else { |
| 2130 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2131 | std::string Str; |
| 2132 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2133 | |
| 2134 | // Get the length of the string. |
| 2135 | uint64_t StrLen = Str.size(); |
| 2136 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2137 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2138 | StructuredList->resizeInits(Context, StrLen); |
| 2139 | |
| 2140 | // Build a literal for each character in the string, and put them into |
| 2141 | // the init list. |
| 2142 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2143 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2144 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 81359b0 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2145 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2146 | if (CharTy != PromotedCharTy) |
| 2147 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
| 2148 | Init, 0, VK_RValue); |
| 2149 | StructuredList->updateInit(Context, i, Init); |
| 2150 | } |
| 2151 | } |
| 2152 | } |
| 2153 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2154 | // Make sure that our non-designated initializer list has space |
| 2155 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2156 | if (!VerifyOnly && |
| 2157 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2158 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2159 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2160 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2161 | // Repeatedly perform subobject initializations in the range |
| 2162 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2163 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2164 | // Move to the next designator |
| 2165 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2166 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2167 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2168 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2169 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2170 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2171 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2172 | // Recurse to check later designated subobjects. |
| 2173 | QualType ElementType = AT->getElementType(); |
| 2174 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2175 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2176 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2177 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 2178 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2179 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2180 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2181 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2182 | return true; |
| 2183 | |
| 2184 | // Move to the next index in the array that we'll be initializing. |
| 2185 | ++DesignatedStartIndex; |
| 2186 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2187 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2188 | |
| 2189 | // If this the first designator, our caller will continue checking |
| 2190 | // the rest of this array subobject. |
| 2191 | if (IsFirstDesignator) { |
| 2192 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2193 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2194 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2195 | return false; |
| 2196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2198 | if (!FinishSubobjectInit) |
| 2199 | return false; |
| 2200 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2201 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2202 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2203 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2204 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2205 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2209 | // Get the structured initializer list for a subobject of type |
| 2210 | // @p CurrentObjectType. |
| 2211 | InitListExpr * |
| 2212 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2213 | QualType CurrentObjectType, |
| 2214 | InitListExpr *StructuredList, |
| 2215 | unsigned StructuredIndex, |
| 2216 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2217 | if (VerifyOnly) |
| 2218 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2219 | Expr *ExistingInit = 0; |
| 2220 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2221 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2222 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2223 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2225 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2226 | return Result; |
| 2227 | |
| 2228 | if (ExistingInit) { |
| 2229 | // We are creating an initializer list that initializes the |
| 2230 | // subobjects of the current object, but there was already an |
| 2231 | // initialization that completely initialized the current |
| 2232 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2234 | // struct X { int a, b; }; |
| 2235 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2237 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2238 | // designated initializer re-initializes the whole |
| 2239 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2240 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2241 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2242 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2243 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2244 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2245 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2246 | << ExistingInit->getSourceRange(); |
| 2247 | } |
| 2248 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2249 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2250 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2251 | InitRange.getBegin(), None, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2252 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2253 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2254 | QualType ResultType = CurrentObjectType; |
| 2255 | if (!ResultType->isArrayType()) |
| 2256 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2257 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2258 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2259 | // Pre-allocate storage for the structured initializer list. |
| 2260 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2261 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2262 | bool GotNumInits = false; |
| 2263 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2264 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2265 | GotNumInits = true; |
| 2266 | } else if (Index < IList->getNumInits()) { |
| 2267 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2268 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2269 | GotNumInits = true; |
| 2270 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2274 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2275 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2276 | NumElements = CAType->getSize().getZExtValue(); |
| 2277 | // Simple heuristic so that we don't allocate a very large |
| 2278 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2279 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2280 | NumElements = 0; |
| 2281 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2282 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2283 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2284 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2285 | RecordDecl *RDecl = RType->getDecl(); |
| 2286 | if (RDecl->isUnion()) |
| 2287 | NumElements = 1; |
| 2288 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2289 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2292 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2293 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2294 | // Link this new initializer list into the structured initializer |
| 2295 | // lists. |
| 2296 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2297 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2298 | else { |
| 2299 | Result->setSyntacticForm(IList); |
| 2300 | SyntacticToSemantic[IList] = Result; |
| 2301 | } |
| 2302 | |
| 2303 | return Result; |
| 2304 | } |
| 2305 | |
| 2306 | /// Update the initializer at index @p StructuredIndex within the |
| 2307 | /// structured initializer list to the value @p expr. |
| 2308 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2309 | unsigned &StructuredIndex, |
| 2310 | Expr *expr) { |
| 2311 | // No structured initializer list to update |
| 2312 | if (!StructuredList) |
| 2313 | return; |
| 2314 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2315 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2316 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2317 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2318 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2319 | diag::warn_initializer_overrides) |
| 2320 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2321 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2322 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2323 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2324 | << PrevInit->getSourceRange(); |
| 2325 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2326 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2327 | ++StructuredIndex; |
| 2328 | } |
| 2329 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2330 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2331 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2332 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2333 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2334 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2335 | /// added, on success. If everything went okay, Value will receive the |
| 2336 | /// value of the constant expression. |
| 2337 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2338 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2339 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2340 | |
| 2341 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2342 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2343 | if (Result.isInvalid()) |
| 2344 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2345 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2346 | if (Value.isSigned() && Value.isNegative()) |
| 2347 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2348 | << Value.toString(10) << Index->getSourceRange(); |
| 2349 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2350 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2351 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2354 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2355 | SourceLocation Loc, |
| 2356 | bool GNUSyntax, |
| 2357 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2358 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2359 | |
| 2360 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2361 | SmallVector<ASTDesignator, 32> Designators; |
| 2362 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2363 | |
| 2364 | // Build designators and check array designator expressions. |
| 2365 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2366 | const Designator &D = Desig.getDesignator(Idx); |
| 2367 | switch (D.getKind()) { |
| 2368 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2370 | D.getFieldLoc())); |
| 2371 | break; |
| 2372 | |
| 2373 | case Designator::ArrayDesignator: { |
| 2374 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2375 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2376 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2377 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2378 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2379 | Invalid = true; |
| 2380 | else { |
| 2381 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2382 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2383 | D.getRBracketLoc())); |
| 2384 | InitExpressions.push_back(Index); |
| 2385 | } |
| 2386 | break; |
| 2387 | } |
| 2388 | |
| 2389 | case Designator::ArrayRangeDesignator: { |
| 2390 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2391 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2392 | llvm::APSInt StartValue; |
| 2393 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2394 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2395 | StartIndex->isValueDependent(); |
| 2396 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2397 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2398 | if (!StartDependent) |
| 2399 | StartIndex = |
| 2400 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2401 | if (!EndDependent) |
| 2402 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2403 | |
| 2404 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2405 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2406 | else { |
| 2407 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2408 | if (StartDependent || EndDependent) { |
| 2409 | // Nothing to compute. |
| 2410 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2411 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2412 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2413 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2414 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2415 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2416 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2417 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2418 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2419 | Invalid = true; |
| 2420 | } else { |
| 2421 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2422 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2423 | D.getEllipsisLoc(), |
| 2424 | D.getRBracketLoc())); |
| 2425 | InitExpressions.push_back(StartIndex); |
| 2426 | InitExpressions.push_back(EndIndex); |
| 2427 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2428 | } |
| 2429 | break; |
| 2430 | } |
| 2431 | } |
| 2432 | } |
| 2433 | |
| 2434 | if (Invalid || Init.isInvalid()) |
| 2435 | return ExprError(); |
| 2436 | |
| 2437 | // Clear out the expressions within the designation. |
| 2438 | Desig.ClearExprs(*this); |
| 2439 | |
| 2440 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2441 | = DesignatedInitExpr::Create(Context, |
| 2442 | Designators.data(), Designators.size(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2443 | InitExpressions, Loc, GNUSyntax, |
| 2444 | Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2445 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2446 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2447 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2448 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2449 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2450 | return Owned(DIE); |
| 2451 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2452 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2453 | //===----------------------------------------------------------------------===// |
| 2454 | // Initialization entity |
| 2455 | //===----------------------------------------------------------------------===// |
| 2456 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2457 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2458 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2459 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2460 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2461 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2462 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2463 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2464 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2465 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2466 | Type = VT->getElementType(); |
| 2467 | } else { |
| 2468 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2469 | assert(CT && "Unexpected type"); |
| 2470 | Kind = EK_ComplexElement; |
| 2471 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2472 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2475 | InitializedEntity |
| 2476 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 2477 | const CXXBaseSpecifier *Base, |
| 2478 | bool IsInheritedVirtualBase) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2479 | InitializedEntity Result; |
| 2480 | Result.Kind = EK_Base; |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2481 | Result.Parent = 0; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2482 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2483 | if (IsInheritedVirtualBase) |
| 2484 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2485 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2486 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2487 | return Result; |
| 2488 | } |
| 2489 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2490 | DeclarationName InitializedEntity::getName() const { |
| 2491 | switch (getKind()) { |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2492 | case EK_Parameter: |
| 2493 | case EK_Parameter_CF_Audited: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2494 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2495 | return (D ? D->getDeclName() : DeclarationName()); |
| 2496 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2497 | |
| 2498 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2499 | case EK_Member: |
| 2500 | return VariableOrMember->getDeclName(); |
| 2501 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2502 | case EK_LambdaCapture: |
Bill Wendling | 2434dcf | 2013-12-05 05:25:04 +0000 | [diff] [blame] | 2503 | return DeclarationName(Capture.VarID); |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2504 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2505 | case EK_Result: |
| 2506 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2507 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2508 | case EK_Temporary: |
| 2509 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2510 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2511 | case EK_ArrayElement: |
| 2512 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2513 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2514 | case EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2515 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2516 | case EK_RelatedResult: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2517 | return DeclarationName(); |
| 2518 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2519 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2520 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2523 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2524 | switch (getKind()) { |
| 2525 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2526 | case EK_Member: |
| 2527 | return VariableOrMember; |
| 2528 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2529 | case EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2530 | case EK_Parameter_CF_Audited: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2531 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2532 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2533 | case EK_Result: |
| 2534 | case EK_Exception: |
| 2535 | case EK_New: |
| 2536 | case EK_Temporary: |
| 2537 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2538 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2539 | case EK_ArrayElement: |
| 2540 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2541 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2542 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2543 | case EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2544 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2545 | case EK_RelatedResult: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2546 | return 0; |
| 2547 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2548 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2549 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2552 | bool InitializedEntity::allowsNRVO() const { |
| 2553 | switch (getKind()) { |
| 2554 | case EK_Result: |
| 2555 | case EK_Exception: |
| 2556 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2557 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2558 | case EK_Variable: |
| 2559 | case EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2560 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2561 | case EK_Member: |
| 2562 | case EK_New: |
| 2563 | case EK_Temporary: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2564 | case EK_CompoundLiteralInit: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2565 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2566 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2567 | case EK_ArrayElement: |
| 2568 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2569 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2570 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2571 | case EK_LambdaCapture: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2572 | case EK_RelatedResult: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2573 | break; |
| 2574 | } |
| 2575 | |
| 2576 | return false; |
| 2577 | } |
| 2578 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2579 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2580 | assert(getParent() != this); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2581 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 2582 | for (unsigned I = 0; I != Depth; ++I) |
| 2583 | OS << "`-"; |
| 2584 | |
| 2585 | switch (getKind()) { |
| 2586 | case EK_Variable: OS << "Variable"; break; |
| 2587 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2588 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 2589 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2590 | case EK_Result: OS << "Result"; break; |
| 2591 | case EK_Exception: OS << "Exception"; break; |
| 2592 | case EK_Member: OS << "Member"; break; |
| 2593 | case EK_New: OS << "New"; break; |
| 2594 | case EK_Temporary: OS << "Temporary"; break; |
| 2595 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2596 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2597 | case EK_Base: OS << "Base"; break; |
| 2598 | case EK_Delegating: OS << "Delegating"; break; |
| 2599 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 2600 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 2601 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 2602 | case EK_BlockElement: OS << "Block"; break; |
| 2603 | case EK_LambdaCapture: |
| 2604 | OS << "LambdaCapture "; |
Bill Wendling | 2434dcf | 2013-12-05 05:25:04 +0000 | [diff] [blame] | 2605 | OS << DeclarationName(Capture.VarID); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2606 | break; |
| 2607 | } |
| 2608 | |
| 2609 | if (Decl *D = getDecl()) { |
| 2610 | OS << " "; |
| 2611 | cast<NamedDecl>(D)->printQualifiedName(OS); |
| 2612 | } |
| 2613 | |
| 2614 | OS << " '" << getType().getAsString() << "'\n"; |
| 2615 | |
| 2616 | return Depth + 1; |
| 2617 | } |
| 2618 | |
| 2619 | void InitializedEntity::dump() const { |
| 2620 | dumpImpl(llvm::errs()); |
| 2621 | } |
| 2622 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2623 | //===----------------------------------------------------------------------===// |
| 2624 | // Initialization sequence |
| 2625 | //===----------------------------------------------------------------------===// |
| 2626 | |
| 2627 | void InitializationSequence::Step::Destroy() { |
| 2628 | switch (Kind) { |
| 2629 | case SK_ResolveAddressOfOverloadedFunction: |
| 2630 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2631 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2632 | case SK_CastDerivedToBaseLValue: |
| 2633 | case SK_BindReference: |
| 2634 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2635 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2636 | case SK_UserConversion: |
| 2637 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2638 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2639 | case SK_QualificationConversionLValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2640 | case SK_LValueToRValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2641 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2642 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2643 | case SK_UnwrapInitList: |
| 2644 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2645 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2646 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2647 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2648 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2649 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2650 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2651 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2652 | case SK_PassByIndirectCopyRestore: |
| 2653 | case SK_PassByIndirectRestore: |
| 2654 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2655 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2656 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2657 | case SK_OCLZeroEvent: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2658 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2659 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2660 | case SK_ConversionSequence: |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2661 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2662 | delete ICS; |
| 2663 | } |
| 2664 | } |
| 2665 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2666 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2667 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2671 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2672 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2673 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2674 | switch (getFailureKind()) { |
| 2675 | case FK_TooManyInitsForReference: |
| 2676 | case FK_ArrayNeedsInitList: |
| 2677 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 2678 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 2679 | case FK_NarrowStringIntoWideCharArray: |
| 2680 | case FK_WideStringIntoCharArray: |
| 2681 | case FK_IncompatWideStringIntoWideChar: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2682 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2683 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2684 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2685 | case FK_RValueReferenceBindingToLValue: |
| 2686 | case FK_ReferenceInitDropsQualifiers: |
| 2687 | case FK_ReferenceInitFailed: |
| 2688 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2689 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2690 | case FK_TooManyInitsForScalar: |
| 2691 | case FK_ReferenceBindingToInitList: |
| 2692 | case FK_InitListBadDestinationType: |
| 2693 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2694 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2695 | case FK_ArrayTypeMismatch: |
| 2696 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2697 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2698 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2699 | case FK_PlaceholderType: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2700 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2701 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2702 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2703 | case FK_ReferenceInitOverloadFailed: |
| 2704 | case FK_UserConversionOverloadFailed: |
| 2705 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2706 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2707 | return FailedOverloadResult == OR_Ambiguous; |
| 2708 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2709 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2710 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2711 | } |
| 2712 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2713 | bool InitializationSequence::isConstructorInitialization() const { |
| 2714 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2715 | } |
| 2716 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2717 | void |
| 2718 | InitializationSequence |
| 2719 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2720 | DeclAccessPair Found, |
| 2721 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2722 | Step S; |
| 2723 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2724 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2725 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2726 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2727 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2728 | Steps.push_back(S); |
| 2729 | } |
| 2730 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2731 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2732 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2733 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2734 | switch (VK) { |
| 2735 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2736 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2737 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2738 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2739 | S.Type = BaseType; |
| 2740 | Steps.push_back(S); |
| 2741 | } |
| 2742 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2743 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2744 | bool BindingTemporary) { |
| 2745 | Step S; |
| 2746 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2747 | S.Type = T; |
| 2748 | Steps.push_back(S); |
| 2749 | } |
| 2750 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2751 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2752 | Step S; |
| 2753 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2754 | S.Type = T; |
| 2755 | Steps.push_back(S); |
| 2756 | } |
| 2757 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2758 | void |
| 2759 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2760 | DeclAccessPair FoundDecl, |
| 2761 | QualType T, |
| 2762 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2763 | Step S; |
| 2764 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2765 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2766 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2767 | S.Function.Function = Function; |
| 2768 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2769 | Steps.push_back(S); |
| 2770 | } |
| 2771 | |
| 2772 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2773 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2774 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2775 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2776 | switch (VK) { |
| 2777 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2778 | S.Kind = SK_QualificationConversionRValue; |
| 2779 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2780 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2781 | S.Kind = SK_QualificationConversionXValue; |
| 2782 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2783 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2784 | S.Kind = SK_QualificationConversionLValue; |
| 2785 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2786 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2787 | S.Type = Ty; |
| 2788 | Steps.push_back(S); |
| 2789 | } |
| 2790 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2791 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2792 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2793 | |
| 2794 | Step S; |
| 2795 | S.Kind = SK_LValueToRValue; |
| 2796 | S.Type = Ty; |
| 2797 | Steps.push_back(S); |
| 2798 | } |
| 2799 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2800 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2801 | const ImplicitConversionSequence &ICS, QualType T, |
| 2802 | bool TopLevelOfInitList) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2803 | Step S; |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2804 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 2805 | : SK_ConversionSequence; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2806 | S.Type = T; |
| 2807 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2808 | Steps.push_back(S); |
| 2809 | } |
| 2810 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2811 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2812 | Step S; |
| 2813 | S.Kind = SK_ListInitialization; |
| 2814 | S.Type = T; |
| 2815 | Steps.push_back(S); |
| 2816 | } |
| 2817 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2818 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2819 | InitializationSequence |
| 2820 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2821 | AccessSpecifier Access, |
| 2822 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2823 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2824 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2825 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2826 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2827 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2828 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2829 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2830 | S.Function.Function = Constructor; |
| 2831 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2832 | Steps.push_back(S); |
| 2833 | } |
| 2834 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2835 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2836 | Step S; |
| 2837 | S.Kind = SK_ZeroInitialization; |
| 2838 | S.Type = T; |
| 2839 | Steps.push_back(S); |
| 2840 | } |
| 2841 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2842 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2843 | Step S; |
| 2844 | S.Kind = SK_CAssignment; |
| 2845 | S.Type = T; |
| 2846 | Steps.push_back(S); |
| 2847 | } |
| 2848 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2849 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2850 | Step S; |
| 2851 | S.Kind = SK_StringInit; |
| 2852 | S.Type = T; |
| 2853 | Steps.push_back(S); |
| 2854 | } |
| 2855 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2856 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2857 | Step S; |
| 2858 | S.Kind = SK_ObjCObjectConversion; |
| 2859 | S.Type = T; |
| 2860 | Steps.push_back(S); |
| 2861 | } |
| 2862 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2863 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2864 | Step S; |
| 2865 | S.Kind = SK_ArrayInit; |
| 2866 | S.Type = T; |
| 2867 | Steps.push_back(S); |
| 2868 | } |
| 2869 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2870 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2871 | Step S; |
| 2872 | S.Kind = SK_ParenthesizedArrayInit; |
| 2873 | S.Type = T; |
| 2874 | Steps.push_back(S); |
| 2875 | } |
| 2876 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2877 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2878 | bool shouldCopy) { |
| 2879 | Step s; |
| 2880 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2881 | : SK_PassByIndirectRestore); |
| 2882 | s.Type = type; |
| 2883 | Steps.push_back(s); |
| 2884 | } |
| 2885 | |
| 2886 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2887 | Step S; |
| 2888 | S.Kind = SK_ProduceObjCObject; |
| 2889 | S.Type = T; |
| 2890 | Steps.push_back(S); |
| 2891 | } |
| 2892 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2893 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2894 | Step S; |
| 2895 | S.Kind = SK_StdInitializerList; |
| 2896 | S.Type = T; |
| 2897 | Steps.push_back(S); |
| 2898 | } |
| 2899 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2900 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 2901 | Step S; |
| 2902 | S.Kind = SK_OCLSamplerInit; |
| 2903 | S.Type = T; |
| 2904 | Steps.push_back(S); |
| 2905 | } |
| 2906 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2907 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 2908 | Step S; |
| 2909 | S.Kind = SK_OCLZeroEvent; |
| 2910 | S.Type = T; |
| 2911 | Steps.push_back(S); |
| 2912 | } |
| 2913 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2914 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2915 | InitListExpr *Syntactic) { |
| 2916 | assert(Syntactic->getNumInits() == 1 && |
| 2917 | "Can only rewrap trivial init lists."); |
| 2918 | Step S; |
| 2919 | S.Kind = SK_UnwrapInitList; |
| 2920 | S.Type = Syntactic->getInit(0)->getType(); |
| 2921 | Steps.insert(Steps.begin(), S); |
| 2922 | |
| 2923 | S.Kind = SK_RewrapInitList; |
| 2924 | S.Type = T; |
| 2925 | S.WrappingSyntacticList = Syntactic; |
| 2926 | Steps.push_back(S); |
| 2927 | } |
| 2928 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2929 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2930 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2931 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2932 | this->Failure = Failure; |
| 2933 | this->FailedOverloadResult = Result; |
| 2934 | } |
| 2935 | |
| 2936 | //===----------------------------------------------------------------------===// |
| 2937 | // Attempt initialization |
| 2938 | //===----------------------------------------------------------------------===// |
| 2939 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2940 | static void MaybeProduceObjCObject(Sema &S, |
| 2941 | InitializationSequence &Sequence, |
| 2942 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2943 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2944 | |
| 2945 | /// When initializing a parameter, produce the value if it's marked |
| 2946 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2947 | if (Entity.isParameterKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2948 | if (!Entity.isParameterConsumed()) |
| 2949 | return; |
| 2950 | |
| 2951 | assert(Entity.getType()->isObjCRetainableType() && |
| 2952 | "consuming an object of unretainable type?"); |
| 2953 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2954 | |
| 2955 | /// When initializing a return value, if the return type is a |
| 2956 | /// retainable type, then returns need to immediately retain the |
| 2957 | /// object. If an autorelease is required, it will be done at the |
| 2958 | /// last instant. |
| 2959 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2960 | if (!Entity.getType()->isObjCRetainableType()) |
| 2961 | return; |
| 2962 | |
| 2963 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2964 | } |
| 2965 | } |
| 2966 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2967 | static void TryListInitialization(Sema &S, |
| 2968 | const InitializedEntity &Entity, |
| 2969 | const InitializationKind &Kind, |
| 2970 | InitListExpr *InitList, |
| 2971 | InitializationSequence &Sequence); |
| 2972 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2973 | /// \brief When initializing from init list via constructor, handle |
| 2974 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2975 | /// |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2976 | /// \return true if we have handled initialization of an object of type |
| 2977 | /// std::initializer_list<T>, false otherwise. |
| 2978 | static bool TryInitializerListConstruction(Sema &S, |
| 2979 | InitListExpr *List, |
| 2980 | QualType DestType, |
| 2981 | InitializationSequence &Sequence) { |
| 2982 | QualType E; |
| 2983 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2984 | return false; |
| 2985 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2986 | if (S.RequireCompleteType(List->getExprLoc(), E, 0)) { |
| 2987 | Sequence.setIncompleteTypeFailure(E); |
| 2988 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2989 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2990 | |
| 2991 | // Try initializing a temporary array from the init list. |
| 2992 | QualType ArrayType = S.Context.getConstantArrayType( |
| 2993 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2994 | List->getNumInits()), |
| 2995 | clang::ArrayType::Normal, 0); |
| 2996 | InitializedEntity HiddenArray = |
| 2997 | InitializedEntity::InitializeTemporary(ArrayType); |
| 2998 | InitializationKind Kind = |
| 2999 | InitializationKind::CreateDirectList(List->getExprLoc()); |
| 3000 | TryListInitialization(S, HiddenArray, Kind, List, Sequence); |
| 3001 | if (Sequence) |
| 3002 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3003 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3006 | static OverloadingResult |
| 3007 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3008 | MultiExprArg Args, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3009 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3010 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3011 | OverloadCandidateSet::iterator &Best, |
| 3012 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3013 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3014 | CandidateSet.clear(); |
| 3015 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3016 | for (ArrayRef<NamedDecl *>::iterator |
| 3017 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3018 | NamedDecl *D = *Con; |
| 3019 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3020 | bool SuppressUserConversions = false; |
| 3021 | |
| 3022 | // Find the constructor (which may be a template). |
| 3023 | CXXConstructorDecl *Constructor = 0; |
| 3024 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 3025 | if (ConstructorTmpl) |
| 3026 | Constructor = cast<CXXConstructorDecl>( |
| 3027 | ConstructorTmpl->getTemplatedDecl()); |
| 3028 | else { |
| 3029 | Constructor = cast<CXXConstructorDecl>(D); |
| 3030 | |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3031 | // C++11 [over.best.ics]p4: |
| 3032 | // However, when considering the argument of a constructor or |
| 3033 | // user-defined conversion function that is a candidate: |
| 3034 | // -- by 13.3.1.3 when invoked for the copying/moving of a temporary |
| 3035 | // in the second step of a class copy-initialization, |
| 3036 | // -- by 13.3.1.7 when passing the initializer list as a single |
| 3037 | // argument or when the initializer list has exactly one elementand |
| 3038 | // a conversion to some class X or reference to (possibly |
| 3039 | // cv-qualified) X is considered for the first parameter of a |
| 3040 | // constructor of X, or |
| 3041 | // -- by 13.3.1.4, 13.3.1.5, or 13.3.1.6 in all cases, |
| 3042 | // only standard conversion sequences and ellipsis conversion sequences |
| 3043 | // are considered. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3044 | if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) && |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3045 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3046 | SuppressUserConversions = true; |
| 3047 | } |
| 3048 | |
| 3049 | if (!Constructor->isInvalidDecl() && |
| 3050 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3051 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3052 | if (ConstructorTmpl) |
| 3053 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3054 | /*ExplicitArgs*/ 0, Args, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3055 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3056 | else { |
| 3057 | // C++ [over.match.copy]p1: |
| 3058 | // - When initializing a temporary to be bound to the first parameter |
| 3059 | // of a constructor that takes a reference to possibly cv-qualified |
| 3060 | // T as its first argument, called with a single argument in the |
| 3061 | // context of direct-initialization, explicit conversion functions |
| 3062 | // are also considered. |
| 3063 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3064 | Args.size() == 1 && |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3065 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3066 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3067 | SuppressUserConversions, |
| 3068 | /*PartialOverloading=*/false, |
| 3069 | /*AllowExplicit=*/AllowExplicitConv); |
| 3070 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3071 | } |
| 3072 | } |
| 3073 | |
| 3074 | // Perform overload resolution and return the result. |
| 3075 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3076 | } |
| 3077 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3078 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3079 | /// enumerates the constructors of the initialized entity and performs overload |
| 3080 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3081 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3082 | /// class type. |
| 3083 | static void TryConstructorInitialization(Sema &S, |
| 3084 | const InitializedEntity &Entity, |
| 3085 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3086 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3087 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3088 | bool InitListSyntax = false) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3089 | assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3090 | "InitListSyntax must come with a single initializer list argument."); |
| 3091 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3092 | // The type we're constructing needs to be complete. |
| 3093 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3094 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3095 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3099 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3100 | CXXRecordDecl *DestRecordDecl |
| 3101 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3102 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3103 | // Build the candidate set directly in the initialization sequence |
| 3104 | // structure, so that it will persist if we fail. |
| 3105 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3106 | |
| 3107 | // Determine whether we are allowed to call explicit constructors or |
| 3108 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3109 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3110 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3111 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3112 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3113 | // applicable constructors are enumerated, and the best one is chosen |
| 3114 | // through overload resolution. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3115 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3116 | // The container holding the constructors can under certain conditions |
| 3117 | // be changed while iterating (e.g. because of deserialization). |
| 3118 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3119 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3120 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3121 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3122 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3123 | bool AsInitializerList = false; |
| 3124 | |
| 3125 | // C++11 [over.match.list]p1: |
| 3126 | // When objects of non-aggregate type T are list-initialized, overload |
| 3127 | // resolution selects the constructor in two phases: |
| 3128 | // - Initially, the candidate functions are the initializer-list |
| 3129 | // constructors of the class T and the argument list consists of the |
| 3130 | // initializer list as a single argument. |
| 3131 | if (InitListSyntax) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3132 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3133 | AsInitializerList = true; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3134 | |
| 3135 | // If the initializer list has no elements and T has a default constructor, |
| 3136 | // the first phase is omitted. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3137 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3138 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3139 | CandidateSet, Ctors, Best, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3140 | CopyInitialization, AllowExplicit, |
| 3141 | /*OnlyListConstructor=*/true, |
| 3142 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3143 | |
| 3144 | // Time to unwrap the init list. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3145 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | // C++11 [over.match.list]p1: |
| 3149 | // - If no viable initializer-list constructor is found, overload resolution |
| 3150 | // is performed again, where the candidate functions are all the |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3151 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3152 | // elements of the initializer list. |
| 3153 | if (Result == OR_No_Viable_Function) { |
| 3154 | AsInitializerList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3155 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3156 | CandidateSet, Ctors, Best, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3157 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3158 | /*OnlyListConstructors=*/false, |
| 3159 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3160 | } |
| 3161 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3162 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3163 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3164 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3165 | Result); |
| 3166 | return; |
| 3167 | } |
| 3168 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3169 | // C++11 [dcl.init]p6: |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3170 | // If a program calls for the default initialization of an object |
| 3171 | // of a const-qualified type T, T shall be a class type with a |
| 3172 | // user-provided default constructor. |
| 3173 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3174 | Entity.getType().isConstQualified() && |
Aaron Ballman | 5121781 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 3175 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3176 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3177 | return; |
| 3178 | } |
| 3179 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3180 | // C++11 [over.match.list]p1: |
| 3181 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3182 | // initializer is ill-formed. |
| 3183 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 3184 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 3185 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3186 | return; |
| 3187 | } |
| 3188 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3189 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3190 | // subsumed by the initialization. |
| 3191 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3192 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 3193 | Best->FoundDecl.getAccess(), |
| 3194 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3195 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3198 | static bool |
| 3199 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3200 | Expr *Initializer, |
| 3201 | QualType &SourceType, |
| 3202 | QualType &UnqualifiedSourceType, |
| 3203 | QualType UnqualifiedTargetType, |
| 3204 | InitializationSequence &Sequence) { |
| 3205 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3206 | S.Context.OverloadTy) { |
| 3207 | DeclAccessPair Found; |
| 3208 | bool HadMultipleCandidates = false; |
| 3209 | if (FunctionDecl *Fn |
| 3210 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3211 | UnqualifiedTargetType, |
| 3212 | false, Found, |
| 3213 | &HadMultipleCandidates)) { |
| 3214 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3215 | HadMultipleCandidates); |
| 3216 | SourceType = Fn->getType(); |
| 3217 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3218 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3219 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3220 | return true; |
| 3221 | } |
| 3222 | } |
| 3223 | return false; |
| 3224 | } |
| 3225 | |
| 3226 | static void TryReferenceInitializationCore(Sema &S, |
| 3227 | const InitializedEntity &Entity, |
| 3228 | const InitializationKind &Kind, |
| 3229 | Expr *Initializer, |
| 3230 | QualType cv1T1, QualType T1, |
| 3231 | Qualifiers T1Quals, |
| 3232 | QualType cv2T2, QualType T2, |
| 3233 | Qualifiers T2Quals, |
| 3234 | InitializationSequence &Sequence); |
| 3235 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3236 | static void TryValueInitialization(Sema &S, |
| 3237 | const InitializedEntity &Entity, |
| 3238 | const InitializationKind &Kind, |
| 3239 | InitializationSequence &Sequence, |
| 3240 | InitListExpr *InitList = 0); |
| 3241 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3242 | /// \brief Attempt list initialization of a reference. |
| 3243 | static void TryReferenceListInitialization(Sema &S, |
| 3244 | const InitializedEntity &Entity, |
| 3245 | const InitializationKind &Kind, |
| 3246 | InitListExpr *InitList, |
Richard Smith | b6e3808 | 2013-06-08 00:02:08 +0000 | [diff] [blame] | 3247 | InitializationSequence &Sequence) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3248 | // First, catch C++03 where this isn't possible. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3249 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3250 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3251 | return; |
| 3252 | } |
| 3253 | |
| 3254 | QualType DestType = Entity.getType(); |
| 3255 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3256 | Qualifiers T1Quals; |
| 3257 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3258 | |
| 3259 | // Reference initialization via an initializer list works thus: |
| 3260 | // If the initializer list consists of a single element that is |
| 3261 | // reference-related to the referenced type, bind directly to that element |
| 3262 | // (possibly creating temporaries). |
| 3263 | // Otherwise, initialize a temporary with the initializer list and |
| 3264 | // bind to that. |
| 3265 | if (InitList->getNumInits() == 1) { |
| 3266 | Expr *Initializer = InitList->getInit(0); |
| 3267 | QualType cv2T2 = Initializer->getType(); |
| 3268 | Qualifiers T2Quals; |
| 3269 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3270 | |
| 3271 | // If this fails, creating a temporary wouldn't work either. |
| 3272 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3273 | T1, Sequence)) |
| 3274 | return; |
| 3275 | |
| 3276 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3277 | bool dummy1, dummy2, dummy3; |
| 3278 | Sema::ReferenceCompareResult RefRelationship |
| 3279 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3280 | dummy2, dummy3); |
| 3281 | if (RefRelationship >= Sema::Ref_Related) { |
| 3282 | // Try to bind the reference here. |
| 3283 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3284 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3285 | if (Sequence) |
| 3286 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3287 | return; |
| 3288 | } |
Richard Smith | 02d65ee | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3289 | |
| 3290 | // Update the initializer if we've resolved an overloaded function. |
| 3291 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3292 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | // Not reference-related. Create a temporary and bind to that. |
| 3296 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3297 | |
| 3298 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3299 | if (Sequence) { |
| 3300 | if (DestType->isRValueReferenceType() || |
| 3301 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3302 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3303 | else |
| 3304 | Sequence.SetFailed( |
| 3305 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3306 | } |
| 3307 | } |
| 3308 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3309 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3310 | static void TryListInitialization(Sema &S, |
| 3311 | const InitializedEntity &Entity, |
| 3312 | const InitializationKind &Kind, |
| 3313 | InitListExpr *InitList, |
| 3314 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3315 | QualType DestType = Entity.getType(); |
| 3316 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3317 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3318 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3319 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3320 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3321 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3322 | return; |
| 3323 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3324 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3325 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3326 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3327 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3328 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3329 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3330 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3331 | return; |
| 3332 | } |
| 3333 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3334 | // C++11 [dcl.init.list]p3: |
| 3335 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3336 | if (!DestType->isAggregateType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3337 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3338 | // - Otherwise, if the initializer list has no elements and T is a |
| 3339 | // class type with a default constructor, the object is |
| 3340 | // value-initialized. |
| 3341 | if (InitList->getNumInits() == 0) { |
| 3342 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3343 | if (RD->hasDefaultConstructor()) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3344 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3345 | return; |
| 3346 | } |
| 3347 | } |
| 3348 | |
| 3349 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3350 | // an initializer_list object constructed [...] |
| 3351 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3352 | return; |
| 3353 | |
| 3354 | // - Otherwise, if T is a class type, constructors are considered. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3355 | Expr *InitListAsExpr = InitList; |
| 3356 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3357 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3358 | } else |
| 3359 | Sequence.SetFailed( |
| 3360 | InitializationSequence::FK_InitListBadDestinationType); |
| 3361 | return; |
| 3362 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3363 | } |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 3364 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
| 3365 | InitList->getNumInits() == 1 && |
| 3366 | InitList->getInit(0)->getType()->isRecordType()) { |
| 3367 | // - Otherwise, if the initializer list has a single element of type E |
| 3368 | // [...references are handled above...], the object or reference is |
| 3369 | // initialized from that element; if a narrowing conversion is required |
| 3370 | // to convert the element to T, the program is ill-formed. |
| 3371 | // |
| 3372 | // Per core-24034, this is direct-initialization if we were performing |
| 3373 | // direct-list-initialization and copy-initialization otherwise. |
| 3374 | // We can't use InitListChecker for this, because it always performs |
| 3375 | // copy-initialization. This only matters if we might use an 'explicit' |
| 3376 | // conversion operator, so we only need to handle the cases where the source |
| 3377 | // is of record type. |
| 3378 | InitializationKind SubKind = |
| 3379 | Kind.getKind() == InitializationKind::IK_DirectList |
| 3380 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 3381 | InitList->getLBraceLoc(), |
| 3382 | InitList->getRBraceLoc()) |
| 3383 | : Kind; |
| 3384 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 3385 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 3386 | /*TopLevelOfInitList*/true); |
| 3387 | if (Sequence) |
| 3388 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 3389 | return; |
| 3390 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3391 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3392 | InitListChecker CheckInitList(S, Entity, InitList, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 3393 | DestType, /*VerifyOnly=*/true); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3394 | if (CheckInitList.HadError()) { |
| 3395 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3396 | return; |
| 3397 | } |
| 3398 | |
| 3399 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3400 | Sequence.AddListInitializationStep(DestType); |
| 3401 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3402 | |
| 3403 | /// \brief Try a reference initialization that involves calling a conversion |
| 3404 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3405 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3406 | const InitializedEntity &Entity, |
| 3407 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3408 | Expr *Initializer, |
| 3409 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3410 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3411 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3412 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3413 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3414 | QualType cv2T2 = Initializer->getType(); |
| 3415 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3416 | |
| 3417 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3418 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3419 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3420 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3421 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3422 | ObjCConversion, |
| 3423 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3424 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3425 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3426 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3427 | (void)ObjCLifetimeConversion; |
| 3428 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3429 | // Build the candidate set directly in the initialization sequence |
| 3430 | // structure, so that it will persist if we fail. |
| 3431 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3432 | CandidateSet.clear(); |
| 3433 | |
| 3434 | // Determine whether we are allowed to call explicit constructors or |
| 3435 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3436 | bool AllowExplicit = Kind.AllowExplicit(); |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3437 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 3438 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3439 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3440 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3441 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3442 | // The type we're converting to is a class type. Enumerate its constructors |
| 3443 | // to see if there is a suitable conversion. |
| 3444 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3445 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3446 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3447 | // The container holding the constructors can under certain conditions |
| 3448 | // be changed while iterating (e.g. because of deserialization). |
| 3449 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3450 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3451 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3452 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3453 | NamedDecl *D = *CI; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3454 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3455 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3456 | // Find the constructor (which may be a template). |
| 3457 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3458 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3459 | if (ConstructorTmpl) |
| 3460 | Constructor = cast<CXXConstructorDecl>( |
| 3461 | ConstructorTmpl->getTemplatedDecl()); |
| 3462 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3463 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3464 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3465 | if (!Constructor->isInvalidDecl() && |
| 3466 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3467 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3468 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3469 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3470 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3471 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3472 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3473 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3474 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3475 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3476 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3477 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3478 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3479 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3480 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3481 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3482 | const RecordType *T2RecordType = 0; |
| 3483 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3484 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3485 | // The type we're converting from is a class type, enumerate its conversion |
| 3486 | // functions. |
| 3487 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3488 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3489 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3490 | CXXRecordDecl::conversion_iterator> |
| 3491 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3492 | for (CXXRecordDecl::conversion_iterator |
| 3493 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3494 | NamedDecl *D = *I; |
| 3495 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3496 | if (isa<UsingShadowDecl>(D)) |
| 3497 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3499 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3500 | CXXConversionDecl *Conv; |
| 3501 | if (ConvTemplate) |
| 3502 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3503 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3504 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3505 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3506 | // If the conversion function doesn't return a reference type, |
| 3507 | // it can't be considered for this conversion unless we're allowed to |
| 3508 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3509 | // FIXME: Do we need to make sure that we only consider conversion |
| 3510 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3511 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3512 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3513 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3514 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3515 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3516 | ActingDC, Initializer, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3517 | DestType, CandidateSet, |
| 3518 | /*AllowObjCConversionOnExplicit=*/ |
| 3519 | false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3520 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3521 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3522 | Initializer, DestType, CandidateSet, |
| 3523 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3524 | } |
| 3525 | } |
| 3526 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3527 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3528 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3529 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3530 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3531 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3532 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3533 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3534 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3535 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3536 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3537 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3538 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3539 | // This is the overload that will be used for this initialization step if we |
| 3540 | // use this initialization. Mark it as referenced. |
| 3541 | Function->setReferenced(); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3542 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3543 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3544 | if (isa<CXXConversionDecl>(Function)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3545 | T2 = Function->getReturnType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3546 | else |
| 3547 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3548 | |
| 3549 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3550 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3551 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3552 | T2.getNonLValueExprType(S.Context), |
| 3553 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3554 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3555 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3556 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3557 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3558 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3559 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3560 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3561 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3562 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3563 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3564 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3565 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3566 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3567 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3568 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3569 | NewDerivedToBase, NewObjCConversion, |
| 3570 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3571 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3572 | // If the type we've converted to is not reference-related to the |
| 3573 | // type we're looking for, then there is another conversion step |
| 3574 | // we need to perform to produce a temporary of the right type |
| 3575 | // that we'll be binding to. |
| 3576 | ImplicitConversionSequence ICS; |
| 3577 | ICS.setStandard(); |
| 3578 | ICS.Standard = Best->FinalConversion; |
| 3579 | T2 = ICS.Standard.getToType(2); |
| 3580 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3581 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3582 | Sequence.AddDerivedToBaseCastStep( |
| 3583 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3584 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3585 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3586 | else if (NewObjCConversion) |
| 3587 | Sequence.AddObjCObjectConversionStep( |
| 3588 | S.Context.getQualifiedType(T1, |
| 3589 | T2.getNonReferenceType().getQualifiers())); |
| 3590 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3591 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3592 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3593 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3594 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3595 | return OR_Success; |
| 3596 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3597 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3598 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3599 | const InitializedEntity &Entity, |
| 3600 | Expr *CurInitExpr); |
| 3601 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3602 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3603 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3604 | const InitializedEntity &Entity, |
| 3605 | const InitializationKind &Kind, |
| 3606 | Expr *Initializer, |
| 3607 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3608 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3609 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3610 | Qualifiers T1Quals; |
| 3611 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3612 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3613 | Qualifiers T2Quals; |
| 3614 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3615 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3616 | // If the initializer is the address of an overloaded function, try |
| 3617 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3618 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3619 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3620 | T1, Sequence)) |
| 3621 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3622 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3623 | // Delegate everything else to a subfunction. |
| 3624 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3625 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3626 | } |
| 3627 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3628 | /// Converts the target of reference initialization so that it has the |
| 3629 | /// appropriate qualifiers and value kind. |
| 3630 | /// |
| 3631 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3632 | /// \code |
| 3633 | /// int x; |
| 3634 | /// const int &r = x; |
| 3635 | /// \endcode |
| 3636 | /// |
| 3637 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3638 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3639 | /// \code |
| 3640 | /// const int &r = someStruct.bitfield; |
| 3641 | /// \endcode |
| 3642 | static ExprValueKind |
| 3643 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3644 | InitializationSequence &Sequence, |
| 3645 | Expr *Initializer, |
| 3646 | QualType cv1T1, |
| 3647 | Qualifiers T1Quals, |
| 3648 | Qualifiers T2Quals, |
| 3649 | bool IsLValueRef) { |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3650 | bool IsNonAddressableType = Initializer->refersToBitField() || |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3651 | Initializer->refersToVectorElement(); |
| 3652 | |
| 3653 | if (IsNonAddressableType) { |
| 3654 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3655 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3656 | // an rvalue reference. |
| 3657 | // |
| 3658 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3659 | // error to be diagnosed later. |
| 3660 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3661 | assert(Initializer->isGLValue()); |
| 3662 | return Initializer->getValueKind(); |
| 3663 | } |
| 3664 | |
| 3665 | // Force a load so we can materialize a temporary. |
| 3666 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3667 | return VK_RValue; |
| 3668 | } |
| 3669 | |
| 3670 | if (T1Quals != T2Quals) { |
| 3671 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3672 | Initializer->getValueKind()); |
| 3673 | } |
| 3674 | |
| 3675 | return Initializer->getValueKind(); |
| 3676 | } |
| 3677 | |
| 3678 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3679 | /// \brief Reference initialization without resolving overloaded functions. |
| 3680 | static void TryReferenceInitializationCore(Sema &S, |
| 3681 | const InitializedEntity &Entity, |
| 3682 | const InitializationKind &Kind, |
| 3683 | Expr *Initializer, |
| 3684 | QualType cv1T1, QualType T1, |
| 3685 | Qualifiers T1Quals, |
| 3686 | QualType cv2T2, QualType T2, |
| 3687 | Qualifiers T2Quals, |
| 3688 | InitializationSequence &Sequence) { |
| 3689 | QualType DestType = Entity.getType(); |
| 3690 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3691 | // Compute some basic properties of the types and the initializer. |
| 3692 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3693 | bool isRValueRef = !isLValueRef; |
| 3694 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3695 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3696 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3697 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3698 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3699 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3700 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3701 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3702 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3703 | // A reference to type "cv1 T1" is initialized by an expression of type |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3704 | // "cv2 T2" as follows: |
| 3705 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3706 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3707 | // expression |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3708 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3709 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3710 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3711 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3712 | bool T1Function = T1->isFunctionType(); |
| 3713 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3714 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3715 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3716 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3717 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3718 | // - is an lvalue (but is not a bit-field), and "cv1 T1" is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3719 | // reference-compatible with "cv2 T2," or |
| 3720 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3721 | // Per C++ [over.best.ics]p2, we don't diagnose whether the lvalue is a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3722 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3723 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3724 | // to decide whether we're actually binding to a temporary created from |
| 3725 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3726 | if (DerivedToBase) |
| 3727 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3728 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3729 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3730 | else if (ObjCConversion) |
| 3731 | Sequence.AddObjCObjectConversionStep( |
| 3732 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3733 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3734 | ExprValueKind ValueKind = |
| 3735 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3736 | cv1T1, T1Quals, T2Quals, |
| 3737 | isLValueRef); |
| 3738 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3739 | return; |
| 3740 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3741 | |
| 3742 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3743 | // reference-related to T2, and can be implicitly converted to an |
| 3744 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3745 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3746 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3747 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3748 | // If we have an rvalue ref to function type here, the rhs must be |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3749 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3750 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3751 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3752 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3753 | S, Entity, Kind, Initializer, /*AllowRValues*/isRValueRef, Sequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3754 | if (ConvOvlResult == OR_Success) |
| 3755 | return; |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3756 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3757 | Sequence.SetOverloadFailure( |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3758 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3759 | ConvOvlResult); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3760 | } |
| 3761 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3762 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3763 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3764 | // non-volatile const type (i.e., cv1 shall be const), or the reference |
Douglas Gregor | 69d8316 | 2011-01-20 16:08:06 +0000 | [diff] [blame] | 3765 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3766 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3767 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3768 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3769 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3770 | Sequence.SetOverloadFailure( |
| 3771 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3772 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3773 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3774 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3775 | ? (RefRelationship == Sema::Ref_Related |
| 3776 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3777 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3778 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3779 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3780 | return; |
| 3781 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3782 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3783 | // - If the initializer expression |
| 3784 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3785 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3786 | // Note: functions are handled below. |
| 3787 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3788 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3789 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3790 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3791 | (InitCategory.isXValue() || |
| 3792 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3793 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3794 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3795 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3796 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3797 | // compiler the freedom to perform a copy here or bind to the |
| 3798 | // object, while C++0x requires that we bind directly to the |
| 3799 | // object. Hence, we always bind to the object without making an |
| 3800 | // extra copy. However, in C++03 requires that we check for the |
| 3801 | // presence of a suitable copy constructor: |
| 3802 | // |
| 3803 | // The constructor that would be used to make the copy shall |
| 3804 | // be callable whether or not the copy is actually done. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3805 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3806 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3807 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3808 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3809 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3810 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3811 | if (DerivedToBase) |
| 3812 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3813 | ValueKind); |
| 3814 | else if (ObjCConversion) |
| 3815 | Sequence.AddObjCObjectConversionStep( |
| 3816 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3817 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3818 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3819 | Initializer, cv1T1, |
| 3820 | T1Quals, T2Quals, |
| 3821 | isLValueRef); |
| 3822 | |
| 3823 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3824 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3825 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3826 | |
| 3827 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3828 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3829 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3830 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3831 | // |
| 3832 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3833 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3834 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3835 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3836 | S, Entity, Kind, Initializer, /*AllowRValues*/true, Sequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3837 | if (ConvOvlResult) |
| 3838 | Sequence.SetOverloadFailure( |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3839 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3840 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3841 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3842 | return; |
| 3843 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3844 | |
Douglas Gregor | defa32e | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 3845 | if ((RefRelationship == Sema::Ref_Compatible || |
| 3846 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 3847 | isRValueRef && InitCategory.isLValue()) { |
| 3848 | Sequence.SetFailed( |
| 3849 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3850 | return; |
| 3851 | } |
| 3852 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3853 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3854 | return; |
| 3855 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3856 | |
| 3857 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3858 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3859 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3860 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3861 | |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3862 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3863 | |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3864 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 3865 | // copy-initialization? |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3866 | ImplicitConversionSequence ICS |
| 3867 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3868 | /*SuppressUserConversions=*/false, |
| 3869 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3870 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3871 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3872 | /*AllowObjCWritebackConversion=*/false); |
| 3873 | |
| 3874 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3875 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3876 | // this into an overloading ambiguity diagnostic. However, we need |
| 3877 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3878 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3879 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3880 | Sequence.SetOverloadFailure( |
| 3881 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3882 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3883 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3884 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3885 | else |
| 3886 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3887 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3888 | } else { |
| 3889 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3890 | } |
| 3891 | |
| 3892 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3893 | // same cv-qualification as, or greater cv-qualification |
| 3894 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3895 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3896 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3897 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3898 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3899 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3900 | return; |
| 3901 | } |
| 3902 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3903 | // [...] If T1 is reference-related to T2 and the reference is an rvalue |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3904 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3905 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3906 | InitCategory.isLValue()) { |
| 3907 | Sequence.SetFailed( |
| 3908 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3909 | return; |
| 3910 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3911 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3912 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3913 | return; |
| 3914 | } |
| 3915 | |
| 3916 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3917 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3918 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3919 | const InitializedEntity &Entity, |
| 3920 | const InitializationKind &Kind, |
| 3921 | Expr *Initializer, |
| 3922 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3923 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3924 | } |
| 3925 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3926 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3927 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3928 | const InitializedEntity &Entity, |
| 3929 | const InitializationKind &Kind, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3930 | InitializationSequence &Sequence, |
| 3931 | InitListExpr *InitList) { |
| 3932 | assert((!InitList || InitList->getNumInits() == 0) && |
| 3933 | "Shouldn't use value-init for non-empty init lists"); |
| 3934 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3935 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3936 | // |
| 3937 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3938 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3939 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3940 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3941 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3942 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3943 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3944 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3945 | bool NeedZeroInitialization = true; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3946 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3947 | // C++98: |
| 3948 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 3949 | // (12.1), then the default constructor for T is called (and the |
| 3950 | // initialization is ill-formed if T has no accessible default |
| 3951 | // constructor); |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3952 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3953 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3954 | } else { |
| 3955 | // C++11: |
| 3956 | // -- if T is a class type (clause 9) with either no default constructor |
| 3957 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3958 | // or deleted, then the object is default-initialized; |
| 3959 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3960 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3961 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3962 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3963 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3964 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3965 | // user-provided or deleted default constructor, then the object is |
| 3966 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3967 | // default-initialized; |
Richard Smith | 6678a05 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 3968 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 3969 | // constructor' part was removed by DR1507. |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3970 | if (NeedZeroInitialization) |
| 3971 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3972 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3973 | // C++03: |
| 3974 | // -- if T is a non-union class type without a user-declared constructor, |
| 3975 | // then every non-static data member and base class component of T is |
| 3976 | // value-initialized; |
| 3977 | // [...] A program that calls for [...] value-initialization of an |
| 3978 | // entity of reference type is ill-formed. |
| 3979 | // |
| 3980 | // C++11 doesn't need this handling, because value-initialization does not |
| 3981 | // occur recursively there, and the implicit default constructor is |
| 3982 | // defined as deleted in the problematic cases. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3983 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3984 | ClassDecl->hasUninitializedReferenceMember()) { |
| 3985 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 3986 | return; |
| 3987 | } |
| 3988 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3989 | // If this is list-value-initialization, pass the empty init list on when |
| 3990 | // building the constructor call. This affects the semantics of a few |
| 3991 | // things (such as whether an explicit default constructor can be called). |
| 3992 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3993 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3994 | bool InitListSyntax = InitList; |
| 3995 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3996 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 3997 | InitListSyntax); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3998 | } |
| 3999 | } |
| 4000 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4001 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4004 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 4005 | static void TryDefaultInitialization(Sema &S, |
| 4006 | const InitializedEntity &Entity, |
| 4007 | const InitializationKind &Kind, |
| 4008 | InitializationSequence &Sequence) { |
| 4009 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4010 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4011 | // C++ [dcl.init]p6: |
| 4012 | // To default-initialize an object of type T means: |
| 4013 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4014 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 4015 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4016 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4017 | // constructor for T is called (and the initialization is ill-formed if |
| 4018 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4019 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 4020 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4021 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4022 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4023 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4024 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4025 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4026 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4027 | // a const-qualified type T, T shall be a class type with a user-provided |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4028 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4029 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4030 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4031 | return; |
| 4032 | } |
| 4033 | |
| 4034 | // If the destination type has a lifetime property, zero-initialize it. |
| 4035 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4036 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4037 | return; |
| 4038 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4041 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 4042 | /// which enumerates all conversion functions and performs overload resolution |
| 4043 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4044 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4045 | const InitializedEntity &Entity, |
| 4046 | const InitializationKind &Kind, |
| 4047 | Expr *Initializer, |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4048 | InitializationSequence &Sequence, |
| 4049 | bool TopLevelOfInitList) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4050 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4051 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4052 | QualType SourceType = Initializer->getType(); |
| 4053 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4054 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4055 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4056 | // Build the candidate set directly in the initialization sequence |
| 4057 | // structure, so that it will persist if we fail. |
| 4058 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 4059 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4060 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4061 | // Determine whether we are allowed to call explicit constructors or |
| 4062 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4063 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4064 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4065 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4066 | // The type we're converting to is a class type. Enumerate its constructors |
| 4067 | // to see if there is a suitable conversion. |
| 4068 | CXXRecordDecl *DestRecordDecl |
| 4069 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4070 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4071 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4072 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4073 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4074 | // The container holding the constructors can under certain conditions |
| 4075 | // be changed while iterating. To be safe we copy the lookup results |
| 4076 | // to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4077 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4078 | for (SmallVectorImpl<NamedDecl *>::iterator |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4079 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4080 | Con != ConEnd; ++Con) { |
| 4081 | NamedDecl *D = *Con; |
| 4082 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4083 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4084 | // Find the constructor (which may be a template). |
| 4085 | CXXConstructorDecl *Constructor = 0; |
| 4086 | FunctionTemplateDecl *ConstructorTmpl |
| 4087 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4088 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4089 | Constructor = cast<CXXConstructorDecl>( |
| 4090 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4091 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4092 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4093 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4094 | if (!Constructor->isInvalidDecl() && |
| 4095 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4096 | if (ConstructorTmpl) |
| 4097 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 4098 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4099 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4100 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4101 | else |
| 4102 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4103 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4104 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4105 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4106 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4107 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4108 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4109 | |
| 4110 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4111 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4112 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4113 | // The type we're converting from is a class type, enumerate its conversion |
| 4114 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4115 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4116 | // We can only enumerate the conversion functions for a complete type; if |
| 4117 | // the type isn't complete, simply skip this step. |
| 4118 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 4119 | CXXRecordDecl *SourceRecordDecl |
| 4120 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4121 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4122 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4123 | CXXRecordDecl::conversion_iterator> |
| 4124 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 4125 | for (CXXRecordDecl::conversion_iterator |
| 4126 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4127 | NamedDecl *D = *I; |
| 4128 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4129 | if (isa<UsingShadowDecl>(D)) |
| 4130 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4131 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4132 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4133 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4134 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4135 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4136 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4137 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4138 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4139 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4140 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4141 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4142 | ActingDC, Initializer, DestType, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 4143 | CandidateSet, AllowExplicit); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4144 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4145 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 4146 | Initializer, DestType, CandidateSet, |
| 4147 | AllowExplicit); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4148 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4149 | } |
| 4150 | } |
| 4151 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4152 | |
| 4153 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4154 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4155 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4156 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4157 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4158 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4159 | Result); |
| 4160 | return; |
| 4161 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4162 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4163 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4164 | Function->setReferenced(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4165 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4166 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4167 | if (isa<CXXConstructorDecl>(Function)) { |
| 4168 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4169 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4170 | // cv-unqualified type of the destination. |
| 4171 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4172 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4173 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4174 | return; |
| 4175 | } |
| 4176 | |
| 4177 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4178 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4179 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4180 | // If we're converting to a class type, there may be an copy of |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4181 | // the resulting temporary object (possible to create an object of |
| 4182 | // a base class type). That copy is not a separate conversion, so |
| 4183 | // we just make a note of the actual destination type (possibly a |
| 4184 | // base class of the type returned by the conversion function) and |
| 4185 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4186 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 4187 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4188 | return; |
| 4189 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4190 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4191 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4192 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4193 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4194 | // If the conversion following the call to the conversion function |
| 4195 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4196 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4197 | Best->FinalConversion.Third) { |
| 4198 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4199 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4200 | ICS.Standard = Best->FinalConversion; |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4201 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4202 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
Richard Smith | 87c2932 | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4205 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 4206 | /// a function with a pointer return type contains a 'return false;' statement. |
| 4207 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 4208 | /// code using that header. |
| 4209 | /// |
| 4210 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 4211 | /// if it's used in a pointer-returning function in a system header. |
| 4212 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 4213 | const InitializedEntity &Entity, |
| 4214 | const Expr *Init) { |
| 4215 | return S.getLangOpts().CPlusPlus11 && |
| 4216 | Entity.getKind() == InitializedEntity::EK_Result && |
| 4217 | Entity.getType()->isPointerType() && |
| 4218 | isa<CXXBoolLiteralExpr>(Init) && |
| 4219 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 4220 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 4221 | } |
| 4222 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4223 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4224 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4225 | |
| 4226 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4227 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4228 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4229 | // Skip parens. |
| 4230 | e = e->IgnoreParens(); |
| 4231 | |
| 4232 | // Skip address-of nodes. |
| 4233 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4234 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4235 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4236 | isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4237 | |
| 4238 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4239 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4240 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4241 | case CK_Dependent: |
| 4242 | case CK_BitCast: |
| 4243 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4244 | case CK_NoOp: |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4245 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4246 | |
| 4247 | case CK_ArrayToPointerDecay: |
| 4248 | return IIK_nonscalar; |
| 4249 | |
| 4250 | case CK_NullToPointer: |
| 4251 | return IIK_okay; |
| 4252 | |
| 4253 | default: |
| 4254 | break; |
| 4255 | } |
| 4256 | |
| 4257 | // If we have a declaration reference, it had better be a local variable. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4258 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4259 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4260 | // load which requires a cleanup. |
| 4261 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4262 | isWeakAccess = true; |
| 4263 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4264 | if (!isAddressOf) return IIK_nonlocal; |
| 4265 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4266 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4267 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4268 | |
| 4269 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4270 | |
| 4271 | // If we have a conditional operator, check both sides. |
| 4272 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4273 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4274 | isWeakAccess)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4275 | return iik; |
| 4276 | |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4277 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4278 | |
| 4279 | // These are never scalar. |
| 4280 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4281 | return IIK_nonscalar; |
| 4282 | |
| 4283 | // Otherwise, it needs to be a null pointer constant. |
| 4284 | } else { |
| 4285 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4286 | ? IIK_okay : IIK_nonlocal); |
| 4287 | } |
| 4288 | |
| 4289 | return IIK_nonlocal; |
| 4290 | } |
| 4291 | |
| 4292 | /// Check whether the given expression is a valid operand for an |
| 4293 | /// indirect copy/restore. |
| 4294 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4295 | assert(src->isRValue()); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4296 | bool isWeakAccess = false; |
| 4297 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4298 | // If isWeakAccess to true, there will be an implicit |
| 4299 | // load which requires a cleanup. |
| 4300 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4301 | S.ExprNeedsCleanups = true; |
| 4302 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4303 | if (iik == IIK_okay) return; |
| 4304 | |
| 4305 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4306 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4307 | << src->getSourceRange(); |
| 4308 | } |
| 4309 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4310 | /// \brief Determine whether we have compatible array types for the |
| 4311 | /// purposes of GNU by-copy array initialization. |
| 4312 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 4313 | const ArrayType *Dest, |
| 4314 | const ArrayType *Source) { |
| 4315 | // If the source and destination array types are equivalent, we're |
| 4316 | // done. |
| 4317 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4318 | return true; |
| 4319 | |
| 4320 | // Make sure that the element types are the same. |
| 4321 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4322 | return false; |
| 4323 | |
| 4324 | // The only mismatch we allow is when the destination is an |
| 4325 | // incomplete array type and the source is a constant array type. |
| 4326 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4327 | } |
| 4328 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4329 | static bool tryObjCWritebackConversion(Sema &S, |
| 4330 | InitializationSequence &Sequence, |
| 4331 | const InitializedEntity &Entity, |
| 4332 | Expr *Initializer) { |
| 4333 | bool ArrayDecay = false; |
| 4334 | QualType ArgType = Initializer->getType(); |
| 4335 | QualType ArgPointee; |
| 4336 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4337 | ArrayDecay = true; |
| 4338 | ArgPointee = ArgArrayType->getElementType(); |
| 4339 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4340 | } |
| 4341 | |
| 4342 | // Handle write-back conversion. |
| 4343 | QualType ConvertedArgType; |
| 4344 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4345 | ConvertedArgType)) |
| 4346 | return false; |
| 4347 | |
| 4348 | // We should copy unless we're passing to an argument explicitly |
| 4349 | // marked 'out'. |
| 4350 | bool ShouldCopy = true; |
| 4351 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4352 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4353 | |
| 4354 | // Do we need an lvalue conversion? |
| 4355 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4356 | ImplicitConversionSequence ICS; |
| 4357 | ICS.setStandard(); |
| 4358 | ICS.Standard.setAsIdentityConversion(); |
| 4359 | |
| 4360 | QualType ResultType; |
| 4361 | if (ArrayDecay) { |
| 4362 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4363 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4364 | } else { |
| 4365 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4366 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4367 | } |
| 4368 | |
| 4369 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4370 | } |
| 4371 | |
| 4372 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4373 | return true; |
| 4374 | } |
| 4375 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4376 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4377 | InitializationSequence &Sequence, |
| 4378 | QualType DestType, |
| 4379 | Expr *Initializer) { |
| 4380 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4381 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4382 | return false; |
| 4383 | |
| 4384 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4385 | return true; |
| 4386 | } |
| 4387 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4388 | // |
| 4389 | // OpenCL 1.2 spec, s6.12.10 |
| 4390 | // |
| 4391 | // The event argument can also be used to associate the |
| 4392 | // async_work_group_copy with a previous async copy allowing |
| 4393 | // an event to be shared by multiple async copies; otherwise |
| 4394 | // event should be zero. |
| 4395 | // |
| 4396 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4397 | InitializationSequence &Sequence, |
| 4398 | QualType DestType, |
| 4399 | Expr *Initializer) { |
| 4400 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4401 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4402 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4403 | return false; |
| 4404 | |
| 4405 | Sequence.AddOCLZeroEventStep(DestType); |
| 4406 | return true; |
| 4407 | } |
| 4408 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4409 | InitializationSequence::InitializationSequence(Sema &S, |
| 4410 | const InitializedEntity &Entity, |
| 4411 | const InitializationKind &Kind, |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4412 | MultiExprArg Args, |
| 4413 | bool TopLevelOfInitList) |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4414 | : FailedCandidateSet(Kind.getLocation()) { |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4415 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList); |
| 4416 | } |
| 4417 | |
| 4418 | void InitializationSequence::InitializeFrom(Sema &S, |
| 4419 | const InitializedEntity &Entity, |
| 4420 | const InitializationKind &Kind, |
| 4421 | MultiExprArg Args, |
| 4422 | bool TopLevelOfInitList) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4423 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4424 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4425 | // Eliminate non-overload placeholder types in the arguments. We |
| 4426 | // need to do this before checking whether types are dependent |
| 4427 | // because lowering a pseudo-object expression might well give us |
| 4428 | // something of dependent type. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4429 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4430 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4431 | // FIXME: should we be doing this here? |
| 4432 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4433 | if (result.isInvalid()) { |
| 4434 | SetFailed(FK_PlaceholderType); |
| 4435 | return; |
| 4436 | } |
| 4437 | Args[I] = result.take(); |
| 4438 | } |
| 4439 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4440 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4441 | // The semantics of initializers are as follows. The destination type is |
| 4442 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4443 | // type is the type of the initializer expression. The source type is not |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4444 | // defined when the initializer is a braced-init-list or when it is a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4445 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4446 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4447 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4448 | if (DestType->isDependentType() || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4449 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4450 | SequenceKind = DependentSequence; |
| 4451 | return; |
| 4452 | } |
| 4453 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4454 | // Almost everything is a normal sequence. |
| 4455 | setSequenceKind(NormalSequence); |
| 4456 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4457 | QualType SourceType; |
| 4458 | Expr *Initializer = 0; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4459 | if (Args.size() == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4460 | Initializer = Args[0]; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 4461 | if (S.getLangOpts().ObjC1) { |
| 4462 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getLocStart(), |
| 4463 | DestType, Initializer->getType(), |
| 4464 | Initializer) || |
| 4465 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 4466 | Args[0] = Initializer; |
| 4467 | |
| 4468 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4469 | if (!isa<InitListExpr>(Initializer)) |
| 4470 | SourceType = Initializer->getType(); |
| 4471 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4472 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4473 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4474 | // object is list-initialized (8.5.4). |
| 4475 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4476 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4477 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4478 | return; |
| 4479 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4480 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4481 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4482 | // - If the destination type is a reference type, see 8.5.3. |
| 4483 | if (DestType->isReferenceType()) { |
| 4484 | // C++0x [dcl.init.ref]p1: |
| 4485 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4486 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4487 | // by an object that can be converted into a T. |
| 4488 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4489 | if (Args.size() != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4490 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4491 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4492 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4493 | return; |
| 4494 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4495 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4496 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4497 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4498 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4499 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4500 | return; |
| 4501 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4502 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4503 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4504 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4505 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4506 | return; |
| 4507 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4508 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4509 | // - If the destination type is an array of characters, an array of |
| 4510 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4511 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4512 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4513 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4514 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4515 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4516 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4517 | return; |
| 4518 | } |
| 4519 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4520 | if (Initializer) { |
| 4521 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 4522 | case SIF_None: |
| 4523 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 4524 | return; |
| 4525 | case SIF_NarrowStringIntoWideChar: |
| 4526 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 4527 | return; |
| 4528 | case SIF_WideStringIntoChar: |
| 4529 | SetFailed(FK_WideStringIntoCharArray); |
| 4530 | return; |
| 4531 | case SIF_IncompatWideStringIntoWideChar: |
| 4532 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 4533 | return; |
| 4534 | case SIF_Other: |
| 4535 | break; |
| 4536 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4537 | } |
| 4538 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4539 | // Note: as an GNU C extension, we allow initialization of an |
| 4540 | // array from a compound literal that creates an array of the same |
| 4541 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4542 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4543 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4544 | Initializer->getType()->isArrayType()) { |
| 4545 | const ArrayType *SourceAT |
| 4546 | = Context.getAsArrayType(Initializer->getType()); |
| 4547 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4548 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4549 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4550 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4551 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4552 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4553 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4554 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4555 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4556 | // class member of array type from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4557 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4558 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4559 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4560 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4561 | *this); |
| 4562 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4563 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4564 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4565 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 4566 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4567 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4568 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4569 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4570 | return; |
| 4571 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4572 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4573 | // Determine whether we should consider writeback conversions for |
| 4574 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4575 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4576 | Entity.isParameterKind(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4577 | |
| 4578 | // We're at the end of the line for C: it's either a write-back conversion |
| 4579 | // or it's a C assignment. There's no need to check anything else. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4580 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4581 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4582 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4583 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4584 | return; |
| 4585 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4586 | |
| 4587 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4588 | return; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4589 | |
| 4590 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4591 | return; |
| 4592 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4593 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4594 | AddCAssignmentStep(DestType); |
| 4595 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4596 | return; |
| 4597 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4598 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4599 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4600 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4601 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4602 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4603 | // - If the initialization is direct-initialization, or if it is |
| 4604 | // copy-initialization where the cv-unqualified version of the |
| 4605 | // source type is the same class as, or a derived class of, the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4606 | // class of the destination, constructors are considered. [...] |
| 4607 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4608 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4609 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4610 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4611 | TryConstructorInitialization(S, Entity, Kind, Args, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4612 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4613 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4614 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4615 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4616 | // used) to a derived class thereof are enumerated as described in |
| 4617 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4618 | // (13.3). |
| 4619 | else |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4620 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this, |
| 4621 | TopLevelOfInitList); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4622 | return; |
| 4623 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4624 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4625 | if (Args.size() > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4626 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4627 | return; |
| 4628 | } |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4629 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4630 | |
| 4631 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4632 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4633 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4634 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this, |
| 4635 | TopLevelOfInitList); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4636 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4637 | return; |
| 4638 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4639 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4640 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4641 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4642 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4643 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4644 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4645 | |
| 4646 | ImplicitConversionSequence ICS |
| 4647 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4648 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4649 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4650 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4651 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4652 | allowObjCWritebackConversion); |
| 4653 | |
| 4654 | if (ICS.isStandard() && |
| 4655 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4656 | // Objective-C ARC writeback conversion. |
| 4657 | |
| 4658 | // We should copy unless we're passing to an argument explicitly |
| 4659 | // marked 'out'. |
| 4660 | bool ShouldCopy = true; |
| 4661 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4662 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4663 | |
| 4664 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4665 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4666 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4667 | ImplicitConversionSequence LvalueICS; |
| 4668 | LvalueICS.setStandard(); |
| 4669 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4670 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4671 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4672 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4673 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4674 | |
| 4675 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4676 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4677 | DeclAccessPair dap; |
Richard Smith | 87c2932 | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4678 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 4679 | AddZeroInitializationStep(Entity.getType()); |
| 4680 | } else if (Initializer->getType() == Context.OverloadTy && |
| 4681 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 4682 | false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4683 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4684 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4685 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4686 | } else { |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4687 | AddConversionSequenceStep(ICS, Entity.getType(), TopLevelOfInitList); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4688 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4689 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4690 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4694 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4695 | StepEnd = Steps.end(); |
| 4696 | Step != StepEnd; ++Step) |
| 4697 | Step->Destroy(); |
| 4698 | } |
| 4699 | |
| 4700 | //===----------------------------------------------------------------------===// |
| 4701 | // Perform initialization |
| 4702 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4703 | static Sema::AssignmentAction |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4704 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4705 | switch(Entity.getKind()) { |
| 4706 | case InitializedEntity::EK_Variable: |
| 4707 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4708 | case InitializedEntity::EK_Exception: |
| 4709 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4710 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4711 | return Sema::AA_Initializing; |
| 4712 | |
| 4713 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4714 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4715 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4716 | return Sema::AA_Sending; |
| 4717 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4718 | return Sema::AA_Passing; |
| 4719 | |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4720 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 4721 | if (Entity.getDecl() && |
| 4722 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4723 | return Sema::AA_Sending; |
| 4724 | |
| 4725 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
| 4726 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4727 | case InitializedEntity::EK_Result: |
| 4728 | return Sema::AA_Returning; |
| 4729 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4730 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | f5200d6 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 4731 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4732 | // FIXME: Can we tell apart casting vs. converting? |
| 4733 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4734 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4735 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4736 | case InitializedEntity::EK_ArrayElement: |
| 4737 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4738 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4739 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4740 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4741 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4742 | return Sema::AA_Initializing; |
| 4743 | } |
| 4744 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4745 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4746 | } |
| 4747 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4748 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4749 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4750 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4751 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4752 | case InitializedEntity::EK_ArrayElement: |
| 4753 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4754 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4755 | case InitializedEntity::EK_New: |
| 4756 | case InitializedEntity::EK_Variable: |
| 4757 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4758 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4759 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4760 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4761 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4762 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4763 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4764 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4765 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4766 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4767 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4768 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4769 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4770 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4771 | return true; |
| 4772 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4773 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4774 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4775 | } |
| 4776 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4777 | /// \brief Whether the given entity, when initialized with an object |
| 4778 | /// created for that initialization, requires destruction. |
| 4779 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4780 | switch (Entity.getKind()) { |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4781 | case InitializedEntity::EK_Result: |
| 4782 | case InitializedEntity::EK_New: |
| 4783 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4784 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4785 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4786 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4787 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4788 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4789 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4790 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4791 | case InitializedEntity::EK_Member: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4792 | case InitializedEntity::EK_Variable: |
| 4793 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4794 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4795 | case InitializedEntity::EK_Temporary: |
| 4796 | case InitializedEntity::EK_ArrayElement: |
| 4797 | case InitializedEntity::EK_Exception: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4798 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4799 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4800 | return true; |
| 4801 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4802 | |
| 4803 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4804 | } |
| 4805 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4806 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4807 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4808 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4809 | OverloadCandidateSet &CandidateSet, |
| 4810 | CXXRecordDecl *Class, |
| 4811 | Expr *CurInitExpr) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4812 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4813 | // The container holding the constructors can under certain conditions |
| 4814 | // be changed while iterating (e.g. because of deserialization). |
| 4815 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4816 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4817 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4818 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4819 | NamedDecl *D = *CI; |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4820 | CXXConstructorDecl *Constructor = 0; |
| 4821 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4822 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4823 | // Handle copy/moveconstructors, only. |
| 4824 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4825 | !Constructor->isCopyOrMoveConstructor() || |
| 4826 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4827 | continue; |
| 4828 | |
| 4829 | DeclAccessPair FoundDecl |
| 4830 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4831 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4832 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4833 | continue; |
| 4834 | } |
| 4835 | |
| 4836 | // Handle constructor templates. |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4837 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4838 | if (ConstructorTmpl->isInvalidDecl()) |
| 4839 | continue; |
| 4840 | |
| 4841 | Constructor = cast<CXXConstructorDecl>( |
| 4842 | ConstructorTmpl->getTemplatedDecl()); |
| 4843 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4844 | continue; |
| 4845 | |
| 4846 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4847 | // candidates? |
| 4848 | DeclAccessPair FoundDecl |
| 4849 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4850 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4851 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4852 | } |
| 4853 | } |
| 4854 | |
| 4855 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4856 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4857 | Expr *Initializer) { |
| 4858 | switch (Entity.getKind()) { |
| 4859 | case InitializedEntity::EK_Result: |
| 4860 | return Entity.getReturnLoc(); |
| 4861 | |
| 4862 | case InitializedEntity::EK_Exception: |
| 4863 | return Entity.getThrowLoc(); |
| 4864 | |
| 4865 | case InitializedEntity::EK_Variable: |
| 4866 | return Entity.getDecl()->getLocation(); |
| 4867 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4868 | case InitializedEntity::EK_LambdaCapture: |
| 4869 | return Entity.getCaptureLoc(); |
| 4870 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4871 | case InitializedEntity::EK_ArrayElement: |
| 4872 | case InitializedEntity::EK_Member: |
| 4873 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4874 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4875 | case InitializedEntity::EK_Temporary: |
| 4876 | case InitializedEntity::EK_New: |
| 4877 | case InitializedEntity::EK_Base: |
| 4878 | case InitializedEntity::EK_Delegating: |
| 4879 | case InitializedEntity::EK_VectorElement: |
| 4880 | case InitializedEntity::EK_ComplexElement: |
| 4881 | case InitializedEntity::EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4882 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4883 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4884 | return Initializer->getLocStart(); |
| 4885 | } |
| 4886 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4887 | } |
| 4888 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4889 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4890 | /// provided by the given initializer by calling the appropriate copy |
| 4891 | /// constructor. |
| 4892 | /// |
| 4893 | /// \param S The Sema object used for type-checking. |
| 4894 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4895 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4896 | /// the type of the initializer expression or a superclass thereof. |
| 4897 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4898 | /// \param Entity The entity being initialized. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4899 | /// |
| 4900 | /// \param CurInit The initializer expression. |
| 4901 | /// |
| 4902 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4903 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4904 | /// an rvalue. |
| 4905 | /// |
| 4906 | /// \returns An expression that copies the initializer expression into |
| 4907 | /// a temporary object, or an error expression if a copy could not be |
| 4908 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4909 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4910 | QualType T, |
| 4911 | const InitializedEntity &Entity, |
| 4912 | ExprResult CurInit, |
| 4913 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4914 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4915 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4916 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4917 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4918 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4919 | if (!Class) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4920 | return CurInit; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4921 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4922 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4923 | // When certain criteria are met, an implementation is allowed to |
| 4924 | // omit the copy/move construction of a class object, even if the |
| 4925 | // copy/move constructor and/or destructor for the object have |
| 4926 | // side effects. [...] |
| 4927 | // - when a temporary class object that has not been bound to a |
| 4928 | // reference (12.2) would be copied/moved to a class object |
| 4929 | // with the same cv-unqualified type, the copy/move operation |
| 4930 | // can be omitted by constructing the temporary object |
| 4931 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4932 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4933 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4934 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4935 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4936 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4937 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4938 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4939 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4940 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4941 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4942 | return CurInit; |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4943 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4944 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4945 | // Only consider constructors and constructor templates. Per |
| 4946 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4947 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4948 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4949 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4950 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4951 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4952 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4953 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4954 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4955 | case OR_Success: |
| 4956 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4957 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4958 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4959 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4960 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4961 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4962 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4963 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4964 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4965 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4966 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4967 | return CurInit; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4968 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4969 | case OR_Ambiguous: |
| 4970 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4971 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4972 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4973 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4974 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4975 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4976 | case OR_Deleted: |
| 4977 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4978 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4979 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4980 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4981 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4982 | } |
| 4983 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4984 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4985 | SmallVector<Expr*, 8> ConstructorArgs; |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4986 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4987 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4988 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4989 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4990 | |
| 4991 | if (IsExtraneousCopy) { |
| 4992 | // If this is a totally extraneous copy for C++03 reference |
| 4993 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4994 | // expression. We don't generate an (elided) copy operation here |
| 4995 | // because doing so would require us to pass down a flag to avoid |
| 4996 | // infinite recursion, where each step adds another extraneous, |
| 4997 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4998 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4999 | // Instantiate the default arguments of any extra parameters in |
| 5000 | // the selected copy constructor, as if we were going to create a |
| 5001 | // proper call to the copy constructor. |
| 5002 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 5003 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 5004 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5005 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5006 | break; |
| 5007 | |
| 5008 | // Build the default argument expression; we don't actually care |
| 5009 | // if this succeeds or not, because this routine will complain |
| 5010 | // if there was a problem. |
| 5011 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 5012 | } |
| 5013 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5014 | return S.Owned(CurInitExpr); |
| 5015 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5016 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5017 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5018 | // constructor call (we might have derived-to-base conversions, or |
| 5019 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5020 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5021 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5022 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5023 | // Actually perform the constructor call. |
| 5024 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5025 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5026 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5027 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5028 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5029 | CXXConstructExpr::CK_Complete, |
| 5030 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5031 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5032 | // If we're supposed to bind temporaries, do so. |
| 5033 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 5034 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5035 | return CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5036 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5037 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5038 | /// \brief Check whether elidable copy construction for binding a reference to |
| 5039 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 5040 | /// -Wc++98-compat. |
| 5041 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 5042 | const InitializedEntity &Entity, |
| 5043 | Expr *CurInitExpr) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5044 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5045 | |
| 5046 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 5047 | if (!Record) |
| 5048 | return; |
| 5049 | |
| 5050 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 5051 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 5052 | == DiagnosticsEngine::Ignored) |
| 5053 | return; |
| 5054 | |
| 5055 | // Find constructors which would have been considered. |
| 5056 | OverloadCandidateSet CandidateSet(Loc); |
| 5057 | LookupCopyAndMoveConstructors( |
| 5058 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 5059 | |
| 5060 | // Perform overload resolution. |
| 5061 | OverloadCandidateSet::iterator Best; |
| 5062 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 5063 | |
| 5064 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 5065 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 5066 | << CurInitExpr->getSourceRange(); |
| 5067 | |
| 5068 | switch (OR) { |
| 5069 | case OR_Success: |
| 5070 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 5071 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5072 | // FIXME: Check default arguments as far as that's possible. |
| 5073 | break; |
| 5074 | |
| 5075 | case OR_No_Viable_Function: |
| 5076 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5077 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5078 | break; |
| 5079 | |
| 5080 | case OR_Ambiguous: |
| 5081 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5082 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5083 | break; |
| 5084 | |
| 5085 | case OR_Deleted: |
| 5086 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5087 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5088 | break; |
| 5089 | } |
| 5090 | } |
| 5091 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5092 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 5093 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5094 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5095 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 5096 | return; |
| 5097 | |
| 5098 | if (Entity.getDecl()->getDeclName()) |
| 5099 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 5100 | << Entity.getDecl()->getDeclName(); |
| 5101 | else |
| 5102 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 5103 | } |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5104 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 5105 | Entity.getMethodDecl()) |
| 5106 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 5107 | diag::note_method_return_type_change) |
| 5108 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5109 | } |
| 5110 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5111 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 5112 | return s.Kind == InitializationSequence::SK_BindReference || |
| 5113 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 5114 | } |
| 5115 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5116 | /// Returns true if the parameters describe a constructor initialization of |
| 5117 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 5118 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 5119 | const InitializationKind &Kind, |
| 5120 | unsigned NumArgs) { |
| 5121 | switch (Entity.getKind()) { |
| 5122 | case InitializedEntity::EK_Temporary: |
| 5123 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5124 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5125 | break; |
| 5126 | default: |
| 5127 | return false; |
| 5128 | } |
| 5129 | |
| 5130 | switch (Kind.getKind()) { |
| 5131 | case InitializationKind::IK_DirectList: |
| 5132 | return true; |
| 5133 | // FIXME: Hack to work around cast weirdness. |
| 5134 | case InitializationKind::IK_Direct: |
| 5135 | case InitializationKind::IK_Value: |
| 5136 | return NumArgs != 1; |
| 5137 | default: |
| 5138 | return false; |
| 5139 | } |
| 5140 | } |
| 5141 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5142 | static ExprResult |
| 5143 | PerformConstructorInitialization(Sema &S, |
| 5144 | const InitializedEntity &Entity, |
| 5145 | const InitializationKind &Kind, |
| 5146 | MultiExprArg Args, |
| 5147 | const InitializationSequence::Step& Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5148 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5149 | bool IsListInitialization, |
| 5150 | SourceLocation LBraceLoc, |
| 5151 | SourceLocation RBraceLoc) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5152 | unsigned NumArgs = Args.size(); |
| 5153 | CXXConstructorDecl *Constructor |
| 5154 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 5155 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 5156 | |
| 5157 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5158 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5159 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 5160 | ? Kind.getEqualLoc() |
| 5161 | : Kind.getLocation(); |
| 5162 | |
| 5163 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 5164 | // Force even a trivial, implicit default constructor to be |
| 5165 | // semantically checked. We do this explicitly because we don't build |
| 5166 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 5167 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5168 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 5169 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5170 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 5171 | } |
| 5172 | |
| 5173 | ExprResult CurInit = S.Owned((Expr *)0); |
| 5174 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5175 | // C++ [over.match.copy]p1: |
| 5176 | // - When initializing a temporary to be bound to the first parameter |
| 5177 | // of a constructor that takes a reference to possibly cv-qualified |
| 5178 | // T as its first argument, called with a single argument in the |
| 5179 | // context of direct-initialization, explicit conversion functions |
| 5180 | // are also considered. |
| 5181 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 5182 | Args.size() == 1 && |
| 5183 | Constructor->isCopyOrMoveConstructor(); |
| 5184 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5185 | // Determine the arguments required to actually perform the constructor |
| 5186 | // call. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5187 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5188 | Loc, ConstructorArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5189 | AllowExplicitConv, |
| 5190 | IsListInitialization)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5191 | return ExprError(); |
| 5192 | |
| 5193 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5194 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5195 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5196 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5197 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 5198 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5199 | |
| 5200 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5201 | if (!TSInfo) |
| 5202 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5203 | SourceRange ParenOrBraceRange = |
| 5204 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 5205 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 5206 | : Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5207 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5208 | CurInit = S.Owned( |
| 5209 | new (S.Context) CXXTemporaryObjectExpr(S.Context, Constructor, |
| 5210 | TSInfo, ConstructorArgs, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5211 | ParenOrBraceRange, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5212 | HadMultipleCandidates, |
Enea Zaffanella | 14dcaa9 | 2013-09-07 11:22:02 +0000 | [diff] [blame] | 5213 | IsListInitialization, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5214 | ConstructorInitRequiresZeroInit)); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5215 | } else { |
| 5216 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 5217 | CXXConstructExpr::CK_Complete; |
| 5218 | |
| 5219 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5220 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 5221 | CXXConstructExpr::CK_VirtualBase : |
| 5222 | CXXConstructExpr::CK_NonVirtualBase; |
| 5223 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 5224 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 5225 | } |
| 5226 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5227 | // Only get the parenthesis or brace range if it is a list initialization or |
| 5228 | // direct construction. |
| 5229 | SourceRange ParenOrBraceRange; |
| 5230 | if (IsListInitialization) |
| 5231 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 5232 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
| 5233 | ParenOrBraceRange = Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5234 | |
| 5235 | // If the entity allows NRVO, mark the construction as elidable |
| 5236 | // unconditionally. |
| 5237 | if (Entity.allowsNRVO()) |
| 5238 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5239 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5240 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5241 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5242 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5243 | ConstructorInitRequiresZeroInit, |
| 5244 | ConstructKind, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5245 | ParenOrBraceRange); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5246 | else |
| 5247 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5248 | Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5249 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5250 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5251 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5252 | ConstructorInitRequiresZeroInit, |
| 5253 | ConstructKind, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5254 | ParenOrBraceRange); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5255 | } |
| 5256 | if (CurInit.isInvalid()) |
| 5257 | return ExprError(); |
| 5258 | |
| 5259 | // Only check access if all of that succeeded. |
| 5260 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 5261 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5262 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 5263 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5264 | |
| 5265 | if (shouldBindAsTemporary(Entity)) |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5266 | CurInit = S.MaybeBindToTemporary(CurInit.take()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5267 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5268 | return CurInit; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5269 | } |
| 5270 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5271 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 5272 | /// longer than the current full-expression. Conservatively returns false if |
| 5273 | /// it's unclear. |
| 5274 | static bool |
| 5275 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 5276 | const InitializedEntity *Top = &Entity; |
| 5277 | while (Top->getParent()) |
| 5278 | Top = Top->getParent(); |
| 5279 | |
| 5280 | switch (Top->getKind()) { |
| 5281 | case InitializedEntity::EK_Variable: |
| 5282 | case InitializedEntity::EK_Result: |
| 5283 | case InitializedEntity::EK_Exception: |
| 5284 | case InitializedEntity::EK_Member: |
| 5285 | case InitializedEntity::EK_New: |
| 5286 | case InitializedEntity::EK_Base: |
| 5287 | case InitializedEntity::EK_Delegating: |
| 5288 | return true; |
| 5289 | |
| 5290 | case InitializedEntity::EK_ArrayElement: |
| 5291 | case InitializedEntity::EK_VectorElement: |
| 5292 | case InitializedEntity::EK_BlockElement: |
| 5293 | case InitializedEntity::EK_ComplexElement: |
| 5294 | // Could not determine what the full initialization is. Assume it might not |
| 5295 | // outlive the full-expression. |
| 5296 | return false; |
| 5297 | |
| 5298 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5299 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5300 | case InitializedEntity::EK_Temporary: |
| 5301 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5302 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5303 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5304 | // The entity being initialized might not outlive the full-expression. |
| 5305 | return false; |
| 5306 | } |
| 5307 | |
| 5308 | llvm_unreachable("unknown entity kind"); |
| 5309 | } |
| 5310 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5311 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 5312 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 5313 | /// the initialization of \p Entity. |
| 5314 | static const ValueDecl * |
| 5315 | getDeclForTemporaryLifetimeExtension(const InitializedEntity &Entity, |
| 5316 | const ValueDecl *FallbackDecl = 0) { |
| 5317 | // C++11 [class.temporary]p5: |
| 5318 | switch (Entity.getKind()) { |
| 5319 | case InitializedEntity::EK_Variable: |
| 5320 | // The temporary [...] persists for the lifetime of the reference |
| 5321 | return Entity.getDecl(); |
| 5322 | |
| 5323 | case InitializedEntity::EK_Member: |
| 5324 | // For subobjects, we look at the complete object. |
| 5325 | if (Entity.getParent()) |
| 5326 | return getDeclForTemporaryLifetimeExtension(*Entity.getParent(), |
| 5327 | Entity.getDecl()); |
| 5328 | |
| 5329 | // except: |
| 5330 | // -- A temporary bound to a reference member in a constructor's |
| 5331 | // ctor-initializer persists until the constructor exits. |
| 5332 | return Entity.getDecl(); |
| 5333 | |
| 5334 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5335 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5336 | // -- A temporary bound to a reference parameter in a function call |
| 5337 | // persists until the completion of the full-expression containing |
| 5338 | // the call. |
| 5339 | case InitializedEntity::EK_Result: |
| 5340 | // -- The lifetime of a temporary bound to the returned value in a |
| 5341 | // function return statement is not extended; the temporary is |
| 5342 | // destroyed at the end of the full-expression in the return statement. |
| 5343 | case InitializedEntity::EK_New: |
| 5344 | // -- A temporary bound to a reference in a new-initializer persists |
| 5345 | // until the completion of the full-expression containing the |
| 5346 | // new-initializer. |
| 5347 | return 0; |
| 5348 | |
| 5349 | case InitializedEntity::EK_Temporary: |
| 5350 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5351 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5352 | // We don't yet know the storage duration of the surrounding temporary. |
| 5353 | // Assume it's got full-expression duration for now, it will patch up our |
| 5354 | // storage duration if that's not correct. |
| 5355 | return 0; |
| 5356 | |
| 5357 | case InitializedEntity::EK_ArrayElement: |
| 5358 | // For subobjects, we look at the complete object. |
| 5359 | return getDeclForTemporaryLifetimeExtension(*Entity.getParent(), |
| 5360 | FallbackDecl); |
| 5361 | |
| 5362 | case InitializedEntity::EK_Base: |
| 5363 | case InitializedEntity::EK_Delegating: |
| 5364 | // We can reach this case for aggregate initialization in a constructor: |
| 5365 | // struct A { int &&r; }; |
| 5366 | // struct B : A { B() : A{0} {} }; |
| 5367 | // In this case, use the innermost field decl as the context. |
| 5368 | return FallbackDecl; |
| 5369 | |
| 5370 | case InitializedEntity::EK_BlockElement: |
| 5371 | case InitializedEntity::EK_LambdaCapture: |
| 5372 | case InitializedEntity::EK_Exception: |
| 5373 | case InitializedEntity::EK_VectorElement: |
| 5374 | case InitializedEntity::EK_ComplexElement: |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5375 | return 0; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5376 | } |
Benjamin Kramer | 6f773e8 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 5377 | llvm_unreachable("unknown entity kind"); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5378 | } |
| 5379 | |
| 5380 | static void performLifetimeExtension(Expr *Init, const ValueDecl *ExtendingD); |
| 5381 | |
| 5382 | /// Update a glvalue expression that is used as the initializer of a reference |
| 5383 | /// to note that its lifetime is extended. |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5384 | /// \return \c true if any temporary had its lifetime extended. |
| 5385 | static bool performReferenceExtension(Expr *Init, const ValueDecl *ExtendingD) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5386 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 5387 | if (ILE->getNumInits() == 1 && ILE->isGLValue()) { |
| 5388 | // This is just redundant braces around an initializer. Step over it. |
| 5389 | Init = ILE->getInit(0); |
| 5390 | } |
| 5391 | } |
| 5392 | |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5393 | // Walk past any constructs which we can lifetime-extend across. |
| 5394 | Expr *Old; |
| 5395 | do { |
| 5396 | Old = Init; |
| 5397 | |
| 5398 | // Step over any subobject adjustments; we may have a materialized |
| 5399 | // temporary inside them. |
| 5400 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5401 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5402 | Init = const_cast<Expr *>( |
| 5403 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5404 | |
| 5405 | // Per current approach for DR1376, look through casts to reference type |
| 5406 | // when performing lifetime extension. |
| 5407 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 5408 | if (CE->getSubExpr()->isGLValue()) |
| 5409 | Init = CE->getSubExpr(); |
| 5410 | |
| 5411 | // FIXME: Per DR1213, subscripting on an array temporary produces an xvalue. |
| 5412 | // It's unclear if binding a reference to that xvalue extends the array |
| 5413 | // temporary. |
| 5414 | } while (Init != Old); |
| 5415 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5416 | if (MaterializeTemporaryExpr *ME = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 5417 | // Update the storage duration of the materialized temporary. |
| 5418 | // FIXME: Rebuild the expression instead of mutating it. |
| 5419 | ME->setExtendingDecl(ExtendingD); |
| 5420 | performLifetimeExtension(ME->GetTemporaryExpr(), ExtendingD); |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5421 | return true; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5422 | } |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5423 | |
| 5424 | return false; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | /// Update a prvalue expression that is going to be materialized as a |
| 5428 | /// lifetime-extended temporary. |
| 5429 | static void performLifetimeExtension(Expr *Init, const ValueDecl *ExtendingD) { |
| 5430 | // Dig out the expression which constructs the extended temporary. |
| 5431 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5432 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5433 | Init = const_cast<Expr *>( |
| 5434 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5435 | |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5436 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 5437 | Init = BTE->getSubExpr(); |
| 5438 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5439 | if (CXXStdInitializerListExpr *ILE = |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5440 | dyn_cast<CXXStdInitializerListExpr>(Init)) { |
| 5441 | performReferenceExtension(ILE->getSubExpr(), ExtendingD); |
| 5442 | return; |
| 5443 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5444 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5445 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5446 | if (ILE->getType()->isArrayType()) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5447 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
| 5448 | performLifetimeExtension(ILE->getInit(I), ExtendingD); |
| 5449 | return; |
| 5450 | } |
| 5451 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5452 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5453 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 5454 | |
| 5455 | // If we lifetime-extend a braced initializer which is initializing an |
| 5456 | // aggregate, and that aggregate contains reference members which are |
| 5457 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 5458 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 5459 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
| 5460 | performReferenceExtension(ILE->getInit(0), ExtendingD); |
| 5461 | else { |
| 5462 | unsigned Index = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5463 | for (const auto *I : RD->fields()) { |
Richard Smith | 3c3af14 | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 5464 | if (Index >= ILE->getNumInits()) |
| 5465 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5466 | if (I->isUnnamedBitfield()) |
| 5467 | continue; |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5468 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5469 | if (I->getType()->isReferenceType()) |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5470 | performReferenceExtension(SubInit, ExtendingD); |
| 5471 | else if (isa<InitListExpr>(SubInit) || |
| 5472 | isa<CXXStdInitializerListExpr>(SubInit)) |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5473 | // This may be either aggregate-initialization of a member or |
| 5474 | // initialization of a std::initializer_list object. Either way, |
| 5475 | // we should recursively lifetime-extend that initializer. |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5476 | performLifetimeExtension(SubInit, ExtendingD); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5477 | ++Index; |
| 5478 | } |
| 5479 | } |
| 5480 | } |
| 5481 | } |
| 5482 | } |
| 5483 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5484 | static void warnOnLifetimeExtension(Sema &S, const InitializedEntity &Entity, |
| 5485 | const Expr *Init, bool IsInitializerList, |
| 5486 | const ValueDecl *ExtendingDecl) { |
| 5487 | // Warn if a field lifetime-extends a temporary. |
| 5488 | if (isa<FieldDecl>(ExtendingDecl)) { |
| 5489 | if (IsInitializerList) { |
| 5490 | S.Diag(Init->getExprLoc(), diag::warn_dangling_std_initializer_list) |
| 5491 | << /*at end of constructor*/true; |
| 5492 | return; |
| 5493 | } |
| 5494 | |
| 5495 | bool IsSubobjectMember = false; |
| 5496 | for (const InitializedEntity *Ent = Entity.getParent(); Ent; |
| 5497 | Ent = Ent->getParent()) { |
| 5498 | if (Ent->getKind() != InitializedEntity::EK_Base) { |
| 5499 | IsSubobjectMember = true; |
| 5500 | break; |
| 5501 | } |
| 5502 | } |
| 5503 | S.Diag(Init->getExprLoc(), |
| 5504 | diag::warn_bind_ref_member_to_temporary) |
| 5505 | << ExtendingDecl << Init->getSourceRange() |
| 5506 | << IsSubobjectMember << IsInitializerList; |
| 5507 | if (IsSubobjectMember) |
| 5508 | S.Diag(ExtendingDecl->getLocation(), |
| 5509 | diag::note_ref_subobject_of_member_declared_here); |
| 5510 | else |
| 5511 | S.Diag(ExtendingDecl->getLocation(), |
| 5512 | diag::note_ref_or_ptr_member_declared_here) |
| 5513 | << /*is pointer*/false; |
| 5514 | } |
| 5515 | } |
| 5516 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5517 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 5518 | const ImplicitConversionSequence &ICS, |
| 5519 | QualType PreNarrowingType, |
| 5520 | QualType EntityType, |
| 5521 | const Expr *PostInit); |
| 5522 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5523 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5524 | InitializationSequence::Perform(Sema &S, |
| 5525 | const InitializedEntity &Entity, |
| 5526 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5527 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5528 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5529 | if (Failed()) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5530 | Diagnose(S, Entity, Kind, Args); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5531 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5532 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5533 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5534 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5535 | // If the declaration is a non-dependent, incomplete array type |
| 5536 | // that has an initializer, then its type will be completed once |
| 5537 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5538 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5539 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5540 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5541 | if (const IncompleteArrayType *ArrayT |
| 5542 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5543 | // FIXME: We don't currently have the ability to accurately |
| 5544 | // compute the length of an initializer list without |
| 5545 | // performing full type-checking of the initializer list |
| 5546 | // (since we have to determine where braces are implicitly |
| 5547 | // introduced and such). So, we fall back to making the array |
| 5548 | // type a dependently-sized array type with no specified |
| 5549 | // bound. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5550 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5551 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5552 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5553 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5554 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5555 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5556 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5557 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5558 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5559 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5560 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
| 5563 | *ResultType |
| 5564 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 5565 | /*NumElts=*/0, |
| 5566 | ArrayT->getSizeModifier(), |
| 5567 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5568 | Brackets); |
| 5569 | } |
| 5570 | |
| 5571 | } |
| 5572 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5573 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5574 | !Kind.isExplicitCast()) { |
| 5575 | // Rebuild the ParenListExpr. |
| 5576 | SourceRange ParenRange = Kind.getParenRange(); |
| 5577 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5578 | Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5579 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5580 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5581 | Kind.isExplicitCast() || |
| 5582 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5583 | return ExprResult(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5584 | } |
| 5585 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5586 | // No steps means no initialization. |
| 5587 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5588 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5589 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5590 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5591 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5592 | !Entity.isParameterKind()) { |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5593 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5594 | // from an initializer list. For parameters, we produce a better warning |
| 5595 | // elsewhere. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5596 | Expr *Init = Args[0]; |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5597 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5598 | << Init->getSourceRange(); |
| 5599 | } |
| 5600 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5601 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5602 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5603 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5604 | Entity.getType()->isPointerType() && |
| 5605 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5606 | Expr *Init = Args[0]; |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5607 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5608 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5609 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5610 | << Init->getSourceRange(); |
| 5611 | } |
| 5612 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5613 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5614 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5615 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5616 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5617 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5618 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5619 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5620 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5621 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5622 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5623 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5624 | // grab the only argument out the Args and place it into the "current" |
| 5625 | // initializer. |
| 5626 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5627 | case SK_ResolveAddressOfOverloadedFunction: |
| 5628 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5629 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5630 | case SK_CastDerivedToBaseLValue: |
| 5631 | case SK_BindReference: |
| 5632 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5633 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5634 | case SK_UserConversion: |
| 5635 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5636 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5637 | case SK_QualificationConversionRValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5638 | case SK_LValueToRValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5639 | case SK_ConversionSequence: |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5640 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5641 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5642 | case SK_UnwrapInitList: |
| 5643 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5644 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5645 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5646 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5647 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5648 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5649 | case SK_PassByIndirectCopyRestore: |
| 5650 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5651 | case SK_ProduceObjCObject: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5652 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5653 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5654 | case SK_OCLZeroEvent: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5655 | assert(Args.size() == 1); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5656 | CurInit = Args[0]; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5657 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5658 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5659 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5660 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5661 | case SK_ConstructorInitialization: |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5662 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5663 | case SK_ZeroInitialization: |
| 5664 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5665 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5666 | |
| 5667 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5668 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5669 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5670 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5671 | Step != StepEnd; ++Step) { |
| 5672 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5673 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5674 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5675 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5676 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5677 | switch (Step->Kind) { |
| 5678 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5679 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5680 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5681 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5682 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5683 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5684 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5685 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5686 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5687 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5688 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5689 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5690 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5691 | case SK_CastDerivedToBaseLValue: { |
| 5692 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5693 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5694 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5695 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5696 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5697 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5698 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5699 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5700 | CurInit.get()->getLocStart(), |
| 5701 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5702 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5703 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5704 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5705 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5706 | QualType T = SourceType; |
| 5707 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5708 | T = Pointer->getPointeeType(); |
| 5709 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5710 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5711 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5712 | } |
| 5713 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5714 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5715 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5716 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5717 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5718 | VK_XValue : |
| 5719 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5720 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 5721 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5722 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5723 | CurInit.get(), |
| 5724 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5725 | break; |
| 5726 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5727 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5728 | case SK_BindReference: |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5729 | // References cannot bind to bit-fields (C++ [dcl.init.ref]p5). |
| 5730 | if (CurInit.get()->refersToBitField()) { |
| 5731 | // We don't necessarily have an unambiguous source bit-field. |
| 5732 | FieldDecl *BitField = CurInit.get()->getSourceBitField(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5733 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5734 | << Entity.getType().isVolatileQualified() |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5735 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 5736 | << (BitField != NULL) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5737 | << CurInit.get()->getSourceRange(); |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5738 | if (BitField) |
| 5739 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 5740 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5741 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5742 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5743 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5744 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5745 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5746 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5747 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5748 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5749 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5750 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5751 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5752 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5753 | // Reference binding does not have any corresponding ASTs. |
| 5754 | |
| 5755 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5756 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5757 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5758 | |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5759 | // Even though we didn't materialize a temporary, the binding may still |
| 5760 | // extend the lifetime of a temporary. This happens if we bind a reference |
| 5761 | // to the result of a cast to reference type. |
| 5762 | if (const ValueDecl *ExtendingDecl = |
| 5763 | getDeclForTemporaryLifetimeExtension(Entity)) { |
| 5764 | if (performReferenceExtension(CurInit.get(), ExtendingDecl)) |
| 5765 | warnOnLifetimeExtension(S, Entity, CurInit.get(), false, |
| 5766 | ExtendingDecl); |
| 5767 | } |
| 5768 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5769 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5770 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5771 | case SK_BindReferenceToTemporary: { |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5772 | // Make sure the "temporary" is actually an rvalue. |
| 5773 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5774 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5775 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5776 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5777 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5778 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5779 | // Maybe lifetime-extend the temporary's subobjects to match the |
| 5780 | // entity's lifetime. |
| 5781 | const ValueDecl *ExtendingDecl = |
| 5782 | getDeclForTemporaryLifetimeExtension(Entity); |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 5783 | if (ExtendingDecl) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5784 | performLifetimeExtension(CurInit.get(), ExtendingDecl); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5785 | warnOnLifetimeExtension(S, Entity, CurInit.get(), false, ExtendingDecl); |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 5786 | } |
| 5787 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5788 | // Materialize the temporary into memory. |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5789 | MaterializeTemporaryExpr *MTE = new (S.Context) MaterializeTemporaryExpr( |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5790 | Entity.getType().getNonReferenceType(), CurInit.get(), |
| 5791 | Entity.getType()->isLValueReferenceType(), ExtendingDecl); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5792 | |
| 5793 | // If we're binding to an Objective-C object that has lifetime, we |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5794 | // need cleanups. Likewise if we're extending this temporary to automatic |
| 5795 | // storage duration -- we need to register its cleanup during the |
| 5796 | // full-expression's cleanups. |
| 5797 | if ((S.getLangOpts().ObjCAutoRefCount && |
| 5798 | MTE->getType()->isObjCLifetimeType()) || |
| 5799 | (MTE->getStorageDuration() == SD_Automatic && |
| 5800 | MTE->getType().isDestructedType())) |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5801 | S.ExprNeedsCleanups = true; |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5802 | |
| 5803 | CurInit = S.Owned(MTE); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5804 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5805 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5806 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5807 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5808 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5809 | /*IsExtraneousCopy=*/true); |
| 5810 | break; |
| 5811 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5812 | case SK_UserConversion: { |
| 5813 | // We have a user-defined conversion that invokes either a constructor |
| 5814 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5815 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5816 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5817 | FunctionDecl *Fn = Step->Function.Function; |
| 5818 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5819 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5820 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5821 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5822 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5823 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5824 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5825 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5826 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5827 | // Determine the arguments required to actually perform the constructor |
| 5828 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5829 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5830 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5831 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5832 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5833 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5834 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5835 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5836 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5837 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5838 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5839 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5840 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5841 | CXXConstructExpr::CK_Complete, |
| 5842 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5843 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5844 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5845 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5846 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5847 | FoundFn.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5848 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5849 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5850 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5851 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5852 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5853 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5854 | S.IsDerivedFrom(SourceType, Class)) |
| 5855 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5856 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5857 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5858 | } else { |
| 5859 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5860 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5861 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5862 | FoundFn); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5863 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5864 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5865 | |
| 5866 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5867 | // derived-to-base conversion? I believe the answer is "no", because |
| 5868 | // we don't want to turn off access control here for c-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5869 | ExprResult CurInitExprRes = |
| 5870 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 5871 | FoundFn, Conversion); |
| 5872 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5873 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5874 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5875 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5876 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5877 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5878 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5879 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5880 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5881 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5882 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5883 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5884 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5885 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5886 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5887 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5888 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5889 | |
| 5890 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5891 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5892 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5893 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5894 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5895 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5896 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5897 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5898 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 5899 | return ExprError(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5900 | } |
| 5901 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5902 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5903 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5904 | CurInit.get()->getType(), |
| 5905 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5906 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5907 | if (MaybeBindToTemp) |
| 5908 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5909 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5910 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5911 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5912 | break; |
| 5913 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5914 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5915 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5916 | case SK_QualificationConversionXValue: |
| 5917 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5918 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5919 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5920 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5921 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5922 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5923 | VK_XValue : |
| 5924 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5925 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5926 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5927 | } |
| 5928 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5929 | case SK_LValueToRValue: { |
| 5930 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
| 5931 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 5932 | CK_LValueToRValue, |
| 5933 | CurInit.take(), |
| 5934 | /*BasePath=*/0, |
| 5935 | VK_RValue)); |
| 5936 | break; |
| 5937 | } |
| 5938 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5939 | case SK_ConversionSequence: |
| 5940 | case SK_ConversionSequenceNoNarrowing: { |
| 5941 | Sema::CheckedConversionKind CCK |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5942 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5943 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5944 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5945 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5946 | ExprResult CurInitExprRes = |
| 5947 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5948 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5949 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5950 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5951 | CurInit = CurInitExprRes; |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5952 | |
| 5953 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
| 5954 | S.getLangOpts().CPlusPlus && !CurInit.get()->isValueDependent()) |
| 5955 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 5956 | CurInit.get()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5957 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5958 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5959 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5960 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5961 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5962 | // If we're not initializing the top-level entity, we need to create an |
| 5963 | // InitializeTemporary entity for our target type. |
| 5964 | QualType Ty = Step->Type; |
| 5965 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5966 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5967 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 5968 | InitListChecker PerformInitList(S, InitEntity, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 5969 | InitList, Ty, /*VerifyOnly=*/false); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5970 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5971 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5972 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5973 | // Hack: We must update *ResultType if available in order to set the |
| 5974 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5975 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5976 | if (ResultType && |
| 5977 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5978 | if ((*ResultType)->isRValueReferenceType()) |
| 5979 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5980 | else if ((*ResultType)->isLValueReferenceType()) |
| 5981 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5982 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5983 | *ResultType = Ty; |
| 5984 | } |
| 5985 | |
| 5986 | InitListExpr *StructuredInitList = |
| 5987 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5988 | CurInit.release(); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5989 | CurInit = shouldBindAsTemporary(InitEntity) |
| 5990 | ? S.MaybeBindToTemporary(StructuredInitList) |
| 5991 | : S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5992 | break; |
| 5993 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5994 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5995 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5996 | // When an initializer list is passed for a parameter of type "reference |
| 5997 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5998 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5999 | // FIXME: This is a hack. What we really should do is create a user |
| 6000 | // conversion step for this case, but this makes it considerably more |
| 6001 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6002 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6003 | Entity.getType().getNonReferenceType()); |
| 6004 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6005 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6006 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 6007 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 6008 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6009 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6010 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 6011 | Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6012 | Kind, Arg, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6013 | ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6014 | /*IsListInitialization*/ true, |
| 6015 | InitList->getLBraceLoc(), |
| 6016 | InitList->getRBraceLoc()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6017 | break; |
| 6018 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6019 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6020 | case SK_UnwrapInitList: |
| 6021 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 6022 | break; |
| 6023 | |
| 6024 | case SK_RewrapInitList: { |
| 6025 | Expr *E = CurInit.take(); |
| 6026 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 6027 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 6028 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6029 | ILE->setSyntacticForm(Syntactic); |
| 6030 | ILE->setType(E->getType()); |
| 6031 | ILE->setValueKind(E->getValueKind()); |
| 6032 | CurInit = S.Owned(ILE); |
| 6033 | break; |
| 6034 | } |
| 6035 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6036 | case SK_ConstructorInitialization: { |
| 6037 | // When an initializer list is passed for a parameter of type "reference |
| 6038 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6039 | // EK_Parameter entity with reference type. |
| 6040 | // FIXME: This is a hack. What we really should do is create a user |
| 6041 | // conversion step for this case, but this makes it considerably more |
| 6042 | // complicated. For now, this will do. |
| 6043 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6044 | Entity.getType().getNonReferenceType()); |
| 6045 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 6046 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 6047 | : Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6048 | Kind, Args, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6049 | ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6050 | /*IsListInitialization*/ false, |
| 6051 | /*LBraceLoc*/ SourceLocation(), |
| 6052 | /*RBraceLoc*/ SourceLocation()); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6053 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6054 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6055 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6056 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6057 | step_iterator NextStep = Step; |
| 6058 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6059 | if (NextStep != StepEnd && |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6060 | (NextStep->Kind == SK_ConstructorInitialization || |
| 6061 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6062 | // The need for zero-initialization is recorded directly into |
| 6063 | // the call to the object's constructor within the next step. |
| 6064 | ConstructorInitRequiresZeroInit = true; |
| 6065 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6066 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6067 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6068 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6069 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6070 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6071 | Kind.getRange().getBegin()); |
| 6072 | |
| 6073 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 6074 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 6075 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6076 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6077 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6078 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6079 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6080 | break; |
| 6081 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6082 | |
| 6083 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6084 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6085 | ExprResult Result = CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6086 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 01ad048 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 6087 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 6088 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6089 | if (Result.isInvalid()) |
| 6090 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6091 | CurInit = Result; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6092 | |
| 6093 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6094 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6095 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6096 | Entity.isParameterKind() && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6097 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6098 | == Sema::Compatible) |
| 6099 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6100 | if (CurInitExprRes.isInvalid()) |
| 6101 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6102 | CurInit = CurInitExprRes; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6103 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6104 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6105 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 6106 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6107 | CurInit.get(), |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 6108 | getAssignmentAction(Entity, true), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6109 | &Complained)) { |
| 6110 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6111 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6112 | } else if (Complained) |
| 6113 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6114 | break; |
| 6115 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6116 | |
| 6117 | case SK_StringInit: { |
| 6118 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6119 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 6120 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6121 | break; |
| 6122 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6123 | |
| 6124 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6125 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6126 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 6127 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6128 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6129 | |
| 6130 | case SK_ArrayInit: |
| 6131 | // Okay: we checked everything before creating this step. Note that |
| 6132 | // this is a GNU extension. |
| 6133 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6134 | << Step->Type << CurInit.get()->getType() |
| 6135 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6136 | |
| 6137 | // If the destination type is an incomplete array type, update the |
| 6138 | // type accordingly. |
| 6139 | if (ResultType) { |
| 6140 | if (const IncompleteArrayType *IncompleteDest |
| 6141 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 6142 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6143 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6144 | *ResultType = S.Context.getConstantArrayType( |
| 6145 | IncompleteDest->getElementType(), |
| 6146 | ConstantSource->getSize(), |
| 6147 | ArrayType::Normal, 0); |
| 6148 | } |
| 6149 | } |
| 6150 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6151 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6152 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6153 | case SK_ParenthesizedArrayInit: |
| 6154 | // Okay: we checked everything before creating this step. Note that |
| 6155 | // this is a GNU extension. |
| 6156 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 6157 | << CurInit.get()->getSourceRange(); |
| 6158 | break; |
| 6159 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6160 | case SK_PassByIndirectCopyRestore: |
| 6161 | case SK_PassByIndirectRestore: |
| 6162 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 6163 | CurInit = S.Owned(new (S.Context) |
| 6164 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 6165 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 6166 | break; |
| 6167 | |
| 6168 | case SK_ProduceObjCObject: |
| 6169 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6170 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6171 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6172 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6173 | |
| 6174 | case SK_StdInitializerList: { |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6175 | S.Diag(CurInit.get()->getExprLoc(), |
| 6176 | diag::warn_cxx98_compat_initializer_list_init) |
| 6177 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6178 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6179 | // Maybe lifetime-extend the array temporary's subobjects to match the |
| 6180 | // entity's lifetime. |
| 6181 | const ValueDecl *ExtendingDecl = |
| 6182 | getDeclForTemporaryLifetimeExtension(Entity); |
| 6183 | if (ExtendingDecl) { |
| 6184 | performLifetimeExtension(CurInit.get(), ExtendingDecl); |
| 6185 | warnOnLifetimeExtension(S, Entity, CurInit.get(), true, ExtendingDecl); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6186 | } |
| 6187 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6188 | // Materialize the temporary into memory. |
| 6189 | MaterializeTemporaryExpr *MTE = new (S.Context) |
| 6190 | MaterializeTemporaryExpr(CurInit.get()->getType(), CurInit.get(), |
| 6191 | /*lvalue reference*/ false, ExtendingDecl); |
| 6192 | |
| 6193 | // Wrap it in a construction of a std::initializer_list<T>. |
| 6194 | CurInit = S.Owned( |
| 6195 | new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE)); |
| 6196 | |
| 6197 | // Bind the result, in case the library has given initializer_list a |
| 6198 | // non-trivial destructor. |
| 6199 | if (shouldBindAsTemporary(Entity)) |
| 6200 | CurInit = S.MaybeBindToTemporary(CurInit.take()); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6201 | break; |
| 6202 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6203 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6204 | case SK_OCLSamplerInit: { |
| 6205 | assert(Step->Type->isSamplerT() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 6206 | "Sampler initialization on non-sampler type."); |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6207 | |
| 6208 | QualType SourceType = CurInit.get()->getType(); |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6209 | |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6210 | if (Entity.isParameterKind()) { |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6211 | if (!SourceType->isSamplerT()) |
| 6212 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 6213 | << SourceType; |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6214 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6215 | llvm_unreachable("Invalid EntityKind!"); |
| 6216 | } |
| 6217 | |
| 6218 | break; |
| 6219 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6220 | case SK_OCLZeroEvent: { |
| 6221 | assert(Step->Type->isEventT() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 6222 | "Event initialization on non-event type."); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6223 | |
| 6224 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
| 6225 | CK_ZeroToOCLEvent, |
| 6226 | CurInit.get()->getValueKind()); |
| 6227 | break; |
| 6228 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6229 | } |
| 6230 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6231 | |
| 6232 | // Diagnose non-fatal problems with the completed initialization. |
| 6233 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6234 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 6235 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 6236 | cast<FieldDecl>(Entity.getDecl()), |
| 6237 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6238 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6239 | return CurInit; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6240 | } |
| 6241 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6242 | /// Somewhere within T there is an uninitialized reference subobject. |
| 6243 | /// Dig it out and diagnose it. |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 6244 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 6245 | QualType T) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6246 | if (T->isReferenceType()) { |
| 6247 | S.Diag(Loc, diag::err_reference_without_init) |
| 6248 | << T.getNonReferenceType(); |
| 6249 | return true; |
| 6250 | } |
| 6251 | |
| 6252 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6253 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 6254 | return false; |
| 6255 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 6256 | for (const auto *FI : RD->fields()) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6257 | if (FI->isUnnamedBitfield()) |
| 6258 | continue; |
| 6259 | |
| 6260 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 6261 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6262 | return true; |
| 6263 | } |
| 6264 | } |
| 6265 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 6266 | for (const auto &BI : RD->bases()) { |
| 6267 | if (DiagnoseUninitializedReference(S, BI.getLocStart(), BI.getType())) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6268 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6269 | return true; |
| 6270 | } |
| 6271 | } |
| 6272 | |
| 6273 | return false; |
| 6274 | } |
| 6275 | |
| 6276 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6277 | //===----------------------------------------------------------------------===// |
| 6278 | // Diagnose initialization failures |
| 6279 | //===----------------------------------------------------------------------===// |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6280 | |
| 6281 | /// Emit notes associated with an initialization that failed due to a |
| 6282 | /// "simple" conversion failure. |
| 6283 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 6284 | Expr *op) { |
| 6285 | QualType destType = entity.getType(); |
| 6286 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 6287 | op->getType()->isObjCObjectPointerType()) { |
| 6288 | |
| 6289 | // Emit a possible note about the conversion failing because the |
| 6290 | // operand is a message send with a related result type. |
| 6291 | S.EmitRelatedResultTypeNote(op); |
| 6292 | |
| 6293 | // Emit a possible note about a return failing because we're |
| 6294 | // expecting a related result type. |
| 6295 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 6296 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 6297 | } |
| 6298 | } |
| 6299 | |
Bill Wendling | 2ca3db4 | 2013-11-22 00:01:44 +0000 | [diff] [blame] | 6300 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 6301 | InitListExpr *InitList) { |
| 6302 | QualType DestType = Entity.getType(); |
| 6303 | |
| 6304 | QualType E; |
| 6305 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 6306 | QualType ArrayType = S.Context.getConstantArrayType( |
| 6307 | E.withConst(), |
| 6308 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 6309 | InitList->getNumInits()), |
| 6310 | clang::ArrayType::Normal, 0); |
| 6311 | InitializedEntity HiddenArray = |
| 6312 | InitializedEntity::InitializeTemporary(ArrayType); |
| 6313 | return diagnoseListInit(S, HiddenArray, InitList); |
| 6314 | } |
| 6315 | |
| 6316 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
| 6317 | /*VerifyOnly=*/false); |
| 6318 | assert(DiagnoseInitList.HadError() && |
| 6319 | "Inconsistent init list check result."); |
| 6320 | } |
| 6321 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6322 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6323 | const InitializedEntity &Entity, |
| 6324 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6325 | ArrayRef<Expr *> Args) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 6326 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6327 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6328 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 6329 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6330 | switch (Failure) { |
| 6331 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6332 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6333 | if (Args.empty()) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6334 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 6335 | // If this is value-initialization, this could be nested some way within |
| 6336 | // the target type. |
| 6337 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 6338 | DestType->isReferenceType()); |
| 6339 | bool Diagnosed = |
| 6340 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 6341 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 6342 | (void)Diagnosed; |
| 6343 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6344 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6345 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6346 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6347 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6348 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6349 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6350 | break; |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6351 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6352 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 6353 | break; |
| 6354 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6355 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 6356 | break; |
| 6357 | case FK_NarrowStringIntoWideCharArray: |
| 6358 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 6359 | break; |
| 6360 | case FK_WideStringIntoCharArray: |
| 6361 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 6362 | break; |
| 6363 | case FK_IncompatWideStringIntoWideChar: |
| 6364 | S.Diag(Kind.getLocation(), |
| 6365 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 6366 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6367 | case FK_ArrayTypeMismatch: |
| 6368 | case FK_NonConstantArrayInit: |
Bill Wendling | 2ca3db4 | 2013-11-22 00:01:44 +0000 | [diff] [blame] | 6369 | S.Diag(Kind.getLocation(), |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6370 | (Failure == FK_ArrayTypeMismatch |
| 6371 | ? diag::err_array_init_different_type |
| 6372 | : diag::err_array_init_non_constant_array)) |
| 6373 | << DestType.getNonReferenceType() |
| 6374 | << Args[0]->getType() |
| 6375 | << Args[0]->getSourceRange(); |
| 6376 | break; |
| 6377 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6378 | case FK_VariableLengthArrayHasInitializer: |
| 6379 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 6380 | << Args[0]->getSourceRange(); |
| 6381 | break; |
| 6382 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6383 | case FK_AddressOfOverloadFailed: { |
| 6384 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6385 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6386 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6387 | true, |
| 6388 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6389 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6390 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6391 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6392 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 6393 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6394 | switch (FailedOverloadResult) { |
| 6395 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6396 | if (Failure == FK_UserConversionOverloadFailed) |
| 6397 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 6398 | << Args[0]->getType() << DestType |
| 6399 | << Args[0]->getSourceRange(); |
| 6400 | else |
| 6401 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 6402 | << DestType << Args[0]->getType() |
| 6403 | << Args[0]->getSourceRange(); |
| 6404 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6405 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6406 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6407 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6408 | case OR_No_Viable_Function: |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 6409 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 6410 | DestType.getNonReferenceType(), |
| 6411 | diag::err_typecheck_nonviable_condition_incomplete, |
| 6412 | Args[0]->getType(), Args[0]->getSourceRange())) |
| 6413 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 6414 | << Args[0]->getType() << Args[0]->getSourceRange() |
| 6415 | << DestType.getNonReferenceType(); |
| 6416 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6417 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6418 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6419 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6420 | case OR_Deleted: { |
| 6421 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 6422 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 6423 | << Args[0]->getSourceRange(); |
| 6424 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6425 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6426 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 6427 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6428 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6429 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6430 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6431 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6432 | } |
| 6433 | break; |
| 6434 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6435 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6436 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6437 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6438 | } |
| 6439 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6440 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6441 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6442 | if (isa<InitListExpr>(Args[0])) { |
| 6443 | S.Diag(Kind.getLocation(), |
| 6444 | diag::err_lvalue_reference_bind_to_initlist) |
| 6445 | << DestType.getNonReferenceType().isVolatileQualified() |
| 6446 | << DestType.getNonReferenceType() |
| 6447 | << Args[0]->getSourceRange(); |
| 6448 | break; |
| 6449 | } |
| 6450 | // Intentional fallthrough |
| 6451 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6452 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6453 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6454 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 6455 | ? diag::err_lvalue_reference_bind_to_temporary |
| 6456 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 6457 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6458 | << DestType.getNonReferenceType() |
| 6459 | << Args[0]->getType() |
| 6460 | << Args[0]->getSourceRange(); |
| 6461 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6462 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6463 | case FK_RValueReferenceBindingToLValue: |
| 6464 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 6465 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6466 | << Args[0]->getSourceRange(); |
| 6467 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6468 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6469 | case FK_ReferenceInitDropsQualifiers: |
| 6470 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 6471 | << DestType.getNonReferenceType() |
| 6472 | << Args[0]->getType() |
| 6473 | << Args[0]->getSourceRange(); |
| 6474 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6475 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6476 | case FK_ReferenceInitFailed: |
| 6477 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 6478 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6479 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6480 | << Args[0]->getType() |
| 6481 | << Args[0]->getSourceRange(); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6482 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6483 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6484 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6485 | case FK_ConversionFailed: { |
| 6486 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6487 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6488 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6489 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6490 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6491 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6492 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6493 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 6494 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6495 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6496 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6497 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6498 | |
| 6499 | case FK_ConversionFromPropertyFailed: |
| 6500 | // No-op. This error has already been reported. |
| 6501 | break; |
| 6502 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6503 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6504 | SourceRange R; |
| 6505 | |
| 6506 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6507 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6508 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6509 | else |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6510 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6511 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6512 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 6513 | if (Kind.isCStyleOrFunctionalCast()) |
| 6514 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 6515 | << R; |
| 6516 | else |
| 6517 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 6518 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6519 | break; |
| 6520 | } |
| 6521 | |
| 6522 | case FK_ReferenceBindingToInitList: |
| 6523 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 6524 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 6525 | break; |
| 6526 | |
| 6527 | case FK_InitListBadDestinationType: |
| 6528 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 6529 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 6530 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6531 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6532 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6533 | case FK_ConstructorOverloadFailed: { |
| 6534 | SourceRange ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6535 | if (Args.size()) |
| 6536 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 6537 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6538 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6539 | if (Failure == FK_ListConstructorOverloadFailed) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6540 | assert(Args.size() == 1 && "List construction from other than 1 argument."); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6541 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6542 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6543 | } |
| 6544 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6545 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 6546 | // bad. |
| 6547 | switch (FailedOverloadResult) { |
| 6548 | case OR_Ambiguous: |
| 6549 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 6550 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6551 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6552 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6553 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6554 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6555 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 6556 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 6557 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 6558 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6559 | // This is implicit default initialization of a member or |
| 6560 | // base within a constructor. If no viable function was |
| 6561 | // found, notify the user that she needs to explicitly |
| 6562 | // initialize this base/member. |
| 6563 | CXXConstructorDecl *Constructor |
| 6564 | = cast<CXXConstructorDecl>(S.CurContext); |
| 6565 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6566 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6567 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6568 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6569 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6570 | << /*base=*/0 |
| 6571 | << Entity.getType(); |
| 6572 | |
| 6573 | RecordDecl *BaseDecl |
| 6574 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 6575 | ->getDecl(); |
| 6576 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 6577 | << S.Context.getTagDeclType(BaseDecl); |
| 6578 | } else { |
| 6579 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6580 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6581 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6582 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6583 | << /*member=*/1 |
| 6584 | << Entity.getName(); |
| 6585 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 6586 | |
| 6587 | if (const RecordType *Record |
| 6588 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6589 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6590 | diag::note_previous_decl) |
| 6591 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6592 | } |
| 6593 | break; |
| 6594 | } |
| 6595 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6596 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6597 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6598 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6599 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6600 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6601 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6602 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6603 | OverloadingResult Ovl |
| 6604 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6605 | if (Ovl != OR_Deleted) { |
| 6606 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6607 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6608 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6609 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6610 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6611 | |
| 6612 | // If this is a defaulted or implicitly-declared function, then |
| 6613 | // it was implicitly deleted. Make it clear that the deletion was |
| 6614 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6615 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6616 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6617 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6618 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6619 | else |
| 6620 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6621 | << true << DestType << ArgsRange; |
| 6622 | |
| 6623 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6624 | break; |
| 6625 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6626 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6627 | case OR_Success: |
| 6628 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6629 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6630 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6631 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6632 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6633 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6634 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6635 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6636 | // This is implicit default-initialization of a const member in |
| 6637 | // a constructor. Complain that it needs to be explicitly |
| 6638 | // initialized. |
| 6639 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6640 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6641 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6642 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6643 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6644 | << /*const=*/1 |
| 6645 | << Entity.getName(); |
| 6646 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6647 | << Entity.getName(); |
| 6648 | } else { |
| 6649 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 6650 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6651 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6652 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6653 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6654 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6655 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6656 | diag::err_init_incomplete_type); |
| 6657 | break; |
| 6658 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6659 | case FK_ListInitializationFailed: { |
| 6660 | // Run the init list checker again to emit diagnostics. |
Bill Wendling | 2ca3db4 | 2013-11-22 00:01:44 +0000 | [diff] [blame] | 6661 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 6662 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6663 | break; |
| 6664 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6665 | |
| 6666 | case FK_PlaceholderType: { |
| 6667 | // FIXME: Already diagnosed! |
| 6668 | break; |
| 6669 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6670 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6671 | case FK_ExplicitConstructor: { |
| 6672 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6673 | << Args[0]->getSourceRange(); |
| 6674 | OverloadCandidateSet::iterator Best; |
| 6675 | OverloadingResult Ovl |
| 6676 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6677 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6678 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6679 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6680 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6681 | break; |
| 6682 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6683 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6684 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6685 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6686 | return true; |
| 6687 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6688 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6689 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6690 | switch (SequenceKind) { |
| 6691 | case FailedSequence: { |
| 6692 | OS << "Failed sequence: "; |
| 6693 | switch (Failure) { |
| 6694 | case FK_TooManyInitsForReference: |
| 6695 | OS << "too many initializers for reference"; |
| 6696 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6697 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6698 | case FK_ArrayNeedsInitList: |
| 6699 | OS << "array requires initializer list"; |
| 6700 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6701 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6702 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6703 | OS << "array requires initializer list or string literal"; |
| 6704 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6705 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6706 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6707 | OS << "array requires initializer list or wide string literal"; |
| 6708 | break; |
| 6709 | |
| 6710 | case FK_NarrowStringIntoWideCharArray: |
| 6711 | OS << "narrow string into wide char array"; |
| 6712 | break; |
| 6713 | |
| 6714 | case FK_WideStringIntoCharArray: |
| 6715 | OS << "wide string into char array"; |
| 6716 | break; |
| 6717 | |
| 6718 | case FK_IncompatWideStringIntoWideChar: |
| 6719 | OS << "incompatible wide string into wide char array"; |
| 6720 | break; |
| 6721 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6722 | case FK_ArrayTypeMismatch: |
| 6723 | OS << "array type mismatch"; |
| 6724 | break; |
| 6725 | |
| 6726 | case FK_NonConstantArrayInit: |
| 6727 | OS << "non-constant array initializer"; |
| 6728 | break; |
| 6729 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6730 | case FK_AddressOfOverloadFailed: |
| 6731 | OS << "address of overloaded function failed"; |
| 6732 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6733 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6734 | case FK_ReferenceInitOverloadFailed: |
| 6735 | OS << "overload resolution for reference initialization failed"; |
| 6736 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6737 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6738 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6739 | OS << "non-const lvalue reference bound to temporary"; |
| 6740 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6741 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6742 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6743 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6744 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6745 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6746 | case FK_RValueReferenceBindingToLValue: |
| 6747 | OS << "rvalue reference bound to an lvalue"; |
| 6748 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6749 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6750 | case FK_ReferenceInitDropsQualifiers: |
| 6751 | OS << "reference initialization drops qualifiers"; |
| 6752 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6753 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6754 | case FK_ReferenceInitFailed: |
| 6755 | OS << "reference initialization failed"; |
| 6756 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6757 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6758 | case FK_ConversionFailed: |
| 6759 | OS << "conversion failed"; |
| 6760 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6761 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6762 | case FK_ConversionFromPropertyFailed: |
| 6763 | OS << "conversion from property failed"; |
| 6764 | break; |
| 6765 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6766 | case FK_TooManyInitsForScalar: |
| 6767 | OS << "too many initializers for scalar"; |
| 6768 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6769 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6770 | case FK_ReferenceBindingToInitList: |
| 6771 | OS << "referencing binding to initializer list"; |
| 6772 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6773 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6774 | case FK_InitListBadDestinationType: |
| 6775 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6776 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6777 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6778 | case FK_UserConversionOverloadFailed: |
| 6779 | OS << "overloading failed for user-defined conversion"; |
| 6780 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6781 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6782 | case FK_ConstructorOverloadFailed: |
| 6783 | OS << "constructor overloading failed"; |
| 6784 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6785 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6786 | case FK_DefaultInitOfConst: |
| 6787 | OS << "default initialization of a const variable"; |
| 6788 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6789 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6790 | case FK_Incomplete: |
| 6791 | OS << "initialization of incomplete type"; |
| 6792 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6793 | |
| 6794 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6795 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6796 | break; |
| 6797 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6798 | case FK_VariableLengthArrayHasInitializer: |
| 6799 | OS << "variable length array has an initializer"; |
| 6800 | break; |
| 6801 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6802 | case FK_PlaceholderType: |
| 6803 | OS << "initializer expression isn't contextually valid"; |
| 6804 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6805 | |
| 6806 | case FK_ListConstructorOverloadFailed: |
| 6807 | OS << "list constructor overloading failed"; |
| 6808 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6809 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6810 | case FK_ExplicitConstructor: |
| 6811 | OS << "list copy initialization chose explicit constructor"; |
| 6812 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6813 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6814 | OS << '\n'; |
| 6815 | return; |
| 6816 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6817 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6818 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6819 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6820 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6821 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6822 | case NormalSequence: |
| 6823 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6824 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6825 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6826 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6827 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6828 | if (S != step_begin()) { |
| 6829 | OS << " -> "; |
| 6830 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6831 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6832 | switch (S->Kind) { |
| 6833 | case SK_ResolveAddressOfOverloadedFunction: |
| 6834 | OS << "resolve address of overloaded function"; |
| 6835 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6836 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6837 | case SK_CastDerivedToBaseRValue: |
| 6838 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6839 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6840 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6841 | case SK_CastDerivedToBaseXValue: |
| 6842 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6843 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6844 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6845 | case SK_CastDerivedToBaseLValue: |
| 6846 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 6847 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6848 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6849 | case SK_BindReference: |
| 6850 | OS << "bind reference to lvalue"; |
| 6851 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6852 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6853 | case SK_BindReferenceToTemporary: |
| 6854 | OS << "bind reference to a temporary"; |
| 6855 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6856 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6857 | case SK_ExtraneousCopyToTemporary: |
| 6858 | OS << "extraneous C++03 copy to temporary"; |
| 6859 | break; |
| 6860 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6861 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6862 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6863 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6864 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6865 | case SK_QualificationConversionRValue: |
| 6866 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6867 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6868 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6869 | case SK_QualificationConversionXValue: |
| 6870 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6871 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6872 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6873 | case SK_QualificationConversionLValue: |
| 6874 | OS << "qualification conversion (lvalue)"; |
| 6875 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6876 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6877 | case SK_LValueToRValue: |
| 6878 | OS << "load (lvalue to rvalue)"; |
| 6879 | break; |
| 6880 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6881 | case SK_ConversionSequence: |
| 6882 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 6883 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6884 | OS << ")"; |
| 6885 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6886 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6887 | case SK_ConversionSequenceNoNarrowing: |
| 6888 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 6889 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6890 | OS << ")"; |
| 6891 | break; |
| 6892 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6893 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6894 | OS << "list aggregate initialization"; |
| 6895 | break; |
| 6896 | |
| 6897 | case SK_ListConstructorCall: |
| 6898 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6899 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6900 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6901 | case SK_UnwrapInitList: |
| 6902 | OS << "unwrap reference initializer list"; |
| 6903 | break; |
| 6904 | |
| 6905 | case SK_RewrapInitList: |
| 6906 | OS << "rewrap reference initializer list"; |
| 6907 | break; |
| 6908 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6909 | case SK_ConstructorInitialization: |
| 6910 | OS << "constructor initialization"; |
| 6911 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6912 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6913 | case SK_ZeroInitialization: |
| 6914 | OS << "zero initialization"; |
| 6915 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6916 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6917 | case SK_CAssignment: |
| 6918 | OS << "C assignment"; |
| 6919 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6920 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6921 | case SK_StringInit: |
| 6922 | OS << "string initialization"; |
| 6923 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6924 | |
| 6925 | case SK_ObjCObjectConversion: |
| 6926 | OS << "Objective-C object conversion"; |
| 6927 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6928 | |
| 6929 | case SK_ArrayInit: |
| 6930 | OS << "array initialization"; |
| 6931 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6932 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6933 | case SK_ParenthesizedArrayInit: |
| 6934 | OS << "parenthesized array initialization"; |
| 6935 | break; |
| 6936 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6937 | case SK_PassByIndirectCopyRestore: |
| 6938 | OS << "pass by indirect copy and restore"; |
| 6939 | break; |
| 6940 | |
| 6941 | case SK_PassByIndirectRestore: |
| 6942 | OS << "pass by indirect restore"; |
| 6943 | break; |
| 6944 | |
| 6945 | case SK_ProduceObjCObject: |
| 6946 | OS << "Objective-C object retension"; |
| 6947 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6948 | |
| 6949 | case SK_StdInitializerList: |
| 6950 | OS << "std::initializer_list from initializer list"; |
| 6951 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6952 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6953 | case SK_OCLSamplerInit: |
| 6954 | OS << "OpenCL sampler_t from integer constant"; |
| 6955 | break; |
| 6956 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6957 | case SK_OCLZeroEvent: |
| 6958 | OS << "OpenCL event_t from zero"; |
| 6959 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6960 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6961 | |
| 6962 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6963 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6964 | |
| 6965 | OS << '\n'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6966 | } |
| 6967 | |
| 6968 | void InitializationSequence::dump() const { |
| 6969 | dump(llvm::errs()); |
| 6970 | } |
| 6971 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6972 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 6973 | const ImplicitConversionSequence &ICS, |
| 6974 | QualType PreNarrowingType, |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6975 | QualType EntityType, |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6976 | const Expr *PostInit) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6977 | const StandardConversionSequence *SCS = 0; |
| 6978 | switch (ICS.getKind()) { |
| 6979 | case ImplicitConversionSequence::StandardConversion: |
| 6980 | SCS = &ICS.Standard; |
| 6981 | break; |
| 6982 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6983 | SCS = &ICS.UserDefined.After; |
| 6984 | break; |
| 6985 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6986 | case ImplicitConversionSequence::EllipsisConversion: |
| 6987 | case ImplicitConversionSequence::BadConversion: |
| 6988 | return; |
| 6989 | } |
| 6990 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6991 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6992 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6993 | QualType ConstantType; |
| 6994 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 6995 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6996 | case NK_Not_Narrowing: |
| 6997 | // No narrowing occurred. |
| 6998 | return; |
| 6999 | |
| 7000 | case NK_Type_Narrowing: |
| 7001 | // This was a floating-to-integer conversion, which is always considered a |
| 7002 | // narrowing conversion even if the value is a constant and can be |
| 7003 | // represented exactly as an integer. |
| 7004 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 3347b49 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7005 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7006 | ? diag::warn_init_list_type_narrowing |
| 7007 | : diag::ext_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7008 | << PostInit->getSourceRange() |
| 7009 | << PreNarrowingType.getLocalUnqualifiedType() |
| 7010 | << EntityType.getLocalUnqualifiedType(); |
| 7011 | break; |
| 7012 | |
| 7013 | case NK_Constant_Narrowing: |
| 7014 | // A constant value was narrowed. |
| 7015 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 3347b49 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7016 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7017 | ? diag::warn_init_list_constant_narrowing |
| 7018 | : diag::ext_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7019 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 7020 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7021 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7022 | break; |
| 7023 | |
| 7024 | case NK_Variable_Narrowing: |
| 7025 | // A variable's value may have been narrowed. |
| 7026 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 3347b49 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7027 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7028 | ? diag::warn_init_list_variable_narrowing |
| 7029 | : diag::ext_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7030 | << PostInit->getSourceRange() |
| 7031 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7032 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7033 | break; |
| 7034 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7035 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7036 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7037 | llvm::raw_svector_ostream OS(StaticCast); |
| 7038 | OS << "static_cast<"; |
| 7039 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 7040 | // It's important to use the typedef's name if there is one so that the |
| 7041 | // fixit doesn't break code using types like int64_t. |
| 7042 | // |
| 7043 | // FIXME: This will break if the typedef requires qualification. But |
| 7044 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 7045 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7046 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7047 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7048 | else { |
| 7049 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 7050 | // with a broken cast. |
| 7051 | return; |
| 7052 | } |
| 7053 | OS << ">("; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7054 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override) |
| 7055 | << PostInit->getSourceRange() |
| 7056 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7057 | << FixItHint::CreateInsertion( |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7058 | S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7059 | } |
| 7060 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7061 | //===----------------------------------------------------------------------===// |
| 7062 | // Initialization helper functions |
| 7063 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7064 | bool |
| 7065 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 7066 | ExprResult Init) { |
| 7067 | if (Init.isInvalid()) |
| 7068 | return false; |
| 7069 | |
| 7070 | Expr *InitE = Init.get(); |
| 7071 | assert(InitE && "No initialization expression"); |
| 7072 | |
Douglas Gregor | 3c394c5 | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 7073 | InitializationKind Kind |
| 7074 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7075 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 7076 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7077 | } |
| 7078 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7079 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7080 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 7081 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7082 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7083 | bool TopLevelOfInitList, |
| 7084 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7085 | if (Init.isInvalid()) |
| 7086 | return ExprError(); |
| 7087 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7088 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7089 | assert(InitE && "No initialization expression?"); |
| 7090 | |
| 7091 | if (EqualLoc.isInvalid()) |
| 7092 | EqualLoc = InitE->getLocStart(); |
| 7093 | |
| 7094 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7095 | EqualLoc, |
| 7096 | AllowExplicit); |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7097 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7098 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7099 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7100 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7101 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7102 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7103 | } |