Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Initialization.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Sema/Designator.h" |
| 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 35 | /// \brief Check whether T is compatible with a wide character type (wchar_t, |
| 36 | /// char16_t or char32_t). |
| 37 | static bool IsWideCharCompatible(QualType T, ASTContext &Context) { |
| 38 | if (Context.typesAreCompatible(Context.getWideCharType(), T)) |
| 39 | return true; |
| 40 | if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) { |
| 41 | return Context.typesAreCompatible(Context.Char16Ty, T) || |
| 42 | Context.typesAreCompatible(Context.Char32Ty, T); |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | enum StringInitFailureKind { |
| 48 | SIF_None, |
| 49 | SIF_NarrowStringIntoWideChar, |
| 50 | SIF_WideStringIntoChar, |
| 51 | SIF_IncompatWideStringIntoWideChar, |
| 52 | SIF_Other |
| 53 | }; |
| 54 | |
| 55 | /// \brief Check whether the array of type AT can be initialized by the Init |
| 56 | /// expression by means of string initialization. Returns SIF_None if so, |
| 57 | /// otherwise returns a StringInitFailureKind that describes why the |
| 58 | /// initialization would not work. |
| 59 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 60 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 61 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 62 | return SIF_Other; |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 64 | // See if this is a string literal or @encode. |
| 65 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 67 | // Handle @encode, which is a narrow string. |
| 68 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 69 | return SIF_None; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 70 | |
| 71 | // Otherwise we can only handle string literals. |
| 72 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 73 | if (SL == 0) |
| 74 | return SIF_Other; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 75 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 76 | const QualType ElemTy = |
| 77 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 78 | |
| 79 | switch (SL->getKind()) { |
| 80 | case StringLiteral::Ascii: |
| 81 | case StringLiteral::UTF8: |
| 82 | // char array can be initialized with a narrow string. |
| 83 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 84 | if (ElemTy->isCharType()) |
| 85 | return SIF_None; |
| 86 | if (IsWideCharCompatible(ElemTy, Context)) |
| 87 | return SIF_NarrowStringIntoWideChar; |
| 88 | return SIF_Other; |
| 89 | // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15: |
| 90 | // "An array with element type compatible with a qualified or unqualified |
| 91 | // version of wchar_t, char16_t, or char32_t may be initialized by a wide |
| 92 | // string literal with the corresponding encoding prefix (L, u, or U, |
| 93 | // respectively), optionally enclosed in braces. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 94 | case StringLiteral::UTF16: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 95 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 96 | return SIF_None; |
| 97 | if (ElemTy->isCharType()) |
| 98 | return SIF_WideStringIntoChar; |
| 99 | if (IsWideCharCompatible(ElemTy, Context)) |
| 100 | return SIF_IncompatWideStringIntoWideChar; |
| 101 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 102 | case StringLiteral::UTF32: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 103 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 104 | return SIF_None; |
| 105 | if (ElemTy->isCharType()) |
| 106 | return SIF_WideStringIntoChar; |
| 107 | if (IsWideCharCompatible(ElemTy, Context)) |
| 108 | return SIF_IncompatWideStringIntoWideChar; |
| 109 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 110 | case StringLiteral::Wide: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 111 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 112 | return SIF_None; |
| 113 | if (ElemTy->isCharType()) |
| 114 | return SIF_WideStringIntoChar; |
| 115 | if (IsWideCharCompatible(ElemTy, Context)) |
| 116 | return SIF_IncompatWideStringIntoWideChar; |
| 117 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 120 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 123 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 124 | ASTContext &Context) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 125 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 126 | if (!arrayType) |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 127 | return SIF_Other; |
| 128 | return IsStringInit(init, arrayType, Context); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 131 | /// Update the type of a string literal, including any surrounding parentheses, |
| 132 | /// to match the type of the object which it is initializing. |
| 133 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 134 | while (true) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 135 | E->setType(Ty); |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 136 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 137 | break; |
| 138 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 139 | E = PE->getSubExpr(); |
| 140 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 141 | E = UO->getSubExpr(); |
| 142 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 143 | E = GSE->getResultExpr(); |
| 144 | else |
| 145 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 146 | } |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 149 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 150 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 151 | // Get the length of the string as parsed. |
| 152 | uint64_t StrLength = |
| 153 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 154 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 156 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 158 | // being initialized to a string literal. |
Benjamin Kramer | 65263b4 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 159 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 160 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 161 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 162 | ConstVal, |
| 163 | ArrayType::Normal, 0); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 164 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 165 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 168 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 170 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 171 | // the size may be smaller or larger than the string we are initializing. |
| 172 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 173 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 174 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 175 | // For Pascal strings it's OK to strip off the terminating null character, |
| 176 | // so the example below is valid: |
| 177 | // |
| 178 | // unsigned char a[2] = "\pa"; |
| 179 | if (SL->isPascal()) |
| 180 | StrLength--; |
| 181 | } |
| 182 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 183 | // [dcl.init.string]p2 |
| 184 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 185 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 186 | diag::err_initializer_string_for_char_array_too_long) |
| 187 | << Str->getSourceRange(); |
| 188 | } else { |
| 189 | // C99 6.7.8p14. |
| 190 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 191 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 192 | diag::warn_initializer_string_for_char_array_too_long) |
| 193 | << Str->getSourceRange(); |
| 194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 196 | // Set the type to the actual size that we are initializing. If we have |
| 197 | // something like: |
| 198 | // char x[1] = "foo"; |
| 199 | // then this will set the string literal's type to char[1]. |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 200 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 203 | //===----------------------------------------------------------------------===// |
| 204 | // Semantic checking for initializer lists. |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 207 | /// @brief Semantic checking for initializer lists. |
| 208 | /// |
| 209 | /// The InitListChecker class contains a set of routines that each |
| 210 | /// handle the initialization of a certain kind of entity, e.g., |
| 211 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 212 | /// InitListChecker itself performs a recursive walk of the subobject |
| 213 | /// structure of the type to be initialized, while stepping through |
| 214 | /// the initializer list one element at a time. The IList and Index |
| 215 | /// parameters to each of the Check* routines contain the active |
| 216 | /// (syntactic) initializer list and the index into that initializer |
| 217 | /// list that represents the current initializer. Each routine is |
| 218 | /// responsible for moving that Index forward as it consumes elements. |
| 219 | /// |
| 220 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 221 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 222 | /// initializer list and the index into that initializer list where we |
| 223 | /// are copying initializers as we map them over to the semantic |
| 224 | /// list. Once we have completed our recursive walk of the subobject |
| 225 | /// structure, we will have constructed a full semantic initializer |
| 226 | /// list. |
| 227 | /// |
| 228 | /// C99 designators cause changes in the initializer list traversal, |
| 229 | /// because they make the initialization "jump" into a specific |
| 230 | /// subobject and then continue the initialization from that |
| 231 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 232 | /// designated subobject and manages backing out the recursion to |
| 233 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 234 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 235 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 236 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 237 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 238 | bool VerifyOnly; // no diagnostics, no structure building |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 239 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 242 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 243 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 244 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 245 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 246 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 247 | InitListExpr *IList, QualType &T, |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 248 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 249 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 250 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 251 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 252 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 253 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 254 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 255 | unsigned &StructuredIndex, |
| 256 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 257 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 258 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 259 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 260 | InitListExpr *StructuredList, |
| 261 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 262 | void CheckComplexType(const InitializedEntity &Entity, |
| 263 | InitListExpr *IList, QualType DeclType, |
| 264 | unsigned &Index, |
| 265 | InitListExpr *StructuredList, |
| 266 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 267 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 268 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 269 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 270 | InitListExpr *StructuredList, |
| 271 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 272 | void CheckReferenceType(const InitializedEntity &Entity, |
| 273 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 274 | unsigned &Index, |
| 275 | InitListExpr *StructuredList, |
| 276 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 277 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 278 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 279 | InitListExpr *StructuredList, |
| 280 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 281 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 282 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 284 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 285 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 286 | unsigned &StructuredIndex, |
| 287 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 288 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 289 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 291 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 292 | InitListExpr *StructuredList, |
| 293 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 294 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 295 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 296 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 298 | RecordDecl::field_iterator *NextField, |
| 299 | llvm::APSInt *NextElementIndex, |
| 300 | unsigned &Index, |
| 301 | InitListExpr *StructuredList, |
| 302 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 303 | bool FinishSubobjectInit, |
| 304 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 305 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 306 | QualType CurrentObjectType, |
| 307 | InitListExpr *StructuredList, |
| 308 | unsigned StructuredIndex, |
| 309 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 310 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 311 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 312 | Expr *expr); |
| 313 | int numArrayElements(QualType DeclType); |
| 314 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 315 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 316 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 317 | const InitializedEntity &ParentEntity, |
| 318 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 319 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 320 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 321 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 322 | Expr *InitExpr, FieldDecl *Field, |
| 323 | bool TopLevelObject); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 324 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 325 | |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 326 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 327 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 328 | InitListExpr *IL, QualType &T, bool VerifyOnly); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 329 | bool HadError() { return hadError; } |
| 330 | |
| 331 | // @brief Retrieves the fully-structured initializer list used for |
| 332 | // semantic analysis and code generation. |
| 333 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 334 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 335 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 336 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 337 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 338 | assert(VerifyOnly && |
| 339 | "CheckValueInitializable is only inteded for verification mode."); |
| 340 | |
| 341 | SourceLocation Loc; |
| 342 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 343 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 344 | InitializationSequence InitSeq(SemaRef, Entity, Kind, None); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 345 | if (InitSeq.Failed()) |
| 346 | hadError = true; |
| 347 | } |
| 348 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 349 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 350 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 351 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 352 | bool &RequiresSecondPass) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 353 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 354 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 355 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 356 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 357 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 358 | // If there's no explicit initializer but we have a default initializer, use |
| 359 | // that. This only happens in C++1y, since classes with default |
| 360 | // initializers are not aggregates in C++11. |
| 361 | if (Field->hasInClassInitializer()) { |
| 362 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 363 | ILE->getRBraceLoc(), Field); |
| 364 | if (Init < NumInits) |
| 365 | ILE->setInit(Init, DIE); |
| 366 | else { |
| 367 | ILE->updateInit(SemaRef.Context, Init, DIE); |
| 368 | RequiresSecondPass = true; |
| 369 | } |
| 370 | return; |
| 371 | } |
| 372 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 373 | // FIXME: We probably don't need to handle references |
| 374 | // specially here, since value-initialization of references is |
| 375 | // handled in InitializationSequence. |
| 376 | if (Field->getType()->isReferenceType()) { |
| 377 | // C++ [dcl.init.aggr]p9: |
| 378 | // If an incomplete or empty initializer-list leaves a |
| 379 | // member of reference type uninitialized, the program is |
| 380 | // ill-formed. |
| 381 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 382 | << Field->getType() |
| 383 | << ILE->getSyntacticForm()->getSourceRange(); |
| 384 | SemaRef.Diag(Field->getLocation(), |
| 385 | diag::note_uninit_reference_member); |
| 386 | hadError = true; |
| 387 | return; |
| 388 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 389 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 390 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 391 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 392 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 393 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 394 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 395 | hadError = true; |
| 396 | return; |
| 397 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 398 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 399 | ExprResult MemberInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 400 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 401 | if (MemberInit.isInvalid()) { |
| 402 | hadError = true; |
| 403 | return; |
| 404 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 405 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 406 | if (hadError) { |
| 407 | // Do nothing |
| 408 | } else if (Init < NumInits) { |
| 409 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 410 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 411 | // Value-initialization requires a constructor call, so |
| 412 | // extend the initializer list to include the constructor |
| 413 | // call and make a note that we'll need to take another pass |
| 414 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 415 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 416 | RequiresSecondPass = true; |
| 417 | } |
| 418 | } else if (InitListExpr *InnerILE |
| 419 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 420 | FillInValueInitializations(MemberEntity, InnerILE, |
| 421 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 424 | /// Recursively replaces NULL values within the given initializer list |
| 425 | /// with expressions that perform value-initialization of the |
| 426 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 427 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 428 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 429 | InitListExpr *ILE, |
| 430 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 432 | "Should not have void type"); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 433 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 434 | if (ILE->getSyntacticForm()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 435 | Loc = ILE->getSyntacticForm()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 437 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 438 | const RecordDecl *RDecl = RType->getDecl(); |
| 439 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 440 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 441 | Entity, ILE, RequiresSecondPass); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 442 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 443 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
| 444 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 445 | FieldEnd = RDecl->field_end(); |
| 446 | Field != FieldEnd; ++Field) { |
| 447 | if (Field->hasInClassInitializer()) { |
| 448 | FillInValueInitForField(0, *Field, Entity, ILE, RequiresSecondPass); |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | } else { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 453 | unsigned Init = 0; |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 454 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 455 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 456 | Field != FieldEnd; ++Field) { |
| 457 | if (Field->isUnnamedBitfield()) |
| 458 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 459 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 460 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 461 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 462 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 463 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 464 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 465 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 466 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 467 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 469 | // Only look at the first initialization of a union. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 470 | if (RDecl->isUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 471 | break; |
| 472 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 477 | |
| 478 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 480 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 481 | unsigned NumInits = ILE->getNumInits(); |
| 482 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 483 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 484 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 485 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 486 | NumElements = CAType->getSize().getZExtValue(); |
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); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 489 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 490 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 491 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 492 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 493 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 494 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 495 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 497 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 498 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 499 | if (hadError) |
| 500 | return; |
| 501 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 502 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 503 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 504 | ElementEntity.setElementIndex(Init); |
| 505 | |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 506 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0); |
| 507 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 508 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 509 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 510 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 511 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 512 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 513 | hadError = true; |
| 514 | return; |
| 515 | } |
| 516 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 517 | ExprResult ElementInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 518 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 519 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 520 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 521 | return; |
| 522 | } |
| 523 | |
| 524 | if (hadError) { |
| 525 | // Do nothing |
| 526 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 527 | // For arrays, just set the expression used for value-initialization |
| 528 | // of the "holes" in the array. |
| 529 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 530 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 531 | else |
| 532 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 533 | } else { |
| 534 | // For arrays, just set the expression used for value-initialization |
| 535 | // of the rest of elements and exit. |
| 536 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 537 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 538 | return; |
| 539 | } |
| 540 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 541 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 542 | // Value-initialization requires a constructor call, so |
| 543 | // extend the initializer list to include the constructor |
| 544 | // call and make a note that we'll need to take another pass |
| 545 | // through the initializer list. |
| 546 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 547 | RequiresSecondPass = true; |
| 548 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 549 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 550 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 551 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 552 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 556 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 557 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 558 | InitListExpr *IL, QualType &T, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 559 | bool VerifyOnly) |
| 560 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 561 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 562 | |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 563 | FullyStructuredList = |
| 564 | getStructuredSubobjectInit(IL, 0, T, 0, 0, IL->getSourceRange()); |
| 565 | CheckExplicitInitList(Entity, IL, T, FullyStructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 566 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 567 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 568 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 569 | bool RequiresSecondPass = false; |
| 570 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 571 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 572 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 573 | RequiresSecondPass); |
| 574 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 578 | // FIXME: use a proper constant |
| 579 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 580 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 581 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 582 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 583 | } |
| 584 | return maxElements; |
| 585 | } |
| 586 | |
| 587 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 588 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 589 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 590 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 591 | Field = structDecl->field_begin(), |
| 592 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 593 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 594 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 595 | ++InitializableMembers; |
| 596 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 597 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 598 | return std::min(InitializableMembers, 1); |
| 599 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 602 | /// Check whether the range of the initializer \p ParentIList from element |
| 603 | /// \p Index onwards can be used to initialize an object of type \p T. Update |
| 604 | /// \p Index to indicate how many elements of the list were consumed. |
| 605 | /// |
| 606 | /// This also fills in \p StructuredList, from element \p StructuredIndex |
| 607 | /// onwards, with the fully-braced, desugared form of the initialization. |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 608 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 609 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 610 | QualType T, unsigned &Index, |
| 611 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 612 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 613 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 614 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 615 | if (T->isArrayType()) |
| 616 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 617 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 618 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 619 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 620 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 621 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 622 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 623 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 624 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 625 | if (!VerifyOnly) |
| 626 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 627 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 628 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 629 | hadError = true; |
| 630 | return; |
| 631 | } |
| 632 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 633 | // Build a structured initializer list corresponding to this subobject. |
| 634 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 636 | StructuredIndex, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 637 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 638 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 639 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 640 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 641 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 642 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 643 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 644 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 645 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 646 | StructuredSubobjectInitIndex); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 647 | |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 648 | if (!VerifyOnly) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 649 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 650 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 651 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 652 | // Update the structured sub-object initializer so that it's ending |
| 653 | // range corresponds with the end of the last initializer it used. |
| 654 | if (EndIndex < ParentIList->getNumInits()) { |
| 655 | SourceLocation EndLoc |
| 656 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 657 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 658 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 659 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 660 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 661 | if (T->isArrayType() || T->isRecordType()) { |
| 662 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 663 | diag::warn_missing_braces) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 664 | << StructuredSubobjectInitList->getSourceRange() |
| 665 | << FixItHint::CreateInsertion( |
| 666 | StructuredSubobjectInitList->getLocStart(), "{") |
| 667 | << FixItHint::CreateInsertion( |
| 668 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 669 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 670 | "}"); |
| 671 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 672 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 675 | /// Check whether the initializer \p IList (that was written with explicit |
| 676 | /// braces) can be used to initialize an object of type \p T. |
| 677 | /// |
| 678 | /// This also fills in \p StructuredList with the fully-braced, desugared |
| 679 | /// form of the initialization. |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 680 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 681 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 682 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 683 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 684 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 685 | if (!VerifyOnly) { |
| 686 | SyntacticToSemantic[IList] = StructuredList; |
| 687 | StructuredList->setSyntacticForm(IList); |
| 688 | } |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 689 | |
| 690 | unsigned Index = 0, StructuredIndex = 0; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 691 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 692 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 693 | if (!VerifyOnly) { |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 694 | QualType ExprTy = T; |
| 695 | if (!ExprTy->isArrayType()) |
| 696 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 697 | IList->setType(ExprTy); |
| 698 | StructuredList->setType(ExprTy); |
| 699 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 700 | if (hadError) |
| 701 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 702 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 703 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 704 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 705 | if (VerifyOnly) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 706 | if (SemaRef.getLangOpts().CPlusPlus || |
| 707 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 708 | IList->getType()->isVectorType())) { |
| 709 | hadError = true; |
| 710 | } |
| 711 | return; |
| 712 | } |
| 713 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 714 | if (StructuredIndex == 1 && |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 715 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 716 | SIF_None) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 717 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 718 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 719 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 720 | hadError = true; |
| 721 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 722 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 723 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 724 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 725 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 726 | // Don't complain for incomplete types, since we'll get an error |
| 727 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 728 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 730 | CurrentObjectType->isArrayType()? 0 : |
| 731 | CurrentObjectType->isVectorType()? 1 : |
| 732 | CurrentObjectType->isScalarType()? 2 : |
| 733 | CurrentObjectType->isUnionType()? 3 : |
| 734 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 735 | |
| 736 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 737 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 738 | DK = diag::err_excess_initializers; |
| 739 | hadError = true; |
| 740 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 741 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 742 | DK = diag::err_excess_initializers; |
| 743 | hadError = true; |
| 744 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 746 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 747 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 750 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 751 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 752 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 753 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 754 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 755 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 756 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 759 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 760 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 762 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 763 | unsigned &Index, |
| 764 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 765 | unsigned &StructuredIndex, |
| 766 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 767 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 768 | // Explicitly braced initializer for complex type can be real+imaginary |
| 769 | // parts. |
| 770 | CheckComplexType(Entity, IList, DeclType, Index, |
| 771 | StructuredList, StructuredIndex); |
| 772 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 773 | CheckScalarType(Entity, IList, DeclType, Index, |
| 774 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 775 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 776 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 777 | StructuredList, StructuredIndex); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 778 | } else if (DeclType->isRecordType()) { |
| 779 | assert(DeclType->isAggregateType() && |
| 780 | "non-aggregate records should be handed in CheckSubElementType"); |
| 781 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 782 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 783 | SubobjectIsDesignatorContext, Index, |
| 784 | StructuredList, StructuredIndex, |
| 785 | TopLevelObject); |
| 786 | } else if (DeclType->isArrayType()) { |
| 787 | llvm::APSInt Zero( |
| 788 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 789 | false); |
| 790 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 791 | SubobjectIsDesignatorContext, Index, |
| 792 | StructuredList, StructuredIndex); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 793 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 794 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 795 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 796 | if (!VerifyOnly) |
| 797 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 798 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 799 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 800 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 801 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 802 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 803 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 804 | if (!VerifyOnly) |
| 805 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 806 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 807 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 808 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 809 | if (!VerifyOnly) |
| 810 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 811 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 812 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 816 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 817 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 818 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 819 | unsigned &Index, |
| 820 | InitListExpr *StructuredList, |
| 821 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 822 | Expr *expr = IList->getInit(Index); |
Richard Smith | 6242a45 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 823 | |
| 824 | if (ElemType->isReferenceType()) |
| 825 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 826 | StructuredList, StructuredIndex); |
| 827 | |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 828 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 829 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 830 | InitListExpr *InnerStructuredList |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 831 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 832 | StructuredList, StructuredIndex, |
| 833 | SubInitList->getSourceRange()); |
Richard Smith | b9bf312 | 2013-09-20 20:10:22 +0000 | [diff] [blame] | 834 | CheckExplicitInitList(Entity, SubInitList, ElemType, |
| 835 | InnerStructuredList); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 836 | ++StructuredIndex; |
| 837 | ++Index; |
| 838 | return; |
| 839 | } |
| 840 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 841 | "non-aggregate records are only possible in C++"); |
| 842 | // C++ initialization is handled later. |
| 843 | } |
| 844 | |
Eli Friedman | 48a2a3a | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 845 | // FIXME: Need to handle atomic aggregate types with implicit init lists. |
| 846 | if (ElemType->isScalarType() || ElemType->isAtomicType()) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 847 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 848 | StructuredList, StructuredIndex); |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 849 | |
Eli Friedman | 48a2a3a | 2013-08-19 22:12:56 +0000 | [diff] [blame] | 850 | assert((ElemType->isRecordType() || ElemType->isVectorType() || |
| 851 | ElemType->isArrayType()) && "Unexpected type"); |
| 852 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 853 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 854 | // arrayType can be incomplete if we're initializing a flexible |
| 855 | // array member. There's nothing we can do with the completed |
| 856 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 857 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 858 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 859 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 860 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 861 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 862 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 863 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 864 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 865 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 866 | |
| 867 | // Fall through for subaggregate initialization. |
| 868 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 869 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 870 | // C++ [dcl.init.aggr]p12: |
| 871 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 872 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 873 | // an initializer-list. If the initializer can initialize a |
| 874 | // member, the member is initialized. [...] |
| 875 | |
| 876 | // FIXME: Better EqualLoc? |
| 877 | InitializationKind Kind = |
| 878 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 879 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 880 | |
| 881 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 882 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 883 | ExprResult Result = |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 884 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 885 | if (Result.isInvalid()) |
| 886 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 887 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 888 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 889 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 890 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 891 | ++Index; |
| 892 | return; |
| 893 | } |
| 894 | |
| 895 | // Fall through for subaggregate initialization |
| 896 | } else { |
| 897 | // C99 6.7.8p13: |
| 898 | // |
| 899 | // The initializer for a structure or union object that has |
| 900 | // automatic storage duration shall be either an initializer |
| 901 | // list as described below, or a single expression that has |
| 902 | // compatible structure or union type. In the latter case, the |
| 903 | // initial value of the object, including unnamed members, is |
| 904 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 905 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 906 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 907 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 908 | !VerifyOnly) |
Eli Friedman | 08f0bbc | 2013-09-17 04:07:04 +0000 | [diff] [blame] | 909 | != Sema::Incompatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 910 | if (ExprRes.isInvalid()) |
| 911 | hadError = true; |
| 912 | else { |
| 913 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 914 | if (ExprRes.isInvalid()) |
| 915 | hadError = true; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 916 | } |
| 917 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 918 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 919 | ++Index; |
| 920 | return; |
| 921 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 922 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 923 | // Fall through for subaggregate initialization |
| 924 | } |
| 925 | |
| 926 | // C++ [dcl.init.aggr]p12: |
| 927 | // |
| 928 | // [...] Otherwise, if the member is itself a non-empty |
| 929 | // subaggregate, brace elision is assumed and the initializer is |
| 930 | // considered for the initialization of the first member of |
| 931 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 932 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 933 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 934 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 935 | StructuredIndex); |
| 936 | ++StructuredIndex; |
| 937 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 938 | if (!VerifyOnly) { |
| 939 | // We cannot initialize this element, so let |
| 940 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 941 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 942 | SemaRef.Owned(expr), |
| 943 | /*TopLevelOfInitList=*/true); |
| 944 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 945 | hadError = true; |
| 946 | ++Index; |
| 947 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 948 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 951 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 952 | InitListExpr *IList, QualType DeclType, |
| 953 | unsigned &Index, |
| 954 | InitListExpr *StructuredList, |
| 955 | unsigned &StructuredIndex) { |
| 956 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 957 | |
| 958 | // As an extension, clang supports complex initializers, which initialize |
| 959 | // a complex number component-wise. When an explicit initializer list for |
| 960 | // a complex number contains two two initializers, this extension kicks in: |
| 961 | // it exepcts the initializer list to contain two elements convertible to |
| 962 | // the element type of the complex type. The first element initializes |
| 963 | // the real part, and the second element intitializes the imaginary part. |
| 964 | |
| 965 | if (IList->getNumInits() != 2) |
| 966 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 967 | StructuredIndex); |
| 968 | |
| 969 | // This is an extension in C. (The builtin _Complex type does not exist |
| 970 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 971 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 972 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 973 | << IList->getSourceRange(); |
| 974 | |
| 975 | // Initialize the complex number. |
| 976 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 977 | InitializedEntity ElementEntity = |
| 978 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 979 | |
| 980 | for (unsigned i = 0; i < 2; ++i) { |
| 981 | ElementEntity.setElementIndex(Index); |
| 982 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 983 | StructuredList, StructuredIndex); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 988 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 989 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 990 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 991 | InitListExpr *StructuredList, |
| 992 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 993 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 994 | if (!VerifyOnly) |
| 995 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 996 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 997 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 998 | diag::err_empty_scalar_initializer) |
| 999 | << IList->getSourceRange(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1000 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1001 | ++Index; |
| 1002 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1003 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1004 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1005 | |
| 1006 | Expr *expr = IList->getInit(Index); |
| 1007 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1008 | if (!VerifyOnly) |
| 1009 | SemaRef.Diag(SubIList->getLocStart(), |
| 1010 | diag::warn_many_braces_around_scalar_init) |
| 1011 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1012 | |
| 1013 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1014 | StructuredIndex); |
| 1015 | return; |
| 1016 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1017 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1018 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1019 | diag::err_designator_for_scalar_init) |
| 1020 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1021 | hadError = true; |
| 1022 | ++Index; |
| 1023 | ++StructuredIndex; |
| 1024 | return; |
| 1025 | } |
| 1026 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1027 | if (VerifyOnly) { |
| 1028 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1029 | hadError = true; |
| 1030 | ++Index; |
| 1031 | return; |
| 1032 | } |
| 1033 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1034 | ExprResult Result = |
| 1035 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1036 | SemaRef.Owned(expr), |
| 1037 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1038 | |
| 1039 | Expr *ResultExpr = 0; |
| 1040 | |
| 1041 | if (Result.isInvalid()) |
| 1042 | hadError = true; // types weren't compatible. |
| 1043 | else { |
| 1044 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1045 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1046 | if (ResultExpr != expr) { |
| 1047 | // The type was promoted, update initializer list. |
| 1048 | IList->setInit(Index, ResultExpr); |
| 1049 | } |
| 1050 | } |
| 1051 | if (hadError) |
| 1052 | ++StructuredIndex; |
| 1053 | else |
| 1054 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1055 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1058 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1059 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1060 | unsigned &Index, |
| 1061 | InitListExpr *StructuredList, |
| 1062 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1063 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1064 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1065 | // general, it would be useful to pass location information down the stack, |
| 1066 | // so that we know the location (or decl) of the "current object" being |
| 1067 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1068 | if (!VerifyOnly) |
| 1069 | SemaRef.Diag(IList->getLocStart(), |
| 1070 | diag::err_init_reference_member_uninitialized) |
| 1071 | << DeclType |
| 1072 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1073 | hadError = true; |
| 1074 | ++Index; |
| 1075 | ++StructuredIndex; |
| 1076 | return; |
| 1077 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1078 | |
| 1079 | Expr *expr = IList->getInit(Index); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1080 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1081 | if (!VerifyOnly) |
| 1082 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1083 | << DeclType << IList->getSourceRange(); |
| 1084 | hadError = true; |
| 1085 | ++Index; |
| 1086 | ++StructuredIndex; |
| 1087 | return; |
| 1088 | } |
| 1089 | |
| 1090 | if (VerifyOnly) { |
| 1091 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1092 | hadError = true; |
| 1093 | ++Index; |
| 1094 | return; |
| 1095 | } |
| 1096 | |
| 1097 | ExprResult Result = |
| 1098 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1099 | SemaRef.Owned(expr), |
| 1100 | /*TopLevelOfInitList=*/true); |
| 1101 | |
| 1102 | if (Result.isInvalid()) |
| 1103 | hadError = true; |
| 1104 | |
| 1105 | expr = Result.takeAs<Expr>(); |
| 1106 | IList->setInit(Index, expr); |
| 1107 | |
| 1108 | if (hadError) |
| 1109 | ++StructuredIndex; |
| 1110 | else |
| 1111 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1112 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1115 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1116 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1117 | unsigned &Index, |
| 1118 | InitListExpr *StructuredList, |
| 1119 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1120 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1121 | unsigned maxElements = VT->getNumElements(); |
| 1122 | unsigned numEltsInit = 0; |
| 1123 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1124 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1125 | if (Index >= IList->getNumInits()) { |
| 1126 | // Make sure the element type can be value-initialized. |
| 1127 | if (VerifyOnly) |
| 1128 | CheckValueInitializable( |
| 1129 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1130 | return; |
| 1131 | } |
| 1132 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1133 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1134 | // If the initializing element is a vector, try to copy-initialize |
| 1135 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1136 | Expr *Init = IList->getInit(Index); |
| 1137 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1138 | if (VerifyOnly) { |
| 1139 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1140 | hadError = true; |
| 1141 | ++Index; |
| 1142 | return; |
| 1143 | } |
| 1144 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1145 | ExprResult Result = |
| 1146 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1147 | SemaRef.Owned(Init), |
| 1148 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1149 | |
| 1150 | Expr *ResultExpr = 0; |
| 1151 | if (Result.isInvalid()) |
| 1152 | hadError = true; // types weren't compatible. |
| 1153 | else { |
| 1154 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1155 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1156 | if (ResultExpr != Init) { |
| 1157 | // The type was promoted, update initializer list. |
| 1158 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1159 | } |
| 1160 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1161 | if (hadError) |
| 1162 | ++StructuredIndex; |
| 1163 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1164 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1165 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1166 | ++Index; |
| 1167 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1168 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1169 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1170 | InitializedEntity ElementEntity = |
| 1171 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1172 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1173 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1174 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1175 | if (Index >= IList->getNumInits()) { |
| 1176 | if (VerifyOnly) |
| 1177 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1178 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1179 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1180 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1181 | ElementEntity.setElementIndex(Index); |
| 1182 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1183 | StructuredList, StructuredIndex); |
| 1184 | } |
| 1185 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1186 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1187 | |
| 1188 | InitializedEntity ElementEntity = |
| 1189 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
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 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1192 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1193 | // Don't attempt to go past the end of the init list |
| 1194 | if (Index >= IList->getNumInits()) |
| 1195 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1196 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1197 | ElementEntity.setElementIndex(Index); |
| 1198 | |
| 1199 | QualType IType = IList->getInit(Index)->getType(); |
| 1200 | if (!IType->isVectorType()) { |
| 1201 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1202 | StructuredList, StructuredIndex); |
| 1203 | ++numEltsInit; |
| 1204 | } else { |
| 1205 | QualType VecType; |
| 1206 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1207 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1208 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1209 | if (IType->isExtVectorType()) |
| 1210 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1211 | else |
| 1212 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1213 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1214 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1215 | StructuredList, StructuredIndex); |
| 1216 | numEltsInit += numIElts; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1221 | if (numEltsInit != maxElements) { |
| 1222 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1223 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1224 | diag::err_vector_incorrect_num_initializers) |
| 1225 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1226 | hadError = true; |
| 1227 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1230 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1231 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1232 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1233 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1234 | unsigned &Index, |
| 1235 | InitListExpr *StructuredList, |
| 1236 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1237 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1238 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1239 | // Check for the special-case of initializing an array with a string. |
| 1240 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1241 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1242 | SIF_None) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1243 | // We place the string literal directly into the resulting |
| 1244 | // initializer list. This is the only place where the structure |
| 1245 | // of the structured initializer list doesn't match exactly, |
| 1246 | // because doing so would involve allocating one character |
| 1247 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1248 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1249 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1250 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1251 | IList->getInit(Index)); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1252 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1253 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1254 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1255 | return; |
| 1256 | } |
| 1257 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1258 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1259 | // Check for VLAs; in standard C it would be possible to check this |
| 1260 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1261 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1262 | if (!VerifyOnly) |
| 1263 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1264 | diag::err_variable_object_no_init) |
| 1265 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1266 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1267 | ++Index; |
| 1268 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1269 | return; |
| 1270 | } |
| 1271 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1272 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1273 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1274 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1275 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1276 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1277 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1278 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1279 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1280 | maxElementsKnown = true; |
| 1281 | } |
| 1282 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1283 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1284 | while (Index < IList->getNumInits()) { |
| 1285 | Expr *Init = IList->getInit(Index); |
| 1286 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1287 | // If we're not the subobject that matches up with the '{' for |
| 1288 | // the designator, we shouldn't be handling the |
| 1289 | // designator. Return immediately. |
| 1290 | if (!SubobjectIsDesignatorContext) |
| 1291 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1292 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1293 | // Handle this designated initializer. elementIndex will be |
| 1294 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1295 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1296 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1297 | StructuredList, StructuredIndex, true, |
| 1298 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1299 | hadError = true; |
| 1300 | continue; |
| 1301 | } |
| 1302 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1303 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1304 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1305 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1306 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1307 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1308 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1309 | // If the array is of incomplete type, keep track of the number of |
| 1310 | // elements in the initializer. |
| 1311 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1312 | maxElements = elementIndex; |
| 1313 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1314 | continue; |
| 1315 | } |
| 1316 | |
| 1317 | // If we know the maximum number of elements, and we've already |
| 1318 | // hit it, stop consuming elements in the initializer list. |
| 1319 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1320 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1321 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1322 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1323 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1324 | Entity); |
| 1325 | // Check this element. |
| 1326 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1327 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1328 | ++elementIndex; |
| 1329 | |
| 1330 | // If the array is of incomplete type, keep track of the number of |
| 1331 | // elements in the initializer. |
| 1332 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1333 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1334 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1335 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1336 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1337 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1338 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1339 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1340 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1341 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1342 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1343 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1344 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1345 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1347 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1348 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1349 | if (!hadError && VerifyOnly) { |
| 1350 | // Check if there are any members of the array that get value-initialized. |
| 1351 | // If so, check if doing that is possible. |
| 1352 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1353 | if (maxElementsKnown && elementIndex < maxElements) |
| 1354 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1355 | SemaRef.Context, 0, Entity)); |
| 1356 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1359 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1360 | Expr *InitExpr, |
| 1361 | FieldDecl *Field, |
| 1362 | bool TopLevelObject) { |
| 1363 | // Handle GNU flexible array initializers. |
| 1364 | unsigned FlexArrayDiag; |
| 1365 | if (isa<InitListExpr>(InitExpr) && |
| 1366 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1367 | // Empty flexible array init always allowed as an extension |
| 1368 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1369 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1370 | // Disallow flexible array init in C++; it is not required for gcc |
| 1371 | // compatibility, and it needs work to IRGen correctly in general. |
| 1372 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1373 | } else if (!TopLevelObject) { |
| 1374 | // Disallow flexible array init on non-top-level object |
| 1375 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1376 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1377 | // Disallow flexible array init on anything which is not a variable. |
| 1378 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1379 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1380 | // Disallow flexible array init on local variables. |
| 1381 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1382 | } else { |
| 1383 | // Allow other cases. |
| 1384 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1385 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1386 | |
| 1387 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1388 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1389 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1390 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1391 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1392 | << Field; |
| 1393 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1394 | |
| 1395 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1396 | } |
| 1397 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1398 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1399 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1400 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1401 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1402 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1403 | unsigned &Index, |
| 1404 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1405 | unsigned &StructuredIndex, |
| 1406 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1407 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1408 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1409 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1410 | // confusion, we forgo checking the intializer for the entire record. |
| 1411 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 72ab277 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1412 | // Assume it was supposed to consume a single initializer. |
| 1413 | ++Index; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1414 | hadError = true; |
| 1415 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1417 | |
| 1418 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1419 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1420 | |
| 1421 | // If there's a default initializer, use it. |
| 1422 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1423 | if (VerifyOnly) |
| 1424 | return; |
| 1425 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1426 | Field != FieldEnd; ++Field) { |
| 1427 | if (Field->hasInClassInitializer()) { |
| 1428 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1429 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1430 | return; |
| 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | // Value-initialize the first named member of the union. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1436 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1437 | Field != FieldEnd; ++Field) { |
| 1438 | if (Field->getDeclName()) { |
| 1439 | if (VerifyOnly) |
| 1440 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1441 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1442 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1443 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1444 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | return; |
| 1448 | } |
| 1449 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1450 | // If structDecl is a forward declaration, this loop won't do |
| 1451 | // anything except look at designated initializers; That's okay, |
| 1452 | // because an error should get printed out elsewhere. It might be |
| 1453 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1454 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1455 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1456 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1457 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1458 | while (Index < IList->getNumInits()) { |
| 1459 | Expr *Init = IList->getInit(Index); |
| 1460 | |
| 1461 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1462 | // If we're not the subobject that matches up with the '{' for |
| 1463 | // the designator, we shouldn't be handling the |
| 1464 | // designator. Return immediately. |
| 1465 | if (!SubobjectIsDesignatorContext) |
| 1466 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1467 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1468 | // Handle this designated initializer. Field will be updated to |
| 1469 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1470 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1471 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1472 | StructuredList, StructuredIndex, |
| 1473 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1474 | hadError = true; |
| 1475 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1476 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1477 | |
| 1478 | // Disable check for missing fields when designators are used. |
| 1479 | // This matches gcc behaviour. |
| 1480 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1481 | continue; |
| 1482 | } |
| 1483 | |
| 1484 | if (Field == FieldEnd) { |
| 1485 | // We've run out of fields. We're done. |
| 1486 | break; |
| 1487 | } |
| 1488 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1489 | // We've already initialized a member of a union. We're done. |
| 1490 | if (InitializedSomething && DeclType->isUnionType()) |
| 1491 | break; |
| 1492 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1493 | // If we've hit the flexible array member at the end, we're done. |
| 1494 | if (Field->getType()->isIncompleteArrayType()) |
| 1495 | break; |
| 1496 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1497 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1498 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1499 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1500 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1501 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1502 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1503 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1504 | bool InvalidUse; |
| 1505 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1506 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1507 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1508 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1509 | IList->getInit(Index)->getLocStart()); |
| 1510 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1511 | ++Index; |
| 1512 | ++Field; |
| 1513 | hadError = true; |
| 1514 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1515 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1516 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1517 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1518 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1519 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1520 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1521 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1522 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1523 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1524 | // Initialize the first field within the union. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1525 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1526 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1527 | |
| 1528 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1529 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1530 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1531 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1532 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1533 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1534 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1535 | // It is possible we have one or more unnamed bitfields remaining. |
| 1536 | // Find first (if any) named field and emit warning. |
| 1537 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1538 | it != end; ++it) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1539 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1540 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1541 | diag::warn_missing_field_initializers) << it->getName(); |
| 1542 | break; |
| 1543 | } |
| 1544 | } |
| 1545 | } |
| 1546 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1547 | // Check that any remaining fields can be value-initialized. |
| 1548 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1549 | !Field->getType()->isIncompleteArrayType()) { |
| 1550 | // FIXME: Should check for holes left by designated initializers too. |
| 1551 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1552 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1553 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1554 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1555 | } |
| 1556 | } |
| 1557 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1559 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1560 | return; |
| 1561 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1562 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1563 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1564 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1565 | ++Index; |
| 1566 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1569 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1570 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1571 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1572 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1573 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1574 | StructuredList, StructuredIndex); |
| 1575 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1576 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1577 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1578 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1579 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1580 | /// \brief Expand a field designator that refers to a member of an |
| 1581 | /// anonymous struct or union into a series of field designators that |
| 1582 | /// refers to the field within the appropriate subobject. |
| 1583 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1584 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | DesignatedInitExpr *DIE, |
| 1586 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1587 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1588 | typedef DesignatedInitExpr::Designator Designator; |
| 1589 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1590 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1591 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1592 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1593 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1594 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1595 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1596 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1597 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1598 | else |
| 1599 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1600 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1601 | assert(isa<FieldDecl>(*PI)); |
| 1602 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | // Expand the current designator into the set of replacement |
| 1606 | // designators, so we have a full subobject path down to where the |
| 1607 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1608 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1609 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1610 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1612 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1613 | /// corresponds to FieldName. |
| 1614 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1615 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1616 | if (!FieldName) |
| 1617 | return 0; |
| 1618 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1619 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1620 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1621 | while (IndirectFieldDecl *IF = |
| 1622 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1623 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1624 | return IF; |
| 1625 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1626 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1627 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1630 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1631 | DesignatedInitExpr *DIE) { |
| 1632 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1633 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1634 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1635 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1636 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1637 | DIE->size(), IndexExprs, |
| 1638 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1639 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1640 | } |
| 1641 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1642 | namespace { |
| 1643 | |
| 1644 | // Callback to only accept typo corrections that are for field members of |
| 1645 | // the given struct or union. |
| 1646 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1647 | public: |
| 1648 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1649 | : Record(RD) {} |
| 1650 | |
| 1651 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1652 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1653 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1654 | } |
| 1655 | |
| 1656 | private: |
| 1657 | RecordDecl *Record; |
| 1658 | }; |
| 1659 | |
| 1660 | } |
| 1661 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1662 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1663 | /// |
| 1664 | /// Determines whether the designated initializer @p DIE, which |
| 1665 | /// resides at the given @p Index within the initializer list @p |
| 1666 | /// IList, is well-formed for a current object of type @p DeclType |
| 1667 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1669 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1670 | /// |
| 1671 | /// @param IList The initializer list in which this designated |
| 1672 | /// initializer occurs. |
| 1673 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1674 | /// @param DIE The designated initializer expression. |
| 1675 | /// |
| 1676 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1677 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1678 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1679 | /// into which the designation in @p DIE should refer. |
| 1680 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1681 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1682 | /// a field, this will be set to the field declaration corresponding |
| 1683 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1684 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1685 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1686 | /// DIE is an array designator or GNU array-range designator, this |
| 1687 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1688 | /// |
| 1689 | /// @param Index Index into @p IList where the designated initializer |
| 1690 | /// @p DIE occurs. |
| 1691 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1692 | /// @param StructuredList The initializer list expression that |
| 1693 | /// describes all of the subobject initializers in the order they'll |
| 1694 | /// actually be initialized. |
| 1695 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1696 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1698 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1699 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1700 | DesignatedInitExpr *DIE, |
| 1701 | unsigned DesigIdx, |
| 1702 | QualType &CurrentObjectType, |
| 1703 | RecordDecl::field_iterator *NextField, |
| 1704 | llvm::APSInt *NextElementIndex, |
| 1705 | unsigned &Index, |
| 1706 | InitListExpr *StructuredList, |
| 1707 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1708 | bool FinishSubobjectInit, |
| 1709 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1710 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1711 | // Check the actual initialization for the designated object type. |
| 1712 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1713 | |
| 1714 | // Temporarily remove the designator expression from the |
| 1715 | // initializer list that the child calls see, so that we don't try |
| 1716 | // to re-process the designator. |
| 1717 | unsigned OldIndex = Index; |
| 1718 | IList->setInit(OldIndex, DIE->getInit()); |
| 1719 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1720 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1721 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1722 | |
| 1723 | // Restore the designated initializer expression in the syntactic |
| 1724 | // form of the initializer list. |
| 1725 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1726 | DIE->setInit(IList->getInit(OldIndex)); |
| 1727 | IList->setInit(OldIndex, DIE); |
| 1728 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1729 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1732 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1733 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1734 | if (!VerifyOnly) { |
| 1735 | assert((IsFirstDesignator || StructuredList) && |
| 1736 | "Need a non-designated initializer list to start from"); |
| 1737 | |
| 1738 | // Determine the structural initializer list that corresponds to the |
| 1739 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1740 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1741 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1742 | StructuredList, StructuredIndex, |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1743 | SourceRange(D->getLocStart(), |
| 1744 | DIE->getLocEnd())); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1745 | assert(StructuredList && "Expected a structured initializer list"); |
| 1746 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1747 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1748 | if (D->isFieldDesignator()) { |
| 1749 | // C99 6.7.8p7: |
| 1750 | // |
| 1751 | // If a designator has the form |
| 1752 | // |
| 1753 | // . identifier |
| 1754 | // |
| 1755 | // then the current object (defined below) shall have |
| 1756 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1757 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1758 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1759 | if (!RT) { |
| 1760 | SourceLocation Loc = D->getDotLoc(); |
| 1761 | if (Loc.isInvalid()) |
| 1762 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1763 | if (!VerifyOnly) |
| 1764 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1765 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1766 | ++Index; |
| 1767 | return true; |
| 1768 | } |
| 1769 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1770 | // Note: we perform a linear search of the fields here, despite |
| 1771 | // the fact that we have a faster lookup method, because we always |
| 1772 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1773 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1774 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1775 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1777 | Field = RT->getDecl()->field_begin(), |
| 1778 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1779 | for (; Field != FieldEnd; ++Field) { |
| 1780 | if (Field->isUnnamedBitfield()) |
| 1781 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1782 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1783 | // If we find a field representing an anonymous field, look in the |
| 1784 | // IndirectFieldDecl that follow for the designated initializer. |
| 1785 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1786 | if (IndirectFieldDecl *IF = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1787 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1788 | // In verify mode, don't modify the original. |
| 1789 | if (VerifyOnly) |
| 1790 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1791 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1792 | D = DIE->getDesignator(DesigIdx); |
| 1793 | break; |
| 1794 | } |
| 1795 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1796 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1797 | break; |
| 1798 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1799 | break; |
| 1800 | |
| 1801 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1804 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1805 | if (VerifyOnly) { |
| 1806 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1807 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1808 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1810 | // There was no normal field in the struct with the designated |
| 1811 | // name. Perform another lookup for this name, which may find |
| 1812 | // something that we can't designate (e.g., a member function), |
| 1813 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1814 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1815 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1816 | FieldDecl *ReplacementField = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1817 | if (Lookup.empty()) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1818 | // Name lookup didn't find anything. Determine whether this |
| 1819 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1820 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1821 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1822 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1823 | Sema::LookupMemberName, /*Scope=*/ 0, /*SS=*/ 0, Validator, |
| 1824 | RT->getDecl())) { |
| 1825 | SemaRef.diagnoseTypo( |
| 1826 | Corrected, |
| 1827 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
| 1828 | << FieldName << CurrentObjectType); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1829 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1830 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1831 | } else { |
| 1832 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1833 | << FieldName << CurrentObjectType; |
| 1834 | ++Index; |
| 1835 | return true; |
| 1836 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1837 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1838 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1839 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1840 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1841 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1842 | << FieldName; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1843 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1844 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1845 | ++Index; |
| 1846 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1847 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1848 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1849 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1850 | // The replacement field comes from typo correction; find it |
| 1851 | // in the list of fields. |
| 1852 | FieldIndex = 0; |
| 1853 | Field = RT->getDecl()->field_begin(); |
| 1854 | for (; Field != FieldEnd; ++Field) { |
| 1855 | if (Field->isUnnamedBitfield()) |
| 1856 | continue; |
| 1857 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1858 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1859 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1860 | break; |
| 1861 | |
| 1862 | ++FieldIndex; |
| 1863 | } |
| 1864 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1865 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1866 | |
| 1867 | // All of the fields of a union are located at the same place in |
| 1868 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1869 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1870 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1871 | if (!VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1872 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1873 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1874 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1875 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1876 | bool InvalidUse; |
| 1877 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1878 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1879 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1880 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1881 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1882 | ++Index; |
| 1883 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1884 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1885 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1886 | if (!VerifyOnly) { |
| 1887 | // Update the designator with the field declaration. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1888 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1889 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1890 | // Make sure that our non-designated initializer list has space |
| 1891 | // for a subobject corresponding to this field. |
| 1892 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1893 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1894 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1895 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1896 | // This designator names a flexible array member. |
| 1897 | if (Field->getType()->isIncompleteArrayType()) { |
| 1898 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1899 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1900 | // We can't designate an object within the flexible array |
| 1901 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1902 | if (!VerifyOnly) { |
| 1903 | DesignatedInitExpr::Designator *NextD |
| 1904 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1905 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1906 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1907 | << SourceRange(NextD->getLocStart(), |
| 1908 | DIE->getLocEnd()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1909 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1910 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1911 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1912 | Invalid = true; |
| 1913 | } |
| 1914 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1915 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1916 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1917 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1918 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1919 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1920 | diag::err_flexible_array_init_needs_braces) |
| 1921 | << DIE->getInit()->getSourceRange(); |
| 1922 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1923 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1924 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1925 | Invalid = true; |
| 1926 | } |
| 1927 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1928 | // Check GNU flexible array initializer. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1929 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1930 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1931 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1932 | |
| 1933 | if (Invalid) { |
| 1934 | ++Index; |
| 1935 | return true; |
| 1936 | } |
| 1937 | |
| 1938 | // Initialize the array. |
| 1939 | bool prevHadError = hadError; |
| 1940 | unsigned newStructuredIndex = FieldIndex; |
| 1941 | unsigned OldIndex = Index; |
| 1942 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1943 | |
| 1944 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1945 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1946 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1947 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1948 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1949 | IList->setInit(OldIndex, DIE); |
| 1950 | if (hadError && !prevHadError) { |
| 1951 | ++Field; |
| 1952 | ++FieldIndex; |
| 1953 | if (NextField) |
| 1954 | *NextField = Field; |
| 1955 | StructuredIndex = FieldIndex; |
| 1956 | return true; |
| 1957 | } |
| 1958 | } else { |
| 1959 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1960 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1961 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1962 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1963 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1964 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1965 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1966 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1967 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1968 | true, false)) |
| 1969 | return true; |
| 1970 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1971 | |
| 1972 | // Find the position of the next field to be initialized in this |
| 1973 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1974 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1975 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1976 | |
| 1977 | // If this the first designator, our caller will continue checking |
| 1978 | // the rest of this struct/class/union subobject. |
| 1979 | if (IsFirstDesignator) { |
| 1980 | if (NextField) |
| 1981 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1982 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1983 | return false; |
| 1984 | } |
| 1985 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1986 | if (!FinishSubobjectInit) |
| 1987 | return false; |
| 1988 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1989 | // We've already initialized something in the union; we're done. |
| 1990 | if (RT->getDecl()->isUnion()) |
| 1991 | return hadError; |
| 1992 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1993 | // Check the remaining fields within this class/struct/union subobject. |
| 1994 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1995 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1996 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1997 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1998 | return hadError && !prevHadError; |
| 1999 | } |
| 2000 | |
| 2001 | // C99 6.7.8p6: |
| 2002 | // |
| 2003 | // If a designator has the form |
| 2004 | // |
| 2005 | // [ constant-expression ] |
| 2006 | // |
| 2007 | // then the current object (defined below) shall have array |
| 2008 | // type and the expression shall be an integer constant |
| 2009 | // expression. If the array is of unknown size, any |
| 2010 | // nonnegative value is valid. |
| 2011 | // |
| 2012 | // Additionally, cope with the GNU extension that permits |
| 2013 | // designators of the form |
| 2014 | // |
| 2015 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2016 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2017 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2018 | if (!VerifyOnly) |
| 2019 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2020 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2021 | ++Index; |
| 2022 | return true; |
| 2023 | } |
| 2024 | |
| 2025 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2026 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2027 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2028 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2029 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2030 | DesignatedEndIndex = DesignatedStartIndex; |
| 2031 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2032 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2033 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2035 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2037 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2038 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2039 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2040 | // Codegen can't handle evaluating array range designators that have side |
| 2041 | // effects, because we replicate the AST value for each initialized element. |
| 2042 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2043 | // elements with something that has a side effect, so codegen can emit an |
| 2044 | // "error unsupported" error instead of miscompiling the app. |
| 2045 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2046 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2047 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2050 | if (isa<ConstantArrayType>(AT)) { |
| 2051 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2052 | DesignatedStartIndex |
| 2053 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2054 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2055 | DesignatedEndIndex |
| 2056 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2057 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2058 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2059 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2060 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2061 | diag::err_array_designator_too_large) |
| 2062 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2063 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2064 | ++Index; |
| 2065 | return true; |
| 2066 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2067 | } else { |
| 2068 | // Make sure the bit-widths and signedness match. |
| 2069 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2070 | DesignatedEndIndex |
| 2071 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2072 | else if (DesignatedStartIndex.getBitWidth() < |
| 2073 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2074 | DesignatedStartIndex |
| 2075 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2076 | DesignatedStartIndex.setIsUnsigned(true); |
| 2077 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2078 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2079 | |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2080 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2081 | // We're modifying a string literal init; we have to decompose the string |
| 2082 | // so we can modify the individual characters. |
| 2083 | ASTContext &Context = SemaRef.Context; |
| 2084 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2085 | |
| 2086 | // Compute the character type |
| 2087 | QualType CharTy = AT->getElementType(); |
| 2088 | |
| 2089 | // Compute the type of the integer literals. |
| 2090 | QualType PromotedCharTy = CharTy; |
| 2091 | if (CharTy->isPromotableIntegerType()) |
| 2092 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2093 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2094 | |
| 2095 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2096 | // Get the length of the string. |
| 2097 | uint64_t StrLen = SL->getLength(); |
| 2098 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2099 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2100 | StructuredList->resizeInits(Context, StrLen); |
| 2101 | |
| 2102 | // Build a literal for each character in the string, and put them into |
| 2103 | // the init list. |
| 2104 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2105 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2106 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 81359b0 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2107 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2108 | if (CharTy != PromotedCharTy) |
| 2109 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
| 2110 | Init, 0, VK_RValue); |
| 2111 | StructuredList->updateInit(Context, i, Init); |
| 2112 | } |
| 2113 | } else { |
| 2114 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2115 | std::string Str; |
| 2116 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2117 | |
| 2118 | // Get the length of the string. |
| 2119 | uint64_t StrLen = Str.size(); |
| 2120 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2121 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2122 | StructuredList->resizeInits(Context, StrLen); |
| 2123 | |
| 2124 | // Build a literal for each character in the string, and put them into |
| 2125 | // the init list. |
| 2126 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2127 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2128 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 81359b0 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2129 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2130 | if (CharTy != PromotedCharTy) |
| 2131 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
| 2132 | Init, 0, VK_RValue); |
| 2133 | StructuredList->updateInit(Context, i, Init); |
| 2134 | } |
| 2135 | } |
| 2136 | } |
| 2137 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2138 | // Make sure that our non-designated initializer list has space |
| 2139 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2140 | if (!VerifyOnly && |
| 2141 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2143 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2144 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2145 | // Repeatedly perform subobject initializations in the range |
| 2146 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2147 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2148 | // Move to the next designator |
| 2149 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2150 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2151 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2152 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2153 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2154 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2155 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2156 | // Recurse to check later designated subobjects. |
| 2157 | QualType ElementType = AT->getElementType(); |
| 2158 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2159 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2160 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2161 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 2162 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2163 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2164 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2165 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2166 | return true; |
| 2167 | |
| 2168 | // Move to the next index in the array that we'll be initializing. |
| 2169 | ++DesignatedStartIndex; |
| 2170 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2171 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2172 | |
| 2173 | // If this the first designator, our caller will continue checking |
| 2174 | // the rest of this array subobject. |
| 2175 | if (IsFirstDesignator) { |
| 2176 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2177 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2178 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2179 | return false; |
| 2180 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2181 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2182 | if (!FinishSubobjectInit) |
| 2183 | return false; |
| 2184 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2185 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2186 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2187 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2188 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2189 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2190 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2193 | // Get the structured initializer list for a subobject of type |
| 2194 | // @p CurrentObjectType. |
| 2195 | InitListExpr * |
| 2196 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2197 | QualType CurrentObjectType, |
| 2198 | InitListExpr *StructuredList, |
| 2199 | unsigned StructuredIndex, |
| 2200 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2201 | if (VerifyOnly) |
| 2202 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2203 | Expr *ExistingInit = 0; |
| 2204 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2205 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2206 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2207 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2208 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2209 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2210 | return Result; |
| 2211 | |
| 2212 | if (ExistingInit) { |
| 2213 | // We are creating an initializer list that initializes the |
| 2214 | // subobjects of the current object, but there was already an |
| 2215 | // initialization that completely initialized the current |
| 2216 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2218 | // struct X { int a, b; }; |
| 2219 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2220 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2221 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2222 | // designated initializer re-initializes the whole |
| 2223 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2225 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2226 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2227 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2228 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2229 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2230 | << ExistingInit->getSourceRange(); |
| 2231 | } |
| 2232 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2234 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2235 | InitRange.getBegin(), None, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2236 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2237 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2238 | QualType ResultType = CurrentObjectType; |
| 2239 | if (!ResultType->isArrayType()) |
| 2240 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2241 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2242 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2243 | // Pre-allocate storage for the structured initializer list. |
| 2244 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2245 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2246 | bool GotNumInits = false; |
| 2247 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2248 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2249 | GotNumInits = true; |
| 2250 | } else if (Index < IList->getNumInits()) { |
| 2251 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2252 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2253 | GotNumInits = true; |
| 2254 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2255 | } |
| 2256 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2257 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2258 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2259 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2260 | NumElements = CAType->getSize().getZExtValue(); |
| 2261 | // Simple heuristic so that we don't allocate a very large |
| 2262 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2263 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2264 | NumElements = 0; |
| 2265 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2266 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2267 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2268 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2269 | RecordDecl *RDecl = RType->getDecl(); |
| 2270 | if (RDecl->isUnion()) |
| 2271 | NumElements = 1; |
| 2272 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2273 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2274 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2277 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2278 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2279 | // Link this new initializer list into the structured initializer |
| 2280 | // lists. |
| 2281 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2282 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2283 | else { |
| 2284 | Result->setSyntacticForm(IList); |
| 2285 | SyntacticToSemantic[IList] = Result; |
| 2286 | } |
| 2287 | |
| 2288 | return Result; |
| 2289 | } |
| 2290 | |
| 2291 | /// Update the initializer at index @p StructuredIndex within the |
| 2292 | /// structured initializer list to the value @p expr. |
| 2293 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2294 | unsigned &StructuredIndex, |
| 2295 | Expr *expr) { |
| 2296 | // No structured initializer list to update |
| 2297 | if (!StructuredList) |
| 2298 | return; |
| 2299 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2300 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2301 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2302 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2303 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2304 | diag::warn_initializer_overrides) |
| 2305 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2306 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2307 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2308 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2309 | << PrevInit->getSourceRange(); |
| 2310 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2311 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2312 | ++StructuredIndex; |
| 2313 | } |
| 2314 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2315 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2316 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2317 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2318 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2319 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2320 | /// added, on success. If everything went okay, Value will receive the |
| 2321 | /// value of the constant expression. |
| 2322 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2323 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2324 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2325 | |
| 2326 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2327 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2328 | if (Result.isInvalid()) |
| 2329 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2330 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2331 | if (Value.isSigned() && Value.isNegative()) |
| 2332 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2333 | << Value.toString(10) << Index->getSourceRange(); |
| 2334 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2335 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2336 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2339 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2340 | SourceLocation Loc, |
| 2341 | bool GNUSyntax, |
| 2342 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2343 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2344 | |
| 2345 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2346 | SmallVector<ASTDesignator, 32> Designators; |
| 2347 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2348 | |
| 2349 | // Build designators and check array designator expressions. |
| 2350 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2351 | const Designator &D = Desig.getDesignator(Idx); |
| 2352 | switch (D.getKind()) { |
| 2353 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2354 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2355 | D.getFieldLoc())); |
| 2356 | break; |
| 2357 | |
| 2358 | case Designator::ArrayDesignator: { |
| 2359 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2360 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2361 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2362 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2363 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2364 | Invalid = true; |
| 2365 | else { |
| 2366 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2367 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2368 | D.getRBracketLoc())); |
| 2369 | InitExpressions.push_back(Index); |
| 2370 | } |
| 2371 | break; |
| 2372 | } |
| 2373 | |
| 2374 | case Designator::ArrayRangeDesignator: { |
| 2375 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2376 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2377 | llvm::APSInt StartValue; |
| 2378 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2379 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2380 | StartIndex->isValueDependent(); |
| 2381 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2382 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2383 | if (!StartDependent) |
| 2384 | StartIndex = |
| 2385 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2386 | if (!EndDependent) |
| 2387 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2388 | |
| 2389 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2390 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2391 | else { |
| 2392 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2393 | if (StartDependent || EndDependent) { |
| 2394 | // Nothing to compute. |
| 2395 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2396 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2397 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2398 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2399 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2400 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2401 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2403 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2404 | Invalid = true; |
| 2405 | } else { |
| 2406 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2407 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2408 | D.getEllipsisLoc(), |
| 2409 | D.getRBracketLoc())); |
| 2410 | InitExpressions.push_back(StartIndex); |
| 2411 | InitExpressions.push_back(EndIndex); |
| 2412 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2413 | } |
| 2414 | break; |
| 2415 | } |
| 2416 | } |
| 2417 | } |
| 2418 | |
| 2419 | if (Invalid || Init.isInvalid()) |
| 2420 | return ExprError(); |
| 2421 | |
| 2422 | // Clear out the expressions within the designation. |
| 2423 | Desig.ClearExprs(*this); |
| 2424 | |
| 2425 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2426 | = DesignatedInitExpr::Create(Context, |
| 2427 | Designators.data(), Designators.size(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2428 | InitExpressions, Loc, GNUSyntax, |
| 2429 | Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2430 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2431 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2432 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2433 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2434 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2435 | return Owned(DIE); |
| 2436 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2437 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2438 | //===----------------------------------------------------------------------===// |
| 2439 | // Initialization entity |
| 2440 | //===----------------------------------------------------------------------===// |
| 2441 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2442 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2443 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2444 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2445 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2446 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2447 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2448 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2449 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2450 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2451 | Type = VT->getElementType(); |
| 2452 | } else { |
| 2453 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2454 | assert(CT && "Unexpected type"); |
| 2455 | Kind = EK_ComplexElement; |
| 2456 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2457 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2460 | InitializedEntity |
| 2461 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 2462 | const CXXBaseSpecifier *Base, |
| 2463 | bool IsInheritedVirtualBase) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2464 | InitializedEntity Result; |
| 2465 | Result.Kind = EK_Base; |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2466 | Result.Parent = 0; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2467 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2468 | if (IsInheritedVirtualBase) |
| 2469 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2470 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2471 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2472 | return Result; |
| 2473 | } |
| 2474 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2475 | DeclarationName InitializedEntity::getName() const { |
| 2476 | switch (getKind()) { |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2477 | case EK_Parameter: |
| 2478 | case EK_Parameter_CF_Audited: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2479 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2480 | return (D ? D->getDeclName() : DeclarationName()); |
| 2481 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2482 | |
| 2483 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2484 | case EK_Member: |
| 2485 | return VariableOrMember->getDeclName(); |
| 2486 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2487 | case EK_LambdaCapture: |
| 2488 | return Capture.Var->getDeclName(); |
| 2489 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2490 | case EK_Result: |
| 2491 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2492 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2493 | case EK_Temporary: |
| 2494 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2495 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2496 | case EK_ArrayElement: |
| 2497 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2498 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2499 | case EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2500 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2501 | case EK_RelatedResult: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2502 | return DeclarationName(); |
| 2503 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2504 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2505 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2506 | } |
| 2507 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2508 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2509 | switch (getKind()) { |
| 2510 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2511 | case EK_Member: |
| 2512 | return VariableOrMember; |
| 2513 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2514 | case EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2515 | case EK_Parameter_CF_Audited: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2516 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2517 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2518 | case EK_Result: |
| 2519 | case EK_Exception: |
| 2520 | case EK_New: |
| 2521 | case EK_Temporary: |
| 2522 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2523 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2524 | case EK_ArrayElement: |
| 2525 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2526 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2527 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2528 | case EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2529 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2530 | case EK_RelatedResult: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2531 | return 0; |
| 2532 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2533 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2534 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2537 | bool InitializedEntity::allowsNRVO() const { |
| 2538 | switch (getKind()) { |
| 2539 | case EK_Result: |
| 2540 | case EK_Exception: |
| 2541 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2542 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2543 | case EK_Variable: |
| 2544 | case EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2545 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2546 | case EK_Member: |
| 2547 | case EK_New: |
| 2548 | case EK_Temporary: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2549 | case EK_CompoundLiteralInit: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2550 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2551 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2552 | case EK_ArrayElement: |
| 2553 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2554 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2555 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2556 | case EK_LambdaCapture: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2557 | case EK_RelatedResult: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2558 | break; |
| 2559 | } |
| 2560 | |
| 2561 | return false; |
| 2562 | } |
| 2563 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2564 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2565 | assert(getParent() != this); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2566 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 2567 | for (unsigned I = 0; I != Depth; ++I) |
| 2568 | OS << "`-"; |
| 2569 | |
| 2570 | switch (getKind()) { |
| 2571 | case EK_Variable: OS << "Variable"; break; |
| 2572 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2573 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 2574 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2575 | case EK_Result: OS << "Result"; break; |
| 2576 | case EK_Exception: OS << "Exception"; break; |
| 2577 | case EK_Member: OS << "Member"; break; |
| 2578 | case EK_New: OS << "New"; break; |
| 2579 | case EK_Temporary: OS << "Temporary"; break; |
| 2580 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2581 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2582 | case EK_Base: OS << "Base"; break; |
| 2583 | case EK_Delegating: OS << "Delegating"; break; |
| 2584 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 2585 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 2586 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 2587 | case EK_BlockElement: OS << "Block"; break; |
| 2588 | case EK_LambdaCapture: |
| 2589 | OS << "LambdaCapture "; |
| 2590 | getCapturedVar()->printName(OS); |
| 2591 | break; |
| 2592 | } |
| 2593 | |
| 2594 | if (Decl *D = getDecl()) { |
| 2595 | OS << " "; |
| 2596 | cast<NamedDecl>(D)->printQualifiedName(OS); |
| 2597 | } |
| 2598 | |
| 2599 | OS << " '" << getType().getAsString() << "'\n"; |
| 2600 | |
| 2601 | return Depth + 1; |
| 2602 | } |
| 2603 | |
| 2604 | void InitializedEntity::dump() const { |
| 2605 | dumpImpl(llvm::errs()); |
| 2606 | } |
| 2607 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2608 | //===----------------------------------------------------------------------===// |
| 2609 | // Initialization sequence |
| 2610 | //===----------------------------------------------------------------------===// |
| 2611 | |
| 2612 | void InitializationSequence::Step::Destroy() { |
| 2613 | switch (Kind) { |
| 2614 | case SK_ResolveAddressOfOverloadedFunction: |
| 2615 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2616 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2617 | case SK_CastDerivedToBaseLValue: |
| 2618 | case SK_BindReference: |
| 2619 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2620 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2621 | case SK_UserConversion: |
| 2622 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2623 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2624 | case SK_QualificationConversionLValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2625 | case SK_LValueToRValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2626 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2627 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2628 | case SK_UnwrapInitList: |
| 2629 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2630 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2631 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2632 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2633 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2634 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2635 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2636 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2637 | case SK_PassByIndirectCopyRestore: |
| 2638 | case SK_PassByIndirectRestore: |
| 2639 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2640 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2641 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2642 | case SK_OCLZeroEvent: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2643 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2644 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2645 | case SK_ConversionSequence: |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2646 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2647 | delete ICS; |
| 2648 | } |
| 2649 | } |
| 2650 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2651 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2652 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
| 2655 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2656 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2657 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2658 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2659 | switch (getFailureKind()) { |
| 2660 | case FK_TooManyInitsForReference: |
| 2661 | case FK_ArrayNeedsInitList: |
| 2662 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 2663 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 2664 | case FK_NarrowStringIntoWideCharArray: |
| 2665 | case FK_WideStringIntoCharArray: |
| 2666 | case FK_IncompatWideStringIntoWideChar: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2667 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2668 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2669 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2670 | case FK_RValueReferenceBindingToLValue: |
| 2671 | case FK_ReferenceInitDropsQualifiers: |
| 2672 | case FK_ReferenceInitFailed: |
| 2673 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2674 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2675 | case FK_TooManyInitsForScalar: |
| 2676 | case FK_ReferenceBindingToInitList: |
| 2677 | case FK_InitListBadDestinationType: |
| 2678 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2679 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2680 | case FK_ArrayTypeMismatch: |
| 2681 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2682 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2683 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2684 | case FK_PlaceholderType: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2685 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2686 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2687 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2688 | case FK_ReferenceInitOverloadFailed: |
| 2689 | case FK_UserConversionOverloadFailed: |
| 2690 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2691 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2692 | return FailedOverloadResult == OR_Ambiguous; |
| 2693 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2694 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2695 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2698 | bool InitializationSequence::isConstructorInitialization() const { |
| 2699 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2700 | } |
| 2701 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2702 | void |
| 2703 | InitializationSequence |
| 2704 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2705 | DeclAccessPair Found, |
| 2706 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2707 | Step S; |
| 2708 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2709 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2710 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2711 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2712 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2713 | Steps.push_back(S); |
| 2714 | } |
| 2715 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2716 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2717 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2718 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2719 | switch (VK) { |
| 2720 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2721 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2722 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2723 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2724 | S.Type = BaseType; |
| 2725 | Steps.push_back(S); |
| 2726 | } |
| 2727 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2728 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2729 | bool BindingTemporary) { |
| 2730 | Step S; |
| 2731 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2732 | S.Type = T; |
| 2733 | Steps.push_back(S); |
| 2734 | } |
| 2735 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2736 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2737 | Step S; |
| 2738 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2739 | S.Type = T; |
| 2740 | Steps.push_back(S); |
| 2741 | } |
| 2742 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2743 | void |
| 2744 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2745 | DeclAccessPair FoundDecl, |
| 2746 | QualType T, |
| 2747 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2748 | Step S; |
| 2749 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2750 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2751 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2752 | S.Function.Function = Function; |
| 2753 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2754 | Steps.push_back(S); |
| 2755 | } |
| 2756 | |
| 2757 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2758 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2759 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2760 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2761 | switch (VK) { |
| 2762 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2763 | S.Kind = SK_QualificationConversionRValue; |
| 2764 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2765 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2766 | S.Kind = SK_QualificationConversionXValue; |
| 2767 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2768 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2769 | S.Kind = SK_QualificationConversionLValue; |
| 2770 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2771 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2772 | S.Type = Ty; |
| 2773 | Steps.push_back(S); |
| 2774 | } |
| 2775 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2776 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2777 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2778 | |
| 2779 | Step S; |
| 2780 | S.Kind = SK_LValueToRValue; |
| 2781 | S.Type = Ty; |
| 2782 | Steps.push_back(S); |
| 2783 | } |
| 2784 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2785 | void InitializationSequence::AddConversionSequenceStep( |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2786 | const ImplicitConversionSequence &ICS, QualType T, |
| 2787 | bool TopLevelOfInitList) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2788 | Step S; |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 2789 | S.Kind = TopLevelOfInitList ? SK_ConversionSequenceNoNarrowing |
| 2790 | : SK_ConversionSequence; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2791 | S.Type = T; |
| 2792 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2793 | Steps.push_back(S); |
| 2794 | } |
| 2795 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2796 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2797 | Step S; |
| 2798 | S.Kind = SK_ListInitialization; |
| 2799 | S.Type = T; |
| 2800 | Steps.push_back(S); |
| 2801 | } |
| 2802 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2803 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2804 | InitializationSequence |
| 2805 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2806 | AccessSpecifier Access, |
| 2807 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2808 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2809 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2810 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2811 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2812 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2813 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2814 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2815 | S.Function.Function = Constructor; |
| 2816 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2817 | Steps.push_back(S); |
| 2818 | } |
| 2819 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2820 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2821 | Step S; |
| 2822 | S.Kind = SK_ZeroInitialization; |
| 2823 | S.Type = T; |
| 2824 | Steps.push_back(S); |
| 2825 | } |
| 2826 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2827 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2828 | Step S; |
| 2829 | S.Kind = SK_CAssignment; |
| 2830 | S.Type = T; |
| 2831 | Steps.push_back(S); |
| 2832 | } |
| 2833 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2834 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2835 | Step S; |
| 2836 | S.Kind = SK_StringInit; |
| 2837 | S.Type = T; |
| 2838 | Steps.push_back(S); |
| 2839 | } |
| 2840 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2841 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2842 | Step S; |
| 2843 | S.Kind = SK_ObjCObjectConversion; |
| 2844 | S.Type = T; |
| 2845 | Steps.push_back(S); |
| 2846 | } |
| 2847 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2848 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2849 | Step S; |
| 2850 | S.Kind = SK_ArrayInit; |
| 2851 | S.Type = T; |
| 2852 | Steps.push_back(S); |
| 2853 | } |
| 2854 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2855 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2856 | Step S; |
| 2857 | S.Kind = SK_ParenthesizedArrayInit; |
| 2858 | S.Type = T; |
| 2859 | Steps.push_back(S); |
| 2860 | } |
| 2861 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2862 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2863 | bool shouldCopy) { |
| 2864 | Step s; |
| 2865 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2866 | : SK_PassByIndirectRestore); |
| 2867 | s.Type = type; |
| 2868 | Steps.push_back(s); |
| 2869 | } |
| 2870 | |
| 2871 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2872 | Step S; |
| 2873 | S.Kind = SK_ProduceObjCObject; |
| 2874 | S.Type = T; |
| 2875 | Steps.push_back(S); |
| 2876 | } |
| 2877 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2878 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2879 | Step S; |
| 2880 | S.Kind = SK_StdInitializerList; |
| 2881 | S.Type = T; |
| 2882 | Steps.push_back(S); |
| 2883 | } |
| 2884 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2885 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 2886 | Step S; |
| 2887 | S.Kind = SK_OCLSamplerInit; |
| 2888 | S.Type = T; |
| 2889 | Steps.push_back(S); |
| 2890 | } |
| 2891 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2892 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 2893 | Step S; |
| 2894 | S.Kind = SK_OCLZeroEvent; |
| 2895 | S.Type = T; |
| 2896 | Steps.push_back(S); |
| 2897 | } |
| 2898 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2899 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2900 | InitListExpr *Syntactic) { |
| 2901 | assert(Syntactic->getNumInits() == 1 && |
| 2902 | "Can only rewrap trivial init lists."); |
| 2903 | Step S; |
| 2904 | S.Kind = SK_UnwrapInitList; |
| 2905 | S.Type = Syntactic->getInit(0)->getType(); |
| 2906 | Steps.insert(Steps.begin(), S); |
| 2907 | |
| 2908 | S.Kind = SK_RewrapInitList; |
| 2909 | S.Type = T; |
| 2910 | S.WrappingSyntacticList = Syntactic; |
| 2911 | Steps.push_back(S); |
| 2912 | } |
| 2913 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2914 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2915 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2916 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2917 | this->Failure = Failure; |
| 2918 | this->FailedOverloadResult = Result; |
| 2919 | } |
| 2920 | |
| 2921 | //===----------------------------------------------------------------------===// |
| 2922 | // Attempt initialization |
| 2923 | //===----------------------------------------------------------------------===// |
| 2924 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2925 | static void MaybeProduceObjCObject(Sema &S, |
| 2926 | InitializationSequence &Sequence, |
| 2927 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2928 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2929 | |
| 2930 | /// When initializing a parameter, produce the value if it's marked |
| 2931 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2932 | if (Entity.isParameterKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2933 | if (!Entity.isParameterConsumed()) |
| 2934 | return; |
| 2935 | |
| 2936 | assert(Entity.getType()->isObjCRetainableType() && |
| 2937 | "consuming an object of unretainable type?"); |
| 2938 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2939 | |
| 2940 | /// When initializing a return value, if the return type is a |
| 2941 | /// retainable type, then returns need to immediately retain the |
| 2942 | /// object. If an autorelease is required, it will be done at the |
| 2943 | /// last instant. |
| 2944 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2945 | if (!Entity.getType()->isObjCRetainableType()) |
| 2946 | return; |
| 2947 | |
| 2948 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2949 | } |
| 2950 | } |
| 2951 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2952 | static void TryListInitialization(Sema &S, |
| 2953 | const InitializedEntity &Entity, |
| 2954 | const InitializationKind &Kind, |
| 2955 | InitListExpr *InitList, |
| 2956 | InitializationSequence &Sequence); |
| 2957 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2958 | /// \brief When initializing from init list via constructor, handle |
| 2959 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2960 | /// |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2961 | /// \return true if we have handled initialization of an object of type |
| 2962 | /// std::initializer_list<T>, false otherwise. |
| 2963 | static bool TryInitializerListConstruction(Sema &S, |
| 2964 | InitListExpr *List, |
| 2965 | QualType DestType, |
| 2966 | InitializationSequence &Sequence) { |
| 2967 | QualType E; |
| 2968 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2969 | return false; |
| 2970 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2971 | if (S.RequireCompleteType(List->getExprLoc(), E, 0)) { |
| 2972 | Sequence.setIncompleteTypeFailure(E); |
| 2973 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2974 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2975 | |
| 2976 | // Try initializing a temporary array from the init list. |
| 2977 | QualType ArrayType = S.Context.getConstantArrayType( |
| 2978 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2979 | List->getNumInits()), |
| 2980 | clang::ArrayType::Normal, 0); |
| 2981 | InitializedEntity HiddenArray = |
| 2982 | InitializedEntity::InitializeTemporary(ArrayType); |
| 2983 | InitializationKind Kind = |
| 2984 | InitializationKind::CreateDirectList(List->getExprLoc()); |
| 2985 | TryListInitialization(S, HiddenArray, Kind, List, Sequence); |
| 2986 | if (Sequence) |
| 2987 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2988 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2989 | } |
| 2990 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2991 | static OverloadingResult |
| 2992 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2993 | MultiExprArg Args, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2994 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2995 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2996 | OverloadCandidateSet::iterator &Best, |
| 2997 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2998 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2999 | CandidateSet.clear(); |
| 3000 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3001 | for (ArrayRef<NamedDecl *>::iterator |
| 3002 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3003 | NamedDecl *D = *Con; |
| 3004 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3005 | bool SuppressUserConversions = false; |
| 3006 | |
| 3007 | // Find the constructor (which may be a template). |
| 3008 | CXXConstructorDecl *Constructor = 0; |
| 3009 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 3010 | if (ConstructorTmpl) |
| 3011 | Constructor = cast<CXXConstructorDecl>( |
| 3012 | ConstructorTmpl->getTemplatedDecl()); |
| 3013 | else { |
| 3014 | Constructor = cast<CXXConstructorDecl>(D); |
| 3015 | |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3016 | // C++11 [over.best.ics]p4: |
| 3017 | // However, when considering the argument of a constructor or |
| 3018 | // user-defined conversion function that is a candidate: |
| 3019 | // -- by 13.3.1.3 when invoked for the copying/moving of a temporary |
| 3020 | // in the second step of a class copy-initialization, |
| 3021 | // -- by 13.3.1.7 when passing the initializer list as a single |
| 3022 | // argument or when the initializer list has exactly one elementand |
| 3023 | // a conversion to some class X or reference to (possibly |
| 3024 | // cv-qualified) X is considered for the first parameter of a |
| 3025 | // constructor of X, or |
| 3026 | // -- by 13.3.1.4, 13.3.1.5, or 13.3.1.6 in all cases, |
| 3027 | // only standard conversion sequences and ellipsis conversion sequences |
| 3028 | // are considered. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3029 | if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) && |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3030 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3031 | SuppressUserConversions = true; |
| 3032 | } |
| 3033 | |
| 3034 | if (!Constructor->isInvalidDecl() && |
| 3035 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3036 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3037 | if (ConstructorTmpl) |
| 3038 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3039 | /*ExplicitArgs*/ 0, Args, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3040 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3041 | else { |
| 3042 | // C++ [over.match.copy]p1: |
| 3043 | // - When initializing a temporary to be bound to the first parameter |
| 3044 | // of a constructor that takes a reference to possibly cv-qualified |
| 3045 | // T as its first argument, called with a single argument in the |
| 3046 | // context of direct-initialization, explicit conversion functions |
| 3047 | // are also considered. |
| 3048 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3049 | Args.size() == 1 && |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3050 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3051 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3052 | SuppressUserConversions, |
| 3053 | /*PartialOverloading=*/false, |
| 3054 | /*AllowExplicit=*/AllowExplicitConv); |
| 3055 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3056 | } |
| 3057 | } |
| 3058 | |
| 3059 | // Perform overload resolution and return the result. |
| 3060 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3061 | } |
| 3062 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3063 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3064 | /// enumerates the constructors of the initialized entity and performs overload |
| 3065 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3066 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3067 | /// class type. |
| 3068 | static void TryConstructorInitialization(Sema &S, |
| 3069 | const InitializedEntity &Entity, |
| 3070 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3071 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3072 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3073 | bool InitListSyntax = false) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3074 | assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3075 | "InitListSyntax must come with a single initializer list argument."); |
| 3076 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3077 | // The type we're constructing needs to be complete. |
| 3078 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3079 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3080 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
| 3083 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3084 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3085 | CXXRecordDecl *DestRecordDecl |
| 3086 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3087 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3088 | // Build the candidate set directly in the initialization sequence |
| 3089 | // structure, so that it will persist if we fail. |
| 3090 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3091 | |
| 3092 | // Determine whether we are allowed to call explicit constructors or |
| 3093 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3094 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3095 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3096 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3097 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3098 | // applicable constructors are enumerated, and the best one is chosen |
| 3099 | // through overload resolution. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3100 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3101 | // The container holding the constructors can under certain conditions |
| 3102 | // be changed while iterating (e.g. because of deserialization). |
| 3103 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3104 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3105 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3106 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3107 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3108 | bool AsInitializerList = false; |
| 3109 | |
| 3110 | // C++11 [over.match.list]p1: |
| 3111 | // When objects of non-aggregate type T are list-initialized, overload |
| 3112 | // resolution selects the constructor in two phases: |
| 3113 | // - Initially, the candidate functions are the initializer-list |
| 3114 | // constructors of the class T and the argument list consists of the |
| 3115 | // initializer list as a single argument. |
| 3116 | if (InitListSyntax) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3117 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3118 | AsInitializerList = true; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3119 | |
| 3120 | // If the initializer list has no elements and T has a default constructor, |
| 3121 | // the first phase is omitted. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3122 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3123 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3124 | CandidateSet, Ctors, Best, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3125 | CopyInitialization, AllowExplicit, |
| 3126 | /*OnlyListConstructor=*/true, |
| 3127 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3128 | |
| 3129 | // Time to unwrap the init list. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3130 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
| 3133 | // C++11 [over.match.list]p1: |
| 3134 | // - If no viable initializer-list constructor is found, overload resolution |
| 3135 | // is performed again, where the candidate functions are all the |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3136 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3137 | // elements of the initializer list. |
| 3138 | if (Result == OR_No_Viable_Function) { |
| 3139 | AsInitializerList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3140 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3141 | CandidateSet, Ctors, Best, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3142 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3143 | /*OnlyListConstructors=*/false, |
| 3144 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3145 | } |
| 3146 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3147 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3148 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3149 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3150 | Result); |
| 3151 | return; |
| 3152 | } |
| 3153 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3154 | // C++11 [dcl.init]p6: |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3155 | // If a program calls for the default initialization of an object |
| 3156 | // of a const-qualified type T, T shall be a class type with a |
| 3157 | // user-provided default constructor. |
| 3158 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3159 | Entity.getType().isConstQualified() && |
Aaron Ballman | 5121781 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 3160 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3161 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3162 | return; |
| 3163 | } |
| 3164 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3165 | // C++11 [over.match.list]p1: |
| 3166 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3167 | // initializer is ill-formed. |
| 3168 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 3169 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 3170 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3171 | return; |
| 3172 | } |
| 3173 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3174 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3175 | // subsumed by the initialization. |
| 3176 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3177 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 3178 | Best->FoundDecl.getAccess(), |
| 3179 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3180 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3183 | static bool |
| 3184 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3185 | Expr *Initializer, |
| 3186 | QualType &SourceType, |
| 3187 | QualType &UnqualifiedSourceType, |
| 3188 | QualType UnqualifiedTargetType, |
| 3189 | InitializationSequence &Sequence) { |
| 3190 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3191 | S.Context.OverloadTy) { |
| 3192 | DeclAccessPair Found; |
| 3193 | bool HadMultipleCandidates = false; |
| 3194 | if (FunctionDecl *Fn |
| 3195 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3196 | UnqualifiedTargetType, |
| 3197 | false, Found, |
| 3198 | &HadMultipleCandidates)) { |
| 3199 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3200 | HadMultipleCandidates); |
| 3201 | SourceType = Fn->getType(); |
| 3202 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3203 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3204 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3205 | return true; |
| 3206 | } |
| 3207 | } |
| 3208 | return false; |
| 3209 | } |
| 3210 | |
| 3211 | static void TryReferenceInitializationCore(Sema &S, |
| 3212 | const InitializedEntity &Entity, |
| 3213 | const InitializationKind &Kind, |
| 3214 | Expr *Initializer, |
| 3215 | QualType cv1T1, QualType T1, |
| 3216 | Qualifiers T1Quals, |
| 3217 | QualType cv2T2, QualType T2, |
| 3218 | Qualifiers T2Quals, |
| 3219 | InitializationSequence &Sequence); |
| 3220 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3221 | static void TryValueInitialization(Sema &S, |
| 3222 | const InitializedEntity &Entity, |
| 3223 | const InitializationKind &Kind, |
| 3224 | InitializationSequence &Sequence, |
| 3225 | InitListExpr *InitList = 0); |
| 3226 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3227 | /// \brief Attempt list initialization of a reference. |
| 3228 | static void TryReferenceListInitialization(Sema &S, |
| 3229 | const InitializedEntity &Entity, |
| 3230 | const InitializationKind &Kind, |
| 3231 | InitListExpr *InitList, |
Richard Smith | b6e3808 | 2013-06-08 00:02:08 +0000 | [diff] [blame] | 3232 | InitializationSequence &Sequence) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3233 | // First, catch C++03 where this isn't possible. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3234 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3235 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3236 | return; |
| 3237 | } |
| 3238 | |
| 3239 | QualType DestType = Entity.getType(); |
| 3240 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3241 | Qualifiers T1Quals; |
| 3242 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3243 | |
| 3244 | // Reference initialization via an initializer list works thus: |
| 3245 | // If the initializer list consists of a single element that is |
| 3246 | // reference-related to the referenced type, bind directly to that element |
| 3247 | // (possibly creating temporaries). |
| 3248 | // Otherwise, initialize a temporary with the initializer list and |
| 3249 | // bind to that. |
| 3250 | if (InitList->getNumInits() == 1) { |
| 3251 | Expr *Initializer = InitList->getInit(0); |
| 3252 | QualType cv2T2 = Initializer->getType(); |
| 3253 | Qualifiers T2Quals; |
| 3254 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3255 | |
| 3256 | // If this fails, creating a temporary wouldn't work either. |
| 3257 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3258 | T1, Sequence)) |
| 3259 | return; |
| 3260 | |
| 3261 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3262 | bool dummy1, dummy2, dummy3; |
| 3263 | Sema::ReferenceCompareResult RefRelationship |
| 3264 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3265 | dummy2, dummy3); |
| 3266 | if (RefRelationship >= Sema::Ref_Related) { |
| 3267 | // Try to bind the reference here. |
| 3268 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3269 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3270 | if (Sequence) |
| 3271 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3272 | return; |
| 3273 | } |
Richard Smith | 02d65ee | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3274 | |
| 3275 | // Update the initializer if we've resolved an overloaded function. |
| 3276 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3277 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | // Not reference-related. Create a temporary and bind to that. |
| 3281 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3282 | |
| 3283 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3284 | if (Sequence) { |
| 3285 | if (DestType->isRValueReferenceType() || |
| 3286 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3287 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3288 | else |
| 3289 | Sequence.SetFailed( |
| 3290 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3291 | } |
| 3292 | } |
| 3293 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3294 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3295 | static void TryListInitialization(Sema &S, |
| 3296 | const InitializedEntity &Entity, |
| 3297 | const InitializationKind &Kind, |
| 3298 | InitListExpr *InitList, |
| 3299 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3300 | QualType DestType = Entity.getType(); |
| 3301 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3302 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3303 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3304 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3305 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3306 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3307 | return; |
| 3308 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3309 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3310 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3311 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3312 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3313 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3314 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3315 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3316 | return; |
| 3317 | } |
| 3318 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3319 | // C++11 [dcl.init.list]p3: |
| 3320 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3321 | if (!DestType->isAggregateType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3322 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3323 | // - Otherwise, if the initializer list has no elements and T is a |
| 3324 | // class type with a default constructor, the object is |
| 3325 | // value-initialized. |
| 3326 | if (InitList->getNumInits() == 0) { |
| 3327 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3328 | if (RD->hasDefaultConstructor()) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3329 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3330 | return; |
| 3331 | } |
| 3332 | } |
| 3333 | |
| 3334 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3335 | // an initializer_list object constructed [...] |
| 3336 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3337 | return; |
| 3338 | |
| 3339 | // - Otherwise, if T is a class type, constructors are considered. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3340 | Expr *InitListAsExpr = InitList; |
| 3341 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3342 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3343 | } else |
| 3344 | Sequence.SetFailed( |
| 3345 | InitializationSequence::FK_InitListBadDestinationType); |
| 3346 | return; |
| 3347 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3348 | } |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 3349 | if (S.getLangOpts().CPlusPlus && !DestType->isAggregateType() && |
| 3350 | InitList->getNumInits() == 1 && |
| 3351 | InitList->getInit(0)->getType()->isRecordType()) { |
| 3352 | // - Otherwise, if the initializer list has a single element of type E |
| 3353 | // [...references are handled above...], the object or reference is |
| 3354 | // initialized from that element; if a narrowing conversion is required |
| 3355 | // to convert the element to T, the program is ill-formed. |
| 3356 | // |
| 3357 | // Per core-24034, this is direct-initialization if we were performing |
| 3358 | // direct-list-initialization and copy-initialization otherwise. |
| 3359 | // We can't use InitListChecker for this, because it always performs |
| 3360 | // copy-initialization. This only matters if we might use an 'explicit' |
| 3361 | // conversion operator, so we only need to handle the cases where the source |
| 3362 | // is of record type. |
| 3363 | InitializationKind SubKind = |
| 3364 | Kind.getKind() == InitializationKind::IK_DirectList |
| 3365 | ? InitializationKind::CreateDirect(Kind.getLocation(), |
| 3366 | InitList->getLBraceLoc(), |
| 3367 | InitList->getRBraceLoc()) |
| 3368 | : Kind; |
| 3369 | Expr *SubInit[1] = { InitList->getInit(0) }; |
| 3370 | Sequence.InitializeFrom(S, Entity, SubKind, SubInit, |
| 3371 | /*TopLevelOfInitList*/true); |
| 3372 | if (Sequence) |
| 3373 | Sequence.RewrapReferenceInitList(Entity.getType(), InitList); |
| 3374 | return; |
| 3375 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3376 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3377 | InitListChecker CheckInitList(S, Entity, InitList, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 3378 | DestType, /*VerifyOnly=*/true); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3379 | if (CheckInitList.HadError()) { |
| 3380 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3381 | return; |
| 3382 | } |
| 3383 | |
| 3384 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3385 | Sequence.AddListInitializationStep(DestType); |
| 3386 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3387 | |
| 3388 | /// \brief Try a reference initialization that involves calling a conversion |
| 3389 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3390 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3391 | const InitializedEntity &Entity, |
| 3392 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3393 | Expr *Initializer, |
| 3394 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3395 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3396 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3397 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3398 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3399 | QualType cv2T2 = Initializer->getType(); |
| 3400 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3401 | |
| 3402 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3403 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3404 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3405 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3406 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3407 | ObjCConversion, |
| 3408 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3409 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3410 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3411 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3412 | (void)ObjCLifetimeConversion; |
| 3413 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3414 | // Build the candidate set directly in the initialization sequence |
| 3415 | // structure, so that it will persist if we fail. |
| 3416 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3417 | CandidateSet.clear(); |
| 3418 | |
| 3419 | // Determine whether we are allowed to call explicit constructors or |
| 3420 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3421 | bool AllowExplicit = Kind.AllowExplicit(); |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3422 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctionsInRefBinding(); |
| 3423 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3424 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3425 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3426 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3427 | // The type we're converting to is a class type. Enumerate its constructors |
| 3428 | // to see if there is a suitable conversion. |
| 3429 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3430 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3431 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3432 | // The container holding the constructors can under certain conditions |
| 3433 | // be changed while iterating (e.g. because of deserialization). |
| 3434 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3435 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3436 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3437 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3438 | NamedDecl *D = *CI; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3439 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3440 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3441 | // Find the constructor (which may be a template). |
| 3442 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3443 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3444 | if (ConstructorTmpl) |
| 3445 | Constructor = cast<CXXConstructorDecl>( |
| 3446 | ConstructorTmpl->getTemplatedDecl()); |
| 3447 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3448 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3449 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3450 | if (!Constructor->isInvalidDecl() && |
| 3451 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3452 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3453 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3454 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3455 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3456 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3457 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3458 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3459 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3460 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3461 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3462 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3463 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3464 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3465 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3466 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3467 | const RecordType *T2RecordType = 0; |
| 3468 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3469 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3470 | // The type we're converting from is a class type, enumerate its conversion |
| 3471 | // functions. |
| 3472 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3473 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3474 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3475 | CXXRecordDecl::conversion_iterator> |
| 3476 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3477 | for (CXXRecordDecl::conversion_iterator |
| 3478 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3479 | NamedDecl *D = *I; |
| 3480 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3481 | if (isa<UsingShadowDecl>(D)) |
| 3482 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3483 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3484 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3485 | CXXConversionDecl *Conv; |
| 3486 | if (ConvTemplate) |
| 3487 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3488 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3489 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3490 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3491 | // If the conversion function doesn't return a reference type, |
| 3492 | // it can't be considered for this conversion unless we're allowed to |
| 3493 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3494 | // FIXME: Do we need to make sure that we only consider conversion |
| 3495 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3496 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3497 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3498 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3499 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3500 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3501 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3502 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3503 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3504 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3505 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3506 | } |
| 3507 | } |
| 3508 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3509 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3510 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3511 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3512 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3513 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3514 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3515 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3516 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3517 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3518 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3519 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3520 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3521 | // This is the overload that will be used for this initialization step if we |
| 3522 | // use this initialization. Mark it as referenced. |
| 3523 | Function->setReferenced(); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3524 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3525 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3526 | if (isa<CXXConversionDecl>(Function)) |
| 3527 | T2 = Function->getResultType(); |
| 3528 | else |
| 3529 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3530 | |
| 3531 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3532 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3533 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3534 | T2.getNonLValueExprType(S.Context), |
| 3535 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3536 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3537 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3538 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3539 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3540 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3541 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3542 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3543 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3544 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3545 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3546 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3547 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3548 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3549 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3550 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3551 | NewDerivedToBase, NewObjCConversion, |
| 3552 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3553 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3554 | // If the type we've converted to is not reference-related to the |
| 3555 | // type we're looking for, then there is another conversion step |
| 3556 | // we need to perform to produce a temporary of the right type |
| 3557 | // that we'll be binding to. |
| 3558 | ImplicitConversionSequence ICS; |
| 3559 | ICS.setStandard(); |
| 3560 | ICS.Standard = Best->FinalConversion; |
| 3561 | T2 = ICS.Standard.getToType(2); |
| 3562 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3563 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3564 | Sequence.AddDerivedToBaseCastStep( |
| 3565 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3566 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3567 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3568 | else if (NewObjCConversion) |
| 3569 | Sequence.AddObjCObjectConversionStep( |
| 3570 | S.Context.getQualifiedType(T1, |
| 3571 | T2.getNonReferenceType().getQualifiers())); |
| 3572 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3573 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3574 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3575 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3576 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3577 | return OR_Success; |
| 3578 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3579 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3580 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3581 | const InitializedEntity &Entity, |
| 3582 | Expr *CurInitExpr); |
| 3583 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3584 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3585 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3586 | const InitializedEntity &Entity, |
| 3587 | const InitializationKind &Kind, |
| 3588 | Expr *Initializer, |
| 3589 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3590 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3591 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3592 | Qualifiers T1Quals; |
| 3593 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3594 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3595 | Qualifiers T2Quals; |
| 3596 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3597 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3598 | // If the initializer is the address of an overloaded function, try |
| 3599 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3600 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3601 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3602 | T1, Sequence)) |
| 3603 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3604 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3605 | // Delegate everything else to a subfunction. |
| 3606 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3607 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3608 | } |
| 3609 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3610 | /// Converts the target of reference initialization so that it has the |
| 3611 | /// appropriate qualifiers and value kind. |
| 3612 | /// |
| 3613 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3614 | /// \code |
| 3615 | /// int x; |
| 3616 | /// const int &r = x; |
| 3617 | /// \endcode |
| 3618 | /// |
| 3619 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3620 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3621 | /// \code |
| 3622 | /// const int &r = someStruct.bitfield; |
| 3623 | /// \endcode |
| 3624 | static ExprValueKind |
| 3625 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3626 | InitializationSequence &Sequence, |
| 3627 | Expr *Initializer, |
| 3628 | QualType cv1T1, |
| 3629 | Qualifiers T1Quals, |
| 3630 | Qualifiers T2Quals, |
| 3631 | bool IsLValueRef) { |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3632 | bool IsNonAddressableType = Initializer->refersToBitField() || |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3633 | Initializer->refersToVectorElement(); |
| 3634 | |
| 3635 | if (IsNonAddressableType) { |
| 3636 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3637 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3638 | // an rvalue reference. |
| 3639 | // |
| 3640 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3641 | // error to be diagnosed later. |
| 3642 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3643 | assert(Initializer->isGLValue()); |
| 3644 | return Initializer->getValueKind(); |
| 3645 | } |
| 3646 | |
| 3647 | // Force a load so we can materialize a temporary. |
| 3648 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3649 | return VK_RValue; |
| 3650 | } |
| 3651 | |
| 3652 | if (T1Quals != T2Quals) { |
| 3653 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3654 | Initializer->getValueKind()); |
| 3655 | } |
| 3656 | |
| 3657 | return Initializer->getValueKind(); |
| 3658 | } |
| 3659 | |
| 3660 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3661 | /// \brief Reference initialization without resolving overloaded functions. |
| 3662 | static void TryReferenceInitializationCore(Sema &S, |
| 3663 | const InitializedEntity &Entity, |
| 3664 | const InitializationKind &Kind, |
| 3665 | Expr *Initializer, |
| 3666 | QualType cv1T1, QualType T1, |
| 3667 | Qualifiers T1Quals, |
| 3668 | QualType cv2T2, QualType T2, |
| 3669 | Qualifiers T2Quals, |
| 3670 | InitializationSequence &Sequence) { |
| 3671 | QualType DestType = Entity.getType(); |
| 3672 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3673 | // Compute some basic properties of the types and the initializer. |
| 3674 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3675 | bool isRValueRef = !isLValueRef; |
| 3676 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3677 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3678 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3679 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3680 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3681 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3682 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3683 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3684 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3685 | // 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] | 3686 | // "cv2 T2" as follows: |
| 3687 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3688 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3689 | // expression |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3690 | // Note the analogous bullet points for rvalue refs to functions. Because |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3691 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3692 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3693 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3694 | bool T1Function = T1->isFunctionType(); |
| 3695 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3696 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3697 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3698 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3699 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3700 | // - 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] | 3701 | // reference-compatible with "cv2 T2," or |
| 3702 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3703 | // 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] | 3704 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3705 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3706 | // to decide whether we're actually binding to a temporary created from |
| 3707 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3708 | if (DerivedToBase) |
| 3709 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3710 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3711 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3712 | else if (ObjCConversion) |
| 3713 | Sequence.AddObjCObjectConversionStep( |
| 3714 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3715 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3716 | ExprValueKind ValueKind = |
| 3717 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3718 | cv1T1, T1Quals, T2Quals, |
| 3719 | isLValueRef); |
| 3720 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3721 | return; |
| 3722 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3723 | |
| 3724 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3725 | // reference-related to T2, and can be implicitly converted to an |
| 3726 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3727 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3728 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3729 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3730 | // 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] | 3731 | // an rvalue. DR1287 removed the "implicitly" here. |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3732 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3733 | (isLValueRef || InitCategory.isRValue())) { |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3734 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3735 | S, Entity, Kind, Initializer, /*AllowRValues*/isRValueRef, Sequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3736 | if (ConvOvlResult == OR_Success) |
| 3737 | return; |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3738 | if (ConvOvlResult != OR_No_Viable_Function) |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3739 | Sequence.SetOverloadFailure( |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3740 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3741 | ConvOvlResult); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3742 | } |
| 3743 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3744 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3745 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3746 | // 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] | 3747 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3748 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3749 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3750 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3751 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3752 | Sequence.SetOverloadFailure( |
| 3753 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3754 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3755 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3756 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3757 | ? (RefRelationship == Sema::Ref_Related |
| 3758 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3759 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3760 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3761 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3762 | return; |
| 3763 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3764 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3765 | // - If the initializer expression |
| 3766 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3767 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3768 | // Note: functions are handled below. |
| 3769 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3770 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3771 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3772 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3773 | (InitCategory.isXValue() || |
| 3774 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3775 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3776 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3777 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3778 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3779 | // compiler the freedom to perform a copy here or bind to the |
| 3780 | // object, while C++0x requires that we bind directly to the |
| 3781 | // object. Hence, we always bind to the object without making an |
| 3782 | // extra copy. However, in C++03 requires that we check for the |
| 3783 | // presence of a suitable copy constructor: |
| 3784 | // |
| 3785 | // The constructor that would be used to make the copy shall |
| 3786 | // be callable whether or not the copy is actually done. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3787 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3788 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3789 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3790 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3791 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3792 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3793 | if (DerivedToBase) |
| 3794 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3795 | ValueKind); |
| 3796 | else if (ObjCConversion) |
| 3797 | Sequence.AddObjCObjectConversionStep( |
| 3798 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3799 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3800 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3801 | Initializer, cv1T1, |
| 3802 | T1Quals, T2Quals, |
| 3803 | isLValueRef); |
| 3804 | |
| 3805 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3806 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3807 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3808 | |
| 3809 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3810 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3811 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3812 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3813 | // |
| 3814 | // DR1287 removes the "implicitly" here. |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3815 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3816 | if (RefRelationship == Sema::Ref_Incompatible) { |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3817 | ConvOvlResult = TryRefInitWithConversionFunction( |
| 3818 | S, Entity, Kind, Initializer, /*AllowRValues*/true, Sequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3819 | if (ConvOvlResult) |
| 3820 | Sequence.SetOverloadFailure( |
Richard Smith | 867521c | 2013-09-21 21:23:47 +0000 | [diff] [blame] | 3821 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3822 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3823 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3824 | return; |
| 3825 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3826 | |
Douglas Gregor | defa32e | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 3827 | if ((RefRelationship == Sema::Ref_Compatible || |
| 3828 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 3829 | isRValueRef && InitCategory.isLValue()) { |
| 3830 | Sequence.SetFailed( |
| 3831 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3832 | return; |
| 3833 | } |
| 3834 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3835 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3836 | return; |
| 3837 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3838 | |
| 3839 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3840 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3841 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3842 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3843 | |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3844 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3845 | |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3846 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 3847 | // copy-initialization? |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3848 | ImplicitConversionSequence ICS |
| 3849 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3850 | /*SuppressUserConversions=*/false, |
| 3851 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3852 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3853 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3854 | /*AllowObjCWritebackConversion=*/false); |
| 3855 | |
| 3856 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3857 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3858 | // this into an overloading ambiguity diagnostic. However, we need |
| 3859 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3860 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3861 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3862 | Sequence.SetOverloadFailure( |
| 3863 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3864 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3865 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3866 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3867 | else |
| 3868 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3869 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3870 | } else { |
| 3871 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3872 | } |
| 3873 | |
| 3874 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3875 | // same cv-qualification as, or greater cv-qualification |
| 3876 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3877 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3878 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3879 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3880 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3881 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3882 | return; |
| 3883 | } |
| 3884 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3885 | // [...] 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] | 3886 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3887 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3888 | InitCategory.isLValue()) { |
| 3889 | Sequence.SetFailed( |
| 3890 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3891 | return; |
| 3892 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3893 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3894 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3895 | return; |
| 3896 | } |
| 3897 | |
| 3898 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3899 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3900 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3901 | const InitializedEntity &Entity, |
| 3902 | const InitializationKind &Kind, |
| 3903 | Expr *Initializer, |
| 3904 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3905 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3906 | } |
| 3907 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3908 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3909 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3910 | const InitializedEntity &Entity, |
| 3911 | const InitializationKind &Kind, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3912 | InitializationSequence &Sequence, |
| 3913 | InitListExpr *InitList) { |
| 3914 | assert((!InitList || InitList->getNumInits() == 0) && |
| 3915 | "Shouldn't use value-init for non-empty init lists"); |
| 3916 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3917 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3918 | // |
| 3919 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3920 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3921 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3922 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3923 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3924 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3925 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3926 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3927 | bool NeedZeroInitialization = true; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3928 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3929 | // C++98: |
| 3930 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 3931 | // (12.1), then the default constructor for T is called (and the |
| 3932 | // initialization is ill-formed if T has no accessible default |
| 3933 | // constructor); |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3934 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3935 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3936 | } else { |
| 3937 | // C++11: |
| 3938 | // -- if T is a class type (clause 9) with either no default constructor |
| 3939 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3940 | // or deleted, then the object is default-initialized; |
| 3941 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3942 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3943 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3944 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3945 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3946 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3947 | // user-provided or deleted default constructor, then the object is |
| 3948 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3949 | // default-initialized; |
Richard Smith | 6678a05 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 3950 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 3951 | // constructor' part was removed by DR1507. |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3952 | if (NeedZeroInitialization) |
| 3953 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3954 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3955 | // C++03: |
| 3956 | // -- if T is a non-union class type without a user-declared constructor, |
| 3957 | // then every non-static data member and base class component of T is |
| 3958 | // value-initialized; |
| 3959 | // [...] A program that calls for [...] value-initialization of an |
| 3960 | // entity of reference type is ill-formed. |
| 3961 | // |
| 3962 | // C++11 doesn't need this handling, because value-initialization does not |
| 3963 | // occur recursively there, and the implicit default constructor is |
| 3964 | // defined as deleted in the problematic cases. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3965 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3966 | ClassDecl->hasUninitializedReferenceMember()) { |
| 3967 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 3968 | return; |
| 3969 | } |
| 3970 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3971 | // If this is list-value-initialization, pass the empty init list on when |
| 3972 | // building the constructor call. This affects the semantics of a few |
| 3973 | // things (such as whether an explicit default constructor can be called). |
| 3974 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3975 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3976 | bool InitListSyntax = InitList; |
| 3977 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3978 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 3979 | InitListSyntax); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3980 | } |
| 3981 | } |
| 3982 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3983 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3986 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3987 | static void TryDefaultInitialization(Sema &S, |
| 3988 | const InitializedEntity &Entity, |
| 3989 | const InitializationKind &Kind, |
| 3990 | InitializationSequence &Sequence) { |
| 3991 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3992 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3993 | // C++ [dcl.init]p6: |
| 3994 | // To default-initialize an object of type T means: |
| 3995 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3996 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3997 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3998 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3999 | // constructor for T is called (and the initialization is ill-formed if |
| 4000 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4001 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 4002 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 4003 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4004 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4005 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4006 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4007 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4008 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4009 | // 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] | 4010 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4011 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4012 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4013 | return; |
| 4014 | } |
| 4015 | |
| 4016 | // If the destination type has a lifetime property, zero-initialize it. |
| 4017 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 4018 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 4019 | return; |
| 4020 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4021 | } |
| 4022 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4023 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 4024 | /// which enumerates all conversion functions and performs overload resolution |
| 4025 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4026 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4027 | const InitializedEntity &Entity, |
| 4028 | const InitializationKind &Kind, |
| 4029 | Expr *Initializer, |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4030 | InitializationSequence &Sequence, |
| 4031 | bool TopLevelOfInitList) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4032 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4033 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 4034 | QualType SourceType = Initializer->getType(); |
| 4035 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 4036 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4037 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4038 | // Build the candidate set directly in the initialization sequence |
| 4039 | // structure, so that it will persist if we fail. |
| 4040 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 4041 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4042 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4043 | // Determine whether we are allowed to call explicit constructors or |
| 4044 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 4045 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4046 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4047 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4048 | // The type we're converting to is a class type. Enumerate its constructors |
| 4049 | // to see if there is a suitable conversion. |
| 4050 | CXXRecordDecl *DestRecordDecl |
| 4051 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4052 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4053 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4054 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4055 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4056 | // The container holding the constructors can under certain conditions |
| 4057 | // be changed while iterating. To be safe we copy the lookup results |
| 4058 | // to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4059 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4060 | for (SmallVectorImpl<NamedDecl *>::iterator |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4061 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4062 | Con != ConEnd; ++Con) { |
| 4063 | NamedDecl *D = *Con; |
| 4064 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4065 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4066 | // Find the constructor (which may be a template). |
| 4067 | CXXConstructorDecl *Constructor = 0; |
| 4068 | FunctionTemplateDecl *ConstructorTmpl |
| 4069 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4070 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4071 | Constructor = cast<CXXConstructorDecl>( |
| 4072 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4073 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4074 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4075 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4076 | if (!Constructor->isInvalidDecl() && |
| 4077 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4078 | if (ConstructorTmpl) |
| 4079 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 4080 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4081 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4082 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4083 | else |
| 4084 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4085 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4086 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4087 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4088 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4089 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4090 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4091 | |
| 4092 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4093 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4094 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4095 | // The type we're converting from is a class type, enumerate its conversion |
| 4096 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4097 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4098 | // We can only enumerate the conversion functions for a complete type; if |
| 4099 | // the type isn't complete, simply skip this step. |
| 4100 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 4101 | CXXRecordDecl *SourceRecordDecl |
| 4102 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4103 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4104 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4105 | CXXRecordDecl::conversion_iterator> |
| 4106 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 4107 | for (CXXRecordDecl::conversion_iterator |
| 4108 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4109 | NamedDecl *D = *I; |
| 4110 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4111 | if (isa<UsingShadowDecl>(D)) |
| 4112 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4113 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4114 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4115 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4116 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4117 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4118 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4119 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4120 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4121 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4122 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4123 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4124 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4125 | CandidateSet); |
| 4126 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4127 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4128 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4129 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4130 | } |
| 4131 | } |
| 4132 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4133 | |
| 4134 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4135 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4136 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4137 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4138 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4139 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4140 | Result); |
| 4141 | return; |
| 4142 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4143 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4144 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4145 | Function->setReferenced(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4146 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4147 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4148 | if (isa<CXXConstructorDecl>(Function)) { |
| 4149 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4150 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4151 | // cv-unqualified type of the destination. |
| 4152 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4153 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4154 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4155 | return; |
| 4156 | } |
| 4157 | |
| 4158 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4159 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4160 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4161 | // 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] | 4162 | // the resulting temporary object (possible to create an object of |
| 4163 | // a base class type). That copy is not a separate conversion, so |
| 4164 | // we just make a note of the actual destination type (possibly a |
| 4165 | // base class of the type returned by the conversion function) and |
| 4166 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4167 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 4168 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4169 | return; |
| 4170 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4171 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4172 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4173 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4174 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4175 | // If the conversion following the call to the conversion function |
| 4176 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4177 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4178 | Best->FinalConversion.Third) { |
| 4179 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4180 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4181 | ICS.Standard = Best->FinalConversion; |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4182 | Sequence.AddConversionSequenceStep(ICS, DestType, TopLevelOfInitList); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4183 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4184 | } |
| 4185 | |
Richard Smith | 87c2932 | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4186 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 4187 | /// a function with a pointer return type contains a 'return false;' statement. |
| 4188 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 4189 | /// code using that header. |
| 4190 | /// |
| 4191 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 4192 | /// if it's used in a pointer-returning function in a system header. |
| 4193 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 4194 | const InitializedEntity &Entity, |
| 4195 | const Expr *Init) { |
| 4196 | return S.getLangOpts().CPlusPlus11 && |
| 4197 | Entity.getKind() == InitializedEntity::EK_Result && |
| 4198 | Entity.getType()->isPointerType() && |
| 4199 | isa<CXXBoolLiteralExpr>(Init) && |
| 4200 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 4201 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 4202 | } |
| 4203 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4204 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4205 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4206 | |
| 4207 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4208 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4209 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4210 | // Skip parens. |
| 4211 | e = e->IgnoreParens(); |
| 4212 | |
| 4213 | // Skip address-of nodes. |
| 4214 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4215 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4216 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4217 | isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4218 | |
| 4219 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4220 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4221 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4222 | case CK_Dependent: |
| 4223 | case CK_BitCast: |
| 4224 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4225 | case CK_NoOp: |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4226 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4227 | |
| 4228 | case CK_ArrayToPointerDecay: |
| 4229 | return IIK_nonscalar; |
| 4230 | |
| 4231 | case CK_NullToPointer: |
| 4232 | return IIK_okay; |
| 4233 | |
| 4234 | default: |
| 4235 | break; |
| 4236 | } |
| 4237 | |
| 4238 | // 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] | 4239 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4240 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4241 | // load which requires a cleanup. |
| 4242 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4243 | isWeakAccess = true; |
| 4244 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4245 | if (!isAddressOf) return IIK_nonlocal; |
| 4246 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4247 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4248 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4249 | |
| 4250 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4251 | |
| 4252 | // If we have a conditional operator, check both sides. |
| 4253 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4254 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4255 | isWeakAccess)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4256 | return iik; |
| 4257 | |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4258 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4259 | |
| 4260 | // These are never scalar. |
| 4261 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4262 | return IIK_nonscalar; |
| 4263 | |
| 4264 | // Otherwise, it needs to be a null pointer constant. |
| 4265 | } else { |
| 4266 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4267 | ? IIK_okay : IIK_nonlocal); |
| 4268 | } |
| 4269 | |
| 4270 | return IIK_nonlocal; |
| 4271 | } |
| 4272 | |
| 4273 | /// Check whether the given expression is a valid operand for an |
| 4274 | /// indirect copy/restore. |
| 4275 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4276 | assert(src->isRValue()); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4277 | bool isWeakAccess = false; |
| 4278 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4279 | // If isWeakAccess to true, there will be an implicit |
| 4280 | // load which requires a cleanup. |
| 4281 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4282 | S.ExprNeedsCleanups = true; |
| 4283 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4284 | if (iik == IIK_okay) return; |
| 4285 | |
| 4286 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4287 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4288 | << src->getSourceRange(); |
| 4289 | } |
| 4290 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4291 | /// \brief Determine whether we have compatible array types for the |
| 4292 | /// purposes of GNU by-copy array initialization. |
| 4293 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 4294 | const ArrayType *Dest, |
| 4295 | const ArrayType *Source) { |
| 4296 | // If the source and destination array types are equivalent, we're |
| 4297 | // done. |
| 4298 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4299 | return true; |
| 4300 | |
| 4301 | // Make sure that the element types are the same. |
| 4302 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4303 | return false; |
| 4304 | |
| 4305 | // The only mismatch we allow is when the destination is an |
| 4306 | // incomplete array type and the source is a constant array type. |
| 4307 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4308 | } |
| 4309 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4310 | static bool tryObjCWritebackConversion(Sema &S, |
| 4311 | InitializationSequence &Sequence, |
| 4312 | const InitializedEntity &Entity, |
| 4313 | Expr *Initializer) { |
| 4314 | bool ArrayDecay = false; |
| 4315 | QualType ArgType = Initializer->getType(); |
| 4316 | QualType ArgPointee; |
| 4317 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4318 | ArrayDecay = true; |
| 4319 | ArgPointee = ArgArrayType->getElementType(); |
| 4320 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4321 | } |
| 4322 | |
| 4323 | // Handle write-back conversion. |
| 4324 | QualType ConvertedArgType; |
| 4325 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4326 | ConvertedArgType)) |
| 4327 | return false; |
| 4328 | |
| 4329 | // We should copy unless we're passing to an argument explicitly |
| 4330 | // marked 'out'. |
| 4331 | bool ShouldCopy = true; |
| 4332 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4333 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4334 | |
| 4335 | // Do we need an lvalue conversion? |
| 4336 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4337 | ImplicitConversionSequence ICS; |
| 4338 | ICS.setStandard(); |
| 4339 | ICS.Standard.setAsIdentityConversion(); |
| 4340 | |
| 4341 | QualType ResultType; |
| 4342 | if (ArrayDecay) { |
| 4343 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4344 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4345 | } else { |
| 4346 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4347 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4348 | } |
| 4349 | |
| 4350 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4351 | } |
| 4352 | |
| 4353 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4354 | return true; |
| 4355 | } |
| 4356 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4357 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4358 | InitializationSequence &Sequence, |
| 4359 | QualType DestType, |
| 4360 | Expr *Initializer) { |
| 4361 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4362 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4363 | return false; |
| 4364 | |
| 4365 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4366 | return true; |
| 4367 | } |
| 4368 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4369 | // |
| 4370 | // OpenCL 1.2 spec, s6.12.10 |
| 4371 | // |
| 4372 | // The event argument can also be used to associate the |
| 4373 | // async_work_group_copy with a previous async copy allowing |
| 4374 | // an event to be shared by multiple async copies; otherwise |
| 4375 | // event should be zero. |
| 4376 | // |
| 4377 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4378 | InitializationSequence &Sequence, |
| 4379 | QualType DestType, |
| 4380 | Expr *Initializer) { |
| 4381 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4382 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4383 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4384 | return false; |
| 4385 | |
| 4386 | Sequence.AddOCLZeroEventStep(DestType); |
| 4387 | return true; |
| 4388 | } |
| 4389 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4390 | InitializationSequence::InitializationSequence(Sema &S, |
| 4391 | const InitializedEntity &Entity, |
| 4392 | const InitializationKind &Kind, |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4393 | MultiExprArg Args, |
| 4394 | bool TopLevelOfInitList) |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4395 | : FailedCandidateSet(Kind.getLocation()) { |
Richard Smith | b390e49 | 2013-09-21 21:55:46 +0000 | [diff] [blame] | 4396 | InitializeFrom(S, Entity, Kind, Args, TopLevelOfInitList); |
| 4397 | } |
| 4398 | |
| 4399 | void InitializationSequence::InitializeFrom(Sema &S, |
| 4400 | const InitializedEntity &Entity, |
| 4401 | const InitializationKind &Kind, |
| 4402 | MultiExprArg Args, |
| 4403 | bool TopLevelOfInitList) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4404 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4405 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4406 | // Eliminate non-overload placeholder types in the arguments. We |
| 4407 | // need to do this before checking whether types are dependent |
| 4408 | // because lowering a pseudo-object expression might well give us |
| 4409 | // something of dependent type. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4410 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4411 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4412 | // FIXME: should we be doing this here? |
| 4413 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4414 | if (result.isInvalid()) { |
| 4415 | SetFailed(FK_PlaceholderType); |
| 4416 | return; |
| 4417 | } |
| 4418 | Args[I] = result.take(); |
| 4419 | } |
| 4420 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4421 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4422 | // The semantics of initializers are as follows. The destination type is |
| 4423 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4424 | // 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] | 4425 | // 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] | 4426 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4427 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4428 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4429 | if (DestType->isDependentType() || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4430 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4431 | SequenceKind = DependentSequence; |
| 4432 | return; |
| 4433 | } |
| 4434 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4435 | // Almost everything is a normal sequence. |
| 4436 | setSequenceKind(NormalSequence); |
| 4437 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4438 | QualType SourceType; |
| 4439 | Expr *Initializer = 0; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4440 | if (Args.size() == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4441 | Initializer = Args[0]; |
| 4442 | if (!isa<InitListExpr>(Initializer)) |
| 4443 | SourceType = Initializer->getType(); |
| 4444 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4445 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4446 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4447 | // object is list-initialized (8.5.4). |
| 4448 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4449 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4450 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4451 | return; |
| 4452 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4453 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4454 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4455 | // - If the destination type is a reference type, see 8.5.3. |
| 4456 | if (DestType->isReferenceType()) { |
| 4457 | // C++0x [dcl.init.ref]p1: |
| 4458 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4459 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4460 | // by an object that can be converted into a T. |
| 4461 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4462 | if (Args.size() != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4463 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4464 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4465 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4466 | return; |
| 4467 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4468 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4469 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4470 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4471 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4472 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4473 | return; |
| 4474 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4475 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4476 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4477 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4478 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4479 | return; |
| 4480 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4481 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4482 | // - If the destination type is an array of characters, an array of |
| 4483 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4484 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4485 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4486 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4487 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4488 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4489 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4490 | return; |
| 4491 | } |
| 4492 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4493 | if (Initializer) { |
| 4494 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 4495 | case SIF_None: |
| 4496 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 4497 | return; |
| 4498 | case SIF_NarrowStringIntoWideChar: |
| 4499 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 4500 | return; |
| 4501 | case SIF_WideStringIntoChar: |
| 4502 | SetFailed(FK_WideStringIntoCharArray); |
| 4503 | return; |
| 4504 | case SIF_IncompatWideStringIntoWideChar: |
| 4505 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 4506 | return; |
| 4507 | case SIF_Other: |
| 4508 | break; |
| 4509 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4510 | } |
| 4511 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4512 | // Note: as an GNU C extension, we allow initialization of an |
| 4513 | // array from a compound literal that creates an array of the same |
| 4514 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4515 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4516 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4517 | Initializer->getType()->isArrayType()) { |
| 4518 | const ArrayType *SourceAT |
| 4519 | = Context.getAsArrayType(Initializer->getType()); |
| 4520 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4521 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4522 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4523 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4524 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4525 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4526 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4527 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4528 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4529 | // class member of array type from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4530 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4531 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4532 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4533 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4534 | *this); |
| 4535 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4536 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4537 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4538 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 4539 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4540 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4541 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4542 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4543 | return; |
| 4544 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4545 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4546 | // Determine whether we should consider writeback conversions for |
| 4547 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4548 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4549 | Entity.isParameterKind(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4550 | |
| 4551 | // We're at the end of the line for C: it's either a write-back conversion |
| 4552 | // 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] | 4553 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4554 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4555 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4556 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4557 | return; |
| 4558 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4559 | |
| 4560 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4561 | return; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4562 | |
| 4563 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4564 | return; |
| 4565 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4566 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4567 | AddCAssignmentStep(DestType); |
| 4568 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4569 | return; |
| 4570 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4571 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4572 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4573 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4574 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4575 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4576 | // - If the initialization is direct-initialization, or if it is |
| 4577 | // copy-initialization where the cv-unqualified version of the |
| 4578 | // 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] | 4579 | // class of the destination, constructors are considered. [...] |
| 4580 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4581 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4582 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4583 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4584 | TryConstructorInitialization(S, Entity, Kind, Args, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4585 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4586 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4587 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4588 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4589 | // used) to a derived class thereof are enumerated as described in |
| 4590 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4591 | // (13.3). |
| 4592 | else |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4593 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this, |
| 4594 | TopLevelOfInitList); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4595 | return; |
| 4596 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4597 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4598 | if (Args.size() > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4599 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4600 | return; |
| 4601 | } |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4602 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4603 | |
| 4604 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4605 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4606 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4607 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this, |
| 4608 | TopLevelOfInitList); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4609 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4610 | return; |
| 4611 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4612 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4613 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4614 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4615 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4616 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4617 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4618 | |
| 4619 | ImplicitConversionSequence ICS |
| 4620 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4621 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4622 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4623 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4624 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4625 | allowObjCWritebackConversion); |
| 4626 | |
| 4627 | if (ICS.isStandard() && |
| 4628 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4629 | // Objective-C ARC writeback conversion. |
| 4630 | |
| 4631 | // We should copy unless we're passing to an argument explicitly |
| 4632 | // marked 'out'. |
| 4633 | bool ShouldCopy = true; |
| 4634 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4635 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4636 | |
| 4637 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4638 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4639 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4640 | ImplicitConversionSequence LvalueICS; |
| 4641 | LvalueICS.setStandard(); |
| 4642 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4643 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4644 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4645 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4646 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4647 | |
| 4648 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4649 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4650 | DeclAccessPair dap; |
Richard Smith | 87c2932 | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4651 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 4652 | AddZeroInitializationStep(Entity.getType()); |
| 4653 | } else if (Initializer->getType() == Context.OverloadTy && |
| 4654 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 4655 | false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4656 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4657 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4658 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4659 | } else { |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 4660 | AddConversionSequenceStep(ICS, Entity.getType(), TopLevelOfInitList); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4661 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4662 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4663 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4664 | } |
| 4665 | |
| 4666 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4667 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4668 | StepEnd = Steps.end(); |
| 4669 | Step != StepEnd; ++Step) |
| 4670 | Step->Destroy(); |
| 4671 | } |
| 4672 | |
| 4673 | //===----------------------------------------------------------------------===// |
| 4674 | // Perform initialization |
| 4675 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4676 | static Sema::AssignmentAction |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4677 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4678 | switch(Entity.getKind()) { |
| 4679 | case InitializedEntity::EK_Variable: |
| 4680 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4681 | case InitializedEntity::EK_Exception: |
| 4682 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4683 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4684 | return Sema::AA_Initializing; |
| 4685 | |
| 4686 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4687 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4688 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4689 | return Sema::AA_Sending; |
| 4690 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4691 | return Sema::AA_Passing; |
| 4692 | |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4693 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 4694 | if (Entity.getDecl() && |
| 4695 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4696 | return Sema::AA_Sending; |
| 4697 | |
| 4698 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
| 4699 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4700 | case InitializedEntity::EK_Result: |
| 4701 | return Sema::AA_Returning; |
| 4702 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4703 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | f5200d6 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 4704 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4705 | // FIXME: Can we tell apart casting vs. converting? |
| 4706 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4707 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4708 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4709 | case InitializedEntity::EK_ArrayElement: |
| 4710 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4711 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4712 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4713 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4714 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4715 | return Sema::AA_Initializing; |
| 4716 | } |
| 4717 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4718 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4719 | } |
| 4720 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4721 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4722 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4723 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4724 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4725 | case InitializedEntity::EK_ArrayElement: |
| 4726 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4727 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4728 | case InitializedEntity::EK_New: |
| 4729 | case InitializedEntity::EK_Variable: |
| 4730 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4731 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4732 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4733 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4734 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4735 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4736 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4737 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4738 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4739 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4740 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4741 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4742 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4743 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4744 | return true; |
| 4745 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4746 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4747 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4748 | } |
| 4749 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4750 | /// \brief Whether the given entity, when initialized with an object |
| 4751 | /// created for that initialization, requires destruction. |
| 4752 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4753 | switch (Entity.getKind()) { |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4754 | case InitializedEntity::EK_Result: |
| 4755 | case InitializedEntity::EK_New: |
| 4756 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4757 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +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: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4760 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4761 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4762 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4763 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4764 | case InitializedEntity::EK_Member: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4765 | case InitializedEntity::EK_Variable: |
| 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 | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4768 | case InitializedEntity::EK_Temporary: |
| 4769 | case InitializedEntity::EK_ArrayElement: |
| 4770 | case InitializedEntity::EK_Exception: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4771 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4772 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4773 | return true; |
| 4774 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4775 | |
| 4776 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4779 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4780 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4781 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4782 | OverloadCandidateSet &CandidateSet, |
| 4783 | CXXRecordDecl *Class, |
| 4784 | Expr *CurInitExpr) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4785 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4786 | // The container holding the constructors can under certain conditions |
| 4787 | // be changed while iterating (e.g. because of deserialization). |
| 4788 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4789 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4790 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4791 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4792 | NamedDecl *D = *CI; |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4793 | CXXConstructorDecl *Constructor = 0; |
| 4794 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4795 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4796 | // Handle copy/moveconstructors, only. |
| 4797 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4798 | !Constructor->isCopyOrMoveConstructor() || |
| 4799 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4800 | continue; |
| 4801 | |
| 4802 | DeclAccessPair FoundDecl |
| 4803 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4804 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4805 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4806 | continue; |
| 4807 | } |
| 4808 | |
| 4809 | // Handle constructor templates. |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4810 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4811 | if (ConstructorTmpl->isInvalidDecl()) |
| 4812 | continue; |
| 4813 | |
| 4814 | Constructor = cast<CXXConstructorDecl>( |
| 4815 | ConstructorTmpl->getTemplatedDecl()); |
| 4816 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4817 | continue; |
| 4818 | |
| 4819 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4820 | // candidates? |
| 4821 | DeclAccessPair FoundDecl |
| 4822 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4823 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4824 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4825 | } |
| 4826 | } |
| 4827 | |
| 4828 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4829 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4830 | Expr *Initializer) { |
| 4831 | switch (Entity.getKind()) { |
| 4832 | case InitializedEntity::EK_Result: |
| 4833 | return Entity.getReturnLoc(); |
| 4834 | |
| 4835 | case InitializedEntity::EK_Exception: |
| 4836 | return Entity.getThrowLoc(); |
| 4837 | |
| 4838 | case InitializedEntity::EK_Variable: |
| 4839 | return Entity.getDecl()->getLocation(); |
| 4840 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4841 | case InitializedEntity::EK_LambdaCapture: |
| 4842 | return Entity.getCaptureLoc(); |
| 4843 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4844 | case InitializedEntity::EK_ArrayElement: |
| 4845 | case InitializedEntity::EK_Member: |
| 4846 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4847 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4848 | case InitializedEntity::EK_Temporary: |
| 4849 | case InitializedEntity::EK_New: |
| 4850 | case InitializedEntity::EK_Base: |
| 4851 | case InitializedEntity::EK_Delegating: |
| 4852 | case InitializedEntity::EK_VectorElement: |
| 4853 | case InitializedEntity::EK_ComplexElement: |
| 4854 | case InitializedEntity::EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4855 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4856 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4857 | return Initializer->getLocStart(); |
| 4858 | } |
| 4859 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4860 | } |
| 4861 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4862 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4863 | /// provided by the given initializer by calling the appropriate copy |
| 4864 | /// constructor. |
| 4865 | /// |
| 4866 | /// \param S The Sema object used for type-checking. |
| 4867 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4868 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4869 | /// the type of the initializer expression or a superclass thereof. |
| 4870 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4871 | /// \param Entity The entity being initialized. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4872 | /// |
| 4873 | /// \param CurInit The initializer expression. |
| 4874 | /// |
| 4875 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4876 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4877 | /// an rvalue. |
| 4878 | /// |
| 4879 | /// \returns An expression that copies the initializer expression into |
| 4880 | /// a temporary object, or an error expression if a copy could not be |
| 4881 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4882 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4883 | QualType T, |
| 4884 | const InitializedEntity &Entity, |
| 4885 | ExprResult CurInit, |
| 4886 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4887 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4888 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4889 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4890 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4891 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4892 | if (!Class) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4893 | return CurInit; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4894 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4895 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4896 | // When certain criteria are met, an implementation is allowed to |
| 4897 | // omit the copy/move construction of a class object, even if the |
| 4898 | // copy/move constructor and/or destructor for the object have |
| 4899 | // side effects. [...] |
| 4900 | // - when a temporary class object that has not been bound to a |
| 4901 | // reference (12.2) would be copied/moved to a class object |
| 4902 | // with the same cv-unqualified type, the copy/move operation |
| 4903 | // can be omitted by constructing the temporary object |
| 4904 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4905 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4906 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4907 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4908 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4909 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4910 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4911 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4912 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4913 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4914 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4915 | return CurInit; |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4916 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4917 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4918 | // Only consider constructors and constructor templates. Per |
| 4919 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4920 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4921 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4922 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4923 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4924 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4925 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4926 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4927 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4928 | case OR_Success: |
| 4929 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4930 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4931 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4932 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4933 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4934 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4935 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4936 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4937 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4938 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4939 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4940 | return CurInit; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4941 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4942 | case OR_Ambiguous: |
| 4943 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4944 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4945 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4946 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4947 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4948 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4949 | case OR_Deleted: |
| 4950 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4951 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4952 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4953 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4954 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4957 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4958 | SmallVector<Expr*, 8> ConstructorArgs; |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4959 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4960 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4961 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4962 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4963 | |
| 4964 | if (IsExtraneousCopy) { |
| 4965 | // If this is a totally extraneous copy for C++03 reference |
| 4966 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4967 | // expression. We don't generate an (elided) copy operation here |
| 4968 | // because doing so would require us to pass down a flag to avoid |
| 4969 | // infinite recursion, where each step adds another extraneous, |
| 4970 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4971 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4972 | // Instantiate the default arguments of any extra parameters in |
| 4973 | // the selected copy constructor, as if we were going to create a |
| 4974 | // proper call to the copy constructor. |
| 4975 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4976 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4977 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4978 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4979 | break; |
| 4980 | |
| 4981 | // Build the default argument expression; we don't actually care |
| 4982 | // if this succeeds or not, because this routine will complain |
| 4983 | // if there was a problem. |
| 4984 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4985 | } |
| 4986 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4987 | return S.Owned(CurInitExpr); |
| 4988 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4989 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4990 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4991 | // constructor call (we might have derived-to-base conversions, or |
| 4992 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4993 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4994 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4995 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4996 | // Actually perform the constructor call. |
| 4997 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4998 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4999 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5000 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5001 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5002 | CXXConstructExpr::CK_Complete, |
| 5003 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5004 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 5005 | // If we're supposed to bind temporaries, do so. |
| 5006 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 5007 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5008 | return CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5009 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5010 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5011 | /// \brief Check whether elidable copy construction for binding a reference to |
| 5012 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 5013 | /// -Wc++98-compat. |
| 5014 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 5015 | const InitializedEntity &Entity, |
| 5016 | Expr *CurInitExpr) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5017 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5018 | |
| 5019 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 5020 | if (!Record) |
| 5021 | return; |
| 5022 | |
| 5023 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 5024 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 5025 | == DiagnosticsEngine::Ignored) |
| 5026 | return; |
| 5027 | |
| 5028 | // Find constructors which would have been considered. |
| 5029 | OverloadCandidateSet CandidateSet(Loc); |
| 5030 | LookupCopyAndMoveConstructors( |
| 5031 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 5032 | |
| 5033 | // Perform overload resolution. |
| 5034 | OverloadCandidateSet::iterator Best; |
| 5035 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 5036 | |
| 5037 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 5038 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 5039 | << CurInitExpr->getSourceRange(); |
| 5040 | |
| 5041 | switch (OR) { |
| 5042 | case OR_Success: |
| 5043 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 5044 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5045 | // FIXME: Check default arguments as far as that's possible. |
| 5046 | break; |
| 5047 | |
| 5048 | case OR_No_Viable_Function: |
| 5049 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5050 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5051 | break; |
| 5052 | |
| 5053 | case OR_Ambiguous: |
| 5054 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 5055 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5056 | break; |
| 5057 | |
| 5058 | case OR_Deleted: |
| 5059 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5060 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5061 | break; |
| 5062 | } |
| 5063 | } |
| 5064 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5065 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 5066 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5067 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5068 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 5069 | return; |
| 5070 | |
| 5071 | if (Entity.getDecl()->getDeclName()) |
| 5072 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 5073 | << Entity.getDecl()->getDeclName(); |
| 5074 | else |
| 5075 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 5076 | } |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5077 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 5078 | Entity.getMethodDecl()) |
| 5079 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 5080 | diag::note_method_return_type_change) |
| 5081 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5082 | } |
| 5083 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5084 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 5085 | return s.Kind == InitializationSequence::SK_BindReference || |
| 5086 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 5087 | } |
| 5088 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5089 | /// Returns true if the parameters describe a constructor initialization of |
| 5090 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 5091 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 5092 | const InitializationKind &Kind, |
| 5093 | unsigned NumArgs) { |
| 5094 | switch (Entity.getKind()) { |
| 5095 | case InitializedEntity::EK_Temporary: |
| 5096 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5097 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5098 | break; |
| 5099 | default: |
| 5100 | return false; |
| 5101 | } |
| 5102 | |
| 5103 | switch (Kind.getKind()) { |
| 5104 | case InitializationKind::IK_DirectList: |
| 5105 | return true; |
| 5106 | // FIXME: Hack to work around cast weirdness. |
| 5107 | case InitializationKind::IK_Direct: |
| 5108 | case InitializationKind::IK_Value: |
| 5109 | return NumArgs != 1; |
| 5110 | default: |
| 5111 | return false; |
| 5112 | } |
| 5113 | } |
| 5114 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5115 | static ExprResult |
| 5116 | PerformConstructorInitialization(Sema &S, |
| 5117 | const InitializedEntity &Entity, |
| 5118 | const InitializationKind &Kind, |
| 5119 | MultiExprArg Args, |
| 5120 | const InitializationSequence::Step& Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5121 | bool &ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5122 | bool IsListInitialization, |
| 5123 | SourceLocation LBraceLoc, |
| 5124 | SourceLocation RBraceLoc) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5125 | unsigned NumArgs = Args.size(); |
| 5126 | CXXConstructorDecl *Constructor |
| 5127 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 5128 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 5129 | |
| 5130 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5131 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5132 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 5133 | ? Kind.getEqualLoc() |
| 5134 | : Kind.getLocation(); |
| 5135 | |
| 5136 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 5137 | // Force even a trivial, implicit default constructor to be |
| 5138 | // semantically checked. We do this explicitly because we don't build |
| 5139 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 5140 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5141 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 5142 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5143 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 5144 | } |
| 5145 | |
| 5146 | ExprResult CurInit = S.Owned((Expr *)0); |
| 5147 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5148 | // C++ [over.match.copy]p1: |
| 5149 | // - When initializing a temporary to be bound to the first parameter |
| 5150 | // of a constructor that takes a reference to possibly cv-qualified |
| 5151 | // T as its first argument, called with a single argument in the |
| 5152 | // context of direct-initialization, explicit conversion functions |
| 5153 | // are also considered. |
| 5154 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 5155 | Args.size() == 1 && |
| 5156 | Constructor->isCopyOrMoveConstructor(); |
| 5157 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5158 | // Determine the arguments required to actually perform the constructor |
| 5159 | // call. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5160 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5161 | Loc, ConstructorArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5162 | AllowExplicitConv, |
| 5163 | IsListInitialization)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5164 | return ExprError(); |
| 5165 | |
| 5166 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5167 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5168 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5169 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5170 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 5171 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5172 | |
| 5173 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5174 | if (!TSInfo) |
| 5175 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5176 | SourceRange ParenOrBraceRange = |
| 5177 | (Kind.getKind() == InitializationKind::IK_DirectList) |
| 5178 | ? SourceRange(LBraceLoc, RBraceLoc) |
| 5179 | : Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5180 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5181 | CurInit = S.Owned( |
| 5182 | new (S.Context) CXXTemporaryObjectExpr(S.Context, Constructor, |
| 5183 | TSInfo, ConstructorArgs, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5184 | ParenOrBraceRange, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5185 | HadMultipleCandidates, |
Enea Zaffanella | 14dcaa9 | 2013-09-07 11:22:02 +0000 | [diff] [blame] | 5186 | IsListInitialization, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5187 | ConstructorInitRequiresZeroInit)); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5188 | } else { |
| 5189 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 5190 | CXXConstructExpr::CK_Complete; |
| 5191 | |
| 5192 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5193 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 5194 | CXXConstructExpr::CK_VirtualBase : |
| 5195 | CXXConstructExpr::CK_NonVirtualBase; |
| 5196 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 5197 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 5198 | } |
| 5199 | |
| 5200 | // Only get the parenthesis range if it is a direct construction. |
| 5201 | SourceRange parenRange = |
| 5202 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 5203 | Kind.getParenRange() : SourceRange(); |
| 5204 | |
| 5205 | // If the entity allows NRVO, mark the construction as elidable |
| 5206 | // unconditionally. |
| 5207 | if (Entity.allowsNRVO()) |
| 5208 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5209 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5210 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5211 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5212 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5213 | ConstructorInitRequiresZeroInit, |
| 5214 | ConstructKind, |
| 5215 | parenRange); |
| 5216 | else |
| 5217 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5218 | Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5219 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5220 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5221 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5222 | ConstructorInitRequiresZeroInit, |
| 5223 | ConstructKind, |
| 5224 | parenRange); |
| 5225 | } |
| 5226 | if (CurInit.isInvalid()) |
| 5227 | return ExprError(); |
| 5228 | |
| 5229 | // Only check access if all of that succeeded. |
| 5230 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 5231 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5232 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 5233 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5234 | |
| 5235 | if (shouldBindAsTemporary(Entity)) |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5236 | CurInit = S.MaybeBindToTemporary(CurInit.take()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5237 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5238 | return CurInit; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5241 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 5242 | /// longer than the current full-expression. Conservatively returns false if |
| 5243 | /// it's unclear. |
| 5244 | static bool |
| 5245 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 5246 | const InitializedEntity *Top = &Entity; |
| 5247 | while (Top->getParent()) |
| 5248 | Top = Top->getParent(); |
| 5249 | |
| 5250 | switch (Top->getKind()) { |
| 5251 | case InitializedEntity::EK_Variable: |
| 5252 | case InitializedEntity::EK_Result: |
| 5253 | case InitializedEntity::EK_Exception: |
| 5254 | case InitializedEntity::EK_Member: |
| 5255 | case InitializedEntity::EK_New: |
| 5256 | case InitializedEntity::EK_Base: |
| 5257 | case InitializedEntity::EK_Delegating: |
| 5258 | return true; |
| 5259 | |
| 5260 | case InitializedEntity::EK_ArrayElement: |
| 5261 | case InitializedEntity::EK_VectorElement: |
| 5262 | case InitializedEntity::EK_BlockElement: |
| 5263 | case InitializedEntity::EK_ComplexElement: |
| 5264 | // Could not determine what the full initialization is. Assume it might not |
| 5265 | // outlive the full-expression. |
| 5266 | return false; |
| 5267 | |
| 5268 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5269 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5270 | case InitializedEntity::EK_Temporary: |
| 5271 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5272 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5273 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5274 | // The entity being initialized might not outlive the full-expression. |
| 5275 | return false; |
| 5276 | } |
| 5277 | |
| 5278 | llvm_unreachable("unknown entity kind"); |
| 5279 | } |
| 5280 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5281 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 5282 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 5283 | /// the initialization of \p Entity. |
| 5284 | static const ValueDecl * |
| 5285 | getDeclForTemporaryLifetimeExtension(const InitializedEntity &Entity, |
| 5286 | const ValueDecl *FallbackDecl = 0) { |
| 5287 | // C++11 [class.temporary]p5: |
| 5288 | switch (Entity.getKind()) { |
| 5289 | case InitializedEntity::EK_Variable: |
| 5290 | // The temporary [...] persists for the lifetime of the reference |
| 5291 | return Entity.getDecl(); |
| 5292 | |
| 5293 | case InitializedEntity::EK_Member: |
| 5294 | // For subobjects, we look at the complete object. |
| 5295 | if (Entity.getParent()) |
| 5296 | return getDeclForTemporaryLifetimeExtension(*Entity.getParent(), |
| 5297 | Entity.getDecl()); |
| 5298 | |
| 5299 | // except: |
| 5300 | // -- A temporary bound to a reference member in a constructor's |
| 5301 | // ctor-initializer persists until the constructor exits. |
| 5302 | return Entity.getDecl(); |
| 5303 | |
| 5304 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5305 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5306 | // -- A temporary bound to a reference parameter in a function call |
| 5307 | // persists until the completion of the full-expression containing |
| 5308 | // the call. |
| 5309 | case InitializedEntity::EK_Result: |
| 5310 | // -- The lifetime of a temporary bound to the returned value in a |
| 5311 | // function return statement is not extended; the temporary is |
| 5312 | // destroyed at the end of the full-expression in the return statement. |
| 5313 | case InitializedEntity::EK_New: |
| 5314 | // -- A temporary bound to a reference in a new-initializer persists |
| 5315 | // until the completion of the full-expression containing the |
| 5316 | // new-initializer. |
| 5317 | return 0; |
| 5318 | |
| 5319 | case InitializedEntity::EK_Temporary: |
| 5320 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5321 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5322 | // We don't yet know the storage duration of the surrounding temporary. |
| 5323 | // Assume it's got full-expression duration for now, it will patch up our |
| 5324 | // storage duration if that's not correct. |
| 5325 | return 0; |
| 5326 | |
| 5327 | case InitializedEntity::EK_ArrayElement: |
| 5328 | // For subobjects, we look at the complete object. |
| 5329 | return getDeclForTemporaryLifetimeExtension(*Entity.getParent(), |
| 5330 | FallbackDecl); |
| 5331 | |
| 5332 | case InitializedEntity::EK_Base: |
| 5333 | case InitializedEntity::EK_Delegating: |
| 5334 | // We can reach this case for aggregate initialization in a constructor: |
| 5335 | // struct A { int &&r; }; |
| 5336 | // struct B : A { B() : A{0} {} }; |
| 5337 | // In this case, use the innermost field decl as the context. |
| 5338 | return FallbackDecl; |
| 5339 | |
| 5340 | case InitializedEntity::EK_BlockElement: |
| 5341 | case InitializedEntity::EK_LambdaCapture: |
| 5342 | case InitializedEntity::EK_Exception: |
| 5343 | case InitializedEntity::EK_VectorElement: |
| 5344 | case InitializedEntity::EK_ComplexElement: |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5345 | return 0; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5346 | } |
Benjamin Kramer | 6f773e8 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 5347 | llvm_unreachable("unknown entity kind"); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5348 | } |
| 5349 | |
| 5350 | static void performLifetimeExtension(Expr *Init, const ValueDecl *ExtendingD); |
| 5351 | |
| 5352 | /// Update a glvalue expression that is used as the initializer of a reference |
| 5353 | /// to note that its lifetime is extended. |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5354 | /// \return \c true if any temporary had its lifetime extended. |
| 5355 | static bool performReferenceExtension(Expr *Init, const ValueDecl *ExtendingD) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5356 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 5357 | if (ILE->getNumInits() == 1 && ILE->isGLValue()) { |
| 5358 | // This is just redundant braces around an initializer. Step over it. |
| 5359 | Init = ILE->getInit(0); |
| 5360 | } |
| 5361 | } |
| 5362 | |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5363 | // Walk past any constructs which we can lifetime-extend across. |
| 5364 | Expr *Old; |
| 5365 | do { |
| 5366 | Old = Init; |
| 5367 | |
| 5368 | // Step over any subobject adjustments; we may have a materialized |
| 5369 | // temporary inside them. |
| 5370 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5371 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5372 | Init = const_cast<Expr *>( |
| 5373 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5374 | |
| 5375 | // Per current approach for DR1376, look through casts to reference type |
| 5376 | // when performing lifetime extension. |
| 5377 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 5378 | if (CE->getSubExpr()->isGLValue()) |
| 5379 | Init = CE->getSubExpr(); |
| 5380 | |
| 5381 | // FIXME: Per DR1213, subscripting on an array temporary produces an xvalue. |
| 5382 | // It's unclear if binding a reference to that xvalue extends the array |
| 5383 | // temporary. |
| 5384 | } while (Init != Old); |
| 5385 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5386 | if (MaterializeTemporaryExpr *ME = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 5387 | // Update the storage duration of the materialized temporary. |
| 5388 | // FIXME: Rebuild the expression instead of mutating it. |
| 5389 | ME->setExtendingDecl(ExtendingD); |
| 5390 | performLifetimeExtension(ME->GetTemporaryExpr(), ExtendingD); |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5391 | return true; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5392 | } |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5393 | |
| 5394 | return false; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5395 | } |
| 5396 | |
| 5397 | /// Update a prvalue expression that is going to be materialized as a |
| 5398 | /// lifetime-extended temporary. |
| 5399 | static void performLifetimeExtension(Expr *Init, const ValueDecl *ExtendingD) { |
| 5400 | // Dig out the expression which constructs the extended temporary. |
| 5401 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5402 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5403 | Init = const_cast<Expr *>( |
| 5404 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5405 | |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5406 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 5407 | Init = BTE->getSubExpr(); |
| 5408 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5409 | if (CXXStdInitializerListExpr *ILE = |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5410 | dyn_cast<CXXStdInitializerListExpr>(Init)) { |
| 5411 | performReferenceExtension(ILE->getSubExpr(), ExtendingD); |
| 5412 | return; |
| 5413 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5414 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5415 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5416 | if (ILE->getType()->isArrayType()) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5417 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
| 5418 | performLifetimeExtension(ILE->getInit(I), ExtendingD); |
| 5419 | return; |
| 5420 | } |
| 5421 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5422 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5423 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 5424 | |
| 5425 | // If we lifetime-extend a braced initializer which is initializing an |
| 5426 | // aggregate, and that aggregate contains reference members which are |
| 5427 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 5428 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 5429 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
| 5430 | performReferenceExtension(ILE->getInit(0), ExtendingD); |
| 5431 | else { |
| 5432 | unsigned Index = 0; |
| 5433 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 5434 | E = RD->field_end(); |
| 5435 | I != E; ++I) { |
Richard Smith | 3c3af14 | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 5436 | if (Index >= ILE->getNumInits()) |
| 5437 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5438 | if (I->isUnnamedBitfield()) |
| 5439 | continue; |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5440 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5441 | if (I->getType()->isReferenceType()) |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5442 | performReferenceExtension(SubInit, ExtendingD); |
| 5443 | else if (isa<InitListExpr>(SubInit) || |
| 5444 | isa<CXXStdInitializerListExpr>(SubInit)) |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5445 | // This may be either aggregate-initialization of a member or |
| 5446 | // initialization of a std::initializer_list object. Either way, |
| 5447 | // we should recursively lifetime-extend that initializer. |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5448 | performLifetimeExtension(SubInit, ExtendingD); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5449 | ++Index; |
| 5450 | } |
| 5451 | } |
| 5452 | } |
| 5453 | } |
| 5454 | } |
| 5455 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5456 | static void warnOnLifetimeExtension(Sema &S, const InitializedEntity &Entity, |
| 5457 | const Expr *Init, bool IsInitializerList, |
| 5458 | const ValueDecl *ExtendingDecl) { |
| 5459 | // Warn if a field lifetime-extends a temporary. |
| 5460 | if (isa<FieldDecl>(ExtendingDecl)) { |
| 5461 | if (IsInitializerList) { |
| 5462 | S.Diag(Init->getExprLoc(), diag::warn_dangling_std_initializer_list) |
| 5463 | << /*at end of constructor*/true; |
| 5464 | return; |
| 5465 | } |
| 5466 | |
| 5467 | bool IsSubobjectMember = false; |
| 5468 | for (const InitializedEntity *Ent = Entity.getParent(); Ent; |
| 5469 | Ent = Ent->getParent()) { |
| 5470 | if (Ent->getKind() != InitializedEntity::EK_Base) { |
| 5471 | IsSubobjectMember = true; |
| 5472 | break; |
| 5473 | } |
| 5474 | } |
| 5475 | S.Diag(Init->getExprLoc(), |
| 5476 | diag::warn_bind_ref_member_to_temporary) |
| 5477 | << ExtendingDecl << Init->getSourceRange() |
| 5478 | << IsSubobjectMember << IsInitializerList; |
| 5479 | if (IsSubobjectMember) |
| 5480 | S.Diag(ExtendingDecl->getLocation(), |
| 5481 | diag::note_ref_subobject_of_member_declared_here); |
| 5482 | else |
| 5483 | S.Diag(ExtendingDecl->getLocation(), |
| 5484 | diag::note_ref_or_ptr_member_declared_here) |
| 5485 | << /*is pointer*/false; |
| 5486 | } |
| 5487 | } |
| 5488 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5489 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 5490 | const ImplicitConversionSequence &ICS, |
| 5491 | QualType PreNarrowingType, |
| 5492 | QualType EntityType, |
| 5493 | const Expr *PostInit); |
| 5494 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5495 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5496 | InitializationSequence::Perform(Sema &S, |
| 5497 | const InitializedEntity &Entity, |
| 5498 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5499 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5500 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5501 | if (Failed()) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5502 | Diagnose(S, Entity, Kind, Args); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5503 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5504 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5505 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5506 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5507 | // If the declaration is a non-dependent, incomplete array type |
| 5508 | // that has an initializer, then its type will be completed once |
| 5509 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5510 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5511 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5512 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5513 | if (const IncompleteArrayType *ArrayT |
| 5514 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5515 | // FIXME: We don't currently have the ability to accurately |
| 5516 | // compute the length of an initializer list without |
| 5517 | // performing full type-checking of the initializer list |
| 5518 | // (since we have to determine where braces are implicitly |
| 5519 | // introduced and such). So, we fall back to making the array |
| 5520 | // type a dependently-sized array type with no specified |
| 5521 | // bound. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5522 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5523 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5524 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5525 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5526 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5527 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5528 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5529 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5530 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5531 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5532 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5533 | } |
| 5534 | |
| 5535 | *ResultType |
| 5536 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 5537 | /*NumElts=*/0, |
| 5538 | ArrayT->getSizeModifier(), |
| 5539 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5540 | Brackets); |
| 5541 | } |
| 5542 | |
| 5543 | } |
| 5544 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5545 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5546 | !Kind.isExplicitCast()) { |
| 5547 | // Rebuild the ParenListExpr. |
| 5548 | SourceRange ParenRange = Kind.getParenRange(); |
| 5549 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5550 | Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5551 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5552 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5553 | Kind.isExplicitCast() || |
| 5554 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5555 | return ExprResult(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5556 | } |
| 5557 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5558 | // No steps means no initialization. |
| 5559 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5560 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5561 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5562 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5563 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5564 | !Entity.isParameterKind()) { |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5565 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5566 | // from an initializer list. For parameters, we produce a better warning |
| 5567 | // elsewhere. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5568 | Expr *Init = Args[0]; |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5569 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5570 | << Init->getSourceRange(); |
| 5571 | } |
| 5572 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5573 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5574 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5575 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5576 | Entity.getType()->isPointerType() && |
| 5577 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5578 | Expr *Init = Args[0]; |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5579 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5580 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5581 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5582 | << Init->getSourceRange(); |
| 5583 | } |
| 5584 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5585 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5586 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5587 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5588 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5589 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5590 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5591 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5592 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5593 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5594 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5595 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5596 | // grab the only argument out the Args and place it into the "current" |
| 5597 | // initializer. |
| 5598 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5599 | case SK_ResolveAddressOfOverloadedFunction: |
| 5600 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5601 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5602 | case SK_CastDerivedToBaseLValue: |
| 5603 | case SK_BindReference: |
| 5604 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5605 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5606 | case SK_UserConversion: |
| 5607 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5608 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5609 | case SK_QualificationConversionRValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5610 | case SK_LValueToRValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5611 | case SK_ConversionSequence: |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5612 | case SK_ConversionSequenceNoNarrowing: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5613 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5614 | case SK_UnwrapInitList: |
| 5615 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5616 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5617 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5618 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5619 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5620 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5621 | case SK_PassByIndirectCopyRestore: |
| 5622 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5623 | case SK_ProduceObjCObject: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5624 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5625 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5626 | case SK_OCLZeroEvent: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5627 | assert(Args.size() == 1); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5628 | CurInit = Args[0]; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5629 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5630 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5631 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5632 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5633 | case SK_ConstructorInitialization: |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5634 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5635 | case SK_ZeroInitialization: |
| 5636 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5637 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5638 | |
| 5639 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5640 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5641 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5642 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5643 | Step != StepEnd; ++Step) { |
| 5644 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5645 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5646 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5647 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5648 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5649 | switch (Step->Kind) { |
| 5650 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5651 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5652 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5653 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5654 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5655 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5656 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5657 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5658 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5659 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5660 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5661 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5662 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5663 | case SK_CastDerivedToBaseLValue: { |
| 5664 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5665 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5666 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5667 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5668 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5669 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5670 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5671 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5672 | CurInit.get()->getLocStart(), |
| 5673 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5674 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5675 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5676 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5677 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5678 | QualType T = SourceType; |
| 5679 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5680 | T = Pointer->getPointeeType(); |
| 5681 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5682 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5683 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5684 | } |
| 5685 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5686 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5687 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5688 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5689 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5690 | VK_XValue : |
| 5691 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5692 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 5693 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5694 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5695 | CurInit.get(), |
| 5696 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5697 | break; |
| 5698 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5699 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5700 | case SK_BindReference: |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5701 | // References cannot bind to bit-fields (C++ [dcl.init.ref]p5). |
| 5702 | if (CurInit.get()->refersToBitField()) { |
| 5703 | // We don't necessarily have an unambiguous source bit-field. |
| 5704 | FieldDecl *BitField = CurInit.get()->getSourceBitField(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5705 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5706 | << Entity.getType().isVolatileQualified() |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5707 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 5708 | << (BitField != NULL) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5709 | << CurInit.get()->getSourceRange(); |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5710 | if (BitField) |
| 5711 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 5712 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5713 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5714 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5715 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5716 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5717 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5718 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5719 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5720 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5721 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5722 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5723 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5724 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5725 | // Reference binding does not have any corresponding ASTs. |
| 5726 | |
| 5727 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5728 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5729 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5730 | |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5731 | // Even though we didn't materialize a temporary, the binding may still |
| 5732 | // extend the lifetime of a temporary. This happens if we bind a reference |
| 5733 | // to the result of a cast to reference type. |
| 5734 | if (const ValueDecl *ExtendingDecl = |
| 5735 | getDeclForTemporaryLifetimeExtension(Entity)) { |
| 5736 | if (performReferenceExtension(CurInit.get(), ExtendingDecl)) |
| 5737 | warnOnLifetimeExtension(S, Entity, CurInit.get(), false, |
| 5738 | ExtendingDecl); |
| 5739 | } |
| 5740 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5741 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5742 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5743 | case SK_BindReferenceToTemporary: { |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5744 | // Make sure the "temporary" is actually an rvalue. |
| 5745 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5746 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5747 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5748 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5749 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5750 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5751 | // Maybe lifetime-extend the temporary's subobjects to match the |
| 5752 | // entity's lifetime. |
| 5753 | const ValueDecl *ExtendingDecl = |
| 5754 | getDeclForTemporaryLifetimeExtension(Entity); |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 5755 | if (ExtendingDecl) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5756 | performLifetimeExtension(CurInit.get(), ExtendingDecl); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5757 | warnOnLifetimeExtension(S, Entity, CurInit.get(), false, ExtendingDecl); |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 5758 | } |
| 5759 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5760 | // Materialize the temporary into memory. |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5761 | MaterializeTemporaryExpr *MTE = new (S.Context) MaterializeTemporaryExpr( |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5762 | Entity.getType().getNonReferenceType(), CurInit.get(), |
| 5763 | Entity.getType()->isLValueReferenceType(), ExtendingDecl); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5764 | |
| 5765 | // 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] | 5766 | // need cleanups. Likewise if we're extending this temporary to automatic |
| 5767 | // storage duration -- we need to register its cleanup during the |
| 5768 | // full-expression's cleanups. |
| 5769 | if ((S.getLangOpts().ObjCAutoRefCount && |
| 5770 | MTE->getType()->isObjCLifetimeType()) || |
| 5771 | (MTE->getStorageDuration() == SD_Automatic && |
| 5772 | MTE->getType().isDestructedType())) |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5773 | S.ExprNeedsCleanups = true; |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5774 | |
| 5775 | CurInit = S.Owned(MTE); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5776 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5777 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5778 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5779 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5780 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5781 | /*IsExtraneousCopy=*/true); |
| 5782 | break; |
| 5783 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5784 | case SK_UserConversion: { |
| 5785 | // We have a user-defined conversion that invokes either a constructor |
| 5786 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5787 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5788 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5789 | FunctionDecl *Fn = Step->Function.Function; |
| 5790 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5791 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5792 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5793 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5794 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5795 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5796 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5797 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5798 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5799 | // Determine the arguments required to actually perform the constructor |
| 5800 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5801 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5802 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5803 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5804 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5805 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5806 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5807 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5808 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5809 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5810 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5811 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5812 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5813 | CXXConstructExpr::CK_Complete, |
| 5814 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5815 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5816 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5817 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5818 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5819 | FoundFn.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5820 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5821 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5822 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5823 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5824 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5825 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5826 | S.IsDerivedFrom(SourceType, Class)) |
| 5827 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5828 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5829 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5830 | } else { |
| 5831 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5832 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5833 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5834 | FoundFn); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5835 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5836 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5837 | |
| 5838 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5839 | // derived-to-base conversion? I believe the answer is "no", because |
| 5840 | // 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] | 5841 | ExprResult CurInitExprRes = |
| 5842 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 5843 | FoundFn, Conversion); |
| 5844 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5845 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5846 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5847 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5848 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5849 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5850 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5851 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 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_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5855 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5856 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5857 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5858 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5859 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5860 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5861 | |
| 5862 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5863 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5864 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5865 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5866 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5867 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5868 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5869 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5870 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 5871 | return ExprError(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5872 | } |
| 5873 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5874 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5875 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5876 | CurInit.get()->getType(), |
| 5877 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5878 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5879 | if (MaybeBindToTemp) |
| 5880 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5881 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5882 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5883 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5884 | break; |
| 5885 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5886 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5887 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5888 | case SK_QualificationConversionXValue: |
| 5889 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5890 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5891 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5892 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5893 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5894 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5895 | VK_XValue : |
| 5896 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5897 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5898 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5899 | } |
| 5900 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5901 | case SK_LValueToRValue: { |
| 5902 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
| 5903 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 5904 | CK_LValueToRValue, |
| 5905 | CurInit.take(), |
| 5906 | /*BasePath=*/0, |
| 5907 | VK_RValue)); |
| 5908 | break; |
| 5909 | } |
| 5910 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5911 | case SK_ConversionSequence: |
| 5912 | case SK_ConversionSequenceNoNarrowing: { |
| 5913 | Sema::CheckedConversionKind CCK |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5914 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5915 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5916 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5917 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5918 | ExprResult CurInitExprRes = |
| 5919 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5920 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5921 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5922 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5923 | CurInit = CurInitExprRes; |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 5924 | |
| 5925 | if (Step->Kind == SK_ConversionSequenceNoNarrowing && |
| 5926 | S.getLangOpts().CPlusPlus && !CurInit.get()->isValueDependent()) |
| 5927 | DiagnoseNarrowingInInitList(S, *Step->ICS, SourceType, Entity.getType(), |
| 5928 | CurInit.get()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5929 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5930 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5931 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5932 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5933 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5934 | // If we're not initializing the top-level entity, we need to create an |
| 5935 | // InitializeTemporary entity for our target type. |
| 5936 | QualType Ty = Step->Type; |
| 5937 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5938 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5939 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 5940 | InitListChecker PerformInitList(S, InitEntity, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 5941 | InitList, Ty, /*VerifyOnly=*/false); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5942 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5943 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5944 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5945 | // Hack: We must update *ResultType if available in order to set the |
| 5946 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5947 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5948 | if (ResultType && |
| 5949 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5950 | if ((*ResultType)->isRValueReferenceType()) |
| 5951 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5952 | else if ((*ResultType)->isLValueReferenceType()) |
| 5953 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5954 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5955 | *ResultType = Ty; |
| 5956 | } |
| 5957 | |
| 5958 | InitListExpr *StructuredInitList = |
| 5959 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5960 | CurInit.release(); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5961 | CurInit = shouldBindAsTemporary(InitEntity) |
| 5962 | ? S.MaybeBindToTemporary(StructuredInitList) |
| 5963 | : S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5964 | break; |
| 5965 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5966 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5967 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5968 | // When an initializer list is passed for a parameter of type "reference |
| 5969 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5970 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5971 | // FIXME: This is a hack. What we really should do is create a user |
| 5972 | // conversion step for this case, but this makes it considerably more |
| 5973 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5974 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5975 | Entity.getType().getNonReferenceType()); |
| 5976 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5977 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5978 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5979 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 5980 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5981 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5982 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 5983 | Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5984 | Kind, Arg, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5985 | ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 5986 | /*IsListInitialization*/ true, |
| 5987 | InitList->getLBraceLoc(), |
| 5988 | InitList->getRBraceLoc()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5989 | break; |
| 5990 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5991 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5992 | case SK_UnwrapInitList: |
| 5993 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 5994 | break; |
| 5995 | |
| 5996 | case SK_RewrapInitList: { |
| 5997 | Expr *E = CurInit.take(); |
| 5998 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 5999 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 6000 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6001 | ILE->setSyntacticForm(Syntactic); |
| 6002 | ILE->setType(E->getType()); |
| 6003 | ILE->setValueKind(E->getValueKind()); |
| 6004 | CurInit = S.Owned(ILE); |
| 6005 | break; |
| 6006 | } |
| 6007 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6008 | case SK_ConstructorInitialization: { |
| 6009 | // When an initializer list is passed for a parameter of type "reference |
| 6010 | // to object", we don't get an EK_Temporary entity, but instead an |
| 6011 | // EK_Parameter entity with reference type. |
| 6012 | // FIXME: This is a hack. What we really should do is create a user |
| 6013 | // conversion step for this case, but this makes it considerably more |
| 6014 | // complicated. For now, this will do. |
| 6015 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 6016 | Entity.getType().getNonReferenceType()); |
| 6017 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 6018 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 6019 | : Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6020 | Kind, Args, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 6021 | ConstructorInitRequiresZeroInit, |
Enea Zaffanella | 1245a54 | 2013-09-07 05:49:53 +0000 | [diff] [blame] | 6022 | /*IsListInitialization*/ false, |
| 6023 | /*LBraceLoc*/ SourceLocation(), |
| 6024 | /*RBraceLoc*/ SourceLocation()); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6025 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 6026 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6027 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6028 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6029 | step_iterator NextStep = Step; |
| 6030 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6031 | if (NextStep != StepEnd && |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 6032 | (NextStep->Kind == SK_ConstructorInitialization || |
| 6033 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6034 | // The need for zero-initialization is recorded directly into |
| 6035 | // the call to the object's constructor within the next step. |
| 6036 | ConstructorInitRequiresZeroInit = true; |
| 6037 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6038 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6039 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6040 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 6041 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6042 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 6043 | Kind.getRange().getBegin()); |
| 6044 | |
| 6045 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 6046 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 6047 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6048 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6049 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6050 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 6051 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 6052 | break; |
| 6053 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6054 | |
| 6055 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6056 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6057 | ExprResult Result = CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6058 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 01ad048 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 6059 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 6060 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6061 | if (Result.isInvalid()) |
| 6062 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6063 | CurInit = Result; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6064 | |
| 6065 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6066 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6067 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6068 | Entity.isParameterKind() && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6069 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6070 | == Sema::Compatible) |
| 6071 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6072 | if (CurInitExprRes.isInvalid()) |
| 6073 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6074 | CurInit = CurInitExprRes; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 6075 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6076 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6077 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 6078 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6079 | CurInit.get(), |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 6080 | getAssignmentAction(Entity, true), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6081 | &Complained)) { |
| 6082 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6083 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6084 | } else if (Complained) |
| 6085 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6086 | break; |
| 6087 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6088 | |
| 6089 | case SK_StringInit: { |
| 6090 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6091 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 6092 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6093 | break; |
| 6094 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6095 | |
| 6096 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6097 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6098 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 6099 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6100 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6101 | |
| 6102 | case SK_ArrayInit: |
| 6103 | // Okay: we checked everything before creating this step. Note that |
| 6104 | // this is a GNU extension. |
| 6105 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6106 | << Step->Type << CurInit.get()->getType() |
| 6107 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6108 | |
| 6109 | // If the destination type is an incomplete array type, update the |
| 6110 | // type accordingly. |
| 6111 | if (ResultType) { |
| 6112 | if (const IncompleteArrayType *IncompleteDest |
| 6113 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 6114 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6115 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6116 | *ResultType = S.Context.getConstantArrayType( |
| 6117 | IncompleteDest->getElementType(), |
| 6118 | ConstantSource->getSize(), |
| 6119 | ArrayType::Normal, 0); |
| 6120 | } |
| 6121 | } |
| 6122 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6123 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6124 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6125 | case SK_ParenthesizedArrayInit: |
| 6126 | // Okay: we checked everything before creating this step. Note that |
| 6127 | // this is a GNU extension. |
| 6128 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 6129 | << CurInit.get()->getSourceRange(); |
| 6130 | break; |
| 6131 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6132 | case SK_PassByIndirectCopyRestore: |
| 6133 | case SK_PassByIndirectRestore: |
| 6134 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 6135 | CurInit = S.Owned(new (S.Context) |
| 6136 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 6137 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 6138 | break; |
| 6139 | |
| 6140 | case SK_ProduceObjCObject: |
| 6141 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6142 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6143 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6144 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6145 | |
| 6146 | case SK_StdInitializerList: { |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6147 | S.Diag(CurInit.get()->getExprLoc(), |
| 6148 | diag::warn_cxx98_compat_initializer_list_init) |
| 6149 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6150 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6151 | // Maybe lifetime-extend the array temporary's subobjects to match the |
| 6152 | // entity's lifetime. |
| 6153 | const ValueDecl *ExtendingDecl = |
| 6154 | getDeclForTemporaryLifetimeExtension(Entity); |
| 6155 | if (ExtendingDecl) { |
| 6156 | performLifetimeExtension(CurInit.get(), ExtendingDecl); |
| 6157 | warnOnLifetimeExtension(S, Entity, CurInit.get(), true, ExtendingDecl); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6158 | } |
| 6159 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6160 | // Materialize the temporary into memory. |
| 6161 | MaterializeTemporaryExpr *MTE = new (S.Context) |
| 6162 | MaterializeTemporaryExpr(CurInit.get()->getType(), CurInit.get(), |
| 6163 | /*lvalue reference*/ false, ExtendingDecl); |
| 6164 | |
| 6165 | // Wrap it in a construction of a std::initializer_list<T>. |
| 6166 | CurInit = S.Owned( |
| 6167 | new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE)); |
| 6168 | |
| 6169 | // Bind the result, in case the library has given initializer_list a |
| 6170 | // non-trivial destructor. |
| 6171 | if (shouldBindAsTemporary(Entity)) |
| 6172 | CurInit = S.MaybeBindToTemporary(CurInit.take()); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6173 | break; |
| 6174 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6175 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6176 | case SK_OCLSamplerInit: { |
| 6177 | assert(Step->Type->isSamplerT() && |
| 6178 | "Sampler initialization on non sampler type."); |
| 6179 | |
| 6180 | QualType SourceType = CurInit.get()->getType(); |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6181 | |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6182 | if (Entity.isParameterKind()) { |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6183 | if (!SourceType->isSamplerT()) |
| 6184 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 6185 | << SourceType; |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6186 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6187 | llvm_unreachable("Invalid EntityKind!"); |
| 6188 | } |
| 6189 | |
| 6190 | break; |
| 6191 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6192 | case SK_OCLZeroEvent: { |
| 6193 | assert(Step->Type->isEventT() && |
| 6194 | "Event initialization on non event type."); |
| 6195 | |
| 6196 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
| 6197 | CK_ZeroToOCLEvent, |
| 6198 | CurInit.get()->getValueKind()); |
| 6199 | break; |
| 6200 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6201 | } |
| 6202 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6203 | |
| 6204 | // Diagnose non-fatal problems with the completed initialization. |
| 6205 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6206 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 6207 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 6208 | cast<FieldDecl>(Entity.getDecl()), |
| 6209 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6210 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6211 | return CurInit; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6212 | } |
| 6213 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6214 | /// Somewhere within T there is an uninitialized reference subobject. |
| 6215 | /// Dig it out and diagnose it. |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 6216 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 6217 | QualType T) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6218 | if (T->isReferenceType()) { |
| 6219 | S.Diag(Loc, diag::err_reference_without_init) |
| 6220 | << T.getNonReferenceType(); |
| 6221 | return true; |
| 6222 | } |
| 6223 | |
| 6224 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6225 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 6226 | return false; |
| 6227 | |
| 6228 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 6229 | FE = RD->field_end(); FI != FE; ++FI) { |
| 6230 | if (FI->isUnnamedBitfield()) |
| 6231 | continue; |
| 6232 | |
| 6233 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 6234 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6235 | return true; |
| 6236 | } |
| 6237 | } |
| 6238 | |
| 6239 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 6240 | BE = RD->bases_end(); |
| 6241 | BI != BE; ++BI) { |
| 6242 | if (DiagnoseUninitializedReference(S, BI->getLocStart(), BI->getType())) { |
| 6243 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6244 | return true; |
| 6245 | } |
| 6246 | } |
| 6247 | |
| 6248 | return false; |
| 6249 | } |
| 6250 | |
| 6251 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6252 | //===----------------------------------------------------------------------===// |
| 6253 | // Diagnose initialization failures |
| 6254 | //===----------------------------------------------------------------------===// |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6255 | |
| 6256 | /// Emit notes associated with an initialization that failed due to a |
| 6257 | /// "simple" conversion failure. |
| 6258 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 6259 | Expr *op) { |
| 6260 | QualType destType = entity.getType(); |
| 6261 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 6262 | op->getType()->isObjCObjectPointerType()) { |
| 6263 | |
| 6264 | // Emit a possible note about the conversion failing because the |
| 6265 | // operand is a message send with a related result type. |
| 6266 | S.EmitRelatedResultTypeNote(op); |
| 6267 | |
| 6268 | // Emit a possible note about a return failing because we're |
| 6269 | // expecting a related result type. |
| 6270 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 6271 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 6272 | } |
| 6273 | } |
| 6274 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6275 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6276 | const InitializedEntity &Entity, |
| 6277 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6278 | ArrayRef<Expr *> Args) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 6279 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6280 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6281 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 6282 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6283 | switch (Failure) { |
| 6284 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6285 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6286 | if (Args.empty()) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6287 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 6288 | // If this is value-initialization, this could be nested some way within |
| 6289 | // the target type. |
| 6290 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 6291 | DestType->isReferenceType()); |
| 6292 | bool Diagnosed = |
| 6293 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 6294 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 6295 | (void)Diagnosed; |
| 6296 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6297 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6298 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6299 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6300 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6301 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6302 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6303 | break; |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6304 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6305 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 6306 | break; |
| 6307 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6308 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 6309 | break; |
| 6310 | case FK_NarrowStringIntoWideCharArray: |
| 6311 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 6312 | break; |
| 6313 | case FK_WideStringIntoCharArray: |
| 6314 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 6315 | break; |
| 6316 | case FK_IncompatWideStringIntoWideChar: |
| 6317 | S.Diag(Kind.getLocation(), |
| 6318 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 6319 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6320 | case FK_ArrayTypeMismatch: |
| 6321 | case FK_NonConstantArrayInit: |
| 6322 | S.Diag(Kind.getLocation(), |
| 6323 | (Failure == FK_ArrayTypeMismatch |
| 6324 | ? diag::err_array_init_different_type |
| 6325 | : diag::err_array_init_non_constant_array)) |
| 6326 | << DestType.getNonReferenceType() |
| 6327 | << Args[0]->getType() |
| 6328 | << Args[0]->getSourceRange(); |
| 6329 | break; |
| 6330 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6331 | case FK_VariableLengthArrayHasInitializer: |
| 6332 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 6333 | << Args[0]->getSourceRange(); |
| 6334 | break; |
| 6335 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6336 | case FK_AddressOfOverloadFailed: { |
| 6337 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6338 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6339 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6340 | true, |
| 6341 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6342 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6343 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6344 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6345 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 6346 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6347 | switch (FailedOverloadResult) { |
| 6348 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6349 | if (Failure == FK_UserConversionOverloadFailed) |
| 6350 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 6351 | << Args[0]->getType() << DestType |
| 6352 | << Args[0]->getSourceRange(); |
| 6353 | else |
| 6354 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 6355 | << DestType << Args[0]->getType() |
| 6356 | << Args[0]->getSourceRange(); |
| 6357 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6358 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6359 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6360 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6361 | case OR_No_Viable_Function: |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 6362 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 6363 | DestType.getNonReferenceType(), |
| 6364 | diag::err_typecheck_nonviable_condition_incomplete, |
| 6365 | Args[0]->getType(), Args[0]->getSourceRange())) |
| 6366 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 6367 | << Args[0]->getType() << Args[0]->getSourceRange() |
| 6368 | << DestType.getNonReferenceType(); |
| 6369 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6370 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6371 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6372 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6373 | case OR_Deleted: { |
| 6374 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 6375 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 6376 | << Args[0]->getSourceRange(); |
| 6377 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6378 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6379 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 6380 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6381 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6382 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6383 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6384 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6385 | } |
| 6386 | break; |
| 6387 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6388 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6389 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6390 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6391 | } |
| 6392 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6393 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6394 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6395 | if (isa<InitListExpr>(Args[0])) { |
| 6396 | S.Diag(Kind.getLocation(), |
| 6397 | diag::err_lvalue_reference_bind_to_initlist) |
| 6398 | << DestType.getNonReferenceType().isVolatileQualified() |
| 6399 | << DestType.getNonReferenceType() |
| 6400 | << Args[0]->getSourceRange(); |
| 6401 | break; |
| 6402 | } |
| 6403 | // Intentional fallthrough |
| 6404 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6405 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6406 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6407 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 6408 | ? diag::err_lvalue_reference_bind_to_temporary |
| 6409 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 6410 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6411 | << DestType.getNonReferenceType() |
| 6412 | << Args[0]->getType() |
| 6413 | << Args[0]->getSourceRange(); |
| 6414 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6415 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6416 | case FK_RValueReferenceBindingToLValue: |
| 6417 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 6418 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6419 | << Args[0]->getSourceRange(); |
| 6420 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6421 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6422 | case FK_ReferenceInitDropsQualifiers: |
| 6423 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 6424 | << DestType.getNonReferenceType() |
| 6425 | << Args[0]->getType() |
| 6426 | << Args[0]->getSourceRange(); |
| 6427 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6428 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6429 | case FK_ReferenceInitFailed: |
| 6430 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 6431 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6432 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6433 | << Args[0]->getType() |
| 6434 | << Args[0]->getSourceRange(); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6435 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6436 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6437 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6438 | case FK_ConversionFailed: { |
| 6439 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6440 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6441 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6442 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6443 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6444 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6445 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6446 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 6447 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6448 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6449 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6450 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6451 | |
| 6452 | case FK_ConversionFromPropertyFailed: |
| 6453 | // No-op. This error has already been reported. |
| 6454 | break; |
| 6455 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6456 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6457 | SourceRange R; |
| 6458 | |
| 6459 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6460 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6461 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6462 | else |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6463 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6464 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6465 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 6466 | if (Kind.isCStyleOrFunctionalCast()) |
| 6467 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 6468 | << R; |
| 6469 | else |
| 6470 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 6471 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6472 | break; |
| 6473 | } |
| 6474 | |
| 6475 | case FK_ReferenceBindingToInitList: |
| 6476 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 6477 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 6478 | break; |
| 6479 | |
| 6480 | case FK_InitListBadDestinationType: |
| 6481 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 6482 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 6483 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6484 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6485 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6486 | case FK_ConstructorOverloadFailed: { |
| 6487 | SourceRange ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6488 | if (Args.size()) |
| 6489 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 6490 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6491 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6492 | if (Failure == FK_ListConstructorOverloadFailed) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6493 | assert(Args.size() == 1 && "List construction from other than 1 argument."); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6494 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6495 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6496 | } |
| 6497 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6498 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 6499 | // bad. |
| 6500 | switch (FailedOverloadResult) { |
| 6501 | case OR_Ambiguous: |
| 6502 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 6503 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6504 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6505 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6506 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6507 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6508 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 6509 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 6510 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 6511 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6512 | // This is implicit default initialization of a member or |
| 6513 | // base within a constructor. If no viable function was |
| 6514 | // found, notify the user that she needs to explicitly |
| 6515 | // initialize this base/member. |
| 6516 | CXXConstructorDecl *Constructor |
| 6517 | = cast<CXXConstructorDecl>(S.CurContext); |
| 6518 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6519 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6520 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6521 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6522 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6523 | << /*base=*/0 |
| 6524 | << Entity.getType(); |
| 6525 | |
| 6526 | RecordDecl *BaseDecl |
| 6527 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 6528 | ->getDecl(); |
| 6529 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 6530 | << S.Context.getTagDeclType(BaseDecl); |
| 6531 | } else { |
| 6532 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6533 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6534 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6535 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6536 | << /*member=*/1 |
| 6537 | << Entity.getName(); |
| 6538 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 6539 | |
| 6540 | if (const RecordType *Record |
| 6541 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6542 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6543 | diag::note_previous_decl) |
| 6544 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6545 | } |
| 6546 | break; |
| 6547 | } |
| 6548 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6549 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6550 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6551 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6552 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6553 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6554 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6555 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6556 | OverloadingResult Ovl |
| 6557 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6558 | if (Ovl != OR_Deleted) { |
| 6559 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6560 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6561 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6562 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6563 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6564 | |
| 6565 | // If this is a defaulted or implicitly-declared function, then |
| 6566 | // it was implicitly deleted. Make it clear that the deletion was |
| 6567 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6568 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6569 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6570 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6571 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6572 | else |
| 6573 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6574 | << true << DestType << ArgsRange; |
| 6575 | |
| 6576 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6577 | break; |
| 6578 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6579 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6580 | case OR_Success: |
| 6581 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6582 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6583 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6584 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6585 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6586 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6587 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6588 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6589 | // This is implicit default-initialization of a const member in |
| 6590 | // a constructor. Complain that it needs to be explicitly |
| 6591 | // initialized. |
| 6592 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6593 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6594 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6595 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6596 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6597 | << /*const=*/1 |
| 6598 | << Entity.getName(); |
| 6599 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6600 | << Entity.getName(); |
| 6601 | } else { |
| 6602 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 6603 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6604 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6605 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6606 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6607 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6608 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6609 | diag::err_init_incomplete_type); |
| 6610 | break; |
| 6611 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6612 | case FK_ListInitializationFailed: { |
| 6613 | // Run the init list checker again to emit diagnostics. |
| 6614 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 6615 | QualType DestType = Entity.getType(); |
| 6616 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 6617 | DestType, /*VerifyOnly=*/false); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6618 | assert(DiagnoseInitList.HadError() && |
| 6619 | "Inconsistent init list check result."); |
| 6620 | break; |
| 6621 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6622 | |
| 6623 | case FK_PlaceholderType: { |
| 6624 | // FIXME: Already diagnosed! |
| 6625 | break; |
| 6626 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6627 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6628 | case FK_ExplicitConstructor: { |
| 6629 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6630 | << Args[0]->getSourceRange(); |
| 6631 | OverloadCandidateSet::iterator Best; |
| 6632 | OverloadingResult Ovl |
| 6633 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6634 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6635 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6636 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6637 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6638 | break; |
| 6639 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6640 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6641 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6642 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6643 | return true; |
| 6644 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6645 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6646 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6647 | switch (SequenceKind) { |
| 6648 | case FailedSequence: { |
| 6649 | OS << "Failed sequence: "; |
| 6650 | switch (Failure) { |
| 6651 | case FK_TooManyInitsForReference: |
| 6652 | OS << "too many initializers for reference"; |
| 6653 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6654 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6655 | case FK_ArrayNeedsInitList: |
| 6656 | OS << "array requires initializer list"; |
| 6657 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6658 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6659 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6660 | OS << "array requires initializer list or string literal"; |
| 6661 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6662 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6663 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6664 | OS << "array requires initializer list or wide string literal"; |
| 6665 | break; |
| 6666 | |
| 6667 | case FK_NarrowStringIntoWideCharArray: |
| 6668 | OS << "narrow string into wide char array"; |
| 6669 | break; |
| 6670 | |
| 6671 | case FK_WideStringIntoCharArray: |
| 6672 | OS << "wide string into char array"; |
| 6673 | break; |
| 6674 | |
| 6675 | case FK_IncompatWideStringIntoWideChar: |
| 6676 | OS << "incompatible wide string into wide char array"; |
| 6677 | break; |
| 6678 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6679 | case FK_ArrayTypeMismatch: |
| 6680 | OS << "array type mismatch"; |
| 6681 | break; |
| 6682 | |
| 6683 | case FK_NonConstantArrayInit: |
| 6684 | OS << "non-constant array initializer"; |
| 6685 | break; |
| 6686 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6687 | case FK_AddressOfOverloadFailed: |
| 6688 | OS << "address of overloaded function failed"; |
| 6689 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6690 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6691 | case FK_ReferenceInitOverloadFailed: |
| 6692 | OS << "overload resolution for reference initialization failed"; |
| 6693 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6694 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6695 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6696 | OS << "non-const lvalue reference bound to temporary"; |
| 6697 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6698 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6699 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6700 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6701 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6702 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6703 | case FK_RValueReferenceBindingToLValue: |
| 6704 | OS << "rvalue reference bound to an lvalue"; |
| 6705 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6706 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6707 | case FK_ReferenceInitDropsQualifiers: |
| 6708 | OS << "reference initialization drops qualifiers"; |
| 6709 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6710 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6711 | case FK_ReferenceInitFailed: |
| 6712 | OS << "reference initialization failed"; |
| 6713 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6714 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6715 | case FK_ConversionFailed: |
| 6716 | OS << "conversion failed"; |
| 6717 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6718 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6719 | case FK_ConversionFromPropertyFailed: |
| 6720 | OS << "conversion from property failed"; |
| 6721 | break; |
| 6722 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6723 | case FK_TooManyInitsForScalar: |
| 6724 | OS << "too many initializers for scalar"; |
| 6725 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6726 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6727 | case FK_ReferenceBindingToInitList: |
| 6728 | OS << "referencing binding to initializer list"; |
| 6729 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6730 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6731 | case FK_InitListBadDestinationType: |
| 6732 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6733 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6734 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6735 | case FK_UserConversionOverloadFailed: |
| 6736 | OS << "overloading failed for user-defined conversion"; |
| 6737 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6738 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6739 | case FK_ConstructorOverloadFailed: |
| 6740 | OS << "constructor overloading failed"; |
| 6741 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6742 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6743 | case FK_DefaultInitOfConst: |
| 6744 | OS << "default initialization of a const variable"; |
| 6745 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6746 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6747 | case FK_Incomplete: |
| 6748 | OS << "initialization of incomplete type"; |
| 6749 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6750 | |
| 6751 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6752 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6753 | break; |
| 6754 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6755 | case FK_VariableLengthArrayHasInitializer: |
| 6756 | OS << "variable length array has an initializer"; |
| 6757 | break; |
| 6758 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6759 | case FK_PlaceholderType: |
| 6760 | OS << "initializer expression isn't contextually valid"; |
| 6761 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6762 | |
| 6763 | case FK_ListConstructorOverloadFailed: |
| 6764 | OS << "list constructor overloading failed"; |
| 6765 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6766 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6767 | case FK_ExplicitConstructor: |
| 6768 | OS << "list copy initialization chose explicit constructor"; |
| 6769 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6770 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6771 | OS << '\n'; |
| 6772 | return; |
| 6773 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6774 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6775 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6776 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6777 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6778 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6779 | case NormalSequence: |
| 6780 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6781 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6782 | } |
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 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6785 | if (S != step_begin()) { |
| 6786 | OS << " -> "; |
| 6787 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6788 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6789 | switch (S->Kind) { |
| 6790 | case SK_ResolveAddressOfOverloadedFunction: |
| 6791 | OS << "resolve address of overloaded function"; |
| 6792 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6793 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6794 | case SK_CastDerivedToBaseRValue: |
| 6795 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6796 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6797 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6798 | case SK_CastDerivedToBaseXValue: |
| 6799 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6800 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6801 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6802 | case SK_CastDerivedToBaseLValue: |
| 6803 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 6804 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6805 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6806 | case SK_BindReference: |
| 6807 | OS << "bind reference to lvalue"; |
| 6808 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6809 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6810 | case SK_BindReferenceToTemporary: |
| 6811 | OS << "bind reference to a temporary"; |
| 6812 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6813 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6814 | case SK_ExtraneousCopyToTemporary: |
| 6815 | OS << "extraneous C++03 copy to temporary"; |
| 6816 | break; |
| 6817 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6818 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6819 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6820 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6821 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6822 | case SK_QualificationConversionRValue: |
| 6823 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6824 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6825 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6826 | case SK_QualificationConversionXValue: |
| 6827 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6828 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6829 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6830 | case SK_QualificationConversionLValue: |
| 6831 | OS << "qualification conversion (lvalue)"; |
| 6832 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6833 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6834 | case SK_LValueToRValue: |
| 6835 | OS << "load (lvalue to rvalue)"; |
| 6836 | break; |
| 6837 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6838 | case SK_ConversionSequence: |
| 6839 | OS << "implicit conversion sequence ("; |
| 6840 | S->ICS->DebugPrint(); // FIXME: use OS |
| 6841 | OS << ")"; |
| 6842 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6843 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6844 | case SK_ConversionSequenceNoNarrowing: |
| 6845 | OS << "implicit conversion sequence with narrowing prohibited ("; |
| 6846 | S->ICS->DebugPrint(); // FIXME: use OS |
| 6847 | OS << ")"; |
| 6848 | break; |
| 6849 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6850 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6851 | OS << "list aggregate initialization"; |
| 6852 | break; |
| 6853 | |
| 6854 | case SK_ListConstructorCall: |
| 6855 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6856 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6857 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6858 | case SK_UnwrapInitList: |
| 6859 | OS << "unwrap reference initializer list"; |
| 6860 | break; |
| 6861 | |
| 6862 | case SK_RewrapInitList: |
| 6863 | OS << "rewrap reference initializer list"; |
| 6864 | break; |
| 6865 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6866 | case SK_ConstructorInitialization: |
| 6867 | OS << "constructor initialization"; |
| 6868 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6869 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6870 | case SK_ZeroInitialization: |
| 6871 | OS << "zero initialization"; |
| 6872 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6873 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6874 | case SK_CAssignment: |
| 6875 | OS << "C assignment"; |
| 6876 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6877 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6878 | case SK_StringInit: |
| 6879 | OS << "string initialization"; |
| 6880 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6881 | |
| 6882 | case SK_ObjCObjectConversion: |
| 6883 | OS << "Objective-C object conversion"; |
| 6884 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6885 | |
| 6886 | case SK_ArrayInit: |
| 6887 | OS << "array initialization"; |
| 6888 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6889 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6890 | case SK_ParenthesizedArrayInit: |
| 6891 | OS << "parenthesized array initialization"; |
| 6892 | break; |
| 6893 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6894 | case SK_PassByIndirectCopyRestore: |
| 6895 | OS << "pass by indirect copy and restore"; |
| 6896 | break; |
| 6897 | |
| 6898 | case SK_PassByIndirectRestore: |
| 6899 | OS << "pass by indirect restore"; |
| 6900 | break; |
| 6901 | |
| 6902 | case SK_ProduceObjCObject: |
| 6903 | OS << "Objective-C object retension"; |
| 6904 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6905 | |
| 6906 | case SK_StdInitializerList: |
| 6907 | OS << "std::initializer_list from initializer list"; |
| 6908 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6909 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6910 | case SK_OCLSamplerInit: |
| 6911 | OS << "OpenCL sampler_t from integer constant"; |
| 6912 | break; |
| 6913 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6914 | case SK_OCLZeroEvent: |
| 6915 | OS << "OpenCL event_t from zero"; |
| 6916 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6917 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6918 | |
| 6919 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6920 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6921 | |
| 6922 | OS << '\n'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6923 | } |
| 6924 | |
| 6925 | void InitializationSequence::dump() const { |
| 6926 | dump(llvm::errs()); |
| 6927 | } |
| 6928 | |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 6929 | static void DiagnoseNarrowingInInitList(Sema &S, |
| 6930 | const ImplicitConversionSequence &ICS, |
| 6931 | QualType PreNarrowingType, |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6932 | QualType EntityType, |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6933 | const Expr *PostInit) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6934 | const StandardConversionSequence *SCS = 0; |
| 6935 | switch (ICS.getKind()) { |
| 6936 | case ImplicitConversionSequence::StandardConversion: |
| 6937 | SCS = &ICS.Standard; |
| 6938 | break; |
| 6939 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6940 | SCS = &ICS.UserDefined.After; |
| 6941 | break; |
| 6942 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6943 | case ImplicitConversionSequence::EllipsisConversion: |
| 6944 | case ImplicitConversionSequence::BadConversion: |
| 6945 | return; |
| 6946 | } |
| 6947 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6948 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6949 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6950 | QualType ConstantType; |
| 6951 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 6952 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6953 | case NK_Not_Narrowing: |
| 6954 | // No narrowing occurred. |
| 6955 | return; |
| 6956 | |
| 6957 | case NK_Type_Narrowing: |
| 6958 | // This was a floating-to-integer conversion, which is always considered a |
| 6959 | // narrowing conversion even if the value is a constant and can be |
| 6960 | // represented exactly as an integer. |
| 6961 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6962 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6963 | diag::warn_init_list_type_narrowing |
| 6964 | : S.isSFINAEContext()? |
| 6965 | diag::err_init_list_type_narrowing_sfinae |
| 6966 | : diag::err_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6967 | << PostInit->getSourceRange() |
| 6968 | << PreNarrowingType.getLocalUnqualifiedType() |
| 6969 | << EntityType.getLocalUnqualifiedType(); |
| 6970 | break; |
| 6971 | |
| 6972 | case NK_Constant_Narrowing: |
| 6973 | // A constant value was narrowed. |
| 6974 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6975 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6976 | diag::warn_init_list_constant_narrowing |
| 6977 | : S.isSFINAEContext()? |
| 6978 | diag::err_init_list_constant_narrowing_sfinae |
| 6979 | : diag::err_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6980 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6981 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6982 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6983 | break; |
| 6984 | |
| 6985 | case NK_Variable_Narrowing: |
| 6986 | // A variable's value may have been narrowed. |
| 6987 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6988 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6989 | diag::warn_init_list_variable_narrowing |
| 6990 | : S.isSFINAEContext()? |
| 6991 | diag::err_init_list_variable_narrowing_sfinae |
| 6992 | : diag::err_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6993 | << PostInit->getSourceRange() |
| 6994 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6995 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6996 | break; |
| 6997 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6998 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6999 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7000 | llvm::raw_svector_ostream OS(StaticCast); |
| 7001 | OS << "static_cast<"; |
| 7002 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 7003 | // It's important to use the typedef's name if there is one so that the |
| 7004 | // fixit doesn't break code using types like int64_t. |
| 7005 | // |
| 7006 | // FIXME: This will break if the typedef requires qualification. But |
| 7007 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 7008 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7009 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7010 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7011 | else { |
| 7012 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 7013 | // with a broken cast. |
| 7014 | return; |
| 7015 | } |
| 7016 | OS << ">("; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7017 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override) |
| 7018 | << PostInit->getSourceRange() |
| 7019 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7020 | << FixItHint::CreateInsertion( |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7021 | S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7022 | } |
| 7023 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7024 | //===----------------------------------------------------------------------===// |
| 7025 | // Initialization helper functions |
| 7026 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7027 | bool |
| 7028 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 7029 | ExprResult Init) { |
| 7030 | if (Init.isInvalid()) |
| 7031 | return false; |
| 7032 | |
| 7033 | Expr *InitE = Init.get(); |
| 7034 | assert(InitE && "No initialization expression"); |
| 7035 | |
Douglas Gregor | 3c394c5 | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 7036 | InitializationKind Kind |
| 7037 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7038 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 7039 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 7040 | } |
| 7041 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 7042 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7043 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 7044 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7045 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7046 | bool TopLevelOfInitList, |
| 7047 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7048 | if (Init.isInvalid()) |
| 7049 | return ExprError(); |
| 7050 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 7051 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7052 | assert(InitE && "No initialization expression?"); |
| 7053 | |
| 7054 | if (EqualLoc.isInvalid()) |
| 7055 | EqualLoc = InitE->getLocStart(); |
| 7056 | |
| 7057 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 7058 | EqualLoc, |
| 7059 | AllowExplicit); |
Richard Smith | 13b228d | 2013-09-21 21:19:19 +0000 | [diff] [blame] | 7060 | InitializationSequence Seq(*this, Entity, Kind, InitE, TopLevelOfInitList); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7061 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 7062 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 7063 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7064 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 7065 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7066 | } |