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 | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 123 | static bool IsStringInit(Expr* init, QualType declType, ASTContext& Context) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 124 | const ArrayType *arrayType = Context.getAsArrayType(declType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 125 | if (!arrayType) |
| 126 | return false; |
| 127 | return IsStringInit(init, arrayType, Context) == SIF_None; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 130 | /// Update the type of a string literal, including any surrounding parentheses, |
| 131 | /// to match the type of the object which it is initializing. |
| 132 | static void updateStringLiteralType(Expr *E, QualType Ty) { |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 133 | while (true) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 134 | E->setType(Ty); |
Richard Smith | 27f9cf3 | 2013-05-06 00:35:47 +0000 | [diff] [blame] | 135 | if (isa<StringLiteral>(E) || isa<ObjCEncodeExpr>(E)) |
| 136 | break; |
| 137 | else if (ParenExpr *PE = dyn_cast<ParenExpr>(E)) |
| 138 | E = PE->getSubExpr(); |
| 139 | else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) |
| 140 | E = UO->getSubExpr(); |
| 141 | else if (GenericSelectionExpr *GSE = dyn_cast<GenericSelectionExpr>(E)) |
| 142 | E = GSE->getResultExpr(); |
| 143 | else |
| 144 | llvm_unreachable("unexpected expr in string literal init"); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 145 | } |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 146 | } |
| 147 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 148 | static void CheckStringInit(Expr *Str, QualType &DeclT, const ArrayType *AT, |
| 149 | Sema &S) { |
Chris Lattner | 79e079d | 2009-02-24 23:10:27 +0000 | [diff] [blame] | 150 | // Get the length of the string as parsed. |
| 151 | uint64_t StrLength = |
| 152 | cast<ConstantArrayType>(Str->getType())->getSize().getZExtValue(); |
| 153 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 155 | if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | // C99 6.7.8p14. We have an array of character type with unknown size |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 157 | // being initialized to a string literal. |
Benjamin Kramer | 65263b4 | 2012-08-04 17:00:46 +0000 | [diff] [blame] | 158 | llvm::APInt ConstVal(32, StrLength); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 159 | // Return a new array type (C99 6.7.8p22). |
John McCall | 46a617a | 2009-10-16 00:14:28 +0000 | [diff] [blame] | 160 | DeclT = S.Context.getConstantArrayType(IAT->getElementType(), |
| 161 | ConstVal, |
| 162 | ArrayType::Normal, 0); |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 163 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | 19da8cd | 2009-02-24 23:01:39 +0000 | [diff] [blame] | 164 | return; |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 165 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 166 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 167 | const ConstantArrayType *CAT = cast<ConstantArrayType>(AT); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 169 | // We have an array of character type with known size. However, |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 170 | // the size may be smaller or larger than the string we are initializing. |
| 171 | // FIXME: Avoid truncation for 64-bit length strings. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 172 | if (S.getLangOpts().CPlusPlus) { |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 173 | if (StringLiteral *SL = dyn_cast<StringLiteral>(Str->IgnoreParens())) { |
Anders Carlsson | b8fc45f | 2011-04-14 00:41:11 +0000 | [diff] [blame] | 174 | // For Pascal strings it's OK to strip off the terminating null character, |
| 175 | // so the example below is valid: |
| 176 | // |
| 177 | // unsigned char a[2] = "\pa"; |
| 178 | if (SL->isPascal()) |
| 179 | StrLength--; |
| 180 | } |
| 181 | |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 182 | // [dcl.init.string]p2 |
| 183 | if (StrLength > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 184 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 185 | diag::err_initializer_string_for_char_array_too_long) |
| 186 | << Str->getSourceRange(); |
| 187 | } else { |
| 188 | // C99 6.7.8p14. |
| 189 | if (StrLength-1 > CAT->getSize().getZExtValue()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 190 | S.Diag(Str->getLocStart(), |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 191 | diag::warn_initializer_string_for_char_array_too_long) |
| 192 | << Str->getSourceRange(); |
| 193 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 194 | |
Eli Friedman | 8718a6a | 2009-05-29 18:22:49 +0000 | [diff] [blame] | 195 | // Set the type to the actual size that we are initializing. If we have |
| 196 | // something like: |
| 197 | // char x[1] = "foo"; |
| 198 | // then this will set the string literal's type to char[1]. |
Richard Smith | 30ae1ed | 2013-05-05 16:40:13 +0000 | [diff] [blame] | 199 | updateStringLiteralType(Str, DeclT); |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | dd8e006 | 2009-02-24 22:27:37 +0000 | [diff] [blame] | 202 | //===----------------------------------------------------------------------===// |
| 203 | // Semantic checking for initializer lists. |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 206 | /// @brief Semantic checking for initializer lists. |
| 207 | /// |
| 208 | /// The InitListChecker class contains a set of routines that each |
| 209 | /// handle the initialization of a certain kind of entity, e.g., |
| 210 | /// arrays, vectors, struct/union types, scalars, etc. The |
| 211 | /// InitListChecker itself performs a recursive walk of the subobject |
| 212 | /// structure of the type to be initialized, while stepping through |
| 213 | /// the initializer list one element at a time. The IList and Index |
| 214 | /// parameters to each of the Check* routines contain the active |
| 215 | /// (syntactic) initializer list and the index into that initializer |
| 216 | /// list that represents the current initializer. Each routine is |
| 217 | /// responsible for moving that Index forward as it consumes elements. |
| 218 | /// |
| 219 | /// Each Check* routine also has a StructuredList/StructuredIndex |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 220 | /// arguments, which contains the current "structured" (semantic) |
Douglas Gregor | 9e80f72 | 2009-01-29 01:05:33 +0000 | [diff] [blame] | 221 | /// initializer list and the index into that initializer list where we |
| 222 | /// are copying initializers as we map them over to the semantic |
| 223 | /// list. Once we have completed our recursive walk of the subobject |
| 224 | /// structure, we will have constructed a full semantic initializer |
| 225 | /// list. |
| 226 | /// |
| 227 | /// C99 designators cause changes in the initializer list traversal, |
| 228 | /// because they make the initialization "jump" into a specific |
| 229 | /// subobject and then continue the initialization from that |
| 230 | /// point. CheckDesignatedInitializer() recursively steps into the |
| 231 | /// designated subobject and manages backing out the recursion to |
| 232 | /// initialize the subobjects after the one designated. |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 233 | namespace { |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 234 | class InitListChecker { |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 235 | Sema &SemaRef; |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 236 | bool hadError; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 237 | bool VerifyOnly; // no diagnostics, no structure building |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 238 | bool AllowBraceElision; |
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, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 329 | InitListExpr *IL, QualType &T, bool VerifyOnly, |
| 330 | bool AllowBraceElision); |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 331 | bool HadError() { return hadError; } |
| 332 | |
| 333 | // @brief Retrieves the fully-structured initializer list used for |
| 334 | // semantic analysis and code generation. |
| 335 | InitListExpr *getFullyStructuredList() const { return FullyStructuredList; } |
| 336 | }; |
Chris Lattner | 8b419b9 | 2009-02-24 22:48:58 +0000 | [diff] [blame] | 337 | } // end anonymous namespace |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 338 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 339 | void InitListChecker::CheckValueInitializable(const InitializedEntity &Entity) { |
| 340 | assert(VerifyOnly && |
| 341 | "CheckValueInitializable is only inteded for verification mode."); |
| 342 | |
| 343 | SourceLocation Loc; |
| 344 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 345 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 346 | InitializationSequence InitSeq(SemaRef, Entity, Kind, None); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 347 | if (InitSeq.Failed()) |
| 348 | hadError = true; |
| 349 | } |
| 350 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 351 | void InitListChecker::FillInValueInitForField(unsigned Init, FieldDecl *Field, |
| 352 | const InitializedEntity &ParentEntity, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 353 | InitListExpr *ILE, |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 354 | bool &RequiresSecondPass) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 355 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 356 | unsigned NumInits = ILE->getNumInits(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 357 | InitializedEntity MemberEntity |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 358 | = InitializedEntity::InitializeMember(Field, &ParentEntity); |
| 359 | if (Init >= NumInits || !ILE->getInit(Init)) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 360 | // If there's no explicit initializer but we have a default initializer, use |
| 361 | // that. This only happens in C++1y, since classes with default |
| 362 | // initializers are not aggregates in C++11. |
| 363 | if (Field->hasInClassInitializer()) { |
| 364 | Expr *DIE = CXXDefaultInitExpr::Create(SemaRef.Context, |
| 365 | ILE->getRBraceLoc(), Field); |
| 366 | if (Init < NumInits) |
| 367 | ILE->setInit(Init, DIE); |
| 368 | else { |
| 369 | ILE->updateInit(SemaRef.Context, Init, DIE); |
| 370 | RequiresSecondPass = true; |
| 371 | } |
| 372 | return; |
| 373 | } |
| 374 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 375 | // FIXME: We probably don't need to handle references |
| 376 | // specially here, since value-initialization of references is |
| 377 | // handled in InitializationSequence. |
| 378 | if (Field->getType()->isReferenceType()) { |
| 379 | // C++ [dcl.init.aggr]p9: |
| 380 | // If an incomplete or empty initializer-list leaves a |
| 381 | // member of reference type uninitialized, the program is |
| 382 | // ill-formed. |
| 383 | SemaRef.Diag(Loc, diag::err_init_reference_member_uninitialized) |
| 384 | << Field->getType() |
| 385 | << ILE->getSyntacticForm()->getSourceRange(); |
| 386 | SemaRef.Diag(Field->getLocation(), |
| 387 | diag::note_uninit_reference_member); |
| 388 | hadError = true; |
| 389 | return; |
| 390 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 392 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 393 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 394 | InitializationSequence InitSeq(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 395 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 396 | InitSeq.Diagnose(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 397 | hadError = true; |
| 398 | return; |
| 399 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 400 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 401 | ExprResult MemberInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 402 | = InitSeq.Perform(SemaRef, MemberEntity, Kind, None); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 403 | if (MemberInit.isInvalid()) { |
| 404 | hadError = true; |
| 405 | return; |
| 406 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 407 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 408 | if (hadError) { |
| 409 | // Do nothing |
| 410 | } else if (Init < NumInits) { |
| 411 | ILE->setInit(Init, MemberInit.takeAs<Expr>()); |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 412 | } else if (InitSeq.isConstructorInitialization()) { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 413 | // Value-initialization requires a constructor call, so |
| 414 | // extend the initializer list to include the constructor |
| 415 | // call and make a note that we'll need to take another pass |
| 416 | // through the initializer list. |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 417 | ILE->updateInit(SemaRef.Context, Init, MemberInit.takeAs<Expr>()); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 418 | RequiresSecondPass = true; |
| 419 | } |
| 420 | } else if (InitListExpr *InnerILE |
| 421 | = dyn_cast<InitListExpr>(ILE->getInit(Init))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 422 | FillInValueInitializations(MemberEntity, InnerILE, |
| 423 | RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 426 | /// Recursively replaces NULL values within the given initializer list |
| 427 | /// with expressions that perform value-initialization of the |
| 428 | /// appropriate type. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 429 | void |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 430 | InitListChecker::FillInValueInitializations(const InitializedEntity &Entity, |
| 431 | InitListExpr *ILE, |
| 432 | bool &RequiresSecondPass) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | assert((ILE->getType() != SemaRef.Context.VoidTy) && |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 434 | "Should not have void type"); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 435 | SourceLocation Loc = ILE->getLocStart(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 436 | if (ILE->getSyntacticForm()) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 437 | Loc = ILE->getSyntacticForm()->getLocStart(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 439 | if (const RecordType *RType = ILE->getType()->getAs<RecordType>()) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 440 | const RecordDecl *RDecl = RType->getDecl(); |
| 441 | if (RDecl->isUnion() && ILE->getInitializedFieldInUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 442 | FillInValueInitForField(0, ILE->getInitializedFieldInUnion(), |
| 443 | Entity, ILE, RequiresSecondPass); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 444 | else if (RDecl->isUnion() && isa<CXXRecordDecl>(RDecl) && |
| 445 | cast<CXXRecordDecl>(RDecl)->hasInClassInitializer()) { |
| 446 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 447 | FieldEnd = RDecl->field_end(); |
| 448 | Field != FieldEnd; ++Field) { |
| 449 | if (Field->hasInClassInitializer()) { |
| 450 | FillInValueInitForField(0, *Field, Entity, ILE, RequiresSecondPass); |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | } else { |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 455 | unsigned Init = 0; |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 456 | for (RecordDecl::field_iterator Field = RDecl->field_begin(), |
| 457 | FieldEnd = RDecl->field_end(); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 458 | Field != FieldEnd; ++Field) { |
| 459 | if (Field->isUnnamedBitfield()) |
| 460 | continue; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 461 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 462 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 463 | return; |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 464 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 465 | FillInValueInitForField(Init, *Field, Entity, ILE, RequiresSecondPass); |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 466 | if (hadError) |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 467 | return; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 469 | ++Init; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 470 | |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 471 | // Only look at the first initialization of a union. |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 472 | if (RDecl->isUnion()) |
Douglas Gregor | d6d37de | 2009-12-22 00:05:34 +0000 | [diff] [blame] | 473 | break; |
| 474 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 478 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 479 | |
| 480 | QualType ElementType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 482 | InitializedEntity ElementEntity = Entity; |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 483 | unsigned NumInits = ILE->getNumInits(); |
| 484 | unsigned NumElements = NumInits; |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 485 | if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 486 | ElementType = AType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 487 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) |
| 488 | NumElements = CAType->getSize().getZExtValue(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 489 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 490 | 0, Entity); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 491 | } else if (const VectorType *VType = ILE->getType()->getAs<VectorType>()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 492 | ElementType = VType->getElementType(); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 493 | NumElements = VType->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 494 | ElementEntity = InitializedEntity::InitializeElement(SemaRef.Context, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 495 | 0, Entity); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | } else |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 497 | ElementType = ILE->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 498 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 499 | |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 500 | for (unsigned Init = 0; Init != NumElements; ++Init) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 501 | if (hadError) |
| 502 | return; |
| 503 | |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 504 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement || |
| 505 | ElementEntity.getKind() == InitializedEntity::EK_VectorElement) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 506 | ElementEntity.setElementIndex(Init); |
| 507 | |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 508 | Expr *InitExpr = (Init < NumInits ? ILE->getInit(Init) : 0); |
| 509 | if (!InitExpr && !ILE->hasArrayFiller()) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 510 | InitializationKind Kind = InitializationKind::CreateValue(Loc, Loc, Loc, |
| 511 | true); |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 512 | InitializationSequence InitSeq(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 513 | if (!InitSeq) { |
Dmitri Gribenko | 5543169 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 514 | InitSeq.Diagnose(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 515 | hadError = true; |
| 516 | return; |
| 517 | } |
| 518 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 519 | ExprResult ElementInit |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 520 | = InitSeq.Perform(SemaRef, ElementEntity, Kind, None); |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 521 | if (ElementInit.isInvalid()) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 522 | hadError = true; |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 523 | return; |
| 524 | } |
| 525 | |
| 526 | if (hadError) { |
| 527 | // Do nothing |
| 528 | } else if (Init < NumInits) { |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 529 | // For arrays, just set the expression used for value-initialization |
| 530 | // of the "holes" in the array. |
| 531 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) |
| 532 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 533 | else |
| 534 | ILE->setInit(Init, ElementInit.takeAs<Expr>()); |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 535 | } else { |
| 536 | // For arrays, just set the expression used for value-initialization |
| 537 | // of the rest of elements and exit. |
| 538 | if (ElementEntity.getKind() == InitializedEntity::EK_ArrayElement) { |
| 539 | ILE->setArrayFiller(ElementInit.takeAs<Expr>()); |
| 540 | return; |
| 541 | } |
| 542 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 543 | if (InitSeq.isConstructorInitialization()) { |
Argyrios Kyrtzidis | 4423ac0 | 2011-04-21 00:27:41 +0000 | [diff] [blame] | 544 | // Value-initialization requires a constructor call, so |
| 545 | // extend the initializer list to include the constructor |
| 546 | // call and make a note that we'll need to take another pass |
| 547 | // through the initializer list. |
| 548 | ILE->updateInit(SemaRef.Context, Init, ElementInit.takeAs<Expr>()); |
| 549 | RequiresSecondPass = true; |
| 550 | } |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 551 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 552 | } else if (InitListExpr *InnerILE |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 553 | = dyn_cast_or_null<InitListExpr>(InitExpr)) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 554 | FillInValueInitializations(ElementEntity, InnerILE, RequiresSecondPass); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
Chris Lattner | 68355a5 | 2009-01-29 05:10:57 +0000 | [diff] [blame] | 558 | |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 559 | InitListChecker::InitListChecker(Sema &S, const InitializedEntity &Entity, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 560 | InitListExpr *IL, QualType &T, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 561 | bool VerifyOnly, bool AllowBraceElision) |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 562 | : SemaRef(S), VerifyOnly(VerifyOnly), AllowBraceElision(AllowBraceElision) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 563 | hadError = false; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 564 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 565 | unsigned newIndex = 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 566 | unsigned newStructuredIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 567 | FullyStructuredList |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 568 | = getStructuredSubobjectInit(IL, newIndex, T, 0, 0, IL->getSourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 569 | CheckExplicitInitList(Entity, IL, T, newIndex, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 570 | FullyStructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 571 | /*TopLevelObject=*/true); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 572 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 573 | if (!hadError && !VerifyOnly) { |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 574 | bool RequiresSecondPass = false; |
| 575 | FillInValueInitializations(Entity, FullyStructuredList, RequiresSecondPass); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 576 | if (RequiresSecondPass && !hadError) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 577 | FillInValueInitializations(Entity, FullyStructuredList, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 578 | RequiresSecondPass); |
| 579 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | int InitListChecker::numArrayElements(QualType DeclType) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 583 | // FIXME: use a proper constant |
| 584 | int maxElements = 0x7FFFFFFF; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 585 | if (const ConstantArrayType *CAT = |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 586 | SemaRef.Context.getAsConstantArrayType(DeclType)) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 587 | maxElements = static_cast<int>(CAT->getSize().getZExtValue()); |
| 588 | } |
| 589 | return maxElements; |
| 590 | } |
| 591 | |
| 592 | int InitListChecker::numStructUnionElements(QualType DeclType) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 593 | RecordDecl *structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 594 | int InitializableMembers = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 595 | for (RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 596 | Field = structDecl->field_begin(), |
| 597 | FieldEnd = structDecl->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 598 | Field != FieldEnd; ++Field) { |
Douglas Gregor | d61db33 | 2011-10-10 17:22:13 +0000 | [diff] [blame] | 599 | if (!Field->isUnnamedBitfield()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 600 | ++InitializableMembers; |
| 601 | } |
Argyrios Kyrtzidis | 39ba4ae | 2008-06-09 23:19:58 +0000 | [diff] [blame] | 602 | if (structDecl->isUnion()) |
Eli Friedman | f84eda3 | 2008-05-25 14:03:31 +0000 | [diff] [blame] | 603 | return std::min(InitializableMembers, 1); |
| 604 | return InitializableMembers - structDecl->hasFlexibleArrayMember(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 607 | void InitListChecker::CheckImplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 608 | InitListExpr *ParentIList, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 609 | QualType T, unsigned &Index, |
| 610 | InitListExpr *StructuredList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 611 | unsigned &StructuredIndex) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 612 | int maxElements = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 613 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 614 | if (T->isArrayType()) |
| 615 | maxElements = numArrayElements(T); |
Douglas Gregor | fb87b89 | 2010-04-26 21:31:17 +0000 | [diff] [blame] | 616 | else if (T->isRecordType()) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 617 | maxElements = numStructUnionElements(T); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 618 | else if (T->isVectorType()) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 619 | maxElements = T->getAs<VectorType>()->getNumElements(); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 620 | else |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 621 | llvm_unreachable("CheckImplicitInitList(): Illegal type"); |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 622 | |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 623 | if (maxElements == 0) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 624 | if (!VerifyOnly) |
| 625 | SemaRef.Diag(ParentIList->getInit(Index)->getLocStart(), |
| 626 | diag::err_implicit_empty_initializer); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 627 | ++Index; |
Eli Friedman | 402256f | 2008-05-25 13:49:22 +0000 | [diff] [blame] | 628 | hadError = true; |
| 629 | return; |
| 630 | } |
| 631 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 632 | // Build a structured initializer list corresponding to this subobject. |
| 633 | InitListExpr *StructuredSubobjectInitList |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | = getStructuredSubobjectInit(ParentIList, Index, T, StructuredList, |
| 635 | StructuredIndex, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 636 | SourceRange(ParentIList->getInit(Index)->getLocStart(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 637 | ParentIList->getSourceRange().getEnd())); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 638 | unsigned StructuredSubobjectInitIndex = 0; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 639 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 640 | // Check the element types and build the structural subobject. |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 641 | unsigned StartIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 642 | CheckListElementTypes(Entity, ParentIList, T, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 643 | /*SubobjectIsDesignatorContext=*/false, Index, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 644 | StructuredSubobjectInitList, |
Eli Friedman | 629f118 | 2011-08-23 20:17:13 +0000 | [diff] [blame] | 645 | StructuredSubobjectInitIndex); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 646 | |
| 647 | if (VerifyOnly) { |
| 648 | if (!AllowBraceElision && (T->isArrayType() || T->isRecordType())) |
| 649 | hadError = true; |
| 650 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 651 | StructuredSubobjectInitList->setType(T); |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 652 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 653 | unsigned EndIndex = (Index == StartIndex? StartIndex : Index - 1); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 654 | // Update the structured sub-object initializer so that it's ending |
| 655 | // range corresponds with the end of the last initializer it used. |
| 656 | if (EndIndex < ParentIList->getNumInits()) { |
| 657 | SourceLocation EndLoc |
| 658 | = ParentIList->getInit(EndIndex)->getSourceRange().getEnd(); |
| 659 | StructuredSubobjectInitList->setRBraceLoc(EndLoc); |
| 660 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 661 | |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 662 | // Complain about missing braces. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 663 | if (T->isArrayType() || T->isRecordType()) { |
| 664 | SemaRef.Diag(StructuredSubobjectInitList->getLocStart(), |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 665 | AllowBraceElision ? diag::warn_missing_braces : |
| 666 | diag::err_missing_braces) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 667 | << StructuredSubobjectInitList->getSourceRange() |
| 668 | << FixItHint::CreateInsertion( |
| 669 | StructuredSubobjectInitList->getLocStart(), "{") |
| 670 | << FixItHint::CreateInsertion( |
| 671 | SemaRef.PP.getLocForEndOfToken( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 672 | StructuredSubobjectInitList->getLocEnd()), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 673 | "}"); |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 674 | if (!AllowBraceElision) |
| 675 | hadError = true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 676 | } |
Tanya Lattner | 1e1d396 | 2010-03-07 04:17:15 +0000 | [diff] [blame] | 677 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 680 | void InitListChecker::CheckExplicitInitList(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 681 | InitListExpr *IList, QualType &T, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 682 | unsigned &Index, |
| 683 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 684 | unsigned &StructuredIndex, |
| 685 | bool TopLevelObject) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 686 | assert(IList->isExplicit() && "Illegal Implicit InitListExpr"); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 687 | if (!VerifyOnly) { |
| 688 | SyntacticToSemantic[IList] = StructuredList; |
| 689 | StructuredList->setSyntacticForm(IList); |
| 690 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 691 | CheckListElementTypes(Entity, IList, T, /*SubobjectIsDesignatorContext=*/true, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 692 | Index, StructuredList, StructuredIndex, TopLevelObject); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 693 | if (!VerifyOnly) { |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 694 | QualType ExprTy = T; |
| 695 | if (!ExprTy->isArrayType()) |
| 696 | ExprTy = ExprTy.getNonLValueExprType(SemaRef.Context); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 697 | IList->setType(ExprTy); |
| 698 | StructuredList->setType(ExprTy); |
| 699 | } |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 700 | if (hadError) |
| 701 | return; |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 702 | |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 703 | if (Index < IList->getNumInits()) { |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 704 | // We have leftover initializers |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 705 | if (VerifyOnly) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 706 | if (SemaRef.getLangOpts().CPlusPlus || |
| 707 | (SemaRef.getLangOpts().OpenCL && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 708 | IList->getType()->isVectorType())) { |
| 709 | hadError = true; |
| 710 | } |
| 711 | return; |
| 712 | } |
| 713 | |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 714 | if (StructuredIndex == 1 && |
| 715 | IsStringInit(StructuredList->getInit(0), T, SemaRef.Context)) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 716 | unsigned DK = diag::warn_excess_initializers_in_char_array_initializer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 717 | if (SemaRef.getLangOpts().CPlusPlus) { |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 718 | DK = diag::err_excess_initializers_in_char_array_initializer; |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 719 | hadError = true; |
| 720 | } |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 721 | // Special-case |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 722 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 723 | << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 724 | } else if (!T->isIncompleteType()) { |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 725 | // Don't complain for incomplete types, since we'll get an error |
| 726 | // elsewhere |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 727 | QualType CurrentObjectType = StructuredList->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 728 | int initKind = |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 729 | CurrentObjectType->isArrayType()? 0 : |
| 730 | CurrentObjectType->isVectorType()? 1 : |
| 731 | CurrentObjectType->isScalarType()? 2 : |
| 732 | CurrentObjectType->isUnionType()? 3 : |
| 733 | 4; |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 734 | |
| 735 | unsigned DK = diag::warn_excess_initializers; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 736 | if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | e540858 | 2009-05-29 20:20:05 +0000 | [diff] [blame] | 737 | DK = diag::err_excess_initializers; |
| 738 | hadError = true; |
| 739 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 740 | if (SemaRef.getLangOpts().OpenCL && initKind == 1) { |
Nate Begeman | 0863452 | 2009-07-07 21:53:06 +0000 | [diff] [blame] | 741 | DK = diag::err_excess_initializers; |
| 742 | hadError = true; |
| 743 | } |
Douglas Gregor | 7c53ca6 | 2009-02-18 22:23:55 +0000 | [diff] [blame] | 744 | |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 745 | SemaRef.Diag(IList->getInit(Index)->getLocStart(), DK) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 746 | << initKind << IList->getInit(Index)->getSourceRange(); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
Eli Friedman | cda25a9 | 2008-05-19 20:20:43 +0000 | [diff] [blame] | 749 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 750 | if (!VerifyOnly && T->isScalarType() && IList->getNumInits() == 1 && |
| 751 | !TopLevelObject) |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 752 | SemaRef.Diag(IList->getLocStart(), diag::warn_braces_around_scalar_init) |
Douglas Gregor | a3a8351 | 2009-04-01 23:51:29 +0000 | [diff] [blame] | 753 | << IList->getSourceRange() |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 754 | << FixItHint::CreateRemoval(IList->getLocStart()) |
| 755 | << FixItHint::CreateRemoval(IList->getLocEnd()); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 758 | void InitListChecker::CheckListElementTypes(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 759 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 761 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 762 | unsigned &Index, |
| 763 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 764 | unsigned &StructuredIndex, |
| 765 | bool TopLevelObject) { |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 766 | if (DeclType->isAnyComplexType() && SubobjectIsDesignatorContext) { |
| 767 | // Explicitly braced initializer for complex type can be real+imaginary |
| 768 | // parts. |
| 769 | CheckComplexType(Entity, IList, DeclType, Index, |
| 770 | StructuredList, StructuredIndex); |
| 771 | } else if (DeclType->isScalarType()) { |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 772 | CheckScalarType(Entity, IList, DeclType, Index, |
| 773 | StructuredList, StructuredIndex); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 774 | } else if (DeclType->isVectorType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 775 | CheckVectorType(Entity, IList, DeclType, Index, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 776 | StructuredList, StructuredIndex); |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 777 | } else if (DeclType->isRecordType()) { |
| 778 | assert(DeclType->isAggregateType() && |
| 779 | "non-aggregate records should be handed in CheckSubElementType"); |
| 780 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
| 781 | CheckStructUnionTypes(Entity, IList, DeclType, RD->field_begin(), |
| 782 | SubobjectIsDesignatorContext, Index, |
| 783 | StructuredList, StructuredIndex, |
| 784 | TopLevelObject); |
| 785 | } else if (DeclType->isArrayType()) { |
| 786 | llvm::APSInt Zero( |
| 787 | SemaRef.Context.getTypeSize(SemaRef.Context.getSizeType()), |
| 788 | false); |
| 789 | CheckArrayType(Entity, IList, DeclType, Zero, |
| 790 | SubobjectIsDesignatorContext, Index, |
| 791 | StructuredList, StructuredIndex); |
Steve Naroff | 6135352 | 2008-08-10 16:05:48 +0000 | [diff] [blame] | 792 | } else if (DeclType->isVoidType() || DeclType->isFunctionType()) { |
| 793 | // This type is invalid, issue a diagnostic. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 794 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 795 | if (!VerifyOnly) |
| 796 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 797 | << DeclType; |
Eli Friedman | d8dc210 | 2008-05-20 05:25:56 +0000 | [diff] [blame] | 798 | hadError = true; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 799 | } else if (DeclType->isReferenceType()) { |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 800 | CheckReferenceType(Entity, IList, DeclType, Index, |
| 801 | StructuredList, StructuredIndex); |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 802 | } else if (DeclType->isObjCObjectType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 803 | if (!VerifyOnly) |
| 804 | SemaRef.Diag(IList->getLocStart(), diag::err_init_objc_class) |
| 805 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 806 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 807 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 808 | if (!VerifyOnly) |
| 809 | SemaRef.Diag(IList->getLocStart(), diag::err_illegal_initializer_type) |
| 810 | << DeclType; |
Douglas Gregor | 4d9e738 | 2010-05-03 18:24:37 +0000 | [diff] [blame] | 811 | hadError = true; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 815 | void InitListChecker::CheckSubElementType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 816 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | QualType ElemType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 818 | unsigned &Index, |
| 819 | InitListExpr *StructuredList, |
| 820 | unsigned &StructuredIndex) { |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 821 | Expr *expr = IList->getInit(Index); |
Eli Friedman | c9c0ea6 | 2008-05-19 20:00:43 +0000 | [diff] [blame] | 822 | if (InitListExpr *SubInitList = dyn_cast<InitListExpr>(expr)) { |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 823 | if (!ElemType->isRecordType() || ElemType->isAggregateType()) { |
| 824 | unsigned newIndex = 0; |
| 825 | unsigned newStructuredIndex = 0; |
| 826 | InitListExpr *newStructuredList |
| 827 | = getStructuredSubobjectInit(IList, Index, ElemType, |
| 828 | StructuredList, StructuredIndex, |
| 829 | SubInitList->getSourceRange()); |
| 830 | CheckExplicitInitList(Entity, SubInitList, ElemType, newIndex, |
| 831 | newStructuredList, newStructuredIndex); |
| 832 | ++StructuredIndex; |
| 833 | ++Index; |
| 834 | return; |
| 835 | } |
| 836 | assert(SemaRef.getLangOpts().CPlusPlus && |
| 837 | "non-aggregate records are only possible in C++"); |
| 838 | // C++ initialization is handled later. |
| 839 | } |
| 840 | |
| 841 | if (ElemType->isScalarType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 842 | return CheckScalarType(Entity, IList, ElemType, Index, |
| 843 | StructuredList, StructuredIndex); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 844 | } else if (ElemType->isReferenceType()) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 845 | return CheckReferenceType(Entity, IList, ElemType, Index, |
| 846 | StructuredList, StructuredIndex); |
| 847 | } |
Anders Carlsson | d28b428 | 2009-08-27 17:18:13 +0000 | [diff] [blame] | 848 | |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 849 | if (const ArrayType *arrayType = SemaRef.Context.getAsArrayType(ElemType)) { |
| 850 | // arrayType can be incomplete if we're initializing a flexible |
| 851 | // array member. There's nothing we can do with the completed |
| 852 | // type here, though. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 853 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 854 | if (IsStringInit(expr, arrayType, SemaRef.Context) == SIF_None) { |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 855 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 856 | CheckStringInit(expr, ElemType, arrayType, SemaRef); |
| 857 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
Eli Friedman | 8a5d929 | 2011-09-26 19:09:09 +0000 | [diff] [blame] | 858 | } |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 859 | ++Index; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 860 | return; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 861 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 862 | |
| 863 | // Fall through for subaggregate initialization. |
| 864 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 865 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 866 | // C++ [dcl.init.aggr]p12: |
| 867 | // All implicit type conversions (clause 4) are considered when |
Sebastian Redl | 5d3d41d | 2011-09-24 17:47:39 +0000 | [diff] [blame] | 868 | // initializing the aggregate member with an initializer from |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 869 | // an initializer-list. If the initializer can initialize a |
| 870 | // member, the member is initialized. [...] |
| 871 | |
| 872 | // FIXME: Better EqualLoc? |
| 873 | InitializationKind Kind = |
| 874 | InitializationKind::CreateCopy(expr->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 875 | InitializationSequence Seq(SemaRef, Entity, Kind, expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 876 | |
| 877 | if (Seq) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 878 | if (!VerifyOnly) { |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 879 | ExprResult Result = |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 880 | Seq.Perform(SemaRef, Entity, Kind, expr); |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 881 | if (Result.isInvalid()) |
| 882 | hadError = true; |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 883 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 884 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
Richard Smith | b6f8d28 | 2011-12-20 04:00:21 +0000 | [diff] [blame] | 885 | Result.takeAs<Expr>()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 886 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 887 | ++Index; |
| 888 | return; |
| 889 | } |
| 890 | |
| 891 | // Fall through for subaggregate initialization |
| 892 | } else { |
| 893 | // C99 6.7.8p13: |
| 894 | // |
| 895 | // The initializer for a structure or union object that has |
| 896 | // automatic storage duration shall be either an initializer |
| 897 | // list as described below, or a single expression that has |
| 898 | // compatible structure or union type. In the latter case, the |
| 899 | // initial value of the object, including unnamed members, is |
| 900 | // that of the expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 901 | ExprResult ExprRes = SemaRef.Owned(expr); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 902 | if ((ElemType->isRecordType() || ElemType->isVectorType()) && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 903 | SemaRef.CheckSingleAssignmentConstraints(ElemType, ExprRes, |
| 904 | !VerifyOnly) |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 905 | == Sema::Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 906 | if (ExprRes.isInvalid()) |
| 907 | hadError = true; |
| 908 | else { |
| 909 | ExprRes = SemaRef.DefaultFunctionArrayLvalueConversion(ExprRes.take()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 910 | if (ExprRes.isInvalid()) |
| 911 | hadError = true; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 912 | } |
| 913 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 914 | ExprRes.takeAs<Expr>()); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 915 | ++Index; |
| 916 | return; |
| 917 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 918 | ExprRes.release(); |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 919 | // Fall through for subaggregate initialization |
| 920 | } |
| 921 | |
| 922 | // C++ [dcl.init.aggr]p12: |
| 923 | // |
| 924 | // [...] Otherwise, if the member is itself a non-empty |
| 925 | // subaggregate, brace elision is assumed and the initializer is |
| 926 | // considered for the initialization of the first member of |
| 927 | // the subaggregate. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 928 | if (!SemaRef.getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 929 | (ElemType->isAggregateType() || ElemType->isVectorType())) { |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 930 | CheckImplicitInitList(Entity, IList, ElemType, Index, StructuredList, |
| 931 | StructuredIndex); |
| 932 | ++StructuredIndex; |
| 933 | } else { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 934 | if (!VerifyOnly) { |
| 935 | // We cannot initialize this element, so let |
| 936 | // PerformCopyInitialization produce the appropriate diagnostic. |
| 937 | SemaRef.PerformCopyInitialization(Entity, SourceLocation(), |
| 938 | SemaRef.Owned(expr), |
| 939 | /*TopLevelOfInitList=*/true); |
| 940 | } |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 941 | hadError = true; |
| 942 | ++Index; |
| 943 | ++StructuredIndex; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 944 | } |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 947 | void InitListChecker::CheckComplexType(const InitializedEntity &Entity, |
| 948 | InitListExpr *IList, QualType DeclType, |
| 949 | unsigned &Index, |
| 950 | InitListExpr *StructuredList, |
| 951 | unsigned &StructuredIndex) { |
| 952 | assert(Index == 0 && "Index in explicit init list must be zero"); |
| 953 | |
| 954 | // As an extension, clang supports complex initializers, which initialize |
| 955 | // a complex number component-wise. When an explicit initializer list for |
| 956 | // a complex number contains two two initializers, this extension kicks in: |
| 957 | // it exepcts the initializer list to contain two elements convertible to |
| 958 | // the element type of the complex type. The first element initializes |
| 959 | // the real part, and the second element intitializes the imaginary part. |
| 960 | |
| 961 | if (IList->getNumInits() != 2) |
| 962 | return CheckScalarType(Entity, IList, DeclType, Index, StructuredList, |
| 963 | StructuredIndex); |
| 964 | |
| 965 | // This is an extension in C. (The builtin _Complex type does not exist |
| 966 | // in the C++ standard.) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 967 | if (!SemaRef.getLangOpts().CPlusPlus && !VerifyOnly) |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 968 | SemaRef.Diag(IList->getLocStart(), diag::ext_complex_component_init) |
| 969 | << IList->getSourceRange(); |
| 970 | |
| 971 | // Initialize the complex number. |
| 972 | QualType elementType = DeclType->getAs<ComplexType>()->getElementType(); |
| 973 | InitializedEntity ElementEntity = |
| 974 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
| 975 | |
| 976 | for (unsigned i = 0; i < 2; ++i) { |
| 977 | ElementEntity.setElementIndex(Index); |
| 978 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 979 | StructuredList, StructuredIndex); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 984 | void InitListChecker::CheckScalarType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 985 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 986 | unsigned &Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 987 | InitListExpr *StructuredList, |
| 988 | unsigned &StructuredIndex) { |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 989 | if (Index >= IList->getNumInits()) { |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 990 | if (!VerifyOnly) |
| 991 | SemaRef.Diag(IList->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 992 | SemaRef.getLangOpts().CPlusPlus11 ? |
Richard Smith | 6b13022 | 2011-10-18 21:39:00 +0000 | [diff] [blame] | 993 | diag::warn_cxx98_compat_empty_scalar_initializer : |
| 994 | diag::err_empty_scalar_initializer) |
| 995 | << IList->getSourceRange(); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 996 | hadError = !SemaRef.getLangOpts().CPlusPlus11; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 997 | ++Index; |
| 998 | ++StructuredIndex; |
Eli Friedman | bb504d3 | 2008-05-19 20:12:18 +0000 | [diff] [blame] | 999 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1000 | } |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1001 | |
| 1002 | Expr *expr = IList->getInit(Index); |
| 1003 | if (InitListExpr *SubIList = dyn_cast<InitListExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1004 | if (!VerifyOnly) |
| 1005 | SemaRef.Diag(SubIList->getLocStart(), |
| 1006 | diag::warn_many_braces_around_scalar_init) |
| 1007 | << SubIList->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1008 | |
| 1009 | CheckScalarType(Entity, SubIList, DeclType, Index, StructuredList, |
| 1010 | StructuredIndex); |
| 1011 | return; |
| 1012 | } else if (isa<DesignatedInitExpr>(expr)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1013 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1014 | SemaRef.Diag(expr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1015 | diag::err_designator_for_scalar_init) |
| 1016 | << DeclType << expr->getSourceRange(); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1017 | hadError = true; |
| 1018 | ++Index; |
| 1019 | ++StructuredIndex; |
| 1020 | return; |
| 1021 | } |
| 1022 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1023 | if (VerifyOnly) { |
| 1024 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1025 | hadError = true; |
| 1026 | ++Index; |
| 1027 | return; |
| 1028 | } |
| 1029 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1030 | ExprResult Result = |
| 1031 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1032 | SemaRef.Owned(expr), |
| 1033 | /*TopLevelOfInitList=*/true); |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1034 | |
| 1035 | Expr *ResultExpr = 0; |
| 1036 | |
| 1037 | if (Result.isInvalid()) |
| 1038 | hadError = true; // types weren't compatible. |
| 1039 | else { |
| 1040 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1041 | |
John McCall | b934c2d | 2010-11-11 00:46:36 +0000 | [diff] [blame] | 1042 | if (ResultExpr != expr) { |
| 1043 | // The type was promoted, update initializer list. |
| 1044 | IList->setInit(Index, ResultExpr); |
| 1045 | } |
| 1046 | } |
| 1047 | if (hadError) |
| 1048 | ++StructuredIndex; |
| 1049 | else |
| 1050 | UpdateStructuredListElement(StructuredList, StructuredIndex, ResultExpr); |
| 1051 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1054 | void InitListChecker::CheckReferenceType(const InitializedEntity &Entity, |
| 1055 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1056 | unsigned &Index, |
| 1057 | InitListExpr *StructuredList, |
| 1058 | unsigned &StructuredIndex) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1059 | if (Index >= IList->getNumInits()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1060 | // FIXME: It would be wonderful if we could point at the actual member. In |
| 1061 | // general, it would be useful to pass location information down the stack, |
| 1062 | // so that we know the location (or decl) of the "current object" being |
| 1063 | // initialized. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1064 | if (!VerifyOnly) |
| 1065 | SemaRef.Diag(IList->getLocStart(), |
| 1066 | diag::err_init_reference_member_uninitialized) |
| 1067 | << DeclType |
| 1068 | << IList->getSourceRange(); |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1069 | hadError = true; |
| 1070 | ++Index; |
| 1071 | ++StructuredIndex; |
| 1072 | return; |
| 1073 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1074 | |
| 1075 | Expr *expr = IList->getInit(Index); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1076 | if (isa<InitListExpr>(expr) && !SemaRef.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1077 | if (!VerifyOnly) |
| 1078 | SemaRef.Diag(IList->getLocStart(), diag::err_init_non_aggr_init_list) |
| 1079 | << DeclType << IList->getSourceRange(); |
| 1080 | hadError = true; |
| 1081 | ++Index; |
| 1082 | ++StructuredIndex; |
| 1083 | return; |
| 1084 | } |
| 1085 | |
| 1086 | if (VerifyOnly) { |
| 1087 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(expr))) |
| 1088 | hadError = true; |
| 1089 | ++Index; |
| 1090 | return; |
| 1091 | } |
| 1092 | |
| 1093 | ExprResult Result = |
| 1094 | SemaRef.PerformCopyInitialization(Entity, expr->getLocStart(), |
| 1095 | SemaRef.Owned(expr), |
| 1096 | /*TopLevelOfInitList=*/true); |
| 1097 | |
| 1098 | if (Result.isInvalid()) |
| 1099 | hadError = true; |
| 1100 | |
| 1101 | expr = Result.takeAs<Expr>(); |
| 1102 | IList->setInit(Index, expr); |
| 1103 | |
| 1104 | if (hadError) |
| 1105 | ++StructuredIndex; |
| 1106 | else |
| 1107 | UpdateStructuredListElement(StructuredList, StructuredIndex, expr); |
| 1108 | ++Index; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1111 | void InitListChecker::CheckVectorType(const InitializedEntity &Entity, |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1112 | InitListExpr *IList, QualType DeclType, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1113 | unsigned &Index, |
| 1114 | InitListExpr *StructuredList, |
| 1115 | unsigned &StructuredIndex) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1116 | const VectorType *VT = DeclType->getAs<VectorType>(); |
| 1117 | unsigned maxElements = VT->getNumElements(); |
| 1118 | unsigned numEltsInit = 0; |
| 1119 | QualType elementType = VT->getElementType(); |
Anders Carlsson | 46f4659 | 2010-01-23 19:55:29 +0000 | [diff] [blame] | 1120 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1121 | if (Index >= IList->getNumInits()) { |
| 1122 | // Make sure the element type can be value-initialized. |
| 1123 | if (VerifyOnly) |
| 1124 | CheckValueInitializable( |
| 1125 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity)); |
| 1126 | return; |
| 1127 | } |
| 1128 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1129 | if (!SemaRef.getLangOpts().OpenCL) { |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1130 | // If the initializing element is a vector, try to copy-initialize |
| 1131 | // instead of breaking it apart (which is doomed to failure anyway). |
| 1132 | Expr *Init = IList->getInit(Index); |
| 1133 | if (!isa<InitListExpr>(Init) && Init->getType()->isVectorType()) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1134 | if (VerifyOnly) { |
| 1135 | if (!SemaRef.CanPerformCopyInitialization(Entity, SemaRef.Owned(Init))) |
| 1136 | hadError = true; |
| 1137 | ++Index; |
| 1138 | return; |
| 1139 | } |
| 1140 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1141 | ExprResult Result = |
| 1142 | SemaRef.PerformCopyInitialization(Entity, Init->getLocStart(), |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 1143 | SemaRef.Owned(Init), |
| 1144 | /*TopLevelOfInitList=*/true); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1145 | |
| 1146 | Expr *ResultExpr = 0; |
| 1147 | if (Result.isInvalid()) |
| 1148 | hadError = true; // types weren't compatible. |
| 1149 | else { |
| 1150 | ResultExpr = Result.takeAs<Expr>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1151 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1152 | if (ResultExpr != Init) { |
| 1153 | // The type was promoted, update initializer list. |
| 1154 | IList->setInit(Index, ResultExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1157 | if (hadError) |
| 1158 | ++StructuredIndex; |
| 1159 | else |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1160 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1161 | ResultExpr); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1162 | ++Index; |
| 1163 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1164 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1166 | InitializedEntity ElementEntity = |
| 1167 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1168 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1169 | for (unsigned i = 0; i < maxElements; ++i, ++numEltsInit) { |
| 1170 | // Don't attempt to go past the end of the init list |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1171 | if (Index >= IList->getNumInits()) { |
| 1172 | if (VerifyOnly) |
| 1173 | CheckValueInitializable(ElementEntity); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1174 | break; |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1175 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1176 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1177 | ElementEntity.setElementIndex(Index); |
| 1178 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1179 | StructuredList, StructuredIndex); |
| 1180 | } |
| 1181 | return; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1182 | } |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1183 | |
| 1184 | InitializedEntity ElementEntity = |
| 1185 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1186 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1187 | // OpenCL initializers allows vectors to be constructed from vectors. |
| 1188 | for (unsigned i = 0; i < maxElements; ++i) { |
| 1189 | // Don't attempt to go past the end of the init list |
| 1190 | if (Index >= IList->getNumInits()) |
| 1191 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1192 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1193 | ElementEntity.setElementIndex(Index); |
| 1194 | |
| 1195 | QualType IType = IList->getInit(Index)->getType(); |
| 1196 | if (!IType->isVectorType()) { |
| 1197 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1198 | StructuredList, StructuredIndex); |
| 1199 | ++numEltsInit; |
| 1200 | } else { |
| 1201 | QualType VecType; |
| 1202 | const VectorType *IVT = IType->getAs<VectorType>(); |
| 1203 | unsigned numIElts = IVT->getNumElements(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1204 | |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1205 | if (IType->isExtVectorType()) |
| 1206 | VecType = SemaRef.Context.getExtVectorType(elementType, numIElts); |
| 1207 | else |
| 1208 | VecType = SemaRef.Context.getVectorType(elementType, numIElts, |
Bob Wilson | e86d78c | 2010-11-10 21:56:12 +0000 | [diff] [blame] | 1209 | IVT->getVectorKind()); |
John McCall | 20e047a | 2010-10-30 00:11:39 +0000 | [diff] [blame] | 1210 | CheckSubElementType(ElementEntity, IList, VecType, Index, |
| 1211 | StructuredList, StructuredIndex); |
| 1212 | numEltsInit += numIElts; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | // OpenCL requires all elements to be initialized. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1217 | if (numEltsInit != maxElements) { |
| 1218 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1219 | SemaRef.Diag(IList->getLocStart(), |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1220 | diag::err_vector_incorrect_num_initializers) |
| 1221 | << (numEltsInit < maxElements) << maxElements << numEltsInit; |
| 1222 | hadError = true; |
| 1223 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1226 | void InitListChecker::CheckArrayType(const InitializedEntity &Entity, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 1227 | InitListExpr *IList, QualType &DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1228 | llvm::APSInt elementIndex, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1229 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1230 | unsigned &Index, |
| 1231 | InitListExpr *StructuredList, |
| 1232 | unsigned &StructuredIndex) { |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1233 | const ArrayType *arrayType = SemaRef.Context.getAsArrayType(DeclType); |
| 1234 | |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1235 | // Check for the special-case of initializing an array with a string. |
| 1236 | if (Index < IList->getNumInits()) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1237 | if (IsStringInit(IList->getInit(Index), arrayType, SemaRef.Context) == |
| 1238 | SIF_None) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1239 | // We place the string literal directly into the resulting |
| 1240 | // initializer list. This is the only place where the structure |
| 1241 | // of the structured initializer list doesn't match exactly, |
| 1242 | // because doing so would involve allocating one character |
| 1243 | // constant for each string. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1244 | if (!VerifyOnly) { |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 1245 | CheckStringInit(IList->getInit(Index), DeclType, arrayType, SemaRef); |
| 1246 | UpdateStructuredListElement(StructuredList, StructuredIndex, |
| 1247 | IList->getInit(Index)); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1248 | StructuredList->resizeInits(SemaRef.Context, StructuredIndex); |
| 1249 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1250 | ++Index; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1251 | return; |
| 1252 | } |
| 1253 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1254 | if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(arrayType)) { |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1255 | // Check for VLAs; in standard C it would be possible to check this |
| 1256 | // earlier, but I don't know where clang accepts VLAs (gcc accepts |
| 1257 | // them in all sorts of strange places). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1258 | if (!VerifyOnly) |
| 1259 | SemaRef.Diag(VAT->getSizeExpr()->getLocStart(), |
| 1260 | diag::err_variable_object_no_init) |
| 1261 | << VAT->getSizeExpr()->getSourceRange(); |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1262 | hadError = true; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1263 | ++Index; |
| 1264 | ++StructuredIndex; |
Eli Friedman | 638e144 | 2008-05-25 13:22:35 +0000 | [diff] [blame] | 1265 | return; |
| 1266 | } |
| 1267 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1268 | // We might know the maximum number of elements in advance. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1269 | llvm::APSInt maxElements(elementIndex.getBitWidth(), |
| 1270 | elementIndex.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1271 | bool maxElementsKnown = false; |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1272 | if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(arrayType)) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1273 | maxElements = CAT->getSize(); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1274 | elementIndex = elementIndex.extOrTrunc(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1275 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1276 | maxElementsKnown = true; |
| 1277 | } |
| 1278 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 1279 | QualType elementType = arrayType->getElementType(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1280 | while (Index < IList->getNumInits()) { |
| 1281 | Expr *Init = IList->getInit(Index); |
| 1282 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1283 | // If we're not the subobject that matches up with the '{' for |
| 1284 | // the designator, we shouldn't be handling the |
| 1285 | // designator. Return immediately. |
| 1286 | if (!SubobjectIsDesignatorContext) |
| 1287 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1288 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1289 | // Handle this designated initializer. elementIndex will be |
| 1290 | // updated to be the next array element we'll initialize. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1291 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1292 | DeclType, 0, &elementIndex, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1293 | StructuredList, StructuredIndex, true, |
| 1294 | false)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1295 | hadError = true; |
| 1296 | continue; |
| 1297 | } |
| 1298 | |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1299 | if (elementIndex.getBitWidth() > maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1300 | maxElements = maxElements.extend(elementIndex.getBitWidth()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1301 | else if (elementIndex.getBitWidth() < maxElements.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 1302 | elementIndex = elementIndex.extend(maxElements.getBitWidth()); |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1303 | elementIndex.setIsUnsigned(maxElements.isUnsigned()); |
Douglas Gregor | f6c717c | 2009-01-23 16:54:12 +0000 | [diff] [blame] | 1304 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1305 | // If the array is of incomplete type, keep track of the number of |
| 1306 | // elements in the initializer. |
| 1307 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1308 | maxElements = elementIndex; |
| 1309 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1310 | continue; |
| 1311 | } |
| 1312 | |
| 1313 | // If we know the maximum number of elements, and we've already |
| 1314 | // hit it, stop consuming elements in the initializer list. |
| 1315 | if (maxElementsKnown && elementIndex == maxElements) |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1316 | break; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1317 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1318 | InitializedEntity ElementEntity = |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1319 | InitializedEntity::InitializeElement(SemaRef.Context, StructuredIndex, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1320 | Entity); |
| 1321 | // Check this element. |
| 1322 | CheckSubElementType(ElementEntity, IList, elementType, Index, |
| 1323 | StructuredList, StructuredIndex); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1324 | ++elementIndex; |
| 1325 | |
| 1326 | // If the array is of incomplete type, keep track of the number of |
| 1327 | // elements in the initializer. |
| 1328 | if (!maxElementsKnown && elementIndex > maxElements) |
| 1329 | maxElements = elementIndex; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1330 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1331 | if (!hadError && DeclType->isIncompleteArrayType() && !VerifyOnly) { |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1332 | // If this is an incomplete array type, the actual type needs to |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1333 | // be calculated here. |
Douglas Gregor | e3fa2de | 2009-01-23 18:58:42 +0000 | [diff] [blame] | 1334 | llvm::APSInt Zero(maxElements.getBitWidth(), maxElements.isUnsigned()); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1335 | if (maxElements == Zero) { |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1336 | // Sizing an array implicitly to zero is not allowed by ISO C, |
| 1337 | // but is supported by GNU. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1338 | SemaRef.Diag(IList->getLocStart(), |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1339 | diag::ext_typecheck_zero_array_size); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1340 | } |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1341 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | DeclType = SemaRef.Context.getConstantArrayType(elementType, maxElements, |
Daniel Dunbar | 396f0bf | 2008-08-18 20:28:46 +0000 | [diff] [blame] | 1343 | ArrayType::Normal, 0); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1344 | } |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1345 | if (!hadError && VerifyOnly) { |
| 1346 | // Check if there are any members of the array that get value-initialized. |
| 1347 | // If so, check if doing that is possible. |
| 1348 | // FIXME: This needs to detect holes left by designated initializers too. |
| 1349 | if (maxElementsKnown && elementIndex < maxElements) |
| 1350 | CheckValueInitializable(InitializedEntity::InitializeElement( |
| 1351 | SemaRef.Context, 0, Entity)); |
| 1352 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1355 | bool InitListChecker::CheckFlexibleArrayInit(const InitializedEntity &Entity, |
| 1356 | Expr *InitExpr, |
| 1357 | FieldDecl *Field, |
| 1358 | bool TopLevelObject) { |
| 1359 | // Handle GNU flexible array initializers. |
| 1360 | unsigned FlexArrayDiag; |
| 1361 | if (isa<InitListExpr>(InitExpr) && |
| 1362 | cast<InitListExpr>(InitExpr)->getNumInits() == 0) { |
| 1363 | // Empty flexible array init always allowed as an extension |
| 1364 | FlexArrayDiag = diag::ext_flexible_array_init; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1365 | } else if (SemaRef.getLangOpts().CPlusPlus) { |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1366 | // Disallow flexible array init in C++; it is not required for gcc |
| 1367 | // compatibility, and it needs work to IRGen correctly in general. |
| 1368 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1369 | } else if (!TopLevelObject) { |
| 1370 | // Disallow flexible array init on non-top-level object |
| 1371 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1372 | } else if (Entity.getKind() != InitializedEntity::EK_Variable) { |
| 1373 | // Disallow flexible array init on anything which is not a variable. |
| 1374 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1375 | } else if (cast<VarDecl>(Entity.getDecl())->hasLocalStorage()) { |
| 1376 | // Disallow flexible array init on local variables. |
| 1377 | FlexArrayDiag = diag::err_flexible_array_init; |
| 1378 | } else { |
| 1379 | // Allow other cases. |
| 1380 | FlexArrayDiag = diag::ext_flexible_array_init; |
| 1381 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1382 | |
| 1383 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1384 | SemaRef.Diag(InitExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1385 | FlexArrayDiag) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1386 | << InitExpr->getLocStart(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1387 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
| 1388 | << Field; |
| 1389 | } |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1390 | |
| 1391 | return FlexArrayDiag != diag::ext_flexible_array_init; |
| 1392 | } |
| 1393 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1394 | void InitListChecker::CheckStructUnionTypes(const InitializedEntity &Entity, |
Anders Carlsson | 2bbae5d | 2010-01-23 20:20:40 +0000 | [diff] [blame] | 1395 | InitListExpr *IList, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1396 | QualType DeclType, |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1397 | RecordDecl::field_iterator Field, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1398 | bool SubobjectIsDesignatorContext, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1399 | unsigned &Index, |
| 1400 | InitListExpr *StructuredList, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1401 | unsigned &StructuredIndex, |
| 1402 | bool TopLevelObject) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1403 | RecordDecl* structDecl = DeclType->getAs<RecordType>()->getDecl(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1405 | // If the record is invalid, some of it's members are invalid. To avoid |
| 1406 | // confusion, we forgo checking the intializer for the entire record. |
| 1407 | if (structDecl->isInvalidDecl()) { |
Richard Smith | 72ab277 | 2012-09-28 21:23:50 +0000 | [diff] [blame] | 1408 | // Assume it was supposed to consume a single initializer. |
| 1409 | ++Index; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1410 | hadError = true; |
| 1411 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (DeclType->isUnionType() && IList->getNumInits() == 0) { |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1415 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1416 | |
| 1417 | // If there's a default initializer, use it. |
| 1418 | if (isa<CXXRecordDecl>(RD) && cast<CXXRecordDecl>(RD)->hasInClassInitializer()) { |
| 1419 | if (VerifyOnly) |
| 1420 | return; |
| 1421 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1422 | Field != FieldEnd; ++Field) { |
| 1423 | if (Field->hasInClassInitializer()) { |
| 1424 | StructuredList->setInitializedFieldInUnion(*Field); |
| 1425 | // FIXME: Actually build a CXXDefaultInitExpr? |
| 1426 | return; |
| 1427 | } |
| 1428 | } |
| 1429 | } |
| 1430 | |
| 1431 | // Value-initialize the first named member of the union. |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1432 | for (RecordDecl::field_iterator FieldEnd = RD->field_end(); |
| 1433 | Field != FieldEnd; ++Field) { |
| 1434 | if (Field->getDeclName()) { |
| 1435 | if (VerifyOnly) |
| 1436 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1437 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1438 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1439 | StructuredList->setInitializedFieldInUnion(*Field); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1440 | break; |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | return; |
| 1444 | } |
| 1445 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1446 | // If structDecl is a forward declaration, this loop won't do |
| 1447 | // anything except look at designated initializers; That's okay, |
| 1448 | // because an error should get printed out elsewhere. It might be |
| 1449 | // worthwhile to skip over the rest of the initializer, though. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1450 | RecordDecl *RD = DeclType->getAs<RecordType>()->getDecl(); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1451 | RecordDecl::field_iterator FieldEnd = RD->field_end(); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1452 | bool InitializedSomething = false; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1453 | bool CheckForMissingFields = true; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1454 | while (Index < IList->getNumInits()) { |
| 1455 | Expr *Init = IList->getInit(Index); |
| 1456 | |
| 1457 | if (DesignatedInitExpr *DIE = dyn_cast<DesignatedInitExpr>(Init)) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1458 | // If we're not the subobject that matches up with the '{' for |
| 1459 | // the designator, we shouldn't be handling the |
| 1460 | // designator. Return immediately. |
| 1461 | if (!SubobjectIsDesignatorContext) |
| 1462 | return; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1463 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1464 | // Handle this designated initializer. Field will be updated to |
| 1465 | // the next field that we'll be initializing. |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1466 | if (CheckDesignatedInitializer(Entity, IList, DIE, 0, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1467 | DeclType, &Field, 0, Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1468 | StructuredList, StructuredIndex, |
| 1469 | true, TopLevelObject)) |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1470 | hadError = true; |
| 1471 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1472 | InitializedSomething = true; |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1473 | |
| 1474 | // Disable check for missing fields when designators are used. |
| 1475 | // This matches gcc behaviour. |
| 1476 | CheckForMissingFields = false; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1477 | continue; |
| 1478 | } |
| 1479 | |
| 1480 | if (Field == FieldEnd) { |
| 1481 | // We've run out of fields. We're done. |
| 1482 | break; |
| 1483 | } |
| 1484 | |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1485 | // We've already initialized a member of a union. We're done. |
| 1486 | if (InitializedSomething && DeclType->isUnionType()) |
| 1487 | break; |
| 1488 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1489 | // If we've hit the flexible array member at the end, we're done. |
| 1490 | if (Field->getType()->isIncompleteArrayType()) |
| 1491 | break; |
| 1492 | |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1493 | if (Field->isUnnamedBitfield()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1494 | // Don't initialize unnamed bitfields, e.g. "int : 20;" |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1495 | ++Field; |
Eli Friedman | b85f707 | 2008-05-19 19:16:24 +0000 | [diff] [blame] | 1496 | continue; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1497 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1498 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1499 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1500 | bool InvalidUse; |
| 1501 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1502 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1503 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1504 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1505 | IList->getInit(Index)->getLocStart()); |
| 1506 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1507 | ++Index; |
| 1508 | ++Field; |
| 1509 | hadError = true; |
| 1510 | continue; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1511 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1512 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1513 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1514 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1515 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
| 1516 | StructuredList, StructuredIndex); |
Douglas Gregor | dfb5e59 | 2009-02-12 19:00:39 +0000 | [diff] [blame] | 1517 | InitializedSomething = true; |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1518 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1519 | if (DeclType->isUnionType() && !VerifyOnly) { |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1520 | // Initialize the first field within the union. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1521 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1522 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1523 | |
| 1524 | ++Field; |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1525 | } |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 1526 | |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1527 | // Emit warnings for missing struct field initializers. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1528 | if (!VerifyOnly && InitializedSomething && CheckForMissingFields && |
| 1529 | Field != FieldEnd && !Field->getType()->isIncompleteArrayType() && |
| 1530 | !DeclType->isUnionType()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1531 | // It is possible we have one or more unnamed bitfields remaining. |
| 1532 | // Find first (if any) named field and emit warning. |
| 1533 | for (RecordDecl::field_iterator it = Field, end = RD->field_end(); |
| 1534 | it != end; ++it) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1535 | if (!it->isUnnamedBitfield() && !it->hasInClassInitializer()) { |
John McCall | 80639de | 2010-03-11 19:32:38 +0000 | [diff] [blame] | 1536 | SemaRef.Diag(IList->getSourceRange().getEnd(), |
| 1537 | diag::warn_missing_field_initializers) << it->getName(); |
| 1538 | break; |
| 1539 | } |
| 1540 | } |
| 1541 | } |
| 1542 | |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1543 | // Check that any remaining fields can be value-initialized. |
| 1544 | if (VerifyOnly && Field != FieldEnd && !DeclType->isUnionType() && |
| 1545 | !Field->getType()->isIncompleteArrayType()) { |
| 1546 | // FIXME: Should check for holes left by designated initializers too. |
| 1547 | for (; Field != FieldEnd && !hadError; ++Field) { |
Richard Smith | c3bf52c | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 1548 | if (!Field->isUnnamedBitfield() && !Field->hasInClassInitializer()) |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1549 | CheckValueInitializable( |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1550 | InitializedEntity::InitializeMember(*Field, &Entity)); |
Sebastian Redl | 3ff5c86 | 2011-10-16 18:19:20 +0000 | [diff] [blame] | 1551 | } |
| 1552 | } |
| 1553 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1554 | if (Field == FieldEnd || !Field->getType()->isIncompleteArrayType() || |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1555 | Index >= IList->getNumInits()) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1556 | return; |
| 1557 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1558 | if (CheckFlexibleArrayInit(Entity, IList->getInit(Index), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1559 | TopLevelObject)) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1560 | hadError = true; |
Douglas Gregor | a645796 | 2009-03-20 00:32:56 +0000 | [diff] [blame] | 1561 | ++Index; |
| 1562 | return; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1565 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1566 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1567 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1568 | if (isa<InitListExpr>(IList->getInit(Index))) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1569 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1570 | StructuredList, StructuredIndex); |
| 1571 | else |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1572 | CheckImplicitInitList(MemberEntity, IList, Field->getType(), Index, |
Anders Carlsson | 987dc6a | 2010-01-23 20:47:59 +0000 | [diff] [blame] | 1573 | StructuredList, StructuredIndex); |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1574 | } |
Steve Naroff | 0cca749 | 2008-05-01 22:18:59 +0000 | [diff] [blame] | 1575 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1576 | /// \brief Expand a field designator that refers to a member of an |
| 1577 | /// anonymous struct or union into a series of field designators that |
| 1578 | /// refers to the field within the appropriate subobject. |
| 1579 | /// |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1580 | static void ExpandAnonymousFieldDesignator(Sema &SemaRef, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | DesignatedInitExpr *DIE, |
| 1582 | unsigned DesigIdx, |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1583 | IndirectFieldDecl *IndirectField) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1584 | typedef DesignatedInitExpr::Designator Designator; |
| 1585 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1586 | // Build the replacement designators. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1587 | SmallVector<Designator, 4> Replacements; |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1588 | for (IndirectFieldDecl::chain_iterator PI = IndirectField->chain_begin(), |
| 1589 | PE = IndirectField->chain_end(); PI != PE; ++PI) { |
| 1590 | if (PI + 1 == PE) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1591 | Replacements.push_back(Designator((IdentifierInfo *)0, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1592 | DIE->getDesignator(DesigIdx)->getDotLoc(), |
| 1593 | DIE->getDesignator(DesigIdx)->getFieldLoc())); |
| 1594 | else |
| 1595 | Replacements.push_back(Designator((IdentifierInfo *)0, SourceLocation(), |
| 1596 | SourceLocation())); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1597 | assert(isa<FieldDecl>(*PI)); |
| 1598 | Replacements.back().setField(cast<FieldDecl>(*PI)); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | // Expand the current designator into the set of replacement |
| 1602 | // designators, so we have a full subobject path down to where the |
| 1603 | // member of the anonymous struct/union is actually stored. |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 1604 | DIE->ExpandDesignator(SemaRef.Context, DesigIdx, &Replacements[0], |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1605 | &Replacements[0] + Replacements.size()); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1606 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1608 | /// \brief Given an implicit anonymous field, search the IndirectField that |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1609 | /// corresponds to FieldName. |
| 1610 | static IndirectFieldDecl *FindIndirectFieldDesignator(FieldDecl *AnonField, |
| 1611 | IdentifierInfo *FieldName) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1612 | if (!FieldName) |
| 1613 | return 0; |
| 1614 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1615 | assert(AnonField->isAnonymousStructOrUnion()); |
| 1616 | Decl *NextDecl = AnonField->getNextDeclInContext(); |
Aaron Ballman | 3e78b19 | 2012-02-09 22:16:56 +0000 | [diff] [blame] | 1617 | while (IndirectFieldDecl *IF = |
| 1618 | dyn_cast_or_null<IndirectFieldDecl>(NextDecl)) { |
Argyrios Kyrtzidis | b22b0a5 | 2012-09-10 22:04:26 +0000 | [diff] [blame] | 1619 | if (FieldName == IF->getAnonField()->getIdentifier()) |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1620 | return IF; |
| 1621 | NextDecl = NextDecl->getNextDeclInContext(); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1622 | } |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1623 | return 0; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1626 | static DesignatedInitExpr *CloneDesignatedInitExpr(Sema &SemaRef, |
| 1627 | DesignatedInitExpr *DIE) { |
| 1628 | unsigned NumIndexExprs = DIE->getNumSubExprs() - 1; |
| 1629 | SmallVector<Expr*, 4> IndexExprs(NumIndexExprs); |
| 1630 | for (unsigned I = 0; I < NumIndexExprs; ++I) |
| 1631 | IndexExprs[I] = DIE->getSubExpr(I + 1); |
| 1632 | return DesignatedInitExpr::Create(SemaRef.Context, DIE->designators_begin(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1633 | DIE->size(), IndexExprs, |
| 1634 | DIE->getEqualOrColonLoc(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1635 | DIE->usesGNUSyntax(), DIE->getInit()); |
| 1636 | } |
| 1637 | |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1638 | namespace { |
| 1639 | |
| 1640 | // Callback to only accept typo corrections that are for field members of |
| 1641 | // the given struct or union. |
| 1642 | class FieldInitializerValidatorCCC : public CorrectionCandidateCallback { |
| 1643 | public: |
| 1644 | explicit FieldInitializerValidatorCCC(RecordDecl *RD) |
| 1645 | : Record(RD) {} |
| 1646 | |
| 1647 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1648 | FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>(); |
| 1649 | return FD && FD->getDeclContext()->getRedeclContext()->Equals(Record); |
| 1650 | } |
| 1651 | |
| 1652 | private: |
| 1653 | RecordDecl *Record; |
| 1654 | }; |
| 1655 | |
| 1656 | } |
| 1657 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1658 | /// @brief Check the well-formedness of a C99 designated initializer. |
| 1659 | /// |
| 1660 | /// Determines whether the designated initializer @p DIE, which |
| 1661 | /// resides at the given @p Index within the initializer list @p |
| 1662 | /// IList, is well-formed for a current object of type @p DeclType |
| 1663 | /// (C99 6.7.8). The actual subobject that this designator refers to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1664 | /// within the current subobject is returned in either |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1665 | /// @p NextField or @p NextElementIndex (whichever is appropriate). |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1666 | /// |
| 1667 | /// @param IList The initializer list in which this designated |
| 1668 | /// initializer occurs. |
| 1669 | /// |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1670 | /// @param DIE The designated initializer expression. |
| 1671 | /// |
| 1672 | /// @param DesigIdx The index of the current designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1673 | /// |
Dmitri Gribenko | 70517ca | 2012-08-23 17:58:28 +0000 | [diff] [blame] | 1674 | /// @param CurrentObjectType The type of the "current object" (C99 6.7.8p17), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1675 | /// into which the designation in @p DIE should refer. |
| 1676 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1677 | /// @param NextField If non-NULL and the first designator in @p DIE is |
| 1678 | /// a field, this will be set to the field declaration corresponding |
| 1679 | /// to the field named by the designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1680 | /// |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1681 | /// @param NextElementIndex If non-NULL and the first designator in @p |
| 1682 | /// DIE is an array designator or GNU array-range designator, this |
| 1683 | /// will be set to the last index initialized by this designator. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1684 | /// |
| 1685 | /// @param Index Index into @p IList where the designated initializer |
| 1686 | /// @p DIE occurs. |
| 1687 | /// |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1688 | /// @param StructuredList The initializer list expression that |
| 1689 | /// describes all of the subobject initializers in the order they'll |
| 1690 | /// actually be initialized. |
| 1691 | /// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1692 | /// @returns true if there was an error, false otherwise. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | bool |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1694 | InitListChecker::CheckDesignatedInitializer(const InitializedEntity &Entity, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1695 | InitListExpr *IList, |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1696 | DesignatedInitExpr *DIE, |
| 1697 | unsigned DesigIdx, |
| 1698 | QualType &CurrentObjectType, |
| 1699 | RecordDecl::field_iterator *NextField, |
| 1700 | llvm::APSInt *NextElementIndex, |
| 1701 | unsigned &Index, |
| 1702 | InitListExpr *StructuredList, |
| 1703 | unsigned &StructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1704 | bool FinishSubobjectInit, |
| 1705 | bool TopLevelObject) { |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1706 | if (DesigIdx == DIE->size()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1707 | // Check the actual initialization for the designated object type. |
| 1708 | bool prevHadError = hadError; |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1709 | |
| 1710 | // Temporarily remove the designator expression from the |
| 1711 | // initializer list that the child calls see, so that we don't try |
| 1712 | // to re-process the designator. |
| 1713 | unsigned OldIndex = Index; |
| 1714 | IList->setInit(OldIndex, DIE->getInit()); |
| 1715 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1716 | CheckSubElementType(Entity, IList, CurrentObjectType, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1717 | StructuredList, StructuredIndex); |
Douglas Gregor | 6fbdc6b | 2009-01-29 00:39:20 +0000 | [diff] [blame] | 1718 | |
| 1719 | // Restore the designated initializer expression in the syntactic |
| 1720 | // form of the initializer list. |
| 1721 | if (IList->getInit(OldIndex) != DIE->getInit()) |
| 1722 | DIE->setInit(IList->getInit(OldIndex)); |
| 1723 | IList->setInit(OldIndex, DIE); |
| 1724 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1725 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1728 | DesignatedInitExpr::Designator *D = DIE->getDesignator(DesigIdx); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1729 | bool IsFirstDesignator = (DesigIdx == 0); |
| 1730 | if (!VerifyOnly) { |
| 1731 | assert((IsFirstDesignator || StructuredList) && |
| 1732 | "Need a non-designated initializer list to start from"); |
| 1733 | |
| 1734 | // Determine the structural initializer list that corresponds to the |
| 1735 | // current subobject. |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 1736 | StructuredList = IsFirstDesignator? SyntacticToSemantic.lookup(IList) |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1737 | : getStructuredSubobjectInit(IList, Index, CurrentObjectType, |
| 1738 | StructuredList, StructuredIndex, |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1739 | SourceRange(D->getLocStart(), |
| 1740 | DIE->getLocEnd())); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1741 | assert(StructuredList && "Expected a structured initializer list"); |
| 1742 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1743 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1744 | if (D->isFieldDesignator()) { |
| 1745 | // C99 6.7.8p7: |
| 1746 | // |
| 1747 | // If a designator has the form |
| 1748 | // |
| 1749 | // . identifier |
| 1750 | // |
| 1751 | // then the current object (defined below) shall have |
| 1752 | // structure or union type and the identifier shall be the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1753 | // name of a member of that type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1754 | const RecordType *RT = CurrentObjectType->getAs<RecordType>(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1755 | if (!RT) { |
| 1756 | SourceLocation Loc = D->getDotLoc(); |
| 1757 | if (Loc.isInvalid()) |
| 1758 | Loc = D->getFieldLoc(); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1759 | if (!VerifyOnly) |
| 1760 | SemaRef.Diag(Loc, diag::err_field_designator_non_aggr) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1761 | << SemaRef.getLangOpts().CPlusPlus << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1762 | ++Index; |
| 1763 | return true; |
| 1764 | } |
| 1765 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1766 | // Note: we perform a linear search of the fields here, despite |
| 1767 | // the fact that we have a faster lookup method, because we always |
| 1768 | // need to compute the field's index. |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1769 | FieldDecl *KnownField = D->getField(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1770 | IdentifierInfo *FieldName = D->getFieldName(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1771 | unsigned FieldIndex = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | RecordDecl::field_iterator |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1773 | Field = RT->getDecl()->field_begin(), |
| 1774 | FieldEnd = RT->getDecl()->field_end(); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1775 | for (; Field != FieldEnd; ++Field) { |
| 1776 | if (Field->isUnnamedBitfield()) |
| 1777 | continue; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1778 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1779 | // If we find a field representing an anonymous field, look in the |
| 1780 | // IndirectFieldDecl that follow for the designated initializer. |
| 1781 | if (!KnownField && Field->isAnonymousStructOrUnion()) { |
| 1782 | if (IndirectFieldDecl *IF = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1783 | FindIndirectFieldDesignator(*Field, FieldName)) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1784 | // In verify mode, don't modify the original. |
| 1785 | if (VerifyOnly) |
| 1786 | DIE = CloneDesignatedInitExpr(SemaRef, DIE); |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1787 | ExpandAnonymousFieldDesignator(SemaRef, DIE, DesigIdx, IF); |
| 1788 | D = DIE->getDesignator(DesigIdx); |
| 1789 | break; |
| 1790 | } |
| 1791 | } |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1792 | if (KnownField && KnownField == *Field) |
Douglas Gregor | 022d13d | 2010-10-08 20:44:28 +0000 | [diff] [blame] | 1793 | break; |
| 1794 | if (FieldName && FieldName == Field->getIdentifier()) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1795 | break; |
| 1796 | |
| 1797 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1800 | if (Field == FieldEnd) { |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1801 | if (VerifyOnly) { |
| 1802 | ++Index; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1803 | return true; // No typo correction when just trying this out. |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1804 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1805 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1806 | // There was no normal field in the struct with the designated |
| 1807 | // name. Perform another lookup for this name, which may find |
| 1808 | // something that we can't designate (e.g., a member function), |
| 1809 | // may find nothing, or may find a member of an anonymous |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1810 | // struct/union. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1811 | DeclContext::lookup_result Lookup = RT->getDecl()->lookup(FieldName); |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1812 | FieldDecl *ReplacementField = 0; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1813 | if (Lookup.empty()) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1814 | // Name lookup didn't find anything. Determine whether this |
| 1815 | // was a typo for another field name. |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1816 | FieldInitializerValidatorCCC Validator(RT->getDecl()); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1817 | TypoCorrection Corrected = SemaRef.CorrectTypo( |
| 1818 | DeclarationNameInfo(FieldName, D->getFieldLoc()), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1819 | Sema::LookupMemberName, /*Scope=*/0, /*SS=*/0, Validator, |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1820 | RT->getDecl()); |
| 1821 | if (Corrected) { |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1822 | std::string CorrectedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1823 | Corrected.getAsString(SemaRef.getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1824 | std::string CorrectedQuotedStr( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1825 | Corrected.getQuoted(SemaRef.getLangOpts())); |
Kaelyn Uhrain | 425d631 | 2012-01-12 19:27:05 +0000 | [diff] [blame] | 1826 | ReplacementField = Corrected.getCorrectionDeclAs<FieldDecl>(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1827 | SemaRef.Diag(D->getFieldLoc(), |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1828 | diag::err_field_designator_unknown_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1829 | << FieldName << CurrentObjectType << CorrectedQuotedStr |
| 1830 | << FixItHint::CreateReplacement(D->getFieldLoc(), CorrectedStr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1831 | SemaRef.Diag(ReplacementField->getLocation(), |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1832 | diag::note_previous_decl) << CorrectedQuotedStr; |
Benjamin Kramer | a41ee49 | 2011-09-25 02:41:26 +0000 | [diff] [blame] | 1833 | hadError = true; |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1834 | } else { |
| 1835 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_unknown) |
| 1836 | << FieldName << CurrentObjectType; |
| 1837 | ++Index; |
| 1838 | return true; |
| 1839 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1840 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1841 | |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1842 | if (!ReplacementField) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1843 | // Name lookup found something, but it wasn't a field. |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 1844 | SemaRef.Diag(D->getFieldLoc(), diag::err_field_designator_nonfield) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1845 | << FieldName; |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 1846 | SemaRef.Diag(Lookup.front()->getLocation(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1847 | diag::note_field_designator_found); |
Eli Friedman | ba79fc2 | 2009-04-16 17:49:48 +0000 | [diff] [blame] | 1848 | ++Index; |
| 1849 | return true; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1850 | } |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1851 | |
Francois Pichet | a0e27f0 | 2010-12-22 03:46:10 +0000 | [diff] [blame] | 1852 | if (!KnownField) { |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1853 | // The replacement field comes from typo correction; find it |
| 1854 | // in the list of fields. |
| 1855 | FieldIndex = 0; |
| 1856 | Field = RT->getDecl()->field_begin(); |
| 1857 | for (; Field != FieldEnd; ++Field) { |
| 1858 | if (Field->isUnnamedBitfield()) |
| 1859 | continue; |
| 1860 | |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1861 | if (ReplacementField == *Field || |
Douglas Gregor | c171e3b | 2010-01-01 00:03:05 +0000 | [diff] [blame] | 1862 | Field->getIdentifier() == ReplacementField->getIdentifier()) |
| 1863 | break; |
| 1864 | |
| 1865 | ++FieldIndex; |
| 1866 | } |
| 1867 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1868 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1869 | |
| 1870 | // All of the fields of a union are located at the same place in |
| 1871 | // the initializer list. |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1872 | if (RT->getDecl()->isUnion()) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1873 | FieldIndex = 0; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1874 | if (!VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1875 | StructuredList->setInitializedFieldInUnion(*Field); |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 1876 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1877 | |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1878 | // Make sure we can use this declaration. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1879 | bool InvalidUse; |
| 1880 | if (VerifyOnly) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1881 | InvalidUse = !SemaRef.CanUseDecl(*Field); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1882 | else |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1883 | InvalidUse = SemaRef.DiagnoseUseOfDecl(*Field, D->getFieldLoc()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1884 | if (InvalidUse) { |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1885 | ++Index; |
| 1886 | return true; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1887 | } |
Douglas Gregor | 54001c1 | 2011-06-29 21:51:31 +0000 | [diff] [blame] | 1888 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1889 | if (!VerifyOnly) { |
| 1890 | // Update the designator with the field declaration. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1891 | D->setField(*Field); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1892 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1893 | // Make sure that our non-designated initializer list has space |
| 1894 | // for a subobject corresponding to this field. |
| 1895 | if (FieldIndex >= StructuredList->getNumInits()) |
| 1896 | StructuredList->resizeInits(SemaRef.Context, FieldIndex + 1); |
| 1897 | } |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1898 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1899 | // This designator names a flexible array member. |
| 1900 | if (Field->getType()->isIncompleteArrayType()) { |
| 1901 | bool Invalid = false; |
Douglas Gregor | 7119971 | 2009-04-15 04:56:10 +0000 | [diff] [blame] | 1902 | if ((DesigIdx + 1) != DIE->size()) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1903 | // We can't designate an object within the flexible array |
| 1904 | // member (because GCC doesn't allow it). |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1905 | if (!VerifyOnly) { |
| 1906 | DesignatedInitExpr::Designator *NextD |
| 1907 | = DIE->getDesignator(DesigIdx + 1); |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1908 | SemaRef.Diag(NextD->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1909 | diag::err_designator_into_flexible_array_member) |
Erik Verbruggen | 65d7831 | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1910 | << SourceRange(NextD->getLocStart(), |
| 1911 | DIE->getLocEnd()); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1912 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1913 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1914 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1915 | Invalid = true; |
| 1916 | } |
| 1917 | |
Chris Lattner | 9046c22 | 2010-10-10 17:49:49 +0000 | [diff] [blame] | 1918 | if (!hadError && !isa<InitListExpr>(DIE->getInit()) && |
| 1919 | !isa<StringLiteral>(DIE->getInit())) { |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1920 | // The initializer is not an initializer list. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1921 | if (!VerifyOnly) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 1922 | SemaRef.Diag(DIE->getInit()->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1923 | diag::err_flexible_array_init_needs_braces) |
| 1924 | << DIE->getInit()->getSourceRange(); |
| 1925 | SemaRef.Diag(Field->getLocation(), diag::note_flexible_array_member) |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1926 | << *Field; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 1927 | } |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1928 | Invalid = true; |
| 1929 | } |
| 1930 | |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1931 | // Check GNU flexible array initializer. |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1932 | if (!Invalid && CheckFlexibleArrayInit(Entity, DIE->getInit(), *Field, |
Eli Friedman | f40fd6b | 2011-08-23 22:24:57 +0000 | [diff] [blame] | 1933 | TopLevelObject)) |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1934 | Invalid = true; |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1935 | |
| 1936 | if (Invalid) { |
| 1937 | ++Index; |
| 1938 | return true; |
| 1939 | } |
| 1940 | |
| 1941 | // Initialize the array. |
| 1942 | bool prevHadError = hadError; |
| 1943 | unsigned newStructuredIndex = FieldIndex; |
| 1944 | unsigned OldIndex = Index; |
| 1945 | IList->setInit(Index, DIE->getInit()); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1946 | |
| 1947 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1948 | InitializedEntity::InitializeMember(*Field, &Entity); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1949 | CheckSubElementType(MemberEntity, IList, Field->getType(), Index, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1950 | StructuredList, newStructuredIndex); |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1952 | IList->setInit(OldIndex, DIE); |
| 1953 | if (hadError && !prevHadError) { |
| 1954 | ++Field; |
| 1955 | ++FieldIndex; |
| 1956 | if (NextField) |
| 1957 | *NextField = Field; |
| 1958 | StructuredIndex = FieldIndex; |
| 1959 | return true; |
| 1960 | } |
| 1961 | } else { |
| 1962 | // Recurse to check later designated subobjects. |
David Blaikie | 262bc18 | 2012-04-30 02:36:29 +0000 | [diff] [blame] | 1963 | QualType FieldType = Field->getType(); |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1964 | unsigned newStructuredIndex = FieldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1965 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1966 | InitializedEntity MemberEntity = |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 1967 | InitializedEntity::InitializeMember(*Field, &Entity); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1968 | if (CheckDesignatedInitializer(MemberEntity, IList, DIE, DesigIdx + 1, |
| 1969 | FieldType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 1970 | StructuredList, newStructuredIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 1971 | true, false)) |
| 1972 | return true; |
| 1973 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1974 | |
| 1975 | // Find the position of the next field to be initialized in this |
| 1976 | // subobject. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1977 | ++Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1978 | ++FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1979 | |
| 1980 | // If this the first designator, our caller will continue checking |
| 1981 | // the rest of this struct/class/union subobject. |
| 1982 | if (IsFirstDesignator) { |
| 1983 | if (NextField) |
| 1984 | *NextField = Field; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1985 | StructuredIndex = FieldIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1986 | return false; |
| 1987 | } |
| 1988 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 1989 | if (!FinishSubobjectInit) |
| 1990 | return false; |
| 1991 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 1992 | // We've already initialized something in the union; we're done. |
| 1993 | if (RT->getDecl()->isUnion()) |
| 1994 | return hadError; |
| 1995 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 1996 | // Check the remaining fields within this class/struct/union subobject. |
| 1997 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 1998 | |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 1999 | CheckStructUnionTypes(Entity, IList, CurrentObjectType, Field, false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2000 | StructuredList, FieldIndex); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2001 | return hadError && !prevHadError; |
| 2002 | } |
| 2003 | |
| 2004 | // C99 6.7.8p6: |
| 2005 | // |
| 2006 | // If a designator has the form |
| 2007 | // |
| 2008 | // [ constant-expression ] |
| 2009 | // |
| 2010 | // then the current object (defined below) shall have array |
| 2011 | // type and the expression shall be an integer constant |
| 2012 | // expression. If the array is of unknown size, any |
| 2013 | // nonnegative value is valid. |
| 2014 | // |
| 2015 | // Additionally, cope with the GNU extension that permits |
| 2016 | // designators of the form |
| 2017 | // |
| 2018 | // [ constant-expression ... constant-expression ] |
Chris Lattner | 0820254 | 2009-02-24 22:50:46 +0000 | [diff] [blame] | 2019 | const ArrayType *AT = SemaRef.Context.getAsArrayType(CurrentObjectType); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2020 | if (!AT) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2021 | if (!VerifyOnly) |
| 2022 | SemaRef.Diag(D->getLBracketLoc(), diag::err_array_designator_non_array) |
| 2023 | << CurrentObjectType; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2024 | ++Index; |
| 2025 | return true; |
| 2026 | } |
| 2027 | |
| 2028 | Expr *IndexExpr = 0; |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2029 | llvm::APSInt DesignatedStartIndex, DesignatedEndIndex; |
| 2030 | if (D->isArrayDesignator()) { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2031 | IndexExpr = DIE->getArrayIndex(*D); |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2032 | DesignatedStartIndex = IndexExpr->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2033 | DesignatedEndIndex = DesignatedStartIndex; |
| 2034 | } else { |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2035 | assert(D->isArrayRangeDesignator() && "Need array-range designator"); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2036 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | DesignatedStartIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2038 | DIE->getArrayRangeStart(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2039 | DesignatedEndIndex = |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 2040 | DIE->getArrayRangeEnd(*D)->EvaluateKnownConstInt(SemaRef.Context); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2041 | IndexExpr = DIE->getArrayRangeEnd(*D); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2042 | |
Chris Lattner | e0fd832 | 2011-02-19 22:28:58 +0000 | [diff] [blame] | 2043 | // Codegen can't handle evaluating array range designators that have side |
| 2044 | // effects, because we replicate the AST value for each initialized element. |
| 2045 | // As such, set the sawArrayRangeDesignator() bit if we initialize multiple |
| 2046 | // elements with something that has a side effect, so codegen can emit an |
| 2047 | // "error unsupported" error instead of miscompiling the app. |
| 2048 | if (DesignatedStartIndex.getZExtValue()!=DesignatedEndIndex.getZExtValue()&& |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2049 | DIE->getInit()->HasSideEffects(SemaRef.Context) && !VerifyOnly) |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 2050 | FullyStructuredList->sawArrayRangeDesignator(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2051 | } |
| 2052 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2053 | if (isa<ConstantArrayType>(AT)) { |
| 2054 | llvm::APSInt MaxElements(cast<ConstantArrayType>(AT)->getSize(), false); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2055 | DesignatedStartIndex |
| 2056 | = DesignatedStartIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2057 | DesignatedStartIndex.setIsUnsigned(MaxElements.isUnsigned()); |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2058 | DesignatedEndIndex |
| 2059 | = DesignatedEndIndex.extOrTrunc(MaxElements.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2060 | DesignatedEndIndex.setIsUnsigned(MaxElements.isUnsigned()); |
| 2061 | if (DesignatedEndIndex >= MaxElements) { |
Eli Friedman | a4e20e1 | 2011-09-26 18:53:43 +0000 | [diff] [blame] | 2062 | if (!VerifyOnly) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2063 | SemaRef.Diag(IndexExpr->getLocStart(), |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2064 | diag::err_array_designator_too_large) |
| 2065 | << DesignatedEndIndex.toString(10) << MaxElements.toString(10) |
| 2066 | << IndexExpr->getSourceRange(); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2067 | ++Index; |
| 2068 | return true; |
| 2069 | } |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2070 | } else { |
| 2071 | // Make sure the bit-widths and signedness match. |
| 2072 | if (DesignatedStartIndex.getBitWidth() > DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2073 | DesignatedEndIndex |
| 2074 | = DesignatedEndIndex.extend(DesignatedStartIndex.getBitWidth()); |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2075 | else if (DesignatedStartIndex.getBitWidth() < |
| 2076 | DesignatedEndIndex.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2077 | DesignatedStartIndex |
| 2078 | = DesignatedStartIndex.extend(DesignatedEndIndex.getBitWidth()); |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2079 | DesignatedStartIndex.setIsUnsigned(true); |
| 2080 | DesignatedEndIndex.setIsUnsigned(true); |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2081 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2083 | // Make sure that our non-designated initializer list has space |
| 2084 | // for a subobject corresponding to this array element. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2085 | if (!VerifyOnly && |
| 2086 | DesignatedEndIndex.getZExtValue() >= StructuredList->getNumInits()) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2087 | StructuredList->resizeInits(SemaRef.Context, |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2088 | DesignatedEndIndex.getZExtValue() + 1); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2089 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2090 | // Repeatedly perform subobject initializations in the range |
| 2091 | // [DesignatedStartIndex, DesignatedEndIndex]. |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2092 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2093 | // Move to the next designator |
| 2094 | unsigned ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2095 | unsigned OldIndex = Index; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2096 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2097 | InitializedEntity ElementEntity = |
Anders Carlsson | 8ff9e86 | 2010-01-23 23:23:01 +0000 | [diff] [blame] | 2098 | InitializedEntity::InitializeElement(SemaRef.Context, 0, Entity); |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2099 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2100 | while (DesignatedStartIndex <= DesignatedEndIndex) { |
| 2101 | // Recurse to check later designated subobjects. |
| 2102 | QualType ElementType = AT->getElementType(); |
| 2103 | Index = OldIndex; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2104 | |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2105 | ElementEntity.setElementIndex(ElementIndex); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2106 | if (CheckDesignatedInitializer(ElementEntity, IList, DIE, DesigIdx + 1, |
| 2107 | ElementType, 0, 0, Index, |
Anders Carlsson | 9a8a70e | 2010-01-23 22:49:02 +0000 | [diff] [blame] | 2108 | StructuredList, ElementIndex, |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 2109 | (DesignatedStartIndex == DesignatedEndIndex), |
| 2110 | false)) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2111 | return true; |
| 2112 | |
| 2113 | // Move to the next index in the array that we'll be initializing. |
| 2114 | ++DesignatedStartIndex; |
| 2115 | ElementIndex = DesignatedStartIndex.getZExtValue(); |
| 2116 | } |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2117 | |
| 2118 | // If this the first designator, our caller will continue checking |
| 2119 | // the rest of this array subobject. |
| 2120 | if (IsFirstDesignator) { |
| 2121 | if (NextElementIndex) |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2122 | *NextElementIndex = DesignatedStartIndex; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2123 | StructuredIndex = ElementIndex; |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2124 | return false; |
| 2125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Douglas Gregor | 34e7946 | 2009-01-28 23:36:17 +0000 | [diff] [blame] | 2127 | if (!FinishSubobjectInit) |
| 2128 | return false; |
| 2129 | |
Douglas Gregor | 87f55cf | 2009-01-22 23:26:18 +0000 | [diff] [blame] | 2130 | // Check the remaining elements within this array subobject. |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2131 | bool prevHadError = hadError; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2132 | CheckArrayType(Entity, IList, CurrentObjectType, DesignatedStartIndex, |
Anders Carlsson | 784f699 | 2010-01-23 20:13:41 +0000 | [diff] [blame] | 2133 | /*SubobjectIsDesignatorContext=*/false, Index, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2134 | StructuredList, ElementIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | return hadError && !prevHadError; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2138 | // Get the structured initializer list for a subobject of type |
| 2139 | // @p CurrentObjectType. |
| 2140 | InitListExpr * |
| 2141 | InitListChecker::getStructuredSubobjectInit(InitListExpr *IList, unsigned Index, |
| 2142 | QualType CurrentObjectType, |
| 2143 | InitListExpr *StructuredList, |
| 2144 | unsigned StructuredIndex, |
| 2145 | SourceRange InitRange) { |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 2146 | if (VerifyOnly) |
| 2147 | return 0; // No structured list in verification-only mode. |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2148 | Expr *ExistingInit = 0; |
| 2149 | if (!StructuredList) |
Benjamin Kramer | a789416 | 2012-02-23 14:48:40 +0000 | [diff] [blame] | 2150 | ExistingInit = SyntacticToSemantic.lookup(IList); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2151 | else if (StructuredIndex < StructuredList->getNumInits()) |
| 2152 | ExistingInit = StructuredList->getInit(StructuredIndex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2153 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2154 | if (InitListExpr *Result = dyn_cast_or_null<InitListExpr>(ExistingInit)) |
| 2155 | return Result; |
| 2156 | |
| 2157 | if (ExistingInit) { |
| 2158 | // We are creating an initializer list that initializes the |
| 2159 | // subobjects of the current object, but there was already an |
| 2160 | // initialization that completely initialized the current |
| 2161 | // subobject, e.g., by a compound literal: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2163 | // struct X { int a, b; }; |
| 2164 | // struct X xs[] = { [0] = (struct X) { 1, 2 }, [0].b = 3 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | // |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2166 | // Here, xs[0].a == 0 and xs[0].b == 3, since the second, |
| 2167 | // designated initializer re-initializes the whole |
| 2168 | // subobject [0], overwriting previous initializers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2169 | SemaRef.Diag(InitRange.getBegin(), |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2170 | diag::warn_subobject_initializer_overrides) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2171 | << InitRange; |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2172 | SemaRef.Diag(ExistingInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2173 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2174 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2175 | << ExistingInit->getSourceRange(); |
| 2176 | } |
| 2177 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2178 | InitListExpr *Result |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2179 | = new (SemaRef.Context) InitListExpr(SemaRef.Context, |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 2180 | InitRange.getBegin(), None, |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2181 | InitRange.getEnd()); |
Douglas Gregor | ed8a93d | 2009-03-01 17:12:46 +0000 | [diff] [blame] | 2182 | |
Eli Friedman | 5c89c39 | 2012-02-23 02:25:10 +0000 | [diff] [blame] | 2183 | QualType ResultType = CurrentObjectType; |
| 2184 | if (!ResultType->isArrayType()) |
| 2185 | ResultType = ResultType.getNonLValueExprType(SemaRef.Context); |
| 2186 | Result->setType(ResultType); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2187 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2188 | // Pre-allocate storage for the structured initializer list. |
| 2189 | unsigned NumElements = 0; |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2190 | unsigned NumInits = 0; |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2191 | bool GotNumInits = false; |
| 2192 | if (!StructuredList) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2193 | NumInits = IList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2194 | GotNumInits = true; |
| 2195 | } else if (Index < IList->getNumInits()) { |
| 2196 | if (InitListExpr *SubList = dyn_cast<InitListExpr>(IList->getInit(Index))) { |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2197 | NumInits = SubList->getNumInits(); |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2198 | GotNumInits = true; |
| 2199 | } |
Douglas Gregor | 0845773 | 2009-03-21 18:13:52 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2202 | if (const ArrayType *AType |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2203 | = SemaRef.Context.getAsArrayType(CurrentObjectType)) { |
| 2204 | if (const ConstantArrayType *CAType = dyn_cast<ConstantArrayType>(AType)) { |
| 2205 | NumElements = CAType->getSize().getZExtValue(); |
| 2206 | // Simple heuristic so that we don't allocate a very large |
| 2207 | // initializer with many empty entries at the end. |
Argyrios Kyrtzidis | f8b1771 | 2011-04-28 18:53:55 +0000 | [diff] [blame] | 2208 | if (GotNumInits && NumElements > NumInits) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2209 | NumElements = 0; |
| 2210 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2211 | } else if (const VectorType *VType = CurrentObjectType->getAs<VectorType>()) |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2212 | NumElements = VType->getNumElements(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2213 | else if (const RecordType *RType = CurrentObjectType->getAs<RecordType>()) { |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2214 | RecordDecl *RDecl = RType->getDecl(); |
| 2215 | if (RDecl->isUnion()) |
| 2216 | NumElements = 1; |
| 2217 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2218 | NumElements = std::distance(RDecl->field_begin(), |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2219 | RDecl->field_end()); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2222 | Result->reserveInits(SemaRef.Context, NumElements); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 2223 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2224 | // Link this new initializer list into the structured initializer |
| 2225 | // lists. |
| 2226 | if (StructuredList) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2227 | StructuredList->updateInit(SemaRef.Context, StructuredIndex, Result); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2228 | else { |
| 2229 | Result->setSyntacticForm(IList); |
| 2230 | SyntacticToSemantic[IList] = Result; |
| 2231 | } |
| 2232 | |
| 2233 | return Result; |
| 2234 | } |
| 2235 | |
| 2236 | /// Update the initializer at index @p StructuredIndex within the |
| 2237 | /// structured initializer list to the value @p expr. |
| 2238 | void InitListChecker::UpdateStructuredListElement(InitListExpr *StructuredList, |
| 2239 | unsigned &StructuredIndex, |
| 2240 | Expr *expr) { |
| 2241 | // No structured initializer list to update |
| 2242 | if (!StructuredList) |
| 2243 | return; |
| 2244 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 2245 | if (Expr *PrevInit = StructuredList->updateInit(SemaRef.Context, |
| 2246 | StructuredIndex, expr)) { |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2247 | // This initializer overwrites a previous initializer. Warn. |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2248 | SemaRef.Diag(expr->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2249 | diag::warn_initializer_overrides) |
| 2250 | << expr->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2251 | SemaRef.Diag(PrevInit->getLocStart(), |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2252 | diag::note_previous_initializer) |
Douglas Gregor | 54f0728 | 2009-01-28 23:43:32 +0000 | [diff] [blame] | 2253 | << /*FIXME:has side effects=*/0 |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2254 | << PrevInit->getSourceRange(); |
| 2255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2256 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 2257 | ++StructuredIndex; |
| 2258 | } |
| 2259 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2260 | /// Check that the given Index expression is a valid array designator |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2261 | /// value. This is essentially just a wrapper around |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2262 | /// VerifyIntegerConstantExpression that also checks for negative values |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2263 | /// and produces a reasonable diagnostic if there is a |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2264 | /// failure. Returns the index expression, possibly with an implicit cast |
| 2265 | /// added, on success. If everything went okay, Value will receive the |
| 2266 | /// value of the constant expression. |
| 2267 | static ExprResult |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2268 | CheckArrayDesignatorExpr(Sema &S, Expr *Index, llvm::APSInt &Value) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 2269 | SourceLocation Loc = Index->getLocStart(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2270 | |
| 2271 | // Make sure this is an integer constant expression. |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2272 | ExprResult Result = S.VerifyIntegerConstantExpression(Index, &Value); |
| 2273 | if (Result.isInvalid()) |
| 2274 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2275 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 2276 | if (Value.isSigned() && Value.isNegative()) |
| 2277 | return S.Diag(Loc, diag::err_array_designator_negative) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2278 | << Value.toString(10) << Index->getSourceRange(); |
| 2279 | |
Douglas Gregor | 53d3d8e | 2009-01-23 21:04:18 +0000 | [diff] [blame] | 2280 | Value.setIsUnsigned(true); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2281 | return Result; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2284 | ExprResult Sema::ActOnDesignatedInitializer(Designation &Desig, |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 2285 | SourceLocation Loc, |
| 2286 | bool GNUSyntax, |
| 2287 | ExprResult Init) { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2288 | typedef DesignatedInitExpr::Designator ASTDesignator; |
| 2289 | |
| 2290 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2291 | SmallVector<ASTDesignator, 32> Designators; |
| 2292 | SmallVector<Expr *, 32> InitExpressions; |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2293 | |
| 2294 | // Build designators and check array designator expressions. |
| 2295 | for (unsigned Idx = 0; Idx < Desig.getNumDesignators(); ++Idx) { |
| 2296 | const Designator &D = Desig.getDesignator(Idx); |
| 2297 | switch (D.getKind()) { |
| 2298 | case Designator::FieldDesignator: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | Designators.push_back(ASTDesignator(D.getField(), D.getDotLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2300 | D.getFieldLoc())); |
| 2301 | break; |
| 2302 | |
| 2303 | case Designator::ArrayDesignator: { |
| 2304 | Expr *Index = static_cast<Expr *>(D.getArrayIndex()); |
| 2305 | llvm::APSInt IndexValue; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2306 | if (!Index->isTypeDependent() && !Index->isValueDependent()) |
| 2307 | Index = CheckArrayDesignatorExpr(*this, Index, IndexValue).take(); |
| 2308 | if (!Index) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2309 | Invalid = true; |
| 2310 | else { |
| 2311 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2312 | D.getLBracketLoc(), |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2313 | D.getRBracketLoc())); |
| 2314 | InitExpressions.push_back(Index); |
| 2315 | } |
| 2316 | break; |
| 2317 | } |
| 2318 | |
| 2319 | case Designator::ArrayRangeDesignator: { |
| 2320 | Expr *StartIndex = static_cast<Expr *>(D.getArrayRangeStart()); |
| 2321 | Expr *EndIndex = static_cast<Expr *>(D.getArrayRangeEnd()); |
| 2322 | llvm::APSInt StartValue; |
| 2323 | llvm::APSInt EndValue; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2324 | bool StartDependent = StartIndex->isTypeDependent() || |
| 2325 | StartIndex->isValueDependent(); |
| 2326 | bool EndDependent = EndIndex->isTypeDependent() || |
| 2327 | EndIndex->isValueDependent(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2328 | if (!StartDependent) |
| 2329 | StartIndex = |
| 2330 | CheckArrayDesignatorExpr(*this, StartIndex, StartValue).take(); |
| 2331 | if (!EndDependent) |
| 2332 | EndIndex = CheckArrayDesignatorExpr(*this, EndIndex, EndValue).take(); |
| 2333 | |
| 2334 | if (!StartIndex || !EndIndex) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2335 | Invalid = true; |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2336 | else { |
| 2337 | // Make sure we're comparing values with the same bit width. |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2338 | if (StartDependent || EndDependent) { |
| 2339 | // Nothing to compute. |
| 2340 | } else if (StartValue.getBitWidth() > EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2341 | EndValue = EndValue.extend(StartValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2342 | else if (StartValue.getBitWidth() < EndValue.getBitWidth()) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2343 | StartValue = StartValue.extend(EndValue.getBitWidth()); |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2344 | |
Douglas Gregor | c4bb7bf | 2009-05-21 23:30:39 +0000 | [diff] [blame] | 2345 | if (!StartDependent && !EndDependent && EndValue < StartValue) { |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2346 | Diag(D.getEllipsisLoc(), diag::err_array_designator_empty_range) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2347 | << StartValue.toString(10) << EndValue.toString(10) |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2348 | << StartIndex->getSourceRange() << EndIndex->getSourceRange(); |
| 2349 | Invalid = true; |
| 2350 | } else { |
| 2351 | Designators.push_back(ASTDesignator(InitExpressions.size(), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2352 | D.getLBracketLoc(), |
Douglas Gregor | d6f584f | 2009-01-23 22:22:29 +0000 | [diff] [blame] | 2353 | D.getEllipsisLoc(), |
| 2354 | D.getRBracketLoc())); |
| 2355 | InitExpressions.push_back(StartIndex); |
| 2356 | InitExpressions.push_back(EndIndex); |
| 2357 | } |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2358 | } |
| 2359 | break; |
| 2360 | } |
| 2361 | } |
| 2362 | } |
| 2363 | |
| 2364 | if (Invalid || Init.isInvalid()) |
| 2365 | return ExprError(); |
| 2366 | |
| 2367 | // Clear out the expressions within the designation. |
| 2368 | Desig.ClearExprs(*this); |
| 2369 | |
| 2370 | DesignatedInitExpr *DIE |
Jay Foad | beaaccd | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 2371 | = DesignatedInitExpr::Create(Context, |
| 2372 | Designators.data(), Designators.size(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 2373 | InitExpressions, Loc, GNUSyntax, |
| 2374 | Init.takeAs<Expr>()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2375 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2376 | if (!getLangOpts().C99) |
Douglas Gregor | 2d75bbd | 2011-01-16 16:13:16 +0000 | [diff] [blame] | 2377 | Diag(DIE->getLocStart(), diag::ext_designated_init) |
| 2378 | << DIE->getSourceRange(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2379 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2380 | return Owned(DIE); |
| 2381 | } |
Douglas Gregor | c34ee5e | 2009-01-29 00:45:39 +0000 | [diff] [blame] | 2382 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2383 | //===----------------------------------------------------------------------===// |
| 2384 | // Initialization entity |
| 2385 | //===----------------------------------------------------------------------===// |
| 2386 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2387 | InitializedEntity::InitializedEntity(ASTContext &Context, unsigned Index, |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2388 | const InitializedEntity &Parent) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2389 | : Parent(&Parent), Index(Index) |
Douglas Gregor | cb57fb9 | 2009-12-16 06:35:08 +0000 | [diff] [blame] | 2390 | { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2391 | if (const ArrayType *AT = Context.getAsArrayType(Parent.getType())) { |
| 2392 | Kind = EK_ArrayElement; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2393 | Type = AT->getElementType(); |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2394 | } else if (const VectorType *VT = Parent.getType()->getAs<VectorType>()) { |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2395 | Kind = EK_VectorElement; |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2396 | Type = VT->getElementType(); |
| 2397 | } else { |
| 2398 | const ComplexType *CT = Parent.getType()->getAs<ComplexType>(); |
| 2399 | assert(CT && "Unexpected type"); |
| 2400 | Kind = EK_ComplexElement; |
| 2401 | Type = CT->getElementType(); |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2402 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2405 | InitializedEntity InitializedEntity::InitializeBase(ASTContext &Context, |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2406 | CXXBaseSpecifier *Base, |
| 2407 | bool IsInheritedVirtualBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2408 | { |
| 2409 | InitializedEntity Result; |
| 2410 | Result.Kind = EK_Base; |
Anders Carlsson | 711f34a | 2010-04-21 19:52:01 +0000 | [diff] [blame] | 2411 | Result.Base = reinterpret_cast<uintptr_t>(Base); |
| 2412 | if (IsInheritedVirtualBase) |
| 2413 | Result.Base |= 0x01; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2414 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 2415 | Result.Type = Base->getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2416 | return Result; |
| 2417 | } |
| 2418 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2419 | DeclarationName InitializedEntity::getName() const { |
| 2420 | switch (getKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2421 | case EK_Parameter: { |
| 2422 | ParmVarDecl *D = reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2423 | return (D ? D->getDeclName() : DeclarationName()); |
| 2424 | } |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 2425 | |
| 2426 | case EK_Variable: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2427 | case EK_Member: |
| 2428 | return VariableOrMember->getDeclName(); |
| 2429 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2430 | case EK_LambdaCapture: |
| 2431 | return Capture.Var->getDeclName(); |
| 2432 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2433 | case EK_Result: |
| 2434 | case EK_Exception: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2435 | case EK_New: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2436 | case EK_Temporary: |
| 2437 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2438 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2439 | case EK_ArrayElement: |
| 2440 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2441 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2442 | case EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2443 | case EK_CompoundLiteralInit: |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2444 | return DeclarationName(); |
| 2445 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2446 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2447 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2450 | DeclaratorDecl *InitializedEntity::getDecl() const { |
| 2451 | switch (getKind()) { |
| 2452 | case EK_Variable: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2453 | case EK_Member: |
| 2454 | return VariableOrMember; |
| 2455 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2456 | case EK_Parameter: |
| 2457 | return reinterpret_cast<ParmVarDecl*>(Parameter & ~0x1); |
| 2458 | |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2459 | case EK_Result: |
| 2460 | case EK_Exception: |
| 2461 | case EK_New: |
| 2462 | case EK_Temporary: |
| 2463 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2464 | case EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 2465 | case EK_ArrayElement: |
| 2466 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2467 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2468 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2469 | case EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2470 | case EK_CompoundLiteralInit: |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2471 | return 0; |
| 2472 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2473 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2474 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2477 | bool InitializedEntity::allowsNRVO() const { |
| 2478 | switch (getKind()) { |
| 2479 | case EK_Result: |
| 2480 | case EK_Exception: |
| 2481 | return LocAndNRVO.NRVO; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2482 | |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2483 | case EK_Variable: |
| 2484 | case EK_Parameter: |
| 2485 | case EK_Member: |
| 2486 | case EK_New: |
| 2487 | case EK_Temporary: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 2488 | case EK_CompoundLiteralInit: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2489 | case EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 2490 | case EK_Delegating: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2491 | case EK_ArrayElement: |
| 2492 | case EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 2493 | case EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 2494 | case EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 2495 | case EK_LambdaCapture: |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 2496 | break; |
| 2497 | } |
| 2498 | |
| 2499 | return false; |
| 2500 | } |
| 2501 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2502 | //===----------------------------------------------------------------------===// |
| 2503 | // Initialization sequence |
| 2504 | //===----------------------------------------------------------------------===// |
| 2505 | |
| 2506 | void InitializationSequence::Step::Destroy() { |
| 2507 | switch (Kind) { |
| 2508 | case SK_ResolveAddressOfOverloadedFunction: |
| 2509 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2510 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2511 | case SK_CastDerivedToBaseLValue: |
| 2512 | case SK_BindReference: |
| 2513 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2514 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2515 | case SK_UserConversion: |
| 2516 | case SK_QualificationConversionRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2517 | case SK_QualificationConversionXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2518 | case SK_QualificationConversionLValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2519 | case SK_LValueToRValue: |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2520 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2521 | case SK_ListConstructorCall: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2522 | case SK_UnwrapInitList: |
| 2523 | case SK_RewrapInitList: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2524 | case SK_ConstructorInitialization: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2525 | case SK_ZeroInitialization: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2526 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2527 | case SK_StringInit: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2528 | case SK_ObjCObjectConversion: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2529 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2530 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2531 | case SK_PassByIndirectCopyRestore: |
| 2532 | case SK_PassByIndirectRestore: |
| 2533 | case SK_ProduceObjCObject: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2534 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2535 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2536 | case SK_OCLZeroEvent: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2537 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2538 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2539 | case SK_ConversionSequence: |
| 2540 | delete ICS; |
| 2541 | } |
| 2542 | } |
| 2543 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2544 | bool InitializationSequence::isDirectReferenceBinding() const { |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 2545 | return !Steps.empty() && Steps.back().Kind == SK_BindReference; |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
| 2548 | bool InitializationSequence::isAmbiguous() const { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 2549 | if (!Failed()) |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2550 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2551 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2552 | switch (getFailureKind()) { |
| 2553 | case FK_TooManyInitsForReference: |
| 2554 | case FK_ArrayNeedsInitList: |
| 2555 | case FK_ArrayNeedsInitListOrStringLiteral: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 2556 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 2557 | case FK_NarrowStringIntoWideCharArray: |
| 2558 | case FK_WideStringIntoCharArray: |
| 2559 | case FK_IncompatWideStringIntoWideChar: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2560 | case FK_AddressOfOverloadFailed: // FIXME: Could do better |
| 2561 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 2562 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 2563 | case FK_RValueReferenceBindingToLValue: |
| 2564 | case FK_ReferenceInitDropsQualifiers: |
| 2565 | case FK_ReferenceInitFailed: |
| 2566 | case FK_ConversionFailed: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2567 | case FK_ConversionFromPropertyFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2568 | case FK_TooManyInitsForScalar: |
| 2569 | case FK_ReferenceBindingToInitList: |
| 2570 | case FK_InitListBadDestinationType: |
| 2571 | case FK_DefaultInitOfConst: |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 2572 | case FK_Incomplete: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2573 | case FK_ArrayTypeMismatch: |
| 2574 | case FK_NonConstantArrayInit: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 2575 | case FK_ListInitializationFailed: |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 2576 | case FK_VariableLengthArrayHasInitializer: |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2577 | case FK_PlaceholderType: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2578 | case FK_InitListElementCopyFailure: |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2579 | case FK_ExplicitConstructor: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2580 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2581 | |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2582 | case FK_ReferenceInitOverloadFailed: |
| 2583 | case FK_UserConversionOverloadFailed: |
| 2584 | case FK_ConstructorOverloadFailed: |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 2585 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2586 | return FailedOverloadResult == OR_Ambiguous; |
| 2587 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2588 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 2589 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | b70cf44 | 2010-03-26 20:14:36 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
Douglas Gregor | d6e44a3 | 2010-04-16 22:09:46 +0000 | [diff] [blame] | 2592 | bool InitializationSequence::isConstructorInitialization() const { |
| 2593 | return !Steps.empty() && Steps.back().Kind == SK_ConstructorInitialization; |
| 2594 | } |
| 2595 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2596 | void |
| 2597 | InitializationSequence |
| 2598 | ::AddAddressOverloadResolutionStep(FunctionDecl *Function, |
| 2599 | DeclAccessPair Found, |
| 2600 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2601 | Step S; |
| 2602 | S.Kind = SK_ResolveAddressOfOverloadedFunction; |
| 2603 | S.Type = Function->getType(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2604 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2605 | S.Function.Function = Function; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2606 | S.Function.FoundDecl = Found; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2607 | Steps.push_back(S); |
| 2608 | } |
| 2609 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2610 | void InitializationSequence::AddDerivedToBaseCastStep(QualType BaseType, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2611 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2612 | Step S; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2613 | switch (VK) { |
| 2614 | case VK_RValue: S.Kind = SK_CastDerivedToBaseRValue; break; |
| 2615 | case VK_XValue: S.Kind = SK_CastDerivedToBaseXValue; break; |
| 2616 | case VK_LValue: S.Kind = SK_CastDerivedToBaseLValue; break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2617 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2618 | S.Type = BaseType; |
| 2619 | Steps.push_back(S); |
| 2620 | } |
| 2621 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2622 | void InitializationSequence::AddReferenceBindingStep(QualType T, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2623 | bool BindingTemporary) { |
| 2624 | Step S; |
| 2625 | S.Kind = BindingTemporary? SK_BindReferenceToTemporary : SK_BindReference; |
| 2626 | S.Type = T; |
| 2627 | Steps.push_back(S); |
| 2628 | } |
| 2629 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 2630 | void InitializationSequence::AddExtraneousCopyToTemporary(QualType T) { |
| 2631 | Step S; |
| 2632 | S.Kind = SK_ExtraneousCopyToTemporary; |
| 2633 | S.Type = T; |
| 2634 | Steps.push_back(S); |
| 2635 | } |
| 2636 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2637 | void |
| 2638 | InitializationSequence::AddUserConversionStep(FunctionDecl *Function, |
| 2639 | DeclAccessPair FoundDecl, |
| 2640 | QualType T, |
| 2641 | bool HadMultipleCandidates) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2642 | Step S; |
| 2643 | S.Kind = SK_UserConversion; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 2644 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2645 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2646 | S.Function.Function = Function; |
| 2647 | S.Function.FoundDecl = FoundDecl; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2648 | Steps.push_back(S); |
| 2649 | } |
| 2650 | |
| 2651 | void InitializationSequence::AddQualificationConversionStep(QualType Ty, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2652 | ExprValueKind VK) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2653 | Step S; |
John McCall | 38a4ffe | 2010-08-26 16:36:35 +0000 | [diff] [blame] | 2654 | S.Kind = SK_QualificationConversionRValue; // work around a gcc warning |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2655 | switch (VK) { |
| 2656 | case VK_RValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2657 | S.Kind = SK_QualificationConversionRValue; |
| 2658 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2659 | case VK_XValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2660 | S.Kind = SK_QualificationConversionXValue; |
| 2661 | break; |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2662 | case VK_LValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2663 | S.Kind = SK_QualificationConversionLValue; |
| 2664 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2665 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2666 | S.Type = Ty; |
| 2667 | Steps.push_back(S); |
| 2668 | } |
| 2669 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 2670 | void InitializationSequence::AddLValueToRValueStep(QualType Ty) { |
| 2671 | assert(!Ty.hasQualifiers() && "rvalues may not have qualifiers"); |
| 2672 | |
| 2673 | Step S; |
| 2674 | S.Kind = SK_LValueToRValue; |
| 2675 | S.Type = Ty; |
| 2676 | Steps.push_back(S); |
| 2677 | } |
| 2678 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2679 | void InitializationSequence::AddConversionSequenceStep( |
| 2680 | const ImplicitConversionSequence &ICS, |
| 2681 | QualType T) { |
| 2682 | Step S; |
| 2683 | S.Kind = SK_ConversionSequence; |
| 2684 | S.Type = T; |
| 2685 | S.ICS = new ImplicitConversionSequence(ICS); |
| 2686 | Steps.push_back(S); |
| 2687 | } |
| 2688 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 2689 | void InitializationSequence::AddListInitializationStep(QualType T) { |
| 2690 | Step S; |
| 2691 | S.Kind = SK_ListInitialization; |
| 2692 | S.Type = T; |
| 2693 | Steps.push_back(S); |
| 2694 | } |
| 2695 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2696 | void |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2697 | InitializationSequence |
| 2698 | ::AddConstructorInitializationStep(CXXConstructorDecl *Constructor, |
| 2699 | AccessSpecifier Access, |
| 2700 | QualType T, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2701 | bool HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2702 | bool FromInitList, bool AsInitList) { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2703 | Step S; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2704 | S.Kind = FromInitList && !AsInitList ? SK_ListConstructorCall |
| 2705 | : SK_ConstructorInitialization; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2706 | S.Type = T; |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 2707 | S.Function.HadMultipleCandidates = HadMultipleCandidates; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 2708 | S.Function.Function = Constructor; |
| 2709 | S.Function.FoundDecl = DeclAccessPair::make(Constructor, Access); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 2710 | Steps.push_back(S); |
| 2711 | } |
| 2712 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 2713 | void InitializationSequence::AddZeroInitializationStep(QualType T) { |
| 2714 | Step S; |
| 2715 | S.Kind = SK_ZeroInitialization; |
| 2716 | S.Type = T; |
| 2717 | Steps.push_back(S); |
| 2718 | } |
| 2719 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 2720 | void InitializationSequence::AddCAssignmentStep(QualType T) { |
| 2721 | Step S; |
| 2722 | S.Kind = SK_CAssignment; |
| 2723 | S.Type = T; |
| 2724 | Steps.push_back(S); |
| 2725 | } |
| 2726 | |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 2727 | void InitializationSequence::AddStringInitStep(QualType T) { |
| 2728 | Step S; |
| 2729 | S.Kind = SK_StringInit; |
| 2730 | S.Type = T; |
| 2731 | Steps.push_back(S); |
| 2732 | } |
| 2733 | |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 2734 | void InitializationSequence::AddObjCObjectConversionStep(QualType T) { |
| 2735 | Step S; |
| 2736 | S.Kind = SK_ObjCObjectConversion; |
| 2737 | S.Type = T; |
| 2738 | Steps.push_back(S); |
| 2739 | } |
| 2740 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 2741 | void InitializationSequence::AddArrayInitStep(QualType T) { |
| 2742 | Step S; |
| 2743 | S.Kind = SK_ArrayInit; |
| 2744 | S.Type = T; |
| 2745 | Steps.push_back(S); |
| 2746 | } |
| 2747 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 2748 | void InitializationSequence::AddParenthesizedArrayInitStep(QualType T) { |
| 2749 | Step S; |
| 2750 | S.Kind = SK_ParenthesizedArrayInit; |
| 2751 | S.Type = T; |
| 2752 | Steps.push_back(S); |
| 2753 | } |
| 2754 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2755 | void InitializationSequence::AddPassByIndirectCopyRestoreStep(QualType type, |
| 2756 | bool shouldCopy) { |
| 2757 | Step s; |
| 2758 | s.Kind = (shouldCopy ? SK_PassByIndirectCopyRestore |
| 2759 | : SK_PassByIndirectRestore); |
| 2760 | s.Type = type; |
| 2761 | Steps.push_back(s); |
| 2762 | } |
| 2763 | |
| 2764 | void InitializationSequence::AddProduceObjCObjectStep(QualType T) { |
| 2765 | Step S; |
| 2766 | S.Kind = SK_ProduceObjCObject; |
| 2767 | S.Type = T; |
| 2768 | Steps.push_back(S); |
| 2769 | } |
| 2770 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 2771 | void InitializationSequence::AddStdInitializerListConstructionStep(QualType T) { |
| 2772 | Step S; |
| 2773 | S.Kind = SK_StdInitializerList; |
| 2774 | S.Type = T; |
| 2775 | Steps.push_back(S); |
| 2776 | } |
| 2777 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2778 | void InitializationSequence::AddOCLSamplerInitStep(QualType T) { |
| 2779 | Step S; |
| 2780 | S.Kind = SK_OCLSamplerInit; |
| 2781 | S.Type = T; |
| 2782 | Steps.push_back(S); |
| 2783 | } |
| 2784 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2785 | void InitializationSequence::AddOCLZeroEventStep(QualType T) { |
| 2786 | Step S; |
| 2787 | S.Kind = SK_OCLZeroEvent; |
| 2788 | S.Type = T; |
| 2789 | Steps.push_back(S); |
| 2790 | } |
| 2791 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 2792 | void InitializationSequence::RewrapReferenceInitList(QualType T, |
| 2793 | InitListExpr *Syntactic) { |
| 2794 | assert(Syntactic->getNumInits() == 1 && |
| 2795 | "Can only rewrap trivial init lists."); |
| 2796 | Step S; |
| 2797 | S.Kind = SK_UnwrapInitList; |
| 2798 | S.Type = Syntactic->getInit(0)->getType(); |
| 2799 | Steps.insert(Steps.begin(), S); |
| 2800 | |
| 2801 | S.Kind = SK_RewrapInitList; |
| 2802 | S.Type = T; |
| 2803 | S.WrappingSyntacticList = Syntactic; |
| 2804 | Steps.push_back(S); |
| 2805 | } |
| 2806 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 2807 | void InitializationSequence::SetOverloadFailure(FailureKind Failure, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2808 | OverloadingResult Result) { |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 2809 | setSequenceKind(FailedSequence); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 2810 | this->Failure = Failure; |
| 2811 | this->FailedOverloadResult = Result; |
| 2812 | } |
| 2813 | |
| 2814 | //===----------------------------------------------------------------------===// |
| 2815 | // Attempt initialization |
| 2816 | //===----------------------------------------------------------------------===// |
| 2817 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2818 | static void MaybeProduceObjCObject(Sema &S, |
| 2819 | InitializationSequence &Sequence, |
| 2820 | const InitializedEntity &Entity) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2821 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2822 | |
| 2823 | /// When initializing a parameter, produce the value if it's marked |
| 2824 | /// __attribute__((ns_consumed)). |
| 2825 | if (Entity.getKind() == InitializedEntity::EK_Parameter) { |
| 2826 | if (!Entity.isParameterConsumed()) |
| 2827 | return; |
| 2828 | |
| 2829 | assert(Entity.getType()->isObjCRetainableType() && |
| 2830 | "consuming an object of unretainable type?"); |
| 2831 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2832 | |
| 2833 | /// When initializing a return value, if the return type is a |
| 2834 | /// retainable type, then returns need to immediately retain the |
| 2835 | /// object. If an autorelease is required, it will be done at the |
| 2836 | /// last instant. |
| 2837 | } else if (Entity.getKind() == InitializedEntity::EK_Result) { |
| 2838 | if (!Entity.getType()->isObjCRetainableType()) |
| 2839 | return; |
| 2840 | |
| 2841 | Sequence.AddProduceObjCObjectStep(Entity.getType()); |
| 2842 | } |
| 2843 | } |
| 2844 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2845 | /// \brief When initializing from init list via constructor, handle |
| 2846 | /// initialization of an object of type std::initializer_list<T>. |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2847 | /// |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2848 | /// \return true if we have handled initialization of an object of type |
| 2849 | /// std::initializer_list<T>, false otherwise. |
| 2850 | static bool TryInitializerListConstruction(Sema &S, |
| 2851 | InitListExpr *List, |
| 2852 | QualType DestType, |
| 2853 | InitializationSequence &Sequence) { |
| 2854 | QualType E; |
| 2855 | if (!S.isStdInitializerList(DestType, &E)) |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 2856 | return false; |
| 2857 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2858 | // Check that each individual element can be copy-constructed. But since we |
| 2859 | // have no place to store further information, we'll recalculate everything |
| 2860 | // later. |
| 2861 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 2862 | S.Context.getConstantArrayType(E, |
| 2863 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 2864 | List->getNumInits()), |
| 2865 | ArrayType::Normal, 0)); |
| 2866 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 2867 | 0, HiddenArray); |
| 2868 | for (unsigned i = 0, n = List->getNumInits(); i < n; ++i) { |
| 2869 | Element.setElementIndex(i); |
| 2870 | if (!S.CanPerformCopyInitialization(Element, List->getInit(i))) { |
| 2871 | Sequence.SetFailed( |
| 2872 | InitializationSequence::FK_InitListElementCopyFailure); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2873 | return true; |
| 2874 | } |
| 2875 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2876 | Sequence.AddStdInitializerListConstructionStep(DestType); |
| 2877 | return true; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2878 | } |
| 2879 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2880 | static OverloadingResult |
| 2881 | ResolveConstructorOverload(Sema &S, SourceLocation DeclLoc, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2882 | MultiExprArg Args, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2883 | OverloadCandidateSet &CandidateSet, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2884 | ArrayRef<NamedDecl *> Ctors, |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2885 | OverloadCandidateSet::iterator &Best, |
| 2886 | bool CopyInitializing, bool AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2887 | bool OnlyListConstructors, bool InitListSyntax) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2888 | CandidateSet.clear(); |
| 2889 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2890 | for (ArrayRef<NamedDecl *>::iterator |
| 2891 | Con = Ctors.begin(), ConEnd = Ctors.end(); Con != ConEnd; ++Con) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2892 | NamedDecl *D = *Con; |
| 2893 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 2894 | bool SuppressUserConversions = false; |
| 2895 | |
| 2896 | // Find the constructor (which may be a template). |
| 2897 | CXXConstructorDecl *Constructor = 0; |
| 2898 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
| 2899 | if (ConstructorTmpl) |
| 2900 | Constructor = cast<CXXConstructorDecl>( |
| 2901 | ConstructorTmpl->getTemplatedDecl()); |
| 2902 | else { |
| 2903 | Constructor = cast<CXXConstructorDecl>(D); |
| 2904 | |
| 2905 | // If we're performing copy initialization using a copy constructor, we |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2906 | // suppress user-defined conversions on the arguments. We do the same for |
| 2907 | // move constructors. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2908 | if ((CopyInitializing || (InitListSyntax && Args.size() == 1)) && |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2909 | Constructor->isCopyOrMoveConstructor()) |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2910 | SuppressUserConversions = true; |
| 2911 | } |
| 2912 | |
| 2913 | if (!Constructor->isInvalidDecl() && |
| 2914 | (AllowExplicit || !Constructor->isExplicit()) && |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2915 | (!OnlyListConstructors || S.isInitListConstructor(Constructor))) { |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2916 | if (ConstructorTmpl) |
| 2917 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2918 | /*ExplicitArgs*/ 0, Args, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 2919 | CandidateSet, SuppressUserConversions); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2920 | else { |
| 2921 | // C++ [over.match.copy]p1: |
| 2922 | // - When initializing a temporary to be bound to the first parameter |
| 2923 | // of a constructor that takes a reference to possibly cv-qualified |
| 2924 | // T as its first argument, called with a single argument in the |
| 2925 | // context of direct-initialization, explicit conversion functions |
| 2926 | // are also considered. |
| 2927 | bool AllowExplicitConv = AllowExplicit && !CopyInitializing && |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2928 | Args.size() == 1 && |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2929 | Constructor->isCopyOrMoveConstructor(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2930 | S.AddOverloadCandidate(Constructor, FoundDecl, Args, CandidateSet, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 2931 | SuppressUserConversions, |
| 2932 | /*PartialOverloading=*/false, |
| 2933 | /*AllowExplicit=*/AllowExplicitConv); |
| 2934 | } |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2935 | } |
| 2936 | } |
| 2937 | |
| 2938 | // Perform overload resolution and return the result. |
| 2939 | return CandidateSet.BestViableFunction(S, DeclLoc, Best); |
| 2940 | } |
| 2941 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2942 | /// \brief Attempt initialization by constructor (C++ [dcl.init]), which |
| 2943 | /// enumerates the constructors of the initialized entity and performs overload |
| 2944 | /// resolution to select the best. |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2945 | /// If InitListSyntax is true, this is list-initialization of a non-aggregate |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2946 | /// class type. |
| 2947 | static void TryConstructorInitialization(Sema &S, |
| 2948 | const InitializedEntity &Entity, |
| 2949 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2950 | MultiExprArg Args, QualType DestType, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2951 | InitializationSequence &Sequence, |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2952 | bool InitListSyntax = false) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 2953 | assert((!InitListSyntax || (Args.size() == 1 && isa<InitListExpr>(Args[0]))) && |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2954 | "InitListSyntax must come with a single initializer list argument."); |
| 2955 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2956 | // The type we're constructing needs to be complete. |
| 2957 | if (S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 2958 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2959 | return; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
| 2962 | const RecordType *DestRecordType = DestType->getAs<RecordType>(); |
| 2963 | assert(DestRecordType && "Constructor initialization requires record type"); |
| 2964 | CXXRecordDecl *DestRecordDecl |
| 2965 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
| 2966 | |
Sebastian Redl | 96715b2 | 2012-02-04 21:27:39 +0000 | [diff] [blame] | 2967 | // Build the candidate set directly in the initialization sequence |
| 2968 | // structure, so that it will persist if we fail. |
| 2969 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 2970 | |
| 2971 | // Determine whether we are allowed to call explicit constructors or |
| 2972 | // explicit conversion operators. |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 2973 | bool AllowExplicit = Kind.AllowExplicit() || InitListSyntax; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2974 | bool CopyInitialization = Kind.getKind() == InitializationKind::IK_Copy; |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 2975 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2976 | // - Otherwise, if T is a class type, constructors are considered. The |
| 2977 | // applicable constructors are enumerated, and the best one is chosen |
| 2978 | // through overload resolution. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2979 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 2980 | // The container holding the constructors can under certain conditions |
| 2981 | // be changed while iterating (e.g. because of deserialization). |
| 2982 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 2983 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2984 | |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2985 | OverloadingResult Result = OR_No_Viable_Function; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 2986 | OverloadCandidateSet::iterator Best; |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2987 | bool AsInitializerList = false; |
| 2988 | |
| 2989 | // C++11 [over.match.list]p1: |
| 2990 | // When objects of non-aggregate type T are list-initialized, overload |
| 2991 | // resolution selects the constructor in two phases: |
| 2992 | // - Initially, the candidate functions are the initializer-list |
| 2993 | // constructors of the class T and the argument list consists of the |
| 2994 | // initializer list as a single argument. |
| 2995 | if (InitListSyntax) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2996 | InitListExpr *ILE = cast<InitListExpr>(Args[0]); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 2997 | AsInitializerList = true; |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 2998 | |
| 2999 | // If the initializer list has no elements and T has a default constructor, |
| 3000 | // the first phase is omitted. |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3001 | if (ILE->getNumInits() != 0 || !DestRecordDecl->hasDefaultConstructor()) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3002 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3003 | CandidateSet, Ctors, Best, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3004 | CopyInitialization, AllowExplicit, |
| 3005 | /*OnlyListConstructor=*/true, |
| 3006 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3007 | |
| 3008 | // Time to unwrap the init list. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3009 | Args = MultiExprArg(ILE->getInits(), ILE->getNumInits()); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | // C++11 [over.match.list]p1: |
| 3013 | // - If no viable initializer-list constructor is found, overload resolution |
| 3014 | // is performed again, where the candidate functions are all the |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3015 | // constructors of the class T and the argument list consists of the |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3016 | // elements of the initializer list. |
| 3017 | if (Result == OR_No_Viable_Function) { |
| 3018 | AsInitializerList = false; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3019 | Result = ResolveConstructorOverload(S, Kind.getLocation(), Args, |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3020 | CandidateSet, Ctors, Best, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3021 | CopyInitialization, AllowExplicit, |
Sebastian Redl | 51ad9cd | 2012-02-29 12:47:43 +0000 | [diff] [blame] | 3022 | /*OnlyListConstructors=*/false, |
| 3023 | InitListSyntax); |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3024 | } |
| 3025 | if (Result) { |
Sebastian Redl | 08ae369 | 2012-02-04 21:27:33 +0000 | [diff] [blame] | 3026 | Sequence.SetOverloadFailure(InitListSyntax ? |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 3027 | InitializationSequence::FK_ListConstructorOverloadFailed : |
| 3028 | InitializationSequence::FK_ConstructorOverloadFailed, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3029 | Result); |
| 3030 | return; |
| 3031 | } |
| 3032 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3033 | // C++11 [dcl.init]p6: |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3034 | // If a program calls for the default initialization of an object |
| 3035 | // of a const-qualified type T, T shall be a class type with a |
| 3036 | // user-provided default constructor. |
| 3037 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 3038 | Entity.getType().isConstQualified() && |
Aaron Ballman | 5121781 | 2012-07-31 22:40:31 +0000 | [diff] [blame] | 3039 | !cast<CXXConstructorDecl>(Best->Function)->isUserProvided()) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3040 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
| 3041 | return; |
| 3042 | } |
| 3043 | |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 3044 | // C++11 [over.match.list]p1: |
| 3045 | // In copy-list-initialization, if an explicit constructor is chosen, the |
| 3046 | // initializer is ill-formed. |
| 3047 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 3048 | if (InitListSyntax && !Kind.AllowExplicit() && CtorDecl->isExplicit()) { |
| 3049 | Sequence.SetFailed(InitializationSequence::FK_ExplicitConstructor); |
| 3050 | return; |
| 3051 | } |
| 3052 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3053 | // Add the constructor initialization step. Any cv-qualification conversion is |
| 3054 | // subsumed by the initialization. |
| 3055 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3056 | Sequence.AddConstructorInitializationStep(CtorDecl, |
| 3057 | Best->FoundDecl.getAccess(), |
| 3058 | DestType, HadMultipleCandidates, |
Sebastian Redl | 6cd03db | 2012-02-04 21:27:47 +0000 | [diff] [blame] | 3059 | InitListSyntax, AsInitializerList); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3062 | static bool |
| 3063 | ResolveOverloadedFunctionForReferenceBinding(Sema &S, |
| 3064 | Expr *Initializer, |
| 3065 | QualType &SourceType, |
| 3066 | QualType &UnqualifiedSourceType, |
| 3067 | QualType UnqualifiedTargetType, |
| 3068 | InitializationSequence &Sequence) { |
| 3069 | if (S.Context.getCanonicalType(UnqualifiedSourceType) == |
| 3070 | S.Context.OverloadTy) { |
| 3071 | DeclAccessPair Found; |
| 3072 | bool HadMultipleCandidates = false; |
| 3073 | if (FunctionDecl *Fn |
| 3074 | = S.ResolveAddressOfOverloadedFunction(Initializer, |
| 3075 | UnqualifiedTargetType, |
| 3076 | false, Found, |
| 3077 | &HadMultipleCandidates)) { |
| 3078 | Sequence.AddAddressOverloadResolutionStep(Fn, Found, |
| 3079 | HadMultipleCandidates); |
| 3080 | SourceType = Fn->getType(); |
| 3081 | UnqualifiedSourceType = SourceType.getUnqualifiedType(); |
| 3082 | } else if (!UnqualifiedTargetType->isRecordType()) { |
| 3083 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3084 | return true; |
| 3085 | } |
| 3086 | } |
| 3087 | return false; |
| 3088 | } |
| 3089 | |
| 3090 | static void TryReferenceInitializationCore(Sema &S, |
| 3091 | const InitializedEntity &Entity, |
| 3092 | const InitializationKind &Kind, |
| 3093 | Expr *Initializer, |
| 3094 | QualType cv1T1, QualType T1, |
| 3095 | Qualifiers T1Quals, |
| 3096 | QualType cv2T2, QualType T2, |
| 3097 | Qualifiers T2Quals, |
| 3098 | InitializationSequence &Sequence); |
| 3099 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3100 | static void TryValueInitialization(Sema &S, |
| 3101 | const InitializedEntity &Entity, |
| 3102 | const InitializationKind &Kind, |
| 3103 | InitializationSequence &Sequence, |
| 3104 | InitListExpr *InitList = 0); |
| 3105 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3106 | static void TryListInitialization(Sema &S, |
| 3107 | const InitializedEntity &Entity, |
| 3108 | const InitializationKind &Kind, |
| 3109 | InitListExpr *InitList, |
| 3110 | InitializationSequence &Sequence); |
| 3111 | |
| 3112 | /// \brief Attempt list initialization of a reference. |
| 3113 | static void TryReferenceListInitialization(Sema &S, |
| 3114 | const InitializedEntity &Entity, |
| 3115 | const InitializationKind &Kind, |
| 3116 | InitListExpr *InitList, |
| 3117 | InitializationSequence &Sequence) |
| 3118 | { |
| 3119 | // First, catch C++03 where this isn't possible. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3120 | if (!S.getLangOpts().CPlusPlus11) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3121 | Sequence.SetFailed(InitializationSequence::FK_ReferenceBindingToInitList); |
| 3122 | return; |
| 3123 | } |
| 3124 | |
| 3125 | QualType DestType = Entity.getType(); |
| 3126 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3127 | Qualifiers T1Quals; |
| 3128 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
| 3129 | |
| 3130 | // Reference initialization via an initializer list works thus: |
| 3131 | // If the initializer list consists of a single element that is |
| 3132 | // reference-related to the referenced type, bind directly to that element |
| 3133 | // (possibly creating temporaries). |
| 3134 | // Otherwise, initialize a temporary with the initializer list and |
| 3135 | // bind to that. |
| 3136 | if (InitList->getNumInits() == 1) { |
| 3137 | Expr *Initializer = InitList->getInit(0); |
| 3138 | QualType cv2T2 = Initializer->getType(); |
| 3139 | Qualifiers T2Quals; |
| 3140 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
| 3141 | |
| 3142 | // If this fails, creating a temporary wouldn't work either. |
| 3143 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3144 | T1, Sequence)) |
| 3145 | return; |
| 3146 | |
| 3147 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3148 | bool dummy1, dummy2, dummy3; |
| 3149 | Sema::ReferenceCompareResult RefRelationship |
| 3150 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, dummy1, |
| 3151 | dummy2, dummy3); |
| 3152 | if (RefRelationship >= Sema::Ref_Related) { |
| 3153 | // Try to bind the reference here. |
| 3154 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3155 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3156 | if (Sequence) |
| 3157 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
| 3158 | return; |
| 3159 | } |
Richard Smith | 02d65ee | 2013-01-15 07:58:29 +0000 | [diff] [blame] | 3160 | |
| 3161 | // Update the initializer if we've resolved an overloaded function. |
| 3162 | if (Sequence.step_begin() != Sequence.step_end()) |
| 3163 | Sequence.RewrapReferenceInitList(cv1T1, InitList); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | // Not reference-related. Create a temporary and bind to that. |
| 3167 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3168 | |
| 3169 | TryListInitialization(S, TempEntity, Kind, InitList, Sequence); |
| 3170 | if (Sequence) { |
| 3171 | if (DestType->isRValueReferenceType() || |
| 3172 | (T1Quals.hasConst() && !T1Quals.hasVolatile())) |
| 3173 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3174 | else |
| 3175 | Sequence.SetFailed( |
| 3176 | InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
| 3177 | } |
| 3178 | } |
| 3179 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3180 | /// \brief Attempt list initialization (C++0x [dcl.init.list]) |
| 3181 | static void TryListInitialization(Sema &S, |
| 3182 | const InitializedEntity &Entity, |
| 3183 | const InitializationKind &Kind, |
| 3184 | InitListExpr *InitList, |
| 3185 | InitializationSequence &Sequence) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3186 | QualType DestType = Entity.getType(); |
| 3187 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3188 | // C++ doesn't allow scalar initialization with more than one argument. |
| 3189 | // But C99 complex numbers are scalars and it makes sense there. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3190 | if (S.getLangOpts().CPlusPlus && DestType->isScalarType() && |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3191 | !DestType->isAnyComplexType() && InitList->getNumInits() > 1) { |
| 3192 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForScalar); |
| 3193 | return; |
| 3194 | } |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3195 | if (DestType->isReferenceType()) { |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3196 | TryReferenceListInitialization(S, Entity, Kind, InitList, Sequence); |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3197 | return; |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3198 | } |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3199 | if (DestType->isRecordType()) { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3200 | if (S.RequireCompleteType(InitList->getLocStart(), DestType, 0)) { |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 3201 | Sequence.setIncompleteTypeFailure(DestType); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3202 | return; |
| 3203 | } |
| 3204 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3205 | // C++11 [dcl.init.list]p3: |
| 3206 | // - If T is an aggregate, aggregate initialization is performed. |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3207 | if (!DestType->isAggregateType()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3208 | if (S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3209 | // - Otherwise, if the initializer list has no elements and T is a |
| 3210 | // class type with a default constructor, the object is |
| 3211 | // value-initialized. |
| 3212 | if (InitList->getNumInits() == 0) { |
| 3213 | CXXRecordDecl *RD = DestType->getAsCXXRecordDecl(); |
Richard Smith | e5411b7 | 2012-12-01 02:35:44 +0000 | [diff] [blame] | 3214 | if (RD->hasDefaultConstructor()) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3215 | TryValueInitialization(S, Entity, Kind, Sequence, InitList); |
| 3216 | return; |
| 3217 | } |
| 3218 | } |
| 3219 | |
| 3220 | // - Otherwise, if T is a specialization of std::initializer_list<E>, |
| 3221 | // an initializer_list object constructed [...] |
| 3222 | if (TryInitializerListConstruction(S, InitList, DestType, Sequence)) |
| 3223 | return; |
| 3224 | |
| 3225 | // - Otherwise, if T is a class type, constructors are considered. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3226 | Expr *InitListAsExpr = InitList; |
| 3227 | TryConstructorInitialization(S, Entity, Kind, InitListAsExpr, DestType, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3228 | Sequence, /*InitListSyntax*/true); |
Sebastian Redl | d2231c9 | 2012-02-19 12:27:43 +0000 | [diff] [blame] | 3229 | } else |
| 3230 | Sequence.SetFailed( |
| 3231 | InitializationSequence::FK_InitListBadDestinationType); |
| 3232 | return; |
| 3233 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3234 | } |
| 3235 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3236 | InitListChecker CheckInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 3237 | DestType, /*VerifyOnly=*/true, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3238 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3239 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 3240 | if (CheckInitList.HadError()) { |
| 3241 | Sequence.SetFailed(InitializationSequence::FK_ListInitializationFailed); |
| 3242 | return; |
| 3243 | } |
| 3244 | |
| 3245 | // Add the list initialization step with the built init list. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 3246 | Sequence.AddListInitializationStep(DestType); |
| 3247 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3248 | |
| 3249 | /// \brief Try a reference initialization that involves calling a conversion |
| 3250 | /// function. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3251 | static OverloadingResult TryRefInitWithConversionFunction(Sema &S, |
| 3252 | const InitializedEntity &Entity, |
| 3253 | const InitializationKind &Kind, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3254 | Expr *Initializer, |
| 3255 | bool AllowRValues, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3256 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3257 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3258 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
| 3259 | QualType T1 = cv1T1.getUnqualifiedType(); |
| 3260 | QualType cv2T2 = Initializer->getType(); |
| 3261 | QualType T2 = cv2T2.getUnqualifiedType(); |
| 3262 | |
| 3263 | bool DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3264 | bool ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3265 | bool ObjCLifetimeConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3266 | assert(!S.CompareReferenceRelationship(Initializer->getLocStart(), |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3267 | T1, T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3268 | ObjCConversion, |
| 3269 | ObjCLifetimeConversion) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3270 | "Must have incompatible references when binding via conversion"); |
Chandler Carruth | 60cfcec | 2009-12-13 01:37:04 +0000 | [diff] [blame] | 3271 | (void)DerivedToBase; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3272 | (void)ObjCConversion; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3273 | (void)ObjCLifetimeConversion; |
| 3274 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3275 | // Build the candidate set directly in the initialization sequence |
| 3276 | // structure, so that it will persist if we fail. |
| 3277 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3278 | CandidateSet.clear(); |
| 3279 | |
| 3280 | // Determine whether we are allowed to call explicit constructors or |
| 3281 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3282 | bool AllowExplicit = Kind.AllowExplicit(); |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3283 | bool AllowExplicitConvs = Kind.allowExplicitConversionFunctions(); |
| 3284 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3285 | const RecordType *T1RecordType = 0; |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3286 | if (AllowRValues && (T1RecordType = T1->getAs<RecordType>()) && |
| 3287 | !S.RequireCompleteType(Kind.getLocation(), T1, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3288 | // The type we're converting to is a class type. Enumerate its constructors |
| 3289 | // to see if there is a suitable conversion. |
| 3290 | CXXRecordDecl *T1RecordDecl = cast<CXXRecordDecl>(T1RecordType->getDecl()); |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3291 | |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3292 | DeclContext::lookup_result R = S.LookupConstructors(T1RecordDecl); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3293 | // The container holding the constructors can under certain conditions |
| 3294 | // be changed while iterating (e.g. because of deserialization). |
| 3295 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3296 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 3297 | for (SmallVector<NamedDecl*, 16>::iterator |
| 3298 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 3299 | NamedDecl *D = *CI; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3300 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
| 3301 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3302 | // Find the constructor (which may be a template). |
| 3303 | CXXConstructorDecl *Constructor = 0; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3304 | FunctionTemplateDecl *ConstructorTmpl = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3305 | if (ConstructorTmpl) |
| 3306 | Constructor = cast<CXXConstructorDecl>( |
| 3307 | ConstructorTmpl->getTemplatedDecl()); |
| 3308 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3309 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3310 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3311 | if (!Constructor->isInvalidDecl() && |
| 3312 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3313 | if (ConstructorTmpl) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3314 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3315 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3316 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3317 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3318 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3319 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3320 | Initializer, CandidateSet, |
Argyrios Kyrtzidis | b72db89 | 2010-10-05 03:05:30 +0000 | [diff] [blame] | 3321 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3322 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3323 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3324 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3325 | if (T1RecordType && T1RecordType->getDecl()->isInvalidDecl()) |
| 3326 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3327 | |
Douglas Gregor | 6b6d01f | 2010-05-07 19:42:26 +0000 | [diff] [blame] | 3328 | const RecordType *T2RecordType = 0; |
| 3329 | if ((T2RecordType = T2->getAs<RecordType>()) && |
| 3330 | !S.RequireCompleteType(Kind.getLocation(), T2, 0)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3331 | // The type we're converting from is a class type, enumerate its conversion |
| 3332 | // functions. |
| 3333 | CXXRecordDecl *T2RecordDecl = cast<CXXRecordDecl>(T2RecordType->getDecl()); |
| 3334 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3335 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3336 | CXXRecordDecl::conversion_iterator> |
| 3337 | Conversions = T2RecordDecl->getVisibleConversionFunctions(); |
| 3338 | for (CXXRecordDecl::conversion_iterator |
| 3339 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3340 | NamedDecl *D = *I; |
| 3341 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3342 | if (isa<UsingShadowDecl>(D)) |
| 3343 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3344 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3345 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3346 | CXXConversionDecl *Conv; |
| 3347 | if (ConvTemplate) |
| 3348 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
| 3349 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3350 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3351 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3352 | // If the conversion function doesn't return a reference type, |
| 3353 | // it can't be considered for this conversion unless we're allowed to |
| 3354 | // consider rvalues. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3355 | // FIXME: Do we need to make sure that we only consider conversion |
| 3356 | // candidates with reference-compatible results? That might be needed to |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3357 | // break recursion. |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3358 | if ((AllowExplicitConvs || !Conv->isExplicit()) && |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3359 | (AllowRValues || Conv->getConversionType()->isLValueReferenceType())){ |
| 3360 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3361 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3362 | ActingDC, Initializer, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3363 | DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3364 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3365 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
Douglas Gregor | 564cb06 | 2011-01-21 00:27:08 +0000 | [diff] [blame] | 3366 | Initializer, DestType, CandidateSet); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3367 | } |
| 3368 | } |
| 3369 | } |
John McCall | 572fc62 | 2010-08-17 07:23:57 +0000 | [diff] [blame] | 3370 | if (T2RecordType && T2RecordType->getDecl()->isInvalidDecl()) |
| 3371 | return OR_No_Viable_Function; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3372 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3373 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3374 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3375 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3376 | OverloadCandidateSet::iterator Best; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3377 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 3378 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3379 | return Result; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3380 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3381 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 3382 | // This is the overload that will be used for this initialization step if we |
| 3383 | // use this initialization. Mark it as referenced. |
| 3384 | Function->setReferenced(); |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 3385 | |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3386 | // Compute the returned type of the conversion. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3387 | if (isa<CXXConversionDecl>(Function)) |
| 3388 | T2 = Function->getResultType(); |
| 3389 | else |
| 3390 | T2 = cv1T1; |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3391 | |
| 3392 | // Add the user-defined conversion step. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3393 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3394 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 3395 | T2.getNonLValueExprType(S.Context), |
| 3396 | HadMultipleCandidates); |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3397 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3398 | // Determine whether we need to perform derived-to-base or |
Eli Friedman | 0398101 | 2009-12-11 02:42:07 +0000 | [diff] [blame] | 3399 | // cv-qualification adjustments. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3400 | ExprValueKind VK = VK_RValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3401 | if (T2->isLValueReferenceType()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3402 | VK = VK_LValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3403 | else if (const RValueReferenceType *RRef = T2->getAs<RValueReferenceType>()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3404 | VK = RRef->getPointeeType()->isFunctionType() ? VK_LValue : VK_XValue; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 3405 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3406 | bool NewDerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3407 | bool NewObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3408 | bool NewObjCLifetimeConversion = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3409 | Sema::ReferenceCompareResult NewRefRelationship |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3410 | = S.CompareReferenceRelationship(DeclLoc, T1, |
Douglas Gregor | 6398235 | 2010-07-13 18:40:04 +0000 | [diff] [blame] | 3411 | T2.getNonLValueExprType(S.Context), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3412 | NewDerivedToBase, NewObjCConversion, |
| 3413 | NewObjCLifetimeConversion); |
Douglas Gregor | a1a9f03 | 2010-03-07 23:17:44 +0000 | [diff] [blame] | 3414 | if (NewRefRelationship == Sema::Ref_Incompatible) { |
| 3415 | // If the type we've converted to is not reference-related to the |
| 3416 | // type we're looking for, then there is another conversion step |
| 3417 | // we need to perform to produce a temporary of the right type |
| 3418 | // that we'll be binding to. |
| 3419 | ImplicitConversionSequence ICS; |
| 3420 | ICS.setStandard(); |
| 3421 | ICS.Standard = Best->FinalConversion; |
| 3422 | T2 = ICS.Standard.getToType(2); |
| 3423 | Sequence.AddConversionSequenceStep(ICS, T2); |
| 3424 | } else if (NewDerivedToBase) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3425 | Sequence.AddDerivedToBaseCastStep( |
| 3426 | S.Context.getQualifiedType(T1, |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3427 | T2.getNonReferenceType().getQualifiers()), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3428 | VK); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3429 | else if (NewObjCConversion) |
| 3430 | Sequence.AddObjCObjectConversionStep( |
| 3431 | S.Context.getQualifiedType(T1, |
| 3432 | T2.getNonReferenceType().getQualifiers())); |
| 3433 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3434 | if (cv1T1.getQualifiers() != T2.getNonReferenceType().getQualifiers()) |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3435 | Sequence.AddQualificationConversionStep(cv1T1, VK); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3436 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3437 | Sequence.AddReferenceBindingStep(cv1T1, !T2->isReferenceType()); |
| 3438 | return OR_Success; |
| 3439 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3440 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3441 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 3442 | const InitializedEntity &Entity, |
| 3443 | Expr *CurInitExpr); |
| 3444 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3445 | /// \brief Attempt reference initialization (C++0x [dcl.init.ref]) |
| 3446 | static void TryReferenceInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3447 | const InitializedEntity &Entity, |
| 3448 | const InitializationKind &Kind, |
| 3449 | Expr *Initializer, |
| 3450 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3451 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3452 | QualType cv1T1 = DestType->getAs<ReferenceType>()->getPointeeType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3453 | Qualifiers T1Quals; |
| 3454 | QualType T1 = S.Context.getUnqualifiedArrayType(cv1T1, T1Quals); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3455 | QualType cv2T2 = Initializer->getType(); |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3456 | Qualifiers T2Quals; |
| 3457 | QualType T2 = S.Context.getUnqualifiedArrayType(cv2T2, T2Quals); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3458 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3459 | // If the initializer is the address of an overloaded function, try |
| 3460 | // to resolve the overloaded function. If all goes well, T2 is the |
| 3461 | // type of the resulting function. |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3462 | if (ResolveOverloadedFunctionForReferenceBinding(S, Initializer, cv2T2, T2, |
| 3463 | T1, Sequence)) |
| 3464 | return; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3465 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3466 | // Delegate everything else to a subfunction. |
| 3467 | TryReferenceInitializationCore(S, Entity, Kind, Initializer, cv1T1, T1, |
| 3468 | T1Quals, cv2T2, T2, T2Quals, Sequence); |
| 3469 | } |
| 3470 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3471 | /// Converts the target of reference initialization so that it has the |
| 3472 | /// appropriate qualifiers and value kind. |
| 3473 | /// |
| 3474 | /// In this case, 'x' is an 'int' lvalue, but it needs to be 'const int'. |
| 3475 | /// \code |
| 3476 | /// int x; |
| 3477 | /// const int &r = x; |
| 3478 | /// \endcode |
| 3479 | /// |
| 3480 | /// In this case the reference is binding to a bitfield lvalue, which isn't |
| 3481 | /// valid. Perform a load to create a lifetime-extended temporary instead. |
| 3482 | /// \code |
| 3483 | /// const int &r = someStruct.bitfield; |
| 3484 | /// \endcode |
| 3485 | static ExprValueKind |
| 3486 | convertQualifiersAndValueKindIfNecessary(Sema &S, |
| 3487 | InitializationSequence &Sequence, |
| 3488 | Expr *Initializer, |
| 3489 | QualType cv1T1, |
| 3490 | Qualifiers T1Quals, |
| 3491 | Qualifiers T2Quals, |
| 3492 | bool IsLValueRef) { |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3493 | bool IsNonAddressableType = Initializer->refersToBitField() || |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3494 | Initializer->refersToVectorElement(); |
| 3495 | |
| 3496 | if (IsNonAddressableType) { |
| 3497 | // C++11 [dcl.init.ref]p5: [...] Otherwise, the reference shall be an |
| 3498 | // lvalue reference to a non-volatile const type, or the reference shall be |
| 3499 | // an rvalue reference. |
| 3500 | // |
| 3501 | // If not, we can't make a temporary and bind to that. Give up and allow the |
| 3502 | // error to be diagnosed later. |
| 3503 | if (IsLValueRef && (!T1Quals.hasConst() || T1Quals.hasVolatile())) { |
| 3504 | assert(Initializer->isGLValue()); |
| 3505 | return Initializer->getValueKind(); |
| 3506 | } |
| 3507 | |
| 3508 | // Force a load so we can materialize a temporary. |
| 3509 | Sequence.AddLValueToRValueStep(cv1T1.getUnqualifiedType()); |
| 3510 | return VK_RValue; |
| 3511 | } |
| 3512 | |
| 3513 | if (T1Quals != T2Quals) { |
| 3514 | Sequence.AddQualificationConversionStep(cv1T1, |
| 3515 | Initializer->getValueKind()); |
| 3516 | } |
| 3517 | |
| 3518 | return Initializer->getValueKind(); |
| 3519 | } |
| 3520 | |
| 3521 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 3522 | /// \brief Reference initialization without resolving overloaded functions. |
| 3523 | static void TryReferenceInitializationCore(Sema &S, |
| 3524 | const InitializedEntity &Entity, |
| 3525 | const InitializationKind &Kind, |
| 3526 | Expr *Initializer, |
| 3527 | QualType cv1T1, QualType T1, |
| 3528 | Qualifiers T1Quals, |
| 3529 | QualType cv2T2, QualType T2, |
| 3530 | Qualifiers T2Quals, |
| 3531 | InitializationSequence &Sequence) { |
| 3532 | QualType DestType = Entity.getType(); |
| 3533 | SourceLocation DeclLoc = Initializer->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3534 | // Compute some basic properties of the types and the initializer. |
| 3535 | bool isLValueRef = DestType->isLValueReferenceType(); |
| 3536 | bool isRValueRef = !isLValueRef; |
| 3537 | bool DerivedToBase = false; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3538 | bool ObjCConversion = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3539 | bool ObjCLifetimeConversion = false; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3540 | Expr::Classification InitCategory = Initializer->Classify(S.Context); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3541 | Sema::ReferenceCompareResult RefRelationship |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3542 | = S.CompareReferenceRelationship(DeclLoc, cv1T1, cv2T2, DerivedToBase, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3543 | ObjCConversion, ObjCLifetimeConversion); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3544 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3545 | // C++0x [dcl.init.ref]p5: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3546 | // 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] | 3547 | // "cv2 T2" as follows: |
| 3548 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3549 | // - If the reference is an lvalue reference and the initializer |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3550 | // expression |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3551 | // Note the analogous bullet points for rvlaue refs to functions. Because |
| 3552 | // there are no function rvalues in C++, rvalue refs to functions are treated |
| 3553 | // like lvalue refs. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3554 | OverloadingResult ConvOvlResult = OR_Success; |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3555 | bool T1Function = T1->isFunctionType(); |
| 3556 | if (isLValueRef || T1Function) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3557 | if (InitCategory.isLValue() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3558 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3559 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3560 | RefRelationship == Sema::Ref_Related))) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3561 | // - 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] | 3562 | // reference-compatible with "cv2 T2," or |
| 3563 | // |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3564 | // 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] | 3565 | // bit-field when we're determining whether the reference initialization |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3566 | // can occur. However, we do pay attention to whether it is a bit-field |
| 3567 | // to decide whether we're actually binding to a temporary created from |
| 3568 | // the bit-field. |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3569 | if (DerivedToBase) |
| 3570 | Sequence.AddDerivedToBaseCastStep( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3571 | S.Context.getQualifiedType(T1, T2Quals), |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3572 | VK_LValue); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 3573 | else if (ObjCConversion) |
| 3574 | Sequence.AddObjCObjectConversionStep( |
| 3575 | S.Context.getQualifiedType(T1, T2Quals)); |
| 3576 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3577 | ExprValueKind ValueKind = |
| 3578 | convertQualifiersAndValueKindIfNecessary(S, Sequence, Initializer, |
| 3579 | cv1T1, T1Quals, T2Quals, |
| 3580 | isLValueRef); |
| 3581 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3582 | return; |
| 3583 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3584 | |
| 3585 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3586 | // reference-related to T2, and can be implicitly converted to an |
| 3587 | // lvalue of type "cv3 T3," where "cv1 T1" is reference-compatible |
| 3588 | // with "cv3 T3" (this conversion is selected by enumerating the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3589 | // applicable conversion functions (13.3.1.6) and choosing the best |
| 3590 | // one through overload resolution (13.3)), |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3591 | // If we have an rvalue ref to function type here, the rhs must be |
| 3592 | // an rvalue. |
| 3593 | if (RefRelationship == Sema::Ref_Incompatible && T2->isRecordType() && |
| 3594 | (isLValueRef || InitCategory.isRValue())) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3595 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, Kind, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3596 | Initializer, |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3597 | /*AllowRValues=*/isRValueRef, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3598 | Sequence); |
| 3599 | if (ConvOvlResult == OR_Success) |
| 3600 | return; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 3601 | if (ConvOvlResult != OR_No_Viable_Function) { |
| 3602 | Sequence.SetOverloadFailure( |
| 3603 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3604 | ConvOvlResult); |
| 3605 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3606 | } |
| 3607 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3608 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3609 | // - Otherwise, the reference shall be an lvalue reference to a |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3610 | // 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] | 3611 | // shall be an rvalue reference. |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3612 | if (isLValueRef && !(T1Quals.hasConst() && !T1Quals.hasVolatile())) { |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3613 | if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3614 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
| 3615 | else if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3616 | Sequence.SetOverloadFailure( |
| 3617 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3618 | ConvOvlResult); |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3619 | else |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3620 | Sequence.SetFailed(InitCategory.isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3621 | ? (RefRelationship == Sema::Ref_Related |
| 3622 | ? InitializationSequence::FK_ReferenceInitDropsQualifiers |
| 3623 | : InitializationSequence::FK_NonConstLValueReferenceBindingToUnrelated) |
| 3624 | : InitializationSequence::FK_NonConstLValueReferenceBindingToTemporary); |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3625 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3626 | return; |
| 3627 | } |
Sebastian Redl | 4680bf2 | 2010-06-30 18:13:39 +0000 | [diff] [blame] | 3628 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3629 | // - If the initializer expression |
| 3630 | // - is an xvalue, class prvalue, array prvalue, or function lvalue and |
| 3631 | // "cv1 T1" is reference-compatible with "cv2 T2" |
| 3632 | // Note: functions are handled below. |
| 3633 | if (!T1Function && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3634 | (RefRelationship >= Sema::Ref_Compatible_With_Added_Qualification || |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3635 | (Kind.isCStyleOrFunctionalCast() && |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3636 | RefRelationship == Sema::Ref_Related)) && |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3637 | (InitCategory.isXValue() || |
| 3638 | (InitCategory.isPRValue() && T2->isRecordType()) || |
| 3639 | (InitCategory.isPRValue() && T2->isArrayType()))) { |
| 3640 | ExprValueKind ValueKind = InitCategory.isXValue()? VK_XValue : VK_RValue; |
| 3641 | if (InitCategory.isPRValue() && T2->isRecordType()) { |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3642 | // The corresponding bullet in C++03 [dcl.init.ref]p5 gives the |
| 3643 | // compiler the freedom to perform a copy here or bind to the |
| 3644 | // object, while C++0x requires that we bind directly to the |
| 3645 | // object. Hence, we always bind to the object without making an |
| 3646 | // extra copy. However, in C++03 requires that we check for the |
| 3647 | // presence of a suitable copy constructor: |
| 3648 | // |
| 3649 | // The constructor that would be used to make the copy shall |
| 3650 | // be callable whether or not the copy is actually done. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3651 | if (!S.getLangOpts().CPlusPlus11 && !S.getLangOpts().MicrosoftExt) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 3652 | Sequence.AddExtraneousCopyToTemporary(cv2T2); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3653 | else if (S.getLangOpts().CPlusPlus11) |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 3654 | CheckCXX98CompatAccessibleCopy(S, Entity, Initializer); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3655 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3656 | |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3657 | if (DerivedToBase) |
| 3658 | Sequence.AddDerivedToBaseCastStep(S.Context.getQualifiedType(T1, T2Quals), |
| 3659 | ValueKind); |
| 3660 | else if (ObjCConversion) |
| 3661 | Sequence.AddObjCObjectConversionStep( |
| 3662 | S.Context.getQualifiedType(T1, T2Quals)); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3663 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 3664 | ValueKind = convertQualifiersAndValueKindIfNecessary(S, Sequence, |
| 3665 | Initializer, cv1T1, |
| 3666 | T1Quals, T2Quals, |
| 3667 | isLValueRef); |
| 3668 | |
| 3669 | Sequence.AddReferenceBindingStep(cv1T1, ValueKind == VK_RValue); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3670 | return; |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3671 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3672 | |
| 3673 | // - has a class type (i.e., T2 is a class type), where T1 is not |
| 3674 | // reference-related to T2, and can be implicitly converted to an |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3675 | // xvalue, class prvalue, or function lvalue of type "cv3 T3", |
| 3676 | // where "cv1 T1" is reference-compatible with "cv3 T3", |
Douglas Gregor | c5db24d | 2011-01-20 16:44:54 +0000 | [diff] [blame] | 3677 | if (T2->isRecordType()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3678 | if (RefRelationship == Sema::Ref_Incompatible) { |
| 3679 | ConvOvlResult = TryRefInitWithConversionFunction(S, Entity, |
| 3680 | Kind, Initializer, |
| 3681 | /*AllowRValues=*/true, |
| 3682 | Sequence); |
| 3683 | if (ConvOvlResult) |
| 3684 | Sequence.SetOverloadFailure( |
| 3685 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3686 | ConvOvlResult); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3687 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3688 | return; |
| 3689 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3690 | |
Douglas Gregor | defa32e | 2013-03-26 23:59:23 +0000 | [diff] [blame] | 3691 | if ((RefRelationship == Sema::Ref_Compatible || |
| 3692 | RefRelationship == Sema::Ref_Compatible_With_Added_Qualification) && |
| 3693 | isRValueRef && InitCategory.isLValue()) { |
| 3694 | Sequence.SetFailed( |
| 3695 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3696 | return; |
| 3697 | } |
| 3698 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3699 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3700 | return; |
| 3701 | } |
NAKAMURA Takumi | 0099530 | 2011-01-27 07:09:49 +0000 | [diff] [blame] | 3702 | |
| 3703 | // - Otherwise, a temporary of type "cv1 T1" is created and initialized |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3704 | // from the initializer expression using the rules for a non-reference |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3705 | // copy initialization (8.5). The reference is then bound to the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3706 | // temporary. [...] |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3707 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3708 | // Determine whether we are allowed to call explicit constructors or |
| 3709 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3710 | bool AllowExplicit = Kind.AllowExplicit(); |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3711 | |
| 3712 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1); |
| 3713 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3714 | ImplicitConversionSequence ICS |
| 3715 | = S.TryImplicitConversion(Initializer, TempEntity.getType(), |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 3716 | /*SuppressUserConversions*/ false, |
| 3717 | AllowExplicit, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 3718 | /*FIXME:InOverloadResolution=*/false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3719 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 3720 | /*AllowObjCWritebackConversion=*/false); |
| 3721 | |
| 3722 | if (ICS.isBad()) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3723 | // FIXME: Use the conversion function set stored in ICS to turn |
| 3724 | // this into an overloading ambiguity diagnostic. However, we need |
| 3725 | // to keep that set as an OverloadCandidateSet rather than as some |
| 3726 | // other kind of set. |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3727 | if (ConvOvlResult && !Sequence.getFailedCandidateSet().empty()) |
| 3728 | Sequence.SetOverloadFailure( |
| 3729 | InitializationSequence::FK_ReferenceInitOverloadFailed, |
| 3730 | ConvOvlResult); |
Douglas Gregor | 3afb977 | 2010-11-08 15:20:28 +0000 | [diff] [blame] | 3731 | else if (S.Context.getCanonicalType(T2) == S.Context.OverloadTy) |
| 3732 | Sequence.SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 3733 | else |
| 3734 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitFailed); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3735 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3736 | } else { |
| 3737 | Sequence.AddConversionSequenceStep(ICS, TempEntity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3738 | } |
| 3739 | |
| 3740 | // [...] If T1 is reference-related to T2, cv1 must be the |
| 3741 | // same cv-qualification as, or greater cv-qualification |
| 3742 | // than, cv2; otherwise, the program is ill-formed. |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3743 | unsigned T1CVRQuals = T1Quals.getCVRQualifiers(); |
| 3744 | unsigned T2CVRQuals = T2Quals.getCVRQualifiers(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3745 | if (RefRelationship == Sema::Ref_Related && |
Chandler Carruth | 5535c38 | 2010-01-12 20:32:25 +0000 | [diff] [blame] | 3746 | (T1CVRQuals | T2CVRQuals) != T1CVRQuals) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3747 | Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers); |
| 3748 | return; |
| 3749 | } |
| 3750 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3751 | // [...] 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] | 3752 | // reference, the initializer expression shall not be an lvalue. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3753 | if (RefRelationship >= Sema::Ref_Related && !isLValueRef && |
Douglas Gregor | b2855ad | 2011-01-21 00:52:42 +0000 | [diff] [blame] | 3754 | InitCategory.isLValue()) { |
| 3755 | Sequence.SetFailed( |
| 3756 | InitializationSequence::FK_RValueReferenceBindingToLValue); |
| 3757 | return; |
| 3758 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3759 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3760 | Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true); |
| 3761 | return; |
| 3762 | } |
| 3763 | |
| 3764 | /// \brief Attempt character array initialization from a string literal |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3765 | /// (C++ [dcl.init.string], C99 6.7.8). |
| 3766 | static void TryStringLiteralInitialization(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3767 | const InitializedEntity &Entity, |
| 3768 | const InitializationKind &Kind, |
| 3769 | Expr *Initializer, |
| 3770 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3771 | Sequence.AddStringInitStep(Entity.getType()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3774 | /// \brief Attempt value initialization (C++ [dcl.init]p7). |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3775 | static void TryValueInitialization(Sema &S, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3776 | const InitializedEntity &Entity, |
| 3777 | const InitializationKind &Kind, |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3778 | InitializationSequence &Sequence, |
| 3779 | InitListExpr *InitList) { |
| 3780 | assert((!InitList || InitList->getNumInits() == 0) && |
| 3781 | "Shouldn't use value-init for non-empty init lists"); |
| 3782 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3783 | // C++98 [dcl.init]p5, C++11 [dcl.init]p7: |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3784 | // |
| 3785 | // To value-initialize an object of type T means: |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3786 | QualType T = Entity.getType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3787 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3788 | // -- if T is an array type, then each element is value-initialized; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3789 | T = S.Context.getBaseElementType(T); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3790 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3791 | if (const RecordType *RT = T->getAs<RecordType>()) { |
| 3792 | if (CXXRecordDecl *ClassDecl = dyn_cast<CXXRecordDecl>(RT->getDecl())) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3793 | bool NeedZeroInitialization = true; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3794 | if (!S.getLangOpts().CPlusPlus11) { |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3795 | // C++98: |
| 3796 | // -- if T is a class type (clause 9) with a user-declared constructor |
| 3797 | // (12.1), then the default constructor for T is called (and the |
| 3798 | // initialization is ill-formed if T has no accessible default |
| 3799 | // constructor); |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3800 | if (ClassDecl->hasUserDeclaredConstructor()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3801 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3802 | } else { |
| 3803 | // C++11: |
| 3804 | // -- if T is a class type (clause 9) with either no default constructor |
| 3805 | // (12.1 [class.ctor]) or a default constructor that is user-provided |
| 3806 | // or deleted, then the object is default-initialized; |
| 3807 | CXXConstructorDecl *CD = S.LookupDefaultConstructor(ClassDecl); |
| 3808 | if (!CD || !CD->getCanonicalDecl()->isDefaulted() || CD->isDeleted()) |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3809 | NeedZeroInitialization = false; |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3810 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3811 | |
Richard Smith | 1d0c9a8 | 2012-02-14 21:14:13 +0000 | [diff] [blame] | 3812 | // -- if T is a (possibly cv-qualified) non-union class type without a |
| 3813 | // user-provided or deleted default constructor, then the object is |
| 3814 | // zero-initialized and, if T has a non-trivial default constructor, |
| 3815 | // default-initialized; |
Richard Smith | 6678a05 | 2012-10-18 00:44:17 +0000 | [diff] [blame] | 3816 | // The 'non-union' here was removed by DR1502. The 'non-trivial default |
| 3817 | // constructor' part was removed by DR1507. |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3818 | if (NeedZeroInitialization) |
| 3819 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3820 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3821 | // C++03: |
| 3822 | // -- if T is a non-union class type without a user-declared constructor, |
| 3823 | // then every non-static data member and base class component of T is |
| 3824 | // value-initialized; |
| 3825 | // [...] A program that calls for [...] value-initialization of an |
| 3826 | // entity of reference type is ill-formed. |
| 3827 | // |
| 3828 | // C++11 doesn't need this handling, because value-initialization does not |
| 3829 | // occur recursively there, and the implicit default constructor is |
| 3830 | // defined as deleted in the problematic cases. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3831 | if (!S.getLangOpts().CPlusPlus11 && |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 3832 | ClassDecl->hasUninitializedReferenceMember()) { |
| 3833 | Sequence.SetFailed(InitializationSequence::FK_TooManyInitsForReference); |
| 3834 | return; |
| 3835 | } |
| 3836 | |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3837 | // If this is list-value-initialization, pass the empty init list on when |
| 3838 | // building the constructor call. This affects the semantics of a few |
| 3839 | // things (such as whether an explicit default constructor can be called). |
| 3840 | Expr *InitListAsExpr = InitList; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3841 | MultiExprArg Args(&InitListAsExpr, InitList ? 1 : 0); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 3842 | bool InitListSyntax = InitList; |
| 3843 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 3844 | return TryConstructorInitialization(S, Entity, Kind, Args, T, Sequence, |
| 3845 | InitListSyntax); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3846 | } |
| 3847 | } |
| 3848 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3849 | Sequence.AddZeroInitializationStep(Entity.getType()); |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 3850 | } |
| 3851 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3852 | /// \brief Attempt default initialization (C++ [dcl.init]p6). |
| 3853 | static void TryDefaultInitialization(Sema &S, |
| 3854 | const InitializedEntity &Entity, |
| 3855 | const InitializationKind &Kind, |
| 3856 | InitializationSequence &Sequence) { |
| 3857 | assert(Kind.getKind() == InitializationKind::IK_Default); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3858 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3859 | // C++ [dcl.init]p6: |
| 3860 | // To default-initialize an object of type T means: |
| 3861 | // - if T is an array type, each element is default-initialized; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3862 | QualType DestType = S.Context.getBaseElementType(Entity.getType()); |
| 3863 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3864 | // - if T is a (possibly cv-qualified) class type (Clause 9), the default |
| 3865 | // constructor for T is called (and the initialization is ill-formed if |
| 3866 | // T has no accessible default constructor); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3867 | if (DestType->isRecordType() && S.getLangOpts().CPlusPlus) { |
Dmitri Gribenko | 62ed889 | 2013-05-05 20:40:26 +0000 | [diff] [blame] | 3868 | TryConstructorInitialization(S, Entity, Kind, None, DestType, Sequence); |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 3869 | return; |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3870 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3871 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3872 | // - otherwise, no initialization is performed. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3873 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3874 | // If a program calls for the default initialization of an object of |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3875 | // 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] | 3876 | // default constructor. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3877 | if (DestType.isConstQualified() && S.getLangOpts().CPlusPlus) { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3878 | Sequence.SetFailed(InitializationSequence::FK_DefaultInitOfConst); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3879 | return; |
| 3880 | } |
| 3881 | |
| 3882 | // If the destination type has a lifetime property, zero-initialize it. |
| 3883 | if (DestType.getQualifiers().hasObjCLifetime()) { |
| 3884 | Sequence.AddZeroInitializationStep(Entity.getType()); |
| 3885 | return; |
| 3886 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3889 | /// \brief Attempt a user-defined conversion between two types (C++ [dcl.init]), |
| 3890 | /// which enumerates all conversion functions and performs overload resolution |
| 3891 | /// to select the best. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3892 | static void TryUserDefinedConversion(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 3893 | const InitializedEntity &Entity, |
| 3894 | const InitializationKind &Kind, |
| 3895 | Expr *Initializer, |
| 3896 | InitializationSequence &Sequence) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3897 | QualType DestType = Entity.getType(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3898 | assert(!DestType->isReferenceType() && "References are handled elsewhere"); |
| 3899 | QualType SourceType = Initializer->getType(); |
| 3900 | assert((DestType->isRecordType() || SourceType->isRecordType()) && |
| 3901 | "Must have a class type to perform a user-defined conversion"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3902 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3903 | // Build the candidate set directly in the initialization sequence |
| 3904 | // structure, so that it will persist if we fail. |
| 3905 | OverloadCandidateSet &CandidateSet = Sequence.getFailedCandidateSet(); |
| 3906 | CandidateSet.clear(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3907 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3908 | // Determine whether we are allowed to call explicit constructors or |
| 3909 | // explicit conversion operators. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 3910 | bool AllowExplicit = Kind.AllowExplicit(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3911 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3912 | if (const RecordType *DestRecordType = DestType->getAs<RecordType>()) { |
| 3913 | // The type we're converting to is a class type. Enumerate its constructors |
| 3914 | // to see if there is a suitable conversion. |
| 3915 | CXXRecordDecl *DestRecordDecl |
| 3916 | = cast<CXXRecordDecl>(DestRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3917 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3918 | // Try to complete the type we're converting to. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3919 | if (!S.RequireCompleteType(Kind.getLocation(), DestType, 0)) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3920 | DeclContext::lookup_result R = S.LookupConstructors(DestRecordDecl); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 3921 | // The container holding the constructors can under certain conditions |
| 3922 | // be changed while iterating. To be safe we copy the lookup results |
| 3923 | // to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 3924 | SmallVector<NamedDecl*, 8> CopyOfCon(R.begin(), R.end()); |
David Blaikie | 3d5cf5e | 2012-10-18 16:57:32 +0000 | [diff] [blame] | 3925 | for (SmallVector<NamedDecl*, 8>::iterator |
| 3926 | Con = CopyOfCon.begin(), ConEnd = CopyOfCon.end(); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3927 | Con != ConEnd; ++Con) { |
| 3928 | NamedDecl *D = *Con; |
| 3929 | DeclAccessPair FoundDecl = DeclAccessPair::make(D, D->getAccess()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3930 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3931 | // Find the constructor (which may be a template). |
| 3932 | CXXConstructorDecl *Constructor = 0; |
| 3933 | FunctionTemplateDecl *ConstructorTmpl |
| 3934 | = dyn_cast<FunctionTemplateDecl>(D); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3935 | if (ConstructorTmpl) |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3936 | Constructor = cast<CXXConstructorDecl>( |
| 3937 | ConstructorTmpl->getTemplatedDecl()); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3938 | else |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3939 | Constructor = cast<CXXConstructorDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3940 | |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3941 | if (!Constructor->isInvalidDecl() && |
| 3942 | Constructor->isConvertingConstructor(AllowExplicit)) { |
| 3943 | if (ConstructorTmpl) |
| 3944 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, |
| 3945 | /*ExplicitArgs*/ 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3946 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3947 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3948 | else |
| 3949 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 3950 | Initializer, CandidateSet, |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 3951 | /*SuppressUserConversions=*/true); |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3952 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3953 | } |
Douglas Gregor | 087fb7d | 2010-04-26 14:36:57 +0000 | [diff] [blame] | 3954 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3955 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3956 | |
| 3957 | SourceLocation DeclLoc = Initializer->getLocStart(); |
| 3958 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3959 | if (const RecordType *SourceRecordType = SourceType->getAs<RecordType>()) { |
| 3960 | // The type we're converting from is a class type, enumerate its conversion |
| 3961 | // functions. |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 3962 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3963 | // We can only enumerate the conversion functions for a complete type; if |
| 3964 | // the type isn't complete, simply skip this step. |
| 3965 | if (!S.RequireCompleteType(DeclLoc, SourceType, 0)) { |
| 3966 | CXXRecordDecl *SourceRecordDecl |
| 3967 | = cast<CXXRecordDecl>(SourceRecordType->getDecl()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3968 | |
Argyrios Kyrtzidis | 9d29543 | 2012-11-28 03:56:09 +0000 | [diff] [blame] | 3969 | std::pair<CXXRecordDecl::conversion_iterator, |
| 3970 | CXXRecordDecl::conversion_iterator> |
| 3971 | Conversions = SourceRecordDecl->getVisibleConversionFunctions(); |
| 3972 | for (CXXRecordDecl::conversion_iterator |
| 3973 | I = Conversions.first, E = Conversions.second; I != E; ++I) { |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3974 | NamedDecl *D = *I; |
| 3975 | CXXRecordDecl *ActingDC = cast<CXXRecordDecl>(D->getDeclContext()); |
| 3976 | if (isa<UsingShadowDecl>(D)) |
| 3977 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3978 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3979 | FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(D); |
| 3980 | CXXConversionDecl *Conv; |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3981 | if (ConvTemplate) |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3982 | Conv = cast<CXXConversionDecl>(ConvTemplate->getTemplatedDecl()); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3983 | else |
John McCall | 32daa42 | 2010-03-31 01:36:47 +0000 | [diff] [blame] | 3984 | Conv = cast<CXXConversionDecl>(D); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3985 | |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3986 | if (AllowExplicit || !Conv->isExplicit()) { |
| 3987 | if (ConvTemplate) |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3988 | S.AddTemplateConversionCandidate(ConvTemplate, I.getPair(), |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3989 | ActingDC, Initializer, DestType, |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3990 | CandidateSet); |
| 3991 | else |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 3992 | S.AddConversionCandidate(Conv, I.getPair(), ActingDC, |
John McCall | 86820f5 | 2010-01-26 01:37:31 +0000 | [diff] [blame] | 3993 | Initializer, DestType, CandidateSet); |
Eli Friedman | 33c2da9 | 2009-12-20 22:12:03 +0000 | [diff] [blame] | 3994 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 3995 | } |
| 3996 | } |
| 3997 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 3998 | |
| 3999 | // Perform overload resolution. If it fails, return the failed result. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4000 | OverloadCandidateSet::iterator Best; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4001 | if (OverloadingResult Result |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4002 | = CandidateSet.BestViableFunction(S, DeclLoc, Best, true)) { |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4003 | Sequence.SetOverloadFailure( |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4004 | InitializationSequence::FK_UserConversionOverloadFailed, |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4005 | Result); |
| 4006 | return; |
| 4007 | } |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4008 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4009 | FunctionDecl *Function = Best->Function; |
Nick Lewycky | 3c86a5c | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 4010 | Function->setReferenced(); |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4011 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4012 | |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4013 | if (isa<CXXConstructorDecl>(Function)) { |
| 4014 | // Add the user-defined conversion step. Any cv-qualification conversion is |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4015 | // subsumed by the initialization. Per DR5, the created temporary is of the |
| 4016 | // cv-unqualified type of the destination. |
| 4017 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, |
| 4018 | DestType.getUnqualifiedType(), |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4019 | HadMultipleCandidates); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4020 | return; |
| 4021 | } |
| 4022 | |
| 4023 | // Add the user-defined conversion step that calls the conversion function. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4024 | QualType ConvType = Function->getCallResultType(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4025 | if (ConvType->getAs<RecordType>()) { |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 4026 | // 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] | 4027 | // the resulting temporary object (possible to create an object of |
| 4028 | // a base class type). That copy is not a separate conversion, so |
| 4029 | // we just make a note of the actual destination type (possibly a |
| 4030 | // base class of the type returned by the conversion function) and |
| 4031 | // let the user-defined conversion step handle the conversion. |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4032 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, DestType, |
| 4033 | HadMultipleCandidates); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4034 | return; |
| 4035 | } |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4036 | |
Abramo Bagnara | 22c107b | 2011-11-19 11:44:21 +0000 | [diff] [blame] | 4037 | Sequence.AddUserConversionStep(Function, Best->FoundDecl, ConvType, |
| 4038 | HadMultipleCandidates); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4039 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4040 | // If the conversion following the call to the conversion function |
| 4041 | // is interesting, add it as a separate step. |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4042 | if (Best->FinalConversion.First || Best->FinalConversion.Second || |
| 4043 | Best->FinalConversion.Third) { |
| 4044 | ImplicitConversionSequence ICS; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 4045 | ICS.setStandard(); |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4046 | ICS.Standard = Best->FinalConversion; |
| 4047 | Sequence.AddConversionSequenceStep(ICS, DestType); |
| 4048 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4049 | } |
| 4050 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4051 | /// The non-zero enum values here are indexes into diagnostic alternatives. |
| 4052 | enum InvalidICRKind { IIK_okay, IIK_nonlocal, IIK_nonscalar }; |
| 4053 | |
| 4054 | /// Determines whether this expression is an acceptable ICR source. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4055 | static InvalidICRKind isInvalidICRSource(ASTContext &C, Expr *e, |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4056 | bool isAddressOf, bool &isWeakAccess) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4057 | // Skip parens. |
| 4058 | e = e->IgnoreParens(); |
| 4059 | |
| 4060 | // Skip address-of nodes. |
| 4061 | if (UnaryOperator *op = dyn_cast<UnaryOperator>(e)) { |
| 4062 | if (op->getOpcode() == UO_AddrOf) |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4063 | return isInvalidICRSource(C, op->getSubExpr(), /*addressof*/ true, |
| 4064 | isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4065 | |
| 4066 | // Skip certain casts. |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4067 | } else if (CastExpr *ce = dyn_cast<CastExpr>(e)) { |
| 4068 | switch (ce->getCastKind()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4069 | case CK_Dependent: |
| 4070 | case CK_BitCast: |
| 4071 | case CK_LValueBitCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4072 | case CK_NoOp: |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4073 | return isInvalidICRSource(C, ce->getSubExpr(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4074 | |
| 4075 | case CK_ArrayToPointerDecay: |
| 4076 | return IIK_nonscalar; |
| 4077 | |
| 4078 | case CK_NullToPointer: |
| 4079 | return IIK_okay; |
| 4080 | |
| 4081 | default: |
| 4082 | break; |
| 4083 | } |
| 4084 | |
| 4085 | // 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] | 4086 | } else if (isa<DeclRefExpr>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4087 | // set isWeakAccess to true, to mean that there will be an implicit |
| 4088 | // load which requires a cleanup. |
| 4089 | if (e->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4090 | isWeakAccess = true; |
| 4091 | |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4092 | if (!isAddressOf) return IIK_nonlocal; |
| 4093 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 4094 | VarDecl *var = dyn_cast<VarDecl>(cast<DeclRefExpr>(e)->getDecl()); |
| 4095 | if (!var) return IIK_nonlocal; |
John McCall | c03fa49 | 2011-06-27 23:59:58 +0000 | [diff] [blame] | 4096 | |
| 4097 | return (var->hasLocalStorage() ? IIK_okay : IIK_nonlocal); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4098 | |
| 4099 | // If we have a conditional operator, check both sides. |
| 4100 | } else if (ConditionalOperator *cond = dyn_cast<ConditionalOperator>(e)) { |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4101 | if (InvalidICRKind iik = isInvalidICRSource(C, cond->getLHS(), isAddressOf, |
| 4102 | isWeakAccess)) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4103 | return iik; |
| 4104 | |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4105 | return isInvalidICRSource(C, cond->getRHS(), isAddressOf, isWeakAccess); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4106 | |
| 4107 | // These are never scalar. |
| 4108 | } else if (isa<ArraySubscriptExpr>(e)) { |
| 4109 | return IIK_nonscalar; |
| 4110 | |
| 4111 | // Otherwise, it needs to be a null pointer constant. |
| 4112 | } else { |
| 4113 | return (e->isNullPointerConstant(C, Expr::NPC_ValueDependentIsNull) |
| 4114 | ? IIK_okay : IIK_nonlocal); |
| 4115 | } |
| 4116 | |
| 4117 | return IIK_nonlocal; |
| 4118 | } |
| 4119 | |
| 4120 | /// Check whether the given expression is a valid operand for an |
| 4121 | /// indirect copy/restore. |
| 4122 | static void checkIndirectCopyRestoreSource(Sema &S, Expr *src) { |
| 4123 | assert(src->isRValue()); |
Fariborz Jahanian | 82c458e | 2012-11-27 23:02:53 +0000 | [diff] [blame] | 4124 | bool isWeakAccess = false; |
| 4125 | InvalidICRKind iik = isInvalidICRSource(S.Context, src, false, isWeakAccess); |
| 4126 | // If isWeakAccess to true, there will be an implicit |
| 4127 | // load which requires a cleanup. |
| 4128 | if (S.getLangOpts().ObjCAutoRefCount && isWeakAccess) |
| 4129 | S.ExprNeedsCleanups = true; |
| 4130 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4131 | if (iik == IIK_okay) return; |
| 4132 | |
| 4133 | S.Diag(src->getExprLoc(), diag::err_arc_nonlocal_writeback) |
| 4134 | << ((unsigned) iik - 1) // shift index into diagnostic explanations |
| 4135 | << src->getSourceRange(); |
| 4136 | } |
| 4137 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4138 | /// \brief Determine whether we have compatible array types for the |
| 4139 | /// purposes of GNU by-copy array initialization. |
| 4140 | static bool hasCompatibleArrayTypes(ASTContext &Context, |
| 4141 | const ArrayType *Dest, |
| 4142 | const ArrayType *Source) { |
| 4143 | // If the source and destination array types are equivalent, we're |
| 4144 | // done. |
| 4145 | if (Context.hasSameType(QualType(Dest, 0), QualType(Source, 0))) |
| 4146 | return true; |
| 4147 | |
| 4148 | // Make sure that the element types are the same. |
| 4149 | if (!Context.hasSameType(Dest->getElementType(), Source->getElementType())) |
| 4150 | return false; |
| 4151 | |
| 4152 | // The only mismatch we allow is when the destination is an |
| 4153 | // incomplete array type and the source is a constant array type. |
| 4154 | return Source->isConstantArrayType() && Dest->isIncompleteArrayType(); |
| 4155 | } |
| 4156 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4157 | static bool tryObjCWritebackConversion(Sema &S, |
| 4158 | InitializationSequence &Sequence, |
| 4159 | const InitializedEntity &Entity, |
| 4160 | Expr *Initializer) { |
| 4161 | bool ArrayDecay = false; |
| 4162 | QualType ArgType = Initializer->getType(); |
| 4163 | QualType ArgPointee; |
| 4164 | if (const ArrayType *ArgArrayType = S.Context.getAsArrayType(ArgType)) { |
| 4165 | ArrayDecay = true; |
| 4166 | ArgPointee = ArgArrayType->getElementType(); |
| 4167 | ArgType = S.Context.getPointerType(ArgPointee); |
| 4168 | } |
| 4169 | |
| 4170 | // Handle write-back conversion. |
| 4171 | QualType ConvertedArgType; |
| 4172 | if (!S.isObjCWritebackConversion(ArgType, Entity.getType(), |
| 4173 | ConvertedArgType)) |
| 4174 | return false; |
| 4175 | |
| 4176 | // We should copy unless we're passing to an argument explicitly |
| 4177 | // marked 'out'. |
| 4178 | bool ShouldCopy = true; |
| 4179 | if (ParmVarDecl *param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4180 | ShouldCopy = (param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4181 | |
| 4182 | // Do we need an lvalue conversion? |
| 4183 | if (ArrayDecay || Initializer->isGLValue()) { |
| 4184 | ImplicitConversionSequence ICS; |
| 4185 | ICS.setStandard(); |
| 4186 | ICS.Standard.setAsIdentityConversion(); |
| 4187 | |
| 4188 | QualType ResultType; |
| 4189 | if (ArrayDecay) { |
| 4190 | ICS.Standard.First = ICK_Array_To_Pointer; |
| 4191 | ResultType = S.Context.getPointerType(ArgPointee); |
| 4192 | } else { |
| 4193 | ICS.Standard.First = ICK_Lvalue_To_Rvalue; |
| 4194 | ResultType = Initializer->getType().getNonLValueExprType(S.Context); |
| 4195 | } |
| 4196 | |
| 4197 | Sequence.AddConversionSequenceStep(ICS, ResultType); |
| 4198 | } |
| 4199 | |
| 4200 | Sequence.AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
| 4201 | return true; |
| 4202 | } |
| 4203 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4204 | static bool TryOCLSamplerInitialization(Sema &S, |
| 4205 | InitializationSequence &Sequence, |
| 4206 | QualType DestType, |
| 4207 | Expr *Initializer) { |
| 4208 | if (!S.getLangOpts().OpenCL || !DestType->isSamplerT() || |
| 4209 | !Initializer->isIntegerConstantExpr(S.getASTContext())) |
| 4210 | return false; |
| 4211 | |
| 4212 | Sequence.AddOCLSamplerInitStep(DestType); |
| 4213 | return true; |
| 4214 | } |
| 4215 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4216 | // |
| 4217 | // OpenCL 1.2 spec, s6.12.10 |
| 4218 | // |
| 4219 | // The event argument can also be used to associate the |
| 4220 | // async_work_group_copy with a previous async copy allowing |
| 4221 | // an event to be shared by multiple async copies; otherwise |
| 4222 | // event should be zero. |
| 4223 | // |
| 4224 | static bool TryOCLZeroEventInitialization(Sema &S, |
| 4225 | InitializationSequence &Sequence, |
| 4226 | QualType DestType, |
| 4227 | Expr *Initializer) { |
| 4228 | if (!S.getLangOpts().OpenCL || !DestType->isEventT() || |
| 4229 | !Initializer->isIntegerConstantExpr(S.getASTContext()) || |
| 4230 | (Initializer->EvaluateKnownConstInt(S.getASTContext()) != 0)) |
| 4231 | return false; |
| 4232 | |
| 4233 | Sequence.AddOCLZeroEventStep(DestType); |
| 4234 | return true; |
| 4235 | } |
| 4236 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4237 | InitializationSequence::InitializationSequence(Sema &S, |
| 4238 | const InitializedEntity &Entity, |
| 4239 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4240 | MultiExprArg Args) |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4241 | : FailedCandidateSet(Kind.getLocation()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4242 | ASTContext &Context = S.Context; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4243 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4244 | // Eliminate non-overload placeholder types in the arguments. We |
| 4245 | // need to do this before checking whether types are dependent |
| 4246 | // because lowering a pseudo-object expression might well give us |
| 4247 | // something of dependent type. |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4248 | for (unsigned I = 0, E = Args.size(); I != E; ++I) |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 4249 | if (Args[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4250 | // FIXME: should we be doing this here? |
| 4251 | ExprResult result = S.CheckPlaceholderExpr(Args[I]); |
| 4252 | if (result.isInvalid()) { |
| 4253 | SetFailed(FK_PlaceholderType); |
| 4254 | return; |
| 4255 | } |
| 4256 | Args[I] = result.take(); |
| 4257 | } |
| 4258 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4259 | // C++0x [dcl.init]p16: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4260 | // The semantics of initializers are as follows. The destination type is |
| 4261 | // the type of the object or reference being initialized and the source |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4262 | // 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] | 4263 | // 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] | 4264 | // parenthesized list of expressions. |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4265 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4266 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4267 | if (DestType->isDependentType() || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4268 | Expr::hasAnyTypeDependentArguments(Args)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4269 | SequenceKind = DependentSequence; |
| 4270 | return; |
| 4271 | } |
| 4272 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 4273 | // Almost everything is a normal sequence. |
| 4274 | setSequenceKind(NormalSequence); |
| 4275 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4276 | QualType SourceType; |
| 4277 | Expr *Initializer = 0; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4278 | if (Args.size() == 1) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4279 | Initializer = Args[0]; |
| 4280 | if (!isa<InitListExpr>(Initializer)) |
| 4281 | SourceType = Initializer->getType(); |
| 4282 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4283 | |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4284 | // - If the initializer is a (non-parenthesized) braced-init-list, the |
| 4285 | // object is list-initialized (8.5.4). |
| 4286 | if (Kind.getKind() != InitializationKind::IK_Direct) { |
| 4287 | if (InitListExpr *InitList = dyn_cast_or_null<InitListExpr>(Initializer)) { |
| 4288 | TryListInitialization(S, Entity, Kind, InitList, *this); |
| 4289 | return; |
| 4290 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4291 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4292 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4293 | // - If the destination type is a reference type, see 8.5.3. |
| 4294 | if (DestType->isReferenceType()) { |
| 4295 | // C++0x [dcl.init.ref]p1: |
| 4296 | // A variable declared to be a T& or T&&, that is, "reference to type T" |
| 4297 | // (8.3.2), shall be initialized by an object, or function, of type T or |
| 4298 | // by an object that can be converted into a T. |
| 4299 | // (Therefore, multiple arguments are not permitted.) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4300 | if (Args.size() != 1) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4301 | SetFailed(FK_TooManyInitsForReference); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4302 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4303 | TryReferenceInitialization(S, Entity, Kind, Args[0], *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4304 | return; |
| 4305 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4306 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4307 | // - If the initializer is (), the object is value-initialized. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4308 | if (Kind.getKind() == InitializationKind::IK_Value || |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4309 | (Kind.getKind() == InitializationKind::IK_Direct && Args.empty())) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4310 | TryValueInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4311 | return; |
| 4312 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4313 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4314 | // Handle default initialization. |
Nick Lewycky | 7663f39 | 2010-11-20 01:29:55 +0000 | [diff] [blame] | 4315 | if (Kind.getKind() == InitializationKind::IK_Default) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4316 | TryDefaultInitialization(S, Entity, Kind, *this); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4317 | return; |
| 4318 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4319 | |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4320 | // - If the destination type is an array of characters, an array of |
| 4321 | // char16_t, an array of char32_t, or an array of wchar_t, and the |
| 4322 | // initializer is a string literal, see 8.5.2. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4323 | // - Otherwise, if the destination type is an array, the program is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4324 | // ill-formed. |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4325 | if (const ArrayType *DestAT = Context.getAsArrayType(DestType)) { |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 4326 | if (Initializer && isa<VariableArrayType>(DestAT)) { |
| 4327 | SetFailed(FK_VariableLengthArrayHasInitializer); |
| 4328 | return; |
| 4329 | } |
| 4330 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4331 | if (Initializer) { |
| 4332 | switch (IsStringInit(Initializer, DestAT, Context)) { |
| 4333 | case SIF_None: |
| 4334 | TryStringLiteralInitialization(S, Entity, Kind, Initializer, *this); |
| 4335 | return; |
| 4336 | case SIF_NarrowStringIntoWideChar: |
| 4337 | SetFailed(FK_NarrowStringIntoWideCharArray); |
| 4338 | return; |
| 4339 | case SIF_WideStringIntoChar: |
| 4340 | SetFailed(FK_WideStringIntoCharArray); |
| 4341 | return; |
| 4342 | case SIF_IncompatWideStringIntoWideChar: |
| 4343 | SetFailed(FK_IncompatWideStringIntoWideChar); |
| 4344 | return; |
| 4345 | case SIF_Other: |
| 4346 | break; |
| 4347 | } |
John McCall | ce6c9b7 | 2011-02-21 07:22:22 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4350 | // Note: as an GNU C extension, we allow initialization of an |
| 4351 | // array from a compound literal that creates an array of the same |
| 4352 | // type, so long as the initializer has no side effects. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4353 | if (!S.getLangOpts().CPlusPlus && Initializer && |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4354 | isa<CompoundLiteralExpr>(Initializer->IgnoreParens()) && |
| 4355 | Initializer->getType()->isArrayType()) { |
| 4356 | const ArrayType *SourceAT |
| 4357 | = Context.getAsArrayType(Initializer->getType()); |
| 4358 | if (!hasCompatibleArrayTypes(S.Context, DestAT, SourceAT)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4359 | SetFailed(FK_ArrayTypeMismatch); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4360 | else if (Initializer->HasSideEffects(S.Context)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4361 | SetFailed(FK_NonConstantArrayInit); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4362 | else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4363 | AddArrayInitStep(DestType); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 4364 | } |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4365 | } |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 4366 | // Note: as a GNU C++ extension, we allow list-initialization of a |
| 4367 | // class member of array type from a parenthesized initializer list. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4368 | else if (S.getLangOpts().CPlusPlus && |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 4369 | Entity.getKind() == InitializedEntity::EK_Member && |
| 4370 | Initializer && isa<InitListExpr>(Initializer)) { |
| 4371 | TryListInitialization(S, Entity, Kind, cast<InitListExpr>(Initializer), |
| 4372 | *this); |
| 4373 | AddParenthesizedArrayInitStep(DestType); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4374 | } else if (DestAT->getElementType()->isCharType()) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4375 | SetFailed(FK_ArrayNeedsInitListOrStringLiteral); |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 4376 | else if (IsWideCharCompatible(DestAT->getElementType(), Context)) |
| 4377 | SetFailed(FK_ArrayNeedsInitListOrWideStringLiteral); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4378 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4379 | SetFailed(FK_ArrayNeedsInitList); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4380 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4381 | return; |
| 4382 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4383 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4384 | // Determine whether we should consider writeback conversions for |
| 4385 | // Objective-C ARC. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4386 | bool allowObjCWritebackConversion = S.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4387 | Entity.getKind() == InitializedEntity::EK_Parameter; |
| 4388 | |
| 4389 | // We're at the end of the line for C: it's either a write-back conversion |
| 4390 | // 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] | 4391 | if (!S.getLangOpts().CPlusPlus) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4392 | // If allowed, check whether this is an Objective-C writeback conversion. |
| 4393 | if (allowObjCWritebackConversion && |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4394 | tryObjCWritebackConversion(S, *this, Entity, Initializer)) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4395 | return; |
| 4396 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4397 | |
| 4398 | if (TryOCLSamplerInitialization(S, *this, DestType, Initializer)) |
| 4399 | return; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4400 | |
| 4401 | if (TryOCLZeroEventInitialization(S, *this, DestType, Initializer)) |
| 4402 | return; |
| 4403 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4404 | // Handle initialization in C |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4405 | AddCAssignmentStep(DestType); |
| 4406 | MaybeProduceObjCObject(S, *this, Entity); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 4407 | return; |
| 4408 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4409 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4410 | assert(S.getLangOpts().CPlusPlus); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4411 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4412 | // - If the destination type is a (possibly cv-qualified) class type: |
| 4413 | if (DestType->isRecordType()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4414 | // - If the initialization is direct-initialization, or if it is |
| 4415 | // copy-initialization where the cv-unqualified version of the |
| 4416 | // 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] | 4417 | // class of the destination, constructors are considered. [...] |
| 4418 | if (Kind.getKind() == InitializationKind::IK_Direct || |
| 4419 | (Kind.getKind() == InitializationKind::IK_Copy && |
| 4420 | (Context.hasSameUnqualifiedType(SourceType, DestType) || |
| 4421 | S.IsDerivedFrom(SourceType, DestType)))) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4422 | TryConstructorInitialization(S, Entity, Kind, Args, |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4423 | Entity.getType(), *this); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4424 | // - Otherwise (i.e., for the remaining copy-initialization cases), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4425 | // user-defined conversion sequences that can convert from the source |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4426 | // type to the destination type or (when a conversion function is |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4427 | // used) to a derived class thereof are enumerated as described in |
| 4428 | // 13.3.1.4, and the best one is chosen through overload resolution |
| 4429 | // (13.3). |
| 4430 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4431 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4432 | return; |
| 4433 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4434 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4435 | if (Args.size() > 1) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4436 | SetFailed(FK_TooManyInitsForScalar); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4437 | return; |
| 4438 | } |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4439 | assert(Args.size() == 1 && "Zero-argument case handled above"); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4440 | |
| 4441 | // - Otherwise, if the source type is a (possibly cv-qualified) class |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4442 | // type, conversion functions are considered. |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4443 | if (!SourceType.isNull() && SourceType->isRecordType()) { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4444 | TryUserDefinedConversion(S, Entity, Kind, Initializer, *this); |
| 4445 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4446 | return; |
| 4447 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4448 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4449 | // - Otherwise, the initial value of the object being initialized is the |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 4450 | // (possibly converted) value of the initializer expression. Standard |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4451 | // conversions (Clause 4) will be used, if necessary, to convert the |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4452 | // initializer expression to the cv-unqualified version of the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4453 | // destination type; no user-defined conversions are considered. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4454 | |
| 4455 | ImplicitConversionSequence ICS |
| 4456 | = S.TryImplicitConversion(Initializer, Entity.getType(), |
| 4457 | /*SuppressUserConversions*/true, |
John McCall | 369371c | 2010-06-04 02:29:22 +0000 | [diff] [blame] | 4458 | /*AllowExplicitConversions*/ false, |
Douglas Gregor | 14d0aee | 2011-01-27 00:58:17 +0000 | [diff] [blame] | 4459 | /*InOverloadResolution*/ false, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4460 | /*CStyle=*/Kind.isCStyleOrFunctionalCast(), |
| 4461 | allowObjCWritebackConversion); |
| 4462 | |
| 4463 | if (ICS.isStandard() && |
| 4464 | ICS.Standard.Second == ICK_Writeback_Conversion) { |
| 4465 | // Objective-C ARC writeback conversion. |
| 4466 | |
| 4467 | // We should copy unless we're passing to an argument explicitly |
| 4468 | // marked 'out'. |
| 4469 | bool ShouldCopy = true; |
| 4470 | if (ParmVarDecl *Param = cast_or_null<ParmVarDecl>(Entity.getDecl())) |
| 4471 | ShouldCopy = (Param->getObjCDeclQualifier() != ParmVarDecl::OBJC_TQ_Out); |
| 4472 | |
| 4473 | // If there was an lvalue adjustment, add it as a separate conversion. |
| 4474 | if (ICS.Standard.First == ICK_Array_To_Pointer || |
| 4475 | ICS.Standard.First == ICK_Lvalue_To_Rvalue) { |
| 4476 | ImplicitConversionSequence LvalueICS; |
| 4477 | LvalueICS.setStandard(); |
| 4478 | LvalueICS.Standard.setAsIdentityConversion(); |
| 4479 | LvalueICS.Standard.setAllToTypes(ICS.Standard.getToType(0)); |
| 4480 | LvalueICS.Standard.First = ICS.Standard.First; |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4481 | AddConversionSequenceStep(LvalueICS, ICS.Standard.getToType(0)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4482 | } |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4483 | |
| 4484 | AddPassByIndirectCopyRestoreStep(Entity.getType(), ShouldCopy); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4485 | } else if (ICS.isBad()) { |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 4486 | DeclAccessPair dap; |
| 4487 | if (Initializer->getType() == Context.OverloadTy && |
| 4488 | !S.ResolveAddressOfOverloadedFunction(Initializer |
| 4489 | , DestType, false, dap)) |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4490 | SetFailed(InitializationSequence::FK_AddressOfOverloadFailed); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4491 | else |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4492 | SetFailed(InitializationSequence::FK_ConversionFailed); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4493 | } else { |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4494 | AddConversionSequenceStep(ICS, Entity.getType()); |
John McCall | 856d379 | 2011-06-16 23:24:51 +0000 | [diff] [blame] | 4495 | |
Rafael Espindola | 12ce0a0 | 2011-07-14 22:58:04 +0000 | [diff] [blame] | 4496 | MaybeProduceObjCObject(S, *this, Entity); |
Douglas Gregor | 8e96043 | 2010-11-08 03:40:48 +0000 | [diff] [blame] | 4497 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4498 | } |
| 4499 | |
| 4500 | InitializationSequence::~InitializationSequence() { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4501 | for (SmallVectorImpl<Step>::iterator Step = Steps.begin(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4502 | StepEnd = Steps.end(); |
| 4503 | Step != StepEnd; ++Step) |
| 4504 | Step->Destroy(); |
| 4505 | } |
| 4506 | |
| 4507 | //===----------------------------------------------------------------------===// |
| 4508 | // Perform initialization |
| 4509 | //===----------------------------------------------------------------------===// |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4510 | static Sema::AssignmentAction |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4511 | getAssignmentAction(const InitializedEntity &Entity) { |
| 4512 | switch(Entity.getKind()) { |
| 4513 | case InitializedEntity::EK_Variable: |
| 4514 | case InitializedEntity::EK_New: |
Douglas Gregor | a3998bd | 2010-12-02 21:47:04 +0000 | [diff] [blame] | 4515 | case InitializedEntity::EK_Exception: |
| 4516 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4517 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4518 | return Sema::AA_Initializing; |
| 4519 | |
| 4520 | case InitializedEntity::EK_Parameter: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4521 | if (Entity.getDecl() && |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 4522 | isa<ObjCMethodDecl>(Entity.getDecl()->getDeclContext())) |
| 4523 | return Sema::AA_Sending; |
| 4524 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4525 | return Sema::AA_Passing; |
| 4526 | |
| 4527 | case InitializedEntity::EK_Result: |
| 4528 | return Sema::AA_Returning; |
| 4529 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4530 | case InitializedEntity::EK_Temporary: |
| 4531 | // FIXME: Can we tell apart casting vs. converting? |
| 4532 | return Sema::AA_Casting; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4533 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4534 | case InitializedEntity::EK_Member: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4535 | case InitializedEntity::EK_ArrayElement: |
| 4536 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4537 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4538 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4539 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4540 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4541 | return Sema::AA_Initializing; |
| 4542 | } |
| 4543 | |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4544 | llvm_unreachable("Invalid EntityKind!"); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4545 | } |
| 4546 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4547 | /// \brief Whether we should bind a created object as a temporary when |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4548 | /// initializing the given entity. |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4549 | static bool shouldBindAsTemporary(const InitializedEntity &Entity) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4550 | switch (Entity.getKind()) { |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4551 | case InitializedEntity::EK_ArrayElement: |
| 4552 | case InitializedEntity::EK_Member: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4553 | case InitializedEntity::EK_Result: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4554 | case InitializedEntity::EK_New: |
| 4555 | case InitializedEntity::EK_Variable: |
| 4556 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4557 | case InitializedEntity::EK_Delegating: |
Anders Carlsson | d3d824d | 2010-01-23 04:34:47 +0000 | [diff] [blame] | 4558 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4559 | case InitializedEntity::EK_ComplexElement: |
Anders Carlsson | a508b7d | 2010-02-06 23:23:06 +0000 | [diff] [blame] | 4560 | case InitializedEntity::EK_Exception: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4561 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4562 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4563 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4564 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4565 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4566 | case InitializedEntity::EK_Parameter: |
| 4567 | case InitializedEntity::EK_Temporary: |
| 4568 | return true; |
| 4569 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4570 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4571 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4572 | } |
| 4573 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4574 | /// \brief Whether the given entity, when initialized with an object |
| 4575 | /// created for that initialization, requires destruction. |
| 4576 | static bool shouldDestroyTemporary(const InitializedEntity &Entity) { |
| 4577 | switch (Entity.getKind()) { |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4578 | case InitializedEntity::EK_Result: |
| 4579 | case InitializedEntity::EK_New: |
| 4580 | case InitializedEntity::EK_Base: |
Sean Hunt | 059ce0d | 2011-05-01 07:04:31 +0000 | [diff] [blame] | 4581 | case InitializedEntity::EK_Delegating: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4582 | case InitializedEntity::EK_VectorElement: |
Eli Friedman | 0c706c2 | 2011-09-19 23:17:44 +0000 | [diff] [blame] | 4583 | case InitializedEntity::EK_ComplexElement: |
Fariborz Jahanian | 310b1c4 | 2010-06-07 16:14:00 +0000 | [diff] [blame] | 4584 | case InitializedEntity::EK_BlockElement: |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4585 | case InitializedEntity::EK_LambdaCapture: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4586 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4587 | |
Richard Smith | 774d8b4 | 2013-01-08 00:08:23 +0000 | [diff] [blame] | 4588 | case InitializedEntity::EK_Member: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4589 | case InitializedEntity::EK_Variable: |
| 4590 | case InitializedEntity::EK_Parameter: |
| 4591 | case InitializedEntity::EK_Temporary: |
| 4592 | case InitializedEntity::EK_ArrayElement: |
| 4593 | case InitializedEntity::EK_Exception: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4594 | case InitializedEntity::EK_CompoundLiteralInit: |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4595 | return true; |
| 4596 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4597 | |
| 4598 | llvm_unreachable("missed an InitializedEntity kind?"); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 4599 | } |
| 4600 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4601 | /// \brief Look for copy and move constructors and constructor templates, for |
| 4602 | /// copying an object via direct-initialization (per C++11 [dcl.init]p16). |
| 4603 | static void LookupCopyAndMoveConstructors(Sema &S, |
| 4604 | OverloadCandidateSet &CandidateSet, |
| 4605 | CXXRecordDecl *Class, |
| 4606 | Expr *CurInitExpr) { |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4607 | DeclContext::lookup_result R = S.LookupConstructors(Class); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4608 | // The container holding the constructors can under certain conditions |
| 4609 | // be changed while iterating (e.g. because of deserialization). |
| 4610 | // To be safe we copy the lookup results to a new container. |
David Blaikie | 3bc93e3 | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 4611 | SmallVector<NamedDecl*, 16> Ctors(R.begin(), R.end()); |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4612 | for (SmallVector<NamedDecl*, 16>::iterator |
| 4613 | CI = Ctors.begin(), CE = Ctors.end(); CI != CE; ++CI) { |
| 4614 | NamedDecl *D = *CI; |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4615 | CXXConstructorDecl *Constructor = 0; |
| 4616 | |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4617 | if ((Constructor = dyn_cast<CXXConstructorDecl>(D))) { |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4618 | // Handle copy/moveconstructors, only. |
| 4619 | if (!Constructor || Constructor->isInvalidDecl() || |
| 4620 | !Constructor->isCopyOrMoveConstructor() || |
| 4621 | !Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4622 | continue; |
| 4623 | |
| 4624 | DeclAccessPair FoundDecl |
| 4625 | = DeclAccessPair::make(Constructor, Constructor->getAccess()); |
| 4626 | S.AddOverloadCandidate(Constructor, FoundDecl, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4627 | CurInitExpr, CandidateSet); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4628 | continue; |
| 4629 | } |
| 4630 | |
| 4631 | // Handle constructor templates. |
Argyrios Kyrtzidis | 8682b93 | 2012-11-13 05:07:23 +0000 | [diff] [blame] | 4632 | FunctionTemplateDecl *ConstructorTmpl = cast<FunctionTemplateDecl>(D); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4633 | if (ConstructorTmpl->isInvalidDecl()) |
| 4634 | continue; |
| 4635 | |
| 4636 | Constructor = cast<CXXConstructorDecl>( |
| 4637 | ConstructorTmpl->getTemplatedDecl()); |
| 4638 | if (!Constructor->isConvertingConstructor(/*AllowExplicit=*/true)) |
| 4639 | continue; |
| 4640 | |
| 4641 | // FIXME: Do we need to limit this to copy-constructor-like |
| 4642 | // candidates? |
| 4643 | DeclAccessPair FoundDecl |
| 4644 | = DeclAccessPair::make(ConstructorTmpl, ConstructorTmpl->getAccess()); |
| 4645 | S.AddTemplateOverloadCandidate(ConstructorTmpl, FoundDecl, 0, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4646 | CurInitExpr, CandidateSet, true); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | /// \brief Get the location at which initialization diagnostics should appear. |
| 4651 | static SourceLocation getInitializationLoc(const InitializedEntity &Entity, |
| 4652 | Expr *Initializer) { |
| 4653 | switch (Entity.getKind()) { |
| 4654 | case InitializedEntity::EK_Result: |
| 4655 | return Entity.getReturnLoc(); |
| 4656 | |
| 4657 | case InitializedEntity::EK_Exception: |
| 4658 | return Entity.getThrowLoc(); |
| 4659 | |
| 4660 | case InitializedEntity::EK_Variable: |
| 4661 | return Entity.getDecl()->getLocation(); |
| 4662 | |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 4663 | case InitializedEntity::EK_LambdaCapture: |
| 4664 | return Entity.getCaptureLoc(); |
| 4665 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4666 | case InitializedEntity::EK_ArrayElement: |
| 4667 | case InitializedEntity::EK_Member: |
| 4668 | case InitializedEntity::EK_Parameter: |
| 4669 | case InitializedEntity::EK_Temporary: |
| 4670 | case InitializedEntity::EK_New: |
| 4671 | case InitializedEntity::EK_Base: |
| 4672 | case InitializedEntity::EK_Delegating: |
| 4673 | case InitializedEntity::EK_VectorElement: |
| 4674 | case InitializedEntity::EK_ComplexElement: |
| 4675 | case InitializedEntity::EK_BlockElement: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4676 | case InitializedEntity::EK_CompoundLiteralInit: |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4677 | return Initializer->getLocStart(); |
| 4678 | } |
| 4679 | llvm_unreachable("missed an InitializedEntity kind?"); |
| 4680 | } |
| 4681 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4682 | /// \brief Make a (potentially elidable) temporary copy of the object |
| 4683 | /// provided by the given initializer by calling the appropriate copy |
| 4684 | /// constructor. |
| 4685 | /// |
| 4686 | /// \param S The Sema object used for type-checking. |
| 4687 | /// |
Abramo Bagnara | 63e7d25 | 2011-01-27 19:55:10 +0000 | [diff] [blame] | 4688 | /// \param T The type of the temporary object, which must either be |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4689 | /// the type of the initializer expression or a superclass thereof. |
| 4690 | /// |
James Dennett | 1dfbd92 | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4691 | /// \param Entity The entity being initialized. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4692 | /// |
| 4693 | /// \param CurInit The initializer expression. |
| 4694 | /// |
| 4695 | /// \param IsExtraneousCopy Whether this is an "extraneous" copy that |
| 4696 | /// is permitted in C++03 (but not C++0x) when binding a reference to |
| 4697 | /// an rvalue. |
| 4698 | /// |
| 4699 | /// \returns An expression that copies the initializer expression into |
| 4700 | /// a temporary object, or an error expression if a copy could not be |
| 4701 | /// created. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4702 | static ExprResult CopyObject(Sema &S, |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 4703 | QualType T, |
| 4704 | const InitializedEntity &Entity, |
| 4705 | ExprResult CurInit, |
| 4706 | bool IsExtraneousCopy) { |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4707 | // Determine which class type we're copying to. |
Anders Carlsson | 1b36a2f | 2010-01-24 00:19:41 +0000 | [diff] [blame] | 4708 | Expr *CurInitExpr = (Expr *)CurInit.get(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4709 | CXXRecordDecl *Class = 0; |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4710 | if (const RecordType *Record = T->getAs<RecordType>()) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4711 | Class = cast<CXXRecordDecl>(Record->getDecl()); |
| 4712 | if (!Class) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4713 | return CurInit; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4714 | |
Douglas Gregor | f5d8f46 | 2011-01-21 18:05:27 +0000 | [diff] [blame] | 4715 | // C++0x [class.copy]p32: |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4716 | // When certain criteria are met, an implementation is allowed to |
| 4717 | // omit the copy/move construction of a class object, even if the |
| 4718 | // copy/move constructor and/or destructor for the object have |
| 4719 | // side effects. [...] |
| 4720 | // - when a temporary class object that has not been bound to a |
| 4721 | // reference (12.2) would be copied/moved to a class object |
| 4722 | // with the same cv-unqualified type, the copy/move operation |
| 4723 | // can be omitted by constructing the temporary object |
| 4724 | // directly into the target of the omitted copy/move |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4725 | // |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 4726 | // Note that the other three bullets are handled elsewhere. Copy |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4727 | // elision for return statements and throw expressions are handled as part |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4728 | // of constructor initialization, while copy elision for exception handlers |
Douglas Gregor | 3c9034c | 2010-05-15 00:13:29 +0000 | [diff] [blame] | 4729 | // is handled by the run-time. |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 4730 | bool Elidable = CurInitExpr->isTemporaryObject(S.Context, Class); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4731 | SourceLocation Loc = getInitializationLoc(Entity, CurInit.get()); |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4732 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4733 | // Make sure that the type we are copying is complete. |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4734 | if (S.RequireCompleteType(Loc, T, diag::err_temp_copy_incomplete)) |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4735 | return CurInit; |
Douglas Gregor | f86fcb3 | 2010-04-24 21:09:25 +0000 | [diff] [blame] | 4736 | |
Douglas Gregor | cc15f01 | 2011-01-21 19:38:21 +0000 | [diff] [blame] | 4737 | // Perform overload resolution using the class's copy/move constructors. |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4738 | // Only consider constructors and constructor templates. Per |
| 4739 | // C++0x [dcl.init]p16, second bullet to class types, this initialization |
| 4740 | // is direct-initialization. |
John McCall | 5769d61 | 2010-02-08 23:07:23 +0000 | [diff] [blame] | 4741 | OverloadCandidateSet CandidateSet(Loc); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4742 | LookupCopyAndMoveConstructors(S, CandidateSet, Class, CurInitExpr); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4743 | |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4744 | bool HadMultipleCandidates = (CandidateSet.size() > 1); |
| 4745 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4746 | OverloadCandidateSet::iterator Best; |
Chandler Carruth | 25ca421 | 2011-02-25 19:41:05 +0000 | [diff] [blame] | 4747 | switch (CandidateSet.BestViableFunction(S, Loc, Best)) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4748 | case OR_Success: |
| 4749 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4750 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4751 | case OR_No_Viable_Function: |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4752 | S.Diag(Loc, IsExtraneousCopy && !S.isSFINAEContext() |
| 4753 | ? diag::ext_rvalue_to_reference_temp_copy_no_viable |
| 4754 | : diag::err_temp_copy_no_viable) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4755 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4756 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4757 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4758 | if (!IsExtraneousCopy || S.isSFINAEContext()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4759 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4760 | return CurInit; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4761 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4762 | case OR_Ambiguous: |
| 4763 | S.Diag(Loc, diag::err_temp_copy_ambiguous) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4764 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4765 | << CurInitExpr->getSourceRange(); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4766 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4767 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4768 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4769 | case OR_Deleted: |
| 4770 | S.Diag(Loc, diag::err_temp_copy_deleted) |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 4771 | << (int)Entity.getKind() << CurInitExpr->getType() |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4772 | << CurInitExpr->getSourceRange(); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4773 | S.NoteDeletedFunction(Best->Function); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4774 | return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4775 | } |
| 4776 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4777 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(Best->Function); |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4778 | SmallVector<Expr*, 8> ConstructorArgs; |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4779 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4780 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 4781 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
Jeffrey Yasskin | 57d12fd | 2010-06-07 15:58:05 +0000 | [diff] [blame] | 4782 | Best->FoundDecl.getAccess(), IsExtraneousCopy); |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4783 | |
| 4784 | if (IsExtraneousCopy) { |
| 4785 | // If this is a totally extraneous copy for C++03 reference |
| 4786 | // binding purposes, just return the original initialization |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4787 | // expression. We don't generate an (elided) copy operation here |
| 4788 | // because doing so would require us to pass down a flag to avoid |
| 4789 | // infinite recursion, where each step adds another extraneous, |
| 4790 | // elidable copy. |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4791 | |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4792 | // Instantiate the default arguments of any extra parameters in |
| 4793 | // the selected copy constructor, as if we were going to create a |
| 4794 | // proper call to the copy constructor. |
| 4795 | for (unsigned I = 1, N = Constructor->getNumParams(); I != N; ++I) { |
| 4796 | ParmVarDecl *Parm = Constructor->getParamDecl(I); |
| 4797 | if (S.RequireCompleteType(Loc, Parm->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4798 | diag::err_call_incomplete_argument)) |
Douglas Gregor | 2559a70 | 2010-04-18 07:57:34 +0000 | [diff] [blame] | 4799 | break; |
| 4800 | |
| 4801 | // Build the default argument expression; we don't actually care |
| 4802 | // if this succeeds or not, because this routine will complain |
| 4803 | // if there was a problem. |
| 4804 | S.BuildCXXDefaultArgExpr(Loc, Constructor, Parm); |
| 4805 | } |
| 4806 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4807 | return S.Owned(CurInitExpr); |
| 4808 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4809 | |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4810 | // Determine the arguments required to actually perform the |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 4811 | // constructor call (we might have derived-to-base conversions, or |
| 4812 | // the copy constructor may have default arguments). |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 4813 | if (S.CompleteConstructorCall(Constructor, CurInitExpr, Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 4814 | return ExprError(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 4815 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4816 | // Actually perform the constructor call. |
| 4817 | CurInit = S.BuildCXXConstructExpr(Loc, T, Constructor, Elidable, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4818 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 4819 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4820 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 4821 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 4822 | CXXConstructExpr::CK_Complete, |
| 4823 | SourceRange()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 4824 | |
Douglas Gregor | b86cf0c | 2010-04-25 00:55:24 +0000 | [diff] [blame] | 4825 | // If we're supposed to bind temporaries, do so. |
| 4826 | if (!CurInit.isInvalid() && shouldBindAsTemporary(Entity)) |
| 4827 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4828 | return CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 4829 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 4830 | |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4831 | /// \brief Check whether elidable copy construction for binding a reference to |
| 4832 | /// a temporary would have succeeded if we were building in C++98 mode, for |
| 4833 | /// -Wc++98-compat. |
| 4834 | static void CheckCXX98CompatAccessibleCopy(Sema &S, |
| 4835 | const InitializedEntity &Entity, |
| 4836 | Expr *CurInitExpr) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4837 | assert(S.getLangOpts().CPlusPlus11); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4838 | |
| 4839 | const RecordType *Record = CurInitExpr->getType()->getAs<RecordType>(); |
| 4840 | if (!Record) |
| 4841 | return; |
| 4842 | |
| 4843 | SourceLocation Loc = getInitializationLoc(Entity, CurInitExpr); |
| 4844 | if (S.Diags.getDiagnosticLevel(diag::warn_cxx98_compat_temp_copy, Loc) |
| 4845 | == DiagnosticsEngine::Ignored) |
| 4846 | return; |
| 4847 | |
| 4848 | // Find constructors which would have been considered. |
| 4849 | OverloadCandidateSet CandidateSet(Loc); |
| 4850 | LookupCopyAndMoveConstructors( |
| 4851 | S, CandidateSet, cast<CXXRecordDecl>(Record->getDecl()), CurInitExpr); |
| 4852 | |
| 4853 | // Perform overload resolution. |
| 4854 | OverloadCandidateSet::iterator Best; |
| 4855 | OverloadingResult OR = CandidateSet.BestViableFunction(S, Loc, Best); |
| 4856 | |
| 4857 | PartialDiagnostic Diag = S.PDiag(diag::warn_cxx98_compat_temp_copy) |
| 4858 | << OR << (int)Entity.getKind() << CurInitExpr->getType() |
| 4859 | << CurInitExpr->getSourceRange(); |
| 4860 | |
| 4861 | switch (OR) { |
| 4862 | case OR_Success: |
| 4863 | S.CheckConstructorAccess(Loc, cast<CXXConstructorDecl>(Best->Function), |
John McCall | b9abd872 | 2012-04-07 03:04:20 +0000 | [diff] [blame] | 4864 | Entity, Best->FoundDecl.getAccess(), Diag); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4865 | // FIXME: Check default arguments as far as that's possible. |
| 4866 | break; |
| 4867 | |
| 4868 | case OR_No_Viable_Function: |
| 4869 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4870 | CandidateSet.NoteCandidates(S, OCD_AllCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4871 | break; |
| 4872 | |
| 4873 | case OR_Ambiguous: |
| 4874 | S.Diag(Loc, Diag); |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 4875 | CandidateSet.NoteCandidates(S, OCD_ViableCandidates, CurInitExpr); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4876 | break; |
| 4877 | |
| 4878 | case OR_Deleted: |
| 4879 | S.Diag(Loc, Diag); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 4880 | S.NoteDeletedFunction(Best->Function); |
Richard Smith | 83da2e7 | 2011-10-19 16:55:56 +0000 | [diff] [blame] | 4881 | break; |
| 4882 | } |
| 4883 | } |
| 4884 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 4885 | void InitializationSequence::PrintInitLocationNote(Sema &S, |
| 4886 | const InitializedEntity &Entity) { |
| 4887 | if (Entity.getKind() == InitializedEntity::EK_Parameter && Entity.getDecl()) { |
| 4888 | if (Entity.getDecl()->getLocation().isInvalid()) |
| 4889 | return; |
| 4890 | |
| 4891 | if (Entity.getDecl()->getDeclName()) |
| 4892 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_named_here) |
| 4893 | << Entity.getDecl()->getDeclName(); |
| 4894 | else |
| 4895 | S.Diag(Entity.getDecl()->getLocation(), diag::note_parameter_here); |
| 4896 | } |
| 4897 | } |
| 4898 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 4899 | static bool isReferenceBinding(const InitializationSequence::Step &s) { |
| 4900 | return s.Kind == InitializationSequence::SK_BindReference || |
| 4901 | s.Kind == InitializationSequence::SK_BindReferenceToTemporary; |
| 4902 | } |
| 4903 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4904 | /// Returns true if the parameters describe a constructor initialization of |
| 4905 | /// an explicit temporary object, e.g. "Point(x, y)". |
| 4906 | static bool isExplicitTemporary(const InitializedEntity &Entity, |
| 4907 | const InitializationKind &Kind, |
| 4908 | unsigned NumArgs) { |
| 4909 | switch (Entity.getKind()) { |
| 4910 | case InitializedEntity::EK_Temporary: |
| 4911 | case InitializedEntity::EK_CompoundLiteralInit: |
| 4912 | break; |
| 4913 | default: |
| 4914 | return false; |
| 4915 | } |
| 4916 | |
| 4917 | switch (Kind.getKind()) { |
| 4918 | case InitializationKind::IK_DirectList: |
| 4919 | return true; |
| 4920 | // FIXME: Hack to work around cast weirdness. |
| 4921 | case InitializationKind::IK_Direct: |
| 4922 | case InitializationKind::IK_Value: |
| 4923 | return NumArgs != 1; |
| 4924 | default: |
| 4925 | return false; |
| 4926 | } |
| 4927 | } |
| 4928 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4929 | static ExprResult |
| 4930 | PerformConstructorInitialization(Sema &S, |
| 4931 | const InitializedEntity &Entity, |
| 4932 | const InitializationKind &Kind, |
| 4933 | MultiExprArg Args, |
| 4934 | const InitializationSequence::Step& Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4935 | bool &ConstructorInitRequiresZeroInit, |
| 4936 | bool IsListInitialization) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4937 | unsigned NumArgs = Args.size(); |
| 4938 | CXXConstructorDecl *Constructor |
| 4939 | = cast<CXXConstructorDecl>(Step.Function.Function); |
| 4940 | bool HadMultipleCandidates = Step.Function.HadMultipleCandidates; |
| 4941 | |
| 4942 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 4943 | SmallVector<Expr*, 8> ConstructorArgs; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4944 | SourceLocation Loc = (Kind.isCopyInit() && Kind.getEqualLoc().isValid()) |
| 4945 | ? Kind.getEqualLoc() |
| 4946 | : Kind.getLocation(); |
| 4947 | |
| 4948 | if (Kind.getKind() == InitializationKind::IK_Default) { |
| 4949 | // Force even a trivial, implicit default constructor to be |
| 4950 | // semantically checked. We do this explicitly because we don't build |
| 4951 | // the definition for completely trivial constructors. |
Matt Beaumont-Gay | 28e4702 | 2012-02-24 08:37:56 +0000 | [diff] [blame] | 4952 | assert(Constructor->getParent() && "No parent class for constructor."); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4953 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor() && |
Douglas Gregor | 5d86f61 | 2012-02-24 07:48:37 +0000 | [diff] [blame] | 4954 | Constructor->isTrivial() && !Constructor->isUsed(false)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4955 | S.DefineImplicitDefaultConstructor(Loc, Constructor); |
| 4956 | } |
| 4957 | |
| 4958 | ExprResult CurInit = S.Owned((Expr *)0); |
| 4959 | |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4960 | // C++ [over.match.copy]p1: |
| 4961 | // - When initializing a temporary to be bound to the first parameter |
| 4962 | // of a constructor that takes a reference to possibly cv-qualified |
| 4963 | // T as its first argument, called with a single argument in the |
| 4964 | // context of direct-initialization, explicit conversion functions |
| 4965 | // are also considered. |
| 4966 | bool AllowExplicitConv = Kind.AllowExplicit() && !Kind.isCopyInit() && |
| 4967 | Args.size() == 1 && |
| 4968 | Constructor->isCopyOrMoveConstructor(); |
| 4969 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4970 | // Determine the arguments required to actually perform the constructor |
| 4971 | // call. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4972 | if (S.CompleteConstructorCall(Constructor, Args, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 4973 | Loc, ConstructorArgs, |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 4974 | AllowExplicitConv, |
| 4975 | IsListInitialization)) |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4976 | return ExprError(); |
| 4977 | |
| 4978 | |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 4979 | if (isExplicitTemporary(Entity, Kind, NumArgs)) { |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4980 | // An explicitly-constructed temporary, e.g., X(1, 2). |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 4981 | S.MarkFunctionReferenced(Loc, Constructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 4982 | if (S.DiagnoseUseOfDecl(Constructor, Loc)) |
| 4983 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4984 | |
| 4985 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 4986 | if (!TSInfo) |
| 4987 | TSInfo = S.Context.getTrivialTypeSourceInfo(Entity.getType(), Loc); |
Sebastian Redl | 188158d | 2012-03-08 21:05:45 +0000 | [diff] [blame] | 4988 | SourceRange ParenRange; |
| 4989 | if (Kind.getKind() != InitializationKind::IK_DirectList) |
| 4990 | ParenRange = Kind.getParenRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4991 | |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 4992 | CurInit = S.Owned( |
| 4993 | new (S.Context) CXXTemporaryObjectExpr(S.Context, Constructor, |
| 4994 | TSInfo, ConstructorArgs, |
| 4995 | ParenRange, IsListInitialization, |
| 4996 | HadMultipleCandidates, |
| 4997 | ConstructorInitRequiresZeroInit)); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 4998 | } else { |
| 4999 | CXXConstructExpr::ConstructionKind ConstructKind = |
| 5000 | CXXConstructExpr::CK_Complete; |
| 5001 | |
| 5002 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 5003 | ConstructKind = Entity.getBaseSpecifier()->isVirtual() ? |
| 5004 | CXXConstructExpr::CK_VirtualBase : |
| 5005 | CXXConstructExpr::CK_NonVirtualBase; |
| 5006 | } else if (Entity.getKind() == InitializedEntity::EK_Delegating) { |
| 5007 | ConstructKind = CXXConstructExpr::CK_Delegating; |
| 5008 | } |
| 5009 | |
| 5010 | // Only get the parenthesis range if it is a direct construction. |
| 5011 | SourceRange parenRange = |
| 5012 | Kind.getKind() == InitializationKind::IK_Direct ? |
| 5013 | Kind.getParenRange() : SourceRange(); |
| 5014 | |
| 5015 | // If the entity allows NRVO, mark the construction as elidable |
| 5016 | // unconditionally. |
| 5017 | if (Entity.allowsNRVO()) |
| 5018 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5019 | Constructor, /*Elidable=*/true, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5020 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5021 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5022 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5023 | ConstructorInitRequiresZeroInit, |
| 5024 | ConstructKind, |
| 5025 | parenRange); |
| 5026 | else |
| 5027 | CurInit = S.BuildCXXConstructExpr(Loc, Entity.getType(), |
| 5028 | Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5029 | ConstructorArgs, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5030 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5031 | IsListInitialization, |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5032 | ConstructorInitRequiresZeroInit, |
| 5033 | ConstructKind, |
| 5034 | parenRange); |
| 5035 | } |
| 5036 | if (CurInit.isInvalid()) |
| 5037 | return ExprError(); |
| 5038 | |
| 5039 | // Only check access if all of that succeeded. |
| 5040 | S.CheckConstructorAccess(Loc, Constructor, Entity, |
| 5041 | Step.Function.FoundDecl.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5042 | if (S.DiagnoseUseOfDecl(Step.Function.FoundDecl, Loc)) |
| 5043 | return ExprError(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5044 | |
| 5045 | if (shouldBindAsTemporary(Entity)) |
| 5046 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
| 5047 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5048 | return CurInit; |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5051 | /// Determine whether the specified InitializedEntity definitely has a lifetime |
| 5052 | /// longer than the current full-expression. Conservatively returns false if |
| 5053 | /// it's unclear. |
| 5054 | static bool |
| 5055 | InitializedEntityOutlivesFullExpression(const InitializedEntity &Entity) { |
| 5056 | const InitializedEntity *Top = &Entity; |
| 5057 | while (Top->getParent()) |
| 5058 | Top = Top->getParent(); |
| 5059 | |
| 5060 | switch (Top->getKind()) { |
| 5061 | case InitializedEntity::EK_Variable: |
| 5062 | case InitializedEntity::EK_Result: |
| 5063 | case InitializedEntity::EK_Exception: |
| 5064 | case InitializedEntity::EK_Member: |
| 5065 | case InitializedEntity::EK_New: |
| 5066 | case InitializedEntity::EK_Base: |
| 5067 | case InitializedEntity::EK_Delegating: |
| 5068 | return true; |
| 5069 | |
| 5070 | case InitializedEntity::EK_ArrayElement: |
| 5071 | case InitializedEntity::EK_VectorElement: |
| 5072 | case InitializedEntity::EK_BlockElement: |
| 5073 | case InitializedEntity::EK_ComplexElement: |
| 5074 | // Could not determine what the full initialization is. Assume it might not |
| 5075 | // outlive the full-expression. |
| 5076 | return false; |
| 5077 | |
| 5078 | case InitializedEntity::EK_Parameter: |
| 5079 | case InitializedEntity::EK_Temporary: |
| 5080 | case InitializedEntity::EK_LambdaCapture: |
Jordan Rose | 2624b81 | 2013-05-06 16:48:12 +0000 | [diff] [blame] | 5081 | case InitializedEntity::EK_CompoundLiteralInit: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5082 | // The entity being initialized might not outlive the full-expression. |
| 5083 | return false; |
| 5084 | } |
| 5085 | |
| 5086 | llvm_unreachable("unknown entity kind"); |
| 5087 | } |
| 5088 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5089 | ExprResult |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5090 | InitializationSequence::Perform(Sema &S, |
| 5091 | const InitializedEntity &Entity, |
| 5092 | const InitializationKind &Kind, |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5093 | MultiExprArg Args, |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5094 | QualType *ResultType) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5095 | if (Failed()) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5096 | Diagnose(S, Entity, Kind, Args); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5097 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5098 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5099 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5100 | if (getKind() == DependentSequence) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5101 | // If the declaration is a non-dependent, incomplete array type |
| 5102 | // that has an initializer, then its type will be completed once |
| 5103 | // the initializer is instantiated. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5104 | if (ResultType && !Entity.getType()->isDependentType() && |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5105 | Args.size() == 1) { |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5106 | QualType DeclType = Entity.getType(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5107 | if (const IncompleteArrayType *ArrayT |
| 5108 | = S.Context.getAsIncompleteArrayType(DeclType)) { |
| 5109 | // FIXME: We don't currently have the ability to accurately |
| 5110 | // compute the length of an initializer list without |
| 5111 | // performing full type-checking of the initializer list |
| 5112 | // (since we have to determine where braces are implicitly |
| 5113 | // introduced and such). So, we fall back to making the array |
| 5114 | // type a dependently-sized array type with no specified |
| 5115 | // bound. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5116 | if (isa<InitListExpr>((Expr *)Args[0])) { |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5117 | SourceRange Brackets; |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5118 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5119 | // Scavange the location of the brackets from the entity, if we can. |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5120 | if (DeclaratorDecl *DD = Entity.getDecl()) { |
| 5121 | if (TypeSourceInfo *TInfo = DD->getTypeSourceInfo()) { |
| 5122 | TypeLoc TL = TInfo->getTypeLoc(); |
David Blaikie | 39e6ab4 | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 5123 | if (IncompleteArrayTypeLoc ArrayLoc = |
| 5124 | TL.getAs<IncompleteArrayTypeLoc>()) |
| 5125 | Brackets = ArrayLoc.getBracketsRange(); |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5126 | } |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | *ResultType |
| 5130 | = S.Context.getDependentSizedArrayType(ArrayT->getElementType(), |
| 5131 | /*NumElts=*/0, |
| 5132 | ArrayT->getSizeModifier(), |
| 5133 | ArrayT->getIndexTypeCVRQualifiers(), |
| 5134 | Brackets); |
| 5135 | } |
| 5136 | |
| 5137 | } |
| 5138 | } |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5139 | if (Kind.getKind() == InitializationKind::IK_Direct && |
| 5140 | !Kind.isExplicitCast()) { |
| 5141 | // Rebuild the ParenListExpr. |
| 5142 | SourceRange ParenRange = Kind.getParenRange(); |
| 5143 | return S.ActOnParenListExpr(ParenRange.getBegin(), ParenRange.getEnd(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5144 | Args); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 5145 | } |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 5146 | assert(Kind.getKind() == InitializationKind::IK_Copy || |
Douglas Gregor | a9b55a4 | 2012-04-04 04:06:51 +0000 | [diff] [blame] | 5147 | Kind.isExplicitCast() || |
| 5148 | Kind.getKind() == InitializationKind::IK_DirectList); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5149 | return ExprResult(Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5150 | } |
| 5151 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 5152 | // No steps means no initialization. |
| 5153 | if (Steps.empty()) |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5154 | return S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5155 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5156 | if (S.getLangOpts().CPlusPlus11 && Entity.getType()->isReferenceType() && |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5157 | Args.size() == 1 && isa<InitListExpr>(Args[0]) && |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5158 | Entity.getKind() != InitializedEntity::EK_Parameter) { |
| 5159 | // Produce a C++98 compatibility warning if we are initializing a reference |
| 5160 | // from an initializer list. For parameters, we produce a better warning |
| 5161 | // elsewhere. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5162 | Expr *Init = Args[0]; |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5163 | S.Diag(Init->getLocStart(), diag::warn_cxx98_compat_reference_list_init) |
| 5164 | << Init->getSourceRange(); |
| 5165 | } |
| 5166 | |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5167 | // Diagnose cases where we initialize a pointer to an array temporary, and the |
| 5168 | // pointer obviously outlives the temporary. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5169 | if (Args.size() == 1 && Args[0]->getType()->isArrayType() && |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5170 | Entity.getType()->isPointerType() && |
| 5171 | InitializedEntityOutlivesFullExpression(Entity)) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5172 | Expr *Init = Args[0]; |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 5173 | Expr::LValueClassification Kind = Init->ClassifyLValue(S.Context); |
| 5174 | if (Kind == Expr::LV_ClassTemporary || Kind == Expr::LV_ArrayTemporary) |
| 5175 | S.Diag(Init->getLocStart(), diag::warn_temporary_array_to_pointer_decay) |
| 5176 | << Init->getSourceRange(); |
| 5177 | } |
| 5178 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5179 | QualType DestType = Entity.getType().getNonReferenceType(); |
| 5180 | // FIXME: Ugly hack around the fact that Entity.getType() is not |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5181 | // the same as Entity.getDecl()->getType() in cases involving type merging, |
| 5182 | // and we want latter when it makes sense. |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5183 | if (ResultType) |
Eli Friedman | a91eb54 | 2009-12-22 02:10:53 +0000 | [diff] [blame] | 5184 | *ResultType = Entity.getDecl() ? Entity.getDecl()->getType() : |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5185 | Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5186 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5187 | ExprResult CurInit = S.Owned((Expr *)0); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5188 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5189 | // For initialization steps that start with a single initializer, |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5190 | // grab the only argument out the Args and place it into the "current" |
| 5191 | // initializer. |
| 5192 | switch (Steps.front().Kind) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5193 | case SK_ResolveAddressOfOverloadedFunction: |
| 5194 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5195 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5196 | case SK_CastDerivedToBaseLValue: |
| 5197 | case SK_BindReference: |
| 5198 | case SK_BindReferenceToTemporary: |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5199 | case SK_ExtraneousCopyToTemporary: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5200 | case SK_UserConversion: |
| 5201 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5202 | case SK_QualificationConversionXValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5203 | case SK_QualificationConversionRValue: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5204 | case SK_LValueToRValue: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5205 | case SK_ConversionSequence: |
| 5206 | case SK_ListInitialization: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5207 | case SK_UnwrapInitList: |
| 5208 | case SK_RewrapInitList: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5209 | case SK_CAssignment: |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5210 | case SK_StringInit: |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5211 | case SK_ObjCObjectConversion: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5212 | case SK_ArrayInit: |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5213 | case SK_ParenthesizedArrayInit: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5214 | case SK_PassByIndirectCopyRestore: |
| 5215 | case SK_PassByIndirectRestore: |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5216 | case SK_ProduceObjCObject: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5217 | case SK_StdInitializerList: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5218 | case SK_OCLSamplerInit: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5219 | case SK_OCLZeroEvent: { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5220 | assert(Args.size() == 1); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5221 | CurInit = Args[0]; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5222 | if (!CurInit.get()) return ExprError(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5223 | break; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5224 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5225 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5226 | case SK_ConstructorInitialization: |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5227 | case SK_ListConstructorCall: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5228 | case SK_ZeroInitialization: |
| 5229 | break; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5230 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5231 | |
| 5232 | // Walk through the computed steps for the initialization sequence, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5233 | // performing the specified conversions along the way. |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5234 | bool ConstructorInitRequiresZeroInit = false; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5235 | for (step_iterator Step = step_begin(), StepEnd = step_end(); |
| 5236 | Step != StepEnd; ++Step) { |
| 5237 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5238 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5239 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5240 | QualType SourceType = CurInit.get() ? CurInit.get()->getType() : QualType(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5241 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5242 | switch (Step->Kind) { |
| 5243 | case SK_ResolveAddressOfOverloadedFunction: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5244 | // Overload resolution determined which function invoke; update the |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5245 | // initializer to reflect that choice. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5246 | S.CheckAddressOfMemberAccess(CurInit.get(), Step->Function.FoundDecl); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5247 | if (S.DiagnoseUseOfDecl(Step->Function.FoundDecl, Kind.getLocation())) |
| 5248 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5249 | CurInit = S.FixOverloadedFunctionReference(CurInit, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5250 | Step->Function.FoundDecl, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5251 | Step->Function.Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5252 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5253 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5254 | case SK_CastDerivedToBaseRValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5255 | case SK_CastDerivedToBaseXValue: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5256 | case SK_CastDerivedToBaseLValue: { |
| 5257 | // We have a derived-to-base cast that produces either an rvalue or an |
| 5258 | // lvalue. Perform that cast. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5259 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5260 | CXXCastPath BasePath; |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5261 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5262 | // Casts to inaccessible base classes are allowed with C-style casts. |
| 5263 | bool IgnoreBaseAccess = Kind.isCStyleOrFunctionalCast(); |
| 5264 | if (S.CheckDerivedToBaseConversion(SourceType, Step->Type, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5265 | CurInit.get()->getLocStart(), |
| 5266 | CurInit.get()->getSourceRange(), |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5267 | &BasePath, IgnoreBaseAccess)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5268 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5269 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5270 | if (S.BasePathInvolvesVirtualBase(BasePath)) { |
| 5271 | QualType T = SourceType; |
| 5272 | if (const PointerType *Pointer = T->getAs<PointerType>()) |
| 5273 | T = Pointer->getPointeeType(); |
| 5274 | if (const RecordType *RecordTy = T->getAs<RecordType>()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5275 | S.MarkVTableUsed(CurInit.get()->getLocStart(), |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 5276 | cast<CXXRecordDecl>(RecordTy->getDecl())); |
| 5277 | } |
| 5278 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5279 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5280 | Step->Kind == SK_CastDerivedToBaseLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5281 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5282 | (Step->Kind == SK_CastDerivedToBaseXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5283 | VK_XValue : |
| 5284 | VK_RValue); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5285 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
| 5286 | Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5287 | CK_DerivedToBase, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5288 | CurInit.get(), |
| 5289 | &BasePath, VK)); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5290 | break; |
| 5291 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5292 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5293 | case SK_BindReference: |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5294 | // References cannot bind to bit-fields (C++ [dcl.init.ref]p5). |
| 5295 | if (CurInit.get()->refersToBitField()) { |
| 5296 | // We don't necessarily have an unambiguous source bit-field. |
| 5297 | FieldDecl *BitField = CurInit.get()->getSourceBitField(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5298 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_bitfield) |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5299 | << Entity.getType().isVolatileQualified() |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5300 | << (BitField ? BitField->getDeclName() : DeclarationName()) |
| 5301 | << (BitField != NULL) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5302 | << CurInit.get()->getSourceRange(); |
John McCall | 993f43f | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 5303 | if (BitField) |
| 5304 | S.Diag(BitField->getLocation(), diag::note_bitfield_decl); |
| 5305 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5306 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5307 | } |
Anders Carlsson | a6fe0bf | 2010-01-29 02:47:33 +0000 | [diff] [blame] | 5308 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5309 | if (CurInit.get()->refersToVectorElement()) { |
John McCall | 41593e3 | 2010-02-02 19:02:38 +0000 | [diff] [blame] | 5310 | // References cannot bind to vector elements. |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5311 | S.Diag(Kind.getLocation(), diag::err_reference_bind_to_vector_element) |
| 5312 | << Entity.getType().isVolatileQualified() |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5313 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5314 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5315 | return ExprError(); |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 5316 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5317 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5318 | // Reference binding does not have any corresponding ASTs. |
| 5319 | |
| 5320 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5321 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5322 | return ExprError(); |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5323 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5324 | break; |
Anders Carlsson | 3aba093 | 2010-01-31 18:34:51 +0000 | [diff] [blame] | 5325 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5326 | case SK_BindReferenceToTemporary: |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5327 | // Make sure the "temporary" is actually an rvalue. |
| 5328 | assert(CurInit.get()->isRValue() && "not a temporary"); |
| 5329 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5330 | // Check exception specifications |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5331 | if (S.CheckExceptionSpecCompatibility(CurInit.get(), DestType)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5332 | return ExprError(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5333 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5334 | // Materialize the temporary into memory. |
Douglas Gregor | b4b7b50 | 2011-06-22 15:05:02 +0000 | [diff] [blame] | 5335 | CurInit = new (S.Context) MaterializeTemporaryExpr( |
| 5336 | Entity.getType().getNonReferenceType(), |
| 5337 | CurInit.get(), |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 5338 | Entity.getType()->isLValueReferenceType()); |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5339 | |
| 5340 | // If we're binding to an Objective-C object that has lifetime, we |
| 5341 | // need cleanups. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5342 | if (S.getLangOpts().ObjCAutoRefCount && |
Douglas Gregor | d7b2316 | 2011-06-22 16:12:01 +0000 | [diff] [blame] | 5343 | CurInit.get()->getType()->isObjCLifetimeType()) |
| 5344 | S.ExprNeedsCleanups = true; |
| 5345 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5346 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5347 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5348 | case SK_ExtraneousCopyToTemporary: |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5349 | CurInit = CopyObject(S, Step->Type, Entity, CurInit, |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5350 | /*IsExtraneousCopy=*/true); |
| 5351 | break; |
| 5352 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5353 | case SK_UserConversion: { |
| 5354 | // We have a user-defined conversion that invokes either a constructor |
| 5355 | // or a conversion function. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5356 | CastKind CastKind; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5357 | bool IsCopy = false; |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5358 | FunctionDecl *Fn = Step->Function.Function; |
| 5359 | DeclAccessPair FoundFn = Step->Function.FoundDecl; |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5360 | bool HadMultipleCandidates = Step->Function.HadMultipleCandidates; |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5361 | bool CreatedObject = false; |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5362 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Fn)) { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5363 | // Build a call to the selected constructor. |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 5364 | SmallVector<Expr*, 8> ConstructorArgs; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5365 | SourceLocation Loc = CurInit.get()->getLocStart(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5366 | CurInit.release(); // Ownership transferred into MultiExprArg, below. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5367 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5368 | // Determine the arguments required to actually perform the constructor |
| 5369 | // call. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5370 | Expr *Arg = CurInit.get(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5371 | if (S.CompleteConstructorCall(Constructor, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5372 | MultiExprArg(&Arg, 1), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5373 | Loc, ConstructorArgs)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5374 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5375 | |
Richard Smith | f2e4dfc | 2012-02-11 19:22:50 +0000 | [diff] [blame] | 5376 | // Build an expression that constructs a temporary. |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5377 | CurInit = S.BuildCXXConstructExpr(Loc, Step->Type, Constructor, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5378 | ConstructorArgs, |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5379 | HadMultipleCandidates, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5380 | /*ListInit*/ false, |
John McCall | 7a1fad3 | 2010-08-24 07:32:53 +0000 | [diff] [blame] | 5381 | /*ZeroInit*/ false, |
Chandler Carruth | 428edaf | 2010-10-25 08:47:36 +0000 | [diff] [blame] | 5382 | CXXConstructExpr::CK_Complete, |
| 5383 | SourceRange()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5384 | if (CurInit.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5385 | return ExprError(); |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5386 | |
Anders Carlsson | 9a68a67 | 2010-04-21 18:47:17 +0000 | [diff] [blame] | 5387 | S.CheckConstructorAccess(Kind.getLocation(), Constructor, Entity, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5388 | FoundFn.getAccess()); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5389 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5390 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5391 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5392 | CastKind = CK_ConstructorConversion; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5393 | QualType Class = S.Context.getTypeDeclType(Constructor->getParent()); |
| 5394 | if (S.Context.hasSameUnqualifiedType(SourceType, Class) || |
| 5395 | S.IsDerivedFrom(SourceType, Class)) |
| 5396 | IsCopy = true; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5397 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5398 | CreatedObject = true; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5399 | } else { |
| 5400 | // Build a call to the conversion function. |
John McCall | b13b737 | 2010-02-01 03:16:54 +0000 | [diff] [blame] | 5401 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(Fn); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5402 | S.CheckMemberOperatorAccess(Kind.getLocation(), CurInit.get(), 0, |
John McCall | 9aa472c | 2010-03-19 07:35:19 +0000 | [diff] [blame] | 5403 | FoundFn); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5404 | if (S.DiagnoseUseOfDecl(FoundFn, Kind.getLocation())) |
| 5405 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5406 | |
| 5407 | // FIXME: Should we move this initialization into a separate |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5408 | // derived-to-base conversion? I believe the answer is "no", because |
| 5409 | // 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] | 5410 | ExprResult CurInitExprRes = |
| 5411 | S.PerformObjectArgumentInitialization(CurInit.take(), /*Qualifier=*/0, |
| 5412 | FoundFn, Conversion); |
| 5413 | if(CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5414 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5415 | CurInit = CurInitExprRes; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5416 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5417 | // Build the actual call to the conversion function. |
Abramo Bagnara | 7cc58b4 | 2011-10-05 07:56:41 +0000 | [diff] [blame] | 5418 | CurInit = S.BuildCXXMemberCallExpr(CurInit.get(), FoundFn, Conversion, |
| 5419 | HadMultipleCandidates); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5420 | if (CurInit.isInvalid() || !CurInit.get()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5421 | return ExprError(); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5422 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5423 | CastKind = CK_UserDefinedConversion; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5424 | |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5425 | CreatedObject = Conversion->getResultType()->isRecordType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5426 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5427 | |
Sebastian Redl | 3b80232 | 2011-07-14 19:07:55 +0000 | [diff] [blame] | 5428 | bool RequiresCopy = !IsCopy && !isReferenceBinding(Steps.back()); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5429 | bool MaybeBindToTemp = RequiresCopy || shouldBindAsTemporary(Entity); |
| 5430 | |
| 5431 | if (!MaybeBindToTemp && CreatedObject && shouldDestroyTemporary(Entity)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5432 | QualType T = CurInit.get()->getType(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5433 | if (const RecordType *Record = T->getAs<RecordType>()) { |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5434 | CXXDestructorDecl *Destructor |
Douglas Gregor | db89f28 | 2010-07-01 22:47:18 +0000 | [diff] [blame] | 5435 | = S.LookupDestructor(cast<CXXRecordDecl>(Record->getDecl())); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5436 | S.CheckDestructorAccess(CurInit.get()->getLocStart(), Destructor, |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5437 | S.PDiag(diag::err_access_dtor_temp) << T); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 5438 | S.MarkFunctionReferenced(CurInit.get()->getLocStart(), Destructor); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5439 | if (S.DiagnoseUseOfDecl(Destructor, CurInit.get()->getLocStart())) |
| 5440 | return ExprError(); |
Douglas Gregor | 4154e0b | 2010-04-24 23:45:46 +0000 | [diff] [blame] | 5441 | } |
| 5442 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5443 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5444 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5445 | CurInit.get()->getType(), |
| 5446 | CastKind, CurInit.get(), 0, |
Eli Friedman | 104be6f | 2011-09-27 01:11:35 +0000 | [diff] [blame] | 5447 | CurInit.get()->getValueKind())); |
Abramo Bagnara | 960809e | 2011-11-16 22:46:05 +0000 | [diff] [blame] | 5448 | if (MaybeBindToTemp) |
| 5449 | CurInit = S.MaybeBindToTemporary(CurInit.takeAs<Expr>()); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 5450 | if (RequiresCopy) |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 5451 | CurInit = CopyObject(S, Entity.getType().getNonReferenceType(), Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5452 | CurInit, /*IsExtraneousCopy=*/false); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5453 | break; |
| 5454 | } |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5455 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5456 | case SK_QualificationConversionLValue: |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5457 | case SK_QualificationConversionXValue: |
| 5458 | case SK_QualificationConversionRValue: { |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5459 | // Perform a qualification conversion; these can never go wrong. |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5460 | ExprValueKind VK = |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5461 | Step->Kind == SK_QualificationConversionLValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5462 | VK_LValue : |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5463 | (Step->Kind == SK_QualificationConversionXValue ? |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 5464 | VK_XValue : |
| 5465 | VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5466 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, CK_NoOp, VK); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5467 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 5468 | } |
| 5469 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 5470 | case SK_LValueToRValue: { |
| 5471 | assert(CurInit.get()->isGLValue() && "cannot load from a prvalue"); |
| 5472 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
| 5473 | CK_LValueToRValue, |
| 5474 | CurInit.take(), |
| 5475 | /*BasePath=*/0, |
| 5476 | VK_RValue)); |
| 5477 | break; |
| 5478 | } |
| 5479 | |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5480 | case SK_ConversionSequence: { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5481 | Sema::CheckedConversionKind CCK |
| 5482 | = Kind.isCStyleCast()? Sema::CCK_CStyleCast |
| 5483 | : Kind.isFunctionalCast()? Sema::CCK_FunctionalCast |
Richard Smith | c8d7f58 | 2011-11-29 22:48:16 +0000 | [diff] [blame] | 5484 | : Kind.isExplicitCast()? Sema::CCK_OtherCast |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5485 | : Sema::CCK_ImplicitConversion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5486 | ExprResult CurInitExprRes = |
| 5487 | S.PerformImplicitConversion(CurInit.get(), Step->Type, *Step->ICS, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5488 | getAssignmentAction(Entity), CCK); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5489 | if (CurInitExprRes.isInvalid()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5490 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5491 | CurInit = CurInitExprRes; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5492 | break; |
Douglas Gregor | f0e43e5 | 2010-04-16 19:30:02 +0000 | [diff] [blame] | 5493 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5494 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5495 | case SK_ListInitialization: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5496 | InitListExpr *InitList = cast<InitListExpr>(CurInit.get()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5497 | // Hack: We must pass *ResultType if available in order to set the type |
| 5498 | // of arrays, e.g. in 'int ar[] = {1, 2, 3};'. |
| 5499 | // But in 'const X &x = {1, 2, 3};' we're supposed to initialize a |
| 5500 | // temporary, not a reference, so we should pass Ty. |
| 5501 | // Worst case: 'const int (&arref)[] = {1, 2, 3};'. |
| 5502 | // Since this step is never used for a reference directly, we explicitly |
| 5503 | // unwrap references here and rewrap them afterwards. |
| 5504 | // We also need to create a InitializeTemporary entity for this. |
| 5505 | QualType Ty = ResultType ? ResultType->getNonReferenceType() : Step->Type; |
Sebastian Redl | cbf8209 | 2012-03-07 16:10:45 +0000 | [diff] [blame] | 5506 | bool IsTemporary = Entity.getType()->isReferenceType(); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5507 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(Ty); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5508 | InitializedEntity InitEntity = IsTemporary ? TempEntity : Entity; |
| 5509 | InitListChecker PerformInitList(S, InitEntity, |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5510 | InitList, Ty, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5511 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5512 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5513 | if (PerformInitList.HadError()) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5514 | return ExprError(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5515 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5516 | if (ResultType) { |
| 5517 | if ((*ResultType)->isRValueReferenceType()) |
| 5518 | Ty = S.Context.getRValueReferenceType(Ty); |
| 5519 | else if ((*ResultType)->isLValueReferenceType()) |
| 5520 | Ty = S.Context.getLValueReferenceType(Ty, |
| 5521 | (*ResultType)->getAs<LValueReferenceType>()->isSpelledAsLValue()); |
| 5522 | *ResultType = Ty; |
| 5523 | } |
| 5524 | |
| 5525 | InitListExpr *StructuredInitList = |
| 5526 | PerformInitList.getFullyStructuredList(); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5527 | CurInit.release(); |
Richard Smith | 802e226 | 2013-02-02 01:13:06 +0000 | [diff] [blame] | 5528 | CurInit = shouldBindAsTemporary(InitEntity) |
| 5529 | ? S.MaybeBindToTemporary(StructuredInitList) |
| 5530 | : S.Owned(StructuredInitList); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 5531 | break; |
| 5532 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5533 | |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5534 | case SK_ListConstructorCall: { |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5535 | // When an initializer list is passed for a parameter of type "reference |
| 5536 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5537 | // EK_Parameter entity with reference type. |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5538 | // FIXME: This is a hack. What we really should do is create a user |
| 5539 | // conversion step for this case, but this makes it considerably more |
| 5540 | // complicated. For now, this will do. |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5541 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5542 | Entity.getType().getNonReferenceType()); |
| 5543 | bool UseTemporary = Entity.getType()->isReferenceType(); |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5544 | assert(Args.size() == 1 && "expected a single argument for list init"); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5545 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5546 | S.Diag(InitList->getExprLoc(), diag::warn_cxx98_compat_ctor_list_init) |
| 5547 | << InitList->getSourceRange(); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5548 | MultiExprArg Arg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 5549 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity : |
| 5550 | Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5551 | Kind, Arg, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5552 | ConstructorInitRequiresZeroInit, |
| 5553 | /*IsListInitialization*/ true); |
Sebastian Redl | 10f04a6 | 2011-12-22 14:44:04 +0000 | [diff] [blame] | 5554 | break; |
| 5555 | } |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 5556 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5557 | case SK_UnwrapInitList: |
| 5558 | CurInit = S.Owned(cast<InitListExpr>(CurInit.take())->getInit(0)); |
| 5559 | break; |
| 5560 | |
| 5561 | case SK_RewrapInitList: { |
| 5562 | Expr *E = CurInit.take(); |
| 5563 | InitListExpr *Syntactic = Step->WrappingSyntacticList; |
| 5564 | InitListExpr *ILE = new (S.Context) InitListExpr(S.Context, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5565 | Syntactic->getLBraceLoc(), E, Syntactic->getRBraceLoc()); |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5566 | ILE->setSyntacticForm(Syntactic); |
| 5567 | ILE->setType(E->getType()); |
| 5568 | ILE->setValueKind(E->getValueKind()); |
| 5569 | CurInit = S.Owned(ILE); |
| 5570 | break; |
| 5571 | } |
| 5572 | |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5573 | case SK_ConstructorInitialization: { |
| 5574 | // When an initializer list is passed for a parameter of type "reference |
| 5575 | // to object", we don't get an EK_Temporary entity, but instead an |
| 5576 | // EK_Parameter entity with reference type. |
| 5577 | // FIXME: This is a hack. What we really should do is create a user |
| 5578 | // conversion step for this case, but this makes it considerably more |
| 5579 | // complicated. For now, this will do. |
| 5580 | InitializedEntity TempEntity = InitializedEntity::InitializeTemporary( |
| 5581 | Entity.getType().getNonReferenceType()); |
| 5582 | bool UseTemporary = Entity.getType()->isReferenceType(); |
| 5583 | CurInit = PerformConstructorInitialization(S, UseTemporary ? TempEntity |
| 5584 | : Entity, |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5585 | Kind, Args, *Step, |
Richard Smith | c83c230 | 2012-12-19 01:39:02 +0000 | [diff] [blame] | 5586 | ConstructorInitRequiresZeroInit, |
| 5587 | /*IsListInitialization*/ false); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 5588 | break; |
Sebastian Redl | bac5cf4 | 2012-02-19 12:27:56 +0000 | [diff] [blame] | 5589 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5590 | |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5591 | case SK_ZeroInitialization: { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5592 | step_iterator NextStep = Step; |
| 5593 | ++NextStep; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5594 | if (NextStep != StepEnd && |
Richard Smith | f4bb8d0 | 2012-07-05 08:39:21 +0000 | [diff] [blame] | 5595 | (NextStep->Kind == SK_ConstructorInitialization || |
| 5596 | NextStep->Kind == SK_ListConstructorCall)) { |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5597 | // The need for zero-initialization is recorded directly into |
| 5598 | // the call to the object's constructor within the next step. |
| 5599 | ConstructorInitRequiresZeroInit = true; |
| 5600 | } else if (Kind.getKind() == InitializationKind::IK_Value && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5601 | S.getLangOpts().CPlusPlus && |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5602 | !Kind.isImplicitValueInit()) { |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5603 | TypeSourceInfo *TSInfo = Entity.getTypeSourceInfo(); |
| 5604 | if (!TSInfo) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5605 | TSInfo = S.Context.getTrivialTypeSourceInfo(Step->Type, |
Douglas Gregor | ab6677e | 2010-09-08 00:15:04 +0000 | [diff] [blame] | 5606 | Kind.getRange().getBegin()); |
| 5607 | |
| 5608 | CurInit = S.Owned(new (S.Context) CXXScalarValueInitExpr( |
| 5609 | TSInfo->getType().getNonLValueExprType(S.Context), |
| 5610 | TSInfo, |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5611 | Kind.getRange().getEnd())); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5612 | } else { |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5613 | CurInit = S.Owned(new (S.Context) ImplicitValueInitExpr(Step->Type)); |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 5614 | } |
Douglas Gregor | 71d1740 | 2009-12-15 00:01:57 +0000 | [diff] [blame] | 5615 | break; |
| 5616 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5617 | |
| 5618 | case SK_CAssignment: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5619 | QualType SourceType = CurInit.get()->getType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5620 | ExprResult Result = CurInit; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5621 | Sema::AssignConvertType ConvTy = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5622 | S.CheckSingleAssignmentConstraints(Step->Type, Result); |
| 5623 | if (Result.isInvalid()) |
| 5624 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5625 | CurInit = Result; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5626 | |
| 5627 | // If this is a call, allow conversion to a transparent union. |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5628 | ExprResult CurInitExprRes = CurInit; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5629 | if (ConvTy != Sema::Compatible && |
| 5630 | Entity.getKind() == InitializedEntity::EK_Parameter && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5631 | S.CheckTransparentUnionArgumentConstraints(Step->Type, CurInitExprRes) |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5632 | == Sema::Compatible) |
| 5633 | ConvTy = Sema::Compatible; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5634 | if (CurInitExprRes.isInvalid()) |
| 5635 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5636 | CurInit = CurInitExprRes; |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 5637 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5638 | bool Complained; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5639 | if (S.DiagnoseAssignmentResult(ConvTy, Kind.getLocation(), |
| 5640 | Step->Type, SourceType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5641 | CurInit.get(), |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5642 | getAssignmentAction(Entity), |
| 5643 | &Complained)) { |
| 5644 | PrintInitLocationNote(S, Entity); |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 5645 | return ExprError(); |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 5646 | } else if (Complained) |
| 5647 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5648 | break; |
| 5649 | } |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5650 | |
| 5651 | case SK_StringInit: { |
| 5652 | QualType Ty = Step->Type; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5653 | CheckStringInit(CurInit.get(), ResultType ? *ResultType : Ty, |
John McCall | fef8b34 | 2011-02-21 07:57:55 +0000 | [diff] [blame] | 5654 | S.Context.getAsArrayType(Ty), S); |
Eli Friedman | cfdc81a | 2009-12-19 08:11:05 +0000 | [diff] [blame] | 5655 | break; |
| 5656 | } |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5657 | |
| 5658 | case SK_ObjCObjectConversion: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5659 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5660 | CK_ObjCObjectLValueCast, |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 5661 | CurInit.get()->getValueKind()); |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 5662 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5663 | |
| 5664 | case SK_ArrayInit: |
| 5665 | // Okay: we checked everything before creating this step. Note that |
| 5666 | // this is a GNU extension. |
| 5667 | S.Diag(Kind.getLocation(), diag::ext_array_init_copy) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5668 | << Step->Type << CurInit.get()->getType() |
| 5669 | << CurInit.get()->getSourceRange(); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5670 | |
| 5671 | // If the destination type is an incomplete array type, update the |
| 5672 | // type accordingly. |
| 5673 | if (ResultType) { |
| 5674 | if (const IncompleteArrayType *IncompleteDest |
| 5675 | = S.Context.getAsIncompleteArrayType(Step->Type)) { |
| 5676 | if (const ConstantArrayType *ConstantSource |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5677 | = S.Context.getAsConstantArrayType(CurInit.get()->getType())) { |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5678 | *ResultType = S.Context.getConstantArrayType( |
| 5679 | IncompleteDest->getElementType(), |
| 5680 | ConstantSource->getSize(), |
| 5681 | ArrayType::Normal, 0); |
| 5682 | } |
| 5683 | } |
| 5684 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5685 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5686 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 5687 | case SK_ParenthesizedArrayInit: |
| 5688 | // Okay: we checked everything before creating this step. Note that |
| 5689 | // this is a GNU extension. |
| 5690 | S.Diag(Kind.getLocation(), diag::ext_array_init_parens) |
| 5691 | << CurInit.get()->getSourceRange(); |
| 5692 | break; |
| 5693 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5694 | case SK_PassByIndirectCopyRestore: |
| 5695 | case SK_PassByIndirectRestore: |
| 5696 | checkIndirectCopyRestoreSource(S, CurInit.get()); |
| 5697 | CurInit = S.Owned(new (S.Context) |
| 5698 | ObjCIndirectCopyRestoreExpr(CurInit.take(), Step->Type, |
| 5699 | Step->Kind == SK_PassByIndirectCopyRestore)); |
| 5700 | break; |
| 5701 | |
| 5702 | case SK_ProduceObjCObject: |
| 5703 | CurInit = S.Owned(ImplicitCastExpr::Create(S.Context, Step->Type, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 5704 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5705 | CurInit.take(), 0, VK_RValue)); |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5706 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5707 | |
| 5708 | case SK_StdInitializerList: { |
| 5709 | QualType Dest = Step->Type; |
| 5710 | QualType E; |
Douglas Gregor | 6c5aaed | 2013-03-25 23:47:01 +0000 | [diff] [blame] | 5711 | bool Success = S.isStdInitializerList(Dest.getNonReferenceType(), &E); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5712 | (void)Success; |
| 5713 | assert(Success && "Destination type changed?"); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 5714 | |
| 5715 | // If the element type has a destructor, check it. |
| 5716 | if (CXXRecordDecl *RD = E->getAsCXXRecordDecl()) { |
| 5717 | if (!RD->hasIrrelevantDestructor()) { |
| 5718 | if (CXXDestructorDecl *Destructor = S.LookupDestructor(RD)) { |
| 5719 | S.MarkFunctionReferenced(Kind.getLocation(), Destructor); |
| 5720 | S.CheckDestructorAccess(Kind.getLocation(), Destructor, |
| 5721 | S.PDiag(diag::err_access_dtor_temp) << E); |
Richard Smith | 82f145d | 2013-05-04 06:44:46 +0000 | [diff] [blame] | 5722 | if (S.DiagnoseUseOfDecl(Destructor, Kind.getLocation())) |
| 5723 | return ExprError(); |
Sebastian Redl | 2835745 | 2012-03-05 19:35:43 +0000 | [diff] [blame] | 5724 | } |
| 5725 | } |
| 5726 | } |
| 5727 | |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5728 | InitListExpr *ILE = cast<InitListExpr>(CurInit.take()); |
Richard Smith | 03544fc | 2012-04-19 06:58:00 +0000 | [diff] [blame] | 5729 | S.Diag(ILE->getExprLoc(), diag::warn_cxx98_compat_initializer_list_init) |
| 5730 | << ILE->getSourceRange(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5731 | unsigned NumInits = ILE->getNumInits(); |
| 5732 | SmallVector<Expr*, 16> Converted(NumInits); |
| 5733 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 5734 | S.Context.getConstantArrayType(E, |
| 5735 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 5736 | NumInits), |
| 5737 | ArrayType::Normal, 0)); |
| 5738 | InitializedEntity Element =InitializedEntity::InitializeElement(S.Context, |
| 5739 | 0, HiddenArray); |
| 5740 | for (unsigned i = 0; i < NumInits; ++i) { |
| 5741 | Element.setElementIndex(i); |
| 5742 | ExprResult Init = S.Owned(ILE->getInit(i)); |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 5743 | ExprResult Res = S.PerformCopyInitialization( |
| 5744 | Element, Init.get()->getExprLoc(), Init, |
| 5745 | /*TopLevelOfInitList=*/ true); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5746 | assert(!Res.isInvalid() && "Result changed since try phase."); |
| 5747 | Converted[i] = Res.take(); |
| 5748 | } |
| 5749 | InitListExpr *Semantic = new (S.Context) |
| 5750 | InitListExpr(S.Context, ILE->getLBraceLoc(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5751 | Converted, ILE->getRBraceLoc()); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5752 | Semantic->setSyntacticForm(ILE); |
| 5753 | Semantic->setType(Dest); |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 5754 | Semantic->setInitializesStdInitializerList(); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 5755 | CurInit = S.Owned(Semantic); |
| 5756 | break; |
| 5757 | } |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 5758 | case SK_OCLSamplerInit: { |
| 5759 | assert(Step->Type->isSamplerT() && |
| 5760 | "Sampler initialization on non sampler type."); |
| 5761 | |
| 5762 | QualType SourceType = CurInit.get()->getType(); |
| 5763 | InitializedEntity::EntityKind EntityKind = Entity.getKind(); |
| 5764 | |
| 5765 | if (EntityKind == InitializedEntity::EK_Parameter) { |
| 5766 | if (!SourceType->isSamplerT()) |
| 5767 | S.Diag(Kind.getLocation(), diag::err_sampler_argument_required) |
| 5768 | << SourceType; |
| 5769 | } else if (EntityKind != InitializedEntity::EK_Variable) { |
| 5770 | llvm_unreachable("Invalid EntityKind!"); |
| 5771 | } |
| 5772 | |
| 5773 | break; |
| 5774 | } |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 5775 | case SK_OCLZeroEvent: { |
| 5776 | assert(Step->Type->isEventT() && |
| 5777 | "Event initialization on non event type."); |
| 5778 | |
| 5779 | CurInit = S.ImpCastExprToType(CurInit.take(), Step->Type, |
| 5780 | CK_ZeroToOCLEvent, |
| 5781 | CurInit.get()->getValueKind()); |
| 5782 | break; |
| 5783 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5784 | } |
| 5785 | } |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 5786 | |
| 5787 | // Diagnose non-fatal problems with the completed initialization. |
| 5788 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 5789 | cast<FieldDecl>(Entity.getDecl())->isBitField()) |
| 5790 | S.CheckBitFieldInitialization(Kind.getLocation(), |
| 5791 | cast<FieldDecl>(Entity.getDecl()), |
| 5792 | CurInit.get()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5793 | |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5794 | return CurInit; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5795 | } |
| 5796 | |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5797 | /// Somewhere within T there is an uninitialized reference subobject. |
| 5798 | /// Dig it out and diagnose it. |
Benjamin Kramer | a574c89 | 2013-02-15 12:30:38 +0000 | [diff] [blame] | 5799 | static bool DiagnoseUninitializedReference(Sema &S, SourceLocation Loc, |
| 5800 | QualType T) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5801 | if (T->isReferenceType()) { |
| 5802 | S.Diag(Loc, diag::err_reference_without_init) |
| 5803 | << T.getNonReferenceType(); |
| 5804 | return true; |
| 5805 | } |
| 5806 | |
| 5807 | CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl(); |
| 5808 | if (!RD || !RD->hasUninitializedReferenceMember()) |
| 5809 | return false; |
| 5810 | |
| 5811 | for (CXXRecordDecl::field_iterator FI = RD->field_begin(), |
| 5812 | FE = RD->field_end(); FI != FE; ++FI) { |
| 5813 | if (FI->isUnnamedBitfield()) |
| 5814 | continue; |
| 5815 | |
| 5816 | if (DiagnoseUninitializedReference(S, FI->getLocation(), FI->getType())) { |
| 5817 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 5818 | return true; |
| 5819 | } |
| 5820 | } |
| 5821 | |
| 5822 | for (CXXRecordDecl::base_class_iterator BI = RD->bases_begin(), |
| 5823 | BE = RD->bases_end(); |
| 5824 | BI != BE; ++BI) { |
| 5825 | if (DiagnoseUninitializedReference(S, BI->getLocStart(), BI->getType())) { |
| 5826 | S.Diag(Loc, diag::note_value_initialization_here) << RD; |
| 5827 | return true; |
| 5828 | } |
| 5829 | } |
| 5830 | |
| 5831 | return false; |
| 5832 | } |
| 5833 | |
| 5834 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5835 | //===----------------------------------------------------------------------===// |
| 5836 | // Diagnose initialization failures |
| 5837 | //===----------------------------------------------------------------------===// |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 5838 | |
| 5839 | /// Emit notes associated with an initialization that failed due to a |
| 5840 | /// "simple" conversion failure. |
| 5841 | static void emitBadConversionNotes(Sema &S, const InitializedEntity &entity, |
| 5842 | Expr *op) { |
| 5843 | QualType destType = entity.getType(); |
| 5844 | if (destType.getNonReferenceType()->isObjCObjectPointerType() && |
| 5845 | op->getType()->isObjCObjectPointerType()) { |
| 5846 | |
| 5847 | // Emit a possible note about the conversion failing because the |
| 5848 | // operand is a message send with a related result type. |
| 5849 | S.EmitRelatedResultTypeNote(op); |
| 5850 | |
| 5851 | // Emit a possible note about a return failing because we're |
| 5852 | // expecting a related result type. |
| 5853 | if (entity.getKind() == InitializedEntity::EK_Result) |
| 5854 | S.EmitRelatedResultTypeNoteForReturn(destType); |
| 5855 | } |
| 5856 | } |
| 5857 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5858 | bool InitializationSequence::Diagnose(Sema &S, |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5859 | const InitializedEntity &Entity, |
| 5860 | const InitializationKind &Kind, |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5861 | ArrayRef<Expr *> Args) { |
Sebastian Redl | d695d6b | 2011-06-05 13:59:05 +0000 | [diff] [blame] | 5862 | if (!Failed()) |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5863 | return false; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5864 | |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5865 | QualType DestType = Entity.getType(); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5866 | switch (Failure) { |
| 5867 | case FK_TooManyInitsForReference: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5868 | // FIXME: Customize for the initialized entity? |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5869 | if (Args.empty()) { |
Richard Smith | d5bc867 | 2012-12-08 02:01:17 +0000 | [diff] [blame] | 5870 | // Dig out the reference subobject which is uninitialized and diagnose it. |
| 5871 | // If this is value-initialization, this could be nested some way within |
| 5872 | // the target type. |
| 5873 | assert(Kind.getKind() == InitializationKind::IK_Value || |
| 5874 | DestType->isReferenceType()); |
| 5875 | bool Diagnosed = |
| 5876 | DiagnoseUninitializedReference(S, Kind.getLocation(), DestType); |
| 5877 | assert(Diagnosed && "couldn't find uninitialized reference to diagnose"); |
| 5878 | (void)Diagnosed; |
| 5879 | } else // FIXME: diagnostic below could be better! |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 5880 | S.Diag(Kind.getLocation(), diag::err_reference_has_multiple_inits) |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5881 | << SourceRange(Args.front()->getLocStart(), Args.back()->getLocEnd()); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5882 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5883 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5884 | case FK_ArrayNeedsInitList: |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5885 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 0; |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5886 | break; |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 5887 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 5888 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 1; |
| 5889 | break; |
| 5890 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 5891 | S.Diag(Kind.getLocation(), diag::err_array_init_not_init_list) << 2; |
| 5892 | break; |
| 5893 | case FK_NarrowStringIntoWideCharArray: |
| 5894 | S.Diag(Kind.getLocation(), diag::err_array_init_narrow_string_into_wchar); |
| 5895 | break; |
| 5896 | case FK_WideStringIntoCharArray: |
| 5897 | S.Diag(Kind.getLocation(), diag::err_array_init_wide_string_into_char); |
| 5898 | break; |
| 5899 | case FK_IncompatWideStringIntoWideChar: |
| 5900 | S.Diag(Kind.getLocation(), |
| 5901 | diag::err_array_init_incompat_wide_string_into_wchar); |
| 5902 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 5903 | case FK_ArrayTypeMismatch: |
| 5904 | case FK_NonConstantArrayInit: |
| 5905 | S.Diag(Kind.getLocation(), |
| 5906 | (Failure == FK_ArrayTypeMismatch |
| 5907 | ? diag::err_array_init_different_type |
| 5908 | : diag::err_array_init_non_constant_array)) |
| 5909 | << DestType.getNonReferenceType() |
| 5910 | << Args[0]->getType() |
| 5911 | << Args[0]->getSourceRange(); |
| 5912 | break; |
| 5913 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 5914 | case FK_VariableLengthArrayHasInitializer: |
| 5915 | S.Diag(Kind.getLocation(), diag::err_variable_object_no_init) |
| 5916 | << Args[0]->getSourceRange(); |
| 5917 | break; |
| 5918 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5919 | case FK_AddressOfOverloadFailed: { |
| 5920 | DeclAccessPair Found; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5921 | S.ResolveAddressOfOverloadedFunction(Args[0], |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5922 | DestType.getNonReferenceType(), |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5923 | true, |
| 5924 | Found); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5925 | break; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 5926 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5927 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5928 | case FK_ReferenceInitOverloadFailed: |
Douglas Gregor | 4a520a2 | 2009-12-14 17:27:33 +0000 | [diff] [blame] | 5929 | case FK_UserConversionOverloadFailed: |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5930 | switch (FailedOverloadResult) { |
| 5931 | case OR_Ambiguous: |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 5932 | if (Failure == FK_UserConversionOverloadFailed) |
| 5933 | S.Diag(Kind.getLocation(), diag::err_typecheck_ambiguous_condition) |
| 5934 | << Args[0]->getType() << DestType |
| 5935 | << Args[0]->getSourceRange(); |
| 5936 | else |
| 5937 | S.Diag(Kind.getLocation(), diag::err_ref_init_ambiguous) |
| 5938 | << DestType << Args[0]->getType() |
| 5939 | << Args[0]->getSourceRange(); |
| 5940 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5941 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5942 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5943 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5944 | case OR_No_Viable_Function: |
| 5945 | S.Diag(Kind.getLocation(), diag::err_typecheck_nonviable_condition) |
| 5946 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5947 | << Args[0]->getSourceRange(); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 5948 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5949 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5950 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5951 | case OR_Deleted: { |
| 5952 | S.Diag(Kind.getLocation(), diag::err_typecheck_deleted_function) |
| 5953 | << Args[0]->getType() << DestType.getNonReferenceType() |
| 5954 | << Args[0]->getSourceRange(); |
| 5955 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 5956 | OverloadingResult Ovl |
Douglas Gregor | 8fcc516 | 2010-09-12 08:07:23 +0000 | [diff] [blame] | 5957 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best, |
| 5958 | true); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5959 | if (Ovl == OR_Deleted) { |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 5960 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5961 | } else { |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5962 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5963 | } |
| 5964 | break; |
| 5965 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5966 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5967 | case OR_Success: |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 5968 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5969 | } |
| 5970 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5971 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5972 | case FK_NonConstLValueReferenceBindingToTemporary: |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 5973 | if (isa<InitListExpr>(Args[0])) { |
| 5974 | S.Diag(Kind.getLocation(), |
| 5975 | diag::err_lvalue_reference_bind_to_initlist) |
| 5976 | << DestType.getNonReferenceType().isVolatileQualified() |
| 5977 | << DestType.getNonReferenceType() |
| 5978 | << Args[0]->getSourceRange(); |
| 5979 | break; |
| 5980 | } |
| 5981 | // Intentional fallthrough |
| 5982 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5983 | case FK_NonConstLValueReferenceBindingToUnrelated: |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5984 | S.Diag(Kind.getLocation(), |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5985 | Failure == FK_NonConstLValueReferenceBindingToTemporary |
| 5986 | ? diag::err_lvalue_reference_bind_to_temporary |
| 5987 | : diag::err_lvalue_reference_bind_to_unrelated) |
Douglas Gregor | ef06e24 | 2010-01-29 19:39:15 +0000 | [diff] [blame] | 5988 | << DestType.getNonReferenceType().isVolatileQualified() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5989 | << DestType.getNonReferenceType() |
| 5990 | << Args[0]->getType() |
| 5991 | << Args[0]->getSourceRange(); |
| 5992 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5993 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5994 | case FK_RValueReferenceBindingToLValue: |
| 5995 | S.Diag(Kind.getLocation(), diag::err_lvalue_to_rvalue_ref) |
Douglas Gregor | fb5d7ef | 2011-01-21 01:04:33 +0000 | [diff] [blame] | 5996 | << DestType.getNonReferenceType() << Args[0]->getType() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 5997 | << Args[0]->getSourceRange(); |
| 5998 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 5999 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6000 | case FK_ReferenceInitDropsQualifiers: |
| 6001 | S.Diag(Kind.getLocation(), diag::err_reference_bind_drops_quals) |
| 6002 | << DestType.getNonReferenceType() |
| 6003 | << Args[0]->getType() |
| 6004 | << Args[0]->getSourceRange(); |
| 6005 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6006 | |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6007 | case FK_ReferenceInitFailed: |
| 6008 | S.Diag(Kind.getLocation(), diag::err_reference_bind_failed) |
| 6009 | << DestType.getNonReferenceType() |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6010 | << Args[0]->isLValue() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6011 | << Args[0]->getType() |
| 6012 | << Args[0]->getSourceRange(); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6013 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6014 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6015 | |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6016 | case FK_ConversionFailed: { |
| 6017 | QualType FromType = Args[0]->getType(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6018 | PartialDiagnostic PDiag = S.PDiag(diag::err_init_conversion_failed) |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6019 | << (int)Entity.getKind() |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6020 | << DestType |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 6021 | << Args[0]->isLValue() |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6022 | << FromType |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6023 | << Args[0]->getSourceRange(); |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 6024 | S.HandleFunctionTypeMismatch(PDiag, FromType, DestType); |
| 6025 | S.Diag(Kind.getLocation(), PDiag); |
John McCall | 7cca821 | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 6026 | emitBadConversionNotes(S, Entity, Args[0]); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6027 | break; |
Douglas Gregor | 1be8eec | 2011-02-19 21:32:49 +0000 | [diff] [blame] | 6028 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6029 | |
| 6030 | case FK_ConversionFromPropertyFailed: |
| 6031 | // No-op. This error has already been reported. |
| 6032 | break; |
| 6033 | |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6034 | case FK_TooManyInitsForScalar: { |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6035 | SourceRange R; |
| 6036 | |
| 6037 | if (InitListExpr *InitList = dyn_cast<InitListExpr>(Args[0])) |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6038 | R = SourceRange(InitList->getInit(0)->getLocEnd(), |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6039 | InitList->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6040 | else |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6041 | R = SourceRange(Args.front()->getLocEnd(), Args.back()->getLocEnd()); |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6042 | |
Douglas Gregor | 19311e7 | 2010-09-08 21:40:08 +0000 | [diff] [blame] | 6043 | R.setBegin(S.PP.getLocForEndOfToken(R.getBegin())); |
| 6044 | if (Kind.isCStyleOrFunctionalCast()) |
| 6045 | S.Diag(Kind.getLocation(), diag::err_builtin_func_cast_more_than_one_arg) |
| 6046 | << R; |
| 6047 | else |
| 6048 | S.Diag(Kind.getLocation(), diag::err_excess_initializers) |
| 6049 | << /*scalar=*/2 << R; |
Douglas Gregor | d87b61f | 2009-12-10 17:56:55 +0000 | [diff] [blame] | 6050 | break; |
| 6051 | } |
| 6052 | |
| 6053 | case FK_ReferenceBindingToInitList: |
| 6054 | S.Diag(Kind.getLocation(), diag::err_reference_bind_init_list) |
| 6055 | << DestType.getNonReferenceType() << Args[0]->getSourceRange(); |
| 6056 | break; |
| 6057 | |
| 6058 | case FK_InitListBadDestinationType: |
| 6059 | S.Diag(Kind.getLocation(), diag::err_init_list_bad_dest_type) |
| 6060 | << (DestType->isRecordType()) << DestType << Args[0]->getSourceRange(); |
| 6061 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6062 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6063 | case FK_ListConstructorOverloadFailed: |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6064 | case FK_ConstructorOverloadFailed: { |
| 6065 | SourceRange ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6066 | if (Args.size()) |
| 6067 | ArgsRange = SourceRange(Args.front()->getLocStart(), |
| 6068 | Args.back()->getLocEnd()); |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6069 | |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6070 | if (Failure == FK_ListConstructorOverloadFailed) { |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6071 | assert(Args.size() == 1 && "List construction from other than 1 argument."); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6072 | InitListExpr *InitList = cast<InitListExpr>(Args[0]); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6073 | Args = MultiExprArg(InitList->getInits(), InitList->getNumInits()); |
Sebastian Redl | cf15cef | 2011-12-22 18:58:38 +0000 | [diff] [blame] | 6074 | } |
| 6075 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6076 | // FIXME: Using "DestType" for the entity we're printing is probably |
| 6077 | // bad. |
| 6078 | switch (FailedOverloadResult) { |
| 6079 | case OR_Ambiguous: |
| 6080 | S.Diag(Kind.getLocation(), diag::err_ovl_ambiguous_init) |
| 6081 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6082 | FailedCandidateSet.NoteCandidates(S, OCD_ViableCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6083 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6084 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6085 | case OR_No_Viable_Function: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6086 | if (Kind.getKind() == InitializationKind::IK_Default && |
| 6087 | (Entity.getKind() == InitializedEntity::EK_Base || |
| 6088 | Entity.getKind() == InitializedEntity::EK_Member) && |
| 6089 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6090 | // This is implicit default initialization of a member or |
| 6091 | // base within a constructor. If no viable function was |
| 6092 | // found, notify the user that she needs to explicitly |
| 6093 | // initialize this base/member. |
| 6094 | CXXConstructorDecl *Constructor |
| 6095 | = cast<CXXConstructorDecl>(S.CurContext); |
| 6096 | if (Entity.getKind() == InitializedEntity::EK_Base) { |
| 6097 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6098 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6099 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6100 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6101 | << /*base=*/0 |
| 6102 | << Entity.getType(); |
| 6103 | |
| 6104 | RecordDecl *BaseDecl |
| 6105 | = Entity.getBaseSpecifier()->getType()->getAs<RecordType>() |
| 6106 | ->getDecl(); |
| 6107 | S.Diag(BaseDecl->getLocation(), diag::note_previous_decl) |
| 6108 | << S.Context.getTagDeclType(BaseDecl); |
| 6109 | } else { |
| 6110 | S.Diag(Kind.getLocation(), diag::err_missing_default_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6111 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6112 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6113 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6114 | << /*member=*/1 |
| 6115 | << Entity.getName(); |
| 6116 | S.Diag(Entity.getDecl()->getLocation(), diag::note_field_decl); |
| 6117 | |
| 6118 | if (const RecordType *Record |
| 6119 | = Entity.getType()->getAs<RecordType>()) |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6120 | S.Diag(Record->getDecl()->getLocation(), |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6121 | diag::note_previous_decl) |
| 6122 | << S.Context.getTagDeclType(Record->getDecl()); |
| 6123 | } |
| 6124 | break; |
| 6125 | } |
| 6126 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6127 | S.Diag(Kind.getLocation(), diag::err_ovl_no_viable_function_in_init) |
| 6128 | << DestType << ArgsRange; |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6129 | FailedCandidateSet.NoteCandidates(S, OCD_AllCandidates, Args); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6130 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6131 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6132 | case OR_Deleted: { |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6133 | OverloadCandidateSet::iterator Best; |
John McCall | 120d63c | 2010-08-24 20:38:10 +0000 | [diff] [blame] | 6134 | OverloadingResult Ovl |
| 6135 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6136 | if (Ovl != OR_Deleted) { |
| 6137 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6138 | << true << DestType << ArgsRange; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6139 | llvm_unreachable("Inconsistent overload resolution?"); |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6140 | break; |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6141 | } |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6142 | |
| 6143 | // If this is a defaulted or implicitly-declared function, then |
| 6144 | // it was implicitly deleted. Make it clear that the deletion was |
| 6145 | // implicit. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6146 | if (S.isImplicitlyDeleted(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6147 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_special_init) |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6148 | << S.getSpecialMember(cast<CXXMethodDecl>(Best->Function)) |
Douglas Gregor | e4e68d4 | 2012-02-15 19:33:52 +0000 | [diff] [blame] | 6149 | << DestType << ArgsRange; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 6150 | else |
| 6151 | S.Diag(Kind.getLocation(), diag::err_ovl_deleted_init) |
| 6152 | << true << DestType << ArgsRange; |
| 6153 | |
| 6154 | S.NoteDeletedFunction(Best->Function); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6155 | break; |
| 6156 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6157 | |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6158 | case OR_Success: |
| 6159 | llvm_unreachable("Conversion did not fail!"); |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6160 | } |
Douglas Gregor | 51c56d6 | 2009-12-14 20:49:26 +0000 | [diff] [blame] | 6161 | } |
David Blaikie | 9fdefb3 | 2012-01-17 08:24:58 +0000 | [diff] [blame] | 6162 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6163 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6164 | case FK_DefaultInitOfConst: |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6165 | if (Entity.getKind() == InitializedEntity::EK_Member && |
| 6166 | isa<CXXConstructorDecl>(S.CurContext)) { |
| 6167 | // This is implicit default-initialization of a const member in |
| 6168 | // a constructor. Complain that it needs to be explicitly |
| 6169 | // initialized. |
| 6170 | CXXConstructorDecl *Constructor = cast<CXXConstructorDecl>(S.CurContext); |
| 6171 | S.Diag(Kind.getLocation(), diag::err_uninitialized_member_in_ctor) |
Richard Smith | 07b0fdc | 2013-03-18 21:12:30 +0000 | [diff] [blame] | 6172 | << (Constructor->getInheritedConstructor() ? 2 : |
| 6173 | Constructor->isImplicit() ? 1 : 0) |
Douglas Gregor | 9db7dbb | 2010-01-31 09:12:51 +0000 | [diff] [blame] | 6174 | << S.Context.getTypeDeclType(Constructor->getParent()) |
| 6175 | << /*const=*/1 |
| 6176 | << Entity.getName(); |
| 6177 | S.Diag(Entity.getDecl()->getLocation(), diag::note_previous_decl) |
| 6178 | << Entity.getName(); |
| 6179 | } else { |
| 6180 | S.Diag(Kind.getLocation(), diag::err_default_init_const) |
| 6181 | << DestType << (bool)DestType->getAs<RecordType>(); |
| 6182 | } |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 6183 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6184 | |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6185 | case FK_Incomplete: |
Douglas Gregor | 69a30b8 | 2012-04-10 20:43:46 +0000 | [diff] [blame] | 6186 | S.RequireCompleteType(Kind.getLocation(), FailedIncompleteType, |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6187 | diag::err_init_incomplete_type); |
| 6188 | break; |
| 6189 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6190 | case FK_ListInitializationFailed: { |
| 6191 | // Run the init list checker again to emit diagnostics. |
| 6192 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 6193 | QualType DestType = Entity.getType(); |
| 6194 | InitListChecker DiagnoseInitList(S, Entity, InitList, |
Sebastian Redl | c223518 | 2011-10-16 18:19:28 +0000 | [diff] [blame] | 6195 | DestType, /*VerifyOnly=*/false, |
Sebastian Redl | 168319c | 2012-02-12 16:37:24 +0000 | [diff] [blame] | 6196 | Kind.getKind() != InitializationKind::IK_DirectList || |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6197 | !S.getLangOpts().CPlusPlus11); |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6198 | assert(DiagnoseInitList.HadError() && |
| 6199 | "Inconsistent init list check result."); |
| 6200 | break; |
| 6201 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6202 | |
| 6203 | case FK_PlaceholderType: { |
| 6204 | // FIXME: Already diagnosed! |
| 6205 | break; |
| 6206 | } |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6207 | |
| 6208 | case FK_InitListElementCopyFailure: { |
| 6209 | // Try to perform all copies again. |
| 6210 | InitListExpr* InitList = cast<InitListExpr>(Args[0]); |
| 6211 | unsigned NumInits = InitList->getNumInits(); |
| 6212 | QualType DestType = Entity.getType(); |
| 6213 | QualType E; |
Douglas Gregor | 6c5aaed | 2013-03-25 23:47:01 +0000 | [diff] [blame] | 6214 | bool Success = S.isStdInitializerList(DestType.getNonReferenceType(), &E); |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6215 | (void)Success; |
| 6216 | assert(Success && "Where did the std::initializer_list go?"); |
| 6217 | InitializedEntity HiddenArray = InitializedEntity::InitializeTemporary( |
| 6218 | S.Context.getConstantArrayType(E, |
| 6219 | llvm::APInt(S.Context.getTypeSize(S.Context.getSizeType()), |
| 6220 | NumInits), |
| 6221 | ArrayType::Normal, 0)); |
| 6222 | InitializedEntity Element = InitializedEntity::InitializeElement(S.Context, |
| 6223 | 0, HiddenArray); |
| 6224 | // Show at most 3 errors. Otherwise, you'd get a lot of errors for errors |
| 6225 | // where the init list type is wrong, e.g. |
| 6226 | // std::initializer_list<void*> list = { 1, 2, 3, 4, 5, 6, 7, 8 }; |
| 6227 | // FIXME: Emit a note if we hit the limit? |
| 6228 | int ErrorCount = 0; |
| 6229 | for (unsigned i = 0; i < NumInits && ErrorCount < 3; ++i) { |
| 6230 | Element.setElementIndex(i); |
| 6231 | ExprResult Init = S.Owned(InitList->getInit(i)); |
| 6232 | if (S.PerformCopyInitialization(Element, Init.get()->getExprLoc(), Init) |
| 6233 | .isInvalid()) |
| 6234 | ++ErrorCount; |
| 6235 | } |
| 6236 | break; |
| 6237 | } |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6238 | |
| 6239 | case FK_ExplicitConstructor: { |
| 6240 | S.Diag(Kind.getLocation(), diag::err_selected_explicit_constructor) |
| 6241 | << Args[0]->getSourceRange(); |
| 6242 | OverloadCandidateSet::iterator Best; |
| 6243 | OverloadingResult Ovl |
| 6244 | = FailedCandidateSet.BestViableFunction(S, Kind.getLocation(), Best); |
Matt Beaumont-Gay | e7d0bbf | 2012-04-02 19:05:35 +0000 | [diff] [blame] | 6245 | (void)Ovl; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6246 | assert(Ovl == OR_Success && "Inconsistent overload resolution"); |
| 6247 | CXXConstructorDecl *CtorDecl = cast<CXXConstructorDecl>(Best->Function); |
| 6248 | S.Diag(CtorDecl->getLocation(), diag::note_constructor_declared_here); |
| 6249 | break; |
| 6250 | } |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6251 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6252 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 6253 | PrintInitLocationNote(S, Entity); |
Douglas Gregor | 20093b4 | 2009-12-09 23:02:17 +0000 | [diff] [blame] | 6254 | return true; |
| 6255 | } |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6256 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 6257 | void InitializationSequence::dump(raw_ostream &OS) const { |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6258 | switch (SequenceKind) { |
| 6259 | case FailedSequence: { |
| 6260 | OS << "Failed sequence: "; |
| 6261 | switch (Failure) { |
| 6262 | case FK_TooManyInitsForReference: |
| 6263 | OS << "too many initializers for reference"; |
| 6264 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6265 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6266 | case FK_ArrayNeedsInitList: |
| 6267 | OS << "array requires initializer list"; |
| 6268 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6269 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6270 | case FK_ArrayNeedsInitListOrStringLiteral: |
| 6271 | OS << "array requires initializer list or string literal"; |
| 6272 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6273 | |
Hans Wennborg | 0ff5074 | 2013-05-15 11:03:04 +0000 | [diff] [blame] | 6274 | case FK_ArrayNeedsInitListOrWideStringLiteral: |
| 6275 | OS << "array requires initializer list or wide string literal"; |
| 6276 | break; |
| 6277 | |
| 6278 | case FK_NarrowStringIntoWideCharArray: |
| 6279 | OS << "narrow string into wide char array"; |
| 6280 | break; |
| 6281 | |
| 6282 | case FK_WideStringIntoCharArray: |
| 6283 | OS << "wide string into char array"; |
| 6284 | break; |
| 6285 | |
| 6286 | case FK_IncompatWideStringIntoWideChar: |
| 6287 | OS << "incompatible wide string into wide char array"; |
| 6288 | break; |
| 6289 | |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6290 | case FK_ArrayTypeMismatch: |
| 6291 | OS << "array type mismatch"; |
| 6292 | break; |
| 6293 | |
| 6294 | case FK_NonConstantArrayInit: |
| 6295 | OS << "non-constant array initializer"; |
| 6296 | break; |
| 6297 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6298 | case FK_AddressOfOverloadFailed: |
| 6299 | OS << "address of overloaded function failed"; |
| 6300 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6301 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6302 | case FK_ReferenceInitOverloadFailed: |
| 6303 | OS << "overload resolution for reference initialization failed"; |
| 6304 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6305 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6306 | case FK_NonConstLValueReferenceBindingToTemporary: |
| 6307 | OS << "non-const lvalue reference bound to temporary"; |
| 6308 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6309 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6310 | case FK_NonConstLValueReferenceBindingToUnrelated: |
| 6311 | OS << "non-const lvalue reference bound to unrelated type"; |
| 6312 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6313 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6314 | case FK_RValueReferenceBindingToLValue: |
| 6315 | OS << "rvalue reference bound to an lvalue"; |
| 6316 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6317 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6318 | case FK_ReferenceInitDropsQualifiers: |
| 6319 | OS << "reference initialization drops qualifiers"; |
| 6320 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6321 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6322 | case FK_ReferenceInitFailed: |
| 6323 | OS << "reference initialization failed"; |
| 6324 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6325 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6326 | case FK_ConversionFailed: |
| 6327 | OS << "conversion failed"; |
| 6328 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6329 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6330 | case FK_ConversionFromPropertyFailed: |
| 6331 | OS << "conversion from property failed"; |
| 6332 | break; |
| 6333 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6334 | case FK_TooManyInitsForScalar: |
| 6335 | OS << "too many initializers for scalar"; |
| 6336 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6337 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6338 | case FK_ReferenceBindingToInitList: |
| 6339 | OS << "referencing binding to initializer list"; |
| 6340 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6341 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6342 | case FK_InitListBadDestinationType: |
| 6343 | OS << "initializer list for non-aggregate, non-scalar type"; |
| 6344 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6345 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6346 | case FK_UserConversionOverloadFailed: |
| 6347 | OS << "overloading failed for user-defined conversion"; |
| 6348 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6349 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6350 | case FK_ConstructorOverloadFailed: |
| 6351 | OS << "constructor overloading failed"; |
| 6352 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6353 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6354 | case FK_DefaultInitOfConst: |
| 6355 | OS << "default initialization of a const variable"; |
| 6356 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6357 | |
Douglas Gregor | 72a43bb | 2010-05-20 22:12:02 +0000 | [diff] [blame] | 6358 | case FK_Incomplete: |
| 6359 | OS << "initialization of incomplete type"; |
| 6360 | break; |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6361 | |
| 6362 | case FK_ListInitializationFailed: |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 6363 | OS << "list initialization checker failure"; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6364 | break; |
| 6365 | |
John McCall | 7307643 | 2012-01-05 00:13:19 +0000 | [diff] [blame] | 6366 | case FK_VariableLengthArrayHasInitializer: |
| 6367 | OS << "variable length array has an initializer"; |
| 6368 | break; |
| 6369 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 6370 | case FK_PlaceholderType: |
| 6371 | OS << "initializer expression isn't contextually valid"; |
| 6372 | break; |
Nick Lewycky | b0c6c33 | 2011-12-22 20:21:32 +0000 | [diff] [blame] | 6373 | |
| 6374 | case FK_ListConstructorOverloadFailed: |
| 6375 | OS << "list constructor overloading failed"; |
| 6376 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6377 | |
| 6378 | case FK_InitListElementCopyFailure: |
| 6379 | OS << "copy construction of initializer list element failed"; |
| 6380 | break; |
Sebastian Redl | 70e24fc | 2012-04-01 19:54:59 +0000 | [diff] [blame] | 6381 | |
| 6382 | case FK_ExplicitConstructor: |
| 6383 | OS << "list copy initialization chose explicit constructor"; |
| 6384 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6385 | } |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6386 | OS << '\n'; |
| 6387 | return; |
| 6388 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6389 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6390 | case DependentSequence: |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6391 | OS << "Dependent sequence\n"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6392 | return; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6393 | |
Sebastian Redl | 7491c49 | 2011-06-05 13:59:11 +0000 | [diff] [blame] | 6394 | case NormalSequence: |
| 6395 | OS << "Normal sequence: "; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6396 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6397 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6398 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6399 | for (step_iterator S = step_begin(), SEnd = step_end(); S != SEnd; ++S) { |
| 6400 | if (S != step_begin()) { |
| 6401 | OS << " -> "; |
| 6402 | } |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6403 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6404 | switch (S->Kind) { |
| 6405 | case SK_ResolveAddressOfOverloadedFunction: |
| 6406 | OS << "resolve address of overloaded function"; |
| 6407 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6408 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6409 | case SK_CastDerivedToBaseRValue: |
| 6410 | OS << "derived-to-base case (rvalue" << S->Type.getAsString() << ")"; |
| 6411 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6412 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6413 | case SK_CastDerivedToBaseXValue: |
| 6414 | OS << "derived-to-base case (xvalue" << S->Type.getAsString() << ")"; |
| 6415 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6416 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6417 | case SK_CastDerivedToBaseLValue: |
| 6418 | OS << "derived-to-base case (lvalue" << S->Type.getAsString() << ")"; |
| 6419 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6420 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6421 | case SK_BindReference: |
| 6422 | OS << "bind reference to lvalue"; |
| 6423 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6424 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6425 | case SK_BindReferenceToTemporary: |
| 6426 | OS << "bind reference to a temporary"; |
| 6427 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6428 | |
Douglas Gregor | 523d46a | 2010-04-18 07:40:54 +0000 | [diff] [blame] | 6429 | case SK_ExtraneousCopyToTemporary: |
| 6430 | OS << "extraneous C++03 copy to temporary"; |
| 6431 | break; |
| 6432 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6433 | case SK_UserConversion: |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6434 | OS << "user-defined conversion via " << *S->Function.Function; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6435 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6436 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6437 | case SK_QualificationConversionRValue: |
| 6438 | OS << "qualification conversion (rvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6439 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6440 | |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6441 | case SK_QualificationConversionXValue: |
| 6442 | OS << "qualification conversion (xvalue)"; |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6443 | break; |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 6444 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6445 | case SK_QualificationConversionLValue: |
| 6446 | OS << "qualification conversion (lvalue)"; |
| 6447 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6448 | |
Jordan Rose | 1fd1e28 | 2013-04-11 00:58:58 +0000 | [diff] [blame] | 6449 | case SK_LValueToRValue: |
| 6450 | OS << "load (lvalue to rvalue)"; |
| 6451 | break; |
| 6452 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6453 | case SK_ConversionSequence: |
| 6454 | OS << "implicit conversion sequence ("; |
| 6455 | S->ICS->DebugPrint(); // FIXME: use OS |
| 6456 | OS << ")"; |
| 6457 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6458 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6459 | case SK_ListInitialization: |
Sebastian Redl | 8713d4e | 2011-09-24 17:47:52 +0000 | [diff] [blame] | 6460 | OS << "list aggregate initialization"; |
| 6461 | break; |
| 6462 | |
| 6463 | case SK_ListConstructorCall: |
| 6464 | OS << "list initialization via constructor"; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6465 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6466 | |
Sebastian Redl | 13dc8f9 | 2011-11-27 16:50:07 +0000 | [diff] [blame] | 6467 | case SK_UnwrapInitList: |
| 6468 | OS << "unwrap reference initializer list"; |
| 6469 | break; |
| 6470 | |
| 6471 | case SK_RewrapInitList: |
| 6472 | OS << "rewrap reference initializer list"; |
| 6473 | break; |
| 6474 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6475 | case SK_ConstructorInitialization: |
| 6476 | OS << "constructor initialization"; |
| 6477 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6478 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6479 | case SK_ZeroInitialization: |
| 6480 | OS << "zero initialization"; |
| 6481 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6482 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6483 | case SK_CAssignment: |
| 6484 | OS << "C assignment"; |
| 6485 | break; |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6486 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6487 | case SK_StringInit: |
| 6488 | OS << "string initialization"; |
| 6489 | break; |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 6490 | |
| 6491 | case SK_ObjCObjectConversion: |
| 6492 | OS << "Objective-C object conversion"; |
| 6493 | break; |
Douglas Gregor | cd9ec3b | 2011-02-22 18:29:51 +0000 | [diff] [blame] | 6494 | |
| 6495 | case SK_ArrayInit: |
| 6496 | OS << "array initialization"; |
| 6497 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6498 | |
Richard Smith | 0f163e9 | 2012-02-15 22:38:09 +0000 | [diff] [blame] | 6499 | case SK_ParenthesizedArrayInit: |
| 6500 | OS << "parenthesized array initialization"; |
| 6501 | break; |
| 6502 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6503 | case SK_PassByIndirectCopyRestore: |
| 6504 | OS << "pass by indirect copy and restore"; |
| 6505 | break; |
| 6506 | |
| 6507 | case SK_PassByIndirectRestore: |
| 6508 | OS << "pass by indirect restore"; |
| 6509 | break; |
| 6510 | |
| 6511 | case SK_ProduceObjCObject: |
| 6512 | OS << "Objective-C object retension"; |
| 6513 | break; |
Sebastian Redl | 2b916b8 | 2012-01-17 22:49:42 +0000 | [diff] [blame] | 6514 | |
| 6515 | case SK_StdInitializerList: |
| 6516 | OS << "std::initializer_list from initializer list"; |
| 6517 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6518 | |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 6519 | case SK_OCLSamplerInit: |
| 6520 | OS << "OpenCL sampler_t from integer constant"; |
| 6521 | break; |
| 6522 | |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 6523 | case SK_OCLZeroEvent: |
| 6524 | OS << "OpenCL event_t from zero"; |
| 6525 | break; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6526 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6527 | |
| 6528 | OS << " [" << S->Type.getAsString() << ']'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6529 | } |
Richard Smith | a4dc51b | 2013-02-05 05:52:24 +0000 | [diff] [blame] | 6530 | |
| 6531 | OS << '\n'; |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 6532 | } |
| 6533 | |
| 6534 | void InitializationSequence::dump() const { |
| 6535 | dump(llvm::errs()); |
| 6536 | } |
| 6537 | |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6538 | static void DiagnoseNarrowingInInitList(Sema &S, InitializationSequence &Seq, |
| 6539 | QualType EntityType, |
| 6540 | const Expr *PreInit, |
| 6541 | const Expr *PostInit) { |
| 6542 | if (Seq.step_begin() == Seq.step_end() || PreInit->isValueDependent()) |
| 6543 | return; |
| 6544 | |
| 6545 | // A narrowing conversion can only appear as the final implicit conversion in |
| 6546 | // an initialization sequence. |
| 6547 | const InitializationSequence::Step &LastStep = Seq.step_end()[-1]; |
| 6548 | if (LastStep.Kind != InitializationSequence::SK_ConversionSequence) |
| 6549 | return; |
| 6550 | |
| 6551 | const ImplicitConversionSequence &ICS = *LastStep.ICS; |
| 6552 | const StandardConversionSequence *SCS = 0; |
| 6553 | switch (ICS.getKind()) { |
| 6554 | case ImplicitConversionSequence::StandardConversion: |
| 6555 | SCS = &ICS.Standard; |
| 6556 | break; |
| 6557 | case ImplicitConversionSequence::UserDefinedConversion: |
| 6558 | SCS = &ICS.UserDefined.After; |
| 6559 | break; |
| 6560 | case ImplicitConversionSequence::AmbiguousConversion: |
| 6561 | case ImplicitConversionSequence::EllipsisConversion: |
| 6562 | case ImplicitConversionSequence::BadConversion: |
| 6563 | return; |
| 6564 | } |
| 6565 | |
| 6566 | // Determine the type prior to the narrowing conversion. If a conversion |
| 6567 | // operator was used, this may be different from both the type of the entity |
| 6568 | // and of the pre-initialization expression. |
| 6569 | QualType PreNarrowingType = PreInit->getType(); |
| 6570 | if (Seq.step_begin() + 1 != Seq.step_end()) |
| 6571 | PreNarrowingType = Seq.step_end()[-2].Type; |
| 6572 | |
| 6573 | // C++11 [dcl.init.list]p7: Check whether this is a narrowing conversion. |
| 6574 | APValue ConstantValue; |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6575 | QualType ConstantType; |
| 6576 | switch (SCS->getNarrowingKind(S.Context, PostInit, ConstantValue, |
| 6577 | ConstantType)) { |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6578 | case NK_Not_Narrowing: |
| 6579 | // No narrowing occurred. |
| 6580 | return; |
| 6581 | |
| 6582 | case NK_Type_Narrowing: |
| 6583 | // This was a floating-to-integer conversion, which is always considered a |
| 6584 | // narrowing conversion even if the value is a constant and can be |
| 6585 | // represented exactly as an integer. |
| 6586 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6587 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6588 | diag::warn_init_list_type_narrowing |
| 6589 | : S.isSFINAEContext()? |
| 6590 | diag::err_init_list_type_narrowing_sfinae |
| 6591 | : diag::err_init_list_type_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6592 | << PostInit->getSourceRange() |
| 6593 | << PreNarrowingType.getLocalUnqualifiedType() |
| 6594 | << EntityType.getLocalUnqualifiedType(); |
| 6595 | break; |
| 6596 | |
| 6597 | case NK_Constant_Narrowing: |
| 6598 | // A constant value was narrowed. |
| 6599 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6600 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6601 | diag::warn_init_list_constant_narrowing |
| 6602 | : S.isSFINAEContext()? |
| 6603 | diag::err_init_list_constant_narrowing_sfinae |
| 6604 | : diag::err_init_list_constant_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6605 | << PostInit->getSourceRange() |
Richard Smith | f602806 | 2012-03-23 23:55:39 +0000 | [diff] [blame] | 6606 | << ConstantValue.getAsString(S.getASTContext(), ConstantType) |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6607 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6608 | break; |
| 6609 | |
| 6610 | case NK_Variable_Narrowing: |
| 6611 | // A variable's value may have been narrowed. |
| 6612 | S.Diag(PostInit->getLocStart(), |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 6613 | S.getLangOpts().MicrosoftExt || !S.getLangOpts().CPlusPlus11? |
Douglas Gregor | f3c82c5 | 2012-01-23 15:29:33 +0000 | [diff] [blame] | 6614 | diag::warn_init_list_variable_narrowing |
| 6615 | : S.isSFINAEContext()? |
| 6616 | diag::err_init_list_variable_narrowing_sfinae |
| 6617 | : diag::err_init_list_variable_narrowing) |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6618 | << PostInit->getSourceRange() |
| 6619 | << PreNarrowingType.getLocalUnqualifiedType() |
Jeffrey Yasskin | 9906149 | 2011-08-29 15:59:37 +0000 | [diff] [blame] | 6620 | << EntityType.getLocalUnqualifiedType(); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6621 | break; |
| 6622 | } |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6623 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6624 | SmallString<128> StaticCast; |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6625 | llvm::raw_svector_ostream OS(StaticCast); |
| 6626 | OS << "static_cast<"; |
| 6627 | if (const TypedefType *TT = EntityType->getAs<TypedefType>()) { |
| 6628 | // It's important to use the typedef's name if there is one so that the |
| 6629 | // fixit doesn't break code using types like int64_t. |
| 6630 | // |
| 6631 | // FIXME: This will break if the typedef requires qualification. But |
| 6632 | // getQualifiedNameAsString() includes non-machine-parsable components. |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 6633 | OS << *TT->getDecl(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6634 | } else if (const BuiltinType *BT = EntityType->getAs<BuiltinType>()) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6635 | OS << BT->getName(S.getLangOpts()); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6636 | else { |
| 6637 | // Oops, we didn't find the actual type of the variable. Don't emit a fixit |
| 6638 | // with a broken cast. |
| 6639 | return; |
| 6640 | } |
| 6641 | OS << ">("; |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6642 | S.Diag(PostInit->getLocStart(), diag::note_init_list_narrowing_override) |
| 6643 | << PostInit->getSourceRange() |
| 6644 | << FixItHint::CreateInsertion(PostInit->getLocStart(), OS.str()) |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6645 | << FixItHint::CreateInsertion( |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6646 | S.getPreprocessor().getLocForEndOfToken(PostInit->getLocEnd()), ")"); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6647 | } |
| 6648 | |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6649 | //===----------------------------------------------------------------------===// |
| 6650 | // Initialization helper functions |
| 6651 | //===----------------------------------------------------------------------===// |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6652 | bool |
| 6653 | Sema::CanPerformCopyInitialization(const InitializedEntity &Entity, |
| 6654 | ExprResult Init) { |
| 6655 | if (Init.isInvalid()) |
| 6656 | return false; |
| 6657 | |
| 6658 | Expr *InitE = Init.get(); |
| 6659 | assert(InitE && "No initialization expression"); |
| 6660 | |
Douglas Gregor | 3c394c5 | 2012-07-31 22:15:04 +0000 | [diff] [blame] | 6661 | InitializationKind Kind |
| 6662 | = InitializationKind::CreateCopy(InitE->getLocStart(), SourceLocation()); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6663 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Sebastian Redl | 383616c | 2011-06-05 12:23:28 +0000 | [diff] [blame] | 6664 | return !Seq.Failed(); |
Sean Hunt | 2be7e90 | 2011-05-12 22:46:29 +0000 | [diff] [blame] | 6665 | } |
| 6666 | |
NAKAMURA Takumi | dfbb02a | 2011-01-27 07:10:08 +0000 | [diff] [blame] | 6667 | ExprResult |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6668 | Sema::PerformCopyInitialization(const InitializedEntity &Entity, |
| 6669 | SourceLocation EqualLoc, |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6670 | ExprResult Init, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6671 | bool TopLevelOfInitList, |
| 6672 | bool AllowExplicit) { |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6673 | if (Init.isInvalid()) |
| 6674 | return ExprError(); |
| 6675 | |
John McCall | 15d7d12 | 2010-11-11 03:21:53 +0000 | [diff] [blame] | 6676 | Expr *InitE = Init.get(); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6677 | assert(InitE && "No initialization expression?"); |
| 6678 | |
| 6679 | if (EqualLoc.isInvalid()) |
| 6680 | EqualLoc = InitE->getLocStart(); |
| 6681 | |
| 6682 | InitializationKind Kind = InitializationKind::CreateCopy(InitE->getLocStart(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 6683 | EqualLoc, |
| 6684 | AllowExplicit); |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6685 | InitializationSequence Seq(*this, Entity, Kind, InitE); |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6686 | Init.release(); |
Jeffrey Yasskin | 1915913 | 2011-07-26 23:20:30 +0000 | [diff] [blame] | 6687 | |
Dmitri Gribenko | 1f78a50 | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 6688 | ExprResult Result = Seq.Perform(*this, Entity, Kind, InitE); |
Richard Smith | 4c3fc9b | 2012-01-18 05:21:49 +0000 | [diff] [blame] | 6689 | |
| 6690 | if (!Result.isInvalid() && TopLevelOfInitList) |
| 6691 | DiagnoseNarrowingInInitList(*this, Seq, Entity.getType(), |
| 6692 | InitE, Result.get()); |
| 6693 | |
| 6694 | return Result; |
Douglas Gregor | 18ef5e2 | 2009-12-18 05:02:21 +0000 | [diff] [blame] | 6695 | } |