Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1 | //===--- SemaInit.cpp - Semantic Analysis for Initializers ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 10 | // This file implements semantic analysis for initializers. |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 11 | // |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Initialization.h" |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | 2078bb9 | 2009-05-27 16:10:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 18 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Sema/Designator.h" |
| 22 | #include "clang/Sema/Lookup.h" |
| 23 | #include "clang/Sema/SemaInternal.h" |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/APInt.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 28 | #include <map> |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 29 | using namespace clang; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 30 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 31 | //===----------------------------------------------------------------------===// |
| 32 | // Sema Initialization Checking |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 35 | /// \brief Check whether T is compatible with a wide character type (wchar_t, |
| 36 | /// char16_t or char32_t). |
| 37 | static bool IsWideCharCompatible(QualType T, ASTContext &Context) { |
| 38 | if (Context.typesAreCompatible(Context.getWideCharType(), T)) |
| 39 | return true; |
| 40 | if (Context.getLangOpts().CPlusPlus || Context.getLangOpts().C11) { |
| 41 | return Context.typesAreCompatible(Context.Char16Ty, T) || |
| 42 | Context.typesAreCompatible(Context.Char32Ty, T); |
| 43 | } |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | enum StringInitFailureKind { |
| 48 | SIF_None, |
| 49 | SIF_NarrowStringIntoWideChar, |
| 50 | SIF_WideStringIntoChar, |
| 51 | SIF_IncompatWideStringIntoWideChar, |
| 52 | SIF_Other |
| 53 | }; |
| 54 | |
| 55 | /// \brief Check whether the array of type AT can be initialized by the Init |
| 56 | /// expression by means of string initialization. Returns SIF_None if so, |
| 57 | /// otherwise returns a StringInitFailureKind that describes why the |
| 58 | /// initialization would not work. |
| 59 | static StringInitFailureKind IsStringInit(Expr *Init, const ArrayType *AT, |
| 60 | ASTContext &Context) { |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 61 | if (!isa<ConstantArrayType>(AT) && !isa<IncompleteArrayType>(AT)) |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 62 | return SIF_Other; |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 64 | // See if this is a string literal or @encode. |
| 65 | Init = Init->IgnoreParens(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 67 | // Handle @encode, which is a narrow string. |
| 68 | if (isa<ObjCEncodeExpr>(Init) && AT->getElementType()->isCharType()) |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 69 | return SIF_None; |
Chris Lattner | 8879e3b | 2009-02-26 23:26:43 +0000 | [diff] [blame] | 70 | |
| 71 | // Otherwise we can only handle string literals. |
| 72 | StringLiteral *SL = dyn_cast<StringLiteral>(Init); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 73 | if (SL == 0) |
| 74 | return SIF_Other; |
Eli Friedman | bb6415c | 2009-05-31 10:54:53 +0000 | [diff] [blame] | 75 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 76 | const QualType ElemTy = |
| 77 | Context.getCanonicalType(AT->getElementType()).getUnqualifiedType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 78 | |
| 79 | switch (SL->getKind()) { |
| 80 | case StringLiteral::Ascii: |
| 81 | case StringLiteral::UTF8: |
| 82 | // char array can be initialized with a narrow string. |
| 83 | // Only allow char x[] = "foo"; not char x[] = L"foo"; |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 84 | if (ElemTy->isCharType()) |
| 85 | return SIF_None; |
| 86 | if (IsWideCharCompatible(ElemTy, Context)) |
| 87 | return SIF_NarrowStringIntoWideChar; |
| 88 | return SIF_Other; |
| 89 | // C99 6.7.8p15 (with correction from DR343), or C11 6.7.9p15: |
| 90 | // "An array with element type compatible with a qualified or unqualified |
| 91 | // version of wchar_t, char16_t, or char32_t may be initialized by a wide |
| 92 | // string literal with the corresponding encoding prefix (L, u, or U, |
| 93 | // respectively), optionally enclosed in braces. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 94 | case StringLiteral::UTF16: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 95 | if (Context.typesAreCompatible(Context.Char16Ty, ElemTy)) |
| 96 | return SIF_None; |
| 97 | if (ElemTy->isCharType()) |
| 98 | return SIF_WideStringIntoChar; |
| 99 | if (IsWideCharCompatible(ElemTy, Context)) |
| 100 | return SIF_IncompatWideStringIntoWideChar; |
| 101 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 102 | case StringLiteral::UTF32: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 103 | if (Context.typesAreCompatible(Context.Char32Ty, ElemTy)) |
| 104 | return SIF_None; |
| 105 | if (ElemTy->isCharType()) |
| 106 | return SIF_WideStringIntoChar; |
| 107 | if (IsWideCharCompatible(ElemTy, Context)) |
| 108 | return SIF_IncompatWideStringIntoWideChar; |
| 109 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 110 | case StringLiteral::Wide: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 111 | if (Context.typesAreCompatible(Context.getWideCharType(), ElemTy)) |
| 112 | return SIF_None; |
| 113 | if (ElemTy->isCharType()) |
| 114 | return SIF_WideStringIntoChar; |
| 115 | if (IsWideCharCompatible(ElemTy, Context)) |
| 116 | return SIF_IncompatWideStringIntoWideChar; |
| 117 | return SIF_Other; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 120 | llvm_unreachable("missed a StringLiteral kind?"); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 123 | static StringInitFailureKind IsStringInit(Expr *init, QualType declType, |
| 124 | ASTContext &Context) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 125 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 126 | if (!arrayType) |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 127 | return SIF_Other; |
| 128 | return IsStringInit(init, arrayType, Context); |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 131 | /// Update the type of a string literal, including any surrounding parentheses, |
| 132 | /// to match the type of the object which it is initializing. |
| 133 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 134 | while (true) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 135 | E->setType(Ty); |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 136 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 137 | break; |
| 138 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 139 | E = PE->getSubExpr(); |
| 140 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 141 | E = UO->getSubExpr(); |
| 142 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 143 | E = GSE->getResultExpr(); |
| 144 | else |
| 145 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 146 | } |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 147 | } |
| 148 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 149 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 150 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 151 | // Get the length of the string as parsed. |
| 152 | uint64_t StrLength = |
| 153 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 154 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 156 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 158 | // being initialized to a string literal. |
Benjamin Kramer | 65263b4 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 159 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 160 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 161 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 162 | ConstVal, |
| 163 | ArrayType::Normal, 0); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 164 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 165 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 168 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 170 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 171 | // the size may be smaller or larger than the string we are initializing. |
| 172 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 173 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 174 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 175 | // For Pascal strings it's OK to strip off the terminating null character, |
| 176 | // so the example below is valid: |
| 177 | // |
| 178 | // unsigned char a[2] = "\pa"; |
| 179 | if (SL->isPascal()) |
| 180 | StrLength--; |
| 181 | } |
| 182 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 183 | // [dcl.init.string]p2 |
| 184 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 185 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 186 | diag::err_initializer_string_for_char_array_too_long) |
| 187 | << Str->getSourceRange(); |
| 188 | } else { |
| 189 | // C99 6.7.8p14. |
| 190 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 191 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 192 | diag::warn_initializer_string_for_char_array_too_long) |
| 193 | << Str->getSourceRange(); |
| 194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 196 | // Set the type to the actual size that we are initializing. If we have |
| 197 | // something like: |
| 198 | // char x[1] = "foo"; |
| 199 | // then this will set the string literal's type to char[1]. |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 200 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 203 | //===----------------------------------------------------------------------===// |
| 204 | // Semantic checking for initializer lists. |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 207 | /// @brief Semantic checking for initializer lists. |
| 208 | /// |
| 209 | /// The InitListChecker class contains a set of routines that each |
| 210 | /// handle the initialization of a certain kind of entity, e.g., |
| 211 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 212 | /// InitListChecker itself performs a recursive walk of the subobject |
| 213 | /// structure of the type to be initialized, while stepping through |
| 214 | /// the initializer list one element at a time. The IList and Index |
| 215 | /// parameters to each of the Check* routines contain the active |
| 216 | /// (syntactic) initializer list and the index into that initializer |
| 217 | /// list that represents the current initializer. Each routine is |
| 218 | /// responsible for moving that Index forward as it consumes elements. |
| 219 | /// |
| 220 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 221 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 222 | /// initializer list and the index into that initializer list where we |
| 223 | /// are copying initializers as we map them over to the semantic |
| 224 | /// list. Once we have completed our recursive walk of the subobject |
| 225 | /// structure, we will have constructed a full semantic initializer |
| 226 | /// list. |
| 227 | /// |
| 228 | /// C99 designators cause changes in the initializer list traversal, |
| 229 | /// because they make the initialization "jump" into a specific |
| 230 | /// subobject and then continue the initialization from that |
| 231 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 232 | /// designated subobject and manages backing out the recursion to |
| 233 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 234 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 235 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 236 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 237 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 238 | bool VerifyOnly; // no diagnostics, no structure building |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 239 | llvm::DenseMap<InitListExpr *, InitListExpr *> SyntacticToSemantic; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 240 | InitListExpr *FullyStructuredList; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 242 | void CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 243 | InitListExpr *ParentIList, QualType T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 244 | unsigned &Index, InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 245 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 246 | void CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 247 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 248 | unsigned &Index, InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 249 | unsigned &StructuredIndex, |
| 250 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 251 | void CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 252 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 253 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 254 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 255 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 256 | unsigned &StructuredIndex, |
| 257 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 258 | void CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 259 | InitListExpr *IList, QualType ElemType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 260 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 261 | InitListExpr *StructuredList, |
| 262 | unsigned &StructuredIndex); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 263 | void CheckComplexType(const InitializedEntity &Entity, |
| 264 | InitListExpr *IList, QualType DeclType, |
| 265 | unsigned &Index, |
| 266 | InitListExpr *StructuredList, |
| 267 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 268 | void CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 269 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 270 | unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 271 | InitListExpr *StructuredList, |
| 272 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 273 | void CheckReferenceType(const InitializedEntity &Entity, |
| 274 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 275 | unsigned &Index, |
| 276 | InitListExpr *StructuredList, |
| 277 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 278 | void CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 279 | InitListExpr *IList, QualType DeclType, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 280 | InitListExpr *StructuredList, |
| 281 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 282 | void CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 283 | InitListExpr *IList, QualType DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 284 | RecordDecl::field_iterator Field, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 285 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 286 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 287 | unsigned &StructuredIndex, |
| 288 | bool TopLevelObject = false); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 289 | void CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 290 | InitListExpr *IList, QualType &DeclType, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | llvm::APSInt elementIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 292 | bool SubobjectIsDesignatorContext, unsigned &Index, |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 293 | InitListExpr *StructuredList, |
| 294 | unsigned &StructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 295 | bool CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 296 | InitListExpr *IList, DesignatedInitExpr *DIE, |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 297 | unsigned DesigIdx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 298 | QualType &CurrentObjectType, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 299 | RecordDecl::field_iterator *NextField, |
| 300 | llvm::APSInt *NextElementIndex, |
| 301 | unsigned &Index, |
| 302 | InitListExpr *StructuredList, |
| 303 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 304 | bool FinishSubobjectInit, |
| 305 | bool TopLevelObject); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 306 | InitListExpr *getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 307 | QualType CurrentObjectType, |
| 308 | InitListExpr *StructuredList, |
| 309 | unsigned StructuredIndex, |
| 310 | SourceRange InitRange); |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 311 | void UpdateStructuredListElement(InitListExpr *StructuredList, |
| 312 | unsigned &StructuredIndex, |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 313 | Expr *expr); |
| 314 | int numArrayElements(QualType DeclType); |
| 315 | int numStructUnionElements(QualType DeclType); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 316 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 317 | void FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 318 | const InitializedEntity &ParentEntity, |
| 319 | InitListExpr *ILE, bool &RequiresSecondPass); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 320 | void FillInValueInitializations(const InitializedEntity &Entity, |
| 321 | InitListExpr *ILE, bool &RequiresSecondPass); |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 322 | bool CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 323 | Expr *InitExpr, FieldDecl *Field, |
| 324 | bool TopLevelObject); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 325 | void CheckValueInitializable(const InitializedEntity &Entity); |
| 326 | |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 327 | public: |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 328 | InitListChecker(Sema &S, const InitializedEntity &Entity, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 329 | InitListExpr *IL, QualType &T, bool VerifyOnly); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 330 | bool HadError() { return hadError; } |
| 331 | |
| 332 | // @brief Retrieves the fully-structured initializer list used for |
| 333 | // semantic analysis and code generation. |
| 334 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 335 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 336 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 337 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 338 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 339 | assert(VerifyOnly && |
| 340 | "CheckValueInitializable is only inteded for verification mode."); |
| 341 | |
| 342 | SourceLocation Loc; |
| 343 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 344 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 345 | InitializationSequence InitSeq(SemaRef, Entity, Kind, None); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 346 | if (InitSeq.Failed()) |
| 347 | hadError = true; |
| 348 | } |
| 349 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 350 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 351 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 352 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 353 | bool &RequiresSecondPass) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 354 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 355 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 356 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 357 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 358 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 359 | // If there's no explicit initializer but we have a default initializer, use |
| 360 | // that. This only happens in C++1y, since classes with default |
| 361 | // initializers are not aggregates in C++11. |
| 362 | if (Field->hasInClassInitializer()) { |
| 363 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 364 | ILE->getRBraceLoc(), Field); |
| 365 | if (Init < NumInits) |
| 366 | ILE->setInit(Init, DIE); |
| 367 | else { |
| 368 | ILE->updateInit(SemaRef.Context, Init, DIE); |
| 369 | RequiresSecondPass = true; |
| 370 | } |
| 371 | return; |
| 372 | } |
| 373 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 374 | // FIXME: We probably don't need to handle references |
| 375 | // specially here, since value-initialization of references is |
| 376 | // handled in InitializationSequence. |
| 377 | if (Field->getType()->isReferenceType()) { |
| 378 | // C++ [dcl.init.aggr]p9: |
| 379 | // If an incomplete or empty initializer-list leaves a |
| 380 | // member of reference type uninitialized, the program is |
| 381 | // ill-formed. |
| 382 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 383 | << Field->getType() |
| 384 | << ILE->getSyntacticForm()->getSourceRange(); |
| 385 | SemaRef.Diag(Field->getLocation(), |
| 386 | diag::note_uninit_reference_member); |
| 387 | hadError = true; |
| 388 | return; |
| 389 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 390 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 391 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 392 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 393 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 394 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 395 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 396 | hadError = true; |
| 397 | return; |
| 398 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 399 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 400 | ExprResult MemberInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 401 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 402 | if (MemberInit.isInvalid()) { |
| 403 | hadError = true; |
| 404 | return; |
| 405 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 406 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 407 | if (hadError) { |
| 408 | // Do nothing |
| 409 | } else if (Init < NumInits) { |
| 410 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 411 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 412 | // Value-initialization requires a constructor call, so |
| 413 | // extend the initializer list to include the constructor |
| 414 | // call and make a note that we'll need to take another pass |
| 415 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 416 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 417 | RequiresSecondPass = true; |
| 418 | } |
| 419 | } else if (InitListExpr *InnerILE |
| 420 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 421 | FillInValueInitializations(MemberEntity, InnerILE, |
| 422 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 425 | /// Recursively replaces NULL values within the given initializer list |
| 426 | /// with expressions that perform value-initialization of the |
| 427 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 428 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 429 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 430 | InitListExpr *ILE, |
| 431 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 433 | "Should not have void type"); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 434 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 435 | if (ILE->getSyntacticForm()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 436 | Loc = ILE->getSyntacticForm()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 438 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 439 | const RecordDecl *RDecl = RType->getDecl(); |
| 440 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 441 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 442 | Entity, ILE, RequiresSecondPass); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 443 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 444 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
| 445 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 446 | FieldEnd = RDecl->field_end(); |
| 447 | Field != FieldEnd; ++Field) { |
| 448 | if (Field->hasInClassInitializer()) { |
| 449 | FillInValueInitForField(0, *Field, Entity, ILE, RequiresSecondPass); |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | } else { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 454 | unsigned Init = 0; |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 455 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 456 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 457 | Field != FieldEnd; ++Field) { |
| 458 | if (Field->isUnnamedBitfield()) |
| 459 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 460 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 461 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 462 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 463 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 464 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 465 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 466 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 467 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 468 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 469 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 470 | // Only look at the first initialization of a union. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 471 | if (RDecl->isUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 472 | break; |
| 473 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 478 | |
| 479 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 480 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 481 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 482 | unsigned NumInits = ILE->getNumInits(); |
| 483 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 484 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 485 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 486 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 487 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 488 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 489 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 490 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 491 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 492 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 493 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 494 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 496 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 498 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 499 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 500 | if (hadError) |
| 501 | return; |
| 502 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 503 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 504 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 505 | ElementEntity.setElementIndex(Init); |
| 506 | |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 507 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0); |
| 508 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 509 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 510 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 511 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 512 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 513 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 514 | hadError = true; |
| 515 | return; |
| 516 | } |
| 517 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 518 | ExprResult ElementInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 519 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 520 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 521 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | |
| 525 | if (hadError) { |
| 526 | // Do nothing |
| 527 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 528 | // For arrays, just set the expression used for value-initialization |
| 529 | // of the "holes" in the array. |
| 530 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 531 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 532 | else |
| 533 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 534 | } else { |
| 535 | // For arrays, just set the expression used for value-initialization |
| 536 | // of the rest of elements and exit. |
| 537 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 538 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 539 | return; |
| 540 | } |
| 541 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 542 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 543 | // Value-initialization requires a constructor call, so |
| 544 | // extend the initializer list to include the constructor |
| 545 | // call and make a note that we'll need to take another pass |
| 546 | // through the initializer list. |
| 547 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 548 | RequiresSecondPass = true; |
| 549 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 550 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 551 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 552 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 553 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 557 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 558 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 559 | InitListExpr *IL, QualType &T, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 560 | bool VerifyOnly) |
| 561 | : SemaRef(S), VerifyOnly(VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 562 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 563 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 564 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 565 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 567 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 568 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 569 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 570 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 571 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 572 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 573 | bool RequiresSecondPass = false; |
| 574 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 575 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 576 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 577 | RequiresSecondPass); |
| 578 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 582 | // FIXME: use a proper constant |
| 583 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 584 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 585 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 586 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 587 | } |
| 588 | return maxElements; |
| 589 | } |
| 590 | |
| 591 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 592 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 593 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 594 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 595 | Field = structDecl->field_begin(), |
| 596 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 597 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 598 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 599 | ++InitializableMembers; |
| 600 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 601 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 602 | return std::min(InitializableMembers, 1); |
| 603 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 606 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 607 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 608 | QualType T, unsigned &Index, |
| 609 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 610 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 611 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 613 | if (T->isArrayType()) |
| 614 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 615 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 616 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 617 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 618 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 619 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 620 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 621 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 622 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 623 | if (!VerifyOnly) |
| 624 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 625 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 626 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 627 | hadError = true; |
| 628 | return; |
| 629 | } |
| 630 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 631 | // Build a structured initializer list corresponding to this subobject. |
| 632 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 633 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 634 | StructuredIndex, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 635 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 636 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 637 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 638 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 639 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 640 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 641 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 642 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 643 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 644 | StructuredSubobjectInitIndex); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 645 | |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 646 | if (!VerifyOnly) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 647 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 648 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 649 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 650 | // Update the structured sub-object initializer so that it's ending |
| 651 | // range corresponds with the end of the last initializer it used. |
| 652 | if (EndIndex < ParentIList->getNumInits()) { |
| 653 | SourceLocation EndLoc |
| 654 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 655 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 656 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 657 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 658 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 659 | if (T->isArrayType() || T->isRecordType()) { |
| 660 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 661 | diag::warn_missing_braces) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 662 | << StructuredSubobjectInitList->getSourceRange() |
| 663 | << FixItHint::CreateInsertion( |
| 664 | StructuredSubobjectInitList->getLocStart(), "{") |
| 665 | << FixItHint::CreateInsertion( |
| 666 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 667 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 668 | "}"); |
| 669 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 670 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 673 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 674 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 675 | unsigned &Index, |
| 676 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 677 | unsigned &StructuredIndex, |
| 678 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 679 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 680 | if (!VerifyOnly) { |
| 681 | SyntacticToSemantic[IList] = StructuredList; |
| 682 | StructuredList->setSyntacticForm(IList); |
| 683 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 684 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 685 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 686 | if (!VerifyOnly) { |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 687 | QualType ExprTy = T; |
| 688 | if (!ExprTy->isArrayType()) |
| 689 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 690 | IList->setType(ExprTy); |
| 691 | StructuredList->setType(ExprTy); |
| 692 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 693 | if (hadError) |
| 694 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 695 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 696 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 697 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 698 | if (VerifyOnly) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 699 | if (SemaRef.getLangOpts().CPlusPlus || |
| 700 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 701 | IList->getType()->isVectorType())) { |
| 702 | hadError = true; |
| 703 | } |
| 704 | return; |
| 705 | } |
| 706 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 707 | if (StructuredIndex == 1 && |
Hans Wennborg | c1fb1e0 | 2013-05-16 09:22:40 +0000 | [diff] [blame] | 708 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context) == |
| 709 | SIF_None) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 710 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 711 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 712 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 713 | hadError = true; |
| 714 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 715 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 716 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 717 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 718 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 719 | // Don't complain for incomplete types, since we'll get an error |
| 720 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 721 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 722 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 723 | CurrentObjectType->isArrayType()? 0 : |
| 724 | CurrentObjectType->isVectorType()? 1 : |
| 725 | CurrentObjectType->isScalarType()? 2 : |
| 726 | CurrentObjectType->isUnionType()? 3 : |
| 727 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 728 | |
| 729 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 730 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 731 | DK = diag::err_excess_initializers; |
| 732 | hadError = true; |
| 733 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 734 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 735 | DK = diag::err_excess_initializers; |
| 736 | hadError = true; |
| 737 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 738 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 739 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 740 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 741 | } |
| 742 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 743 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 744 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 745 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 746 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 747 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 748 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 749 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 752 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 753 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 755 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 756 | unsigned &Index, |
| 757 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 758 | unsigned &StructuredIndex, |
| 759 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 760 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 761 | // Explicitly braced initializer for complex type can be real+imaginary |
| 762 | // parts. |
| 763 | CheckComplexType(Entity, IList, DeclType, Index, |
| 764 | StructuredList, StructuredIndex); |
| 765 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 766 | CheckScalarType(Entity, IList, DeclType, Index, |
| 767 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 768 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 769 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 770 | StructuredList, StructuredIndex); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 771 | } else if (DeclType->isRecordType()) { |
| 772 | assert(DeclType->isAggregateType() && |
| 773 | "non-aggregate records should be handed in CheckSubElementType"); |
| 774 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 775 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 776 | SubobjectIsDesignatorContext, Index, |
| 777 | StructuredList, StructuredIndex, |
| 778 | TopLevelObject); |
| 779 | } else if (DeclType->isArrayType()) { |
| 780 | llvm::APSInt Zero( |
| 781 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 782 | false); |
| 783 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 784 | SubobjectIsDesignatorContext, Index, |
| 785 | StructuredList, StructuredIndex); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 786 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 787 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 788 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 789 | if (!VerifyOnly) |
| 790 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 791 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 792 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 793 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 794 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 795 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 796 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 797 | if (!VerifyOnly) |
| 798 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 799 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 800 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 801 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 802 | if (!VerifyOnly) |
| 803 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 804 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 805 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 809 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 810 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 812 | unsigned &Index, |
| 813 | InitListExpr *StructuredList, |
| 814 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 815 | Expr *expr = IList->getInit(Index); |
Richard Smith | 6242a45 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 816 | |
| 817 | if (ElemType->isReferenceType()) |
| 818 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 819 | StructuredList, StructuredIndex); |
| 820 | |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 821 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 822 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
| 823 | unsigned newIndex = 0; |
| 824 | unsigned newStructuredIndex = 0; |
| 825 | InitListExpr *newStructuredList |
| 826 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 827 | StructuredList, StructuredIndex, |
| 828 | SubInitList->getSourceRange()); |
| 829 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
| 830 | newStructuredList, newStructuredIndex); |
| 831 | ++StructuredIndex; |
| 832 | ++Index; |
| 833 | return; |
| 834 | } |
| 835 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 836 | "non-aggregate records are only possible in C++"); |
| 837 | // C++ initialization is handled later. |
| 838 | } |
| 839 | |
Richard Smith | 6242a45 | 2013-05-31 02:56:17 +0000 | [diff] [blame] | 840 | if (ElemType->isScalarType()) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 841 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 842 | StructuredList, StructuredIndex); |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 843 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 844 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 845 | // arrayType can be incomplete if we're initializing a flexible |
| 846 | // array member. There's nothing we can do with the completed |
| 847 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 848 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 849 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 850 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 851 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 852 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 853 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 854 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 855 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 856 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 857 | |
| 858 | // Fall through for subaggregate initialization. |
| 859 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 860 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 861 | // C++ [dcl.init.aggr]p12: |
| 862 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 863 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 864 | // an initializer-list. If the initializer can initialize a |
| 865 | // member, the member is initialized. [...] |
| 866 | |
| 867 | // FIXME: Better EqualLoc? |
| 868 | InitializationKind Kind = |
| 869 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 870 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 871 | |
| 872 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 873 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 874 | ExprResult Result = |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 875 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 876 | if (Result.isInvalid()) |
| 877 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 878 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 879 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 880 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 881 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 882 | ++Index; |
| 883 | return; |
| 884 | } |
| 885 | |
| 886 | // Fall through for subaggregate initialization |
| 887 | } else { |
| 888 | // C99 6.7.8p13: |
| 889 | // |
| 890 | // The initializer for a structure or union object that has |
| 891 | // automatic storage duration shall be either an initializer |
| 892 | // list as described below, or a single expression that has |
| 893 | // compatible structure or union type. In the latter case, the |
| 894 | // initial value of the object, including unnamed members, is |
| 895 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 896 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 897 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 898 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 899 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 900 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 901 | if (ExprRes.isInvalid()) |
| 902 | hadError = true; |
| 903 | else { |
| 904 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 905 | if (ExprRes.isInvalid()) |
| 906 | hadError = true; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 907 | } |
| 908 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 909 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 910 | ++Index; |
| 911 | return; |
| 912 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 913 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 914 | // Fall through for subaggregate initialization |
| 915 | } |
| 916 | |
| 917 | // C++ [dcl.init.aggr]p12: |
| 918 | // |
| 919 | // [...] Otherwise, if the member is itself a non-empty |
| 920 | // subaggregate, brace elision is assumed and the initializer is |
| 921 | // considered for the initialization of the first member of |
| 922 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 923 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 924 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 925 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 926 | StructuredIndex); |
| 927 | ++StructuredIndex; |
| 928 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 929 | if (!VerifyOnly) { |
| 930 | // We cannot initialize this element, so let |
| 931 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 932 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 933 | SemaRef.Owned(expr), |
| 934 | /*TopLevelOfInitList=*/true); |
| 935 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 936 | hadError = true; |
| 937 | ++Index; |
| 938 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 939 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 942 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 943 | InitListExpr *IList, QualType DeclType, |
| 944 | unsigned &Index, |
| 945 | InitListExpr *StructuredList, |
| 946 | unsigned &StructuredIndex) { |
| 947 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 948 | |
| 949 | // As an extension, clang supports complex initializers, which initialize |
| 950 | // a complex number component-wise. When an explicit initializer list for |
| 951 | // a complex number contains two two initializers, this extension kicks in: |
| 952 | // it exepcts the initializer list to contain two elements convertible to |
| 953 | // the element type of the complex type. The first element initializes |
| 954 | // the real part, and the second element intitializes the imaginary part. |
| 955 | |
| 956 | if (IList->getNumInits() != 2) |
| 957 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 958 | StructuredIndex); |
| 959 | |
| 960 | // This is an extension in C. (The builtin _Complex type does not exist |
| 961 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 962 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 963 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 964 | << IList->getSourceRange(); |
| 965 | |
| 966 | // Initialize the complex number. |
| 967 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 968 | InitializedEntity ElementEntity = |
| 969 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 970 | |
| 971 | for (unsigned i = 0; i < 2; ++i) { |
| 972 | ElementEntity.setElementIndex(Index); |
| 973 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 974 | StructuredList, StructuredIndex); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 979 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 980 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 981 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 982 | InitListExpr *StructuredList, |
| 983 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 984 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 985 | if (!VerifyOnly) |
| 986 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 987 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 988 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 989 | diag::err_empty_scalar_initializer) |
| 990 | << IList->getSourceRange(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 991 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 992 | ++Index; |
| 993 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 994 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 995 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 996 | |
| 997 | Expr *expr = IList->getInit(Index); |
| 998 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 999 | if (!VerifyOnly) |
| 1000 | SemaRef.Diag(SubIList->getLocStart(), |
| 1001 | diag::warn_many_braces_around_scalar_init) |
| 1002 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1003 | |
| 1004 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1005 | StructuredIndex); |
| 1006 | return; |
| 1007 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1008 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1009 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1010 | diag::err_designator_for_scalar_init) |
| 1011 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1012 | hadError = true; |
| 1013 | ++Index; |
| 1014 | ++StructuredIndex; |
| 1015 | return; |
| 1016 | } |
| 1017 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1018 | if (VerifyOnly) { |
| 1019 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1020 | hadError = true; |
| 1021 | ++Index; |
| 1022 | return; |
| 1023 | } |
| 1024 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1025 | ExprResult Result = |
| 1026 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1027 | SemaRef.Owned(expr), |
| 1028 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1029 | |
| 1030 | Expr *ResultExpr = 0; |
| 1031 | |
| 1032 | if (Result.isInvalid()) |
| 1033 | hadError = true; // types weren't compatible. |
| 1034 | else { |
| 1035 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1036 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1037 | if (ResultExpr != expr) { |
| 1038 | // The type was promoted, update initializer list. |
| 1039 | IList->setInit(Index, ResultExpr); |
| 1040 | } |
| 1041 | } |
| 1042 | if (hadError) |
| 1043 | ++StructuredIndex; |
| 1044 | else |
| 1045 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1046 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1049 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1050 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1051 | unsigned &Index, |
| 1052 | InitListExpr *StructuredList, |
| 1053 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1054 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1055 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1056 | // general, it would be useful to pass location information down the stack, |
| 1057 | // so that we know the location (or decl) of the "current object" being |
| 1058 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1059 | if (!VerifyOnly) |
| 1060 | SemaRef.Diag(IList->getLocStart(), |
| 1061 | diag::err_init_reference_member_uninitialized) |
| 1062 | << DeclType |
| 1063 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1064 | hadError = true; |
| 1065 | ++Index; |
| 1066 | ++StructuredIndex; |
| 1067 | return; |
| 1068 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1069 | |
| 1070 | Expr *expr = IList->getInit(Index); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1071 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1072 | if (!VerifyOnly) |
| 1073 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1074 | << DeclType << IList->getSourceRange(); |
| 1075 | hadError = true; |
| 1076 | ++Index; |
| 1077 | ++StructuredIndex; |
| 1078 | return; |
| 1079 | } |
| 1080 | |
| 1081 | if (VerifyOnly) { |
| 1082 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1083 | hadError = true; |
| 1084 | ++Index; |
| 1085 | return; |
| 1086 | } |
| 1087 | |
| 1088 | ExprResult Result = |
| 1089 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1090 | SemaRef.Owned(expr), |
| 1091 | /*TopLevelOfInitList=*/true); |
| 1092 | |
| 1093 | if (Result.isInvalid()) |
| 1094 | hadError = true; |
| 1095 | |
| 1096 | expr = Result.takeAs<Expr>(); |
| 1097 | IList->setInit(Index, expr); |
| 1098 | |
| 1099 | if (hadError) |
| 1100 | ++StructuredIndex; |
| 1101 | else |
| 1102 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1103 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1106 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1107 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1108 | unsigned &Index, |
| 1109 | InitListExpr *StructuredList, |
| 1110 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1111 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1112 | unsigned maxElements = VT->getNumElements(); |
| 1113 | unsigned numEltsInit = 0; |
| 1114 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1115 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1116 | if (Index >= IList->getNumInits()) { |
| 1117 | // Make sure the element type can be value-initialized. |
| 1118 | if (VerifyOnly) |
| 1119 | CheckValueInitializable( |
| 1120 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1121 | return; |
| 1122 | } |
| 1123 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1124 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1125 | // If the initializing element is a vector, try to copy-initialize |
| 1126 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1127 | Expr *Init = IList->getInit(Index); |
| 1128 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1129 | if (VerifyOnly) { |
| 1130 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1131 | hadError = true; |
| 1132 | ++Index; |
| 1133 | return; |
| 1134 | } |
| 1135 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1136 | ExprResult Result = |
| 1137 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1138 | SemaRef.Owned(Init), |
| 1139 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1140 | |
| 1141 | Expr *ResultExpr = 0; |
| 1142 | if (Result.isInvalid()) |
| 1143 | hadError = true; // types weren't compatible. |
| 1144 | else { |
| 1145 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1146 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1147 | if (ResultExpr != Init) { |
| 1148 | // The type was promoted, update initializer list. |
| 1149 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1150 | } |
| 1151 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1152 | if (hadError) |
| 1153 | ++StructuredIndex; |
| 1154 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1155 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1156 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1157 | ++Index; |
| 1158 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1160 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1161 | InitializedEntity ElementEntity = |
| 1162 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1163 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1164 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1165 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1166 | if (Index >= IList->getNumInits()) { |
| 1167 | if (VerifyOnly) |
| 1168 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1169 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1170 | } |
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 | ElementEntity.setElementIndex(Index); |
| 1173 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1174 | StructuredList, StructuredIndex); |
| 1175 | } |
| 1176 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1177 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1178 | |
| 1179 | InitializedEntity ElementEntity = |
| 1180 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1181 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1182 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1183 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1184 | // Don't attempt to go past the end of the init list |
| 1185 | if (Index >= IList->getNumInits()) |
| 1186 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1187 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1188 | ElementEntity.setElementIndex(Index); |
| 1189 | |
| 1190 | QualType IType = IList->getInit(Index)->getType(); |
| 1191 | if (!IType->isVectorType()) { |
| 1192 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1193 | StructuredList, StructuredIndex); |
| 1194 | ++numEltsInit; |
| 1195 | } else { |
| 1196 | QualType VecType; |
| 1197 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1198 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1199 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1200 | if (IType->isExtVectorType()) |
| 1201 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1202 | else |
| 1203 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1204 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1205 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1206 | StructuredList, StructuredIndex); |
| 1207 | numEltsInit += numIElts; |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1212 | if (numEltsInit != maxElements) { |
| 1213 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1214 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1215 | diag::err_vector_incorrect_num_initializers) |
| 1216 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1217 | hadError = true; |
| 1218 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1221 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1222 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1223 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1225 | unsigned &Index, |
| 1226 | InitListExpr *StructuredList, |
| 1227 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1228 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1229 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1230 | // Check for the special-case of initializing an array with a string. |
| 1231 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1232 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1233 | SIF_None) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1234 | // We place the string literal directly into the resulting |
| 1235 | // initializer list. This is the only place where the structure |
| 1236 | // of the structured initializer list doesn't match exactly, |
| 1237 | // because doing so would involve allocating one character |
| 1238 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1239 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1240 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1241 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1242 | IList->getInit(Index)); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1243 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1244 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1245 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1246 | return; |
| 1247 | } |
| 1248 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1249 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1250 | // Check for VLAs; in standard C it would be possible to check this |
| 1251 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1252 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1253 | if (!VerifyOnly) |
| 1254 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1255 | diag::err_variable_object_no_init) |
| 1256 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1257 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1258 | ++Index; |
| 1259 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1260 | return; |
| 1261 | } |
| 1262 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1263 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1264 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1265 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1266 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1267 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1268 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1269 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1270 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1271 | maxElementsKnown = true; |
| 1272 | } |
| 1273 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1274 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1275 | while (Index < IList->getNumInits()) { |
| 1276 | Expr *Init = IList->getInit(Index); |
| 1277 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1278 | // If we're not the subobject that matches up with the '{' for |
| 1279 | // the designator, we shouldn't be handling the |
| 1280 | // designator. Return immediately. |
| 1281 | if (!SubobjectIsDesignatorContext) |
| 1282 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1283 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1284 | // Handle this designated initializer. elementIndex will be |
| 1285 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1286 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1287 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1288 | StructuredList, StructuredIndex, true, |
| 1289 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1290 | hadError = true; |
| 1291 | continue; |
| 1292 | } |
| 1293 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1294 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1295 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1296 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1297 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1298 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1299 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1300 | // If the array is of incomplete type, keep track of the number of |
| 1301 | // elements in the initializer. |
| 1302 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1303 | maxElements = elementIndex; |
| 1304 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1305 | continue; |
| 1306 | } |
| 1307 | |
| 1308 | // If we know the maximum number of elements, and we've already |
| 1309 | // hit it, stop consuming elements in the initializer list. |
| 1310 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1311 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1312 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1313 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1314 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1315 | Entity); |
| 1316 | // Check this element. |
| 1317 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1318 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1319 | ++elementIndex; |
| 1320 | |
| 1321 | // If the array is of incomplete type, keep track of the number of |
| 1322 | // elements in the initializer. |
| 1323 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1324 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1325 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1326 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1327 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1328 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1329 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1330 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1331 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1332 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1333 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1334 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1335 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1336 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1338 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1339 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1340 | if (!hadError && VerifyOnly) { |
| 1341 | // Check if there are any members of the array that get value-initialized. |
| 1342 | // If so, check if doing that is possible. |
| 1343 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1344 | if (maxElementsKnown && elementIndex < maxElements) |
| 1345 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1346 | SemaRef.Context, 0, Entity)); |
| 1347 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1350 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1351 | Expr *InitExpr, |
| 1352 | FieldDecl *Field, |
| 1353 | bool TopLevelObject) { |
| 1354 | // Handle GNU flexible array initializers. |
| 1355 | unsigned FlexArrayDiag; |
| 1356 | if (isa<InitListExpr>(InitExpr) && |
| 1357 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1358 | // Empty flexible array init always allowed as an extension |
| 1359 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1360 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1361 | // Disallow flexible array init in C++; it is not required for gcc |
| 1362 | // compatibility, and it needs work to IRGen correctly in general. |
| 1363 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1364 | } else if (!TopLevelObject) { |
| 1365 | // Disallow flexible array init on non-top-level object |
| 1366 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1367 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1368 | // Disallow flexible array init on anything which is not a variable. |
| 1369 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1370 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1371 | // Disallow flexible array init on local variables. |
| 1372 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1373 | } else { |
| 1374 | // Allow other cases. |
| 1375 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1376 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1377 | |
| 1378 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1379 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1380 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1381 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1382 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1383 | << Field; |
| 1384 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1385 | |
| 1386 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1387 | } |
| 1388 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1389 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1390 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1391 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1392 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1393 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1394 | unsigned &Index, |
| 1395 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1396 | unsigned &StructuredIndex, |
| 1397 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1398 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1399 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1400 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1401 | // confusion, we forgo checking the intializer for the entire record. |
| 1402 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 72ab277 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1403 | // Assume it was supposed to consume a single initializer. |
| 1404 | ++Index; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1405 | hadError = true; |
| 1406 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1407 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1408 | |
| 1409 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1410 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1411 | |
| 1412 | // If there's a default initializer, use it. |
| 1413 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1414 | if (VerifyOnly) |
| 1415 | return; |
| 1416 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1417 | Field != FieldEnd; ++Field) { |
| 1418 | if (Field->hasInClassInitializer()) { |
| 1419 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1420 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1421 | return; |
| 1422 | } |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | // Value-initialize the first named member of the union. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1427 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1428 | Field != FieldEnd; ++Field) { |
| 1429 | if (Field->getDeclName()) { |
| 1430 | if (VerifyOnly) |
| 1431 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1432 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1433 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1434 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1435 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1436 | } |
| 1437 | } |
| 1438 | return; |
| 1439 | } |
| 1440 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1441 | // If structDecl is a forward declaration, this loop won't do |
| 1442 | // anything except look at designated initializers; That's okay, |
| 1443 | // because an error should get printed out elsewhere. It might be |
| 1444 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1445 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1446 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1447 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1448 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1449 | while (Index < IList->getNumInits()) { |
| 1450 | Expr *Init = IList->getInit(Index); |
| 1451 | |
| 1452 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1453 | // If we're not the subobject that matches up with the '{' for |
| 1454 | // the designator, we shouldn't be handling the |
| 1455 | // designator. Return immediately. |
| 1456 | if (!SubobjectIsDesignatorContext) |
| 1457 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1459 | // Handle this designated initializer. Field will be updated to |
| 1460 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1461 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1462 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1463 | StructuredList, StructuredIndex, |
| 1464 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1465 | hadError = true; |
| 1466 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1467 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1468 | |
| 1469 | // Disable check for missing fields when designators are used. |
| 1470 | // This matches gcc behaviour. |
| 1471 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1472 | continue; |
| 1473 | } |
| 1474 | |
| 1475 | if (Field == FieldEnd) { |
| 1476 | // We've run out of fields. We're done. |
| 1477 | break; |
| 1478 | } |
| 1479 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1480 | // We've already initialized a member of a union. We're done. |
| 1481 | if (InitializedSomething && DeclType->isUnionType()) |
| 1482 | break; |
| 1483 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1484 | // If we've hit the flexible array member at the end, we're done. |
| 1485 | if (Field->getType()->isIncompleteArrayType()) |
| 1486 | break; |
| 1487 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1488 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1489 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1490 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1491 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1492 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1493 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1494 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1495 | bool InvalidUse; |
| 1496 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1497 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1498 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1499 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1500 | IList->getInit(Index)->getLocStart()); |
| 1501 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1502 | ++Index; |
| 1503 | ++Field; |
| 1504 | hadError = true; |
| 1505 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1506 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1507 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1508 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1509 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1510 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1511 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1512 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1513 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1514 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1515 | // Initialize the first field within the union. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1516 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1517 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1518 | |
| 1519 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1520 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1521 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1522 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1523 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1524 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1525 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1526 | // It is possible we have one or more unnamed bitfields remaining. |
| 1527 | // Find first (if any) named field and emit warning. |
| 1528 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1529 | it != end; ++it) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1530 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1531 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1532 | diag::warn_missing_field_initializers) << it->getName(); |
| 1533 | break; |
| 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1538 | // Check that any remaining fields can be value-initialized. |
| 1539 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1540 | !Field->getType()->isIncompleteArrayType()) { |
| 1541 | // FIXME: Should check for holes left by designated initializers too. |
| 1542 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1543 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1544 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1545 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1546 | } |
| 1547 | } |
| 1548 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1549 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1550 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1551 | return; |
| 1552 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1553 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1554 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1555 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1556 | ++Index; |
| 1557 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1560 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1561 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1562 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1563 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1564 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1565 | StructuredList, StructuredIndex); |
| 1566 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1567 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1568 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1569 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1570 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1571 | /// \brief Expand a field designator that refers to a member of an |
| 1572 | /// anonymous struct or union into a series of field designators that |
| 1573 | /// refers to the field within the appropriate subobject. |
| 1574 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1575 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1576 | DesignatedInitExpr *DIE, |
| 1577 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1578 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1579 | typedef DesignatedInitExpr::Designator Designator; |
| 1580 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1581 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1582 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1583 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1584 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1585 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1586 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1587 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1588 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1589 | else |
| 1590 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1591 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1592 | assert(isa<FieldDecl>(*PI)); |
| 1593 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | // Expand the current designator into the set of replacement |
| 1597 | // designators, so we have a full subobject path down to where the |
| 1598 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1599 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1600 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1601 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1602 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1603 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1604 | /// corresponds to FieldName. |
| 1605 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1606 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1607 | if (!FieldName) |
| 1608 | return 0; |
| 1609 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1610 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1611 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1612 | while (IndirectFieldDecl *IF = |
| 1613 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1614 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1615 | return IF; |
| 1616 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1617 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1618 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1621 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1622 | DesignatedInitExpr *DIE) { |
| 1623 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1624 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1625 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1626 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1627 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1628 | DIE->size(), IndexExprs, |
| 1629 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1630 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1631 | } |
| 1632 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1633 | namespace { |
| 1634 | |
| 1635 | // Callback to only accept typo corrections that are for field members of |
| 1636 | // the given struct or union. |
| 1637 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1638 | public: |
| 1639 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1640 | : Record(RD) {} |
| 1641 | |
| 1642 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1643 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1644 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1645 | } |
| 1646 | |
| 1647 | private: |
| 1648 | RecordDecl *Record; |
| 1649 | }; |
| 1650 | |
| 1651 | } |
| 1652 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1653 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1654 | /// |
| 1655 | /// Determines whether the designated initializer @p DIE, which |
| 1656 | /// resides at the given @p Index within the initializer list @p |
| 1657 | /// IList, is well-formed for a current object of type @p DeclType |
| 1658 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1660 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1661 | /// |
| 1662 | /// @param IList The initializer list in which this designated |
| 1663 | /// initializer occurs. |
| 1664 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1665 | /// @param DIE The designated initializer expression. |
| 1666 | /// |
| 1667 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1668 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1669 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1670 | /// into which the designation in @p DIE should refer. |
| 1671 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1672 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1673 | /// a field, this will be set to the field declaration corresponding |
| 1674 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1675 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1676 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1677 | /// DIE is an array designator or GNU array-range designator, this |
| 1678 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1679 | /// |
| 1680 | /// @param Index Index into @p IList where the designated initializer |
| 1681 | /// @p DIE occurs. |
| 1682 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1683 | /// @param StructuredList The initializer list expression that |
| 1684 | /// describes all of the subobject initializers in the order they'll |
| 1685 | /// actually be initialized. |
| 1686 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1687 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1689 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1690 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1691 | DesignatedInitExpr *DIE, |
| 1692 | unsigned DesigIdx, |
| 1693 | QualType &CurrentObjectType, |
| 1694 | RecordDecl::field_iterator *NextField, |
| 1695 | llvm::APSInt *NextElementIndex, |
| 1696 | unsigned &Index, |
| 1697 | InitListExpr *StructuredList, |
| 1698 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1699 | bool FinishSubobjectInit, |
| 1700 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1701 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1702 | // Check the actual initialization for the designated object type. |
| 1703 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1704 | |
| 1705 | // Temporarily remove the designator expression from the |
| 1706 | // initializer list that the child calls see, so that we don't try |
| 1707 | // to re-process the designator. |
| 1708 | unsigned OldIndex = Index; |
| 1709 | IList->setInit(OldIndex, DIE->getInit()); |
| 1710 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1711 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1712 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1713 | |
| 1714 | // Restore the designated initializer expression in the syntactic |
| 1715 | // form of the initializer list. |
| 1716 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1717 | DIE->setInit(IList->getInit(OldIndex)); |
| 1718 | IList->setInit(OldIndex, DIE); |
| 1719 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1720 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1723 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1724 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1725 | if (!VerifyOnly) { |
| 1726 | assert((IsFirstDesignator || StructuredList) && |
| 1727 | "Need a non-designated initializer list to start from"); |
| 1728 | |
| 1729 | // Determine the structural initializer list that corresponds to the |
| 1730 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1731 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1732 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1733 | StructuredList, StructuredIndex, |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1734 | SourceRange(D->getLocStart(), |
| 1735 | DIE->getLocEnd())); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1736 | assert(StructuredList && "Expected a structured initializer list"); |
| 1737 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1738 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1739 | if (D->isFieldDesignator()) { |
| 1740 | // C99 6.7.8p7: |
| 1741 | // |
| 1742 | // If a designator has the form |
| 1743 | // |
| 1744 | // . identifier |
| 1745 | // |
| 1746 | // then the current object (defined below) shall have |
| 1747 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1748 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1749 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1750 | if (!RT) { |
| 1751 | SourceLocation Loc = D->getDotLoc(); |
| 1752 | if (Loc.isInvalid()) |
| 1753 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1754 | if (!VerifyOnly) |
| 1755 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1756 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1757 | ++Index; |
| 1758 | return true; |
| 1759 | } |
| 1760 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1761 | // Note: we perform a linear search of the fields here, despite |
| 1762 | // the fact that we have a faster lookup method, because we always |
| 1763 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1764 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1765 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1766 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1768 | Field = RT->getDecl()->field_begin(), |
| 1769 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1770 | for (; Field != FieldEnd; ++Field) { |
| 1771 | if (Field->isUnnamedBitfield()) |
| 1772 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1773 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1774 | // If we find a field representing an anonymous field, look in the |
| 1775 | // IndirectFieldDecl that follow for the designated initializer. |
| 1776 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1777 | if (IndirectFieldDecl *IF = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1778 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1779 | // In verify mode, don't modify the original. |
| 1780 | if (VerifyOnly) |
| 1781 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1782 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1783 | D = DIE->getDesignator(DesigIdx); |
| 1784 | break; |
| 1785 | } |
| 1786 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1787 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1788 | break; |
| 1789 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1790 | break; |
| 1791 | |
| 1792 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1795 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1796 | if (VerifyOnly) { |
| 1797 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1798 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1799 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1800 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1801 | // There was no normal field in the struct with the designated |
| 1802 | // name. Perform another lookup for this name, which may find |
| 1803 | // something that we can't designate (e.g., a member function), |
| 1804 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1805 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1806 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1807 | FieldDecl *ReplacementField = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1808 | if (Lookup.empty()) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1809 | // Name lookup didn't find anything. Determine whether this |
| 1810 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1811 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Richard Smith | 2d67097 | 2013-08-17 00:46:16 +0000 | [diff] [blame^] | 1812 | if (TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1813 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
| 1814 | Sema::LookupMemberName, /*Scope=*/ 0, /*SS=*/ 0, Validator, |
| 1815 | RT->getDecl())) { |
| 1816 | SemaRef.diagnoseTypo( |
| 1817 | Corrected, |
| 1818 | SemaRef.PDiag(diag::err_field_designator_unknown_suggest) |
| 1819 | << FieldName << CurrentObjectType); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1820 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1821 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1822 | } else { |
| 1823 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1824 | << FieldName << CurrentObjectType; |
| 1825 | ++Index; |
| 1826 | return true; |
| 1827 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1828 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1829 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1830 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1831 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1832 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1833 | << FieldName; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1834 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1835 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1836 | ++Index; |
| 1837 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1838 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1839 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1840 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1841 | // The replacement field comes from typo correction; find it |
| 1842 | // in the list of fields. |
| 1843 | FieldIndex = 0; |
| 1844 | Field = RT->getDecl()->field_begin(); |
| 1845 | for (; Field != FieldEnd; ++Field) { |
| 1846 | if (Field->isUnnamedBitfield()) |
| 1847 | continue; |
| 1848 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1849 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1850 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1851 | break; |
| 1852 | |
| 1853 | ++FieldIndex; |
| 1854 | } |
| 1855 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1856 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1857 | |
| 1858 | // All of the fields of a union are located at the same place in |
| 1859 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1860 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1861 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1862 | if (!VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1863 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1864 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1865 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1866 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1867 | bool InvalidUse; |
| 1868 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1869 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1870 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1871 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1872 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1873 | ++Index; |
| 1874 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1875 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1876 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1877 | if (!VerifyOnly) { |
| 1878 | // Update the designator with the field declaration. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1879 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1881 | // Make sure that our non-designated initializer list has space |
| 1882 | // for a subobject corresponding to this field. |
| 1883 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1884 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1885 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1886 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1887 | // This designator names a flexible array member. |
| 1888 | if (Field->getType()->isIncompleteArrayType()) { |
| 1889 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1890 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1891 | // We can't designate an object within the flexible array |
| 1892 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1893 | if (!VerifyOnly) { |
| 1894 | DesignatedInitExpr::Designator *NextD |
| 1895 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1896 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1897 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1898 | << SourceRange(NextD->getLocStart(), |
| 1899 | DIE->getLocEnd()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1900 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1901 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1902 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1903 | Invalid = true; |
| 1904 | } |
| 1905 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1906 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1907 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1908 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1909 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1910 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1911 | diag::err_flexible_array_init_needs_braces) |
| 1912 | << DIE->getInit()->getSourceRange(); |
| 1913 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1914 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1915 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1916 | Invalid = true; |
| 1917 | } |
| 1918 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1919 | // Check GNU flexible array initializer. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1920 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1921 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1922 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1923 | |
| 1924 | if (Invalid) { |
| 1925 | ++Index; |
| 1926 | return true; |
| 1927 | } |
| 1928 | |
| 1929 | // Initialize the array. |
| 1930 | bool prevHadError = hadError; |
| 1931 | unsigned newStructuredIndex = FieldIndex; |
| 1932 | unsigned OldIndex = Index; |
| 1933 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1934 | |
| 1935 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1936 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1937 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1938 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1939 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1940 | IList->setInit(OldIndex, DIE); |
| 1941 | if (hadError && !prevHadError) { |
| 1942 | ++Field; |
| 1943 | ++FieldIndex; |
| 1944 | if (NextField) |
| 1945 | *NextField = Field; |
| 1946 | StructuredIndex = FieldIndex; |
| 1947 | return true; |
| 1948 | } |
| 1949 | } else { |
| 1950 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1951 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1952 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1953 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1954 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1955 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1956 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1957 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1958 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1959 | true, false)) |
| 1960 | return true; |
| 1961 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1962 | |
| 1963 | // Find the position of the next field to be initialized in this |
| 1964 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1965 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1966 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1967 | |
| 1968 | // If this the first designator, our caller will continue checking |
| 1969 | // the rest of this struct/class/union subobject. |
| 1970 | if (IsFirstDesignator) { |
| 1971 | if (NextField) |
| 1972 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1973 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1974 | return false; |
| 1975 | } |
| 1976 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1977 | if (!FinishSubobjectInit) |
| 1978 | return false; |
| 1979 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1980 | // We've already initialized something in the union; we're done. |
| 1981 | if (RT->getDecl()->isUnion()) |
| 1982 | return hadError; |
| 1983 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1984 | // Check the remaining fields within this class/struct/union subobject. |
| 1985 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1986 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1987 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1988 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1989 | return hadError && !prevHadError; |
| 1990 | } |
| 1991 | |
| 1992 | // C99 6.7.8p6: |
| 1993 | // |
| 1994 | // If a designator has the form |
| 1995 | // |
| 1996 | // [ constant-expression ] |
| 1997 | // |
| 1998 | // then the current object (defined below) shall have array |
| 1999 | // type and the expression shall be an integer constant |
| 2000 | // expression. If the array is of unknown size, any |
| 2001 | // nonnegative value is valid. |
| 2002 | // |
| 2003 | // Additionally, cope with the GNU extension that permits |
| 2004 | // designators of the form |
| 2005 | // |
| 2006 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2007 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2008 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2009 | if (!VerifyOnly) |
| 2010 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2011 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2012 | ++Index; |
| 2013 | return true; |
| 2014 | } |
| 2015 | |
| 2016 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2017 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2018 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2019 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2020 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2021 | DesignatedEndIndex = DesignatedStartIndex; |
| 2022 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2023 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2024 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2026 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2028 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2029 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2030 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2031 | // Codegen can't handle evaluating array range designators that have side |
| 2032 | // effects, because we replicate the AST value for each initialized element. |
| 2033 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2034 | // elements with something that has a side effect, so codegen can emit an |
| 2035 | // "error unsupported" error instead of miscompiling the app. |
| 2036 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2037 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2038 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2041 | if (isa<ConstantArrayType>(AT)) { |
| 2042 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2043 | DesignatedStartIndex |
| 2044 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2045 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2046 | DesignatedEndIndex |
| 2047 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2048 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2049 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2050 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2051 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2052 | diag::err_array_designator_too_large) |
| 2053 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2054 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2055 | ++Index; |
| 2056 | return true; |
| 2057 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2058 | } else { |
| 2059 | // Make sure the bit-widths and signedness match. |
| 2060 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2061 | DesignatedEndIndex |
| 2062 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2063 | else if (DesignatedStartIndex.getBitWidth() < |
| 2064 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2065 | DesignatedStartIndex |
| 2066 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2067 | DesignatedStartIndex.setIsUnsigned(true); |
| 2068 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2069 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2070 | |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2071 | if (!VerifyOnly && StructuredList->isStringLiteralInit()) { |
| 2072 | // We're modifying a string literal init; we have to decompose the string |
| 2073 | // so we can modify the individual characters. |
| 2074 | ASTContext &Context = SemaRef.Context; |
| 2075 | Expr *SubExpr = StructuredList->getInit(0)->IgnoreParens(); |
| 2076 | |
| 2077 | // Compute the character type |
| 2078 | QualType CharTy = AT->getElementType(); |
| 2079 | |
| 2080 | // Compute the type of the integer literals. |
| 2081 | QualType PromotedCharTy = CharTy; |
| 2082 | if (CharTy->isPromotableIntegerType()) |
| 2083 | PromotedCharTy = Context.getPromotedIntegerType(CharTy); |
| 2084 | unsigned PromotedCharTyWidth = Context.getTypeSize(PromotedCharTy); |
| 2085 | |
| 2086 | if (StringLiteral *SL = dyn_cast<StringLiteral>(SubExpr)) { |
| 2087 | // Get the length of the string. |
| 2088 | uint64_t StrLen = SL->getLength(); |
| 2089 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2090 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2091 | StructuredList->resizeInits(Context, StrLen); |
| 2092 | |
| 2093 | // Build a literal for each character in the string, and put them into |
| 2094 | // the init list. |
| 2095 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2096 | llvm::APInt CodeUnit(PromotedCharTyWidth, SL->getCodeUnit(i)); |
| 2097 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 81359b0 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2098 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2099 | if (CharTy != PromotedCharTy) |
| 2100 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
| 2101 | Init, 0, VK_RValue); |
| 2102 | StructuredList->updateInit(Context, i, Init); |
| 2103 | } |
| 2104 | } else { |
| 2105 | ObjCEncodeExpr *E = cast<ObjCEncodeExpr>(SubExpr); |
| 2106 | std::string Str; |
| 2107 | Context.getObjCEncodingForType(E->getEncodedType(), Str); |
| 2108 | |
| 2109 | // Get the length of the string. |
| 2110 | uint64_t StrLen = Str.size(); |
| 2111 | if (cast<ConstantArrayType>(AT)->getSize().ult(StrLen)) |
| 2112 | StrLen = cast<ConstantArrayType>(AT)->getSize().getZExtValue(); |
| 2113 | StructuredList->resizeInits(Context, StrLen); |
| 2114 | |
| 2115 | // Build a literal for each character in the string, and put them into |
| 2116 | // the init list. |
| 2117 | for (unsigned i = 0, e = StrLen; i != e; ++i) { |
| 2118 | llvm::APInt CodeUnit(PromotedCharTyWidth, Str[i]); |
| 2119 | Expr *Init = new (Context) IntegerLiteral( |
Eli Friedman | 81359b0 | 2013-06-11 22:26:34 +0000 | [diff] [blame] | 2120 | Context, CodeUnit, PromotedCharTy, SubExpr->getExprLoc()); |
Eli Friedman | 188ddb1 | 2013-06-11 21:48:11 +0000 | [diff] [blame] | 2121 | if (CharTy != PromotedCharTy) |
| 2122 | Init = ImplicitCastExpr::Create(Context, CharTy, CK_IntegralCast, |
| 2123 | Init, 0, VK_RValue); |
| 2124 | StructuredList->updateInit(Context, i, Init); |
| 2125 | } |
| 2126 | } |
| 2127 | } |
| 2128 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2129 | // Make sure that our non-designated initializer list has space |
| 2130 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2131 | if (!VerifyOnly && |
| 2132 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2134 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2135 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2136 | // Repeatedly perform subobject initializations in the range |
| 2137 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2138 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2139 | // Move to the next designator |
| 2140 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2141 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2142 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2143 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2144 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2146 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2147 | // Recurse to check later designated subobjects. |
| 2148 | QualType ElementType = AT->getElementType(); |
| 2149 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2150 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2151 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2152 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 2153 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2154 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2155 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2156 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2157 | return true; |
| 2158 | |
| 2159 | // Move to the next index in the array that we'll be initializing. |
| 2160 | ++DesignatedStartIndex; |
| 2161 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2162 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2163 | |
| 2164 | // If this the first designator, our caller will continue checking |
| 2165 | // the rest of this array subobject. |
| 2166 | if (IsFirstDesignator) { |
| 2167 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2168 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2169 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2170 | return false; |
| 2171 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2173 | if (!FinishSubobjectInit) |
| 2174 | return false; |
| 2175 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2176 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2177 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2178 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2179 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2180 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2181 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2184 | // Get the structured initializer list for a subobject of type |
| 2185 | // @p CurrentObjectType. |
| 2186 | InitListExpr * |
| 2187 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2188 | QualType CurrentObjectType, |
| 2189 | InitListExpr *StructuredList, |
| 2190 | unsigned StructuredIndex, |
| 2191 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2192 | if (VerifyOnly) |
| 2193 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2194 | Expr *ExistingInit = 0; |
| 2195 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2196 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2197 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2198 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2199 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2200 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2201 | return Result; |
| 2202 | |
| 2203 | if (ExistingInit) { |
| 2204 | // We are creating an initializer list that initializes the |
| 2205 | // subobjects of the current object, but there was already an |
| 2206 | // initialization that completely initialized the current |
| 2207 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2208 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2209 | // struct X { int a, b; }; |
| 2210 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2211 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2212 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2213 | // designated initializer re-initializes the whole |
| 2214 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2215 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2216 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2217 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2218 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2219 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2220 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2221 | << ExistingInit->getSourceRange(); |
| 2222 | } |
| 2223 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2225 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2226 | InitRange.getBegin(), None, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2227 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2228 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2229 | QualType ResultType = CurrentObjectType; |
| 2230 | if (!ResultType->isArrayType()) |
| 2231 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2232 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2233 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2234 | // Pre-allocate storage for the structured initializer list. |
| 2235 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2236 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2237 | bool GotNumInits = false; |
| 2238 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2239 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2240 | GotNumInits = true; |
| 2241 | } else if (Index < IList->getNumInits()) { |
| 2242 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2243 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2244 | GotNumInits = true; |
| 2245 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2246 | } |
| 2247 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2249 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2250 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2251 | NumElements = CAType->getSize().getZExtValue(); |
| 2252 | // Simple heuristic so that we don't allocate a very large |
| 2253 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2254 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2255 | NumElements = 0; |
| 2256 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2257 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2258 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2259 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2260 | RecordDecl *RDecl = RType->getDecl(); |
| 2261 | if (RDecl->isUnion()) |
| 2262 | NumElements = 1; |
| 2263 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2264 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2265 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2268 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2269 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2270 | // Link this new initializer list into the structured initializer |
| 2271 | // lists. |
| 2272 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2273 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2274 | else { |
| 2275 | Result->setSyntacticForm(IList); |
| 2276 | SyntacticToSemantic[IList] = Result; |
| 2277 | } |
| 2278 | |
| 2279 | return Result; |
| 2280 | } |
| 2281 | |
| 2282 | /// Update the initializer at index @p StructuredIndex within the |
| 2283 | /// structured initializer list to the value @p expr. |
| 2284 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2285 | unsigned &StructuredIndex, |
| 2286 | Expr *expr) { |
| 2287 | // No structured initializer list to update |
| 2288 | if (!StructuredList) |
| 2289 | return; |
| 2290 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2291 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2292 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2293 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2294 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2295 | diag::warn_initializer_overrides) |
| 2296 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2297 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2298 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2299 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2300 | << PrevInit->getSourceRange(); |
| 2301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2302 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2303 | ++StructuredIndex; |
| 2304 | } |
| 2305 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2306 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2307 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2308 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2309 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2310 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2311 | /// added, on success. If everything went okay, Value will receive the |
| 2312 | /// value of the constant expression. |
| 2313 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2314 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2315 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2316 | |
| 2317 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2318 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2319 | if (Result.isInvalid()) |
| 2320 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2321 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2322 | if (Value.isSigned() && Value.isNegative()) |
| 2323 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2324 | << Value.toString(10) << Index->getSourceRange(); |
| 2325 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2326 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2327 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2330 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2331 | SourceLocation Loc, |
| 2332 | bool GNUSyntax, |
| 2333 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2334 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2335 | |
| 2336 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2337 | SmallVector<ASTDesignator, 32> Designators; |
| 2338 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2339 | |
| 2340 | // Build designators and check array designator expressions. |
| 2341 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2342 | const Designator &D = Desig.getDesignator(Idx); |
| 2343 | switch (D.getKind()) { |
| 2344 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2345 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2346 | D.getFieldLoc())); |
| 2347 | break; |
| 2348 | |
| 2349 | case Designator::ArrayDesignator: { |
| 2350 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2351 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2352 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2353 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2354 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2355 | Invalid = true; |
| 2356 | else { |
| 2357 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2358 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2359 | D.getRBracketLoc())); |
| 2360 | InitExpressions.push_back(Index); |
| 2361 | } |
| 2362 | break; |
| 2363 | } |
| 2364 | |
| 2365 | case Designator::ArrayRangeDesignator: { |
| 2366 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2367 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2368 | llvm::APSInt StartValue; |
| 2369 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2370 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2371 | StartIndex->isValueDependent(); |
| 2372 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2373 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2374 | if (!StartDependent) |
| 2375 | StartIndex = |
| 2376 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2377 | if (!EndDependent) |
| 2378 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2379 | |
| 2380 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2381 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2382 | else { |
| 2383 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2384 | if (StartDependent || EndDependent) { |
| 2385 | // Nothing to compute. |
| 2386 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2387 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2388 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2389 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2390 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2391 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2392 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2393 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2394 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2395 | Invalid = true; |
| 2396 | } else { |
| 2397 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2398 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2399 | D.getEllipsisLoc(), |
| 2400 | D.getRBracketLoc())); |
| 2401 | InitExpressions.push_back(StartIndex); |
| 2402 | InitExpressions.push_back(EndIndex); |
| 2403 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2404 | } |
| 2405 | break; |
| 2406 | } |
| 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | if (Invalid || Init.isInvalid()) |
| 2411 | return ExprError(); |
| 2412 | |
| 2413 | // Clear out the expressions within the designation. |
| 2414 | Desig.ClearExprs(*this); |
| 2415 | |
| 2416 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2417 | = DesignatedInitExpr::Create(Context, |
| 2418 | Designators.data(), Designators.size(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2419 | InitExpressions, Loc, GNUSyntax, |
| 2420 | Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2421 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2422 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2423 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2424 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2425 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2426 | return Owned(DIE); |
| 2427 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2428 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2429 | //===----------------------------------------------------------------------===// |
| 2430 | // Initialization entity |
| 2431 | //===----------------------------------------------------------------------===// |
| 2432 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2433 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2434 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2435 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2436 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2437 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2438 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2439 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2440 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2441 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2442 | Type = VT->getElementType(); |
| 2443 | } else { |
| 2444 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2445 | assert(CT && "Unexpected type"); |
| 2446 | Kind = EK_ComplexElement; |
| 2447 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2448 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Benjamin Kramer | 4c7736e | 2013-07-24 15:28:33 +0000 | [diff] [blame] | 2451 | InitializedEntity |
| 2452 | InitializedEntity::InitializeBase(ASTContext &Context, |
| 2453 | const CXXBaseSpecifier *Base, |
| 2454 | bool IsInheritedVirtualBase) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2455 | InitializedEntity Result; |
| 2456 | Result.Kind = EK_Base; |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2457 | Result.Parent = 0; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2458 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2459 | if (IsInheritedVirtualBase) |
| 2460 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2461 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2462 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2463 | return Result; |
| 2464 | } |
| 2465 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2466 | DeclarationName InitializedEntity::getName() const { |
| 2467 | switch (getKind()) { |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2468 | case EK_Parameter: |
| 2469 | case EK_Parameter_CF_Audited: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2470 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2471 | return (D ? D->getDeclName() : DeclarationName()); |
| 2472 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2473 | |
| 2474 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2475 | case EK_Member: |
| 2476 | return VariableOrMember->getDeclName(); |
| 2477 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2478 | case EK_LambdaCapture: |
| 2479 | return Capture.Var->getDeclName(); |
| 2480 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2481 | case EK_Result: |
| 2482 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2483 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2484 | case EK_Temporary: |
| 2485 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2486 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2487 | case EK_ArrayElement: |
| 2488 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2489 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2490 | case EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2491 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2492 | case EK_RelatedResult: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2493 | return DeclarationName(); |
| 2494 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2495 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2496 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2499 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2500 | switch (getKind()) { |
| 2501 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2502 | case EK_Member: |
| 2503 | return VariableOrMember; |
| 2504 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2505 | case EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2506 | case EK_Parameter_CF_Audited: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2507 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2508 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2509 | case EK_Result: |
| 2510 | case EK_Exception: |
| 2511 | case EK_New: |
| 2512 | case EK_Temporary: |
| 2513 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2514 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2515 | case EK_ArrayElement: |
| 2516 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2517 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2518 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2519 | case EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2520 | case EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2521 | case EK_RelatedResult: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2522 | return 0; |
| 2523 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2524 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2525 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2528 | bool InitializedEntity::allowsNRVO() const { |
| 2529 | switch (getKind()) { |
| 2530 | case EK_Result: |
| 2531 | case EK_Exception: |
| 2532 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2533 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2534 | case EK_Variable: |
| 2535 | case EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2536 | case EK_Parameter_CF_Audited: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2537 | case EK_Member: |
| 2538 | case EK_New: |
| 2539 | case EK_Temporary: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2540 | case EK_CompoundLiteralInit: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2541 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2542 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2543 | case EK_ArrayElement: |
| 2544 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2545 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2546 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2547 | case EK_LambdaCapture: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2548 | case EK_RelatedResult: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2549 | break; |
| 2550 | } |
| 2551 | |
| 2552 | return false; |
| 2553 | } |
| 2554 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2555 | unsigned InitializedEntity::dumpImpl(raw_ostream &OS) const { |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 2556 | assert(getParent() != this); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2557 | unsigned Depth = getParent() ? getParent()->dumpImpl(OS) : 0; |
| 2558 | for (unsigned I = 0; I != Depth; ++I) |
| 2559 | OS << "`-"; |
| 2560 | |
| 2561 | switch (getKind()) { |
| 2562 | case EK_Variable: OS << "Variable"; break; |
| 2563 | case EK_Parameter: OS << "Parameter"; break; |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2564 | case EK_Parameter_CF_Audited: OS << "CF audited function Parameter"; |
| 2565 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2566 | case EK_Result: OS << "Result"; break; |
| 2567 | case EK_Exception: OS << "Exception"; break; |
| 2568 | case EK_Member: OS << "Member"; break; |
| 2569 | case EK_New: OS << "New"; break; |
| 2570 | case EK_Temporary: OS << "Temporary"; break; |
| 2571 | case EK_CompoundLiteralInit: OS << "CompoundLiteral";break; |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 2572 | case EK_RelatedResult: OS << "RelatedResult"; break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 2573 | case EK_Base: OS << "Base"; break; |
| 2574 | case EK_Delegating: OS << "Delegating"; break; |
| 2575 | case EK_ArrayElement: OS << "ArrayElement " << Index; break; |
| 2576 | case EK_VectorElement: OS << "VectorElement " << Index; break; |
| 2577 | case EK_ComplexElement: OS << "ComplexElement " << Index; break; |
| 2578 | case EK_BlockElement: OS << "Block"; break; |
| 2579 | case EK_LambdaCapture: |
| 2580 | OS << "LambdaCapture "; |
| 2581 | getCapturedVar()->printName(OS); |
| 2582 | break; |
| 2583 | } |
| 2584 | |
| 2585 | if (Decl *D = getDecl()) { |
| 2586 | OS << " "; |
| 2587 | cast<NamedDecl>(D)->printQualifiedName(OS); |
| 2588 | } |
| 2589 | |
| 2590 | OS << " '" << getType().getAsString() << "'\n"; |
| 2591 | |
| 2592 | return Depth + 1; |
| 2593 | } |
| 2594 | |
| 2595 | void InitializedEntity::dump() const { |
| 2596 | dumpImpl(llvm::errs()); |
| 2597 | } |
| 2598 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2599 | //===----------------------------------------------------------------------===// |
| 2600 | // Initialization sequence |
| 2601 | //===----------------------------------------------------------------------===// |
| 2602 | |
| 2603 | void InitializationSequence::Step::Destroy() { |
| 2604 | switch (Kind) { |
| 2605 | case SK_ResolveAddressOfOverloadedFunction: |
| 2606 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2607 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2608 | case SK_CastDerivedToBaseLValue: |
| 2609 | case SK_BindReference: |
| 2610 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2611 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2612 | case SK_UserConversion: |
| 2613 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2614 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2615 | case SK_QualificationConversionLValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2616 | case SK_LValueToRValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2617 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2618 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2619 | case SK_UnwrapInitList: |
| 2620 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2621 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2622 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2623 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2624 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2625 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2626 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2627 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2628 | case SK_PassByIndirectCopyRestore: |
| 2629 | case SK_PassByIndirectRestore: |
| 2630 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2631 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2632 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2633 | case SK_OCLZeroEvent: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2634 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2635 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2636 | case SK_ConversionSequence: |
| 2637 | delete ICS; |
| 2638 | } |
| 2639 | } |
| 2640 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2641 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2642 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2646 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2647 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2648 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2649 | switch (getFailureKind()) { |
| 2650 | case FK_TooManyInitsForReference: |
| 2651 | case FK_ArrayNeedsInitList: |
| 2652 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 2653 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 2654 | case FK_NarrowStringIntoWideCharArray: |
| 2655 | case FK_WideStringIntoCharArray: |
| 2656 | case FK_IncompatWideStringIntoWideChar: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2657 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2658 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2659 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2660 | case FK_RValueReferenceBindingToLValue: |
| 2661 | case FK_ReferenceInitDropsQualifiers: |
| 2662 | case FK_ReferenceInitFailed: |
| 2663 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2664 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2665 | case FK_TooManyInitsForScalar: |
| 2666 | case FK_ReferenceBindingToInitList: |
| 2667 | case FK_InitListBadDestinationType: |
| 2668 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2669 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2670 | case FK_ArrayTypeMismatch: |
| 2671 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2672 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2673 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2674 | case FK_PlaceholderType: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2675 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2676 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2677 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2678 | case FK_ReferenceInitOverloadFailed: |
| 2679 | case FK_UserConversionOverloadFailed: |
| 2680 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2681 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2682 | return FailedOverloadResult == OR_Ambiguous; |
| 2683 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2684 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2685 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2686 | } |
| 2687 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2688 | bool InitializationSequence::isConstructorInitialization() const { |
| 2689 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2690 | } |
| 2691 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2692 | void |
| 2693 | InitializationSequence |
| 2694 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2695 | DeclAccessPair Found, |
| 2696 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2697 | Step S; |
| 2698 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2699 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2700 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2701 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2702 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2703 | Steps.push_back(S); |
| 2704 | } |
| 2705 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2706 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2707 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2708 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2709 | switch (VK) { |
| 2710 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2711 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2712 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2713 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2714 | S.Type = BaseType; |
| 2715 | Steps.push_back(S); |
| 2716 | } |
| 2717 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2718 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2719 | bool BindingTemporary) { |
| 2720 | Step S; |
| 2721 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2722 | S.Type = T; |
| 2723 | Steps.push_back(S); |
| 2724 | } |
| 2725 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2726 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2727 | Step S; |
| 2728 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2729 | S.Type = T; |
| 2730 | Steps.push_back(S); |
| 2731 | } |
| 2732 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2733 | void |
| 2734 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2735 | DeclAccessPair FoundDecl, |
| 2736 | QualType T, |
| 2737 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2738 | Step S; |
| 2739 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2740 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2741 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2742 | S.Function.Function = Function; |
| 2743 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2744 | Steps.push_back(S); |
| 2745 | } |
| 2746 | |
| 2747 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2748 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2749 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2750 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2751 | switch (VK) { |
| 2752 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2753 | S.Kind = SK_QualificationConversionRValue; |
| 2754 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2755 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2756 | S.Kind = SK_QualificationConversionXValue; |
| 2757 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2758 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2759 | S.Kind = SK_QualificationConversionLValue; |
| 2760 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2761 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2762 | S.Type = Ty; |
| 2763 | Steps.push_back(S); |
| 2764 | } |
| 2765 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2766 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2767 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2768 | |
| 2769 | Step S; |
| 2770 | S.Kind = SK_LValueToRValue; |
| 2771 | S.Type = Ty; |
| 2772 | Steps.push_back(S); |
| 2773 | } |
| 2774 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2775 | void InitializationSequence::AddConversionSequenceStep( |
| 2776 | const ImplicitConversionSequence &ICS, |
| 2777 | QualType T) { |
| 2778 | Step S; |
| 2779 | S.Kind = SK_ConversionSequence; |
| 2780 | S.Type = T; |
| 2781 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2782 | Steps.push_back(S); |
| 2783 | } |
| 2784 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2785 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2786 | Step S; |
| 2787 | S.Kind = SK_ListInitialization; |
| 2788 | S.Type = T; |
| 2789 | Steps.push_back(S); |
| 2790 | } |
| 2791 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2792 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2793 | InitializationSequence |
| 2794 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2795 | AccessSpecifier Access, |
| 2796 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2797 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2798 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2799 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2800 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2801 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2802 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2803 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2804 | S.Function.Function = Constructor; |
| 2805 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2806 | Steps.push_back(S); |
| 2807 | } |
| 2808 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2809 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2810 | Step S; |
| 2811 | S.Kind = SK_ZeroInitialization; |
| 2812 | S.Type = T; |
| 2813 | Steps.push_back(S); |
| 2814 | } |
| 2815 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2816 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2817 | Step S; |
| 2818 | S.Kind = SK_CAssignment; |
| 2819 | S.Type = T; |
| 2820 | Steps.push_back(S); |
| 2821 | } |
| 2822 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2823 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2824 | Step S; |
| 2825 | S.Kind = SK_StringInit; |
| 2826 | S.Type = T; |
| 2827 | Steps.push_back(S); |
| 2828 | } |
| 2829 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2830 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2831 | Step S; |
| 2832 | S.Kind = SK_ObjCObjectConversion; |
| 2833 | S.Type = T; |
| 2834 | Steps.push_back(S); |
| 2835 | } |
| 2836 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2837 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2838 | Step S; |
| 2839 | S.Kind = SK_ArrayInit; |
| 2840 | S.Type = T; |
| 2841 | Steps.push_back(S); |
| 2842 | } |
| 2843 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2844 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2845 | Step S; |
| 2846 | S.Kind = SK_ParenthesizedArrayInit; |
| 2847 | S.Type = T; |
| 2848 | Steps.push_back(S); |
| 2849 | } |
| 2850 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2851 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2852 | bool shouldCopy) { |
| 2853 | Step s; |
| 2854 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2855 | : SK_PassByIndirectRestore); |
| 2856 | s.Type = type; |
| 2857 | Steps.push_back(s); |
| 2858 | } |
| 2859 | |
| 2860 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2861 | Step S; |
| 2862 | S.Kind = SK_ProduceObjCObject; |
| 2863 | S.Type = T; |
| 2864 | Steps.push_back(S); |
| 2865 | } |
| 2866 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2867 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2868 | Step S; |
| 2869 | S.Kind = SK_StdInitializerList; |
| 2870 | S.Type = T; |
| 2871 | Steps.push_back(S); |
| 2872 | } |
| 2873 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2874 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 2875 | Step S; |
| 2876 | S.Kind = SK_OCLSamplerInit; |
| 2877 | S.Type = T; |
| 2878 | Steps.push_back(S); |
| 2879 | } |
| 2880 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2881 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 2882 | Step S; |
| 2883 | S.Kind = SK_OCLZeroEvent; |
| 2884 | S.Type = T; |
| 2885 | Steps.push_back(S); |
| 2886 | } |
| 2887 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2888 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2889 | InitListExpr *Syntactic) { |
| 2890 | assert(Syntactic->getNumInits() == 1 && |
| 2891 | "Can only rewrap trivial init lists."); |
| 2892 | Step S; |
| 2893 | S.Kind = SK_UnwrapInitList; |
| 2894 | S.Type = Syntactic->getInit(0)->getType(); |
| 2895 | Steps.insert(Steps.begin(), S); |
| 2896 | |
| 2897 | S.Kind = SK_RewrapInitList; |
| 2898 | S.Type = T; |
| 2899 | S.WrappingSyntacticList = Syntactic; |
| 2900 | Steps.push_back(S); |
| 2901 | } |
| 2902 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2903 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2904 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2905 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2906 | this->Failure = Failure; |
| 2907 | this->FailedOverloadResult = Result; |
| 2908 | } |
| 2909 | |
| 2910 | //===----------------------------------------------------------------------===// |
| 2911 | // Attempt initialization |
| 2912 | //===----------------------------------------------------------------------===// |
| 2913 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2914 | static void MaybeProduceObjCObject(Sema &S, |
| 2915 | InitializationSequence &Sequence, |
| 2916 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2917 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2918 | |
| 2919 | /// When initializing a parameter, produce the value if it's marked |
| 2920 | /// __attribute__((ns_consumed)). |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 2921 | if (Entity.isParameterKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2922 | if (!Entity.isParameterConsumed()) |
| 2923 | return; |
| 2924 | |
| 2925 | assert(Entity.getType()->isObjCRetainableType() && |
| 2926 | "consuming an object of unretainable type?"); |
| 2927 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2928 | |
| 2929 | /// When initializing a return value, if the return type is a |
| 2930 | /// retainable type, then returns need to immediately retain the |
| 2931 | /// object. If an autorelease is required, it will be done at the |
| 2932 | /// last instant. |
| 2933 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2934 | if (!Entity.getType()->isObjCRetainableType()) |
| 2935 | return; |
| 2936 | |
| 2937 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2938 | } |
| 2939 | } |
| 2940 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2941 | static void TryListInitialization(Sema &S, |
| 2942 | const InitializedEntity &Entity, |
| 2943 | const InitializationKind &Kind, |
| 2944 | InitListExpr *InitList, |
| 2945 | InitializationSequence &Sequence); |
| 2946 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2947 | /// \brief When initializing from init list via constructor, handle |
| 2948 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2949 | /// |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2950 | /// \return true if we have handled initialization of an object of type |
| 2951 | /// std::initializer_list<T>, false otherwise. |
| 2952 | static bool TryInitializerListConstruction(Sema &S, |
| 2953 | InitListExpr *List, |
| 2954 | QualType DestType, |
| 2955 | InitializationSequence &Sequence) { |
| 2956 | QualType E; |
| 2957 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2958 | return false; |
| 2959 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2960 | if (S.RequireCompleteType(List->getExprLoc(), E, 0)) { |
| 2961 | Sequence.setIncompleteTypeFailure(E); |
| 2962 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2963 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2964 | |
| 2965 | // Try initializing a temporary array from the init list. |
| 2966 | QualType ArrayType = S.Context.getConstantArrayType( |
| 2967 | E.withConst(), llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2968 | List->getNumInits()), |
| 2969 | clang::ArrayType::Normal, 0); |
| 2970 | InitializedEntity HiddenArray = |
| 2971 | InitializedEntity::InitializeTemporary(ArrayType); |
| 2972 | InitializationKind Kind = |
| 2973 | InitializationKind::CreateDirectList(List->getExprLoc()); |
| 2974 | TryListInitialization(S, HiddenArray, Kind, List, Sequence); |
| 2975 | if (Sequence) |
| 2976 | Sequence.AddStdInitializerListConstructionStep(DestType); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2977 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2978 | } |
| 2979 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2980 | static OverloadingResult |
| 2981 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2982 | MultiExprArg Args, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2983 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2984 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2985 | OverloadCandidateSet::iterator &Best, |
| 2986 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2987 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2988 | CandidateSet.clear(); |
| 2989 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2990 | for (ArrayRef<NamedDecl *>::iterator |
| 2991 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2992 | NamedDecl *D = *Con; |
| 2993 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2994 | bool SuppressUserConversions = false; |
| 2995 | |
| 2996 | // Find the constructor (which may be a template). |
| 2997 | CXXConstructorDecl *Constructor = 0; |
| 2998 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 2999 | if (ConstructorTmpl) |
| 3000 | Constructor = cast<CXXConstructorDecl>( |
| 3001 | ConstructorTmpl->getTemplatedDecl()); |
| 3002 | else { |
| 3003 | Constructor = cast<CXXConstructorDecl>(D); |
| 3004 | |
| 3005 | // If we're performing copy initialization using a copy constructor, we |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3006 | // suppress user-defined conversions on the arguments. We do the same for |
| 3007 | // move constructors. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3008 | if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) && |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3009 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3010 | SuppressUserConversions = true; |
| 3011 | } |
| 3012 | |
| 3013 | if (!Constructor->isInvalidDecl() && |
| 3014 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3015 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3016 | if (ConstructorTmpl) |
| 3017 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3018 | /*ExplicitArgs*/ 0, Args, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3019 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3020 | else { |
| 3021 | // C++ [over.match.copy]p1: |
| 3022 | // - When initializing a temporary to be bound to the first parameter |
| 3023 | // of a constructor that takes a reference to possibly cv-qualified |
| 3024 | // T as its first argument, called with a single argument in the |
| 3025 | // context of direct-initialization, explicit conversion functions |
| 3026 | // are also considered. |
| 3027 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3028 | Args.size() == 1 && |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3029 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3030 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3031 | SuppressUserConversions, |
| 3032 | /*PartialOverloading=*/false, |
| 3033 | /*AllowExplicit=*/AllowExplicitConv); |
| 3034 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | // Perform overload resolution and return the result. |
| 3039 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 3040 | } |
| 3041 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3042 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 3043 | /// enumerates the constructors of the initialized entity and performs overload |
| 3044 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3045 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3046 | /// class type. |
| 3047 | static void TryConstructorInitialization(Sema &S, |
| 3048 | const InitializedEntity &Entity, |
| 3049 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3050 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3051 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3052 | bool InitListSyntax = false) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3053 | assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3054 | "InitListSyntax must come with a single initializer list argument."); |
| 3055 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3056 | // The type we're constructing needs to be complete. |
| 3057 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3058 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3059 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
| 3062 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 3063 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 3064 | CXXRecordDecl *DestRecordDecl |
| 3065 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 3066 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 3067 | // Build the candidate set directly in the initialization sequence |
| 3068 | // structure, so that it will persist if we fail. |
| 3069 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3070 | |
| 3071 | // Determine whether we are allowed to call explicit constructors or |
| 3072 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3073 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3074 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3075 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3076 | // - Otherwise, if T is a class type, constructors are considered. The |
| 3077 | // applicable constructors are enumerated, and the best one is chosen |
| 3078 | // through overload resolution. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3079 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3080 | // The container holding the constructors can under certain conditions |
| 3081 | // be changed while iterating (e.g. because of deserialization). |
| 3082 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3083 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3084 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3085 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3086 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3087 | bool AsInitializerList = false; |
| 3088 | |
| 3089 | // C++11 [over.match.list]p1: |
| 3090 | // When objects of non-aggregate type T are list-initialized, overload |
| 3091 | // resolution selects the constructor in two phases: |
| 3092 | // - Initially, the candidate functions are the initializer-list |
| 3093 | // constructors of the class T and the argument list consists of the |
| 3094 | // initializer list as a single argument. |
| 3095 | if (InitListSyntax) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3096 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3097 | AsInitializerList = true; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3098 | |
| 3099 | // If the initializer list has no elements and T has a default constructor, |
| 3100 | // the first phase is omitted. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3101 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3102 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3103 | CandidateSet, Ctors, Best, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3104 | CopyInitialization, AllowExplicit, |
| 3105 | /*OnlyListConstructor=*/true, |
| 3106 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3107 | |
| 3108 | // Time to unwrap the init list. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3109 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | // C++11 [over.match.list]p1: |
| 3113 | // - If no viable initializer-list constructor is found, overload resolution |
| 3114 | // is performed again, where the candidate functions are all the |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3115 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3116 | // elements of the initializer list. |
| 3117 | if (Result == OR_No_Viable_Function) { |
| 3118 | AsInitializerList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3119 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3120 | CandidateSet, Ctors, Best, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3121 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3122 | /*OnlyListConstructors=*/false, |
| 3123 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3124 | } |
| 3125 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3126 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3127 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3128 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3129 | Result); |
| 3130 | return; |
| 3131 | } |
| 3132 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3133 | // C++11 [dcl.init]p6: |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3134 | // If a program calls for the default initialization of an object |
| 3135 | // of a const-qualified type T, T shall be a class type with a |
| 3136 | // user-provided default constructor. |
| 3137 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3138 | Entity.getType().isConstQualified() && |
Aaron Ballman | 5121781 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 3139 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3140 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3141 | return; |
| 3142 | } |
| 3143 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3144 | // C++11 [over.match.list]p1: |
| 3145 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3146 | // initializer is ill-formed. |
| 3147 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 3148 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 3149 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3150 | return; |
| 3151 | } |
| 3152 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3153 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3154 | // subsumed by the initialization. |
| 3155 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3156 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 3157 | Best->FoundDecl.getAccess(), |
| 3158 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3159 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3162 | static bool |
| 3163 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3164 | Expr *Initializer, |
| 3165 | QualType &SourceType, |
| 3166 | QualType &UnqualifiedSourceType, |
| 3167 | QualType UnqualifiedTargetType, |
| 3168 | InitializationSequence &Sequence) { |
| 3169 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3170 | S.Context.OverloadTy) { |
| 3171 | DeclAccessPair Found; |
| 3172 | bool HadMultipleCandidates = false; |
| 3173 | if (FunctionDecl *Fn |
| 3174 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3175 | UnqualifiedTargetType, |
| 3176 | false, Found, |
| 3177 | &HadMultipleCandidates)) { |
| 3178 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3179 | HadMultipleCandidates); |
| 3180 | SourceType = Fn->getType(); |
| 3181 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3182 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3183 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3184 | return true; |
| 3185 | } |
| 3186 | } |
| 3187 | return false; |
| 3188 | } |
| 3189 | |
| 3190 | static void TryReferenceInitializationCore(Sema &S, |
| 3191 | const InitializedEntity &Entity, |
| 3192 | const InitializationKind &Kind, |
| 3193 | Expr *Initializer, |
| 3194 | QualType cv1T1, QualType T1, |
| 3195 | Qualifiers T1Quals, |
| 3196 | QualType cv2T2, QualType T2, |
| 3197 | Qualifiers T2Quals, |
| 3198 | InitializationSequence &Sequence); |
| 3199 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3200 | static void TryValueInitialization(Sema &S, |
| 3201 | const InitializedEntity &Entity, |
| 3202 | const InitializationKind &Kind, |
| 3203 | InitializationSequence &Sequence, |
| 3204 | InitListExpr *InitList = 0); |
| 3205 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3206 | /// \brief Attempt list initialization of a reference. |
| 3207 | static void TryReferenceListInitialization(Sema &S, |
| 3208 | const InitializedEntity &Entity, |
| 3209 | const InitializationKind &Kind, |
| 3210 | InitListExpr *InitList, |
Richard Smith | b6e3808 | 2013-06-08 00:02:08 +0000 | [diff] [blame] | 3211 | InitializationSequence &Sequence) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3212 | // First, catch C++03 where this isn't possible. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3213 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3214 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3215 | return; |
| 3216 | } |
| 3217 | |
| 3218 | QualType DestType = Entity.getType(); |
| 3219 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3220 | Qualifiers T1Quals; |
| 3221 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3222 | |
| 3223 | // Reference initialization via an initializer list works thus: |
| 3224 | // If the initializer list consists of a single element that is |
| 3225 | // reference-related to the referenced type, bind directly to that element |
| 3226 | // (possibly creating temporaries). |
| 3227 | // Otherwise, initialize a temporary with the initializer list and |
| 3228 | // bind to that. |
| 3229 | if (InitList->getNumInits() == 1) { |
| 3230 | Expr *Initializer = InitList->getInit(0); |
| 3231 | QualType cv2T2 = Initializer->getType(); |
| 3232 | Qualifiers T2Quals; |
| 3233 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3234 | |
| 3235 | // If this fails, creating a temporary wouldn't work either. |
| 3236 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3237 | T1, Sequence)) |
| 3238 | return; |
| 3239 | |
| 3240 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3241 | bool dummy1, dummy2, dummy3; |
| 3242 | Sema::ReferenceCompareResult RefRelationship |
| 3243 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3244 | dummy2, dummy3); |
| 3245 | if (RefRelationship >= Sema::Ref_Related) { |
| 3246 | // Try to bind the reference here. |
| 3247 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3248 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3249 | if (Sequence) |
| 3250 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3251 | return; |
| 3252 | } |
Richard Smith | 02d65ee | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3253 | |
| 3254 | // Update the initializer if we've resolved an overloaded function. |
| 3255 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3256 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | // Not reference-related. Create a temporary and bind to that. |
| 3260 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3261 | |
| 3262 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3263 | if (Sequence) { |
| 3264 | if (DestType->isRValueReferenceType() || |
| 3265 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3266 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3267 | else |
| 3268 | Sequence.SetFailed( |
| 3269 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3270 | } |
| 3271 | } |
| 3272 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3273 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3274 | static void TryListInitialization(Sema &S, |
| 3275 | const InitializedEntity &Entity, |
| 3276 | const InitializationKind &Kind, |
| 3277 | InitListExpr *InitList, |
| 3278 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3279 | QualType DestType = Entity.getType(); |
| 3280 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3281 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3282 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3283 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3284 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3285 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3286 | return; |
| 3287 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3288 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3289 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3290 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3291 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3292 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3293 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3294 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3295 | return; |
| 3296 | } |
| 3297 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3298 | // C++11 [dcl.init.list]p3: |
| 3299 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3300 | if (!DestType->isAggregateType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3301 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3302 | // - Otherwise, if the initializer list has no elements and T is a |
| 3303 | // class type with a default constructor, the object is |
| 3304 | // value-initialized. |
| 3305 | if (InitList->getNumInits() == 0) { |
| 3306 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3307 | if (RD->hasDefaultConstructor()) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3308 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3309 | return; |
| 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3314 | // an initializer_list object constructed [...] |
| 3315 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3316 | return; |
| 3317 | |
| 3318 | // - Otherwise, if T is a class type, constructors are considered. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3319 | Expr *InitListAsExpr = InitList; |
| 3320 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3321 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3322 | } else |
| 3323 | Sequence.SetFailed( |
| 3324 | InitializationSequence::FK_InitListBadDestinationType); |
| 3325 | return; |
| 3326 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3329 | InitListChecker CheckInitList(S, Entity, InitList, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 3330 | DestType, /*VerifyOnly=*/true); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3331 | if (CheckInitList.HadError()) { |
| 3332 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3333 | return; |
| 3334 | } |
| 3335 | |
| 3336 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3337 | Sequence.AddListInitializationStep(DestType); |
| 3338 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3339 | |
| 3340 | /// \brief Try a reference initialization that involves calling a conversion |
| 3341 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3342 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3343 | const InitializedEntity &Entity, |
| 3344 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3345 | Expr *Initializer, |
| 3346 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3347 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3348 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3349 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3350 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3351 | QualType cv2T2 = Initializer->getType(); |
| 3352 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3353 | |
| 3354 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3355 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3356 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3357 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3358 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3359 | ObjCConversion, |
| 3360 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3361 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3362 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3363 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3364 | (void)ObjCLifetimeConversion; |
| 3365 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3366 | // Build the candidate set directly in the initialization sequence |
| 3367 | // structure, so that it will persist if we fail. |
| 3368 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3369 | CandidateSet.clear(); |
| 3370 | |
| 3371 | // Determine whether we are allowed to call explicit constructors or |
| 3372 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3373 | bool AllowExplicit = Kind.AllowExplicit(); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3374 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctions(); |
| 3375 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3376 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3377 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3378 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3379 | // The type we're converting to is a class type. Enumerate its constructors |
| 3380 | // to see if there is a suitable conversion. |
| 3381 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3382 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3383 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3384 | // The container holding the constructors can under certain conditions |
| 3385 | // be changed while iterating (e.g. because of deserialization). |
| 3386 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3387 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 3388 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3389 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3390 | NamedDecl *D = *CI; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3391 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3392 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3393 | // Find the constructor (which may be a template). |
| 3394 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3395 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3396 | if (ConstructorTmpl) |
| 3397 | Constructor = cast<CXXConstructorDecl>( |
| 3398 | ConstructorTmpl->getTemplatedDecl()); |
| 3399 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3400 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3401 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3402 | if (!Constructor->isInvalidDecl() && |
| 3403 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3404 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3405 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3406 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3407 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3408 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3409 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3410 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3411 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3412 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3413 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3414 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3415 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3416 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3417 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3418 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3419 | const RecordType *T2RecordType = 0; |
| 3420 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3421 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3422 | // The type we're converting from is a class type, enumerate its conversion |
| 3423 | // functions. |
| 3424 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3425 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3426 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3427 | CXXRecordDecl::conversion_iterator> |
| 3428 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3429 | for (CXXRecordDecl::conversion_iterator |
| 3430 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3431 | NamedDecl *D = *I; |
| 3432 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3433 | if (isa<UsingShadowDecl>(D)) |
| 3434 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3435 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3436 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3437 | CXXConversionDecl *Conv; |
| 3438 | if (ConvTemplate) |
| 3439 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3440 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3441 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3442 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3443 | // If the conversion function doesn't return a reference type, |
| 3444 | // it can't be considered for this conversion unless we're allowed to |
| 3445 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3446 | // FIXME: Do we need to make sure that we only consider conversion |
| 3447 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3448 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3449 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3450 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3451 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3452 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3453 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3454 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3455 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3456 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3457 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3458 | } |
| 3459 | } |
| 3460 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3461 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3462 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3463 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3464 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3465 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3466 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3467 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3468 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3469 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3470 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3471 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3472 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3473 | // This is the overload that will be used for this initialization step if we |
| 3474 | // use this initialization. Mark it as referenced. |
| 3475 | Function->setReferenced(); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3476 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3477 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3478 | if (isa<CXXConversionDecl>(Function)) |
| 3479 | T2 = Function->getResultType(); |
| 3480 | else |
| 3481 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3482 | |
| 3483 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3484 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3485 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3486 | T2.getNonLValueExprType(S.Context), |
| 3487 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3488 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3489 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3490 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3491 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3492 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3493 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3494 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3495 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3496 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3497 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3498 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3499 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3500 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3501 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3502 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3503 | NewDerivedToBase, NewObjCConversion, |
| 3504 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3505 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3506 | // If the type we've converted to is not reference-related to the |
| 3507 | // type we're looking for, then there is another conversion step |
| 3508 | // we need to perform to produce a temporary of the right type |
| 3509 | // that we'll be binding to. |
| 3510 | ImplicitConversionSequence ICS; |
| 3511 | ICS.setStandard(); |
| 3512 | ICS.Standard = Best->FinalConversion; |
| 3513 | T2 = ICS.Standard.getToType(2); |
| 3514 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3515 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3516 | Sequence.AddDerivedToBaseCastStep( |
| 3517 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3518 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3519 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3520 | else if (NewObjCConversion) |
| 3521 | Sequence.AddObjCObjectConversionStep( |
| 3522 | S.Context.getQualifiedType(T1, |
| 3523 | T2.getNonReferenceType().getQualifiers())); |
| 3524 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3525 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3526 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3527 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3528 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3529 | return OR_Success; |
| 3530 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3531 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3532 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3533 | const InitializedEntity &Entity, |
| 3534 | Expr *CurInitExpr); |
| 3535 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3536 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3537 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3538 | const InitializedEntity &Entity, |
| 3539 | const InitializationKind &Kind, |
| 3540 | Expr *Initializer, |
| 3541 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3542 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3543 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3544 | Qualifiers T1Quals; |
| 3545 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3546 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3547 | Qualifiers T2Quals; |
| 3548 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3549 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3550 | // If the initializer is the address of an overloaded function, try |
| 3551 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3552 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3553 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3554 | T1, Sequence)) |
| 3555 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3556 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3557 | // Delegate everything else to a subfunction. |
| 3558 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3559 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3560 | } |
| 3561 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3562 | /// Converts the target of reference initialization so that it has the |
| 3563 | /// appropriate qualifiers and value kind. |
| 3564 | /// |
| 3565 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3566 | /// \code |
| 3567 | /// int x; |
| 3568 | /// const int &r = x; |
| 3569 | /// \endcode |
| 3570 | /// |
| 3571 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3572 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3573 | /// \code |
| 3574 | /// const int &r = someStruct.bitfield; |
| 3575 | /// \endcode |
| 3576 | static ExprValueKind |
| 3577 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3578 | InitializationSequence &Sequence, |
| 3579 | Expr *Initializer, |
| 3580 | QualType cv1T1, |
| 3581 | Qualifiers T1Quals, |
| 3582 | Qualifiers T2Quals, |
| 3583 | bool IsLValueRef) { |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3584 | bool IsNonAddressableType = Initializer->refersToBitField() || |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3585 | Initializer->refersToVectorElement(); |
| 3586 | |
| 3587 | if (IsNonAddressableType) { |
| 3588 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3589 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3590 | // an rvalue reference. |
| 3591 | // |
| 3592 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3593 | // error to be diagnosed later. |
| 3594 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3595 | assert(Initializer->isGLValue()); |
| 3596 | return Initializer->getValueKind(); |
| 3597 | } |
| 3598 | |
| 3599 | // Force a load so we can materialize a temporary. |
| 3600 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3601 | return VK_RValue; |
| 3602 | } |
| 3603 | |
| 3604 | if (T1Quals != T2Quals) { |
| 3605 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3606 | Initializer->getValueKind()); |
| 3607 | } |
| 3608 | |
| 3609 | return Initializer->getValueKind(); |
| 3610 | } |
| 3611 | |
| 3612 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3613 | /// \brief Reference initialization without resolving overloaded functions. |
| 3614 | static void TryReferenceInitializationCore(Sema &S, |
| 3615 | const InitializedEntity &Entity, |
| 3616 | const InitializationKind &Kind, |
| 3617 | Expr *Initializer, |
| 3618 | QualType cv1T1, QualType T1, |
| 3619 | Qualifiers T1Quals, |
| 3620 | QualType cv2T2, QualType T2, |
| 3621 | Qualifiers T2Quals, |
| 3622 | InitializationSequence &Sequence) { |
| 3623 | QualType DestType = Entity.getType(); |
| 3624 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3625 | // Compute some basic properties of the types and the initializer. |
| 3626 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3627 | bool isRValueRef = !isLValueRef; |
| 3628 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3629 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3630 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3631 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3632 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3633 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3634 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3635 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3636 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3637 | // 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] | 3638 | // "cv2 T2" as follows: |
| 3639 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3640 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3641 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3642 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3643 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3644 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3645 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3646 | bool T1Function = T1->isFunctionType(); |
| 3647 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3648 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3649 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3650 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3651 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3652 | // - 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] | 3653 | // reference-compatible with "cv2 T2," or |
| 3654 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3655 | // 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] | 3656 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3657 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3658 | // to decide whether we're actually binding to a temporary created from |
| 3659 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3660 | if (DerivedToBase) |
| 3661 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3662 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3663 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3664 | else if (ObjCConversion) |
| 3665 | Sequence.AddObjCObjectConversionStep( |
| 3666 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3667 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3668 | ExprValueKind ValueKind = |
| 3669 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3670 | cv1T1, T1Quals, T2Quals, |
| 3671 | isLValueRef); |
| 3672 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3673 | return; |
| 3674 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3675 | |
| 3676 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3677 | // reference-related to T2, and can be implicitly converted to an |
| 3678 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3679 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3680 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3681 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3682 | // If we have an rvalue ref to function type here, the rhs must be |
| 3683 | // an rvalue. |
| 3684 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3685 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3686 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3687 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3688 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3689 | Sequence); |
| 3690 | if (ConvOvlResult == OR_Success) |
| 3691 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3692 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3693 | Sequence.SetOverloadFailure( |
| 3694 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3695 | ConvOvlResult); |
| 3696 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3697 | } |
| 3698 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3699 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3700 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3701 | // 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] | 3702 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3703 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3704 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3705 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3706 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3707 | Sequence.SetOverloadFailure( |
| 3708 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3709 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3710 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3711 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3712 | ? (RefRelationship == Sema::Ref_Related |
| 3713 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3714 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3715 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3716 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3717 | return; |
| 3718 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3719 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3720 | // - If the initializer expression |
| 3721 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3722 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3723 | // Note: functions are handled below. |
| 3724 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3725 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3726 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3727 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3728 | (InitCategory.isXValue() || |
| 3729 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3730 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3731 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3732 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3733 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3734 | // compiler the freedom to perform a copy here or bind to the |
| 3735 | // object, while C++0x requires that we bind directly to the |
| 3736 | // object. Hence, we always bind to the object without making an |
| 3737 | // extra copy. However, in C++03 requires that we check for the |
| 3738 | // presence of a suitable copy constructor: |
| 3739 | // |
| 3740 | // The constructor that would be used to make the copy shall |
| 3741 | // be callable whether or not the copy is actually done. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3742 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3743 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3744 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3745 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3746 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3747 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3748 | if (DerivedToBase) |
| 3749 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3750 | ValueKind); |
| 3751 | else if (ObjCConversion) |
| 3752 | Sequence.AddObjCObjectConversionStep( |
| 3753 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3754 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3755 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3756 | Initializer, cv1T1, |
| 3757 | T1Quals, T2Quals, |
| 3758 | isLValueRef); |
| 3759 | |
| 3760 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3761 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3762 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3763 | |
| 3764 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3765 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3766 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3767 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3768 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3769 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3770 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3771 | Kind, Initializer, |
| 3772 | /*AllowRValues=*/true, |
| 3773 | Sequence); |
| 3774 | if (ConvOvlResult) |
| 3775 | Sequence.SetOverloadFailure( |
| 3776 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3777 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3778 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3779 | return; |
| 3780 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3781 | |
Douglas Gregor | defa32e | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 3782 | if ((RefRelationship == Sema::Ref_Compatible || |
| 3783 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 3784 | isRValueRef && InitCategory.isLValue()) { |
| 3785 | Sequence.SetFailed( |
| 3786 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3787 | return; |
| 3788 | } |
| 3789 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3790 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3791 | return; |
| 3792 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3793 | |
| 3794 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3795 | // from the initializer expression using the rules for a non-reference |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3796 | // copy-initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3797 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3798 | |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3799 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3800 | |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3801 | // FIXME: Why do we use an implicit conversion here rather than trying |
| 3802 | // copy-initialization? |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3803 | ImplicitConversionSequence ICS |
| 3804 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
Richard Smith | 4e47ecb | 2013-06-13 00:57:57 +0000 | [diff] [blame] | 3805 | /*SuppressUserConversions=*/false, |
| 3806 | /*AllowExplicit=*/false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3807 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3808 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3809 | /*AllowObjCWritebackConversion=*/false); |
| 3810 | |
| 3811 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3812 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3813 | // this into an overloading ambiguity diagnostic. However, we need |
| 3814 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3815 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3816 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3817 | Sequence.SetOverloadFailure( |
| 3818 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3819 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3820 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3821 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3822 | else |
| 3823 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3824 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3825 | } else { |
| 3826 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3830 | // same cv-qualification as, or greater cv-qualification |
| 3831 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3832 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3833 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3834 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3835 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3836 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3837 | return; |
| 3838 | } |
| 3839 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3840 | // [...] 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] | 3841 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3842 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3843 | InitCategory.isLValue()) { |
| 3844 | Sequence.SetFailed( |
| 3845 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3846 | return; |
| 3847 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3848 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3849 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3850 | return; |
| 3851 | } |
| 3852 | |
| 3853 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3854 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3855 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3856 | const InitializedEntity &Entity, |
| 3857 | const InitializationKind &Kind, |
| 3858 | Expr *Initializer, |
| 3859 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3860 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3863 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3864 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3865 | const InitializedEntity &Entity, |
| 3866 | const InitializationKind &Kind, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3867 | InitializationSequence &Sequence, |
| 3868 | InitListExpr *InitList) { |
| 3869 | assert((!InitList || InitList->getNumInits() == 0) && |
| 3870 | "Shouldn't use value-init for non-empty init lists"); |
| 3871 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3872 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3873 | // |
| 3874 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3875 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3876 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3877 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3878 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3879 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3880 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3881 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3882 | bool NeedZeroInitialization = true; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3883 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3884 | // C++98: |
| 3885 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 3886 | // (12.1), then the default constructor for T is called (and the |
| 3887 | // initialization is ill-formed if T has no accessible default |
| 3888 | // constructor); |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3889 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3890 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3891 | } else { |
| 3892 | // C++11: |
| 3893 | // -- if T is a class type (clause 9) with either no default constructor |
| 3894 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3895 | // or deleted, then the object is default-initialized; |
| 3896 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3897 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3898 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3899 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3900 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3901 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3902 | // user-provided or deleted default constructor, then the object is |
| 3903 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3904 | // default-initialized; |
Richard Smith | 6678a05 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 3905 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 3906 | // constructor' part was removed by DR1507. |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3907 | if (NeedZeroInitialization) |
| 3908 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3909 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3910 | // C++03: |
| 3911 | // -- if T is a non-union class type without a user-declared constructor, |
| 3912 | // then every non-static data member and base class component of T is |
| 3913 | // value-initialized; |
| 3914 | // [...] A program that calls for [...] value-initialization of an |
| 3915 | // entity of reference type is ill-formed. |
| 3916 | // |
| 3917 | // C++11 doesn't need this handling, because value-initialization does not |
| 3918 | // occur recursively there, and the implicit default constructor is |
| 3919 | // defined as deleted in the problematic cases. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3920 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3921 | ClassDecl->hasUninitializedReferenceMember()) { |
| 3922 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 3923 | return; |
| 3924 | } |
| 3925 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3926 | // If this is list-value-initialization, pass the empty init list on when |
| 3927 | // building the constructor call. This affects the semantics of a few |
| 3928 | // things (such as whether an explicit default constructor can be called). |
| 3929 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3930 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3931 | bool InitListSyntax = InitList; |
| 3932 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3933 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 3934 | InitListSyntax); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3935 | } |
| 3936 | } |
| 3937 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3938 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3941 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3942 | static void TryDefaultInitialization(Sema &S, |
| 3943 | const InitializedEntity &Entity, |
| 3944 | const InitializationKind &Kind, |
| 3945 | InitializationSequence &Sequence) { |
| 3946 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3947 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3948 | // C++ [dcl.init]p6: |
| 3949 | // To default-initialize an object of type T means: |
| 3950 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3951 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3952 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3953 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3954 | // constructor for T is called (and the initialization is ill-formed if |
| 3955 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3956 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3957 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3958 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3959 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3960 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3961 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3962 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3963 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3964 | // 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] | 3965 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3966 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3967 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3968 | return; |
| 3969 | } |
| 3970 | |
| 3971 | // If the destination type has a lifetime property, zero-initialize it. |
| 3972 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3973 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3974 | return; |
| 3975 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3978 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3979 | /// which enumerates all conversion functions and performs overload resolution |
| 3980 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3981 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3982 | const InitializedEntity &Entity, |
| 3983 | const InitializationKind &Kind, |
| 3984 | Expr *Initializer, |
| 3985 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3986 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3987 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3988 | QualType SourceType = Initializer->getType(); |
| 3989 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3990 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3991 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3992 | // Build the candidate set directly in the initialization sequence |
| 3993 | // structure, so that it will persist if we fail. |
| 3994 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3995 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3996 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3997 | // Determine whether we are allowed to call explicit constructors or |
| 3998 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3999 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4000 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4001 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 4002 | // The type we're converting to is a class type. Enumerate its constructors |
| 4003 | // to see if there is a suitable conversion. |
| 4004 | CXXRecordDecl *DestRecordDecl |
| 4005 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4006 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4007 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4008 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4009 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4010 | // The container holding the constructors can under certain conditions |
| 4011 | // be changed while iterating. To be safe we copy the lookup results |
| 4012 | // to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4013 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4014 | for (SmallVectorImpl<NamedDecl *>::iterator |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 4015 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4016 | Con != ConEnd; ++Con) { |
| 4017 | NamedDecl *D = *Con; |
| 4018 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4019 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4020 | // Find the constructor (which may be a template). |
| 4021 | CXXConstructorDecl *Constructor = 0; |
| 4022 | FunctionTemplateDecl *ConstructorTmpl |
| 4023 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4024 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4025 | Constructor = cast<CXXConstructorDecl>( |
| 4026 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4027 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4028 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4029 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4030 | if (!Constructor->isInvalidDecl() && |
| 4031 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 4032 | if (ConstructorTmpl) |
| 4033 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 4034 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4035 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4036 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4037 | else |
| 4038 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4039 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 4040 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4041 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4042 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 4043 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4044 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4045 | |
| 4046 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 4047 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4048 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 4049 | // The type we're converting from is a class type, enumerate its conversion |
| 4050 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4051 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4052 | // We can only enumerate the conversion functions for a complete type; if |
| 4053 | // the type isn't complete, simply skip this step. |
| 4054 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 4055 | CXXRecordDecl *SourceRecordDecl |
| 4056 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4057 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 4058 | std::pair<CXXRecordDecl::conversion_iterator, |
| 4059 | CXXRecordDecl::conversion_iterator> |
| 4060 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 4061 | for (CXXRecordDecl::conversion_iterator |
| 4062 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4063 | NamedDecl *D = *I; |
| 4064 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 4065 | if (isa<UsingShadowDecl>(D)) |
| 4066 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4067 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4068 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 4069 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4070 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4071 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4072 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 4073 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4074 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4075 | if (AllowExplicit || !Conv->isExplicit()) { |
| 4076 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4077 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4078 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4079 | CandidateSet); |
| 4080 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 4081 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 4082 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 4083 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4084 | } |
| 4085 | } |
| 4086 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4087 | |
| 4088 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4089 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4090 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4091 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4092 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4093 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4094 | Result); |
| 4095 | return; |
| 4096 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4097 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4098 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4099 | Function->setReferenced(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4100 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4101 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4102 | if (isa<CXXConstructorDecl>(Function)) { |
| 4103 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4104 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4105 | // cv-unqualified type of the destination. |
| 4106 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4107 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4108 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4109 | return; |
| 4110 | } |
| 4111 | |
| 4112 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4113 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4114 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4115 | // 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] | 4116 | // the resulting temporary object (possible to create an object of |
| 4117 | // a base class type). That copy is not a separate conversion, so |
| 4118 | // we just make a note of the actual destination type (possibly a |
| 4119 | // base class of the type returned by the conversion function) and |
| 4120 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4121 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 4122 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4123 | return; |
| 4124 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4125 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4126 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4127 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4128 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4129 | // If the conversion following the call to the conversion function |
| 4130 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4131 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4132 | Best->FinalConversion.Third) { |
| 4133 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4134 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4135 | ICS.Standard = Best->FinalConversion; |
| 4136 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 4137 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
Richard Smith | 87c2932 | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4140 | /// An egregious hack for compatibility with libstdc++-4.2: in <tr1/hashtable>, |
| 4141 | /// a function with a pointer return type contains a 'return false;' statement. |
| 4142 | /// In C++11, 'false' is not a null pointer, so this breaks the build of any |
| 4143 | /// code using that header. |
| 4144 | /// |
| 4145 | /// Work around this by treating 'return false;' as zero-initializing the result |
| 4146 | /// if it's used in a pointer-returning function in a system header. |
| 4147 | static bool isLibstdcxxPointerReturnFalseHack(Sema &S, |
| 4148 | const InitializedEntity &Entity, |
| 4149 | const Expr *Init) { |
| 4150 | return S.getLangOpts().CPlusPlus11 && |
| 4151 | Entity.getKind() == InitializedEntity::EK_Result && |
| 4152 | Entity.getType()->isPointerType() && |
| 4153 | isa<CXXBoolLiteralExpr>(Init) && |
| 4154 | !cast<CXXBoolLiteralExpr>(Init)->getValue() && |
| 4155 | S.getSourceManager().isInSystemHeader(Init->getExprLoc()); |
| 4156 | } |
| 4157 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4158 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4159 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4160 | |
| 4161 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4162 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4163 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4164 | // Skip parens. |
| 4165 | e = e->IgnoreParens(); |
| 4166 | |
| 4167 | // Skip address-of nodes. |
| 4168 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4169 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4170 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4171 | isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4172 | |
| 4173 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4174 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4175 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4176 | case CK_Dependent: |
| 4177 | case CK_BitCast: |
| 4178 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4179 | case CK_NoOp: |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4180 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4181 | |
| 4182 | case CK_ArrayToPointerDecay: |
| 4183 | return IIK_nonscalar; |
| 4184 | |
| 4185 | case CK_NullToPointer: |
| 4186 | return IIK_okay; |
| 4187 | |
| 4188 | default: |
| 4189 | break; |
| 4190 | } |
| 4191 | |
| 4192 | // 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] | 4193 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4194 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4195 | // load which requires a cleanup. |
| 4196 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4197 | isWeakAccess = true; |
| 4198 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4199 | if (!isAddressOf) return IIK_nonlocal; |
| 4200 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4201 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4202 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4203 | |
| 4204 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4205 | |
| 4206 | // If we have a conditional operator, check both sides. |
| 4207 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4208 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4209 | isWeakAccess)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4210 | return iik; |
| 4211 | |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4212 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4213 | |
| 4214 | // These are never scalar. |
| 4215 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4216 | return IIK_nonscalar; |
| 4217 | |
| 4218 | // Otherwise, it needs to be a null pointer constant. |
| 4219 | } else { |
| 4220 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4221 | ? IIK_okay : IIK_nonlocal); |
| 4222 | } |
| 4223 | |
| 4224 | return IIK_nonlocal; |
| 4225 | } |
| 4226 | |
| 4227 | /// Check whether the given expression is a valid operand for an |
| 4228 | /// indirect copy/restore. |
| 4229 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4230 | assert(src->isRValue()); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4231 | bool isWeakAccess = false; |
| 4232 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4233 | // If isWeakAccess to true, there will be an implicit |
| 4234 | // load which requires a cleanup. |
| 4235 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4236 | S.ExprNeedsCleanups = true; |
| 4237 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4238 | if (iik == IIK_okay) return; |
| 4239 | |
| 4240 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4241 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4242 | << src->getSourceRange(); |
| 4243 | } |
| 4244 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4245 | /// \brief Determine whether we have compatible array types for the |
| 4246 | /// purposes of GNU by-copy array initialization. |
| 4247 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 4248 | const ArrayType *Dest, |
| 4249 | const ArrayType *Source) { |
| 4250 | // If the source and destination array types are equivalent, we're |
| 4251 | // done. |
| 4252 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4253 | return true; |
| 4254 | |
| 4255 | // Make sure that the element types are the same. |
| 4256 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4257 | return false; |
| 4258 | |
| 4259 | // The only mismatch we allow is when the destination is an |
| 4260 | // incomplete array type and the source is a constant array type. |
| 4261 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4262 | } |
| 4263 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4264 | static bool tryObjCWritebackConversion(Sema &S, |
| 4265 | InitializationSequence &Sequence, |
| 4266 | const InitializedEntity &Entity, |
| 4267 | Expr *Initializer) { |
| 4268 | bool ArrayDecay = false; |
| 4269 | QualType ArgType = Initializer->getType(); |
| 4270 | QualType ArgPointee; |
| 4271 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4272 | ArrayDecay = true; |
| 4273 | ArgPointee = ArgArrayType->getElementType(); |
| 4274 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4275 | } |
| 4276 | |
| 4277 | // Handle write-back conversion. |
| 4278 | QualType ConvertedArgType; |
| 4279 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4280 | ConvertedArgType)) |
| 4281 | return false; |
| 4282 | |
| 4283 | // We should copy unless we're passing to an argument explicitly |
| 4284 | // marked 'out'. |
| 4285 | bool ShouldCopy = true; |
| 4286 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4287 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4288 | |
| 4289 | // Do we need an lvalue conversion? |
| 4290 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4291 | ImplicitConversionSequence ICS; |
| 4292 | ICS.setStandard(); |
| 4293 | ICS.Standard.setAsIdentityConversion(); |
| 4294 | |
| 4295 | QualType ResultType; |
| 4296 | if (ArrayDecay) { |
| 4297 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4298 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4299 | } else { |
| 4300 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4301 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4302 | } |
| 4303 | |
| 4304 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4305 | } |
| 4306 | |
| 4307 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4308 | return true; |
| 4309 | } |
| 4310 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4311 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4312 | InitializationSequence &Sequence, |
| 4313 | QualType DestType, |
| 4314 | Expr *Initializer) { |
| 4315 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4316 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4317 | return false; |
| 4318 | |
| 4319 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4320 | return true; |
| 4321 | } |
| 4322 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4323 | // |
| 4324 | // OpenCL 1.2 spec, s6.12.10 |
| 4325 | // |
| 4326 | // The event argument can also be used to associate the |
| 4327 | // async_work_group_copy with a previous async copy allowing |
| 4328 | // an event to be shared by multiple async copies; otherwise |
| 4329 | // event should be zero. |
| 4330 | // |
| 4331 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4332 | InitializationSequence &Sequence, |
| 4333 | QualType DestType, |
| 4334 | Expr *Initializer) { |
| 4335 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4336 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4337 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4338 | return false; |
| 4339 | |
| 4340 | Sequence.AddOCLZeroEventStep(DestType); |
| 4341 | return true; |
| 4342 | } |
| 4343 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4344 | InitializationSequence::InitializationSequence(Sema &S, |
| 4345 | const InitializedEntity &Entity, |
| 4346 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4347 | MultiExprArg Args) |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4348 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4349 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4350 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4351 | // Eliminate non-overload placeholder types in the arguments. We |
| 4352 | // need to do this before checking whether types are dependent |
| 4353 | // because lowering a pseudo-object expression might well give us |
| 4354 | // something of dependent type. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4355 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4356 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4357 | // FIXME: should we be doing this here? |
| 4358 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4359 | if (result.isInvalid()) { |
| 4360 | SetFailed(FK_PlaceholderType); |
| 4361 | return; |
| 4362 | } |
| 4363 | Args[I] = result.take(); |
| 4364 | } |
| 4365 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4366 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4367 | // The semantics of initializers are as follows. The destination type is |
| 4368 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4369 | // 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] | 4370 | // 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] | 4371 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4372 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4373 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4374 | if (DestType->isDependentType() || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4375 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4376 | SequenceKind = DependentSequence; |
| 4377 | return; |
| 4378 | } |
| 4379 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4380 | // Almost everything is a normal sequence. |
| 4381 | setSequenceKind(NormalSequence); |
| 4382 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4383 | QualType SourceType; |
| 4384 | Expr *Initializer = 0; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4385 | if (Args.size() == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4386 | Initializer = Args[0]; |
| 4387 | if (!isa<InitListExpr>(Initializer)) |
| 4388 | SourceType = Initializer->getType(); |
| 4389 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4390 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4391 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4392 | // object is list-initialized (8.5.4). |
| 4393 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4394 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4395 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4396 | return; |
| 4397 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4398 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4399 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4400 | // - If the destination type is a reference type, see 8.5.3. |
| 4401 | if (DestType->isReferenceType()) { |
| 4402 | // C++0x [dcl.init.ref]p1: |
| 4403 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4404 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4405 | // by an object that can be converted into a T. |
| 4406 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4407 | if (Args.size() != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4408 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4409 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4410 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4411 | return; |
| 4412 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4413 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4414 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4415 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4416 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4417 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4418 | return; |
| 4419 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4420 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4421 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4422 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4423 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4424 | return; |
| 4425 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4426 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4427 | // - If the destination type is an array of characters, an array of |
| 4428 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4429 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4430 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4431 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4432 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4433 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4434 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4435 | return; |
| 4436 | } |
| 4437 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4438 | if (Initializer) { |
| 4439 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 4440 | case SIF_None: |
| 4441 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 4442 | return; |
| 4443 | case SIF_NarrowStringIntoWideChar: |
| 4444 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 4445 | return; |
| 4446 | case SIF_WideStringIntoChar: |
| 4447 | SetFailed(FK_WideStringIntoCharArray); |
| 4448 | return; |
| 4449 | case SIF_IncompatWideStringIntoWideChar: |
| 4450 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 4451 | return; |
| 4452 | case SIF_Other: |
| 4453 | break; |
| 4454 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4455 | } |
| 4456 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4457 | // Note: as an GNU C extension, we allow initialization of an |
| 4458 | // array from a compound literal that creates an array of the same |
| 4459 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4460 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4461 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4462 | Initializer->getType()->isArrayType()) { |
| 4463 | const ArrayType *SourceAT |
| 4464 | = Context.getAsArrayType(Initializer->getType()); |
| 4465 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4466 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4467 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4468 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4469 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4470 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4471 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4472 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4473 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4474 | // class member of array type from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4475 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4476 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4477 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4478 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4479 | *this); |
| 4480 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4481 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4482 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4483 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 4484 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4485 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4486 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4487 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4488 | return; |
| 4489 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4490 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4491 | // Determine whether we should consider writeback conversions for |
| 4492 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4493 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4494 | Entity.isParameterKind(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4495 | |
| 4496 | // We're at the end of the line for C: it's either a write-back conversion |
| 4497 | // 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] | 4498 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4499 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4500 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4501 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4502 | return; |
| 4503 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4504 | |
| 4505 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4506 | return; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4507 | |
| 4508 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4509 | return; |
| 4510 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4511 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4512 | AddCAssignmentStep(DestType); |
| 4513 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4514 | return; |
| 4515 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4516 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4517 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4518 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4519 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4520 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4521 | // - If the initialization is direct-initialization, or if it is |
| 4522 | // copy-initialization where the cv-unqualified version of the |
| 4523 | // 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] | 4524 | // class of the destination, constructors are considered. [...] |
| 4525 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4526 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4527 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4528 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4529 | TryConstructorInitialization(S, Entity, Kind, Args, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4530 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4531 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4532 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4533 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4534 | // used) to a derived class thereof are enumerated as described in |
| 4535 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4536 | // (13.3). |
| 4537 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4538 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4539 | return; |
| 4540 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4541 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4542 | if (Args.size() > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4543 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4544 | return; |
| 4545 | } |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4546 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4547 | |
| 4548 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4549 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4550 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4551 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 4552 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4553 | return; |
| 4554 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4555 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4556 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4557 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4558 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4559 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4560 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4561 | |
| 4562 | ImplicitConversionSequence ICS |
| 4563 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4564 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4565 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4566 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4567 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4568 | allowObjCWritebackConversion); |
| 4569 | |
| 4570 | if (ICS.isStandard() && |
| 4571 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4572 | // Objective-C ARC writeback conversion. |
| 4573 | |
| 4574 | // We should copy unless we're passing to an argument explicitly |
| 4575 | // marked 'out'. |
| 4576 | bool ShouldCopy = true; |
| 4577 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4578 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4579 | |
| 4580 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4581 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4582 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4583 | ImplicitConversionSequence LvalueICS; |
| 4584 | LvalueICS.setStandard(); |
| 4585 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4586 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4587 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4588 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4589 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4590 | |
| 4591 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4592 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4593 | DeclAccessPair dap; |
Richard Smith | 87c2932 | 2013-06-20 02:18:31 +0000 | [diff] [blame] | 4594 | if (isLibstdcxxPointerReturnFalseHack(S, Entity, Initializer)) { |
| 4595 | AddZeroInitializationStep(Entity.getType()); |
| 4596 | } else if (Initializer->getType() == Context.OverloadTy && |
| 4597 | !S.ResolveAddressOfOverloadedFunction(Initializer, DestType, |
| 4598 | false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4599 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4600 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4601 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4602 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4603 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4604 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4605 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4606 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4610 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4611 | StepEnd = Steps.end(); |
| 4612 | Step != StepEnd; ++Step) |
| 4613 | Step->Destroy(); |
| 4614 | } |
| 4615 | |
| 4616 | //===----------------------------------------------------------------------===// |
| 4617 | // Perform initialization |
| 4618 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4619 | static Sema::AssignmentAction |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4620 | getAssignmentAction(const InitializedEntity &Entity, bool Diagnose = false) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4621 | switch(Entity.getKind()) { |
| 4622 | case InitializedEntity::EK_Variable: |
| 4623 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4624 | case InitializedEntity::EK_Exception: |
| 4625 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4626 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4627 | return Sema::AA_Initializing; |
| 4628 | |
| 4629 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4630 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4631 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4632 | return Sema::AA_Sending; |
| 4633 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4634 | return Sema::AA_Passing; |
| 4635 | |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 4636 | case InitializedEntity::EK_Parameter_CF_Audited: |
| 4637 | if (Entity.getDecl() && |
| 4638 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4639 | return Sema::AA_Sending; |
| 4640 | |
| 4641 | return !Diagnose ? Sema::AA_Passing : Sema::AA_Passing_CFAudited; |
| 4642 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4643 | case InitializedEntity::EK_Result: |
| 4644 | return Sema::AA_Returning; |
| 4645 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4646 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | f5200d6 | 2013-07-11 19:13:34 +0000 | [diff] [blame] | 4647 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4648 | // FIXME: Can we tell apart casting vs. converting? |
| 4649 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4650 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4651 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4652 | case InitializedEntity::EK_ArrayElement: |
| 4653 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4654 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4655 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4656 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4657 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4658 | return Sema::AA_Initializing; |
| 4659 | } |
| 4660 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4661 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4662 | } |
| 4663 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4664 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4665 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4666 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4667 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4668 | case InitializedEntity::EK_ArrayElement: |
| 4669 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4670 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4671 | case InitializedEntity::EK_New: |
| 4672 | case InitializedEntity::EK_Variable: |
| 4673 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4674 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4675 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4676 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4677 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4678 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4679 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4680 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4681 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4682 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4683 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4684 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4685 | case InitializedEntity::EK_Temporary: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4686 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4687 | return true; |
| 4688 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4689 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4690 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4691 | } |
| 4692 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4693 | /// \brief Whether the given entity, when initialized with an object |
| 4694 | /// created for that initialization, requires destruction. |
| 4695 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4696 | switch (Entity.getKind()) { |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4697 | case InitializedEntity::EK_Result: |
| 4698 | case InitializedEntity::EK_New: |
| 4699 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4700 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4701 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4702 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4703 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4704 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4705 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4706 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4707 | case InitializedEntity::EK_Member: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4708 | case InitializedEntity::EK_Variable: |
| 4709 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4710 | case InitializedEntity::EK_Parameter_CF_Audited: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4711 | case InitializedEntity::EK_Temporary: |
| 4712 | case InitializedEntity::EK_ArrayElement: |
| 4713 | case InitializedEntity::EK_Exception: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4714 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4715 | case InitializedEntity::EK_RelatedResult: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4716 | return true; |
| 4717 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4718 | |
| 4719 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4722 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4723 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4724 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4725 | OverloadCandidateSet &CandidateSet, |
| 4726 | CXXRecordDecl *Class, |
| 4727 | Expr *CurInitExpr) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4728 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4729 | // The container holding the constructors can under certain conditions |
| 4730 | // be changed while iterating (e.g. because of deserialization). |
| 4731 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4732 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Craig Topper | 09d19ef | 2013-07-04 03:08:24 +0000 | [diff] [blame] | 4733 | for (SmallVectorImpl<NamedDecl *>::iterator |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4734 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4735 | NamedDecl *D = *CI; |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4736 | CXXConstructorDecl *Constructor = 0; |
| 4737 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4738 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4739 | // Handle copy/moveconstructors, only. |
| 4740 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4741 | !Constructor->isCopyOrMoveConstructor() || |
| 4742 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4743 | continue; |
| 4744 | |
| 4745 | DeclAccessPair FoundDecl |
| 4746 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4747 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4748 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4749 | continue; |
| 4750 | } |
| 4751 | |
| 4752 | // Handle constructor templates. |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4753 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4754 | if (ConstructorTmpl->isInvalidDecl()) |
| 4755 | continue; |
| 4756 | |
| 4757 | Constructor = cast<CXXConstructorDecl>( |
| 4758 | ConstructorTmpl->getTemplatedDecl()); |
| 4759 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4760 | continue; |
| 4761 | |
| 4762 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4763 | // candidates? |
| 4764 | DeclAccessPair FoundDecl |
| 4765 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4766 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4767 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4768 | } |
| 4769 | } |
| 4770 | |
| 4771 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4772 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4773 | Expr *Initializer) { |
| 4774 | switch (Entity.getKind()) { |
| 4775 | case InitializedEntity::EK_Result: |
| 4776 | return Entity.getReturnLoc(); |
| 4777 | |
| 4778 | case InitializedEntity::EK_Exception: |
| 4779 | return Entity.getThrowLoc(); |
| 4780 | |
| 4781 | case InitializedEntity::EK_Variable: |
| 4782 | return Entity.getDecl()->getLocation(); |
| 4783 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4784 | case InitializedEntity::EK_LambdaCapture: |
| 4785 | return Entity.getCaptureLoc(); |
| 4786 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4787 | case InitializedEntity::EK_ArrayElement: |
| 4788 | case InitializedEntity::EK_Member: |
| 4789 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 4790 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4791 | case InitializedEntity::EK_Temporary: |
| 4792 | case InitializedEntity::EK_New: |
| 4793 | case InitializedEntity::EK_Base: |
| 4794 | case InitializedEntity::EK_Delegating: |
| 4795 | case InitializedEntity::EK_VectorElement: |
| 4796 | case InitializedEntity::EK_ComplexElement: |
| 4797 | case InitializedEntity::EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4798 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 4799 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4800 | return Initializer->getLocStart(); |
| 4801 | } |
| 4802 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4803 | } |
| 4804 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4805 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4806 | /// provided by the given initializer by calling the appropriate copy |
| 4807 | /// constructor. |
| 4808 | /// |
| 4809 | /// \param S The Sema object used for type-checking. |
| 4810 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4811 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4812 | /// the type of the initializer expression or a superclass thereof. |
| 4813 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4814 | /// \param Entity The entity being initialized. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4815 | /// |
| 4816 | /// \param CurInit The initializer expression. |
| 4817 | /// |
| 4818 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4819 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4820 | /// an rvalue. |
| 4821 | /// |
| 4822 | /// \returns An expression that copies the initializer expression into |
| 4823 | /// a temporary object, or an error expression if a copy could not be |
| 4824 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4825 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4826 | QualType T, |
| 4827 | const InitializedEntity &Entity, |
| 4828 | ExprResult CurInit, |
| 4829 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4830 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4831 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4832 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4833 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4834 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4835 | if (!Class) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4836 | return CurInit; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4837 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4838 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4839 | // When certain criteria are met, an implementation is allowed to |
| 4840 | // omit the copy/move construction of a class object, even if the |
| 4841 | // copy/move constructor and/or destructor for the object have |
| 4842 | // side effects. [...] |
| 4843 | // - when a temporary class object that has not been bound to a |
| 4844 | // reference (12.2) would be copied/moved to a class object |
| 4845 | // with the same cv-unqualified type, the copy/move operation |
| 4846 | // can be omitted by constructing the temporary object |
| 4847 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4848 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4849 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4850 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4851 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4852 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4853 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4854 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4855 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4856 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4857 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4858 | return CurInit; |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4859 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4860 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4861 | // Only consider constructors and constructor templates. Per |
| 4862 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4863 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4864 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4865 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4866 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4867 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4868 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4869 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4870 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4871 | case OR_Success: |
| 4872 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4873 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4874 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4875 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4876 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4877 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4878 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4879 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4880 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4881 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4882 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4883 | return CurInit; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4884 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4885 | case OR_Ambiguous: |
| 4886 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4887 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4888 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4889 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4890 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4891 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4892 | case OR_Deleted: |
| 4893 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4894 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4895 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4896 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4897 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4898 | } |
| 4899 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4900 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4901 | SmallVector<Expr*, 8> ConstructorArgs; |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4902 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4903 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4904 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4905 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4906 | |
| 4907 | if (IsExtraneousCopy) { |
| 4908 | // If this is a totally extraneous copy for C++03 reference |
| 4909 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4910 | // expression. We don't generate an (elided) copy operation here |
| 4911 | // because doing so would require us to pass down a flag to avoid |
| 4912 | // infinite recursion, where each step adds another extraneous, |
| 4913 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4914 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4915 | // Instantiate the default arguments of any extra parameters in |
| 4916 | // the selected copy constructor, as if we were going to create a |
| 4917 | // proper call to the copy constructor. |
| 4918 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4919 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4920 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4921 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4922 | break; |
| 4923 | |
| 4924 | // Build the default argument expression; we don't actually care |
| 4925 | // if this succeeds or not, because this routine will complain |
| 4926 | // if there was a problem. |
| 4927 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4928 | } |
| 4929 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4930 | return S.Owned(CurInitExpr); |
| 4931 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4932 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4933 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4934 | // constructor call (we might have derived-to-base conversions, or |
| 4935 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4936 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4937 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4938 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4939 | // Actually perform the constructor call. |
| 4940 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4941 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4942 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4943 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4944 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4945 | CXXConstructExpr::CK_Complete, |
| 4946 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4947 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4948 | // If we're supposed to bind temporaries, do so. |
| 4949 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4950 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4951 | return CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4952 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4953 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4954 | /// \brief Check whether elidable copy construction for binding a reference to |
| 4955 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 4956 | /// -Wc++98-compat. |
| 4957 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4958 | const InitializedEntity &Entity, |
| 4959 | Expr *CurInitExpr) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4960 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4961 | |
| 4962 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 4963 | if (!Record) |
| 4964 | return; |
| 4965 | |
| 4966 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 4967 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 4968 | == DiagnosticsEngine::Ignored) |
| 4969 | return; |
| 4970 | |
| 4971 | // Find constructors which would have been considered. |
| 4972 | OverloadCandidateSet CandidateSet(Loc); |
| 4973 | LookupCopyAndMoveConstructors( |
| 4974 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 4975 | |
| 4976 | // Perform overload resolution. |
| 4977 | OverloadCandidateSet::iterator Best; |
| 4978 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 4979 | |
| 4980 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 4981 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 4982 | << CurInitExpr->getSourceRange(); |
| 4983 | |
| 4984 | switch (OR) { |
| 4985 | case OR_Success: |
| 4986 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4987 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4988 | // FIXME: Check default arguments as far as that's possible. |
| 4989 | break; |
| 4990 | |
| 4991 | case OR_No_Viable_Function: |
| 4992 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4993 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4994 | break; |
| 4995 | |
| 4996 | case OR_Ambiguous: |
| 4997 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4998 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4999 | break; |
| 5000 | |
| 5001 | case OR_Deleted: |
| 5002 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5003 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 5004 | break; |
| 5005 | } |
| 5006 | } |
| 5007 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5008 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 5009 | const InitializedEntity &Entity) { |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5010 | if (Entity.isParameterKind() && Entity.getDecl()) { |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5011 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 5012 | return; |
| 5013 | |
| 5014 | if (Entity.getDecl()->getDeclName()) |
| 5015 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 5016 | << Entity.getDecl()->getDeclName(); |
| 5017 | else |
| 5018 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 5019 | } |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5020 | else if (Entity.getKind() == InitializedEntity::EK_RelatedResult && |
| 5021 | Entity.getMethodDecl()) |
| 5022 | S.Diag(Entity.getMethodDecl()->getLocation(), |
| 5023 | diag::note_method_return_type_change) |
| 5024 | << Entity.getMethodDecl()->getDeclName(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5025 | } |
| 5026 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5027 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 5028 | return s.Kind == InitializationSequence::SK_BindReference || |
| 5029 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 5030 | } |
| 5031 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5032 | /// Returns true if the parameters describe a constructor initialization of |
| 5033 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 5034 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 5035 | const InitializationKind &Kind, |
| 5036 | unsigned NumArgs) { |
| 5037 | switch (Entity.getKind()) { |
| 5038 | case InitializedEntity::EK_Temporary: |
| 5039 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5040 | case InitializedEntity::EK_RelatedResult: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5041 | break; |
| 5042 | default: |
| 5043 | return false; |
| 5044 | } |
| 5045 | |
| 5046 | switch (Kind.getKind()) { |
| 5047 | case InitializationKind::IK_DirectList: |
| 5048 | return true; |
| 5049 | // FIXME: Hack to work around cast weirdness. |
| 5050 | case InitializationKind::IK_Direct: |
| 5051 | case InitializationKind::IK_Value: |
| 5052 | return NumArgs != 1; |
| 5053 | default: |
| 5054 | return false; |
| 5055 | } |
| 5056 | } |
| 5057 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5058 | static ExprResult |
| 5059 | PerformConstructorInitialization(Sema &S, |
| 5060 | const InitializedEntity &Entity, |
| 5061 | const InitializationKind &Kind, |
| 5062 | MultiExprArg Args, |
| 5063 | const InitializationSequence::Step& Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5064 | bool &ConstructorInitRequiresZeroInit, |
| 5065 | bool IsListInitialization) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5066 | unsigned NumArgs = Args.size(); |
| 5067 | CXXConstructorDecl *Constructor |
| 5068 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 5069 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 5070 | |
| 5071 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5072 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5073 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 5074 | ? Kind.getEqualLoc() |
| 5075 | : Kind.getLocation(); |
| 5076 | |
| 5077 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 5078 | // Force even a trivial, implicit default constructor to be |
| 5079 | // semantically checked. We do this explicitly because we don't build |
| 5080 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 5081 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5082 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 5083 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5084 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 5085 | } |
| 5086 | |
| 5087 | ExprResult CurInit = S.Owned((Expr *)0); |
| 5088 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5089 | // C++ [over.match.copy]p1: |
| 5090 | // - When initializing a temporary to be bound to the first parameter |
| 5091 | // of a constructor that takes a reference to possibly cv-qualified |
| 5092 | // T as its first argument, called with a single argument in the |
| 5093 | // context of direct-initialization, explicit conversion functions |
| 5094 | // are also considered. |
| 5095 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 5096 | Args.size() == 1 && |
| 5097 | Constructor->isCopyOrMoveConstructor(); |
| 5098 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5099 | // Determine the arguments required to actually perform the constructor |
| 5100 | // call. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5101 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 5102 | Loc, ConstructorArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5103 | AllowExplicitConv, |
| 5104 | IsListInitialization)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5105 | return ExprError(); |
| 5106 | |
| 5107 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5108 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5109 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5110 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5111 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 5112 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5113 | |
| 5114 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5115 | if (!TSInfo) |
| 5116 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 5117 | SourceRange ParenRange; |
| 5118 | if (Kind.getKind() != InitializationKind::IK_DirectList) |
| 5119 | ParenRange = Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5120 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5121 | CurInit = S.Owned( |
| 5122 | new (S.Context) CXXTemporaryObjectExpr(S.Context, Constructor, |
| 5123 | TSInfo, ConstructorArgs, |
| 5124 | ParenRange, IsListInitialization, |
| 5125 | HadMultipleCandidates, |
| 5126 | ConstructorInitRequiresZeroInit)); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5127 | } else { |
| 5128 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 5129 | CXXConstructExpr::CK_Complete; |
| 5130 | |
| 5131 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5132 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 5133 | CXXConstructExpr::CK_VirtualBase : |
| 5134 | CXXConstructExpr::CK_NonVirtualBase; |
| 5135 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 5136 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 5137 | } |
| 5138 | |
| 5139 | // Only get the parenthesis range if it is a direct construction. |
| 5140 | SourceRange parenRange = |
| 5141 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 5142 | Kind.getParenRange() : SourceRange(); |
| 5143 | |
| 5144 | // If the entity allows NRVO, mark the construction as elidable |
| 5145 | // unconditionally. |
| 5146 | if (Entity.allowsNRVO()) |
| 5147 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5148 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5149 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5150 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5151 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5152 | ConstructorInitRequiresZeroInit, |
| 5153 | ConstructKind, |
| 5154 | parenRange); |
| 5155 | else |
| 5156 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5157 | Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5158 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5159 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5160 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5161 | ConstructorInitRequiresZeroInit, |
| 5162 | ConstructKind, |
| 5163 | parenRange); |
| 5164 | } |
| 5165 | if (CurInit.isInvalid()) |
| 5166 | return ExprError(); |
| 5167 | |
| 5168 | // Only check access if all of that succeeded. |
| 5169 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 5170 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5171 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 5172 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5173 | |
| 5174 | if (shouldBindAsTemporary(Entity)) |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5175 | CurInit = S.MaybeBindToTemporary(CurInit.take()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5176 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5177 | return CurInit; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5178 | } |
| 5179 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5180 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 5181 | /// longer than the current full-expression. Conservatively returns false if |
| 5182 | /// it's unclear. |
| 5183 | static bool |
| 5184 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 5185 | const InitializedEntity *Top = &Entity; |
| 5186 | while (Top->getParent()) |
| 5187 | Top = Top->getParent(); |
| 5188 | |
| 5189 | switch (Top->getKind()) { |
| 5190 | case InitializedEntity::EK_Variable: |
| 5191 | case InitializedEntity::EK_Result: |
| 5192 | case InitializedEntity::EK_Exception: |
| 5193 | case InitializedEntity::EK_Member: |
| 5194 | case InitializedEntity::EK_New: |
| 5195 | case InitializedEntity::EK_Base: |
| 5196 | case InitializedEntity::EK_Delegating: |
| 5197 | return true; |
| 5198 | |
| 5199 | case InitializedEntity::EK_ArrayElement: |
| 5200 | case InitializedEntity::EK_VectorElement: |
| 5201 | case InitializedEntity::EK_BlockElement: |
| 5202 | case InitializedEntity::EK_ComplexElement: |
| 5203 | // Could not determine what the full initialization is. Assume it might not |
| 5204 | // outlive the full-expression. |
| 5205 | return false; |
| 5206 | |
| 5207 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5208 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5209 | case InitializedEntity::EK_Temporary: |
| 5210 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5211 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5212 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5213 | // The entity being initialized might not outlive the full-expression. |
| 5214 | return false; |
| 5215 | } |
| 5216 | |
| 5217 | llvm_unreachable("unknown entity kind"); |
| 5218 | } |
| 5219 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5220 | /// Determine the declaration which an initialized entity ultimately refers to, |
| 5221 | /// for the purpose of lifetime-extending a temporary bound to a reference in |
| 5222 | /// the initialization of \p Entity. |
| 5223 | static const ValueDecl * |
| 5224 | getDeclForTemporaryLifetimeExtension(const InitializedEntity &Entity, |
| 5225 | const ValueDecl *FallbackDecl = 0) { |
| 5226 | // C++11 [class.temporary]p5: |
| 5227 | switch (Entity.getKind()) { |
| 5228 | case InitializedEntity::EK_Variable: |
| 5229 | // The temporary [...] persists for the lifetime of the reference |
| 5230 | return Entity.getDecl(); |
| 5231 | |
| 5232 | case InitializedEntity::EK_Member: |
| 5233 | // For subobjects, we look at the complete object. |
| 5234 | if (Entity.getParent()) |
| 5235 | return getDeclForTemporaryLifetimeExtension(*Entity.getParent(), |
| 5236 | Entity.getDecl()); |
| 5237 | |
| 5238 | // except: |
| 5239 | // -- A temporary bound to a reference member in a constructor's |
| 5240 | // ctor-initializer persists until the constructor exits. |
| 5241 | return Entity.getDecl(); |
| 5242 | |
| 5243 | case InitializedEntity::EK_Parameter: |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5244 | case InitializedEntity::EK_Parameter_CF_Audited: |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5245 | // -- A temporary bound to a reference parameter in a function call |
| 5246 | // persists until the completion of the full-expression containing |
| 5247 | // the call. |
| 5248 | case InitializedEntity::EK_Result: |
| 5249 | // -- The lifetime of a temporary bound to the returned value in a |
| 5250 | // function return statement is not extended; the temporary is |
| 5251 | // destroyed at the end of the full-expression in the return statement. |
| 5252 | case InitializedEntity::EK_New: |
| 5253 | // -- A temporary bound to a reference in a new-initializer persists |
| 5254 | // until the completion of the full-expression containing the |
| 5255 | // new-initializer. |
| 5256 | return 0; |
| 5257 | |
| 5258 | case InitializedEntity::EK_Temporary: |
| 5259 | case InitializedEntity::EK_CompoundLiteralInit: |
Fariborz Jahanian | f92a509 | 2013-07-11 16:48:06 +0000 | [diff] [blame] | 5260 | case InitializedEntity::EK_RelatedResult: |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5261 | // We don't yet know the storage duration of the surrounding temporary. |
| 5262 | // Assume it's got full-expression duration for now, it will patch up our |
| 5263 | // storage duration if that's not correct. |
| 5264 | return 0; |
| 5265 | |
| 5266 | case InitializedEntity::EK_ArrayElement: |
| 5267 | // For subobjects, we look at the complete object. |
| 5268 | return getDeclForTemporaryLifetimeExtension(*Entity.getParent(), |
| 5269 | FallbackDecl); |
| 5270 | |
| 5271 | case InitializedEntity::EK_Base: |
| 5272 | case InitializedEntity::EK_Delegating: |
| 5273 | // We can reach this case for aggregate initialization in a constructor: |
| 5274 | // struct A { int &&r; }; |
| 5275 | // struct B : A { B() : A{0} {} }; |
| 5276 | // In this case, use the innermost field decl as the context. |
| 5277 | return FallbackDecl; |
| 5278 | |
| 5279 | case InitializedEntity::EK_BlockElement: |
| 5280 | case InitializedEntity::EK_LambdaCapture: |
| 5281 | case InitializedEntity::EK_Exception: |
| 5282 | case InitializedEntity::EK_VectorElement: |
| 5283 | case InitializedEntity::EK_ComplexElement: |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5284 | return 0; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5285 | } |
Benjamin Kramer | 6f773e8 | 2013-06-05 15:37:50 +0000 | [diff] [blame] | 5286 | llvm_unreachable("unknown entity kind"); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | static void performLifetimeExtension(Expr *Init, const ValueDecl *ExtendingD); |
| 5290 | |
| 5291 | /// Update a glvalue expression that is used as the initializer of a reference |
| 5292 | /// to note that its lifetime is extended. |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5293 | /// \return \c true if any temporary had its lifetime extended. |
| 5294 | static bool performReferenceExtension(Expr *Init, const ValueDecl *ExtendingD) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5295 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
| 5296 | if (ILE->getNumInits() == 1 && ILE->isGLValue()) { |
| 5297 | // This is just redundant braces around an initializer. Step over it. |
| 5298 | Init = ILE->getInit(0); |
| 5299 | } |
| 5300 | } |
| 5301 | |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5302 | // Walk past any constructs which we can lifetime-extend across. |
| 5303 | Expr *Old; |
| 5304 | do { |
| 5305 | Old = Init; |
| 5306 | |
| 5307 | // Step over any subobject adjustments; we may have a materialized |
| 5308 | // temporary inside them. |
| 5309 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5310 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5311 | Init = const_cast<Expr *>( |
| 5312 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5313 | |
| 5314 | // Per current approach for DR1376, look through casts to reference type |
| 5315 | // when performing lifetime extension. |
| 5316 | if (CastExpr *CE = dyn_cast<CastExpr>(Init)) |
| 5317 | if (CE->getSubExpr()->isGLValue()) |
| 5318 | Init = CE->getSubExpr(); |
| 5319 | |
| 5320 | // FIXME: Per DR1213, subscripting on an array temporary produces an xvalue. |
| 5321 | // It's unclear if binding a reference to that xvalue extends the array |
| 5322 | // temporary. |
| 5323 | } while (Init != Old); |
| 5324 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5325 | if (MaterializeTemporaryExpr *ME = dyn_cast<MaterializeTemporaryExpr>(Init)) { |
| 5326 | // Update the storage duration of the materialized temporary. |
| 5327 | // FIXME: Rebuild the expression instead of mutating it. |
| 5328 | ME->setExtendingDecl(ExtendingD); |
| 5329 | performLifetimeExtension(ME->GetTemporaryExpr(), ExtendingD); |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5330 | return true; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5331 | } |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5332 | |
| 5333 | return false; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5334 | } |
| 5335 | |
| 5336 | /// Update a prvalue expression that is going to be materialized as a |
| 5337 | /// lifetime-extended temporary. |
| 5338 | static void performLifetimeExtension(Expr *Init, const ValueDecl *ExtendingD) { |
| 5339 | // Dig out the expression which constructs the extended temporary. |
| 5340 | SmallVector<const Expr *, 2> CommaLHSs; |
| 5341 | SmallVector<SubobjectAdjustment, 2> Adjustments; |
| 5342 | Init = const_cast<Expr *>( |
| 5343 | Init->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments)); |
| 5344 | |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5345 | if (CXXBindTemporaryExpr *BTE = dyn_cast<CXXBindTemporaryExpr>(Init)) |
| 5346 | Init = BTE->getSubExpr(); |
| 5347 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5348 | if (CXXStdInitializerListExpr *ILE = |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5349 | dyn_cast<CXXStdInitializerListExpr>(Init)) { |
| 5350 | performReferenceExtension(ILE->getSubExpr(), ExtendingD); |
| 5351 | return; |
| 5352 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5353 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5354 | if (InitListExpr *ILE = dyn_cast<InitListExpr>(Init)) { |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5355 | if (ILE->getType()->isArrayType()) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5356 | for (unsigned I = 0, N = ILE->getNumInits(); I != N; ++I) |
| 5357 | performLifetimeExtension(ILE->getInit(I), ExtendingD); |
| 5358 | return; |
| 5359 | } |
| 5360 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5361 | if (CXXRecordDecl *RD = ILE->getType()->getAsCXXRecordDecl()) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5362 | assert(RD->isAggregate() && "aggregate init on non-aggregate"); |
| 5363 | |
| 5364 | // If we lifetime-extend a braced initializer which is initializing an |
| 5365 | // aggregate, and that aggregate contains reference members which are |
| 5366 | // bound to temporaries, those temporaries are also lifetime-extended. |
| 5367 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() && |
| 5368 | ILE->getInitializedFieldInUnion()->getType()->isReferenceType()) |
| 5369 | performReferenceExtension(ILE->getInit(0), ExtendingD); |
| 5370 | else { |
| 5371 | unsigned Index = 0; |
| 5372 | for (RecordDecl::field_iterator I = RD->field_begin(), |
| 5373 | E = RD->field_end(); |
| 5374 | I != E; ++I) { |
Richard Smith | 3c3af14 | 2013-07-01 06:08:20 +0000 | [diff] [blame] | 5375 | if (Index >= ILE->getNumInits()) |
| 5376 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5377 | if (I->isUnnamedBitfield()) |
| 5378 | continue; |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5379 | Expr *SubInit = ILE->getInit(Index); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5380 | if (I->getType()->isReferenceType()) |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5381 | performReferenceExtension(SubInit, ExtendingD); |
| 5382 | else if (isa<InitListExpr>(SubInit) || |
| 5383 | isa<CXXStdInitializerListExpr>(SubInit)) |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5384 | // This may be either aggregate-initialization of a member or |
| 5385 | // initialization of a std::initializer_list object. Either way, |
| 5386 | // we should recursively lifetime-extend that initializer. |
Richard Smith | 5771aab | 2013-06-27 22:54:33 +0000 | [diff] [blame] | 5387 | performLifetimeExtension(SubInit, ExtendingD); |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5388 | ++Index; |
| 5389 | } |
| 5390 | } |
| 5391 | } |
| 5392 | } |
| 5393 | } |
| 5394 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5395 | static void warnOnLifetimeExtension(Sema &S, const InitializedEntity &Entity, |
| 5396 | const Expr *Init, bool IsInitializerList, |
| 5397 | const ValueDecl *ExtendingDecl) { |
| 5398 | // Warn if a field lifetime-extends a temporary. |
| 5399 | if (isa<FieldDecl>(ExtendingDecl)) { |
| 5400 | if (IsInitializerList) { |
| 5401 | S.Diag(Init->getExprLoc(), diag::warn_dangling_std_initializer_list) |
| 5402 | << /*at end of constructor*/true; |
| 5403 | return; |
| 5404 | } |
| 5405 | |
| 5406 | bool IsSubobjectMember = false; |
| 5407 | for (const InitializedEntity *Ent = Entity.getParent(); Ent; |
| 5408 | Ent = Ent->getParent()) { |
| 5409 | if (Ent->getKind() != InitializedEntity::EK_Base) { |
| 5410 | IsSubobjectMember = true; |
| 5411 | break; |
| 5412 | } |
| 5413 | } |
| 5414 | S.Diag(Init->getExprLoc(), |
| 5415 | diag::warn_bind_ref_member_to_temporary) |
| 5416 | << ExtendingDecl << Init->getSourceRange() |
| 5417 | << IsSubobjectMember << IsInitializerList; |
| 5418 | if (IsSubobjectMember) |
| 5419 | S.Diag(ExtendingDecl->getLocation(), |
| 5420 | diag::note_ref_subobject_of_member_declared_here); |
| 5421 | else |
| 5422 | S.Diag(ExtendingDecl->getLocation(), |
| 5423 | diag::note_ref_or_ptr_member_declared_here) |
| 5424 | << /*is pointer*/false; |
| 5425 | } |
| 5426 | } |
| 5427 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5428 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5429 | InitializationSequence::Perform(Sema &S, |
| 5430 | const InitializedEntity &Entity, |
| 5431 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5432 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5433 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5434 | if (Failed()) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5435 | Diagnose(S, Entity, Kind, Args); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5436 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5437 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5438 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5439 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5440 | // If the declaration is a non-dependent, incomplete array type |
| 5441 | // that has an initializer, then its type will be completed once |
| 5442 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5443 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5444 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5445 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5446 | if (const IncompleteArrayType *ArrayT |
| 5447 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5448 | // FIXME: We don't currently have the ability to accurately |
| 5449 | // compute the length of an initializer list without |
| 5450 | // performing full type-checking of the initializer list |
| 5451 | // (since we have to determine where braces are implicitly |
| 5452 | // introduced and such). So, we fall back to making the array |
| 5453 | // type a dependently-sized array type with no specified |
| 5454 | // bound. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5455 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5456 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5457 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5458 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5459 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5460 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5461 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5462 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5463 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5464 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5465 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | *ResultType |
| 5469 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 5470 | /*NumElts=*/0, |
| 5471 | ArrayT->getSizeModifier(), |
| 5472 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5473 | Brackets); |
| 5474 | } |
| 5475 | |
| 5476 | } |
| 5477 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5478 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5479 | !Kind.isExplicitCast()) { |
| 5480 | // Rebuild the ParenListExpr. |
| 5481 | SourceRange ParenRange = Kind.getParenRange(); |
| 5482 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5483 | Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5484 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5485 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5486 | Kind.isExplicitCast() || |
| 5487 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5488 | return ExprResult(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5489 | } |
| 5490 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5491 | // No steps means no initialization. |
| 5492 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5493 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5494 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5495 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5496 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5497 | !Entity.isParameterKind()) { |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5498 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5499 | // from an initializer list. For parameters, we produce a better warning |
| 5500 | // elsewhere. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5501 | Expr *Init = Args[0]; |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5502 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5503 | << Init->getSourceRange(); |
| 5504 | } |
| 5505 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5506 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5507 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5508 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5509 | Entity.getType()->isPointerType() && |
| 5510 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5511 | Expr *Init = Args[0]; |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5512 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5513 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5514 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5515 | << Init->getSourceRange(); |
| 5516 | } |
| 5517 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5518 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5519 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5520 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5521 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5522 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5523 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5524 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5525 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5526 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5527 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5528 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5529 | // grab the only argument out the Args and place it into the "current" |
| 5530 | // initializer. |
| 5531 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5532 | case SK_ResolveAddressOfOverloadedFunction: |
| 5533 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5534 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5535 | case SK_CastDerivedToBaseLValue: |
| 5536 | case SK_BindReference: |
| 5537 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5538 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5539 | case SK_UserConversion: |
| 5540 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5541 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5542 | case SK_QualificationConversionRValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5543 | case SK_LValueToRValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5544 | case SK_ConversionSequence: |
| 5545 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5546 | case SK_UnwrapInitList: |
| 5547 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5548 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5549 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5550 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5551 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5552 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5553 | case SK_PassByIndirectCopyRestore: |
| 5554 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5555 | case SK_ProduceObjCObject: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5556 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5557 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5558 | case SK_OCLZeroEvent: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5559 | assert(Args.size() == 1); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5560 | CurInit = Args[0]; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5561 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5562 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5563 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5564 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5565 | case SK_ConstructorInitialization: |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5566 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5567 | case SK_ZeroInitialization: |
| 5568 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5569 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5570 | |
| 5571 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5572 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5573 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5574 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5575 | Step != StepEnd; ++Step) { |
| 5576 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5577 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5578 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5579 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5580 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5581 | switch (Step->Kind) { |
| 5582 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5583 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5584 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5585 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5586 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5587 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5588 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5589 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5590 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5591 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5592 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5593 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5594 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5595 | case SK_CastDerivedToBaseLValue: { |
| 5596 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5597 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5598 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5599 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5600 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5601 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5602 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5603 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5604 | CurInit.get()->getLocStart(), |
| 5605 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5606 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5607 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5608 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5609 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5610 | QualType T = SourceType; |
| 5611 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5612 | T = Pointer->getPointeeType(); |
| 5613 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5614 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5615 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5616 | } |
| 5617 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5618 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5619 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5620 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5621 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5622 | VK_XValue : |
| 5623 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5624 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 5625 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5626 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5627 | CurInit.get(), |
| 5628 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5629 | break; |
| 5630 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5631 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5632 | case SK_BindReference: |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5633 | // References cannot bind to bit-fields (C++ [dcl.init.ref]p5). |
| 5634 | if (CurInit.get()->refersToBitField()) { |
| 5635 | // We don't necessarily have an unambiguous source bit-field. |
| 5636 | FieldDecl *BitField = CurInit.get()->getSourceBitField(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5637 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5638 | << Entity.getType().isVolatileQualified() |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5639 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 5640 | << (BitField != NULL) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5641 | << CurInit.get()->getSourceRange(); |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5642 | if (BitField) |
| 5643 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 5644 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5645 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5646 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5647 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5648 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5649 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5650 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5651 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5652 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5653 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5654 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5655 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5656 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5657 | // Reference binding does not have any corresponding ASTs. |
| 5658 | |
| 5659 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5660 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5661 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5662 | |
Richard Smith | d6b6987 | 2013-06-15 00:30:29 +0000 | [diff] [blame] | 5663 | // Even though we didn't materialize a temporary, the binding may still |
| 5664 | // extend the lifetime of a temporary. This happens if we bind a reference |
| 5665 | // to the result of a cast to reference type. |
| 5666 | if (const ValueDecl *ExtendingDecl = |
| 5667 | getDeclForTemporaryLifetimeExtension(Entity)) { |
| 5668 | if (performReferenceExtension(CurInit.get(), ExtendingDecl)) |
| 5669 | warnOnLifetimeExtension(S, Entity, CurInit.get(), false, |
| 5670 | ExtendingDecl); |
| 5671 | } |
| 5672 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5673 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5674 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5675 | case SK_BindReferenceToTemporary: { |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5676 | // Make sure the "temporary" is actually an rvalue. |
| 5677 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5678 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5679 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5680 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5681 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5682 | |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5683 | // Maybe lifetime-extend the temporary's subobjects to match the |
| 5684 | // entity's lifetime. |
| 5685 | const ValueDecl *ExtendingDecl = |
| 5686 | getDeclForTemporaryLifetimeExtension(Entity); |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 5687 | if (ExtendingDecl) { |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5688 | performLifetimeExtension(CurInit.get(), ExtendingDecl); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5689 | warnOnLifetimeExtension(S, Entity, CurInit.get(), false, ExtendingDecl); |
Richard Smith | a4bb99c | 2013-06-12 21:51:50 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5692 | // Materialize the temporary into memory. |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5693 | MaterializeTemporaryExpr *MTE = new (S.Context) MaterializeTemporaryExpr( |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5694 | Entity.getType().getNonReferenceType(), CurInit.get(), |
| 5695 | Entity.getType()->isLValueReferenceType(), ExtendingDecl); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5696 | |
| 5697 | // If we're binding to an Objective-C object that has lifetime, we |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5698 | // need cleanups. Likewise if we're extending this temporary to automatic |
| 5699 | // storage duration -- we need to register its cleanup during the |
| 5700 | // full-expression's cleanups. |
| 5701 | if ((S.getLangOpts().ObjCAutoRefCount && |
| 5702 | MTE->getType()->isObjCLifetimeType()) || |
| 5703 | (MTE->getStorageDuration() == SD_Automatic && |
| 5704 | MTE->getType().isDestructedType())) |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5705 | S.ExprNeedsCleanups = true; |
Richard Smith | 8a07cd3 | 2013-06-12 20:42:33 +0000 | [diff] [blame] | 5706 | |
| 5707 | CurInit = S.Owned(MTE); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5708 | break; |
Richard Smith | 211c8dd | 2013-06-05 00:46:14 +0000 | [diff] [blame] | 5709 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5710 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5711 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5712 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5713 | /*IsExtraneousCopy=*/true); |
| 5714 | break; |
| 5715 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5716 | case SK_UserConversion: { |
| 5717 | // We have a user-defined conversion that invokes either a constructor |
| 5718 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5719 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5720 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5721 | FunctionDecl *Fn = Step->Function.Function; |
| 5722 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5723 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5724 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5725 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5726 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5727 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5728 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5729 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5730 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5731 | // Determine the arguments required to actually perform the constructor |
| 5732 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5733 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5734 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5735 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5736 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5737 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5738 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5739 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5740 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5741 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5742 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5743 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5744 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5745 | CXXConstructExpr::CK_Complete, |
| 5746 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5747 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5748 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5749 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5750 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5751 | FoundFn.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5752 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5753 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5754 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5755 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5756 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5757 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5758 | S.IsDerivedFrom(SourceType, Class)) |
| 5759 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5760 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5761 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5762 | } else { |
| 5763 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5764 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5765 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5766 | FoundFn); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5767 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5768 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5769 | |
| 5770 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5771 | // derived-to-base conversion? I believe the answer is "no", because |
| 5772 | // 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] | 5773 | ExprResult CurInitExprRes = |
| 5774 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 5775 | FoundFn, Conversion); |
| 5776 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5777 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5778 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5779 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5780 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5781 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5782 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5783 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5784 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5785 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5786 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5787 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5788 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5789 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5790 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5791 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5792 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5793 | |
| 5794 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5795 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5796 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5797 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5798 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5799 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5800 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5801 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5802 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 5803 | return ExprError(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5804 | } |
| 5805 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5806 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5807 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5808 | CurInit.get()->getType(), |
| 5809 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5810 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5811 | if (MaybeBindToTemp) |
| 5812 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5813 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5814 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5815 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5816 | break; |
| 5817 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5818 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5819 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5820 | case SK_QualificationConversionXValue: |
| 5821 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5822 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5823 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5824 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5825 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5826 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5827 | VK_XValue : |
| 5828 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5829 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5830 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5831 | } |
| 5832 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5833 | case SK_LValueToRValue: { |
| 5834 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
| 5835 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 5836 | CK_LValueToRValue, |
| 5837 | CurInit.take(), |
| 5838 | /*BasePath=*/0, |
| 5839 | VK_RValue)); |
| 5840 | break; |
| 5841 | } |
| 5842 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5843 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5844 | Sema::CheckedConversionKind CCK |
| 5845 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5846 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5847 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5848 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5849 | ExprResult CurInitExprRes = |
| 5850 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5851 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5852 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5853 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5854 | CurInit = CurInitExprRes; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5855 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5856 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5857 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5858 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5859 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5860 | // If we're not initializing the top-level entity, we need to create an |
| 5861 | // InitializeTemporary entity for our target type. |
| 5862 | QualType Ty = Step->Type; |
| 5863 | bool IsTemporary = !S.Context.hasSameType(Entity.getType(), Ty); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5864 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5865 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 5866 | InitListChecker PerformInitList(S, InitEntity, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 5867 | InitList, Ty, /*VerifyOnly=*/false); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5868 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5869 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5870 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 5871 | // Hack: We must update *ResultType if available in order to set the |
| 5872 | // bounds of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5873 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5874 | if (ResultType && |
| 5875 | ResultType->getNonReferenceType()->isIncompleteArrayType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5876 | if ((*ResultType)->isRValueReferenceType()) |
| 5877 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5878 | else if ((*ResultType)->isLValueReferenceType()) |
| 5879 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5880 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5881 | *ResultType = Ty; |
| 5882 | } |
| 5883 | |
| 5884 | InitListExpr *StructuredInitList = |
| 5885 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5886 | CurInit.release(); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5887 | CurInit = shouldBindAsTemporary(InitEntity) |
| 5888 | ? S.MaybeBindToTemporary(StructuredInitList) |
| 5889 | : S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5890 | break; |
| 5891 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5892 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5893 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5894 | // When an initializer list is passed for a parameter of type "reference |
| 5895 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5896 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5897 | // FIXME: This is a hack. What we really should do is create a user |
| 5898 | // conversion step for this case, but this makes it considerably more |
| 5899 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5900 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5901 | Entity.getType().getNonReferenceType()); |
| 5902 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5903 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5904 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5905 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 5906 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5907 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5908 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 5909 | Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5910 | Kind, Arg, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5911 | ConstructorInitRequiresZeroInit, |
| 5912 | /*IsListInitialization*/ true); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5913 | break; |
| 5914 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5915 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5916 | case SK_UnwrapInitList: |
| 5917 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 5918 | break; |
| 5919 | |
| 5920 | case SK_RewrapInitList: { |
| 5921 | Expr *E = CurInit.take(); |
| 5922 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 5923 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5924 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5925 | ILE->setSyntacticForm(Syntactic); |
| 5926 | ILE->setType(E->getType()); |
| 5927 | ILE->setValueKind(E->getValueKind()); |
| 5928 | CurInit = S.Owned(ILE); |
| 5929 | break; |
| 5930 | } |
| 5931 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5932 | case SK_ConstructorInitialization: { |
| 5933 | // When an initializer list is passed for a parameter of type "reference |
| 5934 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5935 | // EK_Parameter entity with reference type. |
| 5936 | // FIXME: This is a hack. What we really should do is create a user |
| 5937 | // conversion step for this case, but this makes it considerably more |
| 5938 | // complicated. For now, this will do. |
| 5939 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5940 | Entity.getType().getNonReferenceType()); |
| 5941 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 5942 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 5943 | : Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5944 | Kind, Args, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5945 | ConstructorInitRequiresZeroInit, |
| 5946 | /*IsListInitialization*/ false); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5947 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5948 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5949 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5950 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5951 | step_iterator NextStep = Step; |
| 5952 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5953 | if (NextStep != StepEnd && |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5954 | (NextStep->Kind == SK_ConstructorInitialization || |
| 5955 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5956 | // The need for zero-initialization is recorded directly into |
| 5957 | // the call to the object's constructor within the next step. |
| 5958 | ConstructorInitRequiresZeroInit = true; |
| 5959 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5960 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5961 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5962 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5963 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5964 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5965 | Kind.getRange().getBegin()); |
| 5966 | |
| 5967 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 5968 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 5969 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5970 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5971 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5972 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5973 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5974 | break; |
| 5975 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5976 | |
| 5977 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5978 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5979 | ExprResult Result = CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5980 | Sema::AssignConvertType ConvTy = |
Fariborz Jahanian | 01ad048 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 5981 | S.CheckSingleAssignmentConstraints(Step->Type, Result, true, |
| 5982 | Entity.getKind() == InitializedEntity::EK_Parameter_CF_Audited); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5983 | if (Result.isInvalid()) |
| 5984 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5985 | CurInit = Result; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5986 | |
| 5987 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5988 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5989 | if (ConvTy != Sema::Compatible && |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 5990 | Entity.isParameterKind() && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5991 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5992 | == Sema::Compatible) |
| 5993 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5994 | if (CurInitExprRes.isInvalid()) |
| 5995 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5996 | CurInit = CurInitExprRes; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5997 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5998 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5999 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 6000 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6001 | CurInit.get(), |
Fariborz Jahanian | 3d672e4 | 2013-07-31 23:19:34 +0000 | [diff] [blame] | 6002 | getAssignmentAction(Entity, true), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6003 | &Complained)) { |
| 6004 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 6005 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6006 | } else if (Complained) |
| 6007 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6008 | break; |
| 6009 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6010 | |
| 6011 | case SK_StringInit: { |
| 6012 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6013 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 6014 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 6015 | break; |
| 6016 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6017 | |
| 6018 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6019 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6020 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 6021 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6022 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6023 | |
| 6024 | case SK_ArrayInit: |
| 6025 | // Okay: we checked everything before creating this step. Note that |
| 6026 | // this is a GNU extension. |
| 6027 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6028 | << Step->Type << CurInit.get()->getType() |
| 6029 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6030 | |
| 6031 | // If the destination type is an incomplete array type, update the |
| 6032 | // type accordingly. |
| 6033 | if (ResultType) { |
| 6034 | if (const IncompleteArrayType *IncompleteDest |
| 6035 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 6036 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6037 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6038 | *ResultType = S.Context.getConstantArrayType( |
| 6039 | IncompleteDest->getElementType(), |
| 6040 | ConstantSource->getSize(), |
| 6041 | ArrayType::Normal, 0); |
| 6042 | } |
| 6043 | } |
| 6044 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6045 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6046 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6047 | case SK_ParenthesizedArrayInit: |
| 6048 | // Okay: we checked everything before creating this step. Note that |
| 6049 | // this is a GNU extension. |
| 6050 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 6051 | << CurInit.get()->getSourceRange(); |
| 6052 | break; |
| 6053 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6054 | case SK_PassByIndirectCopyRestore: |
| 6055 | case SK_PassByIndirectRestore: |
| 6056 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 6057 | CurInit = S.Owned(new (S.Context) |
| 6058 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 6059 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 6060 | break; |
| 6061 | |
| 6062 | case SK_ProduceObjCObject: |
| 6063 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 6064 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6065 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6066 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6067 | |
| 6068 | case SK_StdInitializerList: { |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6069 | S.Diag(CurInit.get()->getExprLoc(), |
| 6070 | diag::warn_cxx98_compat_initializer_list_init) |
| 6071 | << CurInit.get()->getSourceRange(); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6072 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6073 | // Maybe lifetime-extend the array temporary's subobjects to match the |
| 6074 | // entity's lifetime. |
| 6075 | const ValueDecl *ExtendingDecl = |
| 6076 | getDeclForTemporaryLifetimeExtension(Entity); |
| 6077 | if (ExtendingDecl) { |
| 6078 | performLifetimeExtension(CurInit.get(), ExtendingDecl); |
| 6079 | warnOnLifetimeExtension(S, Entity, CurInit.get(), true, ExtendingDecl); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 6080 | } |
| 6081 | |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6082 | // Materialize the temporary into memory. |
| 6083 | MaterializeTemporaryExpr *MTE = new (S.Context) |
| 6084 | MaterializeTemporaryExpr(CurInit.get()->getType(), CurInit.get(), |
| 6085 | /*lvalue reference*/ false, ExtendingDecl); |
| 6086 | |
| 6087 | // Wrap it in a construction of a std::initializer_list<T>. |
| 6088 | CurInit = S.Owned( |
| 6089 | new (S.Context) CXXStdInitializerListExpr(Step->Type, MTE)); |
| 6090 | |
| 6091 | // Bind the result, in case the library has given initializer_list a |
| 6092 | // non-trivial destructor. |
| 6093 | if (shouldBindAsTemporary(Entity)) |
| 6094 | CurInit = S.MaybeBindToTemporary(CurInit.take()); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6095 | break; |
| 6096 | } |
Richard Smith | 7c3e615 | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 6097 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6098 | case SK_OCLSamplerInit: { |
| 6099 | assert(Step->Type->isSamplerT() && |
| 6100 | "Sampler initialization on non sampler type."); |
| 6101 | |
| 6102 | QualType SourceType = CurInit.get()->getType(); |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6103 | |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6104 | if (Entity.isParameterKind()) { |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6105 | if (!SourceType->isSamplerT()) |
| 6106 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 6107 | << SourceType; |
Fariborz Jahanian | 2651b7a | 2013-07-31 18:21:45 +0000 | [diff] [blame] | 6108 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6109 | llvm_unreachable("Invalid EntityKind!"); |
| 6110 | } |
| 6111 | |
| 6112 | break; |
| 6113 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6114 | case SK_OCLZeroEvent: { |
| 6115 | assert(Step->Type->isEventT() && |
| 6116 | "Event initialization on non event type."); |
| 6117 | |
| 6118 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
| 6119 | CK_ZeroToOCLEvent, |
| 6120 | CurInit.get()->getValueKind()); |
| 6121 | break; |
| 6122 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6123 | } |
| 6124 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6125 | |
| 6126 | // Diagnose non-fatal problems with the completed initialization. |
| 6127 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6128 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 6129 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 6130 | cast<FieldDecl>(Entity.getDecl()), |
| 6131 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6132 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 6133 | return CurInit; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6134 | } |
| 6135 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6136 | /// Somewhere within T there is an uninitialized reference subobject. |
| 6137 | /// Dig it out and diagnose it. |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 6138 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 6139 | QualType T) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6140 | if (T->isReferenceType()) { |
| 6141 | S.Diag(Loc, diag::err_reference_without_init) |
| 6142 | << T.getNonReferenceType(); |
| 6143 | return true; |
| 6144 | } |
| 6145 | |
| 6146 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 6147 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 6148 | return false; |
| 6149 | |
| 6150 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 6151 | FE = RD->field_end(); FI != FE; ++FI) { |
| 6152 | if (FI->isUnnamedBitfield()) |
| 6153 | continue; |
| 6154 | |
| 6155 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 6156 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6157 | return true; |
| 6158 | } |
| 6159 | } |
| 6160 | |
| 6161 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 6162 | BE = RD->bases_end(); |
| 6163 | BI != BE; ++BI) { |
| 6164 | if (DiagnoseUninitializedReference(S, BI->getLocStart(), BI->getType())) { |
| 6165 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 6166 | return true; |
| 6167 | } |
| 6168 | } |
| 6169 | |
| 6170 | return false; |
| 6171 | } |
| 6172 | |
| 6173 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6174 | //===----------------------------------------------------------------------===// |
| 6175 | // Diagnose initialization failures |
| 6176 | //===----------------------------------------------------------------------===// |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6177 | |
| 6178 | /// Emit notes associated with an initialization that failed due to a |
| 6179 | /// "simple" conversion failure. |
| 6180 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 6181 | Expr *op) { |
| 6182 | QualType destType = entity.getType(); |
| 6183 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 6184 | op->getType()->isObjCObjectPointerType()) { |
| 6185 | |
| 6186 | // Emit a possible note about the conversion failing because the |
| 6187 | // operand is a message send with a related result type. |
| 6188 | S.EmitRelatedResultTypeNote(op); |
| 6189 | |
| 6190 | // Emit a possible note about a return failing because we're |
| 6191 | // expecting a related result type. |
| 6192 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 6193 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 6194 | } |
| 6195 | } |
| 6196 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6197 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6198 | const InitializedEntity &Entity, |
| 6199 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6200 | ArrayRef<Expr *> Args) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 6201 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6202 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6203 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 6204 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6205 | switch (Failure) { |
| 6206 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6207 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6208 | if (Args.empty()) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 6209 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 6210 | // If this is value-initialization, this could be nested some way within |
| 6211 | // the target type. |
| 6212 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 6213 | DestType->isReferenceType()); |
| 6214 | bool Diagnosed = |
| 6215 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 6216 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 6217 | (void)Diagnosed; |
| 6218 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6219 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6220 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6221 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6222 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6223 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6224 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6225 | break; |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6226 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6227 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 6228 | break; |
| 6229 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6230 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 6231 | break; |
| 6232 | case FK_NarrowStringIntoWideCharArray: |
| 6233 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 6234 | break; |
| 6235 | case FK_WideStringIntoCharArray: |
| 6236 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 6237 | break; |
| 6238 | case FK_IncompatWideStringIntoWideChar: |
| 6239 | S.Diag(Kind.getLocation(), |
| 6240 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 6241 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6242 | case FK_ArrayTypeMismatch: |
| 6243 | case FK_NonConstantArrayInit: |
| 6244 | S.Diag(Kind.getLocation(), |
| 6245 | (Failure == FK_ArrayTypeMismatch |
| 6246 | ? diag::err_array_init_different_type |
| 6247 | : diag::err_array_init_non_constant_array)) |
| 6248 | << DestType.getNonReferenceType() |
| 6249 | << Args[0]->getType() |
| 6250 | << Args[0]->getSourceRange(); |
| 6251 | break; |
| 6252 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6253 | case FK_VariableLengthArrayHasInitializer: |
| 6254 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 6255 | << Args[0]->getSourceRange(); |
| 6256 | break; |
| 6257 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6258 | case FK_AddressOfOverloadFailed: { |
| 6259 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6260 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6261 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6262 | true, |
| 6263 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6264 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 6265 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6266 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6267 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 6268 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6269 | switch (FailedOverloadResult) { |
| 6270 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6271 | if (Failure == FK_UserConversionOverloadFailed) |
| 6272 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 6273 | << Args[0]->getType() << DestType |
| 6274 | << Args[0]->getSourceRange(); |
| 6275 | else |
| 6276 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 6277 | << DestType << Args[0]->getType() |
| 6278 | << Args[0]->getSourceRange(); |
| 6279 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6280 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6281 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6282 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6283 | case OR_No_Viable_Function: |
Larisse Voufo | 288f76a | 2013-06-27 03:36:30 +0000 | [diff] [blame] | 6284 | if (!S.RequireCompleteType(Kind.getLocation(), |
Larisse Voufo | 7419d01 | 2013-06-27 01:50:25 +0000 | [diff] [blame] | 6285 | DestType.getNonReferenceType(), |
| 6286 | diag::err_typecheck_nonviable_condition_incomplete, |
| 6287 | Args[0]->getType(), Args[0]->getSourceRange())) |
| 6288 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 6289 | << Args[0]->getType() << Args[0]->getSourceRange() |
| 6290 | << DestType.getNonReferenceType(); |
| 6291 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6292 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6293 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6294 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6295 | case OR_Deleted: { |
| 6296 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 6297 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 6298 | << Args[0]->getSourceRange(); |
| 6299 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6300 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 6301 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 6302 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6303 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6304 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6305 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6306 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6307 | } |
| 6308 | break; |
| 6309 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6310 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6311 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 6312 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6313 | } |
| 6314 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6315 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6316 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6317 | if (isa<InitListExpr>(Args[0])) { |
| 6318 | S.Diag(Kind.getLocation(), |
| 6319 | diag::err_lvalue_reference_bind_to_initlist) |
| 6320 | << DestType.getNonReferenceType().isVolatileQualified() |
| 6321 | << DestType.getNonReferenceType() |
| 6322 | << Args[0]->getSourceRange(); |
| 6323 | break; |
| 6324 | } |
| 6325 | // Intentional fallthrough |
| 6326 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6327 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6328 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6329 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 6330 | ? diag::err_lvalue_reference_bind_to_temporary |
| 6331 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 6332 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6333 | << DestType.getNonReferenceType() |
| 6334 | << Args[0]->getType() |
| 6335 | << Args[0]->getSourceRange(); |
| 6336 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6337 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6338 | case FK_RValueReferenceBindingToLValue: |
| 6339 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 6340 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6341 | << Args[0]->getSourceRange(); |
| 6342 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6343 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6344 | case FK_ReferenceInitDropsQualifiers: |
| 6345 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 6346 | << DestType.getNonReferenceType() |
| 6347 | << Args[0]->getType() |
| 6348 | << Args[0]->getSourceRange(); |
| 6349 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6350 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6351 | case FK_ReferenceInitFailed: |
| 6352 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 6353 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6354 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6355 | << Args[0]->getType() |
| 6356 | << Args[0]->getSourceRange(); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6357 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6358 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6359 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6360 | case FK_ConversionFailed: { |
| 6361 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6362 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6363 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6364 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6365 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6366 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6367 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6368 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 6369 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6370 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6371 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6372 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6373 | |
| 6374 | case FK_ConversionFromPropertyFailed: |
| 6375 | // No-op. This error has already been reported. |
| 6376 | break; |
| 6377 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6378 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6379 | SourceRange R; |
| 6380 | |
| 6381 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6382 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6383 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6384 | else |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6385 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6386 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6387 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 6388 | if (Kind.isCStyleOrFunctionalCast()) |
| 6389 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 6390 | << R; |
| 6391 | else |
| 6392 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 6393 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6394 | break; |
| 6395 | } |
| 6396 | |
| 6397 | case FK_ReferenceBindingToInitList: |
| 6398 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 6399 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 6400 | break; |
| 6401 | |
| 6402 | case FK_InitListBadDestinationType: |
| 6403 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 6404 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 6405 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6406 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6407 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6408 | case FK_ConstructorOverloadFailed: { |
| 6409 | SourceRange ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6410 | if (Args.size()) |
| 6411 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 6412 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6413 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6414 | if (Failure == FK_ListConstructorOverloadFailed) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6415 | assert(Args.size() == 1 && "List construction from other than 1 argument."); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6416 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6417 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6418 | } |
| 6419 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6420 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 6421 | // bad. |
| 6422 | switch (FailedOverloadResult) { |
| 6423 | case OR_Ambiguous: |
| 6424 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 6425 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6426 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6427 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6428 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6429 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6430 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 6431 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 6432 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 6433 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6434 | // This is implicit default initialization of a member or |
| 6435 | // base within a constructor. If no viable function was |
| 6436 | // found, notify the user that she needs to explicitly |
| 6437 | // initialize this base/member. |
| 6438 | CXXConstructorDecl *Constructor |
| 6439 | = cast<CXXConstructorDecl>(S.CurContext); |
| 6440 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6441 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6442 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6443 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6444 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6445 | << /*base=*/0 |
| 6446 | << Entity.getType(); |
| 6447 | |
| 6448 | RecordDecl *BaseDecl |
| 6449 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 6450 | ->getDecl(); |
| 6451 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 6452 | << S.Context.getTagDeclType(BaseDecl); |
| 6453 | } else { |
| 6454 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6455 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6456 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6457 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6458 | << /*member=*/1 |
| 6459 | << Entity.getName(); |
| 6460 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 6461 | |
| 6462 | if (const RecordType *Record |
| 6463 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6464 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6465 | diag::note_previous_decl) |
| 6466 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6467 | } |
| 6468 | break; |
| 6469 | } |
| 6470 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6471 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6472 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6473 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6474 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6475 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6476 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6477 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6478 | OverloadingResult Ovl |
| 6479 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6480 | if (Ovl != OR_Deleted) { |
| 6481 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6482 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6483 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6484 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6485 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6486 | |
| 6487 | // If this is a defaulted or implicitly-declared function, then |
| 6488 | // it was implicitly deleted. Make it clear that the deletion was |
| 6489 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6490 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6491 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6492 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6493 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6494 | else |
| 6495 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6496 | << true << DestType << ArgsRange; |
| 6497 | |
| 6498 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6499 | break; |
| 6500 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6501 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6502 | case OR_Success: |
| 6503 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6504 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6505 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6506 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6507 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6508 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6509 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6510 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6511 | // This is implicit default-initialization of a const member in |
| 6512 | // a constructor. Complain that it needs to be explicitly |
| 6513 | // initialized. |
| 6514 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6515 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6516 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6517 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6518 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6519 | << /*const=*/1 |
| 6520 | << Entity.getName(); |
| 6521 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6522 | << Entity.getName(); |
| 6523 | } else { |
| 6524 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 6525 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6526 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6527 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6528 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6529 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6530 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6531 | diag::err_init_incomplete_type); |
| 6532 | break; |
| 6533 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6534 | case FK_ListInitializationFailed: { |
| 6535 | // Run the init list checker again to emit diagnostics. |
| 6536 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 6537 | QualType DestType = Entity.getType(); |
| 6538 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Richard Smith | 40cba90 | 2013-06-06 11:41:05 +0000 | [diff] [blame] | 6539 | DestType, /*VerifyOnly=*/false); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6540 | assert(DiagnoseInitList.HadError() && |
| 6541 | "Inconsistent init list check result."); |
| 6542 | break; |
| 6543 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6544 | |
| 6545 | case FK_PlaceholderType: { |
| 6546 | // FIXME: Already diagnosed! |
| 6547 | break; |
| 6548 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6549 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6550 | case FK_ExplicitConstructor: { |
| 6551 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6552 | << Args[0]->getSourceRange(); |
| 6553 | OverloadCandidateSet::iterator Best; |
| 6554 | OverloadingResult Ovl |
| 6555 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6556 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6557 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6558 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6559 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6560 | break; |
| 6561 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6562 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6563 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6564 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6565 | return true; |
| 6566 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6567 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6568 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6569 | switch (SequenceKind) { |
| 6570 | case FailedSequence: { |
| 6571 | OS << "Failed sequence: "; |
| 6572 | switch (Failure) { |
| 6573 | case FK_TooManyInitsForReference: |
| 6574 | OS << "too many initializers for reference"; |
| 6575 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6576 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6577 | case FK_ArrayNeedsInitList: |
| 6578 | OS << "array requires initializer list"; |
| 6579 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6580 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6581 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6582 | OS << "array requires initializer list or string literal"; |
| 6583 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6584 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6585 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6586 | OS << "array requires initializer list or wide string literal"; |
| 6587 | break; |
| 6588 | |
| 6589 | case FK_NarrowStringIntoWideCharArray: |
| 6590 | OS << "narrow string into wide char array"; |
| 6591 | break; |
| 6592 | |
| 6593 | case FK_WideStringIntoCharArray: |
| 6594 | OS << "wide string into char array"; |
| 6595 | break; |
| 6596 | |
| 6597 | case FK_IncompatWideStringIntoWideChar: |
| 6598 | OS << "incompatible wide string into wide char array"; |
| 6599 | break; |
| 6600 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6601 | case FK_ArrayTypeMismatch: |
| 6602 | OS << "array type mismatch"; |
| 6603 | break; |
| 6604 | |
| 6605 | case FK_NonConstantArrayInit: |
| 6606 | OS << "non-constant array initializer"; |
| 6607 | break; |
| 6608 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6609 | case FK_AddressOfOverloadFailed: |
| 6610 | OS << "address of overloaded function failed"; |
| 6611 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6612 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6613 | case FK_ReferenceInitOverloadFailed: |
| 6614 | OS << "overload resolution for reference initialization failed"; |
| 6615 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6616 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6617 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6618 | OS << "non-const lvalue reference bound to temporary"; |
| 6619 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6620 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6621 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6622 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6623 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6624 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6625 | case FK_RValueReferenceBindingToLValue: |
| 6626 | OS << "rvalue reference bound to an lvalue"; |
| 6627 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6628 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6629 | case FK_ReferenceInitDropsQualifiers: |
| 6630 | OS << "reference initialization drops qualifiers"; |
| 6631 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6632 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6633 | case FK_ReferenceInitFailed: |
| 6634 | OS << "reference initialization failed"; |
| 6635 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6636 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6637 | case FK_ConversionFailed: |
| 6638 | OS << "conversion failed"; |
| 6639 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6640 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6641 | case FK_ConversionFromPropertyFailed: |
| 6642 | OS << "conversion from property failed"; |
| 6643 | break; |
| 6644 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6645 | case FK_TooManyInitsForScalar: |
| 6646 | OS << "too many initializers for scalar"; |
| 6647 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6648 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6649 | case FK_ReferenceBindingToInitList: |
| 6650 | OS << "referencing binding to initializer list"; |
| 6651 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6652 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6653 | case FK_InitListBadDestinationType: |
| 6654 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6655 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6656 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6657 | case FK_UserConversionOverloadFailed: |
| 6658 | OS << "overloading failed for user-defined conversion"; |
| 6659 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6660 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6661 | case FK_ConstructorOverloadFailed: |
| 6662 | OS << "constructor overloading failed"; |
| 6663 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6664 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6665 | case FK_DefaultInitOfConst: |
| 6666 | OS << "default initialization of a const variable"; |
| 6667 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6668 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6669 | case FK_Incomplete: |
| 6670 | OS << "initialization of incomplete type"; |
| 6671 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6672 | |
| 6673 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6674 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6675 | break; |
| 6676 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6677 | case FK_VariableLengthArrayHasInitializer: |
| 6678 | OS << "variable length array has an initializer"; |
| 6679 | break; |
| 6680 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6681 | case FK_PlaceholderType: |
| 6682 | OS << "initializer expression isn't contextually valid"; |
| 6683 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6684 | |
| 6685 | case FK_ListConstructorOverloadFailed: |
| 6686 | OS << "list constructor overloading failed"; |
| 6687 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6688 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6689 | case FK_ExplicitConstructor: |
| 6690 | OS << "list copy initialization chose explicit constructor"; |
| 6691 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6692 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6693 | OS << '\n'; |
| 6694 | return; |
| 6695 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6696 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6697 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6698 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6699 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6700 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6701 | case NormalSequence: |
| 6702 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6703 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6704 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6705 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6706 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6707 | if (S != step_begin()) { |
| 6708 | OS << " -> "; |
| 6709 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6710 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6711 | switch (S->Kind) { |
| 6712 | case SK_ResolveAddressOfOverloadedFunction: |
| 6713 | OS << "resolve address of overloaded function"; |
| 6714 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6715 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6716 | case SK_CastDerivedToBaseRValue: |
| 6717 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6718 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6719 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6720 | case SK_CastDerivedToBaseXValue: |
| 6721 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6722 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6723 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6724 | case SK_CastDerivedToBaseLValue: |
| 6725 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 6726 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6727 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6728 | case SK_BindReference: |
| 6729 | OS << "bind reference to lvalue"; |
| 6730 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6731 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6732 | case SK_BindReferenceToTemporary: |
| 6733 | OS << "bind reference to a temporary"; |
| 6734 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6735 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6736 | case SK_ExtraneousCopyToTemporary: |
| 6737 | OS << "extraneous C++03 copy to temporary"; |
| 6738 | break; |
| 6739 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6740 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6741 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6742 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6743 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6744 | case SK_QualificationConversionRValue: |
| 6745 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6746 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6747 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6748 | case SK_QualificationConversionXValue: |
| 6749 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6750 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6751 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6752 | case SK_QualificationConversionLValue: |
| 6753 | OS << "qualification conversion (lvalue)"; |
| 6754 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6755 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6756 | case SK_LValueToRValue: |
| 6757 | OS << "load (lvalue to rvalue)"; |
| 6758 | break; |
| 6759 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6760 | case SK_ConversionSequence: |
| 6761 | OS << "implicit conversion sequence ("; |
| 6762 | S->ICS->DebugPrint(); // FIXME: use OS |
| 6763 | OS << ")"; |
| 6764 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6765 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6766 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6767 | OS << "list aggregate initialization"; |
| 6768 | break; |
| 6769 | |
| 6770 | case SK_ListConstructorCall: |
| 6771 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6772 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6773 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6774 | case SK_UnwrapInitList: |
| 6775 | OS << "unwrap reference initializer list"; |
| 6776 | break; |
| 6777 | |
| 6778 | case SK_RewrapInitList: |
| 6779 | OS << "rewrap reference initializer list"; |
| 6780 | break; |
| 6781 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6782 | case SK_ConstructorInitialization: |
| 6783 | OS << "constructor initialization"; |
| 6784 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6785 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6786 | case SK_ZeroInitialization: |
| 6787 | OS << "zero initialization"; |
| 6788 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6789 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6790 | case SK_CAssignment: |
| 6791 | OS << "C assignment"; |
| 6792 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6793 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6794 | case SK_StringInit: |
| 6795 | OS << "string initialization"; |
| 6796 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6797 | |
| 6798 | case SK_ObjCObjectConversion: |
| 6799 | OS << "Objective-C object conversion"; |
| 6800 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6801 | |
| 6802 | case SK_ArrayInit: |
| 6803 | OS << "array initialization"; |
| 6804 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6805 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6806 | case SK_ParenthesizedArrayInit: |
| 6807 | OS << "parenthesized array initialization"; |
| 6808 | break; |
| 6809 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6810 | case SK_PassByIndirectCopyRestore: |
| 6811 | OS << "pass by indirect copy and restore"; |
| 6812 | break; |
| 6813 | |
| 6814 | case SK_PassByIndirectRestore: |
| 6815 | OS << "pass by indirect restore"; |
| 6816 | break; |
| 6817 | |
| 6818 | case SK_ProduceObjCObject: |
| 6819 | OS << "Objective-C object retension"; |
| 6820 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6821 | |
| 6822 | case SK_StdInitializerList: |
| 6823 | OS << "std::initializer_list from initializer list"; |
| 6824 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6825 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6826 | case SK_OCLSamplerInit: |
| 6827 | OS << "OpenCL sampler_t from integer constant"; |
| 6828 | break; |
| 6829 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6830 | case SK_OCLZeroEvent: |
| 6831 | OS << "OpenCL event_t from zero"; |
| 6832 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6833 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6834 | |
| 6835 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6836 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6837 | |
| 6838 | OS << '\n'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6839 | } |
| 6840 | |
| 6841 | void InitializationSequence::dump() const { |
| 6842 | dump(llvm::errs()); |
| 6843 | } |
| 6844 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6845 | static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, |
| 6846 | QualType EntityType, |
| 6847 | const Expr *PreInit, |
| 6848 | const Expr *PostInit) { |
| 6849 | if (Seq.step_begin() == Seq.step_end() || PreInit->isValueDependent()) |
| 6850 | return; |
| 6851 | |
| 6852 | // A narrowing conversion can only appear as the final implicit conversion in |
| 6853 | // an initialization sequence. |
| 6854 | const InitializationSequence::Step &LastStep = Seq.step_end()[-1]; |
| 6855 | if (LastStep.Kind != InitializationSequence::SK_ConversionSequence) |
| 6856 | return; |
| 6857 | |
| 6858 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 6859 | const StandardConversionSequence *SCS = 0; |
| 6860 | switch (ICS.getKind()) { |
| 6861 | case ImplicitConversionSequence::StandardConversion: |
| 6862 | SCS = &ICS.Standard; |
| 6863 | break; |
| 6864 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6865 | SCS = &ICS.UserDefined.After; |
| 6866 | break; |
| 6867 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6868 | case ImplicitConversionSequence::EllipsisConversion: |
| 6869 | case ImplicitConversionSequence::BadConversion: |
| 6870 | return; |
| 6871 | } |
| 6872 | |
| 6873 | // Determine the type prior to the narrowing conversion. If a conversion |
| 6874 | // operator was used, this may be different from both the type of the entity |
| 6875 | // and of the pre-initialization expression. |
| 6876 | QualType PreNarrowingType = PreInit->getType(); |
| 6877 | if (Seq.step_begin() + 1 != Seq.step_end()) |
| 6878 | PreNarrowingType = Seq.step_end()[-2].Type; |
| 6879 | |
| 6880 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6881 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6882 | QualType ConstantType; |
| 6883 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 6884 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6885 | case NK_Not_Narrowing: |
| 6886 | // No narrowing occurred. |
| 6887 | return; |
| 6888 | |
| 6889 | case NK_Type_Narrowing: |
| 6890 | // This was a floating-to-integer conversion, which is always considered a |
| 6891 | // narrowing conversion even if the value is a constant and can be |
| 6892 | // represented exactly as an integer. |
| 6893 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6894 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6895 | diag::warn_init_list_type_narrowing |
| 6896 | : S.isSFINAEContext()? |
| 6897 | diag::err_init_list_type_narrowing_sfinae |
| 6898 | : diag::err_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6899 | << PostInit->getSourceRange() |
| 6900 | << PreNarrowingType.getLocalUnqualifiedType() |
| 6901 | << EntityType.getLocalUnqualifiedType(); |
| 6902 | break; |
| 6903 | |
| 6904 | case NK_Constant_Narrowing: |
| 6905 | // A constant value was narrowed. |
| 6906 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6907 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6908 | diag::warn_init_list_constant_narrowing |
| 6909 | : S.isSFINAEContext()? |
| 6910 | diag::err_init_list_constant_narrowing_sfinae |
| 6911 | : diag::err_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6912 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6913 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6914 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6915 | break; |
| 6916 | |
| 6917 | case NK_Variable_Narrowing: |
| 6918 | // A variable's value may have been narrowed. |
| 6919 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6920 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6921 | diag::warn_init_list_variable_narrowing |
| 6922 | : S.isSFINAEContext()? |
| 6923 | diag::err_init_list_variable_narrowing_sfinae |
| 6924 | : diag::err_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6925 | << PostInit->getSourceRange() |
| 6926 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6927 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6928 | break; |
| 6929 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6930 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6931 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6932 | llvm::raw_svector_ostream OS(StaticCast); |
| 6933 | OS << "static_cast<"; |
| 6934 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 6935 | // It's important to use the typedef's name if there is one so that the |
| 6936 | // fixit doesn't break code using types like int64_t. |
| 6937 | // |
| 6938 | // FIXME: This will break if the typedef requires qualification. But |
| 6939 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6940 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6941 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6942 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6943 | else { |
| 6944 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 6945 | // with a broken cast. |
| 6946 | return; |
| 6947 | } |
| 6948 | OS << ">("; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6949 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override) |
| 6950 | << PostInit->getSourceRange() |
| 6951 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6952 | << FixItHint::CreateInsertion( |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6953 | S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6954 | } |
| 6955 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6956 | //===----------------------------------------------------------------------===// |
| 6957 | // Initialization helper functions |
| 6958 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6959 | bool |
| 6960 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 6961 | ExprResult Init) { |
| 6962 | if (Init.isInvalid()) |
| 6963 | return false; |
| 6964 | |
| 6965 | Expr *InitE = Init.get(); |
| 6966 | assert(InitE && "No initialization expression"); |
| 6967 | |
Douglas Gregor | 3c394c5 | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 6968 | InitializationKind Kind |
| 6969 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6970 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 6971 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6972 | } |
| 6973 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6974 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6975 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 6976 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6977 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6978 | bool TopLevelOfInitList, |
| 6979 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6980 | if (Init.isInvalid()) |
| 6981 | return ExprError(); |
| 6982 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6983 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6984 | assert(InitE && "No initialization expression?"); |
| 6985 | |
| 6986 | if (EqualLoc.isInvalid()) |
| 6987 | EqualLoc = InitE->getLocStart(); |
| 6988 | |
| 6989 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6990 | EqualLoc, |
| 6991 | AllowExplicit); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6992 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6993 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6994 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6995 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6996 | |
| 6997 | if (!Result.isInvalid() && TopLevelOfInitList) |
| 6998 | DiagnoseNarrowingInInitList(*this, Seq, Entity.getType(), |
| 6999 | InitE, Result.get()); |
| 7000 | |
| 7001 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 7002 | } |