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/Sema/Designator.h" |
| 21 | #include "clang/Sema/Lookup.h" |
| 22 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 27 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 28 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 29 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Sema Initialization Checking |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 34 | /// \brief Check whether T is compatible with a wide character type (wchar_t, |
| 35 | /// char16_t or char32_t). |
| 36 | static bool IsWideCharCompatible(QualType T, ASTContext &Context) { |
| 37 | if (Context.typesAreCompatible(Context.getWideCharType(), T)) |
| 38 | return true; |
| 39 | if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) { |
| 40 | return Context.typesAreCompatible(Context.Char16Ty, T) || |
| 41 | Context.typesAreCompatible(Context.Char32Ty, T); |
| 42 | } |
| 43 | return false; |
| 44 | } |
| 45 | |
| 46 | enum StringInitFailureKind { |
| 47 | SIF_None, |
| 48 | SIF_NarrowStringIntoWideChar, |
| 49 | SIF_WideStringIntoChar, |
| 50 | SIF_IncompatWideStringIntoWideChar, |
| 51 | SIF_Other |
| 52 | }; |
| 53 | |
| 54 | /// \brief Check whether the array of type AT can be initialized by the Init |
| 55 | /// expression by means of string initialization. Returns SIF_None if so, |
| 56 | /// otherwise returns a StringInitFailureKind that describes why the |
| 57 | /// initialization would not work. |
| 58 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 59 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 60 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 61 | return SIF_Other; |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 63 | // See if this is a string literal or @encode. |
| 64 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 66 | // Handle @encode, which is a narrow string. |
| 67 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 68 | return SIF_None; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 69 | |
| 70 | // Otherwise we can only handle string literals. |
| 71 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 72 | if (!SL) |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 73 | return SIF_Other; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 74 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 75 | const QualType ElemTy = |
| 76 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 77 | |
| 78 | switch (SL->getKind()) { |
| 79 | case StringLiteral::Ascii: |
| 80 | case StringLiteral::UTF8: |
| 81 | // char array can be initialized with a narrow string. |
| 82 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 83 | if (ElemTy->isCharType()) |
| 84 | return SIF_None; |
| 85 | if (IsWideCharCompatible(ElemTy, Context)) |
| 86 | return SIF_NarrowStringIntoWideChar; |
| 87 | return SIF_Other; |
| 88 | // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15: |
| 89 | // "An array with element type compatible with a qualified or unqualified |
| 90 | // version of wchar_t, char16_t, or char32_t may be initialized by a wide |
| 91 | // string literal with the corresponding encoding prefix (L, u, or U, |
| 92 | // respectively), optionally enclosed in braces. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 93 | case StringLiteral::UTF16: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 94 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 95 | return SIF_None; |
| 96 | if (ElemTy->isCharType()) |
| 97 | return SIF_WideStringIntoChar; |
| 98 | if (IsWideCharCompatible(ElemTy, Context)) |
| 99 | return SIF_IncompatWideStringIntoWideChar; |
| 100 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 101 | case StringLiteral::UTF32: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 102 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 103 | return SIF_None; |
| 104 | if (ElemTy->isCharType()) |
| 105 | return SIF_WideStringIntoChar; |
| 106 | if (IsWideCharCompatible(ElemTy, Context)) |
| 107 | return SIF_IncompatWideStringIntoWideChar; |
| 108 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 109 | case StringLiteral::Wide: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 110 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 111 | return SIF_None; |
| 112 | if (ElemTy->isCharType()) |
| 113 | return SIF_WideStringIntoChar; |
| 114 | if (IsWideCharCompatible(ElemTy, Context)) |
| 115 | return SIF_IncompatWideStringIntoWideChar; |
| 116 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 117 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 119 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 122 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 123 | ASTContext &Context) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 124 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 125 | if (!arrayType) |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 126 | return SIF_Other; |
| 127 | return IsStringInit(init, arrayType, Context); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 130 | /// Update the type of a string literal, including any surrounding parentheses, |
| 131 | /// to match the type of the object which it is initializing. |
| 132 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 133 | while (true) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 134 | E->setType(Ty); |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 135 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 136 | break; |
| 137 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 138 | E = PE->getSubExpr(); |
| 139 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 140 | E = UO->getSubExpr(); |
| 141 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 142 | E = GSE->getResultExpr(); |
| 143 | else |
| 144 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 145 | } |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 146 | } |
| 147 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 148 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 149 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 150 | // Get the length of the string as parsed. |
| 151 | uint64_t StrLength = |
| 152 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 153 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 155 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | // 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] | 157 | // being initialized to a string literal. |
Benjamin Kramer | 65263b4 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 158 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 159 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 160 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 161 | ConstVal, |
| 162 | ArrayType::Normal, 0); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 163 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 164 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 165 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 167 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 169 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 170 | // the size may be smaller or larger than the string we are initializing. |
| 171 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 172 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 173 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 174 | // For Pascal strings it's OK to strip off the terminating null character, |
| 175 | // so the example below is valid: |
| 176 | // |
| 177 | // unsigned char a[2] = "\pa"; |
| 178 | if (SL->isPascal()) |
| 179 | StrLength--; |
| 180 | } |
| 181 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 182 | // [dcl.init.string]p2 |
| 183 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 184 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 185 | diag::err_initializer_string_for_char_array_too_long) |
| 186 | << Str->getSourceRange(); |
| 187 | } else { |
| 188 | // C99 6.7.8p14. |
| 189 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 190 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 191 | diag::warn_initializer_string_for_char_array_too_long) |
| 192 | << Str->getSourceRange(); |
| 193 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 195 | // Set the type to the actual size that we are initializing. If we have |
| 196 | // something like: |
| 197 | // char x[1] = "foo"; |
| 198 | // then this will set the string literal's type to char[1]. |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 199 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 202 | //===----------------------------------------------------------------------===// |
| 203 | // Semantic checking for initializer lists. |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 206 | /// @brief Semantic checking for initializer lists. |
| 207 | /// |
| 208 | /// The InitListChecker class contains a set of routines that each |
| 209 | /// handle the initialization of a certain kind of entity, e.g., |
| 210 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 211 | /// InitListChecker itself performs a recursive walk of the subobject |
| 212 | /// structure of the type to be initialized, while stepping through |
| 213 | /// the initializer list one element at a time. The IList and Index |
| 214 | /// parameters to each of the Check* routines contain the active |
| 215 | /// (syntactic) initializer list and the index into that initializer |
| 216 | /// list that represents the current initializer. Each routine is |
| 217 | /// responsible for moving that Index forward as it consumes elements. |
| 218 | /// |
| 219 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 220 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 221 | /// initializer list and the index into that initializer list where we |
| 222 | /// are copying initializers as we map them over to the semantic |
| 223 | /// list. Once we have completed our recursive walk of the subobject |
| 224 | /// structure, we will have constructed a full semantic initializer |
| 225 | /// list. |
| 226 | /// |
| 227 | /// C99 designators cause changes in the initializer list traversal, |
| 228 | /// because they make the initialization "jump" into a specific |
| 229 | /// subobject and then continue the initialization from that |
| 230 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 231 | /// designated subobject and manages backing out the recursion to |
| 232 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 233 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 234 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 235 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 236 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 237 | bool VerifyOnly; // no diagnostics, no structure building |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 238 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 239 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 241 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 242 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 243 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 244 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 245 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 246 | InitListExpr *IList, QualType &T, |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 247 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 248 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 249 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 250 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 251 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 252 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 253 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 254 | unsigned &StructuredIndex, |
| 255 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 256 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 257 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 258 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 259 | InitListExpr *StructuredList, |
| 260 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 261 | void CheckComplexType(const InitializedEntity &Entity, |
| 262 | InitListExpr *IList, QualType DeclType, |
| 263 | unsigned &Index, |
| 264 | InitListExpr *StructuredList, |
| 265 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 266 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 267 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 268 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 269 | InitListExpr *StructuredList, |
| 270 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 271 | void CheckReferenceType(const InitializedEntity &Entity, |
| 272 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 273 | unsigned &Index, |
| 274 | InitListExpr *StructuredList, |
| 275 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 276 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 277 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 278 | InitListExpr *StructuredList, |
| 279 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 280 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 281 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 282 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 283 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 284 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 285 | unsigned &StructuredIndex, |
| 286 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 287 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 288 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 289 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 290 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 291 | InitListExpr *StructuredList, |
| 292 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 293 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 294 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 295 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 297 | RecordDecl::field_iterator *NextField, |
| 298 | llvm::APSInt *NextElementIndex, |
| 299 | unsigned &Index, |
| 300 | InitListExpr *StructuredList, |
| 301 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 302 | bool FinishSubobjectInit, |
| 303 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 304 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 305 | QualType CurrentObjectType, |
| 306 | InitListExpr *StructuredList, |
| 307 | unsigned StructuredIndex, |
| 308 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 309 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 310 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 311 | Expr *expr); |
| 312 | int numArrayElements(QualType DeclType); |
| 313 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 314 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 315 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 316 | const InitializedEntity &ParentEntity, |
| 317 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 318 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 319 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 320 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 321 | Expr *InitExpr, FieldDecl *Field, |
| 322 | bool TopLevelObject); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 323 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 324 | |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 325 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 326 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 327 | InitListExpr *IL, QualType &T, bool VerifyOnly); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 328 | bool HadError() { return hadError; } |
| 329 | |
| 330 | // @brief Retrieves the fully-structured initializer list used for |
| 331 | // semantic analysis and code generation. |
| 332 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 333 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 334 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 335 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 336 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 337 | assert(VerifyOnly && |
| 338 | "CheckValueInitializable is only inteded for verification mode."); |
| 339 | |
| 340 | SourceLocation Loc; |
| 341 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 342 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 343 | InitializationSequence InitSeq(SemaRef, Entity, Kind, None); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 344 | if (InitSeq.Failed()) |
| 345 | hadError = true; |
| 346 | } |
| 347 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 348 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 349 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 350 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 351 | bool &RequiresSecondPass) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 352 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 353 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 354 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 355 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 356 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 357 | // If there's no explicit initializer but we have a default initializer, use |
| 358 | // that. This only happens in C++1y, since classes with default |
| 359 | // initializers are not aggregates in C++11. |
| 360 | if (Field->hasInClassInitializer()) { |
| 361 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 362 | ILE->getRBraceLoc(), Field); |
| 363 | if (Init < NumInits) |
| 364 | ILE->setInit(Init, DIE); |
| 365 | else { |
| 366 | ILE->updateInit(SemaRef.Context, Init, DIE); |
| 367 | RequiresSecondPass = true; |
| 368 | } |
| 369 | return; |
| 370 | } |
| 371 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 372 | // FIXME: We probably don't need to handle references |
| 373 | // specially here, since value-initialization of references is |
| 374 | // handled in InitializationSequence. |
| 375 | if (Field->getType()->isReferenceType()) { |
| 376 | // C++ [dcl.init.aggr]p9: |
| 377 | // If an incomplete or empty initializer-list leaves a |
| 378 | // member of reference type uninitialized, the program is |
| 379 | // ill-formed. |
| 380 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 381 | << Field->getType() |
| 382 | << ILE->getSyntacticForm()->getSourceRange(); |
| 383 | SemaRef.Diag(Field->getLocation(), |
| 384 | diag::note_uninit_reference_member); |
| 385 | hadError = true; |
| 386 | return; |
| 387 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 388 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 389 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 390 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 391 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 392 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 393 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 394 | hadError = true; |
| 395 | return; |
| 396 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 397 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 398 | ExprResult MemberInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 399 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 400 | if (MemberInit.isInvalid()) { |
| 401 | hadError = true; |
| 402 | return; |
| 403 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 404 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 405 | if (hadError) { |
| 406 | // Do nothing |
| 407 | } else if (Init < NumInits) { |
| 408 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 409 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 410 | // Value-initialization requires a constructor call, so |
| 411 | // extend the initializer list to include the constructor |
| 412 | // call and make a note that we'll need to take another pass |
| 413 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 414 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 415 | RequiresSecondPass = true; |
| 416 | } |
| 417 | } else if (InitListExpr *InnerILE |
| 418 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 419 | FillInValueInitializations(MemberEntity, InnerILE, |
| 420 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 423 | /// Recursively replaces NULL values within the given initializer list |
| 424 | /// with expressions that perform value-initialization of the |
| 425 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 426 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 427 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 428 | InitListExpr *ILE, |
| 429 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 430 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 431 | "Should not have void type"); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 432 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 433 | if (ILE->getSyntacticForm()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 434 | Loc = ILE->getSyntacticForm()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 435 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 436 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 437 | const RecordDecl *RDecl = RType->getDecl(); |
| 438 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 439 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 440 | Entity, ILE, RequiresSecondPass); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 441 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 442 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 443 | for (auto *Field : RDecl->fields()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 444 | if (Field->hasInClassInitializer()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 445 | FillInValueInitForField(0, Field, Entity, ILE, RequiresSecondPass); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | } |
| 449 | } else { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 450 | unsigned Init = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 451 | for (auto *Field : RDecl->fields()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 452 | if (Field->isUnnamedBitfield()) |
| 453 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 454 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 455 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 456 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 457 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 458 | FillInValueInitForField(Init, Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 459 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 460 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 461 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 462 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 463 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 464 | // Only look at the first initialization of a union. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 465 | if (RDecl->isUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 466 | break; |
| 467 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 472 | |
| 473 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 474 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 475 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 476 | unsigned NumInits = ILE->getNumInits(); |
| 477 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 478 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 479 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 480 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 481 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 482 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 483 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 484 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 485 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 486 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 487 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 488 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 490 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 492 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 493 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 494 | if (hadError) |
| 495 | return; |
| 496 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 497 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 498 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 499 | ElementEntity.setElementIndex(Init); |
| 500 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 501 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : nullptr); |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 502 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 503 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 504 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 505 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 506 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 507 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 508 | hadError = true; |
| 509 | return; |
| 510 | } |
| 511 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 512 | ExprResult ElementInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 513 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 514 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 515 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 516 | return; |
| 517 | } |
| 518 | |
| 519 | if (hadError) { |
| 520 | // Do nothing |
| 521 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 522 | // For arrays, just set the expression used for value-initialization |
| 523 | // of the "holes" in the array. |
| 524 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 525 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 526 | else |
| 527 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 528 | } else { |
| 529 | // For arrays, just set the expression used for value-initialization |
| 530 | // of the rest of elements and exit. |
| 531 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 532 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 533 | return; |
| 534 | } |
| 535 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 536 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 537 | // Value-initialization requires a constructor call, so |
| 538 | // extend the initializer list to include the constructor |
| 539 | // call and make a note that we'll need to take another pass |
| 540 | // through the initializer list. |
| 541 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 542 | RequiresSecondPass = true; |
| 543 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 544 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 545 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 546 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 547 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 551 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 552 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 553 | InitListExpr *IL, QualType &T, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 554 | bool VerifyOnly) |
| 555 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 556 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 557 | |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 558 | FullyStructuredList = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 559 | getStructuredSubobjectInit(IL, 0, T, nullptr, 0, IL->getSourceRange()); |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 560 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 561 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 562 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 563 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 564 | bool RequiresSecondPass = false; |
| 565 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 566 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 567 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 568 | RequiresSecondPass); |
| 569 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 573 | // FIXME: use a proper constant |
| 574 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 575 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 576 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 577 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 578 | } |
| 579 | return maxElements; |
| 580 | } |
| 581 | |
| 582 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 583 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 584 | int InitializableMembers = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 585 | for (const auto *Field : structDecl->fields()) |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 586 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 587 | ++InitializableMembers; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 588 | |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 589 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 590 | return std::min(InitializableMembers, 1); |
| 591 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 594 | /// Check whether the range of the initializer \p ParentIList from element |
| 595 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 596 | /// \p Index to indicate how many elements of the list were consumed. |
| 597 | /// |
| 598 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 599 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 600 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 601 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 602 | QualType T, unsigned &Index, |
| 603 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 604 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 605 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 607 | if (T->isArrayType()) |
| 608 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 609 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 610 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 611 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 612 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 613 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 614 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 615 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 616 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 617 | if (!VerifyOnly) |
| 618 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 619 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 620 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 621 | hadError = true; |
| 622 | return; |
| 623 | } |
| 624 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 625 | // Build a structured initializer list corresponding to this subobject. |
| 626 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 628 | StructuredIndex, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 629 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 630 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 631 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 632 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 633 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 634 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 635 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 636 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 638 | StructuredSubobjectInitIndex); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 639 | |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 640 | if (!VerifyOnly) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 641 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 642 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 643 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 644 | // Update the structured sub-object initializer so that it's ending |
| 645 | // range corresponds with the end of the last initializer it used. |
| 646 | if (EndIndex < ParentIList->getNumInits()) { |
| 647 | SourceLocation EndLoc |
| 648 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 649 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 650 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 651 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 652 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 653 | if (T->isArrayType() || T->isRecordType()) { |
| 654 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 655 | diag::warn_missing_braces) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 656 | << StructuredSubobjectInitList->getSourceRange() |
| 657 | << FixItHint::CreateInsertion( |
| 658 | StructuredSubobjectInitList->getLocStart(), "{") |
| 659 | << FixItHint::CreateInsertion( |
| 660 | SemaRef.getLocForEndOfToken( |
| 661 | StructuredSubobjectInitList->getLocEnd()), |
| 662 | "}"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 663 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 664 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 667 | /// Check whether the initializer \p IList (that was written with explicit |
| 668 | /// braces) can be used to initialize an object of type \p T. |
| 669 | /// |
| 670 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 671 | /// form of the initialization. |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 672 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 673 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 674 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 675 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 676 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 677 | if (!VerifyOnly) { |
| 678 | SyntacticToSemantic[IList] = StructuredList; |
| 679 | StructuredList->setSyntacticForm(IList); |
| 680 | } |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 681 | |
| 682 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 683 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 684 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 685 | if (!VerifyOnly) { |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 686 | QualType ExprTy = T; |
| 687 | if (!ExprTy->isArrayType()) |
| 688 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 689 | IList->setType(ExprTy); |
| 690 | StructuredList->setType(ExprTy); |
| 691 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 692 | if (hadError) |
| 693 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 694 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 695 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 696 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 697 | if (VerifyOnly) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 698 | if (SemaRef.getLangOpts().CPlusPlus || |
| 699 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 700 | IList->getType()->isVectorType())) { |
| 701 | hadError = true; |
| 702 | } |
| 703 | return; |
| 704 | } |
| 705 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 706 | if (StructuredIndex == 1 && |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 707 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 708 | SIF_None) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 709 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 710 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 711 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 712 | hadError = true; |
| 713 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 714 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 715 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 716 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 717 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 718 | // Don't complain for incomplete types, since we'll get an error |
| 719 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 720 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 721 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 722 | CurrentObjectType->isArrayType()? 0 : |
| 723 | CurrentObjectType->isVectorType()? 1 : |
| 724 | CurrentObjectType->isScalarType()? 2 : |
| 725 | CurrentObjectType->isUnionType()? 3 : |
| 726 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 727 | |
| 728 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 729 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 730 | DK = diag::err_excess_initializers; |
| 731 | hadError = true; |
| 732 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 733 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 734 | DK = diag::err_excess_initializers; |
| 735 | hadError = true; |
| 736 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 738 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 739 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 742 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 743 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 744 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 745 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 746 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 747 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 748 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 751 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 752 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 754 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 755 | unsigned &Index, |
| 756 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 757 | unsigned &StructuredIndex, |
| 758 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 759 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 760 | // Explicitly braced initializer for complex type can be real+imaginary |
| 761 | // parts. |
| 762 | CheckComplexType(Entity, IList, DeclType, Index, |
| 763 | StructuredList, StructuredIndex); |
| 764 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 765 | CheckScalarType(Entity, IList, DeclType, Index, |
| 766 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 767 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 768 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 769 | StructuredList, StructuredIndex); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 770 | } else if (DeclType->isRecordType()) { |
| 771 | assert(DeclType->isAggregateType() && |
| 772 | "non-aggregate records should be handed in CheckSubElementType"); |
| 773 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 774 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 775 | SubobjectIsDesignatorContext, Index, |
| 776 | StructuredList, StructuredIndex, |
| 777 | TopLevelObject); |
| 778 | } else if (DeclType->isArrayType()) { |
| 779 | llvm::APSInt Zero( |
| 780 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 781 | false); |
| 782 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 783 | SubobjectIsDesignatorContext, Index, |
| 784 | StructuredList, StructuredIndex); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 785 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 786 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 787 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 788 | if (!VerifyOnly) |
| 789 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 790 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 791 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 792 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 793 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 794 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 795 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 796 | if (!VerifyOnly) |
| 797 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 798 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 799 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 800 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 801 | if (!VerifyOnly) |
| 802 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 803 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 804 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 808 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 809 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 810 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 811 | unsigned &Index, |
| 812 | InitListExpr *StructuredList, |
| 813 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 814 | Expr *expr = IList->getInit(Index); |
Richard Smith | 6242a45 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 815 | |
| 816 | if (ElemType->isReferenceType()) |
| 817 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 818 | StructuredList, StructuredIndex); |
| 819 | |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 820 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 821 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 822 | InitListExpr *InnerStructuredList |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 823 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 824 | StructuredList, StructuredIndex, |
| 825 | SubInitList->getSourceRange()); |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 826 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 827 | InnerStructuredList); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 828 | ++StructuredIndex; |
| 829 | ++Index; |
| 830 | return; |
| 831 | } |
| 832 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 833 | "non-aggregate records are only possible in C++"); |
| 834 | // C++ initialization is handled later. |
| 835 | } |
| 836 | |
Eli Friedman | 48a2a3a | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 837 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
| 838 | if (ElemType->isScalarType() || ElemType->isAtomicType()) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 839 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 840 | StructuredList, StructuredIndex); |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 841 | |
Eli Friedman | 48a2a3a | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 842 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
| 843 | ElemType->isArrayType()) && "Unexpected type"); |
| 844 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 845 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 846 | // arrayType can be incomplete if we're initializing a flexible |
| 847 | // array member. There's nothing we can do with the completed |
| 848 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 849 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 850 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 851 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 852 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 853 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 854 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 855 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 856 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 857 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 858 | |
| 859 | // Fall through for subaggregate initialization. |
| 860 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 861 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 862 | // C++ [dcl.init.aggr]p12: |
| 863 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 864 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 865 | // an initializer-list. If the initializer can initialize a |
| 866 | // member, the member is initialized. [...] |
| 867 | |
| 868 | // FIXME: Better EqualLoc? |
| 869 | InitializationKind Kind = |
| 870 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 871 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 872 | |
| 873 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 874 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 875 | ExprResult Result = |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 876 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 877 | if (Result.isInvalid()) |
| 878 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 879 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 880 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 881 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 882 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 883 | ++Index; |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | // Fall through for subaggregate initialization |
| 888 | } else { |
| 889 | // C99 6.7.8p13: |
| 890 | // |
| 891 | // The initializer for a structure or union object that has |
| 892 | // automatic storage duration shall be either an initializer |
| 893 | // list as described below, or a single expression that has |
| 894 | // compatible structure or union type. In the latter case, the |
| 895 | // initial value of the object, including unnamed members, is |
| 896 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 897 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 898 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 899 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 900 | !VerifyOnly) |
Eli Friedman | 08f0bbc | 2013-09-17 04:07:04 +0000 | [diff] [blame] | 901 | != Sema::Incompatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 902 | if (ExprRes.isInvalid()) |
| 903 | hadError = true; |
| 904 | else { |
| 905 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 906 | if (ExprRes.isInvalid()) |
| 907 | hadError = true; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 908 | } |
| 909 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 910 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 911 | ++Index; |
| 912 | return; |
| 913 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 914 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 915 | // Fall through for subaggregate initialization |
| 916 | } |
| 917 | |
| 918 | // C++ [dcl.init.aggr]p12: |
| 919 | // |
| 920 | // [...] Otherwise, if the member is itself a non-empty |
| 921 | // subaggregate, brace elision is assumed and the initializer is |
| 922 | // considered for the initialization of the first member of |
| 923 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 924 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 925 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 926 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 927 | StructuredIndex); |
| 928 | ++StructuredIndex; |
| 929 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 930 | if (!VerifyOnly) { |
| 931 | // We cannot initialize this element, so let |
| 932 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 933 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 934 | SemaRef.Owned(expr), |
| 935 | /*TopLevelOfInitList=*/true); |
| 936 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 937 | hadError = true; |
| 938 | ++Index; |
| 939 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 940 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 941 | } |
| 942 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 943 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 944 | InitListExpr *IList, QualType DeclType, |
| 945 | unsigned &Index, |
| 946 | InitListExpr *StructuredList, |
| 947 | unsigned &StructuredIndex) { |
| 948 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 949 | |
| 950 | // As an extension, clang supports complex initializers, which initialize |
| 951 | // a complex number component-wise. When an explicit initializer list for |
| 952 | // a complex number contains two two initializers, this extension kicks in: |
| 953 | // it exepcts the initializer list to contain two elements convertible to |
| 954 | // the element type of the complex type. The first element initializes |
| 955 | // the real part, and the second element intitializes the imaginary part. |
| 956 | |
| 957 | if (IList->getNumInits() != 2) |
| 958 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 959 | StructuredIndex); |
| 960 | |
| 961 | // This is an extension in C. (The builtin _Complex type does not exist |
| 962 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 963 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 964 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 965 | << IList->getSourceRange(); |
| 966 | |
| 967 | // Initialize the complex number. |
| 968 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 969 | InitializedEntity ElementEntity = |
| 970 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 971 | |
| 972 | for (unsigned i = 0; i < 2; ++i) { |
| 973 | ElementEntity.setElementIndex(Index); |
| 974 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 975 | StructuredList, StructuredIndex); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 980 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 981 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 982 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 983 | InitListExpr *StructuredList, |
| 984 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 985 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 986 | if (!VerifyOnly) |
| 987 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 988 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 989 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 990 | diag::err_empty_scalar_initializer) |
| 991 | << IList->getSourceRange(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 992 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 993 | ++Index; |
| 994 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 995 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 996 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 997 | |
| 998 | Expr *expr = IList->getInit(Index); |
| 999 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1000 | // FIXME: This is invalid, and accepting it causes overload resolution |
| 1001 | // to pick the wrong overload in some corner cases. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1002 | if (!VerifyOnly) |
| 1003 | SemaRef.Diag(SubIList->getLocStart(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1004 | diag::ext_many_braces_around_scalar_init) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1005 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1006 | |
| 1007 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1008 | StructuredIndex); |
| 1009 | return; |
| 1010 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1011 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1012 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1013 | diag::err_designator_for_scalar_init) |
| 1014 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1015 | hadError = true; |
| 1016 | ++Index; |
| 1017 | ++StructuredIndex; |
| 1018 | return; |
| 1019 | } |
| 1020 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1021 | if (VerifyOnly) { |
| 1022 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1023 | hadError = true; |
| 1024 | ++Index; |
| 1025 | return; |
| 1026 | } |
| 1027 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1028 | ExprResult Result = |
| 1029 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1030 | SemaRef.Owned(expr), |
| 1031 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1032 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1033 | Expr *ResultExpr = nullptr; |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1034 | |
| 1035 | if (Result.isInvalid()) |
| 1036 | hadError = true; // types weren't compatible. |
| 1037 | else { |
| 1038 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1039 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1040 | if (ResultExpr != expr) { |
| 1041 | // The type was promoted, update initializer list. |
| 1042 | IList->setInit(Index, ResultExpr); |
| 1043 | } |
| 1044 | } |
| 1045 | if (hadError) |
| 1046 | ++StructuredIndex; |
| 1047 | else |
| 1048 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1049 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1052 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1053 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1054 | unsigned &Index, |
| 1055 | InitListExpr *StructuredList, |
| 1056 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1057 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1058 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1059 | // general, it would be useful to pass location information down the stack, |
| 1060 | // so that we know the location (or decl) of the "current object" being |
| 1061 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1062 | if (!VerifyOnly) |
| 1063 | SemaRef.Diag(IList->getLocStart(), |
| 1064 | diag::err_init_reference_member_uninitialized) |
| 1065 | << DeclType |
| 1066 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1067 | hadError = true; |
| 1068 | ++Index; |
| 1069 | ++StructuredIndex; |
| 1070 | return; |
| 1071 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1072 | |
| 1073 | Expr *expr = IList->getInit(Index); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1074 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1075 | if (!VerifyOnly) |
| 1076 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1077 | << DeclType << IList->getSourceRange(); |
| 1078 | hadError = true; |
| 1079 | ++Index; |
| 1080 | ++StructuredIndex; |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | if (VerifyOnly) { |
| 1085 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1086 | hadError = true; |
| 1087 | ++Index; |
| 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | ExprResult Result = |
| 1092 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1093 | SemaRef.Owned(expr), |
| 1094 | /*TopLevelOfInitList=*/true); |
| 1095 | |
| 1096 | if (Result.isInvalid()) |
| 1097 | hadError = true; |
| 1098 | |
| 1099 | expr = Result.takeAs<Expr>(); |
| 1100 | IList->setInit(Index, expr); |
| 1101 | |
| 1102 | if (hadError) |
| 1103 | ++StructuredIndex; |
| 1104 | else |
| 1105 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1106 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1109 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1110 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1111 | unsigned &Index, |
| 1112 | InitListExpr *StructuredList, |
| 1113 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1114 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1115 | unsigned maxElements = VT->getNumElements(); |
| 1116 | unsigned numEltsInit = 0; |
| 1117 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1118 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1119 | if (Index >= IList->getNumInits()) { |
| 1120 | // Make sure the element type can be value-initialized. |
| 1121 | if (VerifyOnly) |
| 1122 | CheckValueInitializable( |
| 1123 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1124 | return; |
| 1125 | } |
| 1126 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1127 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1128 | // If the initializing element is a vector, try to copy-initialize |
| 1129 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1130 | Expr *Init = IList->getInit(Index); |
| 1131 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1132 | if (VerifyOnly) { |
| 1133 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1134 | hadError = true; |
| 1135 | ++Index; |
| 1136 | return; |
| 1137 | } |
| 1138 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1139 | ExprResult Result = |
| 1140 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1141 | SemaRef.Owned(Init), |
| 1142 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1143 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1144 | Expr *ResultExpr = nullptr; |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1145 | if (Result.isInvalid()) |
| 1146 | hadError = true; // types weren't compatible. |
| 1147 | else { |
| 1148 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1149 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1150 | if (ResultExpr != Init) { |
| 1151 | // The type was promoted, update initializer list. |
| 1152 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1153 | } |
| 1154 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1155 | if (hadError) |
| 1156 | ++StructuredIndex; |
| 1157 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1158 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1159 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1160 | ++Index; |
| 1161 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1162 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1163 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1164 | InitializedEntity ElementEntity = |
| 1165 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1166 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1167 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1168 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1169 | if (Index >= IList->getNumInits()) { |
| 1170 | if (VerifyOnly) |
| 1171 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1172 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1173 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1174 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1175 | ElementEntity.setElementIndex(Index); |
| 1176 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1177 | StructuredList, StructuredIndex); |
| 1178 | } |
| 1179 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1180 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1181 | |
| 1182 | InitializedEntity ElementEntity = |
| 1183 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1184 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1185 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1186 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1187 | // Don't attempt to go past the end of the init list |
| 1188 | if (Index >= IList->getNumInits()) |
| 1189 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1190 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1191 | ElementEntity.setElementIndex(Index); |
| 1192 | |
| 1193 | QualType IType = IList->getInit(Index)->getType(); |
| 1194 | if (!IType->isVectorType()) { |
| 1195 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1196 | StructuredList, StructuredIndex); |
| 1197 | ++numEltsInit; |
| 1198 | } else { |
| 1199 | QualType VecType; |
| 1200 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1201 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1202 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1203 | if (IType->isExtVectorType()) |
| 1204 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1205 | else |
| 1206 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1207 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1208 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1209 | StructuredList, StructuredIndex); |
| 1210 | numEltsInit += numIElts; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1215 | if (numEltsInit != maxElements) { |
| 1216 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1217 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1218 | diag::err_vector_incorrect_num_initializers) |
| 1219 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1220 | hadError = true; |
| 1221 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1224 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1225 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1226 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1227 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1228 | unsigned &Index, |
| 1229 | InitListExpr *StructuredList, |
| 1230 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1231 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1232 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1233 | // Check for the special-case of initializing an array with a string. |
| 1234 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1235 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1236 | SIF_None) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1237 | // We place the string literal directly into the resulting |
| 1238 | // initializer list. This is the only place where the structure |
| 1239 | // of the structured initializer list doesn't match exactly, |
| 1240 | // because doing so would involve allocating one character |
| 1241 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1242 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1243 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1244 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1245 | IList->getInit(Index)); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1246 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1247 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1248 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1249 | return; |
| 1250 | } |
| 1251 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1252 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1253 | // Check for VLAs; in standard C it would be possible to check this |
| 1254 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1255 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1256 | if (!VerifyOnly) |
| 1257 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1258 | diag::err_variable_object_no_init) |
| 1259 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1260 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1261 | ++Index; |
| 1262 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1263 | return; |
| 1264 | } |
| 1265 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1266 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1267 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1268 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1269 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1270 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1271 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1272 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1273 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1274 | maxElementsKnown = true; |
| 1275 | } |
| 1276 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1277 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1278 | while (Index < IList->getNumInits()) { |
| 1279 | Expr *Init = IList->getInit(Index); |
| 1280 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1281 | // If we're not the subobject that matches up with the '{' for |
| 1282 | // the designator, we shouldn't be handling the |
| 1283 | // designator. Return immediately. |
| 1284 | if (!SubobjectIsDesignatorContext) |
| 1285 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1286 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1287 | // Handle this designated initializer. elementIndex will be |
| 1288 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1289 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1290 | DeclType, nullptr, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1291 | StructuredList, StructuredIndex, true, |
| 1292 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1293 | hadError = true; |
| 1294 | continue; |
| 1295 | } |
| 1296 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1297 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1298 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1299 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1300 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1301 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1302 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1303 | // If the array is of incomplete type, keep track of the number of |
| 1304 | // elements in the initializer. |
| 1305 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1306 | maxElements = elementIndex; |
| 1307 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1308 | continue; |
| 1309 | } |
| 1310 | |
| 1311 | // If we know the maximum number of elements, and we've already |
| 1312 | // hit it, stop consuming elements in the initializer list. |
| 1313 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1314 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1315 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1316 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1317 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1318 | Entity); |
| 1319 | // Check this element. |
| 1320 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1321 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1322 | ++elementIndex; |
| 1323 | |
| 1324 | // If the array is of incomplete type, keep track of the number of |
| 1325 | // elements in the initializer. |
| 1326 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1327 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1328 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1329 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1330 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1331 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1332 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1333 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1334 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1335 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1336 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1337 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1338 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1339 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1340 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1341 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1342 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1343 | if (!hadError && VerifyOnly) { |
| 1344 | // Check if there are any members of the array that get value-initialized. |
| 1345 | // If so, check if doing that is possible. |
| 1346 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1347 | if (maxElementsKnown && elementIndex < maxElements) |
| 1348 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1349 | SemaRef.Context, 0, Entity)); |
| 1350 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1353 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1354 | Expr *InitExpr, |
| 1355 | FieldDecl *Field, |
| 1356 | bool TopLevelObject) { |
| 1357 | // Handle GNU flexible array initializers. |
| 1358 | unsigned FlexArrayDiag; |
| 1359 | if (isa<InitListExpr>(InitExpr) && |
| 1360 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1361 | // Empty flexible array init always allowed as an extension |
| 1362 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1363 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1364 | // Disallow flexible array init in C++; it is not required for gcc |
| 1365 | // compatibility, and it needs work to IRGen correctly in general. |
| 1366 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1367 | } else if (!TopLevelObject) { |
| 1368 | // Disallow flexible array init on non-top-level object |
| 1369 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1370 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1371 | // Disallow flexible array init on anything which is not a variable. |
| 1372 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1373 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1374 | // Disallow flexible array init on local variables. |
| 1375 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1376 | } else { |
| 1377 | // Allow other cases. |
| 1378 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1379 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1380 | |
| 1381 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1382 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1383 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1384 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1385 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1386 | << Field; |
| 1387 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1388 | |
| 1389 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1390 | } |
| 1391 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1392 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1393 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1395 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1396 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1397 | unsigned &Index, |
| 1398 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1399 | unsigned &StructuredIndex, |
| 1400 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1401 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1403 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1404 | // confusion, we forgo checking the intializer for the entire record. |
| 1405 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 72ab277 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1406 | // Assume it was supposed to consume a single initializer. |
| 1407 | ++Index; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1408 | hadError = true; |
| 1409 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1410 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1411 | |
| 1412 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1413 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1414 | |
| 1415 | // If there's a default initializer, use it. |
| 1416 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1417 | if (VerifyOnly) |
| 1418 | return; |
| 1419 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1420 | Field != FieldEnd; ++Field) { |
| 1421 | if (Field->hasInClassInitializer()) { |
| 1422 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1423 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1424 | return; |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | // Value-initialize the first named member of the union. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1430 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1431 | Field != FieldEnd; ++Field) { |
| 1432 | if (Field->getDeclName()) { |
| 1433 | if (VerifyOnly) |
| 1434 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1435 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1436 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1437 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1438 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | return; |
| 1442 | } |
| 1443 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1444 | // If structDecl is a forward declaration, this loop won't do |
| 1445 | // anything except look at designated initializers; That's okay, |
| 1446 | // because an error should get printed out elsewhere. It might be |
| 1447 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1448 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1449 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1450 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1451 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1452 | while (Index < IList->getNumInits()) { |
| 1453 | Expr *Init = IList->getInit(Index); |
| 1454 | |
| 1455 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1456 | // If we're not the subobject that matches up with the '{' for |
| 1457 | // the designator, we shouldn't be handling the |
| 1458 | // designator. Return immediately. |
| 1459 | if (!SubobjectIsDesignatorContext) |
| 1460 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1461 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1462 | // Handle this designated initializer. Field will be updated to |
| 1463 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1464 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1465 | DeclType, &Field, nullptr, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1466 | StructuredList, StructuredIndex, |
| 1467 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1468 | hadError = true; |
| 1469 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1470 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1471 | |
| 1472 | // Disable check for missing fields when designators are used. |
| 1473 | // This matches gcc behaviour. |
| 1474 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1475 | continue; |
| 1476 | } |
| 1477 | |
| 1478 | if (Field == FieldEnd) { |
| 1479 | // We've run out of fields. We're done. |
| 1480 | break; |
| 1481 | } |
| 1482 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1483 | // We've already initialized a member of a union. We're done. |
| 1484 | if (InitializedSomething && DeclType->isUnionType()) |
| 1485 | break; |
| 1486 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1487 | // If we've hit the flexible array member at the end, we're done. |
| 1488 | if (Field->getType()->isIncompleteArrayType()) |
| 1489 | break; |
| 1490 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1491 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1492 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1493 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1494 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1495 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1496 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1497 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1498 | bool InvalidUse; |
| 1499 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1500 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1501 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1502 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1503 | IList->getInit(Index)->getLocStart()); |
| 1504 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1505 | ++Index; |
| 1506 | ++Field; |
| 1507 | hadError = true; |
| 1508 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1509 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1510 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1511 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1512 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1513 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1514 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1515 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1516 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1517 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1518 | // Initialize the first field within the union. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1519 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1520 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1521 | |
| 1522 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1523 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1524 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1525 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1526 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1527 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1528 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1529 | // It is possible we have one or more unnamed bitfields remaining. |
| 1530 | // Find first (if any) named field and emit warning. |
| 1531 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1532 | it != end; ++it) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1533 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1534 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1535 | diag::warn_missing_field_initializers) << *it; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1536 | break; |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1541 | // Check that any remaining fields can be value-initialized. |
| 1542 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1543 | !Field->getType()->isIncompleteArrayType()) { |
| 1544 | // FIXME: Should check for holes left by designated initializers too. |
| 1545 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1546 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1547 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1548 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1553 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1554 | return; |
| 1555 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1556 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1557 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1558 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1559 | ++Index; |
| 1560 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1561 | } |
| 1562 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1563 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1564 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1565 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1566 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1567 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1568 | StructuredList, StructuredIndex); |
| 1569 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1570 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1571 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1572 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1573 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1574 | /// \brief Expand a field designator that refers to a member of an |
| 1575 | /// anonymous struct or union into a series of field designators that |
| 1576 | /// refers to the field within the appropriate subobject. |
| 1577 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1578 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1579 | DesignatedInitExpr *DIE, |
| 1580 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1581 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1582 | typedef DesignatedInitExpr::Designator Designator; |
| 1583 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1584 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1585 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1586 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1587 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1588 | if (PI + 1 == PE) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1589 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1590 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1591 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1592 | else |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1593 | Replacements.push_back(Designator((IdentifierInfo *)nullptr, |
| 1594 | SourceLocation(), SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1595 | assert(isa<FieldDecl>(*PI)); |
| 1596 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | // Expand the current designator into the set of replacement |
| 1600 | // designators, so we have a full subobject path down to where the |
| 1601 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1602 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1603 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1604 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1606 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1607 | /// corresponds to FieldName. |
| 1608 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1609 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1610 | if (!FieldName) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1611 | return nullptr; |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1612 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1613 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1614 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1615 | while (IndirectFieldDecl *IF = |
| 1616 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1617 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1618 | return IF; |
| 1619 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1620 | } |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1621 | return nullptr; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1624 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1625 | DesignatedInitExpr *DIE) { |
| 1626 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1627 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1628 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1629 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1630 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1631 | DIE->size(), IndexExprs, |
| 1632 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1633 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1634 | } |
| 1635 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1636 | namespace { |
| 1637 | |
| 1638 | // Callback to only accept typo corrections that are for field members of |
| 1639 | // the given struct or union. |
| 1640 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1641 | public: |
| 1642 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1643 | : Record(RD) {} |
| 1644 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1645 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1646 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1647 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1648 | } |
| 1649 | |
| 1650 | private: |
| 1651 | RecordDecl *Record; |
| 1652 | }; |
| 1653 | |
| 1654 | } |
| 1655 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1656 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1657 | /// |
| 1658 | /// Determines whether the designated initializer @p DIE, which |
| 1659 | /// resides at the given @p Index within the initializer list @p |
| 1660 | /// IList, is well-formed for a current object of type @p DeclType |
| 1661 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1662 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1663 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1664 | /// |
| 1665 | /// @param IList The initializer list in which this designated |
| 1666 | /// initializer occurs. |
| 1667 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1668 | /// @param DIE The designated initializer expression. |
| 1669 | /// |
| 1670 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1671 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1672 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1673 | /// into which the designation in @p DIE should refer. |
| 1674 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1675 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1676 | /// a field, this will be set to the field declaration corresponding |
| 1677 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1678 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1679 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1680 | /// DIE is an array designator or GNU array-range designator, this |
| 1681 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1682 | /// |
| 1683 | /// @param Index Index into @p IList where the designated initializer |
| 1684 | /// @p DIE occurs. |
| 1685 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1686 | /// @param StructuredList The initializer list expression that |
| 1687 | /// describes all of the subobject initializers in the order they'll |
| 1688 | /// actually be initialized. |
| 1689 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1690 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1691 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1692 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1693 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1694 | DesignatedInitExpr *DIE, |
| 1695 | unsigned DesigIdx, |
| 1696 | QualType &CurrentObjectType, |
| 1697 | RecordDecl::field_iterator *NextField, |
| 1698 | llvm::APSInt *NextElementIndex, |
| 1699 | unsigned &Index, |
| 1700 | InitListExpr *StructuredList, |
| 1701 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1702 | bool FinishSubobjectInit, |
| 1703 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1704 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1705 | // Check the actual initialization for the designated object type. |
| 1706 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1707 | |
| 1708 | // Temporarily remove the designator expression from the |
| 1709 | // initializer list that the child calls see, so that we don't try |
| 1710 | // to re-process the designator. |
| 1711 | unsigned OldIndex = Index; |
| 1712 | IList->setInit(OldIndex, DIE->getInit()); |
| 1713 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1714 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1715 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1716 | |
| 1717 | // Restore the designated initializer expression in the syntactic |
| 1718 | // form of the initializer list. |
| 1719 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1720 | DIE->setInit(IList->getInit(OldIndex)); |
| 1721 | IList->setInit(OldIndex, DIE); |
| 1722 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1723 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1726 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1727 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1728 | if (!VerifyOnly) { |
| 1729 | assert((IsFirstDesignator || StructuredList) && |
| 1730 | "Need a non-designated initializer list to start from"); |
| 1731 | |
| 1732 | // Determine the structural initializer list that corresponds to the |
| 1733 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1734 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1735 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1736 | StructuredList, StructuredIndex, |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1737 | SourceRange(D->getLocStart(), |
| 1738 | DIE->getLocEnd())); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1739 | assert(StructuredList && "Expected a structured initializer list"); |
| 1740 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1741 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1742 | if (D->isFieldDesignator()) { |
| 1743 | // C99 6.7.8p7: |
| 1744 | // |
| 1745 | // If a designator has the form |
| 1746 | // |
| 1747 | // . identifier |
| 1748 | // |
| 1749 | // then the current object (defined below) shall have |
| 1750 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1751 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1752 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1753 | if (!RT) { |
| 1754 | SourceLocation Loc = D->getDotLoc(); |
| 1755 | if (Loc.isInvalid()) |
| 1756 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1757 | if (!VerifyOnly) |
| 1758 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1759 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1760 | ++Index; |
| 1761 | return true; |
| 1762 | } |
| 1763 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1764 | // Note: we perform a linear search of the fields here, despite |
| 1765 | // the fact that we have a faster lookup method, because we always |
| 1766 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1767 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1768 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1769 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1771 | Field = RT->getDecl()->field_begin(), |
| 1772 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1773 | for (; Field != FieldEnd; ++Field) { |
| 1774 | if (Field->isUnnamedBitfield()) |
| 1775 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1776 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1777 | // If we find a field representing an anonymous field, look in the |
| 1778 | // IndirectFieldDecl that follow for the designated initializer. |
| 1779 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1780 | if (IndirectFieldDecl *IF = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1781 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1782 | // In verify mode, don't modify the original. |
| 1783 | if (VerifyOnly) |
| 1784 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1785 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1786 | D = DIE->getDesignator(DesigIdx); |
| 1787 | break; |
| 1788 | } |
| 1789 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1790 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1791 | break; |
| 1792 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1793 | break; |
| 1794 | |
| 1795 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1798 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1799 | if (VerifyOnly) { |
| 1800 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1801 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1802 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1803 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1804 | // There was no normal field in the struct with the designated |
| 1805 | // name. Perform another lookup for this name, which may find |
| 1806 | // something that we can't designate (e.g., a member function), |
| 1807 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1808 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1809 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1810 | FieldDecl *ReplacementField = nullptr; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1811 | if (Lookup.empty()) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1812 | // Name lookup didn't find anything. Determine whether this |
| 1813 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1814 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1815 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1816 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1817 | Sema::LookupMemberName, /*Scope=*/ nullptr, /*SS=*/ nullptr, |
| 1818 | Validator, Sema::CTK_ErrorRecovery, RT->getDecl())) { |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1819 | SemaRef.diagnoseTypo( |
| 1820 | Corrected, |
| 1821 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
| 1822 | << FieldName << CurrentObjectType); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1823 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1824 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1825 | } else { |
| 1826 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1827 | << FieldName << CurrentObjectType; |
| 1828 | ++Index; |
| 1829 | return true; |
| 1830 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1831 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1832 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1833 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1834 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1835 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1836 | << FieldName; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1837 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1838 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1839 | ++Index; |
| 1840 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1841 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1842 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1843 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1844 | // The replacement field comes from typo correction; find it |
| 1845 | // in the list of fields. |
| 1846 | FieldIndex = 0; |
| 1847 | Field = RT->getDecl()->field_begin(); |
| 1848 | for (; Field != FieldEnd; ++Field) { |
| 1849 | if (Field->isUnnamedBitfield()) |
| 1850 | continue; |
| 1851 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1852 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1853 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1854 | break; |
| 1855 | |
| 1856 | ++FieldIndex; |
| 1857 | } |
| 1858 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1859 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1860 | |
| 1861 | // All of the fields of a union are located at the same place in |
| 1862 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1863 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1864 | FieldIndex = 0; |
Matthew Curtis | 4e49952 | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1865 | if (!VerifyOnly) { |
| 1866 | FieldDecl *CurrentField = StructuredList->getInitializedFieldInUnion(); |
| 1867 | if (CurrentField && CurrentField != *Field) { |
| 1868 | assert(StructuredList->getNumInits() == 1 |
| 1869 | && "A union should never have more than one initializer!"); |
| 1870 | |
| 1871 | // we're about to throw away an initializer, emit warning |
| 1872 | SemaRef.Diag(D->getFieldLoc(), |
| 1873 | diag::warn_initializer_overrides) |
| 1874 | << D->getSourceRange(); |
| 1875 | Expr *ExistingInit = StructuredList->getInit(0); |
| 1876 | SemaRef.Diag(ExistingInit->getLocStart(), |
| 1877 | diag::note_previous_initializer) |
| 1878 | << /*FIXME:has side effects=*/0 |
| 1879 | << ExistingInit->getSourceRange(); |
| 1880 | |
| 1881 | // remove existing initializer |
| 1882 | StructuredList->resizeInits(SemaRef.Context, 0); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1883 | StructuredList->setInitializedFieldInUnion(nullptr); |
Matthew Curtis | 4e49952 | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1886 | StructuredList->setInitializedFieldInUnion(*Field); |
Matthew Curtis | 4e49952 | 2013-10-03 12:14:24 +0000 | [diff] [blame] | 1887 | } |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1888 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1889 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1890 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1891 | bool InvalidUse; |
| 1892 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1893 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1894 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1895 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1896 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1897 | ++Index; |
| 1898 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1899 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1900 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1901 | if (!VerifyOnly) { |
| 1902 | // Update the designator with the field declaration. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1903 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1904 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1905 | // Make sure that our non-designated initializer list has space |
| 1906 | // for a subobject corresponding to this field. |
| 1907 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1908 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1909 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1910 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1911 | // This designator names a flexible array member. |
| 1912 | if (Field->getType()->isIncompleteArrayType()) { |
| 1913 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1914 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1915 | // We can't designate an object within the flexible array |
| 1916 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1917 | if (!VerifyOnly) { |
| 1918 | DesignatedInitExpr::Designator *NextD |
| 1919 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1920 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1921 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1922 | << SourceRange(NextD->getLocStart(), |
| 1923 | DIE->getLocEnd()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1924 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1925 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1926 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1927 | Invalid = true; |
| 1928 | } |
| 1929 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1930 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1931 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1932 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1933 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1934 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1935 | diag::err_flexible_array_init_needs_braces) |
| 1936 | << DIE->getInit()->getSourceRange(); |
| 1937 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1938 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1939 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1940 | Invalid = true; |
| 1941 | } |
| 1942 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1943 | // Check GNU flexible array initializer. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1944 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1945 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1946 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1947 | |
| 1948 | if (Invalid) { |
| 1949 | ++Index; |
| 1950 | return true; |
| 1951 | } |
| 1952 | |
| 1953 | // Initialize the array. |
| 1954 | bool prevHadError = hadError; |
| 1955 | unsigned newStructuredIndex = FieldIndex; |
| 1956 | unsigned OldIndex = Index; |
| 1957 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1958 | |
| 1959 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1960 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1961 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1962 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1963 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1964 | IList->setInit(OldIndex, DIE); |
| 1965 | if (hadError && !prevHadError) { |
| 1966 | ++Field; |
| 1967 | ++FieldIndex; |
| 1968 | if (NextField) |
| 1969 | *NextField = Field; |
| 1970 | StructuredIndex = FieldIndex; |
| 1971 | return true; |
| 1972 | } |
| 1973 | } else { |
| 1974 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1975 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1976 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1977 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1978 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1979 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1980 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 1981 | FieldType, nullptr, nullptr, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1982 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1983 | true, false)) |
| 1984 | return true; |
| 1985 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1986 | |
| 1987 | // Find the position of the next field to be initialized in this |
| 1988 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1989 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1990 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1991 | |
| 1992 | // If this the first designator, our caller will continue checking |
| 1993 | // the rest of this struct/class/union subobject. |
| 1994 | if (IsFirstDesignator) { |
| 1995 | if (NextField) |
| 1996 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1997 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1998 | return false; |
| 1999 | } |
| 2000 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2001 | if (!FinishSubobjectInit) |
| 2002 | return false; |
| 2003 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2004 | // We've already initialized something in the union; we're done. |
| 2005 | if (RT->getDecl()->isUnion()) |
| 2006 | return hadError; |
| 2007 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2008 | // Check the remaining fields within this class/struct/union subobject. |
| 2009 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2010 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2011 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2012 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2013 | return hadError && !prevHadError; |
| 2014 | } |
| 2015 | |
| 2016 | // C99 6.7.8p6: |
| 2017 | // |
| 2018 | // If a designator has the form |
| 2019 | // |
| 2020 | // [ constant-expression ] |
| 2021 | // |
| 2022 | // then the current object (defined below) shall have array |
| 2023 | // type and the expression shall be an integer constant |
| 2024 | // expression. If the array is of unknown size, any |
| 2025 | // nonnegative value is valid. |
| 2026 | // |
| 2027 | // Additionally, cope with the GNU extension that permits |
| 2028 | // designators of the form |
| 2029 | // |
| 2030 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2031 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2032 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2033 | if (!VerifyOnly) |
| 2034 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2035 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2036 | ++Index; |
| 2037 | return true; |
| 2038 | } |
| 2039 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2040 | Expr *IndexExpr = nullptr; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2041 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2042 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2043 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2044 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2045 | DesignatedEndIndex = DesignatedStartIndex; |
| 2046 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2047 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2048 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2050 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2052 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2053 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2054 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2055 | // Codegen can't handle evaluating array range designators that have side |
| 2056 | // effects, because we replicate the AST value for each initialized element. |
| 2057 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2058 | // elements with something that has a side effect, so codegen can emit an |
| 2059 | // "error unsupported" error instead of miscompiling the app. |
| 2060 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2061 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2062 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2065 | if (isa<ConstantArrayType>(AT)) { |
| 2066 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2067 | DesignatedStartIndex |
| 2068 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2069 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2070 | DesignatedEndIndex |
| 2071 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2072 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2073 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2074 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2075 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2076 | diag::err_array_designator_too_large) |
| 2077 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2078 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2079 | ++Index; |
| 2080 | return true; |
| 2081 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2082 | } else { |
| 2083 | // Make sure the bit-widths and signedness match. |
| 2084 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2085 | DesignatedEndIndex |
| 2086 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2087 | else if (DesignatedStartIndex.getBitWidth() < |
| 2088 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2089 | DesignatedStartIndex |
| 2090 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2091 | DesignatedStartIndex.setIsUnsigned(true); |
| 2092 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2093 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2095 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2096 | // We're modifying a string literal init; we have to decompose the string |
| 2097 | // so we can modify the individual characters. |
| 2098 | ASTContext &Context = SemaRef.Context; |
| 2099 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2100 | |
| 2101 | // Compute the character type |
| 2102 | QualType CharTy = AT->getElementType(); |
| 2103 | |
| 2104 | // Compute the type of the integer literals. |
| 2105 | QualType PromotedCharTy = CharTy; |
| 2106 | if (CharTy->isPromotableIntegerType()) |
| 2107 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2108 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2109 | |
| 2110 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2111 | // Get the length of the string. |
| 2112 | uint64_t StrLen = SL->getLength(); |
| 2113 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2114 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2115 | StructuredList->resizeInits(Context, StrLen); |
| 2116 | |
| 2117 | // Build a literal for each character in the string, and put them into |
| 2118 | // the init list. |
| 2119 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2120 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2121 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 81359b0 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2122 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2123 | if (CharTy != PromotedCharTy) |
| 2124 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2125 | Init, nullptr, VK_RValue); |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2126 | StructuredList->updateInit(Context, i, Init); |
| 2127 | } |
| 2128 | } else { |
| 2129 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2130 | std::string Str; |
| 2131 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2132 | |
| 2133 | // Get the length of the string. |
| 2134 | uint64_t StrLen = Str.size(); |
| 2135 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2136 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2137 | StructuredList->resizeInits(Context, StrLen); |
| 2138 | |
| 2139 | // Build a literal for each character in the string, and put them into |
| 2140 | // the init list. |
| 2141 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2142 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2143 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 81359b0 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2144 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2145 | if (CharTy != PromotedCharTy) |
| 2146 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2147 | Init, nullptr, VK_RValue); |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2148 | StructuredList->updateInit(Context, i, Init); |
| 2149 | } |
| 2150 | } |
| 2151 | } |
| 2152 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2153 | // Make sure that our non-designated initializer list has space |
| 2154 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2155 | if (!VerifyOnly && |
| 2156 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2157 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2158 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2159 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2160 | // Repeatedly perform subobject initializations in the range |
| 2161 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2162 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2163 | // Move to the next designator |
| 2164 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2165 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2166 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2167 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2168 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2169 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2170 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2171 | // Recurse to check later designated subobjects. |
| 2172 | QualType ElementType = AT->getElementType(); |
| 2173 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2174 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2175 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2176 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2177 | ElementType, nullptr, nullptr, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2178 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2179 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2180 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2181 | return true; |
| 2182 | |
| 2183 | // Move to the next index in the array that we'll be initializing. |
| 2184 | ++DesignatedStartIndex; |
| 2185 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2186 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2187 | |
| 2188 | // If this the first designator, our caller will continue checking |
| 2189 | // the rest of this array subobject. |
| 2190 | if (IsFirstDesignator) { |
| 2191 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2192 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2193 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2194 | return false; |
| 2195 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2196 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2197 | if (!FinishSubobjectInit) |
| 2198 | return false; |
| 2199 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2200 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2201 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2202 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2203 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2204 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2206 | } |
| 2207 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2208 | // Get the structured initializer list for a subobject of type |
| 2209 | // @p CurrentObjectType. |
| 2210 | InitListExpr * |
| 2211 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2212 | QualType CurrentObjectType, |
| 2213 | InitListExpr *StructuredList, |
| 2214 | unsigned StructuredIndex, |
| 2215 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2216 | if (VerifyOnly) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2217 | return nullptr; // No structured list in verification-only mode. |
| 2218 | Expr *ExistingInit = nullptr; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2219 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2220 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2221 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2222 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2224 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2225 | return Result; |
| 2226 | |
| 2227 | if (ExistingInit) { |
| 2228 | // We are creating an initializer list that initializes the |
| 2229 | // subobjects of the current object, but there was already an |
| 2230 | // initialization that completely initialized the current |
| 2231 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2233 | // struct X { int a, b; }; |
| 2234 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2235 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2236 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2237 | // designated initializer re-initializes the whole |
| 2238 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2239 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2240 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2241 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2242 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2243 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2244 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2245 | << ExistingInit->getSourceRange(); |
| 2246 | } |
| 2247 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2249 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2250 | InitRange.getBegin(), None, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2251 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2252 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2253 | QualType ResultType = CurrentObjectType; |
| 2254 | if (!ResultType->isArrayType()) |
| 2255 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2256 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2257 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2258 | // Pre-allocate storage for the structured initializer list. |
| 2259 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2260 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2261 | bool GotNumInits = false; |
| 2262 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2263 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2264 | GotNumInits = true; |
| 2265 | } else if (Index < IList->getNumInits()) { |
| 2266 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2267 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2268 | GotNumInits = true; |
| 2269 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2272 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2273 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2274 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2275 | NumElements = CAType->getSize().getZExtValue(); |
| 2276 | // Simple heuristic so that we don't allocate a very large |
| 2277 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2278 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2279 | NumElements = 0; |
| 2280 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2281 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2282 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2283 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2284 | RecordDecl *RDecl = RType->getDecl(); |
| 2285 | if (RDecl->isUnion()) |
| 2286 | NumElements = 1; |
| 2287 | else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 2288 | NumElements = std::distance(RDecl->field_begin(), RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2291 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2292 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2293 | // Link this new initializer list into the structured initializer |
| 2294 | // lists. |
| 2295 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2296 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2297 | else { |
| 2298 | Result->setSyntacticForm(IList); |
| 2299 | SyntacticToSemantic[IList] = Result; |
| 2300 | } |
| 2301 | |
| 2302 | return Result; |
| 2303 | } |
| 2304 | |
| 2305 | /// Update the initializer at index @p StructuredIndex within the |
| 2306 | /// structured initializer list to the value @p expr. |
| 2307 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2308 | unsigned &StructuredIndex, |
| 2309 | Expr *expr) { |
| 2310 | // No structured initializer list to update |
| 2311 | if (!StructuredList) |
| 2312 | return; |
| 2313 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2314 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2315 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2316 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2317 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2318 | diag::warn_initializer_overrides) |
| 2319 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2320 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2321 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2322 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2323 | << PrevInit->getSourceRange(); |
| 2324 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2326 | ++StructuredIndex; |
| 2327 | } |
| 2328 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2329 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2330 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2331 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2332 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2333 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2334 | /// added, on success. If everything went okay, Value will receive the |
| 2335 | /// value of the constant expression. |
| 2336 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2337 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2338 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2339 | |
| 2340 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2341 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2342 | if (Result.isInvalid()) |
| 2343 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2344 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2345 | if (Value.isSigned() && Value.isNegative()) |
| 2346 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2347 | << Value.toString(10) << Index->getSourceRange(); |
| 2348 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2349 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2350 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2351 | } |
| 2352 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2353 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2354 | SourceLocation Loc, |
| 2355 | bool GNUSyntax, |
| 2356 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2357 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2358 | |
| 2359 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2360 | SmallVector<ASTDesignator, 32> Designators; |
| 2361 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2362 | |
| 2363 | // Build designators and check array designator expressions. |
| 2364 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2365 | const Designator &D = Desig.getDesignator(Idx); |
| 2366 | switch (D.getKind()) { |
| 2367 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2368 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2369 | D.getFieldLoc())); |
| 2370 | break; |
| 2371 | |
| 2372 | case Designator::ArrayDesignator: { |
| 2373 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2374 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2375 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2376 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2377 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2378 | Invalid = true; |
| 2379 | else { |
| 2380 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2381 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2382 | D.getRBracketLoc())); |
| 2383 | InitExpressions.push_back(Index); |
| 2384 | } |
| 2385 | break; |
| 2386 | } |
| 2387 | |
| 2388 | case Designator::ArrayRangeDesignator: { |
| 2389 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2390 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2391 | llvm::APSInt StartValue; |
| 2392 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2393 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2394 | StartIndex->isValueDependent(); |
| 2395 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2396 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2397 | if (!StartDependent) |
| 2398 | StartIndex = |
| 2399 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2400 | if (!EndDependent) |
| 2401 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2402 | |
| 2403 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2404 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2405 | else { |
| 2406 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2407 | if (StartDependent || EndDependent) { |
| 2408 | // Nothing to compute. |
| 2409 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2410 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2411 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2412 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2413 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2414 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2415 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2416 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2417 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2418 | Invalid = true; |
| 2419 | } else { |
| 2420 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2421 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2422 | D.getEllipsisLoc(), |
| 2423 | D.getRBracketLoc())); |
| 2424 | InitExpressions.push_back(StartIndex); |
| 2425 | InitExpressions.push_back(EndIndex); |
| 2426 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2427 | } |
| 2428 | break; |
| 2429 | } |
| 2430 | } |
| 2431 | } |
| 2432 | |
| 2433 | if (Invalid || Init.isInvalid()) |
| 2434 | return ExprError(); |
| 2435 | |
| 2436 | // Clear out the expressions within the designation. |
| 2437 | Desig.ClearExprs(*this); |
| 2438 | |
| 2439 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2440 | = DesignatedInitExpr::Create(Context, |
| 2441 | Designators.data(), Designators.size(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2442 | InitExpressions, Loc, GNUSyntax, |
| 2443 | Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2444 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2445 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2446 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2447 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2448 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2449 | return Owned(DIE); |
| 2450 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2451 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2452 | //===----------------------------------------------------------------------===// |
| 2453 | // Initialization entity |
| 2454 | //===----------------------------------------------------------------------===// |
| 2455 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2456 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2457 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2458 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2459 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2460 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2461 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2462 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2463 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2464 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2465 | Type = VT->getElementType(); |
| 2466 | } else { |
| 2467 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2468 | assert(CT && "Unexpected type"); |
| 2469 | Kind = EK_ComplexElement; |
| 2470 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2471 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2474 | InitializedEntity |
| 2475 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 2476 | const CXXBaseSpecifier *Base, |
| 2477 | bool IsInheritedVirtualBase) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2478 | InitializedEntity Result; |
| 2479 | Result.Kind = EK_Base; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2480 | Result.Parent = nullptr; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2481 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2482 | if (IsInheritedVirtualBase) |
| 2483 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2484 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2485 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2486 | return Result; |
| 2487 | } |
| 2488 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2489 | DeclarationName InitializedEntity::getName() const { |
| 2490 | switch (getKind()) { |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2491 | case EK_Parameter: |
| 2492 | case EK_Parameter_CF_Audited: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2493 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2494 | return (D ? D->getDeclName() : DeclarationName()); |
| 2495 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2496 | |
| 2497 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2498 | case EK_Member: |
| 2499 | return VariableOrMember->getDeclName(); |
| 2500 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2501 | case EK_LambdaCapture: |
Bill Wendling | 2434dcf | 2013-12-05 05:25:04 +0000 | [diff] [blame] | 2502 | return DeclarationName(Capture.VarID); |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2503 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2504 | case EK_Result: |
| 2505 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2506 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2507 | case EK_Temporary: |
| 2508 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2509 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2510 | case EK_ArrayElement: |
| 2511 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2512 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2513 | case EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2514 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2515 | case EK_RelatedResult: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2516 | return DeclarationName(); |
| 2517 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2518 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2519 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2522 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2523 | switch (getKind()) { |
| 2524 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2525 | case EK_Member: |
| 2526 | return VariableOrMember; |
| 2527 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2528 | case EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2529 | case EK_Parameter_CF_Audited: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2530 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2531 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2532 | case EK_Result: |
| 2533 | case EK_Exception: |
| 2534 | case EK_New: |
| 2535 | case EK_Temporary: |
| 2536 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2537 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2538 | case EK_ArrayElement: |
| 2539 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2540 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2541 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2542 | case EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2543 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2544 | case EK_RelatedResult: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 2545 | return nullptr; |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2546 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2547 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2548 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2551 | bool InitializedEntity::allowsNRVO() const { |
| 2552 | switch (getKind()) { |
| 2553 | case EK_Result: |
| 2554 | case EK_Exception: |
| 2555 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2556 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2557 | case EK_Variable: |
| 2558 | case EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2559 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2560 | case EK_Member: |
| 2561 | case EK_New: |
| 2562 | case EK_Temporary: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2563 | case EK_CompoundLiteralInit: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2564 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2565 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2566 | case EK_ArrayElement: |
| 2567 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2568 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2569 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2570 | case EK_LambdaCapture: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2571 | case EK_RelatedResult: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2572 | break; |
| 2573 | } |
| 2574 | |
| 2575 | return false; |
| 2576 | } |
| 2577 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2578 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2579 | assert(getParent() != this); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2580 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 2581 | for (unsigned I = 0; I != Depth; ++I) |
| 2582 | OS << "`-"; |
| 2583 | |
| 2584 | switch (getKind()) { |
| 2585 | case EK_Variable: OS << "Variable"; break; |
| 2586 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2587 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 2588 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2589 | case EK_Result: OS << "Result"; break; |
| 2590 | case EK_Exception: OS << "Exception"; break; |
| 2591 | case EK_Member: OS << "Member"; break; |
| 2592 | case EK_New: OS << "New"; break; |
| 2593 | case EK_Temporary: OS << "Temporary"; break; |
| 2594 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2595 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2596 | case EK_Base: OS << "Base"; break; |
| 2597 | case EK_Delegating: OS << "Delegating"; break; |
| 2598 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 2599 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 2600 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 2601 | case EK_BlockElement: OS << "Block"; break; |
| 2602 | case EK_LambdaCapture: |
| 2603 | OS << "LambdaCapture "; |
Bill Wendling | 2434dcf | 2013-12-05 05:25:04 +0000 | [diff] [blame] | 2604 | OS << DeclarationName(Capture.VarID); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2605 | break; |
| 2606 | } |
| 2607 | |
| 2608 | if (Decl *D = getDecl()) { |
| 2609 | OS << " "; |
| 2610 | cast<NamedDecl>(D)->printQualifiedName(OS); |
| 2611 | } |
| 2612 | |
| 2613 | OS << " '" << getType().getAsString() << "'\n"; |
| 2614 | |
| 2615 | return Depth + 1; |
| 2616 | } |
| 2617 | |
| 2618 | void InitializedEntity::dump() const { |
| 2619 | dumpImpl(llvm::errs()); |
| 2620 | } |
| 2621 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2622 | //===----------------------------------------------------------------------===// |
| 2623 | // Initialization sequence |
| 2624 | //===----------------------------------------------------------------------===// |
| 2625 | |
| 2626 | void InitializationSequence::Step::Destroy() { |
| 2627 | switch (Kind) { |
| 2628 | case SK_ResolveAddressOfOverloadedFunction: |
| 2629 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2630 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2631 | case SK_CastDerivedToBaseLValue: |
| 2632 | case SK_BindReference: |
| 2633 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2634 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2635 | case SK_UserConversion: |
| 2636 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2637 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2638 | case SK_QualificationConversionLValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2639 | case SK_LValueToRValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2640 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2641 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2642 | case SK_UnwrapInitList: |
| 2643 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2644 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2645 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2646 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2647 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2648 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2649 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2650 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2651 | case SK_PassByIndirectCopyRestore: |
| 2652 | case SK_PassByIndirectRestore: |
| 2653 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2654 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2655 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2656 | case SK_OCLZeroEvent: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2657 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2658 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2659 | case SK_ConversionSequence: |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2660 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2661 | delete ICS; |
| 2662 | } |
| 2663 | } |
| 2664 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2665 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2666 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2670 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2671 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2672 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2673 | switch (getFailureKind()) { |
| 2674 | case FK_TooManyInitsForReference: |
| 2675 | case FK_ArrayNeedsInitList: |
| 2676 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 2677 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 2678 | case FK_NarrowStringIntoWideCharArray: |
| 2679 | case FK_WideStringIntoCharArray: |
| 2680 | case FK_IncompatWideStringIntoWideChar: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2681 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2682 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2683 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2684 | case FK_RValueReferenceBindingToLValue: |
| 2685 | case FK_ReferenceInitDropsQualifiers: |
| 2686 | case FK_ReferenceInitFailed: |
| 2687 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2688 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2689 | case FK_TooManyInitsForScalar: |
| 2690 | case FK_ReferenceBindingToInitList: |
| 2691 | case FK_InitListBadDestinationType: |
| 2692 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2693 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2694 | case FK_ArrayTypeMismatch: |
| 2695 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2696 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2697 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2698 | case FK_PlaceholderType: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2699 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2700 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2701 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2702 | case FK_ReferenceInitOverloadFailed: |
| 2703 | case FK_UserConversionOverloadFailed: |
| 2704 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2705 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2706 | return FailedOverloadResult == OR_Ambiguous; |
| 2707 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2708 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2709 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2712 | bool InitializationSequence::isConstructorInitialization() const { |
| 2713 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2714 | } |
| 2715 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2716 | void |
| 2717 | InitializationSequence |
| 2718 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2719 | DeclAccessPair Found, |
| 2720 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2721 | Step S; |
| 2722 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2723 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2724 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2725 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2726 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2727 | Steps.push_back(S); |
| 2728 | } |
| 2729 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2730 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2731 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2732 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2733 | switch (VK) { |
| 2734 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2735 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2736 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2737 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2738 | S.Type = BaseType; |
| 2739 | Steps.push_back(S); |
| 2740 | } |
| 2741 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2742 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2743 | bool BindingTemporary) { |
| 2744 | Step S; |
| 2745 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2746 | S.Type = T; |
| 2747 | Steps.push_back(S); |
| 2748 | } |
| 2749 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2750 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2751 | Step S; |
| 2752 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2753 | S.Type = T; |
| 2754 | Steps.push_back(S); |
| 2755 | } |
| 2756 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2757 | void |
| 2758 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2759 | DeclAccessPair FoundDecl, |
| 2760 | QualType T, |
| 2761 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2762 | Step S; |
| 2763 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2764 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2765 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2766 | S.Function.Function = Function; |
| 2767 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2768 | Steps.push_back(S); |
| 2769 | } |
| 2770 | |
| 2771 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2772 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2773 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2774 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2775 | switch (VK) { |
| 2776 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2777 | S.Kind = SK_QualificationConversionRValue; |
| 2778 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2779 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2780 | S.Kind = SK_QualificationConversionXValue; |
| 2781 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2782 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2783 | S.Kind = SK_QualificationConversionLValue; |
| 2784 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2785 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2786 | S.Type = Ty; |
| 2787 | Steps.push_back(S); |
| 2788 | } |
| 2789 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2790 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2791 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2792 | |
| 2793 | Step S; |
| 2794 | S.Kind = SK_LValueToRValue; |
| 2795 | S.Type = Ty; |
| 2796 | Steps.push_back(S); |
| 2797 | } |
| 2798 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2799 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2800 | const ImplicitConversionSequence &ICS, QualType T, |
| 2801 | bool TopLevelOfInitList) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2802 | Step S; |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2803 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 2804 | : SK_ConversionSequence; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2805 | S.Type = T; |
| 2806 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2807 | Steps.push_back(S); |
| 2808 | } |
| 2809 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2810 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2811 | Step S; |
| 2812 | S.Kind = SK_ListInitialization; |
| 2813 | S.Type = T; |
| 2814 | Steps.push_back(S); |
| 2815 | } |
| 2816 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2817 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2818 | InitializationSequence |
| 2819 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2820 | AccessSpecifier Access, |
| 2821 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2822 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2823 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2824 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2825 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2826 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2827 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2828 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2829 | S.Function.Function = Constructor; |
| 2830 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2831 | Steps.push_back(S); |
| 2832 | } |
| 2833 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2834 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2835 | Step S; |
| 2836 | S.Kind = SK_ZeroInitialization; |
| 2837 | S.Type = T; |
| 2838 | Steps.push_back(S); |
| 2839 | } |
| 2840 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2841 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2842 | Step S; |
| 2843 | S.Kind = SK_CAssignment; |
| 2844 | S.Type = T; |
| 2845 | Steps.push_back(S); |
| 2846 | } |
| 2847 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2848 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2849 | Step S; |
| 2850 | S.Kind = SK_StringInit; |
| 2851 | S.Type = T; |
| 2852 | Steps.push_back(S); |
| 2853 | } |
| 2854 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2855 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2856 | Step S; |
| 2857 | S.Kind = SK_ObjCObjectConversion; |
| 2858 | S.Type = T; |
| 2859 | Steps.push_back(S); |
| 2860 | } |
| 2861 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2862 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2863 | Step S; |
| 2864 | S.Kind = SK_ArrayInit; |
| 2865 | S.Type = T; |
| 2866 | Steps.push_back(S); |
| 2867 | } |
| 2868 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2869 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2870 | Step S; |
| 2871 | S.Kind = SK_ParenthesizedArrayInit; |
| 2872 | S.Type = T; |
| 2873 | Steps.push_back(S); |
| 2874 | } |
| 2875 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2876 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2877 | bool shouldCopy) { |
| 2878 | Step s; |
| 2879 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2880 | : SK_PassByIndirectRestore); |
| 2881 | s.Type = type; |
| 2882 | Steps.push_back(s); |
| 2883 | } |
| 2884 | |
| 2885 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2886 | Step S; |
| 2887 | S.Kind = SK_ProduceObjCObject; |
| 2888 | S.Type = T; |
| 2889 | Steps.push_back(S); |
| 2890 | } |
| 2891 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2892 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2893 | Step S; |
| 2894 | S.Kind = SK_StdInitializerList; |
| 2895 | S.Type = T; |
| 2896 | Steps.push_back(S); |
| 2897 | } |
| 2898 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2899 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 2900 | Step S; |
| 2901 | S.Kind = SK_OCLSamplerInit; |
| 2902 | S.Type = T; |
| 2903 | Steps.push_back(S); |
| 2904 | } |
| 2905 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2906 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 2907 | Step S; |
| 2908 | S.Kind = SK_OCLZeroEvent; |
| 2909 | S.Type = T; |
| 2910 | Steps.push_back(S); |
| 2911 | } |
| 2912 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2913 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2914 | InitListExpr *Syntactic) { |
| 2915 | assert(Syntactic->getNumInits() == 1 && |
| 2916 | "Can only rewrap trivial init lists."); |
| 2917 | Step S; |
| 2918 | S.Kind = SK_UnwrapInitList; |
| 2919 | S.Type = Syntactic->getInit(0)->getType(); |
| 2920 | Steps.insert(Steps.begin(), S); |
| 2921 | |
| 2922 | S.Kind = SK_RewrapInitList; |
| 2923 | S.Type = T; |
| 2924 | S.WrappingSyntacticList = Syntactic; |
| 2925 | Steps.push_back(S); |
| 2926 | } |
| 2927 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2928 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2929 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2930 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2931 | this->Failure = Failure; |
| 2932 | this->FailedOverloadResult = Result; |
| 2933 | } |
| 2934 | |
| 2935 | //===----------------------------------------------------------------------===// |
| 2936 | // Attempt initialization |
| 2937 | //===----------------------------------------------------------------------===// |
| 2938 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2939 | static void MaybeProduceObjCObject(Sema &S, |
| 2940 | InitializationSequence &Sequence, |
| 2941 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2942 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2943 | |
| 2944 | /// When initializing a parameter, produce the value if it's marked |
| 2945 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2946 | if (Entity.isParameterKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2947 | if (!Entity.isParameterConsumed()) |
| 2948 | return; |
| 2949 | |
| 2950 | assert(Entity.getType()->isObjCRetainableType() && |
| 2951 | "consuming an object of unretainable type?"); |
| 2952 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2953 | |
| 2954 | /// When initializing a return value, if the return type is a |
| 2955 | /// retainable type, then returns need to immediately retain the |
| 2956 | /// object. If an autorelease is required, it will be done at the |
| 2957 | /// last instant. |
| 2958 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2959 | if (!Entity.getType()->isObjCRetainableType()) |
| 2960 | return; |
| 2961 | |
| 2962 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2963 | } |
| 2964 | } |
| 2965 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2966 | static void TryListInitialization(Sema &S, |
| 2967 | const InitializedEntity &Entity, |
| 2968 | const InitializationKind &Kind, |
| 2969 | InitListExpr *InitList, |
| 2970 | InitializationSequence &Sequence); |
| 2971 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2972 | /// \brief When initializing from init list via constructor, handle |
| 2973 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2974 | /// |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2975 | /// \return true if we have handled initialization of an object of type |
| 2976 | /// std::initializer_list<T>, false otherwise. |
| 2977 | static bool TryInitializerListConstruction(Sema &S, |
| 2978 | InitListExpr *List, |
| 2979 | QualType DestType, |
| 2980 | InitializationSequence &Sequence) { |
| 2981 | QualType E; |
| 2982 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2983 | return false; |
| 2984 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2985 | if (S.RequireCompleteType(List->getExprLoc(), E, 0)) { |
| 2986 | Sequence.setIncompleteTypeFailure(E); |
| 2987 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2988 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2989 | |
| 2990 | // Try initializing a temporary array from the init list. |
| 2991 | QualType ArrayType = S.Context.getConstantArrayType( |
| 2992 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2993 | List->getNumInits()), |
| 2994 | clang::ArrayType::Normal, 0); |
| 2995 | InitializedEntity HiddenArray = |
| 2996 | InitializedEntity::InitializeTemporary(ArrayType); |
| 2997 | InitializationKind Kind = |
| 2998 | InitializationKind::CreateDirectList(List->getExprLoc()); |
| 2999 | TryListInitialization(S, HiddenArray, Kind, List, Sequence); |
| 3000 | if (Sequence) |
| 3001 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3002 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3005 | static OverloadingResult |
| 3006 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3007 | MultiExprArg Args, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3008 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3009 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3010 | OverloadCandidateSet::iterator &Best, |
| 3011 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3012 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3013 | CandidateSet.clear(); |
| 3014 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3015 | for (ArrayRef<NamedDecl *>::iterator |
| 3016 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3017 | NamedDecl *D = *Con; |
| 3018 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3019 | bool SuppressUserConversions = false; |
| 3020 | |
| 3021 | // Find the constructor (which may be a template). |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3022 | CXXConstructorDecl *Constructor = nullptr; |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3023 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 3024 | if (ConstructorTmpl) |
| 3025 | Constructor = cast<CXXConstructorDecl>( |
| 3026 | ConstructorTmpl->getTemplatedDecl()); |
| 3027 | else { |
| 3028 | Constructor = cast<CXXConstructorDecl>(D); |
| 3029 | |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3030 | // C++11 [over.best.ics]p4: |
| 3031 | // However, when considering the argument of a constructor or |
| 3032 | // user-defined conversion function that is a candidate: |
| 3033 | // -- by 13.3.1.3 when invoked for the copying/moving of a temporary |
| 3034 | // in the second step of a class copy-initialization, |
| 3035 | // -- by 13.3.1.7 when passing the initializer list as a single |
| 3036 | // argument or when the initializer list has exactly one elementand |
| 3037 | // a conversion to some class X or reference to (possibly |
| 3038 | // cv-qualified) X is considered for the first parameter of a |
| 3039 | // constructor of X, or |
| 3040 | // -- by 13.3.1.4, 13.3.1.5, or 13.3.1.6 in all cases, |
| 3041 | // only standard conversion sequences and ellipsis conversion sequences |
| 3042 | // are considered. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3043 | if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) && |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3044 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3045 | SuppressUserConversions = true; |
| 3046 | } |
| 3047 | |
| 3048 | if (!Constructor->isInvalidDecl() && |
| 3049 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3050 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3051 | if (ConstructorTmpl) |
| 3052 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3053 | /*ExplicitArgs*/ nullptr, Args, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3054 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3055 | else { |
| 3056 | // C++ [over.match.copy]p1: |
| 3057 | // - When initializing a temporary to be bound to the first parameter |
| 3058 | // of a constructor that takes a reference to possibly cv-qualified |
| 3059 | // T as its first argument, called with a single argument in the |
| 3060 | // context of direct-initialization, explicit conversion functions |
| 3061 | // are also considered. |
| 3062 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3063 | Args.size() == 1 && |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3064 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3065 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3066 | SuppressUserConversions, |
| 3067 | /*PartialOverloading=*/false, |
| 3068 | /*AllowExplicit=*/AllowExplicitConv); |
| 3069 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3070 | } |
| 3071 | } |
| 3072 | |
| 3073 | // Perform overload resolution and return the result. |
| 3074 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3075 | } |
| 3076 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3077 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3078 | /// enumerates the constructors of the initialized entity and performs overload |
| 3079 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3080 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3081 | /// class type. |
| 3082 | static void TryConstructorInitialization(Sema &S, |
| 3083 | const InitializedEntity &Entity, |
| 3084 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3085 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3086 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3087 | bool InitListSyntax = false) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3088 | assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3089 | "InitListSyntax must come with a single initializer list argument."); |
| 3090 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3091 | // The type we're constructing needs to be complete. |
| 3092 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3093 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3094 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
| 3097 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3098 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3099 | CXXRecordDecl *DestRecordDecl |
| 3100 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3101 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3102 | // Build the candidate set directly in the initialization sequence |
| 3103 | // structure, so that it will persist if we fail. |
| 3104 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3105 | |
| 3106 | // Determine whether we are allowed to call explicit constructors or |
| 3107 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3108 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3109 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3110 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3111 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3112 | // applicable constructors are enumerated, and the best one is chosen |
| 3113 | // through overload resolution. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3114 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3115 | // The container holding the constructors can under certain conditions |
| 3116 | // be changed while iterating (e.g. because of deserialization). |
| 3117 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3118 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3119 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3120 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3121 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3122 | bool AsInitializerList = false; |
| 3123 | |
| 3124 | // C++11 [over.match.list]p1: |
| 3125 | // When objects of non-aggregate type T are list-initialized, overload |
| 3126 | // resolution selects the constructor in two phases: |
| 3127 | // - Initially, the candidate functions are the initializer-list |
| 3128 | // constructors of the class T and the argument list consists of the |
| 3129 | // initializer list as a single argument. |
| 3130 | if (InitListSyntax) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3131 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3132 | AsInitializerList = true; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3133 | |
| 3134 | // If the initializer list has no elements and T has a default constructor, |
| 3135 | // the first phase is omitted. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3136 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3137 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3138 | CandidateSet, Ctors, Best, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3139 | CopyInitialization, AllowExplicit, |
| 3140 | /*OnlyListConstructor=*/true, |
| 3141 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3142 | |
| 3143 | // Time to unwrap the init list. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3144 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | // C++11 [over.match.list]p1: |
| 3148 | // - If no viable initializer-list constructor is found, overload resolution |
| 3149 | // is performed again, where the candidate functions are all the |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3150 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3151 | // elements of the initializer list. |
| 3152 | if (Result == OR_No_Viable_Function) { |
| 3153 | AsInitializerList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3154 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3155 | CandidateSet, Ctors, Best, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3156 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3157 | /*OnlyListConstructors=*/false, |
| 3158 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3159 | } |
| 3160 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3161 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3162 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3163 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3164 | Result); |
| 3165 | return; |
| 3166 | } |
| 3167 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3168 | // C++11 [dcl.init]p6: |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3169 | // If a program calls for the default initialization of an object |
| 3170 | // of a const-qualified type T, T shall be a class type with a |
| 3171 | // user-provided default constructor. |
| 3172 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3173 | Entity.getType().isConstQualified() && |
Aaron Ballman | 5121781 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 3174 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3175 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3176 | return; |
| 3177 | } |
| 3178 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3179 | // C++11 [over.match.list]p1: |
| 3180 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3181 | // initializer is ill-formed. |
| 3182 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 3183 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 3184 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3185 | return; |
| 3186 | } |
| 3187 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3188 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3189 | // subsumed by the initialization. |
| 3190 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3191 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 3192 | Best->FoundDecl.getAccess(), |
| 3193 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3194 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3197 | static bool |
| 3198 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3199 | Expr *Initializer, |
| 3200 | QualType &SourceType, |
| 3201 | QualType &UnqualifiedSourceType, |
| 3202 | QualType UnqualifiedTargetType, |
| 3203 | InitializationSequence &Sequence) { |
| 3204 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3205 | S.Context.OverloadTy) { |
| 3206 | DeclAccessPair Found; |
| 3207 | bool HadMultipleCandidates = false; |
| 3208 | if (FunctionDecl *Fn |
| 3209 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3210 | UnqualifiedTargetType, |
| 3211 | false, Found, |
| 3212 | &HadMultipleCandidates)) { |
| 3213 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3214 | HadMultipleCandidates); |
| 3215 | SourceType = Fn->getType(); |
| 3216 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3217 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3218 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3219 | return true; |
| 3220 | } |
| 3221 | } |
| 3222 | return false; |
| 3223 | } |
| 3224 | |
| 3225 | static void TryReferenceInitializationCore(Sema &S, |
| 3226 | const InitializedEntity &Entity, |
| 3227 | const InitializationKind &Kind, |
| 3228 | Expr *Initializer, |
| 3229 | QualType cv1T1, QualType T1, |
| 3230 | Qualifiers T1Quals, |
| 3231 | QualType cv2T2, QualType T2, |
| 3232 | Qualifiers T2Quals, |
| 3233 | InitializationSequence &Sequence); |
| 3234 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3235 | static void TryValueInitialization(Sema &S, |
| 3236 | const InitializedEntity &Entity, |
| 3237 | const InitializationKind &Kind, |
| 3238 | InitializationSequence &Sequence, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3239 | InitListExpr *InitList = nullptr); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3240 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3241 | /// \brief Attempt list initialization of a reference. |
| 3242 | static void TryReferenceListInitialization(Sema &S, |
| 3243 | const InitializedEntity &Entity, |
| 3244 | const InitializationKind &Kind, |
| 3245 | InitListExpr *InitList, |
Richard Smith | b6e3808 | 2013-06-08 00:02:08 +0000 | [diff] [blame] | 3246 | InitializationSequence &Sequence) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3247 | // First, catch C++03 where this isn't possible. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3248 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3249 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3250 | return; |
| 3251 | } |
| 3252 | |
| 3253 | QualType DestType = Entity.getType(); |
| 3254 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3255 | Qualifiers T1Quals; |
| 3256 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3257 | |
| 3258 | // Reference initialization via an initializer list works thus: |
| 3259 | // If the initializer list consists of a single element that is |
| 3260 | // reference-related to the referenced type, bind directly to that element |
| 3261 | // (possibly creating temporaries). |
| 3262 | // Otherwise, initialize a temporary with the initializer list and |
| 3263 | // bind to that. |
| 3264 | if (InitList->getNumInits() == 1) { |
| 3265 | Expr *Initializer = InitList->getInit(0); |
| 3266 | QualType cv2T2 = Initializer->getType(); |
| 3267 | Qualifiers T2Quals; |
| 3268 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3269 | |
| 3270 | // If this fails, creating a temporary wouldn't work either. |
| 3271 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3272 | T1, Sequence)) |
| 3273 | return; |
| 3274 | |
| 3275 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3276 | bool dummy1, dummy2, dummy3; |
| 3277 | Sema::ReferenceCompareResult RefRelationship |
| 3278 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3279 | dummy2, dummy3); |
| 3280 | if (RefRelationship >= Sema::Ref_Related) { |
| 3281 | // Try to bind the reference here. |
| 3282 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3283 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3284 | if (Sequence) |
| 3285 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3286 | return; |
| 3287 | } |
Richard Smith | 02d65ee | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3288 | |
| 3289 | // Update the initializer if we've resolved an overloaded function. |
| 3290 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3291 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | // Not reference-related. Create a temporary and bind to that. |
| 3295 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3296 | |
| 3297 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3298 | if (Sequence) { |
| 3299 | if (DestType->isRValueReferenceType() || |
| 3300 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3301 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3302 | else |
| 3303 | Sequence.SetFailed( |
| 3304 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3305 | } |
| 3306 | } |
| 3307 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3308 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3309 | static void TryListInitialization(Sema &S, |
| 3310 | const InitializedEntity &Entity, |
| 3311 | const InitializationKind &Kind, |
| 3312 | InitListExpr *InitList, |
| 3313 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3314 | QualType DestType = Entity.getType(); |
| 3315 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3316 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3317 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3318 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3319 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3320 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3321 | return; |
| 3322 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3323 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3324 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3325 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3326 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3327 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3328 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3329 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3330 | return; |
| 3331 | } |
| 3332 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3333 | // C++11 [dcl.init.list]p3: |
| 3334 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3335 | if (!DestType->isAggregateType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3336 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3337 | // - Otherwise, if the initializer list has no elements and T is a |
| 3338 | // class type with a default constructor, the object is |
| 3339 | // value-initialized. |
| 3340 | if (InitList->getNumInits() == 0) { |
| 3341 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3342 | if (RD->hasDefaultConstructor()) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3343 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3344 | return; |
| 3345 | } |
| 3346 | } |
| 3347 | |
| 3348 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3349 | // an initializer_list object constructed [...] |
| 3350 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3351 | return; |
| 3352 | |
| 3353 | // - Otherwise, if T is a class type, constructors are considered. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3354 | Expr *InitListAsExpr = InitList; |
| 3355 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3356 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3357 | } else |
| 3358 | Sequence.SetFailed( |
| 3359 | InitializationSequence::FK_InitListBadDestinationType); |
| 3360 | return; |
| 3361 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3362 | } |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 3363 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
| 3364 | InitList->getNumInits() == 1 && |
| 3365 | InitList->getInit(0)->getType()->isRecordType()) { |
| 3366 | // - Otherwise, if the initializer list has a single element of type E |
| 3367 | // [...references are handled above...], the object or reference is |
| 3368 | // initialized from that element; if a narrowing conversion is required |
| 3369 | // to convert the element to T, the program is ill-formed. |
| 3370 | // |
| 3371 | // Per core-24034, this is direct-initialization if we were performing |
| 3372 | // direct-list-initialization and copy-initialization otherwise. |
| 3373 | // We can't use InitListChecker for this, because it always performs |
| 3374 | // copy-initialization. This only matters if we might use an 'explicit' |
| 3375 | // conversion operator, so we only need to handle the cases where the source |
| 3376 | // is of record type. |
| 3377 | InitializationKind SubKind = |
| 3378 | Kind.getKind() == InitializationKind::IK_DirectList |
| 3379 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 3380 | InitList->getLBraceLoc(), |
| 3381 | InitList->getRBraceLoc()) |
| 3382 | : Kind; |
| 3383 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 3384 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 3385 | /*TopLevelOfInitList*/true); |
| 3386 | if (Sequence) |
| 3387 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 3388 | return; |
| 3389 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3390 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3391 | InitListChecker CheckInitList(S, Entity, InitList, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 3392 | DestType, /*VerifyOnly=*/true); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3393 | if (CheckInitList.HadError()) { |
| 3394 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3395 | return; |
| 3396 | } |
| 3397 | |
| 3398 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3399 | Sequence.AddListInitializationStep(DestType); |
| 3400 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3401 | |
| 3402 | /// \brief Try a reference initialization that involves calling a conversion |
| 3403 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3404 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3405 | const InitializedEntity &Entity, |
| 3406 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3407 | Expr *Initializer, |
| 3408 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3409 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3410 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3411 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3412 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3413 | QualType cv2T2 = Initializer->getType(); |
| 3414 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3415 | |
| 3416 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3417 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3418 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3419 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3420 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3421 | ObjCConversion, |
| 3422 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3423 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3424 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3425 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3426 | (void)ObjCLifetimeConversion; |
| 3427 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3428 | // Build the candidate set directly in the initialization sequence |
| 3429 | // structure, so that it will persist if we fail. |
| 3430 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3431 | CandidateSet.clear(); |
| 3432 | |
| 3433 | // Determine whether we are allowed to call explicit constructors or |
| 3434 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3435 | bool AllowExplicit = Kind.AllowExplicit(); |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3436 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 3437 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3438 | const RecordType *T1RecordType = nullptr; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3439 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3440 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3441 | // The type we're converting to is a class type. Enumerate its constructors |
| 3442 | // to see if there is a suitable conversion. |
| 3443 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3444 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3445 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3446 | // The container holding the constructors can under certain conditions |
| 3447 | // be changed while iterating (e.g. because of deserialization). |
| 3448 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3449 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3450 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3451 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3452 | NamedDecl *D = *CI; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3453 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3454 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3455 | // Find the constructor (which may be a template). |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3456 | CXXConstructorDecl *Constructor = nullptr; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3457 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3458 | if (ConstructorTmpl) |
| 3459 | Constructor = cast<CXXConstructorDecl>( |
| 3460 | ConstructorTmpl->getTemplatedDecl()); |
| 3461 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3462 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3463 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3464 | if (!Constructor->isInvalidDecl() && |
| 3465 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3466 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3467 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3468 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3469 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3470 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3471 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3472 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3473 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3474 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3475 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3476 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3477 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3478 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3479 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3480 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 3481 | const RecordType *T2RecordType = nullptr; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3482 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3483 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3484 | // The type we're converting from is a class type, enumerate its conversion |
| 3485 | // functions. |
| 3486 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3487 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3488 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3489 | CXXRecordDecl::conversion_iterator> |
| 3490 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3491 | for (CXXRecordDecl::conversion_iterator |
| 3492 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3493 | NamedDecl *D = *I; |
| 3494 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3495 | if (isa<UsingShadowDecl>(D)) |
| 3496 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3497 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3498 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3499 | CXXConversionDecl *Conv; |
| 3500 | if (ConvTemplate) |
| 3501 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3502 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3503 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3504 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3505 | // If the conversion function doesn't return a reference type, |
| 3506 | // it can't be considered for this conversion unless we're allowed to |
| 3507 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3508 | // FIXME: Do we need to make sure that we only consider conversion |
| 3509 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3510 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3511 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3512 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3513 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3514 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3515 | ActingDC, Initializer, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3516 | DestType, CandidateSet, |
| 3517 | /*AllowObjCConversionOnExplicit=*/ |
| 3518 | false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3519 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3520 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3521 | Initializer, DestType, CandidateSet, |
| 3522 | /*AllowObjCConversionOnExplicit=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3523 | } |
| 3524 | } |
| 3525 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3526 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3527 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3528 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3529 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3530 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3531 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3532 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3533 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3534 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3535 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3536 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3537 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3538 | // This is the overload that will be used for this initialization step if we |
| 3539 | // use this initialization. Mark it as referenced. |
| 3540 | Function->setReferenced(); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3541 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3542 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3543 | if (isa<CXXConversionDecl>(Function)) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 3544 | T2 = Function->getReturnType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3545 | else |
| 3546 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3547 | |
| 3548 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3549 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3550 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3551 | T2.getNonLValueExprType(S.Context), |
| 3552 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3553 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3554 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3555 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3556 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3557 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3558 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3559 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3560 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3561 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3562 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3563 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3564 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3565 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3566 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3567 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3568 | NewDerivedToBase, NewObjCConversion, |
| 3569 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3570 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3571 | // If the type we've converted to is not reference-related to the |
| 3572 | // type we're looking for, then there is another conversion step |
| 3573 | // we need to perform to produce a temporary of the right type |
| 3574 | // that we'll be binding to. |
| 3575 | ImplicitConversionSequence ICS; |
| 3576 | ICS.setStandard(); |
| 3577 | ICS.Standard = Best->FinalConversion; |
| 3578 | T2 = ICS.Standard.getToType(2); |
| 3579 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3580 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3581 | Sequence.AddDerivedToBaseCastStep( |
| 3582 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3583 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3584 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3585 | else if (NewObjCConversion) |
| 3586 | Sequence.AddObjCObjectConversionStep( |
| 3587 | S.Context.getQualifiedType(T1, |
| 3588 | T2.getNonReferenceType().getQualifiers())); |
| 3589 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3590 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3591 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3592 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3593 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3594 | return OR_Success; |
| 3595 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3596 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3597 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3598 | const InitializedEntity &Entity, |
| 3599 | Expr *CurInitExpr); |
| 3600 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3601 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3602 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3603 | const InitializedEntity &Entity, |
| 3604 | const InitializationKind &Kind, |
| 3605 | Expr *Initializer, |
| 3606 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3607 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3608 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3609 | Qualifiers T1Quals; |
| 3610 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3611 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3612 | Qualifiers T2Quals; |
| 3613 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3614 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3615 | // If the initializer is the address of an overloaded function, try |
| 3616 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3617 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3618 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3619 | T1, Sequence)) |
| 3620 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3621 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3622 | // Delegate everything else to a subfunction. |
| 3623 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3624 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3625 | } |
| 3626 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3627 | /// Converts the target of reference initialization so that it has the |
| 3628 | /// appropriate qualifiers and value kind. |
| 3629 | /// |
| 3630 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3631 | /// \code |
| 3632 | /// int x; |
| 3633 | /// const int &r = x; |
| 3634 | /// \endcode |
| 3635 | /// |
| 3636 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3637 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3638 | /// \code |
| 3639 | /// const int &r = someStruct.bitfield; |
| 3640 | /// \endcode |
| 3641 | static ExprValueKind |
| 3642 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3643 | InitializationSequence &Sequence, |
| 3644 | Expr *Initializer, |
| 3645 | QualType cv1T1, |
| 3646 | Qualifiers T1Quals, |
| 3647 | Qualifiers T2Quals, |
| 3648 | bool IsLValueRef) { |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3649 | bool IsNonAddressableType = Initializer->refersToBitField() || |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3650 | Initializer->refersToVectorElement(); |
| 3651 | |
| 3652 | if (IsNonAddressableType) { |
| 3653 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3654 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3655 | // an rvalue reference. |
| 3656 | // |
| 3657 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3658 | // error to be diagnosed later. |
| 3659 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3660 | assert(Initializer->isGLValue()); |
| 3661 | return Initializer->getValueKind(); |
| 3662 | } |
| 3663 | |
| 3664 | // Force a load so we can materialize a temporary. |
| 3665 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3666 | return VK_RValue; |
| 3667 | } |
| 3668 | |
| 3669 | if (T1Quals != T2Quals) { |
| 3670 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3671 | Initializer->getValueKind()); |
| 3672 | } |
| 3673 | |
| 3674 | return Initializer->getValueKind(); |
| 3675 | } |
| 3676 | |
| 3677 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3678 | /// \brief Reference initialization without resolving overloaded functions. |
| 3679 | static void TryReferenceInitializationCore(Sema &S, |
| 3680 | const InitializedEntity &Entity, |
| 3681 | const InitializationKind &Kind, |
| 3682 | Expr *Initializer, |
| 3683 | QualType cv1T1, QualType T1, |
| 3684 | Qualifiers T1Quals, |
| 3685 | QualType cv2T2, QualType T2, |
| 3686 | Qualifiers T2Quals, |
| 3687 | InitializationSequence &Sequence) { |
| 3688 | QualType DestType = Entity.getType(); |
| 3689 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3690 | // Compute some basic properties of the types and the initializer. |
| 3691 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3692 | bool isRValueRef = !isLValueRef; |
| 3693 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3694 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3695 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3696 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3697 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3698 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3699 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3700 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3701 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3702 | // 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] | 3703 | // "cv2 T2" as follows: |
| 3704 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3705 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3706 | // expression |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3707 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3708 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3709 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3710 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3711 | bool T1Function = T1->isFunctionType(); |
| 3712 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3713 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3714 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3715 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3716 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3717 | // - 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] | 3718 | // reference-compatible with "cv2 T2," or |
| 3719 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3720 | // 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] | 3721 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3722 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3723 | // to decide whether we're actually binding to a temporary created from |
| 3724 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3725 | if (DerivedToBase) |
| 3726 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3727 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3728 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3729 | else if (ObjCConversion) |
| 3730 | Sequence.AddObjCObjectConversionStep( |
| 3731 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3732 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3733 | ExprValueKind ValueKind = |
| 3734 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3735 | cv1T1, T1Quals, T2Quals, |
| 3736 | isLValueRef); |
| 3737 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3738 | return; |
| 3739 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3740 | |
| 3741 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3742 | // reference-related to T2, and can be implicitly converted to an |
| 3743 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3744 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3745 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3746 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3747 | // 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] | 3748 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3749 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3750 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3751 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3752 | S, Entity, Kind, Initializer, /*AllowRValues*/isRValueRef, Sequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3753 | if (ConvOvlResult == OR_Success) |
| 3754 | return; |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3755 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3756 | Sequence.SetOverloadFailure( |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3757 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3758 | ConvOvlResult); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3759 | } |
| 3760 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3761 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3762 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3763 | // 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] | 3764 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3765 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3766 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3767 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3768 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3769 | Sequence.SetOverloadFailure( |
| 3770 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3771 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3772 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3773 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3774 | ? (RefRelationship == Sema::Ref_Related |
| 3775 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3776 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3777 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3778 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3779 | return; |
| 3780 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3781 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3782 | // - If the initializer expression |
| 3783 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3784 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3785 | // Note: functions are handled below. |
| 3786 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3787 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3788 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3789 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3790 | (InitCategory.isXValue() || |
| 3791 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3792 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3793 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3794 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3795 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3796 | // compiler the freedom to perform a copy here or bind to the |
| 3797 | // object, while C++0x requires that we bind directly to the |
| 3798 | // object. Hence, we always bind to the object without making an |
| 3799 | // extra copy. However, in C++03 requires that we check for the |
| 3800 | // presence of a suitable copy constructor: |
| 3801 | // |
| 3802 | // The constructor that would be used to make the copy shall |
| 3803 | // be callable whether or not the copy is actually done. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3804 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3805 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3806 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3807 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3808 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3809 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3810 | if (DerivedToBase) |
| 3811 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3812 | ValueKind); |
| 3813 | else if (ObjCConversion) |
| 3814 | Sequence.AddObjCObjectConversionStep( |
| 3815 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3816 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3817 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3818 | Initializer, cv1T1, |
| 3819 | T1Quals, T2Quals, |
| 3820 | isLValueRef); |
| 3821 | |
| 3822 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3823 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3824 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3825 | |
| 3826 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3827 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3828 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3829 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3830 | // |
| 3831 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3832 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3833 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3834 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3835 | S, Entity, Kind, Initializer, /*AllowRValues*/true, Sequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3836 | if (ConvOvlResult) |
| 3837 | Sequence.SetOverloadFailure( |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3838 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3839 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3840 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3841 | return; |
| 3842 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3843 | |
Douglas Gregor | defa32e | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 3844 | if ((RefRelationship == Sema::Ref_Compatible || |
| 3845 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 3846 | isRValueRef && InitCategory.isLValue()) { |
| 3847 | Sequence.SetFailed( |
| 3848 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3849 | return; |
| 3850 | } |
| 3851 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3852 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3853 | return; |
| 3854 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3855 | |
| 3856 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3857 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3858 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3859 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3860 | |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3861 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3862 | |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3863 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 3864 | // copy-initialization? |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3865 | ImplicitConversionSequence ICS |
| 3866 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3867 | /*SuppressUserConversions=*/false, |
| 3868 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3869 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3870 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3871 | /*AllowObjCWritebackConversion=*/false); |
| 3872 | |
| 3873 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3874 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3875 | // this into an overloading ambiguity diagnostic. However, we need |
| 3876 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3877 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3878 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3879 | Sequence.SetOverloadFailure( |
| 3880 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3881 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3882 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3883 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3884 | else |
| 3885 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3886 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3887 | } else { |
| 3888 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3892 | // same cv-qualification as, or greater cv-qualification |
| 3893 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3894 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3895 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3896 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3897 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3898 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3899 | return; |
| 3900 | } |
| 3901 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3902 | // [...] 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] | 3903 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3904 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3905 | InitCategory.isLValue()) { |
| 3906 | Sequence.SetFailed( |
| 3907 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3908 | return; |
| 3909 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3910 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3911 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3912 | return; |
| 3913 | } |
| 3914 | |
| 3915 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3916 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3917 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3918 | const InitializedEntity &Entity, |
| 3919 | const InitializationKind &Kind, |
| 3920 | Expr *Initializer, |
| 3921 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3922 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3925 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3926 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3927 | const InitializedEntity &Entity, |
| 3928 | const InitializationKind &Kind, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3929 | InitializationSequence &Sequence, |
| 3930 | InitListExpr *InitList) { |
| 3931 | assert((!InitList || InitList->getNumInits() == 0) && |
| 3932 | "Shouldn't use value-init for non-empty init lists"); |
| 3933 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3934 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3935 | // |
| 3936 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3937 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3938 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3939 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3940 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3941 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3942 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3943 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3944 | bool NeedZeroInitialization = true; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3945 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3946 | // C++98: |
| 3947 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 3948 | // (12.1), then the default constructor for T is called (and the |
| 3949 | // initialization is ill-formed if T has no accessible default |
| 3950 | // constructor); |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3951 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3952 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3953 | } else { |
| 3954 | // C++11: |
| 3955 | // -- if T is a class type (clause 9) with either no default constructor |
| 3956 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3957 | // or deleted, then the object is default-initialized; |
| 3958 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3959 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3960 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3961 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3962 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3963 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3964 | // user-provided or deleted default constructor, then the object is |
| 3965 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3966 | // default-initialized; |
Richard Smith | 6678a05 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 3967 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 3968 | // constructor' part was removed by DR1507. |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3969 | if (NeedZeroInitialization) |
| 3970 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3971 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3972 | // C++03: |
| 3973 | // -- if T is a non-union class type without a user-declared constructor, |
| 3974 | // then every non-static data member and base class component of T is |
| 3975 | // value-initialized; |
| 3976 | // [...] A program that calls for [...] value-initialization of an |
| 3977 | // entity of reference type is ill-formed. |
| 3978 | // |
| 3979 | // C++11 doesn't need this handling, because value-initialization does not |
| 3980 | // occur recursively there, and the implicit default constructor is |
| 3981 | // defined as deleted in the problematic cases. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3982 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3983 | ClassDecl->hasUninitializedReferenceMember()) { |
| 3984 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 3985 | return; |
| 3986 | } |
| 3987 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3988 | // If this is list-value-initialization, pass the empty init list on when |
| 3989 | // building the constructor call. This affects the semantics of a few |
| 3990 | // things (such as whether an explicit default constructor can be called). |
| 3991 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3992 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3993 | bool InitListSyntax = InitList; |
| 3994 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3995 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 3996 | InitListSyntax); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3997 | } |
| 3998 | } |
| 3999 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4000 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4003 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 4004 | static void TryDefaultInitialization(Sema &S, |
| 4005 | const InitializedEntity &Entity, |
| 4006 | const InitializationKind &Kind, |
| 4007 | InitializationSequence &Sequence) { |
| 4008 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4009 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4010 | // C++ [dcl.init]p6: |
| 4011 | // To default-initialize an object of type T means: |
| 4012 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4013 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 4014 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4015 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 4016 | // constructor for T is called (and the initialization is ill-formed if |
| 4017 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4018 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 4019 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4020 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4021 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4022 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4023 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4024 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4025 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4026 | // 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] | 4027 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4028 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4029 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4030 | return; |
| 4031 | } |
| 4032 | |
| 4033 | // If the destination type has a lifetime property, zero-initialize it. |
| 4034 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4035 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4036 | return; |
| 4037 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4040 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 4041 | /// which enumerates all conversion functions and performs overload resolution |
| 4042 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4043 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4044 | const InitializedEntity &Entity, |
| 4045 | const InitializationKind &Kind, |
| 4046 | Expr *Initializer, |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4047 | InitializationSequence &Sequence, |
| 4048 | bool TopLevelOfInitList) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4049 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4050 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4051 | QualType SourceType = Initializer->getType(); |
| 4052 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4053 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4054 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4055 | // Build the candidate set directly in the initialization sequence |
| 4056 | // structure, so that it will persist if we fail. |
| 4057 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 4058 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4059 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4060 | // Determine whether we are allowed to call explicit constructors or |
| 4061 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4062 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4063 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4064 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4065 | // The type we're converting to is a class type. Enumerate its constructors |
| 4066 | // to see if there is a suitable conversion. |
| 4067 | CXXRecordDecl *DestRecordDecl |
| 4068 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4069 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4070 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4071 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4072 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4073 | // The container holding the constructors can under certain conditions |
| 4074 | // be changed while iterating. To be safe we copy the lookup results |
| 4075 | // to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4076 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4077 | for (SmallVectorImpl<NamedDecl *>::iterator |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4078 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4079 | Con != ConEnd; ++Con) { |
| 4080 | NamedDecl *D = *Con; |
| 4081 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4082 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4083 | // Find the constructor (which may be a template). |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4084 | CXXConstructorDecl *Constructor = nullptr; |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4085 | FunctionTemplateDecl *ConstructorTmpl |
| 4086 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4087 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4088 | Constructor = cast<CXXConstructorDecl>( |
| 4089 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4090 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4091 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4092 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4093 | if (!Constructor->isInvalidDecl() && |
| 4094 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4095 | if (ConstructorTmpl) |
| 4096 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4097 | /*ExplicitArgs*/ nullptr, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4098 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4099 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4100 | else |
| 4101 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4102 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4103 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4104 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4105 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4106 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4107 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4108 | |
| 4109 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4110 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4111 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4112 | // The type we're converting from is a class type, enumerate its conversion |
| 4113 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4114 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4115 | // We can only enumerate the conversion functions for a complete type; if |
| 4116 | // the type isn't complete, simply skip this step. |
| 4117 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 4118 | CXXRecordDecl *SourceRecordDecl |
| 4119 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4120 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4121 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4122 | CXXRecordDecl::conversion_iterator> |
| 4123 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 4124 | for (CXXRecordDecl::conversion_iterator |
| 4125 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4126 | NamedDecl *D = *I; |
| 4127 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4128 | if (isa<UsingShadowDecl>(D)) |
| 4129 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4130 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4131 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4132 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4133 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4134 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4135 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4136 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4137 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4138 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4139 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4140 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4141 | ActingDC, Initializer, DestType, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4142 | CandidateSet, AllowExplicit); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4143 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4144 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4145 | Initializer, DestType, CandidateSet, |
| 4146 | AllowExplicit); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4147 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4148 | } |
| 4149 | } |
| 4150 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4151 | |
| 4152 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4153 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4154 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4155 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4156 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4157 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4158 | Result); |
| 4159 | return; |
| 4160 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4161 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4162 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4163 | Function->setReferenced(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4164 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4165 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4166 | if (isa<CXXConstructorDecl>(Function)) { |
| 4167 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4168 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4169 | // cv-unqualified type of the destination. |
| 4170 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4171 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4172 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4173 | return; |
| 4174 | } |
| 4175 | |
| 4176 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4177 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4178 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4179 | // 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] | 4180 | // the resulting temporary object (possible to create an object of |
| 4181 | // a base class type). That copy is not a separate conversion, so |
| 4182 | // we just make a note of the actual destination type (possibly a |
| 4183 | // base class of the type returned by the conversion function) and |
| 4184 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4185 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 4186 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4187 | return; |
| 4188 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4189 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4190 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4191 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4192 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4193 | // If the conversion following the call to the conversion function |
| 4194 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4195 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4196 | Best->FinalConversion.Third) { |
| 4197 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4198 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4199 | ICS.Standard = Best->FinalConversion; |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4200 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4201 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4202 | } |
| 4203 | |
Richard Smith | 87c2932 | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4204 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 4205 | /// a function with a pointer return type contains a 'return false;' statement. |
| 4206 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 4207 | /// code using that header. |
| 4208 | /// |
| 4209 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 4210 | /// if it's used in a pointer-returning function in a system header. |
| 4211 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 4212 | const InitializedEntity &Entity, |
| 4213 | const Expr *Init) { |
| 4214 | return S.getLangOpts().CPlusPlus11 && |
| 4215 | Entity.getKind() == InitializedEntity::EK_Result && |
| 4216 | Entity.getType()->isPointerType() && |
| 4217 | isa<CXXBoolLiteralExpr>(Init) && |
| 4218 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 4219 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 4220 | } |
| 4221 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4222 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4223 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4224 | |
| 4225 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4226 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4227 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4228 | // Skip parens. |
| 4229 | e = e->IgnoreParens(); |
| 4230 | |
| 4231 | // Skip address-of nodes. |
| 4232 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4233 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4234 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4235 | isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4236 | |
| 4237 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4238 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4239 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4240 | case CK_Dependent: |
| 4241 | case CK_BitCast: |
| 4242 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4243 | case CK_NoOp: |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4244 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4245 | |
| 4246 | case CK_ArrayToPointerDecay: |
| 4247 | return IIK_nonscalar; |
| 4248 | |
| 4249 | case CK_NullToPointer: |
| 4250 | return IIK_okay; |
| 4251 | |
| 4252 | default: |
| 4253 | break; |
| 4254 | } |
| 4255 | |
| 4256 | // 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] | 4257 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4258 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4259 | // load which requires a cleanup. |
| 4260 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4261 | isWeakAccess = true; |
| 4262 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4263 | if (!isAddressOf) return IIK_nonlocal; |
| 4264 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4265 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4266 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4267 | |
| 4268 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4269 | |
| 4270 | // If we have a conditional operator, check both sides. |
| 4271 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4272 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4273 | isWeakAccess)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4274 | return iik; |
| 4275 | |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4276 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4277 | |
| 4278 | // These are never scalar. |
| 4279 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4280 | return IIK_nonscalar; |
| 4281 | |
| 4282 | // Otherwise, it needs to be a null pointer constant. |
| 4283 | } else { |
| 4284 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4285 | ? IIK_okay : IIK_nonlocal); |
| 4286 | } |
| 4287 | |
| 4288 | return IIK_nonlocal; |
| 4289 | } |
| 4290 | |
| 4291 | /// Check whether the given expression is a valid operand for an |
| 4292 | /// indirect copy/restore. |
| 4293 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4294 | assert(src->isRValue()); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4295 | bool isWeakAccess = false; |
| 4296 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4297 | // If isWeakAccess to true, there will be an implicit |
| 4298 | // load which requires a cleanup. |
| 4299 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4300 | S.ExprNeedsCleanups = true; |
| 4301 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4302 | if (iik == IIK_okay) return; |
| 4303 | |
| 4304 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4305 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4306 | << src->getSourceRange(); |
| 4307 | } |
| 4308 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4309 | /// \brief Determine whether we have compatible array types for the |
| 4310 | /// purposes of GNU by-copy array initialization. |
| 4311 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 4312 | const ArrayType *Dest, |
| 4313 | const ArrayType *Source) { |
| 4314 | // If the source and destination array types are equivalent, we're |
| 4315 | // done. |
| 4316 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4317 | return true; |
| 4318 | |
| 4319 | // Make sure that the element types are the same. |
| 4320 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4321 | return false; |
| 4322 | |
| 4323 | // The only mismatch we allow is when the destination is an |
| 4324 | // incomplete array type and the source is a constant array type. |
| 4325 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4326 | } |
| 4327 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4328 | static bool tryObjCWritebackConversion(Sema &S, |
| 4329 | InitializationSequence &Sequence, |
| 4330 | const InitializedEntity &Entity, |
| 4331 | Expr *Initializer) { |
| 4332 | bool ArrayDecay = false; |
| 4333 | QualType ArgType = Initializer->getType(); |
| 4334 | QualType ArgPointee; |
| 4335 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4336 | ArrayDecay = true; |
| 4337 | ArgPointee = ArgArrayType->getElementType(); |
| 4338 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4339 | } |
| 4340 | |
| 4341 | // Handle write-back conversion. |
| 4342 | QualType ConvertedArgType; |
| 4343 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4344 | ConvertedArgType)) |
| 4345 | return false; |
| 4346 | |
| 4347 | // We should copy unless we're passing to an argument explicitly |
| 4348 | // marked 'out'. |
| 4349 | bool ShouldCopy = true; |
| 4350 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4351 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4352 | |
| 4353 | // Do we need an lvalue conversion? |
| 4354 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4355 | ImplicitConversionSequence ICS; |
| 4356 | ICS.setStandard(); |
| 4357 | ICS.Standard.setAsIdentityConversion(); |
| 4358 | |
| 4359 | QualType ResultType; |
| 4360 | if (ArrayDecay) { |
| 4361 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4362 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4363 | } else { |
| 4364 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4365 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4366 | } |
| 4367 | |
| 4368 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4369 | } |
| 4370 | |
| 4371 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4372 | return true; |
| 4373 | } |
| 4374 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4375 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4376 | InitializationSequence &Sequence, |
| 4377 | QualType DestType, |
| 4378 | Expr *Initializer) { |
| 4379 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4380 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4381 | return false; |
| 4382 | |
| 4383 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4384 | return true; |
| 4385 | } |
| 4386 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4387 | // |
| 4388 | // OpenCL 1.2 spec, s6.12.10 |
| 4389 | // |
| 4390 | // The event argument can also be used to associate the |
| 4391 | // async_work_group_copy with a previous async copy allowing |
| 4392 | // an event to be shared by multiple async copies; otherwise |
| 4393 | // event should be zero. |
| 4394 | // |
| 4395 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4396 | InitializationSequence &Sequence, |
| 4397 | QualType DestType, |
| 4398 | Expr *Initializer) { |
| 4399 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4400 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4401 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4402 | return false; |
| 4403 | |
| 4404 | Sequence.AddOCLZeroEventStep(DestType); |
| 4405 | return true; |
| 4406 | } |
| 4407 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4408 | InitializationSequence::InitializationSequence(Sema &S, |
| 4409 | const InitializedEntity &Entity, |
| 4410 | const InitializationKind &Kind, |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4411 | MultiExprArg Args, |
| 4412 | bool TopLevelOfInitList) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4413 | : FailedCandidateSet(Kind.getLocation(), OverloadCandidateSet::CSK_Normal) { |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4414 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList); |
| 4415 | } |
| 4416 | |
| 4417 | void InitializationSequence::InitializeFrom(Sema &S, |
| 4418 | const InitializedEntity &Entity, |
| 4419 | const InitializationKind &Kind, |
| 4420 | MultiExprArg Args, |
| 4421 | bool TopLevelOfInitList) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4422 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4423 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4424 | // Eliminate non-overload placeholder types in the arguments. We |
| 4425 | // need to do this before checking whether types are dependent |
| 4426 | // because lowering a pseudo-object expression might well give us |
| 4427 | // something of dependent type. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4428 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4429 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4430 | // FIXME: should we be doing this here? |
| 4431 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4432 | if (result.isInvalid()) { |
| 4433 | SetFailed(FK_PlaceholderType); |
| 4434 | return; |
| 4435 | } |
| 4436 | Args[I] = result.take(); |
| 4437 | } |
| 4438 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4439 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4440 | // The semantics of initializers are as follows. The destination type is |
| 4441 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4442 | // 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] | 4443 | // 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] | 4444 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4445 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4446 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4447 | if (DestType->isDependentType() || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4448 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4449 | SequenceKind = DependentSequence; |
| 4450 | return; |
| 4451 | } |
| 4452 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4453 | // Almost everything is a normal sequence. |
| 4454 | setSequenceKind(NormalSequence); |
| 4455 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4456 | QualType SourceType; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4457 | Expr *Initializer = nullptr; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4458 | if (Args.size() == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4459 | Initializer = Args[0]; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 4460 | if (S.getLangOpts().ObjC1) { |
| 4461 | if (S.CheckObjCBridgeRelatedConversions(Initializer->getLocStart(), |
| 4462 | DestType, Initializer->getType(), |
| 4463 | Initializer) || |
| 4464 | S.ConversionToObjCStringLiteralCheck(DestType, Initializer)) |
| 4465 | Args[0] = Initializer; |
| 4466 | |
| 4467 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4468 | if (!isa<InitListExpr>(Initializer)) |
| 4469 | SourceType = Initializer->getType(); |
| 4470 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4471 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4472 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4473 | // object is list-initialized (8.5.4). |
| 4474 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4475 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4476 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4477 | return; |
| 4478 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4479 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4480 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4481 | // - If the destination type is a reference type, see 8.5.3. |
| 4482 | if (DestType->isReferenceType()) { |
| 4483 | // C++0x [dcl.init.ref]p1: |
| 4484 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4485 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4486 | // by an object that can be converted into a T. |
| 4487 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4488 | if (Args.size() != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4489 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4490 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4491 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4492 | return; |
| 4493 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4494 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4495 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4496 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4497 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4498 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4499 | return; |
| 4500 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4501 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4502 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4503 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4504 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4505 | return; |
| 4506 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4507 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4508 | // - If the destination type is an array of characters, an array of |
| 4509 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4510 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4511 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4512 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4513 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4514 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4515 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4516 | return; |
| 4517 | } |
| 4518 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4519 | if (Initializer) { |
| 4520 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 4521 | case SIF_None: |
| 4522 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 4523 | return; |
| 4524 | case SIF_NarrowStringIntoWideChar: |
| 4525 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 4526 | return; |
| 4527 | case SIF_WideStringIntoChar: |
| 4528 | SetFailed(FK_WideStringIntoCharArray); |
| 4529 | return; |
| 4530 | case SIF_IncompatWideStringIntoWideChar: |
| 4531 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 4532 | return; |
| 4533 | case SIF_Other: |
| 4534 | break; |
| 4535 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4538 | // Note: as an GNU C extension, we allow initialization of an |
| 4539 | // array from a compound literal that creates an array of the same |
| 4540 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4541 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4542 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4543 | Initializer->getType()->isArrayType()) { |
| 4544 | const ArrayType *SourceAT |
| 4545 | = Context.getAsArrayType(Initializer->getType()); |
| 4546 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4547 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4548 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4549 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4550 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4551 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4552 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4553 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4554 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4555 | // class member of array type from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4556 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4557 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4558 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4559 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4560 | *this); |
| 4561 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4562 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4563 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4564 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 4565 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4566 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4567 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4568 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4569 | return; |
| 4570 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4571 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4572 | // Determine whether we should consider writeback conversions for |
| 4573 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4574 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4575 | Entity.isParameterKind(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4576 | |
| 4577 | // We're at the end of the line for C: it's either a write-back conversion |
| 4578 | // 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] | 4579 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4580 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4581 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4582 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4583 | return; |
| 4584 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4585 | |
| 4586 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4587 | return; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4588 | |
| 4589 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4590 | return; |
| 4591 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4592 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4593 | AddCAssignmentStep(DestType); |
| 4594 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4595 | return; |
| 4596 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4597 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4598 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4599 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4600 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4601 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4602 | // - If the initialization is direct-initialization, or if it is |
| 4603 | // copy-initialization where the cv-unqualified version of the |
| 4604 | // 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] | 4605 | // class of the destination, constructors are considered. [...] |
| 4606 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4607 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4608 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4609 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4610 | TryConstructorInitialization(S, Entity, Kind, Args, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4611 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4612 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4613 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4614 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4615 | // used) to a derived class thereof are enumerated as described in |
| 4616 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4617 | // (13.3). |
| 4618 | else |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4619 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this, |
| 4620 | TopLevelOfInitList); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4621 | return; |
| 4622 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4623 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4624 | if (Args.size() > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4625 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4626 | return; |
| 4627 | } |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4628 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4629 | |
| 4630 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4631 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4632 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4633 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this, |
| 4634 | TopLevelOfInitList); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4635 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4636 | return; |
| 4637 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4638 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4639 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4640 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4641 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4642 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4643 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4644 | |
| 4645 | ImplicitConversionSequence ICS |
| 4646 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4647 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4648 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4649 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4650 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4651 | allowObjCWritebackConversion); |
| 4652 | |
| 4653 | if (ICS.isStandard() && |
| 4654 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4655 | // Objective-C ARC writeback conversion. |
| 4656 | |
| 4657 | // We should copy unless we're passing to an argument explicitly |
| 4658 | // marked 'out'. |
| 4659 | bool ShouldCopy = true; |
| 4660 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4661 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4662 | |
| 4663 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4664 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4665 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4666 | ImplicitConversionSequence LvalueICS; |
| 4667 | LvalueICS.setStandard(); |
| 4668 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4669 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4670 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4671 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4672 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4673 | |
| 4674 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4675 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4676 | DeclAccessPair dap; |
Richard Smith | 87c2932 | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4677 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 4678 | AddZeroInitializationStep(Entity.getType()); |
| 4679 | } else if (Initializer->getType() == Context.OverloadTy && |
| 4680 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 4681 | false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4682 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4683 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4684 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4685 | } else { |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4686 | AddConversionSequenceStep(ICS, Entity.getType(), TopLevelOfInitList); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4687 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4688 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4689 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4693 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4694 | StepEnd = Steps.end(); |
| 4695 | Step != StepEnd; ++Step) |
| 4696 | Step->Destroy(); |
| 4697 | } |
| 4698 | |
| 4699 | //===----------------------------------------------------------------------===// |
| 4700 | // Perform initialization |
| 4701 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4702 | static Sema::AssignmentAction |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4703 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4704 | switch(Entity.getKind()) { |
| 4705 | case InitializedEntity::EK_Variable: |
| 4706 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4707 | case InitializedEntity::EK_Exception: |
| 4708 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4709 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4710 | return Sema::AA_Initializing; |
| 4711 | |
| 4712 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4713 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4714 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4715 | return Sema::AA_Sending; |
| 4716 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4717 | return Sema::AA_Passing; |
| 4718 | |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4719 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 4720 | if (Entity.getDecl() && |
| 4721 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4722 | return Sema::AA_Sending; |
| 4723 | |
| 4724 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
| 4725 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4726 | case InitializedEntity::EK_Result: |
| 4727 | return Sema::AA_Returning; |
| 4728 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4729 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | f5200d6 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 4730 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4731 | // FIXME: Can we tell apart casting vs. converting? |
| 4732 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4733 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4734 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4735 | case InitializedEntity::EK_ArrayElement: |
| 4736 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4737 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4738 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4739 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4740 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4741 | return Sema::AA_Initializing; |
| 4742 | } |
| 4743 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4744 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4747 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4748 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4749 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4750 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4751 | case InitializedEntity::EK_ArrayElement: |
| 4752 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4753 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4754 | case InitializedEntity::EK_New: |
| 4755 | case InitializedEntity::EK_Variable: |
| 4756 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4757 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4758 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4759 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4760 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4761 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4762 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4763 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4764 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4765 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4766 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4767 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4768 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4769 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4770 | return true; |
| 4771 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4772 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4773 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4774 | } |
| 4775 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4776 | /// \brief Whether the given entity, when initialized with an object |
| 4777 | /// created for that initialization, requires destruction. |
| 4778 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4779 | switch (Entity.getKind()) { |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4780 | case InitializedEntity::EK_Result: |
| 4781 | case InitializedEntity::EK_New: |
| 4782 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4783 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4784 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4785 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4786 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4787 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4788 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4789 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4790 | case InitializedEntity::EK_Member: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4791 | case InitializedEntity::EK_Variable: |
| 4792 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4793 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4794 | case InitializedEntity::EK_Temporary: |
| 4795 | case InitializedEntity::EK_ArrayElement: |
| 4796 | case InitializedEntity::EK_Exception: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4797 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4798 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4799 | return true; |
| 4800 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4801 | |
| 4802 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4803 | } |
| 4804 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4805 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4806 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4807 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4808 | OverloadCandidateSet &CandidateSet, |
| 4809 | CXXRecordDecl *Class, |
| 4810 | Expr *CurInitExpr) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4811 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4812 | // The container holding the constructors can under certain conditions |
| 4813 | // be changed while iterating (e.g. because of deserialization). |
| 4814 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4815 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4816 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4817 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4818 | NamedDecl *D = *CI; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4819 | CXXConstructorDecl *Constructor = nullptr; |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4820 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4821 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4822 | // Handle copy/moveconstructors, only. |
| 4823 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4824 | !Constructor->isCopyOrMoveConstructor() || |
| 4825 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4826 | continue; |
| 4827 | |
| 4828 | DeclAccessPair FoundDecl |
| 4829 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4830 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4831 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4832 | continue; |
| 4833 | } |
| 4834 | |
| 4835 | // Handle constructor templates. |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4836 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4837 | if (ConstructorTmpl->isInvalidDecl()) |
| 4838 | continue; |
| 4839 | |
| 4840 | Constructor = cast<CXXConstructorDecl>( |
| 4841 | ConstructorTmpl->getTemplatedDecl()); |
| 4842 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4843 | continue; |
| 4844 | |
| 4845 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4846 | // candidates? |
| 4847 | DeclAccessPair FoundDecl |
| 4848 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4849 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, nullptr, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4850 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4851 | } |
| 4852 | } |
| 4853 | |
| 4854 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4855 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4856 | Expr *Initializer) { |
| 4857 | switch (Entity.getKind()) { |
| 4858 | case InitializedEntity::EK_Result: |
| 4859 | return Entity.getReturnLoc(); |
| 4860 | |
| 4861 | case InitializedEntity::EK_Exception: |
| 4862 | return Entity.getThrowLoc(); |
| 4863 | |
| 4864 | case InitializedEntity::EK_Variable: |
| 4865 | return Entity.getDecl()->getLocation(); |
| 4866 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4867 | case InitializedEntity::EK_LambdaCapture: |
| 4868 | return Entity.getCaptureLoc(); |
| 4869 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4870 | case InitializedEntity::EK_ArrayElement: |
| 4871 | case InitializedEntity::EK_Member: |
| 4872 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4873 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4874 | case InitializedEntity::EK_Temporary: |
| 4875 | case InitializedEntity::EK_New: |
| 4876 | case InitializedEntity::EK_Base: |
| 4877 | case InitializedEntity::EK_Delegating: |
| 4878 | case InitializedEntity::EK_VectorElement: |
| 4879 | case InitializedEntity::EK_ComplexElement: |
| 4880 | case InitializedEntity::EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4881 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4882 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4883 | return Initializer->getLocStart(); |
| 4884 | } |
| 4885 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4886 | } |
| 4887 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4888 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4889 | /// provided by the given initializer by calling the appropriate copy |
| 4890 | /// constructor. |
| 4891 | /// |
| 4892 | /// \param S The Sema object used for type-checking. |
| 4893 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4894 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4895 | /// the type of the initializer expression or a superclass thereof. |
| 4896 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4897 | /// \param Entity The entity being initialized. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4898 | /// |
| 4899 | /// \param CurInit The initializer expression. |
| 4900 | /// |
| 4901 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4902 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4903 | /// an rvalue. |
| 4904 | /// |
| 4905 | /// \returns An expression that copies the initializer expression into |
| 4906 | /// a temporary object, or an error expression if a copy could not be |
| 4907 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4908 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4909 | QualType T, |
| 4910 | const InitializedEntity &Entity, |
| 4911 | ExprResult CurInit, |
| 4912 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4913 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4914 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4915 | CXXRecordDecl *Class = nullptr; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4916 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4917 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4918 | if (!Class) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4919 | return CurInit; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4920 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4921 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4922 | // When certain criteria are met, an implementation is allowed to |
| 4923 | // omit the copy/move construction of a class object, even if the |
| 4924 | // copy/move constructor and/or destructor for the object have |
| 4925 | // side effects. [...] |
| 4926 | // - when a temporary class object that has not been bound to a |
| 4927 | // reference (12.2) would be copied/moved to a class object |
| 4928 | // with the same cv-unqualified type, the copy/move operation |
| 4929 | // can be omitted by constructing the temporary object |
| 4930 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4931 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4932 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4933 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4934 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4935 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4936 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4937 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4938 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4939 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4940 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4941 | return CurInit; |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4942 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4943 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4944 | // Only consider constructors and constructor templates. Per |
| 4945 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4946 | // is direct-initialization. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 4947 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4948 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4949 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4950 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4951 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4952 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4953 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4954 | case OR_Success: |
| 4955 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4956 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4957 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4958 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4959 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4960 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4961 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4962 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4963 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4964 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4965 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4966 | return CurInit; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4967 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4968 | case OR_Ambiguous: |
| 4969 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4970 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4971 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4972 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4973 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4974 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4975 | case OR_Deleted: |
| 4976 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4977 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4978 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4979 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4980 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4983 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4984 | SmallVector<Expr*, 8> ConstructorArgs; |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4985 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4986 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4987 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4988 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4989 | |
| 4990 | if (IsExtraneousCopy) { |
| 4991 | // If this is a totally extraneous copy for C++03 reference |
| 4992 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4993 | // expression. We don't generate an (elided) copy operation here |
| 4994 | // because doing so would require us to pass down a flag to avoid |
| 4995 | // infinite recursion, where each step adds another extraneous, |
| 4996 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4997 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4998 | // Instantiate the default arguments of any extra parameters in |
| 4999 | // the selected copy constructor, as if we were going to create a |
| 5000 | // proper call to the copy constructor. |
| 5001 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 5002 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 5003 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 5004 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 5005 | break; |
| 5006 | |
| 5007 | // Build the default argument expression; we don't actually care |
| 5008 | // if this succeeds or not, because this routine will complain |
| 5009 | // if there was a problem. |
| 5010 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 5011 | } |
| 5012 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5013 | return S.Owned(CurInitExpr); |
| 5014 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5015 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5016 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5017 | // constructor call (we might have derived-to-base conversions, or |
| 5018 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5019 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5020 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 5021 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5022 | // Actually perform the constructor call. |
| 5023 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5024 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5025 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5026 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5027 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5028 | CXXConstructExpr::CK_Complete, |
| 5029 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5030 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5031 | // If we're supposed to bind temporaries, do so. |
| 5032 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 5033 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5034 | return CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5035 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5036 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5037 | /// \brief Check whether elidable copy construction for binding a reference to |
| 5038 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 5039 | /// -Wc++98-compat. |
| 5040 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 5041 | const InitializedEntity &Entity, |
| 5042 | Expr *CurInitExpr) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5043 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5044 | |
| 5045 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 5046 | if (!Record) |
| 5047 | return; |
| 5048 | |
| 5049 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 5050 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 5051 | == DiagnosticsEngine::Ignored) |
| 5052 | return; |
| 5053 | |
| 5054 | // Find constructors which would have been considered. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5055 | OverloadCandidateSet CandidateSet(Loc, OverloadCandidateSet::CSK_Normal); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5056 | LookupCopyAndMoveConstructors( |
| 5057 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 5058 | |
| 5059 | // Perform overload resolution. |
| 5060 | OverloadCandidateSet::iterator Best; |
| 5061 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 5062 | |
| 5063 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 5064 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 5065 | << CurInitExpr->getSourceRange(); |
| 5066 | |
| 5067 | switch (OR) { |
| 5068 | case OR_Success: |
| 5069 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 5070 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5071 | // FIXME: Check default arguments as far as that's possible. |
| 5072 | break; |
| 5073 | |
| 5074 | case OR_No_Viable_Function: |
| 5075 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5076 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5077 | break; |
| 5078 | |
| 5079 | case OR_Ambiguous: |
| 5080 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5081 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5082 | break; |
| 5083 | |
| 5084 | case OR_Deleted: |
| 5085 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5086 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5087 | break; |
| 5088 | } |
| 5089 | } |
| 5090 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5091 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 5092 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5093 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5094 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 5095 | return; |
| 5096 | |
| 5097 | if (Entity.getDecl()->getDeclName()) |
| 5098 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 5099 | << Entity.getDecl()->getDeclName(); |
| 5100 | else |
| 5101 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 5102 | } |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5103 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 5104 | Entity.getMethodDecl()) |
| 5105 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 5106 | diag::note_method_return_type_change) |
| 5107 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5108 | } |
| 5109 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5110 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 5111 | return s.Kind == InitializationSequence::SK_BindReference || |
| 5112 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 5113 | } |
| 5114 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5115 | /// Returns true if the parameters describe a constructor initialization of |
| 5116 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 5117 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 5118 | const InitializationKind &Kind, |
| 5119 | unsigned NumArgs) { |
| 5120 | switch (Entity.getKind()) { |
| 5121 | case InitializedEntity::EK_Temporary: |
| 5122 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5123 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5124 | break; |
| 5125 | default: |
| 5126 | return false; |
| 5127 | } |
| 5128 | |
| 5129 | switch (Kind.getKind()) { |
| 5130 | case InitializationKind::IK_DirectList: |
| 5131 | return true; |
| 5132 | // FIXME: Hack to work around cast weirdness. |
| 5133 | case InitializationKind::IK_Direct: |
| 5134 | case InitializationKind::IK_Value: |
| 5135 | return NumArgs != 1; |
| 5136 | default: |
| 5137 | return false; |
| 5138 | } |
| 5139 | } |
| 5140 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5141 | static ExprResult |
| 5142 | PerformConstructorInitialization(Sema &S, |
| 5143 | const InitializedEntity &Entity, |
| 5144 | const InitializationKind &Kind, |
| 5145 | MultiExprArg Args, |
| 5146 | const InitializationSequence::Step& Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5147 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5148 | bool IsListInitialization, |
| 5149 | SourceLocation LBraceLoc, |
| 5150 | SourceLocation RBraceLoc) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5151 | unsigned NumArgs = Args.size(); |
| 5152 | CXXConstructorDecl *Constructor |
| 5153 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 5154 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 5155 | |
| 5156 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5157 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5158 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 5159 | ? Kind.getEqualLoc() |
| 5160 | : Kind.getLocation(); |
| 5161 | |
| 5162 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 5163 | // Force even a trivial, implicit default constructor to be |
| 5164 | // semantically checked. We do this explicitly because we don't build |
| 5165 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 5166 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5167 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 5168 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5169 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 5170 | } |
| 5171 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5172 | ExprResult CurInit = S.Owned((Expr *)nullptr); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5173 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5174 | // C++ [over.match.copy]p1: |
| 5175 | // - When initializing a temporary to be bound to the first parameter |
| 5176 | // of a constructor that takes a reference to possibly cv-qualified |
| 5177 | // T as its first argument, called with a single argument in the |
| 5178 | // context of direct-initialization, explicit conversion functions |
| 5179 | // are also considered. |
| 5180 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 5181 | Args.size() == 1 && |
| 5182 | Constructor->isCopyOrMoveConstructor(); |
| 5183 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5184 | // Determine the arguments required to actually perform the constructor |
| 5185 | // call. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5186 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5187 | Loc, ConstructorArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5188 | AllowExplicitConv, |
| 5189 | IsListInitialization)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5190 | return ExprError(); |
| 5191 | |
| 5192 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5193 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5194 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5195 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5196 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 5197 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5198 | |
| 5199 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5200 | if (!TSInfo) |
| 5201 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5202 | SourceRange ParenOrBraceRange = |
| 5203 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 5204 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 5205 | : Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5206 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5207 | CurInit = S.Owned( |
| 5208 | new (S.Context) CXXTemporaryObjectExpr(S.Context, Constructor, |
| 5209 | TSInfo, ConstructorArgs, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5210 | ParenOrBraceRange, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5211 | HadMultipleCandidates, |
Enea Zaffanella | 14dcaa9 | 2013-09-07 11:22:02 +0000 | [diff] [blame] | 5212 | IsListInitialization, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5213 | ConstructorInitRequiresZeroInit)); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5214 | } else { |
| 5215 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 5216 | CXXConstructExpr::CK_Complete; |
| 5217 | |
| 5218 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5219 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 5220 | CXXConstructExpr::CK_VirtualBase : |
| 5221 | CXXConstructExpr::CK_NonVirtualBase; |
| 5222 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 5223 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 5224 | } |
| 5225 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5226 | // Only get the parenthesis or brace range if it is a list initialization or |
| 5227 | // direct construction. |
| 5228 | SourceRange ParenOrBraceRange; |
| 5229 | if (IsListInitialization) |
| 5230 | ParenOrBraceRange = SourceRange(LBraceLoc, RBraceLoc); |
| 5231 | else if (Kind.getKind() == InitializationKind::IK_Direct) |
| 5232 | ParenOrBraceRange = Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5233 | |
| 5234 | // If the entity allows NRVO, mark the construction as elidable |
| 5235 | // unconditionally. |
| 5236 | if (Entity.allowsNRVO()) |
| 5237 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5238 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5239 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5240 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5241 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5242 | ConstructorInitRequiresZeroInit, |
| 5243 | ConstructKind, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5244 | ParenOrBraceRange); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5245 | else |
| 5246 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5247 | Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5248 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5249 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5250 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5251 | ConstructorInitRequiresZeroInit, |
| 5252 | ConstructKind, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5253 | ParenOrBraceRange); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5254 | } |
| 5255 | if (CurInit.isInvalid()) |
| 5256 | return ExprError(); |
| 5257 | |
| 5258 | // Only check access if all of that succeeded. |
| 5259 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 5260 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5261 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 5262 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5263 | |
| 5264 | if (shouldBindAsTemporary(Entity)) |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5265 | CurInit = S.MaybeBindToTemporary(CurInit.take()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5266 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5267 | return CurInit; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5270 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 5271 | /// longer than the current full-expression. Conservatively returns false if |
| 5272 | /// it's unclear. |
| 5273 | static bool |
| 5274 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 5275 | const InitializedEntity *Top = &Entity; |
| 5276 | while (Top->getParent()) |
| 5277 | Top = Top->getParent(); |
| 5278 | |
| 5279 | switch (Top->getKind()) { |
| 5280 | case InitializedEntity::EK_Variable: |
| 5281 | case InitializedEntity::EK_Result: |
| 5282 | case InitializedEntity::EK_Exception: |
| 5283 | case InitializedEntity::EK_Member: |
| 5284 | case InitializedEntity::EK_New: |
| 5285 | case InitializedEntity::EK_Base: |
| 5286 | case InitializedEntity::EK_Delegating: |
| 5287 | return true; |
| 5288 | |
| 5289 | case InitializedEntity::EK_ArrayElement: |
| 5290 | case InitializedEntity::EK_VectorElement: |
| 5291 | case InitializedEntity::EK_BlockElement: |
| 5292 | case InitializedEntity::EK_ComplexElement: |
| 5293 | // Could not determine what the full initialization is. Assume it might not |
| 5294 | // outlive the full-expression. |
| 5295 | return false; |
| 5296 | |
| 5297 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5298 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5299 | case InitializedEntity::EK_Temporary: |
| 5300 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5301 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5302 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5303 | // The entity being initialized might not outlive the full-expression. |
| 5304 | return false; |
| 5305 | } |
| 5306 | |
| 5307 | llvm_unreachable("unknown entity kind"); |
| 5308 | } |
| 5309 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5310 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 5311 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 5312 | /// the initialization of \p Entity. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5313 | static const InitializedEntity *getEntityForTemporaryLifetimeExtension( |
| 5314 | const InitializedEntity *Entity, |
| 5315 | const InitializedEntity *FallbackDecl = nullptr) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5316 | // C++11 [class.temporary]p5: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5317 | switch (Entity->getKind()) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5318 | case InitializedEntity::EK_Variable: |
| 5319 | // The temporary [...] persists for the lifetime of the reference |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5320 | return Entity; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5321 | |
| 5322 | case InitializedEntity::EK_Member: |
| 5323 | // For subobjects, we look at the complete object. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5324 | if (Entity->getParent()) |
| 5325 | return getEntityForTemporaryLifetimeExtension(Entity->getParent(), |
| 5326 | Entity); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5327 | |
| 5328 | // except: |
| 5329 | // -- A temporary bound to a reference member in a constructor's |
| 5330 | // ctor-initializer persists until the constructor exits. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5331 | return Entity; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5332 | |
| 5333 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5334 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5335 | // -- A temporary bound to a reference parameter in a function call |
| 5336 | // persists until the completion of the full-expression containing |
| 5337 | // the call. |
| 5338 | case InitializedEntity::EK_Result: |
| 5339 | // -- The lifetime of a temporary bound to the returned value in a |
| 5340 | // function return statement is not extended; the temporary is |
| 5341 | // destroyed at the end of the full-expression in the return statement. |
| 5342 | case InitializedEntity::EK_New: |
| 5343 | // -- A temporary bound to a reference in a new-initializer persists |
| 5344 | // until the completion of the full-expression containing the |
| 5345 | // new-initializer. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5346 | return nullptr; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5347 | |
| 5348 | case InitializedEntity::EK_Temporary: |
| 5349 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5350 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5351 | // We don't yet know the storage duration of the surrounding temporary. |
| 5352 | // Assume it's got full-expression duration for now, it will patch up our |
| 5353 | // storage duration if that's not correct. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5354 | return nullptr; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5355 | |
| 5356 | case InitializedEntity::EK_ArrayElement: |
| 5357 | // For subobjects, we look at the complete object. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5358 | return getEntityForTemporaryLifetimeExtension(Entity->getParent(), |
| 5359 | FallbackDecl); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5360 | |
| 5361 | case InitializedEntity::EK_Base: |
| 5362 | case InitializedEntity::EK_Delegating: |
| 5363 | // We can reach this case for aggregate initialization in a constructor: |
| 5364 | // struct A { int &&r; }; |
| 5365 | // struct B : A { B() : A{0} {} }; |
| 5366 | // In this case, use the innermost field decl as the context. |
| 5367 | return FallbackDecl; |
| 5368 | |
| 5369 | case InitializedEntity::EK_BlockElement: |
| 5370 | case InitializedEntity::EK_LambdaCapture: |
| 5371 | case InitializedEntity::EK_Exception: |
| 5372 | case InitializedEntity::EK_VectorElement: |
| 5373 | case InitializedEntity::EK_ComplexElement: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5374 | return nullptr; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5375 | } |
Benjamin Kramer | 6f773e8 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 5376 | llvm_unreachable("unknown entity kind"); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5377 | } |
| 5378 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5379 | static void performLifetimeExtension(Expr *Init, |
| 5380 | const InitializedEntity *ExtendingEntity); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 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. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5385 | static bool |
| 5386 | performReferenceExtension(Expr *Init, |
| 5387 | const InitializedEntity *ExtendingEntity) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5388 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 5389 | if (ILE->getNumInits() == 1 && ILE->isGLValue()) { |
| 5390 | // This is just redundant braces around an initializer. Step over it. |
| 5391 | Init = ILE->getInit(0); |
| 5392 | } |
| 5393 | } |
| 5394 | |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5395 | // Walk past any constructs which we can lifetime-extend across. |
| 5396 | Expr *Old; |
| 5397 | do { |
| 5398 | Old = Init; |
| 5399 | |
| 5400 | // Step over any subobject adjustments; we may have a materialized |
| 5401 | // temporary inside them. |
| 5402 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5403 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5404 | Init = const_cast<Expr *>( |
| 5405 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5406 | |
| 5407 | // Per current approach for DR1376, look through casts to reference type |
| 5408 | // when performing lifetime extension. |
| 5409 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 5410 | if (CE->getSubExpr()->isGLValue()) |
| 5411 | Init = CE->getSubExpr(); |
| 5412 | |
| 5413 | // FIXME: Per DR1213, subscripting on an array temporary produces an xvalue. |
| 5414 | // It's unclear if binding a reference to that xvalue extends the array |
| 5415 | // temporary. |
| 5416 | } while (Init != Old); |
| 5417 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5418 | if (MaterializeTemporaryExpr *ME = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 5419 | // Update the storage duration of the materialized temporary. |
| 5420 | // FIXME: Rebuild the expression instead of mutating it. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5421 | ME->setExtendingDecl(ExtendingEntity->getDecl(), |
| 5422 | ExtendingEntity->allocateManglingNumber()); |
| 5423 | performLifetimeExtension(ME->GetTemporaryExpr(), ExtendingEntity); |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5424 | return true; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5425 | } |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5426 | |
| 5427 | return false; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | /// Update a prvalue expression that is going to be materialized as a |
| 5431 | /// lifetime-extended temporary. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5432 | static void performLifetimeExtension(Expr *Init, |
| 5433 | const InitializedEntity *ExtendingEntity) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5434 | // Dig out the expression which constructs the extended temporary. |
| 5435 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5436 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5437 | Init = const_cast<Expr *>( |
| 5438 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5439 | |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5440 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 5441 | Init = BTE->getSubExpr(); |
| 5442 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5443 | if (CXXStdInitializerListExpr *ILE = |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5444 | dyn_cast<CXXStdInitializerListExpr>(Init)) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5445 | performReferenceExtension(ILE->getSubExpr(), ExtendingEntity); |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5446 | return; |
| 5447 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5448 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5449 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5450 | if (ILE->getType()->isArrayType()) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5451 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5452 | performLifetimeExtension(ILE->getInit(I), ExtendingEntity); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5453 | return; |
| 5454 | } |
| 5455 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5456 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5457 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 5458 | |
| 5459 | // If we lifetime-extend a braced initializer which is initializing an |
| 5460 | // aggregate, and that aggregate contains reference members which are |
| 5461 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 5462 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 5463 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5464 | performReferenceExtension(ILE->getInit(0), ExtendingEntity); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5465 | else { |
| 5466 | unsigned Index = 0; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5467 | for (const auto *I : RD->fields()) { |
Richard Smith | 3c3af14 | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 5468 | if (Index >= ILE->getNumInits()) |
| 5469 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5470 | if (I->isUnnamedBitfield()) |
| 5471 | continue; |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5472 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5473 | if (I->getType()->isReferenceType()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5474 | performReferenceExtension(SubInit, ExtendingEntity); |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5475 | else if (isa<InitListExpr>(SubInit) || |
| 5476 | isa<CXXStdInitializerListExpr>(SubInit)) |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5477 | // This may be either aggregate-initialization of a member or |
| 5478 | // initialization of a std::initializer_list object. Either way, |
| 5479 | // we should recursively lifetime-extend that initializer. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5480 | performLifetimeExtension(SubInit, ExtendingEntity); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5481 | ++Index; |
| 5482 | } |
| 5483 | } |
| 5484 | } |
| 5485 | } |
| 5486 | } |
| 5487 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5488 | static void warnOnLifetimeExtension(Sema &S, const InitializedEntity &Entity, |
| 5489 | const Expr *Init, bool IsInitializerList, |
| 5490 | const ValueDecl *ExtendingDecl) { |
| 5491 | // Warn if a field lifetime-extends a temporary. |
| 5492 | if (isa<FieldDecl>(ExtendingDecl)) { |
| 5493 | if (IsInitializerList) { |
| 5494 | S.Diag(Init->getExprLoc(), diag::warn_dangling_std_initializer_list) |
| 5495 | << /*at end of constructor*/true; |
| 5496 | return; |
| 5497 | } |
| 5498 | |
| 5499 | bool IsSubobjectMember = false; |
| 5500 | for (const InitializedEntity *Ent = Entity.getParent(); Ent; |
| 5501 | Ent = Ent->getParent()) { |
| 5502 | if (Ent->getKind() != InitializedEntity::EK_Base) { |
| 5503 | IsSubobjectMember = true; |
| 5504 | break; |
| 5505 | } |
| 5506 | } |
| 5507 | S.Diag(Init->getExprLoc(), |
| 5508 | diag::warn_bind_ref_member_to_temporary) |
| 5509 | << ExtendingDecl << Init->getSourceRange() |
| 5510 | << IsSubobjectMember << IsInitializerList; |
| 5511 | if (IsSubobjectMember) |
| 5512 | S.Diag(ExtendingDecl->getLocation(), |
| 5513 | diag::note_ref_subobject_of_member_declared_here); |
| 5514 | else |
| 5515 | S.Diag(ExtendingDecl->getLocation(), |
| 5516 | diag::note_ref_or_ptr_member_declared_here) |
| 5517 | << /*is pointer*/false; |
| 5518 | } |
| 5519 | } |
| 5520 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5521 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 5522 | const ImplicitConversionSequence &ICS, |
| 5523 | QualType PreNarrowingType, |
| 5524 | QualType EntityType, |
| 5525 | const Expr *PostInit); |
| 5526 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5527 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5528 | InitializationSequence::Perform(Sema &S, |
| 5529 | const InitializedEntity &Entity, |
| 5530 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5531 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5532 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5533 | if (Failed()) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5534 | Diagnose(S, Entity, Kind, Args); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5535 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5536 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5537 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5538 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5539 | // If the declaration is a non-dependent, incomplete array type |
| 5540 | // that has an initializer, then its type will be completed once |
| 5541 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5542 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5543 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5544 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5545 | if (const IncompleteArrayType *ArrayT |
| 5546 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5547 | // FIXME: We don't currently have the ability to accurately |
| 5548 | // compute the length of an initializer list without |
| 5549 | // performing full type-checking of the initializer list |
| 5550 | // (since we have to determine where braces are implicitly |
| 5551 | // introduced and such). So, we fall back to making the array |
| 5552 | // type a dependently-sized array type with no specified |
| 5553 | // bound. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5554 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5555 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5556 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5557 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5558 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5559 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5560 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5561 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5562 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5563 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5564 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5565 | } |
| 5566 | |
| 5567 | *ResultType |
| 5568 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5569 | /*NumElts=*/nullptr, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5570 | ArrayT->getSizeModifier(), |
| 5571 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5572 | Brackets); |
| 5573 | } |
| 5574 | |
| 5575 | } |
| 5576 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5577 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5578 | !Kind.isExplicitCast()) { |
| 5579 | // Rebuild the ParenListExpr. |
| 5580 | SourceRange ParenRange = Kind.getParenRange(); |
| 5581 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5582 | Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5583 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5584 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5585 | Kind.isExplicitCast() || |
| 5586 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5587 | return ExprResult(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5588 | } |
| 5589 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5590 | // No steps means no initialization. |
| 5591 | if (Steps.empty()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5592 | return S.Owned((Expr *)nullptr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5593 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5594 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5595 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5596 | !Entity.isParameterKind()) { |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5597 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5598 | // from an initializer list. For parameters, we produce a better warning |
| 5599 | // elsewhere. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5600 | Expr *Init = Args[0]; |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5601 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5602 | << Init->getSourceRange(); |
| 5603 | } |
| 5604 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5605 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5606 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5607 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5608 | Entity.getType()->isPointerType() && |
| 5609 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5610 | Expr *Init = Args[0]; |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5611 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5612 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5613 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5614 | << Init->getSourceRange(); |
| 5615 | } |
| 5616 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5617 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5618 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5619 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5620 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5621 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5622 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5623 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5624 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5625 | ExprResult CurInit = S.Owned((Expr *)nullptr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5626 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5627 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5628 | // grab the only argument out the Args and place it into the "current" |
| 5629 | // initializer. |
| 5630 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5631 | case SK_ResolveAddressOfOverloadedFunction: |
| 5632 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5633 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5634 | case SK_CastDerivedToBaseLValue: |
| 5635 | case SK_BindReference: |
| 5636 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5637 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5638 | case SK_UserConversion: |
| 5639 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5640 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5641 | case SK_QualificationConversionRValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5642 | case SK_LValueToRValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5643 | case SK_ConversionSequence: |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5644 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5645 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5646 | case SK_UnwrapInitList: |
| 5647 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5648 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5649 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5650 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5651 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5652 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5653 | case SK_PassByIndirectCopyRestore: |
| 5654 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5655 | case SK_ProduceObjCObject: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5656 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5657 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5658 | case SK_OCLZeroEvent: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5659 | assert(Args.size() == 1); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5660 | CurInit = Args[0]; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5661 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5662 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5663 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5664 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5665 | case SK_ConstructorInitialization: |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5666 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5667 | case SK_ZeroInitialization: |
| 5668 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5669 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5670 | |
| 5671 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5672 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5673 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5674 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5675 | Step != StepEnd; ++Step) { |
| 5676 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5677 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5678 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5679 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5680 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5681 | switch (Step->Kind) { |
| 5682 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5683 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5684 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5685 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5686 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5687 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5688 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5689 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5690 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5691 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5692 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5693 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5694 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5695 | case SK_CastDerivedToBaseLValue: { |
| 5696 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5697 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5698 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5699 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5700 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5701 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5702 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5703 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5704 | CurInit.get()->getLocStart(), |
| 5705 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5706 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5707 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5708 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5709 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5710 | QualType T = SourceType; |
| 5711 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5712 | T = Pointer->getPointeeType(); |
| 5713 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5714 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5715 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5716 | } |
| 5717 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5718 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5719 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5720 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5721 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5722 | VK_XValue : |
| 5723 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5724 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 5725 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5726 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5727 | CurInit.get(), |
| 5728 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5729 | break; |
| 5730 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5731 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5732 | case SK_BindReference: |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5733 | // References cannot bind to bit-fields (C++ [dcl.init.ref]p5). |
| 5734 | if (CurInit.get()->refersToBitField()) { |
| 5735 | // We don't necessarily have an unambiguous source bit-field. |
| 5736 | FieldDecl *BitField = CurInit.get()->getSourceBitField(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5737 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5738 | << Entity.getType().isVolatileQualified() |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5739 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5740 | << (BitField != nullptr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5741 | << CurInit.get()->getSourceRange(); |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5742 | if (BitField) |
| 5743 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 5744 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5745 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5746 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5747 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5748 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5749 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5750 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5751 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5752 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5753 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5754 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5755 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5756 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5757 | // Reference binding does not have any corresponding ASTs. |
| 5758 | |
| 5759 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5760 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5761 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5762 | |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5763 | // Even though we didn't materialize a temporary, the binding may still |
| 5764 | // extend the lifetime of a temporary. This happens if we bind a reference |
| 5765 | // to the result of a cast to reference type. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5766 | if (const InitializedEntity *ExtendingEntity = |
| 5767 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 5768 | if (performReferenceExtension(CurInit.get(), ExtendingEntity)) |
| 5769 | warnOnLifetimeExtension(S, Entity, CurInit.get(), |
| 5770 | /*IsInitializerList=*/false, |
| 5771 | ExtendingEntity->getDecl()); |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5772 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5773 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5774 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5775 | case SK_BindReferenceToTemporary: { |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5776 | // Make sure the "temporary" is actually an rvalue. |
| 5777 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5778 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5779 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5780 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5781 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5782 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5783 | // Materialize the temporary into memory. |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5784 | MaterializeTemporaryExpr *MTE = new (S.Context) MaterializeTemporaryExpr( |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5785 | Entity.getType().getNonReferenceType(), CurInit.get(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5786 | Entity.getType()->isLValueReferenceType()); |
| 5787 | |
| 5788 | // Maybe lifetime-extend the temporary's subobjects to match the |
| 5789 | // entity's lifetime. |
| 5790 | if (const InitializedEntity *ExtendingEntity = |
| 5791 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 5792 | if (performReferenceExtension(MTE, ExtendingEntity)) |
| 5793 | warnOnLifetimeExtension(S, Entity, CurInit.get(), /*IsInitializerList=*/false, |
| 5794 | ExtendingEntity->getDecl()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5795 | |
| 5796 | // 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] | 5797 | // need cleanups. Likewise if we're extending this temporary to automatic |
| 5798 | // storage duration -- we need to register its cleanup during the |
| 5799 | // full-expression's cleanups. |
| 5800 | if ((S.getLangOpts().ObjCAutoRefCount && |
| 5801 | MTE->getType()->isObjCLifetimeType()) || |
| 5802 | (MTE->getStorageDuration() == SD_Automatic && |
| 5803 | MTE->getType().isDestructedType())) |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5804 | S.ExprNeedsCleanups = true; |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5805 | |
| 5806 | CurInit = S.Owned(MTE); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5807 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5808 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5809 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5810 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5811 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5812 | /*IsExtraneousCopy=*/true); |
| 5813 | break; |
| 5814 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5815 | case SK_UserConversion: { |
| 5816 | // We have a user-defined conversion that invokes either a constructor |
| 5817 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5818 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5819 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5820 | FunctionDecl *Fn = Step->Function.Function; |
| 5821 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5822 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5823 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5824 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5825 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5826 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5827 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5828 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5829 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5830 | // Determine the arguments required to actually perform the constructor |
| 5831 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5832 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5833 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5834 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5835 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5836 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5837 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5838 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5839 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5840 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5841 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5842 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5843 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5844 | CXXConstructExpr::CK_Complete, |
| 5845 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5846 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5847 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5848 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5849 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5850 | FoundFn.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5851 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5852 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5853 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5854 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5855 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5856 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5857 | S.IsDerivedFrom(SourceType, Class)) |
| 5858 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5859 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5860 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5861 | } else { |
| 5862 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5863 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5864 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), nullptr, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5865 | FoundFn); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5866 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5867 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5868 | |
| 5869 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5870 | // derived-to-base conversion? I believe the answer is "no", because |
| 5871 | // 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] | 5872 | ExprResult CurInitExprRes = |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5873 | S.PerformObjectArgumentInitialization(CurInit.take(), |
| 5874 | /*Qualifier=*/nullptr, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5875 | FoundFn, Conversion); |
| 5876 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5877 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5878 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5879 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5880 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5881 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5882 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5883 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5884 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5885 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5886 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5887 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 5888 | CreatedObject = Conversion->getReturnType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5889 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5890 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5891 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5892 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5893 | |
| 5894 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5895 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5896 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5897 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5898 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5899 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5900 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5901 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5902 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 5903 | return ExprError(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5904 | } |
| 5905 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5906 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5907 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5908 | CurInit.get()->getType(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5909 | CastKind, CurInit.get(), |
| 5910 | nullptr, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5911 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5912 | if (MaybeBindToTemp) |
| 5913 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5914 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5915 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5916 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5917 | break; |
| 5918 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5919 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5920 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5921 | case SK_QualificationConversionXValue: |
| 5922 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5923 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5924 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5925 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5926 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5927 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5928 | VK_XValue : |
| 5929 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5930 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5931 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5932 | } |
| 5933 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5934 | case SK_LValueToRValue: { |
| 5935 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
| 5936 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 5937 | CK_LValueToRValue, |
| 5938 | CurInit.take(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 5939 | /*BasePath=*/nullptr, |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5940 | VK_RValue)); |
| 5941 | break; |
| 5942 | } |
| 5943 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5944 | case SK_ConversionSequence: |
| 5945 | case SK_ConversionSequenceNoNarrowing: { |
| 5946 | Sema::CheckedConversionKind CCK |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5947 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5948 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5949 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5950 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5951 | ExprResult CurInitExprRes = |
| 5952 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5953 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5954 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5955 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5956 | CurInit = CurInitExprRes; |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5957 | |
| 5958 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
| 5959 | S.getLangOpts().CPlusPlus && !CurInit.get()->isValueDependent()) |
| 5960 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 5961 | CurInit.get()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5962 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5963 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5964 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5965 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5966 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5967 | // If we're not initializing the top-level entity, we need to create an |
| 5968 | // InitializeTemporary entity for our target type. |
| 5969 | QualType Ty = Step->Type; |
| 5970 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5971 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5972 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 5973 | InitListChecker PerformInitList(S, InitEntity, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 5974 | InitList, Ty, /*VerifyOnly=*/false); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5975 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5976 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5977 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5978 | // Hack: We must update *ResultType if available in order to set the |
| 5979 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5980 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5981 | if (ResultType && |
| 5982 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5983 | if ((*ResultType)->isRValueReferenceType()) |
| 5984 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5985 | else if ((*ResultType)->isLValueReferenceType()) |
| 5986 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5987 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5988 | *ResultType = Ty; |
| 5989 | } |
| 5990 | |
| 5991 | InitListExpr *StructuredInitList = |
| 5992 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5993 | CurInit.release(); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5994 | CurInit = shouldBindAsTemporary(InitEntity) |
| 5995 | ? S.MaybeBindToTemporary(StructuredInitList) |
| 5996 | : S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5997 | break; |
| 5998 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5999 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6000 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6001 | // When an initializer list is passed for a parameter of type "reference |
| 6002 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6003 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6004 | // FIXME: This is a hack. What we really should do is create a user |
| 6005 | // conversion step for this case, but this makes it considerably more |
| 6006 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6007 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6008 | Entity.getType().getNonReferenceType()); |
| 6009 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6010 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6011 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 6012 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 6013 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6014 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6015 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 6016 | Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6017 | Kind, Arg, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6018 | ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6019 | /*IsListInitialization*/ true, |
| 6020 | InitList->getLBraceLoc(), |
| 6021 | InitList->getRBraceLoc()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 6022 | break; |
| 6023 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6024 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6025 | case SK_UnwrapInitList: |
| 6026 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 6027 | break; |
| 6028 | |
| 6029 | case SK_RewrapInitList: { |
| 6030 | Expr *E = CurInit.take(); |
| 6031 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 6032 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 6033 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6034 | ILE->setSyntacticForm(Syntactic); |
| 6035 | ILE->setType(E->getType()); |
| 6036 | ILE->setValueKind(E->getValueKind()); |
| 6037 | CurInit = S.Owned(ILE); |
| 6038 | break; |
| 6039 | } |
| 6040 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6041 | case SK_ConstructorInitialization: { |
| 6042 | // When an initializer list is passed for a parameter of type "reference |
| 6043 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6044 | // EK_Parameter entity with reference type. |
| 6045 | // FIXME: This is a hack. What we really should do is create a user |
| 6046 | // conversion step for this case, but this makes it considerably more |
| 6047 | // complicated. For now, this will do. |
| 6048 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6049 | Entity.getType().getNonReferenceType()); |
| 6050 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 6051 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 6052 | : Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6053 | Kind, Args, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6054 | ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6055 | /*IsListInitialization*/ false, |
| 6056 | /*LBraceLoc*/ SourceLocation(), |
| 6057 | /*RBraceLoc*/ SourceLocation()); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6058 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6059 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6060 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6061 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6062 | step_iterator NextStep = Step; |
| 6063 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6064 | if (NextStep != StepEnd && |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6065 | (NextStep->Kind == SK_ConstructorInitialization || |
| 6066 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6067 | // The need for zero-initialization is recorded directly into |
| 6068 | // the call to the object's constructor within the next step. |
| 6069 | ConstructorInitRequiresZeroInit = true; |
| 6070 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6071 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6072 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6073 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6074 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6075 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6076 | Kind.getRange().getBegin()); |
| 6077 | |
| 6078 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 6079 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 6080 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6081 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6082 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6083 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6084 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6085 | break; |
| 6086 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6087 | |
| 6088 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6089 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6090 | ExprResult Result = CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6091 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 01ad048 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 6092 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 6093 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6094 | if (Result.isInvalid()) |
| 6095 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6096 | CurInit = Result; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6097 | |
| 6098 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6099 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6100 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6101 | Entity.isParameterKind() && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6102 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6103 | == Sema::Compatible) |
| 6104 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6105 | if (CurInitExprRes.isInvalid()) |
| 6106 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6107 | CurInit = CurInitExprRes; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6108 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6109 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6110 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 6111 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6112 | CurInit.get(), |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 6113 | getAssignmentAction(Entity, true), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6114 | &Complained)) { |
| 6115 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6116 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6117 | } else if (Complained) |
| 6118 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6119 | break; |
| 6120 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6121 | |
| 6122 | case SK_StringInit: { |
| 6123 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6124 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 6125 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6126 | break; |
| 6127 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6128 | |
| 6129 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6130 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6131 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 6132 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6133 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6134 | |
| 6135 | case SK_ArrayInit: |
| 6136 | // Okay: we checked everything before creating this step. Note that |
| 6137 | // this is a GNU extension. |
| 6138 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6139 | << Step->Type << CurInit.get()->getType() |
| 6140 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6141 | |
| 6142 | // If the destination type is an incomplete array type, update the |
| 6143 | // type accordingly. |
| 6144 | if (ResultType) { |
| 6145 | if (const IncompleteArrayType *IncompleteDest |
| 6146 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 6147 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6148 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6149 | *ResultType = S.Context.getConstantArrayType( |
| 6150 | IncompleteDest->getElementType(), |
| 6151 | ConstantSource->getSize(), |
| 6152 | ArrayType::Normal, 0); |
| 6153 | } |
| 6154 | } |
| 6155 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6156 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6157 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6158 | case SK_ParenthesizedArrayInit: |
| 6159 | // Okay: we checked everything before creating this step. Note that |
| 6160 | // this is a GNU extension. |
| 6161 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 6162 | << CurInit.get()->getSourceRange(); |
| 6163 | break; |
| 6164 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6165 | case SK_PassByIndirectCopyRestore: |
| 6166 | case SK_PassByIndirectRestore: |
| 6167 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 6168 | CurInit = S.Owned(new (S.Context) |
| 6169 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 6170 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 6171 | break; |
| 6172 | |
| 6173 | case SK_ProduceObjCObject: |
| 6174 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6175 | CK_ARCProduceObject, |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6176 | CurInit.take(), nullptr, |
| 6177 | VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6178 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6179 | |
| 6180 | case SK_StdInitializerList: { |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6181 | S.Diag(CurInit.get()->getExprLoc(), |
| 6182 | diag::warn_cxx98_compat_initializer_list_init) |
| 6183 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6184 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6185 | // Materialize the temporary into memory. |
| 6186 | MaterializeTemporaryExpr *MTE = new (S.Context) |
| 6187 | MaterializeTemporaryExpr(CurInit.get()->getType(), CurInit.get(), |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6188 | /*BoundToLvalueReference=*/false); |
| 6189 | |
| 6190 | // Maybe lifetime-extend the array temporary's subobjects to match the |
| 6191 | // entity's lifetime. |
| 6192 | if (const InitializedEntity *ExtendingEntity = |
| 6193 | getEntityForTemporaryLifetimeExtension(&Entity)) |
| 6194 | if (performReferenceExtension(MTE, ExtendingEntity)) |
| 6195 | warnOnLifetimeExtension(S, Entity, CurInit.get(), |
| 6196 | /*IsInitializerList=*/true, |
| 6197 | ExtendingEntity->getDecl()); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6198 | |
| 6199 | // Wrap it in a construction of a std::initializer_list<T>. |
| 6200 | CurInit = S.Owned( |
| 6201 | new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE)); |
| 6202 | |
| 6203 | // Bind the result, in case the library has given initializer_list a |
| 6204 | // non-trivial destructor. |
| 6205 | if (shouldBindAsTemporary(Entity)) |
| 6206 | CurInit = S.MaybeBindToTemporary(CurInit.take()); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6207 | break; |
| 6208 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6209 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6210 | case SK_OCLSamplerInit: { |
| 6211 | assert(Step->Type->isSamplerT() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6212 | "Sampler initialization on non-sampler type."); |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6213 | |
| 6214 | QualType SourceType = CurInit.get()->getType(); |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6215 | |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6216 | if (Entity.isParameterKind()) { |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6217 | if (!SourceType->isSamplerT()) |
| 6218 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 6219 | << SourceType; |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6220 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6221 | llvm_unreachable("Invalid EntityKind!"); |
| 6222 | } |
| 6223 | |
| 6224 | break; |
| 6225 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6226 | case SK_OCLZeroEvent: { |
| 6227 | assert(Step->Type->isEventT() && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6228 | "Event initialization on non-event type."); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6229 | |
| 6230 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
| 6231 | CK_ZeroToOCLEvent, |
| 6232 | CurInit.get()->getValueKind()); |
| 6233 | break; |
| 6234 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6235 | } |
| 6236 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6237 | |
| 6238 | // Diagnose non-fatal problems with the completed initialization. |
| 6239 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6240 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 6241 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 6242 | cast<FieldDecl>(Entity.getDecl()), |
| 6243 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6244 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6245 | return CurInit; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6246 | } |
| 6247 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6248 | /// Somewhere within T there is an uninitialized reference subobject. |
| 6249 | /// Dig it out and diagnose it. |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 6250 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 6251 | QualType T) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6252 | if (T->isReferenceType()) { |
| 6253 | S.Diag(Loc, diag::err_reference_without_init) |
| 6254 | << T.getNonReferenceType(); |
| 6255 | return true; |
| 6256 | } |
| 6257 | |
| 6258 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6259 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 6260 | return false; |
| 6261 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6262 | for (const auto *FI : RD->fields()) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6263 | if (FI->isUnnamedBitfield()) |
| 6264 | continue; |
| 6265 | |
| 6266 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 6267 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6268 | return true; |
| 6269 | } |
| 6270 | } |
| 6271 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 6272 | for (const auto &BI : RD->bases()) { |
| 6273 | if (DiagnoseUninitializedReference(S, BI.getLocStart(), BI.getType())) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6274 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6275 | return true; |
| 6276 | } |
| 6277 | } |
| 6278 | |
| 6279 | return false; |
| 6280 | } |
| 6281 | |
| 6282 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6283 | //===----------------------------------------------------------------------===// |
| 6284 | // Diagnose initialization failures |
| 6285 | //===----------------------------------------------------------------------===// |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6286 | |
| 6287 | /// Emit notes associated with an initialization that failed due to a |
| 6288 | /// "simple" conversion failure. |
| 6289 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 6290 | Expr *op) { |
| 6291 | QualType destType = entity.getType(); |
| 6292 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 6293 | op->getType()->isObjCObjectPointerType()) { |
| 6294 | |
| 6295 | // Emit a possible note about the conversion failing because the |
| 6296 | // operand is a message send with a related result type. |
| 6297 | S.EmitRelatedResultTypeNote(op); |
| 6298 | |
| 6299 | // Emit a possible note about a return failing because we're |
| 6300 | // expecting a related result type. |
| 6301 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 6302 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 6303 | } |
| 6304 | } |
| 6305 | |
Bill Wendling | 2ca3db4 | 2013-11-22 00:01:44 +0000 | [diff] [blame] | 6306 | static void diagnoseListInit(Sema &S, const InitializedEntity &Entity, |
| 6307 | InitListExpr *InitList) { |
| 6308 | QualType DestType = Entity.getType(); |
| 6309 | |
| 6310 | QualType E; |
| 6311 | if (S.getLangOpts().CPlusPlus11 && S.isStdInitializerList(DestType, &E)) { |
| 6312 | QualType ArrayType = S.Context.getConstantArrayType( |
| 6313 | E.withConst(), |
| 6314 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 6315 | InitList->getNumInits()), |
| 6316 | clang::ArrayType::Normal, 0); |
| 6317 | InitializedEntity HiddenArray = |
| 6318 | InitializedEntity::InitializeTemporary(ArrayType); |
| 6319 | return diagnoseListInit(S, HiddenArray, InitList); |
| 6320 | } |
| 6321 | |
| 6322 | InitListChecker DiagnoseInitList(S, Entity, InitList, DestType, |
| 6323 | /*VerifyOnly=*/false); |
| 6324 | assert(DiagnoseInitList.HadError() && |
| 6325 | "Inconsistent init list check result."); |
| 6326 | } |
| 6327 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6328 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6329 | const InitializedEntity &Entity, |
| 6330 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6331 | ArrayRef<Expr *> Args) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 6332 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6333 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6334 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 6335 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6336 | switch (Failure) { |
| 6337 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6338 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6339 | if (Args.empty()) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6340 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 6341 | // If this is value-initialization, this could be nested some way within |
| 6342 | // the target type. |
| 6343 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 6344 | DestType->isReferenceType()); |
| 6345 | bool Diagnosed = |
| 6346 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 6347 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 6348 | (void)Diagnosed; |
| 6349 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6350 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6351 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6352 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6353 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6354 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6355 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6356 | break; |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6357 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6358 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 6359 | break; |
| 6360 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6361 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 6362 | break; |
| 6363 | case FK_NarrowStringIntoWideCharArray: |
| 6364 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 6365 | break; |
| 6366 | case FK_WideStringIntoCharArray: |
| 6367 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 6368 | break; |
| 6369 | case FK_IncompatWideStringIntoWideChar: |
| 6370 | S.Diag(Kind.getLocation(), |
| 6371 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 6372 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6373 | case FK_ArrayTypeMismatch: |
| 6374 | case FK_NonConstantArrayInit: |
Bill Wendling | 2ca3db4 | 2013-11-22 00:01:44 +0000 | [diff] [blame] | 6375 | S.Diag(Kind.getLocation(), |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6376 | (Failure == FK_ArrayTypeMismatch |
| 6377 | ? diag::err_array_init_different_type |
| 6378 | : diag::err_array_init_non_constant_array)) |
| 6379 | << DestType.getNonReferenceType() |
| 6380 | << Args[0]->getType() |
| 6381 | << Args[0]->getSourceRange(); |
| 6382 | break; |
| 6383 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6384 | case FK_VariableLengthArrayHasInitializer: |
| 6385 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 6386 | << Args[0]->getSourceRange(); |
| 6387 | break; |
| 6388 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6389 | case FK_AddressOfOverloadFailed: { |
| 6390 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6391 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6392 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6393 | true, |
| 6394 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6395 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6396 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6397 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6398 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 6399 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6400 | switch (FailedOverloadResult) { |
| 6401 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6402 | if (Failure == FK_UserConversionOverloadFailed) |
| 6403 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 6404 | << Args[0]->getType() << DestType |
| 6405 | << Args[0]->getSourceRange(); |
| 6406 | else |
| 6407 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 6408 | << DestType << Args[0]->getType() |
| 6409 | << Args[0]->getSourceRange(); |
| 6410 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6411 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6412 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6413 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6414 | case OR_No_Viable_Function: |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 6415 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 6416 | DestType.getNonReferenceType(), |
| 6417 | diag::err_typecheck_nonviable_condition_incomplete, |
| 6418 | Args[0]->getType(), Args[0]->getSourceRange())) |
| 6419 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 6420 | << Args[0]->getType() << Args[0]->getSourceRange() |
| 6421 | << DestType.getNonReferenceType(); |
| 6422 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6423 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6424 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6425 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6426 | case OR_Deleted: { |
| 6427 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 6428 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 6429 | << Args[0]->getSourceRange(); |
| 6430 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6431 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6432 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 6433 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6434 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6435 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6436 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6437 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6438 | } |
| 6439 | break; |
| 6440 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6441 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6442 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6443 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6444 | } |
| 6445 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6446 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6447 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6448 | if (isa<InitListExpr>(Args[0])) { |
| 6449 | S.Diag(Kind.getLocation(), |
| 6450 | diag::err_lvalue_reference_bind_to_initlist) |
| 6451 | << DestType.getNonReferenceType().isVolatileQualified() |
| 6452 | << DestType.getNonReferenceType() |
| 6453 | << Args[0]->getSourceRange(); |
| 6454 | break; |
| 6455 | } |
| 6456 | // Intentional fallthrough |
| 6457 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6458 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6459 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6460 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 6461 | ? diag::err_lvalue_reference_bind_to_temporary |
| 6462 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 6463 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6464 | << DestType.getNonReferenceType() |
| 6465 | << Args[0]->getType() |
| 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_RValueReferenceBindingToLValue: |
| 6470 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 6471 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6472 | << Args[0]->getSourceRange(); |
| 6473 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6474 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6475 | case FK_ReferenceInitDropsQualifiers: |
| 6476 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 6477 | << DestType.getNonReferenceType() |
| 6478 | << Args[0]->getType() |
| 6479 | << Args[0]->getSourceRange(); |
| 6480 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6481 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6482 | case FK_ReferenceInitFailed: |
| 6483 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 6484 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6485 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6486 | << Args[0]->getType() |
| 6487 | << Args[0]->getSourceRange(); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6488 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6489 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6490 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6491 | case FK_ConversionFailed: { |
| 6492 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6493 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6494 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6495 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6496 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6497 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6498 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6499 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 6500 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6501 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6502 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6503 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6504 | |
| 6505 | case FK_ConversionFromPropertyFailed: |
| 6506 | // No-op. This error has already been reported. |
| 6507 | break; |
| 6508 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6509 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6510 | SourceRange R; |
| 6511 | |
| 6512 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6513 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6514 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6515 | else |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6516 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6517 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6518 | R.setBegin(S.getLocForEndOfToken(R.getBegin())); |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6519 | if (Kind.isCStyleOrFunctionalCast()) |
| 6520 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 6521 | << R; |
| 6522 | else |
| 6523 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 6524 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6525 | break; |
| 6526 | } |
| 6527 | |
| 6528 | case FK_ReferenceBindingToInitList: |
| 6529 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 6530 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 6531 | break; |
| 6532 | |
| 6533 | case FK_InitListBadDestinationType: |
| 6534 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 6535 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 6536 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6537 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6538 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6539 | case FK_ConstructorOverloadFailed: { |
| 6540 | SourceRange ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6541 | if (Args.size()) |
| 6542 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 6543 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6544 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6545 | if (Failure == FK_ListConstructorOverloadFailed) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6546 | assert(Args.size() == 1 && "List construction from other than 1 argument."); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6547 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6548 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6549 | } |
| 6550 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6551 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 6552 | // bad. |
| 6553 | switch (FailedOverloadResult) { |
| 6554 | case OR_Ambiguous: |
| 6555 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 6556 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6557 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6558 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6559 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6560 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6561 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 6562 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 6563 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 6564 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6565 | // This is implicit default initialization of a member or |
| 6566 | // base within a constructor. If no viable function was |
| 6567 | // found, notify the user that she needs to explicitly |
| 6568 | // initialize this base/member. |
| 6569 | CXXConstructorDecl *Constructor |
| 6570 | = cast<CXXConstructorDecl>(S.CurContext); |
| 6571 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6572 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6573 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6574 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6575 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6576 | << /*base=*/0 |
| 6577 | << Entity.getType(); |
| 6578 | |
| 6579 | RecordDecl *BaseDecl |
| 6580 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 6581 | ->getDecl(); |
| 6582 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 6583 | << S.Context.getTagDeclType(BaseDecl); |
| 6584 | } else { |
| 6585 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6586 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6587 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6588 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6589 | << /*member=*/1 |
| 6590 | << Entity.getName(); |
| 6591 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 6592 | |
| 6593 | if (const RecordType *Record |
| 6594 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6595 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6596 | diag::note_previous_decl) |
| 6597 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6598 | } |
| 6599 | break; |
| 6600 | } |
| 6601 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6602 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6603 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6604 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6605 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6606 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6607 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6608 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6609 | OverloadingResult Ovl |
| 6610 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6611 | if (Ovl != OR_Deleted) { |
| 6612 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6613 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6614 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6615 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6616 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6617 | |
| 6618 | // If this is a defaulted or implicitly-declared function, then |
| 6619 | // it was implicitly deleted. Make it clear that the deletion was |
| 6620 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6621 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6622 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6623 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6624 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6625 | else |
| 6626 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6627 | << true << DestType << ArgsRange; |
| 6628 | |
| 6629 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6630 | break; |
| 6631 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6632 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6633 | case OR_Success: |
| 6634 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6635 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6636 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6637 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6638 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6639 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6640 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6641 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6642 | // This is implicit default-initialization of a const member in |
| 6643 | // a constructor. Complain that it needs to be explicitly |
| 6644 | // initialized. |
| 6645 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6646 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6647 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6648 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6649 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6650 | << /*const=*/1 |
| 6651 | << Entity.getName(); |
| 6652 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6653 | << Entity.getName(); |
| 6654 | } else { |
| 6655 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 6656 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6657 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6658 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6659 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6660 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6661 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6662 | diag::err_init_incomplete_type); |
| 6663 | break; |
| 6664 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6665 | case FK_ListInitializationFailed: { |
| 6666 | // Run the init list checker again to emit diagnostics. |
Bill Wendling | 2ca3db4 | 2013-11-22 00:01:44 +0000 | [diff] [blame] | 6667 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
| 6668 | diagnoseListInit(S, Entity, InitList); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6669 | break; |
| 6670 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6671 | |
| 6672 | case FK_PlaceholderType: { |
| 6673 | // FIXME: Already diagnosed! |
| 6674 | break; |
| 6675 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6676 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6677 | case FK_ExplicitConstructor: { |
| 6678 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6679 | << Args[0]->getSourceRange(); |
| 6680 | OverloadCandidateSet::iterator Best; |
| 6681 | OverloadingResult Ovl |
| 6682 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6683 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6684 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6685 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6686 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6687 | break; |
| 6688 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6689 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6690 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6691 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6692 | return true; |
| 6693 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6694 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6695 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6696 | switch (SequenceKind) { |
| 6697 | case FailedSequence: { |
| 6698 | OS << "Failed sequence: "; |
| 6699 | switch (Failure) { |
| 6700 | case FK_TooManyInitsForReference: |
| 6701 | OS << "too many initializers for reference"; |
| 6702 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6703 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6704 | case FK_ArrayNeedsInitList: |
| 6705 | OS << "array requires initializer list"; |
| 6706 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6707 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6708 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6709 | OS << "array requires initializer list or string literal"; |
| 6710 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6711 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6712 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6713 | OS << "array requires initializer list or wide string literal"; |
| 6714 | break; |
| 6715 | |
| 6716 | case FK_NarrowStringIntoWideCharArray: |
| 6717 | OS << "narrow string into wide char array"; |
| 6718 | break; |
| 6719 | |
| 6720 | case FK_WideStringIntoCharArray: |
| 6721 | OS << "wide string into char array"; |
| 6722 | break; |
| 6723 | |
| 6724 | case FK_IncompatWideStringIntoWideChar: |
| 6725 | OS << "incompatible wide string into wide char array"; |
| 6726 | break; |
| 6727 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6728 | case FK_ArrayTypeMismatch: |
| 6729 | OS << "array type mismatch"; |
| 6730 | break; |
| 6731 | |
| 6732 | case FK_NonConstantArrayInit: |
| 6733 | OS << "non-constant array initializer"; |
| 6734 | break; |
| 6735 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6736 | case FK_AddressOfOverloadFailed: |
| 6737 | OS << "address of overloaded function failed"; |
| 6738 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6739 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6740 | case FK_ReferenceInitOverloadFailed: |
| 6741 | OS << "overload resolution for reference initialization failed"; |
| 6742 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6743 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6744 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6745 | OS << "non-const lvalue reference bound to temporary"; |
| 6746 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6747 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6748 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6749 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6750 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6751 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6752 | case FK_RValueReferenceBindingToLValue: |
| 6753 | OS << "rvalue reference bound to an lvalue"; |
| 6754 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6755 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6756 | case FK_ReferenceInitDropsQualifiers: |
| 6757 | OS << "reference initialization drops qualifiers"; |
| 6758 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6759 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6760 | case FK_ReferenceInitFailed: |
| 6761 | OS << "reference initialization failed"; |
| 6762 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6763 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6764 | case FK_ConversionFailed: |
| 6765 | OS << "conversion failed"; |
| 6766 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6767 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6768 | case FK_ConversionFromPropertyFailed: |
| 6769 | OS << "conversion from property failed"; |
| 6770 | break; |
| 6771 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6772 | case FK_TooManyInitsForScalar: |
| 6773 | OS << "too many initializers for scalar"; |
| 6774 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6775 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6776 | case FK_ReferenceBindingToInitList: |
| 6777 | OS << "referencing binding to initializer list"; |
| 6778 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6779 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6780 | case FK_InitListBadDestinationType: |
| 6781 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6782 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6783 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6784 | case FK_UserConversionOverloadFailed: |
| 6785 | OS << "overloading failed for user-defined conversion"; |
| 6786 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6787 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6788 | case FK_ConstructorOverloadFailed: |
| 6789 | OS << "constructor overloading failed"; |
| 6790 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6791 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6792 | case FK_DefaultInitOfConst: |
| 6793 | OS << "default initialization of a const variable"; |
| 6794 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6795 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6796 | case FK_Incomplete: |
| 6797 | OS << "initialization of incomplete type"; |
| 6798 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6799 | |
| 6800 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6801 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6802 | break; |
| 6803 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6804 | case FK_VariableLengthArrayHasInitializer: |
| 6805 | OS << "variable length array has an initializer"; |
| 6806 | break; |
| 6807 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6808 | case FK_PlaceholderType: |
| 6809 | OS << "initializer expression isn't contextually valid"; |
| 6810 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6811 | |
| 6812 | case FK_ListConstructorOverloadFailed: |
| 6813 | OS << "list constructor overloading failed"; |
| 6814 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6815 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6816 | case FK_ExplicitConstructor: |
| 6817 | OS << "list copy initialization chose explicit constructor"; |
| 6818 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6819 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6820 | OS << '\n'; |
| 6821 | return; |
| 6822 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6823 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6824 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6825 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6826 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6827 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6828 | case NormalSequence: |
| 6829 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6830 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6831 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6832 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6833 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6834 | if (S != step_begin()) { |
| 6835 | OS << " -> "; |
| 6836 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6837 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6838 | switch (S->Kind) { |
| 6839 | case SK_ResolveAddressOfOverloadedFunction: |
| 6840 | OS << "resolve address of overloaded function"; |
| 6841 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6842 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6843 | case SK_CastDerivedToBaseRValue: |
| 6844 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6845 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6846 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6847 | case SK_CastDerivedToBaseXValue: |
| 6848 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6849 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6850 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6851 | case SK_CastDerivedToBaseLValue: |
| 6852 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 6853 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6854 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6855 | case SK_BindReference: |
| 6856 | OS << "bind reference to lvalue"; |
| 6857 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6858 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6859 | case SK_BindReferenceToTemporary: |
| 6860 | OS << "bind reference to a temporary"; |
| 6861 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6862 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6863 | case SK_ExtraneousCopyToTemporary: |
| 6864 | OS << "extraneous C++03 copy to temporary"; |
| 6865 | break; |
| 6866 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6867 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6868 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6869 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6870 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6871 | case SK_QualificationConversionRValue: |
| 6872 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6873 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6874 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6875 | case SK_QualificationConversionXValue: |
| 6876 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6877 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6878 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6879 | case SK_QualificationConversionLValue: |
| 6880 | OS << "qualification conversion (lvalue)"; |
| 6881 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6882 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6883 | case SK_LValueToRValue: |
| 6884 | OS << "load (lvalue to rvalue)"; |
| 6885 | break; |
| 6886 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6887 | case SK_ConversionSequence: |
| 6888 | OS << "implicit conversion sequence ("; |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 6889 | S->ICS->dump(); // FIXME: use OS |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6890 | OS << ")"; |
| 6891 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6892 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6893 | case SK_ConversionSequenceNoNarrowing: |
| 6894 | OS << "implicit conversion sequence with narrowing prohibited ("; |
Douglas Gregor | 2f8b0cc | 2013-11-08 02:16:10 +0000 | [diff] [blame] | 6895 | S->ICS->dump(); // FIXME: use OS |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6896 | OS << ")"; |
| 6897 | break; |
| 6898 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6899 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6900 | OS << "list aggregate initialization"; |
| 6901 | break; |
| 6902 | |
| 6903 | case SK_ListConstructorCall: |
| 6904 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6905 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6906 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6907 | case SK_UnwrapInitList: |
| 6908 | OS << "unwrap reference initializer list"; |
| 6909 | break; |
| 6910 | |
| 6911 | case SK_RewrapInitList: |
| 6912 | OS << "rewrap reference initializer list"; |
| 6913 | break; |
| 6914 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6915 | case SK_ConstructorInitialization: |
| 6916 | OS << "constructor initialization"; |
| 6917 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6918 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6919 | case SK_ZeroInitialization: |
| 6920 | OS << "zero initialization"; |
| 6921 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6922 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6923 | case SK_CAssignment: |
| 6924 | OS << "C assignment"; |
| 6925 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6926 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6927 | case SK_StringInit: |
| 6928 | OS << "string initialization"; |
| 6929 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6930 | |
| 6931 | case SK_ObjCObjectConversion: |
| 6932 | OS << "Objective-C object conversion"; |
| 6933 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6934 | |
| 6935 | case SK_ArrayInit: |
| 6936 | OS << "array initialization"; |
| 6937 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6938 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6939 | case SK_ParenthesizedArrayInit: |
| 6940 | OS << "parenthesized array initialization"; |
| 6941 | break; |
| 6942 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6943 | case SK_PassByIndirectCopyRestore: |
| 6944 | OS << "pass by indirect copy and restore"; |
| 6945 | break; |
| 6946 | |
| 6947 | case SK_PassByIndirectRestore: |
| 6948 | OS << "pass by indirect restore"; |
| 6949 | break; |
| 6950 | |
| 6951 | case SK_ProduceObjCObject: |
| 6952 | OS << "Objective-C object retension"; |
| 6953 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6954 | |
| 6955 | case SK_StdInitializerList: |
| 6956 | OS << "std::initializer_list from initializer list"; |
| 6957 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6958 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6959 | case SK_OCLSamplerInit: |
| 6960 | OS << "OpenCL sampler_t from integer constant"; |
| 6961 | break; |
| 6962 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6963 | case SK_OCLZeroEvent: |
| 6964 | OS << "OpenCL event_t from zero"; |
| 6965 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6966 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6967 | |
| 6968 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6969 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6970 | |
| 6971 | OS << '\n'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6972 | } |
| 6973 | |
| 6974 | void InitializationSequence::dump() const { |
| 6975 | dump(llvm::errs()); |
| 6976 | } |
| 6977 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6978 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 6979 | const ImplicitConversionSequence &ICS, |
| 6980 | QualType PreNarrowingType, |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6981 | QualType EntityType, |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6982 | const Expr *PostInit) { |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 6983 | const StandardConversionSequence *SCS = nullptr; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6984 | switch (ICS.getKind()) { |
| 6985 | case ImplicitConversionSequence::StandardConversion: |
| 6986 | SCS = &ICS.Standard; |
| 6987 | break; |
| 6988 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6989 | SCS = &ICS.UserDefined.After; |
| 6990 | break; |
| 6991 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6992 | case ImplicitConversionSequence::EllipsisConversion: |
| 6993 | case ImplicitConversionSequence::BadConversion: |
| 6994 | return; |
| 6995 | } |
| 6996 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6997 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6998 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6999 | QualType ConstantType; |
| 7000 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 7001 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7002 | case NK_Not_Narrowing: |
| 7003 | // No narrowing occurred. |
| 7004 | return; |
| 7005 | |
| 7006 | case NK_Type_Narrowing: |
| 7007 | // This was a floating-to-integer conversion, which is always considered a |
| 7008 | // narrowing conversion even if the value is a constant and can be |
| 7009 | // represented exactly as an integer. |
| 7010 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 3347b49 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7011 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7012 | ? diag::warn_init_list_type_narrowing |
| 7013 | : diag::ext_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7014 | << PostInit->getSourceRange() |
| 7015 | << PreNarrowingType.getLocalUnqualifiedType() |
| 7016 | << EntityType.getLocalUnqualifiedType(); |
| 7017 | break; |
| 7018 | |
| 7019 | case NK_Constant_Narrowing: |
| 7020 | // A constant value was narrowed. |
| 7021 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 3347b49 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7022 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7023 | ? diag::warn_init_list_constant_narrowing |
| 7024 | : diag::ext_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7025 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 7026 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7027 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7028 | break; |
| 7029 | |
| 7030 | case NK_Variable_Narrowing: |
| 7031 | // A variable's value may have been narrowed. |
| 7032 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 3347b49 | 2013-11-12 02:41:45 +0000 | [diff] [blame] | 7033 | (S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11) |
| 7034 | ? diag::warn_init_list_variable_narrowing |
| 7035 | : diag::ext_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7036 | << PostInit->getSourceRange() |
| 7037 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 7038 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7039 | break; |
| 7040 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7041 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 7042 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7043 | llvm::raw_svector_ostream OS(StaticCast); |
| 7044 | OS << "static_cast<"; |
| 7045 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 7046 | // It's important to use the typedef's name if there is one so that the |
| 7047 | // fixit doesn't break code using types like int64_t. |
| 7048 | // |
| 7049 | // FIXME: This will break if the typedef requires qualification. But |
| 7050 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 7051 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7052 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7053 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7054 | else { |
| 7055 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 7056 | // with a broken cast. |
| 7057 | return; |
| 7058 | } |
| 7059 | OS << ">("; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 7060 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_silence) |
| 7061 | << PostInit->getSourceRange() |
| 7062 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
| 7063 | << FixItHint::CreateInsertion( |
| 7064 | S.getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7065 | } |
| 7066 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7067 | //===----------------------------------------------------------------------===// |
| 7068 | // Initialization helper functions |
| 7069 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7070 | bool |
| 7071 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 7072 | ExprResult Init) { |
| 7073 | if (Init.isInvalid()) |
| 7074 | return false; |
| 7075 | |
| 7076 | Expr *InitE = Init.get(); |
| 7077 | assert(InitE && "No initialization expression"); |
| 7078 | |
Douglas Gregor | 3c394c5 | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 7079 | InitializationKind Kind |
| 7080 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7081 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 7082 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7083 | } |
| 7084 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7085 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7086 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 7087 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7088 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7089 | bool TopLevelOfInitList, |
| 7090 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7091 | if (Init.isInvalid()) |
| 7092 | return ExprError(); |
| 7093 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7094 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7095 | assert(InitE && "No initialization expression?"); |
| 7096 | |
| 7097 | if (EqualLoc.isInvalid()) |
| 7098 | EqualLoc = InitE->getLocStart(); |
| 7099 | |
| 7100 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7101 | EqualLoc, |
| 7102 | AllowExplicit); |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7103 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7104 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7105 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7106 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7107 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7108 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7109 | } |