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 |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 239 | bool AllowBraceElision; |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 240 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 241 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 242 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 243 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 244 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 245 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 246 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 247 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 248 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 249 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 250 | unsigned &StructuredIndex, |
| 251 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 252 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 253 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 254 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 255 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 256 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 257 | unsigned &StructuredIndex, |
| 258 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 259 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 260 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 261 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 262 | InitListExpr *StructuredList, |
| 263 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 264 | void CheckComplexType(const InitializedEntity &Entity, |
| 265 | InitListExpr *IList, QualType DeclType, |
| 266 | unsigned &Index, |
| 267 | InitListExpr *StructuredList, |
| 268 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 269 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 270 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 271 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 272 | InitListExpr *StructuredList, |
| 273 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 274 | void CheckReferenceType(const InitializedEntity &Entity, |
| 275 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 276 | unsigned &Index, |
| 277 | InitListExpr *StructuredList, |
| 278 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 279 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 280 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 281 | InitListExpr *StructuredList, |
| 282 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 283 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 284 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 285 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 286 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 287 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 288 | unsigned &StructuredIndex, |
| 289 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 290 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 291 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 292 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 293 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 294 | InitListExpr *StructuredList, |
| 295 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 296 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 297 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 298 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 299 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 300 | RecordDecl::field_iterator *NextField, |
| 301 | llvm::APSInt *NextElementIndex, |
| 302 | unsigned &Index, |
| 303 | InitListExpr *StructuredList, |
| 304 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 305 | bool FinishSubobjectInit, |
| 306 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 307 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 308 | QualType CurrentObjectType, |
| 309 | InitListExpr *StructuredList, |
| 310 | unsigned StructuredIndex, |
| 311 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 312 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 313 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 314 | Expr *expr); |
| 315 | int numArrayElements(QualType DeclType); |
| 316 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 317 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 318 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 319 | const InitializedEntity &ParentEntity, |
| 320 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 321 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 322 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 323 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 324 | Expr *InitExpr, FieldDecl *Field, |
| 325 | bool TopLevelObject); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 326 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 327 | |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 328 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 329 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 330 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 331 | bool AllowBraceElision); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 332 | bool HadError() { return hadError; } |
| 333 | |
| 334 | // @brief Retrieves the fully-structured initializer list used for |
| 335 | // semantic analysis and code generation. |
| 336 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 337 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 338 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 339 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 340 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 341 | assert(VerifyOnly && |
| 342 | "CheckValueInitializable is only inteded for verification mode."); |
| 343 | |
| 344 | SourceLocation Loc; |
| 345 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 346 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 347 | InitializationSequence InitSeq(SemaRef, Entity, Kind, None); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 348 | if (InitSeq.Failed()) |
| 349 | hadError = true; |
| 350 | } |
| 351 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 352 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 353 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 354 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 355 | bool &RequiresSecondPass) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 356 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 357 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 358 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 359 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 360 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 361 | // If there's no explicit initializer but we have a default initializer, use |
| 362 | // that. This only happens in C++1y, since classes with default |
| 363 | // initializers are not aggregates in C++11. |
| 364 | if (Field->hasInClassInitializer()) { |
| 365 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 366 | ILE->getRBraceLoc(), Field); |
| 367 | if (Init < NumInits) |
| 368 | ILE->setInit(Init, DIE); |
| 369 | else { |
| 370 | ILE->updateInit(SemaRef.Context, Init, DIE); |
| 371 | RequiresSecondPass = true; |
| 372 | } |
| 373 | return; |
| 374 | } |
| 375 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 376 | // FIXME: We probably don't need to handle references |
| 377 | // specially here, since value-initialization of references is |
| 378 | // handled in InitializationSequence. |
| 379 | if (Field->getType()->isReferenceType()) { |
| 380 | // C++ [dcl.init.aggr]p9: |
| 381 | // If an incomplete or empty initializer-list leaves a |
| 382 | // member of reference type uninitialized, the program is |
| 383 | // ill-formed. |
| 384 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 385 | << Field->getType() |
| 386 | << ILE->getSyntacticForm()->getSourceRange(); |
| 387 | SemaRef.Diag(Field->getLocation(), |
| 388 | diag::note_uninit_reference_member); |
| 389 | hadError = true; |
| 390 | return; |
| 391 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 392 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 393 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 394 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 395 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 396 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 397 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 398 | hadError = true; |
| 399 | return; |
| 400 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 401 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 402 | ExprResult MemberInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 403 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 404 | if (MemberInit.isInvalid()) { |
| 405 | hadError = true; |
| 406 | return; |
| 407 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 408 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 409 | if (hadError) { |
| 410 | // Do nothing |
| 411 | } else if (Init < NumInits) { |
| 412 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 413 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 414 | // Value-initialization requires a constructor call, so |
| 415 | // extend the initializer list to include the constructor |
| 416 | // call and make a note that we'll need to take another pass |
| 417 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 418 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 419 | RequiresSecondPass = true; |
| 420 | } |
| 421 | } else if (InitListExpr *InnerILE |
| 422 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 423 | FillInValueInitializations(MemberEntity, InnerILE, |
| 424 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 425 | } |
| 426 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 427 | /// Recursively replaces NULL values within the given initializer list |
| 428 | /// with expressions that perform value-initialization of the |
| 429 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 430 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 431 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 432 | InitListExpr *ILE, |
| 433 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 435 | "Should not have void type"); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 436 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 437 | if (ILE->getSyntacticForm()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 438 | Loc = ILE->getSyntacticForm()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 439 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 440 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 441 | const RecordDecl *RDecl = RType->getDecl(); |
| 442 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 443 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 444 | Entity, ILE, RequiresSecondPass); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 445 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 446 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
| 447 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 448 | FieldEnd = RDecl->field_end(); |
| 449 | Field != FieldEnd; ++Field) { |
| 450 | if (Field->hasInClassInitializer()) { |
| 451 | FillInValueInitForField(0, *Field, Entity, ILE, RequiresSecondPass); |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | } else { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 456 | unsigned Init = 0; |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 457 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 458 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 459 | Field != FieldEnd; ++Field) { |
| 460 | if (Field->isUnnamedBitfield()) |
| 461 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 462 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 463 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 464 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 465 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 466 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 467 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 468 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 470 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 471 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 472 | // Only look at the first initialization of a union. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 473 | if (RDecl->isUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 474 | break; |
| 475 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 480 | |
| 481 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 483 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 484 | unsigned NumInits = ILE->getNumInits(); |
| 485 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 486 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 487 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 488 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 489 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 490 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 491 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 492 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 493 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 494 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 495 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 496 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 498 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 499 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 501 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 502 | if (hadError) |
| 503 | return; |
| 504 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 505 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 506 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 507 | ElementEntity.setElementIndex(Init); |
| 508 | |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 509 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0); |
| 510 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 511 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 512 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 513 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 514 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 515 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 516 | hadError = true; |
| 517 | return; |
| 518 | } |
| 519 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 520 | ExprResult ElementInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 521 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 522 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 523 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 524 | return; |
| 525 | } |
| 526 | |
| 527 | if (hadError) { |
| 528 | // Do nothing |
| 529 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 530 | // For arrays, just set the expression used for value-initialization |
| 531 | // of the "holes" in the array. |
| 532 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 533 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 534 | else |
| 535 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 536 | } else { |
| 537 | // For arrays, just set the expression used for value-initialization |
| 538 | // of the rest of elements and exit. |
| 539 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 540 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 541 | return; |
| 542 | } |
| 543 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 544 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 545 | // Value-initialization requires a constructor call, so |
| 546 | // extend the initializer list to include the constructor |
| 547 | // call and make a note that we'll need to take another pass |
| 548 | // through the initializer list. |
| 549 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 550 | RequiresSecondPass = true; |
| 551 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 552 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 553 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 554 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 555 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 559 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 560 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 561 | InitListExpr *IL, QualType &T, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 562 | bool VerifyOnly, bool AllowBraceElision) |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 563 | : SemaRef(S), VerifyOnly(VerifyOnly), AllowBraceElision(AllowBraceElision) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 564 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 565 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 566 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 567 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 568 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 569 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 570 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 571 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 572 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 573 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 574 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 575 | bool RequiresSecondPass = false; |
| 576 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 577 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 578 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 579 | RequiresSecondPass); |
| 580 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 584 | // FIXME: use a proper constant |
| 585 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 586 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 587 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 588 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 589 | } |
| 590 | return maxElements; |
| 591 | } |
| 592 | |
| 593 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 594 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 595 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 597 | Field = structDecl->field_begin(), |
| 598 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 599 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 600 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 601 | ++InitializableMembers; |
| 602 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 603 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 604 | return std::min(InitializableMembers, 1); |
| 605 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 606 | } |
| 607 | |
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 | |
| 648 | if (VerifyOnly) { |
| 649 | if (!AllowBraceElision && (T->isArrayType() || T->isRecordType())) |
| 650 | hadError = true; |
| 651 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 652 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 653 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 654 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 655 | // Update the structured sub-object initializer so that it's ending |
| 656 | // range corresponds with the end of the last initializer it used. |
| 657 | if (EndIndex < ParentIList->getNumInits()) { |
| 658 | SourceLocation EndLoc |
| 659 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 660 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 661 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 662 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 663 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 664 | if (T->isArrayType() || T->isRecordType()) { |
| 665 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 666 | AllowBraceElision ? diag::warn_missing_braces : |
| 667 | diag::err_missing_braces) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 668 | << StructuredSubobjectInitList->getSourceRange() |
| 669 | << FixItHint::CreateInsertion( |
| 670 | StructuredSubobjectInitList->getLocStart(), "{") |
| 671 | << FixItHint::CreateInsertion( |
| 672 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 673 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 674 | "}"); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 675 | if (!AllowBraceElision) |
| 676 | hadError = true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 677 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 678 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 681 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 682 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 683 | unsigned &Index, |
| 684 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 685 | unsigned &StructuredIndex, |
| 686 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 687 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 688 | if (!VerifyOnly) { |
| 689 | SyntacticToSemantic[IList] = StructuredList; |
| 690 | StructuredList->setSyntacticForm(IList); |
| 691 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 692 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 693 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 694 | if (!VerifyOnly) { |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 695 | QualType ExprTy = T; |
| 696 | if (!ExprTy->isArrayType()) |
| 697 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 698 | IList->setType(ExprTy); |
| 699 | StructuredList->setType(ExprTy); |
| 700 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 701 | if (hadError) |
| 702 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 703 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 704 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 705 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 706 | if (VerifyOnly) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 707 | if (SemaRef.getLangOpts().CPlusPlus || |
| 708 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 709 | IList->getType()->isVectorType())) { |
| 710 | hadError = true; |
| 711 | } |
| 712 | return; |
| 713 | } |
| 714 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 715 | if (StructuredIndex == 1 && |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 716 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 717 | SIF_None) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 718 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 719 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 720 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 721 | hadError = true; |
| 722 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 723 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 724 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 725 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 726 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 727 | // Don't complain for incomplete types, since we'll get an error |
| 728 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 729 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 730 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 731 | CurrentObjectType->isArrayType()? 0 : |
| 732 | CurrentObjectType->isVectorType()? 1 : |
| 733 | CurrentObjectType->isScalarType()? 2 : |
| 734 | CurrentObjectType->isUnionType()? 3 : |
| 735 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 736 | |
| 737 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 738 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 739 | DK = diag::err_excess_initializers; |
| 740 | hadError = true; |
| 741 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 742 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 743 | DK = diag::err_excess_initializers; |
| 744 | hadError = true; |
| 745 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 747 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 748 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 749 | } |
| 750 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 751 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 752 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 753 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 754 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 755 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 756 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 757 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 760 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 761 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 763 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 764 | unsigned &Index, |
| 765 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 766 | unsigned &StructuredIndex, |
| 767 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 768 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 769 | // Explicitly braced initializer for complex type can be real+imaginary |
| 770 | // parts. |
| 771 | CheckComplexType(Entity, IList, DeclType, Index, |
| 772 | StructuredList, StructuredIndex); |
| 773 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 774 | CheckScalarType(Entity, IList, DeclType, Index, |
| 775 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 776 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 777 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 778 | StructuredList, StructuredIndex); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 779 | } else if (DeclType->isRecordType()) { |
| 780 | assert(DeclType->isAggregateType() && |
| 781 | "non-aggregate records should be handed in CheckSubElementType"); |
| 782 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 783 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 784 | SubobjectIsDesignatorContext, Index, |
| 785 | StructuredList, StructuredIndex, |
| 786 | TopLevelObject); |
| 787 | } else if (DeclType->isArrayType()) { |
| 788 | llvm::APSInt Zero( |
| 789 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 790 | false); |
| 791 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 792 | SubobjectIsDesignatorContext, Index, |
| 793 | StructuredList, StructuredIndex); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 794 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 795 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 796 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 797 | if (!VerifyOnly) |
| 798 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 799 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 800 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 801 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 802 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 803 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 804 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 805 | if (!VerifyOnly) |
| 806 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 807 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 808 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 809 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 810 | if (!VerifyOnly) |
| 811 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 812 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 813 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 814 | } |
| 815 | } |
| 816 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 817 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 818 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 819 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 820 | unsigned &Index, |
| 821 | InitListExpr *StructuredList, |
| 822 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 823 | Expr *expr = IList->getInit(Index); |
Richard Smith | 6242a45 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 824 | |
| 825 | if (ElemType->isReferenceType()) |
| 826 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 827 | StructuredList, StructuredIndex); |
| 828 | |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 829 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 830 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
| 831 | unsigned newIndex = 0; |
| 832 | unsigned newStructuredIndex = 0; |
| 833 | InitListExpr *newStructuredList |
| 834 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 835 | StructuredList, StructuredIndex, |
| 836 | SubInitList->getSourceRange()); |
| 837 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
| 838 | newStructuredList, newStructuredIndex); |
| 839 | ++StructuredIndex; |
| 840 | ++Index; |
| 841 | return; |
| 842 | } |
| 843 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 844 | "non-aggregate records are only possible in C++"); |
| 845 | // C++ initialization is handled later. |
| 846 | } |
| 847 | |
Richard Smith | 6242a45 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 848 | if (ElemType->isScalarType()) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 849 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 850 | StructuredList, StructuredIndex); |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 851 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 852 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 853 | // arrayType can be incomplete if we're initializing a flexible |
| 854 | // array member. There's nothing we can do with the completed |
| 855 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 856 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 857 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 858 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 859 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 860 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 861 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 862 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 863 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 864 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 865 | |
| 866 | // Fall through for subaggregate initialization. |
| 867 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 868 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 869 | // C++ [dcl.init.aggr]p12: |
| 870 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 871 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 872 | // an initializer-list. If the initializer can initialize a |
| 873 | // member, the member is initialized. [...] |
| 874 | |
| 875 | // FIXME: Better EqualLoc? |
| 876 | InitializationKind Kind = |
| 877 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 878 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 879 | |
| 880 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 881 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 882 | ExprResult Result = |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 883 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 884 | if (Result.isInvalid()) |
| 885 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 886 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 887 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 888 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 889 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 890 | ++Index; |
| 891 | return; |
| 892 | } |
| 893 | |
| 894 | // Fall through for subaggregate initialization |
| 895 | } else { |
| 896 | // C99 6.7.8p13: |
| 897 | // |
| 898 | // The initializer for a structure or union object that has |
| 899 | // automatic storage duration shall be either an initializer |
| 900 | // list as described below, or a single expression that has |
| 901 | // compatible structure or union type. In the latter case, the |
| 902 | // initial value of the object, including unnamed members, is |
| 903 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 904 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 905 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 906 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 907 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 908 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 909 | if (ExprRes.isInvalid()) |
| 910 | hadError = true; |
| 911 | else { |
| 912 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 913 | if (ExprRes.isInvalid()) |
| 914 | hadError = true; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 915 | } |
| 916 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 917 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 918 | ++Index; |
| 919 | return; |
| 920 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 921 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 922 | // Fall through for subaggregate initialization |
| 923 | } |
| 924 | |
| 925 | // C++ [dcl.init.aggr]p12: |
| 926 | // |
| 927 | // [...] Otherwise, if the member is itself a non-empty |
| 928 | // subaggregate, brace elision is assumed and the initializer is |
| 929 | // considered for the initialization of the first member of |
| 930 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 931 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 932 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 933 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 934 | StructuredIndex); |
| 935 | ++StructuredIndex; |
| 936 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 937 | if (!VerifyOnly) { |
| 938 | // We cannot initialize this element, so let |
| 939 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 940 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 941 | SemaRef.Owned(expr), |
| 942 | /*TopLevelOfInitList=*/true); |
| 943 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 944 | hadError = true; |
| 945 | ++Index; |
| 946 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 947 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 950 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 951 | InitListExpr *IList, QualType DeclType, |
| 952 | unsigned &Index, |
| 953 | InitListExpr *StructuredList, |
| 954 | unsigned &StructuredIndex) { |
| 955 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 956 | |
| 957 | // As an extension, clang supports complex initializers, which initialize |
| 958 | // a complex number component-wise. When an explicit initializer list for |
| 959 | // a complex number contains two two initializers, this extension kicks in: |
| 960 | // it exepcts the initializer list to contain two elements convertible to |
| 961 | // the element type of the complex type. The first element initializes |
| 962 | // the real part, and the second element intitializes the imaginary part. |
| 963 | |
| 964 | if (IList->getNumInits() != 2) |
| 965 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 966 | StructuredIndex); |
| 967 | |
| 968 | // This is an extension in C. (The builtin _Complex type does not exist |
| 969 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 970 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 971 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 972 | << IList->getSourceRange(); |
| 973 | |
| 974 | // Initialize the complex number. |
| 975 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 976 | InitializedEntity ElementEntity = |
| 977 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 978 | |
| 979 | for (unsigned i = 0; i < 2; ++i) { |
| 980 | ElementEntity.setElementIndex(Index); |
| 981 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 982 | StructuredList, StructuredIndex); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 987 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 988 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 989 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 990 | InitListExpr *StructuredList, |
| 991 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 992 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 993 | if (!VerifyOnly) |
| 994 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 995 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 996 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 997 | diag::err_empty_scalar_initializer) |
| 998 | << IList->getSourceRange(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 999 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1000 | ++Index; |
| 1001 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 1002 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1003 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1004 | |
| 1005 | Expr *expr = IList->getInit(Index); |
| 1006 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1007 | if (!VerifyOnly) |
| 1008 | SemaRef.Diag(SubIList->getLocStart(), |
| 1009 | diag::warn_many_braces_around_scalar_init) |
| 1010 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1011 | |
| 1012 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1013 | StructuredIndex); |
| 1014 | return; |
| 1015 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1016 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1017 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1018 | diag::err_designator_for_scalar_init) |
| 1019 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1020 | hadError = true; |
| 1021 | ++Index; |
| 1022 | ++StructuredIndex; |
| 1023 | return; |
| 1024 | } |
| 1025 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1026 | if (VerifyOnly) { |
| 1027 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1028 | hadError = true; |
| 1029 | ++Index; |
| 1030 | return; |
| 1031 | } |
| 1032 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1033 | ExprResult Result = |
| 1034 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1035 | SemaRef.Owned(expr), |
| 1036 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1037 | |
| 1038 | Expr *ResultExpr = 0; |
| 1039 | |
| 1040 | if (Result.isInvalid()) |
| 1041 | hadError = true; // types weren't compatible. |
| 1042 | else { |
| 1043 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1044 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1045 | if (ResultExpr != expr) { |
| 1046 | // The type was promoted, update initializer list. |
| 1047 | IList->setInit(Index, ResultExpr); |
| 1048 | } |
| 1049 | } |
| 1050 | if (hadError) |
| 1051 | ++StructuredIndex; |
| 1052 | else |
| 1053 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1054 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1057 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1058 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1059 | unsigned &Index, |
| 1060 | InitListExpr *StructuredList, |
| 1061 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1062 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1063 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1064 | // general, it would be useful to pass location information down the stack, |
| 1065 | // so that we know the location (or decl) of the "current object" being |
| 1066 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1067 | if (!VerifyOnly) |
| 1068 | SemaRef.Diag(IList->getLocStart(), |
| 1069 | diag::err_init_reference_member_uninitialized) |
| 1070 | << DeclType |
| 1071 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1072 | hadError = true; |
| 1073 | ++Index; |
| 1074 | ++StructuredIndex; |
| 1075 | return; |
| 1076 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1077 | |
| 1078 | Expr *expr = IList->getInit(Index); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1079 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1080 | if (!VerifyOnly) |
| 1081 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1082 | << DeclType << IList->getSourceRange(); |
| 1083 | hadError = true; |
| 1084 | ++Index; |
| 1085 | ++StructuredIndex; |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | if (VerifyOnly) { |
| 1090 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1091 | hadError = true; |
| 1092 | ++Index; |
| 1093 | return; |
| 1094 | } |
| 1095 | |
| 1096 | ExprResult Result = |
| 1097 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1098 | SemaRef.Owned(expr), |
| 1099 | /*TopLevelOfInitList=*/true); |
| 1100 | |
| 1101 | if (Result.isInvalid()) |
| 1102 | hadError = true; |
| 1103 | |
| 1104 | expr = Result.takeAs<Expr>(); |
| 1105 | IList->setInit(Index, expr); |
| 1106 | |
| 1107 | if (hadError) |
| 1108 | ++StructuredIndex; |
| 1109 | else |
| 1110 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1111 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1114 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1115 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1116 | unsigned &Index, |
| 1117 | InitListExpr *StructuredList, |
| 1118 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1119 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1120 | unsigned maxElements = VT->getNumElements(); |
| 1121 | unsigned numEltsInit = 0; |
| 1122 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1123 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1124 | if (Index >= IList->getNumInits()) { |
| 1125 | // Make sure the element type can be value-initialized. |
| 1126 | if (VerifyOnly) |
| 1127 | CheckValueInitializable( |
| 1128 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1129 | return; |
| 1130 | } |
| 1131 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1132 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1133 | // If the initializing element is a vector, try to copy-initialize |
| 1134 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1135 | Expr *Init = IList->getInit(Index); |
| 1136 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1137 | if (VerifyOnly) { |
| 1138 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1139 | hadError = true; |
| 1140 | ++Index; |
| 1141 | return; |
| 1142 | } |
| 1143 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1144 | ExprResult Result = |
| 1145 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1146 | SemaRef.Owned(Init), |
| 1147 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1148 | |
| 1149 | Expr *ResultExpr = 0; |
| 1150 | if (Result.isInvalid()) |
| 1151 | hadError = true; // types weren't compatible. |
| 1152 | else { |
| 1153 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1154 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1155 | if (ResultExpr != Init) { |
| 1156 | // The type was promoted, update initializer list. |
| 1157 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1158 | } |
| 1159 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1160 | if (hadError) |
| 1161 | ++StructuredIndex; |
| 1162 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1163 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1164 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1165 | ++Index; |
| 1166 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1167 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1168 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1169 | InitializedEntity ElementEntity = |
| 1170 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1171 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1172 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1173 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1174 | if (Index >= IList->getNumInits()) { |
| 1175 | if (VerifyOnly) |
| 1176 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1177 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1178 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1179 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1180 | ElementEntity.setElementIndex(Index); |
| 1181 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1182 | StructuredList, StructuredIndex); |
| 1183 | } |
| 1184 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1185 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1186 | |
| 1187 | InitializedEntity ElementEntity = |
| 1188 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1189 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1190 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1191 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1192 | // Don't attempt to go past the end of the init list |
| 1193 | if (Index >= IList->getNumInits()) |
| 1194 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1195 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1196 | ElementEntity.setElementIndex(Index); |
| 1197 | |
| 1198 | QualType IType = IList->getInit(Index)->getType(); |
| 1199 | if (!IType->isVectorType()) { |
| 1200 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1201 | StructuredList, StructuredIndex); |
| 1202 | ++numEltsInit; |
| 1203 | } else { |
| 1204 | QualType VecType; |
| 1205 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1206 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1207 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1208 | if (IType->isExtVectorType()) |
| 1209 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1210 | else |
| 1211 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1212 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1213 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1214 | StructuredList, StructuredIndex); |
| 1215 | numEltsInit += numIElts; |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1220 | if (numEltsInit != maxElements) { |
| 1221 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1222 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1223 | diag::err_vector_incorrect_num_initializers) |
| 1224 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1225 | hadError = true; |
| 1226 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1229 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1230 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1231 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1232 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1233 | unsigned &Index, |
| 1234 | InitListExpr *StructuredList, |
| 1235 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1236 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1237 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1238 | // Check for the special-case of initializing an array with a string. |
| 1239 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1240 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1241 | SIF_None) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1242 | // We place the string literal directly into the resulting |
| 1243 | // initializer list. This is the only place where the structure |
| 1244 | // of the structured initializer list doesn't match exactly, |
| 1245 | // because doing so would involve allocating one character |
| 1246 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1247 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1248 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1249 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1250 | IList->getInit(Index)); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1251 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1252 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1253 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1254 | return; |
| 1255 | } |
| 1256 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1257 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1258 | // Check for VLAs; in standard C it would be possible to check this |
| 1259 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1260 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1261 | if (!VerifyOnly) |
| 1262 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1263 | diag::err_variable_object_no_init) |
| 1264 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1265 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1266 | ++Index; |
| 1267 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1268 | return; |
| 1269 | } |
| 1270 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1271 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1272 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1273 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1274 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1275 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1276 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1277 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1278 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1279 | maxElementsKnown = true; |
| 1280 | } |
| 1281 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1282 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1283 | while (Index < IList->getNumInits()) { |
| 1284 | Expr *Init = IList->getInit(Index); |
| 1285 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1286 | // If we're not the subobject that matches up with the '{' for |
| 1287 | // the designator, we shouldn't be handling the |
| 1288 | // designator. Return immediately. |
| 1289 | if (!SubobjectIsDesignatorContext) |
| 1290 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1291 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1292 | // Handle this designated initializer. elementIndex will be |
| 1293 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1294 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1295 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1296 | StructuredList, StructuredIndex, true, |
| 1297 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1298 | hadError = true; |
| 1299 | continue; |
| 1300 | } |
| 1301 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1302 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1303 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1304 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1305 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1306 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1307 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1308 | // If the array is of incomplete type, keep track of the number of |
| 1309 | // elements in the initializer. |
| 1310 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1311 | maxElements = elementIndex; |
| 1312 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1313 | continue; |
| 1314 | } |
| 1315 | |
| 1316 | // If we know the maximum number of elements, and we've already |
| 1317 | // hit it, stop consuming elements in the initializer list. |
| 1318 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1319 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1320 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1321 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1322 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1323 | Entity); |
| 1324 | // Check this element. |
| 1325 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1326 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1327 | ++elementIndex; |
| 1328 | |
| 1329 | // If the array is of incomplete type, keep track of the number of |
| 1330 | // elements in the initializer. |
| 1331 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1332 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1333 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1334 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1335 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1336 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1337 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1338 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1339 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1340 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1341 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1342 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1343 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1344 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1345 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1346 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1347 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1348 | if (!hadError && VerifyOnly) { |
| 1349 | // Check if there are any members of the array that get value-initialized. |
| 1350 | // If so, check if doing that is possible. |
| 1351 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1352 | if (maxElementsKnown && elementIndex < maxElements) |
| 1353 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1354 | SemaRef.Context, 0, Entity)); |
| 1355 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1358 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1359 | Expr *InitExpr, |
| 1360 | FieldDecl *Field, |
| 1361 | bool TopLevelObject) { |
| 1362 | // Handle GNU flexible array initializers. |
| 1363 | unsigned FlexArrayDiag; |
| 1364 | if (isa<InitListExpr>(InitExpr) && |
| 1365 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1366 | // Empty flexible array init always allowed as an extension |
| 1367 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1368 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1369 | // Disallow flexible array init in C++; it is not required for gcc |
| 1370 | // compatibility, and it needs work to IRGen correctly in general. |
| 1371 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1372 | } else if (!TopLevelObject) { |
| 1373 | // Disallow flexible array init on non-top-level object |
| 1374 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1375 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1376 | // Disallow flexible array init on anything which is not a variable. |
| 1377 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1378 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1379 | // Disallow flexible array init on local variables. |
| 1380 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1381 | } else { |
| 1382 | // Allow other cases. |
| 1383 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1384 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1385 | |
| 1386 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1387 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1388 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1389 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1390 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1391 | << Field; |
| 1392 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1393 | |
| 1394 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1395 | } |
| 1396 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1397 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1398 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1400 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1401 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1402 | unsigned &Index, |
| 1403 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1404 | unsigned &StructuredIndex, |
| 1405 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1406 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1408 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1409 | // confusion, we forgo checking the intializer for the entire record. |
| 1410 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 72ab277 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1411 | // Assume it was supposed to consume a single initializer. |
| 1412 | ++Index; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1413 | hadError = true; |
| 1414 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1416 | |
| 1417 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1418 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1419 | |
| 1420 | // If there's a default initializer, use it. |
| 1421 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1422 | if (VerifyOnly) |
| 1423 | return; |
| 1424 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1425 | Field != FieldEnd; ++Field) { |
| 1426 | if (Field->hasInClassInitializer()) { |
| 1427 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1428 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1429 | return; |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | // Value-initialize the first named member of the union. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1435 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1436 | Field != FieldEnd; ++Field) { |
| 1437 | if (Field->getDeclName()) { |
| 1438 | if (VerifyOnly) |
| 1439 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1440 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1441 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1442 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1443 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1444 | } |
| 1445 | } |
| 1446 | return; |
| 1447 | } |
| 1448 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1449 | // If structDecl is a forward declaration, this loop won't do |
| 1450 | // anything except look at designated initializers; That's okay, |
| 1451 | // because an error should get printed out elsewhere. It might be |
| 1452 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1453 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1454 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1455 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1456 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1457 | while (Index < IList->getNumInits()) { |
| 1458 | Expr *Init = IList->getInit(Index); |
| 1459 | |
| 1460 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1461 | // If we're not the subobject that matches up with the '{' for |
| 1462 | // the designator, we shouldn't be handling the |
| 1463 | // designator. Return immediately. |
| 1464 | if (!SubobjectIsDesignatorContext) |
| 1465 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1466 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1467 | // Handle this designated initializer. Field will be updated to |
| 1468 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1469 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1470 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1471 | StructuredList, StructuredIndex, |
| 1472 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1473 | hadError = true; |
| 1474 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1475 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1476 | |
| 1477 | // Disable check for missing fields when designators are used. |
| 1478 | // This matches gcc behaviour. |
| 1479 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1480 | continue; |
| 1481 | } |
| 1482 | |
| 1483 | if (Field == FieldEnd) { |
| 1484 | // We've run out of fields. We're done. |
| 1485 | break; |
| 1486 | } |
| 1487 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1488 | // We've already initialized a member of a union. We're done. |
| 1489 | if (InitializedSomething && DeclType->isUnionType()) |
| 1490 | break; |
| 1491 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1492 | // If we've hit the flexible array member at the end, we're done. |
| 1493 | if (Field->getType()->isIncompleteArrayType()) |
| 1494 | break; |
| 1495 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1496 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1497 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1498 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1499 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1500 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1501 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1502 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1503 | bool InvalidUse; |
| 1504 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1505 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1506 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1507 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1508 | IList->getInit(Index)->getLocStart()); |
| 1509 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1510 | ++Index; |
| 1511 | ++Field; |
| 1512 | hadError = true; |
| 1513 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1514 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1515 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1516 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1517 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1518 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1519 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1520 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1521 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1522 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1523 | // Initialize the first field within the union. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1524 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1525 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1526 | |
| 1527 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1528 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1529 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1530 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1531 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1532 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1533 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1534 | // It is possible we have one or more unnamed bitfields remaining. |
| 1535 | // Find first (if any) named field and emit warning. |
| 1536 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1537 | it != end; ++it) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1538 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1539 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1540 | diag::warn_missing_field_initializers) << it->getName(); |
| 1541 | break; |
| 1542 | } |
| 1543 | } |
| 1544 | } |
| 1545 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1546 | // Check that any remaining fields can be value-initialized. |
| 1547 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1548 | !Field->getType()->isIncompleteArrayType()) { |
| 1549 | // FIXME: Should check for holes left by designated initializers too. |
| 1550 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1551 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1552 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1553 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1557 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1558 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1559 | return; |
| 1560 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1561 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1562 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1563 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1564 | ++Index; |
| 1565 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1568 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1569 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1570 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1571 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1572 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1573 | StructuredList, StructuredIndex); |
| 1574 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1575 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1576 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1577 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1578 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1579 | /// \brief Expand a field designator that refers to a member of an |
| 1580 | /// anonymous struct or union into a series of field designators that |
| 1581 | /// refers to the field within the appropriate subobject. |
| 1582 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1583 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1584 | DesignatedInitExpr *DIE, |
| 1585 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1586 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1587 | typedef DesignatedInitExpr::Designator Designator; |
| 1588 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1589 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1590 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1591 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1592 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1593 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1594 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1595 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1596 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1597 | else |
| 1598 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1599 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1600 | assert(isa<FieldDecl>(*PI)); |
| 1601 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
| 1604 | // Expand the current designator into the set of replacement |
| 1605 | // designators, so we have a full subobject path down to where the |
| 1606 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1607 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1608 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1609 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1610 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1611 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1612 | /// corresponds to FieldName. |
| 1613 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1614 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1615 | if (!FieldName) |
| 1616 | return 0; |
| 1617 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1618 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1619 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1620 | while (IndirectFieldDecl *IF = |
| 1621 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1622 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1623 | return IF; |
| 1624 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1625 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1626 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1629 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1630 | DesignatedInitExpr *DIE) { |
| 1631 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1632 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1633 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1634 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1635 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1636 | DIE->size(), IndexExprs, |
| 1637 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1638 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1639 | } |
| 1640 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1641 | namespace { |
| 1642 | |
| 1643 | // Callback to only accept typo corrections that are for field members of |
| 1644 | // the given struct or union. |
| 1645 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1646 | public: |
| 1647 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1648 | : Record(RD) {} |
| 1649 | |
| 1650 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1651 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1652 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1653 | } |
| 1654 | |
| 1655 | private: |
| 1656 | RecordDecl *Record; |
| 1657 | }; |
| 1658 | |
| 1659 | } |
| 1660 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1661 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1662 | /// |
| 1663 | /// Determines whether the designated initializer @p DIE, which |
| 1664 | /// resides at the given @p Index within the initializer list @p |
| 1665 | /// IList, is well-formed for a current object of type @p DeclType |
| 1666 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1667 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1668 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1669 | /// |
| 1670 | /// @param IList The initializer list in which this designated |
| 1671 | /// initializer occurs. |
| 1672 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1673 | /// @param DIE The designated initializer expression. |
| 1674 | /// |
| 1675 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1676 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1677 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1678 | /// into which the designation in @p DIE should refer. |
| 1679 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1680 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1681 | /// a field, this will be set to the field declaration corresponding |
| 1682 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1683 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1684 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1685 | /// DIE is an array designator or GNU array-range designator, this |
| 1686 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1687 | /// |
| 1688 | /// @param Index Index into @p IList where the designated initializer |
| 1689 | /// @p DIE occurs. |
| 1690 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1691 | /// @param StructuredList The initializer list expression that |
| 1692 | /// describes all of the subobject initializers in the order they'll |
| 1693 | /// actually be initialized. |
| 1694 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1695 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1697 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1698 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1699 | DesignatedInitExpr *DIE, |
| 1700 | unsigned DesigIdx, |
| 1701 | QualType &CurrentObjectType, |
| 1702 | RecordDecl::field_iterator *NextField, |
| 1703 | llvm::APSInt *NextElementIndex, |
| 1704 | unsigned &Index, |
| 1705 | InitListExpr *StructuredList, |
| 1706 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1707 | bool FinishSubobjectInit, |
| 1708 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1709 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1710 | // Check the actual initialization for the designated object type. |
| 1711 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1712 | |
| 1713 | // Temporarily remove the designator expression from the |
| 1714 | // initializer list that the child calls see, so that we don't try |
| 1715 | // to re-process the designator. |
| 1716 | unsigned OldIndex = Index; |
| 1717 | IList->setInit(OldIndex, DIE->getInit()); |
| 1718 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1719 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1720 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1721 | |
| 1722 | // Restore the designated initializer expression in the syntactic |
| 1723 | // form of the initializer list. |
| 1724 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1725 | DIE->setInit(IList->getInit(OldIndex)); |
| 1726 | IList->setInit(OldIndex, DIE); |
| 1727 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1728 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1731 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1732 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1733 | if (!VerifyOnly) { |
| 1734 | assert((IsFirstDesignator || StructuredList) && |
| 1735 | "Need a non-designated initializer list to start from"); |
| 1736 | |
| 1737 | // Determine the structural initializer list that corresponds to the |
| 1738 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1739 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1740 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1741 | StructuredList, StructuredIndex, |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1742 | SourceRange(D->getLocStart(), |
| 1743 | DIE->getLocEnd())); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1744 | assert(StructuredList && "Expected a structured initializer list"); |
| 1745 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1746 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1747 | if (D->isFieldDesignator()) { |
| 1748 | // C99 6.7.8p7: |
| 1749 | // |
| 1750 | // If a designator has the form |
| 1751 | // |
| 1752 | // . identifier |
| 1753 | // |
| 1754 | // then the current object (defined below) shall have |
| 1755 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1756 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1757 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1758 | if (!RT) { |
| 1759 | SourceLocation Loc = D->getDotLoc(); |
| 1760 | if (Loc.isInvalid()) |
| 1761 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1762 | if (!VerifyOnly) |
| 1763 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1764 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1765 | ++Index; |
| 1766 | return true; |
| 1767 | } |
| 1768 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1769 | // Note: we perform a linear search of the fields here, despite |
| 1770 | // the fact that we have a faster lookup method, because we always |
| 1771 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1772 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1773 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1774 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1775 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1776 | Field = RT->getDecl()->field_begin(), |
| 1777 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1778 | for (; Field != FieldEnd; ++Field) { |
| 1779 | if (Field->isUnnamedBitfield()) |
| 1780 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1781 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1782 | // If we find a field representing an anonymous field, look in the |
| 1783 | // IndirectFieldDecl that follow for the designated initializer. |
| 1784 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1785 | if (IndirectFieldDecl *IF = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1786 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1787 | // In verify mode, don't modify the original. |
| 1788 | if (VerifyOnly) |
| 1789 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1790 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1791 | D = DIE->getDesignator(DesigIdx); |
| 1792 | break; |
| 1793 | } |
| 1794 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1795 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1796 | break; |
| 1797 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1798 | break; |
| 1799 | |
| 1800 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1801 | } |
| 1802 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1803 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1804 | if (VerifyOnly) { |
| 1805 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1806 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1807 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1808 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1809 | // There was no normal field in the struct with the designated |
| 1810 | // name. Perform another lookup for this name, which may find |
| 1811 | // something that we can't designate (e.g., a member function), |
| 1812 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1814 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1815 | FieldDecl *ReplacementField = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1816 | if (Lookup.empty()) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1817 | // Name lookup didn't find anything. Determine whether this |
| 1818 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1819 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1820 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1821 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1822 | Sema::LookupMemberName, /*Scope=*/0, /*SS=*/0, Validator, |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1823 | RT->getDecl()); |
| 1824 | if (Corrected) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1825 | std::string CorrectedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1826 | Corrected.getAsString(SemaRef.getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1827 | std::string CorrectedQuotedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1828 | Corrected.getQuoted(SemaRef.getLangOpts())); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1829 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1830 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1831 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1832 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1833 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1834 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1835 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1836 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1837 | } else { |
| 1838 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1839 | << FieldName << CurrentObjectType; |
| 1840 | ++Index; |
| 1841 | return true; |
| 1842 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1843 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1844 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1845 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1846 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1847 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1848 | << FieldName; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1849 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1850 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1851 | ++Index; |
| 1852 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1853 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1854 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1855 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1856 | // The replacement field comes from typo correction; find it |
| 1857 | // in the list of fields. |
| 1858 | FieldIndex = 0; |
| 1859 | Field = RT->getDecl()->field_begin(); |
| 1860 | for (; Field != FieldEnd; ++Field) { |
| 1861 | if (Field->isUnnamedBitfield()) |
| 1862 | continue; |
| 1863 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1864 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1865 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1866 | break; |
| 1867 | |
| 1868 | ++FieldIndex; |
| 1869 | } |
| 1870 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1871 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1872 | |
| 1873 | // All of the fields of a union are located at the same place in |
| 1874 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1875 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1876 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1877 | if (!VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1878 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1879 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1880 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1881 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1882 | bool InvalidUse; |
| 1883 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1884 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1885 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1886 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1887 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1888 | ++Index; |
| 1889 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1890 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1891 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1892 | if (!VerifyOnly) { |
| 1893 | // Update the designator with the field declaration. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1894 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1895 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1896 | // Make sure that our non-designated initializer list has space |
| 1897 | // for a subobject corresponding to this field. |
| 1898 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1899 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1900 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1901 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1902 | // This designator names a flexible array member. |
| 1903 | if (Field->getType()->isIncompleteArrayType()) { |
| 1904 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1905 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1906 | // We can't designate an object within the flexible array |
| 1907 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1908 | if (!VerifyOnly) { |
| 1909 | DesignatedInitExpr::Designator *NextD |
| 1910 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1911 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1912 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1913 | << SourceRange(NextD->getLocStart(), |
| 1914 | DIE->getLocEnd()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1915 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1916 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1917 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1918 | Invalid = true; |
| 1919 | } |
| 1920 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1921 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1922 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1923 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1924 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1925 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1926 | diag::err_flexible_array_init_needs_braces) |
| 1927 | << DIE->getInit()->getSourceRange(); |
| 1928 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1929 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1930 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1931 | Invalid = true; |
| 1932 | } |
| 1933 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1934 | // Check GNU flexible array initializer. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1935 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1936 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1937 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1938 | |
| 1939 | if (Invalid) { |
| 1940 | ++Index; |
| 1941 | return true; |
| 1942 | } |
| 1943 | |
| 1944 | // Initialize the array. |
| 1945 | bool prevHadError = hadError; |
| 1946 | unsigned newStructuredIndex = FieldIndex; |
| 1947 | unsigned OldIndex = Index; |
| 1948 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1949 | |
| 1950 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1951 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1952 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1953 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1954 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1955 | IList->setInit(OldIndex, DIE); |
| 1956 | if (hadError && !prevHadError) { |
| 1957 | ++Field; |
| 1958 | ++FieldIndex; |
| 1959 | if (NextField) |
| 1960 | *NextField = Field; |
| 1961 | StructuredIndex = FieldIndex; |
| 1962 | return true; |
| 1963 | } |
| 1964 | } else { |
| 1965 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1966 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1967 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1968 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1969 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1970 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1971 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1972 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1973 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1974 | true, false)) |
| 1975 | return true; |
| 1976 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1977 | |
| 1978 | // Find the position of the next field to be initialized in this |
| 1979 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1980 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1981 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1982 | |
| 1983 | // If this the first designator, our caller will continue checking |
| 1984 | // the rest of this struct/class/union subobject. |
| 1985 | if (IsFirstDesignator) { |
| 1986 | if (NextField) |
| 1987 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1988 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1989 | return false; |
| 1990 | } |
| 1991 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1992 | if (!FinishSubobjectInit) |
| 1993 | return false; |
| 1994 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1995 | // We've already initialized something in the union; we're done. |
| 1996 | if (RT->getDecl()->isUnion()) |
| 1997 | return hadError; |
| 1998 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1999 | // Check the remaining fields within this class/struct/union subobject. |
| 2000 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2001 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2002 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2003 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2004 | return hadError && !prevHadError; |
| 2005 | } |
| 2006 | |
| 2007 | // C99 6.7.8p6: |
| 2008 | // |
| 2009 | // If a designator has the form |
| 2010 | // |
| 2011 | // [ constant-expression ] |
| 2012 | // |
| 2013 | // then the current object (defined below) shall have array |
| 2014 | // type and the expression shall be an integer constant |
| 2015 | // expression. If the array is of unknown size, any |
| 2016 | // nonnegative value is valid. |
| 2017 | // |
| 2018 | // Additionally, cope with the GNU extension that permits |
| 2019 | // designators of the form |
| 2020 | // |
| 2021 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2022 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2023 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2024 | if (!VerifyOnly) |
| 2025 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2026 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2027 | ++Index; |
| 2028 | return true; |
| 2029 | } |
| 2030 | |
| 2031 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2032 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2033 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2034 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2035 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2036 | DesignatedEndIndex = DesignatedStartIndex; |
| 2037 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2038 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2039 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2040 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2041 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2042 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2043 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2044 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2045 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2046 | // Codegen can't handle evaluating array range designators that have side |
| 2047 | // effects, because we replicate the AST value for each initialized element. |
| 2048 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2049 | // elements with something that has a side effect, so codegen can emit an |
| 2050 | // "error unsupported" error instead of miscompiling the app. |
| 2051 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2052 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2053 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2056 | if (isa<ConstantArrayType>(AT)) { |
| 2057 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2058 | DesignatedStartIndex |
| 2059 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2060 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2061 | DesignatedEndIndex |
| 2062 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2063 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2064 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2065 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2066 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2067 | diag::err_array_designator_too_large) |
| 2068 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2069 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2070 | ++Index; |
| 2071 | return true; |
| 2072 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2073 | } else { |
| 2074 | // Make sure the bit-widths and signedness match. |
| 2075 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2076 | DesignatedEndIndex |
| 2077 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2078 | else if (DesignatedStartIndex.getBitWidth() < |
| 2079 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2080 | DesignatedStartIndex |
| 2081 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2082 | DesignatedStartIndex.setIsUnsigned(true); |
| 2083 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2084 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2086 | // Make sure that our non-designated initializer list has space |
| 2087 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2088 | if (!VerifyOnly && |
| 2089 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2090 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2091 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2092 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2093 | // Repeatedly perform subobject initializations in the range |
| 2094 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2095 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2096 | // Move to the next designator |
| 2097 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2098 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2099 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2100 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2101 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2102 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2103 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2104 | // Recurse to check later designated subobjects. |
| 2105 | QualType ElementType = AT->getElementType(); |
| 2106 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2107 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2108 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2109 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 2110 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2111 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2112 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2113 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2114 | return true; |
| 2115 | |
| 2116 | // Move to the next index in the array that we'll be initializing. |
| 2117 | ++DesignatedStartIndex; |
| 2118 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2119 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2120 | |
| 2121 | // If this the first designator, our caller will continue checking |
| 2122 | // the rest of this array subobject. |
| 2123 | if (IsFirstDesignator) { |
| 2124 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2125 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2126 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2127 | return false; |
| 2128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2130 | if (!FinishSubobjectInit) |
| 2131 | return false; |
| 2132 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2133 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2134 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2135 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2136 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2137 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2138 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2141 | // Get the structured initializer list for a subobject of type |
| 2142 | // @p CurrentObjectType. |
| 2143 | InitListExpr * |
| 2144 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2145 | QualType CurrentObjectType, |
| 2146 | InitListExpr *StructuredList, |
| 2147 | unsigned StructuredIndex, |
| 2148 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2149 | if (VerifyOnly) |
| 2150 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2151 | Expr *ExistingInit = 0; |
| 2152 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2153 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2154 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2155 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2157 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2158 | return Result; |
| 2159 | |
| 2160 | if (ExistingInit) { |
| 2161 | // We are creating an initializer list that initializes the |
| 2162 | // subobjects of the current object, but there was already an |
| 2163 | // initialization that completely initialized the current |
| 2164 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2166 | // struct X { int a, b; }; |
| 2167 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2168 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2169 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2170 | // designated initializer re-initializes the whole |
| 2171 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2173 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2174 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2175 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2176 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2177 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2178 | << ExistingInit->getSourceRange(); |
| 2179 | } |
| 2180 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2181 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2182 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2183 | InitRange.getBegin(), None, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2184 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2185 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2186 | QualType ResultType = CurrentObjectType; |
| 2187 | if (!ResultType->isArrayType()) |
| 2188 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2189 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2190 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2191 | // Pre-allocate storage for the structured initializer list. |
| 2192 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2193 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2194 | bool GotNumInits = false; |
| 2195 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2196 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2197 | GotNumInits = true; |
| 2198 | } else if (Index < IList->getNumInits()) { |
| 2199 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2200 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2201 | GotNumInits = true; |
| 2202 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2205 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2206 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2207 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2208 | NumElements = CAType->getSize().getZExtValue(); |
| 2209 | // Simple heuristic so that we don't allocate a very large |
| 2210 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2211 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2212 | NumElements = 0; |
| 2213 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2214 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2215 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2216 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2217 | RecordDecl *RDecl = RType->getDecl(); |
| 2218 | if (RDecl->isUnion()) |
| 2219 | NumElements = 1; |
| 2220 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2221 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2222 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2225 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2226 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2227 | // Link this new initializer list into the structured initializer |
| 2228 | // lists. |
| 2229 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2230 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2231 | else { |
| 2232 | Result->setSyntacticForm(IList); |
| 2233 | SyntacticToSemantic[IList] = Result; |
| 2234 | } |
| 2235 | |
| 2236 | return Result; |
| 2237 | } |
| 2238 | |
| 2239 | /// Update the initializer at index @p StructuredIndex within the |
| 2240 | /// structured initializer list to the value @p expr. |
| 2241 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2242 | unsigned &StructuredIndex, |
| 2243 | Expr *expr) { |
| 2244 | // No structured initializer list to update |
| 2245 | if (!StructuredList) |
| 2246 | return; |
| 2247 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2248 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2249 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2250 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2251 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2252 | diag::warn_initializer_overrides) |
| 2253 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2254 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2255 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2256 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2257 | << PrevInit->getSourceRange(); |
| 2258 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2259 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2260 | ++StructuredIndex; |
| 2261 | } |
| 2262 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2263 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2264 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2265 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2266 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2267 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2268 | /// added, on success. If everything went okay, Value will receive the |
| 2269 | /// value of the constant expression. |
| 2270 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2271 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2272 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2273 | |
| 2274 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2275 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2276 | if (Result.isInvalid()) |
| 2277 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2278 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2279 | if (Value.isSigned() && Value.isNegative()) |
| 2280 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2281 | << Value.toString(10) << Index->getSourceRange(); |
| 2282 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2283 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2284 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2287 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2288 | SourceLocation Loc, |
| 2289 | bool GNUSyntax, |
| 2290 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2291 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2292 | |
| 2293 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2294 | SmallVector<ASTDesignator, 32> Designators; |
| 2295 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2296 | |
| 2297 | // Build designators and check array designator expressions. |
| 2298 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2299 | const Designator &D = Desig.getDesignator(Idx); |
| 2300 | switch (D.getKind()) { |
| 2301 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2302 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2303 | D.getFieldLoc())); |
| 2304 | break; |
| 2305 | |
| 2306 | case Designator::ArrayDesignator: { |
| 2307 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2308 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2309 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2310 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2311 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2312 | Invalid = true; |
| 2313 | else { |
| 2314 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2315 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2316 | D.getRBracketLoc())); |
| 2317 | InitExpressions.push_back(Index); |
| 2318 | } |
| 2319 | break; |
| 2320 | } |
| 2321 | |
| 2322 | case Designator::ArrayRangeDesignator: { |
| 2323 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2324 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2325 | llvm::APSInt StartValue; |
| 2326 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2327 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2328 | StartIndex->isValueDependent(); |
| 2329 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2330 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2331 | if (!StartDependent) |
| 2332 | StartIndex = |
| 2333 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2334 | if (!EndDependent) |
| 2335 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2336 | |
| 2337 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2338 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2339 | else { |
| 2340 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2341 | if (StartDependent || EndDependent) { |
| 2342 | // Nothing to compute. |
| 2343 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2344 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2345 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2346 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2347 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2348 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2349 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2350 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2351 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2352 | Invalid = true; |
| 2353 | } else { |
| 2354 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2356 | D.getEllipsisLoc(), |
| 2357 | D.getRBracketLoc())); |
| 2358 | InitExpressions.push_back(StartIndex); |
| 2359 | InitExpressions.push_back(EndIndex); |
| 2360 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2361 | } |
| 2362 | break; |
| 2363 | } |
| 2364 | } |
| 2365 | } |
| 2366 | |
| 2367 | if (Invalid || Init.isInvalid()) |
| 2368 | return ExprError(); |
| 2369 | |
| 2370 | // Clear out the expressions within the designation. |
| 2371 | Desig.ClearExprs(*this); |
| 2372 | |
| 2373 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2374 | = DesignatedInitExpr::Create(Context, |
| 2375 | Designators.data(), Designators.size(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2376 | InitExpressions, Loc, GNUSyntax, |
| 2377 | Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2378 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2379 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2380 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2381 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2382 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2383 | return Owned(DIE); |
| 2384 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2385 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2386 | //===----------------------------------------------------------------------===// |
| 2387 | // Initialization entity |
| 2388 | //===----------------------------------------------------------------------===// |
| 2389 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2390 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2391 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2392 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2393 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2394 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2395 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2396 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2397 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2398 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2399 | Type = VT->getElementType(); |
| 2400 | } else { |
| 2401 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2402 | assert(CT && "Unexpected type"); |
| 2403 | Kind = EK_ComplexElement; |
| 2404 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2405 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2408 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2409 | CXXBaseSpecifier *Base, |
| 2410 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2411 | { |
| 2412 | InitializedEntity Result; |
| 2413 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2414 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2415 | if (IsInheritedVirtualBase) |
| 2416 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2417 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2418 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2419 | return Result; |
| 2420 | } |
| 2421 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2422 | DeclarationName InitializedEntity::getName() const { |
| 2423 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2424 | case EK_Parameter: { |
| 2425 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2426 | return (D ? D->getDeclName() : DeclarationName()); |
| 2427 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2428 | |
| 2429 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2430 | case EK_Member: |
| 2431 | return VariableOrMember->getDeclName(); |
| 2432 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2433 | case EK_LambdaCapture: |
| 2434 | return Capture.Var->getDeclName(); |
| 2435 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2436 | case EK_Result: |
| 2437 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2438 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2439 | case EK_Temporary: |
| 2440 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2441 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2442 | case EK_ArrayElement: |
| 2443 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2444 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2445 | case EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2446 | case EK_CompoundLiteralInit: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2447 | return DeclarationName(); |
| 2448 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2449 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2450 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2453 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2454 | switch (getKind()) { |
| 2455 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2456 | case EK_Member: |
| 2457 | return VariableOrMember; |
| 2458 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2459 | case EK_Parameter: |
| 2460 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2461 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2462 | case EK_Result: |
| 2463 | case EK_Exception: |
| 2464 | case EK_New: |
| 2465 | case EK_Temporary: |
| 2466 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2467 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2468 | case EK_ArrayElement: |
| 2469 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2470 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2471 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2472 | case EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2473 | case EK_CompoundLiteralInit: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2474 | return 0; |
| 2475 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2476 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2477 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2480 | bool InitializedEntity::allowsNRVO() const { |
| 2481 | switch (getKind()) { |
| 2482 | case EK_Result: |
| 2483 | case EK_Exception: |
| 2484 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2485 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2486 | case EK_Variable: |
| 2487 | case EK_Parameter: |
| 2488 | case EK_Member: |
| 2489 | case EK_New: |
| 2490 | case EK_Temporary: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2491 | case EK_CompoundLiteralInit: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2492 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2493 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2494 | case EK_ArrayElement: |
| 2495 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2496 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2497 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2498 | case EK_LambdaCapture: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2499 | break; |
| 2500 | } |
| 2501 | |
| 2502 | return false; |
| 2503 | } |
| 2504 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2505 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
| 2506 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 2507 | for (unsigned I = 0; I != Depth; ++I) |
| 2508 | OS << "`-"; |
| 2509 | |
| 2510 | switch (getKind()) { |
| 2511 | case EK_Variable: OS << "Variable"; break; |
| 2512 | case EK_Parameter: OS << "Parameter"; break; |
| 2513 | case EK_Result: OS << "Result"; break; |
| 2514 | case EK_Exception: OS << "Exception"; break; |
| 2515 | case EK_Member: OS << "Member"; break; |
| 2516 | case EK_New: OS << "New"; break; |
| 2517 | case EK_Temporary: OS << "Temporary"; break; |
| 2518 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
| 2519 | case EK_Base: OS << "Base"; break; |
| 2520 | case EK_Delegating: OS << "Delegating"; break; |
| 2521 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 2522 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 2523 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 2524 | case EK_BlockElement: OS << "Block"; break; |
| 2525 | case EK_LambdaCapture: |
| 2526 | OS << "LambdaCapture "; |
| 2527 | getCapturedVar()->printName(OS); |
| 2528 | break; |
| 2529 | } |
| 2530 | |
| 2531 | if (Decl *D = getDecl()) { |
| 2532 | OS << " "; |
| 2533 | cast<NamedDecl>(D)->printQualifiedName(OS); |
| 2534 | } |
| 2535 | |
| 2536 | OS << " '" << getType().getAsString() << "'\n"; |
| 2537 | |
| 2538 | return Depth + 1; |
| 2539 | } |
| 2540 | |
| 2541 | void InitializedEntity::dump() const { |
| 2542 | dumpImpl(llvm::errs()); |
| 2543 | } |
| 2544 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2545 | //===----------------------------------------------------------------------===// |
| 2546 | // Initialization sequence |
| 2547 | //===----------------------------------------------------------------------===// |
| 2548 | |
| 2549 | void InitializationSequence::Step::Destroy() { |
| 2550 | switch (Kind) { |
| 2551 | case SK_ResolveAddressOfOverloadedFunction: |
| 2552 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2553 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2554 | case SK_CastDerivedToBaseLValue: |
| 2555 | case SK_BindReference: |
| 2556 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2557 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2558 | case SK_UserConversion: |
| 2559 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2560 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2561 | case SK_QualificationConversionLValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2562 | case SK_LValueToRValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2563 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2564 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2565 | case SK_UnwrapInitList: |
| 2566 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2567 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2568 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2569 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2570 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2571 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2572 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2573 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2574 | case SK_PassByIndirectCopyRestore: |
| 2575 | case SK_PassByIndirectRestore: |
| 2576 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2577 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2578 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2579 | case SK_OCLZeroEvent: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2580 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2581 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2582 | case SK_ConversionSequence: |
| 2583 | delete ICS; |
| 2584 | } |
| 2585 | } |
| 2586 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2587 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2588 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2589 | } |
| 2590 | |
| 2591 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2592 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2593 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2594 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2595 | switch (getFailureKind()) { |
| 2596 | case FK_TooManyInitsForReference: |
| 2597 | case FK_ArrayNeedsInitList: |
| 2598 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 2599 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 2600 | case FK_NarrowStringIntoWideCharArray: |
| 2601 | case FK_WideStringIntoCharArray: |
| 2602 | case FK_IncompatWideStringIntoWideChar: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2603 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2604 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2605 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2606 | case FK_RValueReferenceBindingToLValue: |
| 2607 | case FK_ReferenceInitDropsQualifiers: |
| 2608 | case FK_ReferenceInitFailed: |
| 2609 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2610 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2611 | case FK_TooManyInitsForScalar: |
| 2612 | case FK_ReferenceBindingToInitList: |
| 2613 | case FK_InitListBadDestinationType: |
| 2614 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2615 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2616 | case FK_ArrayTypeMismatch: |
| 2617 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2618 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2619 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2620 | case FK_PlaceholderType: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2621 | case FK_InitListElementCopyFailure: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2622 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2623 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2624 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2625 | case FK_ReferenceInitOverloadFailed: |
| 2626 | case FK_UserConversionOverloadFailed: |
| 2627 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2628 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2629 | return FailedOverloadResult == OR_Ambiguous; |
| 2630 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2631 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2632 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2635 | bool InitializationSequence::isConstructorInitialization() const { |
| 2636 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2637 | } |
| 2638 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2639 | void |
| 2640 | InitializationSequence |
| 2641 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2642 | DeclAccessPair Found, |
| 2643 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2644 | Step S; |
| 2645 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2646 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2647 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2648 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2649 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2650 | Steps.push_back(S); |
| 2651 | } |
| 2652 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2653 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2654 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2655 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2656 | switch (VK) { |
| 2657 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2658 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2659 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2660 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2661 | S.Type = BaseType; |
| 2662 | Steps.push_back(S); |
| 2663 | } |
| 2664 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2665 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2666 | bool BindingTemporary) { |
| 2667 | Step S; |
| 2668 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2669 | S.Type = T; |
| 2670 | Steps.push_back(S); |
| 2671 | } |
| 2672 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2673 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2674 | Step S; |
| 2675 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2676 | S.Type = T; |
| 2677 | Steps.push_back(S); |
| 2678 | } |
| 2679 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2680 | void |
| 2681 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2682 | DeclAccessPair FoundDecl, |
| 2683 | QualType T, |
| 2684 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2685 | Step S; |
| 2686 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2687 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2688 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2689 | S.Function.Function = Function; |
| 2690 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2691 | Steps.push_back(S); |
| 2692 | } |
| 2693 | |
| 2694 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2695 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2696 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2697 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2698 | switch (VK) { |
| 2699 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2700 | S.Kind = SK_QualificationConversionRValue; |
| 2701 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2702 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2703 | S.Kind = SK_QualificationConversionXValue; |
| 2704 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2705 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2706 | S.Kind = SK_QualificationConversionLValue; |
| 2707 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2708 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2709 | S.Type = Ty; |
| 2710 | Steps.push_back(S); |
| 2711 | } |
| 2712 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2713 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2714 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2715 | |
| 2716 | Step S; |
| 2717 | S.Kind = SK_LValueToRValue; |
| 2718 | S.Type = Ty; |
| 2719 | Steps.push_back(S); |
| 2720 | } |
| 2721 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2722 | void InitializationSequence::AddConversionSequenceStep( |
| 2723 | const ImplicitConversionSequence &ICS, |
| 2724 | QualType T) { |
| 2725 | Step S; |
| 2726 | S.Kind = SK_ConversionSequence; |
| 2727 | S.Type = T; |
| 2728 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2729 | Steps.push_back(S); |
| 2730 | } |
| 2731 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2732 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2733 | Step S; |
| 2734 | S.Kind = SK_ListInitialization; |
| 2735 | S.Type = T; |
| 2736 | Steps.push_back(S); |
| 2737 | } |
| 2738 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2739 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2740 | InitializationSequence |
| 2741 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2742 | AccessSpecifier Access, |
| 2743 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2744 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2745 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2746 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2747 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2748 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2749 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2750 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2751 | S.Function.Function = Constructor; |
| 2752 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2753 | Steps.push_back(S); |
| 2754 | } |
| 2755 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2756 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2757 | Step S; |
| 2758 | S.Kind = SK_ZeroInitialization; |
| 2759 | S.Type = T; |
| 2760 | Steps.push_back(S); |
| 2761 | } |
| 2762 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2763 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2764 | Step S; |
| 2765 | S.Kind = SK_CAssignment; |
| 2766 | S.Type = T; |
| 2767 | Steps.push_back(S); |
| 2768 | } |
| 2769 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2770 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2771 | Step S; |
| 2772 | S.Kind = SK_StringInit; |
| 2773 | S.Type = T; |
| 2774 | Steps.push_back(S); |
| 2775 | } |
| 2776 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2777 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2778 | Step S; |
| 2779 | S.Kind = SK_ObjCObjectConversion; |
| 2780 | S.Type = T; |
| 2781 | Steps.push_back(S); |
| 2782 | } |
| 2783 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2784 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2785 | Step S; |
| 2786 | S.Kind = SK_ArrayInit; |
| 2787 | S.Type = T; |
| 2788 | Steps.push_back(S); |
| 2789 | } |
| 2790 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2791 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2792 | Step S; |
| 2793 | S.Kind = SK_ParenthesizedArrayInit; |
| 2794 | S.Type = T; |
| 2795 | Steps.push_back(S); |
| 2796 | } |
| 2797 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2798 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2799 | bool shouldCopy) { |
| 2800 | Step s; |
| 2801 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2802 | : SK_PassByIndirectRestore); |
| 2803 | s.Type = type; |
| 2804 | Steps.push_back(s); |
| 2805 | } |
| 2806 | |
| 2807 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2808 | Step S; |
| 2809 | S.Kind = SK_ProduceObjCObject; |
| 2810 | S.Type = T; |
| 2811 | Steps.push_back(S); |
| 2812 | } |
| 2813 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2814 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2815 | Step S; |
| 2816 | S.Kind = SK_StdInitializerList; |
| 2817 | S.Type = T; |
| 2818 | Steps.push_back(S); |
| 2819 | } |
| 2820 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2821 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 2822 | Step S; |
| 2823 | S.Kind = SK_OCLSamplerInit; |
| 2824 | S.Type = T; |
| 2825 | Steps.push_back(S); |
| 2826 | } |
| 2827 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2828 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 2829 | Step S; |
| 2830 | S.Kind = SK_OCLZeroEvent; |
| 2831 | S.Type = T; |
| 2832 | Steps.push_back(S); |
| 2833 | } |
| 2834 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2835 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2836 | InitListExpr *Syntactic) { |
| 2837 | assert(Syntactic->getNumInits() == 1 && |
| 2838 | "Can only rewrap trivial init lists."); |
| 2839 | Step S; |
| 2840 | S.Kind = SK_UnwrapInitList; |
| 2841 | S.Type = Syntactic->getInit(0)->getType(); |
| 2842 | Steps.insert(Steps.begin(), S); |
| 2843 | |
| 2844 | S.Kind = SK_RewrapInitList; |
| 2845 | S.Type = T; |
| 2846 | S.WrappingSyntacticList = Syntactic; |
| 2847 | Steps.push_back(S); |
| 2848 | } |
| 2849 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2850 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2851 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2852 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2853 | this->Failure = Failure; |
| 2854 | this->FailedOverloadResult = Result; |
| 2855 | } |
| 2856 | |
| 2857 | //===----------------------------------------------------------------------===// |
| 2858 | // Attempt initialization |
| 2859 | //===----------------------------------------------------------------------===// |
| 2860 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2861 | static void MaybeProduceObjCObject(Sema &S, |
| 2862 | InitializationSequence &Sequence, |
| 2863 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2864 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2865 | |
| 2866 | /// When initializing a parameter, produce the value if it's marked |
| 2867 | /// __attribute__((ns_consumed)). |
| 2868 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2869 | if (!Entity.isParameterConsumed()) |
| 2870 | return; |
| 2871 | |
| 2872 | assert(Entity.getType()->isObjCRetainableType() && |
| 2873 | "consuming an object of unretainable type?"); |
| 2874 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2875 | |
| 2876 | /// When initializing a return value, if the return type is a |
| 2877 | /// retainable type, then returns need to immediately retain the |
| 2878 | /// object. If an autorelease is required, it will be done at the |
| 2879 | /// last instant. |
| 2880 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2881 | if (!Entity.getType()->isObjCRetainableType()) |
| 2882 | return; |
| 2883 | |
| 2884 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2885 | } |
| 2886 | } |
| 2887 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2888 | /// \brief When initializing from init list via constructor, handle |
| 2889 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2890 | /// |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2891 | /// \return true if we have handled initialization of an object of type |
| 2892 | /// std::initializer_list<T>, false otherwise. |
| 2893 | static bool TryInitializerListConstruction(Sema &S, |
| 2894 | InitListExpr *List, |
| 2895 | QualType DestType, |
| 2896 | InitializationSequence &Sequence) { |
| 2897 | QualType E; |
| 2898 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2899 | return false; |
| 2900 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2901 | // Check that each individual element can be copy-constructed. But since we |
| 2902 | // have no place to store further information, we'll recalculate everything |
| 2903 | // later. |
| 2904 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 2905 | S.Context.getConstantArrayType(E, |
| 2906 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2907 | List->getNumInits()), |
| 2908 | ArrayType::Normal, 0)); |
| 2909 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 2910 | 0, HiddenArray); |
| 2911 | for (unsigned i = 0, n = List->getNumInits(); i < n; ++i) { |
| 2912 | Element.setElementIndex(i); |
| 2913 | if (!S.CanPerformCopyInitialization(Element, List->getInit(i))) { |
| 2914 | Sequence.SetFailed( |
| 2915 | InitializationSequence::FK_InitListElementCopyFailure); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2916 | return true; |
| 2917 | } |
| 2918 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2919 | Sequence.AddStdInitializerListConstructionStep(DestType); |
| 2920 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2921 | } |
| 2922 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2923 | static OverloadingResult |
| 2924 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2925 | MultiExprArg Args, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2926 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2927 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2928 | OverloadCandidateSet::iterator &Best, |
| 2929 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2930 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2931 | CandidateSet.clear(); |
| 2932 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2933 | for (ArrayRef<NamedDecl *>::iterator |
| 2934 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2935 | NamedDecl *D = *Con; |
| 2936 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2937 | bool SuppressUserConversions = false; |
| 2938 | |
| 2939 | // Find the constructor (which may be a template). |
| 2940 | CXXConstructorDecl *Constructor = 0; |
| 2941 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 2942 | if (ConstructorTmpl) |
| 2943 | Constructor = cast<CXXConstructorDecl>( |
| 2944 | ConstructorTmpl->getTemplatedDecl()); |
| 2945 | else { |
| 2946 | Constructor = cast<CXXConstructorDecl>(D); |
| 2947 | |
| 2948 | // If we're performing copy initialization using a copy constructor, we |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2949 | // suppress user-defined conversions on the arguments. We do the same for |
| 2950 | // move constructors. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2951 | if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) && |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2952 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2953 | SuppressUserConversions = true; |
| 2954 | } |
| 2955 | |
| 2956 | if (!Constructor->isInvalidDecl() && |
| 2957 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2958 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2959 | if (ConstructorTmpl) |
| 2960 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2961 | /*ExplicitArgs*/ 0, Args, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2962 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2963 | else { |
| 2964 | // C++ [over.match.copy]p1: |
| 2965 | // - When initializing a temporary to be bound to the first parameter |
| 2966 | // of a constructor that takes a reference to possibly cv-qualified |
| 2967 | // T as its first argument, called with a single argument in the |
| 2968 | // context of direct-initialization, explicit conversion functions |
| 2969 | // are also considered. |
| 2970 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2971 | Args.size() == 1 && |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2972 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2973 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2974 | SuppressUserConversions, |
| 2975 | /*PartialOverloading=*/false, |
| 2976 | /*AllowExplicit=*/AllowExplicitConv); |
| 2977 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | |
| 2981 | // Perform overload resolution and return the result. |
| 2982 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 2983 | } |
| 2984 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2985 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2986 | /// enumerates the constructors of the initialized entity and performs overload |
| 2987 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2988 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2989 | /// class type. |
| 2990 | static void TryConstructorInitialization(Sema &S, |
| 2991 | const InitializedEntity &Entity, |
| 2992 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2993 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2994 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2995 | bool InitListSyntax = false) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2996 | assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2997 | "InitListSyntax must come with a single initializer list argument."); |
| 2998 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2999 | // The type we're constructing needs to be complete. |
| 3000 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3001 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3002 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3006 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3007 | CXXRecordDecl *DestRecordDecl |
| 3008 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3009 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3010 | // Build the candidate set directly in the initialization sequence |
| 3011 | // structure, so that it will persist if we fail. |
| 3012 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3013 | |
| 3014 | // Determine whether we are allowed to call explicit constructors or |
| 3015 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3016 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3017 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3018 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3019 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3020 | // applicable constructors are enumerated, and the best one is chosen |
| 3021 | // through overload resolution. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3022 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3023 | // The container holding the constructors can under certain conditions |
| 3024 | // be changed while iterating (e.g. because of deserialization). |
| 3025 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3026 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3027 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3028 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3029 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3030 | bool AsInitializerList = false; |
| 3031 | |
| 3032 | // C++11 [over.match.list]p1: |
| 3033 | // When objects of non-aggregate type T are list-initialized, overload |
| 3034 | // resolution selects the constructor in two phases: |
| 3035 | // - Initially, the candidate functions are the initializer-list |
| 3036 | // constructors of the class T and the argument list consists of the |
| 3037 | // initializer list as a single argument. |
| 3038 | if (InitListSyntax) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3039 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3040 | AsInitializerList = true; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3041 | |
| 3042 | // If the initializer list has no elements and T has a default constructor, |
| 3043 | // the first phase is omitted. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3044 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3045 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3046 | CandidateSet, Ctors, Best, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3047 | CopyInitialization, AllowExplicit, |
| 3048 | /*OnlyListConstructor=*/true, |
| 3049 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3050 | |
| 3051 | // Time to unwrap the init list. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3052 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
| 3055 | // C++11 [over.match.list]p1: |
| 3056 | // - If no viable initializer-list constructor is found, overload resolution |
| 3057 | // is performed again, where the candidate functions are all the |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3058 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3059 | // elements of the initializer list. |
| 3060 | if (Result == OR_No_Viable_Function) { |
| 3061 | AsInitializerList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3062 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3063 | CandidateSet, Ctors, Best, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3064 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3065 | /*OnlyListConstructors=*/false, |
| 3066 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3067 | } |
| 3068 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3069 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3070 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3071 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3072 | Result); |
| 3073 | return; |
| 3074 | } |
| 3075 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3076 | // C++11 [dcl.init]p6: |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3077 | // If a program calls for the default initialization of an object |
| 3078 | // of a const-qualified type T, T shall be a class type with a |
| 3079 | // user-provided default constructor. |
| 3080 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3081 | Entity.getType().isConstQualified() && |
Aaron Ballman | 5121781 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 3082 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3083 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3084 | return; |
| 3085 | } |
| 3086 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3087 | // C++11 [over.match.list]p1: |
| 3088 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3089 | // initializer is ill-formed. |
| 3090 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 3091 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 3092 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3093 | return; |
| 3094 | } |
| 3095 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3096 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3097 | // subsumed by the initialization. |
| 3098 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3099 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 3100 | Best->FoundDecl.getAccess(), |
| 3101 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3102 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3105 | static bool |
| 3106 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3107 | Expr *Initializer, |
| 3108 | QualType &SourceType, |
| 3109 | QualType &UnqualifiedSourceType, |
| 3110 | QualType UnqualifiedTargetType, |
| 3111 | InitializationSequence &Sequence) { |
| 3112 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3113 | S.Context.OverloadTy) { |
| 3114 | DeclAccessPair Found; |
| 3115 | bool HadMultipleCandidates = false; |
| 3116 | if (FunctionDecl *Fn |
| 3117 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3118 | UnqualifiedTargetType, |
| 3119 | false, Found, |
| 3120 | &HadMultipleCandidates)) { |
| 3121 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3122 | HadMultipleCandidates); |
| 3123 | SourceType = Fn->getType(); |
| 3124 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3125 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3126 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3127 | return true; |
| 3128 | } |
| 3129 | } |
| 3130 | return false; |
| 3131 | } |
| 3132 | |
| 3133 | static void TryReferenceInitializationCore(Sema &S, |
| 3134 | const InitializedEntity &Entity, |
| 3135 | const InitializationKind &Kind, |
| 3136 | Expr *Initializer, |
| 3137 | QualType cv1T1, QualType T1, |
| 3138 | Qualifiers T1Quals, |
| 3139 | QualType cv2T2, QualType T2, |
| 3140 | Qualifiers T2Quals, |
| 3141 | InitializationSequence &Sequence); |
| 3142 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3143 | static void TryValueInitialization(Sema &S, |
| 3144 | const InitializedEntity &Entity, |
| 3145 | const InitializationKind &Kind, |
| 3146 | InitializationSequence &Sequence, |
| 3147 | InitListExpr *InitList = 0); |
| 3148 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3149 | static void TryListInitialization(Sema &S, |
| 3150 | const InitializedEntity &Entity, |
| 3151 | const InitializationKind &Kind, |
| 3152 | InitListExpr *InitList, |
| 3153 | InitializationSequence &Sequence); |
| 3154 | |
| 3155 | /// \brief Attempt list initialization of a reference. |
| 3156 | static void TryReferenceListInitialization(Sema &S, |
| 3157 | const InitializedEntity &Entity, |
| 3158 | const InitializationKind &Kind, |
| 3159 | InitListExpr *InitList, |
| 3160 | InitializationSequence &Sequence) |
| 3161 | { |
| 3162 | // First, catch C++03 where this isn't possible. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3163 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3164 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3165 | return; |
| 3166 | } |
| 3167 | |
| 3168 | QualType DestType = Entity.getType(); |
| 3169 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3170 | Qualifiers T1Quals; |
| 3171 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3172 | |
| 3173 | // Reference initialization via an initializer list works thus: |
| 3174 | // If the initializer list consists of a single element that is |
| 3175 | // reference-related to the referenced type, bind directly to that element |
| 3176 | // (possibly creating temporaries). |
| 3177 | // Otherwise, initialize a temporary with the initializer list and |
| 3178 | // bind to that. |
| 3179 | if (InitList->getNumInits() == 1) { |
| 3180 | Expr *Initializer = InitList->getInit(0); |
| 3181 | QualType cv2T2 = Initializer->getType(); |
| 3182 | Qualifiers T2Quals; |
| 3183 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3184 | |
| 3185 | // If this fails, creating a temporary wouldn't work either. |
| 3186 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3187 | T1, Sequence)) |
| 3188 | return; |
| 3189 | |
| 3190 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3191 | bool dummy1, dummy2, dummy3; |
| 3192 | Sema::ReferenceCompareResult RefRelationship |
| 3193 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3194 | dummy2, dummy3); |
| 3195 | if (RefRelationship >= Sema::Ref_Related) { |
| 3196 | // Try to bind the reference here. |
| 3197 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3198 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3199 | if (Sequence) |
| 3200 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3201 | return; |
| 3202 | } |
Richard Smith | 02d65ee | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3203 | |
| 3204 | // Update the initializer if we've resolved an overloaded function. |
| 3205 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3206 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | // Not reference-related. Create a temporary and bind to that. |
| 3210 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3211 | |
| 3212 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3213 | if (Sequence) { |
| 3214 | if (DestType->isRValueReferenceType() || |
| 3215 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3216 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3217 | else |
| 3218 | Sequence.SetFailed( |
| 3219 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3220 | } |
| 3221 | } |
| 3222 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3223 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3224 | static void TryListInitialization(Sema &S, |
| 3225 | const InitializedEntity &Entity, |
| 3226 | const InitializationKind &Kind, |
| 3227 | InitListExpr *InitList, |
| 3228 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3229 | QualType DestType = Entity.getType(); |
| 3230 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3231 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3232 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3233 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3234 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3235 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3236 | return; |
| 3237 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3238 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3239 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3240 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3241 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3242 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3243 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3244 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3245 | return; |
| 3246 | } |
| 3247 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3248 | // C++11 [dcl.init.list]p3: |
| 3249 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3250 | if (!DestType->isAggregateType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3251 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3252 | // - Otherwise, if the initializer list has no elements and T is a |
| 3253 | // class type with a default constructor, the object is |
| 3254 | // value-initialized. |
| 3255 | if (InitList->getNumInits() == 0) { |
| 3256 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3257 | if (RD->hasDefaultConstructor()) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3258 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3259 | return; |
| 3260 | } |
| 3261 | } |
| 3262 | |
| 3263 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3264 | // an initializer_list object constructed [...] |
| 3265 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3266 | return; |
| 3267 | |
| 3268 | // - Otherwise, if T is a class type, constructors are considered. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3269 | Expr *InitListAsExpr = InitList; |
| 3270 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3271 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3272 | } else |
| 3273 | Sequence.SetFailed( |
| 3274 | InitializationSequence::FK_InitListBadDestinationType); |
| 3275 | return; |
| 3276 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3279 | InitListChecker CheckInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 3280 | DestType, /*VerifyOnly=*/true, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3281 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3282 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3283 | if (CheckInitList.HadError()) { |
| 3284 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3285 | return; |
| 3286 | } |
| 3287 | |
| 3288 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3289 | Sequence.AddListInitializationStep(DestType); |
| 3290 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3291 | |
| 3292 | /// \brief Try a reference initialization that involves calling a conversion |
| 3293 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3294 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3295 | const InitializedEntity &Entity, |
| 3296 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3297 | Expr *Initializer, |
| 3298 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3299 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3300 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3301 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3302 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3303 | QualType cv2T2 = Initializer->getType(); |
| 3304 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3305 | |
| 3306 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3307 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3308 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3309 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3310 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3311 | ObjCConversion, |
| 3312 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3313 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3314 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3315 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3316 | (void)ObjCLifetimeConversion; |
| 3317 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3318 | // Build the candidate set directly in the initialization sequence |
| 3319 | // structure, so that it will persist if we fail. |
| 3320 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3321 | CandidateSet.clear(); |
| 3322 | |
| 3323 | // Determine whether we are allowed to call explicit constructors or |
| 3324 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3325 | bool AllowExplicit = Kind.AllowExplicit(); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3326 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctions(); |
| 3327 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3328 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3329 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3330 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3331 | // The type we're converting to is a class type. Enumerate its constructors |
| 3332 | // to see if there is a suitable conversion. |
| 3333 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3334 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3335 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3336 | // The container holding the constructors can under certain conditions |
| 3337 | // be changed while iterating (e.g. because of deserialization). |
| 3338 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3339 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3340 | for (SmallVector<NamedDecl*, 16>::iterator |
| 3341 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3342 | NamedDecl *D = *CI; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3343 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3344 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3345 | // Find the constructor (which may be a template). |
| 3346 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3347 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3348 | if (ConstructorTmpl) |
| 3349 | Constructor = cast<CXXConstructorDecl>( |
| 3350 | ConstructorTmpl->getTemplatedDecl()); |
| 3351 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3352 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3353 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3354 | if (!Constructor->isInvalidDecl() && |
| 3355 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3356 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3357 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3358 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3359 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3360 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3361 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3362 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3363 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3364 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3365 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3366 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3367 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3368 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3369 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3370 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3371 | const RecordType *T2RecordType = 0; |
| 3372 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3373 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3374 | // The type we're converting from is a class type, enumerate its conversion |
| 3375 | // functions. |
| 3376 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3377 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3378 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3379 | CXXRecordDecl::conversion_iterator> |
| 3380 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3381 | for (CXXRecordDecl::conversion_iterator |
| 3382 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3383 | NamedDecl *D = *I; |
| 3384 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3385 | if (isa<UsingShadowDecl>(D)) |
| 3386 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3387 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3388 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3389 | CXXConversionDecl *Conv; |
| 3390 | if (ConvTemplate) |
| 3391 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3392 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3393 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3394 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3395 | // If the conversion function doesn't return a reference type, |
| 3396 | // it can't be considered for this conversion unless we're allowed to |
| 3397 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3398 | // FIXME: Do we need to make sure that we only consider conversion |
| 3399 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3400 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3401 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3402 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3403 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3404 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3405 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3406 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3407 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3408 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3409 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3410 | } |
| 3411 | } |
| 3412 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3413 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3414 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3415 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3416 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3417 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3418 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3419 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3420 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3421 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3422 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3423 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3424 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3425 | // This is the overload that will be used for this initialization step if we |
| 3426 | // use this initialization. Mark it as referenced. |
| 3427 | Function->setReferenced(); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3428 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3429 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3430 | if (isa<CXXConversionDecl>(Function)) |
| 3431 | T2 = Function->getResultType(); |
| 3432 | else |
| 3433 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3434 | |
| 3435 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3436 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3437 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3438 | T2.getNonLValueExprType(S.Context), |
| 3439 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3440 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3441 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3442 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3443 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3444 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3445 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3446 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3447 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3448 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3449 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3450 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3451 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3452 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3453 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3454 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3455 | NewDerivedToBase, NewObjCConversion, |
| 3456 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3457 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3458 | // If the type we've converted to is not reference-related to the |
| 3459 | // type we're looking for, then there is another conversion step |
| 3460 | // we need to perform to produce a temporary of the right type |
| 3461 | // that we'll be binding to. |
| 3462 | ImplicitConversionSequence ICS; |
| 3463 | ICS.setStandard(); |
| 3464 | ICS.Standard = Best->FinalConversion; |
| 3465 | T2 = ICS.Standard.getToType(2); |
| 3466 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3467 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3468 | Sequence.AddDerivedToBaseCastStep( |
| 3469 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3470 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3471 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3472 | else if (NewObjCConversion) |
| 3473 | Sequence.AddObjCObjectConversionStep( |
| 3474 | S.Context.getQualifiedType(T1, |
| 3475 | T2.getNonReferenceType().getQualifiers())); |
| 3476 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3477 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3478 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3479 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3480 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3481 | return OR_Success; |
| 3482 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3483 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3484 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3485 | const InitializedEntity &Entity, |
| 3486 | Expr *CurInitExpr); |
| 3487 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3488 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3489 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3490 | const InitializedEntity &Entity, |
| 3491 | const InitializationKind &Kind, |
| 3492 | Expr *Initializer, |
| 3493 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3494 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3495 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3496 | Qualifiers T1Quals; |
| 3497 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3498 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3499 | Qualifiers T2Quals; |
| 3500 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3501 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3502 | // If the initializer is the address of an overloaded function, try |
| 3503 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3504 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3505 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3506 | T1, Sequence)) |
| 3507 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3508 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3509 | // Delegate everything else to a subfunction. |
| 3510 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3511 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3512 | } |
| 3513 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3514 | /// Converts the target of reference initialization so that it has the |
| 3515 | /// appropriate qualifiers and value kind. |
| 3516 | /// |
| 3517 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3518 | /// \code |
| 3519 | /// int x; |
| 3520 | /// const int &r = x; |
| 3521 | /// \endcode |
| 3522 | /// |
| 3523 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3524 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3525 | /// \code |
| 3526 | /// const int &r = someStruct.bitfield; |
| 3527 | /// \endcode |
| 3528 | static ExprValueKind |
| 3529 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3530 | InitializationSequence &Sequence, |
| 3531 | Expr *Initializer, |
| 3532 | QualType cv1T1, |
| 3533 | Qualifiers T1Quals, |
| 3534 | Qualifiers T2Quals, |
| 3535 | bool IsLValueRef) { |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3536 | bool IsNonAddressableType = Initializer->refersToBitField() || |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3537 | Initializer->refersToVectorElement(); |
| 3538 | |
| 3539 | if (IsNonAddressableType) { |
| 3540 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3541 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3542 | // an rvalue reference. |
| 3543 | // |
| 3544 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3545 | // error to be diagnosed later. |
| 3546 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3547 | assert(Initializer->isGLValue()); |
| 3548 | return Initializer->getValueKind(); |
| 3549 | } |
| 3550 | |
| 3551 | // Force a load so we can materialize a temporary. |
| 3552 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3553 | return VK_RValue; |
| 3554 | } |
| 3555 | |
| 3556 | if (T1Quals != T2Quals) { |
| 3557 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3558 | Initializer->getValueKind()); |
| 3559 | } |
| 3560 | |
| 3561 | return Initializer->getValueKind(); |
| 3562 | } |
| 3563 | |
| 3564 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3565 | /// \brief Reference initialization without resolving overloaded functions. |
| 3566 | static void TryReferenceInitializationCore(Sema &S, |
| 3567 | const InitializedEntity &Entity, |
| 3568 | const InitializationKind &Kind, |
| 3569 | Expr *Initializer, |
| 3570 | QualType cv1T1, QualType T1, |
| 3571 | Qualifiers T1Quals, |
| 3572 | QualType cv2T2, QualType T2, |
| 3573 | Qualifiers T2Quals, |
| 3574 | InitializationSequence &Sequence) { |
| 3575 | QualType DestType = Entity.getType(); |
| 3576 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3577 | // Compute some basic properties of the types and the initializer. |
| 3578 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3579 | bool isRValueRef = !isLValueRef; |
| 3580 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3581 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3582 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3583 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3584 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3585 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3586 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3587 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3588 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3589 | // 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] | 3590 | // "cv2 T2" as follows: |
| 3591 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3592 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3593 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3594 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3595 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3596 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3597 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3598 | bool T1Function = T1->isFunctionType(); |
| 3599 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3600 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3601 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3602 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3603 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3604 | // - 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] | 3605 | // reference-compatible with "cv2 T2," or |
| 3606 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3607 | // 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] | 3608 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3609 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3610 | // to decide whether we're actually binding to a temporary created from |
| 3611 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3612 | if (DerivedToBase) |
| 3613 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3614 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3615 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3616 | else if (ObjCConversion) |
| 3617 | Sequence.AddObjCObjectConversionStep( |
| 3618 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3619 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3620 | ExprValueKind ValueKind = |
| 3621 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3622 | cv1T1, T1Quals, T2Quals, |
| 3623 | isLValueRef); |
| 3624 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3625 | return; |
| 3626 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3627 | |
| 3628 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3629 | // reference-related to T2, and can be implicitly converted to an |
| 3630 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3631 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3632 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3633 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3634 | // If we have an rvalue ref to function type here, the rhs must be |
| 3635 | // an rvalue. |
| 3636 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3637 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3638 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3639 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3640 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3641 | Sequence); |
| 3642 | if (ConvOvlResult == OR_Success) |
| 3643 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3644 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3645 | Sequence.SetOverloadFailure( |
| 3646 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3647 | ConvOvlResult); |
| 3648 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3649 | } |
| 3650 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3651 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3652 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3653 | // 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] | 3654 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3655 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3656 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3657 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3658 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3659 | Sequence.SetOverloadFailure( |
| 3660 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3661 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3662 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3663 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3664 | ? (RefRelationship == Sema::Ref_Related |
| 3665 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3666 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3667 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3668 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3669 | return; |
| 3670 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3671 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3672 | // - If the initializer expression |
| 3673 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3674 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3675 | // Note: functions are handled below. |
| 3676 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3677 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3678 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3679 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3680 | (InitCategory.isXValue() || |
| 3681 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3682 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3683 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3684 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3685 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3686 | // compiler the freedom to perform a copy here or bind to the |
| 3687 | // object, while C++0x requires that we bind directly to the |
| 3688 | // object. Hence, we always bind to the object without making an |
| 3689 | // extra copy. However, in C++03 requires that we check for the |
| 3690 | // presence of a suitable copy constructor: |
| 3691 | // |
| 3692 | // The constructor that would be used to make the copy shall |
| 3693 | // be callable whether or not the copy is actually done. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3694 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3695 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3696 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3697 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3698 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3699 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3700 | if (DerivedToBase) |
| 3701 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3702 | ValueKind); |
| 3703 | else if (ObjCConversion) |
| 3704 | Sequence.AddObjCObjectConversionStep( |
| 3705 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3706 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3707 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3708 | Initializer, cv1T1, |
| 3709 | T1Quals, T2Quals, |
| 3710 | isLValueRef); |
| 3711 | |
| 3712 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3713 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3714 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3715 | |
| 3716 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3717 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3718 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3719 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3720 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3721 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3722 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3723 | Kind, Initializer, |
| 3724 | /*AllowRValues=*/true, |
| 3725 | Sequence); |
| 3726 | if (ConvOvlResult) |
| 3727 | Sequence.SetOverloadFailure( |
| 3728 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3729 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3730 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3731 | return; |
| 3732 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3733 | |
Douglas Gregor | defa32e | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 3734 | if ((RefRelationship == Sema::Ref_Compatible || |
| 3735 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 3736 | isRValueRef && InitCategory.isLValue()) { |
| 3737 | Sequence.SetFailed( |
| 3738 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3739 | return; |
| 3740 | } |
| 3741 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3742 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3743 | return; |
| 3744 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3745 | |
| 3746 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3747 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3748 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3749 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3750 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3751 | // Determine whether we are allowed to call explicit constructors or |
| 3752 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3753 | bool AllowExplicit = Kind.AllowExplicit(); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3754 | |
| 3755 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3756 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3757 | ImplicitConversionSequence ICS |
| 3758 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3759 | /*SuppressUserConversions*/ false, |
| 3760 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3761 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3762 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3763 | /*AllowObjCWritebackConversion=*/false); |
| 3764 | |
| 3765 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3766 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3767 | // this into an overloading ambiguity diagnostic. However, we need |
| 3768 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3769 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3770 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3771 | Sequence.SetOverloadFailure( |
| 3772 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3773 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3774 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3775 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3776 | else |
| 3777 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3778 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3779 | } else { |
| 3780 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3784 | // same cv-qualification as, or greater cv-qualification |
| 3785 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3786 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3787 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3788 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3789 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3790 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3791 | return; |
| 3792 | } |
| 3793 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3794 | // [...] 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] | 3795 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3796 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3797 | InitCategory.isLValue()) { |
| 3798 | Sequence.SetFailed( |
| 3799 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3800 | return; |
| 3801 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3802 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3803 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3804 | return; |
| 3805 | } |
| 3806 | |
| 3807 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3808 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3809 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3810 | const InitializedEntity &Entity, |
| 3811 | const InitializationKind &Kind, |
| 3812 | Expr *Initializer, |
| 3813 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3814 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3815 | } |
| 3816 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3817 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3818 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3819 | const InitializedEntity &Entity, |
| 3820 | const InitializationKind &Kind, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3821 | InitializationSequence &Sequence, |
| 3822 | InitListExpr *InitList) { |
| 3823 | assert((!InitList || InitList->getNumInits() == 0) && |
| 3824 | "Shouldn't use value-init for non-empty init lists"); |
| 3825 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3826 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3827 | // |
| 3828 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3829 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3830 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3831 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3832 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3833 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3834 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3835 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3836 | bool NeedZeroInitialization = true; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3837 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3838 | // C++98: |
| 3839 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 3840 | // (12.1), then the default constructor for T is called (and the |
| 3841 | // initialization is ill-formed if T has no accessible default |
| 3842 | // constructor); |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3843 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3844 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3845 | } else { |
| 3846 | // C++11: |
| 3847 | // -- if T is a class type (clause 9) with either no default constructor |
| 3848 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3849 | // or deleted, then the object is default-initialized; |
| 3850 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3851 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3852 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3853 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3854 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3855 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3856 | // user-provided or deleted default constructor, then the object is |
| 3857 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3858 | // default-initialized; |
Richard Smith | 6678a05 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 3859 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 3860 | // constructor' part was removed by DR1507. |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3861 | if (NeedZeroInitialization) |
| 3862 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3863 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3864 | // C++03: |
| 3865 | // -- if T is a non-union class type without a user-declared constructor, |
| 3866 | // then every non-static data member and base class component of T is |
| 3867 | // value-initialized; |
| 3868 | // [...] A program that calls for [...] value-initialization of an |
| 3869 | // entity of reference type is ill-formed. |
| 3870 | // |
| 3871 | // C++11 doesn't need this handling, because value-initialization does not |
| 3872 | // occur recursively there, and the implicit default constructor is |
| 3873 | // defined as deleted in the problematic cases. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3874 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3875 | ClassDecl->hasUninitializedReferenceMember()) { |
| 3876 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 3877 | return; |
| 3878 | } |
| 3879 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3880 | // If this is list-value-initialization, pass the empty init list on when |
| 3881 | // building the constructor call. This affects the semantics of a few |
| 3882 | // things (such as whether an explicit default constructor can be called). |
| 3883 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3884 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3885 | bool InitListSyntax = InitList; |
| 3886 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3887 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 3888 | InitListSyntax); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3889 | } |
| 3890 | } |
| 3891 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3892 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3895 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3896 | static void TryDefaultInitialization(Sema &S, |
| 3897 | const InitializedEntity &Entity, |
| 3898 | const InitializationKind &Kind, |
| 3899 | InitializationSequence &Sequence) { |
| 3900 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3901 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3902 | // C++ [dcl.init]p6: |
| 3903 | // To default-initialize an object of type T means: |
| 3904 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3905 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3906 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3907 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3908 | // constructor for T is called (and the initialization is ill-formed if |
| 3909 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3910 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3911 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3912 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3913 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3914 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3915 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3916 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3917 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3918 | // 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] | 3919 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3920 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3921 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3922 | return; |
| 3923 | } |
| 3924 | |
| 3925 | // If the destination type has a lifetime property, zero-initialize it. |
| 3926 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3927 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3928 | return; |
| 3929 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3932 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3933 | /// which enumerates all conversion functions and performs overload resolution |
| 3934 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3935 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3936 | const InitializedEntity &Entity, |
| 3937 | const InitializationKind &Kind, |
| 3938 | Expr *Initializer, |
| 3939 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3940 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3941 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3942 | QualType SourceType = Initializer->getType(); |
| 3943 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3944 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3945 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3946 | // Build the candidate set directly in the initialization sequence |
| 3947 | // structure, so that it will persist if we fail. |
| 3948 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3949 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3950 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3951 | // Determine whether we are allowed to call explicit constructors or |
| 3952 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3953 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3954 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3955 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3956 | // The type we're converting to is a class type. Enumerate its constructors |
| 3957 | // to see if there is a suitable conversion. |
| 3958 | CXXRecordDecl *DestRecordDecl |
| 3959 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3960 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3961 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3962 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3963 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 3964 | // The container holding the constructors can under certain conditions |
| 3965 | // be changed while iterating. To be safe we copy the lookup results |
| 3966 | // to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3967 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 3968 | for (SmallVector<NamedDecl*, 8>::iterator |
| 3969 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3970 | Con != ConEnd; ++Con) { |
| 3971 | NamedDecl *D = *Con; |
| 3972 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3973 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3974 | // Find the constructor (which may be a template). |
| 3975 | CXXConstructorDecl *Constructor = 0; |
| 3976 | FunctionTemplateDecl *ConstructorTmpl |
| 3977 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3978 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3979 | Constructor = cast<CXXConstructorDecl>( |
| 3980 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3981 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3982 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3983 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3984 | if (!Constructor->isInvalidDecl() && |
| 3985 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3986 | if (ConstructorTmpl) |
| 3987 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3988 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3989 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3990 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3991 | else |
| 3992 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3993 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3994 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3995 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3996 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3997 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3998 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3999 | |
| 4000 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4001 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4002 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4003 | // The type we're converting from is a class type, enumerate its conversion |
| 4004 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4005 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4006 | // We can only enumerate the conversion functions for a complete type; if |
| 4007 | // the type isn't complete, simply skip this step. |
| 4008 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 4009 | CXXRecordDecl *SourceRecordDecl |
| 4010 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4011 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4012 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4013 | CXXRecordDecl::conversion_iterator> |
| 4014 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 4015 | for (CXXRecordDecl::conversion_iterator |
| 4016 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4017 | NamedDecl *D = *I; |
| 4018 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4019 | if (isa<UsingShadowDecl>(D)) |
| 4020 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4021 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4022 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4023 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4024 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4025 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4026 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4027 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4028 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4029 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4030 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4031 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4032 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4033 | CandidateSet); |
| 4034 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4035 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4036 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4037 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4041 | |
| 4042 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4043 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4044 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4045 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4046 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4047 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4048 | Result); |
| 4049 | return; |
| 4050 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4051 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4052 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4053 | Function->setReferenced(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4054 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4055 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4056 | if (isa<CXXConstructorDecl>(Function)) { |
| 4057 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4058 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4059 | // cv-unqualified type of the destination. |
| 4060 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4061 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4062 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4063 | return; |
| 4064 | } |
| 4065 | |
| 4066 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4067 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4068 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4069 | // 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] | 4070 | // the resulting temporary object (possible to create an object of |
| 4071 | // a base class type). That copy is not a separate conversion, so |
| 4072 | // we just make a note of the actual destination type (possibly a |
| 4073 | // base class of the type returned by the conversion function) and |
| 4074 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4075 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 4076 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4077 | return; |
| 4078 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4079 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4080 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4081 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4082 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4083 | // If the conversion following the call to the conversion function |
| 4084 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4085 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4086 | Best->FinalConversion.Third) { |
| 4087 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4088 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4089 | ICS.Standard = Best->FinalConversion; |
| 4090 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 4091 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4092 | } |
| 4093 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4094 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4095 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4096 | |
| 4097 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4098 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4099 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4100 | // Skip parens. |
| 4101 | e = e->IgnoreParens(); |
| 4102 | |
| 4103 | // Skip address-of nodes. |
| 4104 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4105 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4106 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4107 | isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4108 | |
| 4109 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4110 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4111 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4112 | case CK_Dependent: |
| 4113 | case CK_BitCast: |
| 4114 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4115 | case CK_NoOp: |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4116 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4117 | |
| 4118 | case CK_ArrayToPointerDecay: |
| 4119 | return IIK_nonscalar; |
| 4120 | |
| 4121 | case CK_NullToPointer: |
| 4122 | return IIK_okay; |
| 4123 | |
| 4124 | default: |
| 4125 | break; |
| 4126 | } |
| 4127 | |
| 4128 | // 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] | 4129 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4130 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4131 | // load which requires a cleanup. |
| 4132 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4133 | isWeakAccess = true; |
| 4134 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4135 | if (!isAddressOf) return IIK_nonlocal; |
| 4136 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4137 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4138 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4139 | |
| 4140 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4141 | |
| 4142 | // If we have a conditional operator, check both sides. |
| 4143 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4144 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4145 | isWeakAccess)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4146 | return iik; |
| 4147 | |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4148 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4149 | |
| 4150 | // These are never scalar. |
| 4151 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4152 | return IIK_nonscalar; |
| 4153 | |
| 4154 | // Otherwise, it needs to be a null pointer constant. |
| 4155 | } else { |
| 4156 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4157 | ? IIK_okay : IIK_nonlocal); |
| 4158 | } |
| 4159 | |
| 4160 | return IIK_nonlocal; |
| 4161 | } |
| 4162 | |
| 4163 | /// Check whether the given expression is a valid operand for an |
| 4164 | /// indirect copy/restore. |
| 4165 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4166 | assert(src->isRValue()); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4167 | bool isWeakAccess = false; |
| 4168 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4169 | // If isWeakAccess to true, there will be an implicit |
| 4170 | // load which requires a cleanup. |
| 4171 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4172 | S.ExprNeedsCleanups = true; |
| 4173 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4174 | if (iik == IIK_okay) return; |
| 4175 | |
| 4176 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4177 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4178 | << src->getSourceRange(); |
| 4179 | } |
| 4180 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4181 | /// \brief Determine whether we have compatible array types for the |
| 4182 | /// purposes of GNU by-copy array initialization. |
| 4183 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 4184 | const ArrayType *Dest, |
| 4185 | const ArrayType *Source) { |
| 4186 | // If the source and destination array types are equivalent, we're |
| 4187 | // done. |
| 4188 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4189 | return true; |
| 4190 | |
| 4191 | // Make sure that the element types are the same. |
| 4192 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4193 | return false; |
| 4194 | |
| 4195 | // The only mismatch we allow is when the destination is an |
| 4196 | // incomplete array type and the source is a constant array type. |
| 4197 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4198 | } |
| 4199 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4200 | static bool tryObjCWritebackConversion(Sema &S, |
| 4201 | InitializationSequence &Sequence, |
| 4202 | const InitializedEntity &Entity, |
| 4203 | Expr *Initializer) { |
| 4204 | bool ArrayDecay = false; |
| 4205 | QualType ArgType = Initializer->getType(); |
| 4206 | QualType ArgPointee; |
| 4207 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4208 | ArrayDecay = true; |
| 4209 | ArgPointee = ArgArrayType->getElementType(); |
| 4210 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4211 | } |
| 4212 | |
| 4213 | // Handle write-back conversion. |
| 4214 | QualType ConvertedArgType; |
| 4215 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4216 | ConvertedArgType)) |
| 4217 | return false; |
| 4218 | |
| 4219 | // We should copy unless we're passing to an argument explicitly |
| 4220 | // marked 'out'. |
| 4221 | bool ShouldCopy = true; |
| 4222 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4223 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4224 | |
| 4225 | // Do we need an lvalue conversion? |
| 4226 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4227 | ImplicitConversionSequence ICS; |
| 4228 | ICS.setStandard(); |
| 4229 | ICS.Standard.setAsIdentityConversion(); |
| 4230 | |
| 4231 | QualType ResultType; |
| 4232 | if (ArrayDecay) { |
| 4233 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4234 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4235 | } else { |
| 4236 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4237 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4238 | } |
| 4239 | |
| 4240 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4241 | } |
| 4242 | |
| 4243 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4244 | return true; |
| 4245 | } |
| 4246 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4247 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4248 | InitializationSequence &Sequence, |
| 4249 | QualType DestType, |
| 4250 | Expr *Initializer) { |
| 4251 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4252 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4253 | return false; |
| 4254 | |
| 4255 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4256 | return true; |
| 4257 | } |
| 4258 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4259 | // |
| 4260 | // OpenCL 1.2 spec, s6.12.10 |
| 4261 | // |
| 4262 | // The event argument can also be used to associate the |
| 4263 | // async_work_group_copy with a previous async copy allowing |
| 4264 | // an event to be shared by multiple async copies; otherwise |
| 4265 | // event should be zero. |
| 4266 | // |
| 4267 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4268 | InitializationSequence &Sequence, |
| 4269 | QualType DestType, |
| 4270 | Expr *Initializer) { |
| 4271 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4272 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4273 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4274 | return false; |
| 4275 | |
| 4276 | Sequence.AddOCLZeroEventStep(DestType); |
| 4277 | return true; |
| 4278 | } |
| 4279 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4280 | InitializationSequence::InitializationSequence(Sema &S, |
| 4281 | const InitializedEntity &Entity, |
| 4282 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4283 | MultiExprArg Args) |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4284 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4285 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4286 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4287 | // Eliminate non-overload placeholder types in the arguments. We |
| 4288 | // need to do this before checking whether types are dependent |
| 4289 | // because lowering a pseudo-object expression might well give us |
| 4290 | // something of dependent type. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4291 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4292 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4293 | // FIXME: should we be doing this here? |
| 4294 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4295 | if (result.isInvalid()) { |
| 4296 | SetFailed(FK_PlaceholderType); |
| 4297 | return; |
| 4298 | } |
| 4299 | Args[I] = result.take(); |
| 4300 | } |
| 4301 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4302 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4303 | // The semantics of initializers are as follows. The destination type is |
| 4304 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4305 | // 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] | 4306 | // 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] | 4307 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4308 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4309 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4310 | if (DestType->isDependentType() || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4311 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4312 | SequenceKind = DependentSequence; |
| 4313 | return; |
| 4314 | } |
| 4315 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4316 | // Almost everything is a normal sequence. |
| 4317 | setSequenceKind(NormalSequence); |
| 4318 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4319 | QualType SourceType; |
| 4320 | Expr *Initializer = 0; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4321 | if (Args.size() == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4322 | Initializer = Args[0]; |
| 4323 | if (!isa<InitListExpr>(Initializer)) |
| 4324 | SourceType = Initializer->getType(); |
| 4325 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4326 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4327 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4328 | // object is list-initialized (8.5.4). |
| 4329 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4330 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4331 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4332 | return; |
| 4333 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4334 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4335 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4336 | // - If the destination type is a reference type, see 8.5.3. |
| 4337 | if (DestType->isReferenceType()) { |
| 4338 | // C++0x [dcl.init.ref]p1: |
| 4339 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4340 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4341 | // by an object that can be converted into a T. |
| 4342 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4343 | if (Args.size() != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4344 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4345 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4346 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4347 | return; |
| 4348 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4350 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4351 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4352 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4353 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4354 | return; |
| 4355 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4356 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4357 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4358 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4359 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4360 | return; |
| 4361 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4362 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4363 | // - If the destination type is an array of characters, an array of |
| 4364 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4365 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4366 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4367 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4368 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4369 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4370 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4371 | return; |
| 4372 | } |
| 4373 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4374 | if (Initializer) { |
| 4375 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 4376 | case SIF_None: |
| 4377 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 4378 | return; |
| 4379 | case SIF_NarrowStringIntoWideChar: |
| 4380 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 4381 | return; |
| 4382 | case SIF_WideStringIntoChar: |
| 4383 | SetFailed(FK_WideStringIntoCharArray); |
| 4384 | return; |
| 4385 | case SIF_IncompatWideStringIntoWideChar: |
| 4386 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 4387 | return; |
| 4388 | case SIF_Other: |
| 4389 | break; |
| 4390 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4391 | } |
| 4392 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4393 | // Note: as an GNU C extension, we allow initialization of an |
| 4394 | // array from a compound literal that creates an array of the same |
| 4395 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4396 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4397 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4398 | Initializer->getType()->isArrayType()) { |
| 4399 | const ArrayType *SourceAT |
| 4400 | = Context.getAsArrayType(Initializer->getType()); |
| 4401 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4402 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4403 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4404 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4405 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4406 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4407 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4408 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4409 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4410 | // class member of array type from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4411 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4412 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4413 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4414 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4415 | *this); |
| 4416 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4417 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4418 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4419 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 4420 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4421 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4422 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4423 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4424 | return; |
| 4425 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4426 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4427 | // Determine whether we should consider writeback conversions for |
| 4428 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4429 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4430 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 4431 | |
| 4432 | // We're at the end of the line for C: it's either a write-back conversion |
| 4433 | // 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] | 4434 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4435 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4436 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4437 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4438 | return; |
| 4439 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4440 | |
| 4441 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4442 | return; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4443 | |
| 4444 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4445 | return; |
| 4446 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4447 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4448 | AddCAssignmentStep(DestType); |
| 4449 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4450 | return; |
| 4451 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4453 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4454 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4455 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4456 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4457 | // - If the initialization is direct-initialization, or if it is |
| 4458 | // copy-initialization where the cv-unqualified version of the |
| 4459 | // 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] | 4460 | // class of the destination, constructors are considered. [...] |
| 4461 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4462 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4463 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4464 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4465 | TryConstructorInitialization(S, Entity, Kind, Args, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4466 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4467 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4468 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4469 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4470 | // used) to a derived class thereof are enumerated as described in |
| 4471 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4472 | // (13.3). |
| 4473 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4474 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4475 | return; |
| 4476 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4477 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4478 | if (Args.size() > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4479 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4480 | return; |
| 4481 | } |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4482 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4483 | |
| 4484 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4485 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4486 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4487 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 4488 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4489 | return; |
| 4490 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4491 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4492 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4493 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4494 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4495 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4496 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4497 | |
| 4498 | ImplicitConversionSequence ICS |
| 4499 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4500 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4501 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4502 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4503 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4504 | allowObjCWritebackConversion); |
| 4505 | |
| 4506 | if (ICS.isStandard() && |
| 4507 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4508 | // Objective-C ARC writeback conversion. |
| 4509 | |
| 4510 | // We should copy unless we're passing to an argument explicitly |
| 4511 | // marked 'out'. |
| 4512 | bool ShouldCopy = true; |
| 4513 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4514 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4515 | |
| 4516 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4517 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4518 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4519 | ImplicitConversionSequence LvalueICS; |
| 4520 | LvalueICS.setStandard(); |
| 4521 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4522 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4523 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4524 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4525 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4526 | |
| 4527 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4528 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4529 | DeclAccessPair dap; |
| 4530 | if (Initializer->getType() == Context.OverloadTy && |
| 4531 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 4532 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4533 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4534 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4535 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4536 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4537 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4538 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4539 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4540 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
| 4543 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4544 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4545 | StepEnd = Steps.end(); |
| 4546 | Step != StepEnd; ++Step) |
| 4547 | Step->Destroy(); |
| 4548 | } |
| 4549 | |
| 4550 | //===----------------------------------------------------------------------===// |
| 4551 | // Perform initialization |
| 4552 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4553 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4554 | getAssignmentAction(const InitializedEntity &Entity) { |
| 4555 | switch(Entity.getKind()) { |
| 4556 | case InitializedEntity::EK_Variable: |
| 4557 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4558 | case InitializedEntity::EK_Exception: |
| 4559 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4560 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4561 | return Sema::AA_Initializing; |
| 4562 | |
| 4563 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4564 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4565 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4566 | return Sema::AA_Sending; |
| 4567 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4568 | return Sema::AA_Passing; |
| 4569 | |
| 4570 | case InitializedEntity::EK_Result: |
| 4571 | return Sema::AA_Returning; |
| 4572 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4573 | case InitializedEntity::EK_Temporary: |
| 4574 | // FIXME: Can we tell apart casting vs. converting? |
| 4575 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4576 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4577 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4578 | case InitializedEntity::EK_ArrayElement: |
| 4579 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4580 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4581 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4582 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4583 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4584 | return Sema::AA_Initializing; |
| 4585 | } |
| 4586 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4587 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4590 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4591 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4592 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4593 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4594 | case InitializedEntity::EK_ArrayElement: |
| 4595 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4596 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4597 | case InitializedEntity::EK_New: |
| 4598 | case InitializedEntity::EK_Variable: |
| 4599 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4600 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4601 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4602 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4603 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4604 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4605 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4606 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4607 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4608 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4609 | case InitializedEntity::EK_Parameter: |
| 4610 | case InitializedEntity::EK_Temporary: |
| 4611 | return true; |
| 4612 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4614 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4615 | } |
| 4616 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4617 | /// \brief Whether the given entity, when initialized with an object |
| 4618 | /// created for that initialization, requires destruction. |
| 4619 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4620 | switch (Entity.getKind()) { |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4621 | case InitializedEntity::EK_Result: |
| 4622 | case InitializedEntity::EK_New: |
| 4623 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4624 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4625 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4626 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4627 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4628 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4629 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4630 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4631 | case InitializedEntity::EK_Member: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4632 | case InitializedEntity::EK_Variable: |
| 4633 | case InitializedEntity::EK_Parameter: |
| 4634 | case InitializedEntity::EK_Temporary: |
| 4635 | case InitializedEntity::EK_ArrayElement: |
| 4636 | case InitializedEntity::EK_Exception: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4637 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4638 | return true; |
| 4639 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4640 | |
| 4641 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4642 | } |
| 4643 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4644 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4645 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4646 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4647 | OverloadCandidateSet &CandidateSet, |
| 4648 | CXXRecordDecl *Class, |
| 4649 | Expr *CurInitExpr) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4650 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4651 | // The container holding the constructors can under certain conditions |
| 4652 | // be changed while iterating (e.g. because of deserialization). |
| 4653 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4654 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4655 | for (SmallVector<NamedDecl*, 16>::iterator |
| 4656 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4657 | NamedDecl *D = *CI; |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4658 | CXXConstructorDecl *Constructor = 0; |
| 4659 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4660 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4661 | // Handle copy/moveconstructors, only. |
| 4662 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4663 | !Constructor->isCopyOrMoveConstructor() || |
| 4664 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4665 | continue; |
| 4666 | |
| 4667 | DeclAccessPair FoundDecl |
| 4668 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4669 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4670 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4671 | continue; |
| 4672 | } |
| 4673 | |
| 4674 | // Handle constructor templates. |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4675 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4676 | if (ConstructorTmpl->isInvalidDecl()) |
| 4677 | continue; |
| 4678 | |
| 4679 | Constructor = cast<CXXConstructorDecl>( |
| 4680 | ConstructorTmpl->getTemplatedDecl()); |
| 4681 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4682 | continue; |
| 4683 | |
| 4684 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4685 | // candidates? |
| 4686 | DeclAccessPair FoundDecl |
| 4687 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4688 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4689 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4690 | } |
| 4691 | } |
| 4692 | |
| 4693 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4694 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4695 | Expr *Initializer) { |
| 4696 | switch (Entity.getKind()) { |
| 4697 | case InitializedEntity::EK_Result: |
| 4698 | return Entity.getReturnLoc(); |
| 4699 | |
| 4700 | case InitializedEntity::EK_Exception: |
| 4701 | return Entity.getThrowLoc(); |
| 4702 | |
| 4703 | case InitializedEntity::EK_Variable: |
| 4704 | return Entity.getDecl()->getLocation(); |
| 4705 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4706 | case InitializedEntity::EK_LambdaCapture: |
| 4707 | return Entity.getCaptureLoc(); |
| 4708 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4709 | case InitializedEntity::EK_ArrayElement: |
| 4710 | case InitializedEntity::EK_Member: |
| 4711 | case InitializedEntity::EK_Parameter: |
| 4712 | case InitializedEntity::EK_Temporary: |
| 4713 | case InitializedEntity::EK_New: |
| 4714 | case InitializedEntity::EK_Base: |
| 4715 | case InitializedEntity::EK_Delegating: |
| 4716 | case InitializedEntity::EK_VectorElement: |
| 4717 | case InitializedEntity::EK_ComplexElement: |
| 4718 | case InitializedEntity::EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4719 | case InitializedEntity::EK_CompoundLiteralInit: |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4720 | return Initializer->getLocStart(); |
| 4721 | } |
| 4722 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4723 | } |
| 4724 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4725 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4726 | /// provided by the given initializer by calling the appropriate copy |
| 4727 | /// constructor. |
| 4728 | /// |
| 4729 | /// \param S The Sema object used for type-checking. |
| 4730 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4731 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4732 | /// the type of the initializer expression or a superclass thereof. |
| 4733 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4734 | /// \param Entity The entity being initialized. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4735 | /// |
| 4736 | /// \param CurInit The initializer expression. |
| 4737 | /// |
| 4738 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4739 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4740 | /// an rvalue. |
| 4741 | /// |
| 4742 | /// \returns An expression that copies the initializer expression into |
| 4743 | /// a temporary object, or an error expression if a copy could not be |
| 4744 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4745 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4746 | QualType T, |
| 4747 | const InitializedEntity &Entity, |
| 4748 | ExprResult CurInit, |
| 4749 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4750 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4751 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4752 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4753 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4754 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4755 | if (!Class) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4756 | return CurInit; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4757 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4758 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4759 | // When certain criteria are met, an implementation is allowed to |
| 4760 | // omit the copy/move construction of a class object, even if the |
| 4761 | // copy/move constructor and/or destructor for the object have |
| 4762 | // side effects. [...] |
| 4763 | // - when a temporary class object that has not been bound to a |
| 4764 | // reference (12.2) would be copied/moved to a class object |
| 4765 | // with the same cv-unqualified type, the copy/move operation |
| 4766 | // can be omitted by constructing the temporary object |
| 4767 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4769 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4770 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4771 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4772 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4773 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4774 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4775 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4776 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4777 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4778 | return CurInit; |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4779 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4780 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4781 | // Only consider constructors and constructor templates. Per |
| 4782 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4783 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4784 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4785 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4786 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4787 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4788 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4789 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4790 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4791 | case OR_Success: |
| 4792 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4793 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4794 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4795 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4796 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4797 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4798 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4799 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4800 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4801 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4802 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4803 | return CurInit; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4804 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4805 | case OR_Ambiguous: |
| 4806 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4807 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4808 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4809 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4810 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4811 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4812 | case OR_Deleted: |
| 4813 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4814 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4815 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4816 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4817 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4818 | } |
| 4819 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4820 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4821 | SmallVector<Expr*, 8> ConstructorArgs; |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4822 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4823 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4824 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4825 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4826 | |
| 4827 | if (IsExtraneousCopy) { |
| 4828 | // If this is a totally extraneous copy for C++03 reference |
| 4829 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4830 | // expression. We don't generate an (elided) copy operation here |
| 4831 | // because doing so would require us to pass down a flag to avoid |
| 4832 | // infinite recursion, where each step adds another extraneous, |
| 4833 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4834 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4835 | // Instantiate the default arguments of any extra parameters in |
| 4836 | // the selected copy constructor, as if we were going to create a |
| 4837 | // proper call to the copy constructor. |
| 4838 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4839 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4840 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4841 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4842 | break; |
| 4843 | |
| 4844 | // Build the default argument expression; we don't actually care |
| 4845 | // if this succeeds or not, because this routine will complain |
| 4846 | // if there was a problem. |
| 4847 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4848 | } |
| 4849 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4850 | return S.Owned(CurInitExpr); |
| 4851 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4852 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4853 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4854 | // constructor call (we might have derived-to-base conversions, or |
| 4855 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4856 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4857 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4858 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4859 | // Actually perform the constructor call. |
| 4860 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4861 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4862 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4863 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4864 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4865 | CXXConstructExpr::CK_Complete, |
| 4866 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4867 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4868 | // If we're supposed to bind temporaries, do so. |
| 4869 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4870 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4871 | return CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4872 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4873 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4874 | /// \brief Check whether elidable copy construction for binding a reference to |
| 4875 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 4876 | /// -Wc++98-compat. |
| 4877 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4878 | const InitializedEntity &Entity, |
| 4879 | Expr *CurInitExpr) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4880 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4881 | |
| 4882 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 4883 | if (!Record) |
| 4884 | return; |
| 4885 | |
| 4886 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 4887 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 4888 | == DiagnosticsEngine::Ignored) |
| 4889 | return; |
| 4890 | |
| 4891 | // Find constructors which would have been considered. |
| 4892 | OverloadCandidateSet CandidateSet(Loc); |
| 4893 | LookupCopyAndMoveConstructors( |
| 4894 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 4895 | |
| 4896 | // Perform overload resolution. |
| 4897 | OverloadCandidateSet::iterator Best; |
| 4898 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 4899 | |
| 4900 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 4901 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 4902 | << CurInitExpr->getSourceRange(); |
| 4903 | |
| 4904 | switch (OR) { |
| 4905 | case OR_Success: |
| 4906 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4907 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4908 | // FIXME: Check default arguments as far as that's possible. |
| 4909 | break; |
| 4910 | |
| 4911 | case OR_No_Viable_Function: |
| 4912 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4913 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4914 | break; |
| 4915 | |
| 4916 | case OR_Ambiguous: |
| 4917 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4918 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4919 | break; |
| 4920 | |
| 4921 | case OR_Deleted: |
| 4922 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4923 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4924 | break; |
| 4925 | } |
| 4926 | } |
| 4927 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4928 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4929 | const InitializedEntity &Entity) { |
| 4930 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4931 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4932 | return; |
| 4933 | |
| 4934 | if (Entity.getDecl()->getDeclName()) |
| 4935 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4936 | << Entity.getDecl()->getDeclName(); |
| 4937 | else |
| 4938 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4939 | } |
| 4940 | } |
| 4941 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4942 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4943 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4944 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4945 | } |
| 4946 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4947 | /// Returns true if the parameters describe a constructor initialization of |
| 4948 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 4949 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 4950 | const InitializationKind &Kind, |
| 4951 | unsigned NumArgs) { |
| 4952 | switch (Entity.getKind()) { |
| 4953 | case InitializedEntity::EK_Temporary: |
| 4954 | case InitializedEntity::EK_CompoundLiteralInit: |
| 4955 | break; |
| 4956 | default: |
| 4957 | return false; |
| 4958 | } |
| 4959 | |
| 4960 | switch (Kind.getKind()) { |
| 4961 | case InitializationKind::IK_DirectList: |
| 4962 | return true; |
| 4963 | // FIXME: Hack to work around cast weirdness. |
| 4964 | case InitializationKind::IK_Direct: |
| 4965 | case InitializationKind::IK_Value: |
| 4966 | return NumArgs != 1; |
| 4967 | default: |
| 4968 | return false; |
| 4969 | } |
| 4970 | } |
| 4971 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4972 | static ExprResult |
| 4973 | PerformConstructorInitialization(Sema &S, |
| 4974 | const InitializedEntity &Entity, |
| 4975 | const InitializationKind &Kind, |
| 4976 | MultiExprArg Args, |
| 4977 | const InitializationSequence::Step& Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4978 | bool &ConstructorInitRequiresZeroInit, |
| 4979 | bool IsListInitialization) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4980 | unsigned NumArgs = Args.size(); |
| 4981 | CXXConstructorDecl *Constructor |
| 4982 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 4983 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 4984 | |
| 4985 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4986 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4987 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4988 | ? Kind.getEqualLoc() |
| 4989 | : Kind.getLocation(); |
| 4990 | |
| 4991 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4992 | // Force even a trivial, implicit default constructor to be |
| 4993 | // semantically checked. We do this explicitly because we don't build |
| 4994 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 4995 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4996 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 4997 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4998 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4999 | } |
| 5000 | |
| 5001 | ExprResult CurInit = S.Owned((Expr *)0); |
| 5002 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5003 | // C++ [over.match.copy]p1: |
| 5004 | // - When initializing a temporary to be bound to the first parameter |
| 5005 | // of a constructor that takes a reference to possibly cv-qualified |
| 5006 | // T as its first argument, called with a single argument in the |
| 5007 | // context of direct-initialization, explicit conversion functions |
| 5008 | // are also considered. |
| 5009 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 5010 | Args.size() == 1 && |
| 5011 | Constructor->isCopyOrMoveConstructor(); |
| 5012 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5013 | // Determine the arguments required to actually perform the constructor |
| 5014 | // call. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5015 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5016 | Loc, ConstructorArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5017 | AllowExplicitConv, |
| 5018 | IsListInitialization)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5019 | return ExprError(); |
| 5020 | |
| 5021 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5022 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5023 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5024 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5025 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 5026 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5027 | |
| 5028 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5029 | if (!TSInfo) |
| 5030 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 5031 | SourceRange ParenRange; |
| 5032 | if (Kind.getKind() != InitializationKind::IK_DirectList) |
| 5033 | ParenRange = Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5034 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5035 | CurInit = S.Owned( |
| 5036 | new (S.Context) CXXTemporaryObjectExpr(S.Context, Constructor, |
| 5037 | TSInfo, ConstructorArgs, |
| 5038 | ParenRange, IsListInitialization, |
| 5039 | HadMultipleCandidates, |
| 5040 | ConstructorInitRequiresZeroInit)); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5041 | } else { |
| 5042 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 5043 | CXXConstructExpr::CK_Complete; |
| 5044 | |
| 5045 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5046 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 5047 | CXXConstructExpr::CK_VirtualBase : |
| 5048 | CXXConstructExpr::CK_NonVirtualBase; |
| 5049 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 5050 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 5051 | } |
| 5052 | |
| 5053 | // Only get the parenthesis range if it is a direct construction. |
| 5054 | SourceRange parenRange = |
| 5055 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 5056 | Kind.getParenRange() : SourceRange(); |
| 5057 | |
| 5058 | // If the entity allows NRVO, mark the construction as elidable |
| 5059 | // unconditionally. |
| 5060 | if (Entity.allowsNRVO()) |
| 5061 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5062 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5063 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5064 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5065 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5066 | ConstructorInitRequiresZeroInit, |
| 5067 | ConstructKind, |
| 5068 | parenRange); |
| 5069 | else |
| 5070 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5071 | Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5072 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5073 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5074 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5075 | ConstructorInitRequiresZeroInit, |
| 5076 | ConstructKind, |
| 5077 | parenRange); |
| 5078 | } |
| 5079 | if (CurInit.isInvalid()) |
| 5080 | return ExprError(); |
| 5081 | |
| 5082 | // Only check access if all of that succeeded. |
| 5083 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 5084 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5085 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 5086 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5087 | |
| 5088 | if (shouldBindAsTemporary(Entity)) |
| 5089 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 5090 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5091 | return CurInit; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5092 | } |
| 5093 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5094 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 5095 | /// longer than the current full-expression. Conservatively returns false if |
| 5096 | /// it's unclear. |
| 5097 | static bool |
| 5098 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 5099 | const InitializedEntity *Top = &Entity; |
| 5100 | while (Top->getParent()) |
| 5101 | Top = Top->getParent(); |
| 5102 | |
| 5103 | switch (Top->getKind()) { |
| 5104 | case InitializedEntity::EK_Variable: |
| 5105 | case InitializedEntity::EK_Result: |
| 5106 | case InitializedEntity::EK_Exception: |
| 5107 | case InitializedEntity::EK_Member: |
| 5108 | case InitializedEntity::EK_New: |
| 5109 | case InitializedEntity::EK_Base: |
| 5110 | case InitializedEntity::EK_Delegating: |
| 5111 | return true; |
| 5112 | |
| 5113 | case InitializedEntity::EK_ArrayElement: |
| 5114 | case InitializedEntity::EK_VectorElement: |
| 5115 | case InitializedEntity::EK_BlockElement: |
| 5116 | case InitializedEntity::EK_ComplexElement: |
| 5117 | // Could not determine what the full initialization is. Assume it might not |
| 5118 | // outlive the full-expression. |
| 5119 | return false; |
| 5120 | |
| 5121 | case InitializedEntity::EK_Parameter: |
| 5122 | case InitializedEntity::EK_Temporary: |
| 5123 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5124 | case InitializedEntity::EK_CompoundLiteralInit: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5125 | // The entity being initialized might not outlive the full-expression. |
| 5126 | return false; |
| 5127 | } |
| 5128 | |
| 5129 | llvm_unreachable("unknown entity kind"); |
| 5130 | } |
| 5131 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5132 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 5133 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 5134 | /// the initialization of \p Entity. |
| 5135 | static const ValueDecl * |
| 5136 | getDeclForTemporaryLifetimeExtension(const InitializedEntity &Entity, |
| 5137 | const ValueDecl *FallbackDecl = 0) { |
| 5138 | // C++11 [class.temporary]p5: |
| 5139 | switch (Entity.getKind()) { |
| 5140 | case InitializedEntity::EK_Variable: |
| 5141 | // The temporary [...] persists for the lifetime of the reference |
| 5142 | return Entity.getDecl(); |
| 5143 | |
| 5144 | case InitializedEntity::EK_Member: |
| 5145 | // For subobjects, we look at the complete object. |
| 5146 | if (Entity.getParent()) |
| 5147 | return getDeclForTemporaryLifetimeExtension(*Entity.getParent(), |
| 5148 | Entity.getDecl()); |
| 5149 | |
| 5150 | // except: |
| 5151 | // -- A temporary bound to a reference member in a constructor's |
| 5152 | // ctor-initializer persists until the constructor exits. |
| 5153 | return Entity.getDecl(); |
| 5154 | |
| 5155 | case InitializedEntity::EK_Parameter: |
| 5156 | // -- A temporary bound to a reference parameter in a function call |
| 5157 | // persists until the completion of the full-expression containing |
| 5158 | // the call. |
| 5159 | case InitializedEntity::EK_Result: |
| 5160 | // -- The lifetime of a temporary bound to the returned value in a |
| 5161 | // function return statement is not extended; the temporary is |
| 5162 | // destroyed at the end of the full-expression in the return statement. |
| 5163 | case InitializedEntity::EK_New: |
| 5164 | // -- A temporary bound to a reference in a new-initializer persists |
| 5165 | // until the completion of the full-expression containing the |
| 5166 | // new-initializer. |
| 5167 | return 0; |
| 5168 | |
| 5169 | case InitializedEntity::EK_Temporary: |
| 5170 | case InitializedEntity::EK_CompoundLiteralInit: |
| 5171 | // We don't yet know the storage duration of the surrounding temporary. |
| 5172 | // Assume it's got full-expression duration for now, it will patch up our |
| 5173 | // storage duration if that's not correct. |
| 5174 | return 0; |
| 5175 | |
| 5176 | case InitializedEntity::EK_ArrayElement: |
| 5177 | // For subobjects, we look at the complete object. |
| 5178 | return getDeclForTemporaryLifetimeExtension(*Entity.getParent(), |
| 5179 | FallbackDecl); |
| 5180 | |
| 5181 | case InitializedEntity::EK_Base: |
| 5182 | case InitializedEntity::EK_Delegating: |
| 5183 | // We can reach this case for aggregate initialization in a constructor: |
| 5184 | // struct A { int &&r; }; |
| 5185 | // struct B : A { B() : A{0} {} }; |
| 5186 | // In this case, use the innermost field decl as the context. |
| 5187 | return FallbackDecl; |
| 5188 | |
| 5189 | case InitializedEntity::EK_BlockElement: |
| 5190 | case InitializedEntity::EK_LambdaCapture: |
| 5191 | case InitializedEntity::EK_Exception: |
| 5192 | case InitializedEntity::EK_VectorElement: |
| 5193 | case InitializedEntity::EK_ComplexElement: |
| 5194 | llvm_unreachable("should not materialize a temporary to initialize this"); |
| 5195 | } |
Benjamin Kramer | 6f773e8 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 5196 | llvm_unreachable("unknown entity kind"); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5197 | } |
| 5198 | |
| 5199 | static void performLifetimeExtension(Expr *Init, const ValueDecl *ExtendingD); |
| 5200 | |
| 5201 | /// Update a glvalue expression that is used as the initializer of a reference |
| 5202 | /// to note that its lifetime is extended. |
| 5203 | static void performReferenceExtension(Expr *Init, const ValueDecl *ExtendingD) { |
| 5204 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 5205 | if (ILE->getNumInits() == 1 && ILE->isGLValue()) { |
| 5206 | // This is just redundant braces around an initializer. Step over it. |
| 5207 | Init = ILE->getInit(0); |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | if (MaterializeTemporaryExpr *ME = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 5212 | // Update the storage duration of the materialized temporary. |
| 5213 | // FIXME: Rebuild the expression instead of mutating it. |
| 5214 | ME->setExtendingDecl(ExtendingD); |
| 5215 | performLifetimeExtension(ME->GetTemporaryExpr(), ExtendingD); |
| 5216 | } |
| 5217 | } |
| 5218 | |
| 5219 | /// Update a prvalue expression that is going to be materialized as a |
| 5220 | /// lifetime-extended temporary. |
| 5221 | static void performLifetimeExtension(Expr *Init, const ValueDecl *ExtendingD) { |
| 5222 | // Dig out the expression which constructs the extended temporary. |
| 5223 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5224 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5225 | Init = const_cast<Expr *>( |
| 5226 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5227 | |
| 5228 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 5229 | if (ILE->initializesStdInitializerList()) { |
| 5230 | // FIXME: If this is an InitListExpr which creates a std::initializer_list |
| 5231 | // object, we also need to lifetime-extend the underlying array |
| 5232 | // itself. Fix the representation to explicitly materialize an |
| 5233 | // array temporary so we can model this properly. |
| 5234 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
| 5235 | performLifetimeExtension(ILE->getInit(I), ExtendingD); |
| 5236 | return; |
| 5237 | } |
| 5238 | |
| 5239 | CXXRecordDecl *RD = Init->getType()->getAsCXXRecordDecl(); |
| 5240 | if (RD) { |
| 5241 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 5242 | |
| 5243 | // If we lifetime-extend a braced initializer which is initializing an |
| 5244 | // aggregate, and that aggregate contains reference members which are |
| 5245 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 5246 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 5247 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
| 5248 | performReferenceExtension(ILE->getInit(0), ExtendingD); |
| 5249 | else { |
| 5250 | unsigned Index = 0; |
| 5251 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 5252 | E = RD->field_end(); |
| 5253 | I != E; ++I) { |
| 5254 | if (I->isUnnamedBitfield()) |
| 5255 | continue; |
| 5256 | if (I->getType()->isReferenceType()) |
| 5257 | performReferenceExtension(ILE->getInit(Index), ExtendingD); |
| 5258 | else if (isa<InitListExpr>(ILE->getInit(Index))) |
| 5259 | // This may be either aggregate-initialization of a member or |
| 5260 | // initialization of a std::initializer_list object. Either way, |
| 5261 | // we should recursively lifetime-extend that initializer. |
| 5262 | performLifetimeExtension(ILE->getInit(Index), ExtendingD); |
| 5263 | ++Index; |
| 5264 | } |
| 5265 | } |
| 5266 | } |
| 5267 | } |
| 5268 | } |
| 5269 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5270 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5271 | InitializationSequence::Perform(Sema &S, |
| 5272 | const InitializedEntity &Entity, |
| 5273 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5274 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5275 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5276 | if (Failed()) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5277 | Diagnose(S, Entity, Kind, Args); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5278 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5279 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5280 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5281 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5282 | // If the declaration is a non-dependent, incomplete array type |
| 5283 | // that has an initializer, then its type will be completed once |
| 5284 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5285 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5286 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5287 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5288 | if (const IncompleteArrayType *ArrayT |
| 5289 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5290 | // FIXME: We don't currently have the ability to accurately |
| 5291 | // compute the length of an initializer list without |
| 5292 | // performing full type-checking of the initializer list |
| 5293 | // (since we have to determine where braces are implicitly |
| 5294 | // introduced and such). So, we fall back to making the array |
| 5295 | // type a dependently-sized array type with no specified |
| 5296 | // bound. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5297 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5298 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5299 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5300 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5301 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5302 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5303 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5304 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5305 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5306 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5307 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | *ResultType |
| 5311 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 5312 | /*NumElts=*/0, |
| 5313 | ArrayT->getSizeModifier(), |
| 5314 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5315 | Brackets); |
| 5316 | } |
| 5317 | |
| 5318 | } |
| 5319 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5320 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5321 | !Kind.isExplicitCast()) { |
| 5322 | // Rebuild the ParenListExpr. |
| 5323 | SourceRange ParenRange = Kind.getParenRange(); |
| 5324 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5325 | Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5326 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5327 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5328 | Kind.isExplicitCast() || |
| 5329 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5330 | return ExprResult(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5333 | // No steps means no initialization. |
| 5334 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5335 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5336 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5337 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5338 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5339 | Entity.getKind() != InitializedEntity::EK_Parameter) { |
| 5340 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5341 | // from an initializer list. For parameters, we produce a better warning |
| 5342 | // elsewhere. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5343 | Expr *Init = Args[0]; |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5344 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5345 | << Init->getSourceRange(); |
| 5346 | } |
| 5347 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5348 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5349 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5350 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5351 | Entity.getType()->isPointerType() && |
| 5352 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5353 | Expr *Init = Args[0]; |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5354 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5355 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5356 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5357 | << Init->getSourceRange(); |
| 5358 | } |
| 5359 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5360 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5361 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5362 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5363 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5364 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5365 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5366 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5367 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5368 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5369 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5370 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5371 | // grab the only argument out the Args and place it into the "current" |
| 5372 | // initializer. |
| 5373 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5374 | case SK_ResolveAddressOfOverloadedFunction: |
| 5375 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5376 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5377 | case SK_CastDerivedToBaseLValue: |
| 5378 | case SK_BindReference: |
| 5379 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5380 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5381 | case SK_UserConversion: |
| 5382 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5383 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5384 | case SK_QualificationConversionRValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5385 | case SK_LValueToRValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5386 | case SK_ConversionSequence: |
| 5387 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5388 | case SK_UnwrapInitList: |
| 5389 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5390 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5391 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5392 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5393 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5394 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5395 | case SK_PassByIndirectCopyRestore: |
| 5396 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5397 | case SK_ProduceObjCObject: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5398 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5399 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5400 | case SK_OCLZeroEvent: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5401 | assert(Args.size() == 1); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5402 | CurInit = Args[0]; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5403 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5404 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5405 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5406 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5407 | case SK_ConstructorInitialization: |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5408 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5409 | case SK_ZeroInitialization: |
| 5410 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5411 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5412 | |
| 5413 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5414 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5415 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5416 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5417 | Step != StepEnd; ++Step) { |
| 5418 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5419 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5420 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5421 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5422 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5423 | switch (Step->Kind) { |
| 5424 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5425 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5426 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5427 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5428 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5429 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5430 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5431 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5432 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5433 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5434 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5435 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5436 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5437 | case SK_CastDerivedToBaseLValue: { |
| 5438 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5439 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5440 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5441 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5442 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5443 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5444 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5445 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5446 | CurInit.get()->getLocStart(), |
| 5447 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5448 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5449 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5450 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5451 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5452 | QualType T = SourceType; |
| 5453 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5454 | T = Pointer->getPointeeType(); |
| 5455 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5456 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5457 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5458 | } |
| 5459 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5460 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5461 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5462 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5463 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5464 | VK_XValue : |
| 5465 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5466 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 5467 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5468 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5469 | CurInit.get(), |
| 5470 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5471 | break; |
| 5472 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5473 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5474 | case SK_BindReference: |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5475 | // References cannot bind to bit-fields (C++ [dcl.init.ref]p5). |
| 5476 | if (CurInit.get()->refersToBitField()) { |
| 5477 | // We don't necessarily have an unambiguous source bit-field. |
| 5478 | FieldDecl *BitField = CurInit.get()->getSourceBitField(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5479 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5480 | << Entity.getType().isVolatileQualified() |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5481 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 5482 | << (BitField != NULL) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5483 | << CurInit.get()->getSourceRange(); |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5484 | if (BitField) |
| 5485 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 5486 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5487 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5488 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5489 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5490 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5491 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5492 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5493 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5494 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5495 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5496 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5497 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5498 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5499 | // Reference binding does not have any corresponding ASTs. |
| 5500 | |
| 5501 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5502 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5503 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5504 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5505 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5506 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5507 | case SK_BindReferenceToTemporary: { |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5508 | // Make sure the "temporary" is actually an rvalue. |
| 5509 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5510 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5511 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5512 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5513 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5514 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5515 | // Maybe lifetime-extend the temporary's subobjects to match the |
| 5516 | // entity's lifetime. |
| 5517 | const ValueDecl *ExtendingDecl = |
| 5518 | getDeclForTemporaryLifetimeExtension(Entity); |
| 5519 | if (ExtendingDecl) |
| 5520 | performLifetimeExtension(CurInit.get(), ExtendingDecl); |
| 5521 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5522 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 5523 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5524 | Entity.getType().getNonReferenceType(), CurInit.get(), |
| 5525 | Entity.getType()->isLValueReferenceType(), ExtendingDecl); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5526 | |
| 5527 | // If we're binding to an Objective-C object that has lifetime, we |
| 5528 | // need cleanups. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5529 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5530 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 5531 | S.ExprNeedsCleanups = true; |
| 5532 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5533 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5534 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5535 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5536 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5537 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5538 | /*IsExtraneousCopy=*/true); |
| 5539 | break; |
| 5540 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5541 | case SK_UserConversion: { |
| 5542 | // We have a user-defined conversion that invokes either a constructor |
| 5543 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5544 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5545 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5546 | FunctionDecl *Fn = Step->Function.Function; |
| 5547 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5548 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5549 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5550 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5551 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5552 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5553 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5554 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5555 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5556 | // Determine the arguments required to actually perform the constructor |
| 5557 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5558 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5559 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5560 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5561 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5562 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5563 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5564 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5565 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5566 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5567 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5568 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5569 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5570 | CXXConstructExpr::CK_Complete, |
| 5571 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5572 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5573 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5574 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5575 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5576 | FoundFn.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5577 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5578 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5579 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5580 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5581 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5582 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5583 | S.IsDerivedFrom(SourceType, Class)) |
| 5584 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5585 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5586 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5587 | } else { |
| 5588 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5589 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5590 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5591 | FoundFn); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5592 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5593 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5594 | |
| 5595 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5596 | // derived-to-base conversion? I believe the answer is "no", because |
| 5597 | // 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] | 5598 | ExprResult CurInitExprRes = |
| 5599 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 5600 | FoundFn, Conversion); |
| 5601 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5602 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5603 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5604 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5605 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5606 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5607 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5608 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5609 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5610 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5611 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5612 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5613 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5614 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5615 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5616 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5617 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5618 | |
| 5619 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5620 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5621 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5622 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5623 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5624 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5625 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5626 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5627 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 5628 | return ExprError(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5629 | } |
| 5630 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5631 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5632 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5633 | CurInit.get()->getType(), |
| 5634 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5635 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5636 | if (MaybeBindToTemp) |
| 5637 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5638 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5639 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5640 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5641 | break; |
| 5642 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5643 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5644 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5645 | case SK_QualificationConversionXValue: |
| 5646 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5647 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5648 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5649 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5650 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5651 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5652 | VK_XValue : |
| 5653 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5654 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5655 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5656 | } |
| 5657 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5658 | case SK_LValueToRValue: { |
| 5659 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
| 5660 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 5661 | CK_LValueToRValue, |
| 5662 | CurInit.take(), |
| 5663 | /*BasePath=*/0, |
| 5664 | VK_RValue)); |
| 5665 | break; |
| 5666 | } |
| 5667 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5668 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5669 | Sema::CheckedConversionKind CCK |
| 5670 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5671 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5672 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5673 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5674 | ExprResult CurInitExprRes = |
| 5675 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5676 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5677 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5678 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5679 | CurInit = CurInitExprRes; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5680 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5681 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5682 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5683 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5684 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5685 | // Hack: We must pass *ResultType if available in order to set the type |
| 5686 | // of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5687 | // But in 'const X &x = {1, 2, 3};' we're supposed to initialize a |
| 5688 | // temporary, not a reference, so we should pass Ty. |
| 5689 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5690 | // Since this step is never used for a reference directly, we explicitly |
| 5691 | // unwrap references here and rewrap them afterwards. |
| 5692 | // We also need to create a InitializeTemporary entity for this. |
| 5693 | QualType Ty = ResultType ? ResultType->getNonReferenceType() : Step->Type; |
Sebastian Redl | cbf8209 | 2012-03-07 16:10:45 +0000 | [diff] [blame] | 5694 | bool IsTemporary = Entity.getType()->isReferenceType(); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5695 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5696 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 5697 | InitListChecker PerformInitList(S, InitEntity, |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5698 | InitList, Ty, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5699 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5700 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5701 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5702 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5703 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5704 | if (ResultType) { |
| 5705 | if ((*ResultType)->isRValueReferenceType()) |
| 5706 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5707 | else if ((*ResultType)->isLValueReferenceType()) |
| 5708 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5709 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5710 | *ResultType = Ty; |
| 5711 | } |
| 5712 | |
| 5713 | InitListExpr *StructuredInitList = |
| 5714 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5715 | CurInit.release(); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5716 | CurInit = shouldBindAsTemporary(InitEntity) |
| 5717 | ? S.MaybeBindToTemporary(StructuredInitList) |
| 5718 | : S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5719 | break; |
| 5720 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5721 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5722 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5723 | // When an initializer list is passed for a parameter of type "reference |
| 5724 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5725 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5726 | // FIXME: This is a hack. What we really should do is create a user |
| 5727 | // conversion step for this case, but this makes it considerably more |
| 5728 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5729 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5730 | Entity.getType().getNonReferenceType()); |
| 5731 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5732 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5733 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5734 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 5735 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5736 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5737 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 5738 | Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5739 | Kind, Arg, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5740 | ConstructorInitRequiresZeroInit, |
| 5741 | /*IsListInitialization*/ true); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5742 | break; |
| 5743 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5744 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5745 | case SK_UnwrapInitList: |
| 5746 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 5747 | break; |
| 5748 | |
| 5749 | case SK_RewrapInitList: { |
| 5750 | Expr *E = CurInit.take(); |
| 5751 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 5752 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5753 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5754 | ILE->setSyntacticForm(Syntactic); |
| 5755 | ILE->setType(E->getType()); |
| 5756 | ILE->setValueKind(E->getValueKind()); |
| 5757 | CurInit = S.Owned(ILE); |
| 5758 | break; |
| 5759 | } |
| 5760 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5761 | case SK_ConstructorInitialization: { |
| 5762 | // When an initializer list is passed for a parameter of type "reference |
| 5763 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5764 | // EK_Parameter entity with reference type. |
| 5765 | // FIXME: This is a hack. What we really should do is create a user |
| 5766 | // conversion step for this case, but this makes it considerably more |
| 5767 | // complicated. For now, this will do. |
| 5768 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5769 | Entity.getType().getNonReferenceType()); |
| 5770 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 5771 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 5772 | : Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5773 | Kind, Args, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5774 | ConstructorInitRequiresZeroInit, |
| 5775 | /*IsListInitialization*/ false); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5776 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5777 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5778 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5779 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5780 | step_iterator NextStep = Step; |
| 5781 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5782 | if (NextStep != StepEnd && |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5783 | (NextStep->Kind == SK_ConstructorInitialization || |
| 5784 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5785 | // The need for zero-initialization is recorded directly into |
| 5786 | // the call to the object's constructor within the next step. |
| 5787 | ConstructorInitRequiresZeroInit = true; |
| 5788 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5789 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5790 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5791 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5792 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5793 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5794 | Kind.getRange().getBegin()); |
| 5795 | |
| 5796 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 5797 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 5798 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5799 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5800 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5801 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5802 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5803 | break; |
| 5804 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5805 | |
| 5806 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5807 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5808 | ExprResult Result = CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5809 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5810 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 5811 | if (Result.isInvalid()) |
| 5812 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5813 | CurInit = Result; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5814 | |
| 5815 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5816 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5817 | if (ConvTy != Sema::Compatible && |
| 5818 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5819 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5820 | == Sema::Compatible) |
| 5821 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5822 | if (CurInitExprRes.isInvalid()) |
| 5823 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5824 | CurInit = CurInitExprRes; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5825 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5826 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5827 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 5828 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5829 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5830 | getAssignmentAction(Entity), |
| 5831 | &Complained)) { |
| 5832 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5833 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5834 | } else if (Complained) |
| 5835 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5836 | break; |
| 5837 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5838 | |
| 5839 | case SK_StringInit: { |
| 5840 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5841 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 5842 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5843 | break; |
| 5844 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5845 | |
| 5846 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5847 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5848 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5849 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5850 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5851 | |
| 5852 | case SK_ArrayInit: |
| 5853 | // Okay: we checked everything before creating this step. Note that |
| 5854 | // this is a GNU extension. |
| 5855 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5856 | << Step->Type << CurInit.get()->getType() |
| 5857 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5858 | |
| 5859 | // If the destination type is an incomplete array type, update the |
| 5860 | // type accordingly. |
| 5861 | if (ResultType) { |
| 5862 | if (const IncompleteArrayType *IncompleteDest |
| 5863 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 5864 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5865 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5866 | *ResultType = S.Context.getConstantArrayType( |
| 5867 | IncompleteDest->getElementType(), |
| 5868 | ConstantSource->getSize(), |
| 5869 | ArrayType::Normal, 0); |
| 5870 | } |
| 5871 | } |
| 5872 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5873 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5874 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5875 | case SK_ParenthesizedArrayInit: |
| 5876 | // Okay: we checked everything before creating this step. Note that |
| 5877 | // this is a GNU extension. |
| 5878 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 5879 | << CurInit.get()->getSourceRange(); |
| 5880 | break; |
| 5881 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5882 | case SK_PassByIndirectCopyRestore: |
| 5883 | case SK_PassByIndirectRestore: |
| 5884 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 5885 | CurInit = S.Owned(new (S.Context) |
| 5886 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 5887 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 5888 | break; |
| 5889 | |
| 5890 | case SK_ProduceObjCObject: |
| 5891 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5892 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5893 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5894 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5895 | |
| 5896 | case SK_StdInitializerList: { |
| 5897 | QualType Dest = Step->Type; |
| 5898 | QualType E; |
Douglas Gregor | 6c5aaed | 2013-03-25 23:47:01 +0000 | [diff] [blame] | 5899 | bool Success = S.isStdInitializerList(Dest.getNonReferenceType(), &E); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5900 | (void)Success; |
| 5901 | assert(Success && "Destination type changed?"); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 5902 | |
| 5903 | // If the element type has a destructor, check it. |
| 5904 | if (CXXRecordDecl *RD = E->getAsCXXRecordDecl()) { |
| 5905 | if (!RD->hasIrrelevantDestructor()) { |
| 5906 | if (CXXDestructorDecl *Destructor = S.LookupDestructor(RD)) { |
| 5907 | S.MarkFunctionReferenced(Kind.getLocation(), Destructor); |
| 5908 | S.CheckDestructorAccess(Kind.getLocation(), Destructor, |
| 5909 | S.PDiag(diag::err_access_dtor_temp) << E); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5910 | if (S.DiagnoseUseOfDecl(Destructor, Kind.getLocation())) |
| 5911 | return ExprError(); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 5912 | } |
| 5913 | } |
| 5914 | } |
| 5915 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5916 | InitListExpr *ILE = cast<InitListExpr>(CurInit.take()); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5917 | S.Diag(ILE->getExprLoc(), diag::warn_cxx98_compat_initializer_list_init) |
| 5918 | << ILE->getSourceRange(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5919 | unsigned NumInits = ILE->getNumInits(); |
| 5920 | SmallVector<Expr*, 16> Converted(NumInits); |
| 5921 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 5922 | S.Context.getConstantArrayType(E, |
| 5923 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 5924 | NumInits), |
| 5925 | ArrayType::Normal, 0)); |
| 5926 | InitializedEntity Element =InitializedEntity::InitializeElement(S.Context, |
| 5927 | 0, HiddenArray); |
| 5928 | for (unsigned i = 0; i < NumInits; ++i) { |
| 5929 | Element.setElementIndex(i); |
| 5930 | ExprResult Init = S.Owned(ILE->getInit(i)); |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5931 | ExprResult Res = S.PerformCopyInitialization( |
| 5932 | Element, Init.get()->getExprLoc(), Init, |
| 5933 | /*TopLevelOfInitList=*/ true); |
Richard Smith | 2c2f09e | 2013-05-23 23:20:04 +0000 | [diff] [blame] | 5934 | if (Res.isInvalid()) |
| 5935 | return ExprError(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5936 | Converted[i] = Res.take(); |
| 5937 | } |
| 5938 | InitListExpr *Semantic = new (S.Context) |
| 5939 | InitListExpr(S.Context, ILE->getLBraceLoc(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5940 | Converted, ILE->getRBraceLoc()); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5941 | Semantic->setSyntacticForm(ILE); |
| 5942 | Semantic->setType(Dest); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 5943 | Semantic->setInitializesStdInitializerList(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5944 | CurInit = S.Owned(Semantic); |
| 5945 | break; |
| 5946 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5947 | case SK_OCLSamplerInit: { |
| 5948 | assert(Step->Type->isSamplerT() && |
| 5949 | "Sampler initialization on non sampler type."); |
| 5950 | |
| 5951 | QualType SourceType = CurInit.get()->getType(); |
| 5952 | InitializedEntity::EntityKind EntityKind = Entity.getKind(); |
| 5953 | |
| 5954 | if (EntityKind == InitializedEntity::EK_Parameter) { |
| 5955 | if (!SourceType->isSamplerT()) |
| 5956 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 5957 | << SourceType; |
| 5958 | } else if (EntityKind != InitializedEntity::EK_Variable) { |
| 5959 | llvm_unreachable("Invalid EntityKind!"); |
| 5960 | } |
| 5961 | |
| 5962 | break; |
| 5963 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5964 | case SK_OCLZeroEvent: { |
| 5965 | assert(Step->Type->isEventT() && |
| 5966 | "Event initialization on non event type."); |
| 5967 | |
| 5968 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
| 5969 | CK_ZeroToOCLEvent, |
| 5970 | CurInit.get()->getValueKind()); |
| 5971 | break; |
| 5972 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5973 | } |
| 5974 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5975 | |
| 5976 | // Diagnose non-fatal problems with the completed initialization. |
| 5977 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5978 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 5979 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 5980 | cast<FieldDecl>(Entity.getDecl()), |
| 5981 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5982 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5983 | return CurInit; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5984 | } |
| 5985 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5986 | /// Somewhere within T there is an uninitialized reference subobject. |
| 5987 | /// Dig it out and diagnose it. |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 5988 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 5989 | QualType T) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5990 | if (T->isReferenceType()) { |
| 5991 | S.Diag(Loc, diag::err_reference_without_init) |
| 5992 | << T.getNonReferenceType(); |
| 5993 | return true; |
| 5994 | } |
| 5995 | |
| 5996 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 5997 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 5998 | return false; |
| 5999 | |
| 6000 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 6001 | FE = RD->field_end(); FI != FE; ++FI) { |
| 6002 | if (FI->isUnnamedBitfield()) |
| 6003 | continue; |
| 6004 | |
| 6005 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 6006 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6007 | return true; |
| 6008 | } |
| 6009 | } |
| 6010 | |
| 6011 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 6012 | BE = RD->bases_end(); |
| 6013 | BI != BE; ++BI) { |
| 6014 | if (DiagnoseUninitializedReference(S, BI->getLocStart(), BI->getType())) { |
| 6015 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6016 | return true; |
| 6017 | } |
| 6018 | } |
| 6019 | |
| 6020 | return false; |
| 6021 | } |
| 6022 | |
| 6023 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6024 | //===----------------------------------------------------------------------===// |
| 6025 | // Diagnose initialization failures |
| 6026 | //===----------------------------------------------------------------------===// |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6027 | |
| 6028 | /// Emit notes associated with an initialization that failed due to a |
| 6029 | /// "simple" conversion failure. |
| 6030 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 6031 | Expr *op) { |
| 6032 | QualType destType = entity.getType(); |
| 6033 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 6034 | op->getType()->isObjCObjectPointerType()) { |
| 6035 | |
| 6036 | // Emit a possible note about the conversion failing because the |
| 6037 | // operand is a message send with a related result type. |
| 6038 | S.EmitRelatedResultTypeNote(op); |
| 6039 | |
| 6040 | // Emit a possible note about a return failing because we're |
| 6041 | // expecting a related result type. |
| 6042 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 6043 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 6044 | } |
| 6045 | } |
| 6046 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6047 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6048 | const InitializedEntity &Entity, |
| 6049 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6050 | ArrayRef<Expr *> Args) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 6051 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6052 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6053 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 6054 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6055 | switch (Failure) { |
| 6056 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6057 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6058 | if (Args.empty()) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6059 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 6060 | // If this is value-initialization, this could be nested some way within |
| 6061 | // the target type. |
| 6062 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 6063 | DestType->isReferenceType()); |
| 6064 | bool Diagnosed = |
| 6065 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 6066 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 6067 | (void)Diagnosed; |
| 6068 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6069 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6070 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6071 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6072 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6073 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6074 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6075 | break; |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6076 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6077 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 6078 | break; |
| 6079 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6080 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 6081 | break; |
| 6082 | case FK_NarrowStringIntoWideCharArray: |
| 6083 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 6084 | break; |
| 6085 | case FK_WideStringIntoCharArray: |
| 6086 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 6087 | break; |
| 6088 | case FK_IncompatWideStringIntoWideChar: |
| 6089 | S.Diag(Kind.getLocation(), |
| 6090 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 6091 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6092 | case FK_ArrayTypeMismatch: |
| 6093 | case FK_NonConstantArrayInit: |
| 6094 | S.Diag(Kind.getLocation(), |
| 6095 | (Failure == FK_ArrayTypeMismatch |
| 6096 | ? diag::err_array_init_different_type |
| 6097 | : diag::err_array_init_non_constant_array)) |
| 6098 | << DestType.getNonReferenceType() |
| 6099 | << Args[0]->getType() |
| 6100 | << Args[0]->getSourceRange(); |
| 6101 | break; |
| 6102 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6103 | case FK_VariableLengthArrayHasInitializer: |
| 6104 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 6105 | << Args[0]->getSourceRange(); |
| 6106 | break; |
| 6107 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6108 | case FK_AddressOfOverloadFailed: { |
| 6109 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6110 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6111 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6112 | true, |
| 6113 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6114 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6115 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6116 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6117 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 6118 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6119 | switch (FailedOverloadResult) { |
| 6120 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6121 | if (Failure == FK_UserConversionOverloadFailed) |
| 6122 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 6123 | << Args[0]->getType() << DestType |
| 6124 | << Args[0]->getSourceRange(); |
| 6125 | else |
| 6126 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 6127 | << DestType << Args[0]->getType() |
| 6128 | << Args[0]->getSourceRange(); |
| 6129 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6130 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6131 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6132 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6133 | case OR_No_Viable_Function: |
| 6134 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 6135 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 6136 | << Args[0]->getSourceRange(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6137 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6138 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6139 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6140 | case OR_Deleted: { |
| 6141 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 6142 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 6143 | << Args[0]->getSourceRange(); |
| 6144 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6145 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6146 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 6147 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6148 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6149 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6150 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6151 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6152 | } |
| 6153 | break; |
| 6154 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6155 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6156 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6157 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6158 | } |
| 6159 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6160 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6161 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6162 | if (isa<InitListExpr>(Args[0])) { |
| 6163 | S.Diag(Kind.getLocation(), |
| 6164 | diag::err_lvalue_reference_bind_to_initlist) |
| 6165 | << DestType.getNonReferenceType().isVolatileQualified() |
| 6166 | << DestType.getNonReferenceType() |
| 6167 | << Args[0]->getSourceRange(); |
| 6168 | break; |
| 6169 | } |
| 6170 | // Intentional fallthrough |
| 6171 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6172 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6173 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6174 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 6175 | ? diag::err_lvalue_reference_bind_to_temporary |
| 6176 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 6177 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6178 | << DestType.getNonReferenceType() |
| 6179 | << Args[0]->getType() |
| 6180 | << Args[0]->getSourceRange(); |
| 6181 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6182 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6183 | case FK_RValueReferenceBindingToLValue: |
| 6184 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 6185 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6186 | << Args[0]->getSourceRange(); |
| 6187 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6188 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6189 | case FK_ReferenceInitDropsQualifiers: |
| 6190 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 6191 | << DestType.getNonReferenceType() |
| 6192 | << Args[0]->getType() |
| 6193 | << Args[0]->getSourceRange(); |
| 6194 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6195 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6196 | case FK_ReferenceInitFailed: |
| 6197 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 6198 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6199 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6200 | << Args[0]->getType() |
| 6201 | << Args[0]->getSourceRange(); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6202 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6203 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6204 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6205 | case FK_ConversionFailed: { |
| 6206 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6207 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6208 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6209 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6210 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6211 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6212 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6213 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 6214 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6215 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6216 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6217 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6218 | |
| 6219 | case FK_ConversionFromPropertyFailed: |
| 6220 | // No-op. This error has already been reported. |
| 6221 | break; |
| 6222 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6223 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6224 | SourceRange R; |
| 6225 | |
| 6226 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6227 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6228 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6229 | else |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6230 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6231 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6232 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 6233 | if (Kind.isCStyleOrFunctionalCast()) |
| 6234 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 6235 | << R; |
| 6236 | else |
| 6237 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 6238 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6239 | break; |
| 6240 | } |
| 6241 | |
| 6242 | case FK_ReferenceBindingToInitList: |
| 6243 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 6244 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 6245 | break; |
| 6246 | |
| 6247 | case FK_InitListBadDestinationType: |
| 6248 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 6249 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 6250 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6251 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6252 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6253 | case FK_ConstructorOverloadFailed: { |
| 6254 | SourceRange ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6255 | if (Args.size()) |
| 6256 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 6257 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6258 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6259 | if (Failure == FK_ListConstructorOverloadFailed) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6260 | assert(Args.size() == 1 && "List construction from other than 1 argument."); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6261 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6262 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6263 | } |
| 6264 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6265 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 6266 | // bad. |
| 6267 | switch (FailedOverloadResult) { |
| 6268 | case OR_Ambiguous: |
| 6269 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 6270 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6271 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6272 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6273 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6274 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6275 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 6276 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 6277 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 6278 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6279 | // This is implicit default initialization of a member or |
| 6280 | // base within a constructor. If no viable function was |
| 6281 | // found, notify the user that she needs to explicitly |
| 6282 | // initialize this base/member. |
| 6283 | CXXConstructorDecl *Constructor |
| 6284 | = cast<CXXConstructorDecl>(S.CurContext); |
| 6285 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6286 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6287 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6288 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6289 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6290 | << /*base=*/0 |
| 6291 | << Entity.getType(); |
| 6292 | |
| 6293 | RecordDecl *BaseDecl |
| 6294 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 6295 | ->getDecl(); |
| 6296 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 6297 | << S.Context.getTagDeclType(BaseDecl); |
| 6298 | } else { |
| 6299 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6300 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6301 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6302 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6303 | << /*member=*/1 |
| 6304 | << Entity.getName(); |
| 6305 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 6306 | |
| 6307 | if (const RecordType *Record |
| 6308 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6309 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6310 | diag::note_previous_decl) |
| 6311 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6312 | } |
| 6313 | break; |
| 6314 | } |
| 6315 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6316 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6317 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6318 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6319 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6320 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6321 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6322 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6323 | OverloadingResult Ovl |
| 6324 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6325 | if (Ovl != OR_Deleted) { |
| 6326 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6327 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6328 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6329 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6330 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6331 | |
| 6332 | // If this is a defaulted or implicitly-declared function, then |
| 6333 | // it was implicitly deleted. Make it clear that the deletion was |
| 6334 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6335 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6336 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6337 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6338 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6339 | else |
| 6340 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6341 | << true << DestType << ArgsRange; |
| 6342 | |
| 6343 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6344 | break; |
| 6345 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6346 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6347 | case OR_Success: |
| 6348 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6349 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6350 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6351 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6352 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6353 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6354 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6355 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6356 | // This is implicit default-initialization of a const member in |
| 6357 | // a constructor. Complain that it needs to be explicitly |
| 6358 | // initialized. |
| 6359 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6360 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6361 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6362 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6363 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6364 | << /*const=*/1 |
| 6365 | << Entity.getName(); |
| 6366 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6367 | << Entity.getName(); |
| 6368 | } else { |
| 6369 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 6370 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6371 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6372 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6373 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6374 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6375 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6376 | diag::err_init_incomplete_type); |
| 6377 | break; |
| 6378 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6379 | case FK_ListInitializationFailed: { |
| 6380 | // Run the init list checker again to emit diagnostics. |
| 6381 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 6382 | QualType DestType = Entity.getType(); |
| 6383 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 6384 | DestType, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6385 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6386 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6387 | assert(DiagnoseInitList.HadError() && |
| 6388 | "Inconsistent init list check result."); |
| 6389 | break; |
| 6390 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6391 | |
| 6392 | case FK_PlaceholderType: { |
| 6393 | // FIXME: Already diagnosed! |
| 6394 | break; |
| 6395 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6396 | |
| 6397 | case FK_InitListElementCopyFailure: { |
| 6398 | // Try to perform all copies again. |
| 6399 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 6400 | unsigned NumInits = InitList->getNumInits(); |
| 6401 | QualType DestType = Entity.getType(); |
| 6402 | QualType E; |
Douglas Gregor | 6c5aaed | 2013-03-25 23:47:01 +0000 | [diff] [blame] | 6403 | bool Success = S.isStdInitializerList(DestType.getNonReferenceType(), &E); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6404 | (void)Success; |
| 6405 | assert(Success && "Where did the std::initializer_list go?"); |
| 6406 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 6407 | S.Context.getConstantArrayType(E, |
| 6408 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 6409 | NumInits), |
| 6410 | ArrayType::Normal, 0)); |
| 6411 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 6412 | 0, HiddenArray); |
| 6413 | // Show at most 3 errors. Otherwise, you'd get a lot of errors for errors |
| 6414 | // where the init list type is wrong, e.g. |
| 6415 | // std::initializer_list<void*> list = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 6416 | // FIXME: Emit a note if we hit the limit? |
| 6417 | int ErrorCount = 0; |
| 6418 | for (unsigned i = 0; i < NumInits && ErrorCount < 3; ++i) { |
| 6419 | Element.setElementIndex(i); |
| 6420 | ExprResult Init = S.Owned(InitList->getInit(i)); |
| 6421 | if (S.PerformCopyInitialization(Element, Init.get()->getExprLoc(), Init) |
| 6422 | .isInvalid()) |
| 6423 | ++ErrorCount; |
| 6424 | } |
| 6425 | break; |
| 6426 | } |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6427 | |
| 6428 | case FK_ExplicitConstructor: { |
| 6429 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6430 | << Args[0]->getSourceRange(); |
| 6431 | OverloadCandidateSet::iterator Best; |
| 6432 | OverloadingResult Ovl |
| 6433 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6434 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6435 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6436 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6437 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6438 | break; |
| 6439 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6440 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6441 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6442 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6443 | return true; |
| 6444 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6445 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6446 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6447 | switch (SequenceKind) { |
| 6448 | case FailedSequence: { |
| 6449 | OS << "Failed sequence: "; |
| 6450 | switch (Failure) { |
| 6451 | case FK_TooManyInitsForReference: |
| 6452 | OS << "too many initializers for reference"; |
| 6453 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6454 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6455 | case FK_ArrayNeedsInitList: |
| 6456 | OS << "array requires initializer list"; |
| 6457 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6458 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6459 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6460 | OS << "array requires initializer list or string literal"; |
| 6461 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6462 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6463 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6464 | OS << "array requires initializer list or wide string literal"; |
| 6465 | break; |
| 6466 | |
| 6467 | case FK_NarrowStringIntoWideCharArray: |
| 6468 | OS << "narrow string into wide char array"; |
| 6469 | break; |
| 6470 | |
| 6471 | case FK_WideStringIntoCharArray: |
| 6472 | OS << "wide string into char array"; |
| 6473 | break; |
| 6474 | |
| 6475 | case FK_IncompatWideStringIntoWideChar: |
| 6476 | OS << "incompatible wide string into wide char array"; |
| 6477 | break; |
| 6478 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6479 | case FK_ArrayTypeMismatch: |
| 6480 | OS << "array type mismatch"; |
| 6481 | break; |
| 6482 | |
| 6483 | case FK_NonConstantArrayInit: |
| 6484 | OS << "non-constant array initializer"; |
| 6485 | break; |
| 6486 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6487 | case FK_AddressOfOverloadFailed: |
| 6488 | OS << "address of overloaded function failed"; |
| 6489 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6490 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6491 | case FK_ReferenceInitOverloadFailed: |
| 6492 | OS << "overload resolution for reference initialization failed"; |
| 6493 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6494 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6495 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6496 | OS << "non-const lvalue reference bound to temporary"; |
| 6497 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6498 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6499 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6500 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6501 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6502 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6503 | case FK_RValueReferenceBindingToLValue: |
| 6504 | OS << "rvalue reference bound to an lvalue"; |
| 6505 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6506 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6507 | case FK_ReferenceInitDropsQualifiers: |
| 6508 | OS << "reference initialization drops qualifiers"; |
| 6509 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6510 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6511 | case FK_ReferenceInitFailed: |
| 6512 | OS << "reference initialization failed"; |
| 6513 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6514 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6515 | case FK_ConversionFailed: |
| 6516 | OS << "conversion failed"; |
| 6517 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6518 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6519 | case FK_ConversionFromPropertyFailed: |
| 6520 | OS << "conversion from property failed"; |
| 6521 | break; |
| 6522 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6523 | case FK_TooManyInitsForScalar: |
| 6524 | OS << "too many initializers for scalar"; |
| 6525 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6526 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6527 | case FK_ReferenceBindingToInitList: |
| 6528 | OS << "referencing binding to initializer list"; |
| 6529 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6530 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6531 | case FK_InitListBadDestinationType: |
| 6532 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6533 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6534 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6535 | case FK_UserConversionOverloadFailed: |
| 6536 | OS << "overloading failed for user-defined conversion"; |
| 6537 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6538 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6539 | case FK_ConstructorOverloadFailed: |
| 6540 | OS << "constructor overloading failed"; |
| 6541 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6542 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6543 | case FK_DefaultInitOfConst: |
| 6544 | OS << "default initialization of a const variable"; |
| 6545 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6546 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6547 | case FK_Incomplete: |
| 6548 | OS << "initialization of incomplete type"; |
| 6549 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6550 | |
| 6551 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6552 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6553 | break; |
| 6554 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6555 | case FK_VariableLengthArrayHasInitializer: |
| 6556 | OS << "variable length array has an initializer"; |
| 6557 | break; |
| 6558 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6559 | case FK_PlaceholderType: |
| 6560 | OS << "initializer expression isn't contextually valid"; |
| 6561 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6562 | |
| 6563 | case FK_ListConstructorOverloadFailed: |
| 6564 | OS << "list constructor overloading failed"; |
| 6565 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6566 | |
| 6567 | case FK_InitListElementCopyFailure: |
| 6568 | OS << "copy construction of initializer list element failed"; |
| 6569 | break; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6570 | |
| 6571 | case FK_ExplicitConstructor: |
| 6572 | OS << "list copy initialization chose explicit constructor"; |
| 6573 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6574 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6575 | OS << '\n'; |
| 6576 | return; |
| 6577 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6578 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6579 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6580 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6581 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6582 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6583 | case NormalSequence: |
| 6584 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6585 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6586 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6587 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6588 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6589 | if (S != step_begin()) { |
| 6590 | OS << " -> "; |
| 6591 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6592 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6593 | switch (S->Kind) { |
| 6594 | case SK_ResolveAddressOfOverloadedFunction: |
| 6595 | OS << "resolve address of overloaded function"; |
| 6596 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6597 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6598 | case SK_CastDerivedToBaseRValue: |
| 6599 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6600 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6601 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6602 | case SK_CastDerivedToBaseXValue: |
| 6603 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6604 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6605 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6606 | case SK_CastDerivedToBaseLValue: |
| 6607 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 6608 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6609 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6610 | case SK_BindReference: |
| 6611 | OS << "bind reference to lvalue"; |
| 6612 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6613 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6614 | case SK_BindReferenceToTemporary: |
| 6615 | OS << "bind reference to a temporary"; |
| 6616 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6617 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6618 | case SK_ExtraneousCopyToTemporary: |
| 6619 | OS << "extraneous C++03 copy to temporary"; |
| 6620 | break; |
| 6621 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6622 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6623 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6624 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6625 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6626 | case SK_QualificationConversionRValue: |
| 6627 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6628 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6629 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6630 | case SK_QualificationConversionXValue: |
| 6631 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6632 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6633 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6634 | case SK_QualificationConversionLValue: |
| 6635 | OS << "qualification conversion (lvalue)"; |
| 6636 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6637 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6638 | case SK_LValueToRValue: |
| 6639 | OS << "load (lvalue to rvalue)"; |
| 6640 | break; |
| 6641 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6642 | case SK_ConversionSequence: |
| 6643 | OS << "implicit conversion sequence ("; |
| 6644 | S->ICS->DebugPrint(); // FIXME: use OS |
| 6645 | OS << ")"; |
| 6646 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6647 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6648 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6649 | OS << "list aggregate initialization"; |
| 6650 | break; |
| 6651 | |
| 6652 | case SK_ListConstructorCall: |
| 6653 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6654 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6655 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6656 | case SK_UnwrapInitList: |
| 6657 | OS << "unwrap reference initializer list"; |
| 6658 | break; |
| 6659 | |
| 6660 | case SK_RewrapInitList: |
| 6661 | OS << "rewrap reference initializer list"; |
| 6662 | break; |
| 6663 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6664 | case SK_ConstructorInitialization: |
| 6665 | OS << "constructor initialization"; |
| 6666 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6667 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6668 | case SK_ZeroInitialization: |
| 6669 | OS << "zero initialization"; |
| 6670 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6671 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6672 | case SK_CAssignment: |
| 6673 | OS << "C assignment"; |
| 6674 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6675 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6676 | case SK_StringInit: |
| 6677 | OS << "string initialization"; |
| 6678 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6679 | |
| 6680 | case SK_ObjCObjectConversion: |
| 6681 | OS << "Objective-C object conversion"; |
| 6682 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6683 | |
| 6684 | case SK_ArrayInit: |
| 6685 | OS << "array initialization"; |
| 6686 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6687 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6688 | case SK_ParenthesizedArrayInit: |
| 6689 | OS << "parenthesized array initialization"; |
| 6690 | break; |
| 6691 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6692 | case SK_PassByIndirectCopyRestore: |
| 6693 | OS << "pass by indirect copy and restore"; |
| 6694 | break; |
| 6695 | |
| 6696 | case SK_PassByIndirectRestore: |
| 6697 | OS << "pass by indirect restore"; |
| 6698 | break; |
| 6699 | |
| 6700 | case SK_ProduceObjCObject: |
| 6701 | OS << "Objective-C object retension"; |
| 6702 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6703 | |
| 6704 | case SK_StdInitializerList: |
| 6705 | OS << "std::initializer_list from initializer list"; |
| 6706 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6707 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6708 | case SK_OCLSamplerInit: |
| 6709 | OS << "OpenCL sampler_t from integer constant"; |
| 6710 | break; |
| 6711 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6712 | case SK_OCLZeroEvent: |
| 6713 | OS << "OpenCL event_t from zero"; |
| 6714 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6715 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6716 | |
| 6717 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6718 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6719 | |
| 6720 | OS << '\n'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6721 | } |
| 6722 | |
| 6723 | void InitializationSequence::dump() const { |
| 6724 | dump(llvm::errs()); |
| 6725 | } |
| 6726 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6727 | static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, |
| 6728 | QualType EntityType, |
| 6729 | const Expr *PreInit, |
| 6730 | const Expr *PostInit) { |
| 6731 | if (Seq.step_begin() == Seq.step_end() || PreInit->isValueDependent()) |
| 6732 | return; |
| 6733 | |
| 6734 | // A narrowing conversion can only appear as the final implicit conversion in |
| 6735 | // an initialization sequence. |
| 6736 | const InitializationSequence::Step &LastStep = Seq.step_end()[-1]; |
| 6737 | if (LastStep.Kind != InitializationSequence::SK_ConversionSequence) |
| 6738 | return; |
| 6739 | |
| 6740 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 6741 | const StandardConversionSequence *SCS = 0; |
| 6742 | switch (ICS.getKind()) { |
| 6743 | case ImplicitConversionSequence::StandardConversion: |
| 6744 | SCS = &ICS.Standard; |
| 6745 | break; |
| 6746 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6747 | SCS = &ICS.UserDefined.After; |
| 6748 | break; |
| 6749 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6750 | case ImplicitConversionSequence::EllipsisConversion: |
| 6751 | case ImplicitConversionSequence::BadConversion: |
| 6752 | return; |
| 6753 | } |
| 6754 | |
| 6755 | // Determine the type prior to the narrowing conversion. If a conversion |
| 6756 | // operator was used, this may be different from both the type of the entity |
| 6757 | // and of the pre-initialization expression. |
| 6758 | QualType PreNarrowingType = PreInit->getType(); |
| 6759 | if (Seq.step_begin() + 1 != Seq.step_end()) |
| 6760 | PreNarrowingType = Seq.step_end()[-2].Type; |
| 6761 | |
| 6762 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6763 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6764 | QualType ConstantType; |
| 6765 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 6766 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6767 | case NK_Not_Narrowing: |
| 6768 | // No narrowing occurred. |
| 6769 | return; |
| 6770 | |
| 6771 | case NK_Type_Narrowing: |
| 6772 | // This was a floating-to-integer conversion, which is always considered a |
| 6773 | // narrowing conversion even if the value is a constant and can be |
| 6774 | // represented exactly as an integer. |
| 6775 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6776 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6777 | diag::warn_init_list_type_narrowing |
| 6778 | : S.isSFINAEContext()? |
| 6779 | diag::err_init_list_type_narrowing_sfinae |
| 6780 | : diag::err_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6781 | << PostInit->getSourceRange() |
| 6782 | << PreNarrowingType.getLocalUnqualifiedType() |
| 6783 | << EntityType.getLocalUnqualifiedType(); |
| 6784 | break; |
| 6785 | |
| 6786 | case NK_Constant_Narrowing: |
| 6787 | // A constant value was narrowed. |
| 6788 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6789 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6790 | diag::warn_init_list_constant_narrowing |
| 6791 | : S.isSFINAEContext()? |
| 6792 | diag::err_init_list_constant_narrowing_sfinae |
| 6793 | : diag::err_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6794 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6795 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6796 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6797 | break; |
| 6798 | |
| 6799 | case NK_Variable_Narrowing: |
| 6800 | // A variable's value may have been narrowed. |
| 6801 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6802 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6803 | diag::warn_init_list_variable_narrowing |
| 6804 | : S.isSFINAEContext()? |
| 6805 | diag::err_init_list_variable_narrowing_sfinae |
| 6806 | : diag::err_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6807 | << PostInit->getSourceRange() |
| 6808 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6809 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6810 | break; |
| 6811 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6812 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6813 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6814 | llvm::raw_svector_ostream OS(StaticCast); |
| 6815 | OS << "static_cast<"; |
| 6816 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 6817 | // It's important to use the typedef's name if there is one so that the |
| 6818 | // fixit doesn't break code using types like int64_t. |
| 6819 | // |
| 6820 | // FIXME: This will break if the typedef requires qualification. But |
| 6821 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6822 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6823 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6824 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6825 | else { |
| 6826 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 6827 | // with a broken cast. |
| 6828 | return; |
| 6829 | } |
| 6830 | OS << ">("; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6831 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override) |
| 6832 | << PostInit->getSourceRange() |
| 6833 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6834 | << FixItHint::CreateInsertion( |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6835 | S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6836 | } |
| 6837 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6838 | //===----------------------------------------------------------------------===// |
| 6839 | // Initialization helper functions |
| 6840 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6841 | bool |
| 6842 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 6843 | ExprResult Init) { |
| 6844 | if (Init.isInvalid()) |
| 6845 | return false; |
| 6846 | |
| 6847 | Expr *InitE = Init.get(); |
| 6848 | assert(InitE && "No initialization expression"); |
| 6849 | |
Douglas Gregor | 3c394c5 | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 6850 | InitializationKind Kind |
| 6851 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6852 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 6853 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6854 | } |
| 6855 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6856 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6857 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 6858 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6859 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6860 | bool TopLevelOfInitList, |
| 6861 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6862 | if (Init.isInvalid()) |
| 6863 | return ExprError(); |
| 6864 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6865 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6866 | assert(InitE && "No initialization expression?"); |
| 6867 | |
| 6868 | if (EqualLoc.isInvalid()) |
| 6869 | EqualLoc = InitE->getLocStart(); |
| 6870 | |
| 6871 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6872 | EqualLoc, |
| 6873 | AllowExplicit); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6874 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6875 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6876 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6877 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6878 | |
| 6879 | if (!Result.isInvalid() && TopLevelOfInitList) |
| 6880 | DiagnoseNarrowingInInitList(*this, Seq, Entity.getType(), |
| 6881 | InitE, Result.get()); |
| 6882 | |
| 6883 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6884 | } |